@zondax/ledger-cosmos-js 2.1.8 → 3.0.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.
Files changed (54) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +39 -7
  3. package/dist/commandsV1.d.ts +4 -0
  4. package/dist/commandsV1.js +129 -0
  5. package/dist/commandsV1.js.map +1 -0
  6. package/dist/commandsV2.d.ts +4 -0
  7. package/dist/commandsV2.js +104 -0
  8. package/dist/commandsV2.js.map +1 -0
  9. package/dist/common.d.ts +39 -0
  10. package/dist/common.js +143 -132
  11. package/dist/common.js.map +1 -0
  12. package/dist/cosmosApp.d.ts +19 -0
  13. package/dist/cosmosApp.js +377 -0
  14. package/dist/cosmosApp.js.map +1 -0
  15. package/dist/index.d.ts +16 -0
  16. package/dist/index.js +17 -608
  17. package/dist/index.js.map +1 -0
  18. package/package.json +64 -50
  19. package/yarn.lock +6661 -0
  20. package/.babelrc +0 -13
  21. package/.circleci/config.yml +0 -62
  22. package/.editorconfig +0 -14
  23. package/.eslintrc.json +0 -31
  24. package/.idea/codeStyles/Project.xml +0 -57
  25. package/.idea/codeStyles/codeStyleConfig.xml +0 -5
  26. package/.idea/inspectionProfiles/Project_Default.xml +0 -6
  27. package/.idea/jsLibraryMappings.xml +0 -6
  28. package/.idea/ledger-cosmos-js.iml +0 -12
  29. package/.idea/modules.xml +0 -8
  30. package/.idea/vcs.xml +0 -6
  31. package/.idea/workspace.xml +0 -57
  32. package/.prettierrc.json +0 -4
  33. package/certs/.gitkeep +0 -0
  34. package/certs/openssl.cnf +0 -12
  35. package/dist/helperV1.js +0 -127
  36. package/dist/helperV2.js +0 -97
  37. package/docs/example.png +0 -0
  38. package/jest.config.integration.js +0 -9
  39. package/jest.config.js +0 -32
  40. package/jest.config.unit.js +0 -9
  41. package/public/favicon.ico +0 -0
  42. package/public/index.html +0 -17
  43. package/src/common.js +0 -114
  44. package/src/helperV1.js +0 -73
  45. package/src/helperV2.js +0 -44
  46. package/src/index.js +0 -336
  47. package/tests/basic.ispec.js +0 -390
  48. package/tests/basic.spec.js +0 -8
  49. package/tests/jest.js +0 -2
  50. package/vue.config.js +0 -11
  51. package/vue_example/App.vue +0 -29
  52. package/vue_example/assets/logo.png +0 -0
  53. package/vue_example/components/LedgerExample.vue +0 -252
  54. package/vue_example/main.js +0 -9
