@aztec/wallet-sdk 4.1.2 → 4.2.0-aztecnr-rc.2
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/dest/base-wallet/base_wallet.d.ts +23 -17
- package/dest/base-wallet/base_wallet.d.ts.map +1 -1
- package/dest/base-wallet/base_wallet.js +70 -45
- package/dest/base-wallet/index.d.ts +2 -2
- package/dest/base-wallet/index.d.ts.map +1 -1
- package/dest/crypto.d.ts +39 -1
- package/dest/crypto.d.ts.map +1 -1
- package/dest/crypto.js +88 -0
- package/dest/extension/provider/extension_wallet.d.ts +2 -5
- package/dest/extension/provider/extension_wallet.d.ts.map +1 -1
- package/dest/extension/provider/index.d.ts +2 -2
- package/dest/extension/provider/index.d.ts.map +1 -1
- package/dest/iframe/handlers/iframe_connection_handler.d.ts +118 -0
- package/dest/iframe/handlers/iframe_connection_handler.d.ts.map +1 -0
- package/dest/iframe/handlers/iframe_connection_handler.js +228 -0
- package/dest/iframe/handlers/index.d.ts +2 -0
- package/dest/iframe/handlers/index.d.ts.map +1 -0
- package/dest/iframe/handlers/index.js +1 -0
- package/dest/iframe/provider/iframe_discovery.d.ts +25 -0
- package/dest/iframe/provider/iframe_discovery.d.ts.map +1 -0
- package/dest/iframe/provider/iframe_discovery.js +167 -0
- package/dest/iframe/provider/iframe_provider.d.ts +65 -0
- package/dest/iframe/provider/iframe_provider.d.ts.map +1 -0
- package/dest/iframe/provider/iframe_provider.js +257 -0
- package/dest/iframe/provider/iframe_wallet.d.ts +68 -0
- package/dest/iframe/provider/iframe_wallet.d.ts.map +1 -0
- package/dest/iframe/provider/iframe_wallet.js +200 -0
- package/dest/iframe/provider/index.d.ts +4 -0
- package/dest/iframe/provider/index.d.ts.map +1 -0
- package/dest/iframe/provider/index.js +3 -0
- package/dest/manager/types.d.ts +3 -2
- 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 +46 -16
- package/dest/types.d.ts +14 -2
- package/dest/types.d.ts.map +1 -1
- package/dest/types.js +4 -0
- package/package.json +12 -8
- package/src/base-wallet/base_wallet.ts +122 -78
- package/src/base-wallet/index.ts +1 -1
- package/src/crypto.ts +104 -0
- package/src/extension/provider/extension_wallet.ts +1 -6
- package/src/extension/provider/index.ts +1 -1
- package/src/iframe/handlers/iframe_connection_handler.ts +328 -0
- package/src/iframe/handlers/index.ts +7 -0
- package/src/iframe/provider/iframe_discovery.ts +185 -0
- package/src/iframe/provider/iframe_provider.ts +331 -0
- package/src/iframe/provider/iframe_wallet.ts +229 -0
- package/src/iframe/provider/index.ts +3 -0
- package/src/manager/types.ts +2 -1
- package/src/manager/wallet_manager.ts +48 -14
- package/src/types.ts +13 -0
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* IframeWalletProvider — implements {@link WalletProvider} for web wallets loaded in iframes.
|
|
3
|
+
*
|
|
4
|
+
* Flow (mirrors ExtensionProvider):
|
|
5
|
+
* 1. Creates an `<iframe src="walletUrl">` (in app-provided container or floating panel)
|
|
6
|
+
* 2. Waits for WALLET_READY message from the iframe
|
|
7
|
+
* 3. Sends DISCOVERY → waits for DISCOVERY_RESPONSE
|
|
8
|
+
* 4. Sends KEY_EXCHANGE_REQUEST (ECDH public key) → waits for KEY_EXCHANGE_RESPONSE
|
|
9
|
+
* 5. Derives shared session keys, exposes verificationHash
|
|
10
|
+
* 6. On confirm(): returns IframeWallet backed by the established session
|
|
11
|
+
*/
|
|
12
|
+
import type { ChainInfo } from '@aztec/aztec.js/account';
|
|
13
|
+
import type { Wallet } from '@aztec/aztec.js/wallet';
|
|
14
|
+
|
|
15
|
+
import {
|
|
16
|
+
type ExportedPublicKey,
|
|
17
|
+
deriveSessionKeys,
|
|
18
|
+
exportPublicKey,
|
|
19
|
+
generateKeyPair,
|
|
20
|
+
importPublicKey,
|
|
21
|
+
} from '../../crypto.js';
|
|
22
|
+
import type { PendingConnection, ProviderDisconnectionCallback, WalletProvider } from '../../manager/types.js';
|
|
23
|
+
import type { WalletInfo } from '../../types.js';
|
|
24
|
+
import { WalletMessageType } from '../../types.js';
|
|
25
|
+
import { IframeWallet } from './iframe_wallet.js';
|
|
26
|
+
|
|
27
|
+
const READY_TIMEOUT_MS = 15_000;
|
|
28
|
+
const DISCOVERY_TIMEOUT_MS = 15_000;
|
|
29
|
+
const KEY_EXCHANGE_TIMEOUT_MS = 15_000;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Options for {@link IframeWalletProvider.establishSecureChannel}.
|
|
33
|
+
*/
|
|
34
|
+
export interface IframeConnectionOptions {
|
|
35
|
+
/** Container element to inject the iframe into. If omitted, a floating panel is created. */
|
|
36
|
+
container?: HTMLElement;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* A {@link WalletProvider} that connects to a web wallet loaded in a cross-origin iframe.
|
|
41
|
+
*
|
|
42
|
+
* Discovered via {@link discoverWebWallets} and used through the standard
|
|
43
|
+
* `WalletProvider.establishSecureChannel()` flow.
|
|
44
|
+
*/
|
|
45
|
+
export class IframeWalletProvider implements WalletProvider {
|
|
46
|
+
readonly type = 'web' as const;
|
|
47
|
+
|
|
48
|
+
private iframe: HTMLIFrameElement | null = null;
|
|
49
|
+
private _container: HTMLDivElement | null = null;
|
|
50
|
+
private _appOwnsContainer = false;
|
|
51
|
+
private _dragCleanup: (() => void) | null = null;
|
|
52
|
+
private wallet: IframeWallet | null = null;
|
|
53
|
+
private _disconnected = false;
|
|
54
|
+
private disconnectCallbacks: ProviderDisconnectionCallback[] = [];
|
|
55
|
+
|
|
56
|
+
constructor(
|
|
57
|
+
/** Unique wallet identifier. */
|
|
58
|
+
public readonly id: string,
|
|
59
|
+
/** Display name for the wallet. */
|
|
60
|
+
public readonly name: string,
|
|
61
|
+
/** Optional wallet icon URL. */
|
|
62
|
+
public readonly icon: string | undefined,
|
|
63
|
+
private readonly walletUrl: string,
|
|
64
|
+
private readonly chainInfo: ChainInfo,
|
|
65
|
+
) {}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Establishes a secure encrypted channel with the iframe wallet.
|
|
69
|
+
*
|
|
70
|
+
* @param appId - Application identifier for the requesting dApp
|
|
71
|
+
* @param options - Optional container element for the iframe
|
|
72
|
+
* @returns A {@link PendingConnection} with verification hash and confirm/cancel
|
|
73
|
+
*/
|
|
74
|
+
async establishSecureChannel(appId: string, options?: IframeConnectionOptions): Promise<PendingConnection> {
|
|
75
|
+
const iframe = document.createElement('iframe');
|
|
76
|
+
iframe.src = this.walletUrl;
|
|
77
|
+
iframe.style.cssText = 'flex:1;border:none;width:100%;height:100%;display:block;';
|
|
78
|
+
iframe.allow = 'storage-access; cross-origin-isolated';
|
|
79
|
+
this.iframe = iframe;
|
|
80
|
+
|
|
81
|
+
if (options?.container) {
|
|
82
|
+
this._appOwnsContainer = true;
|
|
83
|
+
options.container.appendChild(iframe);
|
|
84
|
+
} else {
|
|
85
|
+
this.createFloatingPanel(iframe);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const walletOrigin = new URL(this.walletUrl).origin;
|
|
89
|
+
|
|
90
|
+
const post = (msg: object) => {
|
|
91
|
+
if (!iframe.contentWindow) {
|
|
92
|
+
throw new Error('Iframe not ready');
|
|
93
|
+
}
|
|
94
|
+
iframe.contentWindow.postMessage(msg, walletOrigin);
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
await waitForMessage(msg => msg.type === WalletMessageType.WALLET_READY, READY_TIMEOUT_MS, walletOrigin);
|
|
98
|
+
|
|
99
|
+
const requestId = globalThis.crypto.randomUUID();
|
|
100
|
+
post({ type: WalletMessageType.DISCOVERY, requestId, appId });
|
|
101
|
+
|
|
102
|
+
const discoveryResp = await waitForMessage(
|
|
103
|
+
msg => msg.type === WalletMessageType.DISCOVERY_RESPONSE && msg.requestId === requestId,
|
|
104
|
+
DISCOVERY_TIMEOUT_MS,
|
|
105
|
+
walletOrigin,
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
const walletInfo = discoveryResp.walletInfo as WalletInfo;
|
|
109
|
+
|
|
110
|
+
const keyPair = await generateKeyPair();
|
|
111
|
+
const dAppPublicKey = await exportPublicKey(keyPair.publicKey);
|
|
112
|
+
post({ type: WalletMessageType.KEY_EXCHANGE_REQUEST, requestId, publicKey: dAppPublicKey });
|
|
113
|
+
|
|
114
|
+
const keyExchangeResp = await waitForMessage(
|
|
115
|
+
msg => msg.type === WalletMessageType.KEY_EXCHANGE_RESPONSE && msg.requestId === requestId,
|
|
116
|
+
KEY_EXCHANGE_TIMEOUT_MS,
|
|
117
|
+
walletOrigin,
|
|
118
|
+
);
|
|
119
|
+
|
|
120
|
+
const walletPublicKey = await importPublicKey(keyExchangeResp.publicKey as ExportedPublicKey);
|
|
121
|
+
const sessionKeys = await deriveSessionKeys(keyPair, walletPublicKey, true);
|
|
122
|
+
const { verificationHash, encryptionKey: sharedKey } = sessionKeys;
|
|
123
|
+
|
|
124
|
+
const iframeWallet = IframeWallet.create(
|
|
125
|
+
walletInfo.id,
|
|
126
|
+
requestId, // sessionId
|
|
127
|
+
iframe.contentWindow!,
|
|
128
|
+
walletOrigin,
|
|
129
|
+
sharedKey,
|
|
130
|
+
this.chainInfo,
|
|
131
|
+
appId,
|
|
132
|
+
);
|
|
133
|
+
|
|
134
|
+
this.wallet = iframeWallet;
|
|
135
|
+
|
|
136
|
+
iframeWallet.onDisconnect(() => {
|
|
137
|
+
this._disconnected = true;
|
|
138
|
+
for (const cb of this.disconnectCallbacks) {
|
|
139
|
+
try {
|
|
140
|
+
cb();
|
|
141
|
+
} catch {
|
|
142
|
+
// ignore
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
let cancelled = false;
|
|
148
|
+
|
|
149
|
+
return {
|
|
150
|
+
verificationHash,
|
|
151
|
+
confirm: (): Promise<Wallet> => {
|
|
152
|
+
if (cancelled) {
|
|
153
|
+
throw new Error('Connection was cancelled');
|
|
154
|
+
}
|
|
155
|
+
return Promise.resolve(iframeWallet.asWallet());
|
|
156
|
+
},
|
|
157
|
+
cancel: () => {
|
|
158
|
+
cancelled = true;
|
|
159
|
+
this.cleanup();
|
|
160
|
+
},
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
disconnect(): Promise<void> {
|
|
165
|
+
if (this.wallet && !this.wallet.isDisconnected()) {
|
|
166
|
+
this.wallet.disconnect();
|
|
167
|
+
}
|
|
168
|
+
this.cleanup();
|
|
169
|
+
return Promise.resolve();
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
onDisconnect(callback: ProviderDisconnectionCallback): () => void {
|
|
173
|
+
this.disconnectCallbacks.push(callback);
|
|
174
|
+
return () => {
|
|
175
|
+
const i = this.disconnectCallbacks.indexOf(callback);
|
|
176
|
+
if (i !== -1) {
|
|
177
|
+
this.disconnectCallbacks.splice(i, 1);
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
isDisconnected(): boolean {
|
|
183
|
+
return this._disconnected;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// ── Floating panel creation ─────────────────────────────────────────────────
|
|
187
|
+
|
|
188
|
+
private createFloatingPanel(iframe: HTMLIFrameElement): void {
|
|
189
|
+
const W = 420,
|
|
190
|
+
H = 500;
|
|
191
|
+
const initLeft = window.innerWidth - W - 24;
|
|
192
|
+
const initTop = window.innerHeight - H - 24;
|
|
193
|
+
|
|
194
|
+
const container = document.createElement('div');
|
|
195
|
+
container.style.cssText = `
|
|
196
|
+
position:fixed;left:${initLeft}px;top:${initTop}px;width:${W}px;height:${H}px;
|
|
197
|
+
border-radius:12px;box-shadow:0 8px 32px rgba(0,0,0,0.4);z-index:999999;
|
|
198
|
+
overflow:hidden;display:flex;flex-direction:column;user-select:none;
|
|
199
|
+
`;
|
|
200
|
+
|
|
201
|
+
const dragHandle = document.createElement('div');
|
|
202
|
+
dragHandle.style.cssText = `
|
|
203
|
+
height:28px;min-height:28px;background:rgba(30,30,30,0.95);cursor:grab;
|
|
204
|
+
display:flex;align-items:center;justify-content:center;
|
|
205
|
+
border-bottom:1px solid rgba(255,255,255,0.08);flex-shrink:0;
|
|
206
|
+
`;
|
|
207
|
+
dragHandle.innerHTML = `<span style="color:rgba(255,255,255,0.3);font-size:14px;letter-spacing:4px">⋮⋮⋮</span>`;
|
|
208
|
+
|
|
209
|
+
const resizeHandle = document.createElement('div');
|
|
210
|
+
resizeHandle.style.cssText =
|
|
211
|
+
'position:absolute;bottom:0;right:0;width:16px;height:16px;cursor:se-resize;z-index:1;';
|
|
212
|
+
resizeHandle.innerHTML = `<svg width="16" height="16" style="opacity:0.3;display:block"><path d="M2 14 L14 2 M6 14 L14 6 M10 14 L14 10" stroke="white" stroke-width="1.5"/></svg>`;
|
|
213
|
+
|
|
214
|
+
container.appendChild(dragHandle);
|
|
215
|
+
container.appendChild(iframe);
|
|
216
|
+
container.appendChild(resizeHandle);
|
|
217
|
+
document.body.appendChild(container);
|
|
218
|
+
this._container = container;
|
|
219
|
+
|
|
220
|
+
let dragging = false;
|
|
221
|
+
let dragOffsetX = 0,
|
|
222
|
+
dragOffsetY = 0;
|
|
223
|
+
|
|
224
|
+
dragHandle.addEventListener('mousedown', (e: MouseEvent) => {
|
|
225
|
+
dragging = true;
|
|
226
|
+
dragHandle.style.cursor = 'grabbing';
|
|
227
|
+
const rect = container.getBoundingClientRect();
|
|
228
|
+
dragOffsetX = e.clientX - rect.left;
|
|
229
|
+
dragOffsetY = e.clientY - rect.top;
|
|
230
|
+
iframe.style.pointerEvents = 'none';
|
|
231
|
+
e.preventDefault();
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
let resizing = false;
|
|
235
|
+
let resizeStartX = 0,
|
|
236
|
+
resizeStartY = 0;
|
|
237
|
+
let resizeStartW = 0,
|
|
238
|
+
resizeStartH = 0;
|
|
239
|
+
const MIN_W = 280,
|
|
240
|
+
MIN_H = 320;
|
|
241
|
+
|
|
242
|
+
resizeHandle.addEventListener('mousedown', (e: MouseEvent) => {
|
|
243
|
+
resizing = true;
|
|
244
|
+
resizeStartX = e.clientX;
|
|
245
|
+
resizeStartY = e.clientY;
|
|
246
|
+
resizeStartW = container.offsetWidth;
|
|
247
|
+
resizeStartH = container.offsetHeight;
|
|
248
|
+
iframe.style.pointerEvents = 'none';
|
|
249
|
+
e.preventDefault();
|
|
250
|
+
e.stopPropagation();
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
const onMouseMove = (e: MouseEvent) => {
|
|
254
|
+
if (dragging) {
|
|
255
|
+
const newLeft = Math.max(0, Math.min(window.innerWidth - container.offsetWidth, e.clientX - dragOffsetX));
|
|
256
|
+
const newTop = Math.max(0, Math.min(window.innerHeight - container.offsetHeight, e.clientY - dragOffsetY));
|
|
257
|
+
container.style.left = `${newLeft}px`;
|
|
258
|
+
container.style.top = `${newTop}px`;
|
|
259
|
+
} else if (resizing) {
|
|
260
|
+
const newW = Math.max(MIN_W, resizeStartW + (e.clientX - resizeStartX));
|
|
261
|
+
const newH = Math.max(MIN_H, resizeStartH + (e.clientY - resizeStartY));
|
|
262
|
+
container.style.width = `${newW}px`;
|
|
263
|
+
container.style.height = `${newH}px`;
|
|
264
|
+
}
|
|
265
|
+
};
|
|
266
|
+
|
|
267
|
+
const onMouseUp = () => {
|
|
268
|
+
if (dragging) {
|
|
269
|
+
dragHandle.style.cursor = 'grab';
|
|
270
|
+
}
|
|
271
|
+
dragging = false;
|
|
272
|
+
resizing = false;
|
|
273
|
+
iframe.style.pointerEvents = '';
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
document.addEventListener('mousemove', onMouseMove);
|
|
277
|
+
document.addEventListener('mouseup', onMouseUp);
|
|
278
|
+
this._dragCleanup = () => {
|
|
279
|
+
document.removeEventListener('mousemove', onMouseMove);
|
|
280
|
+
document.removeEventListener('mouseup', onMouseUp);
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
private cleanup(): void {
|
|
285
|
+
this._dragCleanup?.();
|
|
286
|
+
this._dragCleanup = null;
|
|
287
|
+
|
|
288
|
+
if (this._appOwnsContainer) {
|
|
289
|
+
if (this.iframe && this.iframe.parentNode) {
|
|
290
|
+
this.iframe.parentNode.removeChild(this.iframe);
|
|
291
|
+
}
|
|
292
|
+
} else if (this._container && this._container.parentNode) {
|
|
293
|
+
this._container.parentNode.removeChild(this._container);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
this._container = null;
|
|
297
|
+
this.iframe = null;
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/** @internal */
|
|
302
|
+
function waitForMessage(
|
|
303
|
+
predicate: (msg: Record<string, unknown>) => boolean,
|
|
304
|
+
timeoutMs: number,
|
|
305
|
+
expectedOrigin: string,
|
|
306
|
+
): Promise<Record<string, unknown>> {
|
|
307
|
+
return new Promise((resolve, reject) => {
|
|
308
|
+
const timer = setTimeout(() => {
|
|
309
|
+
window.removeEventListener('message', handler);
|
|
310
|
+
reject(new Error(`Iframe wallet: timed out waiting for message (${timeoutMs}ms)`));
|
|
311
|
+
}, timeoutMs);
|
|
312
|
+
|
|
313
|
+
/** Handles incoming postMessage events, filtering by origin and predicate. */
|
|
314
|
+
function handler(event: MessageEvent) {
|
|
315
|
+
if (event.origin !== expectedOrigin) {
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
318
|
+
const msg = event.data;
|
|
319
|
+
if (!msg || typeof msg !== 'object') {
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
if (predicate(msg as Record<string, unknown>)) {
|
|
323
|
+
clearTimeout(timer);
|
|
324
|
+
window.removeEventListener('message', handler);
|
|
325
|
+
resolve(msg as Record<string, unknown>);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
window.addEventListener('message', handler);
|
|
330
|
+
});
|
|
331
|
+
}
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* IframeWallet — Wallet proxy that communicates with a web wallet loaded in an iframe.
|
|
3
|
+
*
|
|
4
|
+
* This mirrors {@link ExtensionWallet} from `@aztec/wallet-sdk/extension/provider` but uses
|
|
5
|
+
* `window.postMessage` / `window.addEventListener('message')` instead of MessagePort.
|
|
6
|
+
*
|
|
7
|
+
* The wire protocol (encrypted {@link WalletMessage} / {@link WalletResponse}) is identical.
|
|
8
|
+
*/
|
|
9
|
+
import type { ChainInfo } from '@aztec/aztec.js/account';
|
|
10
|
+
import { type Wallet, WalletSchema } from '@aztec/aztec.js/wallet';
|
|
11
|
+
import { jsonStringify } from '@aztec/foundation/json-rpc';
|
|
12
|
+
import { type PromiseWithResolvers, promiseWithResolvers } from '@aztec/foundation/promise';
|
|
13
|
+
import { schemaHasMethod } from '@aztec/foundation/schemas';
|
|
14
|
+
import type { FunctionsOf } from '@aztec/foundation/types';
|
|
15
|
+
|
|
16
|
+
import { type EncryptedPayload, decrypt, encrypt } from '../../crypto.js';
|
|
17
|
+
import { type DisconnectCallback, type WalletMessage, WalletMessageType, type WalletResponse } from '../../types.js';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Internal type representing a wallet method call before encryption.
|
|
21
|
+
* @internal
|
|
22
|
+
*/
|
|
23
|
+
type WalletMethodCall = {
|
|
24
|
+
/** Wallet method name to invoke. */
|
|
25
|
+
type: keyof FunctionsOf<Wallet>;
|
|
26
|
+
/** Arguments to pass to the wallet method. */
|
|
27
|
+
args: unknown[];
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* A wallet implementation that communicates with a web wallet loaded in an iframe
|
|
32
|
+
* using encrypted postMessage.
|
|
33
|
+
*
|
|
34
|
+
* Uses the same Proxy pattern as {@link ExtensionWallet}: intercepts property access,
|
|
35
|
+
* checks if the property is a Wallet method via {@link WalletSchema}, and routes the
|
|
36
|
+
* call through an encrypted postMessage channel.
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```typescript
|
|
40
|
+
* const wallet = IframeWallet.create(walletId, sessionId, iframeWindow, walletOrigin, sharedKey, chainInfo, appId);
|
|
41
|
+
* const accounts = await wallet.asWallet().getAccounts();
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export class IframeWallet {
|
|
45
|
+
private inFlight = new Map<string, PromiseWithResolvers<unknown>>();
|
|
46
|
+
private disconnected = false;
|
|
47
|
+
private disconnectCallbacks: DisconnectCallback[] = [];
|
|
48
|
+
private messageListener: ((e: MessageEvent) => void) | null = null;
|
|
49
|
+
|
|
50
|
+
private constructor(
|
|
51
|
+
private chainInfo: ChainInfo,
|
|
52
|
+
private appId: string,
|
|
53
|
+
private walletId: string,
|
|
54
|
+
private sessionId: string,
|
|
55
|
+
private iframeWindow: Window,
|
|
56
|
+
private walletOrigin: string,
|
|
57
|
+
private sharedKey: CryptoKey,
|
|
58
|
+
) {}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Creates a proxied IframeWallet that implements the {@link Wallet} interface.
|
|
62
|
+
*
|
|
63
|
+
* All Wallet method calls are intercepted by a Proxy, encrypted with the shared
|
|
64
|
+
* AES-256-GCM key, sent via postMessage, and the response is decrypted and
|
|
65
|
+
* validated against {@link WalletSchema}.
|
|
66
|
+
*
|
|
67
|
+
* @param walletId - Unique identifier of the remote wallet
|
|
68
|
+
* @param sessionId - Session identifier from the key exchange
|
|
69
|
+
* @param iframeWindow - The iframe's contentWindow to post messages to
|
|
70
|
+
* @param walletOrigin - Origin of the wallet iframe (for postMessage targeting)
|
|
71
|
+
* @param sharedKey - AES-256-GCM key derived from ECDH key exchange
|
|
72
|
+
* @param chainInfo - Network information (chainId and version)
|
|
73
|
+
* @param appId - Application identifier for the requesting dApp
|
|
74
|
+
* @returns A proxied IframeWallet — call `.asWallet()` to get the typed `Wallet`
|
|
75
|
+
*/
|
|
76
|
+
static create(
|
|
77
|
+
walletId: string,
|
|
78
|
+
sessionId: string,
|
|
79
|
+
iframeWindow: Window,
|
|
80
|
+
walletOrigin: string,
|
|
81
|
+
sharedKey: CryptoKey,
|
|
82
|
+
chainInfo: ChainInfo,
|
|
83
|
+
appId: string,
|
|
84
|
+
): IframeWallet {
|
|
85
|
+
const wallet = new IframeWallet(chainInfo, appId, walletId, sessionId, iframeWindow, walletOrigin, sharedKey);
|
|
86
|
+
|
|
87
|
+
wallet.messageListener = (event: MessageEvent) => {
|
|
88
|
+
if (event.origin !== walletOrigin) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
const msg = event.data;
|
|
92
|
+
if (!msg || typeof msg !== 'object') {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (msg.type === WalletMessageType.SECURE_RESPONSE && msg.sessionId === sessionId) {
|
|
97
|
+
void wallet.handleEncryptedResponse(msg.encrypted as EncryptedPayload);
|
|
98
|
+
} else if (msg.type === WalletMessageType.SESSION_DISCONNECTED && msg.sessionId === sessionId) {
|
|
99
|
+
wallet.handleDisconnect();
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
window.addEventListener('message', wallet.messageListener);
|
|
103
|
+
|
|
104
|
+
return new Proxy(wallet, {
|
|
105
|
+
get: (target, prop, receiver) => {
|
|
106
|
+
if (prop === 'asWallet') {
|
|
107
|
+
return () => receiver as unknown as Wallet;
|
|
108
|
+
} else if (schemaHasMethod(WalletSchema, prop.toString())) {
|
|
109
|
+
return async (...args: unknown[]) => {
|
|
110
|
+
const result = await target.postMessage({
|
|
111
|
+
type: prop.toString() as keyof FunctionsOf<Wallet>,
|
|
112
|
+
args,
|
|
113
|
+
});
|
|
114
|
+
return WalletSchema[prop.toString() as keyof typeof WalletSchema].returnType().parseAsync(result);
|
|
115
|
+
};
|
|
116
|
+
} else {
|
|
117
|
+
return target[prop as keyof IframeWallet];
|
|
118
|
+
}
|
|
119
|
+
},
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Returns this wallet as a typed {@link Wallet} interface.
|
|
125
|
+
* When accessed through the Proxy (via `create()`), returns the proxy itself.
|
|
126
|
+
*/
|
|
127
|
+
asWallet(): Wallet {
|
|
128
|
+
return this as unknown as Wallet;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
private async handleEncryptedResponse(encrypted: EncryptedPayload): Promise<void> {
|
|
132
|
+
try {
|
|
133
|
+
const response = await decrypt<WalletResponse>(this.sharedKey, encrypted);
|
|
134
|
+
const { messageId, result, error, walletId: responseWalletId } = response;
|
|
135
|
+
|
|
136
|
+
if (!messageId || responseWalletId !== this.walletId) {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const pending = this.inFlight.get(messageId);
|
|
141
|
+
if (!pending) {
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (error) {
|
|
146
|
+
pending.reject(new Error(jsonStringify(error)));
|
|
147
|
+
} else {
|
|
148
|
+
pending.resolve(result);
|
|
149
|
+
}
|
|
150
|
+
this.inFlight.delete(messageId);
|
|
151
|
+
} catch {
|
|
152
|
+
// Decryption errors are silently ignored (message not for us or corrupted)
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
private async postMessage(call: WalletMethodCall): Promise<unknown> {
|
|
157
|
+
if (this.disconnected) {
|
|
158
|
+
throw new Error('Wallet has been disconnected');
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const messageId = globalThis.crypto.randomUUID();
|
|
162
|
+
const message: WalletMessage = {
|
|
163
|
+
type: call.type,
|
|
164
|
+
args: call.args,
|
|
165
|
+
messageId,
|
|
166
|
+
chainInfo: this.chainInfo,
|
|
167
|
+
appId: this.appId,
|
|
168
|
+
walletId: this.walletId,
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
const encrypted = await encrypt(this.sharedKey, jsonStringify(message));
|
|
172
|
+
this.iframeWindow.postMessage(
|
|
173
|
+
{ type: WalletMessageType.SECURE_MESSAGE, sessionId: this.sessionId, encrypted },
|
|
174
|
+
this.walletOrigin,
|
|
175
|
+
);
|
|
176
|
+
|
|
177
|
+
const { promise, resolve, reject } = promiseWithResolvers<unknown>();
|
|
178
|
+
this.inFlight.set(messageId, { promise, resolve, reject });
|
|
179
|
+
return promise;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
private handleDisconnect(): void {
|
|
183
|
+
if (this.disconnected) {
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
this.disconnected = true;
|
|
187
|
+
|
|
188
|
+
if (this.messageListener) {
|
|
189
|
+
window.removeEventListener('message', this.messageListener);
|
|
190
|
+
this.messageListener = null;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const error = new Error('Wallet disconnected');
|
|
194
|
+
for (const { reject } of this.inFlight.values()) {
|
|
195
|
+
reject(error);
|
|
196
|
+
}
|
|
197
|
+
this.inFlight.clear();
|
|
198
|
+
|
|
199
|
+
for (const cb of this.disconnectCallbacks) {
|
|
200
|
+
try {
|
|
201
|
+
cb();
|
|
202
|
+
} catch {
|
|
203
|
+
// Ignore errors in disconnect callbacks
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
onDisconnect(callback: DisconnectCallback): () => void {
|
|
209
|
+
this.disconnectCallbacks.push(callback);
|
|
210
|
+
return () => {
|
|
211
|
+
const i = this.disconnectCallbacks.indexOf(callback);
|
|
212
|
+
if (i !== -1) {
|
|
213
|
+
this.disconnectCallbacks.splice(i, 1);
|
|
214
|
+
}
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
isDisconnected(): boolean {
|
|
219
|
+
return this.disconnected;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
disconnect(): void {
|
|
223
|
+
if (this.disconnected) {
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
this.iframeWindow.postMessage({ type: WalletMessageType.DISCONNECT, sessionId: this.sessionId }, this.walletOrigin);
|
|
227
|
+
this.handleDisconnect();
|
|
228
|
+
}
|
|
229
|
+
}
|
package/src/manager/types.ts
CHANGED
|
@@ -96,6 +96,7 @@ export interface WalletProvider {
|
|
|
96
96
|
* matches their wallet before calling `confirm()`.
|
|
97
97
|
*
|
|
98
98
|
* @param appId - Application identifier for the requesting dapp
|
|
99
|
+
* @param options - Optional provider-specific options (e.g. container element for iframe wallets)
|
|
99
100
|
* @returns A pending connection with verification hash and confirm/cancel methods
|
|
100
101
|
*
|
|
101
102
|
* @example
|
|
@@ -110,7 +111,7 @@ export interface WalletProvider {
|
|
|
110
111
|
* const wallet = await pending.confirm();
|
|
111
112
|
* ```
|
|
112
113
|
*/
|
|
113
|
-
establishSecureChannel(appId: string): Promise<PendingConnection>;
|
|
114
|
+
establishSecureChannel(appId: string, options?: Record<string, unknown>): Promise<PendingConnection>;
|
|
114
115
|
/**
|
|
115
116
|
* Disconnects the current wallet and cleans up resources.
|
|
116
117
|
* After calling this, the wallet returned from confirm() should no longer be used.
|
|
@@ -2,6 +2,7 @@ import type { ChainInfo } from '@aztec/aztec.js/account';
|
|
|
2
2
|
import { promiseWithResolvers } from '@aztec/foundation/promise';
|
|
3
3
|
|
|
4
4
|
import { type DiscoveredWallet, ExtensionProvider, ExtensionWallet } from '../extension/provider/index.js';
|
|
5
|
+
import { discoverWebWallets } from '../iframe/provider/iframe_discovery.js';
|
|
5
6
|
import { WalletMessageType } from '../types.js';
|
|
6
7
|
import type {
|
|
7
8
|
DiscoverWalletsOptions,
|
|
@@ -88,6 +89,20 @@ export class WalletManager {
|
|
|
88
89
|
|
|
89
90
|
const { promise: donePromise, resolve: resolveDone } = promiseWithResolvers<void>();
|
|
90
91
|
|
|
92
|
+
const pendingSources = new Set<string>();
|
|
93
|
+
|
|
94
|
+
const emit = (provider: WalletProvider) => {
|
|
95
|
+
options.onWalletDiscovered?.(provider);
|
|
96
|
+
|
|
97
|
+
if (pendingResolve) {
|
|
98
|
+
const resolve = pendingResolve;
|
|
99
|
+
pendingResolve = null;
|
|
100
|
+
resolve({ value: provider, done: false });
|
|
101
|
+
} else {
|
|
102
|
+
pendingProviders.push(provider);
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
|
|
91
106
|
const markComplete = () => {
|
|
92
107
|
completed = true;
|
|
93
108
|
resolveDone();
|
|
@@ -98,7 +113,15 @@ export class WalletManager {
|
|
|
98
113
|
}
|
|
99
114
|
};
|
|
100
115
|
|
|
116
|
+
const sourceComplete = (source: string) => {
|
|
117
|
+
pendingSources.delete(source);
|
|
118
|
+
if (pendingSources.size === 0) {
|
|
119
|
+
markComplete();
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
|
|
101
123
|
if (this.config.extensions?.enabled) {
|
|
124
|
+
pendingSources.add('extensions');
|
|
102
125
|
const extensionConfig = this.config.extensions;
|
|
103
126
|
|
|
104
127
|
void ExtensionProvider.discoverWallets(chainInfo, {
|
|
@@ -107,24 +130,35 @@ export class WalletManager {
|
|
|
107
130
|
signal: abortController.signal,
|
|
108
131
|
onWalletDiscovered: discoveredWallet => {
|
|
109
132
|
const provider = this.createProviderFromDiscoveredWallet(discoveredWallet, chainInfo, extensionConfig);
|
|
110
|
-
if (
|
|
111
|
-
|
|
133
|
+
if (provider) {
|
|
134
|
+
emit(provider);
|
|
112
135
|
}
|
|
136
|
+
},
|
|
137
|
+
}).then(() => sourceComplete('extensions'));
|
|
138
|
+
}
|
|
113
139
|
|
|
114
|
-
|
|
115
|
-
|
|
140
|
+
if (this.config.webWallets?.urls && this.config.webWallets.urls.length > 0) {
|
|
141
|
+
pendingSources.add('webWallets');
|
|
142
|
+
const webSession = discoverWebWallets(this.config.webWallets.urls, chainInfo);
|
|
116
143
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
144
|
+
// Forward discovered web wallets into the shared iterator
|
|
145
|
+
void (async () => {
|
|
146
|
+
try {
|
|
147
|
+
for await (const provider of webSession.wallets) {
|
|
148
|
+
if (abortController.signal.aborted) {
|
|
149
|
+
break;
|
|
150
|
+
}
|
|
151
|
+
emit(provider);
|
|
124
152
|
}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
|
|
153
|
+
} finally {
|
|
154
|
+
sourceComplete('webWallets');
|
|
155
|
+
}
|
|
156
|
+
})();
|
|
157
|
+
|
|
158
|
+
abortController.signal.addEventListener('abort', () => webSession.cancel(), { once: true });
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
if (pendingSources.size === 0) {
|
|
128
162
|
markComplete();
|
|
129
163
|
}
|
|
130
164
|
|
package/src/types.ts
CHANGED
|
@@ -17,6 +17,14 @@ export enum WalletMessageType {
|
|
|
17
17
|
KEY_EXCHANGE_REQUEST = 'aztec-wallet-key-exchange-request',
|
|
18
18
|
/** Key exchange response sent over MessageChannel */
|
|
19
19
|
KEY_EXCHANGE_RESPONSE = 'aztec-wallet-key-exchange-response',
|
|
20
|
+
/** Wallet ready signal */
|
|
21
|
+
WALLET_READY = 'aztec-wallet-ready',
|
|
22
|
+
/** Encrypted wallet message wrapper */
|
|
23
|
+
SECURE_MESSAGE = 'aztec-wallet-secure-message',
|
|
24
|
+
/** Encrypted wallet response wrapper */
|
|
25
|
+
SECURE_RESPONSE = 'aztec-wallet-secure-response',
|
|
26
|
+
/** Session disconnected notification */
|
|
27
|
+
SESSION_DISCONNECTED = 'aztec-wallet-session-disconnected',
|
|
20
28
|
}
|
|
21
29
|
|
|
22
30
|
/**
|
|
@@ -130,3 +138,8 @@ export interface KeyExchangeResponse {
|
|
|
130
138
|
/** Wallet's ECDH public key for deriving shared secret */
|
|
131
139
|
publicKey: ExportedPublicKey;
|
|
132
140
|
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Callback invoked when a wallet connection is disconnected.
|
|
144
|
+
*/
|
|
145
|
+
export type DisconnectCallback = () => void;
|