keymaster-connect 0.1.5 → 0.1.7
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 +11 -21
- package/dist/{chunk-ZDIFZJYD.js → chunk-D2RL3QGR.js} +5 -17
- package/dist/chunk-D2RL3QGR.js.map +1 -0
- package/dist/{chunk-VVOTW5ZT.js → chunk-M2YEI4PG.js} +92 -3
- package/dist/chunk-M2YEI4PG.js.map +1 -0
- package/dist/index.d.ts +14 -173
- package/dist/index.js +127 -548
- package/dist/index.js.map +1 -1
- package/dist/{protocol-C8z30CKK.d.ts → protocol-B7bpi5ro.d.ts} +43 -191
- package/dist/protocol.d.ts +1 -1
- package/dist/protocol.js +1 -1
- package/dist/script.d.ts +1 -1
- package/dist/server.d.ts +24 -17
- package/dist/server.js +112 -340
- package/dist/server.js.map +1 -1
- package/package.json +45 -11
- package/dist/chunk-VVOTW5ZT.js.map +0 -1
- package/dist/chunk-ZDIFZJYD.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,
|
|
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-D2RL3QGR.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, isValidMsFileSourceId, sameNetworkIdentity, validateBsvNetwork, validateNetworkIdentity } from './chunk-D2RL3QGR.js';
|
|
5
5
|
|
|
6
6
|
// src/binary.ts
|
|
7
7
|
function binary(bytes, mime) {
|
|
@@ -143,119 +143,6 @@ 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
|
-
|
|
259
146
|
// src/client.ts
|
|
260
147
|
var KeymasterTransportError = class extends Error {
|
|
261
148
|
constructor(code, message) {
|
|
@@ -295,14 +182,6 @@ var KeymasterConnectClient = class {
|
|
|
295
182
|
get networkIdentity() {
|
|
296
183
|
return this.identityValue;
|
|
297
184
|
}
|
|
298
|
-
/** 最近一次持久化失败的原因;成功后清零。登录成功不受写入失败影响。 */
|
|
299
|
-
get recoverySaveError() {
|
|
300
|
-
return this.recoverySaveErrorValue;
|
|
301
|
-
}
|
|
302
|
-
/** 刷新恢复存储是否可用;不可用时仍可登录,但刷新后需重新登录。 */
|
|
303
|
-
get recoveryStorageAvailable() {
|
|
304
|
-
return this.recoveryStorage !== null;
|
|
305
|
-
}
|
|
306
185
|
targetOrigin;
|
|
307
186
|
mode;
|
|
308
187
|
environment;
|
|
@@ -317,20 +196,8 @@ var KeymasterConnectClient = class {
|
|
|
317
196
|
readyTimer = null;
|
|
318
197
|
closePoller = null;
|
|
319
198
|
authorizationId = null;
|
|
320
|
-
|
|
321
|
-
|
|
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;
|
|
333
|
-
recoveryWaiters = /* @__PURE__ */ new Set();
|
|
199
|
+
/** 等待 ready 与授权响应共用一个建连位置,取消不能影响其他调用。 */
|
|
200
|
+
establishmentActive = false;
|
|
334
201
|
constructor(options) {
|
|
335
202
|
if ("networkIdentity" in options) throw new TypeError("networkIdentity option has been removed; read identity from the connection result");
|
|
336
203
|
this.targetOrigin = normalizeHttpOrigin(options.targetOrigin);
|
|
@@ -344,7 +211,6 @@ var KeymasterConnectClient = class {
|
|
|
344
211
|
closePollMs: positiveNumber(options.closePollMs ?? DEFAULT_CLOSE_POLL_MS, "closePollMs")
|
|
345
212
|
};
|
|
346
213
|
this.environment = options.environment ?? createBrowserEnvironment();
|
|
347
|
-
this.recoveryStorage = resolveRecoveryStorage(options.recoveryStorage);
|
|
348
214
|
}
|
|
349
215
|
/**
|
|
350
216
|
* Current browser transport state.
|
|
@@ -357,15 +223,9 @@ var KeymasterConnectClient = class {
|
|
|
357
223
|
/**
|
|
358
224
|
* Establishes the browser transport.
|
|
359
225
|
*
|
|
360
|
-
* 只建立传输,不恢复授权:iframe / appView 绑定真实 parent / opener;
|
|
361
|
-
* popup 打开完整钱包等待 ready。刷新后的授权恢复请使用 `resume()`,
|
|
362
|
-
* 它会先装载本地恢复记录再复用 `continueConnection()`;`connect()` 本身
|
|
363
|
-
* 不读取恢复记录、不自动恢复。
|
|
364
|
-
*
|
|
365
226
|
* @group Transport
|
|
366
227
|
*/
|
|
367
228
|
connect() {
|
|
368
|
-
if (this.explicitRecoveryActive) return Promise.reject(new TypeError("Connection recovery is already in progress"));
|
|
369
229
|
return this.mode === "popup" ? this.connectPopup() : this.connectAppView();
|
|
370
230
|
}
|
|
371
231
|
/**
|
|
@@ -381,64 +241,82 @@ var KeymasterConnectClient = class {
|
|
|
381
241
|
for (const field of ["walletPublicKeyHex", "networkIdentity", "bsvNetwork", "assetId"]) {
|
|
382
242
|
if (field in params) throw new TypeError(`Removed wallet selector: ${field}`);
|
|
383
243
|
}
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
if (establishing && this.explicitRecoveryActive) throw new TypeError("Connection recovery is already in progress");
|
|
392
|
-
if (!establishing && !this.bindingId) throw new KeymasterProtocolError("invalid_request", "Login, resume or launch before making business requests");
|
|
393
|
-
if ([...this.pending.values()].some((request) => ["connect.login", "connect.resume", "connect.launch"].includes(request.method))) throw new TypeError("Connection establishment is already pending");
|
|
394
|
-
await this.connect();
|
|
395
|
-
if (establishing && this.pending.size) throw new TypeError("Cannot establish a connection while requests are pending");
|
|
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");
|
|
398
|
-
if (options.signal?.aborted) throw abortReason(options.signal);
|
|
399
|
-
const target = this.sessionWindow;
|
|
400
|
-
if (!target || isWindowClosed(target)) {
|
|
401
|
-
throw new KeymasterTransportError("wallet_closed", "Keymaster parent wallet is closed");
|
|
402
|
-
}
|
|
403
|
-
const requestId = options.requestId ?? this.environment.randomUUID();
|
|
404
|
-
if (!requestId || this.pending.has(requestId)) {
|
|
405
|
-
throw new TypeError("requestId must be non-empty and unique among pending requests");
|
|
406
|
-
}
|
|
407
|
-
const timeoutMs = positiveNumber(options.timeoutMs ?? this.options.requestTimeoutMs, "timeoutMs");
|
|
408
|
-
return new Promise((resolve, reject) => {
|
|
409
|
-
const timer = this.environment.setTimeout(() => {
|
|
410
|
-
this.postCancel(requestId);
|
|
411
|
-
this.deletePending(requestId);
|
|
412
|
-
reject(new KeymasterTransportError("request_timeout", `Keymaster request timed out: ${method}`));
|
|
413
|
-
}, timeoutMs);
|
|
414
|
-
const abortListener = options.signal ? () => {
|
|
415
|
-
this.postCancel(requestId);
|
|
416
|
-
this.deletePending(requestId);
|
|
417
|
-
reject(abortReason(options.signal));
|
|
418
|
-
} : void 0;
|
|
419
|
-
if (abortListener) options.signal.addEventListener("abort", abortListener, { once: true });
|
|
420
|
-
this.pending.set(requestId, {
|
|
421
|
-
method,
|
|
422
|
-
resolve: (value) => resolve(value),
|
|
423
|
-
reject,
|
|
424
|
-
timer,
|
|
425
|
-
signal: options.signal,
|
|
426
|
-
abortListener
|
|
427
|
-
});
|
|
428
|
-
try {
|
|
429
|
-
target.postMessage({
|
|
430
|
-
v: PROTOCOL_VERSION,
|
|
431
|
-
type: "request",
|
|
432
|
-
...!establishing ? { bindingId: this.bindingId } : {},
|
|
433
|
-
id: requestId,
|
|
434
|
-
method,
|
|
435
|
-
params
|
|
436
|
-
}, this.targetOrigin);
|
|
437
|
-
} catch (cause) {
|
|
438
|
-
this.deletePending(requestId);
|
|
439
|
-
reject(new KeymasterTransportError("send_failed", errorMessage(cause, "Unable to send Keymaster request")));
|
|
244
|
+
const establishing = ["connect.login", "connect.launch"].includes(method);
|
|
245
|
+
if (this.establishmentActive) throw new TypeError("Connection establishment is already pending");
|
|
246
|
+
if (establishing) this.establishmentActive = true;
|
|
247
|
+
try {
|
|
248
|
+
let effectiveParams = params;
|
|
249
|
+
if ((method === "connect.login" || method === "connect.launch") && !("publicationProof" in params)) {
|
|
250
|
+
effectiveParams = withOwnPublicationProof(params);
|
|
440
251
|
}
|
|
441
|
-
|
|
252
|
+
if (establishing && this.pending.size) throw new TypeError("Cannot establish a connection while requests are pending");
|
|
253
|
+
if (!establishing && !this.bindingId) throw new KeymasterProtocolError("invalid_request", "Login or launch before making business requests");
|
|
254
|
+
if ([...this.pending.values()].some((request) => ["connect.login", "connect.launch"].includes(request.method))) throw new TypeError("Connection establishment is already pending");
|
|
255
|
+
if (establishing) {
|
|
256
|
+
const abortOpening = () => this.disconnect(abortReason(options.signal));
|
|
257
|
+
options.signal?.addEventListener("abort", abortOpening, { once: true });
|
|
258
|
+
try {
|
|
259
|
+
await this.connect();
|
|
260
|
+
} finally {
|
|
261
|
+
options.signal?.removeEventListener("abort", abortOpening);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
if (establishing && this.pending.size) throw new TypeError("Cannot establish a connection while requests are pending");
|
|
265
|
+
if ([...this.pending.values()].some((request) => ["connect.login", "connect.launch"].includes(request.method))) throw new TypeError("Connection establishment is already pending");
|
|
266
|
+
if (options.signal?.aborted) throw abortReason(options.signal);
|
|
267
|
+
const target = this.sessionWindow;
|
|
268
|
+
if (!target || isWindowClosed(target) || !establishing && this.stateValue !== "connected") {
|
|
269
|
+
const error = new KeymasterTransportError("wallet_closed", "Wallet connection is unavailable; login again");
|
|
270
|
+
this.disconnect(error);
|
|
271
|
+
throw error;
|
|
272
|
+
}
|
|
273
|
+
const requestId = options.requestId ?? this.environment.randomUUID();
|
|
274
|
+
if (!requestId || this.pending.has(requestId)) {
|
|
275
|
+
throw new TypeError("requestId must be non-empty and unique among pending requests");
|
|
276
|
+
}
|
|
277
|
+
const timeoutMs = positiveNumber(options.timeoutMs ?? this.options.requestTimeoutMs, "timeoutMs");
|
|
278
|
+
return await new Promise((resolve, reject) => {
|
|
279
|
+
const timer = this.environment.setTimeout(() => {
|
|
280
|
+
this.postCancel(requestId);
|
|
281
|
+
this.deletePending(requestId);
|
|
282
|
+
const error = new KeymasterTransportError("request_timeout", `Keymaster request timed out: ${method}`);
|
|
283
|
+
if (establishing) this.disconnect(error);
|
|
284
|
+
reject(error);
|
|
285
|
+
}, timeoutMs);
|
|
286
|
+
const abortListener = options.signal ? () => {
|
|
287
|
+
this.postCancel(requestId);
|
|
288
|
+
this.deletePending(requestId);
|
|
289
|
+
if (establishing) this.disconnect(abortReason(options.signal));
|
|
290
|
+
reject(abortReason(options.signal));
|
|
291
|
+
} : void 0;
|
|
292
|
+
if (abortListener) options.signal.addEventListener("abort", abortListener, { once: true });
|
|
293
|
+
this.pending.set(requestId, {
|
|
294
|
+
method,
|
|
295
|
+
resolve: (value) => resolve(value),
|
|
296
|
+
reject,
|
|
297
|
+
timer,
|
|
298
|
+
signal: options.signal,
|
|
299
|
+
abortListener
|
|
300
|
+
});
|
|
301
|
+
try {
|
|
302
|
+
target.postMessage({
|
|
303
|
+
v: PROTOCOL_VERSION,
|
|
304
|
+
type: "request",
|
|
305
|
+
...!establishing ? { bindingId: this.bindingId } : {},
|
|
306
|
+
id: requestId,
|
|
307
|
+
method,
|
|
308
|
+
params: effectiveParams
|
|
309
|
+
}, this.targetOrigin);
|
|
310
|
+
} catch (cause) {
|
|
311
|
+
this.deletePending(requestId);
|
|
312
|
+
const error = new KeymasterTransportError("send_failed", errorMessage(cause, "Unable to send Keymaster request"));
|
|
313
|
+
this.disconnect(error);
|
|
314
|
+
reject(error);
|
|
315
|
+
}
|
|
316
|
+
});
|
|
317
|
+
} finally {
|
|
318
|
+
if (establishing) this.establishmentActive = false;
|
|
319
|
+
}
|
|
442
320
|
}
|
|
443
321
|
/**
|
|
444
322
|
* Calls `connect.login` to create a persistent Connect session.
|
|
@@ -446,38 +324,7 @@ var KeymasterConnectClient = class {
|
|
|
446
324
|
* @group Connect
|
|
447
325
|
*/
|
|
448
326
|
login(params, options) {
|
|
449
|
-
return this.request("connect.login", params, options);
|
|
450
|
-
}
|
|
451
|
-
/**
|
|
452
|
-
* Calls `connect.resume` to restore the unlock runtime for a persistent
|
|
453
|
-
* session.
|
|
454
|
-
*
|
|
455
|
-
* 刷新恢复时只把保存的凭据和预期身份载入待恢复上下文,不直接置为已登录;
|
|
456
|
-
* 钱包验证成功、结果身份一致并取得新 binding 后才开放业务调用。
|
|
457
|
-
* 无可用凭据时直接拒绝并提示重新登录,不回退到无凭据的重建默认授权。
|
|
458
|
-
* 恢复请求携带调用方当前入口的 `appIdentity`(如有),不静默丢弃。
|
|
459
|
-
*
|
|
460
|
-
* @group Connect
|
|
461
|
-
*/
|
|
462
|
-
resume(params = {}, options) {
|
|
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);
|
|
327
|
+
return this.request("connect.login", withOwnPublicationProof(params), options);
|
|
481
328
|
}
|
|
482
329
|
/**
|
|
483
330
|
* Calls `connect.logout` to revoke a persistent Connect session.
|
|
@@ -495,7 +342,7 @@ var KeymasterConnectClient = class {
|
|
|
495
342
|
* @group Connect
|
|
496
343
|
*/
|
|
497
344
|
launch(params, options) {
|
|
498
|
-
return this.request("connect.launch", params, options);
|
|
345
|
+
return this.request("connect.launch", withOwnPublicationProof(params), options);
|
|
499
346
|
}
|
|
500
347
|
/**
|
|
501
348
|
* Calls `identity.get` to return signed identity claims for the session
|
|
@@ -561,70 +408,6 @@ var KeymasterConnectClient = class {
|
|
|
561
408
|
channelSubscriptionSet(params, options) {
|
|
562
409
|
return this.request("channel.subscription_set", params, options);
|
|
563
410
|
}
|
|
564
|
-
/**
|
|
565
|
-
* Calls `storage.list` to list paths in the verified application's namespace.
|
|
566
|
-
*
|
|
567
|
-
* @group Storage
|
|
568
|
-
*/
|
|
569
|
-
storageList(params, options) {
|
|
570
|
-
return this.request("storage.list", params, options);
|
|
571
|
-
}
|
|
572
|
-
/**
|
|
573
|
-
* Calls `storage.directory.create` to create an app-scoped directory.
|
|
574
|
-
*
|
|
575
|
-
* @group Storage
|
|
576
|
-
*/
|
|
577
|
-
storageDirectoryCreate(params, options) {
|
|
578
|
-
return this.request("storage.directory.create", params, options);
|
|
579
|
-
}
|
|
580
|
-
/**
|
|
581
|
-
* Calls `storage.directory.delete` to delete an app-scoped directory.
|
|
582
|
-
*
|
|
583
|
-
* @group Storage
|
|
584
|
-
*/
|
|
585
|
-
storageDirectoryDelete(params, options) {
|
|
586
|
-
return this.request("storage.directory.delete", params, options);
|
|
587
|
-
}
|
|
588
|
-
/**
|
|
589
|
-
* Calls `storage.put` to write an app-scoped object.
|
|
590
|
-
*
|
|
591
|
-
* @group Storage
|
|
592
|
-
*/
|
|
593
|
-
storagePut(params, options) {
|
|
594
|
-
return this.request("storage.put", params, options);
|
|
595
|
-
}
|
|
596
|
-
/**
|
|
597
|
-
* Calls `storage.get` to read an app-scoped object.
|
|
598
|
-
*
|
|
599
|
-
* @group Storage
|
|
600
|
-
*/
|
|
601
|
-
storageGet(params, options) {
|
|
602
|
-
return this.request("storage.get", params, options);
|
|
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
|
-
}
|
|
620
|
-
/**
|
|
621
|
-
* Calls `storage.delete` to delete an app-scoped object.
|
|
622
|
-
*
|
|
623
|
-
* @group Storage
|
|
624
|
-
*/
|
|
625
|
-
storageDelete(params, options) {
|
|
626
|
-
return this.request("storage.delete", params, options);
|
|
627
|
-
}
|
|
628
411
|
/**
|
|
629
412
|
* Calls `msfile.stat` to query every enabled MSFile supplier for a seed
|
|
630
413
|
* hash. The caller selects supplier/hash only; price policy stays inside
|
|
@@ -665,124 +448,37 @@ var KeymasterConnectClient = class {
|
|
|
665
448
|
if (!pending) return false;
|
|
666
449
|
this.postCancel(requestId);
|
|
667
450
|
this.deletePending(requestId);
|
|
451
|
+
if (pending.method === "connect.login" || pending.method === "connect.launch") this.disconnect(reason);
|
|
668
452
|
pending.reject(reason);
|
|
669
453
|
return true;
|
|
670
454
|
}
|
|
671
|
-
/**
|
|
672
|
-
* Tears down the local transport. This does not revoke persistent Connect
|
|
673
|
-
* sessions; call {@link logout} before closing when revocation is required.
|
|
674
|
-
*
|
|
675
|
-
* 只结束运行连接,保留刷新恢复材料:`close()`、页面卸载和运行失效都不删除
|
|
676
|
-
* 本地恢复记录;`logout` 成功、明确授权失效或用户主动清除才删除。
|
|
677
|
-
*
|
|
678
|
-
* @group Transport
|
|
679
|
-
*/
|
|
680
|
-
close() {
|
|
681
|
-
if (this.bindingId) {
|
|
682
|
-
try {
|
|
683
|
-
this.sessionWindow?.postMessage({ v: PROTOCOL_VERSION, type: "tools.register", bindingId: this.bindingId, id: this.environment.randomUUID(), tools: [] }, this.targetOrigin);
|
|
684
|
-
} catch {
|
|
685
|
-
}
|
|
686
|
-
}
|
|
687
|
-
this.aiTools.clear(new Error("Connect client closed"));
|
|
688
|
-
for (const id of this.pending.keys()) this.postCancel(id);
|
|
689
|
-
this.identityValue = null;
|
|
690
|
-
this.bindingId = null;
|
|
691
|
-
const error = new KeymasterTransportError("client_closed", "Keymaster client was closed");
|
|
692
|
-
this.rejectAll(error);
|
|
693
|
-
this.failReady(error);
|
|
694
|
-
this.stopClosePolling();
|
|
695
|
-
if (this.listenerInstalled) {
|
|
696
|
-
this.environment.removeMessageListener(this.handleMessage);
|
|
697
|
-
this.listenerInstalled = false;
|
|
698
|
-
}
|
|
699
|
-
if (!this.authorizationId) this.sessionWindow = null;
|
|
700
|
-
this.recoveryId = null;
|
|
701
|
-
this.explicitRecoveryActive = false;
|
|
702
|
-
this.explicitRecoveryProof = void 0;
|
|
703
|
-
this.recoveryReadyContinue = null;
|
|
704
|
-
this.finishRecovery(void 0, error);
|
|
705
|
-
this.setState(this.authorizationId ? "paused" : "disconnected");
|
|
706
|
-
}
|
|
707
|
-
/** 主动恢复可由 popup 重新打开钱包;后台恢复从不打开窗口或重放业务。 */
|
|
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"));
|
|
711
|
-
if (this.pending.size) return Promise.reject(new TypeError("Cannot resume while business requests are pending"));
|
|
712
|
-
this.installListener();
|
|
713
|
-
let awaitReady = false;
|
|
714
|
-
if (this.mode === "popup") {
|
|
715
|
-
if (!this.sessionWindow || isWindowClosed(this.sessionWindow)) {
|
|
716
|
-
const url = new URL(this.options.popupPath, `${this.targetOrigin}/`);
|
|
717
|
-
if (url.origin !== this.targetOrigin) return Promise.reject(new TypeError("popupPath must resolve inside targetOrigin"));
|
|
718
|
-
this.sessionWindow = this.environment.open(url.href, "_blank", "");
|
|
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;
|
|
724
|
-
}
|
|
725
|
-
} else {
|
|
726
|
-
const launcher = this.mode === "iframe" ? this.environment.parent() : this.environment.opener();
|
|
727
|
-
if (!launcher || isWindowClosed(launcher)) return Promise.reject(new KeymasterTransportError("appview_session_lost", "Wallet launcher unavailable; relaunch from the wallet"));
|
|
728
|
-
this.sessionWindow = launcher;
|
|
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
|
-
};
|
|
743
|
-
this.pause(new KeymasterTransportError("wallet_paused", "Re-establishing the original wallet connection"));
|
|
744
|
-
this.startClosePolling();
|
|
745
|
-
return new Promise((resolve, reject) => {
|
|
746
|
-
const finish = (error, result) => {
|
|
747
|
-
this.environment.clearTimeout(timer);
|
|
748
|
-
options.signal?.removeEventListener("abort", aborted);
|
|
749
|
-
this.recoveryWaiters.delete(waiter);
|
|
750
|
-
endExplicitAttempt(!!error);
|
|
751
|
-
if (error) reject(error);
|
|
752
|
-
else resolve(result);
|
|
753
|
-
};
|
|
754
|
-
const waiter = { resolve: (result) => finish(void 0, result), reject: (error) => finish(error) };
|
|
755
|
-
const aborted = () => finish(abortReason(options.signal));
|
|
756
|
-
const timer = this.environment.setTimeout(() => finish(new KeymasterTransportError("request_timeout", "Timed out restoring wallet connection")), positiveNumber(options.timeoutMs ?? this.options.readyTimeoutMs, "timeoutMs"));
|
|
757
|
-
this.recoveryWaiters.add(waiter);
|
|
758
|
-
options.signal?.addEventListener("abort", aborted, { once: true });
|
|
759
|
-
if (awaitReady) {
|
|
760
|
-
this.recoveryReadyContinue = () => this.sendRecovery(this.explicitRecoveryProof);
|
|
761
|
-
} else {
|
|
762
|
-
this.sendRecovery(this.explicitRecoveryProof);
|
|
763
|
-
}
|
|
764
|
-
});
|
|
765
|
-
}
|
|
766
455
|
connectPopup() {
|
|
767
456
|
if (this.isConnected()) return Promise.resolve();
|
|
768
457
|
if (this.readyPromise) return this.readyPromise;
|
|
769
458
|
const url = new URL(this.options.popupPath, `${this.targetOrigin}/`);
|
|
770
459
|
if (url.origin !== this.targetOrigin) return Promise.reject(new TypeError("popupPath must resolve inside targetOrigin"));
|
|
771
460
|
this.installListener();
|
|
772
|
-
|
|
773
|
-
this.sessionWindow = this.environment.open(url.href, "_blank", "");
|
|
774
|
-
if (!this.sessionWindow) {
|
|
775
|
-
this.setState("disconnected");
|
|
776
|
-
return Promise.reject(new KeymasterTransportError("popup_blocked", "Keymaster wallet window was blocked"));
|
|
777
|
-
}
|
|
778
|
-
this.sessionWindowReady = false;
|
|
779
|
-
this.startClosePolling();
|
|
780
|
-
this.readyPromise = new Promise((resolve, reject) => {
|
|
461
|
+
const ready = new Promise((resolve, reject) => {
|
|
781
462
|
this.readyResolve = resolve;
|
|
782
463
|
this.readyReject = reject;
|
|
783
464
|
this.readyTimer = this.environment.setTimeout(() => this.disconnect(new KeymasterTransportError("ready_timeout", "Timed out waiting for the wallet to initialize or unlock")), this.options.readyTimeoutMs);
|
|
784
465
|
});
|
|
785
|
-
|
|
466
|
+
this.readyPromise = ready;
|
|
467
|
+
this.setState("opening");
|
|
468
|
+
if (this.readyPromise !== ready) return ready;
|
|
469
|
+
try {
|
|
470
|
+
const target = this.environment.open(url.href, "_blank", "");
|
|
471
|
+
if (this.readyPromise !== ready) return ready;
|
|
472
|
+
if (!target) {
|
|
473
|
+
this.disconnect(new KeymasterTransportError("popup_blocked", "Keymaster wallet window was blocked"));
|
|
474
|
+
} else {
|
|
475
|
+
this.sessionWindow = target;
|
|
476
|
+
this.startClosePolling();
|
|
477
|
+
}
|
|
478
|
+
} catch (cause) {
|
|
479
|
+
this.disconnect(new KeymasterTransportError("send_failed", errorMessage(cause, "Unable to open Keymaster wallet")));
|
|
480
|
+
}
|
|
481
|
+
return ready;
|
|
786
482
|
}
|
|
787
483
|
connectAppView() {
|
|
788
484
|
if (this.isConnected()) return Promise.resolve();
|
|
@@ -818,29 +514,16 @@ var KeymasterConnectClient = class {
|
|
|
818
514
|
}
|
|
819
515
|
if (this.aiTools.handle(value)) return;
|
|
820
516
|
if (value.type === "ready") {
|
|
821
|
-
this.sessionWindowReady = true;
|
|
822
517
|
if (this.mode === "popup" && this.readyPromise) this.finishReady();
|
|
823
|
-
if (this.
|
|
824
|
-
const send = this.recoveryReadyContinue;
|
|
825
|
-
this.recoveryReadyContinue = null;
|
|
826
|
-
send();
|
|
827
|
-
}
|
|
518
|
+
else if (this.bindingId) this.disconnect(new KeymasterTransportError("wallet_closed", "Wallet runtime changed; login again"));
|
|
828
519
|
return;
|
|
829
520
|
}
|
|
830
521
|
if (value.type === "closing") {
|
|
831
|
-
|
|
832
|
-
else this.disconnect(new KeymasterTransportError("wallet_closed", "Keymaster wallet connection is closing"));
|
|
522
|
+
this.disconnect(new KeymasterTransportError("wallet_closed", "Keymaster wallet connection is closing"));
|
|
833
523
|
return;
|
|
834
524
|
}
|
|
835
525
|
if (value.type === "revoked" && value.authorizationId === this.authorizationId) {
|
|
836
|
-
const revokedId = this.authorizationId;
|
|
837
|
-
this.authorizationId = null;
|
|
838
|
-
this.authorizedIdentity = null;
|
|
839
|
-
this.authorizationVerified = false;
|
|
840
|
-
this.recoveryId = null;
|
|
841
|
-
this.dropRecoveryRecord(revokedId);
|
|
842
526
|
const revokedError = new KeymasterProtocolError("authorization_expired", "Application connection authorization was revoked");
|
|
843
|
-
this.finishRecovery(void 0, revokedError);
|
|
844
527
|
this.disconnect(revokedError);
|
|
845
528
|
return;
|
|
846
529
|
}
|
|
@@ -859,47 +542,6 @@ var KeymasterConnectClient = class {
|
|
|
859
542
|
}
|
|
860
543
|
if (value.type !== "result" || typeof value.id !== "string" || typeof value.ok !== "boolean") return;
|
|
861
544
|
const result = event.data;
|
|
862
|
-
if (this.authorizationId && result.id === this.recoveryId) {
|
|
863
|
-
if (!result.ok) {
|
|
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);
|
|
882
|
-
return;
|
|
883
|
-
}
|
|
884
|
-
try {
|
|
885
|
-
const identity = validateNetworkIdentity(value.networkIdentity);
|
|
886
|
-
const restored = result.result;
|
|
887
|
-
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");
|
|
888
|
-
if (this.bindingId !== value.bindingId) this.pause(new KeymasterTransportError("wallet_paused", "Wallet runtime was replaced"));
|
|
889
|
-
this.identityValue = Object.freeze({ ...identity });
|
|
890
|
-
this.bindingId = value.bindingId;
|
|
891
|
-
this.recoveryId = null;
|
|
892
|
-
this.authorizationVerified = true;
|
|
893
|
-
this.setState("connected");
|
|
894
|
-
this.persistRecoveryRecord(restored.authorizationId, identity);
|
|
895
|
-
this.finishRecovery(restored);
|
|
896
|
-
} catch (error) {
|
|
897
|
-
this.pause(error);
|
|
898
|
-
this.recoveryId = null;
|
|
899
|
-
this.finishRecovery(void 0, error);
|
|
900
|
-
}
|
|
901
|
-
return;
|
|
902
|
-
}
|
|
903
545
|
const pending = this.pending.get(result.id);
|
|
904
546
|
if (!pending) return;
|
|
905
547
|
this.deletePending(result.id);
|
|
@@ -908,42 +550,26 @@ var KeymasterConnectClient = class {
|
|
|
908
550
|
pending.reject(new KeymasterProtocolError("invalid_request", "Malformed Keymaster error result"));
|
|
909
551
|
return;
|
|
910
552
|
}
|
|
911
|
-
if (result.error.code === "connection_expired" || result.error.code === "authorization_expired")
|
|
912
|
-
|
|
913
|
-
else {
|
|
914
|
-
this.identityValue = null;
|
|
915
|
-
this.bindingId = null;
|
|
916
|
-
}
|
|
917
|
-
}
|
|
553
|
+
if (result.error.code === "connection_expired" || result.error.code === "authorization_expired") this.disconnect(new KeymasterProtocolError(result.error.code, result.error.message));
|
|
554
|
+
if (pending.method === "connect.login" || pending.method === "connect.launch") this.disconnect(new KeymasterProtocolError(result.error.code, result.error.message));
|
|
918
555
|
pending.reject(new KeymasterProtocolError(result.error.code, result.error.message));
|
|
919
556
|
return;
|
|
920
557
|
}
|
|
921
558
|
try {
|
|
922
559
|
const identity = validateNetworkIdentity(value.networkIdentity);
|
|
923
|
-
if (["connect.login", "connect.
|
|
560
|
+
if (["connect.login", "connect.launch"].includes(pending.method)) {
|
|
924
561
|
const connected = result.result;
|
|
925
562
|
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");
|
|
926
563
|
this.aiTools.clear(new Error("Connection binding replaced; register tools again"));
|
|
927
564
|
this.identityValue = Object.freeze({ ...identity });
|
|
928
|
-
this.authorizedIdentity = this.identityValue;
|
|
929
565
|
this.authorizationId = connected.authorizationId;
|
|
930
|
-
this.authorizationVerified = true;
|
|
931
|
-
this.recoveryId = null;
|
|
932
566
|
this.bindingId = value.bindingId;
|
|
933
|
-
this.
|
|
567
|
+
this.setState("connected");
|
|
934
568
|
} else if (!this.identityValue || value.bindingId !== this.bindingId || !sameNetworkIdentity(this.identityValue, identity)) {
|
|
935
569
|
throw new Error("Keymaster connection identity mismatch");
|
|
936
570
|
}
|
|
937
571
|
if (pending.method === "connect.logout") {
|
|
938
|
-
|
|
939
|
-
this.aiTools.clear(new Error("Logged out"));
|
|
940
|
-
this.identityValue = null;
|
|
941
|
-
this.bindingId = null;
|
|
942
|
-
this.authorizationId = null;
|
|
943
|
-
this.authorizedIdentity = null;
|
|
944
|
-
this.authorizationVerified = false;
|
|
945
|
-
this.recoveryId = null;
|
|
946
|
-
this.dropRecoveryRecord(logoutId);
|
|
572
|
+
this.disconnect(new KeymasterTransportError("client_closed", "Logged out"));
|
|
947
573
|
}
|
|
948
574
|
pending.resolve(result.result);
|
|
949
575
|
} catch (error) {
|
|
@@ -956,6 +582,7 @@ var KeymasterConnectClient = class {
|
|
|
956
582
|
installListener() {
|
|
957
583
|
if (this.listenerInstalled) return;
|
|
958
584
|
this.environment.addMessageListener(this.handleMessage);
|
|
585
|
+
this.environment.addPageHideListener?.(this.handlePageHide);
|
|
959
586
|
this.listenerInstalled = true;
|
|
960
587
|
}
|
|
961
588
|
finishReady() {
|
|
@@ -976,85 +603,30 @@ var KeymasterConnectClient = class {
|
|
|
976
603
|
this.readyResolve = null;
|
|
977
604
|
this.readyReject = null;
|
|
978
605
|
}
|
|
606
|
+
/** 仅清理本地运行态,页签关闭不发送握手或等待回复。 */
|
|
607
|
+
handlePageHide = () => this.disconnect(new KeymasterTransportError("client_closed", "Application page unloaded"));
|
|
979
608
|
disconnect(reason) {
|
|
980
609
|
this.aiTools.clear(reason);
|
|
981
|
-
for (const id of this.pending.keys()) this.postCancel(id);
|
|
982
610
|
this.identityValue = null;
|
|
983
611
|
this.bindingId = null;
|
|
984
612
|
this.rejectAll(reason);
|
|
985
613
|
this.failReady(reason);
|
|
986
614
|
this.stopClosePolling();
|
|
987
615
|
this.sessionWindow = null;
|
|
988
|
-
this.
|
|
989
|
-
this.
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
}
|
|
994
|
-
/** 暂停仅撤销瞬时绑定和旧请求,持久授权引用及原身份留作恢复校验。 */
|
|
995
|
-
pause(reason) {
|
|
996
|
-
this.aiTools.clear(reason);
|
|
997
|
-
this.identityValue = null;
|
|
998
|
-
this.bindingId = null;
|
|
999
|
-
this.rejectAll(reason);
|
|
1000
|
-
this.failReady(reason);
|
|
1001
|
-
this.setState("paused");
|
|
1002
|
-
}
|
|
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) {
|
|
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;
|
|
1037
|
-
this.recoveryId ??= this.environment.randomUUID();
|
|
1038
|
-
try {
|
|
1039
|
-
this.sessionWindow.postMessage({ v: PROTOCOL_VERSION, type: "reconnect", id: this.recoveryId, authorizationId: this.authorizationId, ...this.bindingId ? { bindingId: this.bindingId } : {}, ...appIdentity ? { appIdentity } : {} }, this.targetOrigin);
|
|
1040
|
-
} catch (error) {
|
|
1041
|
-
this.pause(error);
|
|
1042
|
-
}
|
|
1043
|
-
}
|
|
1044
|
-
finishRecovery(result, error) {
|
|
1045
|
-
for (const waiter of [...this.recoveryWaiters]) {
|
|
1046
|
-
if (error) waiter.reject(error);
|
|
1047
|
-
else if (result) waiter.resolve(result);
|
|
616
|
+
this.authorizationId = null;
|
|
617
|
+
if (this.listenerInstalled) {
|
|
618
|
+
this.environment.removeMessageListener(this.handleMessage);
|
|
619
|
+
this.environment.removePageHideListener?.(this.handlePageHide);
|
|
620
|
+
this.listenerInstalled = false;
|
|
1048
621
|
}
|
|
622
|
+
this.setState("disconnected");
|
|
1049
623
|
}
|
|
1050
624
|
startClosePolling() {
|
|
1051
625
|
this.stopClosePolling();
|
|
1052
626
|
this.closePoller = this.environment.setInterval(() => {
|
|
1053
627
|
if (this.sessionWindow && isWindowClosed(this.sessionWindow)) {
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
} else this.disconnect(new KeymasterTransportError("wallet_closed", "Keymaster parent wallet was closed"));
|
|
1057
|
-
} else if (!this.explicitRecoveryActive) this.sendRecovery();
|
|
628
|
+
this.disconnect(new KeymasterTransportError("wallet_closed", "Wallet window is closed"));
|
|
629
|
+
}
|
|
1058
630
|
}, this.options.closePollMs);
|
|
1059
631
|
}
|
|
1060
632
|
stopClosePolling() {
|
|
@@ -1108,6 +680,8 @@ function createBrowserEnvironment() {
|
|
|
1108
680
|
clearTimeout: (timer) => window.clearTimeout(timer),
|
|
1109
681
|
setInterval: (handler, intervalMs) => window.setInterval(handler, intervalMs),
|
|
1110
682
|
clearInterval: (timer) => window.clearInterval(timer),
|
|
683
|
+
addPageHideListener: (listener) => window.addEventListener("pagehide", listener),
|
|
684
|
+
removePageHideListener: (listener) => window.removeEventListener("pagehide", listener),
|
|
1111
685
|
randomUUID: () => window.crypto.randomUUID()
|
|
1112
686
|
};
|
|
1113
687
|
}
|
|
@@ -1135,7 +709,12 @@ function abortReason(signal) {
|
|
|
1135
709
|
function errorMessage(value, fallback) {
|
|
1136
710
|
return value instanceof Error && value.message ? value.message : fallback;
|
|
1137
711
|
}
|
|
712
|
+
function withOwnPublicationProof(params) {
|
|
713
|
+
if ("publicationProof" in params && params.publicationProof !== void 0) return params;
|
|
714
|
+
const proof = readOwnPublicationProof();
|
|
715
|
+
return proof ? { ...params, publicationProof: proof } : params;
|
|
716
|
+
}
|
|
1138
717
|
|
|
1139
|
-
export { KeymasterConnectClient, KeymasterProtocolError, KeymasterTransportError,
|
|
718
|
+
export { KeymasterConnectClient, KeymasterProtocolError, KeymasterTransportError, binary, binaryBytes, binaryText, binaryToText, createBrowserEnvironment, createKeymasterConnect };
|
|
1140
719
|
//# sourceMappingURL=index.js.map
|
|
1141
720
|
//# sourceMappingURL=index.js.map
|