@zondax/ledger-cosmos-js 4.0.0 → 4.1.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 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 - 2023 Zondax AG
189
+ Copyright 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.npm.md ADDED
@@ -0,0 +1,42 @@
1
+ # @zondax/ledger-cosmos-js
2
+
3
+ ![zondax](docs/zondax_light.png)
4
+
5
+ [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
6
+ [![Main](https://github.com/cosmos/ledger-cosmos-js/workflows/Main/badge.svg)](https://github.com/cosmos/ledger-cosmos-js/actions?query=workflow%3AMain)
7
+ [![npm version](https://badge.fury.io/js/%40cosmos%2Fledger-cosmos-js.svg)](https://badge.fury.io/js/%40cosmos%2Fledger-cosmos-js)
8
+
9
+ This package provides a basic client library to communicate with a Tendermint/Cosmos App running in a Ledger Nano S/S+/X devices
10
+
11
+ We recommend using the npmjs package in order to receive updates/fixes.
12
+
13
+ # Available commands
14
+
15
+ | Operation | Response | Command |
16
+ | -------------------- | --------------------------- | --------------------------- |
17
+ | getVersion | app version | --------------- |
18
+ | publicKey | pubkey | path (legacy command) |
19
+ | getAddressAndPubKey | pubkey + address | path + ( showInDevice ) |
20
+ | showAddressAndPubKey | signed message | path |
21
+ | appInfo | name, version, flags, etc | --------------- |
22
+ | deviceInfo | fw and mcu version, id, etc | Only available in dashboard |
23
+ | sign | signed message | path + message + (HRP) |
24
+
25
+ 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.
26
+
27
+ # Testing with real devices
28
+
29
+ It is possible to test this package with a real Ledger Nano device. To accomplish that, you will need to follow these steps:
30
+
31
+ - Install the application in the Ledger device
32
+ - Install the dependencies from this project
33
+ - Run tests
34
+
35
+ ```shell script
36
+ yarn install
37
+ yarn test:integration
38
+ ```
39
+
40
+ # Who we are?
41
+
42
+ 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)
package/dist/consts.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  /** ******************************************************************************
2
- * (c) 2018 - 2023 Zondax AG
2
+ * (c) Zondax AG
3
3
  *
4
4
  * Licensed under the Apache License, Version 2.0 (the "License");
5
5
  * you may not use this file except in compliance with the License.
package/dist/consts.js CHANGED
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.PKLEN = exports.CLA = void 0;
3
+ exports.PKLEN = exports.P2_VALUES = exports.P1_VALUES = exports.CLA = void 0;
4
4
  /** ******************************************************************************
5
- * (c) 2018 - 2023 Zondax AG
5
+ * (c) Zondax AG
6
6
  *
7
7
  * Licensed under the Apache License, Version 2.0 (the "License");
8
8
  * you may not use this file except in compliance with the License.
@@ -17,5 +17,15 @@ exports.PKLEN = exports.CLA = void 0;
17
17
  * limitations under the License.
18
18
  ******************************************************************************* */
19
19
  exports.CLA = 0x55;
20
+ var P1_VALUES;
21
+ (function (P1_VALUES) {
22
+ P1_VALUES[P1_VALUES["ONLY_RETRIEVE"] = 0] = "ONLY_RETRIEVE";
23
+ P1_VALUES[P1_VALUES["SHOW_ADDRESS_IN_DEVICE"] = 1] = "SHOW_ADDRESS_IN_DEVICE";
24
+ })(P1_VALUES || (exports.P1_VALUES = P1_VALUES = {}));
25
+ var P2_VALUES;
26
+ (function (P2_VALUES) {
27
+ P2_VALUES[P2_VALUES["JSON"] = 0] = "JSON";
28
+ P2_VALUES[P2_VALUES["TEXTUAL"] = 1] = "TEXTUAL";
29
+ })(P2_VALUES || (exports.P2_VALUES = P2_VALUES = {}));
20
30
  exports.PKLEN = 33;
21
31
  //# sourceMappingURL=consts.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"consts.js","sourceRoot":"","sources":["../src/consts.ts"],"names":[],"mappings":";;;AAAA;;;;;;;;;;;;;;mFAcmF;AACtE,QAAA,GAAG,GAAG,IAAI,CAAC;AAYX,QAAA,KAAK,GAAG,EAAE,CAAC","sourcesContent":["/** ******************************************************************************\n * (c) 2018 - 2023 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 ******************************************************************************* */\nexport const CLA = 0x55;\n\nexport const enum P1_VALUES {\n ONLY_RETRIEVE = 0x00,\n SHOW_ADDRESS_IN_DEVICE = 0x01,\n}\n\nexport const enum P2_VALUES {\n JSON = 0x0,\n TEXTUAL = 0x1,\n}\n\nexport const PKLEN = 33;\n"]}
1
+ {"version":3,"file":"consts.js","sourceRoot":"","sources":["../src/consts.ts"],"names":[],"mappings":";;;AAAA;;;;;;;;;;;;;;mFAcmF;AACtE,QAAA,GAAG,GAAG,IAAI,CAAA;AAEvB,IAAkB,SAGjB;AAHD,WAAkB,SAAS;IACzB,2DAAoB,CAAA;IACpB,6EAA6B,CAAA;AAC/B,CAAC,EAHiB,SAAS,yBAAT,SAAS,QAG1B;AAED,IAAkB,SAGjB;AAHD,WAAkB,SAAS;IACzB,yCAAU,CAAA;IACV,+CAAa,CAAA;AACf,CAAC,EAHiB,SAAS,yBAAT,SAAS,QAG1B;AAEY,QAAA,KAAK,GAAG,EAAE,CAAA","sourcesContent":["/** ******************************************************************************\n * (c) 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 ******************************************************************************* */\nexport const CLA = 0x55\n\nexport const enum P1_VALUES {\n ONLY_RETRIEVE = 0x00,\n SHOW_ADDRESS_IN_DEVICE = 0x01,\n}\n\nexport const enum P2_VALUES {\n JSON = 0x0,\n TEXTUAL = 0x1,\n}\n\nexport const PKLEN = 33\n"]}
package/dist/index.d.ts CHANGED
@@ -1,22 +1,6 @@
1
- /** ******************************************************************************
2
- * (c) 2018 - 2023 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
- import { ResponseSign, type ResponseAddress, type ResponsePubkey } from "./types";
17
- import { P2_VALUES } from "./consts";
18
- import type Transport from "@ledgerhq/hw-transport";
19
- import BaseApp, { INSGeneric, ResponsePayload } from "@zondax/ledger-js";
1
+ import BaseApp, { INSGeneric, type LedgerTransport, ResponsePayload } from '@zondax/ledger-js';
2
+ import { P2_VALUES } from './consts';
3
+ import { type ResponseAddress, type ResponsePubkey, ResponseSign } from './types';
20
4
  export default class CosmosApp extends BaseApp {
21
5
  static _INS: {
22
6
  GET_VERSION: number;
@@ -27,8 +11,8 @@ export default class CosmosApp extends BaseApp {
27
11
  cla: number;
28
12
  ins: INSGeneric;
29
13
  p1Values: {
30
- ONLY_RETRIEVE: 0;
31
- SHOW_ADDRESS_IN_DEVICE: 1;
14
+ ONLY_RETRIEVE: number;
15
+ SHOW_ADDRESS_IN_DEVICE: number;
32
16
  };
33
17
  chunkSize: number;
34
18
  requiredPathLengths: number[];
@@ -38,15 +22,15 @@ export default class CosmosApp extends BaseApp {
38
22
  * @param transport - The transport instance.
39
23
  * @throws {Error} - If the transport is not defined.
40
24
  */
41
- constructor(transport: Transport);
25
+ constructor(transport: LedgerTransport);
42
26
  private serializeHRP;
43
27
  private serializeCosmosPath;
44
28
  publicKey(path: string): Promise<ResponsePubkey>;
45
29
  getAddressAndPubKey(path: string, hrp: string): Promise<ResponseAddress>;
46
30
  showAddressAndPubKey(path: string, hrp: string): Promise<ResponseAddress>;
47
- static getBech32FromPK(hrp: string, pk: Buffer): any;
48
- prepareChunks_hrp(path: string, buffer: Buffer, hrp: string | undefined): Promise<Buffer[]>;
31
+ static getBech32FromPK(hrp: string, pk: Buffer): `${Lowercase<string>}1${string}`;
32
+ prepareChunks_hrp(path: string, buffer: Buffer, hrp?: string): Promise<Buffer<ArrayBufferLike>[]>;
49
33
  signSendCosmosChunk(chunkIdx: number, chunkNum: number, chunk: Buffer, txtype?: P2_VALUES): Promise<ResponsePayload>;
50
34
  private signImpl;
51
- sign(path: string, buffer: Buffer, hrp: string | undefined, txtype?: P2_VALUES): Promise<ResponseSign>;
35
+ sign(path: string, buffer: Buffer, hrp?: string, txtype?: P2_VALUES): Promise<ResponseSign>;
52
36
  }
package/dist/index.js CHANGED
@@ -15,20 +15,61 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
15
15
  }) : function(o, v) {
16
16
  o["default"] = v;
17
17
  });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
