Skip to content

Commit 27bf6bb

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

File tree

2 files changed

+63
-26
lines changed

2 files changed

+63
-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

+62-24
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,74 @@
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+
});
1620

17-
export interface StxSignMessageRequestParamsStructured extends StxSignMessageRequestParamsBase {
18-
type: 'structured';
19-
domain: string;
20-
message: string;
21-
}
21+
export type StxSignMessageRequestParamsBase = z.infer<typeof stxSignMessageRequestBaseSchema>;
2222

23-
export type StxSignMessageRequestParams =
24-
| StxSignMessageRequestParamsUtf8
25-
| StxSignMessageRequestParamsStructured;
23+
export const stxSignMessageRequestUtf8Schema = stxSignMessageRequestBaseSchema.merge(
24+
z.object({
25+
messageType: z.literal('utf8'),
26+
message: z.string(),
27+
})
28+
);
2629

27-
export interface StxSignMessageResponseBody {
28-
signature: string;
29-
}
30+
export type StxSignMessageRequestParamsUtf8 = z.infer<typeof stxSignMessageRequestUtf8Schema>;
3031

31-
export type StxSignMessageRequest = RpcRequest<'stx_signMessage', StxSignMessageRequestParams>;
32+
export const stxSignMessageRequestStructuredSchema = stxSignMessageRequestBaseSchema.merge(
33+
z.object({
34+
messageType: z.literal('structured'),
35+
domain: z.string(),
36+
message: z.string(),
37+
})
38+
);
3239

33-
export type StxSignMessageResponse = RpcResponse<StxSignMessageResponseBody>;
40+
export type StxSignMessageRequestParamsStructured = z.infer<
41+
typeof stxSignMessageRequestStructuredSchema
42+
>;
43+
44+
export const stxSignMessageRequestParamsSchema = z.union([
45+
stxSignMessageRequestUtf8Schema,
46+
stxSignMessageRequestStructuredSchema,
47+
]);
48+
49+
export type StxSignMessageRequestParams = z.infer<typeof stxSignMessageRequestParamsSchema>;
50+
51+
export const stxSignMessageRequestSchema = createRpcRequestSchema(
52+
stxSignMessageMethodName,
53+
stxSignMessageRequestParamsSchema
54+
);
55+
56+
export type StxSignMessageRequest = z.infer<typeof stxSignMessageRequestSchema>;
57+
58+
// Response
59+
export const stxSignMessageResponseBodySchema = z.object({
60+
signature: z.string(),
61+
publicKey: z.string(),
62+
});
63+
64+
export type StxSignMessageResponseSchema = z.infer<typeof stxSignMessageResponseBodySchema>;
65+
66+
export const stxSignMessageResponseSchema = createRpcResponseSchema(
67+
stxSignMessageResponseBodySchema,
68+
defaultErrorSchema
69+
);
70+
71+
export type StxSignMessageResponse = z.infer<typeof stxSignMessageResponseSchema>;
3472

3573
export type DefineStxSignMessageMethod = DefineRpcMethod<
3674
StxSignMessageRequest,

0 commit comments

Comments
 (0)