@walletmesh/aztec-rpc-wallet 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
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,104 @@
1
+ import type { ContractClassWithId, PrivateFunction, PublicFunction } from '@aztec/circuits.js';
2
+ import { type ContractArtifact, type FunctionArtifact, FunctionSelector } from '@aztec/foundation/abi';
3
+ import { Fr } from '@aztec/aztec.js';
4
+
5
+ /**
6
+ * Serializes a ContractClassWithId instance to a JSON string.
7
+ * Handles the conversion of complex Aztec types (Fr, Buffer, FunctionSelector) to their string representations.
8
+ *
9
+ * The serialization process handles the following contract components:
10
+ * - version: Contract class version number
11
+ * - artifactHash: Fr value representing the hash of the contract artifact
12
+ * - privateFunctions: Array of private function definitions with selectors and VK hashes
13
+ * - publicFunctions: Array of public function definitions with selectors and bytecode
14
+ * - packedBytecode: Buffer containing the contract's packed bytecode
15
+ * - id: Fr value representing the contract class ID
16
+ *
17
+ * @param contract - The contract class with ID to serialize
18
+ * @returns JSON string representation of the contract class
19
+ * @throws If any contract components cannot be properly serialized
20
+ */
21
+ export function serializeContractClassWithId(contract: ContractClassWithId): string {
22
+ return JSON.stringify(contract, (key, value) => {
23
+ let result = value;
24
+ switch (key) {
25
+ case 'version':
26
+ result = value;
27
+ break;
28
+ case 'artifactHash':
29
+ result = value.toString();
30
+ break;
31
+ case 'privateFunctions':
32
+ result = value.map((fn: PrivateFunction) => ({
33
+ selector: fn.selector.toString(),
34
+ vkHash: fn.vkHash.toString(),
35
+ }));
36
+ break;
37
+ case 'publicFunctions':
38
+ result = value.map((fn: PublicFunction) => ({
39
+ selector: fn.selector.toString(),
40
+ bytecode: fn.bytecode.toString('hex'),
41
+ }));
42
+ break;
43
+ case 'packedBytecode':
44
+ result = value.toString('hex');
45
+ break;
46
+ case 'id':
47
+ result = value.toString();
48
+ break;
49
+ default:
50
+ console.warn(`serializeContractClassWithId: Unknown key: ${key}`);
51
+ break;
52
+ }
53
+ return result;
54
+ });
55
+ }
56
+
57
+ /**
58
+ * Deserializes a JSON string into a ContractClassWithId instance.
59
+ * Reconstructs complex Aztec types from their string representations.
60
+ *
61
+ * The deserialization process handles:
62
+ * - version: Preserved as-is
63
+ * - artifactHash: Reconstructed as Fr from hex string
64
+ * - privateFunctions: Array of functions with:
65
+ * * selector: Reconstructed as FunctionSelector
66
+ * * vkHash: Reconstructed as Fr
67
+ * - publicFunctions: Array of functions with:
68
+ * * selector: Reconstructed as FunctionSelector
69
+ * * bytecode: Reconstructed as Buffer
70
+ * - packedBytecode: Reconstructed as Buffer from hex string
71
+ * - id: Reconstructed as Fr from hex string
72
+ *
73
+ * @param json - The JSON string to deserialize, previously created by serializeContractClassWithId
74
+ * @returns Reconstructed ContractClassWithId instance
75
+ * @throws If the JSON string is malformed or contains invalid data
76
+ */
77
+ export function deserializeContractClassWithId(json: string): ContractClassWithId {
78
+ return JSON.parse(json, (key, value) => {
79
+ switch (key) {
80
+ case 'version':
81
+ return value;
82
+ case 'artifactHash':
83
+ return Fr.fromHexString(value);
84
+ case 'privateFunctions':
85
+ return value.map((fn: { selector: string; vkHash: string }) => ({
86
+ selector: FunctionSelector.fromString(fn.selector),
87
+ vkHash: Fr.fromHexString(fn.vkHash),
88
+ }));
89
+ case 'publicFunctions':
90
+ return value.map((fn: { selector: string; bytecode: string }) => ({
91
+ selector: FunctionSelector.fromString(fn.selector),
92
+ bytecode: Buffer.from(fn.bytecode, 'hex'),
93
+ }));
94
+ case 'packedBytecode':
95
+ return Buffer.from(value, 'hex');
96
+ case 'id':
97
+ return Fr.fromHexString(value);
98
+ default:
99
+ console.warn(`deserializeContractClassWithId: Unknown key: ${key}`);
100
+ break;
101
+ }
102
+ return value;
103
+ });
104
+ }
@@ -0,0 +1,162 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { Fr, AztecAddress } from '@aztec/aztec.js';
3
+ import { Buffer } from 'node:buffer';
4
+ import {
5
+ aztecGetContractInstanceSerializer,
6
+ aztecGetContractClassSerializer,
7
+ aztecGetContractArtifactSerializer,
8
+ aztecRegisterContractSerializer,
9
+ } from './contract.js';
10
+ import {
11
+ getContractClassFromArtifact,
12
+ ContractClassWithIdSchema,
13
+ ContractInstanceWithAddressSchema,
14
+ SerializableContractInstance,
15
+ } from '@aztec/circuits.js';
16
+ import {
17
+ ContractArtifact,
18
+ ContractArtifactSchema,
19
+ type FieldLayout,
20
+ type ContractNote,
21
+ } from '@aztec/foundation/abi';
22
+ import { randomContractArtifact, randomDeployedContract } from '@aztec/circuit-types';
23
+ import { aztecAddressSerializer, frSerializer } from './core.js';
24
+
25
+ describe('Contract Serializers', () => {
26
+ describe('aztec_getContractInstance', () => {
27
+ it('should serialize and deserialize params', () => {
28
+ const address = AztecAddress.random();
29
+ const params = { address };
30
+
31
+ const serialized = aztecGetContractInstanceSerializer.params.serialize(
32
+ 'aztec_getContractInstance',
33
+ params,
34
+ );
35
+ expect(serialized.method).toBe('aztec_getContractInstance');
36
+
37
+ const deserialized = aztecGetContractInstanceSerializer.params.deserialize(
38
+ 'aztec_getContractInstance',
39
+ serialized,
40
+ );
41
+ expect(deserialized.address.toString()).toBe(address.toString());
42
+ });
43
+
44
+ it('should serialize and deserialize result', () => {
45
+ // Create a contract instance with all required fields
46
+ const result = SerializableContractInstance.random().withAddress(AztecAddress.random());
47
+
48
+ const serialized = aztecGetContractInstanceSerializer.result.serialize(
49
+ 'aztec_getContractInstance',
50
+ result,
51
+ );
52
+ expect(serialized.method).toBe('aztec_getContractInstance');
53
+
54
+ const deserialized = aztecGetContractInstanceSerializer.result.deserialize(
55
+ 'aztec_getContractInstance',
56
+ serialized,
57
+ );
58
+ expect(deserialized.address.toString()).toBe(result.address.toString());
59
+ expect(deserialized.contractClassId.toString()).toBe(result.contractClassId.toString());
60
+ expect(deserialized.deployer.toString()).toBe(result.deployer.toString());
61
+ expect(deserialized.salt.toString()).toBe(result.salt.toString());
62
+ expect(deserialized.version).toBe(result.version);
63
+ });
64
+ });
65
+
66
+ describe('aztec_getContractClass', () => {
67
+ it('should serialize and deserialize params', () => {
68
+ const id = Fr.random();
69
+ const params = { id };
70
+
71
+ const serialized = aztecGetContractClassSerializer.params.serialize('aztec_getContractClass', params);
72
+ expect(serialized.method).toBe('aztec_getContractClass');
73
+
74
+ const deserialized = aztecGetContractClassSerializer.params.deserialize(
75
+ 'aztec_getContractClass',
76
+ serialized,
77
+ );
78
+ expect(deserialized.id.toString()).toBe(id.toString());
79
+ });
80
+
81
+ it('should serialize and deserialize result', () => {
82
+ const artifact = randomContractArtifact();
83
+ const result = getContractClassFromArtifact(artifact);
84
+ const serialized = aztecGetContractClassSerializer.result.serialize('aztec_getContractClass', result);
85
+ expect(serialized.method).toBe('aztec_getContractClass');
86
+
87
+ const deserialized = aztecGetContractClassSerializer.result.deserialize(
88
+ 'aztec_getContractClass',
89
+ serialized,
90
+ );
91
+ expect(deserialized.artifactHash.toString()).toEqual(result.artifactHash.toString());
92
+ expect(deserialized.id.toString()).toEqual(result.id.toString());
93
+ expect(deserialized.privateFunctions).toEqual(result.privateFunctions);
94
+ expect(deserialized.publicFunctions).toEqual(result.publicFunctions);
95
+ expect(deserialized.packedBytecode).toEqual(result.packedBytecode);
96
+ });
97
+ });
98
+
99
+ describe('aztec_getContractArtifact', () => {
100
+ it('should serialize and deserialize params', () => {
101
+ const id = Fr.random();
102
+ const serialized = aztecGetContractArtifactSerializer.params.serialize('aztec_getContractArtifact', {
103
+ id,
104
+ });
105
+ expect(serialized.method).toBe('aztec_getContractArtifact');
106
+
107
+ const deserialized = aztecGetContractArtifactSerializer.params.deserialize(
108
+ 'aztec_getContractArtifact',
109
+ serialized,
110
+ );
111
+ expect(deserialized.id.toString()).toBe(id.toString());
112
+ });
113
+
114
+ it('should serialize and deserialize result', () => {
115
+ const result = randomContractArtifact();
116
+ const serialized = aztecGetContractArtifactSerializer.result.serialize(
117
+ 'aztec_getContractArtifact',
118
+ result,
119
+ );
120
+ expect(serialized.method).toBe('aztec_getContractArtifact');
121
+
122
+ const deserialized = aztecGetContractArtifactSerializer.result.deserialize(
123
+ 'aztec_getContractArtifact',
124
+ serialized,
125
+ );
126
+ expect(ContractArtifactSchema.parse(deserialized)).toEqual(result);
127
+ });
128
+ });
129
+
130
+ describe('aztec_registerContract', () => {
131
+ it('should serialize and deserialize params', () => {
132
+ const { instance, artifact } = randomDeployedContract();
133
+ const serialized = aztecRegisterContractSerializer.params.serialize('aztec_registerContract', {
134
+ instance,
135
+ artifact,
136
+ });
137
+ expect(serialized.method).toBe('aztec_registerContract');
138
+
139
+ const deserialized = aztecRegisterContractSerializer.params.deserialize(
140
+ 'aztec_registerContract',
141
+ serialized,
142
+ );
143
+ expect(deserialized.instance.address.toString()).toBe(instance.address.toString());
144
+ expect(deserialized.instance.contractClassId.toString()).toBe(instance.contractClassId.toString());
145
+ expect(deserialized.instance.deployer.toString()).toBe(instance.deployer.toString());
146
+ expect(deserialized.artifact).toEqual(artifact);
147
+ });
148
+
149
+ it('should serialize and deserialize result', () => {
150
+ const result = true;
151
+
152
+ const serialized = aztecRegisterContractSerializer.result.serialize('aztec_registerContract', result);
153
+ expect(serialized.method).toBe('aztec_registerContract');
154
+
155
+ const deserialized = aztecRegisterContractSerializer.result.deserialize(
156
+ 'aztec_registerContract',
157
+ serialized,
158
+ );
159
+ expect(deserialized).toBe(result);
160
+ });
161
+ });
162
+ });
@@ -0,0 +1,350 @@
1
+ import type { AztecWalletMethodMap } from '../types.js';
2
+ import type { JSONRPCSerializedData, JSONRPCSerializer } from './types.js';
3
+ import { encodeBase64, decodeBase64 } from './types.js';
4
+ import { aztecAddressSerializer, frSerializer } from './core.js';
5
+ import { SerializableContractInstance } from '@aztec/circuits.js';
6
+ import { ContractArtifactSchema } from '@aztec/foundation/abi';
7
+ import { serializeContractClassWithId, deserializeContractClassWithId } from './contract-utils.js';
8
+
9
+ /**
10
+ * Serializer for the aztec_getContractInstance RPC method.
11
+ * Handles serialization of contract instance queries and results between JSON-RPC format and native Aztec types.
12
+ * Used to retrieve deployed contract instances by their address.
13
+ */
14
+ export class AztecGetContractInstanceSerializer
15
+ implements
16
+ JSONRPCSerializer<
17
+ AztecWalletMethodMap['aztec_getContractInstance']['params'],
18
+ AztecWalletMethodMap['aztec_getContractInstance']['result']
19
+ >
20
+ {
21
+ params = {
22
+ /**
23
+ * Serializes contract instance query parameters for RPC transport.
24
+ * @param method - The RPC method name
25
+ * @param value - The parameters containing the contract address to look up
26
+ * @returns Serialized address data
27
+ */
28
+ serialize: (
29
+ method: string,
30
+ value: AztecWalletMethodMap['aztec_getContractInstance']['params'],
31
+ ): JSONRPCSerializedData => {
32
+ const { address } = value;
33
+ return {
34
+ method,
35
+ serialized: encodeBase64(aztecAddressSerializer.serialize(address)),
36
+ };
37
+ },
38
+ /**
39
+ * Deserializes contract instance query parameters from RPC transport.
40
+ * @param method - The RPC method name
41
+ * @param data - The serialized address data
42
+ * @returns Deserialized query parameters
43
+ */
44
+ deserialize: (
45
+ method: string,
46
+ data: JSONRPCSerializedData,
47
+ ): AztecWalletMethodMap['aztec_getContractInstance']['params'] => {
48
+ const address = aztecAddressSerializer.deserialize(decodeBase64(data.serialized));
49
+ return { address };
50
+ },
51
+ };
52
+
53
+ result = {
54
+ /**
55
+ * Serializes the contract instance query result.
56
+ * @param method - The RPC method name
57
+ * @param value - The contract instance data including address and state
58
+ * @returns Serialized contract instance data
59
+ */
60
+ serialize: (
61
+ method: string,
62
+ value: AztecWalletMethodMap['aztec_getContractInstance']['result'],
63
+ ): JSONRPCSerializedData => {
64
+ const serializable = new SerializableContractInstance(value);
65
+ const { address } = value;
66
+
67
+ return {
68
+ method,
69
+ serialized: encodeBase64(
70
+ JSON.stringify({
71
+ instance: serializable.toBuffer().toString('hex'),
72
+ address: aztecAddressSerializer.serialize(address),
73
+ }),
74
+ ),
75
+ };
76
+ },
77
+ /**
78
+ * Deserializes the contract instance query result.
79
+ * @param method - The RPC method name
80
+ * @param data - The serialized contract instance data
81
+ * @returns Deserialized contract instance
82
+ */
83
+ deserialize: (
84
+ method: string,
85
+ data: JSONRPCSerializedData,
86
+ ): AztecWalletMethodMap['aztec_getContractInstance']['result'] => {
87
+ const { instance, address } = JSON.parse(decodeBase64(data.serialized));
88
+ const baseInstance = SerializableContractInstance.fromBuffer(Buffer.from(instance, 'hex'));
89
+ const deserializedAddress = aztecAddressSerializer.deserialize(address);
90
+ return baseInstance.withAddress(deserializedAddress);
91
+ },
92
+ };
93
+ }
94
+
95
+ /**
96
+ * Serializer for the aztec_getContractClass RPC method.
97
+ * Handles serialization of contract class queries and results between JSON-RPC format and native Aztec types.
98
+ * Used to retrieve contract class definitions by their ID.
99
+ */
100
+ export class AztecGetContractClassSerializer
101
+ implements
102
+ JSONRPCSerializer<
103
+ AztecWalletMethodMap['aztec_getContractClass']['params'],
104
+ AztecWalletMethodMap['aztec_getContractClass']['result']
105
+ >
106
+ {
107
+ params = {
108
+ /**
109
+ * Serializes contract class query parameters for RPC transport.
110
+ * @param method - The RPC method name
111
+ * @param value - The parameters containing the class ID to look up
112
+ * @returns Serialized class ID data
113
+ */
114
+ serialize: (
115
+ method: string,
116
+ value: AztecWalletMethodMap['aztec_getContractClass']['params'],
117
+ ): JSONRPCSerializedData => {
118
+ const { id } = value;
119
+ return {
120
+ method,
121
+ serialized: encodeBase64(frSerializer.serialize(id)),
122
+ };
123
+ },
124
+ /**
125
+ * Deserializes contract class query parameters from RPC transport.
126
+ * @param method - The RPC method name
127
+ * @param data - The serialized class ID data
128
+ * @returns Deserialized query parameters
129
+ */
130
+ deserialize: (
131
+ method: string,
132
+ data: JSONRPCSerializedData,
133
+ ): AztecWalletMethodMap['aztec_getContractClass']['params'] => {
134
+ const id = frSerializer.deserialize(decodeBase64(data.serialized));
135
+ return { id };
136
+ },
137
+ };
138
+
139
+ result = {
140
+ /**
141
+ * Serializes the contract class query result.
142
+ * @param method - The RPC method name
143
+ * @param value - The contract class definition with its ID
144
+ * @returns Serialized contract class data
145
+ */
146
+ serialize: (
147
+ method: string,
148
+ value: AztecWalletMethodMap['aztec_getContractClass']['result'],
149
+ ): JSONRPCSerializedData => {
150
+ const serialized = serializeContractClassWithId(value);
151
+ return {
152
+ method,
153
+ serialized: encodeBase64(JSON.stringify(serialized)),
154
+ };
155
+ },
156
+ /**
157
+ * Deserializes the contract class query result.
158
+ * @param method - The RPC method name
159
+ * @param data - The serialized contract class data
160
+ * @returns Deserialized contract class with ID
161
+ */
162
+ deserialize: (
163
+ method: string,
164
+ data: JSONRPCSerializedData,
165
+ ): AztecWalletMethodMap['aztec_getContractClass']['result'] => {
166
+ const parsed = JSON.parse(decodeBase64(data.serialized));
167
+ return deserializeContractClassWithId(parsed);
168
+ },
169
+ };
170
+ }
171
+
172
+ /**
173
+ * Serializer for the aztec_getContractArtifact RPC method.
174
+ * Handles serialization of contract artifact queries and results between JSON-RPC format and native Aztec types.
175
+ * Used to retrieve contract artifacts (ABI, bytecode, etc.) by their ID.
176
+ */
177
+ export class AztecGetContractArtifactSerializer
178
+ implements
179
+ JSONRPCSerializer<
180
+ AztecWalletMethodMap['aztec_getContractArtifact']['params'],
181
+ AztecWalletMethodMap['aztec_getContractArtifact']['result']
182
+ >
183
+ {
184
+ params = {
185
+ /**
186
+ * Serializes contract artifact query parameters for RPC transport.
187
+ * @param method - The RPC method name
188
+ * @param value - The parameters containing the artifact ID to look up
189
+ * @returns Serialized artifact ID data
190
+ */
191
+ serialize: (
192
+ method: string,
193
+ value: AztecWalletMethodMap['aztec_getContractArtifact']['params'],
194
+ ): JSONRPCSerializedData => {
195
+ const { id } = value;
196
+ return {
197
+ method,
198
+ serialized: encodeBase64(frSerializer.serialize(id)),
199
+ };
200
+ },
201
+ /**
202
+ * Deserializes contract artifact query parameters from RPC transport.
203
+ * @param method - The RPC method name
204
+ * @param data - The serialized artifact ID data
205
+ * @returns Deserialized query parameters
206
+ */
207
+ deserialize: (
208
+ method: string,
209
+ data: JSONRPCSerializedData,
210
+ ): AztecWalletMethodMap['aztec_getContractArtifact']['params'] => {
211
+ const id = frSerializer.deserialize(decodeBase64(data.serialized));
212
+ return { id };
213
+ },
214
+ };
215
+
216
+ result = {
217
+ /**
218
+ * Serializes the contract artifact query result.
219
+ * @param method - The RPC method name
220
+ * @param value - The contract artifact data
221
+ * @returns Serialized contract artifact
222
+ */
223
+ serialize: (
224
+ method: string,
225
+ value: AztecWalletMethodMap['aztec_getContractArtifact']['result'],
226
+ ): JSONRPCSerializedData => {
227
+ return {
228
+ method,
229
+ serialized: encodeBase64(JSON.stringify(value)),
230
+ };
231
+ },
232
+ /**
233
+ * Deserializes the contract artifact query result.
234
+ * @param method - The RPC method name
235
+ * @param data - The serialized artifact data
236
+ * @returns Deserialized contract artifact
237
+ * @throws If the data doesn't match the contract artifact schema
238
+ */
239
+ deserialize: (
240
+ method: string,
241
+ data: JSONRPCSerializedData,
242
+ ): AztecWalletMethodMap['aztec_getContractArtifact']['result'] => {
243
+ return ContractArtifactSchema.parse(JSON.parse(decodeBase64(data.serialized)));
244
+ },
245
+ };
246
+ }
247
+
248
+ /**
249
+ * Serializer for the aztec_registerContract RPC method.
250
+ * Handles serialization of contract registration requests between JSON-RPC format and native Aztec types.
251
+ * Used to register new contract instances with optional artifacts.
252
+ */
253
+ export class AztecRegisterContractSerializer
254
+ implements
255
+ JSONRPCSerializer<
256
+ AztecWalletMethodMap['aztec_registerContract']['params'],
257
+ AztecWalletMethodMap['aztec_registerContract']['result']
258
+ >
259
+ {
260
+ params = {
261
+ /**
262
+ * Serializes contract registration parameters for RPC transport.
263
+ * @param method - The RPC method name
264
+ * @param value - The parameters containing contract instance and optional artifact
265
+ * @returns Serialized registration data
266
+ */
267
+ serialize: (
268
+ method: string,
269
+ value: AztecWalletMethodMap['aztec_registerContract']['params'],
270
+ ): JSONRPCSerializedData => {
271
+ const { instance, artifact } = value;
272
+ const serializable = new SerializableContractInstance(instance);
273
+
274
+ const serializedInstance = {
275
+ instance: Buffer.from(serializable.toBuffer()).toString('base64'),
276
+ address: aztecAddressSerializer.serialize(instance.address),
277
+ };
278
+
279
+ const serializedArtifact = artifact ? JSON.stringify(artifact) : undefined;
280
+
281
+ return {
282
+ method,
283
+ serialized: encodeBase64(
284
+ JSON.stringify({
285
+ instance: serializedInstance,
286
+ artifact: serializedArtifact,
287
+ }),
288
+ ),
289
+ };
290
+ },
291
+ /**
292
+ * Deserializes contract registration parameters from RPC transport.
293
+ * @param method - The RPC method name
294
+ * @param data - The serialized registration data
295
+ * @returns Deserialized registration parameters
296
+ */
297
+ deserialize: (
298
+ method: string,
299
+ data: JSONRPCSerializedData,
300
+ ): AztecWalletMethodMap['aztec_registerContract']['params'] => {
301
+ const { instance, artifact } = JSON.parse(decodeBase64(data.serialized));
302
+ const { instance: instanceBuffer, address } = instance;
303
+ const baseInstance = SerializableContractInstance.fromBuffer(Buffer.from(instanceBuffer, 'base64'));
304
+ const deserializedAddress = aztecAddressSerializer.deserialize(address);
305
+ return {
306
+ instance: baseInstance.withAddress(deserializedAddress),
307
+ artifact: artifact ? JSON.parse(artifact) : undefined,
308
+ };
309
+ },
310
+ };
311
+
312
+ result = {
313
+ /**
314
+ * Serializes the contract registration result.
315
+ * @param method - The RPC method name
316
+ * @param value - Boolean indicating success of registration
317
+ * @returns Serialized result
318
+ */
319
+ serialize: (
320
+ method: string,
321
+ value: AztecWalletMethodMap['aztec_registerContract']['result'],
322
+ ): JSONRPCSerializedData => {
323
+ return {
324
+ method,
325
+ serialized: encodeBase64(JSON.stringify(value)),
326
+ };
327
+ },
328
+ /**
329
+ * Deserializes the contract registration result.
330
+ * @param method - The RPC method name
331
+ * @param data - The serialized result data
332
+ * @returns Boolean indicating success
333
+ */
334
+ deserialize: (
335
+ method: string,
336
+ data: JSONRPCSerializedData,
337
+ ): AztecWalletMethodMap['aztec_registerContract']['result'] => {
338
+ return JSON.parse(decodeBase64(data.serialized));
339
+ },
340
+ };
341
+ }
342
+
343
+ /**
344
+ * Pre-instantiated serializer instances for Aztec contract-related RPC methods.
345
+ * These instances can be used directly by the RPC handler implementation.
346
+ */
347
+ export const aztecGetContractInstanceSerializer = new AztecGetContractInstanceSerializer();
348
+ export const aztecGetContractClassSerializer = new AztecGetContractClassSerializer();
349
+ export const aztecGetContractArtifactSerializer = new AztecGetContractArtifactSerializer();
350
+ export const aztecRegisterContractSerializer = new AztecRegisterContractSerializer();
@@ -0,0 +1,56 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { Fr, AztecAddress, CompleteAddress, AuthWitness, TxHash } from '@aztec/aztec.js';
3
+ import {
4
+ frSerializer,
5
+ aztecAddressSerializer,
6
+ completeAddressSerializer,
7
+ authWitnessSerializer,
8
+ txHashSerializer,
9
+ } from './core.js';
10
+
11
+ describe('Core Serializers', () => {
12
+ describe('FrSerializer', () => {
13
+ it('should serialize and deserialize Fr values', () => {
14
+ const original = Fr.random();
15
+ const serialized = frSerializer.serialize(original);
16
+ const deserialized = frSerializer.deserialize(serialized);
17
+ expect(deserialized.toString()).toBe(original.toString());
18
+ });
19
+ });
20
+
21
+ describe('AztecAddressSerializer', () => {
22
+ it('should serialize and deserialize AztecAddress values', () => {
23
+ const original = AztecAddress.random();
24
+ const serialized = aztecAddressSerializer.serialize(original);
25
+ const deserialized = aztecAddressSerializer.deserialize(serialized);
26
+ expect(deserialized.toString()).toBe(original.toString());
27
+ });
28
+ });
29
+
30
+ describe('CompleteAddressSerializer', () => {
31
+ it('should serialize and deserialize CompleteAddress values', () => {
32
+ const original = CompleteAddress.random();
33
+ const serialized = completeAddressSerializer.serialize(original);
34
+ const deserialized = completeAddressSerializer.deserialize(serialized);
35
+ expect(deserialized.toString()).toBe(original.toString());
36
+ });
37
+ });
38
+
39
+ describe('AuthWitnessSerializer', () => {
40
+ it('should serialize and deserialize AuthWitness values', () => {
41
+ const original = AuthWitness.random();
42
+ const serialized = authWitnessSerializer.serialize(original);
43
+ const deserialized = authWitnessSerializer.deserialize(serialized);
44
+ expect(deserialized.toString()).toBe(original.toString());
45
+ });
46
+ });
47
+
48
+ describe('TxHashSerializer', () => {
49
+ it('should serialize and deserialize TxHash values', () => {
50
+ const original = TxHash.random();
51
+ const serialized = txHashSerializer.serialize(original);
52
+ const deserialized = txHashSerializer.deserialize(serialized);
53
+ expect(deserialized.toString()).toBe(original.toString());
54
+ });
55
+ });
56
+ });