@onekeyfe/hd-transport 1.2.7 → 1.2.8-alpha.10
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/__tests__/babylon-context-hash.test.js +44 -0
- package/__tests__/messages.test.js +67 -0
- package/__tests__/transport-log.test.js +6 -0
- package/dist/index.d.ts +29 -1
- package/dist/index.js +252510 -31
- package/dist/protocols/index.d.ts.map +1 -1
- package/dist/serialization/protobuf/messages.d.ts.map +1 -1
- package/dist/types/messages.d.ts +19 -0
- package/dist/types/messages.d.ts.map +1 -1
- package/dist/utils/transportLog.d.ts.map +1 -1
- package/lib/protobuf-static.d.ts +5 -0
- package/lib/protobuf-static.js +252460 -0
- package/package.json +5 -5
- package/scripts/protobuf-build.sh +30 -0
- package/scripts/protobuf-static.js +105 -0
- package/scripts/protobuf-types.js +1 -0
- package/src/protocols/index.ts +9 -23
- package/src/serialization/protobuf/messages.ts +34 -3
- package/src/types/messages.ts +24 -0
- package/src/utils/transportLog.ts +1 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@onekeyfe/hd-transport",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.8-alpha.10",
|
|
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",
|
|
@@ -11,9 +11,9 @@
|
|
|
11
11
|
"access": "public"
|
|
12
12
|
},
|
|
13
13
|
"scripts": {
|
|
14
|
-
"dev": "rimraf dist && rollup -c ../../build/rollup.config.js -w",
|
|
15
|
-
"build": "rimraf dist && rollup -c ../../build/rollup.config.js",
|
|
16
|
-
"test": "jest",
|
|
14
|
+
"dev": "node scripts/protobuf-static.js && rimraf dist && rollup -c ../../build/rollup.config.js -w",
|
|
15
|
+
"build": "node scripts/protobuf-static.js && rimraf dist && rollup -c ../../build/rollup.config.js",
|
|
16
|
+
"test": "node scripts/protobuf-static.js && jest",
|
|
17
17
|
"lint": "eslint .",
|
|
18
18
|
"lint:fix": "eslint . --fix",
|
|
19
19
|
"update:protobuf": "./scripts/protobuf-build.sh"
|
|
@@ -28,5 +28,5 @@
|
|
|
28
28
|
"long": "^4.0.0",
|
|
29
29
|
"protobufjs": "^6.11.2"
|
|
30
30
|
},
|
|
31
|
-
"gitHead": "
|
|
31
|
+
"gitHead": "47d2f63acb8e5fa9ee623c060af7c9f83ad6f939"
|
|
32
32
|
}
|
|
@@ -54,6 +54,36 @@ grep -hv -e '^import ' -e '^syntax' -e '^package' -e 'option java_' "$SRC_PATH"/
|
|
|
54
54
|
| sed 's/^option /\/\/ option /' \
|
|
55
55
|
| grep -v ' reserved ' >> "$V1_TMP_PROTO"
|
|
56
56
|
|
|
57
|
+
# Classic 1s exposes these messages before the shared firmware submodule includes them.
|
|
58
|
+
# Wire contract: https://onekeygroup.slack.com/archives/C0A7BUG1DT6/p1791598732003599
|
|
59
|
+
node - "$V1_TMP_PROTO" <<'NODE'
|
|
60
|
+
const fs = require('fs');
|
|
61
|
+
const protoPath = process.argv[2];
|
|
62
|
+
let proto = fs.readFileSync(protoPath, 'utf8');
|
|
63
|
+
proto = proto.replace('enum MessageType {', `enum MessageType {
|
|
64
|
+
MessageType_BabylonDeriveContextHash = 10054 [(bitcoin_only) = true, (wire_in) = true];
|
|
65
|
+
MessageType_BabylonDerivedContextHash = 10055 [(bitcoin_only) = true, (wire_out) = true];`);
|
|
66
|
+
proto += `
|
|
67
|
+
enum CanonicalBitcoinNetwork {
|
|
68
|
+
BITCOIN_MAINNET = 0;
|
|
69
|
+
BITCOIN_TESTNET = 1;
|
|
70
|
+
BITCOIN_SIGNET = 2;
|
|
71
|
+
BITCOIN_REGTEST = 3;
|
|
72
|
+
}
|
|
73
|
+
message BabylonDeriveContextHash {
|
|
74
|
+
repeated uint32 address_n = 1;
|
|
75
|
+
required InputScriptType script_type = 2;
|
|
76
|
+
required bytes app_name = 3;
|
|
77
|
+
required bytes context = 4;
|
|
78
|
+
required CanonicalBitcoinNetwork network = 5;
|
|
79
|
+
}
|
|
80
|
+
message BabylonDerivedContextHash {
|
|
81
|
+
required bytes secret = 1;
|
|
82
|
+
}
|
|
83
|
+
`;
|
|
84
|
+
fs.writeFileSync(protoPath, proto);
|
|
85
|
+
NODE
|
|
86
|
+
|
|
57
87
|
npx pbjs -t json -p "$DIST_PATH" -o "$DIST_PATH/messages.json" --keep-case "$V1_TMP_PROTO"
|
|
58
88
|
cp "$DIST_PATH/messages.json" "$CORE_MESSAGES_DIR/messages.json"
|
|
59
89
|
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const protobuf = require('protobufjs/light');
|
|
4
|
+
const pbjs = require('protobufjs/cli/pbjs');
|
|
5
|
+
|
|
6
|
+
const packageRoot = path.resolve(__dirname, '..');
|
|
7
|
+
const outputDirectory = path.join(packageRoot, 'lib');
|
|
8
|
+
const legacyProtocolInfoSchema = {
|
|
9
|
+
nested: {
|
|
10
|
+
LegacyProtocolInfo: {
|
|
11
|
+
fields: {
|
|
12
|
+
version: { rule: 'required', type: 'uint32', id: 1 },
|
|
13
|
+
supported_messages: {
|
|
14
|
+
rule: 'repeated',
|
|
15
|
+
type: 'uint32',
|
|
16
|
+
id: 2,
|
|
17
|
+
options: { packed: false },
|
|
18
|
+
},
|
|
19
|
+
protobuf_definition: { type: 'string', id: 3 },
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
// Match descriptors independent of JSON property order, without a hash collision risk.
|
|
26
|
+
function schemaKey(schema) {
|
|
27
|
+
return JSON.stringify(schema, (_key, value) => {
|
|
28
|
+
if (value && typeof value === 'object' && !Array.isArray(value)) {
|
|
29
|
+
return Object.keys(value)
|
|
30
|
+
.sort()
|
|
31
|
+
.reduce((result, key) => {
|
|
32
|
+
result[key] = value[key];
|
|
33
|
+
return result;
|
|
34
|
+
}, {});
|
|
35
|
+
}
|
|
36
|
+
return value;
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async function generate() {
|
|
41
|
+
fs.mkdirSync(outputDirectory, { recursive: true });
|
|
42
|
+
const legacyPath = path.join(outputDirectory, 'legacy-protocol-info.json');
|
|
43
|
+
fs.writeFileSync(legacyPath, JSON.stringify(legacyProtocolInfoSchema));
|
|
44
|
+
const sources = [
|
|
45
|
+
path.resolve(packageRoot, '../core/src/data/messages/messages.json'),
|
|
46
|
+
path.resolve(packageRoot, '../core/src/data/messages/messages_legacy_v1.json'),
|
|
47
|
+
path.join(packageRoot, 'messages-protocol-v2.json'),
|
|
48
|
+
legacyPath,
|
|
49
|
+
];
|
|
50
|
+
const entries = [];
|
|
51
|
+
for (const [index, source] of sources.entries()) {
|
|
52
|
+
const schema = JSON.parse(fs.readFileSync(source, 'utf8'));
|
|
53
|
+
const code = await new Promise((resolve, reject) => {
|
|
54
|
+
pbjs.main(
|
|
55
|
+
['-t', 'static', '--no-comments', '--no-service', '--root', `onekey-csp-${index}`, source],
|
|
56
|
+
(error, output) => (error ? reject(error) : resolve(output))
|
|
57
|
+
);
|
|
58
|
+
});
|
|
59
|
+
const types = [];
|
|
60
|
+
const collect = namespace => {
|
|
61
|
+
if (namespace instanceof protobuf.Type) {
|
|
62
|
+
const reference = namespace.fullName
|
|
63
|
+
.split('.')
|
|
64
|
+
.slice(1)
|
|
65
|
+
.map(name => `[${JSON.stringify(name)}]`)
|
|
66
|
+
.join('');
|
|
67
|
+
types.push(`${JSON.stringify(namespace.fullName)}: $root${reference}`);
|
|
68
|
+
}
|
|
69
|
+
(namespace.nestedArray || []).forEach(collect);
|
|
70
|
+
};
|
|
71
|
+
collect(protobuf.Root.fromJSON(schema));
|
|
72
|
+
entries.push(
|
|
73
|
+
`${JSON.stringify(
|
|
74
|
+
schemaKey(schema)
|
|
75
|
+
)}: function () {\nconst $protobuf = { ...protobuf, roots: {} };\n${code}\nreturn {${types.join(
|
|
76
|
+
',\n'
|
|
77
|
+
)}};\n}`
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
fs.writeFileSync(
|
|
81
|
+
path.join(outputDirectory, 'protobuf-static.js'),
|
|
82
|
+
`// Generated by scripts/protobuf-static.js. Do not edit.\n` +
|
|
83
|
+
`/* istanbul ignore file */\n` +
|
|
84
|
+
`const protobuf = require('protobufjs/minimal');\n` +
|
|
85
|
+
`protobuf.util.Long = require('long');\nprotobuf.configure();\n` +
|
|
86
|
+
`${schemaKey.toString()}\n` +
|
|
87
|
+
`module.exports = { schemaKey, legacyProtocolInfoSchema: ${JSON.stringify(
|
|
88
|
+
legacyProtocolInfoSchema
|
|
89
|
+
)}, schemas: {\n${entries.join(',\n')}\n} };\n`
|
|
90
|
+
);
|
|
91
|
+
fs.writeFileSync(
|
|
92
|
+
path.join(outputDirectory, 'protobuf-static.d.ts'),
|
|
93
|
+
`import type { Constructor, INamespace, Type } from 'protobufjs/light';\n` +
|
|
94
|
+
`type StaticMessageConstructor = Constructor<{}> & Pick<Type, 'encode' | 'decode' | 'verify' | 'fromObject' | 'toObject'>;\n` +
|
|
95
|
+
`export function schemaKey(schema: INamespace): string;\n` +
|
|
96
|
+
`export const schemas: Partial<Record<string, () => Record<string, StaticMessageConstructor>>>;\n` +
|
|
97
|
+
`export const legacyProtocolInfoSchema: INamespace;\n`
|
|
98
|
+
);
|
|
99
|
+
fs.unlinkSync(legacyPath);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
generate().catch(error => {
|
|
103
|
+
console.error(error);
|
|
104
|
+
process.exitCode = 1;
|
|
105
|
+
});
|
|
@@ -55,6 +55,7 @@ const hasParsedType = name => types.some(t => t && t.name === name);
|
|
|
55
55
|
// enums used as keys (string), used as values (number) by default
|
|
56
56
|
const ENUM_KEYS = [
|
|
57
57
|
'InputScriptType',
|
|
58
|
+
'CanonicalBitcoinNetwork',
|
|
58
59
|
'OutputScriptType',
|
|
59
60
|
'RequestType',
|
|
60
61
|
'KaspaInputScriptType',
|
package/src/protocols/index.ts
CHANGED
|
@@ -1,12 +1,17 @@
|
|
|
1
1
|
import ByteBuffer from 'bytebuffer';
|
|
2
|
-
import { Reader
|
|
2
|
+
import { Reader } from 'protobufjs/light';
|
|
3
3
|
|
|
4
4
|
import { encodeEnvelopeMessage, encodeMessageChunks, encodeTransportPackets } from './v1/packets';
|
|
5
5
|
import { decodeFirstChunk } from './v1/decode';
|
|
6
6
|
import { decodeMessage as decodeV1Message } from './v1/receive';
|
|
7
|
-
import {
|
|
7
|
+
import {
|
|
8
|
+
createMessageFromName,
|
|
9
|
+
createMessageFromType,
|
|
10
|
+
parseConfigure,
|
|
11
|
+
} from '../serialization/protobuf/messages';
|
|
8
12
|
import { encode as encodeProtobuf } from '../serialization/protobuf/encode';
|
|
9
13
|
import { decode as decodeProtobuf } from '../serialization/protobuf/decode';
|
|
14
|
+
import { legacyProtocolInfoSchema } from '../../lib/protobuf-static';
|
|
10
15
|
import {
|
|
11
16
|
decodeFrame as decodeV2Frame,
|
|
12
17
|
encodeProtobufFrame,
|
|
@@ -30,27 +35,8 @@ type ProtocolV2FrameOptions = {
|
|
|
30
35
|
seq?: number;
|
|
31
36
|
};
|
|
32
37
|
|
|
33
|
-
const LEGACY_PROTOCOL_V2_PROTOCOL_INFO =
|
|
34
|
-
|
|
35
|
-
version: {
|
|
36
|
-
rule: 'required',
|
|
37
|
-
type: 'uint32',
|
|
38
|
-
id: 1,
|
|
39
|
-
},
|
|
40
|
-
supported_messages: {
|
|
41
|
-
rule: 'repeated',
|
|
42
|
-
type: 'uint32',
|
|
43
|
-
id: 2,
|
|
44
|
-
options: {
|
|
45
|
-
packed: false,
|
|
46
|
-
},
|
|
47
|
-
},
|
|
48
|
-
protobuf_definition: {
|
|
49
|
-
type: 'string',
|
|
50
|
-
id: 3,
|
|
51
|
-
},
|
|
52
|
-
},
|
|
53
|
-
});
|
|
38
|
+
const LEGACY_PROTOCOL_V2_PROTOCOL_INFO =
|
|
39
|
+
parseConfigure(legacyProtocolInfoSchema).lookupType('LegacyProtocolInfo');
|
|
54
40
|
|
|
55
41
|
const decodeLegacyProtocolV2ProtocolInfo = (pbPayload: Uint8Array) => {
|
|
56
42
|
const byteBuffer = ByteBuffer.wrap(Buffer.from(pbPayload) as unknown as ArrayBuffer);
|
|
@@ -1,11 +1,42 @@
|
|
|
1
1
|
import * as protobuf from 'protobufjs/light';
|
|
2
2
|
|
|
3
|
+
import { schemaKey, schemas } from '../../../lib/protobuf-static';
|
|
4
|
+
|
|
3
5
|
export function parseConfigure(data: protobuf.INamespace) {
|
|
4
6
|
// @ts-ignore [compatiblity]: connect is sending stringified json
|
|
5
|
-
|
|
6
|
-
|
|
7
|
+
const schema = typeof data === 'string' ? JSON.parse(data) : data;
|
|
8
|
+
const key = schemaKey(schema);
|
|
9
|
+
const createConstructors = schemas[key];
|
|
10
|
+
// Field resolution mutates options; keep built-in descriptors reusable across connections.
|
|
11
|
+
const messages = protobuf.Root.fromJSON(
|
|
12
|
+
createConstructors ? JSON.parse(JSON.stringify(schema)) : schema
|
|
13
|
+
);
|
|
14
|
+
if (createConstructors) {
|
|
15
|
+
const constructors = createConstructors();
|
|
16
|
+
const types: protobuf.Type[] = [];
|
|
17
|
+
const collect = (namespace: protobuf.Namespace) => {
|
|
18
|
+
if (namespace instanceof protobuf.Type) types.push(namespace);
|
|
19
|
+
namespace.nestedArray.forEach(child => {
|
|
20
|
+
if (child instanceof protobuf.Namespace) collect(child);
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
collect(messages);
|
|
24
|
+
// Register every constructor before resolving fields, which access parent.ctor.
|
|
25
|
+
types.forEach(type => {
|
|
26
|
+
type.ctor = constructors[type.fullName];
|
|
27
|
+
});
|
|
28
|
+
types.forEach(type => {
|
|
29
|
+
const constructor = constructors[type.fullName];
|
|
30
|
+
type.encode = constructor.encode;
|
|
31
|
+
type.decode = constructor.decode;
|
|
32
|
+
type.verify = constructor.verify;
|
|
33
|
+
type.fromObject = constructor.fromObject;
|
|
34
|
+
type.toObject = constructor.toObject;
|
|
35
|
+
type.setup = () => type;
|
|
36
|
+
});
|
|
37
|
+
messages.resolveAll();
|
|
7
38
|
}
|
|
8
|
-
return
|
|
39
|
+
return messages;
|
|
9
40
|
}
|
|
10
41
|
|
|
11
42
|
export const createMessageFromName = (messages: protobuf.Root, name: string) => {
|
package/src/types/messages.ts
CHANGED
|
@@ -4554,6 +4554,28 @@ export enum CommandFlags {
|
|
|
4554
4554
|
Factory_Only = 1,
|
|
4555
4555
|
}
|
|
4556
4556
|
|
|
4557
|
+
export enum Enum_CanonicalBitcoinNetwork {
|
|
4558
|
+
BITCOIN_MAINNET = 0,
|
|
4559
|
+
BITCOIN_TESTNET = 1,
|
|
4560
|
+
BITCOIN_SIGNET = 2,
|
|
4561
|
+
BITCOIN_REGTEST = 3,
|
|
4562
|
+
}
|
|
4563
|
+
export type CanonicalBitcoinNetwork = keyof typeof Enum_CanonicalBitcoinNetwork;
|
|
4564
|
+
|
|
4565
|
+
// BabylonDeriveContextHash
|
|
4566
|
+
export type BabylonDeriveContextHash = {
|
|
4567
|
+
address_n: number[];
|
|
4568
|
+
script_type: InputScriptType;
|
|
4569
|
+
app_name: string;
|
|
4570
|
+
context: string;
|
|
4571
|
+
network: CanonicalBitcoinNetwork;
|
|
4572
|
+
};
|
|
4573
|
+
|
|
4574
|
+
// BabylonDerivedContextHash
|
|
4575
|
+
export type BabylonDerivedContextHash = {
|
|
4576
|
+
secret: string;
|
|
4577
|
+
};
|
|
4578
|
+
|
|
4557
4579
|
export type TextMemo = {
|
|
4558
4580
|
text: string;
|
|
4559
4581
|
};
|
|
@@ -5949,6 +5971,8 @@ export type MessageType = {
|
|
|
5949
5971
|
TronSignMessage: TronSignMessage;
|
|
5950
5972
|
TronMessageSignature: TronMessageSignature;
|
|
5951
5973
|
facotry: facotry;
|
|
5974
|
+
BabylonDeriveContextHash: BabylonDeriveContextHash;
|
|
5975
|
+
BabylonDerivedContextHash: BabylonDerivedContextHash;
|
|
5952
5976
|
TextMemo: TextMemo;
|
|
5953
5977
|
RefundMemo: RefundMemo;
|
|
5954
5978
|
CoinPurchaseMemo: CoinPurchaseMemo;
|