Skip to content

Commit

Permalink
Switch to valibot
Browse files Browse the repository at this point in the history
  • Loading branch information
aryzing committed Jun 19, 2024
1 parent 19191f0 commit c89bad6
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 83 deletions.
45 changes: 21 additions & 24 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"lodash.omit": "4.5.0"
},
"peerDependencies": {
"zod": ">=3.23.8"
"valibot": "0.33.2"
},
"devDependencies": {
"@types/jest": "^29.2.6",
Expand All @@ -45,7 +45,7 @@
"ts-jest": "^29.0.5",
"ts-loader": "^9.4.1",
"tsup": "^8.0.2",
"typescript": "^4.9.4",
"typescript": "5.4.5",
"util": "^0.12.4",
"vm-browserify": "^1.1.2",
"webpack": "^5.74.0",
Expand Down
14 changes: 7 additions & 7 deletions src/addresses/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { z } from 'zod';
import * as v from 'valibot';
import type { RequestOptions, RequestPayload } from '../types';

export enum AddressPurpose {
Expand All @@ -21,13 +21,13 @@ export enum AddressType {
stacks = 'stacks',
}

export const addressSchema = z.object({
address: z.string(),
publicKey: z.string(),
purpose: z.nativeEnum(AddressPurpose),
addressType: z.nativeEnum(AddressType),
export const addressSchema = v.object({
address: v.string(),
publicKey: v.string(),
purpose: v.enum(AddressPurpose),
addressType: v.enum(AddressType),
});
export type Address = z.infer<typeof addressSchema>;
export type Address = v.InferOutput<typeof addressSchema>;

export interface GetAddressResponse {
addresses: Address[];
Expand Down
98 changes: 55 additions & 43 deletions src/request/types/btcMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,99 +2,108 @@
* Represents the types and interfaces related to BTC methods.
*/

import { z } from 'zod';
import { AddressPurpose, addressSchema } from '../../addresses';
import { MethodParamsAndResult, rpcRequestMessageSchema } from '../../types';
import * as v from 'valibot';

export const getInfoMethodName = 'getInfo';
export const getInfoParamsSchema = z.undefined();
export const getInfoResultSchema = z.object({
export const getInfoParamsSchema = v.null();
export const getInfoResultSchema = v.object({
/**
* Version of the wallet.
*/
version: z.string(),
version: v.string(),

/**
* [WBIP](https://wbips.netlify.app/wbips/WBIP002) methods supported by the wallet.
*/
methods: z.array(z.string()).optional(),
methods: v.optional(v.array(v.string())),

/**
* List of WBIP standards supported by the wallet. Not currently used.
*/
supports: z.array(z.string()),
supports: v.array(v.string()),
});
export const getInfoSchema = rpcRequestMessageSchema.extend({
method: z.literal(getInfoMethodName),
params: getInfoParamsSchema,
id: z.string(),
export const getInfoSchema = v.object({
...rpcRequestMessageSchema.entries,
...v.object({
method: v.literal(getInfoMethodName),
params: getInfoParamsSchema,
id: v.string(),
}).entries,
});
export type GetInfo = MethodParamsAndResult<
z.infer<typeof getInfoParamsSchema>,
z.infer<typeof getInfoResultSchema>
v.InferOutput<typeof getInfoParamsSchema>,
v.InferOutput<typeof getInfoResultSchema>
>;

export const getAddressesMethodName = 'getAddresses';
export const getAddressesParamsSchema = z.object({
export const getAddressesParamsSchema = v.object({
/**
* The purposes for which to generate addresses. See
* {@linkcode AddressPurpose} for available purposes.
*/
purposes: z.array(z.nativeEnum(AddressPurpose)),
purposes: v.array(v.enum(AddressPurpose)),
/**
* A message to be displayed to the user in the request prompt.
*/
message: z.string().optional(),
message: v.optional(v.string()),
});
export const getAddressesResultSchema = z.object({
export const getAddressesResultSchema = v.object({
/**
* The addresses generated for the given purposes.
*/
addresses: z.array(addressSchema),
addresses: v.array(addressSchema),
});
export const getAddressesRequestMessageSchema = rpcRequestMessageSchema.extend({
method: z.literal(getAddressesMethodName),
params: getAddressesParamsSchema,
id: z.string(),
export const getAddressesRequestMessageSchema = v.object({
...rpcRequestMessageSchema.entries,
...v.object({
method: v.literal(getAddressesMethodName),
params: getAddressesParamsSchema,
id: v.string(),
}).entries,
});
export type GetAddresses = MethodParamsAndResult<
z.infer<typeof getAddressesParamsSchema>,
z.infer<typeof getAddressesResultSchema>
v.InferOutput<typeof getAddressesParamsSchema>,
v.InferOutput<typeof getAddressesResultSchema>
>;

export const signMessageMethodName = 'signMessage';
export const signMessageParamsSchema = z.object({
export const signMessageParamsSchema = v.object({
/**
* The address used for signing.
**/
address: z.string(),
address: v.string(),
/**
* The message to sign.
**/
message: z.string(),
message: v.string(),
});
export const signMessageResultSchema = z.object({
export const signMessageResultSchema = v.object({
/**
* The signature of the message.
*/
signature: z.string(),
signature: v.string(),
/**
* hash of the message.
*/
messageHash: z.string(),
messageHash: v.string(),
/**
* The address used for signing.
*/
address: z.string(),
address: v.string(),
});
export const signMessageRequestMessageSchema = rpcRequestMessageSchema.extend({
method: z.literal(signMessageMethodName),
params: signMessageParamsSchema,
id: z.string(),
export const signMessageRequestMessageSchema = v.object({
...rpcRequestMessageSchema.entries,
...v.object({
method: v.literal(signMessageMethodName),
params: signMessageParamsSchema,
id: v.string(),
}).entries,
});
export type SignMessage = MethodParamsAndResult<
z.infer<typeof signMessageParamsSchema>,
z.infer<typeof signMessageResultSchema>
v.InferOutput<typeof signMessageParamsSchema>,
v.InferOutput<typeof signMessageResultSchema>
>;

type Recipient = {
Expand Down Expand Up @@ -161,13 +170,16 @@ export type SignPsbt = MethodParamsAndResult<SignPsbtParams, SignPsbtResult>;

export const getAccountsMethodName = 'getAccounts';
export const getAccountsParamsSchema = getAddressesParamsSchema;
export const getAccountsResultSchema = z.array(addressSchema);
export const getAccountsRequestMessageSchema = rpcRequestMessageSchema.extend({
method: z.literal(getAccountsMethodName),
params: getAccountsParamsSchema,
id: z.string(),
export const getAccountsResultSchema = v.array(addressSchema);
export const getAccountsRequestMessageSchema = v.object({
...rpcRequestMessageSchema.entries,
...v.object({
method: v.literal(getAccountsMethodName),
params: getAccountsParamsSchema,
id: v.string(),
}).entries,
});
export type GetAccounts = MethodParamsAndResult<
z.infer<typeof getAccountsParamsSchema>,
z.infer<typeof getAccountsResultSchema>
v.InferOutput<typeof getAccountsParamsSchema>,
v.InferOutput<typeof getAccountsResultSchema>
>;
14 changes: 7 additions & 7 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { z } from 'zod';
import * as v from 'valibot';
import type { BitcoinProvider } from './provider';
import { Requests, Return } from './request';

Expand All @@ -25,14 +25,14 @@ export interface RequestOptions<Payload extends RequestPayload, Response> {

// RPC Request and Response types

export const rpcRequestMessageSchema = z.object({
jsonrpc: z.literal('2.0'),
method: z.string(),
params: z.union([z.array(z.unknown()), z.object({}).passthrough()]).optional(),
id: z.union([z.string(), z.number(), z.null()]).optional(),
export const rpcRequestMessageSchema = v.object({
jsonrpc: v.literal('2.0'),
method: v.string(),
params: v.optional(v.union([v.array(v.unknown()), v.looseObject({})])),
id: v.optional(v.union([v.string(), v.number(), v.null()])),
});

export type RpcRequestMessage = z.infer<typeof rpcRequestMessageSchema>;
export type RpcRequestMessage = v.InferOutput<typeof rpcRequestMessageSchema>;

export type RpcId = string | null;

Expand Down

0 comments on commit c89bad6

Please sign in to comment.