Ryan X. Charles
11 years ago
17 changed files with 973 additions and 310 deletions
@ -0,0 +1,138 @@ |
|||||
|
"use strict"; |
||||
|
|
||||
|
var imports = require('soop').imports(); |
||||
|
var Key = imports.Key || require('./Key'); |
||||
|
var bignum = imports.bignum || require('bignum'); |
||||
|
var assert = require('assert'); |
||||
|
|
||||
|
//browser
|
||||
|
if (!process.versions) { |
||||
|
var ECKey = require('./browser/vendor-bundle.js').ECKey; |
||||
|
var ECPointFp = require('./browser/vendor-bundle.js').ECPointFp; |
||||
|
var ECFieldElementFp = require('./browser/vendor-bundle.js').ECFieldElementFp; |
||||
|
var getSECCurveByName = require('./browser/vendor-bundle.js').getSECCurveByName; |
||||
|
var BigInteger = require('./browser/vendor-bundle.js').BigInteger; |
||||
|
var should = require('chai').should(); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
//a point on the secp256k1 curve
|
||||
|
//x and y are bignums
|
||||
|
var Point = function(x, y) { |
||||
|
this.x = x; |
||||
|
this.y = y; |
||||
|
}; |
||||
|
|
||||
|
Point.add = function(p1, p2) { |
||||
|
|
||||
|
//node
|
||||
|
if (process.versions) { |
||||
|
var key1 = p1.toKey(); |
||||
|
key1.compressed = false; |
||||
|
var key2 = p2.toKey(); |
||||
|
key2.compressed = false; |
||||
|
var pubKey = Key.addUncompressed(key1.public, key2.public); |
||||
|
var key = new Key(); |
||||
|
key.compressed = false; |
||||
|
key.public = pubKey; |
||||
|
key.compressed = true; |
||||
|
return Point.fromKey(key); |
||||
|
} |
||||
|
|
||||
|
//browser
|
||||
|
else { |
||||
|
var ecparams = getSECCurveByName('secp256k1'); |
||||
|
|
||||
|
var p1xhex = p1.x.toBuffer({size: 32}).toString('hex'); |
||||
|
var p1x = new BigInteger(p1xhex, 16); |
||||
|
var p1yhex = p1.y.toBuffer({size: 32}).toString('hex'); |
||||
|
var p1y = new BigInteger(p1yhex, 16); |
||||
|
var p1px = new ECFieldElementFp(ecparams.getCurve().getQ(), p1x); |
||||
|
var p1py = new ECFieldElementFp(ecparams.getCurve().getQ(), p1y); |
||||
|
var p1p = new ECPointFp(ecparams.getCurve(), p1px, p1py); |
||||
|
|
||||
|
var p2xhex = p2.x.toBuffer({size: 32}).toString('hex'); |
||||
|
var p2x = new BigInteger(p2xhex, 16); |
||||
|
var p2yhex = p2.y.toBuffer({size: 32}).toString('hex'); |
||||
|
var p2y = new BigInteger(p2yhex, 16); |
||||
|
var p2px = new ECFieldElementFp(ecparams.getCurve().getQ(), p2x); |
||||
|
var p2py = new ECFieldElementFp(ecparams.getCurve().getQ(), p2y); |
||||
|
var p2p = new ECPointFp(ecparams.getCurve(), p2px, p2py); |
||||
|
|
||||
|
var p = p1p.add(p2p); |
||||
|
|
||||
|
var point = new Point(); |
||||
|
var pointxbuf = new Buffer(p.getX().toBigInteger().toByteArrayUnsigned()); |
||||
|
point.x = bignum.fromBuffer(pointxbuf, {size: pointxbuf.length}); |
||||
|
assert(pointxbuf.length <= 32); |
||||
|
var pointybuf = new Buffer(p.getY().toBigInteger().toByteArrayUnsigned()); |
||||
|
assert(pointybuf.length <= 32); |
||||
|
point.y = bignum.fromBuffer(pointybuf, {size: pointybuf.length}); |
||||
|
|
||||
|
return point; |
||||
|
} |
||||
|
|
||||
|
}; |
||||
|
|
||||
|
//convert the public key of a Key into a Point
|
||||
|
Point.fromKey = function(key) { |
||||
|
|
||||
|
//node
|
||||
|
if (process.versions) { |
||||
|
var point = new Point(); |
||||
|
var pubKeyBuf = new Buffer(key.public); |
||||
|
var key2 = new Key(); |
||||
|
key2.compressed = key.compressed; |
||||
|
key2.public = pubKeyBuf; |
||||
|
key2.compressed = false; |
||||
|
point.x = bignum.fromBuffer(key2.public.slice(1, 33), {size: 32}); |
||||
|
point.y = bignum.fromBuffer(key2.public.slice(33, 65), {size: 32}); |
||||
|
return point; |
||||
|
} |
||||
|
|
||||
|
//browser
|
||||
|
else { |
||||
|
var point = new Point(); |
||||
|
var pubKeyBuf = new Buffer(key.public); |
||||
|
var key2 = new ECKey(); |
||||
|
key2.setCompressed(key.compressed); |
||||
|
key2.setPub(Key.bufferToArray(pubKeyBuf)); |
||||
|
key2.setCompressed(false); |
||||
|
point.x = bignum.fromBuffer((new Buffer(key2.getPub())).slice(1, 33), {size: 32}); |
||||
|
point.y = bignum.fromBuffer((new Buffer(key2.getPub())).slice(33, 65), {size: 32}); |
||||
|
return point; |
||||
|
} |
||||
|
}; |
||||
|
|
||||
|
//convert the Point into the Key containing a compressed public key
|
||||
|
Point.prototype.toKey = function() { |
||||
|
|
||||
|
//node
|
||||
|
if (process.versions) { |
||||
|
var xbuf = this.x.toBuffer({size: 32}); |
||||
|
var ybuf = this.y.toBuffer({size: 32}); |
||||
|
var key = new Key(); |
||||
|
key.compressed = false; |
||||
|
var prefix = new Buffer([0x04]); |
||||
|
key.public = Buffer.concat([prefix, xbuf, ybuf]); //this might be wrong
|
||||
|
key.compressed = true; |
||||
|
return key; |
||||
|
} |
||||
|
|
||||
|
//browser
|
||||
|
else { |
||||
|
var xbuf = this.x.toBuffer({size: 32}); |
||||
|
var ybuf = this.y.toBuffer({size: 32}); |
||||
|
var key = new ECKey(); |
||||
|
key.setCompressed(false); |
||||
|
var prefix = new Buffer([0x04]); |
||||
|
var pub = Buffer.concat([prefix, xbuf, ybuf]); //this might be wrong
|
||||
|
key.setPub(Key.bufferToArray(pub)); |
||||
|
key.setCompressed(true); |
||||
|
var key2 = new Key(); |
||||
|
key2.public = new Buffer(key.getPub()); |
||||
|
return key2; |
||||
|
} |
||||
|
}; |
||||
|
|
||||
|
module.exports = require('soop')(Point); |
@ -0,0 +1,297 @@ |
|||||
|
'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'; |
||||
|
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 = BIP32.seed(hex, 'livenet'); |
||||
|
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 = BIP32.seed(hex, 'livenet'); |
||||
|
should.exist(bip32); |
||||
|
bip32.extendedPrivateKeyString().should.equal(vector2_m_private); |
||||
|
bip32.extendedPublicKeyString().should.equal(vector2_m_public); |
||||
|
}); |
||||
|
|
||||
|
}); |
||||
|
|
||||
|
}); |
@ -0,0 +1,69 @@ |
|||||
|
'use strict'; |
||||
|
|
||||
|
var assert = require('assert'); |
||||
|
var chai = chai || require('chai'); |
||||
|
var bitcore = bitcore || require('../bitcore'); |
||||
|
var coinUtil = coinUtil || require('../util/util'); |
||||
|
var buffertools = require('buffertools'); |
||||
|
var bignum = require('bignum'); |
||||
|
|
||||
|
var should = chai.should(); |
||||
|
|
||||
|
var Point = bitcore.Point; |
||||
|
var Key = bitcore.Key; |
||||
|
|
||||
|
describe('Key', function() { |
||||
|
|
||||
|
it('should initialize the main object', function() { |
||||
|
should.exist(Point); |
||||
|
}); |
||||
|
|
||||
|
it('should be able to create instance', function() { |
||||
|
var p = new Point(); |
||||
|
should.exist(p); |
||||
|
}); |
||||
|
|
||||
|
it('should add these two points correctly', function() { |
||||
|
//these points are from one of the BIP32 test vectors
|
||||
|
var axhex = "69b154b42ff9452c31251cb341d7db01ad603dc56d64f9c5fb9e7031b89a241d"; |
||||
|
var ayhex = "eeedc91342b3c8982c1e676435780fe5f9d62f3f692e8d1512485d77fab35997"; |
||||
|
var a = new Point(bignum.fromBuffer((new Buffer(axhex, 'hex')), {size: 32}), bignum.fromBuffer((new Buffer(ayhex, 'hex')), {size: 32})); |
||||
|
var bxhex = "5a784662a4a20a65bf6aab9ae98a6c068a81c52e4b032c0fb5400c706cfccc56"; |
||||
|
var byhex = "7f717885be239daadce76b568958305183ad616ff74ed4dc219a74c26d35f839"; |
||||
|
var b = new Point(bignum.fromBuffer((new Buffer(bxhex, 'hex')), {size: 32}), bignum.fromBuffer((new Buffer(byhex, 'hex')), {size: 32})); |
||||
|
var sxhex = "501e454bf00751f24b1b489aa925215d66af2234e3891c3b21a52bedb3cd711c"; |
||||
|
var syhex = "008794c1df8131b9ad1e1359965b3f3ee2feef0866be693729772be14be881ab"; |
||||
|
var s = new Point(bignum.fromBuffer((new Buffer(sxhex, 'hex')), {size: 32}), bignum.fromBuffer((new Buffer(syhex, 'hex')), {size: 32})); |
||||
|
var sum = Point.add(a, b); |
||||
|
s.x.toBuffer({size: 32}).toString('hex').should.equal(sum.x.toBuffer({size: 32}).toString('hex')); |
||||
|
s.y.toBuffer({size: 32}).toString('hex').should.equal(sum.y.toBuffer({size: 32}).toString('hex')); |
||||
|
}); |
||||
|
|
||||
|
it('should convert a Point into the public key of a Key', function() { |
||||
|
var axhex = "69b154b42ff9452c31251cb341d7db01ad603dc56d64f9c5fb9e7031b89a241d"; |
||||
|
var axbuf = new Buffer(axhex, 'hex'); |
||||
|
var ayhex = "eeedc91342b3c8982c1e676435780fe5f9d62f3f692e8d1512485d77fab35997"; |
||||
|
var aybuf = new Buffer(ayhex, 'hex'); |
||||
|
var a = new Point(bignum.fromBuffer(axbuf, {size: 32}), bignum.fromBuffer(aybuf, {size: 32})); |
||||
|
|
||||
|
var pubKeyBufCompressedHex = "0369b154b42ff9452c31251cb341d7db01ad603dc56d64f9c5fb9e7031b89a241d"; |
||||
|
var key = new Key(); |
||||
|
key.public = new Buffer(pubKeyBufCompressedHex, 'hex'); |
||||
|
|
||||
|
key.public.toString('hex').should.equal(a.toKey().public.toString('hex')); |
||||
|
}); |
||||
|
|
||||
|
it('should convert the public key of a Key into a Point', function() { |
||||
|
var axhex = "69b154b42ff9452c31251cb341d7db01ad603dc56d64f9c5fb9e7031b89a241d"; |
||||
|
var ayhex = "eeedc91342b3c8982c1e676435780fe5f9d62f3f692e8d1512485d77fab35997"; |
||||
|
|
||||
|
var pubKeyBufCompressedHex = "0369b154b42ff9452c31251cb341d7db01ad603dc56d64f9c5fb9e7031b89a241d"; |
||||
|
var key = new Key(); |
||||
|
key.public = new Buffer(pubKeyBufCompressedHex, 'hex'); |
||||
|
|
||||
|
var point = Point.fromKey(key); |
||||
|
point.x.toBuffer({size: 32}).toString('hex').should.equal(axhex); |
||||
|
point.y.toBuffer({size: 32}).toString('hex').should.equal(ayhex); |
||||
|
}); |
||||
|
|
||||
|
}); |
Loading…
Reference in new issue