@aztec/wallet-sdk 0.0.1-commit.96bb3f7 → 0.0.1-commit.993d240

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 (101) hide show
  1. package/README.md +218 -355
  2. package/dest/base-wallet/base_wallet.d.ts +109 -40
  3. package/dest/base-wallet/base_wallet.d.ts.map +1 -1
  4. package/dest/base-wallet/base_wallet.js +286 -71
  5. package/dest/base-wallet/index.d.ts +3 -2
  6. package/dest/base-wallet/index.d.ts.map +1 -1
  7. package/dest/base-wallet/index.js +1 -0
  8. package/dest/base-wallet/utils.d.ts +52 -0
  9. package/dest/base-wallet/utils.d.ts.map +1 -0
  10. package/dest/base-wallet/utils.js +137 -0
  11. package/dest/crypto.d.ts +111 -27
  12. package/dest/crypto.d.ts.map +1 -1
  13. package/dest/crypto.js +307 -41
  14. package/dest/emoji_alphabet.d.ts +35 -0
  15. package/dest/emoji_alphabet.d.ts.map +1 -0
  16. package/dest/emoji_alphabet.js +299 -0
  17. package/dest/extension/handlers/background_connection_handler.d.ts +168 -0
  18. package/dest/extension/handlers/background_connection_handler.d.ts.map +1 -0
  19. package/dest/extension/handlers/background_connection_handler.js +294 -0
  20. package/dest/extension/handlers/content_script_connection_handler.d.ts +57 -0
  21. package/dest/extension/handlers/content_script_connection_handler.d.ts.map +1 -0
  22. package/dest/extension/handlers/content_script_connection_handler.js +193 -0
  23. package/dest/extension/handlers/index.d.ts +12 -0
  24. package/dest/extension/handlers/index.d.ts.map +1 -0
  25. package/dest/extension/handlers/index.js +10 -0
  26. package/dest/extension/handlers/internal_message_types.d.ts +65 -0
  27. package/dest/extension/handlers/internal_message_types.d.ts.map +1 -0
  28. package/dest/extension/handlers/internal_message_types.js +24 -0
  29. package/dest/extension/provider/extension_provider.d.ts +107 -0
  30. package/dest/extension/provider/extension_provider.d.ts.map +1 -0
  31. package/dest/extension/provider/extension_provider.js +160 -0
  32. package/dest/extension/provider/extension_wallet.d.ts +152 -0
  33. package/dest/extension/provider/extension_wallet.d.ts.map +1 -0
  34. package/dest/extension/provider/extension_wallet.js +349 -0
  35. package/dest/extension/provider/index.d.ts +3 -0
  36. package/dest/extension/provider/index.d.ts.map +1 -0
  37. package/dest/{providers/extension → extension/provider}/index.js +0 -1
  38. package/dest/iframe/handlers/iframe_connection_handler.d.ts +122 -0
  39. package/dest/iframe/handlers/iframe_connection_handler.d.ts.map +1 -0
  40. package/dest/iframe/handlers/iframe_connection_handler.js +239 -0
  41. package/dest/iframe/handlers/index.d.ts +2 -0
  42. package/dest/iframe/handlers/index.d.ts.map +1 -0
  43. package/dest/iframe/handlers/index.js +1 -0
  44. package/dest/iframe/provider/iframe_discovery.d.ts +25 -0
  45. package/dest/iframe/provider/iframe_discovery.d.ts.map +1 -0
  46. package/dest/iframe/provider/iframe_discovery.js +167 -0
  47. package/dest/iframe/provider/iframe_provider.d.ts +65 -0
  48. package/dest/iframe/provider/iframe_provider.d.ts.map +1 -0
  49. package/dest/iframe/provider/iframe_provider.js +257 -0
  50. package/dest/iframe/provider/iframe_wallet.d.ts +85 -0
  51. package/dest/iframe/provider/iframe_wallet.d.ts.map +1 -0
  52. package/dest/iframe/provider/iframe_wallet.js +269 -0
  53. package/dest/iframe/provider/index.d.ts +4 -0
  54. package/dest/iframe/provider/index.d.ts.map +1 -0
  55. package/dest/iframe/provider/index.js +3 -0
  56. package/dest/manager/index.d.ts +2 -7
  57. package/dest/manager/index.d.ts.map +1 -1
  58. package/dest/manager/index.js +0 -4
  59. package/dest/manager/types.d.ts +109 -5
  60. package/dest/manager/types.d.ts.map +1 -1
  61. package/dest/manager/types.js +17 -1
  62. package/dest/manager/wallet_manager.d.ts +50 -7
  63. package/dest/manager/wallet_manager.d.ts.map +1 -1
  64. package/dest/manager/wallet_manager.js +208 -29
  65. package/dest/types.d.ts +117 -15
  66. package/dest/types.d.ts.map +1 -1
  67. package/dest/types.js +39 -2
  68. package/package.json +22 -11
  69. package/src/base-wallet/base_wallet.ts +395 -128
  70. package/src/base-wallet/index.ts +7 -1
  71. package/src/base-wallet/utils.ts +248 -0
  72. package/src/crypto.ts +367 -47
  73. package/src/emoji_alphabet.ts +317 -0
  74. package/src/extension/handlers/background_connection_handler.ts +456 -0
  75. package/src/extension/handlers/content_script_connection_handler.ts +264 -0
  76. package/src/extension/handlers/index.ts +25 -0
  77. package/src/extension/handlers/internal_message_types.ts +71 -0
  78. package/src/extension/provider/extension_provider.ts +233 -0
  79. package/src/extension/provider/extension_wallet.ts +410 -0
  80. package/src/extension/provider/index.ts +7 -0
  81. package/src/iframe/handlers/iframe_connection_handler.ts +341 -0
  82. package/src/iframe/handlers/index.ts +7 -0
  83. package/src/iframe/provider/iframe_discovery.ts +185 -0
  84. package/src/iframe/provider/iframe_provider.ts +331 -0
  85. package/src/iframe/provider/iframe_wallet.ts +323 -0
  86. package/src/iframe/provider/index.ts +3 -0
  87. package/src/manager/index.ts +3 -9
  88. package/src/manager/types.ts +113 -4
  89. package/src/manager/wallet_manager.ts +235 -30
  90. package/src/types.ts +129 -14
  91. package/dest/providers/extension/extension_provider.d.ts +0 -17
  92. package/dest/providers/extension/extension_provider.d.ts.map +0 -1
  93. package/dest/providers/extension/extension_provider.js +0 -56
  94. package/dest/providers/extension/extension_wallet.d.ts +0 -95
  95. package/dest/providers/extension/extension_wallet.d.ts.map +0 -1
  96. package/dest/providers/extension/extension_wallet.js +0 -225
  97. package/dest/providers/extension/index.d.ts +0 -5
  98. package/dest/providers/extension/index.d.ts.map +0 -1
  99. package/src/providers/extension/extension_provider.ts +0 -72
  100. package/src/providers/extension/extension_wallet.ts +0 -275
  101. package/src/providers/extension/index.ts +0 -11
