Browse Source

bip32

code from bitcore. original implementation derived from here:
https://github.com/sarchar/brainwallet.github.com/blob/bip32/js/bip32.js
patch-2
Ryan X. Charles 10 years ago
parent
commit
9649cc58e9
  1. 336
      lib/bip32.js
  2. 1
      lib/key.js
  3. 5
      lib/pubkey.js
  4. 322
      test/test.bip32.js
  5. 11
      test/test.pubkey.js

336
lib/bip32.js

@ -0,0 +1,336 @@
var base58 = require('./base58');
var Hash = require('./hash');
var Key = require('./key');
var Pubkey = require('./pubkey');
var Privkey = require('./privkey');
var Point = require('./point');
var Random = require('./random');
var bn = require('./bn');
var constants = require('./constants');
var BIP32 = function(str) {
if (str === 'testnet' || str === 'mainnet') {
this.version = constants[str].bip32privkey;
this.fromRandom();
}
else if (str)
this.fromString(str);
}
BIP32.prototype.fromRandom = function() {
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.key = (new Key()).fromRandom();
this.hasPrivateKey = true;
this.pubKeyHash = Hash.sha256ripemd160(this.key.pubkey.toBuffer());
this.buildExtendedPublicKey();
this.buildExtendedPrivateKey();
};
BIP32.prototype.fromString = function(str) {
var decoded = base58.decode(str);
if (decoded.length != 82)
throw new Error('Not enough data, expected 82 and received ' + decoded.length);
var checksum = decoded.slice(78, 82);
var bytes = decoded.slice(0, 78);
var hash = Hash.sha256sha256(bytes);
if (hash[0] != checksum[0] || hash[1] != checksum[1] || hash[2] != checksum[2] || hash[3] != checksum[3])
throw new Error('Invalid checksum');
if (bytes !== undefined && bytes !== null)
this.initFromBytes(bytes);
};
BIP32.prototype.fromSeed = function(bytes, network) {
if (!network)
network = 'mainnet';
if (!Buffer.isBuffer(bytes))
throw new Error('bip32: bytes must be a buffer');
if (bytes.length < 128 / 8)
throw new Error('bip32: Need more than 128 bytes of entropy');
if (bytes.length > 512 / 8)
throw new Error('bip32: More than 512 bytes of entropy is nonstandard');
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.version = constants[network].bip32privkey;
this.key = new Key();
this.key.privkey = new Privkey(bn.fromBuffer(hash.slice(0, 32)));
this.key.privkey2pubkey();
this.hasPrivateKey = true;
this.pubKeyHash = Hash.sha256ripemd160(this.key.pubkey.toBuffer());
this.buildExtendedPublicKey();
this.buildExtendedPrivateKey();
return this;
};
BIP32.prototype.initFromBytes = function(bytes) {
// Both pub and private extended keys are 78 bytes
if (bytes.length != 78)
throw new Error('bip32: not enough data');
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);
var keyBytes = bytes.slice(45, 78);
var isPrivate =
(this.version == constants.mainnet.bip32privkey ||
this.version == constants.testnet.bip32privkey);
var isPublic =
(this.version == constants.mainnet.bip32pubkey ||
this.version == constants.testnet.bip32pubkey);
if (isPrivate && keyBytes[0] == 0) {
this.key = new Key();
this.key.privkey = new Privkey(bn.fromBuffer(keyBytes.slice(1, 33)));
this.key.privkey2pubkey();
this.pubKeyHash = Hash.sha256ripemd160(this.key.pubkey.toBuffer());
this.hasPrivateKey = true;
} else if (isPublic && (keyBytes[0] == 0x02 || keyBytes[0] == 0x03)) {
this.key = new Key();
this.key.pubkey = (new Pubkey()).fromDER(keyBytes);
this.pubKeyHash = Hash.sha256ripemd160(this.key.pubkey.toBuffer());
this.hasPrivateKey = false;
} else {
throw new Error('Invalid key');
}
this.buildExtendedPublicKey();
this.buildExtendedPrivateKey();
}
BIP32.prototype.buildExtendedPublicKey = function() {
this.extendedPublicKey = new Buffer([]);
var v = null;
switch (this.version) {
case constants.mainnet.bip32pubkey:
case constants.mainnet.bip32privkey:
v = constants.mainnet.bip32pubkey;
break;
case constants.testnet.bip32pubkey:
case constants.testnet.bip32privkey:
v = constants.testnet.bip32pubkey;
break;
default:
throw new Error('bip32: Unknown version');
}
// 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.key.pubkey.toBuffer()
]);
}
BIP32.prototype.extendedPublicKeyString = function(format) {
if (format === undefined || format === 'base58') {
if (!Buffer.isBuffer(this.extendedPublicKey))
console.log('extendedPublicKey: ' + this.extendedPublicKey);
var hash = Hash.sha256sha256(this.extendedPublicKey);
var checksum = hash.slice(0, 4);
var data = Buffer.concat([this.extendedPublicKey, checksum]);
return base58.encode(data);
} else if (format === 'hex') {
return this.extendedPublicKey.toString('hex');;
} else {
throw new Error('bad format');
}
}
BIP32.prototype.buildExtendedPrivateKey = function() {
if (!this.hasPrivateKey) return;
this.extendedPrivateKey = new Buffer([]);
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.key.privkey.n.toBuffer({size: 32})
]);
}
BIP32.prototype.extendedPrivateKeyString = function(format) {
if (format === undefined || format === 'base58') {
var hash = Hash.sha256sha256(this.extendedPrivateKey);
var checksum = hash.slice(0, 4);
var data = Buffer.concat([this.extendedPrivateKey, checksum]);
return base58.encode(data);
} else if (format === 'hex') {
return this.extendedPrivateKey.toString('hex');
} else {
throw new Error('bad format');
}
}
BIP32.prototype.derive = function(path) {
var e = path.split('/');
// Special cases:
if (path == 'm' || path == 'M' || path == 'm\'' || path == 'M\'')
return this;
var bip32 = this;
for (var i in e) {
var c = e[i];
if (i == 0) {
if (c != 'm') throw new Error('invalid path');
continue;
}
var usePrivate = (c.length > 1) && (c[c.length - 1] == '\'');
var childIndex = parseInt(usePrivate ? c.slice(0, c.length - 1) : c) & 0x7fffffff;
if (usePrivate)
childIndex += 0x80000000;
bip32 = bip32.deriveChild(childIndex);
}
return bip32;
}
BIP32.prototype.deriveChild = function(i) {
var ib = [];
ib.push((i >> 24) & 0xff);
ib.push((i >> 16) & 0xff);
ib.push((i >> 8) & 0xff);
ib.push(i & 0xff);
ib = new Buffer(ib);
var usePrivate = (i & 0x80000000) != 0;
var isPrivate =
(this.version == constants.mainnet.bip32privkey ||
this.version == constants.testnet.bip32privkey);
if (usePrivate && (!this.hasPrivateKey || !isPrivate))
throw new Error('Cannot do private key derivation without private key');
var ret = null;
if (this.hasPrivateKey) {
var data = null;
if (usePrivate) {
data = Buffer.concat([new Buffer([0]), this.key.privkey.n.toBuffer({size: 32}), ib]);
} else {
data = Buffer.concat([this.key.pubkey.toBuffer({size: 32}), ib]);
}
var hash = Hash.sha512hmac(data, this.chainCode);
var il = bn.fromBuffer(hash.slice(0, 32), {size: 32});
var ir = hash.slice(32, 64);
// ki = IL + kpar (mod n).
var k = il.add(this.key.privkey.n).mod(Point.getN());
ret = new BIP32();
ret.chainCode = ir;
ret.key = new Key();
ret.key.privkey = new Privkey(k);
ret.key.privkey2pubkey();
ret.hasPrivateKey = true;
} else {
var data = Buffer.concat([this.key.pubkey.toBuffer(), ib]);
var hash = Hash.sha512hmac(data, this.chainCode);
var il = bn(hash.slice(0, 32));
var ir = hash.slice(32, 64);
// Ki = (IL + kpar)*G = IL*G + Kpar
var ilG = Point.getG().mul(il);
var Kpar = this.key.pubkey.p;
var Ki = ilG.add(Kpar);
var newpub = new Pubkey();
newpub.p = Ki;
ret = new BIP32();
ret.chainCode = ir;
var key = new Key();
key.pubkey = newpub;
ret.key = key;
ret.hasPrivateKey = false;
}
ret.childIndex = i;
ret.parentFingerprint = this.pubKeyHash.slice(0, 4);
ret.version = this.version;
ret.depth = this.depth + 1;
ret.pubKeyHash = Hash.sha256ripemd160(ret.key.pubkey.toBuffer());
ret.buildExtendedPublicKey();
ret.buildExtendedPrivateKey();
return ret;
}
function uint(f, size) {
if (f.length < size)
throw new Error('not enough data');
var n = 0;
for (var i = 0; i < size; i++) {
n *= 256;
n += f[i];
}
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);
}
module.exports = BIP32;

