@zondax/ledger-cosmos-js 3.0.1 → 3.0.3

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 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 2018 - 2022 ZondaX AG
189
+ Copyright 2018 - 2023 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,33 +1,30 @@
1
1
  # @zondax/ledger-cosmos-js
2
2
 
3
- ![zondax](docs/zondax_light.png)
3
+ ![zondax_light](docs/zondax_light.png#gh-light-mode-only)
4
+ ![zondax_dark](docs/zondax_dark.png#gh-dark-mode-only)
4
5
 
5
6
  [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
6
- [![Main](https://github.com/Zondax/ledger-cosmos-js/workflows/Main/badge.svg)](https://github.com/Zondax/ledger-cosmos-js/actions?query=workflow%3AMain)
7
- [![npm version](https://badge.fury.io/js/%40zondax%2Fledger-cosmos-js.svg)](https://badge.fury.io/js/%40zondax%2Fledger-cosmos-js)
8
-
7
+ [![Main](https://github.com/cosmos/ledger-cosmos-js/workflows/Main/badge.svg)](https://github.com/cosmos/ledger-cosmos-js/actions?query=workflow%3AMain)
8
+ [![npm version](https://badge.fury.io/js/%40cosmos%2Fledger-cosmos-js.svg)](https://badge.fury.io/js/%40cosmos%2Fledger-cosmos-js)
9
9
 
10
10
  This package provides a basic client library to communicate with a Tendermint/Cosmos App running in a Ledger Nano S/S+/X devices
11
11
 
12
12
  We recommend using the npmjs package in order to receive updates/fixes.
13
13
 
14
-
15
14
  # Available commands
16
15
 
17
- | Operation | Response | Command |
18
- | ---------- | ---------------- | ----------------------- |
19
- | getVersion | app version | --------------- |
20
- | publicKey | pubkey | path (legacy command) |
21
- | getAddressAndPubKey | pubkey + address | path + ( showInDevice ) |
22
- | showAddressAndPubKey | signed message | path |
23
- | appInfo | name, version, flags, etc | --------------- |
24
- | deviceInfo | fw and mcu version, id, etc | Only available in dashboard |
25
- | sign | signed message | path + message |
26
-
16
+ | Operation | Response | Command |
17
+ | -------------------- | --------------------------- | --------------------------- |
18
+ | getVersion | app version | --------------- |
19
+ | publicKey | pubkey | path (legacy command) |
20
+ | getAddressAndPubKey | pubkey + address | path + ( showInDevice ) |
21
+ | showAddressAndPubKey | signed message | path |
22
+ | appInfo | name, version, flags, etc | --------------- |
23
+ | deviceInfo | fw and mcu version, id, etc | Only available in dashboard |
24
+ | sign | signed message | path + message + (HRP) |
27
25
 
28
26
  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.
29
27
 
30
-
31
28
  # Testing with real devices
32
29
 
33
30
  It is possible to test this package with a real Ledger Nano device. To accomplish that, you will need to follow these steps:
@@ -1,4 +1,4 @@
1
1
  /// <reference types="node" />
2
2
  export declare function serializePathv1(path: number[]): Buffer;
3
- export declare function signSendChunkv1(app: any, chunkIdx: number, chunkNum: number, chunk: Buffer): Promise<any>;
3
+ export declare function signSendChunkv1(app: any, chunkIdx: number, chunkNum: number, chunk: Buffer, txType?: number): Promise<any>;
4
4
  export declare function publicKeyv1(app: any, data: Buffer): Promise<any>;
@@ -14,7 +14,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
14
14
  function verb(n) { return function (v) { return step([n, v]); }; }
15
15
  function step(op) {
16
16
  if (f) throw new TypeError("Generator is already executing.");
17
- while (_) try {
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
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
19
  if (y = 0, t) op = [op[0] & 2, t.value];
20
20
  switch (op[0]) {
@@ -73,11 +73,12 @@ function serializePathv1(path) {
73
73
  return buf;
74
74
  }
75
75
  exports.serializePathv1 = serializePathv1;
76
- function signSendChunkv1(app, chunkIdx, chunkNum, chunk) {
76
+ function signSendChunkv1(app, chunkIdx, chunkNum, chunk, txType) {
77
+ if (txType === void 0) { txType = common_1.P2_VALUES.JSON; }
77
78
  return __awaiter(this, void 0, void 0, function () {
78
79
  return __generator(this, function (_a) {
79
80
  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
+ .send(common_1.CLA, common_1.INS.SIGN_SECP256K1, chunkIdx, txType, chunk, [common_1.ERROR_CODE.NoError, 0x6984, 0x6a80])
81
82
  .then(function (response) {
82
83
  var errorCodeData = response.slice(-2);
83
84
  var returnCode = errorCodeData[0] * 256 + errorCodeData[1];
@@ -1 +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,C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O;wBACL,EAAE,IAAA;wBACF,aAAa,EAAE,iBAAiB,CAAC,EAAE,CAAC;wBACpC,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 { 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"]}
1
+ {"version":3,"file":"commandsV1.js","sourceRoot":"","sources":["../src/commandsV1.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;;;;;;mFAcmF;AACnF,mCAAmG;AAEnG,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,CA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AA;oBAC5D,IAAM,EAAE,GAAG,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAA;oBAE7C,OAAO;wBACL,EAAE,IAAA;wBACF,aAAa,EAAE,iBAAiB,CAAC,EAAE,CAAC;wBACpC,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 { CLA, ERROR_CODE, errorCodeToString, INS, processErrorResponse, P2_VALUES } 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, txType = P2_VALUES.JSON) {\n return app.transport\n .send(CLA, INS.SIGN_SECP256K1, chunkIdx, txType, 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"]}
@@ -1,4 +1,4 @@
1
1
  /// <reference types="node" />
2
2
  export declare function serializePathv2(path: number[]): Buffer;
3
- export declare function signSendChunkv2(app: any, chunkIdx: number, chunkNum: number, chunk: Buffer): Promise<any>;
3
+ export declare function signSendChunkv2(app: any, chunkIdx: number, chunkNum: number, chunk: Buffer, txType?: number): Promise<any>;
4
4
  export declare function publicKeyv2(app: any, data: Buffer): Promise<any>;
@@ -14,7 +14,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
14
14
  function verb(n) { return function (v) { return step([n, v]); }; }
15
15
  function step(op) {
16
16
  if (f) throw new TypeError("Generator is already executing.");
17
- while (_) try {
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
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
19
  if (y = 0, t) op = [op[0] & 2, t.value];
20
20
  switch (op[0]) {
@@ -67,7 +67,8 @@ function serializePathv2(path) {
67
67
  return buf;
68
68
  }
69
69
  exports.serializePathv2 = serializePathv2;
70
- function signSendChunkv2(app, chunkIdx, chunkNum, chunk) {
70
+ function signSendChunkv2(app, chunkIdx, chunkNum, chunk, txType) {
71
+ if (txType === void 0) { txType = common_1.P2_VALUES.JSON; }
71
72
  return __awaiter(this, void 0, void 0, function () {
72
73
  var payloadType;
73
74
  return __generator(this, function (_a) {
@@ -78,7 +79,7 @@ function signSendChunkv2(app, chunkIdx, chunkNum, chunk) {
78
79
  if (chunkIdx === chunkNum) {
79
80
  payloadType = common_1.PAYLOAD_TYPE.LAST;
80
81
  }
81
- return [2 /*return*/, (0, commandsV1_1.signSendChunkv1)(app, payloadType, 0, chunk)];
82
+ return [2 /*return*/, (0, commandsV1_1.signSendChunkv1)(app, payloadType, 0, chunk, txType)];
82
83
  });
83
84
  });
84
85
  }
@@ -1 +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,IAAM,GAAG,GAAG,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,CAAA;IAC5B,GAAG,CAAC,aAAa,CAAC,UAAU,GAAG,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA;IAC1C,GAAG,CAAC,aAAa,CAAC,UAA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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"]}
1
+ {"version":3,"file":"commandsV2.js","sourceRoot":"","sources":["../src/commandsV2.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;;;;;;mFAcmF;AACnF,2CAA8C;AAC9C,mCAAiH;AAEjH,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,IAAM,GAAG,GAAG,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,CAAA;IAC5B,GAAG,CAAC,aAAa,CAAC,UAAU,GAAG,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA;IAC1C,GAAG,CAAC,aAAa,CAAC,UAAU,GAAG,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA;IAC1C,GAAG,CAAC,aAAa,CAAC,UAAU,GAAG,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA;IAC1C,GAAG,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAA;IAC9B,GAAG,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAA;IAE9B,OAAO,GAAG,CAAA;AACZ,CAAC;AAbD,0CAaC;AAED,SAAsB,eAAe,CAAC,GAAQ,EAAE,QAAgB,EAAE,QAAgB,EAAE,KAAa,EAAE,MAAuB;IAAvB,uBAAA,EAAA,SAAS,kBAAS,CAAC,IAAI;;;;YACpH,WAAW,GAAG,qBAAY,CAAC,GAAG,CAAA;YAClC,IAAI,QAAQ,KAAK,CAAC,EAAE;gBAClB,WAAW,GAAG,qBAAY,CAAC,IAAI,CAAA;aAChC;YACD,IAAI,QAAQ,KAAK,QAAQ,EAAE;gBACzB,WAAW,GAAG,qBAAY,CAAC,IAAI,CAAA;aAChC;YAED,sBAAO,IAAA,4BAAe,EAAC,GAAG,EAAE,WAAW,EAAE,CAAC,EAAE,KAAK,EAAE,MAAM,CAAC,EAAA;;;CAC3D;AAVD,0CAUC;AAED,SAAsB,WAAW,CAAC,GAAQ,EAAE,IAAY;;;YACtD,sBAAO,GAAG,CAAC,SAAS,CAAC,IAAI,CAAC,YAAG,EAAE,YAAG,CAAC,kBAAkB,EAAE,CAAC,EAAE,CAAC,EAAE,IAAI,EAAE,CAAC,mBAAU,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,UAAC,QAAa;oBAC1G,IAAM,aAAa,GAAG,QAAQ,CAAC,KAAK,CAAC,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, P2_VALUES } 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, txType = P2_VALUES.JSON) {\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, txType)\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 CHANGED
@@ -31,6 +31,10 @@ export declare const P1_VALUES: {
31
31
  ONLY_RETRIEVE: number;
32
32
  SHOW_ADDRESS_IN_DEVICE: number;
33
33
  };
34
+ export declare const P2_VALUES: {
35
+ JSON: number;
36
+ TEXTUAL: number;
37
+ };
34
38
  export declare const ERROR_CODE: {
35
39
  NoError: number;
36
40
  };
package/dist/common.js CHANGED
@@ -29,7 +29,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
29
29
  function verb(n) { return function (v) { return step([n, v]); }; }
30
30
  function step(op) {
31
31
  if (f) throw new TypeError("Generator is already executing.");
32
- while (_) try {
32
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
33
33
  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;
34
34
  if (y = 0, t) op = [op[0] & 2, t.value];
35
35
  switch (op[0]) {
@@ -51,7 +51,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
51
51
  }
52
52
  };
53
53
  exports.__esModule = true;
54
- exports.getVersion = exports.processErrorResponse = exports.errorCodeToString = exports.ERROR_CODE = exports.P1_VALUES = exports.PAYLOAD_TYPE = exports.INS = exports.APP_KEY = exports.CHUNK_SIZE = exports.CLA = void 0;
54
+ exports.getVersion = exports.processErrorResponse = exports.errorCodeToString = exports.ERROR_CODE = exports.P2_VALUES = exports.P1_VALUES = exports.PAYLOAD_TYPE = exports.INS = exports.APP_KEY = exports.CHUNK_SIZE = exports.CLA = void 0;
55
55
  exports.CLA = 0x55;
56
56
  exports.CHUNK_SIZE = 250;
57
57
  exports.APP_KEY = 'CSM';
@@ -70,6 +70,10 @@ exports.P1_VALUES = {
70
70
  ONLY_RETRIEVE: 0x00,
71
71
  SHOW_ADDRESS_IN_DEVICE: 0x01
72
72
  };
73
+ exports.P2_VALUES = {
74
+ JSON: 0x0,
75
+ TEXTUAL: 0x1
76
+ };
73
77
  exports.ERROR_CODE = {
74
78
  NoError: 0x9000
75
79
  };
@@ -1 +1 @@
1
- {"version":3,"file":"common.js","sourceRoot":"","sources":["../src/common.ts"],"names":[],"mappings":";AAAA;;;;;;;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,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAA;oBACxC,IAAM,UAAU,GAAG,aAAa,CAAC,CAAC,CAAC,GAAG,GAAG,GAAG,aAAa,CAAC,CAAC,CAAC,CAAA;oBAE5D,IAAI,QAAQ,GAAG,CAAC,CAAA;oBAChB,IAAI,QAAQ,CAAC,MAAM,IAAI,CAAC,EAAE;wBACxB,+BAA+B;wBAC/B,QAAQ,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAA;wBAC9F,8BAA8B;qBAC/B;oBAED,OAAO;wBACL,WAAW,EAAE,UAAU;wBACvB,aAAa,EAAE,iBAAiB,CAAC,UAAU,CAAC;wBAC5C,MAAM;wBACN,SAAS,EAAE,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC;wBAC5B,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAC;wBAClB,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAC;wBAClB,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAC;wBAClB,aAAa,EAAE,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC;wBAChC,SAAS,EAAE,QAAQ,CAAC,QAAQ,CAAC,EAAE,CAAC;qBACjC,CAAA;gBACH,CAAC,EAAE,oBAAoB,CAAC,EAAA;;;CACzB;AAxBD,gCAwBC","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 ******************************************************************************* */\n\nexport const CLA = 0x55\nexport const CHUNK_SIZE = 250\nexport const APP_KEY = 'CSM'\n\nexport const INS = {\n GET_VERSION: 0x00,\n INS_PUBLIC_KEY_SECP256K1: 0x01, // Obsolete\n SIGN_SECP256K1: 0x02,\n GET_ADDR_SECP256K1: 0x04,\n}\n\nexport const PAYLOAD_TYPE = {\n INIT: 0x00,\n ADD: 0x01,\n LAST: 0x02,\n}\n\nexport const P1_VALUES = {\n ONLY_RETRIEVE: 0x00,\n SHOW_ADDRESS_IN_DEVICE: 0x01,\n}\n\nexport const ERROR_CODE = {\n NoError: 0x9000,\n}\n\nconst ERROR_DESCRIPTION: any = {\n 1: 'U2F: Unknown',\n 2: 'U2F: Bad request',\n 3: 'U2F: Configuration unsupported',\n 4: 'U2F: Device Ineligible',\n 5: 'U2F: Timeout',\n 14: 'Timeout',\n 0x9000: 'No errors',\n 0x9001: 'Device is busy',\n 0x6802: 'Error deriving keys',\n 0x6400: 'Execution Error',\n 0x6700: 'Wrong Length',\n 0x6982: 'Empty Buffer',\n 0x6983: 'Output buffer too small',\n 0x6984: 'Data is invalid',\n 0x6985: 'Conditions not satisfied',\n 0x6986: 'Transaction rejected',\n 0x6a80: 'Bad key handle',\n 0x6b00: 'Invalid P1/P2',\n 0x6d00: 'Instruction not supported',\n 0x6e00: 'App does not seem to be open',\n 0x6f00: 'Unknown error',\n 0x6f01: 'Sign/verify error',\n}\n\nexport function errorCodeToString(statusCode: number) {\n if (statusCode in ERROR_DESCRIPTION) return ERROR_DESCRIPTION[statusCode]\n return `Unknown Status Code: ${statusCode}`\n}\n\nfunction isDict(v: any) {\n return typeof v === 'object' && v !== null && !(v instanceof Array) && !(v instanceof Date)\n}\n\nexport function processErrorResponse(response: any) {\n if (response) {\n if (isDict(response)) {\n if (Object.prototype.hasOwnProperty.call(response, 'statusCode')) {\n return {\n return_code: response.statusCode,\n error_message: errorCodeToString(response.statusCode),\n }\n }\n\n if (\n Object.prototype.hasOwnProperty.call(response, 'return_code') &&\n Object.prototype.hasOwnProperty.call(response, 'error_message')\n ) {\n return response\n }\n }\n return {\n return_code: 0xffff,\n error_message: response.toString(),\n }\n }\n\n return {\n return_code: 0xffff,\n error_message: response.toString(),\n }\n}\n\nexport async function getVersion(transport: any) {\n return transport.send(CLA, INS.GET_VERSION, 0, 0).then((response: any) => {\n const errorCodeData = response.slice(-2)\n const returnCode = errorCodeData[0] * 256 + errorCodeData[1]\n\n let targetId = 0\n if (response.length >= 9) {\n /* eslint-disable no-bitwise */\n targetId = (response[5] << 24) + (response[6] << 16) + (response[7] << 8) + (response[8] << 0)\n /* eslint-enable no-bitwise */\n }\n\n return {\n return_code: returnCode,\n error_message: errorCodeToString(returnCode),\n // ///\n test_mode: response[0] !== 0,\n major: response[1],\n minor: response[2],\n patch: response[3],\n device_locked: response[4] === 1,\n target_id: targetId.toString(16),\n }\n }, processErrorResponse)\n}\n"]}
1
+ {"version":3,"file":"common.js","sourceRoot":"","sources":["../src/common.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;mFAcmF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEtE,QAAA,GAAG,GAAG,IAAI,CAAA;AACV,QAAA,UAAU,GAAG,GAAG,CAAA;AAChB,QAAA,OAAO,GAAG,KAAK,CAAA;AAEf,QAAA,GAAG,GAAG;IACjB,WAAW,EAAE,IAAI;IACjB,wBAAwB,EAAE,IAAI;IAC9B,cAAc,EAAE,IAAI;IACpB,kBAAkB,EAAE,IAAI;CACzB,CAAA;AAEY,QAAA,YAAY,GAAG;IAC1B,IAAI,EAAE,IAAI;IACV,GAAG,EAAE,IAAI;IACT,IAAI,EAAE,IAAI;CACX,CAAA;AAEY,QAAA,SAAS,GAAG;IACvB,aAAa,EAAE,IAAI;IACnB,sBAAsB,EAAE,IAAI;CAC7B,CAAA;AAEY,QAAA,SAAS,G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,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAA;wBAC9F,8BAA8B;qBAC/B;oBAED,OAAO;wBACL,WAAW,EAAE,UAAU;wBACvB,aAAa,EAAE,iBAAiB,CAAC,UAAU,CAAC;wBAC5C,MAAM;wBACN,SAAS,EAAE,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC;wBAC5B,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAC;wBAClB,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAC;wBAClB,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAC;wBAClB,aAAa,EAAE,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC;wBAChC,SAAS,EAAE,QAAQ,CAAC,QAAQ,CAAC,EAAE,CAAC;qBACjC,CAAA;gBACH,CAAC,EAAE,oBAAoB,CAAC,EAAA;;;CACzB;AAxBD,gCAwBC","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 ******************************************************************************* */\n\nexport const CLA = 0x55\nexport const CHUNK_SIZE = 250\nexport const APP_KEY = 'CSM'\n\nexport const INS = {\n GET_VERSION: 0x00,\n INS_PUBLIC_KEY_SECP256K1: 0x01, // Obsolete\n SIGN_SECP256K1: 0x02,\n GET_ADDR_SECP256K1: 0x04,\n}\n\nexport const PAYLOAD_TYPE = {\n INIT: 0x00,\n ADD: 0x01,\n LAST: 0x02,\n}\n\nexport const P1_VALUES = {\n ONLY_RETRIEVE: 0x00,\n SHOW_ADDRESS_IN_DEVICE: 0x01,\n}\n\nexport const P2_VALUES = {\n JSON: 0x0,\n TEXTUAL: 0x1,\n}\n\nexport const ERROR_CODE = {\n NoError: 0x9000,\n}\n\nconst ERROR_DESCRIPTION: any = {\n 1: 'U2F: Unknown',\n 2: 'U2F: Bad request',\n 3: 'U2F: Configuration unsupported',\n 4: 'U2F: Device Ineligible',\n 5: 'U2F: Timeout',\n 14: 'Timeout',\n 0x9000: 'No errors',\n 0x9001: 'Device is busy',\n 0x6802: 'Error deriving keys',\n 0x6400: 'Execution Error',\n 0x6700: 'Wrong Length',\n 0x6982: 'Empty Buffer',\n 0x6983: 'Output buffer too small',\n 0x6984: 'Data is invalid',\n 0x6985: 'Conditions not satisfied',\n 0x6986: 'Transaction rejected',\n 0x6a80: 'Bad key handle',\n 0x6b00: 'Invalid P1/P2',\n 0x6d00: 'Instruction not supported',\n 0x6e00: 'App does not seem to be open',\n 0x6f00: 'Unknown error',\n 0x6f01: 'Sign/verify error',\n}\n\nexport function errorCodeToString(statusCode: number) {\n if (statusCode in ERROR_DESCRIPTION) return ERROR_DESCRIPTION[statusCode]\n return `Unknown Status Code: ${statusCode}`\n}\n\nfunction isDict(v: any) {\n return typeof v === 'object' && v !== null && !(v instanceof Array) && !(v instanceof Date)\n}\n\nexport function processErrorResponse(response: any) {\n if (response) {\n if (isDict(response)) {\n if (Object.prototype.hasOwnProperty.call(response, 'statusCode')) {\n return {\n return_code: response.statusCode,\n error_message: errorCodeToString(response.statusCode),\n }\n }\n\n if (\n Object.prototype.hasOwnProperty.call(response, 'return_code') &&\n Object.prototype.hasOwnProperty.call(response, 'error_message')\n ) {\n return response\n }\n }\n return {\n return_code: 0xffff,\n error_message: response.toString(),\n }\n }\n\n return {\n return_code: 0xffff,\n error_message: response.toString(),\n }\n}\n\nexport async function getVersion(transport: any) {\n return transport.send(CLA, INS.GET_VERSION, 0, 0).then((response: any) => {\n const errorCodeData = response.slice(-2)\n const returnCode = errorCodeData[0] * 256 + errorCodeData[1]\n\n let targetId = 0\n if (response.length >= 9) {\n /* eslint-disable no-bitwise */\n targetId = (response[5] << 24) + (response[6] << 16) + (response[7] << 8) + (response[8] << 0)\n /* eslint-enable no-bitwise */\n }\n\n return {\n return_code: returnCode,\n error_message: errorCodeToString(returnCode),\n // ///\n test_mode: response[0] !== 0,\n major: response[1],\n minor: response[2],\n patch: response[3],\n device_locked: response[4] === 1,\n target_id: targetId.toString(16),\n }\n }, processErrorResponse)\n}\n"]}
@@ -4,16 +4,17 @@ export declare class CosmosApp {
4
4
  transport: Transport;
5
5
  versionResponse?: any;
6
6
  constructor(transport: any);
7
- static serializeHRP(hrp: string): Buffer;
7
+ static serializeHRP(hrp?: string): Buffer;
8
8
  static getBech32FromPK(hrp: string, pk: Buffer): any;
9
9
  serializePath(path: number[]): Promise<Buffer>;
10
- signGetChunks(path: number[], message: string): Promise<Buffer[]>;
10
+ signGetChunks(path: number[], buffer: Buffer): Promise<Buffer[]>;
11
+ prepareChunks(path: number[], buffer: Buffer, hrp?: string): Promise<Buffer[]>;
11
12
  getVersion(): Promise<any>;
12
13
  appInfo(): Promise<any>;
13
14
  deviceInfo(): Promise<any>;
14
15
  publicKey(path: number[]): Promise<any>;
15
16
  getAddressAndPubKey(path: number[], hrp: string, showInDevice?: boolean): Promise<any>;
16
17
  showAddressAndPubKey(path: number[], hrp: string): Promise<any>;
17
- signSendChunk(chunkIdx: number, chunkNum: number, chunk: Buffer): Promise<any>;
18
- sign(path: number[], message: string): Promise<any>;
18
+ signSendChunk(chunkIdx: number, chunkNum: number, chunk: Buffer, txType?: number): Promise<any>;
19
+ sign(path: number[], buffer: Buffer, hrp?: string, txType?: number): Promise<any>;
19
20
  }
package/dist/cosmosApp.js CHANGED
@@ -14,7 +14,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
14
14
  function verb(n) { return function (v) { return step([n, v]); }; }
15
15
  function step(op) {
16
16
  if (f) throw new TypeError("Generator is already executing.");
17
- while (_) try {
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
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
19
  if (y = 0, t) op = [op[0] & 2, t.value];
20
20
  switch (op[0]) {
@@ -108,9 +108,9 @@ var CosmosApp = /** @class */ (function () {
108
108
  });
109
109
  });
110
110
  };
111
- CosmosApp.prototype.signGetChunks = function (path, message) {
111
+ CosmosApp.prototype.signGetChunks = function (path, buffer) {
112
112
  return __awaiter(this, void 0, void 0, function () {
113
- var serializedPath, chunks, buffer, i, end;
113
+ var serializedPath, chunks, i, end;
114
114
  return __generator(this, function (_a) {
115
115
  switch (_a.label) {
116
116
  case 0: return [4 /*yield*/, this.serializePath(path)];
@@ -118,7 +118,6 @@ var CosmosApp = /** @class */ (function () {
118
118
  serializedPath = _a.sent();
119
119
  chunks = [];
120
120
  chunks.push(serializedPath);
121
- buffer = Buffer.from(message);
122
121
  for (i = 0; i < buffer.length; i += common_1.CHUNK_SIZE) {
123
122
  end = i + common_1.CHUNK_SIZE;
124
123
  if (i > buffer.length) {
@@ -131,6 +130,30 @@ var CosmosApp = /** @class */ (function () {
131
130
  });
132
131
  });
133
132
  };
133
+ CosmosApp.prototype.prepareChunks = function (path, buffer, hrp) {
134
+ return __awaiter(this, void 0, void 0, function () {
135
+ var serializedPath, firstChunk, chunks, i, end;
136
+ return __generator(this, function (_a) {
137
+ switch (_a.label) {
138
+ case 0: return [4 /*yield*/, this.serializePath(path)];
139
+ case 1:
140
+ serializedPath = _a.sent();
141
+ firstChunk = (hrp === undefined) ? serializedPath
142
+ : Buffer.concat([serializedPath, CosmosApp.serializeHRP(hrp)]);
143
+ chunks = [];
144
+ chunks.push(firstChunk);
145
+ for (i = 0; i < buffer.length; i += common_1.CHUNK_SIZE) {
146
+ end = i + common_1.CHUNK_SIZE;
147
+ if (i > buffer.length) {
148
+ end = buffer.length;
149
+ }
150
+ chunks.push(buffer.subarray(i, end));
151
+ }
152
+ return [2 /*return*/, chunks];
153
+ }
154
+ });
155
+ });
156
+ };
134
157
  CosmosApp.prototype.getVersion = function () {
135
158
  return __awaiter(this, void 0, void 0, function () {
136
159
  var _a, e_1;
@@ -286,7 +309,9 @@ var CosmosApp = /** @class */ (function () {
286
309
  .then(function (serializedPath) {
287
310
  var data = Buffer.concat([CosmosApp.serializeHRP(hrp), serializedPath]);
288
311
  return _this.transport
289
- .send(common_1.CLA, common_1.INS.GET_ADDR_SECP256K1, showInDevice ? common_1.P1_VALUES.SHOW_ADDRESS_IN_DEVICE : common_1.P1_VALUES.ONLY_RETRIEVE, 0, data, [common_1.ERROR_CODE.NoError])
312
+ .send(common_1.CLA, common_1.INS.GET_ADDR_SECP256K1, showInDevice ? common_1.P1_VALUES.SHOW_ADDRESS_IN_DEVICE : common_1.P1_VALUES.ONLY_RETRIEVE, 0, data, [
313
+ common_1.ERROR_CODE.NoError,
314
+ ])
290
315
  .then(function (response) {
291
316
  var errorCodeData = response.slice(-2);
292
317
  var returnCode = errorCodeData[0] * 256 + errorCodeData[1];
@@ -310,14 +335,15 @@ var CosmosApp = /** @class */ (function () {
310
335
  });
311
336
  });
312
337
  };
313
- CosmosApp.prototype.signSendChunk = function (chunkIdx, chunkNum, chunk) {
338
+ CosmosApp.prototype.signSendChunk = function (chunkIdx, chunkNum, chunk, txType) {
339
+ if (txType === void 0) { txType = common_1.P2_VALUES.JSON; }
314
340
  return __awaiter(this, void 0, void 0, function () {
315
341
  return __generator(this, function (_a) {
316
342
  switch (this.versionResponse.major) {
317
343
  case 1:
318
- return [2 /*return*/, (0, commandsV1_1.signSendChunkv1)(this, chunkIdx, chunkNum, chunk)];
344
+ return [2 /*return*/, (0, commandsV1_1.signSendChunkv1)(this, chunkIdx, chunkNum, chunk, txType)];
319
345
  case 2:
320
- return [2 /*return*/, (0, commandsV2_1.signSendChunkv2)(this, chunkIdx, chunkNum, chunk)];
346
+ return [2 /*return*/, (0, commandsV2_1.signSendChunkv2)(this, chunkIdx, chunkNum, chunk, txType)];
321
347
  default:
322
348
  return [2 /*return*/, {
323
349
  return_code: 0x6400,
@@ -328,12 +354,37 @@ var CosmosApp = /** @class */ (function () {
328
354
  });
329
355
  });
330
356
  };
331
- CosmosApp.prototype.sign = function (path, message) {
357
+ // async sign(path: number[], buffer: Buffer, txType = P2_VALUES.JSON) {
358
+ // return this.signGetChunks(path, buffer).then(chunks => {
359
+ // return this.signSendChunk(1, chunks.length, chunks[0], txType).then(async response => {
360
+ // let result = {
361
+ // return_code: response.return_code,
362
+ // error_message: response.error_message,
363
+ // signature: null,
364
+ // }
365
+ // for (let i = 1; i < chunks.length; i += 1) {
366
+ // // eslint-disable-next-line no-await-in-loop
367
+ // result = await this.signSendChunk(1 + i, chunks.length, chunks[i], txType)
368
+ // if (result.return_code !== ERROR_CODE.NoError) {
369
+ // break
370
+ // }
371
+ // }
372
+ // return {
373
+ // return_code: result.return_code,
374
+ // error_message: result.error_message,
375
+ // // ///
376
+ // signature: result.signature,
377
+ // }
378
+ // }, processErrorResponse)
379
+ // }, processErrorResponse)
380
+ // }
381
+ CosmosApp.prototype.sign = function (path, buffer, hrp, txType) {
382
+ if (txType === void 0) { txType = common_1.P2_VALUES.JSON; }
332
383
  return __awaiter(this, void 0, void 0, function () {
333
384
  var _this = this;
334
385
  return __generator(this, function (_a) {
335
- return [2 /*return*/, this.signGetChunks(path, message).then(function (chunks) {
336
- return _this.signSendChunk(1, chunks.length, chunks[0]).then(function (response) { return __awaiter(_this, void 0, void 0, function () {
386
+ return [2 /*return*/, this.prepareChunks(path, buffer, hrp).then(function (chunks) {
387
+ return _this.signSendChunk(1, chunks.length, chunks[0], txType).then(function (response) { return __awaiter(_this, void 0, void 0, function () {
337
388
  var result, i;
338
389
  return __generator(this, function (_a) {
339
390
  switch (_a.label) {
@@ -347,7 +398,7 @@ var CosmosApp = /** @class */ (function () {
347
398
  _a.label = 1;
348
399
  case 1:
349
400
  if (!(i < chunks.length)) return [3 /*break*/, 4];
350
- return [4 /*yield*/, this.signSendChunk(1 + i, chunks.length, chunks[i])];
401
+ return [4 /*yield*/, this.signSendChunk(1 + i, chunks.length, chunks[i], txType)];
351
402
  case 2:
352
403
  // eslint-disable-next-line no-await-in-loop
353
404
  result = _a.sent();
@@ -1 +1 @@
1
- {"version":3,"file":"cosmosApp.js","sourceRoot":"","sources":["../src/cosmosApp.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;;;;;;mFAcmF;AACnF,mCAA2H;AAG3H,2CAA4E;AAC5E,2CAA4E;AAE5E,IAAM,MAAM,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAA;AAChC,IAAM,MAAM,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAA;AAChC,IAAM,SAAS,GAAG,OAAO,CAAC,WAAW,CAAC,CAAA;AAEtC;IAIE,mBAAY,SAAc;QACxB,IAAI,CAAC,SAAS,EAAE;YACd,MAAM,IAAI,KAAK,CAAC,gCAAgC,CAAC,CAAA;SAClD;QAED,IAAI,CAAC,SAAS,GAAG,SAAS,CAAA;IAC5B,CAAC;IAEM,sBAAY,GAAnB,UAAoB,GAAW;QAC7B,IAAI,GAAG,IAAI,IAAI,IAAI,GAAG,CAAC,MAAM,GAAG,CAAC,IAAI,GAAG,CAAC,MAAM,GAAG,EAAE,EAAE;YACpD,MAAM,IAAI,KAAK,CAAC,aAAa,CAAC,CAAA;SAC/B;QACD,IAAM,GAAG,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,GAAG,GAAG,CAAC,MAAM,CAAC,CAAA;QACxC,GAAG,CAAC,UAAU,CAAC,GAAG,CAAC,MAAM,EAAE,CAAC,CAAC,CAAA;QAC7B,GAAG,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,CAAC,CAAA;QACjB,OAAO,GAAG,CAAA;IACZ,CAAC;IAEM,yBAAe,GAAtB,UAAuB,GAAW,EAAE,EAAU;QAC5C,IAAI,EAAE,CAAC,MAAM,KAAK,EAAE,EAAE;YACpB,MAA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,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC,EAAA;;wCADlE,4CAA4C;wCAC5C,MAAM,GAAG,SAAyD,CAAA;wCAClE,IAAI,MAAM,CAAC,WAAW,KAAK,mBAAU,CAAC,OAAO,EAAE;4CAC7C,wBAAK;yCACN;;;wCALgC,CAAC,IAAI,CAAC,CAAA;;4CAQzC,sBAAO;4CACL,WAAW,EAAE,MAAM,CAAC,WAAW;4CAC/B,aAAa,EAAE,MAAM,CAAC,aAAa;4CACnC,MAAM;4CACN,SAAS,EAAE,MAAM,CAAC,SAAS;yCAC5B,EAAA;;;6BACF,EAAE,6BAAoB,CAAC,CAAA;oBAC1B,CAAC,EAAE,6BAAoB,CAAC,EAAA;;;KACzB;IACH,gBAAC;AAAD,CAAC,AArQD,IAqQC;AArQY,8BAAS","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 { CHUNK_SIZE, CLA, INS, errorCodeToString, getVersion, processErrorResponse, ERROR_CODE, P1_VALUES } from './common'\n\nimport Transport from '@ledgerhq/hw-transport'\nimport { publicKeyv1, serializePathv1, signSendChunkv1 } from './commandsV1'\nimport { publicKeyv2, serializePathv2, signSendChunkv2 } from './commandsV2'\n\nconst crypto = require('crypto')\nconst bech32 = require('bech32')\nconst Ripemd160 = require('ripemd160')\n\nexport class CosmosApp {\n transport: Transport\n versionResponse?: any\n\n constructor(transport: any) {\n if (!transport) {\n throw new Error('Transport has not been defined')\n }\n\n this.transport = transport\n }\n\n static serializeHRP(hrp: string) {\n if (hrp == null || hrp.length < 3 || hrp.length > 83) {\n throw new Error('Invalid HRP')\n }\n const buf = Buffer.alloc(1 + hrp.length)\n buf.writeUInt8(hrp.length, 0)\n buf.write(hrp, 1)\n return buf\n }\n\n static getBech32FromPK(hrp: string, pk: Buffer) {\n if (pk.length !== 33) {\n throw new Error('expected compressed public key [31 bytes]')\n }\n const hashSha256 = crypto.createHash('sha256').update(pk).digest()\n const hashRip = new Ripemd160().update(hashSha256).digest()\n return bech32.encode(hrp, bech32.toWords(hashRip))\n }\n\n async serializePath(path: number[]) {\n this.versionResponse = await getVersion(this.transport)\n\n if (this.versionResponse.return_code !== ERROR_CODE.NoError) {\n throw this.versionResponse\n }\n\n switch (this.versionResponse.major) {\n case 1:\n return serializePathv1(path)\n case 2:\n return serializePathv2(path)\n default:\n return Buffer.alloc(0)\n }\n }\n\n async signGetChunks(path: number[], message: string) {\n const serializedPath = await this.serializePath(path)\n\n const chunks = []\n chunks.push(serializedPath)\n const buffer = Buffer.from(message)\n\n for (let i = 0; i < buffer.length; i += CHUNK_SIZE) {\n let end = i + CHUNK_SIZE\n if (i > buffer.length) {\n end = buffer.length\n }\n chunks.push(buffer.slice(i, end))\n }\n\n return chunks\n }\n\n async getVersion() {\n try {\n this.versionResponse = await getVersion(this.transport)\n return this.versionResponse\n } catch (e) {\n return processErrorResponse(e)\n }\n }\n\n async appInfo() {\n return this.transport.send(0xb0, 0x01, 0, 0).then((response: any) => {\n const errorCodeData = response.slice(-2)\n const returnCode = errorCodeData[0] * 256 + errorCodeData[1]\n\n let appName = ''\n let appVersion = ''\n let flagLen = 0\n let flagsValue = 0\n\n if (response[0] !== 1) {\n // Ledger responds with format ID 1. There is no spec for any format != 1\n return {\n return_code: 0x9001,\n error_message: 'response format ID not recognized',\n }\n } else {\n const appNameLen = response[1]\n appName = response.slice(2, 2 + appNameLen).toString('ascii')\n let idx = 2 + appNameLen\n const appVersionLen = response[idx]\n idx += 1\n appVersion = response.slice(idx, idx + appVersionLen).toString('ascii')\n idx += appVersionLen\n const appFlagsLen = response[idx]\n idx += 1\n flagLen = appFlagsLen\n flagsValue = response[idx]\n }\n\n return {\n return_code: returnCode,\n error_message: errorCodeToString(returnCode),\n // //\n appName,\n appVersion,\n flagLen,\n flagsValue,\n // eslint-disable-next-line no-bitwise\n flag_recovery: (flagsValue & 1) !== 0,\n // eslint-disable-next-line no-bitwise\n flag_signed_mcu_code: (flagsValue & 2) !== 0,\n // eslint-disable-next-line no-bitwise\n flag_onboarded: (flagsValue & 4) !== 0,\n // eslint-disable-next-line no-bitwise\n flag_pin_validated: (flagsValue & 128) !== 0,\n }\n }, processErrorResponse)\n }\n\n async deviceInfo() {\n return this.transport.send(0xe0, 0x01, 0, 0, Buffer.from([]), [ERROR_CODE.NoError, 0x6e00]).then((response: any) => {\n const errorCodeData = response.slice(-2)\n const returnCode = errorCodeData[0] * 256 + errorCodeData[1]\n\n if (returnCode === 0x6e00) {\n return {\n return_code: returnCode,\n error_message: 'This command is only available in the Dashboard',\n }\n }\n\n const targetId = response.slice(0, 4).toString('hex')\n\n let pos = 4\n const secureElementVersionLen = response[pos]\n pos += 1\n const seVersion = response.slice(pos, pos + secureElementVersionLen).toString()\n pos += secureElementVersionLen\n\n const flagsLen = response[pos]\n pos += 1\n const flag = response.slice(pos, pos + flagsLen).toString('hex')\n pos += flagsLen\n\n const mcuVersionLen = response[pos]\n pos += 1\n // Patch issue in mcu version\n let tmp = response.slice(pos, pos + mcuVersionLen)\n if (tmp[mcuVersionLen - 1] === 0) {\n tmp = response.slice(pos, pos + mcuVersionLen - 1)\n }\n const mcuVersion = tmp.toString()\n\n return {\n return_code: returnCode,\n error_message: errorCodeToString(returnCode),\n // //\n targetId,\n seVersion,\n flag,\n mcuVersion,\n }\n }, processErrorResponse)\n }\n\n async publicKey(path: number[]) {\n try {\n const serializedPath = await this.serializePath(path)\n\n switch (this.versionResponse.major) {\n case 1:\n return publicKeyv1(this, serializedPath)\n case 2: {\n const data = Buffer.concat([CosmosApp.serializeHRP('cosmos'), serializedPath])\n return publicKeyv2(this, data)\n }\n default:\n return {\n return_code: 0x6400,\n error_message: 'App Version is not supported',\n }\n }\n } catch (e) {\n return processErrorResponse(e)\n }\n }\n\n async getAddressAndPubKey(path: number[], hrp: string, showInDevice = false) {\n return this.serializePath(path)\n .then((serializedPath: Buffer) => {\n const data = Buffer.concat([CosmosApp.serializeHRP(hrp), serializedPath])\n return this.transport\n .send(CLA, INS.GET_ADDR_SECP256K1, showInDevice ? P1_VALUES.SHOW_ADDRESS_IN_DEVICE : P1_VALUES.ONLY_RETRIEVE, 0, data, [ERROR_CODE.NoError])\n .then((response: any) => {\n const errorCodeData = response.slice(-2)\n const returnCode = errorCodeData[0] * 256 + errorCodeData[1]\n\n const compressedPk = Buffer.from(response.slice(0, 33))\n const bech32Address = Buffer.from(response.slice(33, -2)).toString()\n\n return {\n bech32_address: bech32Address,\n compressed_pk: compressedPk,\n return_code: returnCode,\n error_message: errorCodeToString(returnCode),\n }\n }, processErrorResponse)\n })\n .catch(err => processErrorResponse(err))\n }\n\n async showAddressAndPubKey(path: number[], hrp: string) {\n return this.getAddressAndPubKey(path, hrp, true)\n }\n\n async signSendChunk(chunkIdx: number, chunkNum: number, chunk: Buffer) {\n switch (this.versionResponse.major) {\n case 1:\n return signSendChunkv1(this, chunkIdx, chunkNum, chunk)\n case 2:\n return signSendChunkv2(this, chunkIdx, chunkNum, chunk)\n default:\n return {\n return_code: 0x6400,\n error_message: 'App Version is not supported',\n }\n }\n }\n\n async sign(path: number[], message: string) {\n return this.signGetChunks(path, message).then(chunks => {\n return this.signSendChunk(1, chunks.length, chunks[0]).then(async response => {\n let result = {\n return_code: response.return_code,\n error_message: response.error_message,\n signature: null,\n }\n\n for (let i = 1; i < chunks.length; i += 1) {\n // eslint-disable-next-line no-await-in-loop\n result = await this.signSendChunk(1 + i, chunks.length, chunks[i])\n if (result.return_code !== ERROR_CODE.NoError) {\n break\n }\n }\n\n return {\n return_code: result.return_code,\n error_message: result.error_message,\n // ///\n signature: result.signature,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A4C;wCAC5C,MAAM,GAAG,SAAiE,CAAA;wCAC1E,IAAI,MAAM,CAAC,WAAW,KAAK,mBAAU,CAAC,OAAO,EAAE;4CAC7C,wBAAK;yCACN;;;wCALgC,CAAC,IAAI,CAAC,CAAA;;4CAQzC,sBAAO;4CACL,WAAW,EAAE,MAAM,CAAC,WAAW;4CAC/B,aAAa,EAAE,MAAM,CAAC,aAAa;4CACnC,MAAM;4CACN,SAAS,EAAE,MAAM,CAAC,SAAS;yCAC5B,EAAA;;;6BACF,EAAE,6BAAoB,CAAC,CAAA;oBAC1B,CAAC,EAAE,6BAAoB,CAAC,EAAA;;;KACzB;IACH,gBAAC;AAAD,CAAC,AApTD,IAoTC;AApTY,8BAAS","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 { CHUNK_SIZE, CLA, INS, errorCodeToString, getVersion, processErrorResponse, ERROR_CODE, P1_VALUES, P2_VALUES } from './common'\n\nimport Transport from '@ledgerhq/hw-transport'\nimport { publicKeyv1, serializePathv1, signSendChunkv1 } from './commandsV1'\nimport { publicKeyv2, serializePathv2, signSendChunkv2 } from './commandsV2'\n\nconst crypto = require('crypto')\nconst bech32 = require('bech32')\nconst Ripemd160 = require('ripemd160')\n\nexport class CosmosApp {\n transport: Transport\n versionResponse?: any\n\n constructor(transport: any) {\n if (!transport) {\n throw new Error('Transport has not been defined')\n }\n\n this.transport = transport\n }\n\n static serializeHRP(hrp?: string) {\n if (hrp == null || hrp.length < 3 || hrp.length > 83) {\n throw new Error('Invalid HRP')\n }\n const buf = Buffer.alloc(1 + hrp.length)\n buf.writeUInt8(hrp.length, 0)\n buf.write(hrp, 1)\n return buf\n }\n\n static getBech32FromPK(hrp: string, pk: Buffer) {\n if (pk.length !== 33) {\n throw new Error('expected compressed public key [31 bytes]')\n }\n const hashSha256 = crypto.createHash('sha256').update(pk).digest()\n const hashRip = new Ripemd160().update(hashSha256).digest()\n return bech32.encode(hrp, bech32.toWords(hashRip))\n }\n\n async serializePath(path: number[]) {\n this.versionResponse = await getVersion(this.transport)\n\n if (this.versionResponse.return_code !== ERROR_CODE.NoError) {\n throw this.versionResponse\n }\n\n switch (this.versionResponse.major) {\n case 1:\n return serializePathv1(path)\n case 2:\n return serializePathv2(path)\n default:\n return Buffer.alloc(0)\n }\n }\n\n async signGetChunks(path: number[], buffer: Buffer) {\n const serializedPath = await this.serializePath(path)\n\n const chunks = []\n chunks.push(serializedPath)\n\n for (let i = 0; i < buffer.length; i += CHUNK_SIZE) {\n let end = i + CHUNK_SIZE\n if (i > buffer.length) {\n end = buffer.length\n }\n chunks.push(buffer.slice(i, end))\n }\n\n return chunks\n }\n\n async prepareChunks(path: number[], buffer: Buffer, hrp?: string) {\n const serializedPath = await this.serializePath(path)\n const firstChunk = (hrp === undefined) ? serializedPath\n : Buffer.concat([serializedPath, CosmosApp.serializeHRP(hrp)])\n\n const chunks = []\n chunks.push(firstChunk)\n\n for (let i = 0; i < buffer.length; i += CHUNK_SIZE) {\n let end = i + CHUNK_SIZE\n if (i > buffer.length) {\n end = buffer.length\n }\n chunks.push(buffer.subarray(i, end))\n }\n\n return chunks\n }\n\n async getVersion() {\n try {\n this.versionResponse = await getVersion(this.transport)\n return this.versionResponse\n } catch (e) {\n return processErrorResponse(e)\n }\n }\n\n async appInfo() {\n return this.transport.send(0xb0, 0x01, 0, 0).then((response: any) => {\n const errorCodeData = response.slice(-2)\n const returnCode = errorCodeData[0] * 256 + errorCodeData[1]\n\n let appName = ''\n let appVersion = ''\n let flagLen = 0\n let flagsValue = 0\n\n if (response[0] !== 1) {\n // Ledger responds with format ID 1. There is no spec for any format != 1\n return {\n return_code: 0x9001,\n error_message: 'response format ID not recognized',\n }\n } else {\n const appNameLen = response[1]\n appName = response.slice(2, 2 + appNameLen).toString('ascii')\n let idx = 2 + appNameLen\n const appVersionLen = response[idx]\n idx += 1\n appVersion = response.slice(idx, idx + appVersionLen).toString('ascii')\n idx += appVersionLen\n const appFlagsLen = response[idx]\n idx += 1\n flagLen = appFlagsLen\n flagsValue = response[idx]\n }\n\n return {\n return_code: returnCode,\n error_message: errorCodeToString(returnCode),\n // //\n appName,\n appVersion,\n flagLen,\n flagsValue,\n // eslint-disable-next-line no-bitwise\n flag_recovery: (flagsValue & 1) !== 0,\n // eslint-disable-next-line no-bitwise\n flag_signed_mcu_code: (flagsValue & 2) !== 0,\n // eslint-disable-next-line no-bitwise\n flag_onboarded: (flagsValue & 4) !== 0,\n // eslint-disable-next-line no-bitwise\n flag_pin_validated: (flagsValue & 128) !== 0,\n }\n }, processErrorResponse)\n }\n\n async deviceInfo() {\n return this.transport.send(0xe0, 0x01, 0, 0, Buffer.from([]), [ERROR_CODE.NoError, 0x6e00]).then((response: any) => {\n const errorCodeData = response.slice(-2)\n const returnCode = errorCodeData[0] * 256 + errorCodeData[1]\n\n if (returnCode === 0x6e00) {\n return {\n return_code: returnCode,\n error_message: 'This command is only available in the Dashboard',\n }\n }\n\n const targetId = response.slice(0, 4).toString('hex')\n\n let pos = 4\n const secureElementVersionLen = response[pos]\n pos += 1\n const seVersion = response.slice(pos, pos + secureElementVersionLen).toString()\n pos += secureElementVersionLen\n\n const flagsLen = response[pos]\n pos += 1\n const flag = response.slice(pos, pos + flagsLen).toString('hex')\n pos += flagsLen\n\n const mcuVersionLen = response[pos]\n pos += 1\n // Patch issue in mcu version\n let tmp = response.slice(pos, pos + mcuVersionLen)\n if (tmp[mcuVersionLen - 1] === 0) {\n tmp = response.slice(pos, pos + mcuVersionLen - 1)\n }\n const mcuVersion = tmp.toString()\n\n return {\n return_code: returnCode,\n error_message: errorCodeToString(returnCode),\n // //\n targetId,\n seVersion,\n flag,\n mcuVersion,\n }\n }, processErrorResponse)\n }\n\n async publicKey(path: number[]) {\n try {\n const serializedPath = await this.serializePath(path)\n\n switch (this.versionResponse.major) {\n case 1:\n return publicKeyv1(this, serializedPath)\n case 2: {\n const data = Buffer.concat([CosmosApp.serializeHRP('cosmos'), serializedPath])\n return publicKeyv2(this, data)\n }\n default:\n return {\n return_code: 0x6400,\n error_message: 'App Version is not supported',\n }\n }\n } catch (e) {\n return processErrorResponse(e)\n }\n }\n\n async getAddressAndPubKey(path: number[], hrp: string, showInDevice = false) {\n return this.serializePath(path)\n .then((serializedPath: Buffer) => {\n const data = Buffer.concat([CosmosApp.serializeHRP(hrp), serializedPath])\n return this.transport\n .send(CLA, INS.GET_ADDR_SECP256K1, showInDevice ? P1_VALUES.SHOW_ADDRESS_IN_DEVICE : P1_VALUES.ONLY_RETRIEVE, 0, data, [\n ERROR_CODE.NoError,\n ])\n .then((response: any) => {\n const errorCodeData = response.slice(-2)\n const returnCode = errorCodeData[0] * 256 + errorCodeData[1]\n\n const compressedPk = Buffer.from(response.slice(0, 33))\n const bech32Address = Buffer.from(response.slice(33, -2)).toString()\n\n return {\n bech32_address: bech32Address,\n compressed_pk: compressedPk,\n return_code: returnCode,\n error_message: errorCodeToString(returnCode),\n }\n }, processErrorResponse)\n })\n .catch(err => processErrorResponse(err))\n }\n\n async showAddressAndPubKey(path: number[], hrp: string) {\n return this.getAddressAndPubKey(path, hrp, true)\n }\n\n async signSendChunk(chunkIdx: number, chunkNum: number, chunk: Buffer, txType = P2_VALUES.JSON) {\n switch (this.versionResponse.major) {\n case 1:\n return signSendChunkv1(this, chunkIdx, chunkNum, chunk, txType)\n case 2:\n return signSendChunkv2(this, chunkIdx, chunkNum, chunk, txType)\n default:\n return {\n return_code: 0x6400,\n error_message: 'App Version is not supported',\n }\n }\n }\n\n // async sign(path: number[], buffer: Buffer, txType = P2_VALUES.JSON) {\n // return this.signGetChunks(path, buffer).then(chunks => {\n // return this.signSendChunk(1, chunks.length, chunks[0], txType).then(async response => {\n // let result = {\n // return_code: response.return_code,\n // error_message: response.error_message,\n // signature: null,\n // }\n\n // for (let i = 1; i < chunks.length; i += 1) {\n // // eslint-disable-next-line no-await-in-loop\n // result = await this.signSendChunk(1 + i, chunks.length, chunks[i], txType)\n // if (result.return_code !== ERROR_CODE.NoError) {\n // break\n // }\n // }\n\n // return {\n // return_code: result.return_code,\n // error_message: result.error_message,\n // // ///\n // signature: result.signature,\n // }\n // }, processErrorResponse)\n // }, processErrorResponse)\n // }\n\n async sign(path: number[], buffer: Buffer, hrp?: string, txType = P2_VALUES.JSON) {\n return this.prepareChunks(path, buffer, hrp).then(chunks => {\n return this.signSendChunk(1, chunks.length, chunks[0], txType).then(async response => {\n let result = {\n return_code: response.return_code,\n error_message: response.error_message,\n signature: null,\n }\n\n for (let i = 1; i < chunks.length; i += 1) {\n // eslint-disable-next-line no-await-in-loop\n result = await this.signSendChunk(1 + i, chunks.length, chunks[i], txType)\n if (result.return_code !== ERROR_CODE.NoError) {\n break\n }\n }\n\n return {\n return_code: result.return_code,\n error_message: result.error_message,\n // ///\n signature: result.signature,\n }\n }, processErrorResponse)\n }, processErrorResponse)\n }\n}\n"]}
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@zondax/ledger-cosmos-js",
3
- "author": "ZondaX AG",
3
+ "author": "Zondax AG",
4
4
  "license": "Apache-2.0",
5
- "version": "3.0.1",
5
+ "version": "3.0.3",
6
6
  "description": "Node API for Cosmos App (Ledger Nano S/S+/X)",
7
7
  "main": "./dist/index.js",
8
8
  "typings": "./dist/index.d.ts",
@@ -30,8 +30,17 @@
30
30
  "dependencies": {
31
31
  "@babel/runtime": "^7.14.8",
32
32
  "@ledgerhq/hw-transport": "6.27.1",
33
+ "@ledgerhq/hw-transport-u2f": "^5.36.0-deprecated",
34
+ "@vue/cli-plugin-typescript": "^5.0.8",
35
+ "@vue/cli-service": "^5.0.8",
33
36
  "bech32": "^1.1.4",
34
- "ripemd160": "^2.0.2"
37
+ "buffer": "^6.0.3",
38
+ "crypto": "^1.0.1",
39
+ "crypto-browserify": "^3.12.0",
40
+ "ripemd160": "^2.0.2",
41
+ "stream-browserify": "^3.0.0",
42
+ "vue": "^2.7.7",
43
+ "vue-template-compiler": "^2.7.10"
35
44
  },
36
45
  "devDependencies": {
37
46
  "@babel/cli": "^7.18.10",
@@ -44,13 +53,14 @@
44
53
  "@swc/core": "^1.2.237",
45
54
  "@types/jest": "^26.0.24",
46
55
  "@types/ledgerhq__hw-transport": "4.21.4",
56
+ "@types/node": "^18.11.9",
47
57
  "@typescript-eslint/eslint-plugin": "^5.33.1",
48
58
  "@typescript-eslint/parser": "^5.33.1",
59
+ "babel-eslint": "^10.1.0",
60
+ "babel-jest": "^28.1.3",
49
61
  "bip32": "2.0.6",
50
62
  "bip39": "3.0.2",
51
63
  "copyfiles": "^2.4.1",
52
- "babel-eslint": "^10.1.0",
53
- "babel-jest": "^28.1.3",
54
64
  "core-js": "^3.15.2",
55
65
  "crypto-js": "4.1.1",
56
66
  "eslint": "^8.22.0",
@@ -64,7 +74,8 @@
64
74
  "secp256k1": "^4.0.2",
65
75
  "ts-jest": "26",
66
76
  "ts-node": "^10.8.1",
67
- "typescript": "^4.7.4"
77
+ "typescript": "^4.7.4",
78
+ "webpack": "^5.74.0"
68
79
  },
69
80
  "moduleDirectories": [
70
81
  "node_modules",