@onekeyfe/hwk-keystone-adapter 1.2.8-alpha.117 → 1.2.8-alpha.119
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/dist/index.d.mts +18 -5
- package/dist/index.d.ts +18 -5
- package/dist/index.js +327 -72
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +333 -72
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -47,6 +47,8 @@ module.exports = __toCommonJS(index_exports);
|
|
|
47
47
|
|
|
48
48
|
// src/urEngine/KeystoneUrEngine.ts
|
|
49
49
|
var import_bc_ur_registry_eth = require("@keystonehq/bc-ur-registry-eth");
|
|
50
|
+
var import_bc_ur_registry3 = require("@keystonehq/bc-ur-registry");
|
|
51
|
+
var import_ScriptExpression = require("@keystonehq/bc-ur-registry/dist/ScriptExpression.js");
|
|
50
52
|
var import_keystone_sdk = require("@keystonehq/keystone-sdk");
|
|
51
53
|
var import_bs58check = __toESM(require("bs58check"));
|
|
52
54
|
var bitcoin = __toESM(require("bitcoinjs-lib"));
|
|
@@ -276,6 +278,93 @@ var KeystoneUrEngine = class {
|
|
|
276
278
|
}
|
|
277
279
|
return this.parseMultiAccounts(ur);
|
|
278
280
|
}
|
|
281
|
+
/** Device exports are opt-in: request responses retain their existing format/path checks. */
|
|
282
|
+
parseAccountExport(ur) {
|
|
283
|
+
try {
|
|
284
|
+
const { cbor } = toSdkUr(ur);
|
|
285
|
+
let parsed;
|
|
286
|
+
let keys;
|
|
287
|
+
if (ur.urType === "crypto-account") {
|
|
288
|
+
if (!Object.values(import_bc_ur_registry3.ScriptExpressions).some((script) => script.getTag() === 409)) {
|
|
289
|
+
Object.assign(import_bc_ur_registry3.ScriptExpressions, { TAPROOT: new import_ScriptExpression.ScriptExpression(409, "tr") });
|
|
290
|
+
}
|
|
291
|
+
import_bc_ur_registry3.extend.cbor.patchTags([409]);
|
|
292
|
+
const account = import_bc_ur_registry3.CryptoAccount.fromCBOR(cbor);
|
|
293
|
+
keys = account.getOutputDescriptors().map((output) => {
|
|
294
|
+
const key = output.getHDKey();
|
|
295
|
+
const path = key?.getOrigin()?.getPath();
|
|
296
|
+
const script = output.getScriptExpressions().map((item) => item.getExpression()).join("/");
|
|
297
|
+
const validScript = path?.startsWith("44'/0'/") && script === "pkh" || path?.startsWith("49'/0'/") && script === "sh/wpkh" || path?.startsWith("84'/0'/") && script === "wpkh" || path?.startsWith("86'/0'/") && script === "tr";
|
|
298
|
+
const children = key?.getChildren();
|
|
299
|
+
if (!key || !validScript || children && children.getPath() !== "0/*") {
|
|
300
|
+
throw new Error("Unsupported account descriptor");
|
|
301
|
+
}
|
|
302
|
+
return key;
|
|
303
|
+
});
|
|
304
|
+
const result = this.sdk.parseAccount(toSdkUr(ur));
|
|
305
|
+
parsed = {
|
|
306
|
+
masterFingerprint: requireBip32MasterFingerprint(result.masterFingerprint),
|
|
307
|
+
accounts: result.keys.map(toParsedAccount)
|
|
308
|
+
};
|
|
309
|
+
} else if (ur.urType === "crypto-multi-accounts") {
|
|
310
|
+
keys = import_bc_ur_registry3.CryptoMultiAccounts.fromCBOR(cbor).getKeys();
|
|
311
|
+
parsed = this.parseMultiAccounts(ur);
|
|
312
|
+
} else if (ur.urType === "crypto-hdkey") {
|
|
313
|
+
keys = [import_bc_ur_registry3.CryptoHDKey.fromCBOR(cbor)];
|
|
314
|
+
parsed = this.parseAccountResponse(ur);
|
|
315
|
+
} else {
|
|
316
|
+
throw new Error("Unsupported account export type");
|
|
317
|
+
}
|
|
318
|
+
if (!keys.length || keys.length !== parsed.accounts.length) {
|
|
319
|
+
throw new Error("Empty account export");
|
|
320
|
+
}
|
|
321
|
+
for (const key of keys) {
|
|
322
|
+
if (key.isMaster() || key.isPrivateKey()) throw new Error("Account public keys required");
|
|
323
|
+
const source = key.getOrigin()?.getSourceFingerprint();
|
|
324
|
+
if (source && source.toString("hex") !== parsed.masterFingerprint) {
|
|
325
|
+
throw (0, import_hwk_adapter_core.createHwkError)({
|
|
326
|
+
code: import_hwk_adapter_core.HardwareErrorCode.DeviceMismatch,
|
|
327
|
+
message: "Account export contains a different wallet fingerprint"
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
const byPath = /* @__PURE__ */ new Map();
|
|
332
|
+
for (const account of parsed.accounts) {
|
|
333
|
+
if (!/^m(?:\/\d+')+(?:\/\d+)*$/.test(account.path) || account.path.slice(2).split("/").some((segment) => Number(segment.replace("'", "")) >= BIP32_HARDENED_OFFSET)) {
|
|
334
|
+
throw new Error("Account export has an unsupported derivation path");
|
|
335
|
+
}
|
|
336
|
+
let identity = account.publicKey;
|
|
337
|
+
if (account.extendedPublicKey) {
|
|
338
|
+
const node = import_hdkey.default.fromExtendedKey(account.extendedPublicKey);
|
|
339
|
+
if (node.privateKey || !node.publicKey || !node.chainCode) {
|
|
340
|
+
throw new Error("Account public keys required");
|
|
341
|
+
}
|
|
342
|
+
if (node.publicKey.toString("hex") !== account.publicKey.toLowerCase()) {
|
|
343
|
+
throw new Error("Account public key does not match its extended key");
|
|
344
|
+
}
|
|
345
|
+
identity = `${identity}:${node.chainCode.toString("hex")}`;
|
|
346
|
+
} else if (!/^[0-9a-f]{64}$/i.test(account.publicKey)) {
|
|
347
|
+
throw new Error("Account export is missing a usable public key");
|
|
348
|
+
}
|
|
349
|
+
const previous = byPath.get(account.path);
|
|
350
|
+
if (previous !== void 0 && previous !== identity) {
|
|
351
|
+
throw (0, import_hwk_adapter_core.createHwkError)({
|
|
352
|
+
code: import_hwk_adapter_core.HardwareErrorCode.DeviceMismatch,
|
|
353
|
+
message: "Account export contains conflicting keys for the same path"
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
byPath.set(account.path, identity);
|
|
357
|
+
}
|
|
358
|
+
return parsed;
|
|
359
|
+
} catch (error) {
|
|
360
|
+
if (error && typeof error === "object" && "code" in error && error.code === import_hwk_adapter_core.HardwareErrorCode.DeviceMismatch)
|
|
361
|
+
throw error;
|
|
362
|
+
throw (0, import_hwk_adapter_core.createHwkError)({
|
|
363
|
+
code: import_hwk_adapter_core.HardwareErrorCode.InvalidParams,
|
|
364
|
+
message: "Unable to read account export; scan a supported Keystone account export code"
|
|
365
|
+
});
|
|
366
|
+
}
|
|
367
|
+
}
|
|
279
368
|
// --- EVM ---
|
|
280
369
|
buildEthSignRequest(input) {
|
|
281
370
|
const ur = this.sdk.eth.generateSignRequest({
|
|
@@ -464,6 +553,31 @@ var import_bs58 = __toESM(require("bs58"));
|
|
|
464
553
|
var import_uuid2 = require("uuid");
|
|
465
554
|
var import_hwk_adapter_core3 = require("@onekeyfe/hwk-adapter-core");
|
|
466
555
|
|
|
556
|
+
// src/adapter/firmwareCapabilities.ts
|
|
557
|
+
var MULTI_COIN = {
|
|
558
|
+
variant: "multi-coin",
|
|
559
|
+
supportedChains: ["btc", "evm", "sol", "tron"]
|
|
560
|
+
};
|
|
561
|
+
var BITCOIN_ONLY = {
|
|
562
|
+
variant: "bitcoin-only",
|
|
563
|
+
supportedChains: ["btc"]
|
|
564
|
+
};
|
|
565
|
+
var CYPHERPUNK = {
|
|
566
|
+
variant: "cypherpunk",
|
|
567
|
+
// Firmware support alone is insufficient: this adapter has no Zcash/Monero methods yet.
|
|
568
|
+
supportedChains: ["btc"]
|
|
569
|
+
};
|
|
570
|
+
function getKeystoneFirmwareProfile(firmwareVersion) {
|
|
571
|
+
if (!firmwareVersion || firmwareVersion === "0.0.0") return void 0;
|
|
572
|
+
const version = /^\d{1,4}(?:\.\d{1,4}){1,3}(?:-([a-z0-9.-]{1,40}))?$/i.exec(firmwareVersion);
|
|
573
|
+
if (!version) return void 0;
|
|
574
|
+
const suffix = version[1]?.toUpperCase();
|
|
575
|
+
if (!suffix) return MULTI_COIN;
|
|
576
|
+
if (suffix === "C" || suffix === "CYPHERPUNK" || suffix === "CYPHERPUNK-B") return CYPHERPUNK;
|
|
577
|
+
if (suffix === "B" || suffix === "BTC" || suffix === "BTC-ONLY") return BITCOIN_ONLY;
|
|
578
|
+
return void 0;
|
|
579
|
+
}
|
|
580
|
+
|
|
467
581
|
// src/adapter/pathUtils.ts
|
|
468
582
|
function normalizePath(path) {
|
|
469
583
|
const trimmed = path.trim();
|
|
@@ -530,12 +644,16 @@ function accountKey(hwkChain, path) {
|
|
|
530
644
|
function createDeviceRecord(masterFingerprint) {
|
|
531
645
|
return {
|
|
532
646
|
masterFingerprint,
|
|
533
|
-
connectId: walletConnectId(masterFingerprint)
|
|
534
|
-
importedAt: Date.now()
|
|
647
|
+
connectId: walletConnectId(masterFingerprint)
|
|
535
648
|
};
|
|
536
649
|
}
|
|
537
650
|
var CAPABILITIES = { persistentDeviceIdentity: true };
|
|
538
651
|
function toDeviceInfo(record) {
|
|
652
|
+
return toDeviceInfoForConnection(record, record.usbSessionId ? "usb" : "qr");
|
|
653
|
+
}
|
|
654
|
+
function toDeviceInfoForConnection(record, connectionType) {
|
|
655
|
+
const firmwareVersion = connectionType === "usb" ? record.usbFirmwareVersion : record.deviceVersion;
|
|
656
|
+
const firmware = connectionType === "usb" ? getKeystoneFirmwareProfile(firmwareVersion) : void 0;
|
|
539
657
|
let availableChannels = ["qr"];
|
|
540
658
|
if (record.usbSessionId) {
|
|
541
659
|
availableChannels = record.qrSynced ? ["qr", "usb"] : ["usb"];
|
|
@@ -544,11 +662,14 @@ function toDeviceInfo(record) {
|
|
|
544
662
|
vendor: "keystone",
|
|
545
663
|
model: record.model ?? "unknown",
|
|
546
664
|
modelName: record.model,
|
|
547
|
-
firmwareVersion:
|
|
665
|
+
firmwareVersion: firmwareVersion ?? "0.0.0",
|
|
548
666
|
deviceId: record.masterFingerprint,
|
|
549
667
|
connectId: record.connectId,
|
|
550
|
-
connectionType
|
|
551
|
-
capabilities:
|
|
668
|
+
connectionType,
|
|
669
|
+
capabilities: {
|
|
670
|
+
...CAPABILITIES,
|
|
671
|
+
...firmware ? { supportedChains: [...firmware.supportedChains] } : {}
|
|
672
|
+
},
|
|
552
673
|
raw: {
|
|
553
674
|
availableChannels,
|
|
554
675
|
masterFingerprint: record.masterFingerprint,
|
|
@@ -607,6 +728,7 @@ function keystoneBtcParamsFailure(params) {
|
|
|
607
728
|
}
|
|
608
729
|
return void 0;
|
|
609
730
|
}
|
|
731
|
+
var KEYSTONE_MAX_KEY_DERIVATION_SCHEMAS = 24;
|
|
610
732
|
var KEYSTONE_USB_REATTACH_PROBE_ATTEMPTS = 4;
|
|
611
733
|
var KEYSTONE_USB_REATTACH_PROBE_INTERVAL_MS = 500;
|
|
612
734
|
var CHAIN_TYPE_BY_CHAIN = {
|
|
@@ -654,12 +776,13 @@ function exactHex(value) {
|
|
|
654
776
|
function invalidHexFailure(field) {
|
|
655
777
|
return (0, import_hwk_adapter_core3.failure)(import_hwk_adapter_core3.HardwareErrorCode.InvalidParams, `${field} must be an even-length hex string`);
|
|
656
778
|
}
|
|
657
|
-
function requireExportedPath(parsed, path) {
|
|
779
|
+
function requireExportedPath(parsed, path, accountExport = false) {
|
|
658
780
|
const account = parsed.accounts.find((returned) => normalizePath(returned.path) === path);
|
|
659
781
|
if (!account) {
|
|
660
782
|
throw (0, import_hwk_adapter_core3.createHwkError)({
|
|
661
|
-
code: import_hwk_adapter_core3.HardwareErrorCode.DeviceMismatch,
|
|
662
|
-
message: `Keystone did not return the requested derivation path (${path})
|
|
783
|
+
code: accountExport ? import_hwk_adapter_core3.HardwareErrorCode.DevicePathForbidden : import_hwk_adapter_core3.HardwareErrorCode.DeviceMismatch,
|
|
784
|
+
message: `Keystone did not return the requested derivation path (${path})`,
|
|
785
|
+
...accountExport ? { params: { reason: "account-not-exported" } } : {}
|
|
663
786
|
});
|
|
664
787
|
}
|
|
665
788
|
return account;
|
|
@@ -673,7 +796,9 @@ function coinTypeFailure(chain, path) {
|
|
|
673
796
|
}
|
|
674
797
|
function btcAccountFailure(accountPath) {
|
|
675
798
|
if (isKeystoneSignableBtcAccountPath(accountPath)) return void 0;
|
|
676
|
-
return (0, import_hwk_adapter_core3.failure)(import_hwk_adapter_core3.HardwareErrorCode.DevicePathForbidden, KEYSTONE_BTC_ACCOUNT_FORBIDDEN_MESSAGE
|
|
799
|
+
return (0, import_hwk_adapter_core3.failure)(import_hwk_adapter_core3.HardwareErrorCode.DevicePathForbidden, KEYSTONE_BTC_ACCOUNT_FORBIDDEN_MESSAGE, {
|
|
800
|
+
reason: "btc-account-limit"
|
|
801
|
+
});
|
|
677
802
|
}
|
|
678
803
|
function missingParamFailure(method, param) {
|
|
679
804
|
return (0, import_hwk_adapter_core3.failure)(import_hwk_adapter_core3.HardwareErrorCode.InvalidParams, `${method} requires ${param}`);
|
|
@@ -705,11 +830,8 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
705
830
|
params: { operationId: operation.operationId, reason }
|
|
706
831
|
});
|
|
707
832
|
const queueKey = keystoneQueueKey(operation.operationId);
|
|
708
|
-
const ownsRunningCall = this._jobQueue.getActiveJob()?.deviceId === queueKey;
|
|
709
833
|
this._jobQueue.cancelActiveAndPending(queueKey, ended);
|
|
710
|
-
|
|
711
|
-
this._uiRegistry.cancel(void 0, void 0, operation.operationId);
|
|
712
|
-
}
|
|
834
|
+
this._uiRegistry.cancel(void 0, void 0, operation.operationId);
|
|
713
835
|
}
|
|
714
836
|
}
|
|
715
837
|
this.emitter.emit(import_hwk_adapter_core3.SDK.OPERATION_ENDED, {
|
|
@@ -736,6 +858,7 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
736
858
|
if (!record) return;
|
|
737
859
|
this._operations.endByConnectionKey(connectId, "disconnect");
|
|
738
860
|
record.usbSessionId = void 0;
|
|
861
|
+
record.usbFirmwareVersion = void 0;
|
|
739
862
|
const info = toDeviceInfo(record);
|
|
740
863
|
if (record.qrSynced) {
|
|
741
864
|
this.emitter.emit(import_hwk_adapter_core3.DEVICE.CHANGED, { type: import_hwk_adapter_core3.DEVICE.CHANGED, payload: info });
|
|
@@ -745,7 +868,8 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
745
868
|
}
|
|
746
869
|
};
|
|
747
870
|
this._handleUsbUiEvent = (event) => {
|
|
748
|
-
this.
|
|
871
|
+
const operationId = this._activeOperationId();
|
|
872
|
+
this.emitter.emit("ui-event", operationId ? { ...event, operationId } : event);
|
|
749
873
|
};
|
|
750
874
|
// ---------------------------------------------------------------------------
|
|
751
875
|
// All-network bundle, dispatched to the per-chain methods below.
|
|
@@ -818,6 +942,7 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
818
942
|
success: true,
|
|
819
943
|
payload: {
|
|
820
944
|
...context.payload,
|
|
945
|
+
...access?.channel === "qr" && access.accountExport ? { accountExport: true } : {},
|
|
821
946
|
rootFingerprint: record ? Number.parseInt(record.masterFingerprint, 16) : void 0,
|
|
822
947
|
deviceIdentity: record ? {
|
|
823
948
|
vendor: "keystone",
|
|
@@ -1067,23 +1192,24 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
1067
1192
|
}
|
|
1068
1193
|
getDeviceInfo(connectIdOrOperationId, deviceId) {
|
|
1069
1194
|
try {
|
|
1070
|
-
const
|
|
1195
|
+
const operation = (0, import_hwk_adapter_core3.isHardwareOperationId)(connectIdOrOperationId) ? this._operations.resolve(connectIdOrOperationId) : void 0;
|
|
1196
|
+
const connectId = operation?.connectId ?? connectIdOrOperationId;
|
|
1071
1197
|
const { record } = this._resolveTarget(connectId, deviceId);
|
|
1072
1198
|
if (!record) {
|
|
1073
1199
|
return Promise.resolve(
|
|
1074
1200
|
(0, import_hwk_adapter_core3.failure)(import_hwk_adapter_core3.HardwareErrorCode.DeviceNotFound, "Unknown Keystone device")
|
|
1075
1201
|
);
|
|
1076
1202
|
}
|
|
1077
|
-
|
|
1203
|
+
const info = operation ? toDeviceInfoForConnection(record, operation.connectionType === "qr" ? "qr" : "usb") : toDeviceInfo(record);
|
|
1204
|
+
return Promise.resolve((0, import_hwk_adapter_core3.success)(info));
|
|
1078
1205
|
} catch (err) {
|
|
1079
1206
|
return Promise.resolve(this._errorToFailure(err));
|
|
1080
1207
|
}
|
|
1081
1208
|
}
|
|
1082
1209
|
cancel(connectId) {
|
|
1083
|
-
let namedOperationConnectId;
|
|
1084
1210
|
if ((0, import_hwk_adapter_core3.isHardwareOperationId)(connectId)) {
|
|
1085
1211
|
try {
|
|
1086
|
-
|
|
1212
|
+
this._operations.resolve(connectId);
|
|
1087
1213
|
} catch {
|
|
1088
1214
|
return;
|
|
1089
1215
|
}
|
|
@@ -1093,15 +1219,14 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
1093
1219
|
message: "User aborted operation"
|
|
1094
1220
|
});
|
|
1095
1221
|
if (connectId) {
|
|
1222
|
+
const scopedOperationId = (0, import_hwk_adapter_core3.isHardwareOperationId)(connectId) ? connectId : this._operationRouteForIdentifier(connectId)?.operationId;
|
|
1096
1223
|
const queueKeys = keystoneCancelQueueKeys([
|
|
1097
1224
|
connectId,
|
|
1098
|
-
this._operationRoutes.get(connectId)?.operationId
|
|
1099
|
-
namedOperationConnectId
|
|
1225
|
+
this._operationRoutes.get(connectId)?.operationId
|
|
1100
1226
|
]);
|
|
1101
1227
|
for (const key of queueKeys) {
|
|
1102
1228
|
this._jobQueue.cancelActiveAndPending(key, reason);
|
|
1103
1229
|
}
|
|
1104
|
-
const scopedOperationId = (0, import_hwk_adapter_core3.isHardwareOperationId)(connectId) ? connectId : this._operationRouteForIdentifier(connectId)?.operationId;
|
|
1105
1230
|
if (scopedOperationId) {
|
|
1106
1231
|
this._uiRegistry.cancel(void 0, void 0, scopedOperationId);
|
|
1107
1232
|
}
|
|
@@ -1163,13 +1288,13 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
1163
1288
|
});
|
|
1164
1289
|
responseUr = await this._requestQrAndAwaitResponse(
|
|
1165
1290
|
displayDevice,
|
|
1166
|
-
{ ...requestUr, animated: false },
|
|
1291
|
+
{ ...requestUr, animated: false, allowAccountExport: true },
|
|
1167
1292
|
signal
|
|
1168
1293
|
);
|
|
1169
1294
|
}
|
|
1170
1295
|
_KeystoneAdapter._throwIfAborted(signal);
|
|
1171
|
-
const parsed = this.urEngine.parseAccountResponse(responseUr);
|
|
1172
|
-
if (
|
|
1296
|
+
const parsed = responseUr.accountExport ? this.urEngine.parseAccountExport(responseUr) : this.urEngine.parseAccountResponse(responseUr);
|
|
1297
|
+
if (!responseUr.accountExport) requireExportedPath(parsed, KEYSTONE_COLD_START_PATH);
|
|
1173
1298
|
const record = this._upsertDeviceRecord(parsed);
|
|
1174
1299
|
return (0, import_hwk_adapter_core3.success)(toDeviceInfo(record));
|
|
1175
1300
|
});
|
|
@@ -1366,6 +1491,7 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
1366
1491
|
true,
|
|
1367
1492
|
connectId,
|
|
1368
1493
|
signal,
|
|
1494
|
+
"btc",
|
|
1369
1495
|
"btcSignPsbt"
|
|
1370
1496
|
);
|
|
1371
1497
|
_KeystoneAdapter._throwIfAborted(signal);
|
|
@@ -1713,6 +1839,7 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
1713
1839
|
true,
|
|
1714
1840
|
connectId,
|
|
1715
1841
|
signal,
|
|
1842
|
+
args.chain,
|
|
1716
1843
|
args.operationName
|
|
1717
1844
|
);
|
|
1718
1845
|
_KeystoneAdapter._throwIfAborted(signal);
|
|
@@ -1766,7 +1893,7 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
1766
1893
|
}
|
|
1767
1894
|
if (this._forcedTransport !== "qr" && !routedToQr && record?.usbSessionId) {
|
|
1768
1895
|
return {
|
|
1769
|
-
access: { channel: "usb", usbSessionId: record.usbSessionId },
|
|
1896
|
+
access: { channel: "usb", usbSessionId: record.usbSessionId, qrFallback: {} },
|
|
1770
1897
|
masterFingerprint: record.masterFingerprint
|
|
1771
1898
|
};
|
|
1772
1899
|
}
|
|
@@ -1777,26 +1904,62 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
1777
1904
|
});
|
|
1778
1905
|
}
|
|
1779
1906
|
const schemas = Array.from(requestedByKey.values());
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
|
|
1783
|
-
|
|
1907
|
+
if (schemas.length > KEYSTONE_MAX_KEY_DERIVATION_SCHEMAS) {
|
|
1908
|
+
throw (0, import_hwk_adapter_core3.createHwkError)({
|
|
1909
|
+
code: import_hwk_adapter_core3.HardwareErrorCode.QrRequestTooLarge,
|
|
1910
|
+
message: `Keystone accepts at most ${KEYSTONE_MAX_KEY_DERIVATION_SCHEMAS} account paths per QR request; this one needs ${schemas.length}`,
|
|
1911
|
+
params: { limit: KEYSTONE_MAX_KEY_DERIVATION_SCHEMAS, requested: schemas.length }
|
|
1912
|
+
});
|
|
1913
|
+
}
|
|
1914
|
+
const request = {
|
|
1915
|
+
...this.urEngine.buildKeyDerivationRequest({
|
|
1916
|
+
schemas: schemas.map((schema) => keySchema(schema.hwkChain, schema.path)),
|
|
1917
|
+
origin: this._origin
|
|
1918
|
+
}),
|
|
1919
|
+
animated: false,
|
|
1920
|
+
allowAccountExport: true
|
|
1921
|
+
};
|
|
1784
1922
|
const responseUr = await this._requestQrAndAwaitResponse(
|
|
1785
1923
|
record ? toDeviceInfo(record) : placeholderDeviceInfo(),
|
|
1786
|
-
|
|
1924
|
+
request,
|
|
1787
1925
|
signal
|
|
1788
1926
|
);
|
|
1789
1927
|
_KeystoneAdapter._throwIfAborted(signal);
|
|
1790
|
-
const parsed = this.urEngine.parseAccountResponse(responseUr);
|
|
1928
|
+
const parsed = responseUr.accountExport ? this.urEngine.parseAccountExport(responseUr) : this.urEngine.parseAccountResponse(responseUr);
|
|
1791
1929
|
this._assertParsedIdentity(parsed, target);
|
|
1930
|
+
const firmware = getKeystoneFirmwareProfile(parsed.deviceVersion);
|
|
1931
|
+
if (responseUr.accountExport && parsed.accounts.some((account) => !isBip32Path(account.path))) {
|
|
1932
|
+
throw (0, import_hwk_adapter_core3.createHwkError)({
|
|
1933
|
+
code: import_hwk_adapter_core3.HardwareErrorCode.InvalidParams,
|
|
1934
|
+
message: "Account export contains an invalid derivation path"
|
|
1935
|
+
});
|
|
1936
|
+
}
|
|
1792
1937
|
for (const schema of schemas) {
|
|
1793
|
-
|
|
1794
|
-
|
|
1795
|
-
hwkChain
|
|
1938
|
+
const exported = responseUr.accountExport ? parsed.accounts.find((account) => normalizePath(account.path) === schema.path) : requireExportedPath(parsed, schema.path);
|
|
1939
|
+
if (exported && (!responseUr.accountExport || !firmware || firmware.supportedChains.includes(schema.hwkChain))) {
|
|
1940
|
+
scanned.set(accountKey(schema.hwkChain, schema.path), {
|
|
1941
|
+
...exported,
|
|
1942
|
+
hwkChain: schema.hwkChain
|
|
1943
|
+
});
|
|
1944
|
+
}
|
|
1945
|
+
}
|
|
1946
|
+
if (responseUr.accountExport && !scanned.size) {
|
|
1947
|
+
throw (0, import_hwk_adapter_core3.createHwkError)({
|
|
1948
|
+
code: import_hwk_adapter_core3.HardwareErrorCode.DevicePathForbidden,
|
|
1949
|
+
message: "Account export contains none of the requested supported account paths",
|
|
1950
|
+
params: { reason: "account-not-exported" }
|
|
1796
1951
|
});
|
|
1797
1952
|
}
|
|
1798
1953
|
record = this._upsertDeviceRecord(parsed);
|
|
1799
|
-
return {
|
|
1954
|
+
return {
|
|
1955
|
+
access: {
|
|
1956
|
+
channel: "qr",
|
|
1957
|
+
scanned,
|
|
1958
|
+
firmwareVersion: parsed.deviceVersion,
|
|
1959
|
+
...responseUr.accountExport ? { accountExport: true } : {}
|
|
1960
|
+
},
|
|
1961
|
+
masterFingerprint: record.masterFingerprint
|
|
1962
|
+
};
|
|
1800
1963
|
});
|
|
1801
1964
|
}
|
|
1802
1965
|
/**
|
|
@@ -1870,11 +2033,17 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
1870
2033
|
record = createDeviceRecord(masterFingerprint);
|
|
1871
2034
|
this._devices.set(masterFingerprint, record);
|
|
1872
2035
|
}
|
|
1873
|
-
record.model = parsed.device
|
|
1874
|
-
record.
|
|
1875
|
-
|
|
1876
|
-
|
|
1877
|
-
|
|
2036
|
+
record.model = parsed.device;
|
|
2037
|
+
record.hardwareDeviceId = parsed.deviceId;
|
|
2038
|
+
if (options?.usbSessionId) {
|
|
2039
|
+
record.usbSessionId = options.usbSessionId;
|
|
2040
|
+
record.usbFirmwareVersion = options.usbFirmwareVersion;
|
|
2041
|
+
record.hadUsbSession = true;
|
|
2042
|
+
}
|
|
2043
|
+
if (!options?.viaUsb) {
|
|
2044
|
+
record.deviceVersion = parsed.deviceVersion;
|
|
2045
|
+
record.qrSynced = true;
|
|
2046
|
+
}
|
|
1878
2047
|
const info = toDeviceInfo(record);
|
|
1879
2048
|
const eventType = isNew ? import_hwk_adapter_core3.DEVICE.CONNECT : import_hwk_adapter_core3.DEVICE.CHANGED;
|
|
1880
2049
|
this.emitter.emit(eventType, { type: eventType, payload: info });
|
|
@@ -1958,16 +2127,18 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
1958
2127
|
}
|
|
1959
2128
|
_KeystoneAdapter._throwIfAborted(signal);
|
|
1960
2129
|
const record = this._upsertDeviceRecord(
|
|
1961
|
-
{
|
|
2130
|
+
{
|
|
2131
|
+
masterFingerprint,
|
|
2132
|
+
accounts: [],
|
|
2133
|
+
device: session.deviceInfo.modelName ?? session.deviceInfo.model
|
|
2134
|
+
},
|
|
1962
2135
|
{
|
|
1963
2136
|
viaUsb: true,
|
|
1964
|
-
usbSessionId: session.sessionId
|
|
2137
|
+
usbSessionId: session.sessionId,
|
|
2138
|
+
usbFirmwareVersion: session.deviceInfo.firmwareVersion
|
|
1965
2139
|
}
|
|
1966
2140
|
);
|
|
1967
2141
|
_KeystoneAdapter._throwIfAborted(signal);
|
|
1968
|
-
record.hadUsbSession = true;
|
|
1969
|
-
record.model = session.deviceInfo.modelName ?? session.deviceInfo.model ?? record.model;
|
|
1970
|
-
record.deviceVersion = session.deviceInfo.firmwareVersion ?? record.deviceVersion;
|
|
1971
2142
|
return (0, import_hwk_adapter_core3.success)(toDeviceInfo(record));
|
|
1972
2143
|
} catch (err) {
|
|
1973
2144
|
if (sessionId) {
|
|
@@ -2037,7 +2208,7 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
2037
2208
|
* Decides QR vs USB for one UR round trip. A pinned 'usb' with no live session reattaches once
|
|
2038
2209
|
* and fails closed if that fails; otherwise a failed USB reattach leaves the call on QR.
|
|
2039
2210
|
*/
|
|
2040
|
-
async _resolveUr(record, requestUr, animated, operationId, signal, operationName) {
|
|
2211
|
+
async _resolveUr(record, requestUr, animated, operationId, signal, chain, operationName, qrFallback) {
|
|
2041
2212
|
const releaseOperationRetention = (0, import_hwk_adapter_core3.isHardwareOperationId)(operationId) ? this._operations.retain(operationId) : void 0;
|
|
2042
2213
|
try {
|
|
2043
2214
|
return await this._resolveUrWithRoute(
|
|
@@ -2046,13 +2217,15 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
2046
2217
|
animated,
|
|
2047
2218
|
operationId,
|
|
2048
2219
|
signal,
|
|
2049
|
-
|
|
2220
|
+
chain,
|
|
2221
|
+
operationName,
|
|
2222
|
+
qrFallback
|
|
2050
2223
|
);
|
|
2051
2224
|
} finally {
|
|
2052
2225
|
releaseOperationRetention?.();
|
|
2053
2226
|
}
|
|
2054
2227
|
}
|
|
2055
|
-
async _resolveUrWithRoute(callerRecord, requestUr, animated, operationId, signal, operationName) {
|
|
2228
|
+
async _resolveUrWithRoute(callerRecord, requestUr, animated, operationId, signal, chain, operationName, qrFallback) {
|
|
2056
2229
|
let record = callerRecord;
|
|
2057
2230
|
let interactionRoute;
|
|
2058
2231
|
if ((0, import_hwk_adapter_core3.isHardwareOperationId)(operationId)) {
|
|
@@ -2085,19 +2258,37 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
2085
2258
|
message: "USB channel is not connected for this Keystone wallet"
|
|
2086
2259
|
});
|
|
2087
2260
|
}
|
|
2261
|
+
this._assertChainSupported(record.usbFirmwareVersion, chain);
|
|
2088
2262
|
const result = await this._callUsbResolveUr(
|
|
2089
2263
|
this._usbConnector,
|
|
2090
2264
|
record.usbSessionId,
|
|
2091
2265
|
requestUr,
|
|
2092
2266
|
signal
|
|
2093
2267
|
);
|
|
2094
|
-
if (result.success)
|
|
2268
|
+
if (result.success) {
|
|
2269
|
+
const ur = result.payload;
|
|
2270
|
+
return { urType: ur.urType, urData: ur.urData };
|
|
2271
|
+
}
|
|
2095
2272
|
const usbError = (0, import_hwk_adapter_core3.rehydrateConnectorError)(result.error);
|
|
2096
2273
|
const { origin: usbErrorOrigin, code: usbErrorCode } = usbError;
|
|
2097
2274
|
if (usbErrorCode === import_hwk_adapter_core3.HardwareErrorCode.PayloadTooLarge && usbErrorOrigin !== "device") {
|
|
2098
2275
|
if (!interactionRoute && !this._forcedTransport) {
|
|
2276
|
+
if (!await this._confirmQrFallback(signal, qrFallback)) {
|
|
2277
|
+
throw (0, import_hwk_adapter_core3.createHwkError)({
|
|
2278
|
+
code: import_hwk_adapter_core3.HardwareErrorCode.UserAborted,
|
|
2279
|
+
message: "User declined to show the request too large for USB as a QR code"
|
|
2280
|
+
});
|
|
2281
|
+
}
|
|
2099
2282
|
const displayDevice2 = toDeviceInfo(record);
|
|
2100
|
-
return this._requestQrAndAwaitResponse(
|
|
2283
|
+
return this._requestQrAndAwaitResponse(
|
|
2284
|
+
displayDevice2,
|
|
2285
|
+
{
|
|
2286
|
+
...requestUr,
|
|
2287
|
+
animated,
|
|
2288
|
+
...!operationName ? { allowAccountExport: true } : {}
|
|
2289
|
+
},
|
|
2290
|
+
signal
|
|
2291
|
+
);
|
|
2101
2292
|
}
|
|
2102
2293
|
throw usbError;
|
|
2103
2294
|
}
|
|
@@ -2108,6 +2299,7 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
2108
2299
|
this._usbConnector?.disconnect(lostSessionId).catch(() => void 0);
|
|
2109
2300
|
}
|
|
2110
2301
|
record.usbSessionId = void 0;
|
|
2302
|
+
record.usbFirmwareVersion = void 0;
|
|
2111
2303
|
this.emitter.emit(import_hwk_adapter_core3.DEVICE.CHANGED, { type: import_hwk_adapter_core3.DEVICE.CHANGED, payload: toDeviceInfo(record) });
|
|
2112
2304
|
if (interactionRoute) {
|
|
2113
2305
|
throw this._endDisconnectedOperation(
|
|
@@ -2141,7 +2333,7 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
2141
2333
|
try {
|
|
2142
2334
|
return await this._requestQrAndAwaitResponse(
|
|
2143
2335
|
displayDevice,
|
|
2144
|
-
{ ...requestUr, animated },
|
|
2336
|
+
{ ...requestUr, animated, ...!operationName ? { allowAccountExport: true } : {} },
|
|
2145
2337
|
signal
|
|
2146
2338
|
);
|
|
2147
2339
|
} catch (error) {
|
|
@@ -2177,12 +2369,20 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
2177
2369
|
const target = this._resolveTarget(connectId, deviceId);
|
|
2178
2370
|
if (bundle?.channel === "qr") {
|
|
2179
2371
|
const scanned = bundle.scanned.get(accountKey(hwkChain, syncPath));
|
|
2372
|
+
if (target.record && !scanned && bundle.accountExport) {
|
|
2373
|
+
throw (0, import_hwk_adapter_core3.createHwkError)({
|
|
2374
|
+
code: import_hwk_adapter_core3.HardwareErrorCode.DevicePathForbidden,
|
|
2375
|
+
message: "Scanned account export does not include this account path",
|
|
2376
|
+
params: { reason: "account-not-exported" }
|
|
2377
|
+
});
|
|
2378
|
+
}
|
|
2180
2379
|
if (!target.record || !scanned) {
|
|
2181
2380
|
throw (0, import_hwk_adapter_core3.createHwkError)({
|
|
2182
2381
|
code: import_hwk_adapter_core3.HardwareErrorCode.DeviceMismatch,
|
|
2183
2382
|
message: `Keystone did not return the requested derivation path (${syncPath})`
|
|
2184
2383
|
});
|
|
2185
2384
|
}
|
|
2385
|
+
this._assertChainSupported(bundle.firmwareVersion, hwkChain);
|
|
2186
2386
|
return { record: target.record, account: scanned };
|
|
2187
2387
|
}
|
|
2188
2388
|
if (bundle?.channel === "usb" && target.record?.usbSessionId !== bundle.usbSessionId) {
|
|
@@ -2197,13 +2397,23 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
2197
2397
|
existingRecord = await this._tryUsbAttach(signal, target.expectedMasterFingerprint);
|
|
2198
2398
|
_KeystoneAdapter._throwIfAborted(signal);
|
|
2199
2399
|
}
|
|
2200
|
-
const useSinglePathUsbExport = operationConnectionType ? operationConnectionType === "usb" : this._forcedTransport === "usb" || this._forcedTransport !== "qr" && Boolean(existingRecord?.usbSessionId);
|
|
2201
2400
|
const schemas = [keySchema(hwkChain, syncPath)];
|
|
2202
2401
|
const requestUr = this.urEngine.buildKeyDerivationRequest({ schemas, origin: this._origin });
|
|
2203
|
-
const responseUr = await this._resolveUr(
|
|
2402
|
+
const responseUr = await this._resolveUr(
|
|
2403
|
+
existingRecord,
|
|
2404
|
+
requestUr,
|
|
2405
|
+
false,
|
|
2406
|
+
connectId,
|
|
2407
|
+
signal,
|
|
2408
|
+
hwkChain,
|
|
2409
|
+
void 0,
|
|
2410
|
+
bundle?.channel === "usb" ? bundle.qrFallback : void 0
|
|
2411
|
+
);
|
|
2204
2412
|
_KeystoneAdapter._throwIfAborted(signal);
|
|
2205
|
-
const parsed = this.urEngine.parseAccountResponse(responseUr);
|
|
2206
|
-
|
|
2413
|
+
const parsed = responseUr.accountExport ? this.urEngine.parseAccountExport(responseUr) : this.urEngine.parseAccountResponse(responseUr);
|
|
2414
|
+
this._assertParsedIdentity(parsed, target);
|
|
2415
|
+
const account = requireExportedPath(parsed, syncPath, responseUr.accountExport);
|
|
2416
|
+
const useSinglePathUsbExport = !responseUr.accountExport && (operationConnectionType ? operationConnectionType === "usb" : this._forcedTransport === "usb" || this._forcedTransport !== "qr" && Boolean(existingRecord?.usbSessionId));
|
|
2207
2417
|
let record;
|
|
2208
2418
|
if (useSinglePathUsbExport && existingRecord) {
|
|
2209
2419
|
if (parsed.masterFingerprint !== existingRecord.masterFingerprint) {
|
|
@@ -2214,10 +2424,8 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
2214
2424
|
}
|
|
2215
2425
|
record = existingRecord;
|
|
2216
2426
|
} else {
|
|
2217
|
-
this.
|
|
2218
|
-
record = this._upsertDeviceRecord(parsed
|
|
2219
|
-
viaUsb: operationConnectionType !== "qr" && Boolean(existingRecord?.usbSessionId)
|
|
2220
|
-
});
|
|
2427
|
+
this._assertChainSupported(parsed.deviceVersion, hwkChain);
|
|
2428
|
+
record = this._upsertDeviceRecord(parsed);
|
|
2221
2429
|
}
|
|
2222
2430
|
return { record, account: { ...account, hwkChain } };
|
|
2223
2431
|
}
|
|
@@ -2239,21 +2447,27 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
2239
2447
|
schemas: [keySchema(chain, accountPath)],
|
|
2240
2448
|
origin: this._origin
|
|
2241
2449
|
});
|
|
2242
|
-
const responseUr = await this._resolveUr(void 0, requestUr, false, connectId, signal);
|
|
2450
|
+
const responseUr = await this._resolveUr(void 0, requestUr, false, connectId, signal, chain);
|
|
2243
2451
|
_KeystoneAdapter._throwIfAborted(signal);
|
|
2244
|
-
const parsed = this.urEngine.parseAccountResponse(responseUr);
|
|
2245
|
-
requireExportedPath(parsed, normalizePath(accountPath));
|
|
2246
|
-
|
|
2452
|
+
const parsed = responseUr.accountExport ? this.urEngine.parseAccountExport(responseUr) : this.urEngine.parseAccountResponse(responseUr);
|
|
2453
|
+
requireExportedPath(parsed, normalizePath(accountPath), responseUr.accountExport);
|
|
2454
|
+
this._assertChainSupported(parsed.deviceVersion, chain);
|
|
2455
|
+
const record = this._upsertDeviceRecord(parsed);
|
|
2456
|
+
return { record };
|
|
2457
|
+
}
|
|
2458
|
+
_assertChainSupported(firmwareVersion, chain) {
|
|
2459
|
+
const firmware = getKeystoneFirmwareProfile(firmwareVersion);
|
|
2460
|
+
if (firmware && !firmware.supportedChains.includes(chain)) {
|
|
2461
|
+
throw (0, import_hwk_adapter_core3.createHwkError)({
|
|
2462
|
+
code: import_hwk_adapter_core3.HardwareErrorCode.ChainNotSupported,
|
|
2463
|
+
message: `Keystone ${firmware.variant} firmware cannot use ${chain} through this adapter`,
|
|
2464
|
+
params: { firmwareVariant: firmware.variant, chain }
|
|
2465
|
+
});
|
|
2466
|
+
}
|
|
2247
2467
|
}
|
|
2248
|
-
/**
|
|
2249
|
-
* The operation owning the running job, so a mid-call UI request can name it. Unpinned jobs map
|
|
2250
|
-
* through the operation route; a cold start returns undefined.
|
|
2251
|
-
*/
|
|
2468
|
+
/** Only an operation id supplied to the running call owns its UI prompts. */
|
|
2252
2469
|
_activeOperationId() {
|
|
2253
|
-
|
|
2254
|
-
if (!activeJobId) return void 0;
|
|
2255
|
-
if ((0, import_hwk_adapter_core3.isHardwareOperationId)(activeJobId)) return activeJobId;
|
|
2256
|
-
return this._operationRouteForIdentifier(activeJobId)?.operationId;
|
|
2470
|
+
return this._operations.findOperationIdByJobKey(this._jobQueue.getActiveJob()?.deviceId);
|
|
2257
2471
|
}
|
|
2258
2472
|
/** Routes are keyed by connectId; a job key may be the bare master fingerprint. */
|
|
2259
2473
|
_operationRouteForIdentifier(identifier) {
|
|
@@ -2286,6 +2500,37 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
2286
2500
|
signal.removeEventListener("abort", onAbort);
|
|
2287
2501
|
}
|
|
2288
2502
|
}
|
|
2503
|
+
/**
|
|
2504
|
+
* Asks whether a request too large for USB may be shown as a QR. A timeout or an unanswered
|
|
2505
|
+
* prompt reads as no; a cancelled call keeps its own error.
|
|
2506
|
+
*/
|
|
2507
|
+
async _confirmQrFallback(signal, choice) {
|
|
2508
|
+
if (choice?.confirmed !== void 0) return choice.confirmed;
|
|
2509
|
+
const requestType = import_hwk_adapter_core3.UI_REQUEST.REQUEST_QR_FALLBACK_CONFIRM;
|
|
2510
|
+
const operationId = this._activeOperationId();
|
|
2511
|
+
const release = operationId ? this._operations.retain(operationId) : void 0;
|
|
2512
|
+
try {
|
|
2513
|
+
const waitPromise = this._uiRegistry.wait(requestType, {
|
|
2514
|
+
operationId
|
|
2515
|
+
});
|
|
2516
|
+
this.emitter.emit(requestType, {
|
|
2517
|
+
type: requestType,
|
|
2518
|
+
payload: { vendor: "keystone", operationId }
|
|
2519
|
+
});
|
|
2520
|
+
let confirmed = false;
|
|
2521
|
+
try {
|
|
2522
|
+
confirmed = Boolean(
|
|
2523
|
+
(await this._awaitQrResponse(requestType, waitPromise, signal))?.confirmed
|
|
2524
|
+
);
|
|
2525
|
+
} catch (error) {
|
|
2526
|
+
if (signal.aborted) throw error;
|
|
2527
|
+
}
|
|
2528
|
+
if (choice) choice.confirmed = confirmed;
|
|
2529
|
+
return confirmed;
|
|
2530
|
+
} finally {
|
|
2531
|
+
release?.();
|
|
2532
|
+
}
|
|
2533
|
+
}
|
|
2289
2534
|
/** Shows `data` for the device to scan, or with no `data` asks the host to scan the device. */
|
|
2290
2535
|
async _requestQrAndAwaitResponse(device, data, signal) {
|
|
2291
2536
|
const requestType = data ? import_hwk_adapter_core3.UI_REQUEST.REQUEST_QR_DISPLAY : import_hwk_adapter_core3.UI_REQUEST.REQUEST_QR_SCAN;
|
|
@@ -2308,7 +2553,17 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
2308
2553
|
});
|
|
2309
2554
|
}
|
|
2310
2555
|
const response = await this._awaitQrResponse(requestType, waitPromise, signal);
|
|
2311
|
-
|
|
2556
|
+
if (response.accountExport && data && !data.allowAccountExport) {
|
|
2557
|
+
throw (0, import_hwk_adapter_core3.createHwkError)({
|
|
2558
|
+
code: import_hwk_adapter_core3.HardwareErrorCode.InvalidParams,
|
|
2559
|
+
message: "This request cannot be answered with an account export"
|
|
2560
|
+
});
|
|
2561
|
+
}
|
|
2562
|
+
return {
|
|
2563
|
+
urType: response.urType,
|
|
2564
|
+
urData: response.urData,
|
|
2565
|
+
...!data || response.accountExport ? { accountExport: true } : {}
|
|
2566
|
+
};
|
|
2312
2567
|
} finally {
|
|
2313
2568
|
release?.();
|
|
2314
2569
|
}
|