|
1 |
| -import { DefineRpcMethod, RpcParameterByName, RpcRequest, RpcResponse } from '../../rpc/schemas'; |
| 1 | +import { z } from 'zod'; |
2 | 2 |
|
3 |
| -export const stxMessageSigningTypes = ['utf8', 'structured'] as const; |
| 3 | +import { |
| 4 | + DefineRpcMethod, |
| 5 | + createRpcRequestSchema, |
| 6 | + createRpcResponseSchema, |
| 7 | + defaultErrorSchema, |
| 8 | +} from '../../rpc/schemas'; |
4 | 9 |
|
5 |
| -export type StxSignMessageTypes = (typeof stxMessageSigningTypes)[number]; |
| 10 | +export const stxSignMessageMethodName = 'stx_signMessage'; |
6 | 11 |
|
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>; |
11 | 15 |
|
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 | +}); |
16 | 20 |
|
17 |
| -export interface StxSignMessageRequestParamsStructured extends StxSignMessageRequestParamsBase { |
18 |
| - type: 'structured'; |
19 |
| - domain: string; |
20 |
| - message: string; |
21 |
| -} |
| 21 | +export type StxSignMessageRequestParamsBase = z.infer<typeof stxSignMessageRequestBaseSchema>; |
22 | 22 |
|
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 | +); |
26 | 29 |
|
27 |
| -export interface StxSignMessageResponseBody { |
28 |
| - signature: string; |
29 |
| -} |
| 30 | +export type StxSignMessageRequestParamsUtf8 = z.infer<typeof stxSignMessageRequestUtf8Schema>; |
30 | 31 |
|
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 | +); |
32 | 39 |
|
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>; |
34 | 72 |
|
35 | 73 | export type DefineStxSignMessageMethod = DefineRpcMethod<
|
36 | 74 | StxSignMessageRequest,
|
|
0 commit comments