@aztec/wallet-sdk 0.0.1-commit.f295ac2 → 0.0.1-commit.fc805bf
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 +20 -7
- package/dest/base-wallet/base_wallet.d.ts.map +1 -1
- package/dest/base-wallet/base_wallet.js +29 -11
- 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 +131 -0
- package/dest/extension/provider/extension_wallet.d.ts.map +1 -0
- package/dest/{providers/extension → extension/provider}/extension_wallet.js +48 -95
- 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 +88 -6
- 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 +10 -9
- package/src/base-wallet/base_wallet.ts +45 -20
- 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 +52 -110
- package/src/extension/provider/index.ts +7 -0
- package/src/manager/index.ts +2 -10
- package/src/manager/types.ts +91 -5
- package/src/manager/wallet_manager.ts +192 -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,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal message types for content script ↔ background communication.
|
|
3
|
+
* These are NOT part of the public wallet protocol - they are implementation
|
|
4
|
+
* details for coordinating between extension components.
|
|
5
|
+
*/
|
|
6
|
+
export const InternalMessageType = {
|
|
7
|
+
// Content script → Background
|
|
8
|
+
DISCOVERY_REQUEST: 'discovery-request',
|
|
9
|
+
KEY_EXCHANGE_REQUEST: 'key-exchange-request',
|
|
10
|
+
SECURE_MESSAGE: 'secure-message',
|
|
11
|
+
DISCONNECT_REQUEST: 'disconnect-request',
|
|
12
|
+
// Background → Content script
|
|
13
|
+
DISCOVERY_APPROVED: 'discovery-approved',
|
|
14
|
+
KEY_EXCHANGE_RESPONSE: 'key-exchange-response',
|
|
15
|
+
SECURE_RESPONSE: 'secure-response',
|
|
16
|
+
SESSION_DISCONNECTED: 'session-disconnected',
|
|
17
|
+
} as const;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Message origins for internal extension communication.
|
|
21
|
+
*/
|
|
22
|
+
export const MessageOrigin = {
|
|
23
|
+
BACKGROUND: 'background',
|
|
24
|
+
CONTENT_SCRIPT: 'content-script',
|
|
25
|
+
} as const;
|
|
26
|
+
|
|
27
|
+
/** Union type of message origins. */
|
|
28
|
+
export type MessageOriginType = (typeof MessageOrigin)[keyof typeof MessageOrigin];
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Message sent from content script to background.
|
|
32
|
+
*/
|
|
33
|
+
export interface ContentScriptMessage {
|
|
34
|
+
/** Message source identifier. */
|
|
35
|
+
origin: typeof MessageOrigin.CONTENT_SCRIPT;
|
|
36
|
+
/** Message type. */
|
|
37
|
+
type: string;
|
|
38
|
+
/** Optional session identifier. */
|
|
39
|
+
sessionId?: string;
|
|
40
|
+
/** Optional message payload. */
|
|
41
|
+
content?: unknown;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Message sent from background to content script.
|
|
46
|
+
*/
|
|
47
|
+
export interface BackgroundMessage {
|
|
48
|
+
/** Message source identifier. */
|
|
49
|
+
origin: typeof MessageOrigin.BACKGROUND;
|
|
50
|
+
/** Message type. */
|
|
51
|
+
type: string;
|
|
52
|
+
/** Session identifier. */
|
|
53
|
+
sessionId: string;
|
|
54
|
+
/** Optional message payload. */
|
|
55
|
+
content?: unknown;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Sender information for messages from browser runtime.
|
|
60
|
+
*/
|
|
61
|
+
export interface MessageSender {
|
|
62
|
+
/** Tab information if available. */
|
|
63
|
+
tab?: {
|
|
64
|
+
/** Tab identifier. */
|
|
65
|
+
id?: number;
|
|
66
|
+
/** Tab URL. */
|
|
67
|
+
url?: string;
|
|
68
|
+
};
|
|
69
|
+
}
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import type { ChainInfo } from '@aztec/aztec.js/account';
|
|
2
|
+
import { jsonStringify } from '@aztec/foundation/json-rpc';
|
|
3
|
+
import { promiseWithResolvers } from '@aztec/foundation/promise';
|
|
4
|
+
|
|
5
|
+
import { deriveSessionKeys, exportPublicKey, generateKeyPair, importPublicKey } from '../../crypto.js';
|
|
6
|
+
import {
|
|
7
|
+
type ConnectedWalletInfo,
|
|
8
|
+
type DiscoveryRequest,
|
|
9
|
+
type DiscoveryResponse,
|
|
10
|
+
type KeyExchangeRequest,
|
|
11
|
+
type KeyExchangeResponse,
|
|
12
|
+
type WalletInfo,
|
|
13
|
+
WalletMessageType,
|
|
14
|
+
} from '../../types.js';
|
|
15
|
+
|
|
16
|
+
/** Default discovery timeout - long to give users time to approve */
|
|
17
|
+
const DEFAULT_DISCOVERY_TIMEOUT_MS = 60000; // 60 seconds
|
|
18
|
+
|
|
19
|
+
/** Key exchange timeout - short, wallet should respond quickly after discovery approval */
|
|
20
|
+
const KEY_EXCHANGE_TIMEOUT_MS = 2000; // 2 seconds
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* A discovered wallet before key exchange.
|
|
24
|
+
* Has basic info and MessagePort, but no shared key yet.
|
|
25
|
+
*
|
|
26
|
+
* Call {@link establishSecureChannel} to perform key exchange and get a connected wallet.
|
|
27
|
+
*/
|
|
28
|
+
export class DiscoveredWallet {
|
|
29
|
+
constructor(
|
|
30
|
+
/** Basic wallet information (id, name, icon, version) */
|
|
31
|
+
public readonly info: WalletInfo,
|
|
32
|
+
/** The MessagePort for private communication with the wallet */
|
|
33
|
+
public readonly port: MessagePort,
|
|
34
|
+
/** Request ID for correlation */
|
|
35
|
+
public readonly requestId: string,
|
|
36
|
+
) {}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Establishes a secure connection with this wallet.
|
|
40
|
+
*
|
|
41
|
+
* This method:
|
|
42
|
+
* 1. Generates an ECDH key pair
|
|
43
|
+
* 2. Sends public key to wallet over the MessagePort
|
|
44
|
+
* 3. Receives wallet's public key
|
|
45
|
+
* 4. Derives shared secret and computes verification hash locally
|
|
46
|
+
*
|
|
47
|
+
* **IMPORTANT**: Has a 2 second timeout for MITM defense.
|
|
48
|
+
* Both parties must exchange keys relatively quickly.
|
|
49
|
+
*
|
|
50
|
+
* The verification hash is computed independently by both parties
|
|
51
|
+
* and should be displayed to the user for visual comparison.
|
|
52
|
+
*
|
|
53
|
+
* @returns Connected wallet with shared key and verification hash
|
|
54
|
+
* @throws Error if key exchange fails or times out
|
|
55
|
+
*/
|
|
56
|
+
async establishSecureChannel(): Promise<ConnectedWallet> {
|
|
57
|
+
const keyPair = await generateKeyPair();
|
|
58
|
+
const exportedPublicKey = await exportPublicKey(keyPair.publicKey);
|
|
59
|
+
|
|
60
|
+
const { promise, resolve, reject } = promiseWithResolvers<ConnectedWallet>();
|
|
61
|
+
|
|
62
|
+
const timeoutId = setTimeout(() => {
|
|
63
|
+
reject(new Error('Key exchange timeout'));
|
|
64
|
+
}, KEY_EXCHANGE_TIMEOUT_MS);
|
|
65
|
+
|
|
66
|
+
this.port.onmessage = async (event: MessageEvent) => {
|
|
67
|
+
const data = event.data as KeyExchangeResponse;
|
|
68
|
+
|
|
69
|
+
if (data.type === WalletMessageType.KEY_EXCHANGE_RESPONSE && data.requestId === this.requestId) {
|
|
70
|
+
clearTimeout(timeoutId);
|
|
71
|
+
|
|
72
|
+
try {
|
|
73
|
+
const walletPublicKey = await importPublicKey(data.publicKey);
|
|
74
|
+
const session = await deriveSessionKeys(keyPair, walletPublicKey, true);
|
|
75
|
+
|
|
76
|
+
const connectedInfo: ConnectedWalletInfo = {
|
|
77
|
+
...this.info,
|
|
78
|
+
publicKey: data.publicKey,
|
|
79
|
+
verificationHash: session.verificationHash,
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
resolve({
|
|
83
|
+
info: connectedInfo,
|
|
84
|
+
port: this.port,
|
|
85
|
+
sharedKey: session.encryptionKey,
|
|
86
|
+
});
|
|
87
|
+
} catch (err) {
|
|
88
|
+
reject(new Error(`Key exchange failed: ${err}`));
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
this.port.start();
|
|
94
|
+
|
|
95
|
+
const keyExchangeRequest: KeyExchangeRequest = {
|
|
96
|
+
type: WalletMessageType.KEY_EXCHANGE_REQUEST,
|
|
97
|
+
requestId: this.requestId,
|
|
98
|
+
publicKey: exportedPublicKey,
|
|
99
|
+
};
|
|
100
|
+
this.port.postMessage(keyExchangeRequest);
|
|
101
|
+
|
|
102
|
+
return promise;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* A fully connected wallet with secure channel established.
|
|
108
|
+
* Available after key exchange completes.
|
|
109
|
+
*/
|
|
110
|
+
export interface ConnectedWallet {
|
|
111
|
+
/** Full wallet info including public key and verification hash */
|
|
112
|
+
info: ConnectedWalletInfo;
|
|
113
|
+
/** The MessagePort for encrypted communication */
|
|
114
|
+
port: MessagePort;
|
|
115
|
+
/** The derived AES-256-GCM shared key for encryption */
|
|
116
|
+
sharedKey: CryptoKey;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Options for wallet discovery.
|
|
121
|
+
*/
|
|
122
|
+
export interface DiscoveryOptions {
|
|
123
|
+
/** Application ID making the request */
|
|
124
|
+
appId: string;
|
|
125
|
+
/** How long to wait for user approval (ms). Default: 60000 (60s) */
|
|
126
|
+
timeout?: number;
|
|
127
|
+
/**
|
|
128
|
+
* Callback invoked when a wallet is discovered.
|
|
129
|
+
* Wallets are streamed as users approve them.
|
|
130
|
+
*/
|
|
131
|
+
onWalletDiscovered?: (wallet: DiscoveredWallet) => void;
|
|
132
|
+
/**
|
|
133
|
+
* AbortSignal for cancelling discovery early.
|
|
134
|
+
* When aborted, cleanup happens immediately instead of waiting for timeout.
|
|
135
|
+
*/
|
|
136
|
+
signal?: AbortSignal;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Provider for discovering Aztec wallet extensions.
|
|
141
|
+
*
|
|
142
|
+
* NOTE: Most users should use WalletManager instead of this class directly.
|
|
143
|
+
* WalletManager provides a higher-level API with streaming support.
|
|
144
|
+
*
|
|
145
|
+
* The connection flow is split into two phases for security:
|
|
146
|
+
*
|
|
147
|
+
* 1. **Discovery Phase** ({@link discoverWallets}):
|
|
148
|
+
* - Broadcasts a discovery request (NO public keys)
|
|
149
|
+
* - Wallet shows pending request to user
|
|
150
|
+
* - User must approve before wallet reveals itself
|
|
151
|
+
* - Wallets are streamed via callback as they're approved
|
|
152
|
+
*
|
|
153
|
+
* 2. **Secure Channel Phase** ({@link DiscoveredWallet.establishSecureChannel}):
|
|
154
|
+
* - Performs ECDH key exchange over private MessageChannel
|
|
155
|
+
* - Both parties compute verification hash locally
|
|
156
|
+
* - Has a 2s timeout for MITM defense
|
|
157
|
+
* - Returns connected wallet with shared key and verification hash
|
|
158
|
+
*/
|
|
159
|
+
export class ExtensionProvider {
|
|
160
|
+
/**
|
|
161
|
+
* Discovers wallet extensions that user has approved.
|
|
162
|
+
*
|
|
163
|
+
* Wallets are streamed via the `onWalletDiscovered` callback as users approve them.
|
|
164
|
+
* The promise resolves when the timeout expires or signal is aborted.
|
|
165
|
+
*
|
|
166
|
+
* @param chainInfo - Chain information to check if extensions support this network
|
|
167
|
+
* @param options - Discovery options including appId, appName, timeout, and callback
|
|
168
|
+
* @returns Promise that resolves when discovery completes
|
|
169
|
+
*/
|
|
170
|
+
static discoverWallets(chainInfo: ChainInfo, options: DiscoveryOptions): Promise<void> {
|
|
171
|
+
if (options.signal?.aborted) {
|
|
172
|
+
return Promise.resolve();
|
|
173
|
+
}
|
|
174
|
+
const timeout = options.timeout ?? DEFAULT_DISCOVERY_TIMEOUT_MS;
|
|
175
|
+
|
|
176
|
+
return new Promise(resolve => {
|
|
177
|
+
const requestId = globalThis.crypto.randomUUID();
|
|
178
|
+
let timeoutId: ReturnType<typeof setTimeout> | null = null;
|
|
179
|
+
let completed = false;
|
|
180
|
+
|
|
181
|
+
const finish = () => {
|
|
182
|
+
if (completed) {
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
completed = true;
|
|
186
|
+
|
|
187
|
+
if (timeoutId !== null) {
|
|
188
|
+
clearTimeout(timeoutId);
|
|
189
|
+
}
|
|
190
|
+
window.removeEventListener('message', onMessage);
|
|
191
|
+
options.signal?.removeEventListener('abort', onAbort);
|
|
192
|
+
resolve();
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
const onAbort = () => finish();
|
|
196
|
+
|
|
197
|
+
const onMessage = (event: MessageEvent) => {
|
|
198
|
+
if (completed || event.source !== window) {
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
let data: DiscoveryResponse;
|
|
203
|
+
try {
|
|
204
|
+
data = JSON.parse(event.data);
|
|
205
|
+
} catch {
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
if (data.type !== WalletMessageType.DISCOVERY_RESPONSE || data.requestId !== requestId) {
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const port = event.ports?.[0];
|
|
214
|
+
if (port) {
|
|
215
|
+
options.onWalletDiscovered?.(new DiscoveredWallet(data.walletInfo, port, requestId));
|
|
216
|
+
}
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
options.signal?.addEventListener('abort', onAbort, { once: true });
|
|
220
|
+
window.addEventListener('message', onMessage);
|
|
221
|
+
|
|
222
|
+
const discoveryMessage: DiscoveryRequest = {
|
|
223
|
+
type: WalletMessageType.DISCOVERY,
|
|
224
|
+
requestId,
|
|
225
|
+
appId: options.appId,
|
|
226
|
+
chainInfo,
|
|
227
|
+
};
|
|
228
|
+
window.postMessage(jsonStringify(discoveryMessage), '*');
|
|
229
|
+
|
|
230
|
+
timeoutId = setTimeout(finish, timeout);
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
}
|
|
@@ -6,7 +6,7 @@ import { schemaHasMethod } from '@aztec/foundation/schemas';
|
|
|
6
6
|
import type { FunctionsOf } from '@aztec/foundation/types';
|
|
7
7
|
|
|
8
8
|
import { type EncryptedPayload, decrypt, encrypt } from '../../crypto.js';
|
|
9
|
-
import { type
|
|
9
|
+
import { type WalletMessage, WalletMessageType, type WalletResponse } from '../../types.js';
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
12
|
* Internal type representing a wallet method call before encryption.
|
|
@@ -26,30 +26,30 @@ export type DisconnectCallback = () => void;
|
|
|
26
26
|
|
|
27
27
|
/**
|
|
28
28
|
* A wallet implementation that communicates with browser extension wallets
|
|
29
|
-
* using
|
|
29
|
+
* using an encrypted MessageChannel.
|
|
30
30
|
*
|
|
31
|
-
* This class uses a
|
|
31
|
+
* This class uses a secure channel established after discovery:
|
|
32
32
|
*
|
|
33
|
-
* 1. **MessageChannel**:
|
|
34
|
-
*
|
|
33
|
+
* 1. **MessageChannel**: Created during discovery and transferred via window.postMessage.
|
|
34
|
+
* Note: The port transfer is visible to page scripts, but security comes from encryption.
|
|
35
35
|
*
|
|
36
|
-
* 2. **ECDH Key Exchange**: The shared secret was derived
|
|
37
|
-
* Elliptic Curve Diffie-Hellman key exchange.
|
|
36
|
+
* 2. **ECDH Key Exchange**: The shared secret was derived after discovery using
|
|
37
|
+
* Elliptic Curve Diffie-Hellman key exchange over the MessagePort.
|
|
38
38
|
*
|
|
39
39
|
* 3. **AES-GCM Encryption**: All messages are encrypted using AES-256-GCM,
|
|
40
|
-
* providing both confidentiality and authenticity.
|
|
40
|
+
* providing both confidentiality and authenticity. This is what secures the channel.
|
|
41
41
|
*
|
|
42
42
|
* @example
|
|
43
43
|
* ```typescript
|
|
44
|
-
* //
|
|
45
|
-
* const
|
|
46
|
-
* const
|
|
44
|
+
* // Discover and establish secure channel to a wallet
|
|
45
|
+
* const discoveredWallets = await ExtensionProvider.discoverWallets(chainInfo, { appId: 'my-dapp' });
|
|
46
|
+
* const connection = await discoveredWallets[0].establishSecureChannel();
|
|
47
47
|
*
|
|
48
48
|
* // User can verify emoji if desired
|
|
49
|
-
* console.log('Verify:', hashToEmoji(info.verificationHash!));
|
|
49
|
+
* console.log('Verify:', hashToEmoji(connection.info.verificationHash!));
|
|
50
50
|
*
|
|
51
|
-
* // Create wallet using the
|
|
52
|
-
* const wallet =
|
|
51
|
+
* // Create wallet using the connection
|
|
52
|
+
* const wallet = ExtensionWallet.create(connection.info.id, connection.port, connection.sharedKey, chainInfo, 'my-dapp');
|
|
53
53
|
*
|
|
54
54
|
* // All subsequent calls are encrypted
|
|
55
55
|
* const accounts = await wallet.getAccounts();
|
|
@@ -77,81 +77,56 @@ export class ExtensionWallet {
|
|
|
77
77
|
private sharedKey: CryptoKey,
|
|
78
78
|
) {}
|
|
79
79
|
|
|
80
|
-
/** Cached Wallet proxy instance */
|
|
81
|
-
private walletProxy: Wallet | null = null;
|
|
82
|
-
|
|
83
80
|
/**
|
|
84
|
-
* Creates
|
|
81
|
+
* Creates a Wallet that communicates with a browser extension
|
|
85
82
|
* over a secure encrypted MessageChannel.
|
|
86
83
|
*
|
|
87
|
-
* @param
|
|
88
|
-
* @param
|
|
89
|
-
* @param port - The MessagePort for private communication with the wallet
|
|
84
|
+
* @param extensionId - The unique identifier of the wallet extension
|
|
85
|
+
* @param port - The MessagePort for encrypted communication with the wallet
|
|
90
86
|
* @param sharedKey - The derived AES-256-GCM shared key for encryption
|
|
87
|
+
* @param chainInfo - The chain information (chainId and version) for request context
|
|
91
88
|
* @param appId - Application identifier used to identify the requesting dApp to the wallet
|
|
92
|
-
* @returns
|
|
89
|
+
* @returns A Wallet interface where all method calls are encrypted
|
|
93
90
|
*
|
|
94
91
|
* @example
|
|
95
92
|
* ```typescript
|
|
96
|
-
* const
|
|
97
|
-
* const
|
|
98
|
-
* const
|
|
99
|
-
* info,
|
|
100
|
-
*
|
|
101
|
-
*
|
|
102
|
-
*
|
|
93
|
+
* const discoveredWallets = await ExtensionProvider.discoverWallets(chainInfo, { appId: 'my-defi-app' });
|
|
94
|
+
* const connection = await discoveredWallets[0].establishSecureChannel();
|
|
95
|
+
* const wallet = ExtensionWallet.create(
|
|
96
|
+
* connection.info.id,
|
|
97
|
+
* connection.port,
|
|
98
|
+
* connection.sharedKey,
|
|
99
|
+
* chainInfo,
|
|
103
100
|
* 'my-defi-app'
|
|
104
101
|
* );
|
|
105
102
|
*
|
|
106
|
-
* // Register disconnect handler
|
|
107
|
-
* extensionWallet.onDisconnect(() => console.log('Disconnected!'));
|
|
108
|
-
*
|
|
109
|
-
* // Get the Wallet interface for dApp usage
|
|
110
|
-
* const wallet = extensionWallet.getWallet();
|
|
111
103
|
* const accounts = await wallet.getAccounts();
|
|
112
104
|
* ```
|
|
113
105
|
*/
|
|
114
106
|
static create(
|
|
115
|
-
|
|
116
|
-
chainInfo: ChainInfo,
|
|
107
|
+
extensionId: string,
|
|
117
108
|
port: MessagePort,
|
|
118
109
|
sharedKey: CryptoKey,
|
|
110
|
+
chainInfo: ChainInfo,
|
|
119
111
|
appId: string,
|
|
120
|
-
):
|
|
121
|
-
const wallet = new ExtensionWallet(chainInfo, appId,
|
|
122
|
-
|
|
123
|
-
// Set up message handler
|
|
124
|
-
wallet.port.onmessage = (event: MessageEvent
|
|
125
|
-
|
|
112
|
+
): Wallet {
|
|
113
|
+
const wallet = new ExtensionWallet(chainInfo, appId, extensionId, port, sharedKey);
|
|
114
|
+
|
|
115
|
+
// Set up message handler for encrypted responses and unencrypted control messages
|
|
116
|
+
wallet.port.onmessage = (event: MessageEvent) => {
|
|
117
|
+
const data = event.data;
|
|
118
|
+
// Check for unencrypted disconnect notification
|
|
119
|
+
if (data && typeof data === 'object' && 'type' in data && data.type === WalletMessageType.DISCONNECT) {
|
|
120
|
+
wallet.handleDisconnect();
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
// Otherwise treat as encrypted payload
|
|
124
|
+
void wallet.handleEncryptedResponse(data as EncryptedPayload);
|
|
126
125
|
};
|
|
127
126
|
|
|
128
127
|
wallet.port.start();
|
|
129
128
|
|
|
130
|
-
return wallet
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* Returns a Wallet interface that proxies all method calls through the secure channel.
|
|
135
|
-
*
|
|
136
|
-
* The returned Wallet can be used directly by dApps - all method calls are automatically
|
|
137
|
-
* encrypted and sent to the wallet extension.
|
|
138
|
-
*
|
|
139
|
-
* @returns A Wallet implementation that encrypts all communication
|
|
140
|
-
*
|
|
141
|
-
* @example
|
|
142
|
-
* ```typescript
|
|
143
|
-
* const extensionWallet = ExtensionWallet.create(info, chainInfo, port, sharedKey, 'my-app');
|
|
144
|
-
* const wallet = extensionWallet.getWallet();
|
|
145
|
-
* const accounts = await wallet.getAccounts();
|
|
146
|
-
* ```
|
|
147
|
-
*/
|
|
148
|
-
getWallet(): Wallet {
|
|
149
|
-
if (this.walletProxy) {
|
|
150
|
-
return this.walletProxy;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
// Create a Proxy that intercepts wallet method calls and forwards them to the extension
|
|
154
|
-
this.walletProxy = new Proxy(this, {
|
|
129
|
+
return new Proxy(wallet, {
|
|
155
130
|
get: (target, prop) => {
|
|
156
131
|
if (schemaHasMethod(WalletSchema, prop.toString())) {
|
|
157
132
|
return async (...args: unknown[]) => {
|
|
@@ -166,8 +141,6 @@ export class ExtensionWallet {
|
|
|
166
141
|
}
|
|
167
142
|
},
|
|
168
143
|
}) as unknown as Wallet;
|
|
169
|
-
|
|
170
|
-
return this.walletProxy;
|
|
171
144
|
}
|
|
172
145
|
|
|
173
146
|
/**
|
|
@@ -188,18 +161,6 @@ export class ExtensionWallet {
|
|
|
188
161
|
|
|
189
162
|
const { messageId, result, error, walletId: responseWalletId } = response;
|
|
190
163
|
|
|
191
|
-
// Check for disconnect notification from the wallet backend
|
|
192
|
-
// This is sent as an encrypted error response with a special type
|
|
193
|
-
if (
|
|
194
|
-
error &&
|
|
195
|
-
typeof error === 'object' &&
|
|
196
|
-
'type' in error &&
|
|
197
|
-
(error.type as WalletMessageType) === WalletMessageType.SESSION_DISCONNECTED
|
|
198
|
-
) {
|
|
199
|
-
this.handleDisconnect();
|
|
200
|
-
return;
|
|
201
|
-
}
|
|
202
|
-
|
|
203
164
|
if (!messageId || !responseWalletId) {
|
|
204
165
|
return;
|
|
205
166
|
}
|
|
@@ -254,8 +215,7 @@ export class ExtensionWallet {
|
|
|
254
215
|
walletId: this.extensionId,
|
|
255
216
|
};
|
|
256
217
|
|
|
257
|
-
|
|
258
|
-
const encrypted = await encrypt(this.sharedKey, message);
|
|
218
|
+
const encrypted = await encrypt(this.sharedKey, jsonStringify(message));
|
|
259
219
|
this.port.postMessage(encrypted);
|
|
260
220
|
|
|
261
221
|
const { promise, resolve, reject } = promiseWithResolvers<unknown>();
|
|
@@ -274,27 +234,22 @@ export class ExtensionWallet {
|
|
|
274
234
|
}
|
|
275
235
|
this.disconnected = true;
|
|
276
236
|
|
|
277
|
-
// Close the port to prevent any further messages
|
|
278
237
|
if (this.port) {
|
|
279
238
|
this.port.onmessage = null;
|
|
280
239
|
this.port.close();
|
|
281
240
|
}
|
|
282
241
|
|
|
283
|
-
// Reject all pending requests
|
|
284
|
-
// Note: These rejections should be caught by the callers, but we log them
|
|
285
|
-
// here to help with debugging if they become unhandled
|
|
286
242
|
const error = new Error('Wallet disconnected');
|
|
287
243
|
for (const { reject } of this.inFlight.values()) {
|
|
288
244
|
reject(error);
|
|
289
245
|
}
|
|
290
246
|
this.inFlight.clear();
|
|
291
247
|
|
|
292
|
-
// Notify registered callbacks
|
|
293
248
|
for (const callback of this.disconnectCallbacks) {
|
|
294
249
|
try {
|
|
295
250
|
callback();
|
|
296
251
|
} catch {
|
|
297
|
-
// Ignore errors
|
|
252
|
+
// Ignore errors on disconnect callbacks
|
|
298
253
|
}
|
|
299
254
|
}
|
|
300
255
|
}
|
|
@@ -343,37 +298,24 @@ export class ExtensionWallet {
|
|
|
343
298
|
*
|
|
344
299
|
* @example
|
|
345
300
|
* ```typescript
|
|
346
|
-
* const
|
|
301
|
+
* const extensionWallet = ExtensionWallet.create(extensionId, port, sharedKey, chainInfo, 'my-app');
|
|
347
302
|
* // ... use wallet ...
|
|
348
|
-
* await
|
|
303
|
+
* await extensionWallet.disconnect(); // Clean disconnect when done
|
|
349
304
|
* ```
|
|
350
305
|
*/
|
|
306
|
+
// eslint-disable-next-line require-await -- async for interface compatibility
|
|
351
307
|
async disconnect(): Promise<void> {
|
|
352
308
|
if (this.disconnected) {
|
|
353
309
|
return;
|
|
354
310
|
}
|
|
355
311
|
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
messageId: globalThis.crypto.randomUUID(),
|
|
362
|
-
chainInfo: this.chainInfo,
|
|
363
|
-
appId: this.appId,
|
|
364
|
-
walletId: this.extensionId,
|
|
365
|
-
args: [],
|
|
366
|
-
};
|
|
367
|
-
const encrypted = await encrypt(this.sharedKey, message);
|
|
368
|
-
this.port.postMessage(encrypted);
|
|
369
|
-
} catch {
|
|
370
|
-
// Ignore errors sending disconnect message
|
|
371
|
-
}
|
|
312
|
+
if (this.port) {
|
|
313
|
+
// Send unencrypted disconnect - control messages don't need encryption
|
|
314
|
+
this.port.postMessage({
|
|
315
|
+
type: WalletMessageType.DISCONNECT,
|
|
316
|
+
});
|
|
372
317
|
}
|
|
373
318
|
|
|
374
319
|
this.handleDisconnect();
|
|
375
|
-
if (this.port) {
|
|
376
|
-
this.port.close();
|
|
377
|
-
}
|
|
378
320
|
}
|
|
379
321
|
}
|
package/src/manager/index.ts
CHANGED
|
@@ -5,16 +5,8 @@ export type {
|
|
|
5
5
|
WebWalletConfig,
|
|
6
6
|
WalletProviderType,
|
|
7
7
|
WalletProvider,
|
|
8
|
+
PendingConnection,
|
|
8
9
|
ProviderDisconnectionCallback,
|
|
9
10
|
DiscoverWalletsOptions,
|
|
11
|
+
DiscoverySession,
|
|
10
12
|
} from './types.js';
|
|
11
|
-
|
|
12
|
-
// Re-export types and enums from providers for convenience
|
|
13
|
-
export { WalletMessageType } from '../types.js';
|
|
14
|
-
export type { WalletInfo, WalletMessage, WalletResponse, DiscoveryRequest, DiscoveryResponse } from '../types.js';
|
|
15
|
-
|
|
16
|
-
// Re-export commonly needed utilities for wallet integration
|
|
17
|
-
export { ChainInfoSchema } from '@aztec/aztec.js/account';
|
|
18
|
-
export type { ChainInfo } from '@aztec/aztec.js/account';
|
|
19
|
-
export { WalletSchema } from '@aztec/aztec.js/wallet';
|
|
20
|
-
export { jsonStringify } from '@aztec/foundation/json-rpc';
|