25
35
  Object.defineProperty(exports, "__esModule", { value: true });
26
- const consts_1 = require("./consts");
36
+ /** ******************************************************************************
37
+ * (c) Zondax AG
38
+ *
39
+ * Licensed under the Apache License, Version 2.0 (the "License");
40
+ * you may not use this file except in compliance with the License.
41
+ * You may obtain a copy of the License at
42
+ *
43
+ * http://www.apache.org/licenses/LICENSE-2.0
44
+ *
45
+ * Unless required by applicable law or agreed to in writing, software
46
+ * distributed under the License is distributed on an "AS IS" BASIS,
47
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
48
+ * See the License for the specific language governing permissions and
49
+ * limitations under the License.
50
+ ******************************************************************************* */
51
+ const legacy_js_1 = require("@noble/hashes/legacy.js");
52
+ const sha2_js_1 = require("@noble/hashes/sha2.js");
53
+ const base_1 = require("@scure/base");
27
54
  const ledger_js_1 = __importStar(require("@zondax/ledger-js"));
28
55
  const byteStream_1 = require("@zondax/ledger-js/dist/byteStream");
29
- const crypto = require("crypto");
30
- const bech32 = require("bech32");
31
- const Ripemd160 = require("ripemd160");
56
+ const consts_1 = require("./consts");
57
+ var CosmosCostumError;
58
+ (function (CosmosCostumError) {
59
+ CosmosCostumError[CosmosCostumError["ErrorTransactionDataExceedsBufferCapacity"] = 27016] = "ErrorTransactionDataExceedsBufferCapacity";
60
+ CosmosCostumError[CosmosCostumError["ErrorInvalidHDPathValue"] = 27017] = "ErrorInvalidHDPathValue";
61
+ CosmosCostumError[CosmosCostumError["ErrorHrpWrongLength"] = 27018] = "ErrorHrpWrongLength";
62
+ CosmosCostumError[CosmosCostumError["ErrorInvalidHDPathCoinValue"] = 27019] = "ErrorInvalidHDPathCoinValue";
63
+ CosmosCostumError[CosmosCostumError["ErrorChainConfigNotSupported"] = 27020] = "ErrorChainConfigNotSupported";
64
+ })(CosmosCostumError || (CosmosCostumError = {}));
65
+ const COSMOS_CUSTOM_ERROR_DESCRIPTIONS = {
66
+ ...ledger_js_1.ERROR_DESCRIPTION_OVERRIDE,
67
+ [CosmosCostumError.ErrorTransactionDataExceedsBufferCapacity]: "Transaction data exceeds the device's internal buffer capacity",
68
+ [CosmosCostumError.ErrorInvalidHDPathValue]: 'Invalid HD Path Value. Expert Mode required.',
69
+ [CosmosCostumError.ErrorHrpWrongLength]: 'Invalid HRP length',
70
+ [CosmosCostumError.ErrorInvalidHDPathCoinValue]: 'Invalid HD Path Coin Value',
71
+ [CosmosCostumError.ErrorChainConfigNotSupported]: 'Chain config not supported',
72
+ };
32
73
  class CosmosApp extends ledger_js_1.default {
33
74
  /**
34
75
  * Constructs a new CosmosApp instance.
@@ -38,12 +79,12 @@ class CosmosApp extends ledger_js_1.default {
38
79
  constructor(transport) {
39
80
  super(transport, CosmosApp._params);
40
81
  if (!this.transport) {
41
- throw new Error("Transport has not been defined");
82
+ throw new Error('Transport has not been defined');
42
83
  }
43
84
  }
44
85
  serializeHRP(hrp) {
45
- if (hrp == null || hrp.length < 3 || hrp.length > 83) {
46
- throw new Error("Invalid HRP");
86
+ if (!hrp || hrp.length < 3 || hrp.length > 83) {
87
+ throw new Error('Invalid HRP');
47
88
  }
48
89
  const buf = new byteStream_1.ByteStream();
49
90
  buf.appendUint8(hrp.length);
@@ -51,22 +92,18 @@ class CosmosApp extends ledger_js_1.default {
51
92
  return buf.getCompleteBuffer();
52
93
  }
53
94
  serializeCosmosPath(path) {
54
- if (typeof path !== "string") {
55
- // NOTE: this is probably unnecessary
95
+ if (typeof path !== 'string') {
56
96
  throw new ledger_js_1.ResponseError(ledger_js_1.LedgerError.GenericError, "Path should be a string (e.g \"m/44'/461'/5'/0/3\")");
57
97
  }
58
- if (!path.startsWith("m/")) {
98
+ if (!path.startsWith('m/')) {
59
99
  throw new ledger_js_1.ResponseError(ledger_js_1.LedgerError.GenericError, 'Path should start with "m/" (e.g "m/44\'/461\'/5\'/0/3")');
60
100
  }
61
- const pathArray = path.split("/");
62
- pathArray.shift(); // remove "m"
63
- if (this.REQUIRED_PATH_LENGTHS &&
64
- this.REQUIRED_PATH_LENGTHS.length > 0 &&
65
- !this.REQUIRED_PATH_LENGTHS.includes(pathArray.length)) {
101
+ const pathArray = path.split('/').slice(1);
102
+ if (this.REQUIRED_PATH_LENGTHS && this.REQUIRED_PATH_LENGTHS.length > 0 && !this.REQUIRED_PATH_LENGTHS.includes(pathArray.length)) {
66
103
  throw new ledger_js_1.ResponseError(ledger_js_1.LedgerError.GenericError, "Invalid path length. (e.g \"m/44'/5757'/5'/0/3\")");
67
104
  }
68
105
  const buf = new byteStream_1.ByteStream();
69
- pathArray.forEach((child, i) => {
106
+ pathArray.forEach((child) => {
70
107
  let value = 0;
71
108
  if (child.endsWith("'")) {
72
109
  value += ledger_js_1.HARDENED;
@@ -82,106 +119,86 @@ class CosmosApp extends ledger_js_1.default {
82
119
  return buf.getCompleteBuffer();
83
120
  }
84
121
  async publicKey(path) {
85
- const serializedPath = await this.serializeCosmosPath(path);
86
- const data = Buffer.concat([this.serializeHRP("cosmos"), serializedPath]);
122
+ const serializedPath = this.serializeCosmosPath(path);
123
+ const data = Buffer.concat([this.serializeHRP('cosmos'), serializedPath]);
87
124
  try {
88
125
  const responseBuffer = await this.transport.send(this.CLA, this.INS.GET_ADDR_SECP256K1, 0, 0, data);
89
126
  const response = (0, ledger_js_1.processResponse)(responseBuffer);
90
127
  const compressed_pk = Buffer.from(response.readBytes(consts_1.PKLEN));
91
- return {
92
- compressed_pk,
93
- };
128
+ const bech32_address = response.readBytes(response.length()).toString();
129
+ return { compressed_pk, bech32_address };
94
130
  }
95
131
  catch (e) {
96
- throw (0, ledger_js_1.processErrorResponse)(e);
132
+ throw (0, ledger_js_1.processErrorResponse)(e, COSMOS_CUSTOM_ERROR_DESCRIPTIONS);
97
133
  }
98
134
  }
99
135
  async getAddressAndPubKey(path, hrp) {
100
- const serializedPath = await this.serializeCosmosPath(path);
136
+ const serializedPath = this.serializeCosmosPath(path);
101
137
  const data = Buffer.concat([this.serializeHRP(hrp), serializedPath]);
102
138
  try {
103
- const responseBuffer = await this.transport.send(this.CLA, this.INS.GET_ADDR_SECP256K1, 0 /* P1_VALUES.ONLY_RETRIEVE */, 0, data);
139
+ const responseBuffer = await this.transport.send(this.CLA, this.INS.GET_ADDR_SECP256K1, consts_1.P1_VALUES.ONLY_RETRIEVE, 0, data);
104
140
  const response = (0, ledger_js_1.processResponse)(responseBuffer);
