Andreas Brekken
11 years ago
9 changed files with 314 additions and 272 deletions
@ -1,46 +1,52 @@ |
|||
/* global describe, it */ |
|||
var assert = require('assert'); |
|||
var Address = require('..').Address; |
|||
|
|||
test('string', function() { |
|||
var addr = '18fN1QTGWmHWCA9r2dyDH6FbMEyc7XHmQQ'; |
|||
assert.equal((new Address(addr)).toString(), addr); |
|||
}); |
|||
|
|||
test('valid', function() { |
|||
function validate(addr, type) { |
|||
assert.ok(Address.validate(addr, type)); |
|||
}; |
|||
|
|||
validate('1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa'); |
|||
validate('1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa', 'prod'); |
|||
validate('mzBc4XEFSdzCDcTxAgf6EZXgsZWpztRhef'); |
|||
validate('mzBc4XEFSdzCDcTxAgf6EZXgsZWpztRhef', 'testnet'); |
|||
|
|||
validate('12KYrjTdVGjFMtaxERSk3gphreJ5US8aUP'); |
|||
validate('12QeMLzSrB8XH8FvEzPMVoRxVAzTr5XM2y'); |
|||
validate('1oNLrsHnBcR6dpaBpwz3LSwutbUNkNSjs'); |
|||
validate('1SQHtwR5oJRKLfiWQ2APsAd9miUc4k2ez'); |
|||
validate('116CGDLddrZhMrTwhCVJXtXQpxygTT1kHd'); |
|||
|
|||
// p2sh addresses
|
|||
validate('3NJZLcZEEYBpxYEUGewU4knsQRn1WM5Fkt'); |
|||
validate('3NJZLcZEEYBpxYEUGewU4knsQRn1WM5Fkt', 'prod'); |
|||
validate('2MxKEf2su6FGAUfCEAHreGFQvEYrfYNHvL7'); |
|||
validate('2MxKEf2su6FGAUfCEAHreGFQvEYrfYNHvL7', 'testnet'); |
|||
}); |
|||
|
|||
|
|||
test('invalid', function() { |
|||
function invalid(addr, type) { |
|||
assert.ok(!Address.validate(addr, type)); |
|||
}; |
|||
|
|||
invalid(''); |
|||
invalid('mzBc4XEFSdzCDcTxAgf6EZXgsZWpztRhe'); |
|||
invalid('1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa', 'testnet'); |
|||
invalid('mzBc4XEFSdzCDcTxAgf6EZXgsZWpztRhef', 'prod'); |
|||
|
|||
// invalid base58 string
|
|||
invalid('%%@'); |
|||
}); |
|||
|
|||
var Address = require('../src/address.js'); |
|||
|
|||
describe('Address', function() { |
|||
describe('toString', function() { |
|||
it('defaults to base58', function() { |
|||
var addr = '18fN1QTGWmHWCA9r2dyDH6FbMEyc7XHmQQ'; |
|||
assert.equal((new Address(addr)).toString(), addr); |
|||
}) |
|||
}) |
|||
|
|||
describe('validate', function() { |
|||
it('validates known good addresses', function() { |
|||
function validate(addr) { |
|||
assert.ok(Address.validate(addr)); |
|||
} |
|||
|
|||
validate('1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa'); |
|||
// validate('1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa', 'prod');
|
|||
validate('mzBc4XEFSdzCDcTxAgf6EZXgsZWpztRhef'); |
|||
// validate('mzBc4XEFSdzCDcTxAgf6EZXgsZWpztRhef', 'testnet');
|
|||
|
|||
validate('12KYrjTdVGjFMtaxERSk3gphreJ5US8aUP'); |
|||
validate('12QeMLzSrB8XH8FvEzPMVoRxVAzTr5XM2y'); |
|||
validate('1oNLrsHnBcR6dpaBpwz3LSwutbUNkNSjs'); |
|||
validate('1SQHtwR5oJRKLfiWQ2APsAd9miUc4k2ez'); |
|||
validate('116CGDLddrZhMrTwhCVJXtXQpxygTT1kHd'); |
|||
|
|||
// p2sh addresses
|
|||
validate('3NJZLcZEEYBpxYEUGewU4knsQRn1WM5Fkt'); |
|||
// validate('3NJZLcZEEYBpxYEUGewU4knsQRn1WM5Fkt', 'prod');
|
|||
validate('2MxKEf2su6FGAUfCEAHreGFQvEYrfYNHvL7'); |
|||
// validate('2MxKEf2su6FGAUfCEAHreGFQvEYrfYNHvL7', 'testnet');
|
|||
}) |
|||
|
|||
it('does not validate illegal examples', function() { |
|||
function invalid(addr) { |
|||
assert.ok(!Address.validate(addr)); |
|||
} |
|||
|
|||
invalid(''); |
|||
invalid('mzBc4XEFSdzCDcTxAgf6EZXgsZWpztRhe'); |
|||
|
|||
// invalid('1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa', 'testnet');
|
|||
// invalid('mzBc4XEFSdzCDcTxAgf6EZXgsZWpztRhef', 'prod');
|
|||
|
|||
// invalid base58 string
|
|||
invalid('%%@'); |
|||
}) |
|||
}) |
|||
}) |
|||
|
@ -1,15 +1,22 @@ |
|||
/* global describe, it */ |
|||
var assert = require('assert'); |
|||
var base58 = require('../').base58; |
|||
var conv = require('../').convert; |
|||
|
|||
test('decode base58', function() { |
|||
var enc = '5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ'; |
|||
var hex = '800c28fca386c7a227600b2fe50b7cae11ec86d3bf1fbe471be89827e19d72aa1d507a5b8d'; |
|||
assert.deepEqual(base58.decode(enc), conv.hexToBytes(hex)); |
|||
}); |
|||
describe('base58', function() { |
|||
describe('decode', function() { |
|||
it('validates known examples', function() { |
|||
var enc = '5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ'; |
|||
var hex = '800c28fca386c7a227600b2fe50b7cae11ec86d3bf1fbe471be89827e19d72aa1d507a5b8d'; |
|||
assert.deepEqual(base58.decode(enc), conv.hexToBytes(hex)); |
|||
}) |
|||
}) |
|||
|
|||
test('encode base58', function() { |
|||
var enc = '5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ'; |
|||
var hex = '800c28fca386c7a227600b2fe50b7cae11ec86d3bf1fbe471be89827e19d72aa1d507a5b8d'; |
|||
assert.equal(base58.encode(conv.hexToBytes(hex)), enc); |
|||
}); |
|||
describe('encode', function() { |
|||
it('handles known examples', function() { |
|||
var enc = '5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ'; |
|||
var hex = '800c28fca386c7a227600b2fe50b7cae11ec86d3bf1fbe471be89827e19d72aa1d507a5b8d'; |
|||
assert.equal(base58.encode(conv.hexToBytes(hex)), enc); |
|||
}) |
|||
}) |
|||
}) |
|||
|
@ -1,98 +1,100 @@ |
|||
// Tests from https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#Test_Vectors
|
|||
|
|||
/* global describe, it */ |
|||
var assert = require('assert'); |
|||
var BIP32key = require('..').BIP32key; |
|||
|
|||
var hexToBytes = require('../').convert.hexToBytes; |
|||
var bytesToString = require('../').convert.bytesToString; |
|||
var BIP32key = require('../src/bip32.js'); |
|||
var convert = require('../src/convert.js') |
|||
var hexToBytes = convert.hexToBytes; |
|||
var bytesToString = convert.bytesToString; |
|||
|
|||
var BIP32_PRIME = 0x80000000; |
|||
|
|||
function checkKey(key, extPriv, extPub) { |
|||
assert.equal(key.serialize(), extPriv); |
|||
assert.equal(key.getPub().serialize(), 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); |
|||
describe('BIP32key', function() { |
|||
describe('BIP-0032 Test Vectors', function() { |
|||
// Extracted from https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#Test_Vectors
|
|||
it('handles vector 1', function() { |
|||
var seed_str = '000102030405060708090a0b0c0d0e0f'; |
|||
var seed = bytesToString(hexToBytes(seed_str)); |
|||
|
|||
checkKey(key, |
|||
"xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi", |
|||
"xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8"); |
|||
var key = new BIP32key(seed); |
|||
|
|||
var branch = key.ckd(0+BIP32_PRIME); |
|||
checkKey(key, |
|||
'xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi', |
|||
'xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8'); |
|||
|
|||
checkKey(branch, |
|||
"xprv9uHRZZhk6KAJC1avXpDAp4MDc3sQKNxDiPvvkX8Br5ngLNv1TxvUxt4cV1rGL5hj6KCesnDYUhd7oWgT11eZG7XnxHrnYeSvkzY7d2bhkJ7", |
|||
"xpub68Gmy5EdvgibQVfPdqkBBCHxA5htiqg55crXYuXoQRKfDBFA1WEjWgP6LHhwBZeNK1VTsfTFUHCdrfp1bgwQ9xv5ski8PX9rL2dZXvgGDnw"); |
|||
var branch = key.ckd(0 + BIP32_PRIME); |
|||
|
|||
var branch2 = branch.ckd(1); |
|||
checkKey(branch, |
|||
'xprv9uHRZZhk6KAJC1avXpDAp4MDc3sQKNxDiPvvkX8Br5ngLNv1TxvUxt4cV1rGL5hj6KCesnDYUhd7oWgT11eZG7XnxHrnYeSvkzY7d2bhkJ7', |
|||
'xpub68Gmy5EdvgibQVfPdqkBBCHxA5htiqg55crXYuXoQRKfDBFA1WEjWgP6LHhwBZeNK1VTsfTFUHCdrfp1bgwQ9xv5ski8PX9rL2dZXvgGDnw'); |
|||
|
|||
checkKey(branch2, |
|||
"xprv9wTYmMFdV23N2TdNG573QoEsfRrWKQgWeibmLntzniatZvR9BmLnvSxqu53Kw1UmYPxLgboyZQaXwTCg8MSY3H2EU4pWcQDnRnrVA1xe8fs", |
|||
"xpub6ASuArnXKPbfEwhqN6e3mwBcDTgzisQN1wXN9BJcM47sSikHjJf3UFHKkNAWbWMiGj7Wf5uMash7SyYq527Hqck2AxYysAA7xmALppuCkwQ"); |
|||
var branch2 = branch.ckd(1); |
|||
|
|||
var branch3 = branch2.ckd(2+BIP32_PRIME); |
|||
checkKey(branch2, |
|||
'xprv9wTYmMFdV23N2TdNG573QoEsfRrWKQgWeibmLntzniatZvR9BmLnvSxqu53Kw1UmYPxLgboyZQaXwTCg8MSY3H2EU4pWcQDnRnrVA1xe8fs', |
|||
'xpub6ASuArnXKPbfEwhqN6e3mwBcDTgzisQN1wXN9BJcM47sSikHjJf3UFHKkNAWbWMiGj7Wf5uMash7SyYq527Hqck2AxYysAA7xmALppuCkwQ'); |
|||
|
|||
checkKey(branch3, |
|||
"xprv9z4pot5VBttmtdRTWfWQmoH1taj2axGVzFqSb8C9xaxKymcFzXBDptWmT7FwuEzG3ryjH4ktypQSAewRiNMjANTtpgP4mLTj34bhnZX7UiM", |
|||
"xpub6D4BDPcP2GT577Vvch3R8wDkScZWzQzMMUm3PWbmWvVJrZwQY4VUNgqFJPMM3No2dFDFGTsxxpG5uJh7n7epu4trkrX7x7DogT5Uv6fcLW5"); |
|||
var branch3 = branch2.ckd(2 + BIP32_PRIME); |
|||
|
|||
var branch4 = branch3.ckd(2); |
|||
checkKey(branch3, |
|||
'xprv9z4pot5VBttmtdRTWfWQmoH1taj2axGVzFqSb8C9xaxKymcFzXBDptWmT7FwuEzG3ryjH4ktypQSAewRiNMjANTtpgP4mLTj34bhnZX7UiM', |
|||
'xpub6D4BDPcP2GT577Vvch3R8wDkScZWzQzMMUm3PWbmWvVJrZwQY4VUNgqFJPMM3No2dFDFGTsxxpG5uJh7n7epu4trkrX7x7DogT5Uv6fcLW5'); |
|||
|
|||
checkKey(branch4, |
|||
"xprvA2JDeKCSNNZky6uBCviVfJSKyQ1mDYahRjijr5idH2WwLsEd4Hsb2Tyh8RfQMuPh7f7RtyzTtdrbdqqsunu5Mm3wDvUAKRHSC34sJ7in334", |
|||
"xpub6FHa3pjLCk84BayeJxFW2SP4XRrFd1JYnxeLeU8EqN3vDfZmbqBqaGJAyiLjTAwm6ZLRQUMv1ZACTj37sR62cfN7fe5JnJ7dh8zL4fiyLHV"); |
|||
var branch4 = branch3.ckd(2); |
|||
|
|||
var branch5 = branch4.ckd(1000000000); |
|||
checkKey(branch4, |
|||
'xprvA2JDeKCSNNZky6uBCviVfJSKyQ1mDYahRjijr5idH2WwLsEd4Hsb2Tyh8RfQMuPh7f7RtyzTtdrbdqqsunu5Mm3wDvUAKRHSC34sJ7in334', |
|||
'xpub6FHa3pjLCk84BayeJxFW2SP4XRrFd1JYnxeLeU8EqN3vDfZmbqBqaGJAyiLjTAwm6ZLRQUMv1ZACTj37sR62cfN7fe5JnJ7dh8zL4fiyLHV'); |
|||
|
|||
checkKey(branch5, |
|||
"xprvA41z7zogVVwxVSgdKUHDy1SKmdb533PjDz7J6N6mV6uS3ze1ai8FHa8kmHScGpWmj4WggLyQjgPie1rFSruoUihUZREPSL39UNdE3BBDu76", |
|||
"xpub6H1LXWLaKsWFhvm6RVpEL9P4KfRZSW7abD2ttkWP3SSQvnyA8FSVqNTEcYFgJS2UaFcxupHiYkro49S8yGasTvXEYBVPamhGW6cFJodrTHy"); |
|||
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)); |
|||
it('handles vector 2', function() { |
|||
var seed_str = 'fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542'; |
|||
var seed = bytesToString(hexToBytes(seed_str)); |
|||
|
|||
var key = new BIP32key(seed); |
|||
var key = new BIP32key(seed); |
|||
|
|||
checkKey(key, |
|||
"xprv9s21ZrQH143K31xYSDQpPDxsXRTUcvj2iNHm5NUtrGiGG5e2DtALGdso3pGz6ssrdK4PFmM8NSpSBHNqPqm55Qn3LqFtT2emdEXVYsCzC2U", |
|||
"xpub661MyMwAqRbcFW31YEwpkMuc5THy2PSt5bDMsktWQcFF8syAmRUapSCGu8ED9W6oDMSgv6Zz8idoc4a6mr8BDzTJY47LJhkJ8UB7WEGuduB"); |
|||
checkKey(key, |
|||
'xprv9s21ZrQH143K31xYSDQpPDxsXRTUcvj2iNHm5NUtrGiGG5e2DtALGdso3pGz6ssrdK4PFmM8NSpSBHNqPqm55Qn3LqFtT2emdEXVYsCzC2U', |
|||
'xpub661MyMwAqRbcFW31YEwpkMuc5THy2PSt5bDMsktWQcFF8syAmRUapSCGu8ED9W6oDMSgv6Zz8idoc4a6mr8BDzTJY47LJhkJ8UB7WEGuduB'); |
|||
|
|||
var branch = key.ckd(0); |
|||
var branch = key.ckd(0); |
|||
|
|||
checkKey(branch, |
|||
"xprv9vHkqa6EV4sPZHYqZznhT2NPtPCjKuDKGY38FBWLvgaDx45zo9WQRUT3dKYnjwih2yJD9mkrocEZXo1ex8G81dwSM1fwqWpWkeS3v86pgKt", |
|||
"xpub69H7F5d8KSRgmmdJg2KhpAK8SR3DjMwAdkxj3ZuxV27CprR9LgpeyGmXUbC6wb7ERfvrnKZjXoUmmDznezpbZb7ap6r1D3tgFxHmwMkQTPH"); |
|||
checkKey(branch, |
|||
'xprv9vHkqa6EV4sPZHYqZznhT2NPtPCjKuDKGY38FBWLvgaDx45zo9WQRUT3dKYnjwih2yJD9mkrocEZXo1ex8G81dwSM1fwqWpWkeS3v86pgKt', |
|||
'xpub69H7F5d8KSRgmmdJg2KhpAK8SR3DjMwAdkxj3ZuxV27CprR9LgpeyGmXUbC6wb7ERfvrnKZjXoUmmDznezpbZb7ap6r1D3tgFxHmwMkQTPH'); |
|||
|
|||
var branch2 = branch.ckd(2147483647+BIP32_PRIME); |
|||
var branch2 = branch.ckd(2147483647 + BIP32_PRIME); |
|||
|
|||
checkKey(branch2, |
|||
"xprv9wSp6B7kry3Vj9m1zSnLvN3xH8RdsPP1Mh7fAaR7aRLcQMKTR2vidYEeEg2mUCTAwCd6vnxVrcjfy2kRgVsFawNzmjuHc2YmYRmagcEPdU9", |
|||
"xpub6ASAVgeehLbnwdqV6UKMHVzgqAG8Gr6riv3Fxxpj8ksbH9ebxaEyBLZ85ySDhKiLDBrQSARLq1uNRts8RuJiHjaDMBU4Zn9h8LZNnBC5y4a"); |
|||
checkKey(branch2, |
|||
'xprv9wSp6B7kry3Vj9m1zSnLvN3xH8RdsPP1Mh7fAaR7aRLcQMKTR2vidYEeEg2mUCTAwCd6vnxVrcjfy2kRgVsFawNzmjuHc2YmYRmagcEPdU9', |
|||
'xpub6ASAVgeehLbnwdqV6UKMHVzgqAG8Gr6riv3Fxxpj8ksbH9ebxaEyBLZ85ySDhKiLDBrQSARLq1uNRts8RuJiHjaDMBU4Zn9h8LZNnBC5y4a'); |
|||
|
|||
var branch3 = branch2.ckd(1); |
|||
var branch3 = branch2.ckd(1); |
|||
|
|||
checkKey(branch3, |
|||
"xprv9zFnWC6h2cLgpmSA46vutJzBcfJ8yaJGg8cX1e5StJh45BBciYTRXSd25UEPVuesF9yog62tGAQtHjXajPPdbRCHuWS6T8XA2ECKADdw4Ef", |
|||
"xpub6DF8uhdarytz3FWdA8TvFSvvAh8dP3283MY7p2V4SeE2wyWmG5mg5EwVvmdMVCQcoNJxGoWaU9DCWh89LojfZ537wTfunKau47EL2dhHKon"); |
|||
|
|||
checkKey(branch3, |
|||
'xprv9zFnWC6h2cLgpmSA46vutJzBcfJ8yaJGg8cX1e5StJh45BBciYTRXSd25UEPVuesF9yog62tGAQtHjXajPPdbRCHuWS6T8XA2ECKADdw4Ef', |
|||
'xpub6DF8uhdarytz3FWdA8TvFSvvAh8dP3283MY7p2V4SeE2wyWmG5mg5EwVvmdMVCQcoNJxGoWaU9DCWh89LojfZ537wTfunKau47EL2dhHKon'); |
|||
|
|||
var branch4 = branch3.ckd(2147483646+BIP32_PRIME); |
|||
var branch4 = branch3.ckd(2147483646 + BIP32_PRIME); |
|||
|
|||
checkKey(branch4, |
|||
"xprvA1RpRA33e1JQ7ifknakTFpgNXPmW2YvmhqLQYMmrj4xJXXWYpDPS3xz7iAxn8L39njGVyuoseXzU6rcxFLJ8HFsTjSyQbLYnMpCqE2VbFWc", |
|||
"xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL"); |
|||
checkKey(branch4, |
|||
'xprvA1RpRA33e1JQ7ifknakTFpgNXPmW2YvmhqLQYMmrj4xJXXWYpDPS3xz7iAxn8L39njGVyuoseXzU6rcxFLJ8HFsTjSyQbLYnMpCqE2VbFWc', |
|||
'xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL'); |
|||
|
|||
var branch5 = branch4.ckd(2); |
|||
var branch5 = branch4.ckd(2); |
|||
|
|||
checkKey(branch5, |
|||
"xprvA2nrNbFZABcdryreWet9Ea4LvTJcGsqrMzxHx98MMrotbir7yrKCEXw7nadnHM8Dq38EGfSh6dqA9QWTyefMLEcBYJUuekgW4BYPJcr9E7j", |
|||
"xpub6FnCn6nSzZAw5Tw7cgR9bi15UV96gLZhjDstkXXxvCLsUXBGXPdSnLFbdpq8p9HmGsApME5hQTZ3emM2rnY5agb9rXpVGyy3bdW6EEgAtqt"); |
|||
}); |
|||
checkKey(branch5, |
|||
'xprvA2nrNbFZABcdryreWet9Ea4LvTJcGsqrMzxHx98MMrotbir7yrKCEXw7nadnHM8Dq38EGfSh6dqA9QWTyefMLEcBYJUuekgW4BYPJcr9E7j', |
|||
'xpub6FnCn6nSzZAw5Tw7cgR9bi15UV96gLZhjDstkXXxvCLsUXBGXPdSnLFbdpq8p9HmGsApME5hQTZ3emM2rnY5agb9rXpVGyy3bdW6EEgAtqt'); |
|||
}) |
|||
}) |
|||
}) |
|||
|
@ -1,24 +1,28 @@ |
|||
/* global describe, it */ |
|||
var assert = require('assert'); |
|||
var conv = require('../').convert; |
|||
var convert = require('../src/convert.js'); |
|||
|
|||
var bytesToHex = conv.bytesToHex; |
|||
var hexToBytes = conv.hexToBytes; |
|||
describe('convert', function() { |
|||
describe('bytesToHex', function() { |
|||
it('handles example 1', function() { |
|||
assert.equal(convert.bytesToHex([0, 1, 2, 255]), '000102ff'); |
|||
}) |
|||
}) |
|||
|
|||
test('bytesToHex', function() { |
|||
assert.equal(bytesToHex([0, 1, 2, 255]), '000102ff'); |
|||
}); |
|||
describe('hexToBytes', function() { |
|||
it('handles example 1', function() { |
|||
assert.deepEqual(convert.hexToBytes('000102ff'), [0, 1, 2, 255]); |
|||
}) |
|||
}) |
|||
|
|||
test('hexToBytes', function() { |
|||
assert.deepEqual(hexToBytes('000102ff'), [0, 1, 2, 255]); |
|||
}); |
|||
it('converts from bytes to hex and back', function() { |
|||
var bytes = []; |
|||
for (var i=0 ; i<256 ; ++i) { |
|||
bytes.push(i); |
|||
} |
|||
|
|||
test('bytesToHex - hexToBytes', function() { |
|||
var bytes = []; |
|||
for (var i=0 ; i<256 ; ++i) { |
|||
bytes.push(i); |
|||
} |
|||
|
|||
var hex = bytesToHex(bytes); |
|||
assert.equal(hex.length, 512); |
|||
assert.deepEqual(hexToBytes(hex), bytes); |
|||
}); |
|||
var hex = convert.bytesToHex(bytes); |
|||
assert.equal(hex.length, 512); |
|||
assert.deepEqual(convert.hexToBytes(hex), bytes); |
|||
}) |
|||
}) |
|||
|
@ -1,20 +1,23 @@ |
|||
/* global describe, it */ |
|||
var assert = require('assert'); |
|||
var sec = require('../src/jsbn/sec'); |
|||
var ecdsa = require('../').ecdsa; |
|||
|
|||
var ecparams = sec('secp256k1'); |
|||
|
|||
test("Point multiplication", function () { |
|||
var G = ecparams.getG(); |
|||
var n = ecparams.getN(); |
|||
describe('ecdsa', function() { |
|||
it('handles point ultiplication', function() { |
|||
var G = ecparams.getG(); |
|||
var n = ecparams.getN(); |
|||
|
|||
assert.ok(G.multiply(n).isInfinity(), "Gn is infinite"); |
|||
assert.ok(G.multiply(n).isInfinity(), "Gn is infinite"); |
|||
|
|||
var k = ecdsa.getBigRandom(n); |
|||
var P = G.multiply(k); |
|||
assert.ok(!P.isInfinity(), "kG is not infinite"); |
|||
assert.ok(P.isOnCurve(), "kG on curve"); |
|||
assert.ok(P.multiply(n).isInfinity(), "kGn is infinite"); |
|||
var k = ecdsa.getBigRandom(n); |
|||
var P = G.multiply(k); |
|||
assert.ok(!P.isInfinity(), "kG is not infinite"); |
|||
assert.ok(P.isOnCurve(), "kG on curve"); |
|||
assert.ok(P.multiply(n).isInfinity(), "kGn is infinite"); |
|||
|
|||
assert.ok(P.validate(), "kG validates as a public key"); |
|||
}); |
|||
assert.ok(P.validate(), "kG validates as a public key"); |
|||
}) |
|||
}) |
|||
|
@ -1,35 +1,43 @@ |
|||
/* global describe, it */ |
|||
var assert = require('assert'); |
|||
var Key = require('../').Key; |
|||
var bytesToHex = require('../').convert.bytesToHex; |
|||
var hexToBytes = require('../').convert.hexToBytes; |
|||
var base58 = require('../').base58; |
|||
|
|||
// get public key from private key
|
|||
test('from private base58', function() { |
|||
|
|||
var priv = '18e14a7b6a307f426a94f8114701e7c8e774e7f9a47e2c2035db29a206321725'; |
|||
var pub = '0450863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b23522cd470243453a299fa9e77237716103abc11a1df38855ed6f2ee187e9c582ba6'; |
|||
var key = Key(hexToBytes(priv)); |
|||
|
|||
assert.equal(bytesToHex(key.getPub()), pub); |
|||
assert.equal(key.compressed, false); |
|||
|
|||
var priv = '5HwoXVkHoRM8sL2KmNRS217n1g8mPPBomrY7yehCuXC1115WWsh'; |
|||
var pub = '044f355bdcb7cc0af728ef3cceb9615d90684bb5b2ca5f859ab0f0b704075871aa385b6b1b8ead809ca67454d9683fcf2ba03456d6fe2c4abe2b07f0fbdbb2f1c1'; |
|||
var addr = '1MsHWS1BnwMc3tLE8G35UXsS58fKipzB7a'; |
|||
var key = Key(priv); |
|||
|
|||
assert.equal(key.compressed, false); |
|||
assert.equal(bytesToHex(key.getPub()), pub); |
|||
assert.equal(key.getBitcoinAddress().toString(), addr); |
|||
|
|||
var priv = 'KwntMbt59tTsj8xqpqYqRRWufyjGunvhSyeMo3NTYpFYzZbXJ5Hp'; |
|||
var pub = '034f355bdcb7cc0af728ef3cceb9615d90684bb5b2ca5f859ab0f0b704075871aa' |
|||
var addr = '1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nK9'; |
|||
var key = Key(priv); |
|||
|
|||
assert.equal(key.compressed, true); |
|||
assert.equal(bytesToHex(key.getPub()), pub); |
|||
assert.equal(key.getBitcoinAddress().toString(), addr); |
|||
}); |
|||
|
|||
var ECKey = require('../src/eckey.js').ECKey; |
|||
var convert = require('../src/convert.js'); |
|||
var bytesToHex = convert.bytesToHex; |
|||
var hexToBytes = convert.hexToBytes; |
|||
|
|||
describe('ECKey', function() { |
|||
describe('constructor (base58 private)', function() { |
|||
it('parses case 1', function() { |
|||
var priv = '18e14a7b6a307f426a94f8114701e7c8e774e7f9a47e2c2035db29a206321725'; |
|||
var pub = '0450863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b235' + |
|||
'22cd470243453a299fa9e77237716103abc11a1df38855ed6f2ee187e9c582ba6'; |
|||
var key = new ECKey(hexToBytes(priv)); |
|||
|
|||
assert.equal(bytesToHex(key.getPub()['export']('bytes')), pub); |
|||
assert.equal(key.compressed, false); |
|||
}) |
|||
|
|||
it('parses case 2', function() { |
|||
var priv = '5HwoXVkHoRM8sL2KmNRS217n1g8mPPBomrY7yehCuXC1115WWsh'; |
|||
var pub = '044f355bdcb7cc0af728ef3cceb9615d90684bb5b2ca5f859ab0' + |
|||
'f0b704075871aa385b6b1b8ead809ca67454d9683fcf2ba03456d6fe2c4abe2b07f0fbdbb2f1c1'; |
|||
var addr = '1MsHWS1BnwMc3tLE8G35UXsS58fKipzB7a'; |
|||
var key = new ECKey(priv); |
|||
|
|||
assert.equal(key.compressed, false); |
|||
assert.equal(bytesToHex(key.getPub()['export']('bytes')), pub); |
|||
assert.equal(key.getBitcoinAddress().toString(), addr); |
|||
}) |
|||
|
|||
it('parses case 3', function() { |
|||
var priv = 'KwntMbt59tTsj8xqpqYqRRWufyjGunvhSyeMo3NTYpFYzZbXJ5Hp'; |
|||
var pub = '034f355bdcb7cc0af728ef3cceb9615d90684bb5b2ca5f859ab0f0b704075871aa' |
|||
var addr = '1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nK9'; |
|||
var key = new ECKey(priv); |
|||
|
|||
assert.equal(key.compressed, true); |
|||
assert.equal(bytesToHex(key.getPub()['export']('bytes')), pub); |
|||
assert.equal(key.getBitcoinAddress().toString(), addr); |
|||
}) |
|||
}) |
|||
}) |
|||
|
@ -1,24 +1,28 @@ |
|||
/* global describe, it */ |
|||
var assert = require('assert'); |
|||
var BigInteger = require('../').BigInteger; |
|||
var bytesToHex = require('../').convert.bytesToHex; |
|||
var BigInteger = require('../src/jsbn/jsbn.js') |
|||
var bytesToHex = require('../src/convert.js').bytesToHex; |
|||
|
|||
test('toByteArraySigned', function() { |
|||
function hex(num) { |
|||
var bytes = BigInteger.valueOf(num).toByteArraySigned(); |
|||
var hex = bytesToHex(bytes); |
|||
return '0x' + hex; |
|||
} |
|||
|
|||
assert.equal(hex( 0), '0x'); |
|||
assert.equal(hex( 1), '0x01'); |
|||
assert.equal(hex(-1), '0x81'); |
|||
assert.equal(hex( 127), '0x7f'); |
|||
assert.equal(hex(-127), '0xff'); |
|||
assert.equal(hex( 255), '0x00ff'); |
|||
assert.equal(hex(-255), '0x80ff'); |
|||
assert.equal(hex( 16300), '0x3fac'); |
|||
assert.equal(hex(-16300), '0xbfac'); |
|||
assert.equal(hex( 62300), '0x00f35c'); |
|||
assert.equal(hex(-62300), '0x80f35c'); |
|||
}); |
|||
describe('BigInteger', function() { |
|||
describe('toByteArraySigned', function() { |
|||
it('handles examples', function() { |
|||
function hex(num) { |
|||
var bytes = BigInteger.valueOf(num).toByteArraySigned(); |
|||
var h = bytesToHex(bytes); |
|||
return '0x' + h; |
|||
} |
|||
|
|||
assert.equal(hex( 0), '0x'); |
|||
assert.equal(hex( 1), '0x01'); |
|||
assert.equal(hex(-1), '0x81'); |
|||
assert.equal(hex( 127), '0x7f'); |
|||
assert.equal(hex(-127), '0xff'); |
|||
assert.equal(hex( 255), '0x00ff'); |
|||
assert.equal(hex(-255), '0x80ff'); |
|||
assert.equal(hex( 16300), '0x3fac'); |
|||
assert.equal(hex(-16300), '0xbfac'); |
|||
assert.equal(hex( 62300), '0x00f35c'); |
|||
assert.equal(hex(-62300), '0x80f35c'); |
|||
}) |
|||
}) |
|||
}) |
|||
|
@ -1,28 +1,34 @@ |
|||
/* global describe, it */ |
|||
var assert = require('assert'); |
|||
var Message = require('../').Message; |
|||
var Key = require('../').Key; |
|||
var hexToBytes = require('../').convert.hexToBytes; |
|||
var Message = require('../src/message.js'); |
|||
var ECKey = require('../src/eckey.js').ECKey; |
|||
var hexToBytes = require('../src/convert.js').hexToBytes; |
|||
|
|||
var priv = '18e14a7b6a307f426a94f8114701e7c8e774e7f9a47e2c2035db29a206321725'; |
|||
var addr = '16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM'; |
|||
var msg = 'foobar'; |
|||
|
|||
test('create', function() { |
|||
var key = Key(hexToBytes(priv)); |
|||
assert.equal(key.getBitcoinAddress().toString(), addr); |
|||
describe('Message', function() { |
|||
describe('verify', function() { |
|||
it('passes case 1', function() { |
|||
var key = new ECKey(hexToBytes(priv)); |
|||
assert.equal(key.getBitcoinAddress().toString(), addr); |
|||
|
|||
var sig = Message.signMessage(key, msg); |
|||
assert.ok(Message.verifyMessage(addr, sig, msg)); |
|||
// wrong message
|
|||
assert.ok(!Message.verifyMessage(addr, sig, 'not foobar')); |
|||
// wrong address
|
|||
assert.ok(!Message.verifyMessage('1MsHWS1BnwMc3tLE8G35UXsS58fKipzB7a', sig, msg)); |
|||
}); |
|||
var sig = Message.signMessage(key, msg); |
|||
assert.ok(Message.verifyMessage(addr, sig, msg)); |
|||
|
|||
test('incorrect signature', function() { |
|||
// wrong signature
|
|||
var priv = '5HwoXVkHoRM8sL2KmNRS217n1g8mPPBomrY7yehCuXC1115WWsh'; |
|||
var key = Key(hexToBytes(priv)); |
|||
var sig = Message.signMessage(key, msg); |
|||
assert.ok(!Message.verifyMessage(addr, sig, msg)); |
|||
}); |
|||
// wrong message
|
|||
assert.ok(!Message.verifyMessage(addr, sig, 'not foobar')); |
|||
|
|||
// wrong address
|
|||
assert.ok(!Message.verifyMessage('1MsHWS1BnwMc3tLE8G35UXsS58fKipzB7a', sig, msg)); |
|||
}) |
|||
|
|||
it('passes case 2', function() { |
|||
var priv = '5HwoXVkHoRM8sL2KmNRS217n1g8mPPBomrY7yehCuXC1115WWsh'; |
|||
var key = new ECKey(hexToBytes(priv)); |
|||
var sig = Message.signMessage(key, msg); |
|||
assert.ok(!Message.verifyMessage(addr, sig, msg)); |
|||
}) |
|||
}) |
|||
}) |
|||
|
Loading…
Reference in new issue