Skip to content

Commit

Permalink
feat: udpate types to conform with extension provider
Browse files Browse the repository at this point in the history
  • Loading branch information
camerow committed Dec 16, 2024
1 parent 2ff2258 commit c8f80a8
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 21 deletions.
1 change: 1 addition & 0 deletions packages/rpc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
},
"dependencies": {
"@leather.io/models": "workspace:*",
"@stacks/network": "6.13.0",
"zod": "3.23.8"
},
"devDependencies": {
Expand Down
8 changes: 7 additions & 1 deletion packages/rpc/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { DefineSignMessageMethod } from './methods/sign-message';
import { DefineSignPsbtMethod } from './methods/sign-psbt';
import { DefineStxSignMessageMethod } from './methods/stx-sign-message';
import { DefineStxSignTransactionMethod } from './methods/stx-sign-transaction';
import { DefineSupportedMethods } from './methods/supported-methods';
import { ExtractErrorResponse, ExtractSuccessResponse } from './rpc/schemas';

export * from './rpc/schemas';
Expand All @@ -18,6 +19,10 @@ export * from './methods/get-addresses';
export * from './methods/send-transfer';
export * from './methods/sign-message';
export * from './methods/stx-sign-message';
export * from './methods/stx-sign-transaction';
export * from './methods/supported-methods';
export * from './methods/open';
export * from './methods/open-swap';

export type LeatherRpcMethodMap = DefineGetInfoMethod &
DefineGetAddressesMethod &
Expand All @@ -27,7 +32,8 @@ export type LeatherRpcMethodMap = DefineGetInfoMethod &
DefineStxSignMessageMethod &
DefineStxSignTransactionMethod &
DefineOpenSwapMethod &
DefineOpenMethod;
DefineOpenMethod &
DefineSupportedMethods;

export type RpcRequests = ValueOf<LeatherRpcMethodMap>['request'];

Expand Down
50 changes: 40 additions & 10 deletions packages/rpc/src/methods/send-transfer.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,46 @@
import { DefineRpcMethod, RpcParameterByName, RpcRequest, RpcResponse } from '../rpc/schemas';
import { z } from 'zod';

export interface SendTransferRequestParams extends RpcParameterByName {
account?: number;
address: string;
amount: string;
}
import { DefineRpcMethod, RpcRequest, RpcResponse } from '../rpc/schemas';

export interface SendTransferResponseBody {
txid: string;
}
export const sendTransferRequestParamSchema = z.object({
account: z.number().optional(),
address: z.string(),
amount: z.string(),
});

export type SendTransferRequest = RpcRequest<'sendTransfer', SendTransferRequestParams>;
export const rpcSendTransferParamsLegacySchema = sendTransferRequestParamSchema.extend({
network: z.string(),
});

export type SendTransferRequestParams = z.infer<typeof sendTransferRequestParamSchema>;

export type RpcSendTransferParamsLegacy = z.infer<typeof rpcSendTransferParamsLegacySchema>;

export const transferRecipientParamSchema = z.object({
address: z.string(),
amount: z.string(),
});

export type TransferRecipientParam = z.infer<typeof transferRecipientParamSchema>;

export const rpcSendTransferParamsSchema = z.object({
account: z.number().optional(),
recipients: z.array(transferRecipientParamSchema),
network: z.string(),
});

export type RpcSendTransferParams = z.infer<typeof rpcSendTransferParamsSchema>;

export const sendTransferResponseBodySchema = z.object({
txid: z.string(),
});

export type SendTransferResponseBody = z.infer<typeof sendTransferResponseBodySchema>;

export type SendTransferRequest = RpcRequest<
'sendTransfer',
SendTransferRequestParams | RpcSendTransferParams
>;

export type SendTransferResponse = RpcResponse<SendTransferResponseBody>;

Expand Down
5 changes: 2 additions & 3 deletions packages/rpc/src/methods/sign-psbt.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DefaultNetworkConfigurations } from '@leather.io/models';

