@walletmesh/aztec-rpc-wallet 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (108) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/LICENSE +201 -0
  3. package/README.md +260 -0
  4. package/dist/.tsbuildinfo +1 -0
  5. package/dist/aztecRemoteWallet.d.ts +73 -0
  6. package/dist/aztecRemoteWallet.d.ts.map +1 -0
  7. package/dist/aztecRemoteWallet.js +354 -0
  8. package/dist/chainProvider.d.ts +56 -0
  9. package/dist/chainProvider.d.ts.map +1 -0
  10. package/dist/chainProvider.js +98 -0
  11. package/dist/contractArtifactCache.d.ts +50 -0
  12. package/dist/contractArtifactCache.d.ts.map +1 -0
  13. package/dist/contractArtifactCache.js +66 -0
  14. package/dist/errors.d.ts +50 -0
  15. package/dist/errors.d.ts.map +1 -0
  16. package/dist/errors.js +62 -0
  17. package/dist/handlers/aztecAccountWallet.d.ts +4 -0
  18. package/dist/handlers/aztecAccountWallet.d.ts.map +1 -0
  19. package/dist/handlers/aztecAccountWallet.js +329 -0
  20. package/dist/handlers/transactions.d.ts +21 -0
  21. package/dist/handlers/transactions.d.ts.map +1 -0
  22. package/dist/handlers/transactions.js +90 -0
  23. package/dist/handlers.d.ts +27 -0
  24. package/dist/handlers.d.ts.map +1 -0
  25. package/dist/handlers.js +55 -0
  26. package/dist/index.d.ts +58 -0
  27. package/dist/index.d.ts.map +1 -0
  28. package/dist/index.js +33 -0
  29. package/dist/provider.d.ts +105 -0
  30. package/dist/provider.d.ts.map +1 -0
  31. package/dist/provider.js +160 -0
  32. package/dist/serializers/account.d.ts +167 -0
  33. package/dist/serializers/account.d.ts.map +1 -0
  34. package/dist/serializers/account.js +245 -0
  35. package/dist/serializers/contract-utils.d.ts +40 -0
  36. package/dist/serializers/contract-utils.d.ts.map +1 -0
  37. package/dist/serializers/contract-utils.js +102 -0
  38. package/dist/serializers/contract.d.ts +168 -0
  39. package/dist/serializers/contract.d.ts.map +1 -0
  40. package/dist/serializers/contract.js +268 -0
  41. package/dist/serializers/core.d.ts +110 -0
  42. package/dist/serializers/core.d.ts.map +1 -0
  43. package/dist/serializers/core.js +130 -0
  44. package/dist/serializers/index.d.ts +28 -0
  45. package/dist/serializers/index.d.ts.map +1 -0
  46. package/dist/serializers/index.js +159 -0
  47. package/dist/serializers/log.d.ts +113 -0
  48. package/dist/serializers/log.d.ts.map +1 -0
  49. package/dist/serializers/log.js +231 -0
  50. package/dist/serializers/note.d.ts +127 -0
  51. package/dist/serializers/note.d.ts.map +1 -0
  52. package/dist/serializers/note.js +182 -0
  53. package/dist/serializers/transaction-utils.d.ts +107 -0
  54. package/dist/serializers/transaction-utils.d.ts.map +1 -0
  55. package/dist/serializers/transaction-utils.js +130 -0
  56. package/dist/serializers/transaction.d.ts +103 -0
  57. package/dist/serializers/transaction.d.ts.map +1 -0
  58. package/dist/serializers/transaction.js +238 -0
  59. package/dist/serializers/types.d.ts +49 -0
  60. package/dist/serializers/types.d.ts.map +1 -0
  61. package/dist/serializers/types.js +22 -0
  62. package/dist/types.d.ts +391 -0
  63. package/dist/types.d.ts.map +1 -0
  64. package/dist/types.js +8 -0
  65. package/dist/wallet.d.ts +62 -0
  66. package/dist/wallet.d.ts.map +1 -0
  67. package/dist/wallet.js +77 -0
  68. package/package.json +44 -0
  69. package/src/aztecRemoteWallet.test.ts +542 -0
  70. package/src/aztecRemoteWallet.ts +484 -0
  71. package/src/chainProvider.test.ts +322 -0
  72. package/src/chainProvider.ts +122 -0
  73. package/src/contractArtifactCache.test.ts +126 -0
  74. package/src/contractArtifactCache.ts +75 -0
  75. package/src/errors.ts +71 -0
  76. package/src/handlers/aztecAccountWallet.test.ts +720 -0
  77. package/src/handlers/aztecAccountWallet.ts +593 -0
  78. package/src/handlers/transactions.ts +110 -0
  79. package/src/handlers.test.ts +270 -0
  80. package/src/handlers.ts +70 -0
  81. package/src/index.test.ts +23 -0
  82. package/src/index.ts +64 -0
  83. package/src/provider.test.ts +276 -0
  84. package/src/provider.ts +189 -0
  85. package/src/serializers/account.test.ts +125 -0
  86. package/src/serializers/account.ts +319 -0
  87. package/src/serializers/contract-utils.ts +104 -0
  88. package/src/serializers/contract.test.ts +162 -0
  89. package/src/serializers/contract.ts +350 -0
  90. package/src/serializers/core.test.ts +56 -0
  91. package/src/serializers/core.ts +141 -0
  92. package/src/serializers/index.test.ts +122 -0
  93. package/src/serializers/index.ts +213 -0
  94. package/src/serializers/log.test.ts +119 -0
  95. package/src/serializers/log.ts +283 -0
  96. package/src/serializers/note.test.ts +100 -0
  97. package/src/serializers/note.ts +227 -0
  98. package/src/serializers/transaction-utils.ts +237 -0
  99. package/src/serializers/transaction.test.ts +153 -0
  100. package/src/serializers/transaction.ts +342 -0
  101. package/src/serializers/types.ts +58 -0
  102. package/src/types.ts +295 -0
  103. package/src/wallet.test.ts +275 -0
  104. package/src/wallet.ts +94 -0
  105. package/tsconfig.build.json +6 -0
  106. package/tsconfig.json +11 -0
  107. package/typedoc.json +15 -0
  108. package/vitest.config.ts +10 -0
