@aztec/wallet-sdk 0.0.1-commit.9593d84 → 0.0.1-commit.96bb3f7

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 +173 -139
  2. package/dest/base-wallet/base_wallet.d.ts +5 -5
  3. package/dest/base-wallet/base_wallet.d.ts.map +1 -1
  4. package/dest/base-wallet/base_wallet.js +18 -9
  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 +12 -10
  23. package/src/base-wallet/base_wallet.ts +19 -13
  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,146 @@
1
+ /**
2
+ * Exported public key in JWK format for transmission over untrusted channels.
3
+ *
4
+ * Contains only the public components of an ECDH P-256 key, safe to share.
5
+ */
6
+ export interface ExportedPublicKey {
7
+ /** Key type - always "EC" for elliptic curve */
8
+ kty: string;
9
+ /** Curve name - always "P-256" */
10
+ crv: string;
11
+ /** X coordinate (base64url encoded) */
12
+ x: string;
13
+ /** Y coordinate (base64url encoded) */
14
+ y: string;
15
+ }
16
+ /**
17
+ * Encrypted message payload containing ciphertext and initialization vector.
18
+ *
19
+ * Both fields are base64-encoded for safe transmission as JSON.
20
+ */
21
+ export interface EncryptedPayload {
22
+ /** Initialization vector (base64 encoded, 12 bytes) */
23
+ iv: string;
24
+ /** Ciphertext (base64 encoded) */
25
+ ciphertext: string;
26
+ }
27
+ /**
28
+ * ECDH key pair for secure communication.
29
+ *
30
+ * The private key should never be exported or transmitted.
31
+ * The public key can be exported via {@link exportPublicKey} for exchange.
32
+ */
33
+ export interface SecureKeyPair {
34
+ /** Public key - safe to share */
35
+ publicKey: CryptoKey;
36
+ /** Private key - keep secret, used for key derivation */
37
+ privateKey: CryptoKey;
38
+ }
39
+ /**
40
+ * Generates an ECDH P-256 key pair for key exchange.
41
+ *
42
+ * The generated key pair can be used to derive a shared secret with another
43
+ * party's public key using {@link deriveSharedKey}.
44
+ *
45
+ * @returns A new ECDH key pair
46
+ *
47
+ * @example
48
+ * ```typescript
49
+ * const keyPair = await generateKeyPair();
50
+ * const publicKey = await exportPublicKey(keyPair.publicKey);
51
+ * // Send publicKey to the other party
52
+ * ```
53
+ */
54
+ export declare function generateKeyPair(): Promise<SecureKeyPair>;
55
+ /**
56
+ * Exports a public key to JWK format for transmission.
57
+ *
58
+ * The exported key contains only public components and is safe to transmit
59
+ * over untrusted channels.
60
+ *
61
+ * @param publicKey - The CryptoKey public key to export
62
+ * @returns The public key in JWK format
63
+ *
64
+ * @example
65
+ * ```typescript
66
+ * const keyPair = await generateKeyPair();
67
+ * const exported = await exportPublicKey(keyPair.publicKey);
68
+ * // exported can be JSON serialized and sent to another party
69
+ * ```
70
+ */
71
+ export declare function exportPublicKey(publicKey: CryptoKey): Promise<ExportedPublicKey>;
72
+ /**
73
+ * Imports a public key from JWK format.
74
+ *
75
+ * Used to import the other party's public key for deriving a shared secret.
76
+ *
77
+ * @param exported - The public key in JWK format
78
+ * @returns A CryptoKey that can be used with {@link deriveSharedKey}
79
+ *
80
+ * @example
81
+ * ```typescript
82
+ * // Receive exported public key from other party
83
+ * const theirPublicKey = await importPublicKey(receivedPublicKey);
84
+ * const sharedKey = await deriveSharedKey(myPrivateKey, theirPublicKey);
85
+ * ```
86
+ */
87
+ export declare function importPublicKey(exported: ExportedPublicKey): Promise<CryptoKey>;
88
+ /**
89
+ * Derives a shared AES-256-GCM key from ECDH key exchange.
90
+ *
91
+ * Both parties will derive the same shared key when using their own private key
92
+ * and the other party's public key. This is the core of ECDH key agreement.
93
+ *
94
+ * @param privateKey - Your ECDH private key
95
+ * @param publicKey - The other party's ECDH public key
96
+ * @returns An AES-256-GCM key for encryption/decryption
97
+ *
98
+ * @example
99
+ * ```typescript
100
+ * // Both parties derive the same key
101
+ * const sharedKeyA = await deriveSharedKey(privateKeyA, publicKeyB);
102
+ * const sharedKeyB = await deriveSharedKey(privateKeyB, publicKeyA);
103
+ * // sharedKeyA and sharedKeyB are equivalent
104
+ * ```
105
+ */
106
+ export declare function deriveSharedKey(privateKey: CryptoKey, publicKey: CryptoKey): Promise<CryptoKey>;
107
+ /**
108
+ * Encrypts data using AES-256-GCM.
109
+ *
110
+ * The data is JSON serialized before encryption. A random 12-byte IV is
111
+ * generated for each encryption operation.
112
+ *
113
+ * AES-GCM provides both confidentiality and authenticity - any tampering
114
+ * with the ciphertext will cause decryption to fail.
115
+ *
116
+ * @param key - The AES-GCM key (from {@link deriveSharedKey})
117
+ * @param data - The data to encrypt (will be JSON serialized)
118
+ * @returns The encrypted payload with IV and ciphertext
119
+ *
120
+ * @example
121
+ * ```typescript
122
+ * const encrypted = await encrypt(sharedKey, { action: 'transfer', amount: 100 });
123
+ * // encrypted.iv and encrypted.ciphertext are base64 strings
124
+ * ```
125
+ */
126
+ export declare function encrypt(key: CryptoKey, data: unknown): Promise<EncryptedPayload>;
127
+ /**
128
+ * Decrypts data using AES-256-GCM.
129
+ *
130
+ * The decrypted data is JSON parsed before returning.
131
+ *
132
+ * @typeParam T - The expected type of the decrypted data
133
+ * @param key - The AES-GCM key (from {@link deriveSharedKey})
134
+ * @param payload - The encrypted payload from {@link encrypt}
135
+ * @returns The decrypted and parsed data
136
+ *
137
+ * @throws Error if decryption fails (wrong key or tampered ciphertext)
138
+ *
139
+ * @example
140
+ * ```typescript
141
+ * const decrypted = await decrypt<{ action: string; amount: number }>(sharedKey, encrypted);
142
+ * console.log(decrypted.action); // 'transfer'
143
+ * ```
144
+ */
145
+ export declare function decrypt<T = unknown>(key: CryptoKey, payload: EncryptedPayload): Promise<T>;
146
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY3J5cHRvLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvY3J5cHRvLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQW1DQTs7OztHQUlHO0FBQ0gsTUFBTSxXQUFXLGlCQUFpQjtJQUNoQyxnREFBZ0Q7SUFDaEQsR0FBRyxFQUFFLE1BQU0sQ0FBQztJQUNaLGtDQUFrQztJQUNsQyxHQUFHLEVBQUUsTUFBTSxDQUFDO0lBQ1osdUNBQXVDO0lBQ3ZDLENBQUMsRUFBRSxNQUFNLENBQUM7SUFDVix1Q0FBdUM7SUFDdkMsQ0FBQyxFQUFFLE1BQU0sQ0FBQztDQUNYO0FBRUQ7Ozs7R0FJRztBQUNILE1BQU0sV0FBVyxnQkFBZ0I7SUFDL0IsdURBQXVEO0lBQ3ZELEVBQUUsRUFBRSxNQUFNLENBQUM7SUFDWCxrQ0FBa0M7SUFDbEMsVUFBVSxFQUFFLE1BQU0sQ0FBQztDQUNwQjtBQUVEOzs7OztHQUtHO0FBQ0gsTUFBTSxXQUFXLGFBQWE7SUFDNU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AAC,CAAC,CAQhG"}
package/dest/crypto.js ADDED
@@ -0,0 +1,216 @@
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
+ */ import { jsonStringify } from '@aztec/foundation/json-rpc';
34
+ /**
35
+ * Generates an ECDH P-256 key pair for key exchange.
36
+ *
37
+ * The generated key pair can be used to derive a shared secret with another
38
+ * party's public key using {@link deriveSharedKey}.
39
+ *
40
+ * @returns A new ECDH key pair
41
+ *
42
+ * @example
43
+ * ```typescript
44
+ * const keyPair = await generateKeyPair();
45
+ * const publicKey = await exportPublicKey(keyPair.publicKey);
46
+ * // Send publicKey to the other party
47
+ * ```
48
+ */ export async function generateKeyPair() {
49
+ const keyPair = await crypto.subtle.generateKey({
50
+ name: 'ECDH',
51
+ namedCurve: 'P-256'
52
+ }, true, [
53
+ 'deriveKey'
54
+ ]);
55
+ return {
56
+ publicKey: keyPair.publicKey,
57
+ privateKey: keyPair.privateKey
58
+ };
59
+ }
60
+ /**
61
+ * Exports a public key to JWK format for transmission.
62
+ *
63
+ * The exported key contains only public components and is safe to transmit
64
+ * over untrusted channels.
65
+ *
66
+ * @param publicKey - The CryptoKey public key to export
67
+ * @returns The public key in JWK format
68
+ *
69
+ * @example
70
+ * ```typescript
71
+ * const keyPair = await generateKeyPair();
72
+ * const exported = await exportPublicKey(keyPair.publicKey);
73
+ * // exported can be JSON serialized and sent to another party
74
+ * ```
75
+ */ export async function exportPublicKey(publicKey) {
76
+ const jwk = await crypto.subtle.exportKey('jwk', publicKey);
77
+ return {
78
+ kty: jwk.kty,
79
+ crv: jwk.crv,
80
+ x: jwk.x,
81
+ y: jwk.y
82
+ };
83
+ }
84
+ /**
85
+ * Imports a public key from JWK format.
86
+ *
87
+ * Used to import the other party's public key for deriving a shared secret.
88
+ *
89
+ * @param exported - The public key in JWK format
90
+ * @returns A CryptoKey that can be used with {@link deriveSharedKey}
91
+ *
92
+ * @example
93
+ * ```typescript
94
+ * // Receive exported public key from other party
95
+ * const theirPublicKey = await importPublicKey(receivedPublicKey);
96
+ * const sharedKey = await deriveSharedKey(myPrivateKey, theirPublicKey);
97
+ * ```
98
+ */ export function importPublicKey(exported) {
99
+ return crypto.subtle.importKey('jwk', {
100
+ kty: exported.kty,
101
+ crv: exported.crv,
102
+ x: exported.x,
103
+ y: exported.y
104
+ }, {
105
+ name: 'ECDH',
106
+ namedCurve: 'P-256'
107
+ }, false, []);
108
+ }
109
+ /**
110
+ * Derives a shared AES-256-GCM key from ECDH key exchange.
111
+ *
112
+ * Both parties will derive the same shared key when using their own private key
113
+ * and the other party's public key. This is the core of ECDH key agreement.
114
+ *
115
+ * @param privateKey - Your ECDH private key
116
+ * @param publicKey - The other party's ECDH public key
117
+ * @returns An AES-256-GCM key for encryption/decryption
118
+ *
119
+ * @example
120
+ * ```typescript
121
+ * // Both parties derive the same key
122
+ * const sharedKeyA = await deriveSharedKey(privateKeyA, publicKeyB);
123
+ * const sharedKeyB = await deriveSharedKey(privateKeyB, publicKeyA);
124
+ * // sharedKeyA and sharedKeyB are equivalent
125
+ * ```
126
+ */ export function deriveSharedKey(privateKey, publicKey) {
127
+ return crypto.subtle.deriveKey({
128
+ name: 'ECDH',
129
+ public: publicKey
130
+ }, privateKey, {
131
+ name: 'AES-GCM',
132
+ length: 256
133
+ }, false, [
134
+ 'encrypt',
135
+ 'decrypt'
136
+ ]);
137
+ }
138
+ /**
139
+ * Encrypts data using AES-256-GCM.
140
+ *
141
+ * The data is JSON serialized before encryption. A random 12-byte IV is
142
+ * generated for each encryption operation.
143
+ *
144
+ * AES-GCM provides both confidentiality and authenticity - any tampering
145
+ * with the ciphertext will cause decryption to fail.
146
+ *
147
+ * @param key - The AES-GCM key (from {@link deriveSharedKey})
148
+ * @param data - The data to encrypt (will be JSON serialized)
149
+ * @returns The encrypted payload with IV and ciphertext
150
+ *
151
+ * @example
152
+ * ```typescript
153
+ * const encrypted = await encrypt(sharedKey, { action: 'transfer', amount: 100 });
154
+ * // encrypted.iv and encrypted.ciphertext are base64 strings
155
+ * ```
156
+ */ export async function encrypt(key, data) {
157
+ const iv = crypto.getRandomValues(new Uint8Array(12));
158
+ const encoded = new TextEncoder().encode(jsonStringify(data));
159
+ const ciphertext = await crypto.subtle.encrypt({
160
+ name: 'AES-GCM',
161
+ iv
162
+ }, key, encoded);
163
+ return {
164
+ iv: arrayBufferToBase64(iv),
165
+ ciphertext: arrayBufferToBase64(ciphertext)
166
+ };
167
+ }
168
+ /**
169
+ * Decrypts data using AES-256-GCM.
170
+ *
171
+ * The decrypted data is JSON parsed before returning.
172
+ *
173
+ * @typeParam T - The expected type of the decrypted data
174
+ * @param key - The AES-GCM key (from {@link deriveSharedKey})
175
+ * @param payload - The encrypted payload from {@link encrypt}
176
+ * @returns The decrypted and parsed data
177
+ *
178
+ * @throws Error if decryption fails (wrong key or tampered ciphertext)
179
+ *
180
+ * @example
181
+ * ```typescript
182
+ * const decrypted = await decrypt<{ action: string; amount: number }>(sharedKey, encrypted);
183
+ * console.log(decrypted.action); // 'transfer'
184
+ * ```
185
+ */ export async function decrypt(key, payload) {
186
+ const iv = base64ToArrayBuffer(payload.iv);
187
+ const ciphertext = base64ToArrayBuffer(payload.ciphertext);
188
+ const decrypted = await crypto.subtle.decrypt({
189
+ name: 'AES-GCM',
190
+ iv
191
+ }, key, ciphertext);
192
+ const decoded = new TextDecoder().decode(decrypted);
193
+ return JSON.parse(decoded);
194
+ }
195
+ /**
196
+ * Converts ArrayBuffer to base64 string.
197
+ * @internal
198
+ */ function arrayBufferToBase64(buffer) {
199
+ const bytes = buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer);
200
+ let binary = '';
201
+ for(let i = 0; i < bytes.byteLength; i++){
202
+ binary += String.fromCharCode(bytes[i]);
203
+ }
204
+ return btoa(binary);
205
+ }
206
+ /**
207
+ * Converts base64 string to ArrayBuffer.
208
+ * @internal
209
+ */ function base64ToArrayBuffer(base64) {
210
+ const binary = atob(base64);
211
+ const bytes = new Uint8Array(binary.length);
212
+ for(let i = 0; i < binary.length; i++){
213
+ bytes[i] = binary.charCodeAt(i);
214
+ }
215
+ return bytes.buffer;
216
+ }
@@ -1,8 +1,8 @@
1
1
  export { WalletManager } from './wallet_manager.js';
