keymaster-connect 0.1.4 → 0.1.6
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 +29 -5
- package/dist/{chunk-VVOTW5ZT.js → chunk-M2YEI4PG.js} +92 -3
- package/dist/chunk-M2YEI4PG.js.map +1 -0
- package/dist/{chunk-2EYARFS3.js → chunk-THT6XHHY.js} +5 -5
- package/dist/chunk-THT6XHHY.js.map +1 -0
- package/dist/index.d.ts +109 -14
- package/dist/index.js +303 -28
- package/dist/index.js.map +1 -1
- package/dist/{protocol-NcRDmEKZ.d.ts → protocol-GXCfd2ya.d.ts} +57 -98
- 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 +21 -12
- package/dist/server.js +144 -260
- package/dist/server.js.map +1 -1
- package/package.json +45 -10
- package/dist/chunk-2EYARFS3.js.map +0 -1
- package/dist/chunk-VVOTW5ZT.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { parseToolDefinitions, assertToolPayload, isAiToolMessage, validateToolArguments } from './chunk-
|
|
2
|
-
export { AI_TOOLS_MAX_JSON_CHARS, assertToolJson, assertToolPayload, isAiToolMessage, parseToolDefinitions, validateToolArguments } from './chunk-
|
|
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-
|
|
1
|
+
import { readOwnPublicationProof, parseToolDefinitions, assertToolPayload, isAiToolMessage, validateToolArguments } from './chunk-M2YEI4PG.js';
|
|
2
|
+
export { AI_TOOLS_MAX_JSON_CHARS, assertToolJson, assertToolPayload, isAiToolMessage, parseToolDefinitions, validateToolArguments } from './chunk-M2YEI4PG.js';
|
|
3
|
+
import { PROTOCOL_METHODS, PROTOCOL_VERSION, sameNetworkIdentity, validateNetworkIdentity } from './chunk-THT6XHHY.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-THT6XHHY.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,16 @@ var KeymasterConnectClient = class {
|
|
|
198
319
|
authorizationId = null;
|
|
199
320
|
authorizedIdentity = null;
|
|
200
321
|
recoveryId = null;
|
|
322
|
+
recoveryStorage;
|
|
323
|
+
recoverySaveErrorValue = null;
|
|
324
|
+
/** 显式恢复期间暂停后台心跳;结束(成功/失败/取消/超时)后恢复。 */
|
|
325
|
+
explicitRecoveryActive = false;
|
|
326
|
+
/** 内存授权是否已验证:login/launch/恢复成功置 true;仅从存储装载的待验证凭据为 false。 */
|
|
327
|
+
authorizationVerified = false;
|
|
328
|
+
/** 当前传输窗口是否已可信就绪(收到过匹配来源与窗口的 ready);新开窗口后重置,存在≠就绪。 */
|
|
329
|
+
sessionWindowReady = false;
|
|
330
|
+
/** 新 popup 首发前等待可信窗口 ready 的一次性继续函数;超时/结束时清空。 */
|
|
331
|
+
recoveryReadyContinue = null;
|
|
201
332
|
recoveryWaiters = /* @__PURE__ */ new Set();
|
|
202
333
|
constructor(options) {
|
|
203
334
|
if ("networkIdentity" in options) throw new TypeError("networkIdentity option has been removed; read identity from the connection result");
|
|
@@ -212,6 +343,7 @@ var KeymasterConnectClient = class {
|
|
|
212
343
|
closePollMs: positiveNumber(options.closePollMs ?? DEFAULT_CLOSE_POLL_MS, "closePollMs")
|
|
213
344
|
};
|
|
214
345
|
this.environment = options.environment ?? createBrowserEnvironment();
|
|
346
|
+
this.recoveryStorage = resolveRecoveryStorage(options.recoveryStorage);
|
|
215
347
|
}
|
|
216
348
|
/**
|
|
217
349
|
* Current browser transport state.
|
|
@@ -224,11 +356,15 @@ var KeymasterConnectClient = class {
|
|
|
224
356
|
/**
|
|
225
357
|
* Establishes the browser transport.
|
|
226
358
|
*
|
|
227
|
-
* iframe / appView 绑定真实 parent / opener;
|
|
359
|
+
* 只建立传输,不恢复授权:iframe / appView 绑定真实 parent / opener;
|
|
360
|
+
* popup 打开完整钱包等待 ready。刷新后的授权恢复请使用 `resume()`,
|
|
361
|
+
* 它会先装载本地恢复记录再复用 `continueConnection()`;`connect()` 本身
|
|
362
|
+
* 不读取恢复记录、不自动恢复。
|
|
228
363
|
*
|
|
229
364
|
* @group Transport
|
|
230
365
|
*/
|
|
231
366
|
connect() {
|
|
367
|
+
if (this.explicitRecoveryActive) return Promise.reject(new TypeError("Connection recovery is already in progress"));
|
|
232
368
|
return this.mode === "popup" ? this.connectPopup() : this.connectAppView();
|
|
233
369
|
}
|
|
234
370
|
/**
|
|
@@ -244,15 +380,23 @@ var KeymasterConnectClient = class {
|
|
|
244
380
|
for (const field of ["walletPublicKeyHex", "networkIdentity", "bsvNetwork", "assetId"]) {
|
|
245
381
|
if (field in params) throw new TypeError(`Removed wallet selector: ${field}`);
|
|
246
382
|
}
|
|
247
|
-
if (method === "connect.resume" && this.authorizationId)
|
|
383
|
+
if (method === "connect.resume" && this.authorizationId) {
|
|
384
|
+
return this.continueConnection(options);
|
|
385
|
+
}
|
|
248
386
|
const establishing = ["connect.login", "connect.resume", "connect.launch"].includes(method);
|
|
387
|
+
let effectiveParams = params;
|
|
388
|
+
if ((method === "connect.login" || method === "connect.launch") && !("publicationProof" in params)) {
|
|
389
|
+
effectiveParams = withOwnPublicationProof(params);
|
|
390
|
+
}
|
|
249
391
|
if (!establishing && this.stateValue === "paused") throw new KeymasterTransportError("wallet_paused", "Wallet connection is paused; old business requests are never replayed");
|
|
250
392
|
if (establishing && this.pending.size) throw new TypeError("Cannot establish a connection while requests are pending");
|
|
393
|
+
if (establishing && this.explicitRecoveryActive) throw new TypeError("Connection recovery is already in progress");
|
|
251
394
|
if (!establishing && !this.bindingId) throw new KeymasterProtocolError("invalid_request", "Login, resume or launch before making business requests");
|
|
252
395
|
if ([...this.pending.values()].some((request) => ["connect.login", "connect.resume", "connect.launch"].includes(request.method))) throw new TypeError("Connection establishment is already pending");
|
|
253
396
|
await this.connect();
|
|
254
397
|
if (establishing && this.pending.size) throw new TypeError("Cannot establish a connection while requests are pending");
|
|
255
398
|
if ([...this.pending.values()].some((request) => ["connect.login", "connect.resume", "connect.launch"].includes(request.method))) throw new TypeError("Connection establishment is already pending");
|
|
399
|
+
if (establishing && this.explicitRecoveryActive) throw new TypeError("Connection recovery is already in progress");
|
|
256
400
|
if (options.signal?.aborted) throw abortReason(options.signal);
|
|
257
401
|
const target = this.sessionWindow;
|
|
258
402
|
if (!target || isWindowClosed(target)) {
|
|
@@ -290,7 +434,7 @@ var KeymasterConnectClient = class {
|
|
|
290
434
|
...!establishing ? { bindingId: this.bindingId } : {},
|
|
291
435
|
id: requestId,
|
|
292
436
|
method,
|
|
293
|
-
params
|
|
437
|
+
params: effectiveParams
|
|
294
438
|
}, this.targetOrigin);
|
|
295
439
|
} catch (cause) {
|
|
296
440
|
this.deletePending(requestId);
|
|
@@ -304,16 +448,38 @@ var KeymasterConnectClient = class {
|
|
|
304
448
|
* @group Connect
|
|
305
449
|
*/
|
|
306
450
|
login(params, options) {
|
|
307
|
-
return this.request("connect.login", params, options);
|
|
451
|
+
return this.request("connect.login", withOwnPublicationProof(params), options);
|
|
308
452
|
}
|
|
309
453
|
/**
|
|
310
454
|
* Calls `connect.resume` to restore the unlock runtime for a persistent
|
|
311
455
|
* session.
|
|
312
456
|
*
|
|
457
|
+
* 刷新恢复时只把保存的凭据和预期身份载入待恢复上下文,不直接置为已登录;
|
|
458
|
+
* 钱包验证成功、结果身份一致并取得新 binding 后才开放业务调用。
|
|
459
|
+
* 无可用凭据时直接拒绝并提示重新登录,不回退到无凭据的重建默认授权。
|
|
460
|
+
*
|
|
461
|
+
*
|
|
313
462
|
* @group Connect
|
|
314
463
|
*/
|
|
315
464
|
resume(params = {}, options) {
|
|
316
|
-
return
|
|
465
|
+
if (options?.signal?.aborted) return Promise.reject(abortReason(options.signal));
|
|
466
|
+
if (this.explicitRecoveryActive) return Promise.reject(new TypeError("Connection recovery is already in progress"));
|
|
467
|
+
if (this.authorizationId && this.authorizationVerified) {
|
|
468
|
+
return this.request("connect.resume", params, options);
|
|
469
|
+
}
|
|
470
|
+
const stored = loadConnectionRecoveryRecord({
|
|
471
|
+
recoveryStorage: this.recoveryStorage ?? void 0,
|
|
472
|
+
targetOrigin: this.targetOrigin,
|
|
473
|
+
mode: this.mode
|
|
474
|
+
});
|
|
475
|
+
if (stored) {
|
|
476
|
+
this.authorizationId = stored.authorizationId;
|
|
477
|
+
this.authorizedIdentity = Object.freeze({ ...stored.networkIdentity });
|
|
478
|
+
this.authorizationVerified = false;
|
|
479
|
+
} else if (!this.authorizationId) {
|
|
480
|
+
return Promise.reject(new KeymasterTransportError("client_closed", "No saved Keymaster recovery record for this origin and mode; login again"));
|
|
481
|
+
}
|
|
482
|
+
return this.continueConnection(options ?? {});
|
|
317
483
|
}
|
|
318
484
|
/**
|
|
319
485
|
* Calls `connect.logout` to revoke a persistent Connect session.
|
|
@@ -331,7 +497,7 @@ var KeymasterConnectClient = class {
|
|
|
331
497
|
* @group Connect
|
|
332
498
|
*/
|
|
333
499
|
launch(params, options) {
|
|
334
|
-
return this.request("connect.launch", params, options);
|
|
500
|
+
return this.request("connect.launch", withOwnPublicationProof(params), options);
|
|
335
501
|
}
|
|
336
502
|
/**
|
|
337
503
|
* Calls `identity.get` to return signed identity claims for the session
|
|
@@ -381,17 +547,6 @@ var KeymasterConnectClient = class {
|
|
|
381
547
|
paymentStatus(params, options) {
|
|
382
548
|
return this.request("payment.status", params, options);
|
|
383
549
|
}
|
|
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
550
|
/**
|
|
396
551
|
* 发布到任意精确频道;owner、公钥签名和消息编号由 Keymaster 生成。
|
|
397
552
|
*
|
|
@@ -448,6 +603,22 @@ var KeymasterConnectClient = class {
|
|
|
448
603
|
storageGet(params, options) {
|
|
449
604
|
return this.request("storage.get", params, options);
|
|
450
605
|
}
|
|
606
|
+
/**
|
|
607
|
+
* 可选加密写入:向钱包提交明文,钱包以当前钱包密钥和 App 公钥隔离后加密保存。
|
|
608
|
+
* 路径、条件创建语义与 storagePut 相同,size 返回明文字节数;不会返回加密密钥。
|
|
609
|
+
* @group Storage
|
|
610
|
+
*/
|
|
611
|
+
storagePutEncrypted(params, options) {
|
|
612
|
+
return this.request("storage.encrypted.put", params, options);
|
|
613
|
+
}
|
|
614
|
+
/**
|
|
615
|
+
* 可选加密读取:仅同一钱包能解密;offset/length 作用于明文,ifMatch 仍比较远端版本。
|
|
616
|
+
* 明文对象、错误格式、篡改或密钥不匹配均失败,不自动回退为普通读取。
|
|
617
|
+
* @group Storage
|
|
618
|
+
*/
|
|
619
|
+
storageGetEncrypted(params, options) {
|
|
620
|
+
return this.request("storage.encrypted.get", params, options);
|
|
621
|
+
}
|
|
451
622
|
/**
|
|
452
623
|
* Calls `storage.delete` to delete an app-scoped object.
|
|
453
624
|
*
|
|
@@ -503,6 +674,9 @@ var KeymasterConnectClient = class {
|
|
|
503
674
|
* Tears down the local transport. This does not revoke persistent Connect
|
|
504
675
|
* sessions; call {@link logout} before closing when revocation is required.
|
|
505
676
|
*
|
|
677
|
+
* 只结束运行连接,保留刷新恢复材料:`close()`、页面卸载和运行失效都不删除
|
|
678
|
+
* 本地恢复记录;`logout` 成功、明确授权失效或用户主动清除才删除。
|
|
679
|
+
*
|
|
506
680
|
* @group Transport
|
|
507
681
|
*/
|
|
508
682
|
close() {
|
|
@@ -526,25 +700,45 @@ var KeymasterConnectClient = class {
|
|
|
526
700
|
}
|
|
527
701
|
if (!this.authorizationId) this.sessionWindow = null;
|
|
528
702
|
this.recoveryId = null;
|
|
703
|
+
this.explicitRecoveryActive = false;
|
|
704
|
+
this.recoveryReadyContinue = null;
|
|
529
705
|
this.finishRecovery(void 0, error);
|
|
530
706
|
this.setState(this.authorizationId ? "paused" : "disconnected");
|
|
531
707
|
}
|
|
532
708
|
/** 主动恢复可由 popup 重新打开钱包;后台恢复从不打开窗口或重放业务。 */
|
|
533
709
|
continueConnection(options) {
|
|
710
|
+
if (options.signal?.aborted) return Promise.reject(abortReason(options.signal));
|
|
711
|
+
if (this.explicitRecoveryActive) return Promise.reject(new TypeError("Connection recovery is already in progress"));
|
|
534
712
|
if (this.pending.size) return Promise.reject(new TypeError("Cannot resume while business requests are pending"));
|
|
535
713
|
this.installListener();
|
|
714
|
+
let awaitReady = false;
|
|
536
715
|
if (this.mode === "popup") {
|
|
537
716
|
if (!this.sessionWindow || isWindowClosed(this.sessionWindow)) {
|
|
538
717
|
const url = new URL(this.options.popupPath, `${this.targetOrigin}/`);
|
|
539
718
|
if (url.origin !== this.targetOrigin) return Promise.reject(new TypeError("popupPath must resolve inside targetOrigin"));
|
|
540
719
|
this.sessionWindow = this.environment.open(url.href, "_blank", "");
|
|
541
720
|
if (!this.sessionWindow) return Promise.reject(new KeymasterTransportError("popup_blocked", "Keymaster wallet window was blocked"));
|
|
721
|
+
this.sessionWindowReady = false;
|
|
722
|
+
awaitReady = true;
|
|
723
|
+
} else if (!this.sessionWindowReady) {
|
|
724
|
+
awaitReady = true;
|
|
542
725
|
}
|
|
543
726
|
} else {
|
|
544
727
|
const launcher = this.mode === "iframe" ? this.environment.parent() : this.environment.opener();
|
|
545
728
|
if (!launcher || isWindowClosed(launcher)) return Promise.reject(new KeymasterTransportError("appview_session_lost", "Wallet launcher unavailable; relaunch from the wallet"));
|
|
546
729
|
this.sessionWindow = launcher;
|
|
547
730
|
}
|
|
731
|
+
const wasVerified = this.authorizationVerified;
|
|
732
|
+
this.explicitRecoveryActive = true;
|
|
733
|
+
const endExplicitAttempt = (failed) => {
|
|
734
|
+
this.explicitRecoveryActive = false;
|
|
735
|
+
this.recoveryReadyContinue = null;
|
|
736
|
+
if (failed && !wasVerified && this.authorizationId) {
|
|
737
|
+
this.authorizationId = null;
|
|
738
|
+
this.authorizedIdentity = null;
|
|
739
|
+
this.recoveryId = null;
|
|
740
|
+
}
|
|
741
|
+
};
|
|
548
742
|
this.pause(new KeymasterTransportError("wallet_paused", "Re-establishing the original wallet connection"));
|
|
549
743
|
this.startClosePolling();
|
|
550
744
|
return new Promise((resolve, reject) => {
|
|
@@ -552,6 +746,7 @@ var KeymasterConnectClient = class {
|
|
|
552
746
|
this.environment.clearTimeout(timer);
|
|
553
747
|
options.signal?.removeEventListener("abort", aborted);
|
|
554
748
|
this.recoveryWaiters.delete(waiter);
|
|
749
|
+
endExplicitAttempt(!!error);
|
|
555
750
|
if (error) reject(error);
|
|
556
751
|
else resolve(result);
|
|
557
752
|
};
|
|
@@ -560,7 +755,11 @@ var KeymasterConnectClient = class {
|
|
|
560
755
|
const timer = this.environment.setTimeout(() => finish(new KeymasterTransportError("request_timeout", "Timed out restoring wallet connection")), positiveNumber(options.timeoutMs ?? this.options.readyTimeoutMs, "timeoutMs"));
|
|
561
756
|
this.recoveryWaiters.add(waiter);
|
|
562
757
|
options.signal?.addEventListener("abort", aborted, { once: true });
|
|
563
|
-
|
|
758
|
+
if (awaitReady) {
|
|
759
|
+
this.recoveryReadyContinue = () => this.sendRecovery();
|
|
760
|
+
} else {
|
|
761
|
+
this.sendRecovery();
|
|
762
|
+
}
|
|
564
763
|
});
|
|
565
764
|
}
|
|
566
765
|
connectPopup() {
|
|
@@ -575,6 +774,7 @@ var KeymasterConnectClient = class {
|
|
|
575
774
|
this.setState("disconnected");
|
|
576
775
|
return Promise.reject(new KeymasterTransportError("popup_blocked", "Keymaster wallet window was blocked"));
|
|
577
776
|
}
|
|
777
|
+
this.sessionWindowReady = false;
|
|
578
778
|
this.startClosePolling();
|
|
579
779
|
this.readyPromise = new Promise((resolve, reject) => {
|
|
580
780
|
this.readyResolve = resolve;
|
|
@@ -617,7 +817,13 @@ var KeymasterConnectClient = class {
|
|
|
617
817
|
}
|
|
618
818
|
if (this.aiTools.handle(value)) return;
|
|
619
819
|
if (value.type === "ready") {
|
|
820
|
+
this.sessionWindowReady = true;
|
|
620
821
|
if (this.mode === "popup" && this.readyPromise) this.finishReady();
|
|
822
|
+
if (this.explicitRecoveryActive && this.recoveryReadyContinue) {
|
|
823
|
+
const send = this.recoveryReadyContinue;
|
|
824
|
+
this.recoveryReadyContinue = null;
|
|
825
|
+
send();
|
|
826
|
+
}
|
|
621
827
|
return;
|
|
622
828
|
}
|
|
623
829
|
if (value.type === "closing") {
|
|
@@ -626,11 +832,15 @@ var KeymasterConnectClient = class {
|
|
|
626
832
|
return;
|
|
627
833
|
}
|
|
628
834
|
if (value.type === "revoked" && value.authorizationId === this.authorizationId) {
|
|
835
|
+
const revokedId = this.authorizationId;
|
|
629
836
|
this.authorizationId = null;
|
|
630
837
|
this.authorizedIdentity = null;
|
|
838
|
+
this.authorizationVerified = false;
|
|
631
839
|
this.recoveryId = null;
|
|
632
|
-
this.
|
|
633
|
-
|
|
840
|
+
this.dropRecoveryRecord(revokedId);
|
|
841
|
+
const revokedError = new KeymasterProtocolError("authorization_expired", "Application connection authorization was revoked");
|
|
842
|
+
this.finishRecovery(void 0, revokedError);
|
|
843
|
+
this.disconnect(revokedError);
|
|
634
844
|
return;
|
|
635
845
|
}
|
|
636
846
|
if (value.type === "event") {
|
|
@@ -650,7 +860,24 @@ var KeymasterConnectClient = class {
|
|
|
650
860
|
const result = event.data;
|
|
651
861
|
if (this.authorizationId && result.id === this.recoveryId) {
|
|
652
862
|
if (!result.ok) {
|
|
653
|
-
|
|
863
|
+
const code = result.error?.code;
|
|
864
|
+
const message = result.error?.message ?? "Failed to restore wallet connection";
|
|
865
|
+
if (code === "authorization_expired") {
|
|
866
|
+
const expiredId = this.authorizationId;
|
|
867
|
+
this.authorizationId = null;
|
|
868
|
+
this.authorizedIdentity = null;
|
|
869
|
+
this.authorizationVerified = false;
|
|
870
|
+
this.recoveryId = null;
|
|
871
|
+
this.dropRecoveryRecord(expiredId);
|
|
872
|
+
const expired = new KeymasterProtocolError(code, message);
|
|
873
|
+
this.finishRecovery(void 0, expired);
|
|
874
|
+
this.disconnect(expired);
|
|
875
|
+
return;
|
|
876
|
+
}
|
|
877
|
+
const failure = new KeymasterProtocolError(code || "connection_expired", message);
|
|
878
|
+
this.pause(failure);
|
|
879
|
+
this.recoveryId = null;
|
|
880
|
+
this.finishRecovery(void 0, failure);
|
|
654
881
|
return;
|
|
655
882
|
}
|
|
656
883
|
try {
|
|
@@ -661,10 +888,13 @@ var KeymasterConnectClient = class {
|
|
|
661
888
|
this.identityValue = Object.freeze({ ...identity });
|
|
662
889
|
this.bindingId = value.bindingId;
|
|
663
890
|
this.recoveryId = null;
|
|
891
|
+
this.authorizationVerified = true;
|
|
664
892
|
this.setState("connected");
|
|
893
|
+
this.persistRecoveryRecord(restored.authorizationId, identity);
|
|
665
894
|
this.finishRecovery(restored);
|
|
666
895
|
} catch (error) {
|
|
667
896
|
this.pause(error);
|
|
897
|
+
this.recoveryId = null;
|
|
668
898
|
this.finishRecovery(void 0, error);
|
|
669
899
|
}
|
|
670
900
|
return;
|
|
@@ -677,7 +907,7 @@ var KeymasterConnectClient = class {
|
|
|
677
907
|
pending.reject(new KeymasterProtocolError("invalid_request", "Malformed Keymaster error result"));
|
|
678
908
|
return;
|
|
679
909
|
}
|
|
680
|
-
if (result.error.code === "connection_expired") {
|
|
910
|
+
if (result.error.code === "connection_expired" || result.error.code === "authorization_expired") {
|
|
681
911
|
if (this.authorizationId) this.pause(new KeymasterProtocolError(result.error.code, result.error.message));
|
|
682
912
|
else {
|
|
683
913
|
this.identityValue = null;
|
|
@@ -696,18 +926,23 @@ var KeymasterConnectClient = class {
|
|
|
696
926
|
this.identityValue = Object.freeze({ ...identity });
|
|
697
927
|
this.authorizedIdentity = this.identityValue;
|
|
698
928
|
this.authorizationId = connected.authorizationId;
|
|
929
|
+
this.authorizationVerified = true;
|
|
699
930
|
this.recoveryId = null;
|
|
700
931
|
this.bindingId = value.bindingId;
|
|
932
|
+
this.persistRecoveryRecord(connected.authorizationId, identity);
|
|
701
933
|
} else if (!this.identityValue || value.bindingId !== this.bindingId || !sameNetworkIdentity(this.identityValue, identity)) {
|
|
702
934
|
throw new Error("Keymaster connection identity mismatch");
|
|
703
935
|
}
|
|
704
936
|
if (pending.method === "connect.logout") {
|
|
937
|
+
const logoutId = this.authorizationId;
|
|
705
938
|
this.aiTools.clear(new Error("Logged out"));
|
|
706
939
|
this.identityValue = null;
|
|
707
940
|
this.bindingId = null;
|
|
708
941
|
this.authorizationId = null;
|
|
709
942
|
this.authorizedIdentity = null;
|
|
943
|
+
this.authorizationVerified = false;
|
|
710
944
|
this.recoveryId = null;
|
|
945
|
+
this.dropRecoveryRecord(logoutId);
|
|
711
946
|
}
|
|
712
947
|
pending.resolve(result.result);
|
|
713
948
|
} catch (error) {
|
|
@@ -749,6 +984,9 @@ var KeymasterConnectClient = class {
|
|
|
749
984
|
this.failReady(reason);
|
|
750
985
|
this.stopClosePolling();
|
|
751
986
|
this.sessionWindow = null;
|
|
987
|
+
this.sessionWindowReady = false;
|
|
988
|
+
this.explicitRecoveryActive = false;
|
|
989
|
+
this.recoveryReadyContinue = null;
|
|
752
990
|
this.setState("disconnected");
|
|
753
991
|
}
|
|
754
992
|
/** 暂停仅撤销瞬时绑定和旧请求,持久授权引用及原身份留作恢复校验。 */
|
|
@@ -760,8 +998,40 @@ var KeymasterConnectClient = class {
|
|
|
760
998
|
this.failReady(reason);
|
|
761
999
|
this.setState("paused");
|
|
762
1000
|
}
|
|
1001
|
+
/** 保存最近成功连接的恢复记录;写入失败只记录原因,不影响已成功的登录。 */
|
|
1002
|
+
persistRecoveryRecord(authorizationId, identity) {
|
|
1003
|
+
if (!this.recoveryStorage) {
|
|
1004
|
+
this.recoverySaveErrorValue = "Recovery storage is unavailable; refresh will require re-login";
|
|
1005
|
+
return;
|
|
1006
|
+
}
|
|
1007
|
+
try {
|
|
1008
|
+
saveConnectionRecoveryRecord(this.recoveryStorage, {
|
|
1009
|
+
version: 1,
|
|
1010
|
+
targetOrigin: this.targetOrigin,
|
|
1011
|
+
mode: this.mode,
|
|
1012
|
+
authorizationId,
|
|
1013
|
+
networkIdentity: { ...identity }
|
|
1014
|
+
});
|
|
1015
|
+
this.recoverySaveErrorValue = null;
|
|
1016
|
+
} catch (error) {
|
|
1017
|
+
this.recoverySaveErrorValue = errorMessage(error, "Failed to persist recovery record; refresh will require re-login");
|
|
1018
|
+
}
|
|
1019
|
+
}
|
|
1020
|
+
/** 按目标与授权核对后清除本地恢复记录;旧实例迟到结果不得删除新记录。 */
|
|
1021
|
+
dropRecoveryRecord(authorizationId) {
|
|
1022
|
+
try {
|
|
1023
|
+
clearConnectionRecovery({
|
|
1024
|
+
recoveryStorage: this.recoveryStorage ?? void 0,
|
|
1025
|
+
targetOrigin: this.targetOrigin,
|
|
1026
|
+
...authorizationId ? { authorizationId } : {}
|
|
1027
|
+
});
|
|
1028
|
+
} catch {
|
|
1029
|
+
}
|
|
1030
|
+
}
|
|
763
1031
|
sendRecovery() {
|
|
764
1032
|
if (!this.authorizationId || !this.sessionWindow || isWindowClosed(this.sessionWindow)) return;
|
|
1033
|
+
if (this.stateValue === "opening") return;
|
|
1034
|
+
if ([...this.pending.values()].some((request) => ["connect.login", "connect.resume", "connect.launch"].includes(request.method))) return;
|
|
765
1035
|
this.recoveryId ??= this.environment.randomUUID();
|
|
766
1036
|
try {
|
|
767
1037
|
this.sessionWindow.postMessage({ v: PROTOCOL_VERSION, type: "reconnect", id: this.recoveryId, authorizationId: this.authorizationId, ...this.bindingId ? { bindingId: this.bindingId } : {} }, this.targetOrigin);
|
|
@@ -782,7 +1052,7 @@ var KeymasterConnectClient = class {
|
|
|
782
1052
|
if (this.authorizationId) {
|
|
783
1053
|
if (this.stateValue !== "paused") this.pause(new KeymasterTransportError("wallet_paused", "Wallet window is closed"));
|
|
784
1054
|
} else this.disconnect(new KeymasterTransportError("wallet_closed", "Keymaster parent wallet was closed"));
|
|
785
|
-
} else this.sendRecovery();
|
|
1055
|
+
} else if (!this.explicitRecoveryActive) this.sendRecovery();
|
|
786
1056
|
}, this.options.closePollMs);
|
|
787
1057
|
}
|
|
788
1058
|
stopClosePolling() {
|
|
@@ -863,7 +1133,12 @@ function abortReason(signal) {
|
|
|
863
1133
|
function errorMessage(value, fallback) {
|
|
864
1134
|
return value instanceof Error && value.message ? value.message : fallback;
|
|
865
1135
|
}
|
|
1136
|
+
function withOwnPublicationProof(params) {
|
|
1137
|
+
if ("publicationProof" in params && params.publicationProof !== void 0) return params;
|
|
1138
|
+
const proof = readOwnPublicationProof();
|
|
1139
|
+
return proof ? { ...params, publicationProof: proof } : params;
|
|
1140
|
+
}
|
|
866
1141
|
|
|
867
|
-
export { KeymasterConnectClient, KeymasterProtocolError, KeymasterTransportError, binary, binaryBytes, binaryText, binaryToText, createBrowserEnvironment, createKeymasterConnect };
|
|
1142
|
+
export { KeymasterConnectClient, KeymasterProtocolError, KeymasterTransportError, RECOVERY_STORAGE_KEY, binary, binaryBytes, binaryText, binaryToText, clearConnectionRecovery, createBrowserEnvironment, createKeymasterConnect, isRecoveryStorageAvailable, loadConnectionRecoveryRecord, readConnectionRecovery, resolveRecoveryStorage, saveConnectionRecoveryRecord };
|
|
868
1143
|
//# sourceMappingURL=index.js.map
|
|
869
1144
|
//# sourceMappingURL=index.js.map
|