@aztec/wallet-sdk 0.0.1-commit.fff30aa → 0.0.1-private.3d175340d2
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 +24 -5
- package/dest/base-wallet/base_wallet.d.ts.map +1 -1
- package/dest/base-wallet/base_wallet.js +55 -12
- package/dest/base-wallet/utils.d.ts +5 -3
- package/dest/base-wallet/utils.d.ts.map +1 -1
- package/dest/base-wallet/utils.js +8 -4
- package/dest/extension/handlers/background_connection_handler.d.ts +12 -2
- package/dest/extension/handlers/background_connection_handler.d.ts.map +1 -1
- package/dest/extension/handlers/background_connection_handler.js +44 -8
- package/dest/extension/handlers/content_script_connection_handler.d.ts +2 -1
- package/dest/extension/handlers/content_script_connection_handler.d.ts.map +1 -1
- package/dest/extension/handlers/content_script_connection_handler.js +19 -0
- package/dest/extension/handlers/internal_message_types.d.ts +3 -1
- package/dest/extension/handlers/internal_message_types.d.ts.map +1 -1
- package/dest/extension/handlers/internal_message_types.js +3 -1
- package/dest/extension/provider/extension_wallet.d.ts +26 -3
- package/dest/extension/provider/extension_wallet.d.ts.map +1 -1
- package/dest/extension/provider/extension_wallet.js +80 -9
- package/dest/iframe/handlers/iframe_connection_handler.d.ts +6 -2
- package/dest/iframe/handlers/iframe_connection_handler.d.ts.map +1 -1
- package/dest/iframe/handlers/iframe_connection_handler.js +18 -7
- package/dest/iframe/provider/iframe_wallet.d.ts +20 -3
- package/dest/iframe/provider/iframe_wallet.d.ts.map +1 -1
- package/dest/iframe/provider/iframe_wallet.js +79 -10
- package/dest/types.d.ts +52 -2
- package/dest/types.d.ts.map +1 -1
- package/dest/types.js +25 -0
- package/package.json +8 -8
- package/src/base-wallet/base_wallet.ts +62 -7
- package/src/base-wallet/utils.ts +9 -1
- package/src/extension/handlers/background_connection_handler.ts +42 -9
- package/src/extension/handlers/content_script_connection_handler.ts +18 -0
- package/src/extension/handlers/internal_message_types.ts +2 -0
- package/src/extension/provider/extension_wallet.ts +94 -8
- package/src/iframe/handlers/iframe_connection_handler.ts +21 -8
- package/src/iframe/provider/iframe_wallet.ts +103 -9
- package/src/types.ts +59 -0
|
@@ -2,11 +2,21 @@ import type { ChainInfo } from '@aztec/aztec.js/account';
|
|
|
2
2
|
import { type Wallet, WalletSchema } from '@aztec/aztec.js/wallet';
|
|
3
3
|
import { jsonStringify } from '@aztec/foundation/json-rpc';
|
|
4
4
|
import { type PromiseWithResolvers, promiseWithResolvers } from '@aztec/foundation/promise';
|
|
5
|
-
import { schemaHasMethod } from '@aztec/foundation/schemas';
|
|
5
|
+
import { getSchemaReturnType, 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 {
|
|
9
|
+
import {
|
|
10
|
+
DEFAULT_HEARTBEAT_DEAD_AFTER_MS,
|
|
11
|
+
DEFAULT_HEARTBEAT_INTERVAL_MS,
|
|
12
|
+
type DisconnectCallback,
|
|
13
|
+
type HeartbeatOptions,
|
|
14
|
+
NOOP_LOGGER,
|
|
15
|
+
type WalletMessage,
|
|
16
|
+
WalletMessageType,
|
|
17
|
+
type WalletResponse,
|
|
18
|
+
type WalletSdkLogger,
|
|
19
|
+
} from '../../types.js';
|
|
10
20
|
|
|
11
21
|
/**
|
|
12
22
|
* Internal type representing a wallet method call before encryption.
|
|
@@ -55,6 +65,11 @@ export class ExtensionWallet {
|
|
|
55
65
|
private inFlight = new Map<string, PromiseWithResolvers<unknown>>();
|
|
56
66
|
private disconnected = false;
|
|
57
67
|
private disconnectCallbacks: DisconnectCallback[] = [];
|
|
68
|
+
private heartbeatTimer: ReturnType<typeof setInterval> | null = null;
|
|
69
|
+
private lastInboundAt = 0;
|
|
70
|
+
private log: WalletSdkLogger;
|
|
71
|
+
private heartbeatIntervalMs: number;
|
|
72
|
+
private heartbeatDeadAfterMs: number;
|
|
58
73
|
|
|
59
74
|
/**
|
|
60
75
|
* Private constructor - use {@link ExtensionWallet.create} to instantiate.
|
|
@@ -63,6 +78,8 @@ export class ExtensionWallet {
|
|
|
63
78
|
* @param extensionId - The unique identifier of the target wallet extension
|
|
64
79
|
* @param port - The MessagePort for private communication with the wallet
|
|
65
80
|
* @param sharedKey - The derived AES-256-GCM shared key for encryption
|
|
81
|
+
* @param logger - Optional logger; defaults to a no-op logger
|
|
82
|
+
* @param heartbeatOptions - Optional heartbeat tuning (mostly useful for tests)
|
|
66
83
|
*/
|
|
67
84
|
private constructor(
|
|
68
85
|
private chainInfo: ChainInfo,
|
|
@@ -70,7 +87,13 @@ export class ExtensionWallet {
|
|
|
70
87
|
private extensionId: string,
|
|
71
88
|
private port: MessagePort,
|
|
72
89
|
private sharedKey: CryptoKey,
|
|
73
|
-
|
|
90
|
+
logger?: WalletSdkLogger,
|
|
91
|
+
heartbeatOptions?: HeartbeatOptions,
|
|
92
|
+
) {
|
|
93
|
+
this.log = logger ?? NOOP_LOGGER;
|
|
94
|
+
this.heartbeatIntervalMs = heartbeatOptions?.intervalMs ?? DEFAULT_HEARTBEAT_INTERVAL_MS;
|
|
95
|
+
this.heartbeatDeadAfterMs = heartbeatOptions?.deadAfterMs ?? DEFAULT_HEARTBEAT_DEAD_AFTER_MS;
|
|
96
|
+
}
|
|
74
97
|
|
|
75
98
|
/**
|
|
76
99
|
* Creates a Wallet that communicates with a browser extension
|
|
@@ -81,6 +104,8 @@ export class ExtensionWallet {
|
|
|
81
104
|
* @param sharedKey - The derived AES-256-GCM shared key for encryption
|
|
82
105
|
* @param chainInfo - The chain information (chainId and version) for request context
|
|
83
106
|
* @param appId - Application identifier used to identify the requesting dApp to the wallet
|
|
107
|
+
* @param logger - Optional logger; defaults to a no-op logger to keep extension/page bundles small
|
|
108
|
+
* @param heartbeatOptions - Optional override for heartbeat tuning (mostly useful for tests)
|
|
84
109
|
* @returns A Wallet interface where all method calls are encrypted
|
|
85
110
|
*
|
|
86
111
|
* @example
|
|
@@ -104,13 +129,17 @@ export class ExtensionWallet {
|
|
|
104
129
|
sharedKey: CryptoKey,
|
|
105
130
|
chainInfo: ChainInfo,
|
|
106
131
|
appId: string,
|
|
132
|
+
logger?: WalletSdkLogger,
|
|
133
|
+
heartbeatOptions?: HeartbeatOptions,
|
|
107
134
|
): ExtensionWallet {
|
|
108
|
-
const wallet = new ExtensionWallet(chainInfo, appId, extensionId, port, sharedKey);
|
|
135
|
+
const wallet = new ExtensionWallet(chainInfo, appId, extensionId, port, sharedKey, logger, heartbeatOptions);
|
|
109
136
|
|
|
110
137
|
// Set up message handler for encrypted responses and unencrypted control messages
|
|
111
138
|
wallet.port.onmessage = (event: MessageEvent) => {
|
|
112
139
|
const data = event.data;
|
|
113
|
-
//
|
|
140
|
+
// Any inbound traffic counts as proof of liveness.
|
|
141
|
+
wallet.lastInboundAt = Date.now();
|
|
142
|
+
|
|
114
143
|
if (data && typeof data === 'object' && 'type' in data && data.type === WalletMessageType.DISCONNECT) {
|
|
115
144
|
wallet.handleDisconnect();
|
|
116
145
|
return;
|
|
@@ -131,7 +160,7 @@ export class ExtensionWallet {
|
|
|
131
160
|
type: prop.toString() as keyof FunctionsOf<Wallet>,
|
|
132
161
|
args,
|
|
133
162
|
});
|
|
134
|
-
return WalletSchema[prop.toString() as keyof typeof WalletSchema]
|
|
163
|
+
return getSchemaReturnType(WalletSchema[prop.toString() as keyof typeof WalletSchema]).parseAsync(result);
|
|
135
164
|
};
|
|
136
165
|
} else {
|
|
137
166
|
return target[prop as keyof ExtensionWallet];
|
|
@@ -184,8 +213,10 @@ export class ExtensionWallet {
|
|
|
184
213
|
resolve(result);
|
|
185
214
|
}
|
|
186
215
|
this.inFlight.delete(messageId);
|
|
187
|
-
|
|
188
|
-
} catch {
|
|
216
|
+
this.maybeStopHeartbeat();
|
|
217
|
+
} catch (err) {
|
|
218
|
+
this.log.warn('Failed to decrypt wallet response', { err });
|
|
219
|
+
}
|
|
189
220
|
}
|
|
190
221
|
|
|
191
222
|
/**
|
|
@@ -223,9 +254,59 @@ export class ExtensionWallet {
|
|
|
223
254
|
|
|
224
255
|
const { promise, resolve, reject } = promiseWithResolvers<unknown>();
|
|
225
256
|
this.inFlight.set(messageId, { promise, resolve, reject });
|
|
257
|
+
this.startHeartbeat();
|
|
226
258
|
return promise;
|
|
227
259
|
}
|
|
228
260
|
|
|
261
|
+
/**
|
|
262
|
+
* Start the heartbeat probe loop while at least one request is in flight.
|
|
263
|
+
* Idempotent — calling while already running is a no-op.
|
|
264
|
+
*
|
|
265
|
+
* Heartbeat is opt-in via wire protocol: PINGs are unencrypted control messages
|
|
266
|
+
* (like DISCONNECT). Older wallets that do not understand PING simply drop it,
|
|
267
|
+
* which is safe — we only declare disconnect when **no** inbound traffic of any
|
|
268
|
+
* kind (PONG, encrypted response, DISCONNECT) arrives within the dead window.
|
|
269
|
+
* A wallet that is processing a slow request will reset the timer when it
|
|
270
|
+
* eventually responds, so this never causes false disconnects on legacy peers.
|
|
271
|
+
*/
|
|
272
|
+
private startHeartbeat(): void {
|
|
273
|
+
if (this.heartbeatTimer !== null || this.disconnected) {
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
this.lastInboundAt = Date.now();
|
|
277
|
+
this.heartbeatTimer = setInterval(() => this.heartbeatTick(), this.heartbeatIntervalMs);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
private maybeStopHeartbeat(): void {
|
|
281
|
+
if (this.inFlight.size === 0 && this.heartbeatTimer !== null) {
|
|
282
|
+
clearInterval(this.heartbeatTimer);
|
|
283
|
+
this.heartbeatTimer = null;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
private heartbeatTick(): void {
|
|
288
|
+
if (this.disconnected || this.inFlight.size === 0) {
|
|
289
|
+
this.maybeStopHeartbeat();
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
const idleMs = Date.now() - this.lastInboundAt;
|
|
294
|
+
if (idleMs >= this.heartbeatDeadAfterMs) {
|
|
295
|
+
this.log.warn('Wallet channel unresponsive — declaring disconnect', {
|
|
296
|
+
idleMs,
|
|
297
|
+
inFlight: this.inFlight.size,
|
|
298
|
+
});
|
|
299
|
+
this.handleDisconnect();
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
try {
|
|
304
|
+
this.port.postMessage({ type: WalletMessageType.PING });
|
|
305
|
+
} catch (err) {
|
|
306
|
+
this.log.warn('Failed to send heartbeat PING', { err });
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
|
|
229
310
|
/**
|
|
230
311
|
* Handles wallet disconnection.
|
|
231
312
|
* Rejects all pending requests and notifies registered callbacks.
|
|
@@ -237,6 +318,11 @@ export class ExtensionWallet {
|
|
|
237
318
|
}
|
|
238
319
|
this.disconnected = true;
|
|
239
320
|
|
|
321
|
+
if (this.heartbeatTimer !== null) {
|
|
322
|
+
clearInterval(this.heartbeatTimer);
|
|
323
|
+
this.heartbeatTimer = null;
|
|
324
|
+
}
|
|
325
|
+
|
|
240
326
|
if (this.port) {
|
|
241
327
|
this.port.onmessage = null;
|
|
242
328
|
this.port.close();
|
|
@@ -14,11 +14,10 @@
|
|
|
14
14
|
* so the dApp knows it can send a discovery request.
|
|
15
15
|
*/
|
|
16
16
|
import type { ChainInfo } from '@aztec/aztec.js/account';
|
|
17
|
-
import { createLogger } from '@aztec/aztec.js/log';
|
|
18
17
|
import type { Wallet } from '@aztec/aztec.js/wallet';
|
|
19
18
|
import { WalletSchema } from '@aztec/aztec.js/wallet';
|
|
20
19
|
import { jsonStringify } from '@aztec/foundation/json-rpc';
|
|
21
|
-
import { parseWithOptionals, schemaHasMethod } from '@aztec/foundation/schemas';
|
|
20
|
+
import { getSchemaParameters, parseWithOptionals, schemaHasMethod } from '@aztec/foundation/schemas';
|
|
22
21
|
|
|
23
22
|
import {
|
|
24
23
|
type EncryptedPayload,
|
|
@@ -29,7 +28,7 @@ import {
|
|
|
29
28
|
generateKeyPair,
|
|
30
29
|
importPublicKey,
|
|
31
30
|
} from '../../crypto.js';
|
|
32
|
-
import { type WalletMessage, WalletMessageType, type WalletResponse } from '../../types.js';
|
|
31
|
+
import { type WalletMessage, WalletMessageType, type WalletResponse, type WalletSdkLogger } from '../../types.js';
|
|
33
32
|
|
|
34
33
|
/**
|
|
35
34
|
* A pending discovery request from a dApp (before user approval).
|
|
@@ -75,6 +74,8 @@ export interface IframeConnectionConfig {
|
|
|
75
74
|
walletIcon?: string;
|
|
76
75
|
/** Origins allowed to connect. If empty or undefined, all origins are allowed (dev mode). */
|
|
77
76
|
allowedOrigins?: string[];
|
|
77
|
+
/** Logger used for diagnostics. */
|
|
78
|
+
logger: WalletSdkLogger;
|
|
78
79
|
}
|
|
79
80
|
|
|
80
81
|
/**
|
|
@@ -105,7 +106,7 @@ export interface IframeConnectionCallbacks {
|
|
|
105
106
|
* @example
|
|
106
107
|
* ```typescript
|
|
107
108
|
* const handler = new IframeConnectionHandler(
|
|
108
|
-
* { walletId: 'my-wallet', walletName: 'My Wallet', walletVersion: '1.0.0' },
|
|
109
|
+
* { walletId: 'my-wallet', walletName: 'My Wallet', walletVersion: '1.0.0', logger: console },
|
|
109
110
|
* {
|
|
110
111
|
* onPendingDiscovery: (session) => showApprovalUI(session),
|
|
111
112
|
* getWallet: (appId, chainInfo) => createWalletForApp(appId, chainInfo),
|
|
@@ -117,12 +118,14 @@ export interface IframeConnectionCallbacks {
|
|
|
117
118
|
export class IframeConnectionHandler {
|
|
118
119
|
private pendingSessions = new Map<string, PendingSession>();
|
|
119
120
|
private activeSessions = new Map<string, ActiveSession>();
|
|
120
|
-
private log
|
|
121
|
+
private log: WalletSdkLogger;
|
|
121
122
|
|
|
122
123
|
constructor(
|
|
123
124
|
private config: IframeConnectionConfig,
|
|
124
125
|
private callbacks: IframeConnectionCallbacks,
|
|
125
|
-
) {
|
|
126
|
+
) {
|
|
127
|
+
this.log = config.logger;
|
|
128
|
+
}
|
|
126
129
|
|
|
127
130
|
start(): void {
|
|
128
131
|
window.addEventListener('message', this.handleMessage);
|
|
@@ -203,7 +206,18 @@ export class IframeConnectionHandler {
|
|
|
203
206
|
case WalletMessageType.DISCONNECT:
|
|
204
207
|
this.terminateSession(msg.sessionId);
|
|
205
208
|
break;
|
|
209
|
+
case WalletMessageType.PING:
|
|
210
|
+
this.handlePing(msg.sessionId);
|
|
211
|
+
break;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
private handlePing(sessionId: string): void {
|
|
216
|
+
const session = this.activeSessions.get(sessionId);
|
|
217
|
+
if (!session) {
|
|
218
|
+
return;
|
|
206
219
|
}
|
|
220
|
+
this.postToOrigin(session.origin, { type: WalletMessageType.PONG, sessionId });
|
|
207
221
|
}
|
|
208
222
|
|
|
209
223
|
private handleDiscoveryRequest(msg: Record<string, unknown>, origin: string): void {
|
|
@@ -287,8 +301,7 @@ export class IframeConnectionHandler {
|
|
|
287
301
|
if (!schemaHasMethod(WalletSchema, type)) {
|
|
288
302
|
throw new Error(`Unknown wallet method: ${type}`);
|
|
289
303
|
}
|
|
290
|
-
|
|
291
|
-
const sanitizedArgs = await parseWithOptionals(args, WalletSchema[type].parameters() as any);
|
|
304
|
+
const sanitizedArgs = await parseWithOptionals(args, getSchemaParameters(WalletSchema[type]));
|
|
292
305
|
result = await (wallet as Record<string, (...a: unknown[]) => Promise<unknown>>)[type](...sanitizedArgs);
|
|
293
306
|
} catch (err: unknown) {
|
|
294
307
|
error = err instanceof Error ? err.message : String(err);
|
|
@@ -10,11 +10,21 @@ import type { ChainInfo } from '@aztec/aztec.js/account';
|
|
|
10
10
|
import { type Wallet, WalletSchema } from '@aztec/aztec.js/wallet';
|
|
11
11
|
import { jsonStringify } from '@aztec/foundation/json-rpc';
|
|
12
12
|
import { type PromiseWithResolvers, promiseWithResolvers } from '@aztec/foundation/promise';
|
|
13
|
-
import { schemaHasMethod } from '@aztec/foundation/schemas';
|
|
13
|
+
import { getSchemaReturnType, schemaHasMethod } from '@aztec/foundation/schemas';
|
|
14
14
|
import type { FunctionsOf } from '@aztec/foundation/types';
|
|
15
15
|
|
|
16
16
|
import { type EncryptedPayload, decrypt, encrypt } from '../../crypto.js';
|
|
17
|
-
import {
|
|
17
|
+
import {
|
|
18
|
+
DEFAULT_HEARTBEAT_DEAD_AFTER_MS,
|
|
19
|
+
DEFAULT_HEARTBEAT_INTERVAL_MS,
|
|
20
|
+
type DisconnectCallback,
|
|
21
|
+
type HeartbeatOptions,
|
|
22
|
+
NOOP_LOGGER,
|
|
23
|
+
type WalletMessage,
|
|
24
|
+
WalletMessageType,
|
|
25
|
+
type WalletResponse,
|
|
26
|
+
type WalletSdkLogger,
|
|
27
|
+
} from '../../types.js';
|
|
18
28
|
|
|
19
29
|
/**
|
|
20
30
|
* Internal type representing a wallet method call before encryption.
|
|
@@ -46,6 +56,11 @@ export class IframeWallet {
|
|
|
46
56
|
private disconnected = false;
|
|
47
57
|
private disconnectCallbacks: DisconnectCallback[] = [];
|
|
48
58
|
private messageListener: ((e: MessageEvent) => void) | null = null;
|
|
59
|
+
private heartbeatTimer: ReturnType<typeof setInterval> | null = null;
|
|
60
|
+
private lastInboundAt = 0;
|
|
61
|
+
private log: WalletSdkLogger;
|
|
62
|
+
private heartbeatIntervalMs: number;
|
|
63
|
+
private heartbeatDeadAfterMs: number;
|
|
49
64
|
|
|
50
65
|
private constructor(
|
|
51
66
|
private chainInfo: ChainInfo,
|
|
@@ -55,7 +70,13 @@ export class IframeWallet {
|
|
|
55
70
|
private iframeWindow: Window,
|
|
56
71
|
private walletOrigin: string,
|
|
57
72
|
private sharedKey: CryptoKey,
|
|
58
|
-
|
|
73
|
+
logger?: WalletSdkLogger,
|
|
74
|
+
heartbeatOptions?: HeartbeatOptions,
|
|
75
|
+
) {
|
|
76
|
+
this.log = logger ?? NOOP_LOGGER;
|
|
77
|
+
this.heartbeatIntervalMs = heartbeatOptions?.intervalMs ?? DEFAULT_HEARTBEAT_INTERVAL_MS;
|
|
78
|
+
this.heartbeatDeadAfterMs = heartbeatOptions?.deadAfterMs ?? DEFAULT_HEARTBEAT_DEAD_AFTER_MS;
|
|
79
|
+
}
|
|
59
80
|
|
|
60
81
|
/**
|
|
61
82
|
* Creates a proxied IframeWallet that implements the {@link Wallet} interface.
|
|
@@ -71,6 +92,8 @@ export class IframeWallet {
|
|
|
71
92
|
* @param sharedKey - AES-256-GCM key derived from ECDH key exchange
|
|
72
93
|
* @param chainInfo - Network information (chainId and version)
|
|
73
94
|
* @param appId - Application identifier for the requesting dApp
|
|
95
|
+
* @param logger - Optional logger; defaults to a no-op logger to keep extension/page bundles small
|
|
96
|
+
* @param heartbeatOptions - Optional override for heartbeat tuning (mostly useful for tests)
|
|
74
97
|
* @returns A proxied IframeWallet — call `.asWallet()` to get the typed `Wallet`
|
|
75
98
|
*/
|
|
76
99
|
static create(
|
|
@@ -81,8 +104,20 @@ export class IframeWallet {
|
|
|
81
104
|
sharedKey: CryptoKey,
|
|
82
105
|
chainInfo: ChainInfo,
|
|
83
106
|
appId: string,
|
|
107
|
+
logger?: WalletSdkLogger,
|
|
108
|
+
heartbeatOptions?: HeartbeatOptions,
|
|
84
109
|
): IframeWallet {
|
|
85
|
-
const wallet = new IframeWallet(
|
|
110
|
+
const wallet = new IframeWallet(
|
|
111
|
+
chainInfo,
|
|
112
|
+
appId,
|
|
113
|
+
walletId,
|
|
114
|
+
sessionId,
|
|
115
|
+
iframeWindow,
|
|
116
|
+
walletOrigin,
|
|
117
|
+
sharedKey,
|
|
118
|
+
logger,
|
|
119
|
+
heartbeatOptions,
|
|
120
|
+
);
|
|
86
121
|
|
|
87
122
|
wallet.messageListener = (event: MessageEvent) => {
|
|
88
123
|
if (event.origin !== walletOrigin) {
|
|
@@ -93,9 +128,16 @@ export class IframeWallet {
|
|
|
93
128
|
return;
|
|
94
129
|
}
|
|
95
130
|
|
|
96
|
-
if (msg.
|
|
131
|
+
if (msg.sessionId !== sessionId) {
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// Any inbound traffic on our session counts as proof of liveness.
|
|
136
|
+
wallet.lastInboundAt = Date.now();
|
|
137
|
+
|
|
138
|
+
if (msg.type === WalletMessageType.SECURE_RESPONSE) {
|
|
97
139
|
void wallet.handleEncryptedResponse(msg.encrypted as EncryptedPayload);
|
|
98
|
-
} else if (msg.type === WalletMessageType.SESSION_DISCONNECTED
|
|
140
|
+
} else if (msg.type === WalletMessageType.SESSION_DISCONNECTED) {
|
|
99
141
|
wallet.handleDisconnect();
|
|
100
142
|
}
|
|
101
143
|
};
|
|
@@ -111,7 +153,7 @@ export class IframeWallet {
|
|
|
111
153
|
type: prop.toString() as keyof FunctionsOf<Wallet>,
|
|
112
154
|
args,
|
|
113
155
|
});
|
|
114
|
-
return WalletSchema[prop.toString() as keyof typeof WalletSchema]
|
|
156
|
+
return getSchemaReturnType(WalletSchema[prop.toString() as keyof typeof WalletSchema]).parseAsync(result);
|
|
115
157
|
};
|
|
116
158
|
} else {
|
|
117
159
|
return target[prop as keyof IframeWallet];
|
|
@@ -148,8 +190,9 @@ export class IframeWallet {
|
|
|
148
190
|
pending.resolve(result);
|
|
149
191
|
}
|
|
150
192
|
this.inFlight.delete(messageId);
|
|
151
|
-
|
|
152
|
-
|
|
193
|
+
this.maybeStopHeartbeat();
|
|
194
|
+
} catch (err) {
|
|
195
|
+
this.log.warn('Failed to decrypt wallet response', { err });
|
|
153
196
|
}
|
|
154
197
|
}
|
|
155
198
|
|
|
@@ -176,15 +219,66 @@ export class IframeWallet {
|
|
|
176
219
|
|
|
177
220
|
const { promise, resolve, reject } = promiseWithResolvers<unknown>();
|
|
178
221
|
this.inFlight.set(messageId, { promise, resolve, reject });
|
|
222
|
+
this.startHeartbeat();
|
|
179
223
|
return promise;
|
|
180
224
|
}
|
|
181
225
|
|
|
226
|
+
/**
|
|
227
|
+
* Start liveness probing while at least one request is in flight. PINGs are
|
|
228
|
+
* unencrypted control messages — older wallet handlers that don't understand
|
|
229
|
+
* them simply drop them, but any inbound traffic (PONG, encrypted response,
|
|
230
|
+
* disconnect notice) resets the idle timer, so a slow-but-alive legacy wallet
|
|
231
|
+
* never trips a false disconnect.
|
|
232
|
+
*/
|
|
233
|
+
private startHeartbeat(): void {
|
|
234
|
+
if (this.heartbeatTimer !== null || this.disconnected) {
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
this.lastInboundAt = Date.now();
|
|
238
|
+
this.heartbeatTimer = setInterval(() => this.heartbeatTick(), this.heartbeatIntervalMs);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
private maybeStopHeartbeat(): void {
|
|
242
|
+
if (this.inFlight.size === 0 && this.heartbeatTimer !== null) {
|
|
243
|
+
clearInterval(this.heartbeatTimer);
|
|
244
|
+
this.heartbeatTimer = null;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
private heartbeatTick(): void {
|
|
249
|
+
if (this.disconnected || this.inFlight.size === 0) {
|
|
250
|
+
this.maybeStopHeartbeat();
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
const idleMs = Date.now() - this.lastInboundAt;
|
|
255
|
+
if (idleMs >= this.heartbeatDeadAfterMs) {
|
|
256
|
+
this.log.warn('Iframe wallet channel unresponsive — declaring disconnect', {
|
|
257
|
+
idleMs,
|
|
258
|
+
inFlight: this.inFlight.size,
|
|
259
|
+
});
|
|
260
|
+
this.handleDisconnect();
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
try {
|
|
265
|
+
this.iframeWindow.postMessage({ type: WalletMessageType.PING, sessionId: this.sessionId }, this.walletOrigin);
|
|
266
|
+
} catch (err) {
|
|
267
|
+
this.log.warn('Failed to send heartbeat PING', { err });
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
182
271
|
private handleDisconnect(): void {
|
|
183
272
|
if (this.disconnected) {
|
|
184
273
|
return;
|
|
185
274
|
}
|
|
186
275
|
this.disconnected = true;
|
|
187
276
|
|
|
277
|
+
if (this.heartbeatTimer !== null) {
|
|
278
|
+
clearInterval(this.heartbeatTimer);
|
|
279
|
+
this.heartbeatTimer = null;
|
|
280
|
+
}
|
|
281
|
+
|
|
188
282
|
if (this.messageListener) {
|
|
189
283
|
window.removeEventListener('message', this.messageListener);
|
|
190
284
|
this.messageListener = null;
|
package/src/types.ts
CHANGED
|
@@ -25,6 +25,13 @@ export enum WalletMessageType {
|
|
|
25
25
|
SECURE_RESPONSE = 'aztec-wallet-secure-response',
|
|
26
26
|
/** Session disconnected notification */
|
|
27
27
|
SESSION_DISCONNECTED = 'aztec-wallet-session-disconnected',
|
|
28
|
+
/** Liveness probe sent by the dApp while a request is in flight */
|
|
29
|
+
PING = 'aztec-wallet-ping',
|
|
30
|
+
/** Liveness response from the wallet. Any inbound message (including a regular
|
|
31
|
+
* encrypted response) is treated as proof of liveness, so a missing PONG alone
|
|
32
|
+
* never trips disconnect — the dApp also resets its liveness timer on traffic.
|
|
33
|
+
*/
|
|
34
|
+
PONG = 'aztec-wallet-pong',
|
|
28
35
|
}
|
|
29
36
|
|
|
30
37
|
/**
|
|
@@ -143,3 +150,55 @@ export interface KeyExchangeResponse {
|
|
|
143
150
|
* Callback invoked when a wallet connection is disconnected.
|
|
144
151
|
*/
|
|
145
152
|
export type DisconnectCallback = () => void;
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Default heartbeat tuning shared by both transports.
|
|
156
|
+
*
|
|
157
|
+
* - `intervalMs`: how often the dApp sends PING probes while a request is in flight.
|
|
158
|
+
* - `deadAfterMs`: how long the channel can stay silent (no PONG, no encrypted
|
|
159
|
+
* response, no DISCONNECT) before the dApp declares the wallet unreachable
|
|
160
|
+
* and rejects all in-flight requests. Generous enough that long-running
|
|
161
|
+
* operations (proveTx, sendTx) on legacy wallets that don't reply to PING
|
|
162
|
+
* still succeed — any inbound traffic resets the timer.
|
|
163
|
+
*/
|
|
164
|
+
export const DEFAULT_HEARTBEAT_INTERVAL_MS = 5_000;
|
|
165
|
+
export const DEFAULT_HEARTBEAT_DEAD_AFTER_MS = 300_000;
|
|
166
|
+
|
|
167
|
+
/** Override knobs for the heartbeat — mostly useful for tests. */
|
|
168
|
+
export interface HeartbeatOptions {
|
|
169
|
+
/** How often to send PING probes while a request is in flight (ms). */
|
|
170
|
+
intervalMs?: number;
|
|
171
|
+
/** Idle ceiling before declaring disconnect (ms). */
|
|
172
|
+
deadAfterMs?: number;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Minimal logger surface used by the wallet SDK.
|
|
177
|
+
*
|
|
178
|
+
* Defined locally so that wallet hosts (browser extensions, iframe wallet pages)
|
|
179
|
+
* can pass a simple `console`-backed logger without pulling in the full
|
|
180
|
+
* `@aztec/foundation` logging runtime, which is non-trivial to bundle in those
|
|
181
|
+
* contexts. Structurally compatible with `Logger` from `@aztec/foundation/log`,
|
|
182
|
+
* so dApp-side callers can pass that type directly.
|
|
183
|
+
*/
|
|
184
|
+
export interface WalletSdkLogger {
|
|
185
|
+
/** Diagnostic messages — typically discarded in production. */
|
|
186
|
+
debug: (message: string, data?: unknown) => void;
|
|
187
|
+
/** Informational messages — significant lifecycle events. */
|
|
188
|
+
info: (message: string, data?: unknown) => void;
|
|
189
|
+
/** Recoverable problems — channel decryption failure, missed heartbeats, etc. */
|
|
190
|
+
warn: (message: string, data?: unknown) => void;
|
|
191
|
+
/** Errors that prevent normal operation. */
|
|
192
|
+
error: (message: string, errOrData?: unknown, data?: unknown) => void;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* No-op logger used as the default when callers don't provide one. Discards all
|
|
197
|
+
* messages — wallet hosts that want diagnostics should pass their own logger.
|
|
198
|
+
*/
|
|
199
|
+
export const NOOP_LOGGER: WalletSdkLogger = {
|
|
200
|
+
debug: () => {},
|
|
201
|
+
info: () => {},
|
|
202
|
+
warn: () => {},
|
|
203
|
+
error: () => {},
|
|
204
|
+
};
|