@onekeyfe/hwk-trezor-connector 1.2.8 → 1.2.9

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");
@@ -2654,6 +2677,7 @@ function normalizeEthMessage(message, isHex) {
2654
2677
  }
2655
2678
 
2656
2679
  // src/chains/sol.ts
2680
+ import { bytesToHex, hexToBytes, prepareSolanaOffchainMessageV1 } from "@onekeyfe/hwk-adapter-core";
2657
2681
  async function solGetAddress(ctx, params) {
2658
2682
  const request = readSolGetAddressParams(params);
2659
2683
  const response = await ctx.deviceSession.call("SolanaGetAddress", {
@@ -2700,10 +2724,37 @@ async function solSignTransaction(ctx, params) {
2700
2724
  }
2701
2725
  return { signature };
2702
2726
  }
2703
- async function solSignMessage(_ctx, _params) {
2704
- throw createMethodNotSupportedError(
2705
- "Trezor firmware does not support Solana message signing \u2014 only solSignTransaction is available"
2706
- );
2727
+ async function solSignMessage(ctx, params) {
2728
+ const request = readSolSignMsgParams(params);
2729
+ if (request.messageVersion !== 1) {
2730
+ throw createMethodNotSupportedError(
2731
+ "Trezor supports finalized Solana off-chain message version 1 only"
2732
+ );
2733
+ }
2734
+ const preparedMessage = prepareSolanaOffchainMessageV1({
2735
+ message: hexToBytes(request.message),
2736
+ requiredSigners: request.requiredSigners
2737
+ });
2738
+ const response = await ctx.deviceSession.call("SolanaSignMessage", {
2739
+ address_n: parseBip32Path(request.path),
2740
+ message: {
2741
+ message: preparedMessage.messageText,
2742
+ signers: preparedMessage.requiredSigners
2743
+ }
2744
+ });
2745
+ if (response.type !== "SolanaMessageSignature") {
2746
+ throw new Error(`Expected SolanaMessageSignature response, received ${response.type}`);
2747
+ }
2748
+ const signature = readString(response.message, "signature");
2749
+ if (!signature) {
2750
+ throw new Error("SolanaMessageSignature response did not include a signature");
2751
+ }
2752
+ const signedData = readString(response.message, "signed_data");
2753
+ const expectedSignedData = bytesToHex(preparedMessage.serializedMessage);
2754
+ if (!signedData || stripHexPrefix2(signedData).toLowerCase() !== expectedSignedData) {
2755
+ throw new Error("Trezor signed data does not match the requested Solana off-chain message");
2756
+ }
2757
+ return { signature: stripHexPrefix2(signature).toLowerCase() };
2707
2758
  }
2708
2759
  function readSolGetAddressParams(params) {
2709
2760
  if (!params || typeof params !== "object") {
@@ -2734,6 +2785,25 @@ function readSolSignTxParams(params) {
2734
2785
  assertHexString("serializedTx", serializedTx);
2735
2786
  return params;
2736
2787
  }
2788
+ function readSolSignMsgParams(params) {
2789
+ if (!params || typeof params !== "object") {
2790
+ throw createInvalidParamsError("solSignMessage params must be an object");
2791
+ }
2792
+ const request = params;
2793
+ if (typeof request.path !== "string" || request.path.trim().length === 0) {
2794
+ throw createInvalidParamsError("solSignMessage requires a non-empty path");
2795
+ }
2796
+ if (typeof request.message !== "string" || request.message.length === 0) {
2797
+ throw createInvalidParamsError("solSignMessage requires message as a hex string");
2798
+ }
2799
+ assertHexString("message", request.message);
2800
+ if (request.messageVersion === 1 && !Array.isArray(request.requiredSigners)) {
2801
+ throw createInvalidParamsError(
2802
+ "Solana off-chain message version 1 requires signer public keys"
2803
+ );
2804
+ }
2805
+ return request;
2806
+ }
2737
2807
  function stripHexPrefix2(value) {
2738
2808
  return value.startsWith("0x") || value.startsWith("0X") ? value.slice(2) : value;
2739
2809
  }
@@ -7782,9 +7852,9 @@ var TrezorConnectorBase = class {
7782
7852
  chunkSize;
7783
7853
  thp;
7784
7854
  // Single owned array — passed by reference into every TrezorThpSession via
7785
- // `createThpOptions`. setKnownCredentials() and the auto-merge inside
7786
- // onPairingCredentialsChanged both mutate this array in-place so the next
7787
- // handshake sees fresh credentials without a connector rebuild.
7855
+ // `createThpOptions`. Both host updates and authoritative device updates
7856
+ // replace its contents in-place so the next handshake sees fresh credentials
7857
+ // without a connector rebuild.
7788
7858
  knownCredentials = [];
7789
7859
  deviceSessionFactory;
7790
7860
  eventHandlers = /* @__PURE__ */ new Map();
@@ -7821,21 +7891,6 @@ var TrezorConnectorBase = class {
7821
7891
  this.knownCredentials.push(...credentials);
7822
7892
  }
7823
7893
  }
7824
- /**
7825
- * Push new credentials minted during pairing into the in-memory array.
7826
- * Deduplicates by the device-minted `credential` blob — the host has no
7827
- * other stable identity for a credential.
7828
- */
7829
- mergeKnownCredentials(incoming) {
7830
- for (const cred of incoming) {
7831
- const blob = cred.credential;
7832
- if (!blob) continue;
7833
- const exists2 = this.knownCredentials.some(
7834
- (existing) => existing.credential === blob
7835
- );
7836
- if (!exists2) this.knownCredentials.push(cred);
7837
- }
7838
- }
7839
7894
  resolveUnlistedDevice(_deviceId) {
7840
7895
  return void 0;
7841
7896
  }
@@ -7971,7 +8026,11 @@ var TrezorConnectorBase = class {
7971
8026
  durationMs: Date.now() - startedAt,
7972
8027
  error: errorToLog(error)
7973
8028
  });
7974
- return { success: false, error: serializeConnectorError(error) };
8029
+ const serialized = serializeConnectorError(error);
8030
+ if (error?.code === "ThpPairingFailed") {
8031
+ serialized.code = HardwareErrorCode2.ThpPairingFailed;
8032
+ }
8033
+ return { success: false, error: serialized };
7975
8034
  }
7976
8035
  }
7977
8036
  async dispatchCall(sessionId, method, params) {
@@ -8213,6 +8272,7 @@ var TrezorConnectorBase = class {
8213
8272
  if (this.thp?.onPairingRequest) {
8214
8273
  return this.thp.onPairingRequest(payload);
8215
8274
  }
8275
+ const pairingPromise = this.uiRequests.wait(UI_REQUEST.REQUEST_TREZOR_THP_PAIRING);
8216
8276
  this.emit("ui-request", {
8217
8277
  type: UI_REQUEST.REQUEST_TREZOR_THP_PAIRING,
8218
8278
  payload: {
@@ -8222,7 +8282,7 @@ var TrezorConnectorBase = class {
8222
8282
  nfcData: payload.nfcData
8223
8283
  }
8224
8284
  });
8225
- return this.uiRequests.wait(UI_REQUEST.REQUEST_TREZOR_THP_PAIRING);
8285
+ return pairingPromise;
8226
8286
  },
8227
8287
  onButtonRequest: async (payload) => {
8228
8288
  await this.thp?.onButtonRequest?.(payload);
@@ -8236,11 +8296,10 @@ var TrezorConnectorBase = class {
8236
8296
  },
8237
8297
  onButtonRequestComplete: async (payload) => {
8238
8298
  await this.thp?.onButtonRequestComplete?.(payload);
8299
+ if (payload.responseType === "ButtonRequest") return;
8239
8300
  this.emit("ui-event", {
8240
8301
  type: EConnectorInteraction.InteractionComplete,
8241
- payload: {
8242
- sessionId: device.connectId
8243
- }
8302
+ payload: { sessionId: "" }
8244
8303
  });
8245
8304
  },
8246
8305
  onPinMatrixRequest: async (payload) => {
@@ -8273,12 +8332,21 @@ var TrezorConnectorBase = class {
8273
8332
  return res?.passphraseOnDevice ? { on_device: true } : { passphrase: res?.value ?? "" };
8274
8333
  },
8275
8334
  onPairingCredentialsChanged: async (payload) => {
8276
- this.mergeKnownCredentials(payload.credentials);
8335
+ const blob = (cred) => cred.credential;
8336
+ const rejected = new Set((payload.removed ?? []).map(blob));
8337
+ const merged = this.knownCredentials.filter((cred) => !rejected.has(blob(cred)));
8338
+ for (const cred of payload.credentials) {
8339
+ if (blob(cred) && !merged.some((existing) => blob(existing) === blob(cred))) {
8340
+ merged.push(cred);
8341
+ }
8342
+ }
8343
+ this.setKnownCredentials(merged);
8277
8344
  await this.thp?.onPairingCredentialsChanged?.(payload);
8278
8345
  this.emit("device-trezor-thp-credentials-changed", {
8279
8346
  connectId: device.connectId,
8280
8347
  deviceId: device.deviceId,
8281
- credentials: payload.credentials
8348
+ credentials: payload.credentials,
8349
+ removed: payload.removed
8282
8350
  });
8283
8351
  }
8284
8352
  };