@onekeyfe/hd-transport 1.2.10-alpha.1 → 1.2.10-alpha.2

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.
@@ -11034,6 +11034,94 @@
11034
11034
  }
11035
11035
  }
11036
11036
  },
11037
+ "ZcashAddressType": {
11038
+ "values": {
11039
+ "UNIFIED_ORCHARD_P2PKH": 0,
11040
+ "TRANSPARENT_P2PKH": 1,
11041
+ "UNIFIED_ORCHARD": 2
11042
+ }
11043
+ },
11044
+ "ZcashAddressScope": {
11045
+ "values": {
11046
+ "EXTERNAL": 0,
11047
+ "INTERNAL": 1
11048
+ }
11049
+ },
11050
+ "ZcashGetAddress": {
11051
+ "fields": {
11052
+ "address_n": {
11053
+ "rule": "repeated",
11054
+ "type": "uint32",
11055
+ "id": 1,
11056
+ "options": {
11057
+ "packed": false
11058
+ }
11059
+ },
11060
+ "show_display": {
11061
+ "type": "bool",
11062
+ "id": 2
11063
+ },
11064
+ "include_seed_fingerprint": {
11065
+ "type": "bool",
11066
+ "id": 3
11067
+ },
11068
+ "diversifier_index": {
11069
+ "type": "uint32",
11070
+ "id": 5,
11071
+ "options": {
11072
+ "default": 0
11073
+ }
11074
+ },
11075
+ "address_type": {
11076
+ "type": "ZcashAddressType",
11077
+ "id": 6,
11078
+ "options": {
11079
+ "default": "UNIFIED_ORCHARD_P2PKH"
11080
+ }
11081
+ },
11082
+ "scope": {
11083
+ "type": "ZcashAddressScope",
11084
+ "id": 7,
11085
+ "options": {
11086
+ "default": "EXTERNAL"
11087
+ }
11088
+ }
11089
+ }
11090
+ },
11091
+ "ZcashAddress": {
11092
+ "fields": {
11093
+ "address": {
11094
+ "rule": "required",
11095
+ "type": "string",
11096
+ "id": 1
11097
+ },
11098
+ "seed_fingerprint": {
11099
+ "type": "bytes",
11100
+ "id": 3
11101
+ }
11102
+ }
11103
+ },
11104
+ "ZcashGetFullViewingKey": {
11105
+ "fields": {
11106
+ "address_n": {
11107
+ "rule": "repeated",
11108
+ "type": "uint32",
11109
+ "id": 1,
11110
+ "options": {
11111
+ "packed": false
11112
+ }
11113
+ }
11114
+ }
11115
+ },
11116
+ "ZcashFullViewingKey": {
11117
+ "fields": {
11118
+ "ufvk": {
11119
+ "rule": "required",
11120
+ "type": "string",
11121
+ "id": 1
11122
+ }
11123
+ }
11124
+ },
11037
11125
  "ZcashSignPczt": {
11038
11126
  "fields": {
11039
11127
  "pczt_length": {
@@ -11514,11 +11602,15 @@
11514
11602
  "MessageType_NeoAddress": 12302,
11515
11603
  "MessageType_NeoSignTx": 12303,
11516
11604
  "MessageType_NeoSignedTx": 12304,
11605
+ "MessageType_ZcashGetAddress": 12400,
11606
+ "MessageType_ZcashAddress": 12401,
11517
11607
  "MessageType_ZcashSignPczt": 12402,
11518
11608
  "MessageType_ZcashSignedPczt": 12403,
11519
11609
  "MessageType_ZcashPcztChunkRequest": 12404,
11520
11610
  "MessageType_ZcashPcztChunkAck": 12405,
11521
11611
  "MessageType_ZcashSignedPcztChunkRequest": 12406,
11612
+ "MessageType_ZcashGetFullViewingKey": 12407,
11613
+ "MessageType_ZcashFullViewingKey": 12408,
11522
11614
  "MessageType_DeviceFactoryInfoSet": 60000,
11523
11615
  "MessageType_DeviceFactoryInfoGet": 60001,
11524
11616
  "MessageType_DeviceFactoryInfo": 60002,
@@ -11571,9 +11663,7 @@
11571
11663
  "MessageType_PortfolioUpdate": 61400,
11572
11664
  "MessageType_NftUpdate": 61500,
11573
11665
  "MessageType_OnboardingStatusGet": 61600,
11574
- "MessageType_OnboardingStatus": 61601,
11575
- "MessageType_ZcashGetAddress": 12400,
11576
- "MessageType_ZcashAddress": 12401
11666
+ "MessageType_OnboardingStatus": 61601
11577
11667
  },
11578
11668
  "reserved": [
11579
11669
  [90, 92],
@@ -13792,81 +13882,6 @@
13792
13882
  }
13793
13883
  }
13794
13884
  }
