@onekeyfe/hwk-trezor-core 1.2.10-alpha.3 → 1.2.11-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.js CHANGED
@@ -11774,6 +11774,22 @@ function shouldMarkThpModuleHandled(entry) {
11774
11774
 
11775
11775
  // src/index.ts
11776
11776
  init_buffer();
11777
+ function trackTransportFailure() {
11778
+ let failure;
11779
+ return {
11780
+ run: async (io) => {
11781
+ try {
11782
+ return await io();
11783
+ } catch (error2) {
11784
+ failure ??= { error: error2 };
11785
+ throw error2;
11786
+ }
11787
+ },
11788
+ rethrow: () => {
11789
+ if (failure) throw failure.error;
11790
+ }
11791
+ };
11792
+ }
11777
11793
  var unwrapTransportAbortSignal = (options) => {
11778
11794
  if (!options) return void 0;
11779
11795
  if ("addEventListener" in options && typeof options.addEventListener === "function") {
@@ -11831,20 +11847,26 @@ var TrezorCore = class {
11831
11847
  });
11832
11848
  if (this.protocol.name === "v2") {
11833
11849
  const previousNonce = thpState?.sendNonce;
11850
+ const transportFailure = trackTransportFailure();
11834
11851
  const response = await callThpMessage({
11835
11852
  thpState,
11836
11853
  chunks,
11837
11854
  apiWrite: async (chunk, options2) => {
11838
- await this.transport.write(chunk, unwrapTransportAbortSignal(options2));
11855
+ await transportFailure.run(
11856
+ () => this.transport.write(chunk, unwrapTransportAbortSignal(options2))
11857
+ );
11839
11858
  return { success: true, payload: void 0 };
11840
11859
  },
11841
11860
  apiRead: async (options2) => {
11842
- const payload = await this.transport.read(unwrapTransportAbortSignal(options2));
11861
+ const payload = await transportFailure.run(
11862
+ () => this.transport.read(unwrapTransportAbortSignal(options2))
11863
+ );
11843
11864
  return { success: true, payload: toRuntimeBuffer(payload) };
11844
11865
  },
11845
11866
  signal: options.signal
11846
11867
  });
11847
11868
  if (!response.success) {
11869
+ transportFailure.rethrow();
11848
11870
  throw new Error(response.error.message ?? response.error.code);
11849
11871
  }
11850
11872
  if (previousNonce === thpState?.sendNonce) {
@@ -11881,14 +11903,16 @@ var TrezorCore = class {
11881
11903
  await this.writeChunks(chunks, options.signal);
11882
11904
  }
11883
11905
  async receive(options = {}) {
11906
+ const transportFailure = trackTransportFailure();
11884
11907
  const response = await receiveAndParse(async () => {
11885
- const payload = await this.transport.read(options.signal);
11908
+ const payload = await transportFailure.run(() => this.transport.read(options.signal));
11886
11909
  return {
11887
11910
  success: true,
11888
11911
  payload: toRuntimeBuffer(payload)
11889
11912
  };
11890
11913
  }, this.protocol);
11891
11914
  if (!response.success) {
11915
+ transportFailure.rethrow();
11892
11916
  throw new TrezorProtocolError(response.error.code, response.error.message);
11893
11917
  }
11894
11918
  if (isFailureResponse(response.payload)) {
@@ -11943,16 +11967,17 @@ var TrezorThpProtocol = class {
11943
11967
  }
11944
11968
  const previousNonce = this.thpState.sendNonce;
11945
11969
  const chunk = toRuntimeBuffer(this.encode(params));
11970
+ const transportFailure = trackTransportFailure();
11946
11971
  const result = await callThpMessage({
11947
11972
  thpState: this.thpState,
11948
11973
  chunks: [chunk],
11949
11974
  skipAck: params.skipAck,
11950
11975
  apiWrite: async (data) => {
11951
- await transport.write?.(connectId, Uint8Array.from(data));
11976
+ await transportFailure.run(async () => transport.write?.(connectId, Uint8Array.from(data)));
11952
11977
  return { success: true, payload: void 0 };
11953
11978
  },
11954
11979
  apiRead: async () => {
11955
- const data = await transport.read?.(connectId);
11980
+ const data = await transportFailure.run(async () => transport.read?.(connectId));
11956
11981
  if (!data) {
11957
11982
  return {
11958
11983
  success: false,
@@ -11963,6 +11988,7 @@ var TrezorThpProtocol = class {
11963
11988
  }
11964
11989
  });
11965
11990
  if (!result.success) {
11991
+ transportFailure.rethrow();
11966
11992
  throw new Error(result.error.message ?? result.error.code);
11967
11993
  }
11968
11994
  if (previousNonce === this.thpState.sendNonce) {