From c89bad69f78fedcb54b60b56f863476d1501327f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduard=20Bardaj=C3=AD=20Puig?= Date: Wed, 19 Jun 2024 14:33:06 +0100 Subject: [PATCH] Switch to valibot --- package-lock.json | 45 +++++++-------- package.json | 4 +- src/addresses/types.ts | 14 ++--- src/request/types/btcMethods.ts | 98 ++++++++++++++++++--------------- src/types.ts | 14 ++--- 5 files changed, 92 insertions(+), 83 deletions(-) diff --git a/package-lock.json b/package-lock.json index a281834..de5ec40 100644 --- a/package-lock.json +++ b/package-lock.json @@ -27,14 +27,14 @@ "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", "webpack-cli": "^4.10.0" }, "peerDependencies": { - "zod": ">=3.23.8" + "valibot": "0.33.2" } }, "node_modules/@ampproject/remapping": { @@ -6797,16 +6797,16 @@ } }, "node_modules/typescript": { - "version": "4.9.5", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.9.5.tgz", - "integrity": "sha512-1FXk9E2Hm+QzZQ7z+McJiHL4NW1F2EzMu9Nq9i3zAaGqibafqYwCVU6WyWAuyQRRzOlxou8xZSyXLEN8oKj24g==", + "version": "5.4.5", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.4.5.tgz", + "integrity": "sha512-vcI4UpRgg81oIRUFwR0WSIHKt11nJ7SAVlYNIu+QpqeyXP+gpQJy/Z4+F0aGxSE4MqwjyXvW/TzgkLAx2AGHwQ==", "dev": true, "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" }, "engines": { - "node": ">=4.2.0" + "node": ">=14.17" } }, "node_modules/update-browserslist-db": { @@ -6885,6 +6885,12 @@ "dev": true, "peer": true }, + "node_modules/valibot": { + "version": "0.33.2", + "resolved": "https://registry.npmjs.org/valibot/-/valibot-0.33.2.tgz", + "integrity": "sha512-ZpFWuI+bs5+PP66q4zVFn4e4t/s5jmMw5iPBZmGUoi8iQqXyU9YY/BLCAyk62Z/bNS8qdUNBEyx52952qdqW3w==", + "peer": true + }, "node_modules/vm-browserify": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/vm-browserify/-/vm-browserify-1.1.2.tgz", @@ -7219,15 +7225,6 @@ "funding": { "url": "https://github.com/sponsors/sindresorhus" } - }, - "node_modules/zod": { - "version": "3.23.8", - "resolved": "https://registry.npmjs.org/zod/-/zod-3.23.8.tgz", - "integrity": "sha512-XBx9AXhXktjUqnepgTiE5flcKIYWi/rme0Eaj+5Y0lftuGBq+jyRu/md4WnuxqgP1ubdpNCsYEYPxrzVHD8d6g==", - "peer": true, - "funding": { - "url": "https://github.com/sponsors/colinhacks" - } } }, "dependencies": { @@ -12052,9 +12049,9 @@ "peer": true }, "typescript": { - "version": "4.9.5", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.9.5.tgz", - "integrity": "sha512-1FXk9E2Hm+QzZQ7z+McJiHL4NW1F2EzMu9Nq9i3zAaGqibafqYwCVU6WyWAuyQRRzOlxou8xZSyXLEN8oKj24g==", + "version": "5.4.5", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.4.5.tgz", + "integrity": "sha512-vcI4UpRgg81oIRUFwR0WSIHKt11nJ7SAVlYNIu+QpqeyXP+gpQJy/Z4+F0aGxSE4MqwjyXvW/TzgkLAx2AGHwQ==", "dev": true }, "update-browserslist-db": { @@ -12116,6 +12113,12 @@ } } }, + "valibot": { + "version": "0.33.2", + "resolved": "https://registry.npmjs.org/valibot/-/valibot-0.33.2.tgz", + "integrity": "sha512-ZpFWuI+bs5+PP66q4zVFn4e4t/s5jmMw5iPBZmGUoi8iQqXyU9YY/BLCAyk62Z/bNS8qdUNBEyx52952qdqW3w==", + "peer": true + }, "vm-browserify": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/vm-browserify/-/vm-browserify-1.1.2.tgz", @@ -12352,12 +12355,6 @@ "integrity": "sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==", "dev": true, "peer": true - }, - "zod": { - "version": "3.23.8", - "resolved": "https://registry.npmjs.org/zod/-/zod-3.23.8.tgz", - "integrity": "sha512-XBx9AXhXktjUqnepgTiE5flcKIYWi/rme0Eaj+5Y0lftuGBq+jyRu/md4WnuxqgP1ubdpNCsYEYPxrzVHD8d6g==", - "peer": true } } } diff --git a/package.json b/package.json index e47aee3..a536c87 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ "lodash.omit": "4.5.0" }, "peerDependencies": { - "zod": ">=3.23.8" + "valibot": "0.33.2" }, "devDependencies": { "@types/jest": "^29.2.6", @@ -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", diff --git a/src/addresses/types.ts b/src/addresses/types.ts index fd313e9..7501e01 100644 --- a/src/addresses/types.ts +++ b/src/addresses/types.ts @@ -1,4 +1,4 @@ -import { z } from 'zod'; +import * as v from 'valibot'; import type { RequestOptions, RequestPayload } from '../types'; export enum AddressPurpose { @@ -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; +export type Address = v.InferOutput; export interface GetAddressResponse { addresses: Address[]; diff --git a/src/request/types/btcMethods.ts b/src/request/types/btcMethods.ts index 9507a74..58c0811 100644 --- a/src/request/types/btcMethods.ts +++ b/src/request/types/btcMethods.ts @@ -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, - z.infer + v.InferOutput, + v.InferOutput >; 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, - z.infer + v.InferOutput, + v.InferOutput >; 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, - z.infer + v.InferOutput, + v.InferOutput >; type Recipient = { @@ -161,13 +170,16 @@ export type SignPsbt = MethodParamsAndResult; 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, - z.infer + v.InferOutput, + v.InferOutput >; diff --git a/src/types.ts b/src/types.ts index b171614..4d04b64 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,4 +1,4 @@ -import { z } from 'zod'; +import * as v from 'valibot'; import type { BitcoinProvider } from './provider'; import { Requests, Return } from './request'; @@ -25,14 +25,14 @@ export interface RequestOptions { // 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; +export type RpcRequestMessage = v.InferOutput; export type RpcId = string | null;