keymaster-connect 0.1.4 → 0.1.5
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 +22 -2
- package/dist/{chunk-2EYARFS3.js → chunk-ZDIFZJYD.js} +4 -4
- package/dist/chunk-ZDIFZJYD.js.map +1 -0
- package/dist/index.d.ts +109 -13
- package/dist/index.js +298 -26
- package/dist/index.js.map +1 -1
- package/dist/{protocol-NcRDmEKZ.d.ts → protocol-C8z30CKK.d.ts} +18 -64
- package/dist/protocol.d.ts +1 -1
- package/dist/protocol.js +1 -1
- package/dist/script.d.ts +58 -0
- package/dist/script.js +253 -0
- package/dist/script.js.map +1 -0
- package/dist/server.d.ts +3 -3
- package/dist/server.js +41 -73
- package/dist/server.js.map +1 -1
- package/package.json +3 -2
- package/dist/chunk-2EYARFS3.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { parseToolDefinitions, assertToolPayload, isAiToolMessage, validateToolArguments } from './chunk-VVOTW5ZT.js';
|
|
2
2
|
export { AI_TOOLS_MAX_JSON_CHARS, assertToolJson, assertToolPayload, isAiToolMessage, parseToolDefinitions, validateToolArguments } from './chunk-VVOTW5ZT.js';
|
|
3
|
-
import { PROTOCOL_METHODS, PROTOCOL_VERSION, sameNetworkIdentity, validateNetworkIdentity } from './chunk-
|
|
4
|
-
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-
|
|
3
|
+
import { PROTOCOL_METHODS, PROTOCOL_VERSION, sameNetworkIdentity, validateNetworkIdentity } from './chunk-ZDIFZJYD.js';
|
|
4
|
+
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-ZDIFZJYD.js';
|
|
5
5
|
|
|
6
6
|
// src/binary.ts
|
|
7
7
|
function binary(bytes, mime) {
|
|
@@ -143,6 +143,119 @@ var AiToolClient = class {
|
|
|
143
143
|
}
|
|
144
144
|
};
|
|
145
145
|
|
|
146
|
+
// src/recovery.ts
|
|
147
|
+
var RECOVERY_STORAGE_KEY = "keymaster-connect:recovery:v1";
|
|
148
|
+
function normalizeExactOrigin(input) {
|
|
149
|
+
try {
|
|
150
|
+
const url = new URL(input);
|
|
151
|
+
if (url.protocol !== "http:" && url.protocol !== "https:") return null;
|
|
152
|
+
if (url.origin !== input) return null;
|
|
153
|
+
return url.origin;
|
|
154
|
+
} catch {
|
|
155
|
+
return null;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
function isMode(value) {
|
|
159
|
+
return value === "iframe" || value === "appView" || value === "popup";
|
|
160
|
+
}
|
|
161
|
+
function parseRecord(raw) {
|
|
162
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw)) return null;
|
|
163
|
+
const value = raw;
|
|
164
|
+
if (value.version !== 1) return null;
|
|
165
|
+
if (typeof value.targetOrigin !== "string" || normalizeExactOrigin(value.targetOrigin) === null) return null;
|
|
166
|
+
if (!isMode(value.mode)) return null;
|
|
167
|
+
if (typeof value.authorizationId !== "string" || !value.authorizationId || value.authorizationId.length > 128) return null;
|
|
168
|
+
try {
|
|
169
|
+
const identity = validateNetworkIdentity(value.networkIdentity);
|
|
170
|
+
return {
|
|
171
|
+
version: 1,
|
|
172
|
+
targetOrigin: value.targetOrigin,
|
|
173
|
+
mode: value.mode,
|
|
174
|
+
authorizationId: value.authorizationId,
|
|
175
|
+
networkIdentity: { ...identity }
|
|
176
|
+
};
|
|
177
|
+
} catch {
|
|
178
|
+
return null;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
function resolveRecoveryStorage(explicit) {
|
|
182
|
+
if (explicit) return explicit;
|
|
183
|
+
try {
|
|
184
|
+
if (typeof sessionStorage === "undefined") return null;
|
|
185
|
+
sessionStorage.getItem(RECOVERY_STORAGE_KEY);
|
|
186
|
+
return sessionStorage;
|
|
187
|
+
} catch {
|
|
188
|
+
return null;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
function isRecoveryStorageAvailable(storage) {
|
|
192
|
+
return resolveRecoveryStorage(storage) !== null;
|
|
193
|
+
}
|
|
194
|
+
function readStoredRecord(storage) {
|
|
195
|
+
if (!storage) return null;
|
|
196
|
+
try {
|
|
197
|
+
const raw = storage.getItem(RECOVERY_STORAGE_KEY);
|
|
198
|
+
if (!raw) return null;
|
|
199
|
+
return parseRecord(JSON.parse(raw));
|
|
200
|
+
} catch {
|
|
201
|
+
return null;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
function readConnectionRecovery(init) {
|
|
205
|
+
const storage = resolveRecoveryStorage(init?.recoveryStorage);
|
|
206
|
+
const record = readStoredRecord(storage);
|
|
207
|
+
if (!record) return null;
|
|
208
|
+
if (init?.targetOrigin !== void 0) {
|
|
209
|
+
const expected = normalizeExactOrigin(init.targetOrigin);
|
|
210
|
+
if (!expected || record.targetOrigin !== expected) return null;
|
|
211
|
+
}
|
|
212
|
+
if (init?.mode !== void 0 && record.mode !== init.mode) return null;
|
|
213
|
+
return {
|
|
214
|
+
version: 1,
|
|
215
|
+
targetOrigin: record.targetOrigin,
|
|
216
|
+
mode: record.mode,
|
|
217
|
+
networkIdentity: { ...record.networkIdentity }
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
function loadConnectionRecoveryRecord(init) {
|
|
221
|
+
const expectedOrigin = normalizeExactOrigin(init.targetOrigin);
|
|
222
|
+
if (!expectedOrigin || !isMode(init.mode)) return null;
|
|
223
|
+
const storage = resolveRecoveryStorage(init.recoveryStorage);
|
|
224
|
+
const record = readStoredRecord(storage);
|
|
225
|
+
if (!record) return null;
|
|
226
|
+
if (record.targetOrigin !== expectedOrigin || record.mode !== init.mode) return null;
|
|
227
|
+
return record;
|
|
228
|
+
}
|
|
229
|
+
function saveConnectionRecoveryRecord(storage, record) {
|
|
230
|
+
if (!storage) throw new Error("Recovery storage is unavailable");
|
|
231
|
+
const parsed = parseRecord(record);
|
|
232
|
+
if (!parsed) throw new TypeError("Invalid recovery record");
|
|
233
|
+
storage.setItem(RECOVERY_STORAGE_KEY, JSON.stringify(parsed));
|
|
234
|
+
}
|
|
235
|
+
function clearConnectionRecovery(init) {
|
|
236
|
+
const storage = resolveRecoveryStorage(init?.recoveryStorage);
|
|
237
|
+
if (!storage) return false;
|
|
238
|
+
try {
|
|
239
|
+
const current = readStoredRecord(storage);
|
|
240
|
+
if (!current) {
|
|
241
|
+
try {
|
|
242
|
+
storage.removeItem(RECOVERY_STORAGE_KEY);
|
|
243
|
+
} catch {
|
|
244
|
+
}
|
|
245
|
+
return false;
|
|
246
|
+
}
|
|
247
|
+
if (init?.targetOrigin !== void 0) {
|
|
248
|
+
const expected = normalizeExactOrigin(init.targetOrigin);
|
|
249
|
+
if (!expected || current.targetOrigin !== expected) return false;
|
|
250
|
+
}
|
|
251
|
+
if (init?.authorizationId !== void 0 && current.authorizationId !== init.authorizationId) return false;
|
|
252
|
+
storage.removeItem(RECOVERY_STORAGE_KEY);
|
|
253
|
+
return true;
|
|
254
|
+
} catch {
|
|
255
|
+
return false;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
146
259
|
// src/client.ts
|
|
147
260
|
var KeymasterTransportError = class extends Error {
|
|
148
261
|
constructor(code, message) {
|
|
@@ -182,6 +295,14 @@ var KeymasterConnectClient = class {
|
|
|
182
295
|
get networkIdentity() {
|
|
183
296
|
return this.identityValue;
|
|
184
297
|
}
|
|
298
|
+
/** 最近一次持久化失败的原因;成功后清零。登录成功不受写入失败影响。 */
|
|
299
|
+
get recoverySaveError() {
|
|
300
|
+
return this.recoverySaveErrorValue;
|
|
301
|
+
}
|
|
302
|
+
/** 刷新恢复存储是否可用;不可用时仍可登录,但刷新后需重新登录。 */
|
|
303
|
+
get recoveryStorageAvailable() {
|
|
304
|
+
return this.recoveryStorage !== null;
|
|
305
|
+
}
|
|
185
306
|
targetOrigin;
|
|
186
307
|
mode;
|
|
187
308
|
environment;
|
|
@@ -198,6 +319,17 @@ var KeymasterConnectClient = class {
|
|
|
198
319
|
authorizationId = null;
|
|
199
320
|
authorizedIdentity = null;
|
|
200
321
|
recoveryId = null;
|
|
322
|
+
recoveryStorage;
|
|
323
|
+
recoverySaveErrorValue = null;
|
|
324
|
+
/** 显式恢复期间暂停后台心跳,保证本次尝试使用同一份 proof;结束(成功/失败/取消/超时)后恢复。 */
|
|
325
|
+
explicitRecoveryActive = false;
|
|
326
|
+
explicitRecoveryProof = void 0;
|
|
327
|
+
/** 内存授权是否已验证:login/launch/恢复成功置 true;仅从存储装载的待验证凭据为 false。 */
|
|
328
|
+
authorizationVerified = false;
|
|
329
|
+
/** 当前传输窗口是否已可信就绪(收到过匹配来源与窗口的 ready);新开窗口后重置,存在≠就绪。 */
|
|
330
|
+
sessionWindowReady = false;
|
|
331
|
+
/** 新 popup 首发前等待可信窗口 ready 的一次性继续函数;超时/结束时清空。 */
|
|
332
|
+
recoveryReadyContinue = null;
|
|
201
333
|
recoveryWaiters = /* @__PURE__ */ new Set();
|
|
202
334
|
constructor(options) {
|
|
203
335
|
if ("networkIdentity" in options) throw new TypeError("networkIdentity option has been removed; read identity from the connection result");
|
|
@@ -212,6 +344,7 @@ var KeymasterConnectClient = class {
|
|
|
212
344
|
closePollMs: positiveNumber(options.closePollMs ?? DEFAULT_CLOSE_POLL_MS, "closePollMs")
|
|
213
345
|
};
|
|
214
346
|
this.environment = options.environment ?? createBrowserEnvironment();
|
|
347
|
+
this.recoveryStorage = resolveRecoveryStorage(options.recoveryStorage);
|
|
215
348
|
}
|
|
216
349
|
/**
|
|
217
350
|
* Current browser transport state.
|
|
@@ -224,11 +357,15 @@ var KeymasterConnectClient = class {
|
|
|
224
357
|
/**
|
|
225
358
|
* Establishes the browser transport.
|
|
226
359
|
*
|
|
227
|
-
* iframe / appView 绑定真实 parent / opener;
|
|
360
|
+
* 只建立传输,不恢复授权:iframe / appView 绑定真实 parent / opener;
|
|
361
|
+
* popup 打开完整钱包等待 ready。刷新后的授权恢复请使用 `resume()`,
|
|
362
|
+
* 它会先装载本地恢复记录再复用 `continueConnection()`;`connect()` 本身
|
|
363
|
+
* 不读取恢复记录、不自动恢复。
|
|
228
364
|
*
|
|
229
365
|
* @group Transport
|
|
230
366
|
*/
|
|
231
367
|
connect() {
|
|
368
|
+
if (this.explicitRecoveryActive) return Promise.reject(new TypeError("Connection recovery is already in progress"));
|
|
232
369
|
return this.mode === "popup" ? this.connectPopup() : this.connectAppView();
|
|
233
370
|
}
|
|
234
371
|
/**
|
|
@@ -244,15 +381,20 @@ var KeymasterConnectClient = class {
|
|
|
244
381
|
for (const field of ["walletPublicKeyHex", "networkIdentity", "bsvNetwork", "assetId"]) {
|
|
245
382
|
if (field in params) throw new TypeError(`Removed wallet selector: ${field}`);
|
|
246
383
|
}
|
|
247
|
-
if (method === "connect.resume" && this.authorizationId)
|
|
384
|
+
if (method === "connect.resume" && this.authorizationId) {
|
|
385
|
+
const resumeProof = params.appIdentity;
|
|
386
|
+
return this.continueConnection(options, resumeProof);
|
|
387
|
+
}
|
|
248
388
|
const establishing = ["connect.login", "connect.resume", "connect.launch"].includes(method);
|
|
249
389
|
if (!establishing && this.stateValue === "paused") throw new KeymasterTransportError("wallet_paused", "Wallet connection is paused; old business requests are never replayed");
|
|
250
390
|
if (establishing && this.pending.size) throw new TypeError("Cannot establish a connection while requests are pending");
|
|
391
|
+
if (establishing && this.explicitRecoveryActive) throw new TypeError("Connection recovery is already in progress");
|
|
251
392
|
if (!establishing && !this.bindingId) throw new KeymasterProtocolError("invalid_request", "Login, resume or launch before making business requests");
|
|
252
393
|
if ([...this.pending.values()].some((request) => ["connect.login", "connect.resume", "connect.launch"].includes(request.method))) throw new TypeError("Connection establishment is already pending");
|
|
253
394
|
await this.connect();
|
|
254
395
|
if (establishing && this.pending.size) throw new TypeError("Cannot establish a connection while requests are pending");
|
|
255
396
|
if ([...this.pending.values()].some((request) => ["connect.login", "connect.resume", "connect.launch"].includes(request.method))) throw new TypeError("Connection establishment is already pending");
|
|
397
|
+
if (establishing && this.explicitRecoveryActive) throw new TypeError("Connection recovery is already in progress");
|
|
256
398
|
if (options.signal?.aborted) throw abortReason(options.signal);
|
|
257
399
|
const target = this.sessionWindow;
|
|
258
400
|
if (!target || isWindowClosed(target)) {
|
|
@@ -310,10 +452,32 @@ var KeymasterConnectClient = class {
|
|
|
310
452
|
* Calls `connect.resume` to restore the unlock runtime for a persistent
|
|
311
453
|
* session.
|
|
312
454
|
*
|
|
455
|
+
* 刷新恢复时只把保存的凭据和预期身份载入待恢复上下文,不直接置为已登录;
|
|
456
|
+
* 钱包验证成功、结果身份一致并取得新 binding 后才开放业务调用。
|
|
457
|
+
* 无可用凭据时直接拒绝并提示重新登录,不回退到无凭据的重建默认授权。
|
|
458
|
+
* 恢复请求携带调用方当前入口的 `appIdentity`(如有),不静默丢弃。
|
|
459
|
+
*
|
|
313
460
|
* @group Connect
|
|
314
461
|
*/
|
|
315
462
|
resume(params = {}, options) {
|
|
316
|
-
return
|
|
463
|
+
if (options?.signal?.aborted) return Promise.reject(abortReason(options.signal));
|
|
464
|
+
if (this.explicitRecoveryActive) return Promise.reject(new TypeError("Connection recovery is already in progress"));
|
|
465
|
+
if (this.authorizationId && this.authorizationVerified) {
|
|
466
|
+
return this.request("connect.resume", params, options);
|
|
467
|
+
}
|
|
468
|
+
const stored = loadConnectionRecoveryRecord({
|
|
469
|
+
recoveryStorage: this.recoveryStorage ?? void 0,
|
|
470
|
+
targetOrigin: this.targetOrigin,
|
|
471
|
+
mode: this.mode
|
|
472
|
+
});
|
|
473
|
+
if (stored) {
|
|
474
|
+
this.authorizationId = stored.authorizationId;
|
|
475
|
+
this.authorizedIdentity = Object.freeze({ ...stored.networkIdentity });
|
|
476
|
+
this.authorizationVerified = false;
|
|
477
|
+
} else if (!this.authorizationId) {
|
|
478
|
+
return Promise.reject(new KeymasterTransportError("client_closed", "No saved Keymaster recovery record for this origin and mode; login again"));
|
|
479
|
+
}
|
|
480
|
+
return this.continueConnection(options ?? {}, params.appIdentity);
|
|
317
481
|
}
|
|
318
482
|
/**
|
|
319
483
|
* Calls `connect.logout` to revoke a persistent Connect session.
|
|
@@ -381,17 +545,6 @@ var KeymasterConnectClient = class {
|
|
|
381
545
|
paymentStatus(params, options) {
|
|
382
546
|
return this.request("payment.status", params, options);
|
|
383
547
|
}
|
|
384
|
-
/**
|
|
385
|
-
* @deprecated Connect v1 接收端拒绝旧转账方法,使用 paymentPay。
|
|
386
|
-
* @group Transfer
|
|
387
|
-
*/
|
|
388
|
-
p2pkhTransfer(params, options) {
|
|
389
|
-
return this.request("p2pkh.transfer", params, options);
|
|
390
|
-
}
|
|
391
|
-
/** 注册零额度渠道;用户在钱包设置额度后,付款携带返回的 channelId。 */
|
|
392
|
-
registerPaymentChannel(params, options) {
|
|
393
|
-
return this.request("payment.channel.register", params, options);
|
|
394
|
-
}
|
|
395
548
|
/**
|
|
396
549
|
* 发布到任意精确频道;owner、公钥签名和消息编号由 Keymaster 生成。
|
|
397
550
|
*
|
|
@@ -448,6 +601,22 @@ var KeymasterConnectClient = class {
|
|
|
448
601
|
storageGet(params, options) {
|
|
449
602
|
return this.request("storage.get", params, options);
|
|
450
603
|
}
|
|
604
|
+
/**
|
|
605
|
+
* 可选加密写入:向钱包提交明文,钱包以当前钱包密钥和 App 公钥隔离后加密保存。
|
|
606
|
+
* 路径、条件创建语义与 storagePut 相同,size 返回明文字节数;不会返回加密密钥。
|
|
607
|
+
* @group Storage
|
|
608
|
+
*/
|
|
609
|
+
storagePutEncrypted(params, options) {
|
|
610
|
+
return this.request("storage.encrypted.put", params, options);
|
|
611
|
+
}
|
|
612
|
+
/**
|
|
613
|
+
* 可选加密读取:仅同一钱包能解密;offset/length 作用于明文,ifMatch 仍比较远端版本。
|
|
614
|
+
* 明文对象、错误格式、篡改或密钥不匹配均失败,不自动回退为普通读取。
|
|
615
|
+
* @group Storage
|
|
616
|
+
*/
|
|
617
|
+
storageGetEncrypted(params, options) {
|
|
618
|
+
return this.request("storage.encrypted.get", params, options);
|
|
619
|
+
}
|
|
451
620
|
/**
|
|
452
621
|
* Calls `storage.delete` to delete an app-scoped object.
|
|
453
622
|
*
|
|
@@ -503,6 +672,9 @@ var KeymasterConnectClient = class {
|
|
|
503
672
|
* Tears down the local transport. This does not revoke persistent Connect
|
|
504
673
|
* sessions; call {@link logout} before closing when revocation is required.
|
|
505
674
|
*
|
|
675
|
+
* 只结束运行连接,保留刷新恢复材料:`close()`、页面卸载和运行失效都不删除
|
|
676
|
+
* 本地恢复记录;`logout` 成功、明确授权失效或用户主动清除才删除。
|
|
677
|
+
*
|
|
506
678
|
* @group Transport
|
|
507
679
|
*/
|
|
508
680
|
close() {
|
|
@@ -526,25 +698,48 @@ var KeymasterConnectClient = class {
|
|
|
526
698
|
}
|
|
527
699
|
if (!this.authorizationId) this.sessionWindow = null;
|
|
528
700
|
this.recoveryId = null;
|
|
701
|
+
this.explicitRecoveryActive = false;
|
|
702
|
+
this.explicitRecoveryProof = void 0;
|
|
703
|
+
this.recoveryReadyContinue = null;
|
|
529
704
|
this.finishRecovery(void 0, error);
|
|
530
705
|
this.setState(this.authorizationId ? "paused" : "disconnected");
|
|
531
706
|
}
|
|
532
707
|
/** 主动恢复可由 popup 重新打开钱包;后台恢复从不打开窗口或重放业务。 */
|
|
533
|
-
continueConnection(options) {
|
|
708
|
+
continueConnection(options, appIdentity) {
|
|
709
|
+
if (options.signal?.aborted) return Promise.reject(abortReason(options.signal));
|
|
710
|
+
if (this.explicitRecoveryActive) return Promise.reject(new TypeError("Connection recovery is already in progress"));
|
|
534
711
|
if (this.pending.size) return Promise.reject(new TypeError("Cannot resume while business requests are pending"));
|
|
535
712
|
this.installListener();
|
|
713
|
+
let awaitReady = false;
|
|
536
714
|
if (this.mode === "popup") {
|
|
537
715
|
if (!this.sessionWindow || isWindowClosed(this.sessionWindow)) {
|
|
538
716
|
const url = new URL(this.options.popupPath, `${this.targetOrigin}/`);
|
|
539
717
|
if (url.origin !== this.targetOrigin) return Promise.reject(new TypeError("popupPath must resolve inside targetOrigin"));
|
|
540
718
|
this.sessionWindow = this.environment.open(url.href, "_blank", "");
|
|
541
719
|
if (!this.sessionWindow) return Promise.reject(new KeymasterTransportError("popup_blocked", "Keymaster wallet window was blocked"));
|
|
720
|
+
this.sessionWindowReady = false;
|
|
721
|
+
awaitReady = true;
|
|
722
|
+
} else if (!this.sessionWindowReady) {
|
|
723
|
+
awaitReady = true;
|
|
542
724
|
}
|
|
543
725
|
} else {
|
|
544
726
|
const launcher = this.mode === "iframe" ? this.environment.parent() : this.environment.opener();
|
|
545
727
|
if (!launcher || isWindowClosed(launcher)) return Promise.reject(new KeymasterTransportError("appview_session_lost", "Wallet launcher unavailable; relaunch from the wallet"));
|
|
546
728
|
this.sessionWindow = launcher;
|
|
547
729
|
}
|
|
730
|
+
const wasVerified = this.authorizationVerified;
|
|
731
|
+
this.explicitRecoveryActive = true;
|
|
732
|
+
this.explicitRecoveryProof = appIdentity;
|
|
733
|
+
const endExplicitAttempt = (failed) => {
|
|
734
|
+
this.explicitRecoveryActive = false;
|
|
735
|
+
this.explicitRecoveryProof = void 0;
|
|
736
|
+
this.recoveryReadyContinue = null;
|
|
737
|
+
if (failed && !wasVerified && this.authorizationId) {
|
|
738
|
+
this.authorizationId = null;
|
|
739
|
+
this.authorizedIdentity = null;
|
|
740
|
+
this.recoveryId = null;
|
|
741
|
+
}
|
|
742
|
+
};
|
|
548
743
|
this.pause(new KeymasterTransportError("wallet_paused", "Re-establishing the original wallet connection"));
|
|
549
744
|
this.startClosePolling();
|
|
550
745
|
return new Promise((resolve, reject) => {
|
|
@@ -552,6 +747,7 @@ var KeymasterConnectClient = class {
|
|
|
552
747
|
this.environment.clearTimeout(timer);
|
|
553
748
|
options.signal?.removeEventListener("abort", aborted);
|
|
554
749
|
this.recoveryWaiters.delete(waiter);
|
|
750
|
+
endExplicitAttempt(!!error);
|
|
555
751
|
if (error) reject(error);
|
|
556
752
|
else resolve(result);
|
|
557
753
|
};
|
|
@@ -560,7 +756,11 @@ var KeymasterConnectClient = class {
|
|
|
560
756
|
const timer = this.environment.setTimeout(() => finish(new KeymasterTransportError("request_timeout", "Timed out restoring wallet connection")), positiveNumber(options.timeoutMs ?? this.options.readyTimeoutMs, "timeoutMs"));
|
|
561
757
|
this.recoveryWaiters.add(waiter);
|
|
562
758
|
options.signal?.addEventListener("abort", aborted, { once: true });
|
|
563
|
-
|
|
759
|
+
if (awaitReady) {
|
|
760
|
+
this.recoveryReadyContinue = () => this.sendRecovery(this.explicitRecoveryProof);
|
|
761
|
+
} else {
|
|
762
|
+
this.sendRecovery(this.explicitRecoveryProof);
|
|
763
|
+
}
|
|
564
764
|
});
|
|
565
765
|
}
|
|
566
766
|
connectPopup() {
|
|
@@ -575,6 +775,7 @@ var KeymasterConnectClient = class {
|
|
|
575
775
|
this.setState("disconnected");
|
|
576
776
|
return Promise.reject(new KeymasterTransportError("popup_blocked", "Keymaster wallet window was blocked"));
|
|
577
777
|
}
|
|
778
|
+
this.sessionWindowReady = false;
|
|
578
779
|
this.startClosePolling();
|
|
579
780
|
this.readyPromise = new Promise((resolve, reject) => {
|
|
580
781
|
this.readyResolve = resolve;
|
|
@@ -617,7 +818,13 @@ var KeymasterConnectClient = class {
|
|
|
617
818
|
}
|
|
618
819
|
if (this.aiTools.handle(value)) return;
|
|
619
820
|
if (value.type === "ready") {
|
|
821
|
+
this.sessionWindowReady = true;
|
|
620
822
|
if (this.mode === "popup" && this.readyPromise) this.finishReady();
|
|
823
|
+
if (this.explicitRecoveryActive && this.recoveryReadyContinue) {
|
|
824
|
+
const send = this.recoveryReadyContinue;
|
|
825
|
+
this.recoveryReadyContinue = null;
|
|
826
|
+
send();
|
|
827
|
+
}
|
|
621
828
|
return;
|
|
622
829
|
}
|
|
623
830
|
if (value.type === "closing") {
|
|
@@ -626,11 +833,15 @@ var KeymasterConnectClient = class {
|
|
|
626
833
|
return;
|
|
627
834
|
}
|
|
628
835
|
if (value.type === "revoked" && value.authorizationId === this.authorizationId) {
|
|
836
|
+
const revokedId = this.authorizationId;
|
|
629
837
|
this.authorizationId = null;
|
|
630
838
|
this.authorizedIdentity = null;
|
|
839
|
+
this.authorizationVerified = false;
|
|
631
840
|
this.recoveryId = null;
|
|
632
|
-
this.
|
|
633
|
-
|
|
841
|
+
this.dropRecoveryRecord(revokedId);
|
|
842
|
+
const revokedError = new KeymasterProtocolError("authorization_expired", "Application connection authorization was revoked");
|
|
843
|
+
this.finishRecovery(void 0, revokedError);
|
|
844
|
+
this.disconnect(revokedError);
|
|
634
845
|
return;
|
|
635
846
|
}
|
|
636
847
|
if (value.type === "event") {
|
|
@@ -650,7 +861,24 @@ var KeymasterConnectClient = class {
|
|
|
650
861
|
const result = event.data;
|
|
651
862
|
if (this.authorizationId && result.id === this.recoveryId) {
|
|
652
863
|
if (!result.ok) {
|
|
653
|
-
|
|
864
|
+
const code = result.error?.code;
|
|
865
|
+
const message = result.error?.message ?? "Failed to restore wallet connection";
|
|
866
|
+
if (code === "authorization_expired") {
|
|
867
|
+
const expiredId = this.authorizationId;
|
|
868
|
+
this.authorizationId = null;
|
|
869
|
+
this.authorizedIdentity = null;
|
|
870
|
+
this.authorizationVerified = false;
|
|
871
|
+
this.recoveryId = null;
|
|
872
|
+
this.dropRecoveryRecord(expiredId);
|
|
873
|
+
const expired = new KeymasterProtocolError(code, message);
|
|
874
|
+
this.finishRecovery(void 0, expired);
|
|
875
|
+
this.disconnect(expired);
|
|
876
|
+
return;
|
|
877
|
+
}
|
|
878
|
+
const failure = new KeymasterProtocolError(code || "connection_expired", message);
|
|
879
|
+
this.pause(failure);
|
|
880
|
+
this.recoveryId = null;
|
|
881
|
+
this.finishRecovery(void 0, failure);
|
|
654
882
|
return;
|
|
655
883
|
}
|
|
656
884
|
try {
|
|
@@ -661,10 +889,13 @@ var KeymasterConnectClient = class {
|
|
|
661
889
|
this.identityValue = Object.freeze({ ...identity });
|
|
662
890
|
this.bindingId = value.bindingId;
|
|
663
891
|
this.recoveryId = null;
|
|
892
|
+
this.authorizationVerified = true;
|
|
664
893
|
this.setState("connected");
|
|
894
|
+
this.persistRecoveryRecord(restored.authorizationId, identity);
|
|
665
895
|
this.finishRecovery(restored);
|
|
666
896
|
} catch (error) {
|
|
667
897
|
this.pause(error);
|
|
898
|
+
this.recoveryId = null;
|
|
668
899
|
this.finishRecovery(void 0, error);
|
|
669
900
|
}
|
|
670
901
|
return;
|
|
@@ -677,7 +908,7 @@ var KeymasterConnectClient = class {
|
|
|
677
908
|
pending.reject(new KeymasterProtocolError("invalid_request", "Malformed Keymaster error result"));
|
|
678
909
|
return;
|
|
679
910
|
}
|
|
680
|
-
if (result.error.code === "connection_expired") {
|
|
911
|
+
if (result.error.code === "connection_expired" || result.error.code === "authorization_expired") {
|
|
681
912
|
if (this.authorizationId) this.pause(new KeymasterProtocolError(result.error.code, result.error.message));
|
|
682
913
|
else {
|
|
683
914
|
this.identityValue = null;
|
|
@@ -696,18 +927,23 @@ var KeymasterConnectClient = class {
|
|
|
696
927
|
this.identityValue = Object.freeze({ ...identity });
|
|
697
928
|
this.authorizedIdentity = this.identityValue;
|
|
698
929
|
this.authorizationId = connected.authorizationId;
|
|
930
|
+
this.authorizationVerified = true;
|
|
699
931
|
this.recoveryId = null;
|
|
700
932
|
this.bindingId = value.bindingId;
|
|
933
|
+
this.persistRecoveryRecord(connected.authorizationId, identity);
|
|
701
934
|
} else if (!this.identityValue || value.bindingId !== this.bindingId || !sameNetworkIdentity(this.identityValue, identity)) {
|
|
702
935
|
throw new Error("Keymaster connection identity mismatch");
|
|
703
936
|
}
|
|
704
937
|
if (pending.method === "connect.logout") {
|
|
938
|
+
const logoutId = this.authorizationId;
|
|
705
939
|
this.aiTools.clear(new Error("Logged out"));
|
|
706
940
|
this.identityValue = null;
|
|
707
941
|
this.bindingId = null;
|
|
708
942
|
this.authorizationId = null;
|
|
709
943
|
this.authorizedIdentity = null;
|
|
944
|
+
this.authorizationVerified = false;
|
|
710
945
|
this.recoveryId = null;
|
|
946
|
+
this.dropRecoveryRecord(logoutId);
|
|
711
947
|
}
|
|
712
948
|
pending.resolve(result.result);
|
|
713
949
|
} catch (error) {
|
|
@@ -749,6 +985,10 @@ var KeymasterConnectClient = class {
|
|
|
749
985
|
this.failReady(reason);
|
|
750
986
|
this.stopClosePolling();
|
|
751
987
|
this.sessionWindow = null;
|
|
988
|
+
this.sessionWindowReady = false;
|
|
989
|
+
this.explicitRecoveryActive = false;
|
|
990
|
+
this.explicitRecoveryProof = void 0;
|
|
991
|
+
this.recoveryReadyContinue = null;
|
|
752
992
|
this.setState("disconnected");
|
|
753
993
|
}
|
|
754
994
|
/** 暂停仅撤销瞬时绑定和旧请求,持久授权引用及原身份留作恢复校验。 */
|
|
@@ -760,11 +1000,43 @@ var KeymasterConnectClient = class {
|
|
|
760
1000
|
this.failReady(reason);
|
|
761
1001
|
this.setState("paused");
|
|
762
1002
|
}
|
|
763
|
-
|
|
1003
|
+
/** 保存最近成功连接的恢复记录;写入失败只记录原因,不影响已成功的登录。 */
|
|
1004
|
+
persistRecoveryRecord(authorizationId, identity) {
|
|
1005
|
+
if (!this.recoveryStorage) {
|
|
1006
|
+
this.recoverySaveErrorValue = "Recovery storage is unavailable; refresh will require re-login";
|
|
1007
|
+
return;
|
|
1008
|
+
}
|
|
1009
|
+
try {
|
|
1010
|
+
saveConnectionRecoveryRecord(this.recoveryStorage, {
|
|
1011
|
+
version: 1,
|
|
1012
|
+
targetOrigin: this.targetOrigin,
|
|
1013
|
+
mode: this.mode,
|
|
1014
|
+
authorizationId,
|
|
1015
|
+
networkIdentity: { ...identity }
|
|
1016
|
+
});
|
|
1017
|
+
this.recoverySaveErrorValue = null;
|
|
1018
|
+
} catch (error) {
|
|
1019
|
+
this.recoverySaveErrorValue = errorMessage(error, "Failed to persist recovery record; refresh will require re-login");
|
|
1020
|
+
}
|
|
1021
|
+
}
|
|
1022
|
+
/** 按目标与授权核对后清除本地恢复记录;旧实例迟到结果不得删除新记录。 */
|
|
1023
|
+
dropRecoveryRecord(authorizationId) {
|
|
1024
|
+
try {
|
|
1025
|
+
clearConnectionRecovery({
|
|
1026
|
+
recoveryStorage: this.recoveryStorage ?? void 0,
|
|
1027
|
+
targetOrigin: this.targetOrigin,
|
|
1028
|
+
...authorizationId ? { authorizationId } : {}
|
|
1029
|
+
});
|
|
1030
|
+
} catch {
|
|
1031
|
+
}
|
|
1032
|
+
}
|
|
1033
|
+
sendRecovery(appIdentity) {
|
|
764
1034
|
if (!this.authorizationId || !this.sessionWindow || isWindowClosed(this.sessionWindow)) return;
|
|
1035
|
+
if (this.stateValue === "opening") return;
|
|
1036
|
+
if ([...this.pending.values()].some((request) => ["connect.login", "connect.resume", "connect.launch"].includes(request.method))) return;
|
|
765
1037
|
this.recoveryId ??= this.environment.randomUUID();
|
|
766
1038
|
try {
|
|
767
|
-
this.sessionWindow.postMessage({ v: PROTOCOL_VERSION, type: "reconnect", id: this.recoveryId, authorizationId: this.authorizationId, ...this.bindingId ? { bindingId: this.bindingId } : {} }, this.targetOrigin);
|
|
1039
|
+
this.sessionWindow.postMessage({ v: PROTOCOL_VERSION, type: "reconnect", id: this.recoveryId, authorizationId: this.authorizationId, ...this.bindingId ? { bindingId: this.bindingId } : {}, ...appIdentity ? { appIdentity } : {} }, this.targetOrigin);
|
|
768
1040
|
} catch (error) {
|
|
769
1041
|
this.pause(error);
|
|
770
1042
|
}
|
|
@@ -782,7 +1054,7 @@ var KeymasterConnectClient = class {
|
|
|
782
1054
|
if (this.authorizationId) {
|
|
783
1055
|
if (this.stateValue !== "paused") this.pause(new KeymasterTransportError("wallet_paused", "Wallet window is closed"));
|
|
784
1056
|
} else this.disconnect(new KeymasterTransportError("wallet_closed", "Keymaster parent wallet was closed"));
|
|
785
|
-
} else this.sendRecovery();
|
|
1057
|
+
} else if (!this.explicitRecoveryActive) this.sendRecovery();
|
|
786
1058
|
}, this.options.closePollMs);
|
|
787
1059
|
}
|
|
788
1060
|
stopClosePolling() {
|
|
@@ -864,6 +1136,6 @@ function errorMessage(value, fallback) {
|
|
|
864
1136
|
return value instanceof Error && value.message ? value.message : fallback;
|
|
865
1137
|
}
|
|
866
1138
|
|
|
867
|
-
export { KeymasterConnectClient, KeymasterProtocolError, KeymasterTransportError, binary, binaryBytes, binaryText, binaryToText, createBrowserEnvironment, createKeymasterConnect };
|
|
1139
|
+
export { KeymasterConnectClient, KeymasterProtocolError, KeymasterTransportError, RECOVERY_STORAGE_KEY, binary, binaryBytes, binaryText, binaryToText, clearConnectionRecovery, createBrowserEnvironment, createKeymasterConnect, isRecoveryStorageAvailable, loadConnectionRecoveryRecord, readConnectionRecovery, resolveRecoveryStorage, saveConnectionRecoveryRecord };
|
|
868
1140
|
//# sourceMappingURL=index.js.map
|
|
869
1141
|
//# sourceMappingURL=index.js.map
|