|
| 1 | +const bs58 = require('bs58'); |
| 2 | + |
| 3 | +const INS_GET_PUBKEY = 0x05; |
| 4 | +const INS_GET_APP_CONFIGURATION = 0x04; |
| 5 | +const INS_SIGN_MESSAGE = 0x06; |
| 6 | + |
| 7 | +const P1_NON_CONFIRM = 0x00; |
| 8 | +const P1_CONFIRM = 0x01; |
| 9 | + |
| 10 | +const P2_EXTEND = 0x01; |
| 11 | +const P2_MORE = 0x02; |
| 12 | + |
| 13 | +const MAX_PAYLOAD = 255; |
| 14 | + |
| 15 | +const LEDGER_CLA = 0xe0; |
| 16 | + |
| 17 | +const STATUS_OK = 0x9000; |
| 18 | +const BIP32_HARDENED_BIT = (1 << 31) >>> 0; |
| 19 | + |
| 20 | +class Solana { |
| 21 | + constructor(transport) { |
| 22 | + this.transport = transport; |
| 23 | + } |
| 24 | + /* |
| 25 | + * Helper for chunked send of large payloads |
| 26 | + */ |
| 27 | + async solana_send(transport, instruction, p1, payload) { |
| 28 | + var p2 = 0; |
| 29 | + var payload_offset = 0; |
| 30 | + |
| 31 | + if (payload.length > MAX_PAYLOAD) { |
| 32 | + while (payload.length - payload_offset > MAX_PAYLOAD) { |
| 33 | + const buf = payload.slice(payload_offset, payload_offset + MAX_PAYLOAD); |
| 34 | + payload_offset += MAX_PAYLOAD; |
| 35 | + const reply = await transport.send(LEDGER_CLA, instruction, p1, p2 | P2_MORE, buf); |
| 36 | + if (reply.length != 2) { |
| 37 | + throw new TransportError( |
| 38 | + 'solana_send: Received unexpected reply payload', |
| 39 | + 'UnexpectedReplyPayload' |
| 40 | + ); |
| 41 | + } |
| 42 | + p2 |= P2_EXTEND; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + const buf = payload.slice(payload_offset); |
| 47 | + const reply = await transport.send(LEDGER_CLA, instruction, p1, p2, buf); |
| 48 | + |
| 49 | + return reply.slice(0, reply.length - 2); |
| 50 | + } |
| 51 | + |
| 52 | + _harden(n) { |
| 53 | + return (n | BIP32_HARDENED_BIT) >>> 0; |
| 54 | + } |
| 55 | + |
| 56 | + solana_derivation_path(account, change) { |
| 57 | + var length; |
| 58 | + if (typeof account === 'number') { |
| 59 | + if (typeof change === 'number') { |
| 60 | + length = 4; |
| 61 | + } else { |
| 62 | + length = 3; |
| 63 | + } |
| 64 | + } else { |
| 65 | + length = 2; |
| 66 | + } |
| 67 | + |
| 68 | + var derivation_path = Buffer.alloc(1 + length * 4); |
| 69 | + var offset = 0; |
| 70 | + offset = derivation_path.writeUInt8(length, offset); |
| 71 | + offset = derivation_path.writeUInt32BE(this._harden(44), offset); // Using BIP44 |
| 72 | + offset = derivation_path.writeUInt32BE(this._harden(501), offset); // Solana's BIP44 path |
| 73 | + |
| 74 | + if (length > 2) { |
| 75 | + offset = derivation_path.writeUInt32BE(this._harden(account), offset); |
| 76 | + if (length == 4) { |
| 77 | + offset = derivation_path.writeUInt32BE(this._harden(change), offset); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + return derivation_path; |
| 82 | + } |
| 83 | + |
| 84 | + solana_ledger_get_pubkey() { |
| 85 | + return this.solana_send( |
| 86 | + this.transport, |
| 87 | + INS_GET_PUBKEY, |
| 88 | + P1_CONFIRM, |
| 89 | + this.solana_derivation_path() |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + async solana_ledger_get_version() { |
| 94 | + return this.solana_send( |
| 95 | + this.transport, |
| 96 | + INS_GET_APP_CONFIGURATION, |
| 97 | + P1_NON_CONFIRM, |
| 98 | + Buffer.from('') |
| 99 | + ).then(info => { |
| 100 | + return `${info[2]}.${info[3]}.${info[4]}`; |
| 101 | + }); |
| 102 | + } |
| 103 | + |
| 104 | + solana_ledger_sign_transaction(transaction) { |
| 105 | + let msg_bytes; |
| 106 | + try { |
| 107 | + msg_bytes = transaction.serializeMessage(); |
| 108 | + // XXX: Ledger app only supports a single derivation_path per call ATM |
| 109 | + var num_paths = Buffer.alloc(1); |
| 110 | + |
| 111 | + num_paths.writeUInt8(1); |
| 112 | + |
| 113 | + const payload = Buffer.concat([num_paths, this.solana_derivation_path(), msg_bytes]); |
| 114 | + |
| 115 | + return this.solana_send(this.transport, INS_SIGN_MESSAGE, P1_CONFIRM, payload); |
| 116 | + } catch (e) { |
| 117 | + return; |
| 118 | + } |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +exports.default = Solana; |
0 commit comments