105
141
  const compressed_pk = response.readBytes(consts_1.PKLEN);
106
142
  const bech32_address = response.readBytes(response.length()).toString();
107
- return {
108
- compressed_pk,
109
- bech32_address,
110
- };
143
+ return { compressed_pk, bech32_address };
111
144
  }
112
145
  catch (e) {
113
- throw (0, ledger_js_1.processErrorResponse)(e);
146
+ throw (0, ledger_js_1.processErrorResponse)(e, COSMOS_CUSTOM_ERROR_DESCRIPTIONS);
114
147
  }
115
148
  }
116
149
  async showAddressAndPubKey(path, hrp) {
117
- const serializedPath = await this.serializeCosmosPath(path);
150
+ const serializedPath = this.serializeCosmosPath(path);
118
151
  const data = Buffer.concat([this.serializeHRP(hrp), serializedPath]);
119
152
  try {
120
- const responseBuffer = await this.transport.send(this.CLA, this.INS.GET_ADDR_SECP256K1, 1 /* P1_VALUES.SHOW_ADDRESS_IN_DEVICE */, 0, data);
153
+ const responseBuffer = await this.transport.send(this.CLA, this.INS.GET_ADDR_SECP256K1, consts_1.P1_VALUES.SHOW_ADDRESS_IN_DEVICE, 0, data);
121
154
  const response = (0, ledger_js_1.processResponse)(responseBuffer);
122
155
  const compressed_pk = response.readBytes(consts_1.PKLEN);
123
156
  const bech32_address = response.readBytes(response.length()).toString();
124
- return {
125
- compressed_pk,
126
- bech32_address,
127
- };
157
+ return { compressed_pk, bech32_address };
128
158
  }
129
159
  catch (e) {
130
- throw (0, ledger_js_1.processErrorResponse)(e);
160
+ throw (0, ledger_js_1.processErrorResponse)(e, COSMOS_CUSTOM_ERROR_DESCRIPTIONS);
131
161
  }
132
162
  }
133
163
  static getBech32FromPK(hrp, pk) {
134
164
  if (pk.length !== 33) {
135
- throw new Error("expected compressed public key [31 bytes]");
165
+ throw new Error('expected compressed public key [31 bytes]');
136
166
  }
137
- const hashSha256 = crypto.createHash("sha256").update(pk).digest();
138
- const hashRip = new Ripemd160().update(hashSha256).digest();
139
- return bech32.encode(hrp, bech32.toWords(hashRip));
167
+ const hashSha256 = (0, sha2_js_1.sha256)(pk);
168
+ const hashRip = (0, legacy_js_1.ripemd160)(hashSha256);
169
+ return base_1.bech32.encode(hrp, base_1.bech32.toWords(hashRip));
140
170
  }