1
lib/key.js

@ -16,6 +16,7 @@ Key.prototype.fromRandom = function() {
var condition = this.privkey.n.lt(point.getN());
} while (!condition);
this.privkey2pubkey();
return this;
};
Key.prototype.fromString = function(str) {

5
lib/pubkey.js

@ -27,6 +27,7 @@ Pubkey.prototype.fromDER = function(buf) {
} else {
throw new Error('pubkey: Invalid DER format pubkey');
}
return this;
};
Pubkey.prototype.fromString = function(str) {
@ -39,6 +40,10 @@ Pubkey.prototype.fromX = function(odd, x) {
this.p = point.fromX(odd, x);
};
Pubkey.prototype.toBuffer = function() {
return this.toDER(true);
};
Pubkey.prototype.toDER = function(compressed) {
if (typeof compressed !== 'boolean')
throw new Error('pubkey: Must specify whether the public key is compressed or not (true or false)');

322
test/test.bip32.js

@ -0,0 +1,322 @@
var should = require('chai').should();
var BIP32 = require('../lib/bip32');
describe('BIP32', function() {
//test vectors: https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki
var vector1_master = '000102030405060708090a0b0c0d0e0f';
var vector1_m_public = 'xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8'
var vector1_m_private = 'xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi';
var vector1_m0h_public = 'xpub68Gmy5EdvgibQVfPdqkBBCHxA5htiqg55crXYuXoQRKfDBFA1WEjWgP6LHhwBZeNK1VTsfTFUHCdrfp1bgwQ9xv5ski8PX9rL2dZXvgGDnw';
var vector1_m0h_private = 'xprv9uHRZZhk6KAJC1avXpDAp4MDc3sQKNxDiPvvkX8Br5ngLNv1TxvUxt4cV1rGL5hj6KCesnDYUhd7oWgT11eZG7XnxHrnYeSvkzY7d2bhkJ7';
var vector1_m0h1_public = 'xpub6ASuArnXKPbfEwhqN6e3mwBcDTgzisQN1wXN9BJcM47sSikHjJf3UFHKkNAWbWMiGj7Wf5uMash7SyYq527Hqck2AxYysAA7xmALppuCkwQ';
var vector1_m0h1_private = 'xprv9wTYmMFdV23N2TdNG573QoEsfRrWKQgWeibmLntzniatZvR9BmLnvSxqu53Kw1UmYPxLgboyZQaXwTCg8MSY3H2EU4pWcQDnRnrVA1xe8fs';
var vector1_m0h12h_public = 'xpub6D4BDPcP2GT577Vvch3R8wDkScZWzQzMMUm3PWbmWvVJrZwQY4VUNgqFJPMM3No2dFDFGTsxxpG5uJh7n7epu4trkrX7x7DogT5Uv6fcLW5';
var vector1_m0h12h_private = 'xprv9z4pot5VBttmtdRTWfWQmoH1taj2axGVzFqSb8C9xaxKymcFzXBDptWmT7FwuEzG3ryjH4ktypQSAewRiNMjANTtpgP4mLTj34bhnZX7UiM';
var vector1_m0h12h2_public = 'xpub6FHa3pjLCk84BayeJxFW2SP4XRrFd1JYnxeLeU8EqN3vDfZmbqBqaGJAyiLjTAwm6ZLRQUMv1ZACTj37sR62cfN7fe5JnJ7dh8zL4fiyLHV';
var vector1_m0h12h2_private = 'xprvA2JDeKCSNNZky6uBCviVfJSKyQ1mDYahRjijr5idH2WwLsEd4Hsb2Tyh8RfQMuPh7f7RtyzTtdrbdqqsunu5Mm3wDvUAKRHSC34sJ7in334';
var vector1_m0h12h21000000000_public = 'xpub6H1LXWLaKsWFhvm6RVpEL9P4KfRZSW7abD2ttkWP3SSQvnyA8FSVqNTEcYFgJS2UaFcxupHiYkro49S8yGasTvXEYBVPamhGW6cFJodrTHy';
var vector1_m0h12h21000000000_private = 'xprvA41z7zogVVwxVSgdKUHDy1SKmdb533PjDz7J6N6mV6uS3ze1ai8FHa8kmHScGpWmj4WggLyQjgPie1rFSruoUihUZREPSL39UNdE3BBDu76';
var vector2_master = 'fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542';
var vector2_m_public = 'xpub661MyMwAqRbcFW31YEwpkMuc5THy2PSt5bDMsktWQcFF8syAmRUapSCGu8ED9W6oDMSgv6Zz8idoc4a6mr8BDzTJY47LJhkJ8UB7WEGuduB';
var vector2_m_private = 'xprv9s21ZrQH143K31xYSDQpPDxsXRTUcvj2iNHm5NUtrGiGG5e2DtALGdso3pGz6ssrdK4PFmM8NSpSBHNqPqm55Qn3LqFtT2emdEXVYsCzC2U';
var vector2_m0_public = 'xpub69H7F5d8KSRgmmdJg2KhpAK8SR3DjMwAdkxj3ZuxV27CprR9LgpeyGmXUbC6wb7ERfvrnKZjXoUmmDznezpbZb7ap6r1D3tgFxHmwMkQTPH';
var vector2_m0_private = 'xprv9vHkqa6EV4sPZHYqZznhT2NPtPCjKuDKGY38FBWLvgaDx45zo9WQRUT3dKYnjwih2yJD9mkrocEZXo1ex8G81dwSM1fwqWpWkeS3v86pgKt';
var vector2_m02147483647h_public = 'xpub6ASAVgeehLbnwdqV6UKMHVzgqAG8Gr6riv3Fxxpj8ksbH9ebxaEyBLZ85ySDhKiLDBrQSARLq1uNRts8RuJiHjaDMBU4Zn9h8LZNnBC5y4a';
var vector2_m02147483647h_private = 'xprv9wSp6B7kry3Vj9m1zSnLvN3xH8RdsPP1Mh7fAaR7aRLcQMKTR2vidYEeEg2mUCTAwCd6vnxVrcjfy2kRgVsFawNzmjuHc2YmYRmagcEPdU9';
var vector2_m02147483647h1_public = 'xpub6DF8uhdarytz3FWdA8TvFSvvAh8dP3283MY7p2V4SeE2wyWmG5mg5EwVvmdMVCQcoNJxGoWaU9DCWh89LojfZ537wTfunKau47EL2dhHKon';
var vector2_m02147483647h1_private = 'xprv9zFnWC6h2cLgpmSA46vutJzBcfJ8yaJGg8cX1e5StJh45BBciYTRXSd25UEPVuesF9yog62tGAQtHjXajPPdbRCHuWS6T8XA2ECKADdw4Ef';
var vector2_m02147483647h12147483646h_public = 'xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL';
var vector2_m02147483647h12147483646h_private = 'xprvA1RpRA33e1JQ7ifknakTFpgNXPmW2YvmhqLQYMmrj4xJXXWYpDPS3xz7iAxn8L39njGVyuoseXzU6rcxFLJ8HFsTjSyQbLYnMpCqE2VbFWc';
var vector2_m02147483647h12147483646h2_public = 'xpub6FnCn6nSzZAw5Tw7cgR9bi15UV96gLZhjDstkXXxvCLsUXBGXPdSnLFbdpq8p9HmGsApME5hQTZ3emM2rnY5agb9rXpVGyy3bdW6EEgAtqt';
var vector2_m02147483647h12147483646h2_private = 'xprvA2nrNbFZABcdryreWet9Ea4LvTJcGsqrMzxHx98MMrotbir7yrKCEXw7nadnHM8Dq38EGfSh6dqA9QWTyefMLEcBYJUuekgW4BYPJcr9E7j';
it('should initialize the class', function() {
should.exist(BIP32);
});
it('should create a mainnet bip32', function() {
var bip32 = new BIP32('mainnet');
should.exist(bip32);
});
it('should create a testnet bip32', function() {
var bip32 = new BIP32('testnet');
should.exist(bip32);
});
it('should initialize test vector 1 from the extended public key', function() {
var bip32 = new BIP32(vector1_m_public);
should.exist(bip32);
});
it('should initialize test vector 1 from the extended private key', function() {
var bip32 = new BIP32(vector1_m_private);
should.exist(bip32);
});
it('should get the extended public key from the extended private key for test vector 1', function() {
var bip32 = new BIP32(vector1_m_private);
bip32.extendedPublicKeyString().should.equal(vector1_m_public);
});
it("should get m/0' ext. private key from test vector 1", function() {
var bip32 = new BIP32(vector1_m_private);
var child = bip32.derive("m/0'");
should.exist(child);
child.extendedPrivateKeyString().should.equal(vector1_m0h_private);
});
it("should get m/0' ext. public key from test vector 1", function() {
var bip32 = new BIP32(vector1_m_private);
var child = bip32.derive("m/0'");
should.exist(child);
child.extendedPublicKeyString().should.equal(vector1_m0h_public);
});
it("should get m/0'/1 ext. private key from test vector 1", function() {
var bip32 = new BIP32(vector1_m_private);
var child = bip32.derive("m/0'/1");
should.exist(child);
child.extendedPrivateKeyString().should.equal(vector1_m0h1_private);
});
it("should get m/0'/1 ext. public key from test vector 1", function() {
var bip32 = new BIP32(vector1_m_private);
var child = bip32.derive("m/0'/1");
should.exist(child);
child.extendedPublicKeyString().should.equal(vector1_m0h1_public);
});
it("should get m/0'/1 ext. public key from m/0' public key from test vector 1", function() {
var bip32 = new BIP32(vector1_m_private);
var child = bip32.derive("m/0'");
var child_pub = new BIP32(child.extendedPublicKeyString());
var child2 = child_pub.derive("m/1");
should.exist(child2);
child2.extendedPublicKeyString().should.equal(vector1_m0h1_public);
});
it("should get m/0'/1/2h ext. private key from test vector 1", function() {
var bip32 = new BIP32(vector1_m_private);
var child = bip32.derive("m/0'/1/2'");
should.exist(child);
child.extendedPrivateKeyString().should.equal(vector1_m0h12h_private);
});
it("should get m/0'/1/2h ext. public key from test vector 1", function() {
var bip32 = new BIP32(vector1_m_private);
var child = bip32.derive("m/0'/1/2'");
should.exist(child);
child.extendedPublicKeyString().should.equal(vector1_m0h12h_public);
});
it("should get m/0'/1/2h/2 ext. private key from test vector 1", function() {
var bip32 = new BIP32(vector1_m_private);
var child = bip32.derive("m/0'/1/2'/2");
should.exist(child);
child.extendedPrivateKeyString().should.equal(vector1_m0h12h2_private);
});
it("should get m/0'/1/2'/2 ext. public key from m/0'/1/2' public key from test vector 1", function() {
var bip32 = new BIP32(vector1_m_private);
var child = bip32.derive("m/0'/1/2'");
var child_pub = new BIP32(child.extendedPublicKeyString());
var child2 = child_pub.derive("m/2");
should.exist(child2);
child2.extendedPublicKeyString().should.equal(vector1_m0h12h2_public);
});
it("should get m/0'/1/2h/2 ext. public key from test vector 1", function() {
var bip32 = new BIP32(vector1_m_private);
var child = bip32.derive("m/0'/1/2'/2");
should.exist(child);
child.extendedPublicKeyString().should.equal(vector1_m0h12h2_public);
});
it("should get m/0'/1/2h/2/1000000000 ext. private key from test vector 1", function() {
var bip32 = new BIP32(vector1_m_private);
var child = bip32.derive("m/0'/1/2'/2/1000000000");
should.exist(child);
child.extendedPrivateKeyString().should.equal(vector1_m0h12h21000000000_private);
});
it("should get m/0'/1/2h/2/1000000000 ext. public key from test vector 1", function() {
var bip32 = new BIP32(vector1_m_private);
var child = bip32.derive("m/0'/1/2'/2/1000000000");
should.exist(child);
child.extendedPublicKeyString().should.equal(vector1_m0h12h21000000000_public);
});
it("should get m/0'/1/2'/2/1000000000 ext. public key from m/0'/1/2'/2 public key from test vector 1", function() {
var bip32 = new BIP32(vector1_m_private);
var child = bip32.derive("m/0'/1/2'/2");
var child_pub = new BIP32(child.extendedPublicKeyString());
var child2 = child_pub.derive("m/1000000000");
should.exist(child2);
child2.extendedPublicKeyString().should.equal(vector1_m0h12h21000000000_public);
});
it('should initialize test vector 2 from the extended public key', function() {
var bip32 = new BIP32(vector2_m_public);
should.exist(bip32);
});
it('should initialize test vector 2 from the extended private key', function() {
var bip32 = new BIP32(vector2_m_private);
should.exist(bip32);
});
it('should get the extended public key from the extended private key for test vector 2', function() {
var bip32 = new BIP32(vector2_m_private);
bip32.extendedPublicKeyString().should.equal(vector2_m_public);
});
it("should get m/0 ext. private key from test vector 2", function() {
var bip32 = new BIP32(vector2_m_private);
var child = bip32.derive("m/0");
should.exist(child);
child.extendedPrivateKeyString().should.equal(vector2_m0_private);
});
it("should get m/0 ext. public key from test vector 2", function() {
var bip32 = new BIP32(vector2_m_private);
var child = bip32.derive("m/0");
should.exist(child);
child.extendedPublicKeyString().should.equal(vector2_m0_public);
});
it("should get m/0 ext. public key from m public key from test vector 2", function() {
var bip32 = new BIP32(vector2_m_private);
var child = bip32.derive("m");
var child_pub = new BIP32(child.extendedPublicKeyString());
var child2 = child_pub.derive("m/0");
should.exist(child2);
child2.extendedPublicKeyString().should.equal(vector2_m0_public);
});
it("should get m/0/2147483647h ext. private key from test vector 2", function() {
var bip32 = new BIP32(vector2_m_private);
var child = bip32.derive("m/0/2147483647'");
should.exist(child);
child.extendedPrivateKeyString().should.equal(vector2_m02147483647h_private);
});
it("should get m/0/2147483647h ext. public key from test vector 2", function() {
var bip32 = new BIP32(vector2_m_private);
var child = bip32.derive("m/0/2147483647'");
should.exist(child);
child.extendedPublicKeyString().should.equal(vector2_m02147483647h_public);
});
it("should get m/0/2147483647h/1 ext. private key from test vector 2", function() {
var bip32 = new BIP32(vector2_m_private);
var child = bip32.derive("m/0/2147483647'/1");
should.exist(child);
child.extendedPrivateKeyString().should.equal(vector2_m02147483647h1_private);
});
it("should get m/0/2147483647h/1 ext. public key from test vector 2", function() {
var bip32 = new BIP32(vector2_m_private);
var child = bip32.derive("m/0/2147483647'/1");
should.exist(child);
child.extendedPublicKeyString().should.equal(vector2_m02147483647h1_public);
});
it("should get m/0/2147483647h/1 ext. public key from m/0/2147483647h public key from test vector 2", function() {
var bip32 = new BIP32(vector2_m_private);
var child = bip32.derive("m/0/2147483647'");
var child_pub = new BIP32(child.extendedPublicKeyString());
var child2 = child_pub.derive("m/1");
should.exist(child2);
child2.extendedPublicKeyString().should.equal(vector2_m02147483647h1_public);
});
it("should get m/0/2147483647h/1/2147483646h ext. private key from test vector 2", function() {
var bip32 = new BIP32(vector2_m_private);
var child = bip32.derive("m/0/2147483647'/1/2147483646'");
should.exist(child);
child.extendedPrivateKeyString().should.equal(vector2_m02147483647h12147483646h_private);
});
it("should get m/0/2147483647h/1/2147483646h ext. public key from test vector 2", function() {
var bip32 = new BIP32(vector2_m_private);
var child = bip32.derive("m/0/2147483647'/1/2147483646'");
should.exist(child);
child.extendedPublicKeyString().should.equal(vector2_m02147483647h12147483646h_public);
});
it("should get m/0/2147483647h/1/2147483646h/2 ext. private key from test vector 2", function() {
var bip32 = new BIP32(vector2_m_private);
var child = bip32.derive("m/0/2147483647'/1/2147483646'/2");
should.exist(child);
child.extendedPrivateKeyString().should.equal(vector2_m02147483647h12147483646h2_private);
});
it("should get m/0/2147483647h/1/2147483646h/2 ext. public key from test vector 2", function() {
var bip32 = new BIP32(vector2_m_private);
var child = bip32.derive("m/0/2147483647'/1/2147483646'/2");
should.exist(child);
child.extendedPublicKeyString().should.equal(vector2_m02147483647h12147483646h2_public);
});
it("should get m/0/2147483647h/1/2147483646h/2 ext. public key from m/0/2147483647h/2147483646h public key from test vector 2", function() {
var bip32 = new BIP32(vector2_m_private);
var child = bip32.derive("m/0/2147483647'/1/2147483646'");
var child_pub = new BIP32(child.extendedPublicKeyString());
var child2 = child_pub.derive("m/2");
should.exist(child2);
child2.extendedPublicKeyString().should.equal(vector2_m02147483647h12147483646h2_public);
});
describe('#seed', function() {
it('should initialize a new BIP32 correctly from test vector 1 seed', function() {
var hex = vector1_master;
var bip32 = (new BIP32()).fromSeed(new Buffer(hex, 'hex'), 'mainnet');
should.exist(bip32);
bip32.extendedPrivateKeyString().should.equal(vector1_m_private);
bip32.extendedPublicKeyString().should.equal(vector1_m_public);
});
it('should initialize a new BIP32 correctly from test vector 2 seed', function() {
var hex = vector2_master;
var bip32 = (new BIP32()).fromSeed(new Buffer(hex, 'hex'), 'mainnet');
should.exist(bip32);
bip32.extendedPrivateKeyString().should.equal(vector2_m_private);
bip32.extendedPublicKeyString().should.equal(vector2_m_public);
});
});
describe('testnet', function() {
it('should initialize a new BIP32 correctly from a random BIP32', function() {
var b1 = new BIP32('testnet');
var b2 = new BIP32(b1.extendedPublicKeyString());
b2.extendedPublicKeyString().should.equal(b1.extendedPublicKeyString());
});
it('should generate valid ext pub key for testnet', function() {
var b = new BIP32('testnet');
b.extendedPublicKeyString().substring(0,4).should.equal('tpub');
});
});
describe('derivation in linux', function() {
it('should not be non-deterministic', function(){
var hp = 'm/45\'';
var sp = 'm/45';
var hk = new BIP32('tprv8ZgxMBicQKsPdSF1avR6mXyDj5Uv1XY2UyUHSDpAXQ5TvPN7prGeDppjy4562rBB9gMMAhRfFdJrNDpQ4t69kkqHNEEen3PX1zBJqSehJDH');
//hk.derive(sp).extendedPrivateKeyString().should.equal(
// 'tprv8cSDV3fVD6wqGoLKykTPhRwWLiwD6WBHvYHYkFvp8PJvApm7HCfY9HH9P6Q6iPaCGNsU3LEqh7iJMN7478TqjkLFnf71f9zBXXd7XoiL7dw');
//hk.derive(sp).extendedPrivateKeyString().should.equal(hk.derive(sp).extendedPrivateKeyString());
var epk1 = hk.derive(hp).extendedPrivateKeyString();
var epk2 = hk.derive(hp).extendedPrivateKeyString();
epk1.should.equal(epk2);
//hk.derive(hp).extendedPrivateKeyString().should.equal(
// 'tprv8cSDV3fdYmUoTNGu4xRTm6qh3DPrNxPZzukM5FPdWoa9m22ALFJVGbjnU7J4TC5t3MJp293GtZWssAPuV1PNWGjXavQTnXy9xW6Lee2X6rd');
});
});
});

11
test/test.pubkey.js

@ -64,6 +64,17 @@ describe('pubkey', function() {
});
describe('#toBuffer', function() {
it('should return this compressed DER format', function() {
var x = bn.fromBuffer(new Buffer('1ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a', 'hex'));
var pk = new pubkey();
pk.fromX(true, x);
pk.toBuffer().toString('hex').should.equal('031ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a');
});
});
describe('#toDER', function() {
it('should return this compressed DER format', function() {

Loading…
Cancel
Save