2
2
  export type { WalletManagerConfig, ExtensionWalletConfig, WebWalletConfig, WalletProviderType, WalletProvider, DiscoverWalletsOptions, } from './types.js';
3
- export type { WalletInfo, WalletMessage, WalletResponse, DiscoveryRequest, DiscoveryResponse, } from '../providers/types.js';
3
+ export type { WalletInfo, WalletMessage, WalletResponse, DiscoveryRequest, DiscoveryResponse } from '../types.js';
4
4
  export { ChainInfoSchema } from '@aztec/aztec.js/account';
5
5
  export type { ChainInfo } from '@aztec/aztec.js/account';
6
6
  export { WalletSchema } from '@aztec/aztec.js/wallet';
7
7
  export { jsonStringify } from '@aztec/foundation/json-rp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2xCLGNBQWMsRUFDZCxzQkFBc0IsR0FDdkIsTUFBTSxZQUFZLENBQUM7QUFHcEIsWUFBWSxFQUFFLFVBQVUsRUFBRSxhQUFhLEVBQUUsY0FBYyxFQUFFLGdCQUFnQixFQUFFLGlCQUFpQixFQUFFLE1BQU0sYUFBYSxDQUFDO0FBR2xILE9BQU8sRUFBRSxlQUFlLEVBQUUsTUFBTSx5QkFBeUIsQ0FBQztBQUMxRCxZQUFZLEVBQUUsU0FBUyxFQUFFLE1BQU0seUJBQXlCLENBQUM7QUFDekQsT0FBTyxFQUFFLFlBQVksRUFBRSxNQUFNLHdCQUF3QixDQUFDO0FBQ3RELE9BQU8sRUFBRSxhQUFhLEVBQUUsTUFBTSw0QkFBNEIsQ0FBQyJ9
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/manager/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,YAAY,EACV,mBAAmB,EACnB,qBAAqB,EACrB,eAAe,EACf,kBAAkB,EAClB,cAAc,EACd,sBAAsB,GACvB,MAAM,YAAY,CAAC;AAGpB,YAAY,EACV,UAAU,EACV,aAAa,EACb,cAAc,EACd,gBAAgB,EAChB,iBAAiB,GAClB,MAAM,uBAAuB,CAAC;AAG/B,OAAO,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAC1D,YAAY,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AACzD,OAAO,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AACtD,OAAO,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/manager/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,YAAY,EACV,mBAAmB,EACnB,qBAAqB,EACrB,eAAe,EACf,kBAAkB,EAClB,cAAc,EACd,sBAAsB,GACvB,MAAM,YAAY,CAAC;AAGpB,YAAY,EAAE,UAAU,EAAE,aAAa,EAAE,cAAc,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAGlH,OAAO,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAC1D,YAAY,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AACzD,OAAO,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AACtD,OAAO,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC"}
@@ -51,7 +51,7 @@ import { ExtensionProvider, ExtensionWallet } from '../providers/extension/index
51
51
  metadata: {
52
52
  version: ext.version
53
53
  },
