@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.
- package/LICENSE +1 -1
- package/README.md +39 -7
- package/dist/commandsV1.d.ts +4 -0
- package/dist/commandsV1.js +129 -0
- package/dist/commandsV1.js.map +1 -0
- package/dist/commandsV2.d.ts +4 -0
- package/dist/commandsV2.js +104 -0
- package/dist/commandsV2.js.map +1 -0
- package/dist/common.d.ts +39 -0
- package/dist/common.js +143 -132
- package/dist/common.js.map +1 -0
- package/dist/cosmosApp.d.ts +19 -0
- package/dist/cosmosApp.js +377 -0
- package/dist/cosmosApp.js.map +1 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +17 -608
- package/dist/index.js.map +1 -0
- package/package.json +64 -50
- package/yarn.lock +6661 -0
- package/.babelrc +0 -13
- package/.circleci/config.yml +0 -62
- package/.editorconfig +0 -14
- package/.eslintrc.json +0 -31
- package/.idea/codeStyles/Project.xml +0 -57
- package/.idea/codeStyles/codeStyleConfig.xml +0 -5
- package/.idea/inspectionProfiles/Project_Default.xml +0 -6
- package/.idea/jsLibraryMappings.xml +0 -6
- package/.idea/ledger-cosmos-js.iml +0 -12
- package/.idea/modules.xml +0 -8
- package/.idea/vcs.xml +0 -6
- package/.idea/workspace.xml +0 -57
- package/.prettierrc.json +0 -4
- package/certs/.gitkeep +0 -0
- package/certs/openssl.cnf +0 -12
- package/dist/helperV1.js +0 -127
- package/dist/helperV2.js +0 -97
- package/docs/example.png +0 -0
- package/jest.config.integration.js +0 -9
- package/jest.config.js +0 -32
- package/jest.config.unit.js +0 -9
- package/public/favicon.ico +0 -0
- package/public/index.html +0 -17
- package/src/common.js +0 -114
- package/src/helperV1.js +0 -73
- package/src/helperV2.js +0 -44
- package/src/index.js +0 -336
- package/tests/basic.ispec.js +0 -390
- package/tests/basic.spec.js +0 -8
- package/tests/jest.js +0 -2
- package/vue.config.js +0 -11
- package/vue_example/App.vue +0 -29
- package/vue_example/assets/logo.png +0 -0
- package/vue_example/components/LedgerExample.vue +0 -252
- package/vue_example/main.js +0 -9
package/LICENSE
CHANGED
|
@@ -186,7 +186,7 @@
|
|
|
186
186
|
same "printed page" as the copyright notice for easier
|
|
187
187
|
identification within third-party archives.
|
|
188
188
|
|
|
189
|
-
Copyright
|
|
189
|
+
Copyright 2018 - 2022 ZondaX AG
|
|
190
190
|
|
|
191
191
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
192
|
you may not use this file except in compliance with the License.
|
package/README.md
CHANGED
|
@@ -1,15 +1,47 @@
|
|
|
1
|
-
# ledger-cosmos-js
|
|
1
|
+
# @zondax/ledger-cosmos-js
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+

