@onekeyfe/hd-transport 1.2.0-alpha.1 → 1.2.0-alpha.3

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.
@@ -70,6 +70,14 @@ describe('messages', () => {
70
70
  expect(() => createMessageFromType(messages, 0)).not.toThrow();
71
71
  });
72
72
 
73
+ test('createMessageFromType throws a readable error for unknown ids', () => {
74
+ const messages = parseConfigure(json);
75
+
76
+ expect(() => createMessageFromType(messages, 99999)).toThrow(
77
+ 'MessageType id "99999" is not defined in protobuf schema'
78
+ );
79
+ });
80
+
73
81
  test('createMessageFromName (wire_type case)', () => {
74
82
  const messages = parseConfigure(json);
75
83
  const name = 'TxAckInput';
@@ -15,6 +15,18 @@ const protocolV1Messages = parseConfigure({
15
15
  },
16
16
  },
17
17
  },
18
+ Failure: {
19
+ fields: {
20
+ code: {
21
+ type: 'uint32',
22
+ id: 1,
23
+ },
24
+ message: {
25
+ type: 'string',
26
+ id: 2,
27
+ },
28
+ },
29
+ },
18
30
  ButtonRequest: {
19
31
  fields: {
20
32
  code: {
@@ -32,6 +44,7 @@ const protocolV1Messages = parseConfigure({
32
44
  MessageType: {
33
45
  values: {
34
46
  MessageType_Success: 2,
47
+ MessageType_Failure: 3,
35
48
  MessageType_ButtonRequest: 26,
36
49
  MessageType_OnekeyGetFeatures: 10025,
37
50
  MessageType_OnekeyFeatures: 10026,
@@ -714,6 +727,26 @@ describe('Protocol V2 framing and session', () => {
714
727
  expect(decoded.type).toBe('ButtonRequest');
715
728
  });
716
729
 
730
+ test('decodes legacy V1 Failure as a Protocol V2 fallback', () => {
731
+ // Some device-side rejection paths still return legacy Failure(type=3)
732
+ // inside a Protocol V2 frame. It must surface as a device Failure, not as
733
+ // a protobuf catalog TypeError.
734
+ const frame = ProtocolV2.encodeFrame(
735
+ { ...schemas, protocolV2: schemas.protocolV1 },
736
+ 'Failure',
737
+ {
738
+ code: 1,
739
+ message: 'Action cancelled',
740
+ }
741
+ );
742
+ const decoded = ProtocolV2.decodeFrame(schemas, frame);
743
+ expect(decoded.type).toBe('Failure');
744
+ expect(decoded.message).toEqual({
745
+ code: 1,
746
+ message: 'Action cancelled',
747
+ });
748
+ });
749
+
717
750
  test('does not fall back to legacy V1 messages outside the allowlist', () => {
718
751
  // OnekeyFeatures exists only in the V1 schema and is not allowlisted.
719
752
  const frame = protocolV2.encodeProtobufFrame(10026, new Uint8Array(0));
package/dist/index.js CHANGED
@@ -249,7 +249,11 @@ const createMessageFromName = (messages, name) => {
249
249
  };
250
250
  const createMessageFromType = (messages, typeId) => {
251
251
  const MessageType = messages.lookupEnum('MessageType');
252
- const messageName = MessageType.valuesById[typeId].replace('MessageType_', '');
252
+ const rawMessageName = MessageType.valuesById[typeId];
253
+ if (!rawMessageName) {
254
+ throw new Error(`MessageType id "${typeId}" is not defined in protobuf schema`);
255
+ }
256
+ const messageName = rawMessageName.replace('MessageType_', '');
253
257
  const Message = messages.lookupType(messageName);
254
258
  return {
255
259
  Message,
@@ -605,6 +609,7 @@ const resolveProtocolV2EncodeSchema = (name, schemas) => {
605
609
  }
606
610
  };
607
611
  const PROTOCOL_V2_LEGACY_DECODE_ALLOWLIST = new Set([
612
+ 'Failure',
608
613
  'ButtonRequest',
609
614
  'EntropyRequest',
610
615
  'PinMatrixRequest',
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/protocols/index.ts"],"names":[],"mappings":";AAAA,OAAO,UAAU,MAAM,YAAY,CAAC;AAEpC,OAAO,EAAE,qBAAqB,EAA+C,MAAM,cAAc,CAAC;AAElG,OAAO,EAAE,aAAa,IAAI,eAAe,EAAE,MAAM,cAAc,CAAC;AAMhE,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAC7C,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,MAAM,CAAC;AAElD,eAAO,MAAM,iCAAiC,QAAQ,CAAC;AAEvD,KAAK,iBAAiB,GAAG;IACv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E,qBAAqB,CAAC;IAC/B,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB,CAAC;AAsCF,eAAO,MAAM,UAAU;;;;;;;;;;CAOtB,CAAC;AAEF,eAAO,MAAM,UAAU;yBAEV,iBAAiB,QACpB,MAAM,QACN,OAAO,MAAM,EAAE,OAAO,CAAC,YACpB,sBAAsB;yBAkCtB,iBAAiB,SACnB,UAAU,YACR,KAAK,sBAAsB,EAAE,QAAQ,GAAG,WAAW,GAAG,SAAS,CAAC;;;;;;;;;;CA4B5E,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"messages.d.ts","sourceRoot":"","sources":["../../../src/serialization/protobuf/messages.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,QAAQ,MAAM,kBAAkB,CAAC;AAE7C,wBAAgB,cAAc,CAAC,IAAI,EAAE,QAAQ,CAAC,UAAU,iBAMvD;AAED,eAAO,MAAM,qBAAqB,aAAc,SAAS,IAAI,QAAQ,MAAM;;;CAiB1E,CAAC;AAEF,eAAO,MAAM,qBAAqB,aAAc,SAAS,IAAI,UAAU,MAAM;;;CAW5E,CAAC"}
1
+ {"version":3,"file":"messages.d.ts","sourceRoot":"","sources":["../../../src/serialization/protobuf/messages.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,QAAQ,MAAM,kBAAkB,CAAC;AAE7C,wBAAgB,cAAc,CAAC,IAAI,EAAE,QAAQ,CAAC,UAAU,iBAMvD;AAED,eAAO,MAAM,qBAAqB,aAAc,SAAS,IAAI,QAAQ,MAAM;;;CAiB1E,CAAC;AAEF,eAAO,MAAM,qBAAqB,aAAc,SAAS,IAAI,UAAU,MAAM;;;CAgB5E,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onekeyfe/hd-transport",
3
- "version": "1.2.0-alpha.1",
3
+ "version": "1.2.0-alpha.3",
4
4
  "description": "Transport layer abstractions and utilities for OneKey hardware SDK.",
5
5
  "author": "OneKey",
6
6
  "homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme",
@@ -28,5 +28,5 @@
28
28
  "long": "^4.0.0",
29
29
  "protobufjs": "^6.11.2"
30
30
  },
31
- "gitHead": "4f4aae3a47be7bd54b4db4a10c400056784e01bd"
31
+ "gitHead": "54a4a40baee7e376b9a810dade60007d2c9b00c9"
32
32
  }
@@ -38,6 +38,7 @@ const resolveProtocolV2EncodeSchema = (name: string, schemas: ProtocolV2Schemas)
38
38
  };
39
39
 
40
40
  const PROTOCOL_V2_LEGACY_DECODE_ALLOWLIST = new Set([
41
+ 'Failure',
41
42
  'ButtonRequest',
42
43
  'EntropyRequest',
43
44
  'PinMatrixRequest',
@@ -30,7 +30,12 @@ export const createMessageFromName = (messages: protobuf.Root, name: string) =>
30
30
  export const createMessageFromType = (messages: protobuf.Root, typeId: number) => {
31
31
  const MessageType = messages.lookupEnum('MessageType');
32
32
 
33
- const messageName = MessageType.valuesById[typeId].replace('MessageType_', '');
33
+ const rawMessageName = MessageType.valuesById[typeId];
34
+ if (!rawMessageName) {
35
+ throw new Error(`MessageType id "${typeId}" is not defined in protobuf schema`);
36
+ }
37
+
38
+ const messageName = rawMessageName.replace('MessageType_', '');
34
39
 
35
40
  const Message = messages.lookupType(messageName);
36
41