@aztec/wallet-sdk 0.0.1-commit.d431d1c → 0.0.1-commit.db765a8
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 +217 -294
- package/dest/base-wallet/base_wallet.d.ts +47 -9
- package/dest/base-wallet/base_wallet.d.ts.map +1 -1
- package/dest/base-wallet/base_wallet.js +120 -18
- package/dest/base-wallet/index.d.ts +2 -1
- package/dest/base-wallet/index.d.ts.map +1 -1
- package/dest/base-wallet/index.js +1 -0
- package/dest/base-wallet/utils.d.ts +49 -0
- package/dest/base-wallet/utils.d.ts.map +1 -0
- package/dest/base-wallet/utils.js +131 -0
- package/dest/crypto.d.ts +59 -50
- package/dest/crypto.d.ts.map +1 -1
- package/dest/crypto.js +202 -108
- package/dest/emoji_alphabet.d.ts +35 -0
- package/dest/emoji_alphabet.d.ts.map +1 -0
- package/dest/emoji_alphabet.js +299 -0
- package/dest/extension/handlers/background_connection_handler.d.ts +158 -0
- package/dest/extension/handlers/background_connection_handler.d.ts.map +1 -0
- package/dest/extension/handlers/background_connection_handler.js +258 -0
- package/dest/extension/handlers/content_script_connection_handler.d.ts +56 -0
- package/dest/extension/handlers/content_script_connection_handler.d.ts.map +1 -0
- package/dest/extension/handlers/content_script_connection_handler.js +174 -0
- package/dest/extension/handlers/index.d.ts +12 -0
- package/dest/extension/handlers/index.d.ts.map +1 -0
- package/dest/extension/handlers/index.js +10 -0
- package/dest/extension/handlers/internal_message_types.d.ts +63 -0
- package/dest/extension/handlers/internal_message_types.d.ts.map +1 -0
- package/dest/extension/handlers/internal_message_types.js +22 -0
- package/dest/extension/provider/extension_provider.d.ts +107 -0
- package/dest/extension/provider/extension_provider.d.ts.map +1 -0
- package/dest/extension/provider/extension_provider.js +160 -0
- package/dest/extension/provider/extension_wallet.d.ts +132 -0
- package/dest/extension/provider/extension_wallet.d.ts.map +1 -0
- package/dest/{providers/extension → extension/provider}/extension_wallet.js +57 -97
- package/dest/extension/provider/index.d.ts +3 -0
- package/dest/extension/provider/index.d.ts.map +1 -0
- package/dest/{providers/extension → extension/provider}/index.js +0 -2
- package/dest/manager/index.d.ts +2 -8
- package/dest/manager/index.d.ts.map +1 -1
- package/dest/manager/index.js +0 -6
- package/dest/manager/types.d.ts +91 -9
- package/dest/manager/types.d.ts.map +1 -1
- package/dest/manager/types.js +17 -1
- package/dest/manager/wallet_manager.d.ts +50 -7
- package/dest/manager/wallet_manager.d.ts.map +1 -1
- package/dest/manager/wallet_manager.js +174 -44
- package/dest/types.d.ts +43 -12
- package/dest/types.d.ts.map +1 -1
- package/dest/types.js +3 -2
- package/package.json +17 -10
- package/src/base-wallet/base_wallet.ts +154 -35
- package/src/base-wallet/index.ts +1 -0
- package/src/base-wallet/utils.ts +238 -0
- package/src/crypto.ts +237 -113
- package/src/emoji_alphabet.ts +317 -0
- package/src/extension/handlers/background_connection_handler.ts +423 -0
- package/src/extension/handlers/content_script_connection_handler.ts +246 -0
- package/src/extension/handlers/index.ts +25 -0
- package/src/extension/handlers/internal_message_types.ts +69 -0
- package/src/extension/provider/extension_provider.ts +233 -0
- package/src/{providers/extension → extension/provider}/extension_wallet.ts +61 -111
- package/src/extension/provider/index.ts +7 -0
- package/src/manager/index.ts +2 -10
- package/src/manager/types.ts +94 -8
- package/src/manager/wallet_manager.ts +190 -46
- package/src/types.ts +44 -10
- package/dest/providers/extension/extension_provider.d.ts +0 -63
- package/dest/providers/extension/extension_provider.d.ts.map +0 -1
- package/dest/providers/extension/extension_provider.js +0 -124
- package/dest/providers/extension/extension_wallet.d.ts +0 -155
- package/dest/providers/extension/extension_wallet.d.ts.map +0 -1
- package/dest/providers/extension/index.d.ts +0 -6
- package/dest/providers/extension/index.d.ts.map +0 -1
- package/src/providers/extension/extension_provider.ts +0 -167
- package/src/providers/extension/index.ts +0 -5
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import type { ChainInfo } from '@aztec/aztec.js/account';
|
|
2
|
+
import { type ConnectedWalletInfo, type WalletInfo } from '../../types.js';
|
|
3
|
+
/**
|
|
4
|
+
* A discovered wallet before key exchange.
|
|
5
|
+
* Has basic info and MessagePort, but no shared key yet.
|
|
6
|
+
*
|
|
7
|
+
* Call {@link establishSecureChannel} to perform key exchange and get a connected wallet.
|
|
8
|
+
*/
|
|
9
|
+
export declare class DiscoveredWallet {
|
|
10
|
+
/** Basic wallet information (id, name, icon, version) */
|
|
11
|
+
readonly info: WalletInfo;
|
|
12
|
+
/** The MessagePort for private communication with the wallet */
|
|
13
|
+
readonly port: MessagePort;
|
|
14
|
+
/** Request ID for correlation */
|
|
15
|
+
readonly requestId: string;
|
|
16
|
+
constructor(
|
|
17
|
+
/** Basic wallet information (id, name, icon, version) */
|
|
18
|
+
info: WalletInfo,
|
|
19
|
+
/** The MessagePort for private communication with the wallet */
|
|
20
|
+
port: MessagePort,
|
|
21
|
+
/** Request ID for correlation */
|
|
22
|
+
requestId: string);
|
|
23
|
+
/**
|
|
24
|
+
* Establishes a secure connection with this wallet.
|
|
25
|
+
*
|
|
26
|
+
* This method:
|
|
27
|
+
* 1. Generates an ECDH key pair
|
|
28
|
+
* 2. Sends public key to wallet over the MessagePort
|
|
29
|
+
* 3. Receives wallet's public key
|
|
30
|
+
* 4. Derives shared secret and computes verification hash locally
|
|
31
|
+
*
|
|
32
|
+
* **IMPORTANT**: Has a 2 second timeout for MITM defense.
|
|
33
|
+
* Both parties must exchange keys relatively quickly.
|
|
34
|
+
*
|
|
35
|
+
* The verification hash is computed independently by both parties
|
|
36
|
+
* and should be displayed to the user for visual comparison.
|
|
37
|
+
*
|
|
38
|
+
* @returns Connected wallet with shared key and verification hash
|
|
39
|
+
* @throws Error if key exchange fails or times out
|
|
40
|
+
*/
|
|
41
|
+
establishSecureChannel(): Promise<ConnectedWallet>;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* A fully connected wallet with secure channel established.
|
|
45
|
+
* Available after key exchange completes.
|
|
46
|
+
*/
|
|
47
|
+
export interface ConnectedWallet {
|
|
48
|
+
/** Full wallet info including public key and verification hash */
|
|
49
|
+
info: ConnectedWalletInfo;
|
|
50
|
+
/** The MessagePort for encrypted communication */
|
|
51
|
+
port: MessagePort;
|
|
52
|
+
/** The derived AES-256-GCM shared key for encryption */
|
|
53
|
+
sharedKey: CryptoKey;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Options for wallet discovery.
|
|
57
|
+
*/
|
|
58
|
+
export interface DiscoveryOptions {
|
|
59
|
+
/** Application ID making the request */
|
|
60
|
+
appId: string;
|
|
61
|
+
/** How long to wait for user approval (ms). Default: 60000 (60s) */
|
|
62
|
+
timeout?: number;
|
|
63
|
+
/**
|
|
64
|
+
* Callback invoked when a wallet is discovered.
|
|
65
|
+
* Wallets are streamed as users approve them.
|
|
66
|
+
*/
|
|
67
|
+
onWalletDiscovered?: (wallet: DiscoveredWallet) => void;
|
|
68
|
+
/**
|
|
69
|
+
* AbortSignal for cancelling discovery early.
|
|
70
|
+
* When aborted, cleanup happens immediately instead of waiting for timeout.
|
|
71
|
+
*/
|
|
72
|
+
signal?: AbortSignal;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Provider for discovering Aztec wallet extensions.
|
|
76
|
+
*
|
|
77
|
+
* NOTE: Most users should use WalletManager instead of this class directly.
|
|
78
|
+
* WalletManager provides a higher-level API with streaming support.
|
|
79
|
+
*
|
|
80
|
+
* The connection flow is split into two phases for security:
|
|
81
|
+
*
|
|
82
|
+
* 1. **Discovery Phase** ({@link discoverWallets}):
|
|
83
|
+
* - Broadcasts a discovery request (NO public keys)
|
|
84
|
+
* - Wallet shows pending request to user
|
|
85
|
+
* - User must approve before wallet reveals itself
|
|
86
|
+
* - Wallets are streamed via callback as they're approved
|
|
87
|
+
*
|
|
88
|
+
* 2. **Secure Channel Phase** ({@link DiscoveredWallet.establishSecureChannel}):
|
|
89
|
+
* - Performs ECDH key exchange over private MessageChannel
|
|
90
|
+
* - Both parties compute verification hash locally
|
|
91
|
+
* - Has a 2s timeout for MITM defense
|
|
92
|
+
* - Returns connected wallet with shared key and verification hash
|
|
93
|
+
*/
|
|
94
|
+
export declare class ExtensionProvider {
|
|
95
|
+
/**
|
|
96
|
+
* Discovers wallet extensions that user has approved.
|
|
97
|
+
*
|
|
98
|
+
* Wallets are streamed via the `onWalletDiscovered` callback as users approve them.
|
|
99
|
+
* The promise resolves when the timeout expires or signal is aborted.
|
|
100
|
+
*
|
|
101
|
+
* @param chainInfo - Chain information to check if extensions support this network
|
|
102
|
+
* @param options - Discovery options including appId, appName, timeout, and callback
|
|
103
|
+
* @returns Promise that resolves when discovery completes
|
|
104
|
+
*/
|
|
105
|
+
static discoverWallets(chainInfo: ChainInfo, options: DiscoveryOptions): Promise<void>;
|
|
106
|
+
}
|
|
107
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZXh0ZW5zaW9uX3Byb3ZpZGVyLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi9zcmMvZXh0ZW5zaW9uL3Byb3ZpZGVyL2V4dGVuc2lvbl9wcm92aWRlci50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxPQUFPLEtBQUssRUFBRSxTQUFTLEVBQUUsTUFBTSx5QkFBeUIsQ0FBQztBQUt6RCxPQUFPLEVBQ0wsS0FBSyxtQkFBbUIsRUFLeEIsS0FBSyxVQUFVLEVBRWhCLE1BQU0sZ0JBQWdCLENBQUM7QUFReEI7Ozs7O0dBS0c7QUFDSCxxQkFBYSxnQkFBZ0I7SUFFekIseURBQXlEO2FBQ3pDLElBQUksRUFBRSxVQUFVO0lBQ2hDLGdFQUFnRTthQUNoRCxJQUFJLEVBQUUsV0FBVztJQUNqQyxpQ0FBaUM7YUFDakIsU0FBUyxFQUFFLE1BQU07SUFObkM7SUFDRSx5REFBeUQ7SUFDekMsSUFBSSxFQUFFLFVBQVU7SUFDaEMsZ0VBQWd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json-rpc';
|
|
2
|
+
import { promiseWithResolvers } from '@aztec/foundation/promise';
|
|
3
|
+
import { deriveSessionKeys, exportPublicKey, generateKeyPair, importPublicKey } from '../../crypto.js';
|
|
4
|
+
import { WalletMessageType } from '../../types.js';
|
|
5
|
+
/** Default discovery timeout - long to give users time to approve */ const DEFAULT_DISCOVERY_TIMEOUT_MS = 60000; // 60 seconds
|
|
6
|
+
/** Key exchange timeout - short, wallet should respond quickly after discovery approval */ const KEY_EXCHANGE_TIMEOUT_MS = 2000; // 2 seconds
|
|
7
|
+
/**
|
|
8
|
+
* A discovered wallet before key exchange.
|
|
9
|
+
* Has basic info and MessagePort, but no shared key yet.
|
|
10
|
+
*
|
|
11
|
+
* Call {@link establishSecureChannel} to perform key exchange and get a connected wallet.
|
|
12
|
+
*/ export class DiscoveredWallet {
|
|
13
|
+
info;
|
|
14
|
+
port;
|
|
15
|
+
requestId;
|
|
16
|
+
constructor(/** Basic wallet information (id, name, icon, version) */ info, /** The MessagePort for private communication with the wallet */ port, /** Request ID for correlation */ requestId){
|
|
17
|
+
this.info = info;
|
|
18
|
+
this.port = port;
|
|
19
|
+
this.requestId = requestId;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Establishes a secure connection with this wallet.
|
|
23
|
+
*
|
|
24
|
+
* This method:
|
|
25
|
+
* 1. Generates an ECDH key pair
|
|
26
|
+
* 2. Sends public key to wallet over the MessagePort
|
|
27
|
+
* 3. Receives wallet's public key
|
|
28
|
+
* 4. Derives shared secret and computes verification hash locally
|
|
29
|
+
*
|
|
30
|
+
* **IMPORTANT**: Has a 2 second timeout for MITM defense.
|
|
31
|
+
* Both parties must exchange keys relatively quickly.
|
|
32
|
+
*
|
|
33
|
+
* The verification hash is computed independently by both parties
|
|
34
|
+
* and should be displayed to the user for visual comparison.
|
|
35
|
+
*
|
|
36
|
+
* @returns Connected wallet with shared key and verification hash
|
|
37
|
+
* @throws Error if key exchange fails or times out
|
|
38
|
+
*/ async establishSecureChannel() {
|
|
39
|
+
const keyPair = await generateKeyPair();
|
|
40
|
+
const exportedPublicKey = await exportPublicKey(keyPair.publicKey);
|
|
41
|
+
const { promise, resolve, reject } = promiseWithResolvers();
|
|
42
|
+
const timeoutId = setTimeout(()=>{
|
|
43
|
+
reject(new Error('Key exchange timeout'));
|
|
44
|
+
}, KEY_EXCHANGE_TIMEOUT_MS);
|
|
45
|
+
this.port.onmessage = async (event)=>{
|
|
46
|
+
const data = event.data;
|
|
47
|
+
if (data.type === WalletMessageType.KEY_EXCHANGE_RESPONSE && data.requestId === this.requestId) {
|
|
48
|
+
clearTimeout(timeoutId);
|
|
49
|
+
try {
|
|
50
|
+
const walletPublicKey = await importPublicKey(data.publicKey);
|
|
51
|
+
const session = await deriveSessionKeys(keyPair, walletPublicKey, true);
|
|
52
|
+
const connectedInfo = {
|
|
53
|
+
...this.info,
|
|
54
|
+
publicKey: data.publicKey,
|
|
55
|
+
verificationHash: session.verificationHash
|
|
56
|
+
};
|
|
57
|
+
resolve({
|
|
58
|
+
info: connectedInfo,
|
|
59
|
+
port: this.port,
|
|
60
|
+
sharedKey: session.encryptionKey
|
|
61
|
+
});
|
|
62
|
+
} catch (err) {
|
|
63
|
+
reject(new Error(`Key exchange failed: ${err}`));
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
this.port.start();
|
|
68
|
+
const keyExchangeRequest = {
|
|
69
|
+
type: WalletMessageType.KEY_EXCHANGE_REQUEST,
|
|
70
|
+
requestId: this.requestId,
|
|
71
|
+
publicKey: exportedPublicKey
|
|
72
|
+
};
|
|
73
|
+
this.port.postMessage(keyExchangeRequest);
|
|
74
|
+
return promise;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Provider for discovering Aztec wallet extensions.
|
|
79
|
+
*
|
|
80
|
+
* NOTE: Most users should use WalletManager instead of this class directly.
|
|
81
|
+
* WalletManager provides a higher-level API with streaming support.
|
|
82
|
+
*
|
|
83
|
+
* The connection flow is split into two phases for security:
|
|
84
|
+
*
|
|
85
|
+
* 1. **Discovery Phase** ({@link discoverWallets}):
|
|
86
|
+
* - Broadcasts a discovery request (NO public keys)
|
|
87
|
+
* - Wallet shows pending request to user
|
|
88
|
+
* - User must approve before wallet reveals itself
|
|
89
|
+
* - Wallets are streamed via callback as they're approved
|
|
90
|
+
*
|
|
91
|
+
* 2. **Secure Channel Phase** ({@link DiscoveredWallet.establishSecureChannel}):
|
|
92
|
+
* - Performs ECDH key exchange over private MessageChannel
|
|
93
|
+
* - Both parties compute verification hash locally
|
|
94
|
+
* - Has a 2s timeout for MITM defense
|
|
95
|
+
* - Returns connected wallet with shared key and verification hash
|
|
96
|
+
*/ export class ExtensionProvider {
|
|
97
|
+
/**
|
|
98
|
+
* Discovers wallet extensions that user has approved.
|
|
99
|
+
*
|
|
100
|
+
* Wallets are streamed via the `onWalletDiscovered` callback as users approve them.
|
|
101
|
+
* The promise resolves when the timeout expires or signal is aborted.
|
|
102
|
+
*
|
|
103
|
+
* @param chainInfo - Chain information to check if extensions support this network
|
|
104
|
+
* @param options - Discovery options including appId, appName, timeout, and callback
|
|
105
|
+
* @returns Promise that resolves when discovery completes
|
|
106
|
+
*/ static discoverWallets(chainInfo, options) {
|
|
107
|
+
if (options.signal?.aborted) {
|
|
108
|
+
return Promise.resolve();
|
|
109
|
+
}
|
|
110
|
+
const timeout = options.timeout ?? DEFAULT_DISCOVERY_TIMEOUT_MS;
|
|
111
|
+
return new Promise((resolve)=>{
|
|
112
|
+
const requestId = globalThis.crypto.randomUUID();
|
|
113
|
+
let timeoutId = null;
|
|
114
|
+
let completed = false;
|
|
115
|
+
const finish = ()=>{
|
|
116
|
+
if (completed) {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
completed = true;
|
|
120
|
+
if (timeoutId !== null) {
|
|
121
|
+
clearTimeout(timeoutId);
|
|
122
|
+
}
|
|
123
|
+
window.removeEventListener('message', onMessage);
|
|
124
|
+
options.signal?.removeEventListener('abort', onAbort);
|
|
125
|
+
resolve();
|
|
126
|
+
};
|
|
127
|
+
const onAbort = ()=>finish();
|
|
128
|
+
const onMessage = (event)=>{
|
|
129
|
+
if (completed || event.source !== window) {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
let data;
|
|
133
|
+
try {
|
|
134
|
+
data = JSON.parse(event.data);
|
|
135
|
+
} catch {
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
if (data.type !== WalletMessageType.DISCOVERY_RESPONSE || data.requestId !== requestId) {
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
const port = event.ports?.[0];
|
|
142
|
+
if (port) {
|
|
143
|
+
options.onWalletDiscovered?.(new DiscoveredWallet(data.walletInfo, port, requestId));
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
options.signal?.addEventListener('abort', onAbort, {
|
|
147
|
+
once: true
|
|
148
|
+
});
|
|
149
|
+
window.addEventListener('message', onMessage);
|
|
150
|
+
const discoveryMessage = {
|
|
151
|
+
type: WalletMessageType.DISCOVERY,
|
|
152
|
+
requestId,
|
|
153
|
+
appId: options.appId,
|
|
154
|
+
chainInfo
|
|
155
|
+
};
|
|
156
|
+
window.postMessage(jsonStringify(discoveryMessage), '*');
|
|
157
|
+
timeoutId = setTimeout(finish, timeout);
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import type { ChainInfo } from '@aztec/aztec.js/account';
|
|
2
|
+
import { type Wallet } from '@aztec/aztec.js/wallet';
|
|
3
|
+
/**
|
|
4
|
+
* Callback type for wallet disconnect events.
|
|
5
|
+
*/
|
|
6
|
+
export type DisconnectCallback = () => void;
|
|
7
|
+
/**
|
|
8
|
+
* A wallet implementation that communicates with browser extension wallets
|
|
9
|
+
* using an encrypted MessageChannel.
|
|
10
|
+
*
|
|
11
|
+
* This class uses a secure channel established after discovery:
|
|
12
|
+
*
|
|
13
|
+
* 1. **MessageChannel**: Created during discovery and transferred via window.postMessage.
|
|
14
|
+
* Note: The port transfer is visible to page scripts, but security comes from encryption.
|
|
15
|
+
*
|
|
16
|
+
* 2. **ECDH Key Exchange**: The shared secret was derived after discovery using
|
|
17
|
+
* Elliptic Curve Diffie-Hellman key exchange over the MessagePort.
|
|
18
|
+
*
|
|
19
|
+
* 3. **AES-GCM Encryption**: All messages are encrypted using AES-256-GCM,
|
|
20
|
+
* providing both confidentiality and authenticity. This is what secures the channel.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```typescript
|
|
24
|
+
* // Discover and establish secure channel to a wallet
|
|
25
|
+
* const discoveredWallets = await ExtensionProvider.discoverWallets(chainInfo, { appId: 'my-dapp' });
|
|
26
|
+
* const connection = await discoveredWallets[0].establishSecureChannel();
|
|
27
|
+
*
|
|
28
|
+
* // User can verify emoji if desired
|
|
29
|
+
* console.log('Verify:', hashToEmoji(connection.info.verificationHash!));
|
|
30
|
+
*
|
|
31
|
+
* // Create wallet using the connection
|
|
32
|
+
* const wallet = ExtensionWallet.create(connection.info.id, connection.port, connection.sharedKey, chainInfo, 'my-dapp');
|
|
33
|
+
*
|
|
34
|
+
* // All subsequent calls are encrypted
|
|
35
|
+
* const accounts = await wallet.getAccounts();
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export declare class ExtensionWallet {
|
|
39
|
+
private chainInfo;
|
|
40
|
+
private appId;
|
|
41
|
+
private extensionId;
|
|
42
|
+
private port;
|
|
43
|
+
private sharedKey;
|
|
44
|
+
/** Map of pending requests awaiting responses, keyed by message ID */
|
|
45
|
+
private inFlight;
|
|
46
|
+
private disconnected;
|
|
47
|
+
private disconnectCallbacks;
|
|
48
|
+
/**
|
|
49
|
+
* Private constructor - use {@link ExtensionWallet.create} to instantiate.
|
|
50
|
+
* @param chainInfo - The chain information (chainId and version)
|
|
51
|
+
* @param appId - Application identifier for the requesting dApp
|
|
52
|
+
* @param extensionId - The unique identifier of the target wallet extension
|
|
53
|
+
* @param port - The MessagePort for private communication with the wallet
|
|
54
|
+
* @param sharedKey - The derived AES-256-GCM shared key for encryption
|
|
55
|
+
*/
|
|
56
|
+
private constructor();
|
|
57
|
+
/**
|
|
58
|
+
* Creates a Wallet that communicates with a browser extension
|
|
59
|
+
* over a secure encrypted MessageChannel.
|
|
60
|
+
*
|
|
61
|
+
* @param extensionId - The unique identifier of the wallet extension
|
|
62
|
+
* @param port - The MessagePort for encrypted communication with the wallet
|
|
63
|
+
* @param sharedKey - The derived AES-256-GCM shared key for encryption
|
|
64
|
+
* @param chainInfo - The chain information (chainId and version) for request context
|
|
65
|
+
* @param appId - Application identifier used to identify the requesting dApp to the wallet
|
|
66
|
+
* @returns A Wallet interface where all method calls are encrypted
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```typescript
|
|
70
|
+
* const discoveredWallets = await ExtensionProvider.discoverWallets(chainInfo, { appId: 'my-defi-app' });
|
|
71
|
+
* const connection = await discoveredWallets[0].establishSecureChannel();
|
|
72
|
+
* const wallet = ExtensionWallet.create(
|
|
73
|
+
* connection.info.id,
|
|
74
|
+
* connection.port,
|
|
75
|
+
* connection.sharedKey,
|
|
76
|
+
* chainInfo,
|
|
77
|
+
* 'my-defi-app'
|
|
78
|
+
* );
|
|
79
|
+
*
|
|
80
|
+
* const accounts = await wallet.getAccounts();
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
static create(extensionId: string, port: MessagePort, sharedKey: CryptoKey, chainInfo: ChainInfo, appId: string): ExtensionWallet;
|
|
84
|
+
asWallet(): Wallet;
|
|
85
|
+
private handleEncryptedResponse;
|
|
86
|
+
private postMessage;
|
|
87
|
+
/**
|
|
88
|
+
* Handles wallet disconnection.
|
|
89
|
+
* Rejects all pending requests and notifies registered callbacks.
|
|
90
|
+
* @internal
|
|
91
|
+
*/
|
|
92
|
+
private handleDisconnect;
|
|
93
|
+
/**
|
|
94
|
+
* Registers a callback to be invoked when the wallet disconnects.
|
|
95
|
+
*
|
|
96
|
+
* @param callback - Function to call when wallet disconnects
|
|
97
|
+
* @returns A function to unregister the callback
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* ```typescript
|
|
101
|
+
* const wallet = await ExtensionWallet.create(...);
|
|
102
|
+
* const unsubscribe = wallet.onDisconnect(() => {
|
|
103
|
+
* console.log('Wallet disconnected! Please reconnect.');
|
|
104
|
+
* // Clean up UI, prompt user to reconnect, etc.
|
|
105
|
+
* });
|
|
106
|
+
* // Later: unsubscribe(); to stop receiving notifications
|
|
107
|
+
* ```
|
|
108
|
+
*/
|
|
109
|
+
onDisconnect(callback: DisconnectCallback): () => void;
|
|
110
|
+
/**
|
|
111
|
+
* Returns whether the wallet has been disconnected.
|
|
112
|
+
*
|
|
113
|
+
* @returns true if the wallet is no longer connected
|
|
114
|
+
*/
|
|
115
|
+
isDisconnected(): boolean;
|
|
116
|
+
/**
|
|
117
|
+
* Disconnects from the wallet and cleans up resources.
|
|
118
|
+
*
|
|
119
|
+
* This method notifies the wallet extension that the session is ending,
|
|
120
|
+
* allowing it to clean up its state. After calling this method, the wallet
|
|
121
|
+
* instance can no longer be used and any pending requests will be rejected.
|
|
122
|
+
*
|
|
123
|
+
* @example
|
|
124
|
+
* ```typescript
|
|
125
|
+
* const extensionWallet = ExtensionWallet.create(extensionId, port, sharedKey, chainInfo, 'my-app');
|
|
126
|
+
* // ... use wallet ...
|
|
127
|
+
* await extensionWallet.disconnect(); // Clean disconnect when done
|
|
128
|
+
* ```
|
|
129
|
+
*/
|
|
130
|
+
disconnect(): Promise<void>;
|
|
131
|
+
}
|
|
132
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZXh0ZW5zaW9uX3dhbGxldC5kLnRzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vc3JjL2V4dGVuc2lvbi9wcm92aWRlci9leHRlbnNpb25fd2FsbGV0LnRzIl0sIm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,EAAE,kBAAkB,GAAG,MAAM,IAAI,CAQrD;IAED;;;;OAIG;IACH,cAAc,IAAI,OAAO,CAExB;IAED;;;;;;;;;;;;;OAaG;IAEG,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC,CAahC;CACF"}
|
|
@@ -6,30 +6,30 @@ import { decrypt, encrypt } from '../../crypto.js';
|
|
|
6
6
|
import { WalletMessageType } from '../../types.js';
|
|
7
7
|
/**
|
|
8
8
|
* A wallet implementation that communicates with browser extension wallets
|
|
9
|
-
* using
|
|
9
|
+
* using an encrypted MessageChannel.
|
|
10
10
|
*
|
|
11
|
-
* This class uses a
|
|
11
|
+
* This class uses a secure channel established after discovery:
|
|
12
12
|
*
|
|
13
|
-
* 1. **MessageChannel**:
|
|
14
|
-
*
|
|
13
|
+
* 1. **MessageChannel**: Created during discovery and transferred via window.postMessage.
|
|
14
|
+
* Note: The port transfer is visible to page scripts, but security comes from encryption.
|
|
15
15
|
*
|
|
16
|
-
* 2. **ECDH Key Exchange**: The shared secret was derived
|
|
17
|
-
* Elliptic Curve Diffie-Hellman key exchange.
|
|
16
|
+
* 2. **ECDH Key Exchange**: The shared secret was derived after discovery using
|
|
17
|
+
* Elliptic Curve Diffie-Hellman key exchange over the MessagePort.
|
|
18
18
|
*
|
|
19
19
|
* 3. **AES-GCM Encryption**: All messages are encrypted using AES-256-GCM,
|
|
20
|
-
* providing both confidentiality and authenticity.
|
|
20
|
+
* providing both confidentiality and authenticity. This is what secures the channel.
|
|
21
21
|
*
|
|
22
22
|
* @example
|
|
23
23
|
* ```typescript
|
|
24
|
-
* //
|
|
25
|
-
* const
|
|
26
|
-
* const
|
|
24
|
+
* // Discover and establish secure channel to a wallet
|
|
25
|
+
* const discoveredWallets = await ExtensionProvider.discoverWallets(chainInfo, { appId: 'my-dapp' });
|
|
26
|
+
* const connection = await discoveredWallets[0].establishSecureChannel();
|
|
27
27
|
*
|
|
28
28
|
* // User can verify emoji if desired
|
|
29
|
-
* console.log('Verify:', hashToEmoji(info.verificationHash!));
|
|
29
|
+
* console.log('Verify:', hashToEmoji(connection.info.verificationHash!));
|
|
30
30
|
*
|
|
31
|
-
* // Create wallet using the
|
|
32
|
-
* const wallet =
|
|
31
|
+
* // Create wallet using the connection
|
|
32
|
+
* const wallet = ExtensionWallet.create(connection.info.id, connection.port, connection.sharedKey, chainInfo, 'my-dapp');
|
|
33
33
|
*
|
|
34
34
|
* // All subsequent calls are encrypted
|
|
35
35
|
* const accounts = await wallet.getAccounts();
|
|
@@ -59,70 +59,51 @@ import { WalletMessageType } from '../../types.js';
|
|
|
59
59
|
this.inFlight = new Map();
|
|
60
60
|
this.disconnected = false;
|
|
61
61
|
this.disconnectCallbacks = [];
|
|
62
|
-
this.walletProxy = null;
|
|
63
62
|
}
|
|
64
|
-
/** Cached Wallet proxy instance */ walletProxy;
|
|
65
63
|
/**
|
|
66
|
-
* Creates
|
|
64
|
+
* Creates a Wallet that communicates with a browser extension
|
|
67
65
|
* over a secure encrypted MessageChannel.
|
|
68
66
|
*
|
|
69
|
-
* @param
|
|
70
|
-
* @param
|
|
71
|
-
* @param port - The MessagePort for private communication with the wallet
|
|
67
|
+
* @param extensionId - The unique identifier of the wallet extension
|
|
68
|
+
* @param port - The MessagePort for encrypted communication with the wallet
|
|
72
69
|
* @param sharedKey - The derived AES-256-GCM shared key for encryption
|
|
70
|
+
* @param chainInfo - The chain information (chainId and version) for request context
|
|
73
71
|
* @param appId - Application identifier used to identify the requesting dApp to the wallet
|
|
74
|
-
* @returns
|
|
72
|
+
* @returns A Wallet interface where all method calls are encrypted
|
|
75
73
|
*
|
|
76
74
|
* @example
|
|
77
75
|
* ```typescript
|
|
78
|
-
* const
|
|
79
|
-
* const
|
|
80
|
-
* const
|
|
81
|
-
* info,
|
|
82
|
-
*
|
|
83
|
-
*
|
|
84
|
-
*
|
|
76
|
+
* const discoveredWallets = await ExtensionProvider.discoverWallets(chainInfo, { appId: 'my-defi-app' });
|
|
77
|
+
* const connection = await discoveredWallets[0].establishSecureChannel();
|
|
78
|
+
* const wallet = ExtensionWallet.create(
|
|
79
|
+
* connection.info.id,
|
|
80
|
+
* connection.port,
|
|
81
|
+
* connection.sharedKey,
|
|
82
|
+
* chainInfo,
|
|
85
83
|
* 'my-defi-app'
|
|
86
84
|
* );
|
|
87
85
|
*
|
|
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
86
|
* const accounts = await wallet.getAccounts();
|
|
94
87
|
* ```
|
|
95
|
-
*/ static create(
|
|
96
|
-
const wallet = new ExtensionWallet(chainInfo, appId,
|
|
97
|
-
// Set up message handler
|
|
88
|
+
*/ static create(extensionId, port, sharedKey, chainInfo, appId) {
|
|
89
|
+
const wallet = new ExtensionWallet(chainInfo, appId, extensionId, port, sharedKey);
|
|
90
|
+
// Set up message handler for encrypted responses and unencrypted control messages
|
|
98
91
|
wallet.port.onmessage = (event)=>{
|
|
99
|
-
|
|
92
|
+
const data = event.data;
|
|
93
|
+
// Check for unencrypted disconnect notification
|
|
94
|
+
if (data && typeof data === 'object' && 'type' in data && data.type === WalletMessageType.DISCONNECT) {
|
|
95
|
+
wallet.handleDisconnect();
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
// Otherwise treat as encrypted payload
|
|
99
|
+
void wallet.handleEncryptedResponse(data);
|
|
100
100
|
};
|
|
101
101
|
wallet.port.start();
|
|
102
|
-
return wallet
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
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
|
-
}
|
|
122
|
-
// Create a Proxy that intercepts wallet method calls and forwards them to the extension
|
|
123
|
-
this.walletProxy = new Proxy(this, {
|
|
124
|
-
get: (target, prop)=>{
|
|
125
|
-
if (schemaHasMethod(WalletSchema, prop.toString())) {
|
|
102
|
+
return new Proxy(wallet, {
|
|
103
|
+
get: (target, prop, receiver)=>{
|
|
104
|
+
if (prop === 'asWallet') {
|
|
105
|
+
return ()=>receiver;
|
|
106
|
+
} else if (schemaHasMethod(WalletSchema, prop.toString())) {
|
|
126
107
|
return async (...args)=>{
|
|
127
108
|
const result = await target.postMessage({
|
|
128
109
|
type: prop.toString(),
|
|
@@ -135,7 +116,11 @@ import { WalletMessageType } from '../../types.js';
|
|
|
135
116
|
}
|
|
136
117
|
}
|
|
137
118
|
});
|
|
138
|
-
|
|
119
|
+
}
|
|
120
|
+
asWallet() {
|
|
121
|
+
// Overridden by the proxy in create() to return the proxy itself.
|
|
122
|
+
// This body is never reached when accessed through create().
|
|
123
|
+
return this;
|
|
139
124
|
}
|
|
140
125
|
/**
|
|
141
126
|
* Handles an encrypted response received from the wallet extension.
|
|
@@ -151,12 +136,6 @@ import { WalletMessageType } from '../../types.js';
|
|
|
151
136
|
try {
|
|
152
137
|
const response = await decrypt(this.sharedKey, encrypted);
|
|
153
138
|
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
139
|
if (!messageId || !responseWalletId) {
|
|
161
140
|
return;
|
|
162
141
|
}
|
|
@@ -203,8 +182,7 @@ import { WalletMessageType } from '../../types.js';
|
|
|
203
182
|
appId: this.appId,
|
|
204
183
|
walletId: this.extensionId
|
|
205
184
|
};
|
|
206
|
-
|
|
207
|
-
const encrypted = await encrypt(this.sharedKey, message);
|
|
185
|
+
const encrypted = await encrypt(this.sharedKey, jsonStringify(message));
|
|
208
186
|
this.port.postMessage(encrypted);
|
|
209
187
|
const { promise, resolve, reject } = promiseWithResolvers();
|
|
210
188
|
this.inFlight.set(messageId, {
|
|
@@ -223,25 +201,20 @@ import { WalletMessageType } from '../../types.js';
|
|
|
223
201
|
return;
|
|
224
202
|
}
|
|
225
203
|
this.disconnected = true;
|
|
226
|
-
// Close the port to prevent any further messages
|
|
227
204
|
if (this.port) {
|
|
228
205
|
this.port.onmessage = null;
|
|
229
206
|
this.port.close();
|
|
230
207
|
}
|
|
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
208
|
const error = new Error('Wallet disconnected');
|
|
235
209
|
for (const { reject } of this.inFlight.values()){
|
|
236
210
|
reject(error);
|
|
237
211
|
}
|
|
238
212
|
this.inFlight.clear();
|
|
239
|
-
// Notify registered callbacks
|
|
240
213
|
for (const callback of this.disconnectCallbacks){
|
|
241
214
|
try {
|
|
242
215
|
callback();
|
|
243
216
|
} catch {
|
|
244
|
-
// Ignore errors
|
|
217
|
+
// Ignore errors on disconnect callbacks
|
|
245
218
|
}
|
|
246
219
|
}
|
|
247
220
|
}
|
|
@@ -285,34 +258,21 @@ import { WalletMessageType } from '../../types.js';
|
|
|
285
258
|
*
|
|
286
259
|
* @example
|
|
287
260
|
* ```typescript
|
|
288
|
-
* const
|
|
261
|
+
* const extensionWallet = ExtensionWallet.create(extensionId, port, sharedKey, chainInfo, 'my-app');
|
|
289
262
|
* // ... use wallet ...
|
|
290
|
-
* await
|
|
263
|
+
* await extensionWallet.disconnect(); // Clean disconnect when done
|
|
291
264
|
* ```
|
|
292
|
-
*/ async
|
|
265
|
+
*/ // eslint-disable-next-line require-await -- async for interface compatibility
|
|
266
|
+
async disconnect() {
|
|
293
267
|
if (this.disconnected) {
|
|
294
268
|
return;
|
|
295
269
|
}
|
|
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
270
|
if (this.port) {
|
|
315
|
-
|
|
271
|
+
// Send unencrypted disconnect - control messages don't need encryption
|
|
272
|
+
this.port.postMessage({
|
|
273
|
+
type: WalletMessageType.DISCONNECT
|
|
274
|
+
});
|
|
316
275
|
}
|
|
276
|
+
this.handleDisconnect();
|
|
317
277
|
}
|
|
318
278
|
}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { ExtensionWallet, type DisconnectCallback } from './extension_wallet.js';
|
|
2
|
+
export { ExtensionProvider, type DiscoveredWallet, type ConnectedWallet, type DiscoveryOptions, } from './extension_provider.js';
|
|
3
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uL3NyYy9leHRlbnNpb24vcHJvdmlkZXIvaW5kZXgudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxFQUFFLGVBQWUsRUFBRSxLQUFLLGtCQUFrQixFQUFFLE1BQU0sdUJBQXVCLENBQUM7QUFDakYsT0FBTyxFQUNMLGlCQUFpQixFQUNqQixLQUFLLGdCQUFnQixFQUNyQixLQUFLLGVBQWUsRUFDcEIsS0FBSyxnQkFBZ0IsR0FDdEIsTUFBTSx5QkFBeUIsQ0FBQyJ9
|