suggestions by ryan

This commit is contained in:
Manuel Araoz 2014-06-12 16:54:59 -03:00
parent b402bbfa6c
commit 0d670ff979
2 changed files with 155 additions and 171 deletions

View File

@ -4558,7 +4558,6 @@ var Point = imports.Point || require('./Point');
var SecureRandom = imports.SecureRandom || require('./SecureRandom'); var SecureRandom = imports.SecureRandom || require('./SecureRandom');
var bignum = imports.bignum || require('bignum'); var bignum = imports.bignum || require('bignum');
var networks = require('../networks'); var networks = require('../networks');
var BufferPut = require('bufferput');
var secp256k1_n = new bignum('FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141', 16); var secp256k1_n = new bignum('FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141', 16);
var secp256k1_Gx = new bignum('79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798', 16); var secp256k1_Gx = new bignum('79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798', 16);
@ -4572,7 +4571,8 @@ var HierarchicalKey = function(bytes) {
if (typeof bytes == 'undefined' || bytes == 'mainnet' || bytes == 'livenet') { if (typeof bytes == 'undefined' || bytes == 'mainnet' || bytes == 'livenet') {
bytes = 'livenet'; bytes = 'livenet';
this.version = networks['livenet'].hkeyPrivateVersion; this.version = networks['livenet'].hkeyPrivateVersion;
} else if (bytes == 'testnet') { }
else if (bytes == 'testnet') {
this.version = networks['testnet'].hkeyPrivateVersion; this.version = networks['testnet'].hkeyPrivateVersion;
} }
if (bytes == 'livenet' || bytes == 'testnet') { if (bytes == 'livenet' || bytes == 'testnet') {
@ -4694,17 +4694,21 @@ HierarchicalKey.prototype.buildExtendedPublicKey = function() {
throw new Error('Unknown version'); throw new Error('Unknown version');
} }
var r = new BufferPut(); // Version
r = r.word32be(v); this.extendedPublicKey = Buffer.concat([
r = r.word8(this.depth); new Buffer([v >> 24]),
r = r.put(this.parentFingerprint); new Buffer([(v >> 16) & 0xff]),
r = r.word32be(this.childIndex); new Buffer([(v >> 8) & 0xff]),
r = r.put(this.chainCode); new Buffer([v & 0xff]),
r = r.put(this.eckey.public); new Buffer([this.depth]),
this.parentFingerprint,
new Buffer([this.childIndex >>> 24]),
this.extendedPublicKey = new Buffer(0); new Buffer([(this.childIndex >>> 16) & 0xff]),
this.extendedPublicKey = r.buffer(); new Buffer([(this.childIndex >>> 8) & 0xff]),
new Buffer([this.childIndex & 0xff]),
this.chainCode,
this.eckey.public
]);
} }
HierarchicalKey.prototype.extendedPublicKeyString = function(format) { HierarchicalKey.prototype.extendedPublicKeyString = function(format) {
@ -4726,16 +4730,21 @@ HierarchicalKey.prototype.buildExtendedPrivateKey = function() {
var v = this.version; var v = this.version;
var r = new BufferPut(); this.extendedPrivateKey = Buffer.concat([
r = r.word32be(v); new Buffer([v >> 24]),
r = r.word8(this.depth); new Buffer([(v >> 16) & 0xff]),
r = r.put(this.parentFingerprint); new Buffer([(v >> 8) & 0xff]),
r = r.word32be(this.childIndex); new Buffer([v & 0xff]),
r = r.put(this.chainCode); new Buffer([this.depth]),
r = r.word8(0); this.parentFingerprint,
r = r.put(this.eckey.private); new Buffer([this.childIndex >>> 24]),
new Buffer([(this.childIndex >>> 16) & 0xff]),
this.extendedPrivateKey = r.buffer(); new Buffer([(this.childIndex >>> 8) & 0xff]),
new Buffer([this.childIndex & 0xff]),
this.chainCode,
new Buffer([0]),
this.eckey.private
]);
} }
HierarchicalKey.prototype.extendedPrivateKeyString = function(format) { HierarchicalKey.prototype.extendedPrivateKeyString = function(format) {
@ -4771,9 +4780,8 @@ HierarchicalKey.prototype.derive = function(path) {
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; var childIndex = parseInt(usePrivate ? c.slice(0, c.length - 1) : c) & 0x7fffffff;
if (usePrivate) { if (usePrivate)
childIndex += 0x80000000; childIndex += 0x80000000;
}
hkey = hkey.deriveChild(childIndex); hkey = hkey.deriveChild(childIndex);
} }
@ -4809,24 +4817,18 @@ HierarchicalKey.prototype.deriveChild = function(i) {
} }
var hash = coinUtil.sha512hmac(data, this.chainCode); var hash = coinUtil.sha512hmac(data, this.chainCode);
var il = bignum.fromBuffer(hash.slice(0, 32), { var il = bignum.fromBuffer(hash.slice(0, 32), {size: 32});
size: 32
});
var ir = hash.slice(32, 64); var ir = hash.slice(32, 64);
// ki = IL + kpar (mod n). // ki = IL + kpar (mod n).
var priv = bignum.fromBuffer(this.eckey.private, { var priv = bignum.fromBuffer(this.eckey.private, {size: 32});
size: 32
});
var k = il.add(priv).mod(secp256k1_n); var k = il.add(priv).mod(secp256k1_n);
ret = new HierarchicalKey(null); ret = new HierarchicalKey(null);
ret.chainCode = ir; ret.chainCode = ir;
ret.eckey = new Key(); ret.eckey = new Key();
ret.eckey.private = k.toBuffer({ ret.eckey.private = k.toBuffer({size: 32});
size: 64
});
ret.eckey.regenerateSync(); ret.eckey.regenerateSync();
ret.hasPrivateKey = true; ret.hasPrivateKey = true;
@ -4868,6 +4870,7 @@ HierarchicalKey.prototype.deriveChild = function(i) {
ret.buildExtendedPublicKey(); ret.buildExtendedPublicKey();
ret.buildExtendedPrivateKey(); ret.buildExtendedPrivateKey();
return ret; return ret;
} }
@ -4883,26 +4886,15 @@ function uint(f, size) {
return n; return n;
} }
function u8(f) { function u8(f) {return uint(f,1);}
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 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); module.exports = require('soop')(HierarchicalKey);
}).call(this,require("buffer").Buffer) }).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){ (function (Buffer){
'use strict'; 'use strict';
var imports = require('soop').imports(); var imports = require('soop').imports();

View File

@ -6,7 +6,6 @@ var Point = imports.Point || require('./Point');
var SecureRandom = imports.SecureRandom || require('./SecureRandom'); var SecureRandom = imports.SecureRandom || require('./SecureRandom');
var bignum = imports.bignum || require('bignum'); var bignum = imports.bignum || require('bignum');
var networks = require('../networks'); var networks = require('../networks');
var BufferPut = require('bufferput');
var secp256k1_n = new bignum('FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141', 16); var secp256k1_n = new bignum('FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141', 16);
var secp256k1_Gx = new bignum('79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798', 16); var secp256k1_Gx = new bignum('79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798', 16);
@ -20,7 +19,8 @@ var HierarchicalKey = function(bytes) {
if (typeof bytes == 'undefined' || bytes == 'mainnet' || bytes == 'livenet') { if (typeof bytes == 'undefined' || bytes == 'mainnet' || bytes == 'livenet') {
bytes = 'livenet'; bytes = 'livenet';
this.version = networks['livenet'].hkeyPrivateVersion; this.version = networks['livenet'].hkeyPrivateVersion;
} else if (bytes == 'testnet') { }
else if (bytes == 'testnet') {
this.version = networks['testnet'].hkeyPrivateVersion; this.version = networks['testnet'].hkeyPrivateVersion;
} }
if (bytes == 'livenet' || bytes == 'testnet') { if (bytes == 'livenet' || bytes == 'testnet') {
@ -142,17 +142,21 @@ HierarchicalKey.prototype.buildExtendedPublicKey = function() {
throw new Error('Unknown version'); throw new Error('Unknown version');
} }
var r = new BufferPut(); // Version
r = r.word32be(v); this.extendedPublicKey = Buffer.concat([
r = r.word8(this.depth); new Buffer([v >> 24]),
r = r.put(this.parentFingerprint); new Buffer([(v >> 16) & 0xff]),
r = r.word32be(this.childIndex); new Buffer([(v >> 8) & 0xff]),
r = r.put(this.chainCode); new Buffer([v & 0xff]),
r = r.put(this.eckey.public); new Buffer([this.depth]),
this.parentFingerprint,
new Buffer([this.childIndex >>> 24]),
this.extendedPublicKey = new Buffer(0); new Buffer([(this.childIndex >>> 16) & 0xff]),
this.extendedPublicKey = r.buffer(); new Buffer([(this.childIndex >>> 8) & 0xff]),
new Buffer([this.childIndex & 0xff]),
this.chainCode,
this.eckey.public
]);
} }
HierarchicalKey.prototype.extendedPublicKeyString = function(format) { HierarchicalKey.prototype.extendedPublicKeyString = function(format) {
@ -174,16 +178,21 @@ HierarchicalKey.prototype.buildExtendedPrivateKey = function() {
var v = this.version; var v = this.version;
var r = new BufferPut(); this.extendedPrivateKey = Buffer.concat([
r = r.word32be(v); new Buffer([v >> 24]),
r = r.word8(this.depth); new Buffer([(v >> 16) & 0xff]),
r = r.put(this.parentFingerprint); new Buffer([(v >> 8) & 0xff]),
r = r.word32be(this.childIndex); new Buffer([v & 0xff]),
r = r.put(this.chainCode); new Buffer([this.depth]),
r = r.word8(0); this.parentFingerprint,
r = r.put(this.eckey.private); new Buffer([this.childIndex >>> 24]),
new Buffer([(this.childIndex >>> 16) & 0xff]),
this.extendedPrivateKey = r.buffer(); new Buffer([(this.childIndex >>> 8) & 0xff]),
new Buffer([this.childIndex & 0xff]),
this.chainCode,
new Buffer([0]),
this.eckey.private
]);
} }
HierarchicalKey.prototype.extendedPrivateKeyString = function(format) { HierarchicalKey.prototype.extendedPrivateKeyString = function(format) {
@ -219,9 +228,8 @@ HierarchicalKey.prototype.derive = function(path) {
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; var childIndex = parseInt(usePrivate ? c.slice(0, c.length - 1) : c) & 0x7fffffff;
if (usePrivate) { if (usePrivate)
childIndex += 0x80000000; childIndex += 0x80000000;
}
hkey = hkey.deriveChild(childIndex); hkey = hkey.deriveChild(childIndex);
} }
@ -257,24 +265,18 @@ HierarchicalKey.prototype.deriveChild = function(i) {
} }
var hash = coinUtil.sha512hmac(data, this.chainCode); var hash = coinUtil.sha512hmac(data, this.chainCode);
var il = bignum.fromBuffer(hash.slice(0, 32), { var il = bignum.fromBuffer(hash.slice(0, 32), {size: 32});
size: 32
});
var ir = hash.slice(32, 64); var ir = hash.slice(32, 64);
// ki = IL + kpar (mod n). // ki = IL + kpar (mod n).
var priv = bignum.fromBuffer(this.eckey.private, { var priv = bignum.fromBuffer(this.eckey.private, {size: 32});
size: 32
});
var k = il.add(priv).mod(secp256k1_n); var k = il.add(priv).mod(secp256k1_n);
ret = new HierarchicalKey(null); ret = new HierarchicalKey(null);
ret.chainCode = ir; ret.chainCode = ir;
ret.eckey = new Key(); ret.eckey = new Key();
ret.eckey.private = k.toBuffer({ ret.eckey.private = k.toBuffer({size: 32});
size: 64
});
ret.eckey.regenerateSync(); ret.eckey.regenerateSync();
ret.hasPrivateKey = true; ret.hasPrivateKey = true;
@ -316,6 +318,7 @@ HierarchicalKey.prototype.deriveChild = function(i) {
ret.buildExtendedPublicKey(); ret.buildExtendedPublicKey();
ret.buildExtendedPrivateKey(); ret.buildExtendedPrivateKey();
return ret; return ret;
} }
@ -331,20 +334,9 @@ function uint(f, size) {
return n; return n;
} }
function u8(f) { function u8(f) {return uint(f,1);}
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 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); module.exports = require('soop')(HierarchicalKey);