@walletmesh/aztec-rpc-wallet 0.3.0 → 0.4.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 (185) hide show
  1. package/CHANGELOG.md +26 -0
  2. package/README.md +290 -228
  3. package/dist/.tsbuildinfo +1 -1
  4. package/dist/client/aztec-dapp-wallet.d.ts +401 -0
  5. package/dist/client/aztec-dapp-wallet.d.ts.map +1 -0
  6. package/dist/client/aztec-dapp-wallet.js +705 -0
  7. package/dist/client/aztec-router-provider.d.ts +58 -0
  8. package/dist/client/aztec-router-provider.d.ts.map +1 -0
  9. package/dist/client/aztec-router-provider.js +62 -0
  10. package/dist/client/helpers.d.ts +44 -0
  11. package/dist/client/helpers.d.ts.map +1 -0
  12. package/dist/client/helpers.js +79 -0
  13. package/dist/client/register-serializers.d.ts +41 -0
  14. package/dist/client/register-serializers.d.ts.map +1 -0
  15. package/dist/client/register-serializers.js +97 -0
  16. package/dist/contractArtifactCache.d.ts +49 -32
  17. package/dist/contractArtifactCache.d.ts.map +1 -1
  18. package/dist/contractArtifactCache.js +47 -34
  19. package/dist/errors.d.ts +50 -8
  20. package/dist/errors.d.ts.map +1 -1
  21. package/dist/errors.js +50 -10
  22. package/dist/index.d.ts +53 -40
  23. package/dist/index.d.ts.map +1 -1
  24. package/dist/index.js +52 -17
  25. package/dist/types.d.ts +345 -268
  26. package/dist/types.d.ts.map +1 -1
  27. package/dist/types.js +10 -8
  28. package/dist/wallet/create-node.d.ts +73 -0
  29. package/dist/wallet/create-node.d.ts.map +1 -0
  30. package/dist/wallet/create-node.js +99 -0
  31. package/dist/wallet/handlers/account.d.ts +67 -0
  32. package/dist/wallet/handlers/account.d.ts.map +1 -0
  33. package/dist/wallet/handlers/account.js +85 -0
  34. package/dist/wallet/handlers/contract-interaction.d.ts +77 -0
  35. package/dist/wallet/handlers/contract-interaction.d.ts.map +1 -0
  36. package/dist/wallet/handlers/contract-interaction.js +219 -0
  37. package/dist/wallet/handlers/contract.d.ts +96 -0
  38. package/dist/wallet/handlers/contract.d.ts.map +1 -0
  39. package/dist/wallet/handlers/contract.js +146 -0
  40. package/dist/wallet/handlers/event.d.ts +62 -0
  41. package/dist/wallet/handlers/event.d.ts.map +1 -0
  42. package/dist/wallet/handlers/event.js +85 -0
  43. package/dist/wallet/handlers/index.d.ts +88 -0
  44. package/dist/wallet/handlers/index.d.ts.map +1 -0
  45. package/dist/wallet/handlers/index.js +47 -0
  46. package/dist/wallet/handlers/node.d.ts +117 -0
  47. package/dist/wallet/handlers/node.d.ts.map +1 -0
  48. package/dist/wallet/handlers/node.js +196 -0
  49. package/dist/wallet/handlers/senders.d.ts +70 -0
  50. package/dist/wallet/handlers/senders.d.ts.map +1 -0
  51. package/dist/wallet/handlers/senders.js +92 -0
  52. package/dist/wallet/handlers/transaction.d.ts +123 -0
  53. package/dist/wallet/handlers/transaction.d.ts.map +1 -0
  54. package/dist/wallet/handlers/transaction.js +191 -0
  55. package/dist/wallet/serializers.d.ts +75 -0
  56. package/dist/wallet/serializers.d.ts.map +1 -0
  57. package/dist/wallet/serializers.js +501 -0
  58. package/docs/README.md +290 -229
  59. package/docs/classes/AztecDappWallet.md +1304 -0
  60. package/docs/classes/AztecRouterProvider.md +1058 -0
  61. package/docs/classes/AztecWalletError.md +124 -47
  62. package/docs/classes/ContractArtifactCache.md +48 -31
  63. package/docs/functions/connectAztec.md +62 -0
  64. package/docs/functions/createAztecWallet.md +46 -0
  65. package/docs/functions/createAztecWalletNode.md +96 -0
  66. package/docs/functions/registerAztecSerializers.md +61 -0
  67. package/docs/functions/registerWalletAztecSerializers.md +39 -0
  68. package/docs/globals.md +16 -14
  69. package/docs/interfaces/AztecHandlerContext.md +54 -0
  70. package/docs/interfaces/AztecWalletContext.md +50 -0
  71. package/docs/interfaces/AztecWalletMethodMap.md +471 -470
  72. package/docs/type-aliases/AztecChainId.md +16 -3
  73. package/docs/variables/ALL_AZTEC_METHODS.md +20 -0
  74. package/docs/variables/AztecWalletErrorMap.md +9 -4
  75. package/docs/variables/AztecWalletSerializer.md +45 -0
  76. package/package.json +9 -9
  77. package/src/client/aztec-dapp-wallet.test.ts +628 -0
  78. package/src/client/aztec-dapp-wallet.ts +879 -0
  79. package/src/client/aztec-router-provider.test.ts +235 -0
  80. package/src/client/aztec-router-provider.ts +64 -0
  81. package/src/client/helpers.test.ts +187 -0
  82. package/src/client/helpers.ts +91 -0
  83. package/src/client/register-serializers.ts +108 -0
  84. package/src/contractArtifactCache.test.ts +21 -10
  85. package/src/contractArtifactCache.ts +54 -35
  86. package/src/errors.ts +58 -10
  87. package/src/index.test.ts +2 -6
  88. package/src/index.ts +73 -37
  89. package/src/types.ts +379 -217
  90. package/src/wallet/create-node.test.ts +332 -0
  91. package/src/wallet/create-node.ts +120 -0
  92. package/src/wallet/handlers/account.test.ts +172 -0
  93. package/src/wallet/handlers/account.ts +99 -0
  94. package/src/wallet/handlers/contract-interaction.test.ts +248 -0
  95. package/src/wallet/handlers/contract-interaction.ts +269 -0
  96. package/src/wallet/handlers/contract.test.ts +245 -0
  97. package/src/wallet/handlers/contract.ts +174 -0
  98. package/src/wallet/handlers/event.test.ts +216 -0
  99. package/src/wallet/handlers/event.ts +99 -0
  100. package/src/wallet/handlers/index.ts +84 -0
  101. package/src/wallet/handlers/node.test.ts +304 -0
  102. package/src/wallet/handlers/node.ts +230 -0
  103. package/src/wallet/handlers/senders.test.ts +172 -0
  104. package/src/wallet/handlers/senders.ts +106 -0
  105. package/src/wallet/handlers/transaction.test.ts +371 -0
  106. package/src/wallet/handlers/transaction.ts +239 -0
  107. package/src/wallet/serializers.test.ts +253 -0
  108. package/src/wallet/serializers.ts +586 -0
  109. package/typedoc.json +23 -1
  110. package/dist/aztecRemoteWallet.d.ts +0 -70
  111. package/dist/aztecRemoteWallet.d.ts.map +0 -1
  112. package/dist/aztecRemoteWallet.js +0 -335
  113. package/dist/chainProvider.d.ts +0 -56
  114. package/dist/chainProvider.d.ts.map +0 -1
  115. package/dist/chainProvider.js +0 -98
  116. package/dist/handlers/aztecAccountWallet.d.ts +0 -4
  117. package/dist/handlers/aztecAccountWallet.d.ts.map +0 -1
  118. package/dist/handlers/aztecAccountWallet.js +0 -296
  119. package/dist/handlers/transactions.d.ts +0 -21
  120. package/dist/handlers/transactions.d.ts.map +0 -1
  121. package/dist/handlers/transactions.js +0 -98
  122. package/dist/handlers.d.ts +0 -27
  123. package/dist/handlers.d.ts.map +0 -1
  124. package/dist/handlers.js +0 -55
  125. package/dist/provider.d.ts +0 -105
  126. package/dist/provider.d.ts.map +0 -1
  127. package/dist/provider.js +0 -160
  128. package/dist/serializers/account.d.ts +0 -164
  129. package/dist/serializers/account.d.ts.map +0 -1
  130. package/dist/serializers/account.js +0 -244
  131. package/dist/serializers/contract.d.ts +0 -62
  132. package/dist/serializers/contract.d.ts.map +0 -1
  133. package/dist/serializers/contract.js +0 -130
  134. package/dist/serializers/index.d.ts +0 -21
  135. package/dist/serializers/index.d.ts.map +0 -1
  136. package/dist/serializers/index.js +0 -154
  137. package/dist/serializers/log.d.ts +0 -66
  138. package/dist/serializers/log.d.ts.map +0 -1
  139. package/dist/serializers/log.js +0 -222
  140. package/dist/serializers/note.d.ts +0 -124
  141. package/dist/serializers/note.d.ts.map +0 -1
  142. package/dist/serializers/note.js +0 -208
  143. package/dist/serializers/transaction.d.ts +0 -99
  144. package/dist/serializers/transaction.d.ts.map +0 -1
  145. package/dist/serializers/transaction.js +0 -275
  146. package/dist/wallet.d.ts +0 -62
  147. package/dist/wallet.d.ts.map +0 -1
  148. package/dist/wallet.js +0 -77
  149. package/docs/classes/AztecChainProvider.md +0 -553
  150. package/docs/classes/AztecChainWallet.md +0 -409
  151. package/docs/classes/AztecProvider.md +0 -1112
  152. package/docs/interfaces/AztecWalletBaseMethodMap.md +0 -135
  153. package/docs/interfaces/AztecWalletEventMap.md +0 -17
  154. package/docs/type-aliases/AztecChainWalletMiddleware.md +0 -13
  155. package/docs/type-aliases/AztecWalletContext.md +0 -29
  156. package/docs/type-aliases/AztecWalletMethodHandler.md +0 -37
  157. package/docs/type-aliases/AztecWalletMiddleware.md +0 -13
  158. package/docs/type-aliases/AztecWalletRouterClient.md +0 -13
  159. package/docs/type-aliases/TransactionFunctionCall.md +0 -33
  160. package/docs/type-aliases/TransactionParams.md +0 -27
  161. package/src/aztecRemoteWallet.test.ts +0 -504
  162. package/src/aztecRemoteWallet.ts +0 -467
  163. package/src/chainProvider.test.ts +0 -401
  164. package/src/chainProvider.ts +0 -116
  165. package/src/handlers/aztecAccountWallet.test.ts +0 -649
  166. package/src/handlers/aztecAccountWallet.ts +0 -532
  167. package/src/handlers/transactions.ts +0 -124
  168. package/src/handlers.test.ts +0 -270
  169. package/src/handlers.ts +0 -70
  170. package/src/provider.test.ts +0 -274
  171. package/src/provider.ts +0 -189
  172. package/src/serializers/account.test.ts +0 -125
  173. package/src/serializers/account.ts +0 -301
  174. package/src/serializers/contract.test.ts +0 -24
  175. package/src/serializers/contract.ts +0 -183
  176. package/src/serializers/index.test.ts +0 -136
  177. package/src/serializers/index.ts +0 -191
  178. package/src/serializers/log.test.ts +0 -286
  179. package/src/serializers/log.ts +0 -292
  180. package/src/serializers/note.test.ts +0 -125
  181. package/src/serializers/note.ts +0 -250
  182. package/src/serializers/transaction.test.ts +0 -320
  183. package/src/serializers/transaction.ts +0 -409
  184. package/src/wallet.test.ts +0 -275
  185. package/src/wallet.ts +0 -94
