diff --git a/browser/bundle.js b/browser/bundle.js index 71b028b..7577a5b 100644 --- a/browser/bundle.js +++ b/browser/bundle.js @@ -4558,7 +4558,6 @@ var Point = imports.Point || require('./Point'); var SecureRandom = imports.SecureRandom || require('./SecureRandom'); var bignum = imports.bignum || require('bignum'); var networks = require('../networks'); -var BufferPut = require('bufferput'); var secp256k1_n = new bignum('FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141', 16); var secp256k1_Gx = new bignum('79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798', 16); @@ -4572,7 +4571,8 @@ var HierarchicalKey = function(bytes) { if (typeof bytes == 'undefined' || bytes == 'mainnet' || bytes == 'livenet') { bytes = 'livenet'; this.version = networks['livenet'].hkeyPrivateVersion; - } else if (bytes == 'testnet') { + } + else if (bytes == 'testnet') { this.version = networks['testnet'].hkeyPrivateVersion; } if (bytes == 'livenet' || bytes == 'testnet') { @@ -4587,12 +4587,12 @@ var HierarchicalKey = function(bytes) { this.buildExtendedPrivateKey(); return; } - + // decode base58 if (typeof bytes === 'string') { var decoded = base58.decode(bytes); if (decoded.length != 82) - throw new Error('Not enough data, expected 82 and received ' + decoded.length); + throw new Error('Not enough data, expected 82 and received '+decoded.length); var checksum = decoded.slice(78, 82); bytes = decoded.slice(0, 78); @@ -4603,7 +4603,7 @@ var HierarchicalKey = function(bytes) { } } - if (bytes !== undefined && bytes !== null) + if (bytes !== undefined && bytes !== null) this.initFromBytes(bytes); } @@ -4613,9 +4613,9 @@ HierarchicalKey.seed = function(bytes, network) { if (!Buffer.isBuffer(bytes)) bytes = new Buffer(bytes, 'hex'); //if not buffer, assume hex - if (bytes.length < 128 / 8) + if (bytes.length < 128/8) return false; //need more entropy - if (bytes.length > 512 / 8) + if (bytes.length > 512/8) return false; var hash = coinUtil.sha512hmac(bytes, new Buffer('Bitcoin seed')); @@ -4639,23 +4639,23 @@ HierarchicalKey.seed = function(bytes, network) { HierarchicalKey.prototype.initFromBytes = function(bytes) { // Both pub and private extended keys are 78 bytes - if (bytes.length != 78) throw new Error('not enough data'); + if(bytes.length != 78) throw new Error('not enough data'); - this.version = u32(bytes.slice(0, 4)); - this.depth = u8(bytes.slice(4, 5)); + this.version = u32(bytes.slice(0, 4)); + this.depth = u8(bytes.slice(4, 5)); this.parentFingerprint = bytes.slice(5, 9); - this.childIndex = u32(bytes.slice(9, 13)); - this.chainCode = bytes.slice(13, 45); - + this.childIndex = u32(bytes.slice(9, 13)); + this.chainCode = bytes.slice(13, 45); + var keyBytes = bytes.slice(45, 78); - var isPrivate = - (this.version == networks['livenet'].hkeyPrivateVersion || - this.version == networks['testnet'].hkeyPrivateVersion); + var isPrivate = + (this.version == networks['livenet'].hkeyPrivateVersion || + this.version == networks['testnet'].hkeyPrivateVersion ); - var isPublic = - (this.version == networks['livenet'].hkeyPublicVersion || - this.version == networks['testnet'].hkeyPublicVersion); + var isPublic = + (this.version == networks['livenet'].hkeyPublicVersion || + this.version == networks['testnet'].hkeyPublicVersion ); if (isPrivate && keyBytes[0] == 0) { this.eckey = new Key(); @@ -4681,30 +4681,34 @@ HierarchicalKey.prototype.buildExtendedPublicKey = function() { this.extendedPublicKey = new Buffer([]); var v = null; - switch (this.version) { - case networks['livenet'].hkeyPublicVersion: - case networks['livenet'].hkeyPrivateVersion: - v = networks['livenet'].hkeyPublicVersion; - break; - case networks['testnet'].hkeyPublicVersion: - case networks['testnet'].hkeyPrivateVersion: - v = networks['testnet'].hkeyPublicVersion; - break; - default: - throw new Error('Unknown version'); + switch(this.version) { + case networks['livenet'].hkeyPublicVersion: + case networks['livenet'].hkeyPrivateVersion: + v = networks['livenet'].hkeyPublicVersion; + break; + case networks['testnet'].hkeyPublicVersion: + case networks['testnet'].hkeyPrivateVersion: + v = networks['testnet'].hkeyPublicVersion; + break; + default: + throw new Error('Unknown version'); } - var r = new BufferPut(); - r = r.word32be(v); - r = r.word8(this.depth); - r = r.put(this.parentFingerprint); - r = r.word32be(this.childIndex); - r = r.put(this.chainCode); - r = r.put(this.eckey.public); - - - this.extendedPublicKey = new Buffer(0); - this.extendedPublicKey = r.buffer(); + // Version + this.extendedPublicKey = Buffer.concat([ + new Buffer([v >> 24]), + new Buffer([(v >> 16) & 0xff]), + new Buffer([(v >> 8) & 0xff]), + new Buffer([v & 0xff]), + new Buffer([this.depth]), + this.parentFingerprint, + new Buffer([this.childIndex >>> 24]), + new Buffer([(this.childIndex >>> 16) & 0xff]), + new Buffer([(this.childIndex >>> 8) & 0xff]), + new Buffer([this.childIndex & 0xff]), + this.chainCode, + this.eckey.public + ]); } HierarchicalKey.prototype.extendedPublicKeyString = function(format) { @@ -4726,16 +4730,21 @@ HierarchicalKey.prototype.buildExtendedPrivateKey = function() { var v = this.version; - var r = new BufferPut(); - r = r.word32be(v); - r = r.word8(this.depth); - r = r.put(this.parentFingerprint); - r = r.word32be(this.childIndex); - r = r.put(this.chainCode); - r = r.word8(0); - r = r.put(this.eckey.private); - - this.extendedPrivateKey = r.buffer(); + this.extendedPrivateKey = Buffer.concat([ + new Buffer([v >> 24]), + new Buffer([(v >> 16) & 0xff]), + new Buffer([(v >> 8) & 0xff]), + new Buffer([v & 0xff]), + new Buffer([this.depth]), + this.parentFingerprint, + new Buffer([this.childIndex >>> 24]), + new Buffer([(this.childIndex >>> 16) & 0xff]), + new Buffer([(this.childIndex >>> 8) & 0xff]), + new Buffer([this.childIndex & 0xff]), + this.chainCode, + new Buffer([0]), + this.eckey.private + ]); } HierarchicalKey.prototype.extendedPrivateKeyString = function(format) { @@ -4763,17 +4772,16 @@ HierarchicalKey.prototype.derive = function(path) { for (var i in e) { var c = e[i]; - if (i == 0) { + if (i == 0 ) { if (c != 'm') throw new Error('invalid path'); continue; } - var usePrivate = (c.length > 1) && (c[c.length - 1] == '\''); + var usePrivate = (c.length > 1) && (c[c.length-1] == '\''); var childIndex = parseInt(usePrivate ? c.slice(0, c.length - 1) : c) & 0x7fffffff; - if (usePrivate) { + if (usePrivate) childIndex += 0x80000000; - } hkey = hkey.deriveChild(childIndex); } @@ -4785,15 +4793,15 @@ HierarchicalKey.prototype.deriveChild = function(i) { var ib = []; ib.push((i >> 24) & 0xff); ib.push((i >> 16) & 0xff); - ib.push((i >> 8) & 0xff); + ib.push((i >> 8) & 0xff); ib.push(i & 0xff); ib = new Buffer(ib); var usePrivate = (i & 0x80000000) != 0; - var isPrivate = - (this.version == networks['livenet'].hkeyPrivateVersion || - this.version == networks['testnet'].hkeyPrivateVersion); + var isPrivate = + (this.version == networks['livenet'].hkeyPrivateVersion || + this.version == networks['testnet'].hkeyPrivateVersion ); if (usePrivate && (!this.hasPrivateKey || !isPrivate)) throw new Error('Cannot do private key derivation without private key'); @@ -4809,24 +4817,18 @@ HierarchicalKey.prototype.deriveChild = function(i) { } var hash = coinUtil.sha512hmac(data, this.chainCode); - var il = bignum.fromBuffer(hash.slice(0, 32), { - size: 32 - }); + var il = bignum.fromBuffer(hash.slice(0, 32), {size: 32}); var ir = hash.slice(32, 64); // ki = IL + kpar (mod n). - var priv = bignum.fromBuffer(this.eckey.private, { - size: 32 - }); + var priv = bignum.fromBuffer(this.eckey.private, {size: 32}); var k = il.add(priv).mod(secp256k1_n); ret = new HierarchicalKey(null); ret.chainCode = ir; ret.eckey = new Key(); - ret.eckey.private = k.toBuffer({ - size: 64 - }); + ret.eckey.private = k.toBuffer({size: 32}); ret.eckey.regenerateSync(); ret.hasPrivateKey = true; @@ -4852,14 +4854,14 @@ HierarchicalKey.prototype.deriveChild = function(i) { ret.chainCode = new Buffer(ir); var eckey = new Key(); - eckey.public = newpub; + eckey.public = newpub; eckey.compressed = true; ret.eckey = eckey; ret.hasPrivateKey = false; } ret.childIndex = i; - ret.parentFingerprint = this.pubKeyHash.slice(0, 4); + ret.parentFingerprint = this.pubKeyHash.slice(0,4); ret.version = this.version; ret.depth = this.depth + 1; @@ -4868,6 +4870,7 @@ HierarchicalKey.prototype.deriveChild = function(i) { ret.buildExtendedPublicKey(); ret.buildExtendedPrivateKey(); + return ret; } @@ -4883,26 +4886,15 @@ function uint(f, size) { return n; } -function u8(f) { - return uint(f, 1); -} - -function u16(f) { - return uint(f, 2); -} - -function u32(f) { - return uint(f, 4); -} - -function u64(f) { - return uint(f, 8); -} +function u8(f) {return uint(f,1);} +function u16(f) {return uint(f,2);} +function u32(f) {return uint(f,4);} +function u64(f) {return uint(f,8);} module.exports = require('soop')(HierarchicalKey); }).call(this,require("buffer").Buffer) -},{"../networks":"ULNIu2","../util":143,"./Base58":"6VqyzY","./Key":"ALJ4PS","./Point":"6tXgqr","./SecureRandom":"p4SiC2","bignum":58,"buffer":85,"bufferput":"aXRuS6","soop":129}],"CBDCgz":[function(require,module,exports){ +},{"../networks":"ULNIu2","../util":143,"./Base58":"6VqyzY","./Key":"ALJ4PS","./Point":"6tXgqr","./SecureRandom":"p4SiC2","bignum":58,"buffer":85,"soop":129}],"CBDCgz":[function(require,module,exports){ (function (Buffer){ 'use strict'; var imports = require('soop').imports(); diff --git a/lib/HierarchicalKey.js b/lib/HierarchicalKey.js index 24fc8ff..4f45afb 100644 --- a/lib/HierarchicalKey.js +++ b/lib/HierarchicalKey.js @@ -6,7 +6,6 @@ var Point = imports.Point || require('./Point'); var SecureRandom = imports.SecureRandom || require('./SecureRandom'); var bignum = imports.bignum || require('bignum'); var networks = require('../networks'); -var BufferPut = require('bufferput'); var secp256k1_n = new bignum('FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141', 16); var secp256k1_Gx = new bignum('79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798', 16); @@ -20,7 +19,8 @@ var HierarchicalKey = function(bytes) { if (typeof bytes == 'undefined' || bytes == 'mainnet' || bytes == 'livenet') { bytes = 'livenet'; this.version = networks['livenet'].hkeyPrivateVersion; - } else if (bytes == 'testnet') { + } + else if (bytes == 'testnet') { this.version = networks['testnet'].hkeyPrivateVersion; } if (bytes == 'livenet' || bytes == 'testnet') { @@ -35,12 +35,12 @@ var HierarchicalKey = function(bytes) { this.buildExtendedPrivateKey(); return; } - + // decode base58 if (typeof bytes === 'string') { var decoded = base58.decode(bytes); if (decoded.length != 82) - throw new Error('Not enough data, expected 82 and received ' + decoded.length); + throw new Error('Not enough data, expected 82 and received '+decoded.length); var checksum = decoded.slice(78, 82); bytes = decoded.slice(0, 78); @@ -51,7 +51,7 @@ var HierarchicalKey = function(bytes) { } } - if (bytes !== undefined && bytes !== null) + if (bytes !== undefined && bytes !== null) this.initFromBytes(bytes); } @@ -61,9 +61,9 @@ HierarchicalKey.seed = function(bytes, network) { if (!Buffer.isBuffer(bytes)) bytes = new Buffer(bytes, 'hex'); //if not buffer, assume hex - if (bytes.length < 128 / 8) + if (bytes.length < 128/8) return false; //need more entropy - if (bytes.length > 512 / 8) + if (bytes.length > 512/8) return false; var hash = coinUtil.sha512hmac(bytes, new Buffer('Bitcoin seed')); @@ -87,23 +87,23 @@ HierarchicalKey.seed = function(bytes, network) { HierarchicalKey.prototype.initFromBytes = function(bytes) { // Both pub and private extended keys are 78 bytes - if (bytes.length != 78) throw new Error('not enough data'); + if(bytes.length != 78) throw new Error('not enough data'); - this.version = u32(bytes.slice(0, 4)); - this.depth = u8(bytes.slice(4, 5)); + this.version = u32(bytes.slice(0, 4)); + this.depth = u8(bytes.slice(4, 5)); this.parentFingerprint = bytes.slice(5, 9); - this.childIndex = u32(bytes.slice(9, 13)); - this.chainCode = bytes.slice(13, 45); - + this.childIndex = u32(bytes.slice(9, 13)); + this.chainCode = bytes.slice(13, 45); + var keyBytes = bytes.slice(45, 78); - var isPrivate = - (this.version == networks['livenet'].hkeyPrivateVersion || - this.version == networks['testnet'].hkeyPrivateVersion); + var isPrivate = + (this.version == networks['livenet'].hkeyPrivateVersion || + this.version == networks['testnet'].hkeyPrivateVersion ); - var isPublic = - (this.version == networks['livenet'].hkeyPublicVersion || - this.version == networks['testnet'].hkeyPublicVersion); + var isPublic = + (this.version == networks['livenet'].hkeyPublicVersion || + this.version == networks['testnet'].hkeyPublicVersion ); if (isPrivate && keyBytes[0] == 0) { this.eckey = new Key(); @@ -129,30 +129,34 @@ HierarchicalKey.prototype.buildExtendedPublicKey = function() { this.extendedPublicKey = new Buffer([]); var v = null; - switch (this.version) { - case networks['livenet'].hkeyPublicVersion: - case networks['livenet'].hkeyPrivateVersion: - v = networks['livenet'].hkeyPublicVersion; - break; - case networks['testnet'].hkeyPublicVersion: - case networks['testnet'].hkeyPrivateVersion: - v = networks['testnet'].hkeyPublicVersion; - break; - default: - throw new Error('Unknown version'); + switch(this.version) { + case networks['livenet'].hkeyPublicVersion: + case networks['livenet'].hkeyPrivateVersion: + v = networks['livenet'].hkeyPublicVersion; + break; + case networks['testnet'].hkeyPublicVersion: + case networks['testnet'].hkeyPrivateVersion: + v = networks['testnet'].hkeyPublicVersion; + break; + default: + throw new Error('Unknown version'); } - var r = new BufferPut(); - r = r.word32be(v); - r = r.word8(this.depth); - r = r.put(this.parentFingerprint); - r = r.word32be(this.childIndex); - r = r.put(this.chainCode); - r = r.put(this.eckey.public); - - - this.extendedPublicKey = new Buffer(0); - this.extendedPublicKey = r.buffer(); + // Version + this.extendedPublicKey = Buffer.concat([ + new Buffer([v >> 24]), + new Buffer([(v >> 16) & 0xff]), + new Buffer([(v >> 8) & 0xff]), + new Buffer([v & 0xff]), + new Buffer([this.depth]), + this.parentFingerprint, + new Buffer([this.childIndex >>> 24]), + new Buffer([(this.childIndex >>> 16) & 0xff]), + new Buffer([(this.childIndex >>> 8) & 0xff]), + new Buffer([this.childIndex & 0xff]), + this.chainCode, + this.eckey.public + ]); } HierarchicalKey.prototype.extendedPublicKeyString = function(format) { @@ -174,16 +178,21 @@ HierarchicalKey.prototype.buildExtendedPrivateKey = function() { var v = this.version; - var r = new BufferPut(); - r = r.word32be(v); - r = r.word8(this.depth); - r = r.put(this.parentFingerprint); - r = r.word32be(this.childIndex); - r = r.put(this.chainCode); - r = r.word8(0); - r = r.put(this.eckey.private); - - this.extendedPrivateKey = r.buffer(); + this.extendedPrivateKey = Buffer.concat([ + new Buffer([v >> 24]), + new Buffer([(v >> 16) & 0xff]), + new Buffer([(v >> 8) & 0xff]), + new Buffer([v & 0xff]), + new Buffer([this.depth]), + this.parentFingerprint, + new Buffer([this.childIndex >>> 24]), + new Buffer([(this.childIndex >>> 16) & 0xff]), + new Buffer([(this.childIndex >>> 8) & 0xff]), + new Buffer([this.childIndex & 0xff]), + this.chainCode, + new Buffer([0]), + this.eckey.private + ]); } HierarchicalKey.prototype.extendedPrivateKeyString = function(format) { @@ -211,17 +220,16 @@ HierarchicalKey.prototype.derive = function(path) { for (var i in e) { var c = e[i]; - if (i == 0) { + if (i == 0 ) { if (c != 'm') throw new Error('invalid path'); continue; } - var usePrivate = (c.length > 1) && (c[c.length - 1] == '\''); + var usePrivate = (c.length > 1) && (c[c.length-1] == '\''); var childIndex = parseInt(usePrivate ? c.slice(0, c.length - 1) : c) & 0x7fffffff; - if (usePrivate) { + if (usePrivate) childIndex += 0x80000000; - } hkey = hkey.deriveChild(childIndex); } @@ -233,15 +241,15 @@ HierarchicalKey.prototype.deriveChild = function(i) { var ib = []; ib.push((i >> 24) & 0xff); ib.push((i >> 16) & 0xff); - ib.push((i >> 8) & 0xff); + ib.push((i >> 8) & 0xff); ib.push(i & 0xff); ib = new Buffer(ib); var usePrivate = (i & 0x80000000) != 0; - var isPrivate = - (this.version == networks['livenet'].hkeyPrivateVersion || - this.version == networks['testnet'].hkeyPrivateVersion); + var isPrivate = + (this.version == networks['livenet'].hkeyPrivateVersion || + this.version == networks['testnet'].hkeyPrivateVersion ); if (usePrivate && (!this.hasPrivateKey || !isPrivate)) throw new Error('Cannot do private key derivation without private key'); @@ -257,24 +265,18 @@ HierarchicalKey.prototype.deriveChild = function(i) { } var hash = coinUtil.sha512hmac(data, this.chainCode); - var il = bignum.fromBuffer(hash.slice(0, 32), { - size: 32 - }); + var il = bignum.fromBuffer(hash.slice(0, 32), {size: 32}); var ir = hash.slice(32, 64); // ki = IL + kpar (mod n). - var priv = bignum.fromBuffer(this.eckey.private, { - size: 32 - }); + var priv = bignum.fromBuffer(this.eckey.private, {size: 32}); var k = il.add(priv).mod(secp256k1_n); ret = new HierarchicalKey(null); ret.chainCode = ir; ret.eckey = new Key(); - ret.eckey.private = k.toBuffer({ - size: 64 - }); + ret.eckey.private = k.toBuffer({size: 32}); ret.eckey.regenerateSync(); ret.hasPrivateKey = true; @@ -300,14 +302,14 @@ HierarchicalKey.prototype.deriveChild = function(i) { ret.chainCode = new Buffer(ir); var eckey = new Key(); - eckey.public = newpub; + eckey.public = newpub; eckey.compressed = true; ret.eckey = eckey; ret.hasPrivateKey = false; } ret.childIndex = i; - ret.parentFingerprint = this.pubKeyHash.slice(0, 4); + ret.parentFingerprint = this.pubKeyHash.slice(0,4); ret.version = this.version; ret.depth = this.depth + 1; @@ -316,6 +318,7 @@ HierarchicalKey.prototype.deriveChild = function(i) { ret.buildExtendedPublicKey(); ret.buildExtendedPrivateKey(); + return ret; } @@ -331,20 +334,9 @@ function uint(f, size) { return n; } -function u8(f) { - return uint(f, 1); -} - -function u16(f) { - return uint(f, 2); -} - -function u32(f) { - return uint(f, 4); -} - -function u64(f) { - return uint(f, 8); -} +function u8(f) {return uint(f,1);} +function u16(f) {return uint(f,2);} +function u32(f) {return uint(f,4);} +function u64(f) {return uint(f,8);} module.exports = require('soop')(HierarchicalKey);