@onekeyfe/hwk-keystone-adapter 1.2.8-alpha.1 → 1.2.8-alpha.13
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 +38 -28
- package/dist/index.d.ts +38 -28
- package/dist/index.js +481 -489
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +483 -490
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/index.mjs
CHANGED
|
@@ -7,6 +7,7 @@ import HDKey from "hdkey";
|
|
|
7
7
|
import { parse as uuidParse, stringify as uuidStringify } from "uuid";
|
|
8
8
|
import {
|
|
9
9
|
HardwareErrorCode,
|
|
10
|
+
boundedCborFromHex,
|
|
10
11
|
createHwkError,
|
|
11
12
|
parseWalletMasterFingerprint
|
|
12
13
|
} from "@onekeyfe/hwk-adapter-core";
|
|
@@ -70,11 +71,12 @@ var TronSignRequest = class _TronSignRequest extends RegistryItem {
|
|
|
70
71
|
static parsePath(path, xfp) {
|
|
71
72
|
const segments = path.replace(/^[mM]\//, "").split("/");
|
|
72
73
|
const components = segments.map((segment) => {
|
|
73
|
-
const
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
}
|
|
74
|
+
const match = /^(\d+)('?)$/.exec(segment);
|
|
75
|
+
const index = match ? Number(match[1]) : NaN;
|
|
76
|
+
if (!match || index >= 2147483648) {
|
|
77
|
+
throw new Error(`Invalid TRON derivation path segment (${segment})`);
|
|
78
|
+
}
|
|
79
|
+
return new PathComponent({ index, hardened: match[2] === "'" });
|
|
78
80
|
});
|
|
79
81
|
return new CryptoKeypath(components, Buffer.from(xfp, "hex"));
|
|
80
82
|
}
|
|
@@ -138,7 +140,7 @@ var DERIVATION_CURVE = {
|
|
|
138
140
|
ed25519: Curve.ed25519
|
|
139
141
|
};
|
|
140
142
|
function toSdkUr(ur) {
|
|
141
|
-
return new UR(
|
|
143
|
+
return new UR(boundedCborFromHex(ur.urData), ur.urType);
|
|
142
144
|
}
|
|
143
145
|
function fromSdkUr(ur) {
|
|
144
146
|
return { urType: ur.type, urData: ur.cbor.toString("hex") };
|
|
@@ -190,6 +192,11 @@ function relativeHdPath(relativeDerivePath) {
|
|
|
190
192
|
}
|
|
191
193
|
return `m/${segments.join("/")}`;
|
|
192
194
|
}
|
|
195
|
+
var FIRMWARE_VERSION = /^\d{1,4}(\.\d{1,4}){1,3}(-[0-9A-Za-z.]{1,20})?$/;
|
|
196
|
+
var DEVICE_TEXT = /^[^\p{Cc}\p{Cf}]{1,64}$/u;
|
|
197
|
+
function deviceText(value, pattern) {
|
|
198
|
+
return typeof value === "string" && pattern.test(value) ? value : void 0;
|
|
199
|
+
}
|
|
193
200
|
var KeystoneUrEngine = class {
|
|
194
201
|
constructor(origin = "OneKey") {
|
|
195
202
|
this.sdk = new KeystoneSDK({ origin });
|
|
@@ -199,9 +206,9 @@ var KeystoneUrEngine = class {
|
|
|
199
206
|
const parsed = this.sdk.parseMultiAccounts(toSdkUr(ur));
|
|
200
207
|
return {
|
|
201
208
|
masterFingerprint: requireBip32MasterFingerprint(parsed.masterFingerprint),
|
|
202
|
-
device: parsed.device,
|
|
203
|
-
deviceId: parsed.deviceId,
|
|
204
|
-
deviceVersion: parsed.deviceVersion,
|
|
209
|
+
device: deviceText(parsed.device, DEVICE_TEXT),
|
|
210
|
+
deviceId: deviceText(parsed.deviceId, DEVICE_TEXT),
|
|
211
|
+
deviceVersion: deviceText(parsed.deviceVersion, FIRMWARE_VERSION),
|
|
205
212
|
accounts: parsed.keys.map(toParsedAccount)
|
|
206
213
|
};
|
|
207
214
|
}
|
|
@@ -263,10 +270,7 @@ var KeystoneUrEngine = class {
|
|
|
263
270
|
return generateAddressFromXpub(xpub, relativeHdPath(relativeDerivePath));
|
|
264
271
|
}
|
|
265
272
|
// --- BTC (PSBT transaction signing + plain message signing) ---
|
|
266
|
-
/**
|
|
267
|
-
* `CryptoHDKey` always emits mainnet xpub version bytes (`0488B21E`), so `hdkey` parses it as is.
|
|
268
|
-
* `p2tr` needs an ECC lib for BIP-341 tweaking, not wired here.
|
|
269
|
-
*/
|
|
273
|
+
/** `CryptoHDKey` always emits mainnet xpub version bytes (`0488B21E`), so `hdkey` parses it as is. */
|
|
270
274
|
deriveBtcAddressFromXpub(xpub, relativeDerivePath, scriptType) {
|
|
271
275
|
const node = HDKey.fromExtendedKey(xpub).derive(relativeHdPath(relativeDerivePath));
|
|
272
276
|
if (!node.publicKey) throw new Error("HDKey derivation did not produce a public key");
|
|
@@ -312,9 +316,27 @@ var KeystoneUrEngine = class {
|
|
|
312
316
|
const ur = this.sdk.btc.generatePSBT(Buffer.from(unsignedPsbtHex, "hex"));
|
|
313
317
|
return fromSdkUr(ur);
|
|
314
318
|
}
|
|
315
|
-
/**
|
|
316
|
-
|
|
317
|
-
|
|
319
|
+
/**
|
|
320
|
+
* Returns the hex-encoded (possibly still-unsigned-in-part) PSBT the device replied with. A PSBT
|
|
321
|
+
* carries no request id, so the reply is bound to the request by its unsigned transaction, which
|
|
322
|
+
* signing never changes (BIP-174); a scan of another PSBT's signature is refused.
|
|
323
|
+
*/
|
|
324
|
+
parseBtcPsbt(ur, requestPsbtHex) {
|
|
325
|
+
const signedPsbtHex = this.sdk.btc.parsePSBT(toSdkUr(ur));
|
|
326
|
+
let sameTransaction = false;
|
|
327
|
+
try {
|
|
328
|
+
const unsignedTx = (hex) => Buffer.from(bitcoin.Psbt.fromHex(hex).data.globalMap.unsignedTx.toBuffer());
|
|
329
|
+
sameTransaction = unsignedTx(signedPsbtHex).equals(unsignedTx(requestPsbtHex));
|
|
330
|
+
} catch {
|
|
331
|
+
sameTransaction = false;
|
|
332
|
+
}
|
|
333
|
+
if (!sameTransaction) {
|
|
334
|
+
throw createHwkError({
|
|
335
|
+
code: HardwareErrorCode.DeviceMismatch,
|
|
336
|
+
message: "Keystone returned a PSBT for another transaction than the one requested"
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
return signedPsbtHex;
|
|
318
340
|
}
|
|
319
341
|
buildBtcMessageSignRequest(params) {
|
|
320
342
|
const ur = this.sdk.btc.generateSignRequest({
|
|
@@ -374,7 +396,7 @@ var KeystoneUrEngine = class {
|
|
|
374
396
|
if (ur.urType !== "tron-signature") {
|
|
375
397
|
throw new Error(`Expected a tron-signature UR, got ${ur.urType}`);
|
|
376
398
|
}
|
|
377
|
-
const signature = TronSignature.fromCBOR(
|
|
399
|
+
const signature = TronSignature.fromCBOR(boundedCborFromHex(ur.urData));
|
|
378
400
|
const requestId = signature.getRequestId();
|
|
379
401
|
return {
|
|
380
402
|
requestId: requestId ? uuidStringify(requestId) : void 0,
|
|
@@ -426,6 +448,7 @@ import {
|
|
|
426
448
|
UI_REQUEST_TIMEOUT_TAG,
|
|
427
449
|
UI_RESPONSE,
|
|
428
450
|
UiRequestRegistry,
|
|
451
|
+
buildAllNetworkCancelledFailure,
|
|
429
452
|
createHardwareSearchTargetId,
|
|
430
453
|
createHwkError as createHwkError2,
|
|
431
454
|
deriveDeviceFingerprint,
|
|
@@ -445,7 +468,6 @@ import {
|
|
|
445
468
|
rehydrateConnectorError,
|
|
446
469
|
resolveHardwareOperationTarget as resolveGenericHardwareOperationTarget,
|
|
447
470
|
runAllNetworkGetAddress,
|
|
448
|
-
stripHex,
|
|
449
471
|
success
|
|
450
472
|
} from "@onekeyfe/hwk-adapter-core";
|
|
451
473
|
|
|
@@ -454,6 +476,14 @@ function normalizePath(path) {
|
|
|
454
476
|
const trimmed = path.trim();
|
|
455
477
|
return /^m\//i.test(trimmed) ? `m/${trimmed.slice(2)}` : `m/${trimmed}`;
|
|
456
478
|
}
|
|
479
|
+
var BIP32_HARDENED_OFFSET2 = 2147483648;
|
|
480
|
+
function isBip32Path(path) {
|
|
481
|
+
const segments = normalizePath(path).slice(2).split("/");
|
|
482
|
+
return segments.every((segment) => {
|
|
483
|
+
const match = /^(\d+)'?$/.exec(segment);
|
|
484
|
+
return match !== null && Number(match[1]) < BIP32_HARDENED_OFFSET2;
|
|
485
|
+
});
|
|
486
|
+
}
|
|
457
487
|
function splitAccountPath(path) {
|
|
458
488
|
const normalized = normalizePath(path);
|
|
459
489
|
const segments = normalized.slice(2).split("/");
|
|
@@ -590,12 +620,6 @@ function waitForKeystoneUsbReattachProbe() {
|
|
|
590
620
|
setTimeout(resolve, KEYSTONE_USB_REATTACH_PROBE_INTERVAL_MS);
|
|
591
621
|
});
|
|
592
622
|
}
|
|
593
|
-
function buildCancelledFailure(signal) {
|
|
594
|
-
const reason = signal.reason;
|
|
595
|
-
const code = typeof reason?.code === "number" ? reason.code : HardwareErrorCode2.UserAborted;
|
|
596
|
-
const message = typeof reason?.message === "string" ? reason.message : "";
|
|
597
|
-
return failure(code, message || "All-network get-address cancelled");
|
|
598
|
-
}
|
|
599
623
|
var BIP44_COIN_TYPE_TO_CHAIN = {
|
|
600
624
|
60: "evm",
|
|
601
625
|
0: "btc",
|
|
@@ -606,6 +630,23 @@ function hasChainCoinType(chain, path) {
|
|
|
606
630
|
const match = normalizePath(path).match(/^m\/\d+'\/(\d+)'/);
|
|
607
631
|
return Boolean(match) && BIP44_COIN_TYPE_TO_CHAIN[Number(match?.[1])] === chain;
|
|
608
632
|
}
|
|
633
|
+
function exactHex(value) {
|
|
634
|
+
const hex = value.replace(/^0x/i, "");
|
|
635
|
+
return /^(?:[0-9a-f]{2})*$/i.test(hex) ? hex : void 0;
|
|
636
|
+
}
|
|
637
|
+
function invalidHexFailure(field) {
|
|
638
|
+
return failure(HardwareErrorCode2.InvalidParams, `${field} must be an even-length hex string`);
|
|
639
|
+
}
|
|
640
|
+
function requireExportedPath(parsed, path) {
|
|
641
|
+
const account = parsed.accounts.find((returned) => normalizePath(returned.path) === path);
|
|
642
|
+
if (!account) {
|
|
643
|
+
throw createHwkError2({
|
|
644
|
+
code: HardwareErrorCode2.DeviceMismatch,
|
|
645
|
+
message: `Keystone did not return the requested derivation path (${path})`
|
|
646
|
+
});
|
|
647
|
+
}
|
|
648
|
+
return account;
|
|
649
|
+
}
|
|
609
650
|
function coinTypeFailure(chain, path) {
|
|
610
651
|
if (hasChainCoinType(chain, path)) return void 0;
|
|
611
652
|
return failure(
|
|
@@ -613,6 +654,19 @@ function coinTypeFailure(chain, path) {
|
|
|
613
654
|
`Keystone ${chain} needs a path with its own BIP44 coin type, not ${path}`
|
|
614
655
|
);
|
|
615
656
|
}
|
|
657
|
+
function btcAccountFailure(accountPath) {
|
|
658
|
+
if (isKeystoneSignableBtcAccountPath(accountPath)) return void 0;
|
|
659
|
+
return failure(HardwareErrorCode2.DevicePathForbidden, KEYSTONE_BTC_ACCOUNT_FORBIDDEN_MESSAGE);
|
|
660
|
+
}
|
|
661
|
+
function missingParamFailure(method, param) {
|
|
662
|
+
return failure(HardwareErrorCode2.InvalidParams, `${method} requires ${param}`);
|
|
663
|
+
}
|
|
664
|
+
function messageToHex(message, hex) {
|
|
665
|
+
return hex ? exactHex(message) : Buffer.from(message, "utf8").toString("hex");
|
|
666
|
+
}
|
|
667
|
+
function evmMessageSignature(sig) {
|
|
668
|
+
return { signature: ensure0x(sig.r + sig.s + sig.v) };
|
|
669
|
+
}
|
|
616
670
|
var KeystoneAdapter = class _KeystoneAdapter {
|
|
617
671
|
constructor(options) {
|
|
618
672
|
this.vendor = "keystone";
|
|
@@ -627,6 +681,15 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
627
681
|
const route = this._operationRoutes.get(operation.connectId);
|
|
628
682
|
if (route?.operationId === operation.operationId) {
|
|
629
683
|
this._operationRoutes.delete(operation.connectId);
|
|
684
|
+
if (route.connectionType === "qr") {
|
|
685
|
+
const ended = createHwkError2({
|
|
686
|
+
code: HardwareErrorCode2.OperationEnded,
|
|
687
|
+
message: "Keystone operation has ended",
|
|
688
|
+
params: { operationId: operation.operationId, reason }
|
|
689
|
+
});
|
|
690
|
+
this._jobQueue.cancelActiveAndPending(operation.operationId, ended);
|
|
691
|
+
this._uiRegistry.cancel(void 0, void 0, operation.operationId);
|
|
692
|
+
}
|
|
630
693
|
}
|
|
631
694
|
this.emitter.emit(SDK.OPERATION_ENDED, {
|
|
632
695
|
type: SDK.OPERATION_ENDED,
|
|
@@ -703,7 +766,7 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
703
766
|
callItem: async ({ method, item, index }) => {
|
|
704
767
|
if (scope.signal.aborted) {
|
|
705
768
|
cancelledIndexes.add(index);
|
|
706
|
-
return
|
|
769
|
+
return buildAllNetworkCancelledFailure(scope.signal);
|
|
707
770
|
}
|
|
708
771
|
const commonArgs = {
|
|
709
772
|
path: item.path,
|
|
@@ -725,11 +788,6 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
725
788
|
return this.solGetAddress(effectiveConnectId, itemDeviceId, commonArgs, access);
|
|
726
789
|
case "tronGetAddress":
|
|
727
790
|
return this.tronGetAddress(effectiveConnectId, itemDeviceId, commonArgs, access);
|
|
728
|
-
default:
|
|
729
|
-
return failure(
|
|
730
|
-
HardwareErrorCode2.MethodNotSupported,
|
|
731
|
-
`Unsupported method: ${String(method)}`
|
|
732
|
-
);
|
|
733
791
|
}
|
|
734
792
|
},
|
|
735
793
|
attachIdentity: (context) => {
|
|
@@ -770,9 +828,6 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
770
828
|
this._usbConnector?.on("device-disconnect", this._handleUsbDisconnect);
|
|
771
829
|
this._usbConnector?.on("ui-event", this._handleUsbUiEvent);
|
|
772
830
|
}
|
|
773
|
-
// ---------------------------------------------------------------------------
|
|
774
|
-
// Lifecycle / transport
|
|
775
|
-
// ---------------------------------------------------------------------------
|
|
776
831
|
get activeTransport() {
|
|
777
832
|
if (this._devices.size === 0) return null;
|
|
778
833
|
const hasUsbSession = Array.from(this._devices.values()).some((r) => r.usbSessionId);
|
|
@@ -802,9 +857,6 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
802
857
|
this._devices.clear();
|
|
803
858
|
this.emitter.removeAllListeners();
|
|
804
859
|
}
|
|
805
|
-
// ---------------------------------------------------------------------------
|
|
806
|
-
// Device table
|
|
807
|
-
// ---------------------------------------------------------------------------
|
|
808
860
|
/**
|
|
809
861
|
* Returns known wallets plus USB scan results; USB entries have no mfp until `connectDevice()`
|
|
810
862
|
* claims them. Explicit QR discovery returns one virtual target.
|
|
@@ -877,11 +929,9 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
877
929
|
}
|
|
878
930
|
/** Best-effort disconnect; the session is retired either way so it cannot be selected again. */
|
|
879
931
|
async _retireUsbSession(sessionId) {
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
} catch {
|
|
884
|
-
}
|
|
932
|
+
try {
|
|
933
|
+
await this._usbConnector?.disconnect(sessionId);
|
|
934
|
+
} catch {
|
|
885
935
|
}
|
|
886
936
|
this._handleUsbDisconnect({ connectId: sessionId });
|
|
887
937
|
}
|
|
@@ -900,13 +950,6 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
900
950
|
serialNumber: device.serialNumber
|
|
901
951
|
}));
|
|
902
952
|
}
|
|
903
|
-
async listConnectionTargets(options) {
|
|
904
|
-
const targets = await this.searchDeviceTargets(options);
|
|
905
|
-
return targets.map(({ searchTargetId, ...target }) => ({
|
|
906
|
-
...target,
|
|
907
|
-
targetId: searchTargetId
|
|
908
|
-
}));
|
|
909
|
-
}
|
|
910
953
|
async connectDevice(searchTargetId) {
|
|
911
954
|
const scope = this._jobQueue.createCancelScope(searchTargetId);
|
|
912
955
|
try {
|
|
@@ -915,16 +958,10 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
915
958
|
const discoveredConnectionType = this._searchDeviceTargets.get(searchTargetId);
|
|
916
959
|
if (discoveredConnectionType === "qr") {
|
|
917
960
|
selectedConnectionType = "qr";
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
961
|
+
connected = await _KeystoneAdapter._untilAborted(
|
|
962
|
+
scope.signal,
|
|
963
|
+
this._importFromQr({}, searchTargetId)
|
|
921
964
|
);
|
|
922
|
-
scope.signal.addEventListener("abort", abortColdStart, { once: true });
|
|
923
|
-
try {
|
|
924
|
-
connected = await this.importFromQr();
|
|
925
|
-
} finally {
|
|
926
|
-
scope.signal.removeEventListener("abort", abortColdStart);
|
|
927
|
-
}
|
|
928
965
|
} else if (discoveredConnectionType === "usb") {
|
|
929
966
|
selectedConnectionType = "usb";
|
|
930
967
|
if (!this._usbConnector) {
|
|
@@ -933,7 +970,7 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
933
970
|
"No USB connector configured for this Keystone adapter"
|
|
934
971
|
);
|
|
935
972
|
}
|
|
936
|
-
connected = await this._connectUsb({}, searchTargetId, scope.signal);
|
|
973
|
+
connected = await this._connectUsb(this._usbConnector, {}, searchTargetId, scope.signal);
|
|
937
974
|
} else if (hasHardwareRuntimeIdPrefix(searchTargetId)) {
|
|
938
975
|
return failure(
|
|
939
976
|
HardwareErrorCode2.DeviceNotFound,
|
|
@@ -954,7 +991,7 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
954
991
|
);
|
|
955
992
|
} else {
|
|
956
993
|
selectedConnectionType = "usb";
|
|
957
|
-
connected = await this._connectUsb(target, void 0, scope.signal);
|
|
994
|
+
connected = await this._connectUsb(this._usbConnector, target, void 0, scope.signal);
|
|
958
995
|
}
|
|
959
996
|
}
|
|
960
997
|
_KeystoneAdapter._throwIfAborted(scope.signal);
|
|
@@ -1013,10 +1050,7 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
1013
1050
|
const { record } = this._resolveTarget(connectId, deviceId);
|
|
1014
1051
|
if (!record) {
|
|
1015
1052
|
return Promise.resolve(
|
|
1016
|
-
failure(
|
|
1017
|
-
HardwareErrorCode2.DeviceNotFound,
|
|
1018
|
-
`Unknown Keystone device: ${connectId || deviceId}`
|
|
1019
|
-
)
|
|
1053
|
+
failure(HardwareErrorCode2.DeviceNotFound, "Unknown Keystone device")
|
|
1020
1054
|
);
|
|
1021
1055
|
}
|
|
1022
1056
|
return Promise.resolve(success(toDeviceInfo(record)));
|
|
@@ -1092,18 +1126,21 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
1092
1126
|
// Explicit account import: call before signing to avoid a cold sync per call.
|
|
1093
1127
|
// ---------------------------------------------------------------------------
|
|
1094
1128
|
async importFromQr(options = {}) {
|
|
1129
|
+
return this._importFromQr(options, KEYSTONE_COLD_START_JOB_LABEL);
|
|
1130
|
+
}
|
|
1131
|
+
async _importFromQr(options, jobKey) {
|
|
1095
1132
|
try {
|
|
1096
|
-
return await this._jobQueue.enqueue(
|
|
1133
|
+
return await this._jobQueue.enqueue(jobKey, async (signal) => {
|
|
1097
1134
|
const displayDevice = placeholderDeviceInfo();
|
|
1098
1135
|
let responseUr;
|
|
1099
1136
|
if (options.mode === "scan") {
|
|
1100
|
-
responseUr = await this.
|
|
1137
|
+
responseUr = await this._requestQrAndAwaitResponse(displayDevice, void 0, signal);
|
|
1101
1138
|
} else {
|
|
1102
1139
|
const requestUr = this.urEngine.buildKeyDerivationRequest({
|
|
1103
1140
|
schemas: [keySchema("evm", KEYSTONE_COLD_START_PATH)],
|
|
1104
1141
|
origin: this._origin
|
|
1105
1142
|
});
|
|
1106
|
-
responseUr = await this.
|
|
1143
|
+
responseUr = await this._requestQrAndAwaitResponse(
|
|
1107
1144
|
displayDevice,
|
|
1108
1145
|
{ ...requestUr, animated: false },
|
|
1109
1146
|
signal
|
|
@@ -1111,6 +1148,7 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
1111
1148
|
}
|
|
1112
1149
|
_KeystoneAdapter._throwIfAborted(signal);
|
|
1113
1150
|
const parsed = this.urEngine.parseAccountResponse(responseUr);
|
|
1151
|
+
if (options.mode !== "scan") requireExportedPath(parsed, KEYSTONE_COLD_START_PATH);
|
|
1114
1152
|
const record = this._upsertDeviceRecord(parsed);
|
|
1115
1153
|
return success(toDeviceInfo(record));
|
|
1116
1154
|
});
|
|
@@ -1118,163 +1156,103 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
1118
1156
|
return this._errorToFailure(err);
|
|
1119
1157
|
}
|
|
1120
1158
|
}
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
if (!operationTarget.success) return operationTarget;
|
|
1127
|
-
const connectId = operationTarget.payload.targetId;
|
|
1128
|
-
const deviceId = deviceIdArg ?? void 0;
|
|
1129
|
-
if (!params) return failure(HardwareErrorCode2.InvalidParams, "evmGetAddress requires params");
|
|
1130
|
-
if (!params.path)
|
|
1131
|
-
return failure(HardwareErrorCode2.InvalidParams, "evmGetAddress requires params.path");
|
|
1159
|
+
async evmGetAddress(connectIdArg, deviceIdArg, paramsArg, bundle) {
|
|
1160
|
+
const call = this._beginCall("evmGetAddress", connectIdArg, deviceIdArg, paramsArg);
|
|
1161
|
+
if (!call.success) return call;
|
|
1162
|
+
const { params } = call.payload;
|
|
1163
|
+
if (!params.path) return missingParamFailure("evmGetAddress", "params.path");
|
|
1132
1164
|
const evmCoinTypeFailure = coinTypeFailure("evm", params.path);
|
|
1133
1165
|
if (evmCoinTypeFailure) return evmCoinTypeFailure;
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
connectId,
|
|
1144
|
-
deviceId,
|
|
1145
|
-
"evm",
|
|
1146
|
-
accountPath,
|
|
1147
|
-
signal,
|
|
1148
|
-
bundle
|
|
1149
|
-
);
|
|
1150
|
-
const address = this.urEngine.deriveEvmAddressFromXpub(xpub, relativeDerivePath);
|
|
1151
|
-
return success({ address, path: normalizePath(params.path) });
|
|
1152
|
-
});
|
|
1166
|
+
return this._addressFromAccountXpub(
|
|
1167
|
+
"evmGetAddress",
|
|
1168
|
+
"evm",
|
|
1169
|
+
call.payload,
|
|
1170
|
+
params.path,
|
|
1171
|
+
"m/44'/60'/0'/0/0",
|
|
1172
|
+
bundle,
|
|
1173
|
+
(xpub, relativeDerivePath) => this.urEngine.deriveEvmAddressFromXpub(xpub, relativeDerivePath)
|
|
1174
|
+
);
|
|
1153
1175
|
}
|
|
1154
|
-
async evmSignTransaction(connectIdArg, deviceIdArg,
|
|
1155
|
-
const
|
|
1156
|
-
if (!
|
|
1157
|
-
const
|
|
1158
|
-
|
|
1159
|
-
if (!params)
|
|
1160
|
-
return failure(HardwareErrorCode2.InvalidParams, "evmSignTransaction requires params");
|
|
1161
|
-
if (!params.path) {
|
|
1162
|
-
return failure(HardwareErrorCode2.InvalidParams, "evmSignTransaction requires params.path");
|
|
1163
|
-
}
|
|
1176
|
+
async evmSignTransaction(connectIdArg, deviceIdArg, paramsArg) {
|
|
1177
|
+
const call = this._beginCall("evmSignTransaction", connectIdArg, deviceIdArg, paramsArg);
|
|
1178
|
+
if (!call.success) return call;
|
|
1179
|
+
const { params } = call.payload;
|
|
1180
|
+
if (!params.path) return missingParamFailure("evmSignTransaction", "params.path");
|
|
1164
1181
|
if (!("serializedTx" in params) || typeof params.serializedTx !== "string" || !params.serializedTx) {
|
|
1165
1182
|
return failure(
|
|
1166
1183
|
HardwareErrorCode2.MethodNotSupported,
|
|
1167
1184
|
"Keystone only signs a fully RLP-serialized transaction (params.serializedTx) \u2014 structured-field signing is not supported"
|
|
1168
1185
|
);
|
|
1169
1186
|
}
|
|
1170
|
-
const rawTxHex =
|
|
1171
|
-
|
|
1187
|
+
const rawTxHex = exactHex(params.serializedTx);
|
|
1188
|
+
if (rawTxHex === void 0) return invalidHexFailure("serializedTx");
|
|
1172
1189
|
const dataType = parseInt(rawTxHex.slice(0, 2), 16) < 192 ? "typedTransaction" : "transaction";
|
|
1173
|
-
return this.
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
path,
|
|
1183
|
-
xfp,
|
|
1184
|
-
chainId: params.chainId,
|
|
1185
|
-
origin: this._origin
|
|
1186
|
-
}),
|
|
1187
|
-
parseResponse: (ur) => this.urEngine.parseEthSignature(ur),
|
|
1188
|
-
toPayload: (sig) => ({
|
|
1189
|
-
v: ensure0x(sig.v),
|
|
1190
|
-
r: ensure0x(sig.r),
|
|
1191
|
-
s: ensure0x(sig.s)
|
|
1192
|
-
})
|
|
1193
|
-
});
|
|
1190
|
+
return this._evmSign(
|
|
1191
|
+
"evmSignTransaction",
|
|
1192
|
+
call.payload,
|
|
1193
|
+
params.path,
|
|
1194
|
+
params.chainId,
|
|
1195
|
+
rawTxHex,
|
|
1196
|
+
dataType,
|
|
1197
|
+
(sig) => ({ v: ensure0x(sig.v), r: ensure0x(sig.r), s: ensure0x(sig.s) })
|
|
1198
|
+
);
|
|
1194
1199
|
}
|
|
1195
|
-
async evmSignMessage(connectIdArg, deviceIdArg,
|
|
1196
|
-
const
|
|
1197
|
-
if (!
|
|
1198
|
-
const
|
|
1199
|
-
const deviceId = deviceIdArg ?? void 0;
|
|
1200
|
-
if (!params) return failure(HardwareErrorCode2.InvalidParams, "evmSignMessage requires params");
|
|
1200
|
+
async evmSignMessage(connectIdArg, deviceIdArg, paramsArg) {
|
|
1201
|
+
const call = this._beginCall("evmSignMessage", connectIdArg, deviceIdArg, paramsArg);
|
|
1202
|
+
if (!call.success) return call;
|
|
1203
|
+
const { params } = call.payload;
|
|
1201
1204
|
if (!params.path || params.message === void 0) {
|
|
1202
|
-
return
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
requestId,
|
|
1216
|
-
unsignedTxHex: messageHex,
|
|
1217
|
-
dataType: "personalMessage",
|
|
1218
|
-
path,
|
|
1219
|
-
xfp,
|
|
1220
|
-
chainId: params.chainId,
|
|
1221
|
-
origin: this._origin
|
|
1222
|
-
}),
|
|
1223
|
-
parseResponse: (ur) => this.urEngine.parseEthSignature(ur),
|
|
1224
|
-
toPayload: (sig) => ({ signature: ensure0x(sig.r + sig.s + sig.v) })
|
|
1225
|
-
});
|
|
1205
|
+
return missingParamFailure("evmSignMessage", "params.path and params.message");
|
|
1206
|
+
}
|
|
1207
|
+
const messageHex = messageToHex(params.message, params.hex);
|
|
1208
|
+
if (messageHex === void 0) return invalidHexFailure("message");
|
|
1209
|
+
return this._evmSign(
|
|
1210
|
+
"evmSignMessage",
|
|
1211
|
+
call.payload,
|
|
1212
|
+
params.path,
|
|
1213
|
+
params.chainId,
|
|
1214
|
+
messageHex,
|
|
1215
|
+
"personalMessage",
|
|
1216
|
+
evmMessageSignature
|
|
1217
|
+
);
|
|
1226
1218
|
}
|
|
1227
|
-
async evmSignTypedData(connectIdArg, deviceIdArg,
|
|
1228
|
-
const
|
|
1229
|
-
if (!
|
|
1230
|
-
const
|
|
1231
|
-
|
|
1232
|
-
if (!params)
|
|
1233
|
-
return failure(HardwareErrorCode2.InvalidParams, "evmSignTypedData requires params");
|
|
1234
|
-
if (!params.path) {
|
|
1235
|
-
return failure(HardwareErrorCode2.InvalidParams, "evmSignTypedData requires params.path");
|
|
1236
|
-
}
|
|
1219
|
+
async evmSignTypedData(connectIdArg, deviceIdArg, paramsArg) {
|
|
1220
|
+
const call = this._beginCall("evmSignTypedData", connectIdArg, deviceIdArg, paramsArg);
|
|
1221
|
+
if (!call.success) return call;
|
|
1222
|
+
const { params } = call.payload;
|
|
1223
|
+
if (!params.path) return missingParamFailure("evmSignTypedData", "params.path");
|
|
1237
1224
|
if (params.mode === "hash") {
|
|
1238
1225
|
return failure(
|
|
1239
1226
|
HardwareErrorCode2.MethodNotSupported,
|
|
1240
1227
|
"Keystone always displays the full EIP-712 payload for on-device review \u2014 pre-hashed signing is not supported"
|
|
1241
1228
|
);
|
|
1242
1229
|
}
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
chainId: params.chainId,
|
|
1260
|
-
origin: this._origin
|
|
1261
|
-
}),
|
|
1262
|
-
parseResponse: (ur) => this.urEngine.parseEthSignature(ur),
|
|
1263
|
-
toPayload: (sig) => ({ signature: ensure0x(sig.r + sig.s + sig.v) })
|
|
1264
|
-
});
|
|
1230
|
+
if (typeof params.dataJson !== "string" || !params.dataJson) {
|
|
1231
|
+
return failure(
|
|
1232
|
+
HardwareErrorCode2.InvalidParams,
|
|
1233
|
+
"Keystone evmSignTypedData requires params.dataJson, the original typed-data JSON text"
|
|
1234
|
+
);
|
|
1235
|
+
}
|
|
1236
|
+
const signDataHex = Buffer.from(params.dataJson, "utf8").toString("hex");
|
|
1237
|
+
return this._evmSign(
|
|
1238
|
+
"evmSignTypedData",
|
|
1239
|
+
call.payload,
|
|
1240
|
+
params.path,
|
|
1241
|
+
params.chainId,
|
|
1242
|
+
signDataHex,
|
|
1243
|
+
"typedData",
|
|
1244
|
+
evmMessageSignature
|
|
1245
|
+
);
|
|
1265
1246
|
}
|
|
1266
1247
|
// ---------------------------------------------------------------------------
|
|
1267
1248
|
// BTC: 44'/49'/84' addresses derive offline from the account xpub; P2TR addresses
|
|
1268
1249
|
// and structured-field tx signing are unsupported (the host builds a PSBT instead).
|
|
1269
1250
|
// ---------------------------------------------------------------------------
|
|
1270
|
-
async btcGetAddress(connectIdArg, deviceIdArg,
|
|
1271
|
-
const
|
|
1272
|
-
if (!
|
|
1273
|
-
const
|
|
1274
|
-
|
|
1275
|
-
if (!params) return failure(HardwareErrorCode2.InvalidParams, "btcGetAddress requires params");
|
|
1276
|
-
if (!params.path)
|
|
1277
|
-
return failure(HardwareErrorCode2.InvalidParams, "btcGetAddress requires params.path");
|
|
1251
|
+
async btcGetAddress(connectIdArg, deviceIdArg, paramsArg, bundle) {
|
|
1252
|
+
const call = this._beginCall("btcGetAddress", connectIdArg, deviceIdArg, paramsArg);
|
|
1253
|
+
if (!call.success) return call;
|
|
1254
|
+
const { params } = call.payload;
|
|
1255
|
+
if (!params.path) return missingParamFailure("btcGetAddress", "params.path");
|
|
1278
1256
|
const btcParamsFailure = keystoneBtcParamsFailure(params);
|
|
1279
1257
|
if (btcParamsFailure) return btcParamsFailure;
|
|
1280
1258
|
const scriptType = btcScriptTypeFromPath(params.path);
|
|
@@ -1290,42 +1268,25 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
1290
1268
|
"P2TR (purpose 86') needs an elliptic-curve library for BIP-341 tweaking, not yet wired in \u2014 44'/49'/84' work"
|
|
1291
1269
|
);
|
|
1292
1270
|
}
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
return failure(HardwareErrorCode2.DevicePathForbidden, KEYSTONE_BTC_ACCOUNT_FORBIDDEN_MESSAGE);
|
|
1303
|
-
}
|
|
1304
|
-
return this._runJob(connectId, deviceId, async (signal) => {
|
|
1305
|
-
const xpub = await this._fetchAccountXpub(
|
|
1306
|
-
connectId,
|
|
1307
|
-
deviceId,
|
|
1308
|
-
"btc",
|
|
1309
|
-
accountPath,
|
|
1310
|
-
signal,
|
|
1311
|
-
bundle
|
|
1312
|
-
);
|
|
1313
|
-
const address = this.urEngine.deriveBtcAddressFromXpub(xpub, relativeDerivePath, scriptType);
|
|
1314
|
-
return success({ address, path: normalizedPath });
|
|
1315
|
-
});
|
|
1271
|
+
return this._addressFromAccountXpub(
|
|
1272
|
+
"btcGetAddress",
|
|
1273
|
+
"btc",
|
|
1274
|
+
call.payload,
|
|
1275
|
+
params.path,
|
|
1276
|
+
"m/84'/0'/0'/0/0",
|
|
1277
|
+
bundle,
|
|
1278
|
+
(xpub, relativeDerivePath) => this.urEngine.deriveBtcAddressFromXpub(xpub, relativeDerivePath, scriptType)
|
|
1279
|
+
);
|
|
1316
1280
|
}
|
|
1317
1281
|
/**
|
|
1318
1282
|
* `params.path` must be an account path (`m/84'/0'/0'`), the level Keystone syncs. An xpub is
|
|
1319
1283
|
* script-type agnostic, so `86'` works here even though `btcGetAddress` rejects it.
|
|
1320
1284
|
*/
|
|
1321
|
-
async btcGetPublicKey(connectIdArg, deviceIdArg,
|
|
1322
|
-
const
|
|
1323
|
-
if (!
|
|
1324
|
-
const connectId =
|
|
1325
|
-
|
|
1326
|
-
if (!params) return failure(HardwareErrorCode2.InvalidParams, "btcGetPublicKey requires params");
|
|
1327
|
-
if (!params.path)
|
|
1328
|
-
return failure(HardwareErrorCode2.InvalidParams, "btcGetPublicKey requires params.path");
|
|
1285
|
+
async btcGetPublicKey(connectIdArg, deviceIdArg, paramsArg, bundle) {
|
|
1286
|
+
const call = this._beginCall("btcGetPublicKey", connectIdArg, deviceIdArg, paramsArg);
|
|
1287
|
+
if (!call.success) return call;
|
|
1288
|
+
const { connectId, deviceId, params } = call.payload;
|
|
1289
|
+
if (!params.path) return missingParamFailure("btcGetPublicKey", "params.path");
|
|
1329
1290
|
const btcParamsFailure = keystoneBtcParamsFailure(params);
|
|
1330
1291
|
if (btcParamsFailure) return btcParamsFailure;
|
|
1331
1292
|
const { accountPath, relativeDerivePath } = splitAccountPath(params.path);
|
|
@@ -1335,9 +1296,8 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
1335
1296
|
"btcGetPublicKey requires an account path, e.g. m/84'/0'/0' (not a leaf path)"
|
|
1336
1297
|
);
|
|
1337
1298
|
}
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
}
|
|
1299
|
+
const accountFailure = btcAccountFailure(accountPath);
|
|
1300
|
+
if (accountFailure) return accountFailure;
|
|
1341
1301
|
return this._runJob(connectId, deviceId, async (signal) => {
|
|
1342
1302
|
const xpub = await this._fetchAccountXpub(
|
|
1343
1303
|
connectId,
|
|
@@ -1354,7 +1314,8 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
1354
1314
|
chainCode: meta.chainCode,
|
|
1355
1315
|
depth: meta.depth,
|
|
1356
1316
|
fingerprint: meta.parentFingerprint,
|
|
1357
|
-
path: normalizePath(accountPath)
|
|
1317
|
+
path: normalizePath(accountPath),
|
|
1318
|
+
accountKey: this._exportedAccountKey(accountPath, xpub)
|
|
1358
1319
|
});
|
|
1359
1320
|
});
|
|
1360
1321
|
}
|
|
@@ -1364,23 +1325,20 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
1364
1325
|
"use btcSignPsbt \u2014 structured input/output signing needs host-side PSBT construction, not yet wired in"
|
|
1365
1326
|
);
|
|
1366
1327
|
}
|
|
1367
|
-
async btcSignPsbt(connectIdArg, deviceIdArg,
|
|
1368
|
-
const
|
|
1369
|
-
if (!
|
|
1370
|
-
const connectId =
|
|
1371
|
-
|
|
1372
|
-
if (!params) return
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
if (!isKeystoneSignableBtcAccountPath(splitAccountPath(params.path).accountPath)) {
|
|
1378
|
-
return failure(HardwareErrorCode2.DevicePathForbidden, KEYSTONE_BTC_ACCOUNT_FORBIDDEN_MESSAGE);
|
|
1379
|
-
}
|
|
1328
|
+
async btcSignPsbt(connectIdArg, deviceIdArg, paramsArg) {
|
|
1329
|
+
const call = this._beginCall("btcSignPsbt", connectIdArg, deviceIdArg, paramsArg);
|
|
1330
|
+
if (!call.success) return call;
|
|
1331
|
+
const { connectId, deviceId, params } = call.payload;
|
|
1332
|
+
if (!params.psbt) return missingParamFailure("btcSignPsbt", "params.psbt");
|
|
1333
|
+
if (!params.path) return missingParamFailure("btcSignPsbt", "params.path");
|
|
1334
|
+
const accountFailure = btcAccountFailure(splitAccountPath(params.path).accountPath);
|
|
1335
|
+
if (accountFailure) return accountFailure;
|
|
1336
|
+
const requestPsbtHex = exactHex(params.psbt);
|
|
1337
|
+
if (requestPsbtHex === void 0) return invalidHexFailure("psbt");
|
|
1380
1338
|
return this._runJob(connectId, deviceId, async (signal) => {
|
|
1381
1339
|
const { record } = await this._ensureWalletKnown(connectId, deviceId, "btc", signal);
|
|
1382
1340
|
_KeystoneAdapter._throwIfAborted(signal);
|
|
1383
|
-
const requestUr = this.urEngine.buildBtcPsbtRequest(
|
|
1341
|
+
const requestUr = this.urEngine.buildBtcPsbtRequest(requestPsbtHex);
|
|
1384
1342
|
const responseUr = await this._resolveUr(
|
|
1385
1343
|
record,
|
|
1386
1344
|
requestUr,
|
|
@@ -1390,30 +1348,26 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
1390
1348
|
"btcSignPsbt"
|
|
1391
1349
|
);
|
|
1392
1350
|
_KeystoneAdapter._throwIfAborted(signal);
|
|
1393
|
-
const signedPsbt = this.urEngine.parseBtcPsbt(responseUr);
|
|
1351
|
+
const signedPsbt = this.urEngine.parseBtcPsbt(responseUr, requestPsbtHex);
|
|
1394
1352
|
return success({ signedPsbt });
|
|
1395
1353
|
});
|
|
1396
1354
|
}
|
|
1397
|
-
async btcSignMessage(connectIdArg, deviceIdArg,
|
|
1398
|
-
const
|
|
1399
|
-
if (!
|
|
1400
|
-
const connectId =
|
|
1401
|
-
const deviceId = deviceIdArg ?? void 0;
|
|
1402
|
-
if (!params) return failure(HardwareErrorCode2.InvalidParams, "btcSignMessage requires params");
|
|
1355
|
+
async btcSignMessage(connectIdArg, deviceIdArg, paramsArg) {
|
|
1356
|
+
const call = this._beginCall("btcSignMessage", connectIdArg, deviceIdArg, paramsArg);
|
|
1357
|
+
if (!call.success) return call;
|
|
1358
|
+
const { connectId, deviceId, params } = call.payload;
|
|
1403
1359
|
if (!params.path || params.message === void 0) {
|
|
1404
|
-
return
|
|
1405
|
-
HardwareErrorCode2.InvalidParams,
|
|
1406
|
-
"btcSignMessage requires params.path and params.message"
|
|
1407
|
-
);
|
|
1360
|
+
return missingParamFailure("btcSignMessage", "params.path and params.message");
|
|
1408
1361
|
}
|
|
1409
|
-
const messageHex =
|
|
1362
|
+
const messageHex = messageToHex(params.message, params.hex);
|
|
1363
|
+
if (messageHex === void 0) return invalidHexFailure("message");
|
|
1410
1364
|
const path = normalizePath(params.path);
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
}
|
|
1365
|
+
const accountFailure = btcAccountFailure(splitAccountPath(path).accountPath);
|
|
1366
|
+
if (accountFailure) return accountFailure;
|
|
1414
1367
|
return this._signWithWallet({
|
|
1415
1368
|
connectId,
|
|
1416
1369
|
deviceId,
|
|
1370
|
+
path,
|
|
1417
1371
|
chain: "btc",
|
|
1418
1372
|
operationName: "btcSignMessage",
|
|
1419
1373
|
buildRequest: (requestId, xfp) => this.urEngine.buildBtcMessageSignRequest({
|
|
@@ -1441,17 +1395,11 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
1441
1395
|
return success({ masterFingerprint: record.masterFingerprint });
|
|
1442
1396
|
});
|
|
1443
1397
|
}
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
if (!operationTarget.success) return operationTarget;
|
|
1450
|
-
const connectId = operationTarget.payload.targetId;
|
|
1451
|
-
const deviceId = deviceIdArg ?? void 0;
|
|
1452
|
-
if (!params) return failure(HardwareErrorCode2.InvalidParams, "solGetAddress requires params");
|
|
1453
|
-
if (!params.path)
|
|
1454
|
-
return failure(HardwareErrorCode2.InvalidParams, "solGetAddress requires params.path");
|
|
1398
|
+
async solGetAddress(connectIdArg, deviceIdArg, paramsArg, bundle) {
|
|
1399
|
+
const call = this._beginCall("solGetAddress", connectIdArg, deviceIdArg, paramsArg);
|
|
1400
|
+
if (!call.success) return call;
|
|
1401
|
+
const { connectId, deviceId, params } = call.payload;
|
|
1402
|
+
if (!params.path) return missingParamFailure("solGetAddress", "params.path");
|
|
1455
1403
|
const solCoinTypeFailure = coinTypeFailure("sol", params.path);
|
|
1456
1404
|
if (solCoinTypeFailure) return solCoinTypeFailure;
|
|
1457
1405
|
const path = normalizePath(params.path);
|
|
@@ -1471,180 +1419,218 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
1471
1419
|
bundle
|
|
1472
1420
|
);
|
|
1473
1421
|
_KeystoneAdapter._throwIfAborted(signal);
|
|
1422
|
+
if (!/^[0-9a-f]{64}$/i.test(account.publicKey)) {
|
|
1423
|
+
throw createHwkError2({
|
|
1424
|
+
code: HardwareErrorCode2.DeviceMismatch,
|
|
1425
|
+
message: "Keystone returned a Solana key that is not a 32-byte Ed25519 public key"
|
|
1426
|
+
});
|
|
1427
|
+
}
|
|
1474
1428
|
const address = bs58.encode(Buffer.from(account.publicKey, "hex"));
|
|
1475
|
-
return success({
|
|
1429
|
+
return success({
|
|
1430
|
+
address,
|
|
1431
|
+
path,
|
|
1432
|
+
accountKey: { path, publicKey: account.publicKey }
|
|
1433
|
+
});
|
|
1476
1434
|
});
|
|
1477
1435
|
}
|
|
1478
|
-
async solSignTransaction(connectIdArg, deviceIdArg,
|
|
1479
|
-
const
|
|
1480
|
-
if (!
|
|
1481
|
-
const
|
|
1482
|
-
const deviceId = deviceIdArg ?? void 0;
|
|
1483
|
-
if (!params)
|
|
1484
|
-
return failure(HardwareErrorCode2.InvalidParams, "solSignTransaction requires params");
|
|
1436
|
+
async solSignTransaction(connectIdArg, deviceIdArg, paramsArg) {
|
|
1437
|
+
const call = this._beginCall("solSignTransaction", connectIdArg, deviceIdArg, paramsArg);
|
|
1438
|
+
if (!call.success) return call;
|
|
1439
|
+
const { params } = call.payload;
|
|
1485
1440
|
if (!params.path || !params.serializedTx) {
|
|
1486
|
-
return
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
buildRequest: (requestId, xfp) => this.urEngine.buildSolSignRequest({
|
|
1498
|
-
requestId,
|
|
1499
|
-
unsignedPayloadHex: stripHex(params.serializedTx),
|
|
1500
|
-
dataType: "transaction",
|
|
1501
|
-
path,
|
|
1502
|
-
xfp,
|
|
1503
|
-
origin: this._origin
|
|
1504
|
-
}),
|
|
1505
|
-
parseResponse: (ur) => this.urEngine.parseSolSignature(ur),
|
|
1506
|
-
toPayload: (sig) => ({ signature: sig.signature })
|
|
1507
|
-
});
|
|
1441
|
+
return missingParamFailure("solSignTransaction", "params.path and params.serializedTx");
|
|
1442
|
+
}
|
|
1443
|
+
const unsignedPayloadHex = exactHex(params.serializedTx);
|
|
1444
|
+
if (unsignedPayloadHex === void 0) return invalidHexFailure("serializedTx");
|
|
1445
|
+
return this._solSign(
|
|
1446
|
+
"solSignTransaction",
|
|
1447
|
+
call.payload,
|
|
1448
|
+
params.path,
|
|
1449
|
+
unsignedPayloadHex,
|
|
1450
|
+
"transaction"
|
|
1451
|
+
);
|
|
1508
1452
|
}
|
|
1509
|
-
async solSignMessage(connectIdArg, deviceIdArg,
|
|
1510
|
-
const
|
|
1511
|
-
if (!
|
|
1512
|
-
const
|
|
1513
|
-
const deviceId = deviceIdArg ?? void 0;
|
|
1514
|
-
if (!params) return failure(HardwareErrorCode2.InvalidParams, "solSignMessage requires params");
|
|
1453
|
+
async solSignMessage(connectIdArg, deviceIdArg, paramsArg) {
|
|
1454
|
+
const call = this._beginCall("solSignMessage", connectIdArg, deviceIdArg, paramsArg);
|
|
1455
|
+
if (!call.success) return call;
|
|
1456
|
+
const { params } = call.payload;
|
|
1515
1457
|
if (!params.path || !params.message) {
|
|
1458
|
+
return missingParamFailure("solSignMessage", "params.path and params.message");
|
|
1459
|
+
}
|
|
1460
|
+
if (params.messageVersion !== void 0) {
|
|
1516
1461
|
return failure(
|
|
1517
|
-
HardwareErrorCode2.
|
|
1518
|
-
"
|
|
1462
|
+
HardwareErrorCode2.MethodNotSupported,
|
|
1463
|
+
"Keystone signs Solana messages as raw bytes only; off-chain message format is not supported"
|
|
1519
1464
|
);
|
|
1520
1465
|
}
|
|
1521
|
-
const
|
|
1522
|
-
return
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
dataType: "message",
|
|
1531
|
-
path,
|
|
1532
|
-
xfp,
|
|
1533
|
-
origin: this._origin
|
|
1534
|
-
}),
|
|
1535
|
-
parseResponse: (ur) => this.urEngine.parseSolSignature(ur),
|
|
1536
|
-
toPayload: (sig) => ({ signature: sig.signature })
|
|
1537
|
-
});
|
|
1466
|
+
const unsignedPayloadHex = exactHex(params.message);
|
|
1467
|
+
if (unsignedPayloadHex === void 0) return invalidHexFailure("message");
|
|
1468
|
+
return this._solSign(
|
|
1469
|
+
"solSignMessage",
|
|
1470
|
+
call.payload,
|
|
1471
|
+
params.path,
|
|
1472
|
+
unsignedPayloadHex,
|
|
1473
|
+
"message"
|
|
1474
|
+
);
|
|
1538
1475
|
}
|
|
1539
1476
|
// ---------------------------------------------------------------------------
|
|
1540
1477
|
// TRON: keystone-sdk-rust layout (tags 5201/5202, firmware >= 2.5.0), not sdk.tron's
|
|
1541
1478
|
// protobuf module; OneKey Pro's encoding under the same tag is rejected by the firmware.
|
|
1542
1479
|
// ---------------------------------------------------------------------------
|
|
1543
|
-
async tronGetAddress(connectIdArg, deviceIdArg,
|
|
1544
|
-
const
|
|
1545
|
-
if (!
|
|
1546
|
-
const
|
|
1547
|
-
|
|
1548
|
-
if (!params) return failure(HardwareErrorCode2.InvalidParams, "tronGetAddress requires params");
|
|
1549
|
-
if (!params.path)
|
|
1550
|
-
return failure(HardwareErrorCode2.InvalidParams, "tronGetAddress requires params.path");
|
|
1480
|
+
async tronGetAddress(connectIdArg, deviceIdArg, paramsArg, bundle) {
|
|
1481
|
+
const call = this._beginCall("tronGetAddress", connectIdArg, deviceIdArg, paramsArg);
|
|
1482
|
+
if (!call.success) return call;
|
|
1483
|
+
const { params } = call.payload;
|
|
1484
|
+
if (!params.path) return missingParamFailure("tronGetAddress", "params.path");
|
|
1551
1485
|
const tronCoinTypeFailure = coinTypeFailure("tron", params.path);
|
|
1552
1486
|
if (tronCoinTypeFailure) return tronCoinTypeFailure;
|
|
1553
|
-
|
|
1487
|
+
return this._addressFromAccountXpub(
|
|
1488
|
+
"tronGetAddress",
|
|
1489
|
+
"tron",
|
|
1490
|
+
call.payload,
|
|
1491
|
+
params.path,
|
|
1492
|
+
"m/44'/195'/0'/0/0",
|
|
1493
|
+
bundle,
|
|
1494
|
+
(xpub, relativeDerivePath) => this.urEngine.deriveTronAddressFromXpub(xpub, relativeDerivePath)
|
|
1495
|
+
);
|
|
1496
|
+
}
|
|
1497
|
+
async tronSignTransaction(connectIdArg, deviceIdArg, paramsArg) {
|
|
1498
|
+
const call = this._beginCall("tronSignTransaction", connectIdArg, deviceIdArg, paramsArg);
|
|
1499
|
+
if (!call.success) return call;
|
|
1500
|
+
const { params } = call.payload;
|
|
1501
|
+
if (!params.path) return missingParamFailure("tronSignTransaction", "params.path");
|
|
1502
|
+
if (!params.rawTxHex) {
|
|
1503
|
+
return failure(
|
|
1504
|
+
HardwareErrorCode2.InvalidParams,
|
|
1505
|
+
"Keystone only signs a fully protobuf-serialized TRON transaction (params.rawTxHex) \u2014 the Trezor-style structured contract fields have no equivalent here"
|
|
1506
|
+
);
|
|
1507
|
+
}
|
|
1508
|
+
const rawTxHex = exactHex(params.rawTxHex);
|
|
1509
|
+
if (rawTxHex === void 0) return invalidHexFailure("rawTxHex");
|
|
1510
|
+
return this._tronSign("tronSignTransaction", call.payload, params.path, rawTxHex);
|
|
1511
|
+
}
|
|
1512
|
+
async tronSignMessage(connectIdArg, deviceIdArg, paramsArg) {
|
|
1513
|
+
const call = this._beginCall("tronSignMessage", connectIdArg, deviceIdArg, paramsArg);
|
|
1514
|
+
if (!call.success) return call;
|
|
1515
|
+
const { params } = call.payload;
|
|
1516
|
+
if (!params.path) return missingParamFailure("tronSignMessage", "params.path");
|
|
1517
|
+
if (!params.messageHex) return missingParamFailure("tronSignMessage", "params.messageHex");
|
|
1518
|
+
if (params.messageType !== "V2") {
|
|
1519
|
+
return failure(
|
|
1520
|
+
HardwareErrorCode2.MethodNotSupported,
|
|
1521
|
+
'Keystone signs TRON messages as TIP-191 signMessageV2 only (messageType: "V2")'
|
|
1522
|
+
);
|
|
1523
|
+
}
|
|
1524
|
+
const messageHex = exactHex(params.messageHex);
|
|
1525
|
+
if (messageHex === void 0) return invalidHexFailure("messageHex");
|
|
1526
|
+
return this._tronSign(
|
|
1527
|
+
"tronSignMessage",
|
|
1528
|
+
call.payload,
|
|
1529
|
+
params.path,
|
|
1530
|
+
messageHex,
|
|
1531
|
+
2 /* PersonalMessage */
|
|
1532
|
+
);
|
|
1533
|
+
}
|
|
1534
|
+
/** Resolves the call's target and requires params, before any method-specific check. */
|
|
1535
|
+
_beginCall(method, connectIdArg, deviceIdArg, params) {
|
|
1536
|
+
const operationTarget = this._resolveOperationTarget(connectIdArg, params?.operationId);
|
|
1537
|
+
if (!operationTarget.success) return operationTarget;
|
|
1538
|
+
if (!params) return missingParamFailure(method, "params");
|
|
1539
|
+
return success({
|
|
1540
|
+
connectId: operationTarget.payload.targetId,
|
|
1541
|
+
deviceId: deviceIdArg ?? void 0,
|
|
1542
|
+
params
|
|
1543
|
+
});
|
|
1544
|
+
}
|
|
1545
|
+
/** Derives a leaf address offline from its account xpub. */
|
|
1546
|
+
async _addressFromAccountXpub(method, chain, { connectId, deviceId }, path, leafExample, bundle, derive) {
|
|
1547
|
+
const normalizedPath = normalizePath(path);
|
|
1548
|
+
const { accountPath, relativeDerivePath } = splitAccountPath(normalizedPath);
|
|
1554
1549
|
if (!relativeDerivePath) {
|
|
1555
1550
|
return failure(
|
|
1556
1551
|
HardwareErrorCode2.InvalidParams,
|
|
1557
|
-
|
|
1552
|
+
`${method} requires a full leaf path, e.g. ${leafExample}`
|
|
1558
1553
|
);
|
|
1559
1554
|
}
|
|
1555
|
+
const accountFailure = chain === "btc" ? btcAccountFailure(accountPath) : void 0;
|
|
1556
|
+
if (accountFailure) return accountFailure;
|
|
1560
1557
|
return this._runJob(connectId, deviceId, async (signal) => {
|
|
1561
1558
|
const xpub = await this._fetchAccountXpub(
|
|
1562
1559
|
connectId,
|
|
1563
1560
|
deviceId,
|
|
1564
|
-
|
|
1561
|
+
chain,
|
|
1565
1562
|
accountPath,
|
|
1566
1563
|
signal,
|
|
1567
1564
|
bundle
|
|
1568
1565
|
);
|
|
1569
|
-
|
|
1570
|
-
|
|
1566
|
+
return success({
|
|
1567
|
+
address: derive(xpub, relativeDerivePath),
|
|
1568
|
+
path: normalizedPath,
|
|
1569
|
+
accountKey: this._exportedAccountKey(accountPath, xpub)
|
|
1570
|
+
});
|
|
1571
1571
|
});
|
|
1572
1572
|
}
|
|
1573
|
-
|
|
1574
|
-
const
|
|
1575
|
-
if (!operationTarget.success) return operationTarget;
|
|
1576
|
-
const connectId = operationTarget.payload.targetId;
|
|
1577
|
-
const deviceId = deviceIdArg ?? void 0;
|
|
1578
|
-
if (!params)
|
|
1579
|
-
return failure(HardwareErrorCode2.InvalidParams, "tronSignTransaction requires params");
|
|
1580
|
-
if (!params.path) {
|
|
1581
|
-
return failure(HardwareErrorCode2.InvalidParams, "tronSignTransaction requires params.path");
|
|
1582
|
-
}
|
|
1583
|
-
if (!params.rawTxHex) {
|
|
1584
|
-
return failure(
|
|
1585
|
-
HardwareErrorCode2.InvalidParams,
|
|
1586
|
-
"Keystone only signs a fully protobuf-serialized TRON transaction (params.rawTxHex) \u2014 the Trezor-style structured contract fields have no equivalent here"
|
|
1587
|
-
);
|
|
1588
|
-
}
|
|
1589
|
-
const rawTxHex = stripHex(params.rawTxHex);
|
|
1590
|
-
const path = normalizePath(params.path);
|
|
1573
|
+
_evmSign(method, target, rawPath, chainId, signDataHex, dataType, toPayload) {
|
|
1574
|
+
const path = normalizePath(rawPath);
|
|
1591
1575
|
return this._signWithWallet({
|
|
1592
|
-
connectId,
|
|
1593
|
-
deviceId,
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1576
|
+
connectId: target.connectId,
|
|
1577
|
+
deviceId: target.deviceId,
|
|
1578
|
+
path,
|
|
1579
|
+
chain: "evm",
|
|
1580
|
+
operationName: method,
|
|
1581
|
+
buildRequest: (requestId, xfp) => this.urEngine.buildEthSignRequest({
|
|
1582
|
+
requestId,
|
|
1583
|
+
unsignedTxHex: signDataHex,
|
|
1584
|
+
dataType,
|
|
1585
|
+
path,
|
|
1586
|
+
xfp,
|
|
1587
|
+
chainId,
|
|
1588
|
+
origin: this._origin
|
|
1589
|
+
}),
|
|
1590
|
+
parseResponse: (ur) => this.urEngine.parseEthSignature(ur),
|
|
1591
|
+
toPayload
|
|
1592
|
+
});
|
|
1593
|
+
}
|
|
1594
|
+
_solSign(method, target, rawPath, unsignedPayloadHex, dataType) {
|
|
1595
|
+
const path = normalizePath(rawPath);
|
|
1596
|
+
return this._signWithWallet({
|
|
1597
|
+
connectId: target.connectId,
|
|
1598
|
+
deviceId: target.deviceId,
|
|
1599
|
+
path,
|
|
1600
|
+
chain: "sol",
|
|
1601
|
+
operationName: method,
|
|
1602
|
+
buildRequest: (requestId, xfp) => this.urEngine.buildSolSignRequest({
|
|
1597
1603
|
requestId,
|
|
1598
|
-
|
|
1604
|
+
unsignedPayloadHex,
|
|
1605
|
+
dataType,
|
|
1599
1606
|
path,
|
|
1600
1607
|
xfp,
|
|
1601
1608
|
origin: this._origin
|
|
1602
1609
|
}),
|
|
1603
|
-
parseResponse: (ur) => this.urEngine.
|
|
1604
|
-
// Keystone signs rawTxHex as given and returns only the signature, so no serializedTx.
|
|
1610
|
+
parseResponse: (ur) => this.urEngine.parseSolSignature(ur),
|
|
1605
1611
|
toPayload: (sig) => ({ signature: sig.signature })
|
|
1606
1612
|
});
|
|
1607
1613
|
}
|
|
1608
|
-
|
|
1609
|
-
const
|
|
1610
|
-
if (!operationTarget.success) return operationTarget;
|
|
1611
|
-
const connectId = operationTarget.payload.targetId;
|
|
1612
|
-
const deviceId = deviceIdArg ?? void 0;
|
|
1613
|
-
if (!params) return failure(HardwareErrorCode2.InvalidParams, "tronSignMessage requires params");
|
|
1614
|
-
if (!params.path) {
|
|
1615
|
-
return failure(HardwareErrorCode2.InvalidParams, "tronSignMessage requires params.path");
|
|
1616
|
-
}
|
|
1617
|
-
if (!params.messageHex) {
|
|
1618
|
-
return failure(HardwareErrorCode2.InvalidParams, "tronSignMessage requires params.messageHex");
|
|
1619
|
-
}
|
|
1620
|
-
if (params.messageType !== "V2") {
|
|
1621
|
-
return failure(
|
|
1622
|
-
HardwareErrorCode2.MethodNotSupported,
|
|
1623
|
-
'Keystone signs TRON messages as TIP-191 signMessageV2 only (messageType: "V2")'
|
|
1624
|
-
);
|
|
1625
|
-
}
|
|
1626
|
-
const messageHex = stripHex(params.messageHex);
|
|
1627
|
-
const path = normalizePath(params.path);
|
|
1614
|
+
_tronSign(method, target, rawPath, signDataHex, signType) {
|
|
1615
|
+
const path = normalizePath(rawPath);
|
|
1628
1616
|
return this._signWithWallet({
|
|
1629
|
-
connectId,
|
|
1630
|
-
deviceId,
|
|
1617
|
+
connectId: target.connectId,
|
|
1618
|
+
deviceId: target.deviceId,
|
|
1619
|
+
path,
|
|
1631
1620
|
chain: "tron",
|
|
1632
|
-
operationName:
|
|
1621
|
+
operationName: method,
|
|
1633
1622
|
buildRequest: (requestId, xfp) => this.urEngine.buildTronSignRequest({
|
|
1634
1623
|
requestId,
|
|
1635
|
-
rawTxHex:
|
|
1624
|
+
rawTxHex: signDataHex,
|
|
1636
1625
|
path,
|
|
1637
1626
|
xfp,
|
|
1638
|
-
signType
|
|
1627
|
+
signType,
|
|
1639
1628
|
origin: this._origin
|
|
1640
1629
|
}),
|
|
1641
1630
|
parseResponse: (ur) => this.urEngine.parseTronSignature(ur),
|
|
1642
1631
|
toPayload: (sig) => ({ signature: sig.signature })
|
|
1643
1632
|
});
|
|
1644
1633
|
}
|
|
1645
|
-
// ---------------------------------------------------------------------------
|
|
1646
|
-
// Internals
|
|
1647
|
-
// ---------------------------------------------------------------------------
|
|
1648
1634
|
async _runJob(connectId, deviceId, job) {
|
|
1649
1635
|
try {
|
|
1650
1636
|
return await this._jobQueue.enqueue(keystoneQueueKey(connectId, deviceId), job);
|
|
@@ -1668,14 +1654,33 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
1668
1654
|
message: "Keystone did not return an extended public key for this account path"
|
|
1669
1655
|
});
|
|
1670
1656
|
}
|
|
1657
|
+
const { publicKey } = this.urEngine.parseXpubMeta(account.extendedPublicKey);
|
|
1658
|
+
if (!/^0[23]/.test(publicKey)) {
|
|
1659
|
+
throw createHwkError2({
|
|
1660
|
+
code: HardwareErrorCode2.DeviceMismatch,
|
|
1661
|
+
message: "Keystone returned an account key that is not a public key"
|
|
1662
|
+
});
|
|
1663
|
+
}
|
|
1671
1664
|
return account.extendedPublicKey;
|
|
1672
1665
|
}
|
|
1666
|
+
/** The exported key behind an address, reduced to the fields that identify it. */
|
|
1667
|
+
_exportedAccountKey(accountPath, xpub) {
|
|
1668
|
+
const { publicKey, chainCode } = this.urEngine.parseXpubMeta(xpub);
|
|
1669
|
+
return { path: normalizePath(accountPath), publicKey, chainCode, xpub };
|
|
1670
|
+
}
|
|
1673
1671
|
/**
|
|
1674
1672
|
* Signing needs only the wallet's mfp: the device re-derives the key from path+xfp, so no
|
|
1675
1673
|
* per-path account lookup happens here.
|
|
1676
1674
|
*/
|
|
1677
1675
|
_signWithWallet(args) {
|
|
1678
1676
|
const { connectId, deviceId } = args;
|
|
1677
|
+
if (!isBip32Path(args.path)) {
|
|
1678
|
+
return Promise.resolve(
|
|
1679
|
+
failure(HardwareErrorCode2.InvalidParams, `Invalid derivation path (${args.path})`)
|
|
1680
|
+
);
|
|
1681
|
+
}
|
|
1682
|
+
const chainCoinTypeFailure = coinTypeFailure(args.chain, args.path);
|
|
1683
|
+
if (chainCoinTypeFailure) return Promise.resolve(chainCoinTypeFailure);
|
|
1679
1684
|
return this._runJob(connectId, deviceId, async (signal) => {
|
|
1680
1685
|
const { record } = await this._ensureWalletKnown(connectId, deviceId, args.chain, signal);
|
|
1681
1686
|
_KeystoneAdapter._throwIfAborted(signal);
|
|
@@ -1712,8 +1717,6 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
1712
1717
|
case "btcGetPublicKey":
|
|
1713
1718
|
case "solGetAddress":
|
|
1714
1719
|
return { hwkChain, path };
|
|
1715
|
-
default:
|
|
1716
|
-
return void 0;
|
|
1717
1720
|
}
|
|
1718
1721
|
}
|
|
1719
1722
|
/**
|
|
@@ -1757,7 +1760,7 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
1757
1760
|
schemas: schemas.map((schema) => keySchema(schema.hwkChain, schema.path)),
|
|
1758
1761
|
origin: this._origin
|
|
1759
1762
|
});
|
|
1760
|
-
const responseUr = await this.
|
|
1763
|
+
const responseUr = await this._requestQrAndAwaitResponse(
|
|
1761
1764
|
record ? toDeviceInfo(record) : placeholderDeviceInfo(),
|
|
1762
1765
|
{ ...requestUr, animated: false },
|
|
1763
1766
|
signal
|
|
@@ -1765,26 +1768,16 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
1765
1768
|
_KeystoneAdapter._throwIfAborted(signal);
|
|
1766
1769
|
const parsed = this.urEngine.parseAccountResponse(responseUr);
|
|
1767
1770
|
this._assertParsedIdentity(parsed, target);
|
|
1768
|
-
record = this._upsertDeviceRecord(parsed);
|
|
1769
1771
|
for (const schema of schemas) {
|
|
1770
|
-
const account = parsed.accounts.find(
|
|
1771
|
-
(returned) => normalizePath(returned.path) === schema.path
|
|
1772
|
-
);
|
|
1773
|
-
if (!account) {
|
|
1774
|
-
throw createHwkError2({
|
|
1775
|
-
code: HardwareErrorCode2.DeviceMismatch,
|
|
1776
|
-
message: `Keystone did not return the requested derivation path (${schema.path})`
|
|
1777
|
-
});
|
|
1778
|
-
}
|
|
1779
1772
|
scanned.set(accountKey(schema.hwkChain, schema.path), {
|
|
1780
|
-
...
|
|
1773
|
+
...requireExportedPath(parsed, schema.path),
|
|
1781
1774
|
hwkChain: schema.hwkChain
|
|
1782
1775
|
});
|
|
1783
1776
|
}
|
|
1777
|
+
record = this._upsertDeviceRecord(parsed);
|
|
1784
1778
|
return { access: { channel: "qr", scanned }, masterFingerprint: record.masterFingerprint };
|
|
1785
1779
|
});
|
|
1786
1780
|
}
|
|
1787
|
-
/** Resolve the wallet's master fingerprint from the connectId and/or deviceId the caller passed. */
|
|
1788
1781
|
/**
|
|
1789
1782
|
* A positional wallet id and an operation id must name the same wallet: otherwise the call
|
|
1790
1783
|
* would run on the operation's wallet while the caller files the result under the other one.
|
|
@@ -1839,7 +1832,8 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
1839
1832
|
if (expected.expectedMasterFingerprint && parsed.masterFingerprint !== expected.expectedMasterFingerprint) {
|
|
1840
1833
|
throw createHwkError2({
|
|
1841
1834
|
code: HardwareErrorCode2.DeviceMismatch,
|
|
1842
|
-
|
|
1835
|
+
// The master fingerprint identifies the wallet; keep it out of messages that reach logs.
|
|
1836
|
+
message: "Connected Keystone wallet does not match the requested wallet"
|
|
1843
1837
|
});
|
|
1844
1838
|
}
|
|
1845
1839
|
}
|
|
@@ -1869,7 +1863,7 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
1869
1863
|
* Opens a permitted USB Keystone and takes its mfp as the wallet identity. A known wallet gains
|
|
1870
1864
|
* USB in place (`device-changed`); an unseen one becomes USB-only.
|
|
1871
1865
|
*/
|
|
1872
|
-
async _connectUsb(expected, searchTargetId, signal) {
|
|
1866
|
+
async _connectUsb(connector, expected, searchTargetId, signal) {
|
|
1873
1867
|
if (this._pendingUsbTeardowns > 0 || this._abandonedUsbConnects > 0) {
|
|
1874
1868
|
return failure(
|
|
1875
1869
|
HardwareErrorCode2.DeviceBusyInternal,
|
|
@@ -1882,28 +1876,22 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
1882
1876
|
abandoned = true;
|
|
1883
1877
|
this._abandonedUsbConnects += 1;
|
|
1884
1878
|
};
|
|
1885
|
-
signal
|
|
1879
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
1886
1880
|
const pending = this._usbConnectTail.then(async () => {
|
|
1887
1881
|
_KeystoneAdapter._throwIfAborted(signal);
|
|
1888
|
-
return this._connectUsbExclusive(expected, searchTargetId, signal);
|
|
1882
|
+
return this._connectUsbExclusive(connector, expected, searchTargetId, signal);
|
|
1889
1883
|
});
|
|
1890
1884
|
const settled = pending.finally(() => {
|
|
1891
|
-
signal
|
|
1885
|
+
signal.removeEventListener("abort", onAbort);
|
|
1892
1886
|
if (abandoned) this._abandonedUsbConnects -= 1;
|
|
1893
1887
|
});
|
|
1894
1888
|
this._usbConnectTail = settled.then(
|
|
1895
1889
|
() => void 0,
|
|
1896
1890
|
() => void 0
|
|
1897
1891
|
);
|
|
1898
|
-
return
|
|
1892
|
+
return _KeystoneAdapter._abortable(signal, settled);
|
|
1899
1893
|
}
|
|
1900
|
-
async _connectUsbExclusive(expected, searchTargetId, signal) {
|
|
1901
|
-
if (!this._usbConnector) {
|
|
1902
|
-
return failure(
|
|
1903
|
-
HardwareErrorCode2.TransportNotAvailable,
|
|
1904
|
-
"No USB connector configured for this Keystone adapter"
|
|
1905
|
-
);
|
|
1906
|
-
}
|
|
1894
|
+
async _connectUsbExclusive(connector, expected, searchTargetId, signal) {
|
|
1907
1895
|
let sessionId;
|
|
1908
1896
|
try {
|
|
1909
1897
|
let connectTarget = { type: "default" };
|
|
@@ -1915,7 +1903,6 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
1915
1903
|
deviceIdentity: expected.expectedMasterFingerprint
|
|
1916
1904
|
};
|
|
1917
1905
|
}
|
|
1918
|
-
const connector = this._usbConnector;
|
|
1919
1906
|
const session = await (connector.connectTarget ? connector.connectTarget(connectTarget) : connector.connect(searchTargetId ?? expected.expectedMasterFingerprint));
|
|
1920
1907
|
sessionId = session.sessionId;
|
|
1921
1908
|
_KeystoneAdapter._throwIfAborted(signal);
|
|
@@ -1929,16 +1916,11 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
1929
1916
|
});
|
|
1930
1917
|
}
|
|
1931
1918
|
const masterFingerprint = mfpValue.toLowerCase();
|
|
1932
|
-
|
|
1933
|
-
throw createHwkError2({
|
|
1934
|
-
code: HardwareErrorCode2.DeviceMismatch,
|
|
1935
|
-
message: `Connected Keystone wallet (mfp ${masterFingerprint}) does not match the requested wallet fingerprint (${expected.expectedMasterFingerprint})`
|
|
1936
|
-
});
|
|
1937
|
-
}
|
|
1919
|
+
this._assertParsedIdentity({ masterFingerprint, accounts: [] }, expected);
|
|
1938
1920
|
const previousUsbSessionId = this._devices.get(masterFingerprint)?.usbSessionId;
|
|
1939
1921
|
if (previousUsbSessionId && previousUsbSessionId !== session.sessionId) {
|
|
1940
1922
|
try {
|
|
1941
|
-
await
|
|
1923
|
+
await connector.disconnect(previousUsbSessionId);
|
|
1942
1924
|
} catch {
|
|
1943
1925
|
}
|
|
1944
1926
|
}
|
|
@@ -1958,7 +1940,7 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
1958
1940
|
} catch (err) {
|
|
1959
1941
|
if (sessionId) {
|
|
1960
1942
|
try {
|
|
1961
|
-
await
|
|
1943
|
+
await connector.disconnect(sessionId);
|
|
1962
1944
|
} catch {
|
|
1963
1945
|
}
|
|
1964
1946
|
}
|
|
@@ -2004,9 +1986,14 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
2004
1986
|
}
|
|
2005
1987
|
return void 0;
|
|
2006
1988
|
}
|
|
2007
|
-
const attached = await this._connectUsb(
|
|
1989
|
+
const attached = await this._connectUsb(
|
|
1990
|
+
this._usbConnector,
|
|
1991
|
+
{ expectedMasterFingerprint },
|
|
1992
|
+
void 0,
|
|
1993
|
+
signal
|
|
1994
|
+
);
|
|
2008
1995
|
_KeystoneAdapter._throwIfAborted(signal);
|
|
2009
|
-
if (!attached.success && attached.payload.code === HardwareErrorCode2.DeviceBusyInternal) {
|
|
1996
|
+
if (!attached.success && (attached.payload.code === HardwareErrorCode2.DeviceBusyInternal || attached.payload.code === HardwareErrorCode2.DeviceOneDeviceOnly)) {
|
|
2010
1997
|
throw createHwkError2({
|
|
2011
1998
|
...attached.payload,
|
|
2012
1999
|
message: attached.payload.error
|
|
@@ -2066,9 +2053,9 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
2066
2053
|
message: "USB channel is not connected for this Keystone wallet"
|
|
2067
2054
|
});
|
|
2068
2055
|
}
|
|
2069
|
-
const result = await this.
|
|
2056
|
+
const result = await this._callUsbResolveUr(
|
|
2057
|
+
this._usbConnector,
|
|
2070
2058
|
record.usbSessionId,
|
|
2071
|
-
"resolveUr",
|
|
2072
2059
|
requestUr,
|
|
2073
2060
|
signal
|
|
2074
2061
|
);
|
|
@@ -2078,11 +2065,7 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
2078
2065
|
if (usbErrorCode === HardwareErrorCode2.PayloadTooLarge && usbErrorOrigin !== "device") {
|
|
2079
2066
|
if (!interactionRoute && !this._forcedTransport) {
|
|
2080
2067
|
const displayDevice2 = toDeviceInfo(record);
|
|
2081
|
-
return this.
|
|
2082
|
-
displayDevice2,
|
|
2083
|
-
{ ...requestUr, animated },
|
|
2084
|
-
signal
|
|
2085
|
-
);
|
|
2068
|
+
return this._requestQrAndAwaitResponse(displayDevice2, { ...requestUr, animated }, signal);
|
|
2086
2069
|
}
|
|
2087
2070
|
throw usbError;
|
|
2088
2071
|
}
|
|
@@ -2093,6 +2076,7 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
2093
2076
|
this._usbConnector?.disconnect(lostSessionId).catch(() => void 0);
|
|
2094
2077
|
}
|
|
2095
2078
|
record.usbSessionId = void 0;
|
|
2079
|
+
this.emitter.emit(DEVICE.CHANGED, { type: DEVICE.CHANGED, payload: toDeviceInfo(record) });
|
|
2096
2080
|
if (interactionRoute) {
|
|
2097
2081
|
throw this._endDisconnectedOperation(
|
|
2098
2082
|
interactionRoute.operationId,
|
|
@@ -2110,11 +2094,20 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
2110
2094
|
});
|
|
2111
2095
|
}
|
|
2112
2096
|
}
|
|
2097
|
+
if (operationName && usbError.replyUnreadable === true) {
|
|
2098
|
+
throw createHwkError2({
|
|
2099
|
+
code: usbErrorCode ?? HardwareErrorCode2.UnknownError,
|
|
2100
|
+
message: `Keystone ${operationName} may have completed but its reply could not be read`,
|
|
2101
|
+
origin: "device",
|
|
2102
|
+
recovery: { scope: "unknown" },
|
|
2103
|
+
params: operationMayHaveCompletedParams(operationName)
|
|
2104
|
+
});
|
|
2105
|
+
}
|
|
2113
2106
|
throw usbError;
|
|
2114
2107
|
}
|
|
2115
2108
|
const displayDevice = record ? toDeviceInfo(record) : placeholderDeviceInfo();
|
|
2116
2109
|
try {
|
|
2117
|
-
return await this.
|
|
2110
|
+
return await this._requestQrAndAwaitResponse(
|
|
2118
2111
|
displayDevice,
|
|
2119
2112
|
{ ...requestUr, animated },
|
|
2120
2113
|
signal
|
|
@@ -2178,6 +2171,7 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
2178
2171
|
const responseUr = await this._resolveUr(existingRecord, requestUr, false, connectId, signal);
|
|
2179
2172
|
_KeystoneAdapter._throwIfAborted(signal);
|
|
2180
2173
|
const parsed = this.urEngine.parseAccountResponse(responseUr);
|
|
2174
|
+
const account = requireExportedPath(parsed, syncPath);
|
|
2181
2175
|
let record;
|
|
2182
2176
|
if (useSinglePathUsbExport && existingRecord) {
|
|
2183
2177
|
if (parsed.masterFingerprint !== existingRecord.masterFingerprint) {
|
|
@@ -2193,13 +2187,6 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
2193
2187
|
viaUsb: operationConnectionType !== "qr" && Boolean(existingRecord?.usbSessionId)
|
|
2194
2188
|
});
|
|
2195
2189
|
}
|
|
2196
|
-
const account = parsed.accounts.find((a) => normalizePath(a.path) === syncPath);
|
|
2197
|
-
if (!account) {
|
|
2198
|
-
throw createHwkError2({
|
|
2199
|
-
code: HardwareErrorCode2.DeviceMismatch,
|
|
2200
|
-
message: `Keystone did not return the requested derivation path (${syncPath})`
|
|
2201
|
-
});
|
|
2202
|
-
}
|
|
2203
2190
|
return { record, account: { ...account, hwkChain } };
|
|
2204
2191
|
}
|
|
2205
2192
|
/**
|
|
@@ -2222,7 +2209,9 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
2222
2209
|
});
|
|
2223
2210
|
const responseUr = await this._resolveUr(void 0, requestUr, false, connectId, signal);
|
|
2224
2211
|
_KeystoneAdapter._throwIfAborted(signal);
|
|
2225
|
-
|
|
2212
|
+
const parsed = this.urEngine.parseAccountResponse(responseUr);
|
|
2213
|
+
requireExportedPath(parsed, normalizePath(accountPath));
|
|
2214
|
+
return { record: this._upsertDeviceRecord(parsed) };
|
|
2226
2215
|
}
|
|
2227
2216
|
/**
|
|
2228
2217
|
* The operation owning the running job, so a mid-call UI request can name it. Unpinned jobs map
|
|
@@ -2248,7 +2237,6 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
2248
2237
|
* a cancel reach the wait before the QR timeout.
|
|
2249
2238
|
*/
|
|
2250
2239
|
async _awaitQrResponse(requestType, pending, signal) {
|
|
2251
|
-
if (!signal) return pending;
|
|
2252
2240
|
let settled = false;
|
|
2253
2241
|
const onAbort = () => {
|
|
2254
2242
|
if (!settled) this._uiRegistry.cancel(requestType);
|
|
@@ -2257,55 +2245,50 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
2257
2245
|
else signal.addEventListener("abort", onAbort, { once: true });
|
|
2258
2246
|
try {
|
|
2259
2247
|
return await pending;
|
|
2248
|
+
} catch (error) {
|
|
2249
|
+
const reason = signal.reason;
|
|
2250
|
+
if (signal.aborted && reason?.code === HardwareErrorCode2.OperationEnded) throw signal.reason;
|
|
2251
|
+
throw error;
|
|
2260
2252
|
} finally {
|
|
2261
2253
|
settled = true;
|
|
2262
2254
|
signal.removeEventListener("abort", onAbort);
|
|
2263
2255
|
}
|
|
2264
2256
|
}
|
|
2265
|
-
|
|
2257
|
+
/** Shows `data` for the device to scan, or with no `data` asks the host to scan the device. */
|
|
2258
|
+
async _requestQrAndAwaitResponse(device, data, signal) {
|
|
2259
|
+
const requestType = data ? UI_REQUEST.REQUEST_QR_DISPLAY : UI_REQUEST.REQUEST_QR_SCAN;
|
|
2266
2260
|
const operationId = this._activeOperationId();
|
|
2267
|
-
const
|
|
2268
|
-
|
|
2269
|
-
|
|
2270
|
-
|
|
2271
|
-
|
|
2272
|
-
type: UI_REQUEST.REQUEST_QR_DISPLAY,
|
|
2273
|
-
payload: { device, data, operationId }
|
|
2274
|
-
});
|
|
2275
|
-
const response = await this._awaitQrResponse(
|
|
2276
|
-
UI_REQUEST.REQUEST_QR_DISPLAY,
|
|
2277
|
-
waitPromise,
|
|
2278
|
-
signal
|
|
2279
|
-
);
|
|
2280
|
-
return { urType: response.urType, urData: response.urData };
|
|
2281
|
-
}
|
|
2282
|
-
async _requestQrScanAndAwaitResponse(device, signal) {
|
|
2283
|
-
const operationId = this._activeOperationId();
|
|
2284
|
-
const waitPromise = this._uiRegistry.wait(
|
|
2285
|
-
UI_REQUEST.REQUEST_QR_SCAN,
|
|
2286
|
-
{ timeoutMs: this._qrTimeoutMs, operationId }
|
|
2287
|
-
);
|
|
2288
|
-
this.emitter.emit(UI_REQUEST.REQUEST_QR_SCAN, {
|
|
2289
|
-
type: UI_REQUEST.REQUEST_QR_SCAN,
|
|
2290
|
-
payload: { device, operationId }
|
|
2291
|
-
});
|
|
2292
|
-
const response = await this._awaitQrResponse(UI_REQUEST.REQUEST_QR_SCAN, waitPromise, signal);
|
|
2293
|
-
return { urType: response.urType, urData: response.urData };
|
|
2294
|
-
}
|
|
2295
|
-
async _callUsbConnector(sessionId, method, params, signal) {
|
|
2296
|
-
if (!this._usbConnector) {
|
|
2297
|
-
throw createHwkError2({
|
|
2298
|
-
code: HardwareErrorCode2.TransportNotAvailable,
|
|
2299
|
-
message: "No USB connector configured for this Keystone adapter"
|
|
2261
|
+
const release = operationId ? this._operations.retain(operationId) : void 0;
|
|
2262
|
+
try {
|
|
2263
|
+
const waitPromise = this._uiRegistry.wait(requestType, {
|
|
2264
|
+
timeoutMs: this._qrTimeoutMs,
|
|
2265
|
+
operationId
|
|
2300
2266
|
});
|
|
2267
|
+
if (data) {
|
|
2268
|
+
this.emitter.emit(UI_REQUEST.REQUEST_QR_DISPLAY, {
|
|
2269
|
+
type: UI_REQUEST.REQUEST_QR_DISPLAY,
|
|
2270
|
+
payload: { device, data, operationId }
|
|
2271
|
+
});
|
|
2272
|
+
} else {
|
|
2273
|
+
this.emitter.emit(UI_REQUEST.REQUEST_QR_SCAN, {
|
|
2274
|
+
type: UI_REQUEST.REQUEST_QR_SCAN,
|
|
2275
|
+
payload: { device, operationId }
|
|
2276
|
+
});
|
|
2277
|
+
}
|
|
2278
|
+
const response = await this._awaitQrResponse(requestType, waitPromise, signal);
|
|
2279
|
+
return { urType: response.urType, urData: response.urData };
|
|
2280
|
+
} finally {
|
|
2281
|
+
release?.();
|
|
2301
2282
|
}
|
|
2283
|
+
}
|
|
2284
|
+
async _callUsbResolveUr(connector, sessionId, requestUr, signal) {
|
|
2302
2285
|
if (this._pendingUsbTeardowns > 0 || this._unsettledUsbOperations.size > 0) {
|
|
2303
2286
|
throw createHwkError2({
|
|
2304
2287
|
code: HardwareErrorCode2.DeviceBusyInternal,
|
|
2305
|
-
message:
|
|
2288
|
+
message: "Keystone USB is busy while calling resolveUr"
|
|
2306
2289
|
});
|
|
2307
2290
|
}
|
|
2308
|
-
if (signal
|
|
2291
|
+
if (signal.aborted) {
|
|
2309
2292
|
throw signal.reason instanceof Error ? signal.reason : createHwkError2({
|
|
2310
2293
|
code: HardwareErrorCode2.UserAborted,
|
|
2311
2294
|
message: "User aborted operation"
|
|
@@ -2314,12 +2297,12 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
2314
2297
|
const releaseOperation = this._retainUsbOperation(`call:${sessionId}`);
|
|
2315
2298
|
let rawCall;
|
|
2316
2299
|
try {
|
|
2317
|
-
rawCall =
|
|
2300
|
+
rawCall = connector.call(sessionId, "resolveUr", requestUr).finally(releaseOperation);
|
|
2318
2301
|
} catch (error) {
|
|
2319
2302
|
releaseOperation();
|
|
2320
2303
|
throw error;
|
|
2321
2304
|
}
|
|
2322
|
-
return
|
|
2305
|
+
return _KeystoneAdapter._abortable(signal, rawCall);
|
|
2323
2306
|
}
|
|
2324
2307
|
_runUsbTeardown(task) {
|
|
2325
2308
|
const previous = this._usbTeardownTail;
|
|
@@ -2381,6 +2364,16 @@ var KeystoneAdapter = class _KeystoneAdapter {
|
|
|
2381
2364
|
);
|
|
2382
2365
|
});
|
|
2383
2366
|
}
|
|
2367
|
+
/** Settles with `signal`'s reason once it aborts; a later outcome of `promise` is dropped. */
|
|
2368
|
+
static _untilAborted(signal, promise) {
|
|
2369
|
+
promise.catch(() => void 0);
|
|
2370
|
+
if (signal.aborted) return Promise.reject(signal.reason);
|
|
2371
|
+
return new Promise((resolve, reject) => {
|
|
2372
|
+
const onAbort = () => reject(signal.reason);
|
|
2373
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
2374
|
+
promise.then(resolve, reject).finally(() => signal.removeEventListener("abort", onAbort)).catch(() => void 0);
|
|
2375
|
+
});
|
|
2376
|
+
}
|
|
2384
2377
|
static _throwIfAborted(signal) {
|
|
2385
2378
|
if (signal?.aborted) {
|
|
2386
2379
|
throw signal.reason instanceof Error ? signal.reason : new Error("Operation aborted");
|