@ledgerhq/hw-app-trx 6.27.1 → 6.27.2-yolo.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,5 @@
1
+ @ledgerhq/hw-app-trx:build: cache hit, replaying output a34120b0deed53f6
2
+ @ledgerhq/hw-app-trx:build:
3
+ @ledgerhq/hw-app-trx:build: > @ledgerhq/hw-app-trx@6.27.1 build /home/runner/work/ledger-live/ledger-live/libs/ledgerjs/packages/hw-app-trx
4
+ @ledgerhq/hw-app-trx:build: > tsc && tsc -m ES6 --outDir lib-es
5
+ @ledgerhq/hw-app-trx:build:
package/CHANGELOG.md ADDED
@@ -0,0 +1,9 @@
1
+ # @ledgerhq/hw-app-trx
2
+
3
+ ## 6.27.2-yolo.0
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [[`203b927b4`](https://github.com/LedgerHQ/ledger-live/commit/203b927b4e5bca3402c85a88c536d519adb18c5f)]:
8
+ - @ledgerhq/errors@6.10.1-yolo.0
9
+ - @ledgerhq/hw-transport@6.27.2-yolo.0
package/jest.config.ts ADDED
@@ -0,0 +1,6 @@
1
+ import baseConfig from "../../jest.config";
2
+
3
+ export default {
4
+ ...baseConfig,
5
+ rootDir: __dirname,
6
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ledgerhq/hw-app-trx",
3
- "version": "6.27.1",
3
+ "version": "6.27.2-yolo.0",
4
4
  "description": "Ledger Hardware Wallet Tron Application API",
5
5
  "keywords": [
6
6
  "Ledger",
@@ -27,14 +27,21 @@
27
27
  "types": "lib/Trx.d.ts",
28
28
  "license": "Apache-2.0",
29
29
  "dependencies": {
30
- "@ledgerhq/errors": "^6.10.0",
31
- "@ledgerhq/hw-transport": "^6.27.1"
30
+ "@ledgerhq/errors": "^6.10.1-yolo.0",
31
+ "@ledgerhq/hw-transport": "^6.27.2-yolo.0"
32
32
  },
33
- "scripts": {
34
- "clean": "bash ../../script/clean.sh",
35
- "build": "bash ../../script/build.sh",
36
- "watch": "bash ../../script/watch.sh",
37
- "doc": "bash ../../script/doc.sh"
33
+ "devDependencies": {
34
+ "@ledgerhq/hw-transport-mocker": "^6.27.2-yolo.0"
38
35
  },
39
- "gitHead": "9639f96a970e1f46e9e39d0c2c361ce2289923be"
40
- }
36
+ "gitHead": "dd0dea64b58e5a9125c8a422dcffd29e5ef6abec",
37
+ "scripts": {
38
+ "clean": "rimraf lib lib-es",
39
+ "build": "tsc && tsc -m ES6 --outDir lib-es",
40
+ "prewatch": "pnpm build",
41
+ "watch": "tsc --watch",
42
+ "doc": "documentation readme src/** --section=API --pe ts --re ts --re d.ts",
43
+ "lint": "eslint ./src --no-error-on-unmatched-pattern --ext .ts,.tsx",
44
+ "lint:fix": "pnpm lint --fix",
45
+ "test": "jest"
46
+ }
47
+ }
package/LICENSE DELETED
@@ -1,202 +0,0 @@
1
- Apache License
2
- Version 2.0, January 2004
3
- http://www.apache.org/licenses/
4
-
5
- TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
-
7
- 1. Definitions.
8
-
9
- "License" shall mean the terms and conditions for use, reproduction,
10
- and distribution as defined by Sections 1 through 9 of this document.
11
-
12
- "Licensor" shall mean the copyright owner or entity authorized by
13
- the copyright owner that is granting the License.
14
-
15
- "Legal Entity" shall mean the union of the acting entity and all
16
- other entities that control, are controlled by, or are under common
17
- control with that entity. For the purposes of this definition,
18
- "control" means (i) the power, direct or indirect, to cause the
19
- direction or management of such entity, whether by contract or
20
- otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
- outstanding shares, or (iii) beneficial ownership of such entity.
22
-
23
- "You" (or "Your") shall mean an individual or Legal Entity
24
- exercising permissions granted by this License.
25
-
26
- "Source" form shall mean the preferred form for making modifications,
27
- including but not limited to software source code, documentation
28
- source, and configuration files.
29
-
30
- "Object" form shall mean any form resulting from mechanical
31
- transformation or translation of a Source form, including but
32
- not limited to compiled object code, generated documentation,
33
- and conversions to other media types.
34
-
35
- "Work" shall mean the work of authorship, whether in Source or
36
- Object form, made available under the License, as indicated by a
37
- copyright notice that is included in or attached to the work
38
- (an example is provided in the Appendix below).
39
-
40
- "Derivative Works" shall mean any work, whether in Source or Object
41
- form, that is based on (or derived from) the Work and for which the
42
- editorial revisions, annotations, elaborations, or other modifications
43
- represent, as a whole, an original work of authorship. For the purposes
44
- of this License, Derivative Works shall not include works that remain
45
- separable from, or merely link (or bind by name) to the interfaces of,
46
- the Work and Derivative Works thereof.
47
-
48
- "Contribution" shall mean any work of authorship, including
49
- the original version of the Work and any modifications or additions
50
- to that Work or Derivative Works thereof, that is intentionally
51
- submitted to Licensor for inclusion in the Work by the copyright owner
52
- or by an individual or Legal Entity authorized to submit on behalf of
53
- the copyright owner. For the purposes of this definition, "submitted"
54
- means any form of electronic, verbal, or written communication sent
55
- to the Licensor or its representatives, including but not limited to
56
- communication on electronic mailing lists, source code control systems,
57
- and issue tracking systems that are managed by, or on behalf of, the
58
- Licensor for the purpose of discussing and improving the Work, but
59
- excluding communication that is conspicuously marked or otherwise
60
- designated in writing by the copyright owner as "Not a Contribution."
61
-
62
- "Contributor" shall mean Licensor and any individual or Legal Entity
63
- on behalf of whom a Contribution has been received by Licensor and
64
- subsequently incorporated within the Work.
65
-
66
- 2. Grant of Copyright License. Subject to the terms and conditions of
67
- this License, each Contributor hereby grants to You a perpetual,
68
- worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
- copyright license to reproduce, prepare Derivative Works of,
70
- publicly display, publicly perform, sublicense, and distribute the
71
- Work and such Derivative Works in Source or Object form.
72
-
73
- 3. Grant of Patent License. Subject to the terms and conditions of
74
- this License, each Contributor hereby grants to You a perpetual,
75
- worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
- (except as stated in this section) patent license to make, have made,
77
- use, offer to sell, sell, import, and otherwise transfer the Work,
78
- where such license applies only to those patent claims licensable
79
- by such Contributor that are necessarily infringed by their
80
- Contribution(s) alone or by combination of their Contribution(s)
81
- with the Work to which such Contribution(s) was submitted. If You
82
- institute patent litigation against any entity (including a
83
- cross-claim or counterclaim in a lawsuit) alleging that the Work
84
- or a Contribution incorporated within the Work constitutes direct
85
- or contributory patent infringement, then any patent licenses
86
- granted to You under this License for that Work shall terminate
87
- as of the date such litigation is filed.
88
-
89
- 4. Redistribution. You may reproduce and distribute copies of the
90
- Work or Derivative Works thereof in any medium, with or without
91
- modifications, and in Source or Object form, provided that You
92
- meet the following conditions:
93
-
94
- (a) You must give any other recipients of the Work or
95
- Derivative Works a copy of this License; and
96
-
97
- (b) You must cause any modified files to carry prominent notices
98
- stating that You changed the files; and
99
-
100
- (c) You must retain, in the Source form of any Derivative Works
101
- that You distribute, all copyright, patent, trademark, and
102
- attribution notices from the Source form of the Work,
103
- excluding those notices that do not pertain to any part of
104
- the Derivative Works; and
105
-
106
- (d) If the Work includes a "NOTICE" text file as part of its
107
- distribution, then any Derivative Works that You distribute must
108
- include a readable copy of the attribution notices contained
109
- within such NOTICE file, excluding those notices that do not
110
- pertain to any part of the Derivative Works, in at least one
111
- of the following places: within a NOTICE text file distributed
112
- as part of the Derivative Works; within the Source form or
113
- documentation, if provided along with the Derivative Works; or,
114
- within a display generated by the Derivative Works, if and
115
- wherever such third-party notices normally appear. The contents
116
- of the NOTICE file are for informational purposes only and
117
- do not modify the License. You may add Your own attribution
118
- notices within Derivative Works that You distribute, alongside
119
- or as an addendum to the NOTICE text from the Work, provided
120
- that such additional attribution notices cannot be construed
121
- as modifying the License.
122
-
123
- You may add Your own copyright statement to Your modifications and
124
- may provide additional or different license terms and conditions
125
- for use, reproduction, or distribution of Your modifications, or
126
- for any such Derivative Works as a whole, provided Your use,
127
- reproduction, and distribution of the Work otherwise complies with
128
- the conditions stated in this License.
129
-
130
- 5. Submission of Contributions. Unless You explicitly state otherwise,
131
- any Contribution intentionally submitted for inclusion in the Work
132
- by You to the Licensor shall be under the terms and conditions of
133
- this License, without any additional terms or conditions.
134
- Notwithstanding the above, nothing herein shall supersede or modify
135
- the terms of any separate license agreement you may have executed
136
- with Licensor regarding such Contributions.
137
-
138
- 6. Trademarks. This License does not grant permission to use the trade
139
- names, trademarks, service marks, or product names of the Licensor,
140
- except as required for reasonable and customary use in describing the
141
- origin of the Work and reproducing the content of the NOTICE file.
142
-
143
- 7. Disclaimer of Warranty. Unless required by applicable law or
144
- agreed to in writing, Licensor provides the Work (and each
145
- Contributor provides its Contributions) on an "AS IS" BASIS,
146
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
- implied, including, without limitation, any warranties or conditions
148
- of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
- PARTICULAR PURPOSE. You are solely responsible for determining the
150
- appropriateness of using or redistributing the Work and assume any
151
- risks associated with Your exercise of permissions under this License.
152
-
153
- 8. Limitation of Liability. In no event and under no legal theory,
154
- whether in tort (including negligence), contract, or otherwise,
155
- unless required by applicable law (such as deliberate and grossly
156
- negligent acts) or agreed to in writing, shall any Contributor be
157
- liable to You for damages, including any direct, indirect, special,
158
- incidental, or consequential damages of any character arising as a
159
- result of this License or out of the use or inability to use the
160
- Work (including but not limited to damages for loss of goodwill,
161
- work stoppage, computer failure or malfunction, or any and all
162
- other commercial damages or losses), even if such Contributor
163
- has been advised of the possibility of such damages.
164
-
165
- 9. Accepting Warranty or Additional Liability. While redistributing
166
- the Work or Derivative Works thereof, You may choose to offer,
167
- and charge a fee for, acceptance of support, warranty, indemnity,
168
- or other liability obligations and/or rights consistent with this
169
- License. However, in accepting such obligations, You may act only
170
- on Your own behalf and on Your sole responsibility, not on behalf
171
- of any other Contributor, and only if You agree to indemnify,
172
- defend, and hold each Contributor harmless for any liability
173
- incurred by, or claims asserted against, such Contributor by reason
174
- of your accepting any such warranty or additional liability.
175
-
176
- END OF TERMS AND CONDITIONS
177
-
178
- APPENDIX: How to apply the Apache License to your work.
179
-
180
- To apply the Apache License to your work, attach the following
181
- boilerplate notice, with the fields enclosed by brackets "{}"
182
- replaced with your own identifying information. (Don't include
183
- the brackets!) The text should be enclosed in the appropriate
184
- comment syntax for the file format. We also recommend that a
185
- file or class name and description of purpose be included on the
186
- same "printed page" as the copyright notice for easier
187
- identification within third-party archives.
188
-
189
- Copyright {yyyy} {name of copyright owner}
190
-
191
- Licensed under the Apache License, Version 2.0 (the "License");
192
- you may not use this file except in compliance with the License.
193
- You may obtain a copy of the License at
194
-
195
- http://www.apache.org/licenses/LICENSE-2.0
196
-
197
- Unless required by applicable law or agreed to in writing, software
198
- distributed under the License is distributed on an "AS IS" BASIS,
199
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
- See the License for the specific language governing permissions and
201
- limitations under the License.
202
-
@@ -1,362 +0,0 @@
1
- /********************************************************************************
2
- * Ledger Node JS API
3
- * (c) 2016-2017 Ledger
4
- *
5
- * Licensed under the Apache License, Version 2.0 (the "License");
6
- * you may not use this file except in compliance with the License.
7
- * You may obtain a copy of the License at
8
- *
9
- * http://www.apache.org/licenses/LICENSE-2.0
10
- *
11
- * Unless required by applicable law or agreed to in writing, software
12
- * distributed under the License is distributed on an "AS IS" BASIS,
13
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
- * See the License for the specific language governing permissions and
15
- * limitations under the License.
16
- ********************************************************************************/
17
- //@flow
18
-
19
- // FIXME drop:
20
- import { splitPath, foreach, decodeVarint } from "./utils";
21
- //import { StatusCodes, TransportStatusError } from "@ledgerhq/errors";
22
- import type Transport from "@ledgerhq/hw-transport";
23
-
24
- const remapTransactionRelatedErrors = (e) => {
25
- if (e && e.statusCode === 0x6a80) {
26
- // TODO:
27
- }
28
- return e;
29
- };
30
-
31
- const PATH_SIZE = 4;
32
- const PATHS_LENGTH_SIZE = 1;
33
- const CLA = 0xe0;
34
- const ADDRESS = 0x02;
35
- const SIGN = 0x04;
36
- const SIGN_HASH = 0x05;
37
- const SIGN_MESSAGE = 0x08;
38
- const ECDH_SECRET = 0x0a;
39
- const VERSION = 0x06;
40
- const CHUNK_SIZE = 250;
41
-
42
- /**
43
- * Tron API
44
- *
45
- * @example
46
- * import Trx from "@ledgerhq/hw-app-trx";
47
- * const trx = new Trx(transport)
48
- */
49
- export default class Trx {
50
- transport: Transport<*>;
51
-
52
- constructor(transport: Transport<*>, scrambleKey: string = "TRX") {
53
- this.transport = transport;
54
- transport.decorateAppAPIMethods(
55
- this,
56
- [
57
- "getAddress",
58
- "getECDHPairKey",
59
- "signTransaction",
60
- "signTransactionHash",
61
- "signPersonalMessage",
62
- "getAppConfiguration",
63
- ],
64
- scrambleKey
65
- );
66
- }
67
-
68
- /**
69
- * get Tron address for a given BIP 32 path.
70
- * @param path a path in BIP 32 format
71
- * @option boolDisplay optionally enable or not the display
72
- * @return an object with a publicKey and address
73
- * @example
74
- * const address = await tron.getAddress("44'/195'/0'/0/0").then(o => o.address)
75
- */
76
- getAddress(
77
- path: string,
78
- boolDisplay?: boolean
79
- ): Promise<{
80
- publicKey: string,
81
- address: string,
82
- }> {
83
- let paths = splitPath(path);
84
- let buffer = Buffer.alloc(PATHS_LENGTH_SIZE + paths.length * PATH_SIZE);
85
- buffer[0] = paths.length;
86
- paths.forEach((element, index) => {
87
- buffer.writeUInt32BE(element, 1 + 4 * index);
88
- });
89
- return this.transport
90
- .send(CLA, ADDRESS, boolDisplay ? 0x01 : 0x00, 0x00, buffer)
91
- .then((response) => {
92
- let result = {};
93
- let publicKeyLength = response[0];
94
- let addressLength = response[1 + publicKeyLength];
95
- result.publicKey = response
96
- .slice(1, 1 + publicKeyLength)
97
- .toString("hex");
98
- result.address = response
99
- .slice(
100
- 1 + publicKeyLength + 1,
101
- 1 + publicKeyLength + 1 + addressLength
102
- )
103
- .toString("ascii");
104
- return result;
105
- });
106
- }
107
-
108
- getNextLength(tx: Buffer) {
109
- const field = decodeVarint(tx, 0);
110
- const data = decodeVarint(tx, field.pos);
111
- if ((field.value & 0x07) === 0) return data.pos;
112
- return data.value + data.pos;
113
- }
114
-
115
- /**
116
- * sign a Tron transaction with a given BIP 32 path and Token Names
117
- *
118
- * @param path a path in BIP 32 format
119
- * @param rawTxHex a raw transaction hex string
120
- * @param tokenSignatures Tokens Signatures array
121
- * @option version pack message based on ledger firmware version
122
- * @option smartContract boolean hack to set limit buffer on ledger device
123
- * @return a signature as hex string
124
- * @example
125
- * const signature = await tron.signTransaction("44'/195'/0'/0/0", "0a02f5942208704dda506d59dceb40f0f4978f802e5a69080112650a2d747970652e676f6f676c65617069732e636f6d2f70726f746f636f6c2e5472616e73666572436f6e747261637412340a1541978dbd103cfe59c35e753d09dd44ae1ae64621c7121541e2ae49db6a70b9b4757d2137a43b69b24a445780188ef8b5ba0470cbb5948f802e", [], 105);
126
- */
127
- signTransaction(
128
- path: string,
129
- rawTxHex: string,
130
- tokenSignatures: string[]
131
- ): Promise<string> {
132
- const paths = splitPath(path);
133
- let rawTx = Buffer.from(rawTxHex, "hex");
134
- const toSend = [];
135
-
136
- let data = Buffer.alloc(PATHS_LENGTH_SIZE + paths.length * PATH_SIZE);
137
- // write path for first chuck only
138
- data[0] = paths.length;
139
- paths.forEach((element, index) => {
140
- data.writeUInt32BE(element, 1 + 4 * index);
141
- });
142
-
143
- while (rawTx.length > 0) {
144
- // get next message field
145
- const newpos = this.getNextLength(rawTx);
146
- if (newpos > CHUNK_SIZE) throw new Error("Too many bytes to encode.");
147
- if (data.length + newpos > CHUNK_SIZE) {
148
- toSend.push(data);
149
- data = Buffer.alloc(0);
150
- continue;
151
- }
152
- // append data
153
- data = Buffer.concat([data, rawTx.slice(0, newpos)]);
154
- rawTx = rawTx.slice(newpos, rawTx.length);
155
- }
156
- toSend.push(data);
157
-
158
- const startBytes = [];
159
- let response;
160
-
161
- const tokenPos = toSend.length;
162
- if (tokenSignatures !== undefined) {
163
- for (let i = 0; i < tokenSignatures.length; i += 1) {
164
- const buffer = Buffer.from(tokenSignatures[i], "hex");
165
- toSend.push(buffer);
166
- }
167
- }
168
-
169
- // get startBytes
170
- if (toSend.length === 1) {
171
- startBytes.push(0x10);
172
- } else {
173
- startBytes.push(0x00);
174
- for (let i = 1; i < toSend.length - 1; i += 1) {
175
- if (i >= tokenPos) {
176
- startBytes.push(0xa0 | 0x00 | (i - tokenPos)); // eslint-disable-line no-bitwise
177
- } else {
178
- startBytes.push(0x80);
179
- }
180
- }
181
- if (tokenSignatures !== undefined && tokenSignatures.length) {
182
- startBytes.push(0xa0 | 0x08 | (tokenSignatures.length - 1)); // eslint-disable-line no-bitwise
183
- } else {
184
- startBytes.push(0x90);
185
- }
186
- }
187
-
188
- return foreach(toSend, (data, i) => {
189
- return this.transport
190
- .send(CLA, SIGN, startBytes[i], 0x00, data)
191
- .then((apduResponse) => {
192
- response = apduResponse;
193
- });
194
- }).then(
195
- () => {
196
- return response.slice(0, 65).toString("hex");
197
- },
198
- (e) => {
199
- throw remapTransactionRelatedErrors(e);
200
- }
201
- );
202
- }
203
-
204
- /**
205
- * sign a Tron transaction hash with a given BIP 32 path
206
- *
207
- * @param path a path in BIP 32 format
208
- * @param rawTxHex a raw transaction hex string
209
- * @return a signature as hex string
210
- * @example
211
- * const signature = await tron.signTransactionHash("44'/195'/0'/0/0", "25b18a55f86afb10e7aca38d0073d04c80397c6636069193953fdefaea0b8369");
212
- */
213
- signTransactionHash(path: string, rawTxHashHex: string): Promise<string> {
214
- const paths = splitPath(path);
215
- let data = Buffer.alloc(PATHS_LENGTH_SIZE + paths.length * PATH_SIZE);
216
- data[0] = paths.length;
217
- paths.forEach((element, index) => {
218
- data.writeUInt32BE(element, 1 + 4 * index);
219
- });
220
- data = Buffer.concat([data, Buffer.from(rawTxHashHex, "hex")]);
221
-
222
- return this.transport
223
- .send(CLA, SIGN_HASH, 0x00, 0x00, data)
224
- .then((response) => {
225
- return response.slice(0, 65).toString("hex");
226
- });
227
- }
228
-
229
- /**
230
- * get the version of the Tron app installed on the hardware device
231
- *
232
- * @return an object with a version
233
- * @example
234
- * const result = await tron.getAppConfiguration();
235
- * {
236
- * "version": "0.1.5",
237
- * "versionN": "105".
238
- * "allowData": false,
239
- * "allowContract": false,
240
- * "truncateAddress": false,
241
- * "signByHash": false
242
- * }
243
- */
244
- getAppConfiguration(): Promise<{
245
- allowContract: Boolean,
246
- truncateAddress: Boolean,
247
- allowData: Boolean,
248
- signByHash: Boolean,
249
- version: string,
250
- versionN: number,
251
- }> {
252
- return this.transport.send(CLA, VERSION, 0x00, 0x00).then((response) => {
253
- // eslint-disable-next-line no-bitwise
254
- let signByHash = (response[0] & (1 << 3)) > 0;
255
- // eslint-disable-next-line no-bitwise
256
- let truncateAddress = (response[0] & (1 << 2)) > 0;
257
- // eslint-disable-next-line no-bitwise
258
- let allowContract = (response[0] & (1 << 1)) > 0;
259
- // eslint-disable-next-line no-bitwise
260
- let allowData = (response[0] & (1 << 0)) > 0;
261
- if (response[1] === 0 && response[2] === 1 && response[3] < 2) {
262
- allowData = true;
263
- allowContract = false;
264
- }
265
- if (response[1] === 0 && response[2] === 1 && response[3] < 5) {
266
- truncateAddress = false;
267
- }
268
- const result = {
269
- version: `${response[1]}.${response[2]}.${response[3]}`,
270
- versionN: response[1] * 10000 + response[2] * 100 + response[3],
271
- allowData,
272
- allowContract,
273
- truncateAddress,
274
- signByHash,
275
- };
276
- return result;
277
- });
278
- }
279
-
280
- /**
281
- * sign a Tron Message with a given BIP 32 path
282
- *
283
- * @param path a path in BIP 32 format
284
- * @param message hex string to sign
285
- * @return a signature as hex string
286
- * @example
287
- * const signature = await tron.signPersonalMessage("44'/195'/0'/0/0", "43727970746f436861696e2d54726f6e5352204c6564676572205472616e73616374696f6e73205465737473");
288
- */
289
- signPersonalMessage(path: string, messageHex: string): Promise<string> {
290
- let paths = splitPath(path);
291
- const message = Buffer.from(messageHex, "hex");
292
- let offset = 0;
293
- let toSend = [];
294
-
295
- const size = message.length.toString(16);
296
- const sizePack = "00000000".substr(size.length) + size;
297
- const packed = Buffer.concat([Buffer.from(sizePack, "hex"), message]);
298
-
299
- while (offset < packed.length) {
300
- // Use small buffer to be compatible with old and new protocol
301
- let maxChunkSize =
302
- offset === 0 ? CHUNK_SIZE - 1 - paths.length * 4 : CHUNK_SIZE;
303
- let chunkSize =
304
- offset + maxChunkSize > packed.length
305
- ? packed.length - offset
306
- : maxChunkSize;
307
- let buffer = Buffer.alloc(
308
- offset === 0 ? 1 + paths.length * 4 + chunkSize : chunkSize
309
- );
310
- if (offset === 0) {
311
- buffer[0] = paths.length;
312
- paths.forEach((element, index) => {
313
- buffer.writeUInt32BE(element, 1 + 4 * index);
314
- });
315
- packed.copy(buffer, 1 + 4 * paths.length, offset, offset + chunkSize);
316
- } else {
317
- packed.copy(buffer, 0, offset, offset + chunkSize);
318
- }
319
- toSend.push(buffer);
320
- offset += chunkSize;
321
- }
322
- let response;
323
- return foreach(toSend, (data, i) => {
324
- return this.transport
325
- .send(CLA, SIGN_MESSAGE, i === 0 ? 0x00 : 0x80, 0x00, data)
326
- .then((apduResponse) => {
327
- response = apduResponse;
328
- });
329
- }).then(() => {
330
- return response.slice(0, 65).toString("hex");
331
- });
332
- }
333
-
334
- /**
335
- * get Tron address for a given BIP 32 path.
336
- * @param path a path in BIP 32 format
337
- * @param publicKey address public key to generate pair key
338
- * @return shared key hex string,
339
- * @example
340
- * const signature = await tron.getECDHPairKey("44'/195'/0'/0/0", "04ff21f8e64d3a3c0198edfbb7afdc79be959432e92e2f8a1984bb436a414b8edcec0345aad0c1bf7da04fd036dd7f9f617e30669224283d950fab9dd84831dc83");
341
- */
342
- getECDHPairKey(
343
- path: string,
344
- publicKey: string
345
- ): Promise<{
346
- publicKey: string,
347
- address: string,
348
- }> {
349
- const paths = splitPath(path);
350
- const data = Buffer.from(publicKey, "hex");
351
- const buffer = Buffer.alloc(1 + paths.length * 4 + data.length);
352
- buffer[0] = paths.length;
353
- paths.forEach((element, index) => {
354
- buffer.writeUInt32BE(element, 1 + 4 * index);
355
- });
356
- data.copy(buffer, 1 + 4 * paths.length, 0, data.length);
357
-
358
- return this.transport
359
- .send(CLA, ECDH_SECRET, 0x00, 0x01, buffer)
360
- .then((response) => response.slice(0, 65).toString("hex"));
361
- }
362
- }
@@ -1,126 +0,0 @@
1
- /********************************************************************************
2
- * Ledger Node JS API
3
- * (c) 2016-2017 Ledger
4
- *
5
- * Licensed under the Apache License, Version 2.0 (the "License");
6
- * you may not use this file except in compliance with the License.
7
- * You may obtain a copy of the License at
8
- *
9
- * http://www.apache.org/licenses/LICENSE-2.0
10
- *
11
- * Unless required by applicable law or agreed to in writing, software
12
- * distributed under the License is distributed on an "AS IS" BASIS,
13
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
- * See the License for the specific language governing permissions and
15
- * limitations under the License.
16
- ********************************************************************************/
17
- //@flow
18
-
19
- type Defer<T> = {
20
- promise: Promise<T>,
21
- resolve: (T) => void,
22
- reject: (any) => void,
23
- };
24
-
25
- export function defer<T>(): Defer<T> {
26
- let resolve, reject;
27
- let promise = new Promise(function (success, failure) {
28
- resolve = success;
29
- reject = failure;
30
- });
31
- if (!resolve || !reject) throw "defer() error"; // this never happens and is just to make flow happy
32
- return { promise, resolve, reject };
33
- }
34
-
35
- // TODO use bip32-path library
36
- export function splitPath(path: string): number[] {
37
- let result = [];
38
- let components = path.split("/");
39
- components.forEach((element) => {
40
- let number = parseInt(element, 10);
41
- if (isNaN(number)) {
42
- return; // FIXME shouldn't it throws instead?
43
- }
44
- if (element.length > 1 && element[element.length - 1] === "'") {
45
- number += 0x80000000;
46
- }
47
- result.push(number);
48
- });
49
- return result;
50
- }
51
-
52
- // TODO use async await
53
-
54
- export function eachSeries<A>(arr: A[], fun: (A) => Promise<*>): Promise<*> {
55
- return arr.reduce((p, e) => p.then(() => fun(e)), Promise.resolve());
56
- }
57
-
58
- export function foreach<T, A>(
59
- arr: T[],
60
- callback: (T, number) => Promise<A>
61
- ): Promise<A[]> {
62
- function iterate(index, array, result) {
63
- if (index >= array.length) {
64
- return result;
65
- } else
66
- return callback(array[index], index).then(function (res) {
67
- result.push(res);
68
- return iterate(index + 1, array, result);
69
- });
70
- }
71
- return Promise.resolve().then(() => iterate(0, arr, []));
72
- }
73
-
74
- export function doIf(
75
- condition: boolean,
76
- callback: () => any | Promise<any>
77
- ): Promise<void> {
78
- return Promise.resolve().then(() => {
79
- if (condition) {
80
- return callback();
81
- }
82
- });
83
- }
84
-
85
- export function asyncWhile<T>(
86
- predicate: () => boolean,
87
- callback: () => Promise<T>
88
- ): Promise<Array<T>> {
89
- function iterate(result) {
90
- if (!predicate()) {
91
- return result;
92
- } else {
93
- return callback().then((res) => {
94
- result.push(res);
95
- return iterate(result);
96
- });
97
- }
98
- }
99
- return Promise.resolve([]).then(iterate);
100
- }
101
-
102
- interface DecodeResult {
103
- value: number;
104
- pos: number;
105
- }
106
-
107
- export function decodeVarint(stream: Buffer, index: number): DecodeResult {
108
- let result = 0;
109
- let shift = 0;
110
- let pos = index;
111
- // eslint-disable-next-line no-constant-condition
112
- while (shift < 64) {
113
- const b = stream[pos];
114
- result |= (b & 0x7f) << shift;
115
- pos += 1;
116
- if (!(b & 0x80)) {
117
- result &= 0xffffffff;
118
- return {
119
- value: result,
120
- pos,
121
- };
122
- }
123
- shift += 7;
124
- }
125
- throw new Error("Too many bytes when decoding varint.");
126
- }