|
|
@ -6,6 +6,7 @@ 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); |
|
|
@ -19,8 +20,7 @@ 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') { |
|
|
@ -142,20 +142,15 @@ HierarchicalKey.prototype.buildExtendedPublicKey = function() { |
|
|
|
throw new Error('Unknown 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 |
|
|
|
]); |
|
|
|
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 = r.buffer(); |
|
|
|
console.log('a'); |
|
|
|
} |
|
|
|
|
|
|
@ -178,21 +173,16 @@ HierarchicalKey.prototype.buildExtendedPrivateKey = function() { |
|
|
|
|
|
|
|
var v = this.version; |
|
|
|
|
|
|
|
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 |
|
|
|
]); |
|
|
|
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(); |
|
|
|
} |
|
|
|
|
|
|
|
HierarchicalKey.prototype.extendedPrivateKeyString = function(format) { |
|
|
@ -266,18 +256,24 @@ 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: 32}); |
|
|
|
ret.eckey.private = k.toBuffer({ |
|
|
|
size: 32 |
|
|
|
}); |
|
|
|
ret.eckey.regenerateSync(); |
|
|
|
ret.hasPrivateKey = true; |
|
|
|
|
|
|
@ -335,9 +331,20 @@ 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); |
|
|
|