Skip to content

Commit 3f08229

Browse files
committed
fix(rpc): refactor msg signing types, add publicKey param in response
1 parent b2084b7 commit 3f08229

File tree

2 files changed

+57
-26
lines changed

2 files changed

+57
-26
lines changed

packages/rpc/src/methods/stacks/stx-get-addresses.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { z } from 'zod';
22

33
import {
44
DefineRpcMethod,
5-
RpcResponse,
65
createRpcRequestSchema,
76
createRpcResponseSchema,
87
defaultErrorSchema,
@@ -31,7 +30,7 @@ export const stxGetAddressesResponseSchema = createRpcResponseSchema(
3130
defaultErrorSchema
3231
);
3332

34-
export type StxGetAddressesResponse = RpcResponse<typeof stxGetAddressesResponseBodySchema>;
33+
export type StxGetAddressesResponse = z.infer<typeof stxGetAddressesResponseSchema>;
3534

3635
export type DefineStxGetAddressesMethod = DefineRpcMethod<
3736
StxGetAddressesRequest,

packages/rpc/src/methods/stacks/stx-sign-message.ts

+56-24
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,68 @@
1-
import { DefineRpcMethod, RpcParameterByName, RpcRequest, RpcResponse } from '../../rpc/schemas';
1+
import { z } from 'zod';
22

3-
export const stxMessageSigningTypes = ['utf8', 'structured'] as const;
3+
import {
4+
DefineRpcMethod,
5+
createRpcRequestSchema,
6+
createRpcResponseSchema,
7+
defaultErrorSchema,
8+
} from '../../rpc/schemas';
49

5-
export type StxSignMessageTypes = (typeof stxMessageSigningTypes)[number];
10+
export const stxSignMessageMethodName = 'stx_signMessage';
611

7-
export interface StxSignMessageRequestParamsBase extends RpcParameterByName {
8-
messageType: StxSignMessageTypes;
9-
network?: 'mainnet' | 'testnet' | 'devnet' | 'mocknet';
10-
}
12+
// Request
13+
export const stxSignMessageTypeSchema = z.enum(['utf8', 'structured']);
14+
export type StxSignMessageTypes = z.infer<typeof stxSignMessageTypeSchema>;
1115

12-
export interface StxSignMessageRequestParamsUtf8 extends StxSignMessageRequestParamsBase {
13-
type: 'utf8';
14-
message: string;
15-
}
16+
export const stxSignMessageRequestBaseSchema = z.object({
17+
messageType: stxSignMessageTypeSchema,
18+
network: z.optional(z.enum(['mainnet', 'testnet', 'devnet', 'mocknet'])),
19+
});
20+
export type StxSignMessageRequestParamsBase = z.infer<typeof stxSignMessageRequestBaseSchema>;
1621

17-
export interface StxSignMessageRequestParamsStructured extends StxSignMessageRequestParamsBase {
18-
type: 'structured';
19-
domain: string;
20-
message: string;
21-
}
22+
export const stxSignMessageRequestUtf8Schema = stxSignMessageRequestBaseSchema.merge(
23+
z.object({
24+
messageType: z.literal('utf8'),
25+
message: z.string(),
26+
})
27+
);
28+
export type StxSignMessageRequestParamsUtf8 = z.infer<typeof stxSignMessageRequestUtf8Schema>;
2229

23-
export type StxSignMessageRequestParams =
24-
| StxSignMessageRequestParamsUtf8
25-
| StxSignMessageRequestParamsStructured;
30+
export const stxSignMessageRequestStructuredSchema = stxSignMessageRequestBaseSchema.merge(
31+
z.object({
32+
messageType: z.literal('structured'),
33+
domain: z.string(),
34+
message: z.string(),
35+
})
36+
);
37+
export type StxSignMessageRequestParamsStructured = z.infer<
38+
typeof stxSignMessageRequestStructuredSchema
39+
>;
40+
41+
export const stxSignMessageRequestParamsSchema = z.union([
42+
stxSignMessageRequestUtf8Schema,
43+
stxSignMessageRequestStructuredSchema,
44+
]);
45+
export type StxSignMessageRequestParams = z.infer<typeof stxSignMessageRequestParamsSchema>;
46+
47+
export const stxSignMessageRequestSchema = createRpcRequestSchema(
48+
stxSignMessageMethodName,
49+
stxSignMessageRequestParamsSchema
50+
);
51+
export type StxSignMessageRequest = z.infer<typeof stxSignMessageRequestSchema>;
2652

27-
export interface StxSignMessageResponseBody {
28-
signature: string;
29-
}
53+
// Response
54+
export const stxSignMessageResponseBodySchema = z.object({
55+
signature: z.string(),
56+
publicKey: z.string(),
57+
});
58+
export type StxSignMessageResponseBodySchema = z.infer<typeof stxSignMessageResponseBodySchema>;
3059

31-
export type StxSignMessageRequest = RpcRequest<'stx_signMessage', StxSignMessageRequestParams>;
60+
export const stxSignMessageResponseSchema = createRpcResponseSchema(
61+
stxSignMessageResponseBodySchema,
62+
defaultErrorSchema
63+
);
3264

33-
export type StxSignMessageResponse = RpcResponse<StxSignMessageResponseBody>;
65+
export type StxSignMessageResponse = z.infer<typeof stxSignMessageResponseSchema>;
3466

3567
export type DefineStxSignMessageMethod = DefineRpcMethod<
3668
StxSignMessageRequest,

0 commit comments

Comments
 (0)