keymaster-connect 0.1.0 → 0.1.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/README.md +6 -4
- package/dist/{chunk-AAVOIS5Q.js → chunk-W2TNWBLP.js} +2 -8
- package/dist/chunk-W2TNWBLP.js.map +1 -0
- package/dist/index.d.ts +22 -49
- package/dist/index.js +120 -110
- package/dist/index.js.map +1 -1
- package/dist/protocol.d.ts +20 -45
- package/dist/protocol.js +1 -1
- package/dist/server.d.ts +7 -4
- package/dist/server.js +16 -13
- package/dist/server.js.map +1 -1
- package/package.json +1 -1
- package/dist/chunk-AAVOIS5Q.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { PROTOCOL_VERSION, sameNetworkIdentity, validateNetworkIdentity } from './chunk-
|
|
2
|
-
export { MSFILE_BLOCK_SIZE_BYTES, MSFILE_DIGEST_SIZE_BYTES, MSFILE_LOCAL_SOURCE_ID, MSFILE_MAX_BLOCK_BYTES, MSFILE_MAX_CONTENT_BYTES, MSFILE_MAX_ERROR_MESSAGE_BYTES, MSFILE_MAX_HEADER_BYTES, MSFILE_MAX_SEED_BYTES, MSFILE_PROTOCOL_ID, PROTOCOL_METHODS, PROTOCOL_VERSION, STORAGE_DEFAULT_LIST_LIMIT, STORAGE_MAX_LIST_LIMIT, STORAGE_MAX_PAYLOAD_BYTES, isValidMsFileSourceId, sameNetworkIdentity, validateBsvNetwork, validateNetworkIdentity } from './chunk-
|
|
1
|
+
import { PROTOCOL_METHODS, PROTOCOL_VERSION, sameNetworkIdentity, validateNetworkIdentity } from './chunk-W2TNWBLP.js';
|
|
2
|
+
export { MSFILE_BLOCK_SIZE_BYTES, MSFILE_DIGEST_SIZE_BYTES, MSFILE_LOCAL_SOURCE_ID, MSFILE_MAX_BLOCK_BYTES, MSFILE_MAX_CONTENT_BYTES, MSFILE_MAX_ERROR_MESSAGE_BYTES, MSFILE_MAX_HEADER_BYTES, MSFILE_MAX_SEED_BYTES, MSFILE_PROTOCOL_ID, PROTOCOL_METHODS, PROTOCOL_VERSION, STORAGE_DEFAULT_LIST_LIMIT, STORAGE_MAX_LIST_LIMIT, STORAGE_MAX_PAYLOAD_BYTES, isValidMsFileSourceId, sameNetworkIdentity, validateBsvNetwork, validateNetworkIdentity } from './chunk-W2TNWBLP.js';
|
|
3
3
|
|
|
4
4
|
// src/binary.ts
|
|
5
5
|
function binary(bytes, mime) {
|
|
@@ -42,9 +42,7 @@ var KeymasterProtocolError = class extends Error {
|
|
|
42
42
|
}
|
|
43
43
|
code;
|
|
44
44
|
};
|
|
45
|
-
var
|
|
46
|
-
var DEFAULT_POPUP_NAME = "keymaster-connect-v1";
|
|
47
|
-
var DEFAULT_READY_TIMEOUT_MS = 3e4;
|
|
45
|
+
var DEFAULT_READY_TIMEOUT_MS = 12e4;
|
|
48
46
|
var DEFAULT_REQUEST_TIMEOUT_MS = 12e4;
|
|
49
47
|
var DEFAULT_CLOSE_POLL_MS = 500;
|
|
50
48
|
var KeymasterConnectClient = class {
|
|
@@ -55,29 +53,27 @@ var KeymasterConnectClient = class {
|
|
|
55
53
|
return this.identityValue;
|
|
56
54
|
}
|
|
57
55
|
targetOrigin;
|
|
58
|
-
mode;
|
|
59
56
|
environment;
|
|
60
57
|
options;
|
|
61
58
|
pending = /* @__PURE__ */ new Map();
|
|
62
59
|
stateValue = "idle";
|
|
63
60
|
sessionWindow = null;
|
|
64
|
-
ownsSessionWindow = false;
|
|
65
61
|
listenerInstalled = false;
|
|
66
62
|
readyPromise = null;
|
|
67
63
|
readyResolve = null;
|
|
68
64
|
readyReject = null;
|
|
69
65
|
readyTimer = null;
|
|
70
66
|
closePoller = null;
|
|
67
|
+
authorizationId = null;
|
|
68
|
+
authorizedIdentity = null;
|
|
69
|
+
recoveryId = null;
|
|
70
|
+
recoveryWaiters = /* @__PURE__ */ new Set();
|
|
71
71
|
constructor(options) {
|
|
72
72
|
if ("networkIdentity" in options) throw new TypeError("networkIdentity option has been removed; read identity from the connection result");
|
|
73
73
|
this.targetOrigin = normalizeHttpOrigin(options.targetOrigin);
|
|
74
|
-
|
|
74
|
+
if (options.mode !== void 0 && options.mode !== "appView") throw new TypeError("Connect apps must be launched inside the wallet");
|
|
75
75
|
this.options = {
|
|
76
76
|
...options,
|
|
77
|
-
popupPath: options.popupPath ?? DEFAULT_POPUP_PATH,
|
|
78
|
-
popupName: options.popupName ?? DEFAULT_POPUP_NAME,
|
|
79
|
-
popupWidth: positiveNumber(options.popupWidth ?? 520, "popupWidth"),
|
|
80
|
-
popupHeight: positiveNumber(options.popupHeight ?? 760, "popupHeight"),
|
|
81
77
|
readyTimeoutMs: positiveNumber(options.readyTimeoutMs ?? DEFAULT_READY_TIMEOUT_MS, "readyTimeoutMs"),
|
|
82
78
|
requestTimeoutMs: positiveNumber(options.requestTimeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS, "requestTimeoutMs"),
|
|
83
79
|
closePollMs: positiveNumber(options.closePollMs ?? DEFAULT_CLOSE_POLL_MS, "closePollMs")
|
|
@@ -95,14 +91,12 @@ var KeymasterConnectClient = class {
|
|
|
95
91
|
/**
|
|
96
92
|
* Establishes the browser transport.
|
|
97
93
|
*
|
|
98
|
-
*
|
|
99
|
-
* adopts `window.opener`, installs the listener, and sends the child `ready`
|
|
100
|
-
* message without ever opening a fallback popup.
|
|
94
|
+
* 绑定浏览器真实父页面,精确校验来源;独立页面直接失败,不打开钱包窗口。
|
|
101
95
|
*
|
|
102
96
|
* @group Transport
|
|
103
97
|
*/
|
|
104
98
|
connect() {
|
|
105
|
-
return this.
|
|
99
|
+
return this.connectAppView();
|
|
106
100
|
}
|
|
107
101
|
/**
|
|
108
102
|
* Sends any public Connect method with compile-time parameter and result
|
|
@@ -112,11 +106,14 @@ var KeymasterConnectClient = class {
|
|
|
112
106
|
*/
|
|
113
107
|
async request(method, params, options = {}) {
|
|
114
108
|
if (options.signal?.aborted) throw abortReason(options.signal);
|
|
109
|
+
if (!PROTOCOL_METHODS.includes(method)) throw new TypeError("Unknown Connect method");
|
|
115
110
|
if (!params || typeof params !== "object" || Array.isArray(params)) throw new TypeError("params must be an object");
|
|
116
111
|
for (const field of ["walletPublicKeyHex", "networkIdentity", "bsvNetwork", "assetId"]) {
|
|
117
112
|
if (field in params) throw new TypeError(`Removed wallet selector: ${field}`);
|
|
118
113
|
}
|
|
114
|
+
if (method === "connect.resume" && this.authorizationId) return this.continueConnection(options);
|
|
119
115
|
const establishing = ["connect.login", "connect.resume", "connect.launch"].includes(method);
|
|
116
|
+
if (!establishing && this.stateValue === "paused") throw new KeymasterTransportError("wallet_paused", "Wallet connection is paused; old business requests are never replayed");
|
|
120
117
|
if (establishing && this.pending.size) throw new TypeError("Cannot establish a connection while requests are pending");
|
|
121
118
|
if (!establishing && !this.bindingId) throw new KeymasterProtocolError("invalid_request", "Login, resume or launch before making business requests");
|
|
122
119
|
if ([...this.pending.values()].some((request) => ["connect.login", "connect.resume", "connect.launch"].includes(request.method))) throw new TypeError("Connection establishment is already pending");
|
|
@@ -126,7 +123,7 @@ var KeymasterConnectClient = class {
|
|
|
126
123
|
if (options.signal?.aborted) throw abortReason(options.signal);
|
|
127
124
|
const target = this.sessionWindow;
|
|
128
125
|
if (!target || isWindowClosed(target)) {
|
|
129
|
-
throw new KeymasterTransportError("
|
|
126
|
+
throw new KeymasterTransportError("wallet_closed", "Keymaster parent wallet is closed");
|
|
130
127
|
}
|
|
131
128
|
const requestId = options.requestId ?? this.environment.randomUUID();
|
|
132
129
|
if (!requestId || this.pending.has(requestId)) {
|
|
@@ -201,9 +198,6 @@ var KeymasterConnectClient = class {
|
|
|
201
198
|
* @group Connect
|
|
202
199
|
*/
|
|
203
200
|
launch(params, options) {
|
|
204
|
-
if (this.mode !== "appView") {
|
|
205
|
-
return Promise.reject(new TypeError('connect.launch requires mode: "appView"'));
|
|
206
|
-
}
|
|
207
201
|
return this.request("connect.launch", params, options);
|
|
208
202
|
}
|
|
209
203
|
/**
|
|
@@ -281,33 +275,6 @@ var KeymasterConnectClient = class {
|
|
|
281
275
|
channelSubscriptionSet(params, options) {
|
|
282
276
|
return this.request("channel.subscription_set", params, options);
|
|
283
277
|
}
|
|
284
|
-
/**
|
|
285
|
-
* 一次读取 Keymaster 当前的 BSV 展示价格(金额 + 单位)。
|
|
286
|
-
*
|
|
287
|
-
* 未就绪(未配置 / 未收到快照 / 订阅出错)时返回 `amount: "0.00"`;
|
|
288
|
-
* 价格只作显示参考,不用于业务判断。
|
|
289
|
-
*
|
|
290
|
-
* @group Price
|
|
291
|
-
*/
|
|
292
|
-
priceGet(params, options) {
|
|
293
|
-
return this.request("price.get", params, options);
|
|
294
|
-
}
|
|
295
|
-
/**
|
|
296
|
-
* 订阅 `price.changed` 推送;订阅前不会收到价格事件。
|
|
297
|
-
*
|
|
298
|
-
* @group Price
|
|
299
|
-
*/
|
|
300
|
-
priceSubscribe(params, options) {
|
|
301
|
-
return this.request("price.subscribe", params, options);
|
|
302
|
-
}
|
|
303
|
-
/**
|
|
304
|
-
* 取消当前会话的 `price.changed` 推送。
|
|
305
|
-
*
|
|
306
|
-
* @group Price
|
|
307
|
-
*/
|
|
308
|
-
priceUnsubscribe(params, options) {
|
|
309
|
-
return this.request("price.unsubscribe", params, options);
|
|
310
|
-
}
|
|
311
278
|
/**
|
|
312
279
|
* Calls `storage.list` to list paths in the verified application's namespace.
|
|
313
280
|
*
|
|
@@ -406,6 +373,7 @@ var KeymasterConnectClient = class {
|
|
|
406
373
|
* @group Transport
|
|
407
374
|
*/
|
|
408
375
|
close() {
|
|
376
|
+
for (const id of this.pending.keys()) this.postCancel(id);
|
|
409
377
|
this.identityValue = null;
|
|
410
378
|
this.bindingId = null;
|
|
411
379
|
const error = new KeymasterTransportError("client_closed", "Keymaster client was closed");
|
|
@@ -416,61 +384,51 @@ var KeymasterConnectClient = class {
|
|
|
416
384
|
this.environment.removeMessageListener(this.handleMessage);
|
|
417
385
|
this.listenerInstalled = false;
|
|
418
386
|
}
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
this.
|
|
427
|
-
this.setState("disconnected");
|
|
428
|
-
}
|
|
429
|
-
connectPopup() {
|
|
430
|
-
if (this.isConnected()) return Promise.resolve();
|
|
431
|
-
if (this.readyPromise) return this.readyPromise;
|
|
432
|
-
const url = new URL(this.options.popupPath, `${this.targetOrigin}/`);
|
|
433
|
-
if (url.origin !== this.targetOrigin) {
|
|
434
|
-
return Promise.reject(new TypeError("popupPath must resolve inside targetOrigin"));
|
|
435
|
-
}
|
|
387
|
+
if (!this.authorizationId) this.sessionWindow = null;
|
|
388
|
+
this.recoveryId = null;
|
|
389
|
+
this.finishRecovery(void 0, error);
|
|
390
|
+
this.setState(this.authorizationId ? "paused" : "disconnected");
|
|
391
|
+
}
|
|
392
|
+
/** 恢复原父钱包连接;任何情况下都不另开窗口或重放旧业务。 */
|
|
393
|
+
continueConnection(options) {
|
|
394
|
+
if (this.pending.size) return Promise.reject(new TypeError("Cannot resume while business requests are pending"));
|
|
436
395
|
this.installListener();
|
|
437
|
-
this.
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
popupFeatures(this.options.popupWidth, this.options.popupHeight)
|
|
442
|
-
);
|
|
443
|
-
if (!this.sessionWindow) {
|
|
444
|
-
this.setState("disconnected");
|
|
445
|
-
return Promise.reject(new KeymasterTransportError("popup_blocked", "Keymaster Session Window was blocked"));
|
|
446
|
-
}
|
|
447
|
-
this.ownsSessionWindow = true;
|
|
396
|
+
const parent = this.environment.parent();
|
|
397
|
+
if (!parent || isWindowClosed(parent)) return Promise.reject(new KeymasterTransportError("appview_session_lost", "No wallet parent is available; relaunch from the wallet"));
|
|
398
|
+
this.sessionWindow = parent;
|
|
399
|
+
this.pause(new KeymasterTransportError("wallet_paused", "Re-establishing the original wallet connection"));
|
|
448
400
|
this.startClosePolling();
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
this.
|
|
454
|
-
|
|
401
|
+
return new Promise((resolve, reject) => {
|
|
402
|
+
const finish = (error, result) => {
|
|
403
|
+
this.environment.clearTimeout(timer);
|
|
404
|
+
options.signal?.removeEventListener("abort", aborted);
|
|
405
|
+
this.recoveryWaiters.delete(waiter);
|
|
406
|
+
if (error) reject(error);
|
|
407
|
+
else resolve(result);
|
|
408
|
+
};
|
|
409
|
+
const waiter = { resolve: (result) => finish(void 0, result), reject: (error) => finish(error) };
|
|
410
|
+
const aborted = () => finish(abortReason(options.signal));
|
|
411
|
+
const timer = this.environment.setTimeout(() => finish(new KeymasterTransportError("request_timeout", "Timed out restoring wallet connection")), positiveNumber(options.timeoutMs ?? this.options.readyTimeoutMs, "timeoutMs"));
|
|
412
|
+
this.recoveryWaiters.add(waiter);
|
|
413
|
+
options.signal?.addEventListener("abort", aborted, { once: true });
|
|
414
|
+
this.sendRecovery();
|
|
455
415
|
});
|
|
456
|
-
return this.readyPromise;
|
|
457
416
|
}
|
|
458
417
|
connectAppView() {
|
|
459
418
|
if (this.isConnected()) return Promise.resolve();
|
|
460
|
-
const
|
|
461
|
-
if (!
|
|
462
|
-
const code = this.stateValue === "idle" ? "
|
|
419
|
+
const parent = this.environment.parent();
|
|
420
|
+
if (!parent || isWindowClosed(parent)) {
|
|
421
|
+
const code = this.stateValue === "idle" ? "not_embedded" : "appview_session_lost";
|
|
463
422
|
this.setState("disconnected");
|
|
464
423
|
return Promise.reject(new KeymasterTransportError(
|
|
465
424
|
code,
|
|
466
|
-
"No live
|
|
425
|
+
"No live wallet parent is available; launch the app from Keymaster"
|
|
467
426
|
));
|
|
468
427
|
}
|
|
469
428
|
this.installListener();
|
|
470
|
-
this.sessionWindow =
|
|
471
|
-
this.ownsSessionWindow = false;
|
|
429
|
+
this.sessionWindow = parent;
|
|
472
430
|
try {
|
|
473
|
-
|
|
431
|
+
parent.postMessage({ v: PROTOCOL_VERSION, type: "ready" }, this.targetOrigin);
|
|
474
432
|
} catch (cause) {
|
|
475
433
|
this.sessionWindow = null;
|
|
476
434
|
this.setState("disconnected");
|
|
@@ -488,15 +446,22 @@ var KeymasterConnectClient = class {
|
|
|
488
446
|
if (value.type === "ready" || value.type === "result") this.disconnect(new KeymasterTransportError("unsupported_protocol", "Incompatible Keymaster protocol version"));
|
|
489
447
|
return;
|
|
490
448
|
}
|
|
491
|
-
if (value.type === "ready"
|
|
492
|
-
|
|
449
|
+
if (value.type === "ready") return;
|
|
450
|
+
if (value.type === "closing") {
|
|
451
|
+
if (this.authorizationId) this.pause(new KeymasterTransportError("wallet_paused", "Wallet runtime is unavailable"));
|
|
452
|
+
else this.disconnect(new KeymasterTransportError("wallet_closed", "Keymaster wallet connection is closing"));
|
|
493
453
|
return;
|
|
494
454
|
}
|
|
495
|
-
if (value.type === "
|
|
496
|
-
this.
|
|
455
|
+
if (value.type === "revoked" && value.authorizationId === this.authorizationId) {
|
|
456
|
+
this.authorizationId = null;
|
|
457
|
+
this.authorizedIdentity = null;
|
|
458
|
+
this.recoveryId = null;
|
|
459
|
+
this.finishRecovery(void 0, new KeymasterProtocolError("connection_expired", "Application connection authorization was revoked"));
|
|
460
|
+
this.disconnect(new KeymasterProtocolError("connection_expired", "Application connection authorization was revoked"));
|
|
497
461
|
return;
|
|
498
462
|
}
|
|
499
463
|
if (value.type === "event") {
|
|
464
|
+
if (value.event !== "channel.message_received") return;
|
|
500
465
|
try {
|
|
501
466
|
if (!this.identityValue || value.bindingId !== this.bindingId || !sameNetworkIdentity(this.identityValue, validateNetworkIdentity(value.networkIdentity))) return;
|
|
502
467
|
} catch {
|
|
@@ -510,6 +475,27 @@ var KeymasterConnectClient = class {
|
|
|
510
475
|
}
|
|
511
476
|
if (value.type !== "result" || typeof value.id !== "string" || typeof value.ok !== "boolean") return;
|
|
512
477
|
const result = event.data;
|
|
478
|
+
if (this.authorizationId && result.id === this.recoveryId) {
|
|
479
|
+
if (!result.ok) {
|
|
480
|
+
this.pause(new KeymasterProtocolError(result.error.code, result.error.message));
|
|
481
|
+
return;
|
|
482
|
+
}
|
|
483
|
+
try {
|
|
484
|
+
const identity = validateNetworkIdentity(value.networkIdentity);
|
|
485
|
+
const restored = result.result;
|
|
486
|
+
if (!this.authorizedIdentity || !sameNetworkIdentity(identity, this.authorizedIdentity) || !sameNetworkIdentity(identity, validateNetworkIdentity(restored.networkIdentity)) || restored.authorizationId !== this.authorizationId || typeof value.bindingId !== "string" || !value.bindingId) throw new Error("Recovery must retain the original wallet and authorization");
|
|
487
|
+
if (this.bindingId !== value.bindingId) this.pause(new KeymasterTransportError("wallet_paused", "Wallet runtime was replaced"));
|
|
488
|
+
this.identityValue = Object.freeze({ ...identity });
|
|
489
|
+
this.bindingId = value.bindingId;
|
|
490
|
+
this.recoveryId = null;
|
|
491
|
+
this.setState("connected");
|
|
492
|
+
this.finishRecovery(restored);
|
|
493
|
+
} catch (error) {
|
|
494
|
+
this.pause(error);
|
|
495
|
+
this.finishRecovery(void 0, error);
|
|
496
|
+
}
|
|
497
|
+
return;
|
|
498
|
+
}
|
|
513
499
|
const pending = this.pending.get(result.id);
|
|
514
500
|
if (!pending) return;
|
|
515
501
|
this.deletePending(result.id);
|
|
@@ -519,8 +505,11 @@ var KeymasterConnectClient = class {
|
|
|
519
505
|
return;
|
|
520
506
|
}
|
|
521
507
|
if (result.error.code === "connection_expired") {
|
|
522
|
-
this.
|
|
523
|
-
|
|
508
|
+
if (this.authorizationId) this.pause(new KeymasterProtocolError(result.error.code, result.error.message));
|
|
509
|
+
else {
|
|
510
|
+
this.identityValue = null;
|
|
511
|
+
this.bindingId = null;
|
|
512
|
+
}
|
|
524
513
|
}
|
|
525
514
|
pending.reject(new KeymasterProtocolError(result.error.code, result.error.message));
|
|
526
515
|
return;
|
|
@@ -529,8 +518,11 @@ var KeymasterConnectClient = class {
|
|
|
529
518
|
const identity = validateNetworkIdentity(value.networkIdentity);
|
|
530
519
|
if (["connect.login", "connect.resume", "connect.launch"].includes(pending.method)) {
|
|
531
520
|
const connected = result.result;
|
|
532
|
-
if (!sameNetworkIdentity(identity, validateNetworkIdentity(connected.networkIdentity)) || typeof value.bindingId !== "string" || !value.bindingId) throw new Error("Invalid connection identity or binding");
|
|
521
|
+
if (!sameNetworkIdentity(identity, validateNetworkIdentity(connected.networkIdentity)) || typeof value.bindingId !== "string" || !value.bindingId || typeof connected.authorizationId !== "string" || !connected.authorizationId) throw new Error("Invalid connection identity or binding");
|
|
533
522
|
this.identityValue = Object.freeze({ ...identity });
|
|
523
|
+
this.authorizedIdentity = this.identityValue;
|
|
524
|
+
this.authorizationId = connected.authorizationId;
|
|
525
|
+
this.recoveryId = null;
|
|
534
526
|
this.bindingId = value.bindingId;
|
|
535
527
|
} else if (!this.identityValue || value.bindingId !== this.bindingId || !sameNetworkIdentity(this.identityValue, identity)) {
|
|
536
528
|
throw new Error("Keymaster connection identity mismatch");
|
|
@@ -538,6 +530,9 @@ var KeymasterConnectClient = class {
|
|
|
538
530
|
if (pending.method === "connect.logout") {
|
|
539
531
|
this.identityValue = null;
|
|
540
532
|
this.bindingId = null;
|
|
533
|
+
this.authorizationId = null;
|
|
534
|
+
this.authorizedIdentity = null;
|
|
535
|
+
this.recoveryId = null;
|
|
541
536
|
}
|
|
542
537
|
pending.resolve(result.result);
|
|
543
538
|
} catch (error) {
|
|
@@ -571,27 +566,46 @@ var KeymasterConnectClient = class {
|
|
|
571
566
|
this.readyReject = null;
|
|
572
567
|
}
|
|
573
568
|
disconnect(reason) {
|
|
569
|
+
for (const id of this.pending.keys()) this.postCancel(id);
|
|
574
570
|
this.identityValue = null;
|
|
575
571
|
this.bindingId = null;
|
|
576
572
|
this.rejectAll(reason);
|
|
577
573
|
this.failReady(reason);
|
|
578
574
|
this.stopClosePolling();
|
|
579
|
-
try {
|
|
580
|
-
if (this.ownsSessionWindow && this.sessionWindow && !isWindowClosed(this.sessionWindow)) {
|
|
581
|
-
this.sessionWindow.close();
|
|
582
|
-
}
|
|
583
|
-
} catch {
|
|
584
|
-
}
|
|
585
575
|
this.sessionWindow = null;
|
|
586
|
-
this.ownsSessionWindow = false;
|
|
587
576
|
this.setState("disconnected");
|
|
588
577
|
}
|
|
578
|
+
/** 暂停仅撤销瞬时绑定和旧请求,持久授权引用及原身份留作恢复校验。 */
|
|
579
|
+
pause(reason) {
|
|
580
|
+
this.identityValue = null;
|
|
581
|
+
this.bindingId = null;
|
|
582
|
+
this.rejectAll(reason);
|
|
583
|
+
this.failReady(reason);
|
|
584
|
+
this.setState("paused");
|
|
585
|
+
}
|
|
586
|
+
sendRecovery() {
|
|
587
|
+
if (!this.authorizationId || !this.sessionWindow || isWindowClosed(this.sessionWindow)) return;
|
|
588
|
+
this.recoveryId ??= this.environment.randomUUID();
|
|
589
|
+
try {
|
|
590
|
+
this.sessionWindow.postMessage({ v: PROTOCOL_VERSION, type: "reconnect", id: this.recoveryId, authorizationId: this.authorizationId, ...this.bindingId ? { bindingId: this.bindingId } : {} }, this.targetOrigin);
|
|
591
|
+
} catch (error) {
|
|
592
|
+
this.pause(error);
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
finishRecovery(result, error) {
|
|
596
|
+
for (const waiter of [...this.recoveryWaiters]) {
|
|
597
|
+
if (error) waiter.reject(error);
|
|
598
|
+
else if (result) waiter.resolve(result);
|
|
599
|
+
}
|
|
600
|
+
}
|
|
589
601
|
startClosePolling() {
|
|
590
602
|
this.stopClosePolling();
|
|
591
603
|
this.closePoller = this.environment.setInterval(() => {
|
|
592
604
|
if (this.sessionWindow && isWindowClosed(this.sessionWindow)) {
|
|
593
|
-
this.
|
|
594
|
-
|
|
605
|
+
if (this.authorizationId) {
|
|
606
|
+
if (this.stateValue !== "paused") this.pause(new KeymasterTransportError("wallet_paused", "Wallet window is closed"));
|
|
607
|
+
} else this.disconnect(new KeymasterTransportError("wallet_closed", "Keymaster parent wallet was closed"));
|
|
608
|
+
} else this.sendRecovery();
|
|
595
609
|
}, this.options.closePollMs);
|
|
596
610
|
}
|
|
597
611
|
stopClosePolling() {
|
|
@@ -636,8 +650,7 @@ function createBrowserEnvironment() {
|
|
|
636
650
|
throw new TypeError("keymaster-connect requires a browser Window or an explicit environment");
|
|
637
651
|
}
|
|
638
652
|
return {
|
|
639
|
-
|
|
640
|
-
opener: () => window.opener,
|
|
653
|
+
parent: () => window.parent !== window ? window.parent : null,
|
|
641
654
|
addMessageListener: (listener) => window.addEventListener("message", listener),
|
|
642
655
|
removeMessageListener: (listener) => window.removeEventListener("message", listener),
|
|
643
656
|
setTimeout: (handler, timeoutMs) => window.setTimeout(handler, timeoutMs),
|
|
@@ -654,9 +667,6 @@ function normalizeHttpOrigin(input) {
|
|
|
654
667
|
}
|
|
655
668
|
return url.origin;
|
|
656
669
|
}
|
|
657
|
-
function popupFeatures(width, height) {
|
|
658
|
-
return `popup=yes,width=${Math.max(320, Math.trunc(width))},height=${Math.max(320, Math.trunc(height))}`;
|
|
659
|
-
}
|
|
660
670
|
function positiveNumber(value, name) {
|
|
661
671
|
if (!Number.isFinite(value) || value <= 0) throw new TypeError(`${name} must be a positive number`);
|
|
662
672
|
return value;
|