package/src/common.js DELETED
@@ -1,114 +0,0 @@
1
- export const CLA = 0x55;
2
- export const CHUNK_SIZE = 250;
3
- export const APP_KEY = "CSM";
4
-
5
- export const INS = {
6
- GET_VERSION: 0x00,
7
- INS_PUBLIC_KEY_SECP256K1: 0x01, // Obsolete
8
- SIGN_SECP256K1: 0x02,
9
- GET_ADDR_SECP256K1: 0x04,
10
- };
11
-
12
- export const PAYLOAD_TYPE = {
13
- INIT: 0x00,
14
- ADD: 0x01,
15
- LAST: 0x02,
16
- };
17
-
18
- export const P1_VALUES = {
19
- ONLY_RETRIEVE: 0x00,
20
- SHOW_ADDRESS_IN_DEVICE: 0x01,
21
- };
22
-
23
- export const ERROR_CODE = {
24
- NoError: 0x9000,
25
- };
26
-
27
- const ERROR_DESCRIPTION = {
28
- 1: "U2F: Unknown",
29
- 2: "U2F: Bad request",
30
- 3: "U2F: Configuration unsupported",
31
- 4: "U2F: Device Ineligible",
32
- 5: "U2F: Timeout",
33
- 14: "Timeout",
34
- 0x9000: "No errors",
35
- 0x9001: "Device is busy",
36
- 0x6802: "Error deriving keys",
37
- 0x6400: "Execution Error",
38
- 0x6700: "Wrong Length",
39
- 0x6982: "Empty Buffer",
40
- 0x6983: "Output buffer too small",
41
- 0x6984: "Data is invalid",
42
- 0x6985: "Conditions not satisfied",
43
- 0x6986: "Transaction rejected",
44
- 0x6a80: "Bad key handle",
45
- 0x6b00: "Invalid P1/P2",
46
- 0x6d00: "Instruction not supported",
47
- 0x6e00: "App does not seem to be open",
48
- 0x6f00: "Unknown error",
49
- 0x6f01: "Sign/verify error",
50
- };
51
-
52
- export function errorCodeToString(statusCode) {
53
- if (statusCode in ERROR_DESCRIPTION) return ERROR_DESCRIPTION[statusCode];
54
- return `Unknown Status Code: ${statusCode}`;
55
- }
56
-
57
- function isDict(v) {
58
- return typeof v === "object" && v !== null && !(v instanceof Array) && !(v instanceof Date);
59
- }
60
-
61
- export function processErrorResponse(response) {
62
- if (response) {
63
- if (isDict(response)) {
64
- if (Object.prototype.hasOwnProperty.call(response, "statusCode")) {
65
- return {
66
- return_code: response.statusCode,
67
- error_message: errorCodeToString(response.statusCode),
68
- };
69
- }
70
-
71
- if (
72
- Object.prototype.hasOwnProperty.call(response, "return_code") &&
73
- Object.prototype.hasOwnProperty.call(response, "error_message")
74
- ) {
75
- return response;
76
- }
77
- }
78
- return {
79
- return_code: 0xffff,
80
- error_message: response.toString(),
81
- };
82
- }
83
-
84
- return {
85
- return_code: 0xffff,
86
- error_message: response.toString(),
87
- };
88
- }
89
-
90
- export async function getVersion(transport) {
91
- return transport.send(CLA, INS.GET_VERSION, 0, 0).then(response => {
92
- const errorCodeData = response.slice(-2);
93
- const returnCode = errorCodeData[0] * 256 + errorCodeData[1];
94
-
95
- let targetId = 0;
96
- if (response.length >= 9) {
97
- /* eslint-disable no-bitwise */
98
- targetId = (response[5] << 24) + (response[6] << 16) + (response[7] << 8) + (response[8] << 0);
99
- /* eslint-enable no-bitwise */
100
- }
101
-
102
- return {
103
- return_code: returnCode,
104
- error_message: errorCodeToString(returnCode),
105
- // ///
106
- test_mode: response[0] !== 0,
107
- major: response[1],
108
- minor: response[2],
109
- patch: response[3],
110
- device_locked: response[4] === 1,
111
- target_id: targetId.toString(16),
112
- };
113
- }, processErrorResponse);
114
- }
package/src/helperV1.js DELETED
@@ -1,73 +0,0 @@
1
- import { CLA, ERROR_CODE, errorCodeToString, INS, processErrorResponse } from "./common";
2
-
3
- export function serializePathv1(path) {
4
- if (path == null || path.length < 3) {
5
- throw new Error("Invalid path.");
6
- }
7
- if (path.length > 10) {
8
- throw new Error("Invalid path. Length should be <= 10");
9
- }
10
- const buf = Buffer.alloc(1 + 4 * path.length);
11
- buf.writeUInt8(path.length, 0);
12
- for (let i = 0; i < path.length; i += 1) {
13
- let v = path[i];
14
- if (i < 3) {
15
- // eslint-disable-next-line no-bitwise
16
- v |= 0x80000000; // Harden
17
- }
18
- buf.writeInt32LE(v, 1 + i * 4);
19
- }
20
- return buf;
21
- }
22
-
23
- export async function signSendChunkv1(app, chunkIdx, chunkNum, chunk) {
24
- return app.transport
25
- .send(CLA, INS.SIGN_SECP256K1, chunkIdx, chunkNum, chunk, [ERROR_CODE.NoError, 0x6984, 0x6a80])
26
- .then(response => {
27
- const errorCodeData = response.slice(-2);
28
- const returnCode = errorCodeData[0] * 256 + errorCodeData[1];
29
- let errorMessage = errorCodeToString(returnCode);
30
-
31
- if (returnCode === 0x6a80 || returnCode === 0x6984) {
32
- errorMessage = `${errorMessage} : ${response.slice(0, response.length - 2).toString("ascii")}`;
33
- }
34
-
35
- let signature = null;
36
- if (response.length > 2) {
37
- signature = response.slice(0, response.length - 2);
38
- }
39
-
40
- return {
41
- signature,
42
- return_code: returnCode,
43
- error_message: errorMessage,
44
- };
45
- }, processErrorResponse);
46
- }
47
-
48
- function compressPublicKey(publicKey) {
49
- if (publicKey.length !== 65) {
50
- throw new Error("decompressed public key length should be 65 bytes");
51
- }
52
- const y = publicKey.slice(33, 65);
53
- // eslint-disable-next-line no-bitwise
54
- const z = Buffer.from([2 + (y[y.length - 1] & 1)]);
55
- return Buffer.concat([z, publicKey.slice(1, 33)]);
56
- }
57
-
58
- export async function publicKeyv1(app, data) {
59
- return app.transport
60
- .send(CLA, INS.INS_PUBLIC_KEY_SECP256K1, 0, 0, data, [ERROR_CODE.NoError])
61
- .then(response => {
62
- const errorCodeData = response.slice(-2);
63
- const returnCode = errorCodeData[0] * 256 + errorCodeData[1];
64
- const pk = Buffer.from(response.slice(0, 65));
65
-
66
- return {
67
- pk,
68
- compressed_pk: compressPublicKey(pk),
69
- return_code: returnCode,
70
- error_message: errorCodeToString(returnCode),
71
- };
72
- }, processErrorResponse);
73
- }
package/src/helperV2.js DELETED
@@ -1,44 +0,0 @@
1
- import { signSendChunkv1 } from "./helperV1";
2
- import { CLA, ERROR_CODE, errorCodeToString, INS, PAYLOAD_TYPE, processErrorResponse } from "./common";
3
-
4
- export function serializePathv2(path) {
5
- if (!path || path.length !== 5) {
6
- throw new Error("Invalid path.");
7
- }
8
-
9
- const buf = Buffer.alloc(20);
10
- buf.writeUInt32LE(0x80000000 + path[0], 0);
11
- buf.writeUInt32LE(0x80000000 + path[1], 4);
12
- buf.writeUInt32LE(0x80000000 + path[2], 8);
13
- buf.writeUInt32LE(path[3], 12);
14
- buf.writeUInt32LE(path[4], 16);
15
-
16
- return buf;
17
- }
18
-
19
- export async function signSendChunkv2(app, chunkIdx, chunkNum, chunk) {
20
- let payloadType = PAYLOAD_TYPE.ADD;
21
- if (chunkIdx === 1) {
22
- payloadType = PAYLOAD_TYPE.INIT;
23
- }
24
- if (chunkIdx === chunkNum) {
25
- payloadType = PAYLOAD_TYPE.LAST;
26
- }
27
-
28
- return signSendChunkv1(app, payloadType, 0, chunk);
29
- }
30
-
31
- export async function publicKeyv2(app, data) {
32
- return app.transport.send(CLA, INS.GET_ADDR_SECP256K1, 0, 0, data, [ERROR_CODE.NoError]).then(response => {
33
- const errorCodeData = response.slice(-2);
34
- const returnCode = errorCodeData[0] * 256 + errorCodeData[1];
35
- const compressedPk = Buffer.from(response.slice(0, 33));
36
-
37
- return {
38
- pk: "OBSOLETE PROPERTY",
39
- compressed_pk: compressedPk,
40
- return_code: returnCode,
41
- error_message: errorCodeToString(returnCode),
42
- };
43
- }, processErrorResponse);
44
- }
package/src/index.js DELETED
@@ -1,336 +0,0 @@
1
- /** ******************************************************************************
2
- * (c) 2019 ZondaX GmbH
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
-
18
- import crypto from "crypto";
19
- import Ripemd160 from "ripemd160";
20
- import bech32 from "bech32";
21
- import { publicKeyv1, serializePathv1, signSendChunkv1 } from "./helperV1";
22
- import { publicKeyv2, serializePathv2, signSendChunkv2 } from "./helperV2";
23
- import {
24
- APP_KEY,
25
- CHUNK_SIZE,
26
- CLA,
27
- INS,
28
- errorCodeToString,
29
- getVersion,
30
- processErrorResponse,
31
- ERROR_CODE,
32
- P1_VALUES,
33
- } from "./common";
34
-
35
- export default class CosmosApp {
36
- constructor(transport, scrambleKey = APP_KEY) {
37
- if (!transport) {
38
- throw new Error("Transport has not been defined");
39
- }
40
-
41
- this.transport = transport;
42
- transport.decorateAppAPIMethods(
43
- this,
44
- ["getVersion", "sign", "getAddressAndPubKey", "appInfo", "deviceInfo", "getBech32FromPK"],
45
- scrambleKey,
46
- );
47
- }
48
-
49
- static serializeHRP(hrp) {
50
- if (hrp == null || hrp.length < 3 || hrp.length > 83) {
51
- throw new Error("Invalid HRP");
52
- }
53
- const buf = Buffer.alloc(1 + hrp.length);
54
- buf.writeUInt8(hrp.length, 0);
55
- buf.write(hrp, 1);
56
- return buf;
57
- }
58
-
59
- static getBech32FromPK(hrp, pk) {
60
- if (pk.length !== 33) {
61
- throw new Error("expected compressed public key [31 bytes]");
62
- }
63
- const hashSha256 = crypto
64
- .createHash("sha256")
65
- .update(pk)
66
- .digest();
67
- const hashRip = new Ripemd160().update(hashSha256).digest();
68
- return bech32.encode(hrp, bech32.toWords(hashRip));
69
- }
70
-
71
- async serializePath(path) {
72
- this.versionResponse = await getVersion(this.transport);
73
-
74
- if (this.versionResponse.return_code !== ERROR_CODE.NoError) {
75
- throw this.versionResponse;
76
- }
77
-
78
- switch (this.versionResponse.major) {
79
- case 1:
80
- return serializePathv1(path);
81
- case 2:
82
- return serializePathv2(path);
83
- default:
84
- return {
85
- return_code: 0x6400,
86
- error_message: "App Version is not supported",
87
- };
88
- }
89
- }
90
-
91
- async signGetChunks(path, message) {
92
- const serializedPath = await this.serializePath(path);
93
-
94
- const chunks = [];
95
- chunks.push(serializedPath);
96
- const buffer = Buffer.from(message);
97
-
98
- for (let i = 0; i < buffer.length; i += CHUNK_SIZE) {
99
- let end = i + CHUNK_SIZE;
100
- if (i > buffer.length) {
101
- end = buffer.length;
102
- }
103
- chunks.push(buffer.slice(i, end));
104
- }
105
-
106
- return chunks;
107
- }
108
-
109
- async getVersion() {
110
- try {
111
- this.versionResponse = await getVersion(this.transport);
112
- return this.versionResponse;
113
- } catch (e) {
114
- return processErrorResponse(e);
115
- }
116
- }
117
-
118
- async appInfo() {
119
- return this.transport.send(0xb0, 0x01, 0, 0).then(response => {
120
- const errorCodeData = response.slice(-2);
121
- const returnCode = errorCodeData[0] * 256 + errorCodeData[1];
122
-
123
- const result = {};
124
-
125
- let appName = "err";
126
- let appVersion = "err";
127
- let flagLen = 0;
128
- let flagsValue = 0;
129
-
130
- if (response[0] !== 1) {
131
- // Ledger responds with format ID 1. There is no spec for any format != 1
132
- result.error_message = "response format ID not recognized";
133
- result.return_code = 0x9001;
134
- } else {
135
- const appNameLen = response[1];
136
- appName = response.slice(2, 2 + appNameLen).toString("ascii");
137
- let idx = 2 + appNameLen;
138
- const appVersionLen = response[idx];
139
- idx += 1;
140
- appVersion = response.slice(idx, idx + appVersionLen).toString("ascii");
141
- idx += appVersionLen;
142
- const appFlagsLen = response[idx];
143
- idx += 1;
144
- flagLen = appFlagsLen;
145
- flagsValue = response[idx];
146
- }
147
-
148
- return {
149
- return_code: returnCode,
150
- error_message: errorCodeToString(returnCode),
151
- // //
152
- appName,
153
- appVersion,
154
- flagLen,
155
- flagsValue,
156
- // eslint-disable-next-line no-bitwise
157
- flag_recovery: (flagsValue & 1) !== 0,
158
- // eslint-disable-next-line no-bitwise
159
- flag_signed_mcu_code: (flagsValue & 2) !== 0,
160
- // eslint-disable-next-line no-bitwise
161
- flag_onboarded: (flagsValue & 4) !== 0,
162
- // eslint-disable-next-line no-bitwise
163
- flag_pin_validated: (flagsValue & 128) !== 0,
164
- };
165
- }, processErrorResponse);
166
- }
167
-
168
- async deviceInfo() {
169
- return this.transport
170
- .send(0xe0, 0x01, 0, 0, Buffer.from([]), [ERROR_CODE.NoError, 0x6e00])
171
- .then(response => {
172
- const errorCodeData = response.slice(-2);
173
- const returnCode = errorCodeData[0] * 256 + errorCodeData[1];
174
-
175
- if (returnCode === 0x6e00) {
176
- return {
177
- return_code: returnCode,
178
- error_message: "This command is only available in the Dashboard",
179
- };
180
- }
181
-
182
- const targetId = response.slice(0, 4).toString("hex");
183
-
184
- let pos = 4;
185
- const secureElementVersionLen = response[pos];
186
- pos += 1;
187
- const seVersion = response.slice(pos, pos + secureElementVersionLen).toString();
188
- pos += secureElementVersionLen;
189
-
190
- const flagsLen = response[pos];
191
- pos += 1;
192
- const flag = response.slice(pos, pos + flagsLen).toString("hex");
193
- pos += flagsLen;
194
-
195
- const mcuVersionLen = response[pos];
196
- pos += 1;
197
- // Patch issue in mcu version
198
- let tmp = response.slice(pos, pos + mcuVersionLen);
199
- if (tmp[mcuVersionLen - 1] === 0) {
200
- tmp = response.slice(pos, pos + mcuVersionLen - 1);
201
- }
202
- const mcuVersion = tmp.toString();
203
-
204
- return {
205
- return_code: returnCode,
206
- error_message: errorCodeToString(returnCode),
207
- // //
208
- targetId,
209
- seVersion,
210
- flag,
211
- mcuVersion,
212
- };
213
- }, processErrorResponse);
214
- }
215
-
216
- async publicKey(path) {
217
- try {
218
- const serializedPath = await this.serializePath(path);
219
-
220
- switch (this.versionResponse.major) {
221
- case 1:
222
- return publicKeyv1(this, serializedPath);
223
- case 2: {
224
- const data = Buffer.concat([CosmosApp.serializeHRP("cosmos"), serializedPath]);
225
- return publicKeyv2(this, data);
226
- }
227
- default:
228
- return {
229
- return_code: 0x6400,
230
- error_message: "App Version is not supported",
231
- };
232
- }
233
- } catch (e) {
234
- return processErrorResponse(e);
235
- }
236
- }
237
-
238
- async getAddressAndPubKey(path, hrp) {
239
- try {
240
- return this.serializePath(path)
241
- .then(serializedPath => {
242
- const data = Buffer.concat([CosmosApp.serializeHRP(hrp), serializedPath]);
243
- return this.transport
244
- .send(CLA, INS.GET_ADDR_SECP256K1, P1_VALUES.ONLY_RETRIEVE, 0, data, [ERROR_CODE.NoError])
245
- .then(response => {
246
- const errorCodeData = response.slice(-2);
247
- const returnCode = errorCodeData[0] * 256 + errorCodeData[1];
248
-
249
- const compressedPk = Buffer.from(response.slice(0, 33));
250
- const bech32Address = Buffer.from(response.slice(33, -2)).toString();
251
-
252
- return {
253
- bech32_address: bech32Address,
254
- compressed_pk: compressedPk,
255
- return_code: returnCode,
256
- error_message: errorCodeToString(returnCode),
257
- };
258
- }, processErrorResponse);
259
- })
260
- .catch(err => processErrorResponse(err));
261
- } catch (e) {
262
- return processErrorResponse(e);
263
- }
264
- }
265
-
266
- async showAddressAndPubKey(path, hrp) {
267
- try {
268
- return this.serializePath(path)
269
- .then(serializedPath => {
270
- const data = Buffer.concat([CosmosApp.serializeHRP(hrp), serializedPath]);
271
- return this.transport
272
- .send(CLA, INS.GET_ADDR_SECP256K1, P1_VALUES.SHOW_ADDRESS_IN_DEVICE, 0, data, [
273
- ERROR_CODE.NoError,
274
- ])
275
- .then(response => {
276
- const errorCodeData = response.slice(-2);
277
- const returnCode = errorCodeData[0] * 256 + errorCodeData[1];
278
-
279
- const compressedPk = Buffer.from(response.slice(0, 33));
280
- const bech32Address = Buffer.from(response.slice(33, -2)).toString();
281
-
282
- return {
283
- bech32_address: bech32Address,
284
- compressed_pk: compressedPk,
285
- return_code: returnCode,
286
- error_message: errorCodeToString(returnCode),
287
- };
288
- }, processErrorResponse);
289
- })
290
- .catch(err => processErrorResponse(err));
291
- } catch (e) {
292
- return processErrorResponse(e);
293
- }
294
- }
295
-
296
- async signSendChunk(chunkIdx, chunkNum, chunk) {
297
- switch (this.versionResponse.major) {
298
- case 1:
299
- return signSendChunkv1(this, chunkIdx, chunkNum, chunk);
300
- case 2:
301
- return signSendChunkv2(this, chunkIdx, chunkNum, chunk);
302
- default:
303
- return {
304
- return_code: 0x6400,
305
- error_message: "App Version is not supported",
306
- };
307
- }
308
- }
309
-
310
- async sign(path, message) {
311
- return this.signGetChunks(path, message).then(chunks => {
312
- return this.signSendChunk(1, chunks.length, chunks[0], [ERROR_CODE.NoError]).then(async response => {
313
- let result = {
314
- return_code: response.return_code,
315
- error_message: response.error_message,
316
- signature: null,
317
- };
318
-
319
- for (let i = 1; i < chunks.length; i += 1) {
320
- // eslint-disable-next-line no-await-in-loop
321
- result = await this.signSendChunk(1 + i, chunks.length, chunks[i]);
322
- if (result.return_code !== ERROR_CODE.NoError) {
323
- break;
324
- }
325
- }
326
-
327
- return {
328
- return_code: result.return_code,
329
- error_message: result.error_message,
330
- // ///
331
- signature: result.signature,
332
- };
333
- }, processErrorResponse);
334
- }, processErrorResponse);
335
- }
336
- }