141
171
  async prepareChunks_hrp(path, buffer, hrp) {
142
- const serializedPath = await this.serializeCosmosPath(path);
143
- const firstChunk = hrp === undefined ? serializedPath : Buffer.concat([serializedPath, this.serializeHRP(hrp)]);
144
- const chunks = [];
145
- chunks.push(firstChunk);
172
+ const serializedPath = this.serializeCosmosPath(path);
173
+ const firstChunk = hrp ? Buffer.concat([serializedPath, this.serializeHRP(hrp)]) : serializedPath;
174
+ const chunks = [firstChunk];
146
175
  for (let i = 0; i < buffer.length; i += this.CHUNK_SIZE) {
147
- let end = i + this.CHUNK_SIZE;
148
- if (i > buffer.length) {
149
- end = buffer.length;
150
- }
176
+ const end = Math.min(i + this.CHUNK_SIZE, buffer.length);
151
177
  chunks.push(buffer.subarray(i, end));
152
178
  }
153
179
  return chunks;
154
180
  }
155
- async signSendCosmosChunk(chunkIdx, chunkNum, chunk, txtype = 0 /* P2_VALUES.JSON */) {
156
- let payloadType = ledger_js_1.PAYLOAD_TYPE.ADD;
157
- if (chunkIdx === 1) {
158
- payloadType = ledger_js_1.PAYLOAD_TYPE.INIT;
159
- }
160
- if (chunkIdx === chunkNum) {
161
- payloadType = ledger_js_1.PAYLOAD_TYPE.LAST;
162
- }
181
+ async signSendCosmosChunk(chunkIdx, chunkNum, chunk, txtype = consts_1.P2_VALUES.JSON) {
182
+ const payloadType = chunkIdx === 1 ? ledger_js_1.PAYLOAD_TYPE.INIT : chunkIdx === chunkNum ? ledger_js_1.PAYLOAD_TYPE.LAST : ledger_js_1.PAYLOAD_TYPE.ADD;
163
183
  const statusList = [ledger_js_1.LedgerError.NoErrors, ledger_js_1.LedgerError.DataIsInvalid, ledger_js_1.LedgerError.BadKeyHandle];
164
184
  const responseBuffer = await this.transport.send(this.CLA, this.INS.SIGN_SECP256K1, payloadType, txtype, chunk, statusList);
165
- const response = (0, ledger_js_1.processResponse)(responseBuffer);
166
- return response;
185
+ return (0, ledger_js_1.processResponse)(responseBuffer);
167
186
  }
168
- async signImpl(path, buffer, hrp, txtype) {
187
+ async signImpl(path, buffer, txtype, hrp) {
169
188
  const chunks = await this.prepareChunks_hrp(path, buffer, hrp);
170
189
  try {
171
190
  let result = await this.signSendCosmosChunk(1, chunks.length, chunks[0], txtype);
172
- for (let i = 1; i < chunks.length; i += 1) {
173
- result = await this.signSendCosmosChunk(1 + i, chunks.length, chunks[i], txtype);
191
+ for (let i = 1; i < chunks.length; i++) {
192
+ result = await this.signSendCosmosChunk(i + 1, chunks.length, chunks[i], txtype);
174
193
  }
175
- return {
176
- signature: result.readBytes(result.length()),
177
- };
194
+ return { signature: result.readBytes(result.length()) };
178
195
  }
179
196
  catch (e) {
180
- throw (0, ledger_js_1.processErrorResponse)(e);
197
+ throw (0, ledger_js_1.processErrorResponse)(e, COSMOS_CUSTOM_ERROR_DESCRIPTIONS);
181
198
  }
182
199
  }
183
- async sign(path, buffer, hrp, txtype = 0 /* P2_VALUES.JSON */) {
184
- return await this.signImpl(path, buffer, hrp, txtype);
200
+ async sign(path, buffer, hrp, txtype = consts_1.P2_VALUES.JSON) {
201
+ return this.signImpl(path, buffer, txtype, hrp);
185
202
  }
186
203
  }
187
204
  CosmosApp._INS = {
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAgBA,qCAA4D;AAE5D,+DAS2B;AAC3B,kEAA+D;AAE/D,MAAM,MAAM,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC;AACjC,MAAM,MAAM,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC;AACjC,MAAM,SAAS,GAAG,OAAO,CAAC,WAAW,CAAC,CAAC;AAEvC,MAAqB,SAAU,SAAQ,mBAAO;IAe5C;;;;OAIG;IACH,YAAY,SAAoB;QAC9B,KAAK,CAAC,SAAS,EAAE,SAAS,CAAC,OAAO,CAAC,CAAC;QAEpC,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,CAAC;YACpB,MAAM,IAAI,KAAK,CAAC,gCAAgC,CAAC,CAAC;QACpD,CAAC;IACH,CAAC;IAEO,YAAY,CAAC,GAAW;QAC9B,IAAI,GAAG,IAAI,IAAI,IAAI,GAAG,CAAC,MAAM,GAAG,CAAC,IAAI,GAAG,CAAC,MAAM,GAAG,EAAE,EAAE,CAAC;YACrD,MAAM,IAAI,KAAK,CAAC,aAAa,CAAC,CAAC;QACjC,CAAC;QAED,MAAM,GAAG,GAAG,IAAI,uBAAU,EAAE,CAAC;QAC7B,GAAG,CAAC,WAAW,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QAC5B,GAAG,CAAC,WAAW,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;QAElC,OAAO,GAAG,CAAC,iBAAiB,EAAE,CAAC;IACjC,CAAC;IAEO,mBAAmB,CAAC,IAAY;QACtC,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;YAC7B,qCAAqC;YACrC,MAAM,IAAI,yBAAa,CACrB,uBA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1C,MAAM,GAAG,MAAM,IAAI,CAAC,mBAAmB,CAAC,CAAC,GAAG,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC;YACnF,CAAC;YAED,OAAO;gBACL,SAAS,EAAE,MAAM,CAAC,SAAS,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC;aAC7C,CAAC;QACJ,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC;YACX,MAAM,IAAA,gCAAoB,EAAC,CAAC,CAAC,CAAC;QAChC,CAAC;IACH,CAAC;IAED,KAAK,CAAC,IAAI,CACR,IAAY,EACZ,MAAc,EACd,GAAuB,EACvB,MAAM,yBAAiB;QAEvB,OAAO,MAAM,IAAI,CAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,CAAC,CAAC;IACxD,CAAC;;AAtPM,cAAI,GAAG;IACZ,WAAW,EAAE,IAAc;IAC3B,cAAc,EAAE,IAAc;IAC9B,kBAAkB,EAAE,IAAc;CACnC,CAAC;AAEK,iBAAO,GAAG;IACf,GAAG,EAAE,YAAG;IACR,GAAG,EAAE,EAAE,GAAG,SAAS,CAAC,IAAI,EAAgB;IACxC,QAAQ,EAAE,EAAE,aAAa,EAAE,IAAS,EAAE,sBAAsB,EAAE,IAAS,EAAE;IACzE,SAAS,EAAE,GAAG;IACd,mBAAmB,EAAE,CAAC,CAAC,CAAC;CACzB,CAAC;kBAbiB,SAAS","sourcesContent":["/** ******************************************************************************\n * (c) 2018 - 2023 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 { ResponseSign, type ResponseAddress, type ResponsePubkey } from \"./types\";\nimport { CLA, P1_VALUES, P2_VALUES, PKLEN } from \"./consts\";\nimport type Transport from \"@ledgerhq/hw-transport\";\nimport BaseApp, {\n HARDENED,\n INSGeneric,\n LedgerError,\n PAYLOAD_TYPE,\n ResponseError,\n ResponsePayload,\n processErrorResponse,\n processResponse,\n} from \"@zondax/ledger-js\";\nimport { ByteStream } from \"@zondax/ledger-js/dist/byteStream\";\n\nconst crypto = require(\"crypto\");\nconst bech32 = require(\"bech32\");\nconst Ripemd160 = require(\"ripemd160\");\n\nexport default class CosmosApp extends BaseApp {\n static _INS = {\n GET_VERSION: 0x00 as number,\n SIGN_SECP256K1: 0x02 as number,\n GET_ADDR_SECP256K1: 0x04 as number,\n };\n\n static _params = {\n cla: CLA,\n ins: { ...CosmosApp._INS } as INSGeneric,\n p1Values: { ONLY_RETRIEVE: 0x00 as 0, SHOW_ADDRESS_IN_DEVICE: 0x01 as 1 },\n chunkSize: 250,\n requiredPathLengths: [5],\n };\n\n /**\n * Constructs a new CosmosApp instance.\n * @param transport - The transport instance.\n * @throws {Error} - If the transport is not defined.\n */\n constructor(transport: Transport) {\n super(transport, CosmosApp._params);\n\n if (!this.transport) {\n throw new Error(\"Transport has not been defined\");\n }\n }\n\n private serializeHRP(hrp: string) {\n if (hrp == null || hrp.length < 3 || hrp.length > 83) {\n throw new Error(\"Invalid HRP\");\n }\n\n const buf = new ByteStream();\n buf.appendUint8(hrp.length);\n buf.appendBytes(Buffer.from(hrp));\n\n return buf.getCompleteBuffer();\n }\n\n private serializeCosmosPath(path: string) {\n if (typeof path !== \"string\") {\n // NOTE: this is probably unnecessary\n throw new ResponseError(\n LedgerError.GenericError,\n \"Path should be a string (e.g \\\"m/44'/461'/5'/0/3\\\")\",\n );\n }\n\n if (!path.startsWith(\"m/\")) {\n throw new ResponseError(\n LedgerError.GenericError,\n 'Path should start with \"m/\" (e.g \"m/44\\'/461\\'/5\\'/0/3\")',\n );\n }\n\n const pathArray = path.split(\"/\");\n pathArray.shift(); // remove \"m\"\n\n if (\n this.REQUIRED_PATH_LENGTHS &&\n this.REQUIRED_PATH_LENGTHS.length > 0 &&\n !this.REQUIRED_PATH_LENGTHS.includes(pathArray.length)\n ) {\n throw new ResponseError(LedgerError.GenericError, \"Invalid path length. (e.g \\\"m/44'/5757'/5'/0/3\\\")\");\n }\n\n const buf = new ByteStream();\n pathArray.forEach((child: string, i: number) => {\n let value = 0;\n\n if (child.endsWith(\"'\")) {\n value += HARDENED;\n child = child.slice(0, -1);\n }\n\n const numChild = Number(child);\n\n if (Number.isNaN(numChild)) {\n throw new ResponseError(\n LedgerError.GenericError,\n `Invalid path : ${child} is not a number. (e.g \"m/44'/461'/5'/0/3\")`,\n );\n }\n value += numChild;\n buf.appendUint32(value);\n });\n\n return buf.getCompleteBuffer();\n }\n\n async publicKey(path: string): Promise<ResponsePubkey> {\n const serializedPath = await this.serializeCosmosPath(path);\n const data = Buffer.concat([this.serializeHRP(\"cosmos\"), serializedPath]);\n\n try {\n const responseBuffer = await this.transport.send(this.CLA, this.INS.GET_ADDR_SECP256K1, 0, 0, data);\n const response = processResponse(responseBuffer);\n const compressed_pk = Buffer.from(response.readBytes(PKLEN));\n\n return {\n compressed_pk,\n } as ResponsePubkey;\n } catch (e) {\n throw processErrorResponse(e);\n }\n }\n\n async getAddressAndPubKey(path: string, hrp: string): Promise<ResponseAddress> {\n const serializedPath = await this.serializeCosmosPath(path);\n const data = Buffer.concat([this.serializeHRP(hrp), serializedPath]);\n\n try {\n const responseBuffer = await this.transport.send(\n this.CLA,\n this.INS.GET_ADDR_SECP256K1,\n P1_VALUES.ONLY_RETRIEVE,\n 0,\n data,\n );\n\n const response = processResponse(responseBuffer);\n const compressed_pk = response.readBytes(PKLEN);\n const bech32_address = response.readBytes(response.length()).toString();\n\n return {\n compressed_pk,\n bech32_address,\n } as ResponseAddress;\n } catch (e) {\n throw processErrorResponse(e);\n }\n }\n\n async showAddressAndPubKey(path: string, hrp: string): Promise<ResponseAddress> {\n const serializedPath = await this.serializeCosmosPath(path);\n const data = Buffer.concat([this.serializeHRP(hrp), serializedPath]);\n\n try {\n const responseBuffer = await this.transport.send(\n this.CLA,\n this.INS.GET_ADDR_SECP256K1,\n P1_VALUES.SHOW_ADDRESS_IN_DEVICE,\n 0,\n data,\n );\n\n const response = processResponse(responseBuffer);\n const compressed_pk = response.readBytes(PKLEN);\n const bech32_address = response.readBytes(response.length()).toString();\n\n return {\n compressed_pk,\n bech32_address,\n } as ResponseAddress;\n } catch (e) {\n throw processErrorResponse(e);\n }\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 prepareChunks_hrp(path: string, buffer: Buffer, hrp: string | undefined) {\n const serializedPath = await this.serializeCosmosPath(path);\n const firstChunk =\n hrp === undefined ? serializedPath : Buffer.concat([serializedPath, this.serializeHRP(hrp)]);\n\n const chunks = [];\n chunks.push(firstChunk);\n\n for (let i = 0; i < buffer.length; i += this.CHUNK_SIZE) {\n let end = i + this.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 signSendCosmosChunk(\n chunkIdx: number,\n chunkNum: number,\n chunk: Buffer,\n txtype = P2_VALUES.JSON,\n ): Promise<ResponsePayload> {\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 const statusList = [LedgerError.NoErrors, LedgerError.DataIsInvalid, LedgerError.BadKeyHandle];\n\n const responseBuffer = await this.transport.send(\n this.CLA,\n this.INS.SIGN_SECP256K1,\n payloadType,\n txtype,\n chunk,\n statusList,\n );\n const response = processResponse(responseBuffer);\n\n return response;\n }\n\n private async signImpl(\n path: string,\n buffer: Buffer,\n hrp: string | undefined,\n txtype: number,\n ): Promise<ResponseSign> {\n const chunks = await this.prepareChunks_hrp(path, buffer, hrp);\n\n try {\n let result = await this.signSendCosmosChunk(1, chunks.length, chunks[0], txtype);\n\n for (let i = 1; i < chunks.length; i += 1) {\n result = await this.signSendCosmosChunk(1 + i, chunks.length, chunks[i], txtype);\n }\n\n return {\n signature: result.readBytes(result.length()),\n };\n } catch (e) {\n throw processErrorResponse(e);\n }\n }\n\n async sign(\n path: string,\n buffer: Buffer,\n hrp: string | undefined,\n txtype = P2_VALUES.JSON,\n ): Promise<ResponseSign> {\n return await this.signImpl(path, buffer, hrp, txtype);\n }\n}\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;;;;;;mFAcmF;AACnF,uDAAmD;AACnD,mDAA8C;AAC9C,sCAAoC;AACpC,+DAW0B;AAC1B,kEAA8D;AAE9D,qCAA2D;AAG3D,IAAK,iBAMJ;AAND,WAAK,iBAAiB;IACpB,uIAAkD,CAAA;IAClD,mGAAgC,CAAA;IAChC,2FAA4B,CAAA;IAC5B,2GAAoC,CAAA;IACpC,6GAAqC,CAAA;AACvC,CAAC,EANI,iBAAiB,KAAjB,iBAAiB,QAMrB;AAED,MAAM,gCAAgC,GAAG;IACvC,GAAG,sCAA0B;IAC7B,CAAC,iBAAiB,CAAC,yCAAyC,CAAC,EAAE,gEAAgE;IAC/H,CAAC,iBAAiB,CAAC,uBAAuB,CAAC,EAAE,8CAA8C;IAC3F,CAAC,iBAAiB,CAAC,mBAAmB,CAAC,EAAE,oBAAoB;IAC7D,CAAC,iBAAiB,CAAC,2BAA2B,CAAC,EAAE,4BAA4B;IAC7E,CAAC,iBAAiB,CAAC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;QAC5E,OAAO,IAAI,CAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,CAAC,CAAA;IACjD,CAAC;;AA9KM,cAAI,GAAG;IACZ,WAAW,EAAE,IAAI;IACjB,cAAc,EAAE,IAAI;IACpB,kBAAkB,EAAE,IAAI;CACzB,CAAA;AAEM,iBAAO,GAAG;IACf,GAAG,EAAE,YAAG;IACR,GAAG,EAAE,EAAE,GAAG,SAAS,CAAC,IAAI,EAAgB;IACxC,QAAQ,EAAE,EAAE,aAAa,EAAE,IAAI,EAAE,sBAAsB,EAAE,IAAI,EAAE;IAC/D,SAAS,EAAE,GAAG;IACd,mBAAmB,EAAE,CAAC,CAAC,CAAC;CACzB,CAAA;kBAbkB,SAAS","sourcesContent":["/** ******************************************************************************\n * (c) 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 { ripemd160 } from '@noble/hashes/legacy.js'\nimport { sha256 } from '@noble/hashes/sha2.js'\nimport { bech32 } from '@scure/base'\nimport BaseApp, {\n ERROR_DESCRIPTION_OVERRIDE,\n HARDENED,\n INSGeneric,\n LedgerError,\n type LedgerTransport,\n PAYLOAD_TYPE,\n ResponseError,\n ResponsePayload,\n processErrorResponse,\n processResponse,\n} from '@zondax/ledger-js'\nimport { ByteStream } from '@zondax/ledger-js/dist/byteStream'\n\nimport { CLA, P1_VALUES, P2_VALUES, PKLEN } from './consts'\nimport { type ResponseAddress, type ResponsePubkey, ResponseSign } from './types'\n\nenum CosmosCostumError {\n ErrorTransactionDataExceedsBufferCapacity = 0x6988,\n ErrorInvalidHDPathValue = 0x6989,\n ErrorHrpWrongLength = 0x698a,\n ErrorInvalidHDPathCoinValue = 0x698b,\n ErrorChainConfigNotSupported = 0x698c,\n}\n\nconst COSMOS_CUSTOM_ERROR_DESCRIPTIONS = {\n ...ERROR_DESCRIPTION_OVERRIDE,\n [CosmosCostumError.ErrorTransactionDataExceedsBufferCapacity]: \"Transaction data exceeds the device's internal buffer capacity\",\n [CosmosCostumError.ErrorInvalidHDPathValue]: 'Invalid HD Path Value. Expert Mode required.',\n [CosmosCostumError.ErrorHrpWrongLength]: 'Invalid HRP length',\n [CosmosCostumError.ErrorInvalidHDPathCoinValue]: 'Invalid HD Path Coin Value',\n [CosmosCostumError.ErrorChainConfigNotSupported]: 'Chain config not supported',\n}\n\nexport default class CosmosApp extends BaseApp {\n static _INS = {\n GET_VERSION: 0x00,\n SIGN_SECP256K1: 0x02,\n GET_ADDR_SECP256K1: 0x04,\n }\n\n static _params = {\n cla: CLA,\n ins: { ...CosmosApp._INS } as INSGeneric,\n p1Values: { ONLY_RETRIEVE: 0x00, SHOW_ADDRESS_IN_DEVICE: 0x01 },\n chunkSize: 250,\n requiredPathLengths: [5],\n }\n\n /**\n * Constructs a new CosmosApp instance.\n * @param transport - The transport instance.\n * @throws {Error} - If the transport is not defined.\n */\n constructor(transport: LedgerTransport) {\n super(transport, CosmosApp._params)\n\n if (!this.transport) {\n throw new Error('Transport has not been defined')\n }\n }\n\n private serializeHRP(hrp: string) {\n if (!hrp || hrp.length < 3 || hrp.length > 83) {\n throw new Error('Invalid HRP')\n }\n\n const buf = new ByteStream()\n buf.appendUint8(hrp.length)\n buf.appendBytes(Buffer.from(hrp))\n\n return buf.getCompleteBuffer()\n }\n\n private serializeCosmosPath(path: string) {\n if (typeof path !== 'string') {\n throw new ResponseError(LedgerError.GenericError, \"Path should be a string (e.g \\\"m/44'/461'/5'/0/3\\\")\")\n }\n\n if (!path.startsWith('m/')) {\n throw new ResponseError(LedgerError.GenericError, 'Path should start with \"m/\" (e.g \"m/44\\'/461\\'/5\\'/0/3\")')\n }\n\n const pathArray = path.split('/').slice(1)\n\n if (this.REQUIRED_PATH_LENGTHS && this.REQUIRED_PATH_LENGTHS.length > 0 && !this.REQUIRED_PATH_LENGTHS.includes(pathArray.length)) {\n throw new ResponseError(LedgerError.GenericError, \"Invalid path length. (e.g \\\"m/44'/5757'/5'/0/3\\\")\")\n }\n\n const buf = new ByteStream()\n pathArray.forEach((child: string) => {\n let value = 0\n\n if (child.endsWith(\"'\")) {\n value += HARDENED\n child = child.slice(0, -1)\n }\n\n const numChild = Number(child)\n\n if (Number.isNaN(numChild)) {\n throw new ResponseError(LedgerError.GenericError, `Invalid path : ${child} is not a number. (e.g \"m/44'/461'/5'/0/3\")`)\n }\n value += numChild\n buf.appendUint32(value)\n })\n\n return buf.getCompleteBuffer()\n }\n\n async publicKey(path: string): Promise<ResponsePubkey> {\n const serializedPath = this.serializeCosmosPath(path)\n const data = Buffer.concat([this.serializeHRP('cosmos'), serializedPath])\n\n try {\n const responseBuffer = await this.transport.send(this.CLA, this.INS.GET_ADDR_SECP256K1, 0, 0, data)\n const response = processResponse(responseBuffer)\n const compressed_pk = Buffer.from(response.readBytes(PKLEN))\n const bech32_address = response.readBytes(response.length()).toString()\n\n return { compressed_pk, bech32_address }\n } catch (e) {\n throw processErrorResponse(e, COSMOS_CUSTOM_ERROR_DESCRIPTIONS)\n }\n }\n\n async getAddressAndPubKey(path: string, hrp: string): Promise<ResponseAddress> {\n const serializedPath = this.serializeCosmosPath(path)\n const data = Buffer.concat([this.serializeHRP(hrp), serializedPath])\n\n try {\n const responseBuffer = await this.transport.send(this.CLA, this.INS.GET_ADDR_SECP256K1, P1_VALUES.ONLY_RETRIEVE, 0, data)\n\n const response = processResponse(responseBuffer)\n const compressed_pk = response.readBytes(PKLEN)\n const bech32_address = response.readBytes(response.length()).toString()\n\n return { compressed_pk, bech32_address }\n } catch (e) {\n throw processErrorResponse(e, COSMOS_CUSTOM_ERROR_DESCRIPTIONS)\n }\n }\n\n async showAddressAndPubKey(path: string, hrp: string): Promise<ResponseAddress> {\n const serializedPath = this.serializeCosmosPath(path)\n const data = Buffer.concat([this.serializeHRP(hrp), serializedPath])\n\n try {\n const responseBuffer = await this.transport.send(this.CLA, this.INS.GET_ADDR_SECP256K1, P1_VALUES.SHOW_ADDRESS_IN_DEVICE, 0, data)\n\n const response = processResponse(responseBuffer)\n const compressed_pk = response.readBytes(PKLEN)\n const bech32_address = response.readBytes(response.length()).toString()\n\n return { compressed_pk, bech32_address }\n } catch (e) {\n throw processErrorResponse(e, COSMOS_CUSTOM_ERROR_DESCRIPTIONS)\n }\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 = sha256(pk)\n const hashRip = ripemd160(hashSha256)\n return bech32.encode(hrp, bech32.toWords(hashRip))\n }\n\n async prepareChunks_hrp(path: string, buffer: Buffer, hrp?: string) {\n const serializedPath = this.serializeCosmosPath(path)\n const firstChunk = hrp ? Buffer.concat([serializedPath, this.serializeHRP(hrp)]) : serializedPath\n\n const chunks = [firstChunk]\n\n for (let i = 0; i < buffer.length; i += this.CHUNK_SIZE) {\n const end = Math.min(i + this.CHUNK_SIZE, buffer.length)\n chunks.push(buffer.subarray(i, end))\n }\n\n return chunks\n }\n\n async signSendCosmosChunk(chunkIdx: number, chunkNum: number, chunk: Buffer, txtype = P2_VALUES.JSON): Promise<ResponsePayload> {\n const payloadType = chunkIdx === 1 ? PAYLOAD_TYPE.INIT : chunkIdx === chunkNum ? PAYLOAD_TYPE.LAST : PAYLOAD_TYPE.ADD\n\n const statusList = [LedgerError.NoErrors, LedgerError.DataIsInvalid, LedgerError.BadKeyHandle]\n\n const responseBuffer = await this.transport.send(this.CLA, this.INS.SIGN_SECP256K1, payloadType, txtype, chunk, statusList)\n return processResponse(responseBuffer)\n }\n\n private async signImpl(path: string, buffer: Buffer, txtype: number, hrp?: string): Promise<ResponseSign> {\n const chunks = await this.prepareChunks_hrp(path, buffer, hrp)\n\n try {\n let result = await this.signSendCosmosChunk(1, chunks.length, chunks[0], txtype)\n\n for (let i = 1; i < chunks.length; i++) {\n result = await this.signSendCosmosChunk(i + 1, chunks.length, chunks[i], txtype)\n }\n\n return { signature: result.readBytes(result.length()) }\n } catch (e) {\n throw processErrorResponse(e, COSMOS_CUSTOM_ERROR_DESCRIPTIONS)\n }\n }\n async sign(path: string, buffer: Buffer, hrp?: string, txtype = P2_VALUES.JSON): Promise<ResponseSign> {\n return this.signImpl(path, buffer, txtype, hrp)\n }\n}\n"]}
package/dist/types.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"","sourcesContent":["export interface ResponseAddress {\n bech32_address: string;\n compressed_pk: Buffer;\n}\n\nexport interface ResponsePubkey {\n bech32_address: string;\n compressed_pk: Buffer;\n}\n\nexport interface ResponseSign {\n signature: Buffer;\n}\n"]}
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"","sourcesContent":["export interface ResponseAddress {\n bech32_address: string\n compressed_pk: Buffer\n}\n\nexport interface ResponsePubkey {\n bech32_address: string\n compressed_pk: Buffer\n}\n\nexport interface ResponseSign {\n signature: Buffer\n}\n"]}
package/package.json CHANGED
@@ -1,98 +1,81 @@
1
1
  {
2
2
  "name": "@zondax/ledger-cosmos-js",
3
- "author": "Zondax AG",
4
- "license": "Apache-2.0",
5
- "version": "4.0.0",
3
+ "version": "4.1.0",
6
4
  "description": "Node API for Cosmos App (Ledger Nano S/S+/X)",
7
- "main": "./dist/index.js",
8
- "typings": "./dist/index.d.ts",
9
- "types": "./dist/index.d.ts",
10
- "repository": {
11
- "type": "git",
12
- "url": "git+https://github.com/cosmos/ledger-cosmos-js.git"
13
- },
14
5
  "keywords": [
15
6
  "Ledger",
16
7
  "Typescript",
17
8
  "Tendermint",
18
9
  "Cosmos"
19
10
  ],
11
+ "homepage": "https://github.com/Zondax/ledger-cosmos-js",
20
12
  "bugs": {
21
- "url": "https://github.com/cosmos/ledger-cosmos-js/issues"
13
+ "url": "https://github.com/Zondax/ledger-cosmos-js/issues"
14
+ },
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+https://github.com/Zondax/ledger-cosmos-js.git"
22
18
  },
23
- "homepage": "https://github.com/cosmos/ledger-cosmos-js",
24
- "resolutions": {
25
- "@ledgerhq/hw-transport": "6.27.1",
26
- "@ledgerhq/devices": "6.27.1",
27
- "@ledgerhq/hw-transport-node-hid": "6.27.1",
28
- "@ledgerhq/hw-transport-node-hid-noevents": "6.27.1"
19
+ "license": "Apache-2.0",
20
+ "author": "Zondax AG",
21
+ "main": "./dist/index.js",
22
+ "types": "./dist/index.d.ts",
23
+ "typings": "./dist/index.d.ts",
24
+ "files": [
25
+ "dist/**",
26
+ "LICENSE",
27
+ "package.json"
28
+ ],
29
+ "scripts": {
30
+ "build": "tsc",
31
+ "format": "FORCE_COLOR=1 prettier --write . && sort-package-json",
32
+ "format:check": "FORCE_COLOR=1 prettier --check .",
33
+ "lint": "eslint .",
34
+ "lint:fix": "eslint --fix .",
35
+ "test": "pnpm build && jest .",
36
+ "upgrade": "pnpm dlx npm-check-updates -i"
29
37
  },
30
38
  "dependencies": {
31
- "@ledgerhq/hw-transport": "6.30.6",
32
- "@zondax/ledger-js": "^0.10.0",
33
- "bech32": "^2.0.0",
39
+ "@noble/hashes": "^1.8.0",
40
+ "@scure/base": "^1.2.6",
41
+ "@zondax/ledger-js": "^2.0.0",
34
42
  "buffer": "^6.0.3",
35
- "crypto": "^1.0.1",
36
- "crypto-browserify": "^3.12.0",
37
- "ripemd160": "^2.0.2",
38
- "stream-browserify": "^3.0.0"
43
+ "ripemd160": "^2.0.2"
39
44
  },
40
45
  "devDependencies": {
41
- "@ledgerhq/hw-transport-node-hid": "6.28.6",
42
- "@ledgerhq/hw-transport-webusb": "6.28.6",
43
- "@ledgerhq/hw-transport-mocker": "^6.28.6",
44
- "@swc/core": "^1.6.1",
45
- "@types/jest": "^29.5.12",
46
- "@types/ledgerhq__hw-transport": "4.21.8",
47
- "@types/node": "^20.14.2",
48
- "@typescript-eslint/eslint-plugin": "^7.13.0",
49
- "@typescript-eslint/parser": "^7.13.0",
50
- "bip32": "4.0.0",
46
+ "@ledgerhq/hw-transport-mocker": "^6.29.9",
47
+ "@ledgerhq/hw-transport-node-hid": "^6.29.10",
48
+ "@noble/curves": "^1.9.7",
49
+ "@trivago/prettier-plugin-sort-imports": "^5.2.2",
50
+ "@types/jest": "^30.0.0",
51
+ "@types/node": "^24.3.0",
52
+ "@typescript-eslint/eslint-plugin": "^8.40.0",
53
+ "@typescript-eslint/parser": "^8.40.0",
54
+ "bip32": "5.0.0-rc.0",
51
55
  "bip39": "3.1.0",
52
- "copyfiles": "^2.4.1",
53
- "core-js": "^3.37.1",
54
- "crypto-js": "4.2.0",
55
- "eslint": "^9.5.0",
56
- "eslint-config-prettier": "^9.1.0",
57
- "eslint-plugin-import": "^2.29.1",
58
- "eslint-plugin-jest": "^28.6.0",
59
- "eslint-plugin-prettier": "^5.1.3",
60
- "eslint-plugin-unused-imports": "^3.2.0",
61
- "eslint-plugin-tsdoc": "^0.2.17",
62
- "jest": "^29.7.0",
63
- "jest-serial-runner": "^1.2.0",
64
- "prettier": "^3.3.2",
65
- "secp256k1": "^5.0.0",
66
- "ts-jest": "29.1.5",
56
+ "eslint": "^9.33.0",
57
+ "eslint-config-prettier": "^10.1.8",
58
+ "eslint-plugin-import": "^2.32.0",
59
+ "eslint-plugin-jest": "^29.0.1",
60
+ "eslint-plugin-tsdoc": "^0.4.0",
61
+ "eslint-plugin-unused-imports": "^4.2.0",
62
+ "jest": "^30.0.5",
63
+ "jest-runner": "^30.0.5",
64
+ "jest-serial-runner": "^1.2.2",
65
+ "prettier": "^3.6.2",
66
+ "sort-package-json": "^3.4.0",
67
+ "ts-jest": "29.4.1",
67
68
  "ts-node": "^10.9.2",
68
- "typescript": "^5.4.5",
69
- "webpack": "^5.92.0"
69
+ "typescript": "^5.9.2"
70
+ },
71
+ "volta": {
72
+ "node": "20.11.1"
70
73
  },
71
- "moduleDirectories": [
72
- "node_modules",
73
- "dist"
74
- ],
75
- "browserslist": [
76
- "> 1%",
77
- "last 2 versions"
78
- ],
79
- "files": [
80
- "dist/*",
81
- "LICENSE",
82
- "yarn.lock"
83
- ],
84
74
  "publishConfig": {
85
75
  "access": "public"
86
76
  },
87
- "peerDependencies": {
88
- "@babel/core": "^7.17.5",
89
- "@types/node": "^17.0.21"
90
- },
91
- "scripts": {
92
- "build": "tsc",
93
- "lint": "eslint .",
94
- "lint:fix": "eslint --fix .",
95
- "format": "prettier --write .",
96
- "test": "yarn build && jest"
97
- }
77
+ "moduleDirectories": [
78
+ "node_modules",
79
+ "dist"
80
+ ]
98
81
  }