|
|
@ -22,12 +22,12 @@ var BIP32 = function BIP32(obj) { |
|
|
|
BIP32.prototype.set = function(obj) { |
|
|
|
this.version = typeof obj.version !== 'undefined' ? obj.version : this.version; |
|
|
|
this.depth = typeof obj.depth !== 'undefined' ? obj.depth : this.depth; |
|
|
|
this.parentFingerprint = obj.parentFingerprint || this.parentFingerprint; |
|
|
|
this.childIndex = obj.childIndex || this.childIndex; |
|
|
|
this.chainCode = obj.chainCode || this.chainCode; |
|
|
|
this.parentfingerprint = obj.parentfingerprint || this.parentfingerprint; |
|
|
|
this.childindex = obj.childindex || this.childindex; |
|
|
|
this.chaincode = obj.chaincode || this.chaincode; |
|
|
|
this.keypair = obj.keypair || this.keypair; |
|
|
|
this.hasPrivateKey = typeof obj.hasPrivateKey !== 'undefined' ? obj.hasPrivateKey : this.hasPrivateKey; |
|
|
|
this.pubKeyHash = obj.pubKeyHash || this.pubKeyHash; |
|
|
|
this.hasprivkey = typeof obj.hasprivkey !== 'undefined' ? obj.hasprivkey : this.hasprivkey; |
|
|
|
this.pubkeyhash = obj.pubkeyhash || this.pubkeyhash; |
|
|
|
this.xpubkey = obj.xpubkey || this.xpubkey; |
|
|
|
this.xprivkey = obj.xprivkey || this.xprivkey; |
|
|
|
return this; |
|
|
@ -38,12 +38,12 @@ BIP32.prototype.fromRandom = function(networkstr) { |
|
|
|
networkstr = 'mainnet'; |
|
|
|
this.version = constants[networkstr].bip32privkey; |
|
|
|
this.depth = 0x00; |
|
|
|
this.parentFingerprint = new Buffer([0, 0, 0, 0]); |
|
|
|
this.childIndex = new Buffer([0, 0, 0, 0]); |
|
|
|
this.chainCode = Random.getRandomBuffer(32); |
|
|
|
this.parentfingerprint = new Buffer([0, 0, 0, 0]); |
|
|
|
this.childindex = new Buffer([0, 0, 0, 0]); |
|
|
|
this.chaincode = Random.getRandomBuffer(32); |
|
|
|
this.keypair = (new Keypair()).fromRandom(); |
|
|
|
this.hasPrivateKey = true; |
|
|
|
this.pubKeyHash = Hash.sha256ripemd160(this.keypair.pubkey.toBuffer()); |
|
|
|
this.hasprivkey = true; |
|
|
|
this.pubkeyhash = Hash.sha256ripemd160(this.keypair.pubkey.toBuffer()); |
|
|
|
this.buildxpubkey(); |
|
|
|
this.buildxprivkey(); |
|
|
|
}; |
|
|
@ -67,15 +67,15 @@ BIP32.prototype.fromSeed = function(bytes, networkstr) { |
|
|
|
var hash = Hash.sha512hmac(bytes, new Buffer('Bitcoin seed')); |
|
|
|
|
|
|
|
this.depth = 0x00; |
|
|
|
this.parentFingerprint = new Buffer([0, 0, 0, 0]); |
|
|
|
this.childIndex = new Buffer([0, 0, 0, 0]); |
|
|
|
this.chainCode = hash.slice(32, 64); |
|
|
|
this.parentfingerprint = new Buffer([0, 0, 0, 0]); |
|
|
|
this.childindex = new Buffer([0, 0, 0, 0]); |
|
|
|
this.chaincode = hash.slice(32, 64); |
|
|
|
this.version = constants[networkstr].bip32privkey; |
|
|
|
this.keypair = new Keypair(); |
|
|
|
this.keypair.privkey = new Privkey({bn: bn.fromBuffer(hash.slice(0, 32))}); |
|
|
|
this.keypair.privkey2pubkey(); |
|
|
|
this.hasPrivateKey = true; |
|
|
|
this.pubKeyHash = Hash.sha256ripemd160(this.keypair.pubkey.toBuffer()); |
|
|
|
this.hasprivkey = true; |
|
|
|
this.pubkeyhash = Hash.sha256ripemd160(this.keypair.pubkey.toBuffer()); |
|
|
|
|
|
|
|
this.buildxpubkey(); |
|
|
|
this.buildxprivkey(); |
|
|
@ -90,9 +90,9 @@ BIP32.prototype.initFromBytes = function(bytes) { |
|
|
|
|
|
|
|
this.version = bytes.slice(0, 4).readUInt32BE(0); |
|
|
|
this.depth = bytes.slice(4, 5).readUInt8(0); |
|
|
|
this.parentFingerprint = bytes.slice(5, 9); |
|
|
|
this.childIndex = bytes.slice(9, 13).readUInt32BE(0); |
|
|
|
this.chainCode = bytes.slice(13, 45); |
|
|
|
this.parentfingerprint = bytes.slice(5, 9); |
|
|
|
this.childindex = bytes.slice(9, 13).readUInt32BE(0); |
|
|
|
this.chaincode = bytes.slice(13, 45); |
|
|
|
|
|
|
|
var keyBytes = bytes.slice(45, 78); |
|
|
|
|
|
|
@ -108,13 +108,13 @@ BIP32.prototype.initFromBytes = function(bytes) { |
|
|
|
this.keypair = new Keypair(); |
|
|
|
this.keypair.privkey = new Privkey({bn: bn.fromBuffer(keyBytes.slice(1, 33))}); |
|
|
|
this.keypair.privkey2pubkey(); |
|
|
|
this.pubKeyHash = Hash.sha256ripemd160(this.keypair.pubkey.toBuffer()); |
|
|
|
this.hasPrivateKey = true; |
|
|
|
this.pubkeyhash = Hash.sha256ripemd160(this.keypair.pubkey.toBuffer()); |
|
|
|
this.hasprivkey = true; |
|
|
|
} else if (isPublic && (keyBytes[0] == 0x02 || keyBytes[0] == 0x03)) { |
|
|
|
this.keypair = new Keypair(); |
|
|
|
this.keypair.pubkey = (new Pubkey()).fromDER(keyBytes); |
|
|
|
this.pubKeyHash = Hash.sha256ripemd160(this.keypair.pubkey.toBuffer()); |
|
|
|
this.hasPrivateKey = false; |
|
|
|
this.pubkeyhash = Hash.sha256ripemd160(this.keypair.pubkey.toBuffer()); |
|
|
|
this.hasprivkey = false; |
|
|
|
} else { |
|
|
|
throw new Error('Invalid key'); |
|
|
|
} |
|
|
@ -147,12 +147,12 @@ BIP32.prototype.buildxpubkey = function() { |
|
|
|
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.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.keypair.pubkey.toBuffer() |
|
|
|
]); |
|
|
|
} |
|
|
@ -168,7 +168,7 @@ BIP32.prototype.xpubkeyString = function(format) { |
|
|
|
} |
|
|
|
|
|
|
|
BIP32.prototype.buildxprivkey = function() { |
|
|
|
if (!this.hasPrivateKey) return; |
|
|
|
if (!this.hasprivkey) return; |
|
|
|
this.xprivkey = new Buffer([]); |
|
|
|
|
|
|
|
var v = this.version; |
|
|
@ -179,12 +179,12 @@ BIP32.prototype.buildxprivkey = function() { |
|
|
|
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.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.keypair.privkey.bn.toBuffer({size: 32}) |
|
|
|
]); |
|
|
@ -221,12 +221,12 @@ BIP32.prototype.derive = function(path) { |
|
|
|
throw new Error('invalid path'); |
|
|
|
|
|
|
|
var usePrivate = (c.length > 1) && (c[c.length - 1] == '\''); |
|
|
|
var childIndex = parseInt(usePrivate ? c.slice(0, c.length - 1) : c) & 0x7fffffff; |
|
|
|
var childindex = parseInt(usePrivate ? c.slice(0, c.length - 1) : c) & 0x7fffffff; |
|
|
|
|
|
|
|
if (usePrivate) |
|
|
|
childIndex += 0x80000000; |
|
|
|
childindex += 0x80000000; |
|
|
|
|
|
|
|
bip32 = bip32.deriveChild(childIndex); |
|
|
|
bip32 = bip32.deriveChild(childindex); |
|
|
|
} |
|
|
|
|
|
|
|
return bip32; |
|
|
@ -249,11 +249,11 @@ BIP32.prototype.deriveChild = function(i) { |
|
|
|
(this.version == constants.mainnet.bip32privkey || |
|
|
|
this.version == constants.testnet.bip32privkey); |
|
|
|
|
|
|
|
if (usePrivate && (!this.hasPrivateKey || !isPrivate)) |
|
|
|
if (usePrivate && (!this.hasprivkey || !isPrivate)) |
|
|
|
throw new Error('Cannot do private key derivation without private key'); |
|
|
|
|
|
|
|
var ret = null; |
|
|
|
if (this.hasPrivateKey) { |
|
|
|
if (this.hasprivkey) { |
|
|
|
var data = null; |
|
|
|
|
|
|
|
if (usePrivate) { |
|
|
@ -262,7 +262,7 @@ BIP32.prototype.deriveChild = function(i) { |
|
|
|
data = Buffer.concat([this.keypair.pubkey.toBuffer({size: 32}), ib]); |
|
|
|
} |
|
|
|
|
|
|
|
var hash = Hash.sha512hmac(data, this.chainCode); |
|
|
|
var hash = Hash.sha512hmac(data, this.chaincode); |
|
|
|
var il = bn.fromBuffer(hash.slice(0, 32), {size: 32}); |
|
|
|
var ir = hash.slice(32, 64); |
|
|
|
|
|
|
@ -270,16 +270,16 @@ BIP32.prototype.deriveChild = function(i) { |
|
|
|
var k = il.add(this.keypair.privkey.bn).mod(Point.getN()); |
|
|
|
|
|
|
|
ret = new BIP32(); |
|
|
|
ret.chainCode = ir; |
|
|
|
ret.chaincode = ir; |
|
|
|
|
|
|
|
ret.keypair = new Keypair(); |
|
|
|
ret.keypair.privkey = new Privkey({bn: k}); |
|
|
|
ret.keypair.privkey2pubkey(); |
|
|
|
ret.hasPrivateKey = true; |
|
|
|
ret.hasprivkey = true; |
|
|
|
|
|
|
|
} else { |
|
|
|
var data = Buffer.concat([this.keypair.pubkey.toBuffer(), ib]); |
|
|
|
var hash = Hash.sha512hmac(data, this.chainCode); |
|
|
|
var hash = Hash.sha512hmac(data, this.chaincode); |
|
|
|
var il = bn(hash.slice(0, 32)); |
|
|
|
var ir = hash.slice(32, 64); |
|
|
|
|
|
|
@ -291,20 +291,20 @@ BIP32.prototype.deriveChild = function(i) { |
|
|
|
newpub.point = Ki; |
|
|
|
|
|
|
|
ret = new BIP32(); |
|
|
|
ret.chainCode = ir; |
|
|
|
ret.chaincode = ir; |
|
|
|
|
|
|
|
var keypair = new Keypair(); |
|
|
|
keypair.pubkey = newpub; |
|
|
|
ret.keypair = keypair; |
|
|
|
ret.hasPrivateKey = false; |
|
|
|
ret.hasprivkey = false; |
|
|
|
} |
|
|
|
|
|
|
|
ret.childIndex = i; |
|
|
|
ret.parentFingerprint = this.pubKeyHash.slice(0, 4); |
|
|
|
ret.childindex = i; |
|
|
|
ret.parentfingerprint = this.pubkeyhash.slice(0, 4); |
|
|
|
ret.version = this.version; |
|
|
|
ret.depth = this.depth + 1; |
|
|
|
|
|
|
|
ret.pubKeyHash = Hash.sha256ripemd160(ret.keypair.pubkey.toBuffer()); |
|
|
|
ret.pubkeyhash = Hash.sha256ripemd160(ret.keypair.pubkey.toBuffer()); |
|
|
|
|
|
|
|
ret.buildxpubkey(); |
|
|
|
ret.buildxprivkey(); |
|
|
|