import { DefineRpcMethod, RpcRequest, RpcResponse } from '../rpc/schemas';
import { DefineRpcMethod, RpcParameterByName, RpcRequest, RpcResponse } from '../rpc/schemas';

/**
* DEFAULT -- all inputs, all outputs
Expand All @@ -21,14 +21,13 @@ export enum SignatureHash {
SINGLE_ANYONECANPAY = 0x83,
}

export interface SignPsbtRequestParams {
export interface SignPsbtRequestParams extends RpcParameterByName {
account?: number;
allowedSighash?: SignatureHash[];
broadcast: boolean;
hex: string;
network: DefaultNetworkConfigurations;
signAtIndex?: number | number[];
[x: string]: unknown;
}

export interface SignPsbtResponseBody {
Expand Down
7 changes: 3 additions & 4 deletions packages/rpc/src/methods/stx-sign-message.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { DefineRpcMethod, RpcRequest, RpcResponse } from '../rpc/schemas';
import { DefineRpcMethod, RpcParameterByName, RpcRequest, RpcResponse } from '../rpc/schemas';

export const stxMessageSigningTypes = ['utf8', 'structured'] as const;

export type StxSignMessageTypes = (typeof stxMessageSigningTypes)[number];

export interface StxSignMessageRequestParamsBase {
type: StxSignMessageTypes;
export interface StxSignMessageRequestParamsBase extends RpcParameterByName {
messageType: StxSignMessageTypes;
network?: 'mainnet' | 'testnet' | 'devnet' | 'mocknet';
[x: string]: unknown;
}

export interface StxSignMessageRequestParamsUtf8 extends StxSignMessageRequestParamsBase {
Expand Down
6 changes: 4 additions & 2 deletions packages/rpc/src/methods/stx-sign-transaction.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { StacksNetworks } from '@stacks/network';
import { z } from 'zod';

import { DefineRpcMethod, RpcRequest, RpcResponse } from '../rpc/schemas';

export const stxSignTransactionMethodName = 'stx_signTransaction' as const;

const stxSignTransactionRequestParamsSchema = z.object({
txHex: z.string(),
export const stxSignTransactionRequestParamsSchema = z.object({
stxAddress: z.string().optional(),
txHex: z.string(),
attachment: z.string().optional(),
accountIndex: z.string().optional(),
network: z.enum(StacksNetworks).optional(),
});

export type StxSignTransactionRequestParams = z.infer<typeof stxSignTransactionRequestParamsSchema>;
Expand Down
27 changes: 27 additions & 0 deletions packages/rpc/src/methods/supported-methods.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { z } from 'zod';

import { DefineRpcMethod, RpcRequest, RpcSuccessResponse } from '../rpc/schemas';

export const supportedMethodsMethodName = 'supportedMethods' as const;

export type SupportedMethodsRequest = RpcRequest<typeof supportedMethodsMethodName>;

export const supportedMethodSchema = z.object({
name: z.string(),
docsUrl: z.union([z.string(), z.array(z.string())]),
});

export const supportedMethodsResponseSchema = z.object({
documentation: z.string(),
methods: z.array(supportedMethodSchema),
});

type SupportedMethodsResponse = RpcSuccessResponse<{
documentation: string;
methods: z.infer<typeof supportedMethodSchema>[];
}>;

export type DefineSupportedMethods = DefineRpcMethod<
SupportedMethodsRequest,
SupportedMethodsResponse
>;
1 change: 1 addition & 0 deletions packages/rpc/src/rpc/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export function createRpcRequestSchema<TMethod extends z.ZodTypeAny, TParam exte
type RpcRequestSchema<TMethod, TParam extends RpcParameter = RpcParameter> = ReturnType<
typeof createRpcRequestSchema<z.ZodType<TMethod>, z.ZodType<TParam>>
>;

export type RpcRequest<TMethod, TParam extends RpcParameter = RpcParameter> = z.infer<
RpcRequestSchema<TMethod, TParam>
>;
Expand Down
6 changes: 5 additions & 1 deletion pnpm-lock.yaml

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

0 comments on commit c8f80a8

Please sign in to comment.