Skip to content

Commit

Permalink
Revert "Add message schema validators"
Browse files Browse the repository at this point in the history
  • Loading branch information
teebszet authored Jul 1, 2024
1 parent d17e8b2 commit 84a0f4b
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 193 deletions.
31 changes: 8 additions & 23 deletions package-lock.json

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

5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@
"jsontokens": "4.0.1",
"lodash.omit": "4.5.0"
},
"peerDependencies": {
"valibot": "0.33.2"
},
"devDependencies": {
"@types/jest": "^29.2.6",
"@types/lodash.omit": "4.5.9",
Expand All @@ -45,7 +42,7 @@
"ts-jest": "^29.0.5",
"ts-loader": "^9.4.1",
"tsup": "^8.0.2",
"typescript": "5.4.5",
"typescript": "^4.9.4",
"util": "^0.12.4",
"vm-browserify": "^1.1.2",
"webpack": "^5.74.0",
Expand Down
14 changes: 6 additions & 8 deletions src/addresses/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as v from 'valibot';
import type { RequestOptions, RequestPayload } from '../types';

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

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

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

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

export const getInfoMethodName = 'getInfo';
export const getInfoParamsSchema = v.null();
export const getInfoResultSchema = v.object({
/**
* Version of the wallet.
*/
version: v.string(),
type GetInfoResult = {
version: number | string;
methods?: Array<string>;
supports?: Array<string>;
};

/**
* [WBIP](https://wbips.netlify.app/wbips/WBIP002) methods supported by the wallet.
*/
methods: v.optional(v.array(v.string())),
export type GetInfo = MethodParamsAndResult<null, GetInfoResult>;

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

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

export const signMessageMethodName = 'signMessage';
export const signMessageParamsSchema = v.object({
message?: string;
};

/**
* The addresses generated for the given purposes.
*/
type GetAddressesResult = {
addresses: Array<Address>;
};

export type GetAddresses = MethodParamsAndResult<GetAddressesParams, GetAddressesResult>;

export type SignMessageParams = {
/**
* The address used for signing.
**/
address: v.string(),
address: string;
/**
* The message to sign.
**/
message: v.string(),
});
export const signMessageResultSchema = v.object({
message: string;
};

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

export type SignMessage = MethodParamsAndResult<SignMessageParams, SignMessageResult>;

type Recipient = {
/**
Expand Down Expand Up @@ -168,18 +124,21 @@ export type SignPsbtResult = {

export type SignPsbt = MethodParamsAndResult<SignPsbtParams, SignPsbtResult>;

export const getAccountsMethodName = 'getAccounts';
export const getAccountsParamsSchema = getAddressesParamsSchema;
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<
v.InferOutput<typeof getAccountsParamsSchema>,
v.InferOutput<typeof getAccountsResultSchema>
>;
export type GetAccountsParams = {
/**
* The purposes for which to generate addresses.
* possible values are "payment", "ordinals", ...
*/
purposes: Array<AddressPurpose>;
/**
* a message to be displayed to the user in the request prompt.
*/
/**
* a message to be displayed to the user in the request prompt.
*/
message?: string;
};

export type GetAccountResult = Address[];

export type GetAccounts = MethodParamsAndResult<GetAccountsParams, GetAccountResult>;
9 changes: 1 addition & 8 deletions src/request/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import {
StxSignTransaction,
StxTransferStx,
} from './stxMethods';
import { Connect, Disconnect } from './walletMethods';

export interface StxRequests {
stx_callContract: StxCallContract;
Expand Down Expand Up @@ -65,16 +64,10 @@ export interface RunesRequests {

export type RunesRequestMethod = keyof RunesRequests;

export interface WalletMethods {
wallet_connect: Connect;
wallet_disconnect: Disconnect;
}

export type Requests = BtcRequests & StxRequests & RunesRequests & WalletMethods;
export type Requests = BtcRequests & StxRequests & RunesRequests;

export type Return<Method> = Method extends keyof Requests ? Requests[Method]['result'] : never;
export type Params<Method> = Method extends keyof Requests ? Requests[Method]['params'] : never;

export * from './stxMethods';
export * from './btcMethods';
export * from './walletMethods';
34 changes: 0 additions & 34 deletions src/request/types/walletMethods.ts

This file was deleted.

20 changes: 0 additions & 20 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as v from 'valibot';
import type { BitcoinProvider } from './provider';
import { Requests, Return } from './request';

Expand Down Expand Up @@ -26,25 +25,6 @@ export interface RequestOptions<Payload extends RequestPayload, Response> {

// RPC Request and Response types

export const rpcRequestMessageSchema = v.object({
jsonrpc: v.literal('2.0'),
method: v.string(),
params: v.optional(
v.union([
v.array(v.unknown()),
v.looseObject({}),
// Note: This is to support current incorrect usage of RPC 2.0. Params need
// to be either an array or an object when provided. Changing this now would
// be a breaking change, so accepting null values for now. Tracking in
// https://linear.app/xverseapp/issue/ENG-4538.
v.null(),
])
),
id: v.optional(v.union([v.string(), v.number(), v.null()])),
});

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

export type RpcId = string | null;

export interface RpcBase {
Expand Down

0 comments on commit 84a0f4b

Please sign in to comment.