|
| 1 | +var common = require('./common'); |
| 2 | +var bitcoin = require('bitgoinjs-lib'); |
| 3 | +var ecurve = require('ecurve'); |
| 4 | +var curve = ecurve.getCurveByName('secp256k1'); |
| 5 | +var BigInteger = require('bigi'); |
| 6 | +var createHmac = require('create-hmac'); |
| 7 | +var HDNode = bitcoin.HDNode; |
| 8 | + |
| 9 | +var secp256k1; |
| 10 | + |
| 11 | +try { |
| 12 | + secp256k1 = require('secp256k1'); |
| 13 | +} catch (err) { |
| 14 | + console.log('running without secp256k1 acceleration'); |
| 15 | +} |
| 16 | + |
| 17 | +// Check for IE, and disable secp256k1, due to: |
| 18 | +// https://github.com/indutny/bn.js/issues/133 |
| 19 | +var isIE = (({}).constructor.name === undefined); |
| 20 | +if (isIE) { |
| 21 | + secp256k1 = undefined; |
| 22 | +} |
| 23 | + |
| 24 | +bitcoin.getNetwork = function(network) { |
| 25 | + network = network || common.getNetwork(); |
| 26 | + return bitcoin.networks[network]; |
| 27 | +}; |
| 28 | + |
| 29 | +bitcoin.makeRandomKey = function() { |
| 30 | + return bitcoin.ECPair.makeRandom({ network: bitcoin.getNetwork() }); |
| 31 | +}; |
| 32 | + |
| 33 | +HDNode.prototype.getKey = function(network) { |
| 34 | + network = network || bitcoin.getNetwork(); |
| 35 | + var k = this.keyPair; |
| 36 | + var result = new bitcoin.ECPair(k.d, k.d ? null : k.Q, { network: network, compressed: k.compressed }); |
| 37 | + // Creating Q from d takes ~25ms, so if it's not created, use native bindings to pre-compute |
| 38 | + if (!result.__Q && secp256k1) { |
| 39 | + result.__Q = ecurve.Point.decodeFrom(curve, secp256k1.publicKeyCreate(k.d.toBuffer(32), false)); |
| 40 | + } |
| 41 | + return result; |
| 42 | +}; |
| 43 | + |
| 44 | +/** |
| 45 | + * Derive a child HDNode from a parent HDNode and index. Uses secp256k1 to speed |
| 46 | + * up public key derivations by 100x vs. bitcoinjs-lib implementation. |
| 47 | + * |
| 48 | + * @param {HDNode} hdnode parent HDNode |
| 49 | + * @param {Number} index child index |
| 50 | + * @returns {HDNode} derived HDNode |
| 51 | + */ |
| 52 | +var deriveFast = function (hdnode, index) { |
| 53 | + // no fast path for private key derivations -- delegate to standard method |
| 54 | + if (!secp256k1 || hdnode.keyPair.d) { |
| 55 | + return hdnode.derive(index); |
| 56 | + } |
| 57 | + |
| 58 | + var isHardened = index >= bitcoin.HDNode.HIGHEST_BIT; |
| 59 | + if (isHardened) { |
| 60 | + throw new Error('cannot derive hardened key from public key'); |
| 61 | + } |
| 62 | + |
| 63 | + var indexBuffer = new Buffer(4); |
| 64 | + indexBuffer.writeUInt32BE(index, 0); |
| 65 | + |
| 66 | + var data; |
| 67 | + |
| 68 | + // data = serP(point(kpar)) || ser32(index) |
| 69 | + // = serP(Kpar) || ser32(index) |
| 70 | + data = Buffer.concat([ |
| 71 | + hdnode.keyPair.getPublicKeyBuffer(), |
| 72 | + indexBuffer |
| 73 | + ]); |
| 74 | + |
| 75 | + var I = createHmac('sha512', hdnode.chainCode).update(data).digest(); |
| 76 | + var IL = I.slice(0, 32); |
| 77 | + var IR = I.slice(32); |
| 78 | + |
| 79 | + var pIL = BigInteger.fromBuffer(IL); |
| 80 | + |
| 81 | + // In case parse256(IL) >= n, proceed with the next value for i |
| 82 | + if (pIL.compareTo(curve.n) >= 0) { |
| 83 | + return deriveFast(hdnode, index + 1); |
| 84 | + } |
| 85 | + |
| 86 | + // Private parent key -> private child key |
| 87 | + var hd; |
| 88 | + // Ki = point(parse256(IL)) + Kpar |
| 89 | + // = G*IL + Kpar |
| 90 | + |
| 91 | + // The expensive op is the point multiply -- use secp256k1 lib to do that |
| 92 | + var Ki = ecurve.Point.decodeFrom(curve, secp256k1.publicKeyCreate(IL, false)).add(hdnode.keyPair.Q); |
| 93 | + |
| 94 | + // In case Ki is the point at infinity, proceed with the next value for i |
| 95 | + if (curve.isInfinity(Ki)) { |
| 96 | + return deriveFast(hdnode, index + 1); |
| 97 | + } |
| 98 | + |
| 99 | + var keyPair = new bitcoin.ECPair(null, Ki, { network: hdnode.keyPair.network }); |
| 100 | + hd = new bitcoin.HDNode(keyPair, IR); |
| 101 | + |
| 102 | + hd.depth = hdnode.depth + 1; |
| 103 | + hd.index = index; |
| 104 | + hd.parentFingerprint = hdnode.getFingerprint().readUInt32BE(0); |
| 105 | + |
| 106 | + return hd; |
| 107 | +}; |
| 108 | + |
| 109 | +if (secp256k1) { |
| 110 | + bitcoin.ECPair.prototype.sign = function (hash) { |
| 111 | + if (!this.d) { |
| 112 | + throw new Error('Missing private key'); |
| 113 | + } |
| 114 | + var sig = secp256k1.sign(hash, this.d.toBuffer(32)).signature; |
| 115 | + return bitcoin.ECSignature.fromDER(secp256k1.signatureExport(sig)); |
| 116 | + }; |
| 117 | + |
| 118 | + bitcoin.ECPair.prototype.verify = function (hash, signature) { |
| 119 | + signature = new bitcoin.ECSignature(signature.r, signature.s); |
| 120 | + signature = secp256k1.signatureNormalize(secp256k1.signatureImport(signature.toDER())); |
| 121 | + return secp256k1.verify(hash, signature, this.getPublicKeyBuffer()); |
| 122 | + }; |
| 123 | +} |
| 124 | + |
| 125 | +/** |
| 126 | + * Derive a BIP32 path, given a root key |
| 127 | + * We cache keys at each level of hierarchy we derive, to avoid re-deriving (approx 25ms per derivation) |
| 128 | + * @param rootKey key to derive off |
| 129 | + * @param path the path, e.g. 'm/0/0/0/1' |
| 130 | + * @returns {*} the derived hd key |
| 131 | + */ |
| 132 | +bitcoin.hdPath = function(rootKey) { |
| 133 | + var cache = {}; |
| 134 | + var derive = function (path) { |
| 135 | + var components = path.split('/').filter(function (c) { |
| 136 | + return c !== ''; |
| 137 | + }); |
| 138 | + // strip any extraneous / characters |
| 139 | + path = components.join('/'); |
| 140 | + if (cache[path]) { |
| 141 | + return cache[path]; |
| 142 | + } |
| 143 | + var len = components.length; |
| 144 | + if (len === 0 || len === 1 && components[0] === 'm') { |
| 145 | + return rootKey; |
| 146 | + } |
| 147 | + var parentPath = components.slice(0, len - 1).join('/'); |
| 148 | + var parentKey = derive(parentPath); |
| 149 | + var el = components[len - 1]; |
| 150 | + |
| 151 | + var hardened = false; |
| 152 | + if (el[el.length - 1] === "'") { |
| 153 | + hardened = true; |
| 154 | + } |
| 155 | + var index = parseInt(el); |
| 156 | + var derived; |
| 157 | + if (hardened) { |
| 158 | + derived = parentKey.deriveHardened(index); |
| 159 | + } else { |
| 160 | + derived = deriveFast(parentKey, index); |
| 161 | + } |
| 162 | + cache[path] = derived; |
| 163 | + return derived; |
| 164 | + }; |
| 165 | + |
| 166 | + var deriveKey = function(path) { |
| 167 | + var hdNode = this.derive(path); |
| 168 | + return hdNode.keyPair; |
| 169 | + }; |
| 170 | + |
| 171 | + return { |
| 172 | + derive: derive, |
| 173 | + deriveKey: deriveKey |
| 174 | + }; |
| 175 | +}; |
| 176 | + |
| 177 | +module.exports = bitcoin; |
0 commit comments