Key -> Keypair
This commit is contained in:
parent
7390b15f89
commit
bd16eddf54
56
lib/bip32.js
56
lib/bip32.js
@ -25,7 +25,7 @@ BIP32.prototype.set = function(obj) {
|
|||||||
this.parentFingerprint = obj.parentFingerprint || this.parentFingerprint;
|
this.parentFingerprint = obj.parentFingerprint || this.parentFingerprint;
|
||||||
this.childIndex = obj.childIndex || this.childIndex;
|
this.childIndex = obj.childIndex || this.childIndex;
|
||||||
this.chainCode = obj.chainCode || this.chainCode;
|
this.chainCode = obj.chainCode || this.chainCode;
|
||||||
this.key = obj.key || this.key;
|
this.keypair = obj.keypair || this.keypair;
|
||||||
this.hasPrivateKey = typeof obj.hasPrivateKey !== 'undefined' ? obj.hasPrivateKey : this.hasPrivateKey;
|
this.hasPrivateKey = typeof obj.hasPrivateKey !== 'undefined' ? obj.hasPrivateKey : this.hasPrivateKey;
|
||||||
this.pubKeyHash = obj.pubKeyHash || this.pubKeyHash;
|
this.pubKeyHash = obj.pubKeyHash || this.pubKeyHash;
|
||||||
this.extendedPublicKey = obj.extendedPublicKey || this.extendedPublicKey;
|
this.extendedPublicKey = obj.extendedPublicKey || this.extendedPublicKey;
|
||||||
@ -41,9 +41,9 @@ BIP32.prototype.fromRandom = function(networkstr) {
|
|||||||
this.parentFingerprint = new Buffer([0, 0, 0, 0]);
|
this.parentFingerprint = new Buffer([0, 0, 0, 0]);
|
||||||
this.childIndex = new Buffer([0, 0, 0, 0]);
|
this.childIndex = new Buffer([0, 0, 0, 0]);
|
||||||
this.chainCode = Random.getRandomBuffer(32);
|
this.chainCode = Random.getRandomBuffer(32);
|
||||||
this.key = (new Keypair()).fromRandom();
|
this.keypair = (new Keypair()).fromRandom();
|
||||||
this.hasPrivateKey = true;
|
this.hasPrivateKey = true;
|
||||||
this.pubKeyHash = Hash.sha256ripemd160(this.key.pubkey.toBuffer());
|
this.pubKeyHash = Hash.sha256ripemd160(this.keypair.pubkey.toBuffer());
|
||||||
this.buildExtendedPublicKey();
|
this.buildExtendedPublicKey();
|
||||||
this.buildExtendedPrivateKey();
|
this.buildExtendedPrivateKey();
|
||||||
};
|
};
|
||||||
@ -71,11 +71,11 @@ BIP32.prototype.fromSeed = function(bytes, networkstr) {
|
|||||||
this.childIndex = new Buffer([0, 0, 0, 0]);
|
this.childIndex = new Buffer([0, 0, 0, 0]);
|
||||||
this.chainCode = hash.slice(32, 64);
|
this.chainCode = hash.slice(32, 64);
|
||||||
this.version = constants[networkstr].bip32privkey;
|
this.version = constants[networkstr].bip32privkey;
|
||||||
this.key = new Keypair();
|
this.keypair = new Keypair();
|
||||||
this.key.privkey = new Privkey({bn: bn.fromBuffer(hash.slice(0, 32))});
|
this.keypair.privkey = new Privkey({bn: bn.fromBuffer(hash.slice(0, 32))});
|
||||||
this.key.privkey2pubkey();
|
this.keypair.privkey2pubkey();
|
||||||
this.hasPrivateKey = true;
|
this.hasPrivateKey = true;
|
||||||
this.pubKeyHash = Hash.sha256ripemd160(this.key.pubkey.toBuffer());
|
this.pubKeyHash = Hash.sha256ripemd160(this.keypair.pubkey.toBuffer());
|
||||||
|
|
||||||
this.buildExtendedPublicKey();
|
this.buildExtendedPublicKey();
|
||||||
this.buildExtendedPrivateKey();
|
this.buildExtendedPrivateKey();
|
||||||
@ -105,15 +105,15 @@ BIP32.prototype.initFromBytes = function(bytes) {
|
|||||||
this.version == constants.testnet.bip32pubkey);
|
this.version == constants.testnet.bip32pubkey);
|
||||||
|
|
||||||
if (isPrivate && keyBytes[0] == 0) {
|
if (isPrivate && keyBytes[0] == 0) {
|
||||||
this.key = new Keypair();
|
this.keypair = new Keypair();
|
||||||
this.key.privkey = new Privkey({bn: bn.fromBuffer(keyBytes.slice(1, 33))});
|
this.keypair.privkey = new Privkey({bn: bn.fromBuffer(keyBytes.slice(1, 33))});
|
||||||
this.key.privkey2pubkey();
|
this.keypair.privkey2pubkey();
|
||||||
this.pubKeyHash = Hash.sha256ripemd160(this.key.pubkey.toBuffer());
|
this.pubKeyHash = Hash.sha256ripemd160(this.keypair.pubkey.toBuffer());
|
||||||
this.hasPrivateKey = true;
|
this.hasPrivateKey = true;
|
||||||
} else if (isPublic && (keyBytes[0] == 0x02 || keyBytes[0] == 0x03)) {
|
} else if (isPublic && (keyBytes[0] == 0x02 || keyBytes[0] == 0x03)) {
|
||||||
this.key = new Keypair();
|
this.keypair = new Keypair();
|
||||||
this.key.pubkey = (new Pubkey()).fromDER(keyBytes);
|
this.keypair.pubkey = (new Pubkey()).fromDER(keyBytes);
|
||||||
this.pubKeyHash = Hash.sha256ripemd160(this.key.pubkey.toBuffer());
|
this.pubKeyHash = Hash.sha256ripemd160(this.keypair.pubkey.toBuffer());
|
||||||
this.hasPrivateKey = false;
|
this.hasPrivateKey = false;
|
||||||
} else {
|
} else {
|
||||||
throw new Error('Invalid key');
|
throw new Error('Invalid key');
|
||||||
@ -153,7 +153,7 @@ BIP32.prototype.buildExtendedPublicKey = function() {
|
|||||||
new Buffer([(this.childIndex >>> 8) & 0xff]),
|
new Buffer([(this.childIndex >>> 8) & 0xff]),
|
||||||
new Buffer([this.childIndex & 0xff]),
|
new Buffer([this.childIndex & 0xff]),
|
||||||
this.chainCode,
|
this.chainCode,
|
||||||
this.key.pubkey.toBuffer()
|
this.keypair.pubkey.toBuffer()
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -186,7 +186,7 @@ BIP32.prototype.buildExtendedPrivateKey = function() {
|
|||||||
new Buffer([this.childIndex & 0xff]),
|
new Buffer([this.childIndex & 0xff]),
|
||||||
this.chainCode,
|
this.chainCode,
|
||||||
new Buffer([0]),
|
new Buffer([0]),
|
||||||
this.key.privkey.bn.toBuffer({size: 32})
|
this.keypair.privkey.bn.toBuffer({size: 32})
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -257,9 +257,9 @@ BIP32.prototype.deriveChild = function(i) {
|
|||||||
var data = null;
|
var data = null;
|
||||||
|
|
||||||
if (usePrivate) {
|
if (usePrivate) {
|
||||||
data = Buffer.concat([new Buffer([0]), this.key.privkey.bn.toBuffer({size: 32}), ib]);
|
data = Buffer.concat([new Buffer([0]), this.keypair.privkey.bn.toBuffer({size: 32}), ib]);
|
||||||
} else {
|
} else {
|
||||||
data = Buffer.concat([this.key.pubkey.toBuffer({size: 32}), ib]);
|
data = Buffer.concat([this.keypair.pubkey.toBuffer({size: 32}), ib]);
|
||||||
}
|
}
|
||||||
|
|
||||||
var hash = Hash.sha512hmac(data, this.chainCode);
|
var hash = Hash.sha512hmac(data, this.chainCode);
|
||||||
@ -267,25 +267,25 @@ BIP32.prototype.deriveChild = function(i) {
|
|||||||
var ir = hash.slice(32, 64);
|
var ir = hash.slice(32, 64);
|
||||||
|
|
||||||
// ki = IL + kpar (mod n).
|
// ki = IL + kpar (mod n).
|
||||||
var k = il.add(this.key.privkey.bn).mod(Point.getN());
|
var k = il.add(this.keypair.privkey.bn).mod(Point.getN());
|
||||||
|
|
||||||
ret = new BIP32();
|
ret = new BIP32();
|
||||||
ret.chainCode = ir;
|
ret.chainCode = ir;
|
||||||
|
|
||||||
ret.key = new Keypair();
|
ret.keypair = new Keypair();
|
||||||
ret.key.privkey = new Privkey({bn: k});
|
ret.keypair.privkey = new Privkey({bn: k});
|
||||||
ret.key.privkey2pubkey();
|
ret.keypair.privkey2pubkey();
|
||||||
ret.hasPrivateKey = true;
|
ret.hasPrivateKey = true;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
var data = Buffer.concat([this.key.pubkey.toBuffer(), ib]);
|
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 il = bn(hash.slice(0, 32));
|
||||||
var ir = hash.slice(32, 64);
|
var ir = hash.slice(32, 64);
|
||||||
|
|
||||||
// Ki = (IL + kpar)*G = IL*G + Kpar
|
// Ki = (IL + kpar)*G = IL*G + Kpar
|
||||||
var ilG = Point.getG().mul(il);
|
var ilG = Point.getG().mul(il);
|
||||||
var Kpar = this.key.pubkey.point;
|
var Kpar = this.keypair.pubkey.point;
|
||||||
var Ki = ilG.add(Kpar);
|
var Ki = ilG.add(Kpar);
|
||||||
var newpub = new Pubkey();
|
var newpub = new Pubkey();
|
||||||
newpub.point = Ki;
|
newpub.point = Ki;
|
||||||
@ -293,9 +293,9 @@ BIP32.prototype.deriveChild = function(i) {
|
|||||||
ret = new BIP32();
|
ret = new BIP32();
|
||||||
ret.chainCode = ir;
|
ret.chainCode = ir;
|
||||||
|
|
||||||
var key = new Keypair();
|
var keypair = new Keypair();
|
||||||
key.pubkey = newpub;
|
keypair.pubkey = newpub;
|
||||||
ret.key = key;
|
ret.keypair = keypair;
|
||||||
ret.hasPrivateKey = false;
|
ret.hasPrivateKey = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -304,7 +304,7 @@ BIP32.prototype.deriveChild = function(i) {
|
|||||||
ret.version = this.version;
|
ret.version = this.version;
|
||||||
ret.depth = this.depth + 1;
|
ret.depth = this.depth + 1;
|
||||||
|
|
||||||
ret.pubKeyHash = Hash.sha256ripemd160(ret.key.pubkey.toBuffer());
|
ret.pubKeyHash = Hash.sha256ripemd160(ret.keypair.pubkey.toBuffer());
|
||||||
|
|
||||||
ret.buildExtendedPublicKey();
|
ret.buildExtendedPublicKey();
|
||||||
ret.buildExtendedPrivateKey();
|
ret.buildExtendedPrivateKey();
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user