@ledgerhq/hw-app-icon 1.1.0-next.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,4 @@
1
+
2
+ > @ledgerhq/hw-app-icon@1.0.0 build /home/runner/work/ledger-live/ledger-live/libs/ledgerjs/packages/hw-app-icon
3
+ > tsc && tsc -m ES6 --outDir lib-es
4
+
package/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ # @ledgerhq/hw-app-icon
2
+
3
+ ## 1.1.0-next.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#7075](https://github.com/LedgerHQ/ledger-live/pull/7075) [`db9c2d7`](https://github.com/LedgerHQ/ledger-live/commit/db9c2d78fb74df586c3ea1b9fb75ce3b014a0f4b) Thanks [@hedi-edelbloute](https://github.com/hedi-edelbloute)! - Integrate Sync, Send, Receive, Create Account for Icon network
package/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2017-present Ledger https://www.ledger.com/
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,82 @@
1
+ ## @ledgerhq/hw-app-icon
2
+
3
+ Ledger Hardware Wallet Icon JavaScript bindings.
4
+
5
+ ## API
6
+
7
+ <!-- Generated by documentation.js. Update this documentation by updating the source code. -->
8
+
9
+ #### Table of Contents
10
+
11
+ * [Icx](#icx)
12
+ * [Parameters](#parameters)
13
+ * [Examples](#examples)
14
+ * [getAddress](#getaddress)
15
+ * [Parameters](#parameters-1)
16
+ * [Examples](#examples-1)
17
+ * [signTransaction](#signtransaction)
18
+ * [Parameters](#parameters-2)
19
+ * [Examples](#examples-2)
20
+ * [getAppConfiguration](#getappconfiguration)
21
+
22
+ ### Icx
23
+
24
+ ICON API
25
+
26
+ #### Parameters
27
+
28
+ * `transport` **Transport**&#x20;
29
+
30
+ #### Examples
31
+
32
+ ```javascript
33
+ import Icx from "@ledgerhq/hw-app-icx";
34
+ const icx = new Icx(transport)
35
+ ```
36
+
37
+ #### getAddress
38
+
39
+ Returns public key and ICON address for a given BIP 32 path.
40
+
41
+ ##### Parameters
42
+
43
+ * `path` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** a path in BIP 32 format
44
+ * `boolDisplay` (optional, default `false`)
45
+ * `boolChaincode` (optional, default `true`)
46
+
47
+ ##### Examples
48
+
49
+ ```javascript
50
+ icx.getAddress("44'/4801368'/0'", true, true).then(o => o.address)
51
+ ```
52
+
53
+ Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)<{publicKey: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String), address: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String), chainCode: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?}>** an object with a publickey(hexa string), address(string) and
54
+ (optionally) chaincode(hexa string)
55
+
56
+ #### signTransaction
57
+
58
+ Signs a transaction and returns signed message given the raw transaction
59
+ and the BIP 32 path of the account to sign
60
+
61
+ ##### Parameters
62
+
63
+ * `path` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** a path in BIP 32 format
64
+ * `rawTxAscii` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** raw transaction data to sign in ASCII string format
65
+
66
+ ##### Examples
67
+
68
+ ```javascript
69
+ icx.signTransaction("44'/4801368'/0'",
70
+ "icx_sendTransaction.fee.0x2386f26fc10000." +
71
+ "from.hxc9ecad30b05a0650a337452fce031e0c60eacc3a.nonce.0x3." +
72
+ "to.hx4c5101add2caa6a920420cf951f7dd7c7df6ca24.value.0xde0b6b3a7640000")
73
+ .then(result => ...)
74
+ ```
75
+
76
+ Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)<{signedRawTxBase64: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String), hashHex: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)}>** an object with a base64 encoded signature and hash in hexa string
77
+
78
+ #### getAppConfiguration
79
+
80
+ Returns the application configurations such as versions.
81
+
82
+ Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)<{majorVersion: [number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number), minorVersion: [number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number), patchVersion: [number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)}>** major/minor/patch versions of Icon application
package/jest.config.ts ADDED
@@ -0,0 +1,6 @@
1
+ import baseConfig from "../../jest.config";
2
+
3
+ export default {
4
+ ...baseConfig,
5
+ rootDir: __dirname,
6
+ };
package/lib/Icon.d.ts ADDED
@@ -0,0 +1,70 @@
1
+ /********************************************************************************
2
+ * Ledger JS API for ICON
3
+ * (c) 2018 ICON Foundation
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ ********************************************************************************/
17
+ import type Transport from "@ledgerhq/hw-transport";
18
+ /**
19
+ * ICON API
20
+ *
21
+ * @example
22
+ * import Icx from "@ledgerhq/hw-app-icx";
23
+ * const icx = new Icx(transport)
24
+ */
25
+ export default class Icx {
26
+ transport: Transport;
27
+ constructor(transport: Transport);
28
+ /**
29
+ * Returns public key and ICON address for a given BIP 32 path.
30
+ * @param path a path in BIP 32 format
31
+ * @option boolDisplay optionally enable or not the display
32
+ * @option boolChaincode optionally enable or not the chaincode request
33
+ * @return an object with a publickey(hexa string), address(string) and
34
+ * (optionally) chaincode(hexa string)
35
+ * @example
36
+ * icx.getAddress("44'/4801368'/0'", true, true).then(o => o.address)
37
+ */
38
+ getAddress(path: string, boolDisplay?: boolean, boolChaincode?: boolean): Promise<{
39
+ publicKey: string;
40
+ address: string;
41
+ chainCode?: string;
42
+ }>;
43
+ /**
44
+ * Signs a transaction and returns signed message given the raw transaction
45
+ * and the BIP 32 path of the account to sign
46
+ * @param path a path in BIP 32 format
47
+ * @param rawTxAscii raw transaction data to sign in ASCII string format
48
+ * @return an object with a base64 encoded signature and hash in hexa string
49
+ * @example
50
+ * icx.signTransaction("44'/4801368'/0'",
51
+ * "icx_sendTransaction.fee.0x2386f26fc10000." +
52
+ * "from.hxc9ecad30b05a0650a337452fce031e0c60eacc3a.nonce.0x3." +
53
+ * "to.hx4c5101add2caa6a920420cf951f7dd7c7df6ca24.value.0xde0b6b3a7640000")
54
+ * .then(result => ...)
55
+ */
56
+ signTransaction(path: string, rawTxAscii: string): Promise<{
57
+ signedRawTxBase64: string;
58
+ hashHex: string;
59
+ }>;
60
+ /**
61
+ * Returns the application configurations such as versions.
62
+ * @return major/minor/patch versions of Icon application
63
+ */
64
+ getAppConfiguration(): Promise<{
65
+ majorVersion: number;
66
+ minorVersion: number;
67
+ patchVersion: number;
68
+ }>;
69
+ }
70
+ //# sourceMappingURL=Icon.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Icon.d.ts","sourceRoot":"","sources":["../src/Icon.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;kFAekF;AAGlF,OAAO,KAAK,SAAS,MAAM,wBAAwB,CAAC;AAapD;;;;;;GAMG;AACH,MAAM,CAAC,OAAO,OAAO,GAAG;IACtB,SAAS,EAAE,SAAS,CAAC;gBAET,SAAS,EAAE,SAAS;IAShC;;;;;;;;;OASG;IACH,UAAU,CACR,IAAI,EAAE,MAAM,EACZ,WAAW,UAAQ,EACnB,aAAa,UAAO,GACnB,OAAO,CAAC;QACT,SAAS,EAAE,MAAM,CAAC;QAClB,OAAO,EAAE,MAAM,CAAC;QAChB,SAAS,CAAC,EAAE,MAAM,CAAC;KACpB,CAAC;IAoCF;;;;;;;;;;;;OAYG;IACH,eAAe,CACb,IAAI,EAAE,MAAM,EACZ,UAAU,EAAE,MAAM,GACjB,OAAO,CAAC;QACT,iBAAiB,EAAE,MAAM,CAAC;QAC1B,OAAO,EAAE,MAAM,CAAC;KACjB,CAAC;IA4CF;;;OAGG;IACH,mBAAmB,IAAI,OAAO,CAAC;QAC7B,YAAY,EAAE,MAAM,CAAC;QACrB,YAAY,EAAE,MAAM,CAAC;QACrB,YAAY,EAAE,MAAM,CAAC;KACtB,CAAC;CASH"}
package/lib/Icon.js ADDED
@@ -0,0 +1,147 @@
1
+ "use strict";
2
+ /********************************************************************************
3
+ * Ledger JS API for ICON
4
+ * (c) 2018 ICON Foundation
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ ********************************************************************************/
18
+ Object.defineProperty(exports, "__esModule", { value: true });
19
+ const utils_1 = require("./utils");
20
+ const errors_1 = require("@ledgerhq/errors");
21
+ const CLA = 0xe0;
22
+ const INS = {
23
+ GET_VERSION: 0x06,
24
+ GET_ADDR: 0x02,
25
+ SIGN: 0x04,
26
+ };
27
+ const SW_OK = 0x9000;
28
+ const SW_CANCEL = 0x6986;
29
+ /**
30
+ * ICON API
31
+ *
32
+ * @example
33
+ * import Icx from "@ledgerhq/hw-app-icx";
34
+ * const icx = new Icx(transport)
35
+ */
36
+ class Icx {
37
+ constructor(transport) {
38
+ this.transport = transport;
39
+ transport.decorateAppAPIMethods(this, ["getAddress", "signTransaction", "getAppConfiguration"], "ICON");
40
+ }
41
+ /**
42
+ * Returns public key and ICON address for a given BIP 32 path.
43
+ * @param path a path in BIP 32 format
44
+ * @option boolDisplay optionally enable or not the display
45
+ * @option boolChaincode optionally enable or not the chaincode request
46
+ * @return an object with a publickey(hexa string), address(string) and
47
+ * (optionally) chaincode(hexa string)
48
+ * @example
49
+ * icx.getAddress("44'/4801368'/0'", true, true).then(o => o.address)
50
+ */
51
+ getAddress(path, boolDisplay = false, boolChaincode = true) {
52
+ const paths = (0, utils_1.splitPath)(path);
53
+ const buffer = Buffer.alloc(1 + paths.length * 4);
54
+ buffer[0] = paths.length;
55
+ paths.forEach((element, index) => {
56
+ buffer.writeUInt32BE(element, 1 + 4 * index);
57
+ });
58
+ const statusList = [SW_OK, SW_CANCEL];
59
+ return this.transport
60
+ .send(CLA, INS.GET_ADDR, boolDisplay ? 0x01 : 0x00, boolChaincode ? 0x01 : 0x00, buffer, statusList)
61
+ .then(response => {
62
+ const publicKeyLength = response[0];
63
+ const addressLength = response[1 + publicKeyLength];
64
+ const errorCodeData = response.slice(-2);
65
+ const returnCode = errorCodeData[0] * 0x100 + errorCodeData[1];
66
+ if (returnCode === SW_CANCEL) {
67
+ throw new errors_1.UserRefusedAddress();
68
+ }
69
+ return {
70
+ publicKey: response.slice(1, 1 + publicKeyLength).toString("hex"),
71
+ address: response
72
+ .slice(1 + publicKeyLength + 1, 1 + publicKeyLength + 1 + addressLength)
73
+ .toString(),
74
+ chainCode: boolChaincode ? response.slice(-32).toString("hex") : undefined,
75
+ };
76
+ });
77
+ }
78
+ /**
79
+ * Signs a transaction and returns signed message given the raw transaction
80
+ * and the BIP 32 path of the account to sign
81
+ * @param path a path in BIP 32 format
82
+ * @param rawTxAscii raw transaction data to sign in ASCII string format
83
+ * @return an object with a base64 encoded signature and hash in hexa string
84
+ * @example
85
+ * icx.signTransaction("44'/4801368'/0'",
86
+ * "icx_sendTransaction.fee.0x2386f26fc10000." +
87
+ * "from.hxc9ecad30b05a0650a337452fce031e0c60eacc3a.nonce.0x3." +
88
+ * "to.hx4c5101add2caa6a920420cf951f7dd7c7df6ca24.value.0xde0b6b3a7640000")
89
+ * .then(result => ...)
90
+ */
91
+ signTransaction(path, rawTxAscii) {
92
+ const paths = (0, utils_1.splitPath)(path);
93
+ let offset = 0;
94
+ const rawTx = Buffer.from(rawTxAscii);
95
+ const toSend = [];
96
+ let response;
97
+ while (offset !== rawTx.length) {
98
+ const maxChunkSize = offset === 0 ? 150 - 1 - paths.length * 4 - 4 : 150;
99
+ const chunkSize = offset + maxChunkSize > rawTx.length ? rawTx.length - offset : maxChunkSize;
100
+ const buffer = Buffer.alloc(offset === 0 ? 1 + paths.length * 4 + 4 + chunkSize : chunkSize);
101
+ if (offset === 0) {
102
+ buffer[0] = paths.length;
103
+ paths.forEach((element, index) => {
104
+ buffer.writeUInt32BE(element, 1 + 4 * index);
105
+ });
106
+ buffer.writeUInt32BE(rawTx.length, 1 + 4 * paths.length);
107
+ rawTx.copy(buffer, 1 + 4 * paths.length + 4, offset, offset + chunkSize);
108
+ }
109
+ else {
110
+ rawTx.copy(buffer, 0, offset, offset + chunkSize);
111
+ }
112
+ toSend.push(buffer);
113
+ offset += chunkSize;
114
+ }
115
+ return (0, utils_1.foreach)(toSend, (data, i) => this.transport
116
+ .send(CLA, INS.SIGN, i === 0 ? 0x00 : 0x80, 0x00, data, [SW_OK, SW_CANCEL])
117
+ .then(apduResponse => {
118
+ response = apduResponse;
119
+ })).then(() => {
120
+ const errorCodeData = response.slice(-2);
121
+ const returnCode = errorCodeData[0] * 0x100 + errorCodeData[1];
122
+ if (returnCode === SW_CANCEL) {
123
+ throw new errors_1.UserRefusedOnDevice();
124
+ }
125
+ // r, s, v are aligned sequencially
126
+ return {
127
+ signedRawTxBase64: (0, utils_1.hexToBase64)(response.slice(0, 32 + 32 + 1).toString("hex")),
128
+ hashHex: response.slice(32 + 32 + 1, 32 + 32 + 1 + 32).toString("hex"),
129
+ };
130
+ });
131
+ }
132
+ /**
133
+ * Returns the application configurations such as versions.
134
+ * @return major/minor/patch versions of Icon application
135
+ */
136
+ getAppConfiguration() {
137
+ return this.transport.send(CLA, INS.GET_VERSION, 0x00, 0x00).then(response => {
138
+ return {
139
+ majorVersion: response[0],
140
+ minorVersion: response[1],
141
+ patchVersion: response[2],
142
+ };
143
+ });
144
+ }
145
+ }
146
+ exports.default = Icx;
147
+ //# sourceMappingURL=Icon.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Icon.js","sourceRoot":"","sources":["../src/Icon.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;kFAekF;;AAElF,mCAA0D;AAE1D,6CAA2E;AAE3E,MAAM,GAAG,GAAG,IAAI,CAAC;AACjB,MAAM,GAAG,GAAG;IACV,WAAW,EAAE,IAAI;IACjB,QAAQ,EAAE,IAAI;IACd,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,EAAE,QAAQ,CAAC,CAAC,CAAC;aAC1B,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AAlJD,sBAkJC"}
package/lib/utils.d.ts ADDED
@@ -0,0 +1,22 @@
1
+ /********************************************************************************
2
+ * Ledger Node JS API for ICON
3
+ * (c) 2016-2017 Ledger
4
+ *
5
+ * Modifications (c) 2018 ICON Foundation
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ ********************************************************************************/
19
+ export declare function splitPath(path: string): number[];
20
+ export declare function foreach<T, A>(arr: T[], callback: (T: any, number: any) => Promise<A>): Promise<A[]>;
21
+ export declare function hexToBase64(hexString: string): string;
22
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;kFAiBkF;AAGlF,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,CAchD;AAED,wBAAgB,OAAO,CAAC,CAAC,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,EAAE,QAAQ,EAAE,CAAC,CAAC,KAAA,EAAE,MAAM,KAAA,KAAK,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,EAAE,CAAC,CAWzF;AAED,wBAAgB,WAAW,CAAC,SAAS,EAAE,MAAM,UAQ5C"}
package/lib/utils.js ADDED
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ /********************************************************************************
3
+ * Ledger Node JS API for ICON
4
+ * (c) 2016-2017 Ledger
5
+ *
6
+ * Modifications (c) 2018 ICON Foundation
7
+ *
8
+ * Licensed under the Apache License, Version 2.0 (the "License");
9
+ * you may not use this file except in compliance with the License.
10
+ * You may obtain a copy of the License at
11
+ *
12
+ * http://www.apache.org/licenses/LICENSE-2.0
13
+ *
14
+ * Unless required by applicable law or agreed to in writing, software
15
+ * distributed under the License is distributed on an "AS IS" BASIS,
16
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ * See the License for the specific language governing permissions and
18
+ * limitations under the License.
19
+ ********************************************************************************/
20
+ Object.defineProperty(exports, "__esModule", { value: true });
21
+ exports.hexToBase64 = exports.foreach = exports.splitPath = void 0;
22
+ // TODO use bip32-path library
23
+ function splitPath(path) {
24
+ const result = [];
25
+ const components = path.split("/");
26
+ components.forEach(element => {
27
+ let number = parseInt(element, 10);
28
+ if (isNaN(number)) {
29
+ return; // FIXME shouldn't it throws instead?
30
+ }
31
+ if (element.length > 1 && element[element.length - 1] === "'") {
32
+ number += 0x80000000;
33
+ }
34
+ result.push(number);
35
+ });
36
+ return result;
37
+ }
38
+ exports.splitPath = splitPath;
39
+ function foreach(arr, callback) {
40
+ function iterate(index, array, result) {
41
+ if (index >= array.length) {
42
+ return result;
43
+ }
44
+ else
45
+ return callback(array[index], index).then(function (res) {
46
+ result.push(res);
47
+ return iterate(index + 1, array, result);
48
+ });
49
+ }
50
+ return Promise.resolve().then(() => iterate(0, arr, []));
51
+ }
52
+ exports.foreach = foreach;
53
+ function hexToBase64(hexString) {
54
+ return btoa((hexString.match(/\w{2}/g) || [])
55
+ .map(function (a) {
56
+ return String.fromCharCode(parseInt(a, 16));
57
+ })
58
+ .join(""));
59
+ }
60
+ exports.hexToBase64 = hexToBase64;
61
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;k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AC3D,CAAC;AAXD,0BAWC;AAED,SAAgB,WAAW,CAAC,SAAiB;IAC3C,OAAO,IAAI,CACT,CAAC,SAAS,CAAC,KAAK,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAC;SAC9B,GAAG,CAAC,UAAU,CAAC;QACd,OAAO,MAAM,CAAC,YAAY,CAAC,QAAQ,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC;IAC9C,CAAC,CAAC;SACD,IAAI,CAAC,EAAE,CAAC,CACZ,CAAC;AACJ,CAAC;AARD,kCAQC"}
@@ -0,0 +1,70 @@
1
+ /********************************************************************************
2
+ * Ledger JS API for ICON
3
+ * (c) 2018 ICON Foundation
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ ********************************************************************************/
17
+ import type Transport from "@ledgerhq/hw-transport";
18
+ /**
19
+ * ICON API
20
+ *
21
+ * @example
22
+ * import Icx from "@ledgerhq/hw-app-icx";
23
+ * const icx = new Icx(transport)
24
+ */
25
+ export default class Icx {
26
+ transport: Transport;
27
+ constructor(transport: Transport);
28
+ /**
29
+ * Returns public key and ICON address for a given BIP 32 path.
30
+ * @param path a path in BIP 32 format
31
+ * @option boolDisplay optionally enable or not the display
32
+ * @option boolChaincode optionally enable or not the chaincode request
33
+ * @return an object with a publickey(hexa string), address(string) and
34
+ * (optionally) chaincode(hexa string)
35
+ * @example
36
+ * icx.getAddress("44'/4801368'/0'", true, true).then(o => o.address)
37
+ */
38
+ getAddress(path: string, boolDisplay?: boolean, boolChaincode?: boolean): Promise<{
39
+ publicKey: string;
40
+ address: string;
41
+ chainCode?: string;
42
+ }>;
43
+ /**
44
+ * Signs a transaction and returns signed message given the raw transaction
45
+ * and the BIP 32 path of the account to sign
46
+ * @param path a path in BIP 32 format
47
+ * @param rawTxAscii raw transaction data to sign in ASCII string format
48
+ * @return an object with a base64 encoded signature and hash in hexa string
49
+ * @example
50
+ * icx.signTransaction("44'/4801368'/0'",
51
+ * "icx_sendTransaction.fee.0x2386f26fc10000." +
52
+ * "from.hxc9ecad30b05a0650a337452fce031e0c60eacc3a.nonce.0x3." +
53
+ * "to.hx4c5101add2caa6a920420cf951f7dd7c7df6ca24.value.0xde0b6b3a7640000")
54
+ * .then(result => ...)
55
+ */
56
+ signTransaction(path: string, rawTxAscii: string): Promise<{
57
+ signedRawTxBase64: string;
58
+ hashHex: string;
59
+ }>;
60
+ /**
61
+ * Returns the application configurations such as versions.
62
+ * @return major/minor/patch versions of Icon application
63
+ */
64
+ getAppConfiguration(): Promise<{
65
+ majorVersion: number;
66
+ minorVersion: number;
67
+ patchVersion: number;
68
+ }>;
69
+ }
70
+ //# sourceMappingURL=Icon.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Icon.d.ts","sourceRoot":"","sources":["../src/Icon.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;kFAekF;AAGlF,OAAO,KAAK,SAAS,MAAM,wBAAwB,CAAC;AAapD;;;;;;GAMG;AACH,MAAM,CAAC,OAAO,OAAO,GAAG;IACtB,SAAS,EAAE,SAAS,CAAC;gBAET,SAAS,EAAE,SAAS;IAShC;;;;;;;;;OASG;IACH,UAAU,CACR,IAAI,EAAE,MAAM,EACZ,WAAW,UAAQ,EACnB,aAAa,UAAO,GACnB,OAAO,CAAC;QACT,SAAS,EAAE,MAAM,CAAC;QAClB,OAAO,EAAE,MAAM,CAAC;QAChB,SAAS,CAAC,EAAE,MAAM,CAAC;KACpB,CAAC;IAoCF;;;;;;;;;;;;OAYG;IACH,eAAe,CACb,IAAI,EAAE,MAAM,EACZ,UAAU,EAAE,MAAM,GACjB,OAAO,CAAC;QACT,iBAAiB,EAAE,MAAM,CAAC;QAC1B,OAAO,EAAE,MAAM,CAAC;KACjB,CAAC;IA4CF;;;OAGG;IACH,mBAAmB,IAAI,OAAO,CAAC;QAC7B,YAAY,EAAE,MAAM,CAAC;QACrB,YAAY,EAAE,MAAM,CAAC;QACrB,YAAY,EAAE,MAAM,CAAC;KACtB,CAAC;CASH"}
package/lib-es/Icon.js ADDED
@@ -0,0 +1,144 @@
1
+ /********************************************************************************
2
+ * Ledger JS API for ICON
3
+ * (c) 2018 ICON Foundation
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ ********************************************************************************/
17
+ import { splitPath, foreach, hexToBase64 } from "./utils";
18
+ import { UserRefusedOnDevice, UserRefusedAddress } from "@ledgerhq/errors";
19
+ const CLA = 0xe0;
20
+ const INS = {
21
+ GET_VERSION: 0x06,
22
+ GET_ADDR: 0x02,
23
+ SIGN: 0x04,
24
+ };
25
+ const SW_OK = 0x9000;
26
+ const SW_CANCEL = 0x6986;
27
+ /**
28
+ * ICON API
29
+ *
30
+ * @example
31
+ * import Icx from "@ledgerhq/hw-app-icx";
32
+ * const icx = new Icx(transport)
33
+ */
34
+ export default class Icx {
35
+ constructor(transport) {
36
+ this.transport = transport;
37
+ transport.decorateAppAPIMethods(this, ["getAddress", "signTransaction", "getAppConfiguration"], "ICON");
38
+ }
39
+ /**
40
+ * Returns public key and ICON address for a given BIP 32 path.
41
+ * @param path a path in BIP 32 format
42
+ * @option boolDisplay optionally enable or not the display
43
+ * @option boolChaincode optionally enable or not the chaincode request
44
+ * @return an object with a publickey(hexa string), address(string) and
45
+ * (optionally) chaincode(hexa string)
46
+ * @example
47
+ * icx.getAddress("44'/4801368'/0'", true, true).then(o => o.address)
48
+ */
49
+ getAddress(path, boolDisplay = false, boolChaincode = true) {
50
+ const paths = splitPath(path);
51
+ const buffer = Buffer.alloc(1 + paths.length * 4);
52
+ buffer[0] = paths.length;
53
+ paths.forEach((element, index) => {
54
+ buffer.writeUInt32BE(element, 1 + 4 * index);
55
+ });
56
+ const statusList = [SW_OK, SW_CANCEL];
57
+ return this.transport
58
+ .send(CLA, INS.GET_ADDR, boolDisplay ? 0x01 : 0x00, boolChaincode ? 0x01 : 0x00, buffer, statusList)
59
+ .then(response => {
60
+ const publicKeyLength = response[0];
61
+ const addressLength = response[1 + publicKeyLength];
62
+ const errorCodeData = response.slice(-2);
63
+ const returnCode = errorCodeData[0] * 0x100 + errorCodeData[1];
64
+ if (returnCode === SW_CANCEL) {
65
+ throw new UserRefusedAddress();
66
+ }
67
+ return {
68
+ publicKey: response.slice(1, 1 + publicKeyLength).toString("hex"),
69
+ address: response
70
+ .slice(1 + publicKeyLength + 1, 1 + publicKeyLength + 1 + addressLength)
71
+ .toString(),
72
+ chainCode: boolChaincode ? response.slice(-32).toString("hex") : undefined,
73
+ };
74
+ });
75
+ }
76
+ /**
77
+ * Signs a transaction and returns signed message given the raw transaction
78
+ * and the BIP 32 path of the account to sign
79
+ * @param path a path in BIP 32 format
80
+ * @param rawTxAscii raw transaction data to sign in ASCII string format
81
+ * @return an object with a base64 encoded signature and hash in hexa string
82
+ * @example
83
+ * icx.signTransaction("44'/4801368'/0'",
84
+ * "icx_sendTransaction.fee.0x2386f26fc10000." +
85
+ * "from.hxc9ecad30b05a0650a337452fce031e0c60eacc3a.nonce.0x3." +
86
+ * "to.hx4c5101add2caa6a920420cf951f7dd7c7df6ca24.value.0xde0b6b3a7640000")
87
+ * .then(result => ...)
88
+ */
89
+ signTransaction(path, rawTxAscii) {
90
+ const paths = splitPath(path);
91
+ let offset = 0;
92
+ const rawTx = Buffer.from(rawTxAscii);
93
+ const toSend = [];
94
+ let response;
95
+ while (offset !== rawTx.length) {
96
+ const maxChunkSize = offset === 0 ? 150 - 1 - paths.length * 4 - 4 : 150;
97
+ const chunkSize = offset + maxChunkSize > rawTx.length ? rawTx.length - offset : maxChunkSize;
98
+ const buffer = Buffer.alloc(offset === 0 ? 1 + paths.length * 4 + 4 + chunkSize : chunkSize);
99
+ if (offset === 0) {
100
+ buffer[0] = paths.length;
101
+ paths.forEach((element, index) => {
102
+ buffer.writeUInt32BE(element, 1 + 4 * index);
103
+ });
104
+ buffer.writeUInt32BE(rawTx.length, 1 + 4 * paths.length);
105
+ rawTx.copy(buffer, 1 + 4 * paths.length + 4, offset, offset + chunkSize);
106
+ }
107
+ else {
108
+ rawTx.copy(buffer, 0, offset, offset + chunkSize);
109
+ }
110
+ toSend.push(buffer);
111
+ offset += chunkSize;
112
+ }
113
+ return foreach(toSend, (data, i) => this.transport
114
+ .send(CLA, INS.SIGN, i === 0 ? 0x00 : 0x80, 0x00, data, [SW_OK, SW_CANCEL])
115
+ .then(apduResponse => {
116
+ response = apduResponse;
117
+ })).then(() => {
118
+ const errorCodeData = response.slice(-2);
119
+ const returnCode = errorCodeData[0] * 0x100 + errorCodeData[1];
120
+ if (returnCode === SW_CANCEL) {
121
+ throw new UserRefusedOnDevice();
122
+ }
123
+ // r, s, v are aligned sequencially
124
+ return {
125
+ signedRawTxBase64: hexToBase64(response.slice(0, 32 + 32 + 1).toString("hex")),
126
+ hashHex: response.slice(32 + 32 + 1, 32 + 32 + 1 + 32).toString("hex"),
127
+ };
128
+ });
129
+ }
130
+ /**
131
+ * Returns the application configurations such as versions.
132
+ * @return major/minor/patch versions of Icon application
133
+ */
134
+ getAppConfiguration() {
135
+ return this.transport.send(CLA, INS.GET_VERSION, 0x00, 0x00).then(response => {
136
+ return {
137
+ majorVersion: response[0],
138
+ minorVersion: response[1],
139
+ patchVersion: response[2],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E,EAAE,GAAG,EAAE,GAAG,CAAC,GAAG,EAAE,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC;aACvE,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;;OAGG;IACH,mBAAmB;QAKjB,OAAO,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,CAAC,WAAW,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,EAAE;YAC3E,OAAO;gBACL,YAAY,EAAE,QAAQ,CAAC,CAAC,CAAC;gBACzB,YAAY,EAAE,QAAQ,CAAC,CAAC,CAAC;gBACzB,YAAY,EAAE,QAAQ,CAAC,CAAC,CAAC;aAC1B,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC;CACF"}
@@ -0,0 +1,22 @@
1
+ /********************************************************************************
2
+ * Ledger Node JS API for ICON
3
+ * (c) 2016-2017 Ledger
4
+ *
5
+ * Modifications (c) 2018 ICON Foundation
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ ********************************************************************************/
19
+ export declare function splitPath(path: string): number[];
20
+ export declare function foreach<T, A>(arr: T[], callback: (T: any, number: any) => Promise<A>): Promise<A[]>;
21
+ export declare function hexToBase64(hexString: string): string;
22
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;kFAiBkF;AAGlF,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,CAchD;AAED,wBAAgB,OAAO,CAAC,CAAC,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,EAAE,QAAQ,EAAE,CAAC,CAAC,KAAA,EAAE,MAAM,KAAA,KAAK,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,EAAE,CAAC,CAWzF;AAED,wBAAgB,WAAW,CAAC,SAAS,EAAE,MAAM,UAQ5C"}
@@ -0,0 +1,55 @@
1
+ /********************************************************************************
2
+ * Ledger Node JS API for ICON
3
+ * (c) 2016-2017 Ledger
4
+ *
5
+ * Modifications (c) 2018 ICON Foundation
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ ********************************************************************************/
19
+ // TODO use bip32-path library
20
+ export function splitPath(path) {
21
+ const result = [];
22
+ const components = path.split("/");
23
+ components.forEach(element => {
24
+ let number = parseInt(element, 10);
25
+ if (isNaN(number)) {
26
+ return; // FIXME shouldn't it throws instead?
27
+ }
28
+ if (element.length > 1 && element[element.length - 1] === "'") {
29
+ number += 0x80000000;
30
+ }
31
+ result.push(number);
32
+ });
33
+ return result;
34
+ }
35
+ export function foreach(arr, callback) {
36
+ function iterate(index, array, result) {
37
+ if (index >= array.length) {
38
+ return result;
39
+ }
40
+ else
41
+ return callback(array[index], index).then(function (res) {
42
+ result.push(res);
43
+ return iterate(index + 1, array, result);
44
+ });
45
+ }
46
+ return Promise.resolve().then(() => iterate(0, arr, []));
47
+ }
48
+ export function hexToBase64(hexString) {
49
+ return btoa((hexString.match(/\w{2}/g) || [])
50
+ .map(function (a) {
51
+ return String.fromCharCode(parseInt(a, 16));
52
+ })
53
+ .join(""));
54
+ }
55
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;kFAiBkF;AAElF,8BAA8B;AAC9B,MAAM,UAAU,SAAS,CAAC,IAAY;IACpC,MAAM,MAAM,GAAa,EAAE,CAAC;IAC5B,MAAM,UAAU,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IACnC,UAAU,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE;QAC3B,IAAI,MAAM,GAAG,QAAQ,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC;QACnC,IAAI,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC;YAClB,OAAO,CAAC,qCAAqC;QAC/C,CAAC;QACD,IAAI,OAAO,CAAC,MAAM,GAAG,CAAC,IAAI,OAAO,CAAC,OAAO,CAAC,MAAM,GAAG,CAAC,CAAC,KAAK,GAAG,EAAE,CAAC;YAC9D,MAAM,IAAI,UAAU,CAAC;QACvB,CAAC;QACD,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IACtB,CAAC,CAAC,CAAC;IACH,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,MAAM,UAAU,OAAO,CAAO,GAAQ,EAAE,QAAmC;IACzE,SAAS,OAAO,CAAC,KAAK,EAAE,KAAK,EAAE,MAAM;QACnC,IAAI,KAAK,IAAI,KAAK,CAAC,MAAM,EAAE,CAAC;YAC1B,OAAO,MAAM,CAAC;QAChB,CAAC;;YACC,OAAO,QAAQ,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,KAAK,CAAC,CAAC,IAAI,CAAC,UAAU,GAAG;gBACrD,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;gBACjB,OAAO,OAAO,CAAC,KAAK,GAAG,CAAC,EAAE,KAAK,EAAE,MAAM,CAAC,CAAC;YAC3C,CAAC,CAAC,CAAC;IACP,CAAC;IACD,OAAO,OAAO,CAAC,OAAO,EAAE,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,OAAO,CAAC,CAAC,EAAE,GAAG,EAAE,EAAE,CAAC,CAAC,CAAC;AAC3D,CAAC;AAED,MAAM,UAAU,WAAW,CAAC,SAAiB;IAC3C,OAAO,IAAI,CACT,CAAC,SAAS,CAAC,KAAK,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAC;SAC9B,GAAG,CAAC,UAAU,CAAC;QACd,OAAO,MAAM,CAAC,YAAY,CAAC,QAAQ,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC;IAC9C,CAAC,CAAC;SACD,IAAI,CAAC,EAAE,CAAC,CACZ,CAAC;AACJ,CAAC"}
package/package.json ADDED
@@ -0,0 +1,56 @@
1
+ {
2
+ "name": "@ledgerhq/hw-app-icon",
3
+ "version": "1.1.0-next.0",
4
+ "description": "Ledger Hardware Wallet ICON Application API",
5
+ "keywords": [
6
+ "Ledger",
7
+ "LedgerWallet",
8
+ "icx",
9
+ "Icon",
10
+ "NanoS",
11
+ "Blue",
12
+ "Hardware Wallet"
13
+ ],
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "https://github.com/LedgerHQ/ledger-live"
17
+ },
18
+ "bugs": {
19
+ "url": "https://github.com/LedgerHQ/ledger-live/issues"
20
+ },
21
+ "homepage": "https://github.com/LedgerHQ/ledger-live",
22
+ "publishConfig": {
23
+ "access": "public"
24
+ },
25
+ "main": "lib/Icon.js",
26
+ "module": "lib-es/Icon.js",
27
+ "types": "lib/Icon.d.ts",
28
+ "license": "Apache-2.0",
29
+ "dependencies": {
30
+ "bip32-path": "^0.4.2",
31
+ "@ledgerhq/errors": "^6.18.0",
32
+ "@ledgerhq/hw-transport": "^6.31.2"
33
+ },
34
+ "devDependencies": {
35
+ "@types/jest": "^29.5.10",
36
+ "@types/node": "^20.8.10",
37
+ "jest": "^29.7.0",
38
+ "rimraf": "^4.4.1",
39
+ "source-map-support": "^0.5.21",
40
+ "ts-jest": "^29.1.1",
41
+ "ts-node": "^10.4.0",
42
+ "documentation": "14.0.2",
43
+ "@ledgerhq/hw-transport-mocker": "^6.29.2",
44
+ "@ledgerhq/hw-transport-node-speculos": "^6.29.2"
45
+ },
46
+ "scripts": {
47
+ "clean": "rimraf lib lib-es",
48
+ "build": "tsc && tsc -m ES6 --outDir lib-es",
49
+ "prewatch": "pnpm build",
50
+ "watch": "tsc --watch",
51
+ "doc": "documentation readme src/** --section=API --pe ts --re ts --re d.ts",
52
+ "lint": "eslint ./src --no-error-on-unmatched-pattern --ext .ts,.tsx",
53
+ "lint:fix": "pnpm lint --fix",
54
+ "test": "jest"
55
+ }
56
+ }
package/src/Icon.ts ADDED
@@ -0,0 +1,185 @@
1
+ /********************************************************************************
2
+ * Ledger JS API for ICON
3
+ * (c) 2018 ICON Foundation
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ ********************************************************************************/
17
+
18
+ import { splitPath, foreach, hexToBase64 } from "./utils";
19
+ import type Transport from "@ledgerhq/hw-transport";
20
+ import { UserRefusedOnDevice, UserRefusedAddress } from "@ledgerhq/errors";
21
+
22
+ const CLA = 0xe0;
23
+ const INS = {
24
+ GET_VERSION: 0x06,
25
+ GET_ADDR: 0x02,
26
+ SIGN: 0x04,
27
+ };
28
+
29
+ const SW_OK = 0x9000;
30
+ const SW_CANCEL = 0x6986;
31
+
32
+ /**
33
+ * ICON API
34
+ *
35
+ * @example
36
+ * import Icx from "@ledgerhq/hw-app-icx";
37
+ * const icx = new Icx(transport)
38
+ */
39
+ export default class Icx {
40
+ transport: Transport;
41
+
42
+ constructor(transport: Transport) {
43
+ this.transport = transport;
44
+ transport.decorateAppAPIMethods(
45
+ this,
46
+ ["getAddress", "signTransaction", "getAppConfiguration"],
47
+ "ICON",
48
+ );
49
+ }
50
+
51
+ /**
52
+ * Returns public key and ICON address for a given BIP 32 path.
53
+ * @param path a path in BIP 32 format
54
+ * @option boolDisplay optionally enable or not the display
55
+ * @option boolChaincode optionally enable or not the chaincode request
56
+ * @return an object with a publickey(hexa string), address(string) and
57
+ * (optionally) chaincode(hexa string)
58
+ * @example
59
+ * icx.getAddress("44'/4801368'/0'", true, true).then(o => o.address)
60
+ */
61
+ getAddress(
62
+ path: string,
63
+ boolDisplay = false,
64
+ boolChaincode = true,
65
+ ): Promise<{
66
+ publicKey: string;
67
+ address: string;
68
+ chainCode?: string;
69
+ }> {
70
+ const paths = splitPath(path);
71
+ const buffer = Buffer.alloc(1 + paths.length * 4);
72
+ buffer[0] = paths.length;
73
+ paths.forEach((element, index) => {
74
+ buffer.writeUInt32BE(element, 1 + 4 * index);
75
+ });
76
+ const statusList = [SW_OK, SW_CANCEL];
77
+ return this.transport
78
+ .send(
79
+ CLA,
80
+ INS.GET_ADDR,
81
+ boolDisplay ? 0x01 : 0x00,
82
+ boolChaincode ? 0x01 : 0x00,
83
+ buffer,
84
+ statusList,
85
+ )
86
+ .then(response => {
87
+ const publicKeyLength = response[0];
88
+ const addressLength = response[1 + publicKeyLength];
89
+ const errorCodeData = response.slice(-2);
90
+ const returnCode = errorCodeData[0] * 0x100 + errorCodeData[1];
91
+
92
+ if (returnCode === SW_CANCEL) {
93
+ throw new UserRefusedAddress();
94
+ }
95
+ return {
96
+ publicKey: response.slice(1, 1 + publicKeyLength).toString("hex"),
97
+ address: response
98
+ .slice(1 + publicKeyLength + 1, 1 + publicKeyLength + 1 + addressLength)
99
+ .toString(),
100
+ chainCode: boolChaincode ? response.slice(-32).toString("hex") : undefined,
101
+ };
102
+ });
103
+ }
104
+
105
+ /**
106
+ * Signs a transaction and returns signed message given the raw transaction
107
+ * and the BIP 32 path of the account to sign
108
+ * @param path a path in BIP 32 format
109
+ * @param rawTxAscii raw transaction data to sign in ASCII string format
110
+ * @return an object with a base64 encoded signature and hash in hexa string
111
+ * @example
112
+ * icx.signTransaction("44'/4801368'/0'",
113
+ * "icx_sendTransaction.fee.0x2386f26fc10000." +
114
+ * "from.hxc9ecad30b05a0650a337452fce031e0c60eacc3a.nonce.0x3." +
115
+ * "to.hx4c5101add2caa6a920420cf951f7dd7c7df6ca24.value.0xde0b6b3a7640000")
116
+ * .then(result => ...)
117
+ */
118
+ signTransaction(
119
+ path: string,
120
+ rawTxAscii: string,
121
+ ): Promise<{
122
+ signedRawTxBase64: string;
123
+ hashHex: string;
124
+ }> {
125
+ const paths = splitPath(path);
126
+ let offset = 0;
127
+ const rawTx = Buffer.from(rawTxAscii);
128
+ const toSend: Buffer[] = [];
129
+ let response: Buffer;
130
+ while (offset !== rawTx.length) {
131
+ const maxChunkSize = offset === 0 ? 150 - 1 - paths.length * 4 - 4 : 150;
132
+ const chunkSize = offset + maxChunkSize > rawTx.length ? rawTx.length - offset : maxChunkSize;
133
+ const buffer = Buffer.alloc(offset === 0 ? 1 + paths.length * 4 + 4 + chunkSize : chunkSize);
134
+ if (offset === 0) {
135
+ buffer[0] = paths.length;
136
+ paths.forEach((element, index) => {
137
+ buffer.writeUInt32BE(element, 1 + 4 * index);
138
+ });
139
+ buffer.writeUInt32BE(rawTx.length, 1 + 4 * paths.length);
140
+ rawTx.copy(buffer, 1 + 4 * paths.length + 4, offset, offset + chunkSize);
141
+ } else {
142
+ rawTx.copy(buffer, 0, offset, offset + chunkSize);
143
+ }
144
+ toSend.push(buffer);
145
+ offset += chunkSize;
146
+ }
147
+ return foreach(toSend, (data, i) =>
148
+ this.transport
149
+ .send(CLA, INS.SIGN, i === 0 ? 0x00 : 0x80, 0x00, data, [SW_OK, SW_CANCEL])
150
+ .then(apduResponse => {
151
+ response = apduResponse;
152
+ }),
153
+ ).then(() => {
154
+ const errorCodeData = response.slice(-2);
155
+ const returnCode = errorCodeData[0] * 0x100 + errorCodeData[1];
156
+
157
+ if (returnCode === SW_CANCEL) {
158
+ throw new UserRefusedOnDevice();
159
+ }
160
+ // r, s, v are aligned sequencially
161
+ return {
162
+ signedRawTxBase64: hexToBase64(response.slice(0, 32 + 32 + 1).toString("hex")),
163
+ hashHex: response.slice(32 + 32 + 1, 32 + 32 + 1 + 32).toString("hex"),
164
+ };
165
+ });
166
+ }
167
+
168
+ /**
169
+ * Returns the application configurations such as versions.
170
+ * @return major/minor/patch versions of Icon application
171
+ */
172
+ getAppConfiguration(): Promise<{
173
+ majorVersion: number;
174
+ minorVersion: number;
175
+ patchVersion: number;
176
+ }> {
177
+ return this.transport.send(CLA, INS.GET_VERSION, 0x00, 0x00).then(response => {
178
+ return {
179
+ majorVersion: response[0],
180
+ minorVersion: response[1],
181
+ patchVersion: response[2],
182
+ };
183
+ });
184
+ }
185
+ }
package/src/utils.ts ADDED
@@ -0,0 +1,58 @@
1
+ /********************************************************************************
2
+ * Ledger Node JS API for ICON
3
+ * (c) 2016-2017 Ledger
4
+ *
5
+ * Modifications (c) 2018 ICON Foundation
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ ********************************************************************************/
19
+
20
+ // TODO use bip32-path library
21
+ export function splitPath(path: string): number[] {
22
+ const result: number[] = [];
23
+ const components = path.split("/");
24
+ components.forEach(element => {
25
+ let number = parseInt(element, 10);
26
+ if (isNaN(number)) {
27
+ return; // FIXME shouldn't it throws instead?
28
+ }
29
+ if (element.length > 1 && element[element.length - 1] === "'") {
30
+ number += 0x80000000;
31
+ }
32
+ result.push(number);
33
+ });
34
+ return result;
35
+ }
36
+
37
+ export function foreach<T, A>(arr: T[], callback: (T, number) => Promise<A>): Promise<A[]> {
38
+ function iterate(index, array, result) {
39
+ if (index >= array.length) {
40
+ return result;
41
+ } else
42
+ return callback(array[index], index).then(function (res) {
43
+ result.push(res);
44
+ return iterate(index + 1, array, result);
45
+ });
46
+ }
47
+ return Promise.resolve().then(() => iterate(0, arr, []));
48
+ }
49
+
50
+ export function hexToBase64(hexString: string) {
51
+ return btoa(
52
+ (hexString.match(/\w{2}/g) || [])
53
+ .map(function (a) {
54
+ return String.fromCharCode(parseInt(a, 16));
55
+ })
56
+ .join(""),
57
+ );
58
+ }
@@ -0,0 +1,35 @@
1
+ import {
2
+ openTransportReplayer,
3
+ RecordStore,
4
+ } from "@ledgerhq/hw-transport-mocker";
5
+ import Icon from "../src/Icon";
6
+
7
+ test("Icon init", async () => {
8
+ const transport = await openTransportReplayer(RecordStore.fromString(""));
9
+ const icx = new Icon(transport);
10
+ expect(icx).not.toBe(undefined);
11
+ });
12
+
13
+ test("getAppConfiguration", async () => {
14
+ const transport = await openTransportReplayer(
15
+ RecordStore.fromString(`
16
+ => e006000000
17
+ <= 0102039000
18
+ `)
19
+ );
20
+ const icon = new Icon(transport);
21
+ const result = await icon.getAppConfiguration();
22
+ expect(result).toEqual({
23
+ majorVersion: 1,
24
+ minorVersion: 2,
25
+ patchVersion: 3
26
+ });
27
+ });
28
+
29
+ test("should throw on invalid derivation path", async () => {
30
+ const transport = await openTransportReplayer(new RecordStore());
31
+ const icon = new Icon(transport);
32
+ return expect(
33
+ icon.getAddress("some invalid derivation path", false)
34
+ ).rejects.toThrow("EOF: no more APDU to replay");
35
+ });
package/tsconfig.json ADDED
@@ -0,0 +1,7 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "lib"
5
+ },
6
+ "include": ["src/**/*"]
7
+ }