@@ -1,275 +0,0 @@
1
- import type { ChainInfo } from '@aztec/aztec.js/account';
2
- import { type Wallet, WalletSchema } from '@aztec/aztec.js/wallet';
3
- import { jsonStringify } from '@aztec/foundation/json-rpc';
4
- import { type PromiseWithResolvers, promiseWithResolvers } from '@aztec/foundation/promise';
5
- import { schemaHasMethod } from '@aztec/foundation/schemas';
6
- import type { FunctionsOf } from '@aztec/foundation/types';
7
-
8
- import {
9
- type EncryptedPayload,
10
- type ExportedPublicKey,
11
- decrypt,
12
- deriveSharedKey,
13
- encrypt,
14
- exportPublicKey,
15
- generateKeyPair,
16
- importPublicKey,
17
- } from '../../crypto.js';
18
- import type { ConnectRequest, WalletInfo, WalletMessage, WalletResponse } from '../../types.js';
19
-
20
- /**
21
- * Internal type representing a wallet method call before encryption.
22
- * @internal
23
- */
24
- type WalletMethodCall = {
25
- /** The wallet method name to invoke */
26
- type: keyof FunctionsOf<Wallet>;
27
- /** Arguments to pass to the wallet method */
28
- args: unknown[];
29
- };
30
-
31
- /**
32
- * A wallet implementation that communicates with browser extension wallets
33
- * using a secure encrypted MessageChannel.
34
- *
35
- * This class establishes a private communication channel with a wallet extension
36
- * using the following security mechanisms:
37
- *
38
- * 1. **MessageChannel**: Creates a private communication channel that is not
39
- * visible to other scripts on the page (unlike window.postMessage).
40
- *
41
- * 2. **ECDH Key Exchange**: Uses Elliptic Curve Diffie-Hellman to derive a
42
- * shared secret between the dApp and wallet without transmitting private keys.
43
- *
44
- * 3. **AES-GCM Encryption**: All messages after channel establishment are
45
- * encrypted using AES-256-GCM, providing both confidentiality and authenticity.
46
- *
47
- * @example
48
- * ```typescript
49
- * // Discovery returns wallet info including the wallet's public key
50
- * const wallets = await ExtensionProvider.discoverExtensions(chainInfo);
51
- * const walletInfo = wallets[0];
52
- *
53
- * // Create a secure connection to the wallet
54
- * const wallet = await ExtensionWallet.create(walletInfo, chainInfo, 'my-dapp');
55
- *
56
- * // All subsequent calls are encrypted
57
- * const accounts = await wallet.getAccounts();
58
- * ```
59
- */
60
- export class ExtensionWallet {
61
- /** Map of pending requests awaiting responses, keyed by message ID */
62
- private inFlight = new Map<string, PromiseWithResolvers<unknown>>();
63
-
64
- /** The MessagePort for private communication with the extension */
65
- private port: MessagePort | null = null;
66
-
67
- /** The derived AES-GCM key for encrypting/decrypting messages */
68
- private sharedKey: CryptoKey | null = null;
69
-
70
- /**
71
- * Private constructor - use {@link ExtensionWallet.create} to instantiate.
72
- * @param chainInfo - The chain information (chainId and version)
73
- * @param appId - Application identifier for the requesting dApp
74
- * @param extensionId - The unique identifier of the target wallet extension
75
- */
76
- private constructor(
77
- private chainInfo: ChainInfo,
78
- private appId: string,
79
- private extensionId: string,
80
- ) {}
81
-
82
- /**
83
- * Creates an ExtensionWallet instance that proxies wallet calls to a browser extension
84
- * over a secure encrypted MessageChannel.
85
- *
86
- * The connection process:
87
- * 1. Generates an ECDH key pair for this session
88
- * 2. Derives a shared AES-256 key using the wallet's public key
89
- * 3. Creates a MessageChannel and transfers one port to the extension
90
- * 4. Returns a Proxy that encrypts all wallet method calls
91
- *
92
- * @param walletInfo - The discovered wallet information, including the wallet's ECDH public key
93
- * @param chainInfo - The chain information (chainId and version) for request context
94
- * @param appId - Application identifier used to identify the requesting dApp to the wallet
95
- * @returns A Promise resolving to a Wallet implementation that encrypts all communication
96
- *
97
- * @throws Error if the secure channel cannot be established
98
- *
99
- * @example
100
- * ```typescript
101
- * const wallet = await ExtensionWallet.create(
102
- * walletInfo,
103
- * { chainId: Fr(31337), version: Fr(0) },
104
- * 'my-defi-app'
105
- * );
106
- * ```
107
- */
108
- static async create(walletInfo: WalletInfo, chainInfo: ChainInfo, appId: string): Promise<Wallet> {
109
- const wallet = new ExtensionWallet(chainInfo, appId, walletInfo.id);
110
-
111
- if (!walletInfo.publicKey) {
112
- throw new Error('Wallet does not support secure channel establishment (missing public key)');
113
- }
114
-
115
- await wallet.establishSecureChannel(walletInfo.publicKey);
116
-
117
- // Create a Proxy that intercepts wallet method calls and forwards them to the extension
118
- return new Proxy(wallet, {
119
- get: (target, prop) => {
120
- if (schemaHasMethod(WalletSchema, prop.toString())) {
121
- return async (...args: unknown[]) => {
122
- const result = await target.postMessage({
123
- type: prop.toString() as keyof FunctionsOf<Wallet>,
124
- args,
125
- });
126
- return WalletSchema[prop.toString() as keyof typeof WalletSchema].returnType().parseAsync(result);
127
- };
128
- } else {
129
- return target[prop as keyof ExtensionWallet];
130
- }
131
- },
132
- }) as unknown as Wallet;
133
- }
134
-
135
- /**
136
- * Establishes a secure MessageChannel with ECDH key exchange.
137
- *
138
- * This method performs the cryptographic handshake:
139
- * 1. Generates a new ECDH P-256 key pair for this session
140
- * 2. Imports the wallet's public key and derives a shared secret
141
- * 3. Creates a MessageChannel for private communication
142
- * 4. Sends a connection request with our public key via window.postMessage
143
- * (this is the only public message - subsequent communication uses the private channel)
144
- *
145
- * @param walletExportedPublicKey - The wallet's ECDH public key in JWK format
146
- */
147
- private async establishSecureChannel(walletExportedPublicKey: ExportedPublicKey): Promise<void> {
148
- const keyPair = await generateKeyPair();
149
- const exportedPublicKey = await exportPublicKey(keyPair.publicKey);
150
-
151
- const walletPublicKey = await importPublicKey(walletExportedPublicKey);
152
- this.sharedKey = await deriveSharedKey(keyPair.privateKey, walletPublicKey);
153
-
154
- const channel = new MessageChannel();
155
- this.port = channel.port1;
156
-
157
- this.port.onmessage = async (event: MessageEvent<EncryptedPayload>) => {
158
- await this.handleEncryptedResponse(event.data);
159
- };
160
-
161
- this.port.start();
162
-
163
- // Send connection request with our public key and transfer port2 to content script
164
- // This is the only public postMessage - it contains our public key (safe to expose)
165
- // and transfers the MessagePort for subsequent private communication
166
- const connectRequest: ConnectRequest = {
167
- type: 'aztec-wallet-connect',
168
- walletId: this.extensionId,
169
- appId: this.appId,
170
- publicKey: exportedPublicKey,
171
- };
172
-
173
- window.postMessage(jsonStringify(connectRequest), '*', [channel.port2]);
174
- }
175
-
176
- /**
177
- * Handles an encrypted response received from the wallet extension.
178
- *
179
- * Decrypts the response using the shared AES key and resolves or rejects
180
- * the corresponding pending promise based on the response content.
181
- *
182
- * @param encrypted - The encrypted response from the wallet
183
- */
184
- private async handleEncryptedResponse(encrypted: EncryptedPayload): Promise<void> {
185
- if (!this.sharedKey) {
186
- return;
187
- }
188
-
189
- try {
190
- const response = await decrypt<WalletResponse>(this.sharedKey, encrypted);
191
-
192
- const { messageId, result, error, walletId: responseWalletId } = response;
193
-
194
- if (!messageId || !responseWalletId) {
195
- return;
196
- }
197
-
198
- if (this.extensionId !== responseWalletId) {
199
- return;
200
- }
201
-
202
- if (!this.inFlight.has(messageId)) {
203
- return;
204
- }
205
-
206
- const { resolve, reject } = this.inFlight.get(messageId)!;
207
-
208
- if (error) {
209
- reject(new Error(jsonStringify(error)));
210
- } else {
211
- resolve(result);
212
- }
213
- this.inFlight.delete(messageId);
214
- // eslint-disable-next-line no-empty
215
- } catch {}
216
- }
217
-
218
- /**
219
- * Sends an encrypted wallet method call over the secure MessageChannel.
220
- *
221
- * The message is encrypted using AES-256-GCM with the shared key derived
222
- * during channel establishment. A unique message ID is generated to correlate
223
- * the response.
224
- *
225
- * @param call - The wallet method call containing method name and arguments
226
- * @returns A Promise that resolves with the decrypted result from the wallet
227
- *
228
- * @throws Error if the secure channel has not been established
229
- */
230
- private async postMessage(call: WalletMethodCall): Promise<unknown> {
231
- if (!this.port || !this.sharedKey) {
232
- throw new Error('Secure channel not established');
233
- }
234
-
235
- const messageId = globalThis.crypto.randomUUID();
236
- const message: WalletMessage = {
237
- type: call.type,
238
- args: call.args,
239
- messageId,
240
- chainInfo: this.chainInfo,
241
- appId: this.appId,
242
- walletId: this.extensionId,
243
- };
244
-
245
- // Encrypt the message and send over the private MessageChannel
246
- const encrypted = await encrypt(this.sharedKey, message);
247
- this.port.postMessage(encrypted);
248
-
249
- const { promise, resolve, reject } = promiseWithResolvers<unknown>();
250
- this.inFlight.set(messageId, { promise, resolve, reject });
251
- return promise;
252
- }
253
-
254
- /**
255
- * Closes the secure channel and cleans up resources.
256
- *
257
- * After calling this method, the wallet instance can no longer be used.
258
- * Any pending requests will not receive responses.
259
- *
260
- * @example
261
- * ```typescript
262
- * const wallet = await ExtensionWallet.create(walletInfo, chainInfo, 'my-app');
263
- * // ... use wallet ...
264
- * wallet.close(); // Clean up when done
265
- * ```
266
- */
267
- close(): void {
268
- if (this.port) {
269
- this.port.close();
270
- this.port = null;
271
- }
272
- this.sharedKey = null;
273
- this.inFlight.clear();
274
- }
275
- }
@@ -1,11 +0,0 @@
1
- export { ExtensionWallet } from './extension_wallet.js';
2
- export { ExtensionProvider } from './extension_provider.js';
3
- export * from '../../crypto.js';
4
- export type {
5
- WalletInfo,
6
- WalletMessage,
7
- WalletResponse,
8
- DiscoveryRequest,
9
- DiscoveryResponse,
10
- ConnectRequest,
11
- } from '../../types.js';