|
|
2
5
|
|
|
3
6
|
[](https://opensource.org/licenses/Apache-2.0)
|
|
4
|
-
[](https://github.com/Zondax/ledger-cosmos-js/actions?query=workflow%3AMain)
|
|
8
|
+
[](https://badge.fury.io/js/%40zondax%2Fledger-cosmos-js)
|
|
9
|
+
|
|
6
10
|
|
|
7
|
-
This package provides a basic client library to communicate with a Tendermint/Cosmos App running in a Ledger Nano S/X
|
|
11
|
+
This package provides a basic client library to communicate with a Tendermint/Cosmos App running in a Ledger Nano S/S+/X devices
|
|
8
12
|
|
|
9
13
|
We recommend using the npmjs package in order to receive updates/fixes.
|
|
10
14
|
|
|
11
|
-
This repo also includes a simple Vue example for U2F and WebUSB.
|
|
12
15
|
|
|
13
|
-
|
|
16
|
+
# Available commands
|
|
17
|
+
|
|
18
|
+
| Operation | Response | Command |
|
|
19
|
+
| ---------- | ---------------- | ----------------------- |
|
|
20
|
+
| getVersion | app version | --------------- |
|
|
21
|
+
| publicKey | pubkey | path (legacy command) |
|
|
22
|
+
| getAddressAndPubKey | pubkey + address | path + ( showInDevice ) |
|
|
23
|
+
| showAddressAndPubKey | signed message | path |
|
|
24
|
+
| appInfo | name, version, flags, etc | --------------- |
|
|
25
|
+
| deviceInfo | fw and mcu version, id, etc | Only available in dashboard |
|
|
26
|
+
| sign | signed message | path + message |
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
getAddress command requires that you set the derivation path (account, change, index) and has an option parameter to display the address on the device. By default, it will retrieve the information without user confirmation.
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
# Testing with real devices
|
|
33
|
+
|
|
34
|
+
It is possible to test this package with a real Ledger Nano device. To accomplish that, you will need to follow these steps:
|
|
35
|
+
|
|
36
|
+
- Install the application in the Ledger device
|
|
37
|
+
- Install the dependencies from this project
|
|
38
|
+
- Run tests
|
|
39
|
+
|
|
40
|
+
```shell script
|
|
41
|
+
yarn install
|
|
42
|
+
yarn test:integration
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
# Who we are?
|
|
14
46
|
|
|
15
|
-
|
|
47
|
+
We are Zondax, a company pioneering blockchain services. If you want to know more about us, please visit us at [zondax.ch](https://zondax.ch)
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
export declare function serializePathv1(path: number[]): Buffer;
|
|
3
|
+
export declare function signSendChunkv1(app: any, chunkIdx: number, chunkNum: number, chunk: Buffer): Promise<any>;
|
|
4
|
+
export declare function publicKeyv1(app: any, data: Buffer): Promise<any>;
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (_) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
exports.__esModule = true;
|
|
39
|
+
exports.publicKeyv1 = exports.signSendChunkv1 = exports.serializePathv1 = void 0;
|
|
40
|
+
/** ******************************************************************************
|
|
41
|
+
* (c) 2018 - 2022 Zondax AG
|
|
42
|
+
*
|
|
43
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
44
|
+
* you may not use this file except in compliance with the License.
|
|
45
|
+
* You may obtain a copy of the License at
|
|
46
|
+
*
|
|
47
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
48
|
+
*
|
|
49
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
50
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
51
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
52
|
+
* See the License for the specific language governing permissions and
|
|
53
|
+
* limitations under the License.
|
|
54
|
+
******************************************************************************* */
|
|
55
|
+
var common_1 = require("./common");
|
|
56
|
+
function serializePathv1(path) {
|
|
57
|
+
if (path == null || path.length < 3) {
|
|
58
|
+
throw new Error('Invalid path.');
|
|
59
|
+
}
|
|
60
|
+
if (path.length > 10) {
|
|
61
|
+
throw new Error('Invalid path. Length should be <= 10');
|
|
62
|
+
}
|
|
63
|
+
var buf = Buffer.alloc(1 + 4 * path.length);
|
|
64
|
+
buf.writeUInt8(path.length, 0);
|
|
65
|
+
for (var i = 0; i < path.length; i += 1) {
|
|
66
|
+
var v = path[i];
|
|
67
|
+
if (i < 3) {
|
|
68
|
+
// eslint-disable-next-line no-bitwise
|
|
69
|
+
v |= 0x80000000; // Harden
|
|
70
|
+
}
|
|
71
|
+
buf.writeInt32LE(v, 1 + i * 4);
|
|
72
|
+
}
|
|
73
|
+
return buf;
|
|
74
|
+
}
|
|
75
|
+
exports.serializePathv1 = serializePathv1;
|
|
76
|
+
function signSendChunkv1(app, chunkIdx, chunkNum, chunk) {
|
|
77
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
78
|
+
return __generator(this, function (_a) {
|
|
79
|
+
return [2 /*return*/, app.transport
|
|
80
|
+
.send(common_1.CLA, common_1.INS.SIGN_SECP256K1, chunkIdx, chunkNum, chunk, [common_1.ERROR_CODE.NoError, 0x6984, 0x6a80])
|
|
81
|
+
.then(function (response) {
|
|
82
|
+
var errorCodeData = response.slice(-2);
|
|
83
|
+
var returnCode = errorCodeData[0] * 256 + errorCodeData[1];
|
|
84
|
+
var errorMessage = (0, common_1.errorCodeToString)(returnCode);
|
|
85
|
+
if (returnCode === 0x6a80 || returnCode === 0x6984) {
|
|
86
|
+
errorMessage = "".concat(errorMessage, " : ").concat(response.slice(0, response.length - 2).toString('ascii'));
|
|
87
|
+
}
|
|
88
|
+
var signature = null;
|
|
89
|
+
if (response.length > 2) {
|
|
90
|
+
signature = response.slice(0, response.length - 2);
|
|
91
|
+
}
|
|
92
|
+
return {
|
|
93
|
+
signature: signature,
|
|
94
|
+
return_code: returnCode,
|
|
95
|
+
error_message: errorMessage
|
|
96
|
+
};
|
|
97
|
+
}, common_1.processErrorResponse)];
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
exports.signSendChunkv1 = signSendChunkv1;
|
|
102
|
+
function compressPublicKey(publicKey) {
|
|
103
|
+
if (publicKey.length !== 65) {
|
|
104
|
+
throw new Error('decompressed public key length should be 65 bytes');
|
|
105
|
+
}
|
|
106
|
+
var y = publicKey.slice(33, 65);
|
|
107
|
+
// eslint-disable-next-line no-bitwise
|
|
108
|
+
var z = Buffer.from([2 + (y[y.length - 1] & 1)]);
|
|
109
|
+
return Buffer.concat([z, publicKey.slice(1, 33)]);
|
|
110
|
+
}
|
|
111
|
+
function publicKeyv1(app, data) {
|
|
112
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
113
|
+
return __generator(this, function (_a) {
|
|
114
|
+
return [2 /*return*/, app.transport.send(common_1.CLA, common_1.INS.INS_PUBLIC_KEY_SECP256K1, 0, 0, data, [common_1.ERROR_CODE.NoError]).then(function (response) {
|
|
115
|
+
var errorCodeData = response.slice(-2);
|
|
116
|
+
var returnCode = errorCodeData[0] * 256 + errorCodeData[1];
|
|
117
|
+
var pk = Buffer.from(response.slice(0, 65));
|
|
118
|
+
return {
|
|
119
|
+
pk: pk,
|
|
120
|
+
compressed_pk: compressPublicKey(pk),
|
|
121
|
+
return_code: returnCode,
|
|
122
|
+
error_message: (0, common_1.errorCodeToString)(returnCode)
|
|
123
|
+
};
|
|
124
|
+
}, common_1.processErrorResponse)];
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
exports.publicKeyv1 = publicKeyv1;
|
|
129
|
+
//# sourceMappingURL=commandsV1.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"commandsV1.js","sourceRoot":"","sources":["../src/commandsV1.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;;;;;;mFAcmF;AACnF,mCAAwF;AAExF,SAAgB,eAAe,CAAC,IAAc;IAC5C,IAAI,IAAI,IAAI,IAAI,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC,EAAE;QACnC,MAAM,IAAI,KAAK,CAAC,eAAe,CAAC,CAAA;KACjC;IACD,IAAI,IAAI,CAAC,MAAM,GAAG,EAAE,EAAE;QACpB,MAAM,IAAI,KAAK,CAAC,sCAAsC,CAAC,CAAA;KACxD;IACD,IAAM,GAAG,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,MAAM,CAAC,CAAA;IAC7C,GAAG,CAAC,UAAU,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC,CAAA;IAC9B,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,IAAI,CAAC,EAAE;QACvC,IAAI,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,CAAA;QACf,IAAI,CAAC,GAAG,CAAC,EAAE;YACT,sC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E,IAAA,0BAAiB,EAAC,UAAU,CAAC;qBAC7C,CAAA;gBACH,CAAC,EAAE,6BAAoB,CAAC,EAAA;;;CACzB;AAbD,kCAaC","sourcesContent":["/** ******************************************************************************\n * (c) 2018 - 2022 Zondax AG\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n ******************************************************************************* */\nimport { CLA, ERROR_CODE, errorCodeToString, INS, processErrorResponse } from './common'\n\nexport function serializePathv1(path: number[]) {\n if (path == null || path.length < 3) {\n throw new Error('Invalid path.')\n }\n if (path.length > 10) {\n throw new Error('Invalid path. Length should be <= 10')\n }\n const buf = Buffer.alloc(1 + 4 * path.length)\n buf.writeUInt8(path.length, 0)\n for (let i = 0; i < path.length; i += 1) {\n let v = path[i]\n if (i < 3) {\n // eslint-disable-next-line no-bitwise\n v |= 0x80000000 // Harden\n }\n buf.writeInt32LE(v, 1 + i * 4)\n }\n return buf\n}\n\nexport async function signSendChunkv1(app: any, chunkIdx: number, chunkNum: number, chunk: Buffer) {\n return app.transport\n .send(CLA, INS.SIGN_SECP256K1, chunkIdx, chunkNum, chunk, [ERROR_CODE.NoError, 0x6984, 0x6a80])\n .then((response: any) => {\n const errorCodeData = response.slice(-2)\n const returnCode = errorCodeData[0] * 256 + errorCodeData[1]\n let errorMessage = errorCodeToString(returnCode)\n\n if (returnCode === 0x6a80 || returnCode === 0x6984) {\n errorMessage = `${errorMessage} : ${response.slice(0, response.length - 2).toString('ascii')}`\n }\n\n let signature = null\n if (response.length > 2) {\n signature = response.slice(0, response.length - 2)\n }\n\n return {\n signature,\n return_code: returnCode,\n error_message: errorMessage,\n }\n }, processErrorResponse)\n}\n\nfunction compressPublicKey(publicKey: Buffer) {\n if (publicKey.length !== 65) {\n throw new Error('decompressed public key length should be 65 bytes')\n }\n const y = publicKey.slice(33, 65)\n // eslint-disable-next-line no-bitwise\n const z = Buffer.from([2 + (y[y.length - 1] & 1)])\n return Buffer.concat([z, publicKey.slice(1, 33)])\n}\n\nexport async function publicKeyv1(app: any, data: Buffer) {\n return app.transport.send(CLA, INS.INS_PUBLIC_KEY_SECP256K1, 0, 0, data, [ERROR_CODE.NoError]).then((response: any) => {\n const errorCodeData = response.slice(-2)\n const returnCode = errorCodeData[0] * 256 + errorCodeData[1]\n const pk = Buffer.from(response.slice(0, 65))\n\n return {\n pk,\n compressed_pk: compressPublicKey(pk),\n return_code: returnCode,\n error_message: errorCodeToString(returnCode),\n }\n }, processErrorResponse)\n}\n"]}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
export declare function serializePathv2(path: number[]): Buffer;
|
|
3
|
+
export declare function signSendChunkv2(app: any, chunkIdx: number, chunkNum: number, chunk: Buffer): Promise<any>;
|
|
4
|
+
export declare function publicKeyv2(app: any, data: Buffer): Promise<any>;
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (_) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
exports.__esModule = true;
|
|
39
|
+
exports.publicKeyv2 = exports.signSendChunkv2 = exports.serializePathv2 = void 0;
|
|
40
|
+
/** ******************************************************************************
|
|
41
|
+
* (c) 2018 - 2022 Zondax AG
|
|
42
|
+
*
|
|
43
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
44
|
+
* you may not use this file except in compliance with the License.
|
|
45
|
+
* You may obtain a copy of the License at
|
|
46
|
+
*
|
|
47
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
48
|
+
*
|
|
49
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
50
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
51
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
52
|
+
* See the License for the specific language governing permissions and
|
|
53
|
+
* limitations under the License.
|
|
54
|
+
******************************************************************************* */
|
|
55
|
+
var commandsV1_1 = require("./commandsV1");
|
|
56
|
+
var common_1 = require("./common");
|
|
57
|
+
function serializePathv2(path) {
|
|
58
|
+
if (!path || path.length !== 5) {
|
|
59
|
+
throw new Error('Invalid path.');
|
|
60
|
+
}
|
|
61
|
+
var buf = Buffer.alloc(20);
|
|
62
|
+
buf.writeUInt32LE(0x80000000 + path[0], 0);
|
|
63
|
+
buf.writeUInt32LE(0x80000000 + path[1], 4);
|
|
64
|
+
buf.writeUInt32LE(0x80000000 + path[2], 8);
|
|
65
|
+
buf.writeUInt32LE(path[3], 12);
|
|
66
|
+
buf.writeUInt32LE(path[4], 16);
|
|
67
|
+
return buf;
|
|
68
|
+
}
|
|
69
|
+
exports.serializePathv2 = serializePathv2;
|
|
70
|
+
function signSendChunkv2(app, chunkIdx, chunkNum, chunk) {
|
|
71
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
72
|
+
var payloadType;
|
|
73
|
+
return __generator(this, function (_a) {
|
|
74
|
+
payloadType = common_1.PAYLOAD_TYPE.ADD;
|
|
75
|
+
if (chunkIdx === 1) {
|
|
76
|
+
payloadType = common_1.PAYLOAD_TYPE.INIT;
|
|
77
|
+
}
|
|
78
|
+
if (chunkIdx === chunkNum) {
|
|
79
|
+
payloadType = common_1.PAYLOAD_TYPE.LAST;
|
|
80
|
+
}
|
|
81
|
+
return [2 /*return*/, (0, commandsV1_1.signSendChunkv1)(app, payloadType, 0, chunk)];
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
exports.signSendChunkv2 = signSendChunkv2;
|
|
86
|
+
function publicKeyv2(app, data) {
|
|
87
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
88
|
+
return __generator(this, function (_a) {
|
|
89
|
+
return [2 /*return*/, app.transport.send(common_1.CLA, common_1.INS.GET_ADDR_SECP256K1, 0, 0, data, [common_1.ERROR_CODE.NoError]).then(function (response) {
|
|
90
|
+
var errorCodeData = response.slice(-2);
|
|
91
|
+
var returnCode = errorCodeData[0] * 256 + errorCodeData[1];
|
|
92
|
+
var compressedPk = Buffer.from(response.slice(0, 33));
|
|
93
|
+
return {
|
|
94
|
+
pk: 'OBSOLETE PROPERTY',
|
|
95
|
+
compressed_pk: compressedPk,
|
|
96
|
+
return_code: returnCode,
|
|
97
|
+
error_message: (0, common_1.errorCodeToString)(returnCode)
|
|
98
|
+
};
|
|
99
|
+
}, common_1.processErrorResponse)];
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
exports.publicKeyv2 = publicKeyv2;
|
|
104
|
+
//# sourceMappingURL=commandsV2.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"commandsV2.js","sourceRoot":"","sources":["../src/commandsV2.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;;;;;;mFAcmF;AACnF,2CAA8C;AAC9C,mCAAsG;AAEtG,SAAgB,eAAe,CAAC,IAAc;IAC5C,IAAI,CAAC,IAAI,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE;QAC9B,MAAM,IAAI,KAAK,CAAC,eAAe,CAAC,CAAA;KACjC;IAED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C,CAAC,CAAC,CAAC,CAAA;oBACxC,IAAM,UAAU,GAAG,aAAa,CAAC,CAAC,CAAC,GAAG,GAAG,GAAG,aAAa,CAAC,CAAC,CAAC,CAAA;oBAC5D,IAAM,YAAY,GAAG,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAA;oBAEvD,OAAO;wBACL,EAAE,EAAE,mBAAmB;wBACvB,aAAa,EAAE,YAAY;wBAC3B,WAAW,EAAE,UAAU;wBACvB,aAAa,EAAE,IAAA,0BAAiB,EAAC,UAAU,CAAC;qBAC7C,CAAA;gBACH,CAAC,EAAE,6BAAoB,CAAC,EAAA;;;CACzB;AAbD,kCAaC","sourcesContent":["/** ******************************************************************************\n * (c) 2018 - 2022 Zondax AG\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n ******************************************************************************* */\nimport { signSendChunkv1 } from './commandsV1'\nimport { CLA, ERROR_CODE, errorCodeToString, INS, PAYLOAD_TYPE, processErrorResponse } from './common'\n\nexport function serializePathv2(path: number[]) {\n if (!path || path.length !== 5) {\n throw new Error('Invalid path.')\n }\n\n const buf = Buffer.alloc(20)\n buf.writeUInt32LE(0x80000000 + path[0], 0)\n buf.writeUInt32LE(0x80000000 + path[1], 4)\n buf.writeUInt32LE(0x80000000 + path[2], 8)\n buf.writeUInt32LE(path[3], 12)\n buf.writeUInt32LE(path[4], 16)\n\n return buf\n}\n\nexport async function signSendChunkv2(app: any, chunkIdx: number, chunkNum: number, chunk: Buffer) {\n let payloadType = PAYLOAD_TYPE.ADD\n if (chunkIdx === 1) {\n payloadType = PAYLOAD_TYPE.INIT\n }\n if (chunkIdx === chunkNum) {\n payloadType = PAYLOAD_TYPE.LAST\n }\n\n return signSendChunkv1(app, payloadType, 0, chunk)\n}\n\nexport async function publicKeyv2(app: any, data: Buffer) {\n return app.transport.send(CLA, INS.GET_ADDR_SECP256K1, 0, 0, data, [ERROR_CODE.NoError]).then((response: any) => {\n const errorCodeData = response.slice(-2)\n const returnCode = errorCodeData[0] * 256 + errorCodeData[1]\n const compressedPk = Buffer.from(response.slice(0, 33))\n\n return {\n pk: 'OBSOLETE PROPERTY',\n compressed_pk: compressedPk,\n return_code: returnCode,\n error_message: errorCodeToString(returnCode),\n }\n }, processErrorResponse)\n}\n"]}
|
package/dist/common.d.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/** ******************************************************************************
|
|
2
|
+
* (c) 2018 - 2022 Zondax AG
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
******************************************************************************* */
|
|
16
|
+
export declare const CLA = 85;
|
|
17
|
+
export declare const CHUNK_SIZE = 250;
|
|
18
|
+
export declare const APP_KEY = "CSM";
|
|
19
|
+
export declare const INS: {
|
|
20
|
+
GET_VERSION: number;
|
|
21
|
+
INS_PUBLIC_KEY_SECP256K1: number;
|
|
22
|
+
SIGN_SECP256K1: number;
|
|
23
|
+
GET_ADDR_SECP256K1: number;
|
|
24
|
+
};
|
|
25
|
+
export declare const PAYLOAD_TYPE: {
|
|
26
|
+
INIT: number;
|
|
27
|
+
ADD: number;
|
|
28
|
+
LAST: number;
|
|
29
|
+
};
|
|
30
|
+
export declare const P1_VALUES: {
|
|
31
|
+
ONLY_RETRIEVE: number;
|
|
32
|
+
SHOW_ADDRESS_IN_DEVICE: number;
|
|
33
|
+
};
|
|
34
|
+
export declare const ERROR_CODE: {
|
|
35
|
+
NoError: number;
|
|
36
|
+
};
|
|
37
|
+
export declare function errorCodeToString(statusCode: number): any;
|
|
38
|
+
export declare function processErrorResponse(response: any): any;
|
|
39
|
+
export declare function getVersion(transport: any): Promise<any>;
|