@aztec/wallet-sdk 4.0.0-nightly.20260107 → 4.0.0-nightly.20260110

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 (33) hide show
  1. package/README.md +172 -138
  2. package/dest/base-wallet/base_wallet.d.ts +2 -2
  3. package/dest/base-wallet/base_wallet.d.ts.map +1 -1
  4. package/dest/base-wallet/base_wallet.js +3 -3
  5. package/dest/crypto.d.ts +146 -0
  6. package/dest/crypto.d.ts.map +1 -0
  7. package/dest/crypto.js +216 -0
  8. package/dest/manager/index.d.ts +2 -2
  9. package/dest/manager/index.d.ts.map +1 -1
  10. package/dest/manager/wallet_manager.js +1 -1
  11. package/dest/providers/extension/extension_provider.d.ts +2 -2
  12. package/dest/providers/extension/extension_provider.d.ts.map +1 -1
  13. package/dest/providers/extension/extension_wallet.d.ts +79 -7
  14. package/dest/providers/extension/extension_wallet.d.ts.map +1 -1
  15. package/dest/providers/extension/extension_wallet.js +173 -44
  16. package/dest/providers/extension/index.d.ts +3 -2
  17. package/dest/providers/extension/index.d.ts.map +1 -1
  18. package/dest/providers/extension/index.js +1 -0
  19. package/dest/types.d.ts +83 -0
  20. package/dest/types.d.ts.map +1 -0
  21. package/dest/types.js +3 -0
  22. package/package.json +10 -8
  23. package/src/base-wallet/base_wallet.ts +2 -2
  24. package/src/crypto.ts +283 -0
  25. package/src/manager/index.ts +1 -7
  26. package/src/manager/wallet_manager.ts +1 -1
  27. package/src/providers/extension/extension_provider.ts +1 -1
  28. package/src/providers/extension/extension_wallet.ts +206 -55
  29. package/src/providers/extension/index.ts +9 -1
  30. package/src/{providers/types.ts → types.ts} +22 -4
  31. package/dest/providers/types.d.ts +0 -67
  32. package/dest/providers/types.d.ts.map +0 -1
  33. package/dest/providers/types.js +0 -3
