@onekeyfe/hwk-trezor-core 1.1.29-alpha.2 → 1.1.29-alpha.4

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 CHANGED
@@ -241,7 +241,6 @@ declare class TrezorDeviceSession {
241
241
  get isThp(): boolean;
242
242
  getThpState(): TrezorThpState | undefined;
243
243
  initialize(): Promise<Record<string, unknown>>;
244
- private handshakeCancel;
245
244
  call(name: string, data?: Record<string, unknown>): Promise<{
246
245
  type: string;
247
246
  message: Record<string, unknown>;
package/dist/index.d.ts CHANGED
@@ -241,7 +241,6 @@ declare class TrezorDeviceSession {
241
241
  get isThp(): boolean;
242
242
  getThpState(): TrezorThpState | undefined;
243
243
  initialize(): Promise<Record<string, unknown>>;
244
- private handshakeCancel;
245
244
  call(name: string, data?: Record<string, unknown>): Promise<{
246
245
  type: string;
247
246
  message: Record<string, unknown>;
package/dist/index.js CHANGED
@@ -11592,19 +11592,19 @@ var ALWAYS_KEPT_EVENT_PREFIXES = [
11592
11592
  "thp.call.",
11593
11593
  "core.call.",
11594
11594
  "core.send.",
11595
- "call.dispatch.",
11596
- "capability.check",
11597
- "[TrezorSuiteAlignTrace] handshakeCancel.",
11598
- "[TrezorSuiteAlignTrace] protocolProbe.",
11599
- "[TrezorSuiteAlignTrace] call.dispatch.",
11600
- "[TrezorUSBInitTrace] core.call.",
11601
- "[TrezorUSBInitTrace] core.send."
11595
+ "connector.call.",
11596
+ "webusb.scan.filtered",
11597
+ "webusb.connector.enumerate.filtered",
11598
+ "ble.connector.enumerate.filtered",
11599
+ "capability.check"
11602
11600
  ];
11603
11601
  var DROPPED_EVENT_PREFIXES = [
11604
11602
  "connector.search.",
11605
11603
  "webusb.scan.",
11604
+ "webusb.connector.enumerate.",
11606
11605
  "webusb.closeDevice.",
11607
11606
  "webusb.reset.",
11607
+ "ble.connector.enumerate.",
11608
11608
  "ble.renderer.scan.",
11609
11609
  "ble.main.scan.",
11610
11610
  "ble.scan.",
@@ -11732,7 +11732,7 @@ var TrezorCore = class {
11732
11732
  async call(name4, data = {}, options = {}) {
11733
11733
  const thpState = options.thpState;
11734
11734
  const { message, chunks } = this.buildChunks(name4, data, thpState);
11735
- this.log("debug", "[TrezorUSBInitTrace] core.call.start", {
11735
+ this.log("debug", "core.call.start", {
11736
11736
  protocol: this.protocol.name,
11737
11737
  name: name4,
11738
11738
  messageBytes: message.length,
@@ -11781,7 +11781,7 @@ var TrezorCore = class {
11781
11781
  async send(name4, data = {}, options = {}) {
11782
11782
  const thpState = options.thpState;
11783
11783
  const { message, chunks } = this.buildChunks(name4, data, thpState);
11784
- this.log("debug", "[TrezorUSBInitTrace] core.send.start", {
11784
+ this.log("debug", "core.send.start", {
11785
11785
  protocol: this.protocol.name,
11786
11786
  name: name4,
11787
11787
  messageBytes: message.length,
@@ -11899,7 +11899,6 @@ var TrezorThpProtocol = class {
11899
11899
  this.thpState.sync("recv", message.type);
11900
11900
  }
11901
11901
  };
11902
- var TREZOR_PROTOCOL_MALFORMED = "Malformed protocol format";
11903
11902
  var defaultCoreFactory = (transport, protocol, chunkSize, logger) => new TrezorCore({ transport, protocol, chunkSize, logger });
11904
11903
  var normalizePassphrase = (value) => (value ?? "").normalize("NFKD");
11905
11904
  var normalizeCreateAppSessionOptions = (options = {}) => {
@@ -11970,21 +11969,7 @@ var TrezorDeviceSession = class {
11970
11969
  this.protocol = "v1";
11971
11970
  const optsLogger = this.thpOptions?.logger;
11972
11971
  this.core = this.coreFactory(this.transport, protocol_v1_exports, this.chunkSize, optsLogger);
11973
- const cancelResult = await this.handshakeCancel();
11974
- if (cancelResult === "thp") {
11975
- if (!this.thpOptions) {
11976
- throw new Error(
11977
- "Trezor THP required but the connector was constructed without `thp` options. Pass `thp: { hostName, appName, ... }` so the device can complete pairing."
11978
- );
11979
- }
11980
- this.protocol = "v1";
11981
- this.core = void 0;
11982
- return this.initializeThp();
11983
- }
11984
11972
  try {
11985
- this.log("info", "[TrezorSuiteAlignTrace] protocolProbe.v1Initialize.start", {
11986
- connectionType: this.connectionType ?? "unknown"
11987
- });
11988
11973
  this.log("debug", "session.call.request", { protocol: "v1", name: "Initialize" });
11989
11974
  const response = await this.core.call("Initialize", {});
11990
11975
  this.log("debug", "session.call.response", {
@@ -11993,15 +11978,8 @@ var TrezorDeviceSession = class {
11993
11978
  responseType: response.type
11994
11979
  });
11995
11980
  this.currentFeatures = expectMessage(response, "Features").message;
11996
- this.log("info", "[TrezorSuiteAlignTrace] protocolProbe.v1Initialize.done", {
11997
- responseType: response.type,
11998
- features: summarizeFeatures(this.currentFeatures)
11999
- });
12000
11981
  } catch (error2) {
12001
11982
  if (error2 instanceof TrezorFailureError && error2.code === "Failure_InvalidProtocol") {
12002
- this.log("info", "[TrezorSuiteAlignTrace] protocolProbe.thpFallback", {
12003
- reason: "Failure_InvalidProtocol"
12004
- });
12005
11983
  this.log("info", "session.initialize.thp.fallback", {
12006
11984
  reason: "device replied Failure_InvalidProtocol to v1 Initialize"
12007
11985
  });
@@ -12015,10 +11993,6 @@ var TrezorDeviceSession = class {
12015
11993
  return this.initializeThp();
12016
11994
  }
12017
11995
  this.log("error", "session.initialize.error", { error: errorToLog(error2) });
12018
- this.log("error", "[TrezorSuiteAlignTrace] protocolProbe.error", {
12019
- attemptedProtocol: "v1",
12020
- error: errorToLog(error2)
12021
- });
12022
11996
  throw error2;
12023
11997
  }
12024
11998
  this.log("info", "session.initialize.done", {
@@ -12027,83 +12001,6 @@ var TrezorDeviceSession = class {
12027
12001
  });
12028
12002
  return this.currentFeatures;
12029
12003
  }
12030
- async handshakeCancel() {
12031
- if (!this.core) return "v1";
12032
- this.log("info", "[TrezorSuiteAlignTrace] handshakeCancel.start", {
12033
- connectionType: this.connectionType ?? "unknown"
12034
- });
12035
- try {
12036
- await this.core.send("Cancel", {});
12037
- this.log("info", "[TrezorSuiteAlignTrace] handshakeCancel.send.done", {
12038
- connectionType: this.connectionType ?? "unknown"
12039
- });
12040
- } catch (error2) {
12041
- this.log("info", "[TrezorSuiteAlignTrace] handshakeCancel.send.ignored", {
12042
- error: errorToLog(error2)
12043
- });
12044
- return "v1";
12045
- }
12046
- for (let attempt = 0; attempt < 10; attempt += 1) {
12047
- const abortController = new AbortController();
12048
- const timeoutId = setTimeout(() => {
12049
- abortController.abort(new Error("Trezor handshake Cancel read timeout"));
12050
- }, 1e3);
12051
- try {
12052
- this.log("info", "[TrezorSuiteAlignTrace] handshakeCancel.read.attempt", {
12053
- attempt
12054
- });
12055
- const response = await this.core.receive({
12056
- signal: abortController.signal,
12057
- name: "Cancel"
12058
- });
12059
- this.log("info", "[TrezorSuiteAlignTrace] handshakeCancel.done", {
12060
- attempt,
12061
- responseType: response.type
12062
- });
12063
- return "v1";
12064
- } catch (error2) {
12065
- if (error2 instanceof TrezorFailureError) {
12066
- this.log("info", "[TrezorSuiteAlignTrace] handshakeCancel.failure", {
12067
- attempt,
12068
- code: error2.code
12069
- });
12070
- if (error2.code === "Failure_InvalidProtocol") {
12071
- this.log("info", "[TrezorSuiteAlignTrace] handshakeCancel.thpDetected", {
12072
- reason: "Failure_InvalidProtocol"
12073
- });
12074
- return "thp";
12075
- }
12076
- if (error2.code === "Failure_Busy") {
12077
- throw error2;
12078
- }
12079
- return "v1";
12080
- }
12081
- if (error2 instanceof TrezorProtocolError && error2.code === TREZOR_PROTOCOL_MALFORMED) {
12082
- this.log("info", "[TrezorSuiteAlignTrace] handshakeCancel.read.malformed", {
12083
- attempt,
12084
- error: errorToLog(error2)
12085
- });
12086
- continue;
12087
- }
12088
- if (isHandshakeCancelReadTimeout(error2)) {
12089
- this.log("info", "[TrezorSuiteAlignTrace] handshakeCancel.read.timeout.reconnect", {
12090
- attempt
12091
- });
12092
- await this.transport.reconnect?.();
12093
- continue;
12094
- }
12095
- this.log("info", "[TrezorSuiteAlignTrace] handshakeCancel.read.ignored", {
12096
- attempt,
12097
- error: errorToLog(error2)
12098
- });
12099
- return "v1";
12100
- } finally {
12101
- clearTimeout(timeoutId);
12102
- }
12103
- }
12104
- this.log("info", "[TrezorSuiteAlignTrace] handshakeCancel.read.limitReached", {});
12105
- return "v1";
12106
- }
12107
12004
  async call(name4, data = {}) {
12108
12005
  if (!this.core) {
12109
12006
  await this.initialize();
@@ -12185,10 +12082,6 @@ var TrezorDeviceSession = class {
12185
12082
  const response = await this.core.call("GetFeatures", {}, { thpState: this.thpState });
12186
12083
  this.log("info", "thp.getFeatures.response", { responseType: response.type });
12187
12084
  this.currentFeatures = expectMessage(response, "Features").message;
12188
- this.log("info", "[TrezorSuiteAlignTrace] protocolProbe.thpGetFeatures.done", {
12189
- responseType: response.type,
12190
- features: summarizeFeatures(this.currentFeatures)
12191
- });
12192
12085
  } catch (error2) {
12193
12086
  const code = error2?.code;
12194
12087
  if (code === "ThpDeviceLocked" && !hasRetried) {
@@ -12366,7 +12259,9 @@ var TrezorThpSession = class {
12366
12259
  await this.pair();
12367
12260
  }
12368
12261
  if (this.thpState.phase !== "paired") {
12369
- throw new Error("THP pairing is required before GetFeatures");
12262
+ throw Object.assign(new Error("THP pairing is required before GetFeatures"), {
12263
+ code: "ThpPairingRequired"
12264
+ });
12370
12265
  }
12371
12266
  this.log("info", "thp.session.done", { phase: this.thpState.phase });
12372
12267
  }
@@ -12456,6 +12351,12 @@ var TrezorThpSession = class {
12456
12351
  const completionState = completion.message.state;
12457
12352
  this.thpState.setIsPaired(completionState !== 0);
12458
12353
  this.thpState.setPhase("pairing");
12354
+ if (!completionState && handshakeCredentials.credentials) {
12355
+ this.thpState.removePairingCredential(handshakeCredentials.credentials);
12356
+ await this.options.onPairingCredentialsChanged?.({
12357
+ credentials: this.thpState.pairingCredentials
12358
+ });
12359
+ }
12459
12360
  this.log("info", "thp.handshake.done", {
12460
12361
  completionState,
12461
12362
  isPaired: this.thpState.isPaired,
@@ -12688,9 +12589,6 @@ function errorToLog(error2) {
12688
12589
  }
12689
12590
  return String(error2);
12690
12591
  }
12691
- function isHandshakeCancelReadTimeout(error2) {
12692
- return error2 instanceof Error && error2.message === "Trezor handshake Cancel read timeout";
12693
- }
12694
12592
  function summarizeFeatures(features) {
12695
12593
  if (!features) return void 0;
12696
12594
  return {