@onekeyfe/hd-transport 1.2.10-alpha.2 → 1.2.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__/solana-offchain-message-v1.test.js +33 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +26 -109027
- package/dist/protocols/index.d.ts.map +1 -1
- package/dist/serialization/protobuf/messages.d.ts +1 -1
- package/dist/serialization/protobuf/messages.d.ts.map +1 -1
- package/jest.config.js +0 -3
- package/package.json +4 -6
- package/scripts/protobuf-build.sh +0 -7
- package/src/protocols/index.ts +23 -10
- package/src/serialization/protobuf/messages.ts +6 -12
- package/__tests__/static-protobuf.test.js +0 -436
- package/scripts/protobuf-static-csp.js +0 -103
- package/scripts/protobuf-static.js +0 -85
- package/src/serialization/protobuf/generated/codecs.d.ts +0 -3
- package/src/serialization/protobuf/generated/codecs.js +0 -108990
- package/src/serialization/protobuf/legacy-protocol-info.json +0 -16
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
// Run after lerna run build, with --disallow-code-generation-from-strings.
|
|
2
|
-
const assert = require('assert');
|
|
3
|
-
const path = require('path');
|
|
4
|
-
const vm = require('vm');
|
|
5
|
-
const { readFileSync } = require('fs');
|
|
6
|
-
const { build } = require('tsup');
|
|
7
|
-
const protobuf = require('protobufjs/light');
|
|
8
|
-
const { default: transport } = require('../dist');
|
|
9
|
-
|
|
10
|
-
const v1 = require('../../core/src/data/messages/messages.json');
|
|
11
|
-
const legacy = require('../../core/src/data/messages/messages_legacy_v1.json');
|
|
12
|
-
const v2 = require('../messages-protocol-v2.json');
|
|
13
|
-
|
|
14
|
-
const schemas = [v1, legacy, v2];
|
|
15
|
-
function collectTypes(namespace) {
|
|
16
|
-
return [
|
|
17
|
-
...(namespace instanceof protobuf.Type ? [namespace] : []),
|
|
18
|
-
...(namespace.nestedArray || []).flatMap(collectTypes),
|
|
19
|
-
];
|
|
20
|
-
}
|
|
21
|
-
let blocked = 0;
|
|
22
|
-
const dynamic = () => {
|
|
23
|
-
blocked += 1;
|
|
24
|
-
throw new Error('Unexpected dynamic protobuf setup');
|
|
25
|
-
};
|
|
26
|
-
protobuf.Type.prototype.setup = dynamic;
|
|
27
|
-
protobuf.Type.generateConstructor = dynamic;
|
|
28
|
-
let count = 0;
|
|
29
|
-
for (const schema of schemas) {
|
|
30
|
-
const root = transport.parseConfigure(schema);
|
|
31
|
-
for (const type of collectTypes(root)) {
|
|
32
|
-
// Missing required fields can throw; no constructor or codec may fall back to codegen.
|
|
33
|
-
for (const operation of [
|
|
34
|
-
() => type.fromObject({}),
|
|
35
|
-
() => type.encode(type.create()).finish(),
|
|
36
|
-
() => type.decode(new Uint8Array()),
|
|
37
|
-
() => type.verify({}),
|
|
38
|
-
() => type.toObject(type.create()),
|
|
39
|
-
]) {
|
|
40
|
-
try {
|
|
41
|
-
operation();
|
|
42
|
-
} catch (error) {
|
|
43
|
-
if (error instanceof EvalError) throw error;
|
|
44
|
-
}
|
|
45
|
-
count += 1;
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
assert.strictEqual(blocked, 0);
|
|
50
|
-
console.log(`CSP static protobuf: ${count} operations, no dynamic setup`);
|
|
51
|
-
|
|
52
|
-
async function checkBrowser() {
|
|
53
|
-
const outDir = path.resolve(__dirname, '../../../node_modules/.cache/protobuf-static/browser');
|
|
54
|
-
await build({
|
|
55
|
-
entry: { browser: path.resolve(__dirname, '../dist/index.js') },
|
|
56
|
-
outDir,
|
|
57
|
-
format: ['iife'],
|
|
58
|
-
globalName: 'transport',
|
|
59
|
-
platform: 'browser',
|
|
60
|
-
noExternal: [/.*/],
|
|
61
|
-
splitting: false,
|
|
62
|
-
silent: true,
|
|
63
|
-
config: false,
|
|
64
|
-
});
|
|
65
|
-
const code = readFileSync(path.join(outDir, 'browser.global.js'), 'utf8');
|
|
66
|
-
const context = vm.createContext(
|
|
67
|
-
{ schemas },
|
|
68
|
-
{ codeGeneration: { strings: false, wasm: false } }
|
|
69
|
-
);
|
|
70
|
-
vm.runInContext(code, context);
|
|
71
|
-
const operations = vm.runInContext(
|
|
72
|
-
`
|
|
73
|
-
let count = 0;
|
|
74
|
-
function visit(namespace) {
|
|
75
|
-
if (namespace.fields) {
|
|
76
|
-
if (!Object.hasOwn(namespace, 'encode')) throw Error('Missing static codec');
|
|
77
|
-
for (const operation of [
|
|
78
|
-
() => namespace.fromObject({}),
|
|
79
|
-
() => namespace.encode(namespace.create()).finish(),
|
|
80
|
-
() => namespace.decode(new Uint8Array()),
|
|
81
|
-
() => namespace.verify({}),
|
|
82
|
-
() => namespace.toObject(namespace.create())
|
|
83
|
-
]) {
|
|
84
|
-
try { operation(); } catch (error) {
|
|
85
|
-
if (error instanceof EvalError) throw error;
|
|
86
|
-
}
|
|
87
|
-
count += 1;
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
for (const child of namespace.nestedArray || []) visit(child);
|
|
91
|
-
}
|
|
92
|
-
for (const schema of schemas) visit(transport.default.parseConfigure(schema));
|
|
93
|
-
count;
|
|
94
|
-
`,
|
|
95
|
-
context
|
|
96
|
-
);
|
|
97
|
-
assert.strictEqual(operations, count);
|
|
98
|
-
console.log(`Browser CSP: ${operations} operations, ${code.length} bytes before minification`);
|
|
99
|
-
}
|
|
100
|
-
checkBrowser().catch(error => {
|
|
101
|
-
console.error(error.message);
|
|
102
|
-
process.exitCode = 1;
|
|
103
|
-
});
|
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
const fs = require('fs');
|
|
2
|
-
const path = require('path');
|
|
3
|
-
const protobuf = require('protobufjs/light');
|
|
4
|
-
const Long = require('long');
|
|
5
|
-
|
|
6
|
-
protobuf.util.Long = Long;
|
|
7
|
-
protobuf.configure();
|
|
8
|
-
|
|
9
|
-
const packageRoot = path.resolve(__dirname, '..');
|
|
10
|
-
const outputDirectory = path.join(packageRoot, 'src/serialization/protobuf/generated');
|
|
11
|
-
const schemaPaths = [
|
|
12
|
-
'../core/src/data/messages/messages.json',
|
|
13
|
-
'../core/src/data/messages/messages_legacy_v1.json',
|
|
14
|
-
'messages-protocol-v2.json',
|
|
15
|
-
'src/serialization/protobuf/legacy-protocol-info.json',
|
|
16
|
-
];
|
|
17
|
-
const methods = ['encode', 'decode', 'verify', 'fromObject', 'toObject'];
|
|
18
|
-
const factories = new Map();
|
|
19
|
-
const registrations = [];
|
|
20
|
-
|
|
21
|
-
function collectTypes(namespace) {
|
|
22
|
-
return [
|
|
23
|
-
...(namespace instanceof protobuf.Type ? [namespace] : []),
|
|
24
|
-
...(namespace.nestedArray || []).flatMap(collectTypes),
|
|
25
|
-
];
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
for (const schemaPath of schemaPaths) {
|
|
29
|
-
const schema = JSON.parse(fs.readFileSync(path.join(packageRoot, schemaPath), 'utf8'));
|
|
30
|
-
const root = protobuf.Root.fromJSON(schema);
|
|
31
|
-
// Capture before resolution, which can normalize field options in place.
|
|
32
|
-
const signature = JSON.stringify(root.toJSON());
|
|
33
|
-
const registrationsForSchema = [];
|
|
34
|
-
for (const type of collectTypes(root)) {
|
|
35
|
-
type.setup();
|
|
36
|
-
const source = [
|
|
37
|
-
`type.ctor = ${type.ctor.toString()};`,
|
|
38
|
-
'const types = type.fieldsArray.map(field => field.resolvedType);',
|
|
39
|
-
...methods.map(method => `type.${method} = ${type[method].toString()};`),
|
|
40
|
-
].join('\n');
|
|
41
|
-
if (!factories.has(source)) factories.set(source, factories.size);
|
|
42
|
-
registrationsForSchema.push([type.fullName, factories.get(source)]);
|
|
43
|
-
}
|
|
44
|
-
registrations.push({ signature, types: registrationsForSchema });
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
// Keep the exact generated algorithms, including legacy conversion quirks.
|
|
48
|
-
const source = [
|
|
49
|
-
'// @generated by scripts/protobuf-static.js. DO NOT EDIT.',
|
|
50
|
-
`const { Reader, Writer, util } = require('protobufjs/minimal');`,
|
|
51
|
-
'const factories = [',
|
|
52
|
-
...Array.from(factories.keys(), body => `function(type) {\n${body}\n},`),
|
|
53
|
-
'];',
|
|
54
|
-
`const schemas = ${JSON.stringify(registrations)};`,
|
|
55
|
-
'exports.installStaticCodecs = function(root) {',
|
|
56
|
-
' const signature = JSON.stringify(root.toJSON());',
|
|
57
|
-
' const schema = schemas.find(candidate => candidate.signature === signature);',
|
|
58
|
-
' if (!schema) return false;',
|
|
59
|
-
' for (const [name, factory] of schema.types) factories[factory](root.lookupType(name));',
|
|
60
|
-
' return true;',
|
|
61
|
-
'};',
|
|
62
|
-
'',
|
|
63
|
-
].join('\n');
|
|
64
|
-
const declaration = [
|
|
65
|
-
'// @generated by scripts/protobuf-static.js. DO NOT EDIT.',
|
|
66
|
-
"import type { Root } from 'protobufjs/light';",
|
|
67
|
-
'export declare function installStaticCodecs(root: Root): boolean;',
|
|
68
|
-
'',
|
|
69
|
-
].join('\n');
|
|
70
|
-
|
|
71
|
-
for (const [name, content] of [
|
|
72
|
-
['codecs.js', source],
|
|
73
|
-
['codecs.d.ts', declaration],
|
|
74
|
-
]) {
|
|
75
|
-
const destination = path.join(outputDirectory, name);
|
|
76
|
-
if (process.argv.includes('--check')) {
|
|
77
|
-
if (!fs.existsSync(destination) || fs.readFileSync(destination, 'utf8') !== content) {
|
|
78
|
-
throw new Error(`Static protobuf is stale: run yarn update-protobuf:static (${name})`);
|
|
79
|
-
}
|
|
80
|
-
} else {
|
|
81
|
-
fs.mkdirSync(outputDirectory, { recursive: true });
|
|
82
|
-
fs.writeFileSync(destination, content);
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
console.log(`Static protobuf: ${registrations.length} schemas, ${factories.size} shared codecs`);
|