merge
This commit is contained in:
commit
6edea532ba
4 changed files with 104 additions and 5 deletions
|
@ -50,7 +50,7 @@ BIP32key.prototype.deserialize = function(str) {
|
|||
i: util.bytesToNum(bytes.slice(9,13).reverse()),
|
||||
chaincode: bytes.slice(13,45),
|
||||
key: type == 'priv' ? new ECKey(bytes.slice(46,78).concat([1]),true)
|
||||
: new ECPubKey(bytes.slice(45,78))
|
||||
: new ECPubKey(bytes.slice(45,78),true)
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -76,7 +76,7 @@ BIP32key.prototype.ckd = function(i) {
|
|||
else pub = this.key.export('bytes')
|
||||
|
||||
if (i >= 2147483648) {
|
||||
if (this.priv) throw new Error("Can't do private derivation on public key!")
|
||||
if (!priv) throw new Error("Can't do private derivation on public key!")
|
||||
blob = [0].concat(priv.slice(0,32),util.numToBytes(i,4).reverse())
|
||||
}
|
||||
else blob = pub.concat(util.numToBytes(i,4).reverse())
|
||||
|
|
|
@ -285,10 +285,12 @@ var ECDSA = {
|
|||
* This function simply tries all four cases and returns the value
|
||||
* that resulted in a successful pubkey recovery.
|
||||
*/
|
||||
calcPubkeyRecoveryParam: function (address, r, s, hash)
|
||||
calcPubkeyRecoveryParam: function (origPubkey, r, s, hash)
|
||||
{
|
||||
var address = origPubkey.getBitcoinAddress().toString();
|
||||
for (var i = 0; i < 4; i++) {
|
||||
var pubkey = ECDSA.recoverPubKey(r, s, hash, i);
|
||||
pubkey.compressed = origPubkey.compressed;
|
||||
if (pubkey.getBitcoinAddress().toString() == address) {
|
||||
return i;
|
||||
}
|
||||
|
|
|
@ -34,8 +34,7 @@ Message.signMessage = function (key, message, compressed) {
|
|||
|
||||
var obj = ecdsa.parseSig(sig);
|
||||
|
||||
var address = key.getBitcoinAddress().toString();
|
||||
var i = ecdsa.calcPubkeyRecoveryParam(address, obj.r, obj.s, hash);
|
||||
var i = ecdsa.calcPubkeyRecoveryParam(key, obj.r, obj.s, hash);
|
||||
|
||||
i += 27;
|
||||
if (compressed) i += 4;
|
||||
|
|
98
test/bip32.js
Normal file
98
test/bip32.js
Normal file
|
@ -0,0 +1,98 @@
|
|||
// Tests from https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#Test_Vectors
|
||||
|
||||
var assert = require('assert');
|
||||
var BIP32key = require('..').BIP32key;
|
||||
|
||||
var hexToBytes = require('../').convert.hexToBytes;
|
||||
var bytesToString = require('../').convert.bytesToString;
|
||||
|
||||
var BIP32_PRIME = 0x80000000;
|
||||
|
||||
function checkKey(key, extPriv, extPub) {
|
||||
assert.equal(key.serialize(), extPriv);
|
||||
assert.equal(key.getPub().serialize(), extPub);
|
||||
}
|
||||
|
||||
test("BIP32 Test vector 1", function () {
|
||||
var seed_str = '000102030405060708090a0b0c0d0e0f';
|
||||
var seed = bytesToString(hexToBytes(seed_str));
|
||||
|
||||
var key = new BIP32key(seed);
|
||||
|
||||
checkKey(key,
|
||||
"xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi",
|
||||
"xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8");
|
||||
|
||||
var branch = key.ckd(0+BIP32_PRIME);
|
||||
|
||||
checkKey(branch,
|
||||
"xprv9uHRZZhk6KAJC1avXpDAp4MDc3sQKNxDiPvvkX8Br5ngLNv1TxvUxt4cV1rGL5hj6KCesnDYUhd7oWgT11eZG7XnxHrnYeSvkzY7d2bhkJ7",
|
||||
"xpub68Gmy5EdvgibQVfPdqkBBCHxA5htiqg55crXYuXoQRKfDBFA1WEjWgP6LHhwBZeNK1VTsfTFUHCdrfp1bgwQ9xv5ski8PX9rL2dZXvgGDnw");
|
||||
|
||||
var branch2 = branch.ckd(1);
|
||||
|
||||
checkKey(branch2,
|
||||
"xprv9wTYmMFdV23N2TdNG573QoEsfRrWKQgWeibmLntzniatZvR9BmLnvSxqu53Kw1UmYPxLgboyZQaXwTCg8MSY3H2EU4pWcQDnRnrVA1xe8fs",
|
||||
"xpub6ASuArnXKPbfEwhqN6e3mwBcDTgzisQN1wXN9BJcM47sSikHjJf3UFHKkNAWbWMiGj7Wf5uMash7SyYq527Hqck2AxYysAA7xmALppuCkwQ");
|
||||
|
||||
var branch3 = branch2.ckd(2+BIP32_PRIME);
|
||||
|
||||
checkKey(branch3,
|
||||
"xprv9z4pot5VBttmtdRTWfWQmoH1taj2axGVzFqSb8C9xaxKymcFzXBDptWmT7FwuEzG3ryjH4ktypQSAewRiNMjANTtpgP4mLTj34bhnZX7UiM",
|
||||
"xpub6D4BDPcP2GT577Vvch3R8wDkScZWzQzMMUm3PWbmWvVJrZwQY4VUNgqFJPMM3No2dFDFGTsxxpG5uJh7n7epu4trkrX7x7DogT5Uv6fcLW5");
|
||||
|
||||
var branch4 = branch3.ckd(2);
|
||||
|
||||
checkKey(branch4,
|
||||
"xprvA2JDeKCSNNZky6uBCviVfJSKyQ1mDYahRjijr5idH2WwLsEd4Hsb2Tyh8RfQMuPh7f7RtyzTtdrbdqqsunu5Mm3wDvUAKRHSC34sJ7in334",
|
||||
"xpub6FHa3pjLCk84BayeJxFW2SP4XRrFd1JYnxeLeU8EqN3vDfZmbqBqaGJAyiLjTAwm6ZLRQUMv1ZACTj37sR62cfN7fe5JnJ7dh8zL4fiyLHV");
|
||||
|
||||
var branch5 = branch4.ckd(1000000000);
|
||||
|
||||
checkKey(branch5,
|
||||
"xprvA41z7zogVVwxVSgdKUHDy1SKmdb533PjDz7J6N6mV6uS3ze1ai8FHa8kmHScGpWmj4WggLyQjgPie1rFSruoUihUZREPSL39UNdE3BBDu76",
|
||||
"xpub6H1LXWLaKsWFhvm6RVpEL9P4KfRZSW7abD2ttkWP3SSQvnyA8FSVqNTEcYFgJS2UaFcxupHiYkro49S8yGasTvXEYBVPamhGW6cFJodrTHy");
|
||||
|
||||
});
|
||||
|
||||
test("BIP32 Test vector 2", function () {
|
||||
var seed_str = 'fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542';
|
||||
var seed = bytesToString(hexToBytes(seed_str));
|
||||
|
||||
var key = new BIP32key(seed);
|
||||
|
||||
checkKey(key,
|
||||
"xprv9s21ZrQH143K31xYSDQpPDxsXRTUcvj2iNHm5NUtrGiGG5e2DtALGdso3pGz6ssrdK4PFmM8NSpSBHNqPqm55Qn3LqFtT2emdEXVYsCzC2U",
|
||||
"xpub661MyMwAqRbcFW31YEwpkMuc5THy2PSt5bDMsktWQcFF8syAmRUapSCGu8ED9W6oDMSgv6Zz8idoc4a6mr8BDzTJY47LJhkJ8UB7WEGuduB");
|
||||
|
||||
var branch = key.ckd(0);
|
||||
|
||||
checkKey(branch,
|
||||
"xprv9vHkqa6EV4sPZHYqZznhT2NPtPCjKuDKGY38FBWLvgaDx45zo9WQRUT3dKYnjwih2yJD9mkrocEZXo1ex8G81dwSM1fwqWpWkeS3v86pgKt",
|
||||
"xpub69H7F5d8KSRgmmdJg2KhpAK8SR3DjMwAdkxj3ZuxV27CprR9LgpeyGmXUbC6wb7ERfvrnKZjXoUmmDznezpbZb7ap6r1D3tgFxHmwMkQTPH");
|
||||
|
||||
var branch2 = branch.ckd(2147483647+BIP32_PRIME);
|
||||
|
||||
checkKey(branch2,
|
||||
"xprv9wSp6B7kry3Vj9m1zSnLvN3xH8RdsPP1Mh7fAaR7aRLcQMKTR2vidYEeEg2mUCTAwCd6vnxVrcjfy2kRgVsFawNzmjuHc2YmYRmagcEPdU9",
|
||||
"xpub6ASAVgeehLbnwdqV6UKMHVzgqAG8Gr6riv3Fxxpj8ksbH9ebxaEyBLZ85ySDhKiLDBrQSARLq1uNRts8RuJiHjaDMBU4Zn9h8LZNnBC5y4a");
|
||||
|
||||
var branch3 = branch2.ckd(1);
|
||||
|
||||
checkKey(branch3,
|
||||
"xprv9zFnWC6h2cLgpmSA46vutJzBcfJ8yaJGg8cX1e5StJh45BBciYTRXSd25UEPVuesF9yog62tGAQtHjXajPPdbRCHuWS6T8XA2ECKADdw4Ef",
|
||||
"xpub6DF8uhdarytz3FWdA8TvFSvvAh8dP3283MY7p2V4SeE2wyWmG5mg5EwVvmdMVCQcoNJxGoWaU9DCWh89LojfZ537wTfunKau47EL2dhHKon");
|
||||
|
||||
|
||||
var branch4 = branch3.ckd(2147483646+BIP32_PRIME);
|
||||
|
||||
checkKey(branch4,
|
||||
"xprvA1RpRA33e1JQ7ifknakTFpgNXPmW2YvmhqLQYMmrj4xJXXWYpDPS3xz7iAxn8L39njGVyuoseXzU6rcxFLJ8HFsTjSyQbLYnMpCqE2VbFWc",
|
||||
"xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL");
|
||||
|
||||
var branch5 = branch4.ckd(2);
|
||||
|
||||
checkKey(branch5,
|
||||
"xprvA2nrNbFZABcdryreWet9Ea4LvTJcGsqrMzxHx98MMrotbir7yrKCEXw7nadnHM8Dq38EGfSh6dqA9QWTyefMLEcBYJUuekgW4BYPJcr9E7j",
|
||||
"xpub6FnCn6nSzZAw5Tw7cgR9bi15UV96gLZhjDstkXXxvCLsUXBGXPdSnLFbdpq8p9HmGsApME5hQTZ3emM2rnY5agb9rXpVGyy3bdW6EEgAtqt");
|
||||
});
|
Loading…
Reference in a new issue