13795
- },
13796
- "ZcashAddressType": {
13797
- "values": {
13798
- "UNIFIED_ORCHARD_P2PKH": 0,
13799
- "TRANSPARENT_P2PKH": 1,
13800
- "UNIFIED_ORCHARD": 2
13801
- }
13802
- },
13803
- "ZcashAddressScope": {
13804
- "values": {
13805
- "EXTERNAL": 0,
13806
- "INTERNAL": 1
13807
- }
13808
- },
13809
- "ZcashGetAddress": {
13810
- "fields": {
13811
- "address_n": {
13812
- "rule": "repeated",
13813
- "type": "uint32",
13814
- "id": 1,
13815
- "options": {
13816
- "packed": false
13817
- }
13818
- },
13819
- "show_display": {
13820
- "type": "bool",
13821
- "id": 2
13822
- },
13823
- "include_seed_fingerprint": {
13824
- "type": "bool",
13825
- "id": 3
13826
- },
13827
- "include_ufvk": {
13828
- "type": "bool",
13829
- "id": 4
13830
- },
13831
- "diversifier_index": {
13832
- "type": "uint32",
13833
- "id": 5,
13834
- "options": {
13835
- "default": 0
13836
- }
13837
- },
13838
- "address_type": {
13839
- "type": "ZcashAddressType",
13840
- "id": 6,
13841
- "options": {
13842
- "default": "UNIFIED_ORCHARD_P2PKH"
13843
- }
13844
- },
13845
- "scope": {
13846
- "type": "ZcashAddressScope",
13847
- "id": 7,
13848
- "options": {
13849
- "default": "EXTERNAL"
13850
- }
13851
- }
13852
- }
13853
- },
13854
- "ZcashAddress": {
13855
- "fields": {
13856
- "address": {
13857
- "rule": "required",
13858
- "type": "string",
13859
- "id": 1
13860
- },
13861
- "ufvk": {
13862
- "type": "string",
13863
- "id": 2
13864
- },
13865
- "seed_fingerprint": {
13866
- "type": "bytes",
13867
- "id": 3
13868
- }
13869
- }
13870
13885
  }
13871
13886
  }
13872
13887
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onekeyfe/hd-transport",
3
- "version": "1.2.10-alpha.1",
3
+ "version": "1.2.10-alpha.2",
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",
@@ -12,8 +12,10 @@
12
12
  },
13
13
  "scripts": {
14
14
  "dev": "rimraf dist && rollup -c ../../build/rollup.config.js -w",
15
- "build": "rimraf dist && rollup -c ../../build/rollup.config.js",
15
+ "build": "node scripts/protobuf-static.js --check && rimraf dist && rollup -c ../../build/rollup.config.js",
16
16
  "test": "jest",
17
+ "test:static-csp": "node --disallow-code-generation-from-strings scripts/protobuf-static-csp.js",
18
+ "check:static": "node scripts/protobuf-static.js --check",
17
19
  "lint": "eslint .",
18
20
  "lint:fix": "eslint . --fix",
19
21
  "update:protobuf": "./scripts/protobuf-build.sh"
@@ -26,7 +28,7 @@
26
28
  "dependencies": {
27
29
  "bytebuffer": "^5.0.1",
28
30
  "long": "^4.0.0",
29
- "protobufjs": "^6.11.2"
31
+ "protobufjs": "6.11.6"
30
32
  },
31
- "gitHead": "df785eba29f144fac2cfdcbd808426e0a2c259b2"
33
+ "gitHead": "2bcc72a43379abb005405f18f914330f3f2dbfd7"
32
34
  }
@@ -7,6 +7,11 @@ PACKAGE_ROOT="$PARENT_PATH/.."
7
7
  REPO_ROOT="$PARENT_PATH/../../.."
8
8
  CORE_MESSAGES_DIR="$REPO_ROOT/packages/core/src/data/messages"
9
9
 
10
+ if [[ "${1:-}" == "--static-only" ]]; then
11
+ node "$PARENT_PATH/protobuf-static.js"
12
+ exit 0
13
+ fi
14
+
10
15
  SRC="../../submodules/firmware/common/protob"
11
16
  DIST="."
12
17
  LANG="typescript"
@@ -166,3 +171,5 @@ yarn --cwd "$PACKAGE_ROOT" prettier --write "$CORE_MESSAGES_DIR/messages.json"
166
171
  yarn --cwd "$PACKAGE_ROOT" prettier --write "$PACKAGE_ROOT/messages-protocol-v2.json"
167
172
  yarn --cwd "$PACKAGE_ROOT" prettier --write "$CORE_MESSAGES_DIR/messages-protocol-v2.json"
168
173
  yarn --cwd "$PACKAGE_ROOT" prettier --write "$PACKAGE_ROOT/src/types/messages.ts"
174
+
175
+ node "$PARENT_PATH/protobuf-static.js"
@@ -0,0 +1,103 @@
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
+ });
@@ -0,0 +1,85 @@
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`);
@@ -1,10 +1,14 @@
1
1
  import ByteBuffer from 'bytebuffer';
2
- import { Reader, Type } from 'protobufjs/light';
2
+ import { Reader } from 'protobufjs/minimal';
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 { createMessageFromName, createMessageFromType } from '../serialization/protobuf/messages';
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';
10
14
  import {
@@ -14,6 +18,8 @@ import {
14
18
  isAckFrame,
15
19
  } from './v2';
16
20
 
21
+ import legacyProtocolInfo from '../serialization/protobuf/legacy-protocol-info.json';
22
+
17
23
  import type { Root } from 'protobufjs/light';
18
24
 
19
25
  export const PROTOCOL_V2_SYS_MESSAGE_THRESHOLD = 60000;
@@ -30,27 +36,8 @@ type ProtocolV2FrameOptions = {
30
36
  seq?: number;
31
37
  };
32
38
 
33
- const LEGACY_PROTOCOL_V2_PROTOCOL_INFO = Type.fromJSON('LegacyProtocolInfo', {
34
- fields: {
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
- });
39
+ const LEGACY_PROTOCOL_V2_PROTOCOL_INFO =
40
+ parseConfigure(legacyProtocolInfo).lookupType('LegacyProtocolInfo');
54
41
 
55
42
  const decodeLegacyProtocolV2ProtocolInfo = (pbPayload: Uint8Array) => {
56
43
  const byteBuffer = ByteBuffer.wrap(Buffer.from(pbPayload) as unknown as ArrayBuffer);
@@ -0,0 +1,3 @@
1
+ // @generated by scripts/protobuf-static.js. DO NOT EDIT.
2
+ import type { Root } from 'protobufjs/light';
3
+ export declare function installStaticCodecs(root: Root): boolean;