|
| 1 | +/******************************************************************************** |
| 2 | + * Ledger Node JS API |
| 3 | + * (c) 2016-2017 Ledger |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + ********************************************************************************/ |
| 17 | +// @flow |
| 18 | + |
| 19 | +import Int64 from "node-int64"; |
| 20 | +import type Transport from "@ledgerhq/hw-transport"; |
| 21 | +import { TransportStatusError } from "@ledgerhq/hw-transport"; |
| 22 | + |
| 23 | +const CLA = 0x80; |
| 24 | + |
| 25 | +const INS_GET_PUBLIC_KEY = 0x01; |
| 26 | +const INS_SET_TX = 0x02; |
| 27 | +const INS_SIGN_TX = 0x03; |
| 28 | +const INS_APP_INFO = 0x04; |
| 29 | + |
| 30 | +const P1_FIRST = 0x01; |
| 31 | +const P1_NEXT = 0x02; |
| 32 | +const P1_LAST = 0x03; |
| 33 | + |
| 34 | +const P2_SINGLE_TX = 0x01; |
| 35 | +const P2_MULTI_TX = 0x02; |
| 36 | + |
| 37 | +const MAX_APDU_SIZE = 64; |
| 38 | +const OFFSET_CDATA = 5; |
| 39 | +const MAX_ADDR_PRINT_LENGTH = 12; |
| 40 | +const INDEX_MAX = 0xFFFFFFFF; |
| 41 | + |
| 42 | +const INDEX_NAN = 0x5003; |
| 43 | +const INDEX_MAX_EXCEEDED = 0x5302; |
| 44 | + |
| 45 | +/** |
| 46 | + * Cardano ADA API |
| 47 | + * |
| 48 | + * @example |
| 49 | + * import Ada from "@ledgerhq/hw-app-ada"; |
| 50 | + * const ada = new Ada(transport); |
| 51 | + */ |
| 52 | +export default class Ada { |
| 53 | + transport: Transport<*>; |
| 54 | + methods: Array<string>; |
| 55 | + |
| 56 | + constructor(transport: Transport<*>) { |
| 57 | + this.transport = transport; |
| 58 | + this.methods = [ "isConnected", "getWalletRecoveryPassphrase", "getWalletPublicKeyWithIndex", "signTransaction" ]; |
| 59 | + this.transport.decorateAppAPIMethods(this, this.methods, "ADA"); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Checks if the device is connected and if so, returns an object |
| 64 | + * containing the app version. |
| 65 | + * |
| 66 | + * @returns {Promise<{major:number, minor:number, patch:number}>} Result object containing the application version number. |
| 67 | + * |
| 68 | + * @example |
| 69 | + * const { major, minor, patch } = await ada.isConnected(); |
| 70 | + * console.log(`App version ${major}.${minor}.${patch}`); |
| 71 | + * |
| 72 | + */ |
| 73 | + async isConnected(): Promise<{ major: string, minor: string, patch: string }> { |
| 74 | + const response = await this.transport.send(CLA, INS_APP_INFO, 0x00, 0x00); |
| 75 | + |
| 76 | + const [ major, minor, patch ] = response; |
| 77 | + return { major, minor, patch }; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * @description Get the root extended public key of the wallet, |
| 82 | + * also known as the wallet recovery passphrase. |
| 83 | + * BIP 32 Path M 44' /1815' |
| 84 | + * 32 Byte Public Key |
| 85 | + * 32 Byte Chain Code |
| 86 | + * |
| 87 | + * @return {Promise<{ publicKey:string, chainCode:string }>} The result object containing the root wallet public key and chaincode. |
| 88 | + * |
| 89 | + * @example |
| 90 | + * const { publicKey, chainCode } = await ada.getWalletRecoveryPassphrase(); |
| 91 | + * console.log(publicKey); |
| 92 | + * console.log(chainCode); |
| 93 | + * |
| 94 | + */ |
| 95 | + async getWalletRecoveryPassphrase(): Promise<{ publicKey: string, chainCode: string }> { |
| 96 | + const response = await this.transport.send(CLA, INS_GET_PUBLIC_KEY, 0x01, 0x00); |
| 97 | + |
| 98 | + const [ publicKeyLength ] = response; |
| 99 | + const publicKey = response.slice(1, 1 + publicKeyLength).toString("hex"); |
| 100 | + const chainCode = response.slice(1 + publicKeyLength, 1 + publicKeyLength + 32).toString("hex"); |
| 101 | + |
| 102 | + return { publicKey, chainCode }; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * @description Get a public key from the specified BIP 32 index. |
| 107 | + * The BIP 32 index is from the path at `44'/1815'/0'/[index]`. |
| 108 | + * |
| 109 | + * @param {number} index The index to retrieve. |
| 110 | + * @return {Promise<{ publicKey:string }>} The public key for the given index. |
| 111 | + * |
| 112 | + * @throws 5201 - Non-hardened index passed in, Index < 0x80000000 |
| 113 | + * @throws 5202 - Invalid header |
| 114 | + * @throws 5003 - Index not a number |
| 115 | + * |
| 116 | + * @example |
| 117 | + * const { publicKey } = await ada.getWalletPublicKeyWithIndex(0xC001CODE); |
| 118 | + * console.log(publicKey); |
| 119 | + * |
| 120 | + */ |
| 121 | + async getWalletPublicKeyWithIndex(index: number): Promise<{ publicKey: string }> { |
| 122 | + if (isNaN(index)) { |
| 123 | + throw new TransportStatusError(INDEX_NAN); |
| 124 | + } |
| 125 | + |
| 126 | + const data = Buffer.alloc(4); |
| 127 | + data.writeUInt32BE(index, 0); |
| 128 | + |
| 129 | + const response = await this.transport.send(CLA, INS_GET_PUBLIC_KEY, 0x02, 0x00, data); |
| 130 | + |
| 131 | + const [ publicKeyLength ] = response; |
| 132 | + const publicKey = response.slice(1, 1 + publicKeyLength).toString("hex"); |
| 133 | + |
| 134 | + return { publicKey }; |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * @description Signs a hex encoded transaction with the given indexes. |
| 139 | + * The transaction is hased using Blake2b on the Ledger device. |
| 140 | + * Then, signed by the private key derived from each of the passed in indexes at |
| 141 | + * path 44'/1815'/0'/[index]. |
| 142 | + * |
| 143 | + * @param {string} txHex The transaction to be signed. |
| 144 | + * @param {number[]} indexes The indexes of the keys to be used for signing. |
| 145 | + * @return {Array.Promise<{ digest:string }>} An array of result objects containing a digest for each of the passed in indexes. |
| 146 | + * |
| 147 | + * @throws 5001 - Tx > 1024 bytes |
| 148 | + * @throws 5301 - Index < 0x80000000 |
| 149 | + * @throws 5302 - Index > 0xFFFFFFFF |
| 150 | + * @throws 5003 - Index not a number |
| 151 | + * |
| 152 | + * @example |
| 153 | + * const transaction = '839F8200D8185826825820E981442C2BE40475BB42193CA35907861D90715854DE6FCBA767B98F1789B51219439AFF9F8282D818584A83581CE7FE8E468D2249F18CD7BF9AEC0D4374B7D3E18609EDE8589F82F7F0A20058208200581C240596B9B63FC010C06FBE92CF6F820587406534795958C411E662DC014443C0688E001A6768CC861B0037699E3EA6D064FFA0'; |
| 154 | + * const { digest } = await ada.signTransaction(transaction, [0xF005BA11]); |
| 155 | + * console.log(`Signed successfully: ${digest}`); |
| 156 | + * |
| 157 | + */ |
| 158 | + async signTransaction(txHex: string, indexes: Array<number>): Promise<Array<{ digest: string }>> { |
| 159 | + await this.setTransaction(txHex); |
| 160 | + return this.signTransactionWithIndexes(indexes); |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Set the transaction. |
| 165 | + * |
| 166 | + * @param {string} txHex The transaction to be set. |
| 167 | + * @return Promise<{ inputs?: string, outputs?: string, txs?: Array<{ address: string, amount: string }> }> The response from the device. |
| 168 | + * @private |
| 169 | + */ |
| 170 | + async setTransaction(txHex: string): Promise<{ inputs?: string, outputs?: string, txs?: Array<{ address: string, amount: string }> }> { |
| 171 | + const rawTx = Buffer.from(txHex, "hex"); |
| 172 | + const chunkSize = MAX_APDU_SIZE - OFFSET_CDATA; |
| 173 | + let response = {}; |
| 174 | + |
| 175 | + for (let i = 0; i < rawTx.length; i += chunkSize) { |
| 176 | + const chunk = rawTx.slice(i, i + chunkSize); |
| 177 | + const p2 = rawTx.length < chunkSize ? P2_SINGLE_TX : P2_MULTI_TX; |
| 178 | + let p1 = P1_NEXT; |
| 179 | + |
| 180 | + if (i === 0) { |
| 181 | + p1 = P1_FIRST; |
| 182 | + } else if (i + chunkSize >= rawTx.length) { |
| 183 | + p1 = P1_LAST; |
| 184 | + } |
| 185 | + |
| 186 | + const res = await this.transport.send(CLA, INS_SET_TX, p1, p2, chunk); |
| 187 | + |
| 188 | + if (res.length > 4) { |
| 189 | + const [ inputs, outputs ] = res; |
| 190 | + const txs = []; |
| 191 | + |
| 192 | + let offset = 2; |
| 193 | + for (let i = 0; i < outputs; i++) { |
| 194 | + let address = res.slice(offset, offset + MAX_ADDR_PRINT_LENGTH).toString(); |
| 195 | + offset += MAX_ADDR_PRINT_LENGTH; |
| 196 | + let amount = new Int64(res.readUInt32LE(offset + 4), res.readUInt32LE(offset)).toOctetString(); |
| 197 | + txs.push({ address, amount }); |
| 198 | + offset += 8; |
| 199 | + } |
| 200 | + |
| 201 | + response = { inputs, outputs, txs }; |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + return response; |
| 206 | + } |
| 207 | + |
| 208 | + /** |
| 209 | + * Sign the set transaction with the given indexes. |
| 210 | + * Note that setTransaction must be called prior to this being called. |
| 211 | + * |
| 212 | + * @param {number[]} indexes The indexes of the keys to be used for signing. |
| 213 | + * @returns {Array.Promise<Object>} An array of result objects containing a digest for each of the passed in indexes. |
| 214 | + * @private |
| 215 | + */ |
| 216 | + async signTransactionWithIndexes(indexes: Array<number>): Promise<Array<{ digest: string }>> { |
| 217 | + let response = []; |
| 218 | + |
| 219 | + for (let i = 0; i < indexes.length; i++) { |
| 220 | + if (isNaN(indexes[i])) { |
| 221 | + throw new TransportStatusError(INDEX_NAN); |
| 222 | + } |
| 223 | + |
| 224 | + if (indexes[i] > INDEX_MAX) { |
| 225 | + throw new TransportStatusError(INDEX_MAX_EXCEEDED); |
| 226 | + } |
| 227 | + |
| 228 | + const data = Buffer.alloc(4); |
| 229 | + data.writeUInt32BE(indexes[i], 0); |
| 230 | + |
| 231 | + const res = await this.transport.send(CLA, INS_SIGN_TX, 0x00, 0x00, data); |
| 232 | + const digest = res.slice(0, res.length - 2).toString("hex"); |
| 233 | + |
| 234 | + response.push({ digest }); |
| 235 | + } |
| 236 | + |
| 237 | + return response; |
| 238 | + } |
| 239 | +} |
0 commit comments