@onekeyfe/hwk-trezor-connector 1.2.7 → 1.2.8-alpha.1

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.mjs CHANGED
@@ -75,14 +75,12 @@ var BTC_SCRIPT_TYPE_MAP = {
75
75
  p2pkh: "SPENDADDRESS",
76
76
  p2sh: "SPENDP2SHWITNESS",
77
77
  p2wpkh: "SPENDWITNESS",
78
- p2wsh: "SPENDWITNESS",
79
78
  p2tr: "SPENDTAPROOT"
80
79
  };
81
80
  var BTC_CHANGE_OUTPUT_SCRIPT_TYPE_MAP = {
82
81
  p2pkh: "PAYTOADDRESS",
83
82
  p2sh: "PAYTOP2SHWITNESS",
84
83
  p2wpkh: "PAYTOWITNESS",
85
- p2wsh: "PAYTOWITNESS",
86
84
  p2tr: "PAYTOTAPROOT"
87
85
  };
88
86
  var BTC_COIN_NAME_ALIASES = {
@@ -106,7 +104,8 @@ function normalizeBtcCoin(coin) {
106
104
  return BTC_COIN_NAME_ALIASES[coin.toLowerCase()] ?? coin;
107
105
  }
108
106
  function deriveScriptTypeFromPath(path) {
109
- const normalized = path.startsWith("m/") ? path.slice(2) : path;
107
+ const trimmed = path.trim();
108
+ const normalized = /^m\//i.test(trimmed) ? trimmed.slice(2) : trimmed;
110
109
  const purpose = normalized.split("/")[0]?.replace(/['hH]$/, "");
111
110
  switch (purpose) {
112
111
  case "86":
@@ -118,16 +117,30 @@ function deriveScriptTypeFromPath(path) {
118
117
  case "44":
119
118
  return "p2pkh";
120
119
  default:
121
- return "p2wpkh";
120
+ return void 0;
122
121
  }
123
122
  }
123
+ function resolveScriptType(map, scriptType, path) {
124
+ const resolved = scriptType ?? deriveScriptTypeFromPath(path);
125
+ if (!resolved) {
126
+ throw createInvalidParamsError(
127
+ `Cannot infer the BTC script type from ${path}; pass scriptType`
128
+ );
129
+ }
130
+ if (resolved === "p2wsh") {
131
+ throw createInvalidParamsError(
132
+ "BTC p2wsh needs a multisig script; a single key has no p2wsh form"
133
+ );
134
+ }
135
+ return map[resolved];
136
+ }
124
137
  async function btcGetAddress(ctx, params) {
125
138
  const request = readBtcGetAddressParams(params);
126
139
  const response = await ctx.deviceSession.call("GetAddress", {
127
140
  address_n: parseBip32Path(request.path),
128
141
  coin_name: normalizeBtcCoin(request.coin),
129
142
  show_display: request.showOnDevice ?? false,
130
- script_type: BTC_SCRIPT_TYPE_MAP[request.scriptType ?? deriveScriptTypeFromPath(request.path)]
143
+ script_type: resolveScriptType(BTC_SCRIPT_TYPE_MAP, request.scriptType, request.path)
131
144
  });
132
145
  if (response.type !== "Address") {
133
146
  throw new Error(`Expected Address response, received ${response.type}`);
@@ -160,7 +173,7 @@ async function btcSignMessage(ctx, params) {
160
173
  address_n: parseBip32Path(request.path),
161
174
  message: normalizeBtcMessage(request.message, request.hex ?? false),
162
175
  coin_name: normalizeBtcCoin(request.coin),
163
- script_type: BTC_SCRIPT_TYPE_MAP[deriveScriptTypeFromPath(request.path)]
176
+ script_type: resolveScriptType(BTC_SCRIPT_TYPE_MAP, void 0, request.path)
164
177
  };
165
178
  if (request.noScriptType) {
166
179
  message.no_script_type = true;
@@ -405,7 +418,7 @@ function inputToTrezor(input) {
405
418
  prev_hash: input.prevHash,
406
419
  prev_index: input.prevIndex,
407
420
  amount: input.amount,
408
- script_type: BTC_SCRIPT_TYPE_MAP[input.scriptType ?? deriveScriptTypeFromPath(input.path)]
421
+ script_type: resolveScriptType(BTC_SCRIPT_TYPE_MAP, input.scriptType, input.path)
409
422
  };
410
423
  if (input.sequence !== void 0) result.sequence = input.sequence;
411
424
  if (input.origHash !== void 0) result.orig_hash = input.origHash;
@@ -457,7 +470,11 @@ function outputToTrezor(output2) {
457
470
  return withOutputMetadata({
458
471
  address_n: parseBip32Path(output2.path),
459
472
  amount: output2.amount,
460
- script_type: BTC_CHANGE_OUTPUT_SCRIPT_TYPE_MAP[output2.scriptType ?? deriveScriptTypeFromPath(output2.path)]
473
+ script_type: resolveScriptType(
474
+ BTC_CHANGE_OUTPUT_SCRIPT_TYPE_MAP,
475
+ output2.scriptType,
476
+ output2.path
477
+ )
461
478
  });
462
479
  }
463
480
  throw createInvalidParamsError(
@@ -648,6 +665,12 @@ function readBtcGetAddressParams(params) {
648
665
  if (typeof path !== "string" || path.trim().length === 0) {
649
666
  throw createInvalidParamsError("btcGetAddress requires a non-empty path");
650
667
  }
668
+ const { addressIndex, change } = params;
669
+ if (addressIndex !== void 0 || change !== void 0) {
670
+ throw createInvalidParamsError(
671
+ "Trezor btcGetAddress takes the full address path; addressIndex and change are not supported"
672
+ );
673
+ }
651
674
  const { coin } = params;
652
675
  if (coin !== void 0 && typeof coin !== "string") {
653
676
  throw createInvalidParamsError("btcGetAddress coin must be a string when provided");
@@ -2652,6 +2675,7 @@ function normalizeEthMessage(message, isHex) {
2652
2675
  }
2653
2676
 
2654
2677
  // src/chains/sol.ts
2678
+ import { bytesToHex, hexToBytes, prepareSolanaOffchainMessageV1 } from "@onekeyfe/hwk-adapter-core";
2655
2679
  async function solGetAddress(ctx, params) {
2656
2680
  const request = readSolGetAddressParams(params);
2657
2681
  const response = await ctx.deviceSession.call("SolanaGetAddress", {
@@ -2698,10 +2722,37 @@ async function solSignTransaction(ctx, params) {
2698
2722
  }
2699
2723
  return { signature };
2700
2724
  }
2701
- async function solSignMessage(_ctx, _params) {
2702
- throw createMethodNotSupportedError(
2703
- "Trezor firmware does not support Solana message signing \u2014 only solSignTransaction is available"
2704
- );
2725
+ async function solSignMessage(ctx, params) {
2726
+ const request = readSolSignMsgParams(params);
2727
+ if (request.messageVersion !== 1) {
2728
+ throw createMethodNotSupportedError(
2729
+ "Trezor supports finalized Solana off-chain message version 1 only"
2730
+ );
2731
+ }
2732
+ const preparedMessage = prepareSolanaOffchainMessageV1({
2733
+ message: hexToBytes(request.message),
2734
+ requiredSigners: request.requiredSigners
2735
+ });
2736
+ const response = await ctx.deviceSession.call("SolanaSignMessage", {
2737
+ address_n: parseBip32Path(request.path),
2738
+ message: {
2739
+ message: preparedMessage.messageText,
2740
+ signers: preparedMessage.requiredSigners
2741
+ }
2742
+ });
2743
+ if (response.type !== "SolanaMessageSignature") {
2744
+ throw new Error(`Expected SolanaMessageSignature response, received ${response.type}`);
2745
+ }
2746
+ const signature = readString(response.message, "signature");
2747
+ if (!signature) {
2748
+ throw new Error("SolanaMessageSignature response did not include a signature");
2749
+ }
2750
+ const signedData = readString(response.message, "signed_data");
2751
+ const expectedSignedData = bytesToHex(preparedMessage.serializedMessage);
2752
+ if (!signedData || stripHexPrefix2(signedData).toLowerCase() !== expectedSignedData) {
2753
+ throw new Error("Trezor signed data does not match the requested Solana off-chain message");
2754
+ }
2755
+ return { signature: stripHexPrefix2(signature).toLowerCase() };
2705
2756
  }
2706
2757
  function readSolGetAddressParams(params) {
2707
2758
  if (!params || typeof params !== "object") {
@@ -2732,6 +2783,25 @@ function readSolSignTxParams(params) {
2732
2783
  assertHexString("serializedTx", serializedTx);
2733
2784
  return params;
2734
2785
  }
2786
+ function readSolSignMsgParams(params) {
2787
+ if (!params || typeof params !== "object") {
2788
+ throw createInvalidParamsError("solSignMessage params must be an object");
2789
+ }
2790
+ const request = params;
2791
+ if (typeof request.path !== "string" || request.path.trim().length === 0) {
2792
+ throw createInvalidParamsError("solSignMessage requires a non-empty path");
2793
+ }
2794
+ if (typeof request.message !== "string" || request.message.length === 0) {
2795
+ throw createInvalidParamsError("solSignMessage requires message as a hex string");
2796
+ }
2797
+ assertHexString("message", request.message);
2798
+ if (request.messageVersion === 1 && !Array.isArray(request.requiredSigners)) {
2799
+ throw createInvalidParamsError(
2800
+ "Solana off-chain message version 1 requires signer public keys"
2801
+ );
2802
+ }
2803
+ return request;
2804
+ }
2735
2805
  function stripHexPrefix2(value) {
2736
2806
  return value.startsWith("0x") || value.startsWith("0X") ? value.slice(2) : value;
2737
2807
  }
@@ -7780,9 +7850,9 @@ var TrezorConnectorBase = class {
7780
7850
  chunkSize;
7781
7851
  thp;
7782
7852
  // Single owned array — passed by reference into every TrezorThpSession via
7783
- // `createThpOptions`. setKnownCredentials() and the auto-merge inside
7784
- // onPairingCredentialsChanged both mutate this array in-place so the next
7785
- // handshake sees fresh credentials without a connector rebuild.
7853
+ // `createThpOptions`. Both host updates and authoritative device updates
7854
+ // replace its contents in-place so the next handshake sees fresh credentials
7855
+ // without a connector rebuild.
7786
7856
  knownCredentials = [];
7787
7857
  deviceSessionFactory;
7788
7858
  eventHandlers = /* @__PURE__ */ new Map();
@@ -7819,21 +7889,6 @@ var TrezorConnectorBase = class {
7819
7889
  this.knownCredentials.push(...credentials);
7820
7890
  }
7821
7891
  }
7822
- /**
7823
- * Push new credentials minted during pairing into the in-memory array.
7824
- * Deduplicates by the device-minted `credential` blob — the host has no
7825
- * other stable identity for a credential.
7826
- */
7827
- mergeKnownCredentials(incoming) {
7828
- for (const cred of incoming) {
7829
- const blob = cred.credential;
7830
- if (!blob) continue;
7831
- const exists2 = this.knownCredentials.some(
7832
- (existing) => existing.credential === blob
7833
- );
7834
- if (!exists2) this.knownCredentials.push(cred);
7835
- }
7836
- }
7837
7892
  resolveUnlistedDevice(_deviceId) {
7838
7893
  return void 0;
7839
7894
  }
@@ -8211,6 +8266,7 @@ var TrezorConnectorBase = class {
8211
8266
  if (this.thp?.onPairingRequest) {
8212
8267
  return this.thp.onPairingRequest(payload);
8213
8268
  }
8269
+ const pairingPromise = this.uiRequests.wait(UI_REQUEST.REQUEST_TREZOR_THP_PAIRING);
8214
8270
  this.emit("ui-request", {
8215
8271
  type: UI_REQUEST.REQUEST_TREZOR_THP_PAIRING,
8216
8272
  payload: {
@@ -8220,7 +8276,7 @@ var TrezorConnectorBase = class {
8220
8276
  nfcData: payload.nfcData
8221
8277
  }
8222
8278
  });
8223
- return this.uiRequests.wait(UI_REQUEST.REQUEST_TREZOR_THP_PAIRING);
8279
+ return pairingPromise;
8224
8280
  },
8225
8281
  onButtonRequest: async (payload) => {
8226
8282
  await this.thp?.onButtonRequest?.(payload);
@@ -8234,11 +8290,10 @@ var TrezorConnectorBase = class {
8234
8290
  },
8235
8291
  onButtonRequestComplete: async (payload) => {
8236
8292
  await this.thp?.onButtonRequestComplete?.(payload);
8293
+ if (payload.responseType === "ButtonRequest") return;
8237
8294
  this.emit("ui-event", {
8238
8295
  type: EConnectorInteraction.InteractionComplete,
8239
- payload: {
8240
- sessionId: device.connectId
8241
- }
8296
+ payload: { sessionId: "" }
8242
8297
  });
8243
8298
  },
8244
8299
  onPinMatrixRequest: async (payload) => {
@@ -8271,7 +8326,15 @@ var TrezorConnectorBase = class {
8271
8326
  return res?.passphraseOnDevice ? { on_device: true } : { passphrase: res?.value ?? "" };
8272
8327
  },
8273
8328
  onPairingCredentialsChanged: async (payload) => {
8274
- this.mergeKnownCredentials(payload.credentials);
8329
+ const blob = (cred) => cred.credential;
8330
+ const rejected = new Set((payload.removed ?? []).map(blob));
8331
+ const merged = this.knownCredentials.filter((cred) => !rejected.has(blob(cred)));
8332
+ for (const cred of payload.credentials) {
8333
+ if (blob(cred) && !merged.some((existing) => blob(existing) === blob(cred))) {
8334
+ merged.push(cred);
8335
+ }
8336
+ }
8337
+ this.setKnownCredentials(merged);
8275
8338
  await this.thp?.onPairingCredentialsChanged?.(payload);
8276
8339
  this.emit("device-trezor-thp-credentials-changed", {
8277
8340
  connectId: device.connectId,