@@ -0,0 +1,141 @@
1
+ import { Fr, AztecAddress, CompleteAddress, AuthWitness, TxHash } from '@aztec/aztec.js';
2
+ import { encodeBase64, decodeBase64 } from './types.js';
3
+ import type { TypeSerializer } from './types.js';
4
+
5
+ /**
6
+ * Serializer for Fr (field element) values in the Aztec protocol.
7
+ * Handles conversion of field elements to/from base64-encoded strings for RPC transport.
8
+ * Field elements are fundamental to Aztec's cryptographic operations.
9
+ */
10
+ export class FrSerializer implements TypeSerializer<Fr> {
11
+ /**
12
+ * Converts an Fr value to a base64-encoded string.
13
+ * @param value - The field element to serialize
14
+ * @returns Base64-encoded string representation
15
+ */
16
+ serialize(value: Fr): string {
17
+ return encodeBase64(value.toString());
18
+ }
19
+
20
+ /**
21
+ * Reconstructs an Fr value from a base64-encoded string.
22
+ * @param data - The base64-encoded string to deserialize
23
+ * @returns Reconstructed field element
24
+ * @throws If the input string is not a valid field element representation
25
+ */
26
+ deserialize(data: string): Fr {
27
+ return Fr.fromHexString(decodeBase64(data));
28
+ }
29
+ }
30
+
31
+ /**
32
+ * Serializer for Aztec protocol addresses.
33
+ * Handles conversion of AztecAddress instances to/from base64-encoded strings.
34
+ * Aztec addresses represent accounts and contracts in the protocol.
35
+ */
36
+ export class AztecAddressSerializer implements TypeSerializer<AztecAddress> {
37
+ /**
38
+ * Converts an AztecAddress to a base64-encoded string.
39
+ * @param value - The Aztec address to serialize
40
+ * @returns Base64-encoded string representation
41
+ */
42
+ serialize(value: AztecAddress): string {
43
+ return encodeBase64(value.toString());
44
+ }
45
+
46
+ /**
47
+ * Reconstructs an AztecAddress from a base64-encoded string.
48
+ * @param data - The base64-encoded string to deserialize
49
+ * @returns Reconstructed Aztec address
50
+ * @throws If the input string is not a valid address representation
51
+ */
52
+ deserialize(data: string): AztecAddress {
53
+ return AztecAddress.fromString(decodeBase64(data));
54
+ }
55
+ }
56
+
57
+ /**
58
+ * Serializer for complete Aztec addresses that include additional metadata.
59
+ * CompleteAddress extends AztecAddress with extra information needed for certain operations.
60
+ */
61
+ export class CompleteAddressSerializer implements TypeSerializer<CompleteAddress> {
62
+ /**
63
+ * Converts a CompleteAddress to a base64-encoded string.
64
+ * @param value - The complete address to serialize
65
+ * @returns Base64-encoded string representation
66
+ */
67
+ serialize(value: CompleteAddress): string {
68
+ return encodeBase64(value.toString());
69
+ }
70
+
71
+ /**
72
+ * Reconstructs a CompleteAddress from a base64-encoded string.
73
+ * @param data - The base64-encoded string to deserialize
74
+ * @returns Reconstructed complete address
75
+ * @throws If the input string is not a valid complete address representation
76
+ */
77
+ deserialize(data: string): CompleteAddress {
78
+ return CompleteAddress.fromString(decodeBase64(data));
79
+ }
80
+ }
81
+
82
+ /**
83
+ * Serializer for authentication witnesses in the Aztec protocol.
84
+ * AuthWitness values are used to prove transaction authorization.
85
+ */
86
+ export class AuthWitnessSerializer implements TypeSerializer<AuthWitness> {
87
+ /**
88
+ * Converts an AuthWitness to a base64-encoded string.
89
+ * @param value - The authentication witness to serialize
90
+ * @returns Base64-encoded string representation
91
+ */
92
+ serialize(value: AuthWitness): string {
93
+ return encodeBase64(value.toString());
94
+ }
95
+
96
+ /**
97
+ * Reconstructs an AuthWitness from a base64-encoded string.
98
+ * @param data - The base64-encoded string to deserialize
99
+ * @returns Reconstructed authentication witness
100
+ * @throws If the input string is not a valid auth witness representation
101
+ */
102
+ deserialize(data: string): AuthWitness {
103
+ return AuthWitness.fromString(decodeBase64(data));
104
+ }
105
+ }
106
+
107
+ /**
108
+ * Serializer for transaction hashes in the Aztec protocol.
109
+ * TxHash values uniquely identify transactions and are used for lookups and references.
110
+ */
111
+ export class TxHashSerializer implements TypeSerializer<TxHash> {
112
+ /**
113
+ * Converts a TxHash to a base64-encoded string.
114
+ * @param value - The transaction hash to serialize
115
+ * @returns Base64-encoded string representation
116
+ */
117
+ serialize(value: TxHash): string {
118
+ return encodeBase64(value.toString());
119
+ }
120
+
121
+ /**
122
+ * Reconstructs a TxHash from a base64-encoded string.
123
+ * @param data - The base64-encoded string to deserialize
124
+ * @returns Reconstructed transaction hash
125
+ * @throws If the input string is not a valid transaction hash representation
126
+ */
127
+ deserialize(data: string): TxHash {
128
+ return TxHash.fromString(decodeBase64(data));
129
+ }
130
+ }
131
+
132
+ /**
133
+ * Pre-instantiated serializer instances for common Aztec types.
134
+ * These singletons should be used instead of creating new instances
135
+ * to ensure consistent serialization across the application.
136
+ */
137
+ export const frSerializer = new FrSerializer();
138
+ export const aztecAddressSerializer = new AztecAddressSerializer();
139
+ export const completeAddressSerializer = new CompleteAddressSerializer();
140
+ export const authWitnessSerializer = new AuthWitnessSerializer();
141
+ export const txHashSerializer = new TxHashSerializer();
@@ -0,0 +1,122 @@
1
+ import { describe, expect, it, beforeEach, vi } from 'vitest';
2
+ import { AztecWalletSerializer } from './index.js';
3
+ import { AztecAddress, Tx } from '@aztec/aztec.js';
4
+ import type { JSONRPCSerializer } from './types.js';
5
+
6
+ describe('AztecWalletSerializer', () => {
7
+ // Test known method (using aztec_setScopes as an example)
8
+ const knownMethod = 'aztec_setScopes';
9
+
10
+ // Test unknown method
11
+ const unknownMethod = 'unknown_method';
12
+
13
+ // Type assertion since we verify result exists in beforeEach
14
+ const resultSerializer = (AztecWalletSerializer as Required<JSONRPCSerializer<unknown, unknown>>).result;
15
+
16
+ describe('params', () => {
17
+ it('should serialize params for known methods', () => {
18
+ const params = { scopes: [AztecAddress.random()] };
19
+ const result = AztecWalletSerializer.params.serialize(knownMethod, params);
20
+ expect(result).toBeDefined();
21
+ expect(result.method).toBe(knownMethod);
22
+ expect(typeof result.serialized).toBe('string');
23
+ });
24
+
25
+ it('should wrap unknown method params in JSONRPCSerializedData format', () => {
26
+ const params = { someParam: 'value' };
27
+ const result = AztecWalletSerializer.params.serialize(unknownMethod, params);
28
+ expect(result).toBeDefined();
29
+ expect(result.method).toBe(unknownMethod);
30
+ expect(typeof result.serialized).toBe('string');
31
+ });
32
+
33
+ it('should pass through serialized data for unknown methods', () => {
34
+ const serializedData = { serialized: 'base64data', method: unknownMethod };
35
+ const result = AztecWalletSerializer.params.deserialize(unknownMethod, serializedData);
36
+ expect(result).toEqual(serializedData);
37
+ });
38
+
39
+ it('should throw error when params serializer fails', () => {
40
+ const params = { scopes: undefined }; // Invalid - scopes must be an array
41
+ expect(() => AztecWalletSerializer.params.serialize(knownMethod, params)).toThrow(
42
+ /Failed to serialize params/,
43
+ );
44
+ });
45
+
46
+ it('should throw error when params deserializer fails', () => {
47
+ const serializedData = { serialized: 'invalid-data', method: knownMethod };
48
+ expect(() => AztecWalletSerializer.params.deserialize(knownMethod, serializedData)).toThrow(
49
+ /Failed to deserialize params/,
50
+ );
51
+ });
52
+ });
53
+
54
+ describe('result', () => {
55
+ beforeEach(() => {
56
+ // Ensure result serializer exists
57
+ expect(resultSerializer).toBeDefined();
58
+ });
59
+
60
+ it('should serialize result for known methods', () => {
61
+ const value = true;
62
+ const result = resultSerializer.serialize(knownMethod, value);
63
+ expect(result).toBeDefined();
64
+ expect(result.method).toBe(knownMethod);
65
+ expect(typeof result.serialized).toBe('string');
66
+ });
67
+
68
+ it('should wrap unknown method result in JSONRPCSerializedData format', () => {
69
+ const testResult = { someResult: 'value' };
70
+ const result = resultSerializer.serialize(unknownMethod, testResult);
71
+ expect(result).toBeDefined();
72
+ expect(result.method).toBe(unknownMethod);
73
+ expect(typeof result.serialized).toBe('string');
74
+ });
75
+
76
+ it('should pass through serialized data for unknown methods', () => {
77
+ const serializedData = { serialized: 'base64data', method: unknownMethod };
78
+ const result = resultSerializer.deserialize(unknownMethod, serializedData);
79
+ expect(result).toEqual(serializedData);
80
+ });
81
+
82
+ it('should throw error when result serializer fails', () => {
83
+ interface CircularRef {
84
+ ref: CircularRef | null;
85
+ }
86
+ const circular: CircularRef = { ref: null };
87
+ circular.ref = circular; // Create circular reference which can't be JSON stringified
88
+ expect(() => resultSerializer.serialize(knownMethod, circular)).toThrow(/Failed to serialize result/);
89
+ });
90
+
91
+ it('should throw error when result deserializer fails', () => {
92
+ const serializedData = { serialized: 'invalid-data', method: knownMethod };
93
+ expect(() => resultSerializer.deserialize(knownMethod, serializedData)).toThrow(
94
+ /Failed to deserialize result/,
95
+ );
96
+ });
97
+ });
98
+
99
+ describe('integration', () => {
100
+ it('should handle contract methods', () => {
101
+ const method = 'aztec_getContractInstance';
102
+ const params = { address: AztecAddress.random() };
103
+
104
+ const serializedParams = AztecWalletSerializer.params.serialize(method, params);
105
+ expect(serializedParams).toBeDefined();
106
+ expect(serializedParams.method).toBe(method);
107
+ expect(typeof serializedParams.serialized).toBe('string');
108
+ });
109
+
110
+ it('should handle transaction methods', () => {
111
+ const method = 'aztec_sendTx';
112
+ const params = {
113
+ tx: Tx.random(),
114
+ };
115
+
116
+ const serializedParams = AztecWalletSerializer.params.serialize(method, params);
117
+ expect(serializedParams).toBeDefined();
118
+ expect(serializedParams.method).toBe(method);
119
+ expect(typeof serializedParams.serialized).toBe('string');
120
+ });
121
+ });
122
+ });
@@ -0,0 +1,213 @@
1
+ import type { AztecWalletMethodMap } from '../types.js';
2
+ import type { JSONRPCSerializer, JSONRPCSerializedData } from './types.js';
3
+ import { encodeBase64 } from './types.js';
4
+
5
+ /**
6
+ * Re-export all serializer types and implementations.
7
+ * This provides a single entry point for importing any serializer functionality.
8
+ */
9
+ export * from './types.js';
10
+ export * from './core.js';
11
+ export * from './account.js';
12
+ export * from './contract.js';
13
+ export * from './transaction.js';
14
+ export * from './note.js';
15
+ export * from './log.js';
16
+
17
+ // Import all serializer instances
18
+ import {
19
+ aztecSetScopesSerializer,
20
+ aztecRegisterAccountSerializer,
21
+ aztecAddAuthWitnessSerializer,
22
+ aztecGetAuthWitnessSerializer,
23
+ } from './account.js';
24
+ import {
25
+ aztecGetContractInstanceSerializer,
26
+ aztecGetContractClassSerializer,
27
+ aztecGetContractArtifactSerializer,
28
+ aztecRegisterContractSerializer,
29
+ } from './contract.js';
30
+ import {
31
+ aztecCreateTxExecutionRequestSerializer,
32
+ aztecProveTxSerializer,
33
+ aztecSendTxSerializer,
34
+ aztecGetTxReceiptSerializer,
35
+ } from './transaction.js';
36
+ import {
37
+ aztecGetIncomingNotesSerializer,
38
+ aztecAddNoteSerializer,
39
+ aztecAddNullifiedNoteSerializer,
40
+ } from './note.js';
41
+ import {
42
+ aztecGetUnencryptedLogsSerializer,
43
+ aztecGetEncryptedEventsSerializer,
44
+ aztecGetUnencryptedEventsSerializer,
45
+ } from './log.js';
46
+
47
+ /**
48
+ * Type alias for Aztec RPC method names.
49
+ * Represents all available methods in the Aztec wallet RPC interface.
50
+ */
51
+ type AztecMethodName = keyof AztecWalletMethodMap;
52
+
53
+ /**
54
+ * Type helper for extracting parameter types for a given method.
55
+ * @typeParam T - The method name to get parameters for
56
+ */
57
+ type AztecMethodParams<T extends AztecMethodName> = AztecWalletMethodMap[T]['params'];
58
+
59
+ /**
60
+ * Type helper for extracting result types for a given method.
61
+ * @typeParam T - The method name to get result type for
62
+ */
63
+ type AztecMethodResult<T extends AztecMethodName> = AztecWalletMethodMap[T]['result'];
64
+
65
+ /**
66
+ * Registry of all available method serializers.
67
+ * Maps each RPC method name to its corresponding serializer implementation.
68
+ * This mapping is used by the main AztecWalletSerializer to route method calls
69
+ * to the appropriate specialized serializer.
70
+ */
71
+ const methodSerializers: Record<AztecMethodName, JSONRPCSerializer<unknown, unknown>> = {
72
+ // Account methods
73
+ aztec_setScopes: aztecSetScopesSerializer,
74
+ aztec_registerAccount: aztecRegisterAccountSerializer,
75
+ aztec_addAuthWitness: aztecAddAuthWitnessSerializer,
76
+ aztec_getAuthWitness: aztecGetAuthWitnessSerializer,
77
+
78
+ // Contract methods
79
+ aztec_getContractInstance: aztecGetContractInstanceSerializer,
80
+ aztec_getContractClass: aztecGetContractClassSerializer,
81
+ aztec_getContractArtifact: aztecGetContractArtifactSerializer,
82
+ aztec_registerContract: aztecRegisterContractSerializer,
83
+
84
+ // Transaction methods
85
+ aztec_createTxExecutionRequest: aztecCreateTxExecutionRequestSerializer,
86
+ aztec_proveTx: aztecProveTxSerializer,
87
+ aztec_sendTx: aztecSendTxSerializer,
88
+ aztec_getTxReceipt: aztecGetTxReceiptSerializer,
89
+
90
+ // Note methods
91
+ aztec_getIncomingNotes: aztecGetIncomingNotesSerializer,
92
+ aztec_addNote: aztecAddNoteSerializer,
93
+ aztec_addNullifiedNote: aztecAddNullifiedNoteSerializer,
94
+
95
+ // Log methods
96
+ aztec_getUnencryptedLogs: aztecGetUnencryptedLogsSerializer,
97
+ aztec_getEncryptedEvents: aztecGetEncryptedEventsSerializer,
98
+ aztec_getUnencryptedEvents: aztecGetUnencryptedEventsSerializer,
99
+ };
100
+
101
+ /**
102
+ * Helper function to wrap unknown values in a standard JSON-RPC format.
103
+ * Used as a fallback when no specific serializer is available for a method.
104
+ *
105
+ * @param method - The RPC method name
106
+ * @param value - The value to wrap
107
+ * @returns Standardized JSON-RPC data structure
108
+ */
109
+ function wrapUnknownValue(method: string, value: unknown): JSONRPCSerializedData {
110
+ return {
111
+ method,
112
+ serialized: encodeBase64(JSON.stringify(value)),
113
+ };
114
+ }
115
+
116
+ /**
117
+ * Main serializer for the Aztec wallet RPC interface.
118
+ * Provides a unified interface for serializing all supported RPC methods.
119
+ *
120
+ * This serializer:
121
+ * 1. Routes each method call to its specialized serializer from methodSerializers
122
+ * 2. Provides fallback handling for unknown methods
123
+ * 3. Wraps all serialization operations in proper error handling
124
+ *
125
+ * The serializer handles both:
126
+ * - Parameters: Incoming RPC call parameters
127
+ * - Results: Outgoing RPC call results
128
+ */
129
+ export const AztecWalletSerializer: JSONRPCSerializer<unknown, unknown> = {
130
+ params: {
131
+ /**
132
+ * Serializes RPC method parameters using the appropriate method serializer.
133
+ * @param method - The RPC method name
134
+ * @param value - The parameters to serialize
135
+ * @returns Serialized parameter data
136
+ * @throws If serialization fails or encounters an error
137
+ */
138
+ serialize: (method: string, value: unknown): JSONRPCSerializedData => {
139
+ const serializer = methodSerializers[method as AztecMethodName];
140
+ if (!serializer?.params) {
141
+ return wrapUnknownValue(method, value);
142
+ }
143
+
144
+ try {
145
+ return serializer.params.serialize(method, value);
146
+ } catch (error) {
147
+ throw new Error(`Failed to serialize params for method ${method}: ${error}`);
148
+ }
149
+ },
150
+ /**
151
+ * Deserializes RPC method parameters using the appropriate method serializer.
152
+ * @param method - The RPC method name
153
+ * @param data - The serialized parameter data
154
+ * @returns Deserialized parameters
155
+ * @throws If deserialization fails or encounters an error
156
+ */
157
+ deserialize: (method: string, data: JSONRPCSerializedData): unknown => {
158
+ const serializer = methodSerializers[method as AztecMethodName];
159
+ if (!serializer?.params) {
160
+ return data;
161
+ }
162
+
163
+ try {
164
+ return serializer.params.deserialize(method, data);
165
+ } catch (error) {
166
+ throw new Error(`Failed to deserialize params for method ${method}: ${error}`);
167
+ }
168
+ },
169
+ },
170
+ result: {
171
+ /**
172
+ * Serializes RPC method results using the appropriate method serializer.
173
+ * @param method - The RPC method name
174
+ * @param value - The result to serialize
175
+ * @returns Serialized result data
176
+ * @throws If serialization fails or encounters an error
177
+ */
178
+ serialize: (method: string, value: unknown): JSONRPCSerializedData => {
179
+ const serializer = methodSerializers[method as AztecMethodName];
180
+ if (!serializer?.result) {
181
+ return wrapUnknownValue(method, value);
182
+ }
183
+
184
+ try {
185
+ return serializer.result.serialize(method, value);
186
+ } catch (error) {
187
+ throw new Error(`Failed to serialize result for method ${method}: ${error}`);
188
+ }
189
+ },
190
+ /**
191
+ * Deserializes RPC method results using the appropriate method serializer.
192
+ * @param method - The RPC method name
193
+ * @param data - The serialized result data
194
+ * @returns Deserialized result
195
+ * @throws If deserialization fails or encounters an error
196
+ */
197
+ deserialize: (method: string, data: JSONRPCSerializedData): unknown => {
198
+ const serializer = methodSerializers[method as AztecMethodName];
199
+ if (!serializer?.result) {
200
+ return data;
201
+ }
202
+
203
+ try {
204
+ return serializer.result.deserialize(method, data);
205
+ } catch (error) {
206
+ throw new Error(`Failed to deserialize result for method ${method}: ${error}`);
207
+ }
208
+ },
209
+ },
210
+ };
211
+
212
+ // Export the main serializer interface
213
+ export type { JSONRPCSerializer } from './types.js';
@@ -0,0 +1,119 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { Point, AztecAddress } from '@aztec/aztec.js';
3
+ import { EventSelector } from '@aztec/foundation/abi';
4
+ import type { AbiType } from '@aztec/foundation/abi';
5
+ import {
6
+ ExtendedUnencryptedL2Log,
7
+ type GetUnencryptedLogsResponse,
8
+ type LogFilter,
9
+ LogId,
10
+ TxHash,
11
+ } from '@aztec/circuit-types';
12
+ import {
13
+ aztecGetUnencryptedLogsSerializer,
14
+ aztecGetEncryptedEventsSerializer,
15
+ aztecGetUnencryptedEventsSerializer,
16
+ } from './log.js';
17
+
18
+ describe('Log Serializers', () => {
19
+ describe('aztec_getUnencryptedLogs', () => {
20
+ const METHOD = 'aztec_getUnencryptedLogs';
21
+
22
+ it('should serialize and deserialize params', () => {
23
+ const filter: LogFilter = {
24
+ txHash: TxHash.random(),
25
+ fromBlock: 0,
26
+ toBlock: 100,
27
+ afterLog: LogId.random(),
28
+ contractAddress: AztecAddress.random(),
29
+ };
30
+
31
+ const params = { filter };
32
+
33
+ const serialized = aztecGetUnencryptedLogsSerializer.params.serialize(METHOD, params);
34
+ expect(serialized.method).toBe(METHOD);
35
+
36
+ const deserialized = aztecGetUnencryptedLogsSerializer.params.deserialize(METHOD, serialized);
37
+ expect(deserialized.filter.contractAddress?.toString()).toBe(filter.contractAddress?.toString());
38
+ expect(deserialized.filter.txHash?.toString()).toBe(filter.txHash?.toString());
39
+ expect(deserialized.filter.fromBlock).toBe(filter.fromBlock);
40
+ expect(deserialized.filter.toBlock).toBe(filter.toBlock);
41
+ expect(deserialized.filter.afterLog?.toString()).toBe(filter.afterLog?.toString());
42
+ });
43
+
44
+ it('should serialize and deserialize result', () => {
45
+ const result: GetUnencryptedLogsResponse = {
46
+ logs: [ExtendedUnencryptedL2Log.random()],
47
+ maxLogsHit: false,
48
+ };
49
+
50
+ const serialized = aztecGetUnencryptedLogsSerializer.result.serialize(METHOD, result);
51
+ expect(serialized.method).toBe(METHOD);
52
+
53
+ const deserialized = aztecGetUnencryptedLogsSerializer.result.deserialize(METHOD, serialized);
54
+ expect(deserialized.logs[0].toString()).toBe(result.logs[0].toString());
55
+ expect(deserialized.logs.length).toBe(result.logs.length);
56
+ expect(deserialized.maxLogsHit).toBe(result.maxLogsHit);
57
+ });
58
+ });
59
+
60
+ describe('aztec_getEncryptedEvents', () => {
61
+ const METHOD = 'aztec_getEncryptedEvents';
62
+
63
+ it('should serialize and deserialize params', () => {
64
+ const event = {
65
+ eventSelector: EventSelector.fromString('0x12345678'),
66
+ abiType: {
67
+ kind: 'field',
68
+ } as AbiType,
69
+ fieldNames: ['field1', 'field2'],
70
+ };
71
+ const params = {
72
+ event,
73
+ from: 0,
74
+ limit: 10,
75
+ vpks: [Point.random(), Point.random()],
76
+ };
77
+
78
+ const serialized = aztecGetEncryptedEventsSerializer.params.serialize(METHOD, params);
79
+ expect(serialized.method).toBe(METHOD);
80
+
81
+ const deserialized = aztecGetEncryptedEventsSerializer.params.deserialize(METHOD, serialized);
82
+ expect(deserialized.event.eventSelector.toString()).toBe(event.eventSelector.toString());
83
+ expect(deserialized.event.abiType).toEqual(event.abiType);
84
+ expect(deserialized.event.fieldNames).toEqual(event.fieldNames);
85
+ expect(deserialized.from).toBe(params.from);
86
+ expect(deserialized.limit).toBe(params.limit);
87
+ expect(deserialized.vpks?.map((p) => p.toString())).toEqual(params.vpks.map((p) => p.toString()));
88
+ });
89
+ });
90
+
91
+ describe('aztec_getUnencryptedEvents', () => {
92
+ const METHOD = 'aztec_getUnencryptedEvents';
93
+
94
+ it('should serialize and deserialize params', () => {
95
+ const event = {
96
+ eventSelector: EventSelector.fromString('0x12345678'),
97
+ abiType: {
98
+ kind: 'field',
99
+ } as AbiType,
100
+ fieldNames: ['field1', 'field2'],
101
+ };
102
+ const params = {
103
+ event,
104
+ from: 0,
105
+ limit: 10,
106
+ };
107
+
108
+ const serialized = aztecGetUnencryptedEventsSerializer.params.serialize(METHOD, params);
109
+ expect(serialized.method).toBe(METHOD);
110
+
111
+ const deserialized = aztecGetUnencryptedEventsSerializer.params.deserialize(METHOD, serialized);
112
+ expect(deserialized.event.eventSelector.toString()).toBe(event.eventSelector.toString());
113
+ expect(deserialized.event.abiType).toEqual(event.abiType);
114
+ expect(deserialized.event.fieldNames).toEqual(event.fieldNames);
115
+ expect(deserialized.from).toBe(params.from);
116
+ expect(deserialized.limit).toBe(params.limit);
117
+ });
118
+ });
119
+ });