Get test vector 1 working in node
This commit is contained in:
parent
c5c0ecc918
commit
22b57feb7b
56
BIP32.js
56
BIP32.js
|
@ -1,8 +1,9 @@
|
|||
//var base58 = imports.base58 || require('base58-native').base58Check;
|
||||
var imports = require('soop').imports();
|
||||
var base58 = imports.base58 || require('base58-native').base58;
|
||||
var coinUtil = imports.coinUtil || require('./util/util');
|
||||
var Key = imports.Key || require('./Key');
|
||||
var bignum = require('bignum');
|
||||
var bignum = imports.bignum || require('bignum');
|
||||
var crypto = require('crypto');
|
||||
|
||||
var BITCOIN_MAINNET_PUBLIC = 0x0488b21e;
|
||||
var BITCOIN_MAINNET_PRIVATE = 0x0488ade4;
|
||||
|
@ -16,7 +17,8 @@ var LITECOIN_MAINNET_PUBLIC = 0x019da462;
|
|||
var LITECOIN_MAINNET_PRIVATE = 0x019d9cfe;
|
||||
var LITECOIN_TESTNET_PUBLIC = 0x0436f6e1;
|
||||
var LITECOIN_TESTNET_PRIVATE = 0x0436ef7d;
|
||||
var SECP256K1_N = new bignum("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141", 16);
|
||||
var secp256k1_n = new bignum("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141", 16);
|
||||
var secp256k1_G = new bignum("79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798", 16); //x coordinate
|
||||
|
||||
var BIP32 = function(bytes) {
|
||||
// decode base58
|
||||
|
@ -105,7 +107,7 @@ BIP32.prototype.init_from_bytes = function(bytes) {
|
|||
}
|
||||
|
||||
BIP32.prototype.build_extended_public_key = function() {
|
||||
this.extended_public_key = [];
|
||||
this.extended_public_key = new Buffer([]);
|
||||
|
||||
var v = null;
|
||||
switch(this.version) {
|
||||
|
@ -160,7 +162,7 @@ BIP32.prototype.build_extended_public_key = function() {
|
|||
this.extended_public_key = Buffer.concat([this.extended_public_key, this.chain_code]);
|
||||
|
||||
// Public key
|
||||
this.extended_public_key = Buffer.concat([this.extended_public_key, this.eckey.pub]);
|
||||
this.extended_public_key = Buffer.concat([this.extended_public_key, this.eckey.public]);
|
||||
}
|
||||
|
||||
BIP32.prototype.extended_public_key_string = function(format) {
|
||||
|
@ -178,7 +180,7 @@ BIP32.prototype.extended_public_key_string = function(format) {
|
|||
|
||||
BIP32.prototype.build_extended_private_key = function() {
|
||||
if (!this.has_private_key) return;
|
||||
this.extended_private_key = new Buffer();
|
||||
this.extended_private_key = new Buffer([]);
|
||||
|
||||
var v = this.version;
|
||||
|
||||
|
@ -204,7 +206,6 @@ BIP32.prototype.build_extended_private_key = function() {
|
|||
this.extended_private_key = Buffer.concat([this.extended_private_key, this.chain_code]);
|
||||
|
||||
// Private key
|
||||
this.extended_private_key.push(0);
|
||||
this.extended_private_key = Buffer.concat([this.extended_private_key, new Buffer([0])]);
|
||||
this.extended_private_key = Buffer.concat([this.extended_private_key, this.eckey.private]);
|
||||
}
|
||||
|
@ -256,7 +257,7 @@ BIP32.prototype.derive_child = function(i) {
|
|||
ib.push((i >> 24) & 0xff);
|
||||
ib.push((i >> 16) & 0xff);
|
||||
ib.push((i >> 8) & 0xff);
|
||||
ib.push(i & 0xff );
|
||||
ib.push(i & 0xff);
|
||||
ib = new Buffer(ib);
|
||||
|
||||
var use_private = (i & 0x80000000) != 0;
|
||||
|
@ -291,18 +292,17 @@ BIP32.prototype.derive_child = function(i) {
|
|||
*/
|
||||
var hmac = crypto.createHmac('sha512', this.chain_code);
|
||||
var hash = hmac.update(data).digest();
|
||||
var il = bignum.fromBufer(hash.slice(0, 64), {size: 32});
|
||||
var ir = hash.slice(64, 128);
|
||||
var il = bignum.fromBuffer(hash.slice(0, 32), {size: 32});
|
||||
var ir = hash.slice(32, 64);
|
||||
|
||||
// ki = IL + kpar (mod n).
|
||||
//TODO: Fix this somehow
|
||||
var priv = bignum.fromBuffer(this.eckey.priv, {size: 32});
|
||||
var k = il.add(priv).mod(SECP256K1_N);
|
||||
var priv = bignum.fromBuffer(this.eckey.private, {size: 32});
|
||||
var k = il.add(priv).mod(secp256k1_n);
|
||||
|
||||
ret = new BIP32();
|
||||
ret.chain_code = ir;
|
||||
|
||||
ret.eckey = new bitcore.Key();
|
||||
ret.eckey = new Key();
|
||||
ret.eckey.private = k.toBuffer({size: 32});
|
||||
ret.eckey.regenerateSync();
|
||||
ret.has_private_key = true;
|
||||
|
@ -317,24 +317,24 @@ BIP32.prototype.derive_child = function(i) {
|
|||
var ir = Crypto.util.hexToBytes(hash.slice(64, 128));
|
||||
*/
|
||||
var data = Buffer.concat([this.eckey.public, ib]);
|
||||
var hash = coinUtil.sha512(this.chain_code); //TODO: replace with HMAC
|
||||
var il = bignum.fromBuffer(hash.slice(0, 64).toString('hex'), 16);
|
||||
var ir = hash.slice(64, 128);
|
||||
var hmac = crypto.createHmac('sha512', this.chain_code);
|
||||
var hash = hmac.update(data).digest();
|
||||
var il = bignum.fromBuffer(hash.slice(0, 32), {size: 32});
|
||||
var ir = hash.slice(32, 64);
|
||||
|
||||
// Ki = (IL + kpar)*G = IL*G + Kpar
|
||||
//TODO: Fix this somehow
|
||||
var key = new bitcore.Key();
|
||||
key.private = il;
|
||||
key.regenerateSync();
|
||||
var k = key.public;
|
||||
//TODO: now add this.eckey.pub
|
||||
//var k = ecparams.getG().multiply(il).add(this.eckey.pub);
|
||||
var pub = new bignum(this.eckey.public, {size: 32});
|
||||
var k = secp256k1_G.mul(il).add(pub);
|
||||
|
||||
//compressed pubkey must start with 0x02 just like compressed G
|
||||
var kbuf = Buffer.concat([new Buffer(0x02), k.toBuffer({size: 32})]);
|
||||
|
||||
ret = new BIP32();
|
||||
ret.chain_code = new Buffer(ir);
|
||||
|
||||
ret.eckey = new bitcore.key();
|
||||
ret.eckey.pub = k;
|
||||
ret.eckey = new Key();
|
||||
ret.eckey.public = kbuf;
|
||||
ret.has_private_key = false;
|
||||
}
|
||||
|
||||
|
@ -343,8 +343,8 @@ BIP32.prototype.derive_child = function(i) {
|
|||
ret.version = this.version;
|
||||
ret.depth = this.depth + 1;
|
||||
|
||||
ret.eckey.setCompressed(true);
|
||||
ret.pubKeyHash = coinUtil.sha256ripe160(ret.eckey.pub.getEncoded(true));
|
||||
ret.eckey.compressed = true;
|
||||
ret.pubKeyHash = coinUtil.sha256ripe160(ret.eckey.public);
|
||||
|
||||
ret.build_extended_public_key();
|
||||
ret.build_extended_private_key();
|
||||
|
@ -401,3 +401,5 @@ function decompress_pubkey(key_bytes) {
|
|||
return new ECPointFp(curve, curve.fromBigInteger(x), curve.fromBigInteger(y));
|
||||
}
|
||||
*/
|
||||
|
||||
module.exports = require('soop')(BIP32);
|
||||
|
|
|
@ -23,6 +23,7 @@ requireWhenAccessed('EncodedData', './util/EncodedData');
|
|||
requireWhenAccessed('VersionedData', './util/VersionedData');
|
||||
requireWhenAccessed('BinaryParser', './util/BinaryParser');
|
||||
requireWhenAccessed('Address', './Address');
|
||||
requireWhenAccessed('BIP32', './BIP32');
|
||||
requireWhenAccessed('Opcode', './Opcode');
|
||||
requireWhenAccessed('Script', './Script');
|
||||
requireWhenAccessed('Transaction', './Transaction');
|
||||
|
|
|
@ -0,0 +1,115 @@
|
|||
'use strict';
|
||||
|
||||
var chai = chai || require('chai');
|
||||
var should = chai.should();
|
||||
var bitcore = bitcore || require('../bitcore');
|
||||
var BIP32 = bitcore.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';
|
||||
|
||||
|
||||
it('should initialize the class', function() {
|
||||
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', function() {
|
||||
var bip32 = new BIP32(vector1_m_private);
|
||||
bip32.extended_public_key_string().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.extended_private_key_string().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.extended_public_key_string().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.extended_private_key_string().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.extended_public_key_string().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.extended_private_key_string().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.extended_public_key_string().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.extended_private_key_string().should.equal(vector1_m0h12h2_private);
|
||||
});
|
||||
|
||||
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.extended_public_key_string().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.extended_private_key_string().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.extended_public_key_string().should.equal(vector1_m0h12h21000000000_public);
|
||||
});
|
||||
|
||||
});
|
Loading…
Reference in New Issue