@@ -0,0 +1,83 @@
1
+ import type { ChainInfo } from '@aztec/aztec.js/account';
2
+ import type { ExportedPublicKey } from './crypto.js';
3
+ /**
4
+ * Information about an installed Aztec wallet
5
+ */
6
+ export interface WalletInfo {
7
+ /** Unique identifier for the wallet */
8
+ id: string;
9
+ /** Display name of the wallet */
10
+ name: string;
11
+ /** URL to the wallet's icon */
12
+ icon?: string;
13
+ /** Wallet version */
14
+ version: string;
15
+ /** Wallet's ECDH public key for secure channel establishment */
16
+ publicKey: ExportedPublicKey;
17
+ }
18
+ /**
19
+ * Message format for wallet communication (internal, before encryption)
20
+ */
21
+ export interface WalletMessage {
22
+ /** Unique message ID for tracking responses */
23
+ messageId: string;
24
+ /** The wallet method to call */
25
+ type: string;
26
+ /** Arguments for the method */
27
+ args: unknown[];
28
+ /** Chain information */
29
+ chainInfo: ChainInfo;
30
+ /** Application ID making the request */
31
+ appId: string;
32
+ /** Wallet ID to target a specific wallet */
33
+ walletId: string;
34
+ }
35
+ /**
36
+ * Response message from wallet
37
+ */
38
+ export interface WalletResponse {
39
+ /** Message ID matching the request */
40
+ messageId: string;
41
+ /** Result data (if successful) */
42
+ result?: unknown;
43
+ /** Error data (if failed) */
44
+ error?: unknown;
45
+ /** Wallet ID that sent the response */
46
+ walletId: string;
47
+ }
48
+ /**
49
+ * Discovery message for finding installed wallets (public, unencrypted)
50
+ */
51
+ export interface DiscoveryRequest {
52
+ /** Message type for discovery */
53
+ type: 'aztec-wallet-discovery';
54
+ /** Request ID */
55
+ requestId: string;
56
+ /** Chain information to check if wallet supports this network */
57
+ chainInfo: ChainInfo;
58
+ }
59
+ /**
60
+ * Discovery response from a wallet (public, unencrypted)
61
+ */
62
+ export interface DiscoveryResponse {
63
+ /** Message type for discovery response */
64
+ type: 'aztec-wallet-discovery-response';
65
+ /** Request ID matching the discovery request */
66
+ requestId: string;
67
+ /** Wallet information */
68
+ walletInfo: WalletInfo;
69
+ }
70
+ /**
71
+ * Connection request to establish secure channel
72
+ */
73
+ export interface ConnectRequest {
74
+ /** Message type for connection */
75
+ type: 'aztec-wallet-connect';
76
+ /** Target wallet ID */
77
+ walletId: string;
78
+ /** Application ID */
79
+ appId: string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;IACzB,UAAU,EAAE,UAAU,CAAC;CACxB;AAED;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B,kCAAkC;IAClC,IAAI,EAAE,sBAAsB,CAAC;IAC7B,uBAAuB;IACvB,QAAQ,EAAE,MAAM,CAAC;IACjB,qBAAqB;IACrB,KAAK,EAAE,MAAM,CAAC;IACd,wDAAwD;IACxD,SAAS,EAAE,iBAAiB,CAAC;CAC9B"}
package/dest/types.js ADDED
@@ -0,0 +1,3 @@
1
+ /**
2
+ * Connection request to establish secure channel
3
+ */ export { };
package/package.json CHANGED
@@ -1,11 +1,13 @@
1
1
  {
2
2
  "name": "@aztec/wallet-sdk",
3
3
  "homepage": "https://github.com/AztecProtocol/aztec-packages/tree/master/yarn-project/wallet-sdk",
4
- "version": "4.0.0-nightly.20260107",
4
+ "version": "4.0.0-nightly.20260110",
5
5
  "type": "module",
6
6
  "exports": {
7
7
  "./base-wallet": "./dest/base-wallet/index.js",
8
8
  "./providers/extension": "./dest/providers/extension/index.js",
9
+ "./crypto": "./dest/crypto.js",
10
+ "./types": "./dest/types.js",
9
11
  "./manager": "./dest/manager/index.js"
10
12
  },
11
13
  "typedocOptions": {
@@ -62,15 +64,15 @@
62
64
  ]
63
65
  },
64
66
  "dependencies": {
65
- "@aztec/aztec.js": "4.0.0-nightly.20260107",
66
- "@aztec/constants": "4.0.0-nightly.20260107",
67
- "@aztec/entrypoints": "4.0.0-nightly.20260107",
68
- "@aztec/foundation": "4.0.0-nightly.20260107",
69
- "@aztec/pxe": "4.0.0-nightly.20260107",
70
- "@aztec/stdlib": "4.0.0-nightly.20260107"
67
+ "@aztec/aztec.js": "4.0.0-nightly.20260110",
68
+ "@aztec/constants": "4.0.0-nightly.20260110",
69
+ "@aztec/entrypoints": "4.0.0-nightly.20260110",
70
+ "@aztec/foundation": "4.0.0-nightly.20260110",
71
+ "@aztec/pxe": "4.0.0-nightly.20260110",
72
+ "@aztec/stdlib": "4.0.0-nightly.20260110"
71
73
  },
72
74
  "devDependencies": {
73
- "@aztec/noir-contracts.js": "4.0.0-nightly.20260107",
75
+ "@aztec/noir-contracts.js": "4.0.0-nightly.20260110",
74
76
  "@jest/globals": "^30.0.0",
75
77
  "@types/jest": "^30.0.0",
76
78
  "@types/node": "^22.15.17",
@@ -76,7 +76,7 @@ export type FeeOptions = {
76
76
  export abstract class BaseWallet implements Wallet {
77
77
  protected log = createLogger('wallet-sdk:base_wallet');
78
78
 
79
- protected baseFeePadding = 0.5;
79
+ protected minFeePadding = 0.5;
80
80
  protected cancellableTransactions = false;
81
81
 
82
82
  // Protected because we want to force wallets to instantiate their own PXE.
@@ -165,7 +165,7 @@ export abstract class BaseWallet implements Wallet {
165
165
  gasSettings?: Partial<FieldsOf<GasSettings>>,
166
166
  ): Promise<FeeOptions> {
167
167
  const maxFeesPerGas =
168
- gasSettings?.maxFeesPerGas ?? (await this.aztecNode.getCurrentBaseFees()).mul(1 + this.baseFeePadding);
168
+ gasSettings?.maxFeesPerGas ?? (await this.aztecNode.getCurrentMinFees()).mul(1 + this.minFeePadding);
169
169
  let accountFeePaymentMethodOptions;
170
170
  // The transaction does not include a fee payment method, so we set the flag
171
171
  // for the account to use its fee juice balance
package/src/crypto.ts ADDED
@@ -0,0 +1,283 @@
1
+ /**
2
+ * Cryptographic utilities for secure wallet communication.
3
+ *
4
+ * This module provides ECDH key exchange and AES-GCM encryption primitives
5
+ * for establishing secure communication channels between dApps and wallet extensions.
6
+ *
7
+ * The crypto flow:
8
+ * 1. Both parties generate ECDH key pairs using {@link generateKeyPair}
9
+ * 2. Public keys are exchanged (exported via {@link exportPublicKey}, imported via {@link importPublicKey})
10
+ * 3. Both parties derive the same shared secret using {@link deriveSharedKey}
11
+ * 4. Messages are encrypted/decrypted using {@link encrypt} and {@link decrypt}
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * // Party A
16
+ * const keyPairA = await generateKeyPair();
17
+ * const publicKeyA = await exportPublicKey(keyPairA.publicKey);
18
+ *
19
+ * // Party B
20
+ * const keyPairB = await generateKeyPair();
21
+ * const publicKeyB = await exportPublicKey(keyPairB.publicKey);
22
+ *
23
+ * // Exchange public keys, then derive shared secret
24
+ * const importedB = await importPublicKey(publicKeyB);
25
+ * const sharedKeyA = await deriveSharedKey(keyPairA.privateKey, importedB);
26
+ *
27
+ * // Encrypt and decrypt
28
+ * const encrypted = await encrypt(sharedKeyA, { message: 'hello' });
29
+ * const decrypted = await decrypt(sharedKeyB, encrypted);
30
+ * ```
31
+ *
32
+ * @packageDocumentation
33
+ */
34
+ import { jsonStringify } from '@aztec/foundation/json-rpc';
35
+
36
+ /**
37
+ * Exported public key in JWK format for transmission over untrusted channels.
38
+ *
39
+ * Contains only the public components of an ECDH P-256 key, safe to share.
40
+ */
41
+ export interface ExportedPublicKey {
42
+ /** Key type - always "EC" for elliptic curve */
43
+ kty: string;
44
+ /** Curve name - always "P-256" */
45
+ crv: string;
46
+ /** X coordinate (base64url encoded) */
47
+ x: string;
48
+ /** Y coordinate (base64url encoded) */
49
+ y: string;
50
+ }
51
+
52
+ /**
53
+ * Encrypted message payload containing ciphertext and initialization vector.
54
+ *
55
+ * Both fields are base64-encoded for safe transmission as JSON.
56
+ */
57
+ export interface EncryptedPayload {
58
+ /** Initialization vector (base64 encoded, 12 bytes) */
59
+ iv: string;
60
+ /** Ciphertext (base64 encoded) */
61
+ ciphertext: string;
62
+ }
63
+
64
+ /**
65
+ * ECDH key pair for secure communication.
66
+ *
67
+ * The private key should never be exported or transmitted.
68
+ * The public key can be exported via {@link exportPublicKey} for exchange.
69
+ */
70
+ export interface SecureKeyPair {
71
+ /** Public key - safe to share */
72
+ publicKey: CryptoKey;
73
+ /** Private key - keep secret, used for key derivation */
74
+ privateKey: CryptoKey;
75
+ }
76
+
77
+ /**
78
+ * Generates an ECDH P-256 key pair for key exchange.
79
+ *
80
+ * The generated key pair can be used to derive a shared secret with another
81
+ * party's public key using {@link deriveSharedKey}.
82
+ *
83
+ * @returns A new ECDH key pair
84
+ *
85
+ * @example
86
+ * ```typescript
87
+ * const keyPair = await generateKeyPair();
88
+ * const publicKey = await exportPublicKey(keyPair.publicKey);
89
+ * // Send publicKey to the other party
90
+ * ```
91
+ */
92
+ export async function generateKeyPair(): Promise<SecureKeyPair> {
93
+ const keyPair = await crypto.subtle.generateKey(
94
+ {
95
+ name: 'ECDH',
96
+ namedCurve: 'P-256',
97
+ },
98
+ true, // extractable (needed to export public key)
99
+ ['deriveKey'],
100
+ );
101
+ return {
102
+ publicKey: keyPair.publicKey,
103
+ privateKey: keyPair.privateKey,
104
+ };
105
+ }
106
+
107
+ /**
108
+ * Exports a public key to JWK format for transmission.
109
+ *
110
+ * The exported key contains only public components and is safe to transmit
111
+ * over untrusted channels.
112
+ *
113
+ * @param publicKey - The CryptoKey public key to export
114
+ * @returns The public key in JWK format
115
+ *
116
+ * @example
117
+ * ```typescript
118
+ * const keyPair = await generateKeyPair();
119
+ * const exported = await exportPublicKey(keyPair.publicKey);
120
+ * // exported can be JSON serialized and sent to another party
121
+ * ```
122
+ */
123
+ export async function exportPublicKey(publicKey: CryptoKey): Promise<ExportedPublicKey> {
124
+ const jwk = await crypto.subtle.exportKey('jwk', publicKey);
125
+ return {
126
+ kty: jwk.kty!,
127
+ crv: jwk.crv!,
128
+ x: jwk.x!,
129
+ y: jwk.y!,
130
+ };
131
+ }
132
+
133
+ /**
134
+ * Imports a public key from JWK format.
135
+ *
136
+ * Used to import the other party's public key for deriving a shared secret.
137
+ *
138
+ * @param exported - The public key in JWK format
139
+ * @returns A CryptoKey that can be used with {@link deriveSharedKey}
140
+ *
141
+ * @example
142
+ * ```typescript
143
+ * // Receive exported public key from other party
144
+ * const theirPublicKey = await importPublicKey(receivedPublicKey);
145
+ * const sharedKey = await deriveSharedKey(myPrivateKey, theirPublicKey);
146
+ * ```
147
+ */
148
+ export function importPublicKey(exported: ExportedPublicKey): Promise<CryptoKey> {
149
+ return crypto.subtle.importKey(
150
+ 'jwk',
151
+ {
152
+ kty: exported.kty,
153
+ crv: exported.crv,
154
+ x: exported.x,
155
+ y: exported.y,
156
+ },
157
+ {
158
+ name: 'ECDH',
159
+ namedCurve: 'P-256',
160
+ },
161
+ false,
162
+ [],
163
+ );
164
+ }
165
+
166
+ /**
167
+ * Derives a shared AES-256-GCM key from ECDH key exchange.
168
+ *
169
+ * Both parties will derive the same shared key when using their own private key
170
+ * and the other party's public key. This is the core of ECDH key agreement.
171
+ *
172
+ * @param privateKey - Your ECDH private key
173
+ * @param publicKey - The other party's ECDH public key
174
+ * @returns An AES-256-GCM key for encryption/decryption
175
+ *
176
+ * @example
177
+ * ```typescript
178
+ * // Both parties derive the same key
179
+ * const sharedKeyA = await deriveSharedKey(privateKeyA, publicKeyB);
180
+ * const sharedKeyB = await deriveSharedKey(privateKeyB, publicKeyA);
181
+ * // sharedKeyA and sharedKeyB are equivalent
182
+ * ```
183
+ */
184
+ export function deriveSharedKey(privateKey: CryptoKey, publicKey: CryptoKey): Promise<CryptoKey> {
185
+ return crypto.subtle.deriveKey(
186
+ {
187
+ name: 'ECDH',
188
+ public: publicKey,
189
+ },
190
+ privateKey,
191
+ {
192
+ name: 'AES-GCM',
193
+ length: 256,
194
+ },
195
+ false,
196
+ ['encrypt', 'decrypt'],
197
+ );
198
+ }
199
+
200
+ /**
201
+ * Encrypts data using AES-256-GCM.
202
+ *
203
+ * The data is JSON serialized before encryption. A random 12-byte IV is
204
+ * generated for each encryption operation.
205
+ *
206
+ * AES-GCM provides both confidentiality and authenticity - any tampering
207
+ * with the ciphertext will cause decryption to fail.
208
+ *
209
+ * @param key - The AES-GCM key (from {@link deriveSharedKey})
210
+ * @param data - The data to encrypt (will be JSON serialized)
211
+ * @returns The encrypted payload with IV and ciphertext
212
+ *
213
+ * @example
214
+ * ```typescript
215
+ * const encrypted = await encrypt(sharedKey, { action: 'transfer', amount: 100 });
216
+ * // encrypted.iv and encrypted.ciphertext are base64 strings
217
+ * ```
218
+ */
219
+ export async function encrypt(key: CryptoKey, data: unknown): Promise<EncryptedPayload> {
220
+ const iv = crypto.getRandomValues(new Uint8Array(12));
221
+ const encoded = new TextEncoder().encode(jsonStringify(data));
222
+
223
+ const ciphertext = await crypto.subtle.encrypt({ name: 'AES-GCM', iv }, key, encoded);
224
+
225
+ return {
226
+ iv: arrayBufferToBase64(iv),
227
+ ciphertext: arrayBufferToBase64(ciphertext),
228
+ };
229
+ }
230
+
231
+ /**
232
+ * Decrypts data using AES-256-GCM.
233
+ *
234
+ * The decrypted data is JSON parsed before returning.
235
+ *
236
+ * @typeParam T - The expected type of the decrypted data
237
+ * @param key - The AES-GCM key (from {@link deriveSharedKey})
238
+ * @param payload - The encrypted payload from {@link encrypt}
239
+ * @returns The decrypted and parsed data
240
+ *
241
+ * @throws Error if decryption fails (wrong key or tampered ciphertext)
242
+ *
243
+ * @example
244
+ * ```typescript
245
+ * const decrypted = await decrypt<{ action: string; amount: number }>(sharedKey, encrypted);
246
+ * console.log(decrypted.action); // 'transfer'
247
+ * ```
248
+ */
249
+ export async function decrypt<T = unknown>(key: CryptoKey, payload: EncryptedPayload): Promise<T> {
250
+ const iv = base64ToArrayBuffer(payload.iv);
251
+ const ciphertext = base64ToArrayBuffer(payload.ciphertext);
252
+
253
+ const decrypted = await crypto.subtle.decrypt({ name: 'AES-GCM', iv }, key, ciphertext);
254
+
255
+ const decoded = new TextDecoder().decode(decrypted);
256
+ return JSON.parse(decoded) as T;
257
+ }
258
+
259
+ /**
260
+ * Converts ArrayBuffer to base64 string.
261
+ * @internal
262
+ */
263
+ function arrayBufferToBase64(buffer: ArrayBuffer | Uint8Array): string {
264
+ const bytes = buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer);
265
+ let binary = '';
266
+ for (let i = 0; i < bytes.byteLength; i++) {
267
+ binary += String.fromCharCode(bytes[i]);
268
+ }
269
+ return btoa(binary);
270
+ }
271
+
272
+ /**
273
+ * Converts base64 string to ArrayBuffer.
274
+ * @internal
275
+ */
276
+ function base64ToArrayBuffer(base64: string): ArrayBuffer {
277
+ const binary = atob(base64);
278
+ const bytes = new Uint8Array(binary.length);
279
+ for (let i = 0; i < binary.length; i++) {
280
+ bytes[i] = binary.charCodeAt(i);
281
+ }
282
+ return bytes.buffer;
283
+ }
@@ -9,13 +9,7 @@ export type {
9
9
  } from './types.js';
10
10
 
11
11
  // Re-export types from providers for convenience
12
- export type {
13
- WalletInfo,
14
- WalletMessage,
15
- WalletResponse,
16
- DiscoveryRequest,
17
- DiscoveryResponse,
18
- } from '../providers/types.js';
12
+ export type { WalletInfo, WalletMessage, WalletResponse, DiscoveryRequest, DiscoveryResponse } from '../types.js';
19
13
 
20
14
  // Re-export commonly needed utilities for wallet integration
21
15
  export { ChainInfoSchema } from '@aztec/aztec.js/account';
@@ -54,7 +54,7 @@ export class WalletManager {
54
54
  metadata: {
55
55
  version: ext.version,
56
56
  },
57
- connect: (appId: string) => Promise.resolve(ExtensionWallet.create(chainInfo, appId, ext.id)),
57
+ connect: (appId: string) => ExtensionWallet.create(ext, chainInfo, appId),
58
58
  });
59
59
  }
60
60
  }
@@ -2,7 +2,7 @@ import type { ChainInfo } from '@aztec/aztec.js/account';
2
2
  import { jsonStringify } from '@aztec/foundation/json-rpc';
3
3
  import { promiseWithResolvers } from '@aztec/foundation/promise';
4
4
 
5
- import type { DiscoveryRequest, DiscoveryResponse, WalletInfo } from '../types.js';
5
+ import type { DiscoveryRequest, DiscoveryResponse, WalletInfo } from '../../types.js';
6
6
 
7
7
  /**
8
8
  * Provider for discovering and managing Aztec wallet extensions