54
- connect: (appId)=>Promise.resolve(ExtensionWallet.create(chainInfo, appId, ext.id))
54
+ connect: (appId)=>ExtensionWallet.create(ext, chainInfo, appId)
55
55
  });
56
56
  }
57
57
  }
@@ -1,5 +1,5 @@
1
1
  import type { ChainInfo } from '@aztec/aztec.js/account';
2
- import type { WalletInfo } from '../types.js';
2
+ import type { WalletInfo } from '../../types.js';
3
3
  /**
4
4
  * Provider for discovering and managing Aztec wallet extensions
5
5
  */
@@ -14,4 +14,4 @@ export declare class ExtensionProvider {
14
14
  */
15
15
  static discoverExtensions(chainInfo: ChainInfo, timeout?: number): Promise<WalletInfo[]>;
16
16
  }
17
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZXh0ZW5zaW9uX3Byb3ZpZGVyLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi9zcmMvcHJvdmlkZXJzL2V4dGVuc2lvbi9leHRlbnNpb25fcHJvdmlkZXIudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxLQUFLLEVBQUUsU0FBUyxFQUFFLE1BQU0seUJBQXlCLENBQUM7QUFJekQsT0FBTyxLQUFLLEVBQXVDLFVBQVUsRUFBRSxNQUFNLGFBQWEsQ0FBQztBQUVuRjs7R0FFRztBQUNILHFCQUFhLGlCQUFpQjtJQUM1QixPQUFPLENBQUMsTUFBTSxDQUFDLG9CQUFvQixDQUFzQztJQUN6RSxPQUFPLENBQUMsTUFBTSxDQUFDLG1CQUFtQixDQUFTO0lBRTNDOzs7OztPQUtHO0lBQ0gsT0FBYSxrQkFBa0IsQ0FBQyxTQUFTLEVBQUUsU0FBUyxFQUFFLE9BQU8sR0FBRSxNQUFhLEdBQUcsT0FBTyxDQUFDLFVBQVUsRUFBRSxDQUFDLENBbURuRztDQUNGIn0=
17
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZXh0ZW5zaW9uX3Byb3ZpZGVyLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi9zcmMvcHJvdmlkZXJzL2V4dGVuc2lvbi9leHRlbnNpb25fcHJvdmlkZXIudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxLQUFLLEVBQUUsU0FBUyxFQUFFLE1BQU0seUJBQXlCLENBQUM7QUFJekQsT0FBTyxLQUFLLEVBQXVDLFVBQVUsRUFBRSxNQUFNLGdCQUFnQixDQUFDO0FBRXRGOztHQUVHO0FBQ0gscUJBQWEsaUJBQWlCO0lBQzVCLE9BQU8sQ0FBQyxNQUFNLENBQUMsb0JBQW9CLENBQXNDO0lBQ3pFLE9BQU8sQ0FBQyxNQUFNLENBQUMsbUJBQW1CLENBQVM7SUFFM0M7Ozs7O09BS0c7SUFDSCxPQUFhLGtCQUFrQixDQUFDLFNBQVMsRUFBRSxTQUFTLEVBQUUsT0FBTyxHQUFFLE1BQWEsR0FBRyxPQUFPLENBQUMsVUFBVSxFQUFFLENBQUMsQ0FtRG5HO0NBQ0YifQ==
@@ -1 +1 @@
1
- {"version":3,"file":"extension_provider.d.ts","sourceRoot":"","sources":["../../../src/providers/extension/extension_provider.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AAIzD,OAAO,KAAK,EAAuC,UAAU,EAAE,MAAM,aAAa,CAAC;AAEnF;;GAEG;AACH,qBAAa,iBAAiB;IAC5B,OAAO,CAAC,MAAM,CAAC,oBAAoB,CAAsC;IACzE,OAAO,CAAC,MAAM,CAAC,mBAAmB,CAAS;IAE3C;;;;;OAKG;IACH,OAAa,kBAAkB,CAAC,SAAS,EAAE,SAAS,EAAE,OAAO,GAAE,MAAa,GAAG,OAAO,CAAC,UAAU,EAAE,CAAC,CAmDnG;CACF"}
1
+ {"version":3,"file":"extension_provider.d.ts","sourceRoot":"","sources":["../../../src/providers/extension/extension_provider.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AAIzD,OAAO,KAAK,EAAuC,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAEtF;;GAEG;AACH,qBAAa,iBAAiB;IAC5B,OAAO,CAAC,MAAM,CAAC,oBAAoB,CAAsC;IACzE,OAAO,CAAC,MAAM,CAAC,mBAAmB,CAAS;IAE3C;;;;;OAKG;IACH,OAAa,kBAAkB,CAAC,SAAS,EAAE,SAAS,EAAE,OAAO,GAAE,MAAa,GAAG,OAAO,CAAC,UAAU,EAAE,CAAC,CAmDnG;CACF"}
@@ -1,23 +1,95 @@
1
1
  import type { ChainInfo } from '@aztec/aztec.js/account';
2
2
  import { type Wallet } from '@aztec/aztec.js/wallet';
3
+ import type { WalletInfo } from '../../types.js';
3
4
  /**
4
5
  * A wallet implementation that communicates with browser extension wallets
5
- * Supports multiple extensions by targeting specific extension IDs
6
+ * using a secure encrypted MessageChannel.
7
+ *
8
+ * This class establishes a private communication channel with a wallet extension
9
+ * using the following security mechanisms:
10
+ *
11
+ * 1. **MessageChannel**: Creates a private communication channel that is not
12
+ * visible to other scripts on the page (unlike window.postMessage).
13
+ *
14
+ * 2. **ECDH Key Exchange**: Uses Elliptic Curve Diffie-Hellman to derive a
15
+ * shared secret between the dApp and wallet without transmitting private keys.
16
+ *
17
+ * 3. **AES-GCM Encryption**: All messages after channel establishment are
18
+ * encrypted using AES-256-GCM, providing both confidentiality and authenticity.
19
+ *
20
+ * @example
21
+ * ```typescript
22
+ * // Discovery returns wallet info including the wallet's public key
23
+ * const wallets = await ExtensionProvider.discoverExtensions(chainInfo);
24
+ * const walletInfo = wallets[0];
25
+ *
26
+ * // Create a secure connection to the wallet
27
+ * const wallet = await ExtensionWallet.create(walletInfo, chainInfo, 'my-dapp');
28
+ *
29
+ * // All subsequent calls are encrypted
30
+ * const accounts = await wallet.getAccounts();
31
+ * ```
6
32
  */
7
33
  export declare class ExtensionWallet {
8
34
  private chainInfo;
9
35
  private appId;
10
36
  private extensionId;
37
+ /** Map of pending requests awaiting responses, keyed by message ID */
11
38
  private inFlight;
39
+ /** The MessagePort for private communication with the extension */
40
+ private port;
41
+ /** The derived AES-GCM key for encrypting/decrypting messages */
42
+ private sharedKey;
43
+ /**
44
+ * Private constructor - use {@link ExtensionWallet.create} to instantiate.
45
+ * @param chainInfo - The chain information (chainId and version)
46
+ * @param appId - Application identifier for the requesting dApp
47
+ * @param extensionId - The unique identifier of the target wallet extension
48
+ */
12
49
  private constructor();
13
50
  /**
14
51
  * Creates an ExtensionWallet instance that proxies wallet calls to a browser extension
15
- * @param chainInfo - The chain information (chainId and version)
16
- * @param appId - Application identifier for the requesting dapp
17
- * @param extensionId - Specific extension ID to communicate with
18
- * @returns A Proxy object that implements the Wallet interface
52
+ * over a secure encrypted MessageChannel.
53
+ *
54
+ * The connection process:
55
+ * 1. Generates an ECDH key pair for this session
56
+ * 2. Derives a shared AES-256 key using the wallet's public key
57
+ * 3. Creates a MessageChannel and transfers one port to the extension
58
+ * 4. Returns a Proxy that encrypts all wallet method calls
59
+ *
60
+ * @param walletInfo - The discovered wallet information, including the wallet's ECDH public key
61
+ * @param chainInfo - The chain information (chainId and version) for request context
62
+ * @param appId - Application identifier used to identify the requesting dApp to the wallet
63
+ * @returns A Promise resolving to a Wallet implementation that encrypts all communication
64
+ *
65
+ * @throws Error if the secure channel cannot be established
66
+ *
67
+ * @example
68
+ * ```typescript
69
+ * const wallet = await ExtensionWallet.create(
70
+ * walletInfo,
71
+ * { chainId: Fr(31337), version: Fr(0) },
72
+ * 'my-defi-app'
73
+ * );
74
+ * ```
19
75
  */
20
- static create(chainInfo: ChainInfo, appId: string, extensionId: string): Wallet;
76
+ static create(walletInfo: WalletInfo, chainInfo: ChainInfo, appId: string): Promise<Wallet>;
77
+ private establishSecureChannel;
78
+ private handleEncryptedResponse;
21
79
  private postMessage;
80
+ /**
81
+ * Closes the secure channel and cleans up resources.
82
+ *
83
+ * After calling this method, the wallet instance can no longer be used.
84
+ * Any pending requests will not receive responses.
85
+ *
86
+ * @example
87
+ * ```typescript
88
+ * const wallet = await ExtensionWallet.create(walletInfo, chainInfo, 'my-app');
89
+ * // ... use wallet ...
90
+ * wallet.close(); // Clean up when done
91
+ * ```
92
+ */
93
+ close(): void;
22
94
  }
23
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZXh0ZW5zaW9uX3dhbGxldC5kLnRzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vc3JjL3Byb3ZpZGVycy9leHRlbnNpb24vZXh0ZW5zaW9uX3dhbGxldC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxPQUFPLEtBQUssRUFBRSxTQUFTLEVBQUUsTUFBTSx5QkFBeUIsQ0FBQztBQUN6RCxPQUFPLEVBQUUsS0FBSyxNQUFNLEVBQWdCLE1BQU0sd0JBQXdCLENBQUM7QUFzQm5FOzs7R0FHRztBQUNILHFCQUFhLGVBQWU7SUFJeEIsT0FBTyxDQUFDLFNBQVM7SUFDakIsT0FBTyxDQUFDLEtBQUs7SUFDYixPQUFPLENBQUMsV0FBVztJQUxyQixPQUFPLENBQUMsUUFBUSxDQUF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ozs7Ozs7O09BeUJHO0lBQ0gsT0FBYSxNQUFNLENBQUMsVUFBVSxFQUFFLFVBQVUsRUFBRSxTQUFTLEVBQUUsU0FBUyxFQUFFLEtBQUssRUFBRSxNQUFNLEdBQUcsT0FBTyxDQUFDLE1BQU0sQ0FBQyxDQXlCaEc7WUFjYSxzQkFBc0I7WUFxQ3RCLHVCQUF1QjtZQThDdkIsV0FBVztJQXdCekI7Ozs7Ozs7Ozs7OztPQVlHO0lBQ0gsS0FBSyxJQUFJLElBQUksQ0FPWjtDQUNGIn0=
@@ -1 +1 @@
1
- {"version":3,"file":"extension_wallet.d.ts","sourceRoot":"","sources":["../../../src/providers/extension/extension_wallet.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AACzD,OAAO,EAAE,KAAK,MAAM,EAAgB,MAAM,wBAAwB,CAAC;AAsBnE;;;GAGG;AACH,qBAAa,eAAe;IAIxB,OAAO,CAAC,SAAS;IACjB,OAAO,CAAC,KAAK;IACb,OAAO,CAAC,WAAW;IALrB,OAAO,CAAC,QAAQ,CAAoD;IAEpE,OAAO,eAIH;IAEJ;;;;;;OAMG;IACH,MAAM,CAAC,MAAM,CAAC,SAAS,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,GAAG,MAAM,CA6D9E;IAED,OAAO,CAAC,WAAW;CAiBpB"}
1
+ {"version":3,"file":"extension_wallet.d.ts","sourceRoot":"","sources":["../../../src/providers/extension/extension_wallet.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AACzD,OAAO,EAAE,KAAK,MAAM,EAAgB,MAAM,wBAAwB,CAAC;AAgBnE,OAAO,KAAK,EAAkB,UAAU,EAAiC,MAAM,gBAAgB,CAAC;AAahG;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,qBAAa,eAAe;IAiBxB,OAAO,CAAC,S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