-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(rpc): add stx_signTransaction, test schemas, closes leather-…
- Loading branch information
1 parent
ac5231b
commit 314a880
Showing
15 changed files
with
241 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
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(), | ||
stxAddress: z.string().optional(), | ||
attachment: z.string().optional(), | ||
}); | ||
|
||
export type StxSignTransactionRequestParams = z.infer<typeof stxSignTransactionRequestParamsSchema>; | ||
|
||
const stxSignTransactionResponseSchema = z.object({ txHex: z.string() }); | ||
|
||
export type StxSignTransactionResponseBody = z.infer<typeof stxSignTransactionResponseSchema>; | ||
|
||
export type StxSignTransactionRequest = RpcRequest< | ||
typeof stxSignTransactionMethodName, | ||
StxSignTransactionRequestParams | ||
>; | ||
|
||
export type StxSignTransactionResponse = RpcResponse<StxSignTransactionResponseBody>; | ||
|
||
export type DefineStxSignTransactionMethod = DefineRpcMethod< | ||
StxSignTransactionRequest, | ||
StxSignTransactionResponse | ||
>; |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { z } from 'zod'; | ||
|
||
import { | ||
createRpcErrorResponseSchema, | ||
createRpcSuccessResponseSchema, | ||
rpcBasePropsSchema, | ||
} from './schemas'; | ||
|
||
const baseRequestData = { jsonrpc: '2.0', id: '1' }; | ||
|
||
describe('RPC method schemas', () => { | ||
test('the base RPC schema', () => { | ||
expect(rpcBasePropsSchema.safeParse(baseRequestData).success).toEqual(true); | ||
}); | ||
|
||
describe(createRpcSuccessResponseSchema.name, () => { | ||
test('validates a successful response with basic result object', () => { | ||
const schema = createRpcSuccessResponseSchema(z.object({ value: z.string() })); | ||
const validResponse = { ...baseRequestData, result: { value: 'test' } }; | ||
|
||
expect(schema.safeParse(validResponse).success).toBe(true); | ||
}); | ||
|
||
test('fails validation when missing required RPC base properties', () => { | ||
const schema = createRpcSuccessResponseSchema(z.object({ value: z.string() })); | ||
const invalidResponse = { result: { value: 'test' } }; | ||
|
||
expect(schema.safeParse(invalidResponse).success).toBe(false); | ||
}); | ||
|
||
test('fails validation when result does not match schema', () => { | ||
const schema = createRpcSuccessResponseSchema(z.object({ value: z.number() })); | ||
const invalidResponse = { ...baseRequestData, result: { value: 'not a number' } }; | ||
|
||
expect(schema.safeParse(invalidResponse).success).toBe(false); | ||
}); | ||
}); | ||
|
||
describe(createRpcErrorResponseSchema.name, () => { | ||
const rpcErrorSchema = z.object({ | ||
code: z.number(), | ||
message: z.string(), | ||
data: z.object({}).optional(), | ||
}); | ||
|
||
test('validates error response with basic error object', () => { | ||
const schema = createRpcErrorResponseSchema(rpcErrorSchema); | ||
const validResponse = { | ||
...baseRequestData, | ||
error: { | ||
code: -32600, | ||
message: 'Invalid Request', | ||
}, | ||
}; | ||
|
||
expect(schema.safeParse(validResponse).success).toBe(true); | ||
}); | ||
|
||
test('validates error response with optional error data', () => { | ||
const schema = createRpcErrorResponseSchema(rpcErrorSchema); | ||
const validResponse = { | ||
...baseRequestData, | ||
error: { | ||
code: -32603, | ||
message: 'Internal error', | ||
data: { details: 'Database connection failed' }, | ||
}, | ||
}; | ||
|
||
expect(schema.safeParse(validResponse).success).toBe(true); | ||
}); | ||
|
||
test('fails validation when missing required RPC properties', () => { | ||
const schema = createRpcErrorResponseSchema(rpcErrorSchema); | ||
const invalidResponse = { | ||
error: { | ||
code: -32600, | ||
message: 'Invalid Request', | ||
}, | ||
}; | ||
expect(schema.safeParse(invalidResponse).success).toBe(false); | ||
}); | ||
|
||
test('fails validation with invalid error code', () => { | ||
const schema = createRpcErrorResponseSchema(rpcErrorSchema); | ||
const invalidResponse = { | ||
...baseRequestData, | ||
error: { | ||
code: 'not-a-number', | ||
message: 'Invalid Request', | ||
}, | ||
}; | ||
|
||
expect(schema.safeParse(invalidResponse).success).toBe(false); | ||
}); | ||
|
||
test('fails validation when missing error message', () => { | ||
const schema = createRpcErrorResponseSchema(rpcErrorSchema); | ||
const invalidResponse = { | ||
...baseRequestData, | ||
error: { code: -32600 }, | ||
}; | ||
|
||
expect(schema.safeParse(invalidResponse).success).toBe(false); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.