From f591cc0111dd87b6da1bc7f18e3bbd13689f68bf Mon Sep 17 00:00:00 2001 From: Andreas Brekken Date: Sat, 11 Jan 2014 13:57:43 +0700 Subject: [PATCH] Reformat all tests for more modern mocha syntax (describe, it) --- test/address.js | 78 +++++++++++++++------------- test/base58.js | 27 ++++++---- test/bip32.js | 134 ++++++++++++++++++++++++------------------------ test/convert.js | 42 ++++++++------- test/ec.js | 25 +++++---- test/eckey.js | 64 +++++++++++++---------- test/jsbn.js | 46 +++++++++-------- test/message.js | 46 +++++++++-------- test/misc.js | 94 ++++++++++++++++----------------- 9 files changed, 299 insertions(+), 257 deletions(-) diff --git a/test/address.js b/test/address.js index 1fc1662..2a9fb2c 100644 --- a/test/address.js +++ b/test/address.js @@ -1,46 +1,52 @@ +/* global describe, it */ var assert = require('assert'); -var Address = require('..').Address; +var Address = require('../src/address.js'); -test('string', function() { - var addr = '18fN1QTGWmHWCA9r2dyDH6FbMEyc7XHmQQ'; - assert.equal((new Address(addr)).toString(), addr); -}); +describe('Address', function() { + describe('toString', function() { + it('defaults to base58', function() { + var addr = '18fN1QTGWmHWCA9r2dyDH6FbMEyc7XHmQQ'; + assert.equal((new Address(addr)).toString(), addr); + }) + }) -test('valid', function() { - function validate(addr, type) { - assert.ok(Address.validate(addr, type)); - }; + 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('1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa'); + // validate('1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa', 'prod'); + validate('mzBc4XEFSdzCDcTxAgf6EZXgsZWpztRhef'); + // validate('mzBc4XEFSdzCDcTxAgf6EZXgsZWpztRhef', 'testnet'); - validate('12KYrjTdVGjFMtaxERSk3gphreJ5US8aUP'); - validate('12QeMLzSrB8XH8FvEzPMVoRxVAzTr5XM2y'); - validate('1oNLrsHnBcR6dpaBpwz3LSwutbUNkNSjs'); - validate('1SQHtwR5oJRKLfiWQ2APsAd9miUc4k2ez'); - validate('116CGDLddrZhMrTwhCVJXtXQpxygTT1kHd'); + validate('12KYrjTdVGjFMtaxERSk3gphreJ5US8aUP'); + validate('12QeMLzSrB8XH8FvEzPMVoRxVAzTr5XM2y'); + validate('1oNLrsHnBcR6dpaBpwz3LSwutbUNkNSjs'); + validate('1SQHtwR5oJRKLfiWQ2APsAd9miUc4k2ez'); + validate('116CGDLddrZhMrTwhCVJXtXQpxygTT1kHd'); - // p2sh addresses - validate('3NJZLcZEEYBpxYEUGewU4knsQRn1WM5Fkt'); - validate('3NJZLcZEEYBpxYEUGewU4knsQRn1WM5Fkt', 'prod'); - validate('2MxKEf2su6FGAUfCEAHreGFQvEYrfYNHvL7'); - validate('2MxKEf2su6FGAUfCEAHreGFQvEYrfYNHvL7', 'testnet'); -}); + // 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)); + } -test('invalid', function() { - function invalid(addr, type) { - assert.ok(!Address.validate(addr, type)); - }; + invalid(''); + invalid('mzBc4XEFSdzCDcTxAgf6EZXgsZWpztRhe'); - invalid(''); - invalid('mzBc4XEFSdzCDcTxAgf6EZXgsZWpztRhe'); - invalid('1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa', 'testnet'); - invalid('mzBc4XEFSdzCDcTxAgf6EZXgsZWpztRhef', 'prod'); - - // invalid base58 string - invalid('%%@'); -}); + // invalid('1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa', 'testnet'); + // invalid('mzBc4XEFSdzCDcTxAgf6EZXgsZWpztRhef', 'prod'); + // invalid base58 string + invalid('%%@'); + }) + }) +}) diff --git a/test/base58.js b/test/base58.js index b7a0cef..4743450 100644 --- a/test/base58.js +++ b/test/base58.js @@ -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); + }) + }) +}) diff --git a/test/bip32.js b/test/bip32.js index 2ccee37..8dad59d 100644 --- a/test/bip32.js +++ b/test/bip32.js @@ -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'); + }) + }) +}) diff --git a/test/convert.js b/test/convert.js index aa7eafe..643cde8 100644 --- a/test/convert.js +++ b/test/convert.js @@ -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); + }) +}) diff --git a/test/ec.js b/test/ec.js index 6ae6e74..bc4b979 100644 --- a/test/ec.js +++ b/test/ec.js @@ -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"); + }) +}) diff --git a/test/eckey.js b/test/eckey.js index acca4d7..6c53999 100644 --- a/test/eckey.js +++ b/test/eckey.js @@ -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; +var ECKey = require('../src/eckey.js').ECKey; +var convert = require('../src/convert.js'); +var bytesToHex = convert.bytesToHex; +var hexToBytes = convert.hexToBytes; -// get public key from private key -test('from private base58', function() { +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)); - var priv = '18e14a7b6a307f426a94f8114701e7c8e774e7f9a47e2c2035db29a206321725'; - var pub = '0450863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b23522cd470243453a299fa9e77237716103abc11a1df38855ed6f2ee187e9c582ba6'; - var key = Key(hexToBytes(priv)); + assert.equal(bytesToHex(key.getPub()['export']('bytes')), pub); + assert.equal(key.compressed, false); + }) - assert.equal(bytesToHex(key.getPub()), 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); - var priv = '5HwoXVkHoRM8sL2KmNRS217n1g8mPPBomrY7yehCuXC1115WWsh'; - var pub = '044f355bdcb7cc0af728ef3cceb9615d90684bb5b2ca5f859ab0f0b704075871aa385b6b1b8ead809ca67454d9683fcf2ba03456d6fe2c4abe2b07f0fbdbb2f1c1'; - var addr = '1MsHWS1BnwMc3tLE8G35UXsS58fKipzB7a'; - var key = Key(priv); + assert.equal(key.compressed, false); + assert.equal(bytesToHex(key.getPub()['export']('bytes')), pub); + assert.equal(key.getBitcoinAddress().toString(), addr); + }) - 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); -}); + 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); + }) + }) +}) diff --git a/test/jsbn.js b/test/jsbn.js index 7b1746f..c2320b3 100644 --- a/test/jsbn.js +++ b/test/jsbn.js @@ -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'); + }) + }) +}) diff --git a/test/message.js b/test/message.js index a202d01..553cd13 100644 --- a/test/message.js +++ b/test/message.js @@ -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)); + }) + }) +}) diff --git a/test/misc.js b/test/misc.js index d3b361b..1ed0b8a 100644 --- a/test/misc.js +++ b/test/misc.js @@ -1,9 +1,9 @@ +/* global it */ var assert = require('assert'); var bitcoinjs = require('../'); var sec = require('../src/jsbn/sec'); -var BigInteger = require('../src/jsbn/jsbn'); - -var Crypto = require('../src/crypto-js/crypto'); +var BigInteger = require('../src/jsbn/jsbn.js'); +var Crypto = require('../src/crypto-js/crypto.js'); var SecureRandom = require('../src/jsbn/rng'); var rng = new SecureRandom(); @@ -11,56 +11,58 @@ var rng = new SecureRandom(); var ecparams = sec('secp256k1'); var ECPointFp = bitcoinjs.ECPointFp; -test("Keys & Key Management", function () { +it('Keys & Key Management', function () { + var p1 = bitcoinjs.Key().getPub()['export']('bytes'); + assert.equal(p1.length, 65); - var p1 = bitcoinjs.Key().getPub(); - assert.equal(p1.length, 65); + var p1_q = ECPointFp.decodeFrom(ecparams.getCurve(), p1); + assert.ok(p1_q); + assert.ok(p1_q.validate()); - var p1_q = ECPointFp.decodeFrom(ecparams.getCurve(), p1); - assert.ok(p1_q); - assert.ok(p1_q.validate()); + var p2 = bitcoinjs.convert.hexToBytes( + '0486f356006a38b847bedec1bf47013776925d939d5a35a97a4d1263e550c7f1a' + + 'b5aba44ab74d22892097a0e851addf07ba97e33416df5affaceeb35d5607cd23c'); - var p2 = bitcoinjs.convert.hexToBytes( - "0486f356006a38b847bedec1bf47013776925d939d5a35a97a4d1263e550c7f1a" + - "b5aba44ab74d22892097a0e851addf07ba97e33416df5affaceeb35d5607cd23c" - ); - - var p2_q = ECPointFp.decodeFrom(ecparams.getCurve(), p2); - assert.ok(p2_q); - assert.ok(p2_q.validate()); + var p2_q = ECPointFp.decodeFrom(ecparams.getCurve(), p2); + assert.ok(p2_q); + assert.ok(p2_q.validate()); }); -test("Signing and Verifying", function () { +it('Signing and Verifying', function () { + var s1 = bitcoinjs.Key(); + var sig_a = s1.sign(BigInteger.ZERO); + assert.ok(sig_a, 'Sign null'); - var s1 = bitcoinjs.Key(); - var sig_a = s1.sign(BigInteger.ZERO); - assert.ok(sig_a, "Sign null"); + assert.ok(s1.verify(BigInteger.ZERO, sig_a)); - assert.ok(s1.verify(BigInteger.ZERO, sig_a)); + var message = new BigInteger(1024, rng).toByteArrayUnsigned(); + var hash = Crypto.SHA256(message, { + asBytes: true + }); + var sig_b = s1.sign(hash); + assert.ok(sig_b, 'Sign random string'); + assert.ok(s1.verify(hash, sig_b)); - var message = new BigInteger(1024, rng).toByteArrayUnsigned(); - var hash = Crypto.SHA256(message, {asBytes: true}); - var sig_b = s1.sign(hash); - assert.ok(sig_b, "Sign random string"); - assert.ok(s1.verify(hash, sig_b)); + var message2 = bitcoinjs.convert.hexToBytes( + '12dce2c169986b3346827ffb2305cf393984627f5f9722a1b1368e933c8d' + + 'd296653fbe5d7ac031c4962ad0eb1c4298c3b91d244e1116b4a76a130c13' + + '1e7aec7fa70184a71a2e66797052831511b93c6e8d72ae58a1980eaacb66' + + '8a33f50d7cefb96a5dab897b5efcb99cbafb0d777cb83fc9b2115b69c0fa' + + '3d82507b932b84e4'); - var message2 = bitcoinjs.convert.hexToBytes( - "12dce2c169986b3346827ffb2305cf393984627f5f9722a1b1368e933c8d" + - "d296653fbe5d7ac031c4962ad0eb1c4298c3b91d244e1116b4a76a130c13" + - "1e7aec7fa70184a71a2e66797052831511b93c6e8d72ae58a1980eaacb66" + - "8a33f50d7cefb96a5dab897b5efcb99cbafb0d777cb83fc9b2115b69c0fa" + - "3d82507b932b84e4" - ); - var hash2 = Crypto.SHA256(message2, {asBytes: true}); - var sig_c = bitcoinjs.convert.hexToBytes( - "3044022038d9b8dd5c9fbf330565c1f51d72a59ba869aeb2c2001be959d3" + - "79e861ec71960220a73945f32cf90d03127d2c3410d16cee120fa1a4b4c3" + - "f273ab082801a95506c4" - ); - var s2 = bitcoinjs.convert.hexToBytes( - "045a1594316e433fb91f35ef4874610d22177c3f1a1060f6c1e70a609d51" + - "b20be5795cd2a5eae0d6b872ba42db95e9afaeea3fbb89e98099575b6828" + - "609a978528" - ); - assert.ok(bitcoinjs.ecdsa.verify(hash2, sig_c, s2), "Verify constant signature"); + var hash2 = Crypto.SHA256(message2, { + asBytes: true + }); + + var sig_c = bitcoinjs.convert.hexToBytes( + '3044022038d9b8dd5c9fbf330565c1f51d72a59ba869aeb2c2001be959d3' + + '79e861ec71960220a73945f32cf90d03127d2c3410d16cee120fa1a4b4c3' + + 'f273ab082801a95506c4'); + + var s2 = bitcoinjs.convert.hexToBytes( + '045a1594316e433fb91f35ef4874610d22177c3f1a1060f6c1e70a609d51' + + 'b20be5795cd2a5eae0d6b872ba42db95e9afaeea3fbb89e98099575b6828' + + '609a978528'); + + assert.ok(bitcoinjs.ecdsa.verify(hash2, sig_c, s2), 'Verify constant signature'); });