@onekeyfe/hd-transport 1.2.8-alpha.11 → 1.2.8-alpha.13

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.
@@ -1,105 +0,0 @@
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
- });