@aztec/wallet-sdk 0.0.1-commit.21caa21 → 0.0.1-commit.3469e52
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.
- package/README.md +240 -266
- package/dest/base-wallet/base_wallet.d.ts +20 -10
- package/dest/base-wallet/base_wallet.d.ts.map +1 -1
- package/dest/base-wallet/base_wallet.js +51 -21
- package/dest/crypto.d.ts +183 -0
- package/dest/crypto.d.ts.map +1 -0
- package/dest/crypto.js +300 -0
- package/dest/manager/index.d.ts +4 -3
- package/dest/manager/index.d.ts.map +1 -1
- package/dest/manager/index.js +2 -0
- package/dest/manager/types.d.ts +22 -1
- package/dest/manager/types.d.ts.map +1 -1
- package/dest/manager/wallet_manager.d.ts +1 -1
- package/dest/manager/wallet_manager.d.ts.map +1 -1
- package/dest/manager/wallet_manager.js +34 -15
- package/dest/providers/extension/extension_provider.d.ts +53 -7
- package/dest/providers/extension/extension_provider.d.ts.map +1 -1
- package/dest/providers/extension/extension_provider.js +81 -13
- package/dest/providers/extension/extension_wallet.d.ts +140 -8
- package/dest/providers/extension/extension_wallet.d.ts.map +1 -1
- package/dest/providers/extension/extension_wallet.js +268 -46
- package/dest/providers/extension/index.d.ts +6 -4
- package/dest/providers/extension/index.d.ts.map +1 -1
- package/dest/providers/extension/index.js +2 -0
- package/dest/types.d.ts +92 -0
- package/dest/types.d.ts.map +1 -0
- package/dest/types.js +10 -0
- package/package.json +13 -11
- package/src/base-wallet/base_wallet.ts +67 -33
- package/src/crypto.ts +375 -0
- package/src/manager/index.ts +4 -8
- package/src/manager/types.ts +22 -0
- package/src/manager/wallet_manager.ts +43 -16
- package/src/providers/extension/extension_provider.ts +112 -17
- package/src/providers/extension/extension_wallet.ts +310 -55
- package/src/providers/extension/index.ts +5 -3
- package/src/{providers/types.ts → types.ts} +33 -6
- package/dest/providers/types.d.ts +0 -67
- package/dest/providers/types.d.ts.map +0 -1
- package/dest/providers/types.js +0 -3
|
@@ -1,23 +1,62 @@
|
|
|
1
1
|
import { jsonStringify } from '@aztec/foundation/json-rpc';
|
|
2
2
|
import { promiseWithResolvers } from '@aztec/foundation/promise';
|
|
3
|
+
import { deriveSharedKey, exportPublicKey, generateKeyPair, hashSharedSecret, importPublicKey } from '../../crypto.js';
|
|
4
|
+
import { WalletMessageType } from '../../types.js';
|
|
3
5
|
/**
|
|
4
|
-
* Provider for discovering
|
|
6
|
+
* Provider for discovering Aztec wallet extensions.
|
|
7
|
+
*
|
|
8
|
+
* This class handles the discovery phase of wallet communication:
|
|
9
|
+
* 1. Broadcasts a discovery request with the dApp's public key
|
|
10
|
+
* 2. Receives responses from installed wallets with their public keys
|
|
11
|
+
* 3. Derives shared secrets and computes verification hashes
|
|
12
|
+
* 4. Returns discovered wallets with their secure channel components
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* const wallets = await ExtensionProvider.discoverExtensions(chainInfo);
|
|
17
|
+
* // Display wallets to user with optional emoji verification
|
|
18
|
+
* for (const wallet of wallets) {
|
|
19
|
+
* const emoji = hashToEmoji(wallet.info.verificationHash!);
|
|
20
|
+
* console.log(`${wallet.info.name}: ${emoji}`);
|
|
21
|
+
* }
|
|
22
|
+
* // User selects a wallet after verifying
|
|
23
|
+
* const wallet = await ExtensionWallet.create(wallets[0], chainInfo, 'my-app');
|
|
24
|
+
* ```
|
|
5
25
|
*/ export class ExtensionProvider {
|
|
6
|
-
static discoveredExtensions = new Map();
|
|
7
26
|
static discoveryInProgress = false;
|
|
8
27
|
/**
|
|
9
|
-
* Discovers all installed Aztec wallet extensions
|
|
28
|
+
* Discovers all installed Aztec wallet extensions and establishes secure channel components.
|
|
29
|
+
*
|
|
30
|
+
* This method:
|
|
31
|
+
* 1. Generates an ECDH key pair for this discovery session
|
|
32
|
+
* 2. Broadcasts a discovery request with the dApp's public key
|
|
33
|
+
* 3. Receives responses from wallets with their public keys and MessagePorts
|
|
34
|
+
* 4. Derives shared secrets and computes verification hashes
|
|
35
|
+
*
|
|
10
36
|
* @param chainInfo - Chain information to check if extensions support this network
|
|
11
37
|
* @param timeout - How long to wait for extensions to respond (ms)
|
|
12
|
-
* @returns Array of discovered
|
|
38
|
+
* @returns Array of discovered wallets with their secure channel components
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```typescript
|
|
42
|
+
* const wallets = await ExtensionProvider.discoverExtensions({
|
|
43
|
+
* chainId: Fr(31337),
|
|
44
|
+
* version: Fr(0)
|
|
45
|
+
* });
|
|
46
|
+
* // Access wallet info and secure channel
|
|
47
|
+
* const { info, port, sharedKey } = wallets[0];
|
|
48
|
+
* ```
|
|
13
49
|
*/ static async discoverExtensions(chainInfo, timeout = 1000) {
|
|
14
|
-
// If discovery is in progress, wait
|
|
50
|
+
// If discovery is already in progress, wait and return empty
|
|
51
|
+
// (caller should retry or handle appropriately)
|
|
15
52
|
if (this.discoveryInProgress) {
|
|
16
53
|
await new Promise((resolve)=>setTimeout(resolve, timeout));
|
|
17
|
-
return
|
|
54
|
+
return [];
|
|
18
55
|
}
|
|
19
56
|
this.discoveryInProgress = true;
|
|
20
|
-
this
|
|
57
|
+
// Generate key pair for this discovery session
|
|
58
|
+
const keyPair = await generateKeyPair();
|
|
59
|
+
const exportedPublicKey = await exportPublicKey(keyPair.publicKey);
|
|
21
60
|
const { promise, resolve } = promiseWithResolvers();
|
|
22
61
|
const requestId = globalThis.crypto.randomUUID();
|
|
23
62
|
const responses = [];
|
|
@@ -32,17 +71,46 @@ import { promiseWithResolvers } from '@aztec/foundation/promise';
|
|
|
32
71
|
} catch {
|
|
33
72
|
return;
|
|
34
73
|
}
|
|
35
|
-
if (data.type ===
|
|
36
|
-
|
|
37
|
-
|
|
74
|
+
if (data.type === WalletMessageType.DISCOVERY_RESPONSE && data.requestId === requestId) {
|
|
75
|
+
// Get the MessagePort from the event
|
|
76
|
+
const port = event.ports?.[0];
|
|
77
|
+
if (!port) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
// Derive shared key from wallet's public key
|
|
81
|
+
const walletPublicKey = data.walletInfo.publicKey;
|
|
82
|
+
if (!walletPublicKey) {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
void (async ()=>{
|
|
86
|
+
try {
|
|
87
|
+
const importedWalletKey = await importPublicKey(walletPublicKey);
|
|
88
|
+
const sharedKey = await deriveSharedKey(keyPair.privateKey, importedWalletKey);
|
|
89
|
+
// Compute verification hash
|
|
90
|
+
const verificationHash = await hashSharedSecret(sharedKey);
|
|
91
|
+
// Create wallet info with verification hash
|
|
92
|
+
const walletInfo = {
|
|
93
|
+
...data.walletInfo,
|
|
94
|
+
verificationHash
|
|
95
|
+
};
|
|
96
|
+
responses.push({
|
|
97
|
+
info: walletInfo,
|
|
98
|
+
port,
|
|
99
|
+
sharedKey
|
|
100
|
+
});
|
|
101
|
+
} catch {
|
|
102
|
+
// Failed to derive key, skip this wallet
|
|
103
|
+
}
|
|
104
|
+
})();
|
|
38
105
|
}
|
|
39
106
|
};
|
|
40
107
|
window.addEventListener('message', handleMessage);
|
|
41
|
-
// Send discovery message
|
|
108
|
+
// Send discovery message with our public key
|
|
42
109
|
const discoveryMessage = {
|
|
43
|
-
type:
|
|
110
|
+
type: WalletMessageType.DISCOVERY,
|
|
44
111
|
requestId,
|
|
45
|
-
chainInfo
|
|
112
|
+
chainInfo,
|
|
113
|
+
publicKey: exportedPublicKey
|
|
46
114
|
};
|
|
47
115
|
window.postMessage(jsonStringify(discoveryMessage), '*');
|
|
48
116
|
// Wait for responses
|
|
@@ -1,23 +1,155 @@
|
|
|
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';
|
|
4
|
+
/**
|
|
5
|
+
* Callback type for wallet disconnect events.
|
|
6
|
+
*/
|
|
7
|
+
export type DisconnectCallback = () => void;
|
|
3
8
|
/**
|
|
4
9
|
* A wallet implementation that communicates with browser extension wallets
|
|
5
|
-
*
|
|
10
|
+
* using a secure encrypted MessageChannel.
|
|
11
|
+
*
|
|
12
|
+
* This class uses a pre-established secure channel from the discovery phase:
|
|
13
|
+
*
|
|
14
|
+
* 1. **MessageChannel**: A private communication channel created during discovery,
|
|
15
|
+
* not visible to other scripts on the page (unlike window.postMessage).
|
|
16
|
+
*
|
|
17
|
+
* 2. **ECDH Key Exchange**: The shared secret was derived during discovery using
|
|
18
|
+
* Elliptic Curve Diffie-Hellman key exchange.
|
|
19
|
+
*
|
|
20
|
+
* 3. **AES-GCM Encryption**: All messages are encrypted using AES-256-GCM,
|
|
21
|
+
* providing both confidentiality and authenticity.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```typescript
|
|
25
|
+
* // Discovery returns wallets with secure channel components
|
|
26
|
+
* const wallets = await ExtensionProvider.discoverExtensions(chainInfo);
|
|
27
|
+
* const { info, port, sharedKey } = wallets[0];
|
|
28
|
+
*
|
|
29
|
+
* // User can verify emoji if desired
|
|
30
|
+
* console.log('Verify:', hashToEmoji(info.verificationHash!));
|
|
31
|
+
*
|
|
32
|
+
* // Create wallet using the discovered components
|
|
33
|
+
* const wallet = await ExtensionWallet.create(info, chainInfo, port, sharedKey, 'my-dapp');
|
|
34
|
+
*
|
|
35
|
+
* // All subsequent calls are encrypted
|
|
36
|
+
* const accounts = await wallet.getAccounts();
|
|
37
|
+
* ```
|
|
6
38
|
*/
|
|
7
39
|
export declare class ExtensionWallet {
|
|
8
40
|
private chainInfo;
|
|
9
41
|
private appId;
|
|
10
42
|
private extensionId;
|
|
43
|
+
private port;
|
|
44
|
+
private sharedKey;
|
|
45
|
+
/** Map of pending requests awaiting responses, keyed by message ID */
|
|
11
46
|
private inFlight;
|
|
12
|
-
private
|
|
47
|
+
private disconnected;
|
|
48
|
+
private disconnectCallbacks;
|
|
13
49
|
/**
|
|
14
|
-
*
|
|
50
|
+
* Private constructor - use {@link ExtensionWallet.create} to instantiate.
|
|
15
51
|
* @param chainInfo - The chain information (chainId and version)
|
|
16
|
-
* @param appId - Application identifier for the requesting
|
|
17
|
-
* @param extensionId -
|
|
18
|
-
* @
|
|
52
|
+
* @param appId - Application identifier for the requesting dApp
|
|
53
|
+
* @param extensionId - The unique identifier of the target wallet extension
|
|
54
|
+
* @param port - The MessagePort for private communication with the wallet
|
|
55
|
+
* @param sharedKey - The derived AES-256-GCM shared key for encryption
|
|
56
|
+
*/
|
|
57
|
+
private constructor();
|
|
58
|
+
/** Cached Wallet proxy instance */
|
|
59
|
+
private walletProxy;
|
|
60
|
+
/**
|
|
61
|
+
* Creates an ExtensionWallet instance that communicates with a browser extension
|
|
62
|
+
* over a secure encrypted MessageChannel.
|
|
63
|
+
*
|
|
64
|
+
* @param walletInfo - The wallet info from ExtensionProvider.discoverExtensions()
|
|
65
|
+
* @param chainInfo - The chain information (chainId and version) for request context
|
|
66
|
+
* @param port - The MessagePort for private communication with the wallet
|
|
67
|
+
* @param sharedKey - The derived AES-256-GCM shared key for encryption
|
|
68
|
+
* @param appId - Application identifier used to identify the requesting dApp to the wallet
|
|
69
|
+
* @returns The ExtensionWallet instance. Use {@link getWallet} to get the Wallet interface.
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```typescript
|
|
73
|
+
* const wallets = await ExtensionProvider.discoverExtensions(chainInfo);
|
|
74
|
+
* const { info, port, sharedKey } = wallets[0];
|
|
75
|
+
* const extensionWallet = ExtensionWallet.create(
|
|
76
|
+
* info,
|
|
77
|
+
* { chainId: Fr(31337), version: Fr(0) },
|
|
78
|
+
* port,
|
|
79
|
+
* sharedKey,
|
|
80
|
+
* 'my-defi-app'
|
|
81
|
+
* );
|
|
82
|
+
*
|
|
83
|
+
* // Register disconnect handler
|
|
84
|
+
* extensionWallet.onDisconnect(() => console.log('Disconnected!'));
|
|
85
|
+
*
|
|
86
|
+
* // Get the Wallet interface for dApp usage
|
|
87
|
+
* const wallet = extensionWallet.getWallet();
|
|
88
|
+
* const accounts = await wallet.getAccounts();
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
static create(walletInfo: WalletInfo, chainInfo: ChainInfo, port: MessagePort, sharedKey: CryptoKey, appId: string): ExtensionWallet;
|
|
92
|
+
/**
|
|
93
|
+
* Returns a Wallet interface that proxies all method calls through the secure channel.
|
|
94
|
+
*
|
|
95
|
+
* The returned Wallet can be used directly by dApps - all method calls are automatically
|
|
96
|
+
* encrypted and sent to the wallet extension.
|
|
97
|
+
*
|
|
98
|
+
* @returns A Wallet implementation that encrypts all communication
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* ```typescript
|
|
102
|
+
* const extensionWallet = ExtensionWallet.create(info, chainInfo, port, sharedKey, 'my-app');
|
|
103
|
+
* const wallet = extensionWallet.getWallet();
|
|
104
|
+
* const accounts = await wallet.getAccounts();
|
|
105
|
+
* ```
|
|
19
106
|
*/
|
|
20
|
-
|
|
107
|
+
getWallet(): Wallet;
|
|
108
|
+
private handleEncryptedResponse;
|
|
21
109
|
private postMessage;
|
|
110
|
+
/**
|
|
111
|
+
* Handles wallet disconnection.
|
|
112
|
+
* Rejects all pending requests and notifies registered callbacks.
|
|
113
|
+
* @internal
|
|
114
|
+
*/
|
|
115
|
+
private handleDisconnect;
|
|
116
|
+
/**
|
|
117
|
+
* Registers a callback to be invoked when the wallet disconnects.
|
|
118
|
+
*
|
|
119
|
+
* @param callback - Function to call when wallet disconnects
|
|
120
|
+
* @returns A function to unregister the callback
|
|
121
|
+
*
|
|
122
|
+
* @example
|
|
123
|
+
* ```typescript
|
|
124
|
+
* const wallet = await ExtensionWallet.create(...);
|
|
125
|
+
* const unsubscribe = wallet.onDisconnect(() => {
|
|
126
|
+
* console.log('Wallet disconnected! Please reconnect.');
|
|
127
|
+
* // Clean up UI, prompt user to reconnect, etc.
|
|
128
|
+
* });
|
|
129
|
+
* // Later: unsubscribe(); to stop receiving notifications
|
|
130
|
+
* ```
|
|
131
|
+
*/
|
|
132
|
+
onDisconnect(callback: DisconnectCallback): () => void;
|
|
133
|
+
/**
|
|
134
|
+
* Returns whether the wallet has been disconnected.
|
|
135
|
+
*
|
|
136
|
+
* @returns true if the wallet is no longer connected
|
|
137
|
+
*/
|
|
138
|
+
isDisconnected(): boolean;
|
|
139
|
+
/**
|
|
140
|
+
* Disconnects from the wallet and cleans up resources.
|
|
141
|
+
*
|
|
142
|
+
* This method notifies the wallet extension that the session is ending,
|
|
143
|
+
* allowing it to clean up its state. After calling this method, the wallet
|
|
144
|
+
* instance can no longer be used and any pending requests will be rejected.
|
|
145
|
+
*
|
|
146
|
+
* @example
|
|
147
|
+
* ```typescript
|
|
148
|
+
* const wallet = await provider.connect('my-app');
|
|
149
|
+
* // ... use wallet ...
|
|
150
|
+
* await wallet.disconnect(); // Clean disconnect when done
|
|
151
|
+
* ```
|
|
152
|
+
*/
|
|
153
|
+
disconnect(): Promise<void>;
|
|
22
154
|
}
|
|
23
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
155
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZXh0ZW5zaW9uX3dhbGxldC5kLnRzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vc3JjL3Byb3ZpZGVycy9leHRlbnNpb24vZXh0ZW5zaW9uX3dhbGxldC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxPQUFPLEtBQUssRUFBRSxTQUFTLEVBQUUsTUFBTSx5QkFBeUIsQ0FBQztBQUN6RCxPQUFPLEVBQUUsS0FBSyxNQUFNLEVBQWdCLE1BQU0sd0JBQXdCLENBQUM7QUFPbkUsT0FBTyxFQUFFLEtBQUssVUFBVSxFQUE4RCxNQUFNLGdCQUFnQixDQUFDO0FBYTdHOztHQUVHO0FBQ0gsTUFBTSxNQUFNLGtCQUFrQixHQUFHLE1BQU0sSUFBSSxDQUFDO0FBRTVDOzs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7R0E4Qkc7QUFDSCxxQkFBYSxlQUFlO0lBZXhCLE9BQU8sQ0FBQyxTQUFTO0lBQ2pCLE9BQU8sQ0FBQyxLQUFLO0lBQ2IsT0FBTyxDQUFDLFdBQVc7SUFDbkIsT0FBTyxDQUFDLElBQUk7SUFDWixPQUFPLENBQUMsU0FBUztJQWxCbkIsc0VBQXNFO0lBQ3RFLE9BQU8sQ0FBQyxRQUFRLENBQW9EO0lBQ3BFLE9BQU8sQ0FBQyxZQUFZLENBQVM7SUFDN0IsT0FBTyxDQUFDLG1CQUFtQixDQUE0QjtJQUV2RDs7Ozs7OztPQU9HO0lBQ0gsT0FBTyxlQU1IO0lBRUosbUNBQW1DO0lBQ25DLE9BQU8sQ0FBQyxXQUFXLENBQXVCO0lBRTFDOzs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7T0E4Qkc7SUFDSCxNQUFNLENBQUMsTUFBTSxDQUNYLFVBQVUsRUFBRSxVQUFVLEVBQ3RCLFNBQVMsRUFBRSxTQUFTLEVBQ3BCLElBQUksRUFBRSxXQUFXLEVBQ2pCLFNBQVMsRUFBRSxTQUFTLEVBQ3BCLEtBQUssRUFBRSxNQUFNLEdBQ1osZUFBZSxDQVdqQjtJQUVEOzs7Ozs7Ozs7Ozs7OztPQWNHO0lBQ0gsU0FBUyxJQUFJLE1BQU0sQ0F1QmxCO1lBVWEsdUJBQXVCO1lBMER2QixXQUFXO0lBMkJ6Qjs7OztPQUlHO0lBQ0gsT0FBTyxDQUFDLGdCQUFnQjtJQStCeEI7Ozs7Ozs7Ozs7Ozs7OztPQWVHO0lBQ0gsWUFBWSxDQUFDLFFBQVEsRUFBRSxrQkFBa0IsR0FBRyxNQUFNLElBQUksQ0FRckQ7SUFFRDs7OztPQUlHO0lBQ0gsY0FBYyxJQUFJLE9BQU8sQ0FFeEI7SUFFRDs7Ozs7Ozs7Ozs7OztPQWFHO0lBQ0csVUFBVSxJQUFJLE9BQU8sQ0FBQyxJQUFJLENBQUMsQ0EyQmhDO0NBQ0YifQ==
|
|
@@ -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;
|
|
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;AAOnE,OAAO,EAAE,KAAK,UAAU,EAA8D,MAAM,gBAAgB,CAAC;AAa7G;;GAEG;AACH,MAAM,MAAM,kBAAkB,GAAG,MAAM,IAAI,CAAC;AAE5C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,qBAAa,eAAe;IAexB,OAAO,CAAC,SAAS;IACjB,OAAO,CAAC,KAAK;IACb,OAAO,CAAC,WAAW;IACnB,OAAO,CAAC,IAAI;IACZ,OAAO,CAAC,SAAS;IAlBnB,sEAAsE;IACtE,OAAO,CAAC,QAAQ,CAAoD;IACpE,OAAO,CAAC,YAAY,CAAS;IAC7B,OAAO,CAAC,mBAAmB,CAA4B;IAEvD;;;;;;;OAOG;IACH,OAAO,eAMH;IAEJ,mCAAmC;IACnC,OAAO,CAAC,WAAW,CAAuB;IAE1C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA8BG;IACH,MAAM,CAAC,MAAM,CACX,UAAU,EAAE,UAAU,EACtB,SAAS,EAAE,SAAS,EACpB,IAAI,EAAE,WAAW,EACjB,SAAS,EAAE,SAAS,EACpB,KAAK,EAAE,MAAM,GACZ,eAAe,CAWjB;IAED;;;;;;;;;;;;;;OAcG;IACH,SAAS,IAAI,MAAM,CAuBlB;YAUa,uBAAuB;YA0DvB,WAAW;IA2BzB;;;;OAIG;IACH,OAAO,CAAC,gBAAgB;IA+BxB;;;;;;;;;;;;;;;OAeG;IACH,YAAY,CAAC,QAAQ,EAAE,kBAAkB,GAAG,MAAM,IAAI,CAQrD;IAED;;;;OAIG;IACH,cAAc,IAAI,OAAO,CAExB;IAED;;;;;;;;;;;;;OAaG;IACG,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC,CA2BhC;CACF"}
|
|
@@ -2,63 +2,125 @@ import { WalletSchema } from '@aztec/aztec.js/wallet';
|
|
|
2
2
|
import { jsonStringify } from '@aztec/foundation/json-rpc';
|
|
3
3
|
import { promiseWithResolvers } from '@aztec/foundation/promise';
|
|
4
4
|
import { schemaHasMethod } from '@aztec/foundation/schemas';
|
|
5
|
+
import { decrypt, encrypt } from '../../crypto.js';
|
|
6
|
+
import { WalletMessageType } from '../../types.js';
|
|
5
7
|
/**
|
|
6
8
|
* A wallet implementation that communicates with browser extension wallets
|
|
7
|
-
*
|
|
9
|
+
* using a secure encrypted MessageChannel.
|
|
10
|
+
*
|
|
11
|
+
* This class uses a pre-established secure channel from the discovery phase:
|
|
12
|
+
*
|
|
13
|
+
* 1. **MessageChannel**: A private communication channel created during discovery,
|
|
14
|
+
* not visible to other scripts on the page (unlike window.postMessage).
|
|
15
|
+
*
|
|
16
|
+
* 2. **ECDH Key Exchange**: The shared secret was derived during discovery using
|
|
17
|
+
* Elliptic Curve Diffie-Hellman key exchange.
|
|
18
|
+
*
|
|
19
|
+
* 3. **AES-GCM Encryption**: All messages are encrypted using AES-256-GCM,
|
|
20
|
+
* providing both confidentiality and authenticity.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```typescript
|
|
24
|
+
* // Discovery returns wallets with secure channel components
|
|
25
|
+
* const wallets = await ExtensionProvider.discoverExtensions(chainInfo);
|
|
26
|
+
* const { info, port, sharedKey } = wallets[0];
|
|
27
|
+
*
|
|
28
|
+
* // User can verify emoji if desired
|
|
29
|
+
* console.log('Verify:', hashToEmoji(info.verificationHash!));
|
|
30
|
+
*
|
|
31
|
+
* // Create wallet using the discovered components
|
|
32
|
+
* const wallet = await ExtensionWallet.create(info, chainInfo, port, sharedKey, 'my-dapp');
|
|
33
|
+
*
|
|
34
|
+
* // All subsequent calls are encrypted
|
|
35
|
+
* const accounts = await wallet.getAccounts();
|
|
36
|
+
* ```
|
|
8
37
|
*/ export class ExtensionWallet {
|
|
9
38
|
chainInfo;
|
|
10
39
|
appId;
|
|
11
40
|
extensionId;
|
|
12
|
-
|
|
13
|
-
|
|
41
|
+
port;
|
|
42
|
+
sharedKey;
|
|
43
|
+
/** Map of pending requests awaiting responses, keyed by message ID */ inFlight;
|
|
44
|
+
disconnected;
|
|
45
|
+
disconnectCallbacks;
|
|
46
|
+
/**
|
|
47
|
+
* Private constructor - use {@link ExtensionWallet.create} to instantiate.
|
|
48
|
+
* @param chainInfo - The chain information (chainId and version)
|
|
49
|
+
* @param appId - Application identifier for the requesting dApp
|
|
50
|
+
* @param extensionId - The unique identifier of the target wallet extension
|
|
51
|
+
* @param port - The MessagePort for private communication with the wallet
|
|
52
|
+
* @param sharedKey - The derived AES-256-GCM shared key for encryption
|
|
53
|
+
*/ constructor(chainInfo, appId, extensionId, port, sharedKey){
|
|
14
54
|
this.chainInfo = chainInfo;
|
|
15
55
|
this.appId = appId;
|
|
16
56
|
this.extensionId = extensionId;
|
|
57
|
+
this.port = port;
|
|
58
|
+
this.sharedKey = sharedKey;
|
|
17
59
|
this.inFlight = new Map();
|
|
60
|
+
this.disconnected = false;
|
|
61
|
+
this.disconnectCallbacks = [];
|
|
62
|
+
this.walletProxy = null;
|
|
18
63
|
}
|
|
64
|
+
/** Cached Wallet proxy instance */ walletProxy;
|
|
19
65
|
/**
|
|
20
|
-
* Creates an ExtensionWallet instance that
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
* @param
|
|
24
|
-
* @
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
66
|
+
* Creates an ExtensionWallet instance that communicates with a browser extension
|
|
67
|
+
* over a secure encrypted MessageChannel.
|
|
68
|
+
*
|
|
69
|
+
* @param walletInfo - The wallet info from ExtensionProvider.discoverExtensions()
|
|
70
|
+
* @param chainInfo - The chain information (chainId and version) for request context
|
|
71
|
+
* @param port - The MessagePort for private communication with the wallet
|
|
72
|
+
* @param sharedKey - The derived AES-256-GCM shared key for encryption
|
|
73
|
+
* @param appId - Application identifier used to identify the requesting dApp to the wallet
|
|
74
|
+
* @returns The ExtensionWallet instance. Use {@link getWallet} to get the Wallet interface.
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* ```typescript
|
|
78
|
+
* const wallets = await ExtensionProvider.discoverExtensions(chainInfo);
|
|
79
|
+
* const { info, port, sharedKey } = wallets[0];
|
|
80
|
+
* const extensionWallet = ExtensionWallet.create(
|
|
81
|
+
* info,
|
|
82
|
+
* { chainId: Fr(31337), version: Fr(0) },
|
|
83
|
+
* port,
|
|
84
|
+
* sharedKey,
|
|
85
|
+
* 'my-defi-app'
|
|
86
|
+
* );
|
|
87
|
+
*
|
|
88
|
+
* // Register disconnect handler
|
|
89
|
+
* extensionWallet.onDisconnect(() => console.log('Disconnected!'));
|
|
90
|
+
*
|
|
91
|
+
* // Get the Wallet interface for dApp usage
|
|
92
|
+
* const wallet = extensionWallet.getWallet();
|
|
93
|
+
* const accounts = await wallet.getAccounts();
|
|
94
|
+
* ```
|
|
95
|
+
*/ static create(walletInfo, chainInfo, port, sharedKey, appId) {
|
|
96
|
+
const wallet = new ExtensionWallet(chainInfo, appId, walletInfo.id, port, sharedKey);
|
|
97
|
+
// Set up message handler - all messages are now encrypted
|
|
98
|
+
wallet.port.onmessage = (event)=>{
|
|
99
|
+
void wallet.handleEncryptedResponse(event.data);
|
|
100
|
+
};
|
|
101
|
+
wallet.port.start();
|
|
102
|
+
return wallet;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Returns a Wallet interface that proxies all method calls through the secure channel.
|
|
106
|
+
*
|
|
107
|
+
* The returned Wallet can be used directly by dApps - all method calls are automatically
|
|
108
|
+
* encrypted and sent to the wallet extension.
|
|
109
|
+
*
|
|
110
|
+
* @returns A Wallet implementation that encrypts all communication
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* ```typescript
|
|
114
|
+
* const extensionWallet = ExtensionWallet.create(info, chainInfo, port, sharedKey, 'my-app');
|
|
115
|
+
* const wallet = extensionWallet.getWallet();
|
|
116
|
+
* const accounts = await wallet.getAccounts();
|
|
117
|
+
* ```
|
|
118
|
+
*/ getWallet() {
|
|
119
|
+
if (this.walletProxy) {
|
|
120
|
+
return this.walletProxy;
|
|
121
|
+
}
|
|
60
122
|
// Create a Proxy that intercepts wallet method calls and forwards them to the extension
|
|
61
|
-
|
|
123
|
+
this.walletProxy = new Proxy(this, {
|
|
62
124
|
get: (target, prop)=>{
|
|
63
125
|
if (schemaHasMethod(WalletSchema, prop.toString())) {
|
|
64
126
|
return async (...args)=>{
|
|
@@ -73,8 +135,65 @@ import { schemaHasMethod } from '@aztec/foundation/schemas';
|
|
|
73
135
|
}
|
|
74
136
|
}
|
|
75
137
|
});
|
|
138
|
+
return this.walletProxy;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Handles an encrypted response received from the wallet extension.
|
|
142
|
+
*
|
|
143
|
+
* Decrypts the response using the shared AES key and resolves or rejects
|
|
144
|
+
* the corresponding pending promise based on the response content.
|
|
145
|
+
*
|
|
146
|
+
* @param encrypted - The encrypted response from the wallet
|
|
147
|
+
*/ async handleEncryptedResponse(encrypted) {
|
|
148
|
+
if (!this.sharedKey) {
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
try {
|
|
152
|
+
const response = await decrypt(this.sharedKey, encrypted);
|
|
153
|
+
const { messageId, result, error, walletId: responseWalletId } = response;
|
|
154
|
+
// Check for disconnect notification from the wallet backend
|
|
155
|
+
// This is sent as an encrypted error response with a special type
|
|
156
|
+
if (error && typeof error === 'object' && 'type' in error && error.type === WalletMessageType.SESSION_DISCONNECTED) {
|
|
157
|
+
this.handleDisconnect();
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
if (!messageId || !responseWalletId) {
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
if (this.extensionId !== responseWalletId) {
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
if (!this.inFlight.has(messageId)) {
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
const { resolve, reject } = this.inFlight.get(messageId);
|
|
170
|
+
if (error) {
|
|
171
|
+
reject(new Error(jsonStringify(error)));
|
|
172
|
+
} else {
|
|
173
|
+
resolve(result);
|
|
174
|
+
}
|
|
175
|
+
this.inFlight.delete(messageId);
|
|
176
|
+
// eslint-disable-next-line no-empty
|
|
177
|
+
} catch {}
|
|
76
178
|
}
|
|
77
|
-
|
|
179
|
+
/**
|
|
180
|
+
* Sends an encrypted wallet method call over the secure MessageChannel.
|
|
181
|
+
*
|
|
182
|
+
* The message is encrypted using AES-256-GCM with the shared key derived
|
|
183
|
+
* during discovery. A unique message ID is generated to correlate
|
|
184
|
+
* the response.
|
|
185
|
+
*
|
|
186
|
+
* @param call - The wallet method call containing method name and arguments
|
|
187
|
+
* @returns A Promise that resolves with the decrypted result from the wallet
|
|
188
|
+
*
|
|
189
|
+
* @throws Error if the secure channel has not been established or wallet is disconnected
|
|
190
|
+
*/ async postMessage(call) {
|
|
191
|
+
if (this.disconnected) {
|
|
192
|
+
throw new Error('Wallet has been disconnected');
|
|
193
|
+
}
|
|
194
|
+
if (!this.port || !this.sharedKey) {
|
|
195
|
+
throw new Error('Secure channel not established');
|
|
196
|
+
}
|
|
78
197
|
const messageId = globalThis.crypto.randomUUID();
|
|
79
198
|
const message = {
|
|
80
199
|
type: call.type,
|
|
@@ -84,7 +203,9 @@ import { schemaHasMethod } from '@aztec/foundation/schemas';
|
|
|
84
203
|
appId: this.appId,
|
|
85
204
|
walletId: this.extensionId
|
|
86
205
|
};
|
|
87
|
-
|
|
206
|
+
// Encrypt the message and send over the private MessageChannel
|
|
207
|
+
const encrypted = await encrypt(this.sharedKey, message);
|
|
208
|
+
this.port.postMessage(encrypted);
|
|
88
209
|
const { promise, resolve, reject } = promiseWithResolvers();
|
|
89
210
|
this.inFlight.set(messageId, {
|
|
90
211
|
promise,
|
|
@@ -93,4 +214,105 @@ import { schemaHasMethod } from '@aztec/foundation/schemas';
|
|
|
93
214
|
});
|
|
94
215
|
return promise;
|
|
95
216
|
}
|
|
217
|
+
/**
|
|
218
|
+
* Handles wallet disconnection.
|
|
219
|
+
* Rejects all pending requests and notifies registered callbacks.
|
|
220
|
+
* @internal
|
|
221
|
+
*/ handleDisconnect() {
|
|
222
|
+
if (this.disconnected) {
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
this.disconnected = true;
|
|
226
|
+
// Close the port to prevent any further messages
|
|
227
|
+
if (this.port) {
|
|
228
|
+
this.port.onmessage = null;
|
|
229
|
+
this.port.close();
|
|
230
|
+
}
|
|
231
|
+
// Reject all pending requests
|
|
232
|
+
// Note: These rejections should be caught by the callers, but we log them
|
|
233
|
+
// here to help with debugging if they become unhandled
|
|
234
|
+
const error = new Error('Wallet disconnected');
|
|
235
|
+
for (const { reject } of this.inFlight.values()){
|
|
236
|
+
reject(error);
|
|
237
|
+
}
|
|
238
|
+
this.inFlight.clear();
|
|
239
|
+
// Notify registered callbacks
|
|
240
|
+
for (const callback of this.disconnectCallbacks){
|
|
241
|
+
try {
|
|
242
|
+
callback();
|
|
243
|
+
} catch {
|
|
244
|
+
// Ignore errors in callbacks
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* Registers a callback to be invoked when the wallet disconnects.
|
|
250
|
+
*
|
|
251
|
+
* @param callback - Function to call when wallet disconnects
|
|
252
|
+
* @returns A function to unregister the callback
|
|
253
|
+
*
|
|
254
|
+
* @example
|
|
255
|
+
* ```typescript
|
|
256
|
+
* const wallet = await ExtensionWallet.create(...);
|
|
257
|
+
* const unsubscribe = wallet.onDisconnect(() => {
|
|
258
|
+
* console.log('Wallet disconnected! Please reconnect.');
|
|
259
|
+
* // Clean up UI, prompt user to reconnect, etc.
|
|
260
|
+
* });
|
|
261
|
+
* // Later: unsubscribe(); to stop receiving notifications
|
|
262
|
+
* ```
|
|
263
|
+
*/ onDisconnect(callback) {
|
|
264
|
+
this.disconnectCallbacks.push(callback);
|
|
265
|
+
return ()=>{
|
|
266
|
+
const index = this.disconnectCallbacks.indexOf(callback);
|
|
267
|
+
if (index !== -1) {
|
|
268
|
+
this.disconnectCallbacks.splice(index, 1);
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* Returns whether the wallet has been disconnected.
|
|
274
|
+
*
|
|
275
|
+
* @returns true if the wallet is no longer connected
|
|
276
|
+
*/ isDisconnected() {
|
|
277
|
+
return this.disconnected;
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* Disconnects from the wallet and cleans up resources.
|
|
281
|
+
*
|
|
282
|
+
* This method notifies the wallet extension that the session is ending,
|
|
283
|
+
* allowing it to clean up its state. After calling this method, the wallet
|
|
284
|
+
* instance can no longer be used and any pending requests will be rejected.
|
|
285
|
+
*
|
|
286
|
+
* @example
|
|
287
|
+
* ```typescript
|
|
288
|
+
* const wallet = await provider.connect('my-app');
|
|
289
|
+
* // ... use wallet ...
|
|
290
|
+
* await wallet.disconnect(); // Clean disconnect when done
|
|
291
|
+
* ```
|
|
292
|
+
*/ async disconnect() {
|
|
293
|
+
if (this.disconnected) {
|
|
294
|
+
return;
|
|
295
|
+
}
|
|
296
|
+
// Send disconnect message to extension before closing
|
|
297
|
+
if (this.port && this.sharedKey) {
|
|
298
|
+
try {
|
|
299
|
+
const message = {
|
|
300
|
+
type: WalletMessageType.DISCONNECT,
|
|
301
|
+
messageId: globalThis.crypto.randomUUID(),
|
|
302
|
+
chainInfo: this.chainInfo,
|
|
303
|
+
appId: this.appId,
|
|
304
|
+
walletId: this.extensionId,
|
|
305
|
+
args: []
|
|
306
|
+
};
|
|
307
|
+
const encrypted = await encrypt(this.sharedKey, message);
|
|
308
|
+
this.port.postMessage(encrypted);
|
|
309
|
+
} catch {
|
|
310
|
+
// Ignore errors sending disconnect message
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
this.handleDisconnect();
|
|
314
|
+
if (this.port) {
|
|
315
|
+
this.port.close();
|
|
316
|
+
}
|
|
317
|
+
}
|
|
96
318
|
}
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
export { ExtensionWallet } from './extension_wallet.js';
|
|
2
|
-
export { ExtensionProvider } from './extension_provider.js';
|
|
3
|
-
export
|
|
4
|
-
|
|
1
|
+
export { ExtensionWallet, type DisconnectCallback } from './extension_wallet.js';
|
|
2
|
+
export { ExtensionProvider, type DiscoveredWallet } from './extension_provider.js';
|
|
3
|
+
export * from '../../crypto.js';
|
|
4
|
+
export { WalletMessageType } from '../../types.js';
|
|
5
|
+
export type { WalletInfo, WalletMessage, WalletResponse, DiscoveryRequest, DiscoveryResponse } from '../../types.js';
|
|
6
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uL3NyYy9wcm92aWRlcnMvZXh0ZW5zaW9uL2luZGV4LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQU8sRUFBRSxlQUFlLEVBQUUsS0FBSyxrQkFBa0IsRUFBRSxNQUFNLHVCQUF1QixDQUFDO0FBQ2pGLE9BQU8sRUFBRSxpQkFBaUIsRUFBRSxLQUFLLGdCQUFnQixFQUFFLE1BQU0seUJBQXlCLENBQUM7QUFDbkYsY0FBYyxpQkFBaUIsQ0FBQztBQUNoQyxPQUFPLEVBQUUsaUJBQWlCLEVBQUUsTUFBTSxnQkFBZ0IsQ0FBQztBQUNuRCxZQUFZLEVBQUUsVUFBVSxFQUFFLGFBQWEsRUFBRSxjQUFjLEVBQUUsZ0JBQWdCLEVBQUUsaUJBQWlCLEVBQUUsTUFBTSxnQkFBZ0IsQ0FBQyJ9
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/providers/extension/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/providers/extension/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,KAAK,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AACjF,OAAO,EAAE,iBAAiB,EAAE,KAAK,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AACnF,cAAc,iBAAiB,CAAC;AAChC,OAAO,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AACnD,YAAY,EAAE,UAAU,EAAE,aAAa,EAAE,cAAc,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC"}
|