@onekeyfe/hd-transport 1.0.7-alpha.4 → 1.0.8-alpha.0
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.
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
const ProtoBuf = require('protobufjs/light');
|
|
2
|
+
const ByteBuffer = require('bytebuffer');
|
|
3
|
+
|
|
4
|
+
const { decode } = require('../src/serialization/protobuf/decode');
|
|
5
|
+
const { decode: decodeProtocol } = require('../src/serialization/protocol/decode');
|
|
6
|
+
|
|
7
|
+
// eslint-disable-next-line import/no-unresolved
|
|
8
|
+
const messages = require('../messages.json');
|
|
9
|
+
|
|
10
|
+
const fixtures = [
|
|
11
|
+
{
|
|
12
|
+
name: 'Features-error-battery_level',
|
|
13
|
+
encodeMessage:
|
|
14
|
+
'0011000000260a096f6e656b65792e736f1002180020002801900101aa010131b00101b80163c00163c020ff',
|
|
15
|
+
out: {
|
|
16
|
+
vendor: 'onekey.so',
|
|
17
|
+
major_version: 2,
|
|
18
|
+
minor_version: 0,
|
|
19
|
+
patch_version: 0,
|
|
20
|
+
bootloader_mode: true,
|
|
21
|
+
firmware_present: true,
|
|
22
|
+
capabilities: [],
|
|
23
|
+
model: '1',
|
|
24
|
+
fw_major: 1,
|
|
25
|
+
fw_minor: 99,
|
|
26
|
+
fw_patch: 99,
|
|
27
|
+
battery_level: 4,
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
name: 'Features-success',
|
|
32
|
+
encodeMessage:
|
|
33
|
+
'0011000000260a096f6e656b65792e736f1002180020002801900101aa010131b00102b80163c00163c02004',
|
|
34
|
+
out: {
|
|
35
|
+
vendor: 'onekey.so',
|
|
36
|
+
major_version: 2,
|
|
37
|
+
minor_version: 0,
|
|
38
|
+
patch_version: 0,
|
|
39
|
+
bootloader_mode: true,
|
|
40
|
+
firmware_present: true,
|
|
41
|
+
capabilities: [],
|
|
42
|
+
model: '1',
|
|
43
|
+
fw_major: 2,
|
|
44
|
+
fw_minor: 99,
|
|
45
|
+
fw_patch: 99,
|
|
46
|
+
battery_level: 4,
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
];
|
|
50
|
+
|
|
51
|
+
describe('Fix messages decode', () => {
|
|
52
|
+
const Messages = ProtoBuf.Root.fromJSON(messages);
|
|
53
|
+
const Message = Messages.lookup(`Features`);
|
|
54
|
+
fixtures.forEach(f => {
|
|
55
|
+
describe(f.name, () => {
|
|
56
|
+
test('decode', () => {
|
|
57
|
+
// deserialize
|
|
58
|
+
const encoded = ByteBuffer.fromHex(f.encodeMessage);
|
|
59
|
+
const { buffer } = decodeProtocol(encoded);
|
|
60
|
+
const decoded = decode(Message, buffer);
|
|
61
|
+
|
|
62
|
+
// filter null values
|
|
63
|
+
Object.keys(decoded).forEach(key => {
|
|
64
|
+
if (decoded[key] == null) {
|
|
65
|
+
delete decoded[key];
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
expect(decoded).toEqual(f.out);
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
});
|
package/dist/index.js
CHANGED
|
@@ -135,7 +135,19 @@ function messageToJSON(Message, fields) {
|
|
|
135
135
|
const decode$1 = (Message, data) => {
|
|
136
136
|
const buff = data.toBuffer();
|
|
137
137
|
const a = new Uint8Array(buff);
|
|
138
|
-
|
|
138
|
+
let decoded;
|
|
139
|
+
try {
|
|
140
|
+
decoded = Message.decode(a);
|
|
141
|
+
}
|
|
142
|
+
catch (error) {
|
|
143
|
+
if (a.length > 1 && a[a.length - 1] === 0xff) {
|
|
144
|
+
a[a.length - 1] = 0x04;
|
|
145
|
+
decoded = Message.decode(a);
|
|
146
|
+
}
|
|
147
|
+
else {
|
|
148
|
+
throw error;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
139
151
|
return messageToJSON(decoded, decoded.$type.fields);
|
|
140
152
|
};
|
|
141
153
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"decode.d.ts","sourceRoot":"","sources":["../../../src/serialization/protobuf/decode.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAkB,MAAM,kBAAkB,CAAC;AACxD,OAAO,UAAU,MAAM,YAAY,CAAC;AAuEpC,eAAO,MAAM,MAAM,YAAa,IAAI,QAAQ,UAAU;;
|
|
1
|
+
{"version":3,"file":"decode.d.ts","sourceRoot":"","sources":["../../../src/serialization/protobuf/decode.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAkB,MAAM,kBAAkB,CAAC;AACxD,OAAO,UAAU,MAAM,YAAY,CAAC;AAuEpC,eAAO,MAAM,MAAM,YAAa,IAAI,QAAQ,UAAU;;CAsBrD,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@onekeyfe/hd-transport",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.8-alpha.0",
|
|
4
4
|
"description": "> TODO: description",
|
|
5
5
|
"author": "OneKey",
|
|
6
6
|
"homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme",
|
|
@@ -26,5 +26,5 @@
|
|
|
26
26
|
"long": "^4.0.0",
|
|
27
27
|
"protobufjs": "^6.11.2"
|
|
28
28
|
},
|
|
29
|
-
"gitHead": "
|
|
29
|
+
"gitHead": "5e68522825bfe3e42fac05c795fbee6caa7cc260"
|
|
30
30
|
}
|
|
@@ -73,7 +73,19 @@ function messageToJSON(Message: Message<Record<string, unknown>>, fields: Type['
|
|
|
73
73
|
export const decode = (Message: Type, data: ByteBuffer) => {
|
|
74
74
|
const buff = data.toBuffer();
|
|
75
75
|
const a = new Uint8Array(buff);
|
|
76
|
-
|
|
76
|
+
|
|
77
|
+
let decoded;
|
|
78
|
+
try {
|
|
79
|
+
decoded = Message.decode(a);
|
|
80
|
+
} catch (error) {
|
|
81
|
+
// Fix a battery_level problem with the device
|
|
82
|
+
if (a.length > 1 && a[a.length - 1] === 0xff) {
|
|
83
|
+
a[a.length - 1] = 0x04;
|
|
84
|
+
decoded = Message.decode(a);
|
|
85
|
+
} else {
|
|
86
|
+
throw error;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
77
89
|
|
|
78
90
|
// [compatibility]: in the end it should be possible to get rid of messageToJSON method and call
|
|
79
91
|
// Message.toObject(decoded) to return result as plain javascript object. This method should be able to do
|