@@ -0,0 +1,628 @@
1
+ import { describe, it, expect, vi, beforeEach, beforeAll, type Mock } from 'vitest';
2
+ import { AztecDappWallet, createAztecWallet } from './aztec-dapp-wallet.js';
3
+ import { AztecAddress, TxHash, CompleteAddress, Fr } from '@aztec/aztec.js';
4
+ import type { ContractArtifact, ContractInstanceWithAddress, Tx, TxExecutionRequest } from '@aztec/aztec.js';
5
+ import type { ExecutionPayload } from '@aztec/entrypoints/payload';
6
+ import type { FeeOptions, TxExecutionOptions } from '@aztec/entrypoints/interfaces';
7
+ import type { WalletRouterProvider } from '@walletmesh/router';
8
+
9
+ // Mock provider
10
+ const createMockProvider = () => {
11
+ const call = vi.fn() as Mock;
12
+ const on = vi.fn();
13
+ const off = vi.fn();
14
+ return {
15
+ call,
16
+ on,
17
+ off,
18
+ } as unknown as WalletRouterProvider & { call: Mock };
19
+ };
20
+
21
+ // Mock Aztec objects - we'll create these asynchronously in tests or beforeEach hooks
22
+ let mockAddress: AztecAddress;
23
+ let mockCompleteAddress: CompleteAddress;
24
+ let mockTxHash: TxHash;
25
+ const testChainId = 'aztec:test';
26
+
27
+ // Helper to create mock objects
28
+ async function createMockObjects() {
29
+ // Create a CompleteAddress first, then extract its address
30
+ mockCompleteAddress = await CompleteAddress.random();
31
+ mockAddress = mockCompleteAddress.address;
32
+ mockTxHash = await TxHash.random();
33
+ }
34
+
35
+ describe('AztecDappWallet', () => {
36
+ let provider: ReturnType<typeof createMockProvider>;
37
+ let wallet: AztecDappWallet;
38
+
39
+ beforeAll(async () => {
40
+ await createMockObjects();
41
+ });
42
+
43
+ beforeEach(() => {
44
+ provider = createMockProvider();
45
+ wallet = new AztecDappWallet(provider, testChainId);
46
+ });
47
+
48
+ describe('Cached properties', () => {
49
+ it('should throw when accessing address before initialization', () => {
50
+ expect(() => wallet.getAddress()).toThrow(
51
+ 'Wallet not initialized. Call createAztecWallet() to properly initialize the wallet.',
52
+ );
53
+ });
54
+
55
+ it('should throw when accessing completeAddress before initialization', () => {
56
+ expect(() => wallet.getCompleteAddress()).toThrow(
57
+ 'Wallet not initialized. Call createAztecWallet() to properly initialize the wallet.',
58
+ );
59
+ });
60
+
61
+ it('should throw when accessing chainId before initialization', () => {
62
+ expect(() => wallet.getChainId()).toThrow(
63
+ 'Chain ID not initialized. Call createAztecWallet() to properly initialize the wallet.',
64
+ );
65
+ });
66
+
67
+ it('should throw when accessing version before initialization', () => {
68
+ expect(() => wallet.getVersion()).toThrow(
69
+ 'Wallet version not initialized. Call createAztecWallet() to properly initialize the wallet.',
70
+ );
71
+ });
72
+
73
+ it('should return cached values after initialization', async () => {
74
+ const mockChainIdFr = new Fr(31337n);
75
+ const mockVersionFr = new Fr(1n);
76
+
77
+ provider.call.mockImplementation(async (_chainId: string, { method }: { method: string }) => {
78
+ switch (method) {
79
+ case 'aztec_getCompleteAddress':
80
+ return mockCompleteAddress;
81
+ case 'aztec_getChainId':
82
+ return mockChainIdFr;
83
+ case 'aztec_getVersion':
84
+ return mockVersionFr;
85
+ default:
86
+ throw new Error(`Unexpected method: ${method}`);
87
+ }
88
+ });
89
+
90
+ await wallet.initialize();
91
+
92
+ expect(wallet.getAddress()).toEqual(mockAddress);
93
+ expect(wallet.getCompleteAddress()).toEqual(mockCompleteAddress);
94
+ expect(wallet.getChainId()).toEqual(mockChainIdFr);
95
+ expect(wallet.getVersion()).toEqual(mockVersionFr);
96
+ });
97
+ });
98
+
99
+ describe('initialize', () => {
100
+ it('should fetch all cached values', async () => {
101
+ const mockChainIdFr = new Fr(31337n);
102
+ const mockVersionFr = new Fr(1n);
103
+
104
+ provider.call.mockImplementation(async (_chainId: string, { method }: { method: string }) => {
105
+ switch (method) {
106
+ case 'aztec_getCompleteAddress':
107
+ return mockCompleteAddress;
108
+ case 'aztec_getChainId':
109
+ return mockChainIdFr;
110
+ case 'aztec_getVersion':
111
+ return mockVersionFr;
112
+ default:
113
+ throw new Error(`Unexpected method: ${method}`);
114
+ }
115
+ });
116
+
117
+ await wallet.initialize();
118
+
119
+ expect(provider.call).toHaveBeenCalledWith(testChainId, { method: 'aztec_getCompleteAddress' });
120
+ expect(provider.call).toHaveBeenCalledWith(testChainId, { method: 'aztec_getChainId' });
121
+ expect(provider.call).toHaveBeenCalledWith(testChainId, { method: 'aztec_getVersion' });
122
+ });
123
+
124
+ it('should handle initialization errors', async () => {
125
+ provider.call.mockRejectedValue(new Error('Provider error'));
126
+
127
+ await expect(wallet.initialize()).rejects.toThrow('Provider error');
128
+ });
129
+ });
130
+
131
+ describe('Account methods', () => {
132
+ beforeEach(async () => {
133
+ // Initialize wallet with default chain ID for testing
134
+ const mockChainIdFr = new Fr(31337n);
135
+ const mockVersionFr = new Fr(1n);
136
+
137
+ provider.call.mockImplementation(async (_chainId: string, { method }: { method: string }) => {
138
+ switch (method) {
139
+ case 'aztec_getCompleteAddress':
140
+ return mockCompleteAddress;
141
+ case 'aztec_getChainId':
142
+ return mockChainIdFr;
143
+ case 'aztec_getVersion':
144
+ return mockVersionFr;
145
+ default:
146
+ return null;
147
+ }
148
+ });
149
+ await wallet.initialize();
150
+ provider.call.mockClear();
151
+ });
152
+
153
+ it('should call aztec_getAddress', async () => {
154
+ provider.call.mockResolvedValue(mockAddress);
155
+
156
+ const result = await wallet.getAddressAsync();
157
+
158
+ expect(provider.call).toHaveBeenCalledWith(testChainId, { method: 'aztec_getAddress' });
159
+ expect(result).toEqual(mockAddress);
160
+ });
161
+
162
+ it('should call aztec_getCompleteAddress', async () => {
163
+ provider.call.mockResolvedValue(mockCompleteAddress);
164
+
165
+ const result = await wallet.getCompleteAddressAsync();
166
+
167
+ expect(provider.call).toHaveBeenCalledWith(testChainId, { method: 'aztec_getCompleteAddress' });
168
+ expect(result).toEqual(mockCompleteAddress);
169
+ });
170
+
171
+ it('should call aztec_createAuthWit with Fr', async () => {
172
+ const messageHash = await Fr.random();
173
+ const authWitness = { messageHash };
174
+ provider.call.mockResolvedValue(authWitness);
175
+
176
+ const result = await wallet.createAuthWit(messageHash);
177
+
178
+ expect(provider.call).toHaveBeenCalledWith(testChainId, {
179
+ method: 'aztec_createAuthWit',
180
+ params: { intent: messageHash },
181
+ });
182
+ expect(result).toEqual(authWitness);
183
+ });
184
+
185
+ it('should call aztec_createAuthWit with Buffer', async () => {
186
+ const buffer = Buffer.from('test');
187
+ const authWitness = { messageHash: await Fr.random() };
188
+ provider.call.mockResolvedValue(authWitness);
189
+
190
+ const result = await wallet.createAuthWit(buffer);
191
+
192
+ expect(provider.call).toHaveBeenCalledWith(testChainId, {
193
+ method: 'aztec_createAuthWit',
194
+ params: { intent: buffer },
195
+ });
196
+ expect(result).toEqual(authWitness);
197
+ });
198
+ });
199
+
200
+ describe('Transaction methods', () => {
201
+ beforeEach(async () => {
202
+ const mockChainIdFr = new Fr(31337n);
203
+ const mockVersionFr = new Fr(1n);
204
+
205
+ provider.call.mockImplementation(async (_chainId: string, { method }: { method: string }) => {
206
+ switch (method) {
207
+ case 'aztec_getCompleteAddress':
208
+ return mockCompleteAddress;
209
+ case 'aztec_getChainId':
210
+ return mockChainIdFr;
211
+ case 'aztec_getVersion':
212
+ return mockVersionFr;
213
+ default:
214
+ return null;
215
+ }
216
+ });
217
+ await wallet.initialize();
218
+ provider.call.mockClear();
219
+ });
220
+
221
+ it('should create transaction execution request locally', async () => {
222
+ // The createTxExecutionRequest method now uses the local DefaultAccountEntrypoint
223
+ // instead of making an RPC call. Since we can't easily mock the entrypoint,
224
+ // we'll just verify it doesn't make an RPC call for this method.
225
+ const executionPayload = {
226
+ calls: [],
227
+ authWitnesses: [],
228
+ capsules: [],
229
+ extraHashedArgs: [],
230
+ } as ExecutionPayload;
231
+ const feeOptions = {} as FeeOptions;
232
+ const txOptions = {} as TxExecutionOptions;
233
+
234
+ // This will throw because we haven't mocked all the internal dependencies
235
+ // but that's okay - we just want to verify no RPC call is made
236
+ await expect(
237
+ wallet.createTxExecutionRequest(executionPayload, feeOptions, txOptions),
238
+ ).rejects.toThrow();
239
+
240
+ // Verify no RPC call was made for createTxExecutionRequest
241
+ expect(provider.call).not.toHaveBeenCalledWith(
242
+ expect.any(String),
243
+ expect.objectContaining({ method: 'aztec_createTxExecutionRequest' }),
244
+ );
245
+ });
246
+
247
+ it('should call aztec_sendTx', async () => {
248
+ const tx = { request: {} } as unknown as Tx;
249
+ provider.call.mockResolvedValue(mockTxHash);
250
+
251
+ const result = await wallet.sendTx(tx);
252
+
253
+ expect(provider.call).toHaveBeenCalledWith(testChainId, {
254
+ method: 'aztec_sendTx',
255
+ params: { tx },
256
+ });
257
+ expect(result).toEqual(mockTxHash);
258
+ });
259
+
260
+ it('should call aztec_getTxReceipt', async () => {
261
+ const receipt = { txHash: mockTxHash };
262
+ provider.call.mockResolvedValue(receipt);
263
+
264
+ const result = await wallet.getTxReceipt(mockTxHash);
265
+
266
+ expect(provider.call).toHaveBeenCalledWith(testChainId, {
267
+ method: 'aztec_getTxReceipt',
268
+ params: { txHash: mockTxHash },
269
+ });
270
+ expect(result).toEqual(receipt);
271
+ });
272
+
273
+ it('should call aztec_getCurrentBaseFees', async () => {
274
+ const fees = { gasPrice: 100n };
275
+ provider.call.mockResolvedValue(fees);
276
+
277
+ const result = await wallet.getCurrentBaseFees();
278
+
279
+ expect(provider.call).toHaveBeenCalledWith(testChainId, {
280
+ method: 'aztec_getCurrentBaseFees',
281
+ });
282
+ expect(result).toEqual(fees);
283
+ });
284
+ });
285
+
286
+ describe('Block methods', () => {
287
+ beforeEach(async () => {
288
+ const mockChainIdFr = new Fr(31337n);
289
+ const mockVersionFr = new Fr(1n);
290
+
291
+ provider.call.mockImplementation(async (_chainId: string, { method }: { method: string }) => {
292
+ switch (method) {
293
+ case 'aztec_getCompleteAddress':
294
+ return mockCompleteAddress;
295
+ case 'aztec_getChainId':
296
+ return mockChainIdFr;
297
+ case 'aztec_getVersion':
298
+ return mockVersionFr;
299
+ default:
300
+ return null;
301
+ }
302
+ });
303
+ await wallet.initialize();
304
+ provider.call.mockClear();
305
+ });
306
+
307
+ it('should call aztec_getBlock', async () => {
308
+ const block = { blockNumber: 1 };
309
+ provider.call.mockResolvedValue(block);
310
+
311
+ const result = await wallet.getBlock(1);
312
+
313
+ expect(provider.call).toHaveBeenCalledWith(testChainId, {
314
+ method: 'aztec_getBlock',
315
+ params: { number: 1 },
316
+ });
317
+ expect(result).toEqual(block);
318
+ });
319
+
320
+ it('should call aztec_getBlockNumber', async () => {
321
+ provider.call.mockResolvedValue(100);
322
+
323
+ const result = await wallet.getBlockNumber();
324
+
325
+ expect(provider.call).toHaveBeenCalledWith(testChainId, {
326
+ method: 'aztec_getBlockNumber',
327
+ });
328
+ expect(result).toBe(100);
329
+ });
330
+ });
331
+
332
+ describe('Contract methods', () => {
333
+ beforeEach(async () => {
334
+ const mockChainIdFr = new Fr(31337n);
335
+ const mockVersionFr = new Fr(1n);
336
+
337
+ provider.call.mockImplementation(async (_chainId: string, { method }: { method: string }) => {
338
+ switch (method) {
339
+ case 'aztec_getCompleteAddress':
340
+ return mockCompleteAddress;
341
+ case 'aztec_getChainId':
342
+ return mockChainIdFr;
343
+ case 'aztec_getVersion':
344
+ return mockVersionFr;
345
+ default:
346
+ return null;
347
+ }
348
+ });
349
+ await wallet.initialize();
350
+ provider.call.mockClear();
351
+ });
352
+
353
+ it('should call aztec_registerContract', async () => {
354
+ const contract = { instance: { address: mockAddress } } as { instance: ContractInstanceWithAddress };
355
+ provider.call.mockResolvedValue(undefined);
356
+
357
+ await wallet.registerContract(contract);
358
+
359
+ expect(provider.call).toHaveBeenCalledWith(testChainId, {
360
+ method: 'aztec_registerContract',
361
+ params: { instance: contract.instance },
362
+ });
363
+ });
364
+
365
+ it('should call aztec_registerContractClass', async () => {
366
+ const artifact = { name: 'TestContract' } as ContractArtifact;
367
+ provider.call.mockResolvedValue(undefined);
368
+
369
+ await wallet.registerContractClass(artifact);
370
+
371
+ expect(provider.call).toHaveBeenCalledWith(testChainId, {
372
+ method: 'aztec_registerContractClass',
373
+ params: { artifact },
374
+ });
375
+ });
376
+
377
+ it('should call aztec_getContracts', async () => {
378
+ const contracts: unknown[] = [];
379
+ provider.call.mockResolvedValue(contracts);
380
+
381
+ const result = await wallet.getContracts();
382
+
383
+ expect(provider.call).toHaveBeenCalledWith(testChainId, {
384
+ method: 'aztec_getContracts',
385
+ });
386
+ expect(result).toEqual(contracts);
387
+ });
388
+ });
389
+
390
+ describe('Node info methods', () => {
391
+ beforeEach(async () => {
392
+ const mockChainIdFr = new Fr(31337n);
393
+ const mockVersionFr = new Fr(1n);
394
+
395
+ provider.call.mockImplementation(async (_chainId: string, { method }: { method: string }) => {
396
+ switch (method) {
397
+ case 'aztec_getCompleteAddress':
398
+ return mockCompleteAddress;
399
+ case 'aztec_getChainId':
400
+ return mockChainIdFr;
401
+ case 'aztec_getVersion':
402
+ return mockVersionFr;
403
+ default:
404
+ return null;
405
+ }
406
+ });
407
+ await wallet.initialize();
408
+ provider.call.mockClear();
409
+ });
410
+
411
+ it('should call aztec_getNodeInfo', async () => {
412
+ const nodeInfo = { version: '1.0.0' };
413
+ provider.call.mockResolvedValue(nodeInfo);
414
+
415
+ const result = await wallet.getNodeInfo();
416
+
417
+ expect(provider.call).toHaveBeenCalledWith(testChainId, {
418
+ method: 'aztec_getNodeInfo',
419
+ });
420
+ expect(result).toEqual(nodeInfo);
421
+ });
422
+
423
+ it('should return cached chainId', () => {
424
+ // chainId should already be cached from initialization
425
+ const result = wallet.getChainId();
426
+ expect(result.toBigInt()).toBe(31337n);
427
+ });
428
+
429
+ it('should call aztec_getChainId async', async () => {
430
+ const mockChainIdFr = new Fr(31337n);
431
+ provider.call.mockResolvedValue(mockChainIdFr);
432
+
433
+ const result = await wallet.getChainIdAsync();
434
+
435
+ expect(provider.call).toHaveBeenCalledWith(testChainId, {
436
+ method: 'aztec_getChainId',
437
+ });
438
+ expect(result).toEqual(mockChainIdFr);
439
+ });
440
+
441
+ it('should return cached version', () => {
442
+ // version should already be cached from initialization
443
+ const result = wallet.getVersion();
444
+ expect(result.toBigInt()).toBe(1n);
445
+ });
446
+
447
+ it('should call aztec_getVersion async', async () => {
448
+ const mockVersionFr = new Fr(1n);
449
+ provider.call.mockResolvedValue(mockVersionFr);
450
+
451
+ const result = await wallet.getVersionAsync();
452
+
453
+ expect(provider.call).toHaveBeenCalledWith(testChainId, {
454
+ method: 'aztec_getVersion',
455
+ });
456
+ expect(result).toEqual(mockVersionFr);
457
+ });
458
+ });
459
+
460
+ describe('Sender methods', () => {
461
+ beforeEach(async () => {
462
+ const mockChainIdFr = new Fr(31337n);
463
+ const mockVersionFr = new Fr(1n);
464
+
465
+ provider.call.mockImplementation(async (_chainId: string, { method }: { method: string }) => {
466
+ switch (method) {
467
+ case 'aztec_getCompleteAddress':
468
+ return mockCompleteAddress;
469
+ case 'aztec_getChainId':
470
+ return mockChainIdFr;
471
+ case 'aztec_getVersion':
472
+ return mockVersionFr;
473
+ default:
474
+ return null;
475
+ }
476
+ });
477
+ await wallet.initialize();
478
+ provider.call.mockClear();
479
+ });
480
+
481
+ it('should call aztec_registerSender', async () => {
482
+ const senderAddress = await AztecAddress.random();
483
+ // registerSender returns the passed address after registration
484
+ provider.call.mockResolvedValue(undefined);
485
+
486
+ const result = await wallet.registerSender(senderAddress);
487
+
488
+ expect(provider.call).toHaveBeenCalledWith(testChainId, {
489
+ method: 'aztec_registerSender',
490
+ params: { sender: senderAddress },
491
+ });
492
+ expect(result).toEqual(senderAddress);
493
+ });
494
+
495
+ it('should call aztec_getSenders', async () => {
496
+ const senders = [mockAddress];
497
+ provider.call.mockResolvedValue(senders);
498
+
499
+ const result = await wallet.getSenders();
500
+
501
+ expect(provider.call).toHaveBeenCalledWith(testChainId, {
502
+ method: 'aztec_getSenders',
503
+ });
504
+ expect(result).toEqual(senders);
505
+ });
506
+
507
+ it('should call aztec_removeSender', async () => {
508
+ provider.call.mockResolvedValue(undefined);
509
+
510
+ await wallet.removeSender(mockAddress);
511
+
512
+ expect(provider.call).toHaveBeenCalledWith(testChainId, {
513
+ method: 'aztec_removeSender',
514
+ params: { sender: mockAddress },
515
+ });
516
+ });
517
+ });
518
+
519
+ describe('Additional methods', () => {
520
+ beforeEach(async () => {
521
+ const mockChainIdFr = new Fr(31337n);
522
+ const mockVersionFr = new Fr(1n);
523
+
524
+ provider.call.mockImplementation(async (_chainId: string, { method }: { method: string }) => {
525
+ switch (method) {
526
+ case 'aztec_getCompleteAddress':
527
+ return mockCompleteAddress;
528
+ case 'aztec_getChainId':
529
+ return mockChainIdFr;
530
+ case 'aztec_getVersion':
531
+ return mockVersionFr;
532
+ default:
533
+ return null;
534
+ }
535
+ });
536
+ await wallet.initialize();
537
+ provider.call.mockClear();
538
+ });
539
+
540
+ it('should call aztec_simulateTx', async () => {
541
+ const txRequest = { request: {} } as unknown as TxExecutionRequest;
542
+ const simulatedTx = { tx: txRequest, privateReturnValues: [] };
543
+ const msgSender = await AztecAddress.random();
544
+ provider.call.mockResolvedValue(simulatedTx);
545
+
546
+ const result = await wallet.simulateTx(txRequest, true, msgSender);
547
+
548
+ expect(provider.call).toHaveBeenCalledWith(testChainId, {
549
+ method: 'aztec_simulateTx',
550
+ params: {
551
+ txRequest,
552
+ simulatePublic: true,
553
+ msgSender,
554
+ },
555
+ });
556
+ expect(result).toEqual(simulatedTx);
557
+ });
558
+
559
+ it('should call aztec_getPXEInfo', async () => {
560
+ const pxeInfo = { version: '1.0.0' };
561
+ provider.call.mockResolvedValue(pxeInfo);
562
+
563
+ const result = await wallet.getPXEInfo();
564
+
565
+ expect(provider.call).toHaveBeenCalledWith(testChainId, {
566
+ method: 'aztec_getPXEInfo',
567
+ });
568
+ expect(result).toEqual(pxeInfo);
569
+ });
570
+ });
571
+
572
+ describe('createAztecWallet', () => {
573
+ it('should create and initialize a wallet with default chainId', async () => {
574
+ const mockChainIdFr = new Fr(31337n);
575
+ const mockVersionFr = new Fr(1n);
576
+
577
+ provider.call.mockImplementation(async (chainId: string, { method }: { method: string }) => {
578
+ expect(chainId).toBe('aztec:mainnet'); // default chainId
579
+ switch (method) {
580
+ case 'aztec_getCompleteAddress':
581
+ return mockCompleteAddress;
582
+ case 'aztec_getChainId':
583
+ return mockChainIdFr;
584
+ case 'aztec_getVersion':
585
+ return mockVersionFr;
586
+ default:
587
+ throw new Error(`Unexpected method: ${method}`);
588
+ }
589
+ });
590
+
591
+ const wallet = await createAztecWallet(provider);
592
+
593
+ expect(wallet).toBeInstanceOf(AztecDappWallet);
594
+ expect(wallet.getAddress()).toEqual(mockAddress);
595
+ expect(wallet.getCompleteAddress()).toEqual(mockCompleteAddress);
596
+ expect(wallet.getChainId()).toEqual(mockChainIdFr);
597
+ expect(wallet.getVersion()).toEqual(mockVersionFr);
598
+ });
599
+
600
+ it('should create and initialize a wallet with custom chainId', async () => {
601
+ const mockChainIdFr = new Fr(31337n);
602
+ const mockVersionFr = new Fr(1n);
603
+ const customChainId = 'aztec:custom';
604
+
605
+ provider.call.mockImplementation(async (chainId: string, { method }: { method: string }) => {
606
+ expect(chainId).toBe(customChainId);
607
+ switch (method) {
608
+ case 'aztec_getCompleteAddress':
609
+ return mockCompleteAddress;
610
+ case 'aztec_getChainId':
611
+ return mockChainIdFr;
612
+ case 'aztec_getVersion':
613
+ return mockVersionFr;
614
+ default:
615
+ throw new Error(`Unexpected method: ${method}`);
616
+ }
617
+ });
618
+
619
+ const wallet = await createAztecWallet(provider, customChainId);
620
+
621
+ expect(wallet).toBeInstanceOf(AztecDappWallet);
622
+ expect(wallet.getAddress()).toEqual(mockAddress);
623
+ expect(wallet.getCompleteAddress()).toEqual(mockCompleteAddress);
624
+ expect(wallet.getChainId()).toEqual(mockChainIdFr);
625
+ expect(wallet.getVersion()).toEqual(mockVersionFr);
626
+ });
627
+ });
628
+ });