@onekeyfe/hwk-trezor-connector 1.2.1 → 1.2.2-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
@@ -7273,7 +7273,7 @@ var transformSchemaFields = (operation, schema, sourceData) => {
7273
7273
  } else if (schemaField.fieldKind === "scalar") {
7274
7274
  transformedData[schemaField.name] = transformField(schemaField, fieldValue);
7275
7275
  } else if (schemaField.fieldKind === "enum") {
7276
- transformedData[schemaField.name] = fieldValue;
7276
+ transformedData[schemaField.name] = transformField(schemaField, fieldValue);
7277
7277
  } else {
7278
7278
  transformedData[schemaField.name] = fieldValue;
7279
7279
  }
@@ -7383,8 +7383,10 @@ var ProtobufManager = () => {
7383
7383
  const decode = (type, data) => {
7384
7384
  const { schema, messageName } = findSchema(type);
7385
7385
  const binaryPayload = Buffer.isBuffer(data) ? data : Buffer.from(data);
7386
- const patchedPayload = messageName === "Failure" && binaryPayload.subarray(0, 2).compare(Buffer.from("0811", "hex")) === 0 ? binaryPayload.subarray(0, 2) : binaryPayload;
7387
- const decoded = fromBinary(schema, patchedPayload, { readUnknownFields: false });
7386
+ const patchedPayload = messageName === "Failure" && binaryPayload[0] === 8 && binaryPayload[1] === 17 ? binaryPayload.subarray(0, 2) : binaryPayload;
7387
+ const decoded = fromBinary(schema, Uint8Array.from(patchedPayload), {
7388
+ readUnknownFields: false
7389
+ });
7388
7390
  const json = toJson(schema, decoded, { useProtoFieldName: true });
7389
7391
  const message = transformSchemaFields("decode", schema, json);
7390
7392
  return {
@@ -7678,6 +7680,111 @@ function getTrezorResponseMessage(response) {
7678
7680
  }
7679
7681
  return {};
7680
7682
  }
7683
+ var AUTHENTICITY_PROOF_CHUNK_SIZE = 500;
7684
+ var MAX_AUTHENTICITY_PROOF_PART_SIZE = 64 * 1024;
7685
+ var MAX_AUTHENTICITY_CERTIFICATE_COUNT = 4;
7686
+ var validateAuthenticityProofPartSize = (size) => {
7687
+ if (typeof size !== "number" || !Number.isSafeInteger(size) || size < 0 || size > MAX_AUTHENTICITY_PROOF_PART_SIZE) {
7688
+ throw (0, import_hwk_adapter_core2.createHwkError)({
7689
+ code: import_hwk_adapter_core2.HardwareErrorCode.UnknownError,
7690
+ message: `Invalid Trezor authenticity proof part size: ${String(size)}`
7691
+ });
7692
+ }
7693
+ return size;
7694
+ };
7695
+ var readAuthenticityProofPart = async ({
7696
+ deviceSession,
7697
+ proofType,
7698
+ index,
7699
+ size
7700
+ }) => {
7701
+ const expectedSize = validateAuthenticityProofPartSize(size);
7702
+ let offset = 0;
7703
+ let result = "";
7704
+ while (offset < expectedSize) {
7705
+ const requestedSize = Math.min(AUTHENTICITY_PROOF_CHUNK_SIZE, expectedSize - offset);
7706
+ const request = {
7707
+ proof_type: proofType,
7708
+ offset,
7709
+ size: requestedSize
7710
+ };
7711
+ if (index !== void 0) request.index = index;
7712
+ const response = await deviceSession.call(
7713
+ "GetAuthenticityProofChunk",
7714
+ request
7715
+ );
7716
+ if (response.type !== "AuthenticityProofChunk") {
7717
+ throw (0, import_hwk_adapter_core2.createHwkError)({
7718
+ code: import_hwk_adapter_core2.HardwareErrorCode.UnknownError,
7719
+ message: `Expected AuthenticityProofChunk, received ${response.type}`
7720
+ });
7721
+ }
7722
+ const chunk = response.message?.chunk;
7723
+ if (typeof chunk !== "string" || chunk.length !== requestedSize * 2) {
7724
+ throw (0, import_hwk_adapter_core2.createHwkError)({
7725
+ code: import_hwk_adapter_core2.HardwareErrorCode.UnknownError,
7726
+ message: "Trezor returned an invalid authenticity proof chunk"
7727
+ });
7728
+ }
7729
+ result += chunk;
7730
+ offset += requestedSize;
7731
+ }
7732
+ return result;
7733
+ };
7734
+ var readAuthenticityProofStream = async (deviceSession, sizes) => {
7735
+ const readCertificates = async (proofType, values) => {
7736
+ if (!Array.isArray(values)) {
7737
+ throw (0, import_hwk_adapter_core2.createHwkError)({
7738
+ code: import_hwk_adapter_core2.HardwareErrorCode.UnknownError,
7739
+ message: "Trezor returned invalid authenticity certificate sizes"
7740
+ });
7741
+ }
7742
+ if (values.length > MAX_AUTHENTICITY_CERTIFICATE_COUNT) {
7743
+ throw (0, import_hwk_adapter_core2.createHwkError)({
7744
+ code: import_hwk_adapter_core2.HardwareErrorCode.UnknownError,
7745
+ message: `Trezor authenticity proof contains too many certificates: ${values.length}`
7746
+ });
7747
+ }
7748
+ const certificates = [];
7749
+ for (let index = 0; index < values.length; index += 1) {
7750
+ certificates.push(
7751
+ await readAuthenticityProofPart({
7752
+ deviceSession,
7753
+ proofType,
7754
+ index,
7755
+ size: validateAuthenticityProofPartSize(values[index])
7756
+ })
7757
+ );
7758
+ }
7759
+ return certificates;
7760
+ };
7761
+ const readSignature = async (proofType, size) => {
7762
+ if (size === void 0 || size === null || size === 0) return void 0;
7763
+ return readAuthenticityProofPart({
7764
+ deviceSession,
7765
+ proofType,
7766
+ size: validateAuthenticityProofPartSize(size)
7767
+ });
7768
+ };
7769
+ try {
7770
+ const optigaCertificates = await readCertificates(0, sizes.optiga_certificates ?? []);
7771
+ const optigaSignature = await readSignature(0, sizes.optiga_signature);
7772
+ const tropicCertificates = await readCertificates(1, sizes.tropic_certificates ?? []);
7773
+ const tropicSignature = await readSignature(1, sizes.tropic_signature);
7774
+ const mcuCertificates = await readCertificates(2, sizes.mcu_certificates ?? []);
7775
+ const mcuSignature = await readSignature(2, sizes.mcu_signature);
7776
+ return {
7777
+ optiga_certificates: optigaCertificates,
7778
+ optiga_signature: optigaSignature,
7779
+ tropic_certificates: tropicCertificates,
7780
+ tropic_signature: tropicSignature,
7781
+ mcu_certificates: mcuCertificates,
7782
+ mcu_signature: mcuSignature
7783
+ };
7784
+ } finally {
7785
+ await deviceSession.call("GetAuthenticityProofChunk", { offset: 0, size: 0 });
7786
+ }
7787
+ };
7681
7788
  var TrezorConnectorBase = class {
7682
7789
  connectionType;
7683
7790
  chunkSize;
@@ -7922,6 +8029,26 @@ var TrezorConnectorBase = class {
7922
8029
  );
7923
8030
  case "wipeDevice":
7924
8031
  return getTrezorResponseMessage(await session.deviceSession.call("WipeDevice", {}));
8032
+ case "authenticateDevice": {
8033
+ const p = params ?? {};
8034
+ const response = await session.deviceSession.call("AuthenticateDevice", {
8035
+ challenge: p.challenge,
8036
+ stream: true
8037
+ });
8038
+ if (response.type === "AuthenticityProof") {
8039
+ return getTrezorResponseMessage(response);
8040
+ }
8041
+ if (response.type !== "AuthenticityProofSizes") {
8042
+ throw (0, import_hwk_adapter_core2.createHwkError)({
8043
+ code: import_hwk_adapter_core2.HardwareErrorCode.UnknownError,
8044
+ message: `Expected Trezor authenticity proof, received ${response.type}`
8045
+ });
8046
+ }
8047
+ return readAuthenticityProofStream(
8048
+ session.deviceSession,
8049
+ response.message
8050
+ );
8051
+ }
7925
8052
  // --- THP application-session control (Trezor-specific, driven by the
7926
8053
  // adapter to align the active passphrase session before a chain call).
7927
8054
  // The connector deals only in session ids; wallet identity belongs to the