From d2e61d1fa22c3e1a61db77555b075e3155ce267d Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sat, 10 Dec 2016 14:25:02 -0800 Subject: [PATCH] test: cleanup tests. --- lib/primitives/tx.js | 2 +- test/aes-test.js | 3 +- test/bip150-test.js | 29 ++++--- test/bip151-test.js | 11 +-- test/bip70-test.js | 42 ++++++---- test/block-test.js | 93 +++++++++++---------- test/bloom-test.js | 89 +++++++++++--------- test/chachapoly-test.js | 16 ++-- test/chain-test.js | 60 +++++++------- test/hd-test.js | 21 +++-- test/http-test.js | 39 ++++----- test/mempool-test.js | 108 ++++++++++++------------ test/mnemonic-test.js | 28 ++++--- test/protocol-test.js | 50 +++++++----- test/script-test.js | 42 +++++----- test/tx-test.js | 70 +++++++++------- test/utils-test.js | 103 ++++++++++++++--------- test/wallet-test.js | 177 +++++++++++++++++++++------------------- 18 files changed, 536 insertions(+), 447 deletions(-) diff --git a/lib/primitives/tx.js b/lib/primitives/tx.js index 7b9a322d..e252f28e 100644 --- a/lib/primitives/tx.js +++ b/lib/primitives/tx.js @@ -2044,7 +2044,7 @@ TX.prototype.format = function format(view) { flag: this.flag, inputs: this.inputs.map(function(input) { var coin = view ? view.getOutput(input) : null; - return input.inspect(coin); + return input.format(coin); }), outputs: this.outputs, locktime: this.locktime diff --git a/test/aes-test.js b/test/aes-test.js index 3924b9b3..838d7979 100644 --- a/test/aes-test.js +++ b/test/aes-test.js @@ -1,9 +1,8 @@ 'use strict'; -var BN = require('bn.js'); +var assert = require('assert'); var util = require('../lib/utils/util'); var crypto = require('../lib/crypto/crypto'); -var assert = require('assert'); var aes = require('../lib/crypto/aes'); var nativeCrypto = require('crypto'); diff --git a/test/bip150-test.js b/test/bip150-test.js index a36610fd..65b67db7 100644 --- a/test/bip150-test.js +++ b/test/bip150-test.js @@ -1,26 +1,25 @@ 'use strict'; -var BN = require('bn.js'); -var bcoin = require('../').set('main'); -var util = bcoin.util; -var crypto = require('../lib/crypto/crypto'); -var constants = bcoin.constants; -var network = bcoin.networks; var assert = require('assert'); +var util = require('../lib/utils/util'); +var crypto = require('../lib/crypto/crypto'); +var ec = require('../lib/crypto/ec'); +var BIP150 = require('../lib/net/bip150'); +var BIP151 = require('../lib/net/bip151'); describe('BIP150', function() { - var db = new bcoin.bip150.AuthDB(); - var ck = bcoin.ec.generatePrivateKey(); - var sk = bcoin.ec.generatePrivateKey(); + var db = new BIP150.AuthDB(); + var ck = ec.generatePrivateKey(); + var sk = ec.generatePrivateKey(); - db.addAuthorized(bcoin.ec.publicKeyCreate(ck, true)); - db.addKnown('server', bcoin.ec.publicKeyCreate(sk, true)); + db.addAuthorized(ec.publicKeyCreate(ck, true)); + db.addKnown('server', ec.publicKeyCreate(sk, true)); - var client = new bcoin.bip151(); - var server = new bcoin.bip151(); + var client = new BIP151(); + var server = new BIP151(); - client.bip150 = new bcoin.bip150(client, 'server', true, db, ck); - server.bip150 = new bcoin.bip150(server, 'client', false, db, sk); + client.bip150 = new BIP150(client, 'server', true, db, ck); + server.bip150 = new BIP150(server, 'client', false, db, sk); function payload() { return new Buffer('deadbeef', 'hex'); diff --git a/test/bip151-test.js b/test/bip151-test.js index 0fd5fd2b..ed89abce 100644 --- a/test/bip151-test.js +++ b/test/bip151-test.js @@ -1,16 +1,11 @@ 'use strict'; -var BN = require('bn.js'); -var bcoin = require('../').set('main'); -var util = bcoin.util; -var crypto = require('../lib/crypto/crypto'); -var constants = bcoin.constants; -var network = bcoin.networks; var assert = require('assert'); +var BIP151 = require('../lib/net/bip151'); describe('BIP151', function() { - var client = new bcoin.bip151(); - var server = new bcoin.bip151(); + var client = new BIP151(); + var server = new BIP151(); function payload() { return new Buffer('deadbeef', 'hex'); diff --git a/test/bip70-test.js b/test/bip70-test.js index 52d372d0..7da84682 100644 --- a/test/bip70-test.js +++ b/test/bip70-test.js @@ -1,16 +1,14 @@ 'use strict'; -var BN = require('bn.js'); -var bcoin = require('../').set('main'); -var util = bcoin.util; -var crypto = require('../lib/crypto/crypto'); -var constants = bcoin.constants; -var network = bcoin.networks; var assert = require('assert'); -var tests = require('./data/bip70.json'); +var util = require('../lib/utils/util'); +var crypto = require('../lib/crypto/crypto'); var bip70 = require('../lib/bip70'); +var Address = require('../lib/primitives/address'); var x509 = bip70.x509; +var tests = require('./data/bip70.json'); + tests.valid = new Buffer(tests.valid, 'hex'); tests.invalid = new Buffer(tests.invalid, 'hex'); tests.untrusted = new Buffer(tests.untrusted, 'hex'); @@ -27,13 +25,16 @@ x509.trusted = {}; describe('BIP70', function() { function testRequest(data) { var request = bip70.PaymentRequest.fromRaw(data); + var ser; + assert.equal(request.pkiType, 'x509+sha256'); assert(request.pkiData); assert(request.getChain()); assert(request.paymentDetails); assert(request.paymentDetails.memo.length !== 0); assert(request.paymentDetails.paymentUrl.length !== 0); - var ser = request.toRaw(); + + ser = request.toRaw(); assert.equal(ser.toString('hex'), data.toString('hex')); assert(request.verify()); } @@ -62,7 +63,7 @@ describe('BIP70', function() { assert(request.verifyChain()); - var request = bip70.PaymentRequest.fromRaw(tests.invalid); + request = bip70.PaymentRequest.fromRaw(tests.invalid); assert.equal(request.version, 1); assert.equal(request.getChain().length, 3); @@ -83,7 +84,7 @@ describe('BIP70', function() { assert.deepStrictEqual(request.paymentDetails.getData('json'), {foo:1}); assert(!request.verify()); - var request = bip70.PaymentRequest.fromRaw(tests.untrusted); + request = bip70.PaymentRequest.fromRaw(tests.untrusted); assert.equal(request.version, -1); assert.equal(request.getChain().length, 2); @@ -102,12 +103,17 @@ describe('BIP70', function() { }); it('should fail to verify cert signatures when enforcing trust', function() { + var request; + x509.allowUntrusted = false; - var request = bip70.PaymentRequest.fromRaw(tests.valid); + + request = bip70.PaymentRequest.fromRaw(tests.valid); assert(!request.verifyChain()); - var request = bip70.PaymentRequest.fromRaw(tests.invalid); + + request = bip70.PaymentRequest.fromRaw(tests.invalid); assert(!request.verifyChain()); - var request = bip70.PaymentRequest.fromRaw(tests.untrusted); + + request = bip70.PaymentRequest.fromRaw(tests.untrusted); assert(!request.verifyChain()); }); @@ -115,7 +121,8 @@ describe('BIP70', function() { var request = bip70.PaymentRequest.fromRaw(tests.valid); x509.setTrust([request.getChain().pop()]); assert(request.verifyChain()); - var request = bip70.PaymentRequest.fromRaw(tests.untrusted); + + request = bip70.PaymentRequest.fromRaw(tests.untrusted); assert(!request.verifyChain()); }); @@ -134,7 +141,8 @@ describe('BIP70', function() { it('should validate untrusted once again', function() { var request = bip70.PaymentRequest.fromRaw(tests.untrusted); x509.setTrust([request.getChain().pop()]); - var request = bip70.PaymentRequest.fromRaw(tests.untrusted); + + request = bip70.PaymentRequest.fromRaw(tests.untrusted); assert(request.verifyChain()); assert.equal(request.getCA().name, 'DigiCert SHA2 Extended Validation Server CA'); @@ -158,8 +166,8 @@ describe('BIP70', function() { time: util.now(), expires: util.now() + 3600, outputs: [ - { value: 10000, address: bcoin.address() }, - { value: 50000, address: bcoin.address() } + { value: 10000, address: new Address() }, + { value: 50000, address: new Address() } ], merchantData: { foo: 'bar' } } diff --git a/test/block-test.js b/test/block-test.js index 987eac91..761b38d9 100644 --- a/test/block-test.js +++ b/test/block-test.js @@ -1,7 +1,7 @@ 'use strict'; -var assert = require('assert'); var fs = require('fs'); +var assert = require('assert'); var util = require('../lib/utils/util'); var btcutils = require('../lib/btc/utils'); var crypto = require('../lib/crypto/crypto'); @@ -16,10 +16,13 @@ var constants = require('../lib/protocol/constants'); var bip152 = require('../lib/net/bip152'); var block300025 = require('./data/block300025.json'); -var cmpct = fs.readFileSync(__dirname + '/data/compactblock.hex', 'utf8').trim().split('\n'); -var cmpct2 = fs.readFileSync(__dirname + '/data/cmpct2', 'utf8').trim(); +var cmpct1 = fs.readFileSync(__dirname + '/data/compactblock.hex', 'utf8'); +var cmpct2 = fs.readFileSync(__dirname + '/data/cmpct2', 'utf8'); var cmpct2block = fs.readFileSync(__dirname + '/data/cmpct2.bin'); +cmpct1 = cmpct1.trim().split('\n'); +cmpct2 = cmpct2.trim(); + describe('Block', function() { var mblock, raw, block, raw2; @@ -245,14 +248,14 @@ describe('Block', function() { }); it('should handle compact block', function(cb) { - var cblock = bip152.CompactBlock.fromRaw(cmpct[0], 'hex'); - var block = Block.fromRaw(cmpct[1], 'hex'); - var cblock2 = bip152.CompactBlock.fromBlock(block, false, cblock.keyNonce); + var block = Block.fromRaw(cmpct1[1], 'hex'); + var cblock1 = bip152.CompactBlock.fromRaw(cmpct1[0], 'hex'); + var cblock2 = bip152.CompactBlock.fromBlock(block, false, cblock1.keyNonce); var map = {}; var i, tx, mempool, result; - assert.equal(cblock.toRaw().toString('hex'), cmpct[0]); - assert.equal(cblock2.toRaw().toString('hex'), cmpct[0]); + assert.equal(cblock1.toRaw().toString('hex'), cmpct1[0]); + assert.equal(cblock2.toRaw().toString('hex'), cmpct1[0]); for (i = 0; i < block.txs.length; i++) { tx = block.txs[i]; @@ -268,30 +271,30 @@ describe('Block', function() { } }; - assert.equal(cblock.sid(block.txs[1].hash()), 125673511480291); + assert.equal(cblock1.sid(block.txs[1].hash()), 125673511480291); - result = cblock.fillMempool(false, mempool); + result = cblock1.fillMempool(false, mempool); assert(result); - for (i = 0; i < cblock.available.length; i++) - assert(cblock.available[i]); + for (i = 0; i < cblock1.available.length; i++) + assert(cblock1.available[i]); assert.equal( - cblock.toBlock().toRaw().toString('hex'), + cblock1.toBlock().toRaw().toString('hex'), block.toRaw().toString('hex')); cb(); }); it('should handle half-full compact block', function(cb) { - var cblock = bip152.CompactBlock.fromRaw(cmpct[0], 'hex'); - var block = Block.fromRaw(cmpct[1], 'hex'); - var cblock2 = bip152.CompactBlock.fromBlock(block, false, cblock.keyNonce); + var block = Block.fromRaw(cmpct1[1], 'hex'); + var cblock1 = bip152.CompactBlock.fromRaw(cmpct1[0], 'hex'); + var cblock2 = bip152.CompactBlock.fromBlock(block, false, cblock1.keyNonce); var map = {}; var i, tx, mid, keys, mempool, result, req, res; - assert.equal(cblock.toRaw().toString('hex'), cmpct[0]); - assert.equal(cblock2.toRaw().toString('hex'), cmpct[0]); + assert.equal(cblock1.toRaw().toString('hex'), cmpct1[0]); + assert.equal(cblock2.toRaw().toString('hex'), cmpct1[0]); for (i = 0; i < block.txs.length; i++) { tx = block.txs[i]; @@ -310,43 +313,43 @@ describe('Block', function() { } }; - assert.equal(cblock.sid(block.txs[1].hash()), 125673511480291); + assert.equal(cblock1.sid(block.txs[1].hash()), 125673511480291); - result = cblock.fillMempool(false, mempool); + result = cblock1.fillMempool(false, mempool); assert(!result); - req = cblock.toRequest(); - assert.equal(req.hash, cblock.hash('hex')); + req = cblock1.toRequest(); + assert.equal(req.hash, cblock1.hash('hex')); assert.deepEqual(req.indexes, [5, 6, 7, 8, 9]); req = bip152.TXRequest.fromRaw(req.toRaw()); - assert.equal(req.hash, cblock.hash('hex')); + assert.equal(req.hash, cblock1.hash('hex')); assert.deepEqual(req.indexes, [5, 6, 7, 8, 9]); res = bip152.TXResponse.fromBlock(block, req); res = bip152.TXResponse.fromRaw(res.toRaw()); - result = cblock.fillMissing(res); + result = cblock1.fillMissing(res); assert(result); - for (i = 0; i < cblock.available.length; i++) - assert(cblock.available[i]); + for (i = 0; i < cblock1.available.length; i++) + assert(cblock1.available[i]); assert.equal( - cblock.toBlock().toRaw().toString('hex'), + cblock1.toBlock().toRaw().toString('hex'), block.toRaw().toString('hex')); cb(); }); it('should handle compact block', function(cb) { - var cblock = bip152.CompactBlock.fromRaw(cmpct2, 'hex'); var block = Block.fromRaw(cmpct2block); - var cblock2 = bip152.CompactBlock.fromBlock(block, false, cblock.keyNonce); + var cblock1 = bip152.CompactBlock.fromRaw(cmpct2, 'hex'); + var cblock2 = bip152.CompactBlock.fromBlock(block, false, cblock1.keyNonce); var map = {}; var i, tx, result, mempool; - assert.equal(cblock.toRaw().toString('hex'), cmpct2); + assert.equal(cblock1.toRaw().toString('hex'), cmpct2); assert.equal(cblock2.toRaw().toString('hex'), cmpct2); for (i = 0; i < block.txs.length; i++) { @@ -363,27 +366,27 @@ describe('Block', function() { } }; - result = cblock.fillMempool(false, mempool); + result = cblock1.fillMempool(false, mempool); assert(result); - for (i = 0; i < cblock.available.length; i++) - assert(cblock.available[i]); + for (i = 0; i < cblock1.available.length; i++) + assert(cblock1.available[i]); assert.equal( - cblock.toBlock().toRaw().toString('hex'), + cblock1.toBlock().toRaw().toString('hex'), block.toRaw().toString('hex')); cb(); }); it('should handle half-full compact block', function(cb) { - var cblock = bip152.CompactBlock.fromRaw(cmpct2, 'hex'); var block = Block.fromRaw(cmpct2block); - var cblock2 = bip152.CompactBlock.fromBlock(block, false, cblock.keyNonce); + var cblock1 = bip152.CompactBlock.fromRaw(cmpct2, 'hex'); + var cblock2 = bip152.CompactBlock.fromBlock(block, false, cblock1.keyNonce); var map = {}; var i, tx, mid, keys, mempool, result, req, res; - assert.equal(cblock.toRaw().toString('hex'), cmpct2); + assert.equal(cblock1.toRaw().toString('hex'), cmpct2); assert.equal(cblock2.toRaw().toString('hex'), cmpct2); for (i = 0; i < block.txs.length; i++) { @@ -403,26 +406,26 @@ describe('Block', function() { } }; - result = cblock.fillMempool(false, mempool); + result = cblock1.fillMempool(false, mempool); assert(!result); - req = cblock.toRequest(); - assert.equal(req.hash, cblock.hash('hex')); + req = cblock1.toRequest(); + assert.equal(req.hash, cblock1.hash('hex')); req = bip152.TXRequest.fromRaw(req.toRaw()); - assert.equal(req.hash, cblock.hash('hex')); + assert.equal(req.hash, cblock1.hash('hex')); res = bip152.TXResponse.fromBlock(block, req); res = bip152.TXResponse.fromRaw(res.toRaw()); - result = cblock.fillMissing(res); + result = cblock1.fillMissing(res); assert(result); - for (i = 0; i < cblock.available.length; i++) - assert(cblock.available[i]); + for (i = 0; i < cblock1.available.length; i++) + assert(cblock1.available[i]); assert.equal( - cblock.toBlock().toRaw().toString('hex'), + cblock1.toBlock().toRaw().toString('hex'), block.toRaw().toString('hex')); cb(); diff --git a/test/bloom-test.js b/test/bloom-test.js index aed3b440..bf626ea2 100644 --- a/test/bloom-test.js +++ b/test/bloom-test.js @@ -1,27 +1,26 @@ 'use strict'; -var bcoin = require('../').set('main'); -var util = bcoin.util; +var assert = require('assert'); +var util = require('../lib/utils/util'); +var constants = require('../lib/protocol/constants'); var crypto = require('../lib/crypto/crypto'); var Bloom = require('../lib/utils/bloom'); var murmur3 = require('../lib/utils/murmur3'); -var constants = bcoin.constants; -var assert = require('assert'); describe('Bloom', function() { - this.timeout(20000); - var filterHex = '' + '000000000000000000000000000000000000000000000000088004000000000000000' + '000000000200000000000000000000000000000000800000000000000000002000000' + '000000000000002000000000000000000000000000000000000000000040000200000' + '0000000001000000800000080000000'; - it('should do proper murmur3', function() { - function mm(str, seed, expect, enc) { - assert.equal(murmur3(new Buffer(str, enc || 'ascii'), seed), expect); - } + function mm(str, seed, expect, enc) { + assert.equal(murmur3(new Buffer(str, enc || 'ascii'), seed), expect); + } + this.timeout(20000); + + it('should do proper murmur3', function() { mm('', 0, 0); mm('', 0xfba4c795, 0x6a396f08); mm('00', 0xfba4c795, 0x2a101837); @@ -74,14 +73,17 @@ describe('Bloom', function() { it('should handle 1m ops with regular filter', function() { var filter = Bloom.fromRate(210000, 0.00001, -1); + var i, j, str; + filter.tweak = 0xdeadbeef; + // ~1m operations - for (var i = 0; i < 1000; i++) { - var str = 'foobar' + i; + for (i = 0; i < 1000; i++) { + str = 'foobar' + i; filter.add(str, 'ascii'); - var j = i; + j = i; do { - var str = 'foobar' + j; + str = 'foobar' + j; assert(filter.test(str, 'ascii') === true); assert(filter.test(str + '-', 'ascii') === false); } while (j--); @@ -90,14 +92,17 @@ describe('Bloom', function() { it('should handle 1m ops with rolling filter', function() { var filter = new Bloom.Rolling(210000, 0.00001); + var i, j, str; + filter.tweak = 0xdeadbeef; + // ~1m operations - for (var i = 0; i < 1000; i++) { - var str = 'foobar' + i; + for (i = 0; i < 1000; i++) { + str = 'foobar' + i; filter.add(str, 'ascii'); - var j = i; + j = i; do { - var str = 'foobar' + j; + str = 'foobar' + j; assert(filter.test(str, 'ascii') === true); assert(filter.test(str + '-', 'ascii') === false); } while (j--); @@ -106,58 +111,66 @@ describe('Bloom', function() { it('should handle rolling generations', function() { var filter = new Bloom.Rolling(50, 0.00001); + var i, j, str; + filter.tweak = 0xdeadbeee; - for (var i = 0; i < 25; i++) { - var str = 'foobar' + i; + + for (i = 0; i < 25; i++) { + str = 'foobar' + i; filter.add(str, 'ascii'); - var j = i; + j = i; do { - var str = 'foobar' + j; + str = 'foobar' + j; assert(filter.test(str, 'ascii') === true); assert(filter.test(str + '-', 'ascii') === false); } while (j--); } - for (var i = 25; i < 50; i++) { - var str = 'foobar' + i; + + for (i = 25; i < 50; i++) { + str = 'foobar' + i; filter.add(str, 'ascii'); - var j = i; + j = i; do { - var str = 'foobar' + j; + str = 'foobar' + j; assert(filter.test(str, 'ascii') === true, str); assert(filter.test(str + '-', 'ascii') === false, str); } while (j--); } - for (var i = 50; i < 75; i++) { - var str = 'foobar' + i; + + for (i = 50; i < 75; i++) { + str = 'foobar' + i; filter.add(str, 'ascii'); - var j = i; + j = i; do { - var str = 'foobar' + j; + str = 'foobar' + j; assert(filter.test(str, 'ascii') === true, str); assert(filter.test(str + '-', 'ascii') === false, str); } while (j--); } - for (var i = 75; i < 100; i++) { - var str = 'foobar' + i; + + for (i = 75; i < 100; i++) { + str = 'foobar' + i; filter.add(str, 'ascii'); - var j = i; + j = i; do { - var str = 'foobar' + j; + str = 'foobar' + j; assert(filter.test(str, 'ascii') === true, str); assert(filter.test(str + '-', 'ascii') === false, str); } while (j-- > 25); assert(filter.test('foobar 24', 'ascii') === false); } - for (var i = 100; i < 125; i++) { - var str = 'foobar' + i; + + for (i = 100; i < 125; i++) { + str = 'foobar' + i; filter.add(str, 'ascii'); - var j = i; + j = i; do { - var str = 'foobar' + j; + str = 'foobar' + j; assert(filter.test(str, 'ascii') === true, str); assert(filter.test(str + '-', 'ascii') === false, str); } while (j-- > 50); } + assert(filter.test('foobar 49', 'ascii') === false); }); }); diff --git a/test/chachapoly-test.js b/test/chachapoly-test.js index e2d237d5..e24b4eac 100644 --- a/test/chachapoly-test.js +++ b/test/chachapoly-test.js @@ -13,19 +13,20 @@ describe('ChaCha20 / Poly1305 / AEAD', function() { var plain = options.plain; var ciphertext = options.ciphertext; var counter = options.counter; + var chacha, plainenc; key = new Buffer(key, 'hex'); nonce = new Buffer(nonce, 'hex'); plain = new Buffer(plain, 'hex'); ciphertext = new Buffer(ciphertext, 'hex'); - var chacha = new ChaCha20(); + chacha = new ChaCha20(); chacha.init(key, nonce, counter); - var plainenc = new Buffer(plain); + plainenc = new Buffer(plain); chacha.encrypt(plainenc); assert.deepEqual(plainenc, ciphertext); - var chacha = new ChaCha20(); + chacha = new ChaCha20(); chacha.init(key, nonce, counter); chacha.encrypt(ciphertext); assert.deepEqual(plain, ciphertext); @@ -39,6 +40,7 @@ describe('ChaCha20 / Poly1305 / AEAD', function() { var pk = options.pk; var ciphertext = options.ciphertext; var tag = options.tag; + var aead, plainenc; plain = new Buffer(plain, 'hex'); aad = new Buffer(aad, 'hex'); @@ -48,17 +50,17 @@ describe('ChaCha20 / Poly1305 / AEAD', function() { ciphertext = new Buffer(ciphertext, 'hex'); tag = new Buffer(tag, 'hex'); - var aead = new AEAD(); + aead = new AEAD(); aead.init(key, nonce); assert.equal(aead.chacha20.getCounter(), 1); assert.deepEqual(aead.polyKey, pk); aead.aad(aad); - var plainenc = new Buffer(plain); + plainenc = new Buffer(plain); aead.encrypt(plainenc); assert.deepEqual(plainenc, ciphertext); assert.deepEqual(aead.finish(), tag); - var aead = new AEAD(); + aead = new AEAD(); aead.init(key, nonce); assert.equal(aead.chacha20.getCounter(), 1); assert.deepEqual(aead.polyKey, pk); @@ -191,8 +193,6 @@ describe('ChaCha20 / Poly1305 / AEAD', function() { it('should create an AEAD and encrypt', function() { testAEAD({ - // 'Ladies and Gentlemen of the class of \'99: If I could' - // + ' offer you only one tip for the future, sunscreen would be it.'; plain: '' + '4c616469657320616e642047656e746c656d656e206f662074686520636c6' + '17373206f66202739393a204966204920636f756c64206f666665722' diff --git a/test/chain-test.js b/test/chain-test.js index bee7280d..5910fc5e 100644 --- a/test/chain-test.js +++ b/test/chain-test.js @@ -1,32 +1,36 @@ 'use strict'; -var BN = require('bn.js'); -var bcoin = require('../').set('regtest'); -var constants = bcoin.constants; -var util = bcoin.util; -var crypto = require('../lib/crypto/crypto'); var assert = require('assert'); -var opcodes = constants.opcodes; +var BN = require('bn.js'); +var constants = require('../lib/protocol/constants'); +var util = require('../lib/utils/util'); var co = require('../lib/utils/co'); -var cob = co.cob; +var crypto = require('../lib/crypto/crypto'); var CoinView = require('../lib/blockchain/coinview'); +var Coin = require('../lib/primitives/coin'); +var Script = require('../lib/script/script'); +var FullNode = require('../lib/node/fullnode'); +var TX = require('../lib/primitives/tx'); +var MTX = require('../lib/primitives/mtx'); // var Client = require('../lib/wallet/client'); +var opcodes = constants.opcodes; +var cob = co.cob; describe('Chain', function() { - var chain, wallet, node, miner, walletdb, mempool; - var tip1, tip2, cb1, cb2, mineBlock; + var node = new FullNode({ db: 'memory', apiKey: 'foo', network: 'regtest' }); + var chain = node.chain; + var mempool = node.mempool; + var walletdb = node.walletdb; + var miner = node.miner; + var wallet, tip1, tip2, cb1, cb2, mineBlock; + + // walletdb.client = new Client({ apiKey: 'foo', network: 'regtest' }); + walletdb.options.resolution = false; + + node.on('error', function() {}); this.timeout(5000); - node = new bcoin.fullnode({ db: 'memory', apiKey: 'foo' }); - // node.walletdb.client = new Client({ apiKey: 'foo', network: 'regtest' }); - chain = node.chain; - mempool = node.mempool; - walletdb = node.walletdb; - walletdb.options.resolution = false; - miner = node.miner; - node.on('error', function() {}); - mineBlock = co(function* mineBlock(tip, tx) { var attempt = yield miner.createBlock(tip); var redeemer; @@ -34,7 +38,7 @@ describe('Chain', function() { if (!tx) return yield attempt.mineAsync(); - redeemer = bcoin.mtx(); + redeemer = new MTX(); redeemer.addOutput({ address: wallet.getReceive(), @@ -225,7 +229,7 @@ describe('Chain', function() { yield chain.add(block); tx = block.txs[1]; - output = bcoin.coin.fromTX(tx, 1, chain.height); + output = Coin.fromTX(tx, 1, chain.height); coin = yield chain.db.getCoin(tx.hash('hex'), 1); @@ -318,11 +322,11 @@ describe('Chain', function() { var attempt = yield miner.createBlock(); var redeemer; - redeemer = bcoin.mtx(); + redeemer = new MTX(); redeemer.addOutput({ script: [ - bcoin.script.array(new BN(1)), + Script.array(new BN(1)), constants.opcodes.OP_CHECKSEQUENCEVERIFY ], value: 10 * 1e8 @@ -348,11 +352,11 @@ describe('Chain', function() { csv = block.txs[1]; - redeemer = bcoin.mtx(); + redeemer = new MTX(); redeemer.addOutput({ script: [ - bcoin.script.array(new BN(2)), + Script.array(new BN(2)), constants.opcodes.OP_CHECKSEQUENCEVERIFY ], value: 10 * 1e8 @@ -374,11 +378,11 @@ describe('Chain', function() { var csv = (yield chain.db.getBlock(chain.height)).txs[1]; var block, attempt, redeemer, err; - redeemer = bcoin.mtx(); + redeemer = new MTX(); redeemer.addOutput({ script: [ - bcoin.script.array(new BN(1)), + Script.array(new BN(1)), constants.opcodes.OP_CHECKSEQUENCEVERIFY ], value: 10 * 1e8 @@ -418,11 +422,11 @@ describe('Chain', function() { csv = block.txs[1]; - redeemer = bcoin.mtx(); + redeemer = new MTX(); redeemer.addOutput({ script: [ - bcoin.script.array(new BN(2)), + Script.array(new BN(2)), constants.opcodes.OP_CHECKSEQUENCEVERIFY ], value: 10 * 1e8 diff --git a/test/hd-test.js b/test/hd-test.js index 5dc6dde4..3c6066b1 100644 --- a/test/hd-test.js +++ b/test/hd-test.js @@ -1,11 +1,10 @@ 'use strict'; -var BN = require('bn.js'); -var bcoin = require('../').set('main'); -var util = bcoin.util; +var assert = require('assert'); +var util = require('../lib/utils/util'); +var HD = require('../lib/hd'); var base58 = require('../lib/utils/base58'); var crypto = require('../lib/crypto/crypto'); -var assert = require('assert'); // https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki var vector1 = { @@ -98,7 +97,7 @@ describe('HD', function() { }); it('should create master private key', function() { - master = bcoin.hd.PrivateKey.fromSeed(new Buffer(seed, 'hex')); + master = HD.PrivateKey.fromSeed(new Buffer(seed, 'hex')); assert.equal(master.toBase58(), master_priv); assert.equal(master.toPublic().toBase58(), master_pub); }); @@ -140,23 +139,23 @@ describe('HD', function() { it('should derive correctly when private key has leading zeros', function() { var key = 'xprv9s21ZrQH143K3ckY9DgU79uMTJkQRLdbCCVDh81SnxTgPzLLGax6uHeBULTtaEtcAvKjXfT7ZWtHzKjTpujMkUd9dDb8msDeAfnJxrgAYhr'; - var hdkey = bcoin.hd.PrivateKey.fromBase58(key); + var hdkey = HD.PrivateKey.fromBase58(key); assert.equal(hdkey.privateKey.toString('hex'), '00000055378cf5fafb56c711c674143f9b0ee82ab0ba2924f19b64f5ae7cdbfd'); var child = hdkey.derivePath('m/44\'/0\'/0\'/0/0\''); assert.equal(child.privateKey.toString('hex'), '3348069561d2a0fb925e74bf198762acc47dce7db27372257d2d959a9e6f8aeb'); }); it('should deserialize master private key', function() { - bcoin.hd.PrivateKey.fromBase58(master.toBase58()); + HD.PrivateKey.fromBase58(master.toBase58()); }); it('should deserialize master public key', function() { - bcoin.hd.PublicKey.fromBase58(master.toPublic().toBase58()); + HD.PublicKey.fromBase58(master.toPublic().toBase58()); }); it('should deserialize and reserialize', function() { - var key = bcoin.hd.fromMnemonic(); - assert.equal(bcoin.hd.fromJSON(key.toJSON()).toBase58(), key.toBase58()); + var key = HD.fromMnemonic(); + assert.equal(HD.fromJSON(key.toJSON()).toBase58(), key.toBase58()); }); function ub58(data) { @@ -175,7 +174,7 @@ describe('HD', function() { delete vector.seed; delete vector.m; it('should create from a seed', function() { - master = bcoin.hd.PrivateKey.fromSeed(new Buffer(seed, 'hex')); + master = HD.PrivateKey.fromSeed(new Buffer(seed, 'hex')); equal(master.toBase58(), m.prv); equal(master.toPublic().toBase58(), m.pub); }); diff --git a/test/http-test.js b/test/http-test.js index fc07135a..117c29ef 100644 --- a/test/http-test.js +++ b/test/http-test.js @@ -1,15 +1,16 @@ 'use strict'; -var BN = require('bn.js'); -var bcoin = require('../').set('regtest'); -var constants = bcoin.constants; -var network = bcoin.networks; -var util = bcoin.util; -var crypto = require('../lib/crypto/crypto'); var assert = require('assert'); -var scriptTypes = constants.scriptTypes; +var constants = require('../lib/protocol/constants'); +var network = require('../lib/protocol/networks'); +var util = require('../lib/utils/util'); +var crypto = require('../lib/crypto/crypto'); var co = require('../lib/utils/co'); var Amount = require('../lib/btc/amount'); +var MTX = require('../lib/primitives/mtx'); +var HTTP = require('../lib/http'); +var FullNode = require('../lib/node/fullnode'); +var scriptTypes = constants.scriptTypes; var cob = co.cob; var dummyInput = { @@ -20,25 +21,25 @@ var dummyInput = { }; describe('HTTP', function() { - var request = bcoin.http.request; - var w, addr, hash; + var request = HTTP.request; + var node, wallet, w, addr, hash; - this.timeout(15000); - - var node = new bcoin.fullnode({ + node = new FullNode({ network: 'regtest', apiKey: 'foo', walletAuth: true, db: 'memory' }); - var wallet = new bcoin.http.Wallet({ + wallet = new HTTP.Wallet({ network: 'regtest', apiKey: 'foo' }); node.on('error', function() {}); + this.timeout(15000); + it('should open node', cob(function* () { constants.tx.COINBASE_MATURITY = 0; yield node.open(); @@ -65,17 +66,17 @@ describe('HTTP', function() { })); it('should fill with funds', cob(function* () { - var balance, receive, details; + var tx, balance, receive, details; // Coinbase - var t1 = bcoin.mtx() + tx = MTX() .addOutput(addr, 50460) .addOutput(addr, 50460) .addOutput(addr, 50460) .addOutput(addr, 50460); - t1.addInput(dummyInput); - t1 = t1.toTX(); + tx.addInput(dummyInput); + tx = tx.toTX(); wallet.once('balance', function(b) { balance = b; @@ -89,7 +90,7 @@ describe('HTTP', function() { details = d; }); - yield node.walletdb.addTX(t1); + yield node.walletdb.addTX(tx); yield co.timeout(300); assert(receive); @@ -100,7 +101,7 @@ describe('HTTP', function() { assert.equal(Amount.value(balance.confirmed), 0); assert.equal(Amount.value(balance.unconfirmed), 201840); assert(details); - assert.equal(details.hash, t1.rhash()); + assert.equal(details.hash, tx.rhash()); })); it('should get balance', cob(function* () { diff --git a/test/mempool-test.js b/test/mempool-test.js index 62205f94..4535f6fc 100644 --- a/test/mempool-test.js +++ b/test/mempool-test.js @@ -1,46 +1,54 @@ 'use strict'; -var BN = require('bn.js'); -var bcoin = require('../').set('main'); -var constants = bcoin.constants; -var util = bcoin.util; -var crypto = require('../lib/crypto/crypto'); var assert = require('assert'); -var opcodes = constants.opcodes; -var cob = require('../lib/utils/co').cob; +var constants = require('../lib/protocol/constants'); +var util = require('../lib/utils/util'); +var crypto = require('../lib/crypto/crypto'); +var co = require('../lib/utils/co'); var MempoolEntry = require('../lib/mempool/mempoolentry'); +var Mempool = require('../lib/mempool/mempool'); +var Chain = require('../lib/blockchain/chain'); +var WalletDB = require('../lib/wallet/walletdb'); +var MTX = require('../lib/primitives/mtx'); +var Coin = require('../lib/primitives/coin'); +var KeyRing = require('../lib/primitives/keyring'); +var Address = require('../lib/primitives/address'); +var Script = require('../lib/script/script'); +var Witness = require('../lib/script/witness'); +var Block = require('../lib/primitives/block'); +var opcodes = constants.opcodes; +var cob = co.cob; describe('Mempool', function() { - var chain, mempool, walletdb; - var wallet, cached; + var chain, mempool, walletdb, wallet, cached; this.timeout(5000); - chain = new bcoin.chain({ + chain = new Chain({ name: 'mp-chain', db: 'memory' }); - mempool = new bcoin.mempool({ + mempool = new Mempool({ chain: chain, name: 'mempool-test', db: 'memory' }); - walletdb = new bcoin.walletdb({ + walletdb = new WalletDB({ name: 'mempool-wallet-test', db: 'memory', verify: true }); function dummy(prev, prevHash) { - var funding = bcoin.mtx(); + var funding = new MTX(); var coin, entry; if (!prevHash) prevHash = constants.ONE_HASH.toString('hex'); - coin = new bcoin.coin({ + coin = new Coin({ version: 1, height: 0, value: 0, @@ -58,7 +66,7 @@ describe('Mempool', function() { mempool.trackEntry(entry, funding.view); - return bcoin.coin.fromTX(funding, 0, -1); + return Coin.fromTX(funding, 0, -1); } it('should open mempool', cob(function* () { @@ -75,24 +83,24 @@ describe('Mempool', function() { })); it('should handle incoming orphans and TXs', cob(function* () { - var kp = bcoin.keyring.generate(); + var kp = KeyRing.generate(); var w = wallet; var t1, t2, t3, t4, f1, fake, prev, sig, balance, txs; - t1 = bcoin.mtx() + t1 = MTX() .addOutput(w.getAddress(), 50000) .addOutput(w.getAddress(), 10000); - prev = new bcoin.script([kp.publicKey, opcodes.OP_CHECKSIG]); + prev = new Script([kp.publicKey, opcodes.OP_CHECKSIG]); t1.addInput(dummy(prev)); sig = t1.signature(0, prev, 70000, kp.privateKey, 'all', 0); - t1.inputs[0].script = new bcoin.script([sig]); + t1.inputs[0].script = new Script([sig]); // balance: 51000 yield w.sign(t1); t1 = t1.toTX(); - t2 = bcoin.mtx() + t2 = MTX() .addInput(t1, 0) // 50000 .addOutput(w.getAddress(), 20000) .addOutput(w.getAddress(), 20000); @@ -101,7 +109,7 @@ describe('Mempool', function() { yield w.sign(t2); t2 = t2.toTX(); - t3 = bcoin.mtx() + t3 = MTX() .addInput(t1, 1) // 10000 .addInput(t2, 0) // 20000 .addOutput(w.getAddress(), 23000); @@ -110,7 +118,7 @@ describe('Mempool', function() { yield w.sign(t3); t3 = t3.toTX(); - t4 = bcoin.mtx() + t4 = MTX() .addInput(t2, 1) // 24000 .addInput(t3, 0) // 23000 .addOutput(w.getAddress(), 11000) @@ -120,15 +128,15 @@ describe('Mempool', function() { yield w.sign(t4); t4 = t4.toTX(); - f1 = bcoin.mtx() + f1 = MTX() .addInput(t4, 1) // 11000 - .addOutput(new bcoin.address(), 9000); + .addOutput(new Address(), 9000); // balance: 11000 yield w.sign(f1); f1 = f1.toTX(); - fake = bcoin.mtx() + fake = MTX() .addInput(t1, 1) // 1000 (already redeemed) .addOutput(w.getAddress(), 6000); // 6000 instead of 500 @@ -175,14 +183,14 @@ describe('Mempool', function() { it('should handle locktime', cob(function* () { var w = wallet; - var kp = bcoin.keyring.generate(); + var kp = KeyRing.generate(); var tx, prev, prevHash, sig; - tx = bcoin.mtx() + tx = MTX() .addOutput(w.getAddress(), 50000) .addOutput(w.getAddress(), 10000); - prev = new bcoin.script([kp.publicKey, opcodes.OP_CHECKSIG]); + prev = new Script([kp.publicKey, opcodes.OP_CHECKSIG]); prevHash = crypto.randomBytes(32).toString('hex'); tx.addInput(dummy(prev, prevHash)); @@ -191,7 +199,7 @@ describe('Mempool', function() { chain.tip.height = 200; sig = tx.signature(0, prev, 70000, kp.privateKey, 'all', 0); - tx.inputs[0].script = new bcoin.script([sig]), + tx.inputs[0].script = new Script([sig]), tx = tx.toTX(); @@ -201,14 +209,14 @@ describe('Mempool', function() { it('should handle invalid locktime', cob(function* () { var w = wallet; - var kp = bcoin.keyring.generate(); + var kp = KeyRing.generate(); var tx, prev, prevHash, sig, err; - tx = bcoin.mtx() + tx = MTX() .addOutput(w.getAddress(), 50000) .addOutput(w.getAddress(), 10000); - prev = new bcoin.script([kp.publicKey, opcodes.OP_CHECKSIG]); + prev = new Script([kp.publicKey, opcodes.OP_CHECKSIG]); prevHash = crypto.randomBytes(32).toString('hex'); tx.addInput(dummy(prev, prevHash)); @@ -216,7 +224,7 @@ describe('Mempool', function() { chain.tip.height = 200 - 1; sig = tx.signature(0, prev, 70000, kp.privateKey, 'all', 0); - tx.inputs[0].script = new bcoin.script([sig]), + tx.inputs[0].script = new Script([sig]), tx = tx.toTX(); try { @@ -232,25 +240,25 @@ describe('Mempool', function() { it('should not cache a malleated wtx with mutated sig', cob(function* () { var w = wallet; - var kp = bcoin.keyring.generate(); + var kp = KeyRing.generate(); var tx, prev, prevHash, prevs, sig, tx, err; kp.witness = true; - tx = bcoin.mtx() + tx = MTX() .addOutput(w.getAddress(), 50000) .addOutput(w.getAddress(), 10000); - prev = new bcoin.script([0, kp.getKeyHash()]); + prev = new Script([0, kp.getKeyHash()]); prevHash = crypto.randomBytes(32).toString('hex'); tx.addInput(dummy(prev, prevHash)); - prevs = bcoin.script.fromPubkeyhash(kp.getKeyHash()); + prevs = Script.fromPubkeyhash(kp.getKeyHash()); sig = tx.signature(0, prevs, 70000, kp.privateKey, 'all', 1); sig[sig.length - 1] = 0; - tx.inputs[0].witness = new bcoin.witness([sig, kp.publicKey]); + tx.inputs[0].witness = new Witness([sig, kp.publicKey]); tx = tx.toTX(); try { @@ -265,20 +273,20 @@ describe('Mempool', function() { it('should not cache a malleated tx with unnecessary witness', cob(function* () { var w = wallet; - var kp = bcoin.keyring.generate(); + var kp = KeyRing.generate(); var tx, prev, prevHash, sig, tx, err; - tx = bcoin.mtx() + tx = MTX() .addOutput(w.getAddress(), 50000) .addOutput(w.getAddress(), 10000); - prev = new bcoin.script([kp.publicKey, opcodes.OP_CHECKSIG]); + prev = new Script([kp.publicKey, opcodes.OP_CHECKSIG]); prevHash = crypto.randomBytes(32).toString('hex'); tx.addInput(dummy(prev, prevHash)); sig = tx.signature(0, prev, 70000, kp.privateKey, 'all', 0); - tx.inputs[0].script = new bcoin.script([sig]); + tx.inputs[0].script = new Script([sig]); tx.inputs[0].witness.push(new Buffer(0)); tx = tx.toTX(); @@ -294,16 +302,16 @@ describe('Mempool', function() { it('should not cache a malleated wtx with wit removed', cob(function* () { var w = wallet; - var kp = bcoin.keyring.generate(); + var kp = KeyRing.generate(); var tx, prev, prevHash, tx, err; kp.witness = true; - tx = bcoin.mtx() + tx = MTX() .addOutput(w.getAddress(), 50000) .addOutput(w.getAddress(), 10000); - prev = new bcoin.script([0, kp.getKeyHash()]); + prev = new Script([0, kp.getKeyHash()]); prevHash = crypto.randomBytes(32).toString('hex'); tx.addInput(dummy(prev, prevHash)); @@ -323,14 +331,14 @@ describe('Mempool', function() { it('should cache non-malleated tx without sig', cob(function* () { var w = wallet; - var kp = bcoin.keyring.generate(); + var kp = KeyRing.generate(); var tx, prev, prevHash, tx, err; - tx = bcoin.mtx() + tx = MTX() .addOutput(w.getAddress(), 50000) .addOutput(w.getAddress(), 10000); - prev = new bcoin.script([kp.publicKey, opcodes.OP_CHECKSIG]); + prev = new Script([kp.publicKey, opcodes.OP_CHECKSIG]); prevHash = crypto.randomBytes(32).toString('hex'); tx.addInput(dummy(prev, prevHash)); @@ -353,7 +361,7 @@ describe('Mempool', function() { var w = wallet; var tx, input, tx, block; - tx = bcoin.mtx() + tx = MTX() .addOutput(w.getAddress(), 50000); input = { @@ -367,7 +375,7 @@ describe('Mempool', function() { tx = tx.toTX(); - block = new bcoin.block(); + block = new Block(); block.txs.push(tx); assert(mempool.hasReject(cached.hash())); diff --git a/test/mnemonic-test.js b/test/mnemonic-test.js index c585b1d7..4a63b43c 100644 --- a/test/mnemonic-test.js +++ b/test/mnemonic-test.js @@ -1,10 +1,10 @@ 'use strict'; -var BN = require('bn.js'); -var bcoin = require('../').set('main'); -var util = bcoin.util; -var crypto = require('../lib/crypto/crypto'); var assert = require('assert'); +var util = require('../lib/utils/util'); +var crypto = require('../lib/crypto/crypto'); +var HD = require('../lib/hd'); + var mnemonic1 = require('./data/mnemonic1').english; var mnemonic2 = require('./data/mnemonic2'); @@ -15,14 +15,18 @@ describe('Mnemonic', function() { var seed = new Buffer(data[2], 'hex'); var xpriv = data[3]; it('should create an english mnemonic (' + i + ')', function() { - var mnemonic = new bcoin.hd.Mnemonic({ + var mnemonic, key; + + mnemonic = new HD.Mnemonic({ language: 'english', entropy: entropy, passphrase: 'TREZOR' }); + assert.equal(mnemonic.getPhrase(), phrase); assert.equal(mnemonic.toSeed().toString('hex'), seed.toString('hex')); - var key = bcoin.hd.fromMnemonic(mnemonic); + + key = HD.fromMnemonic(mnemonic); assert.equal(key.toBase58(), xpriv); }); }); @@ -34,21 +38,25 @@ describe('Mnemonic', function() { var passphrase = data.passphrase; var xpriv = data.bip32_xprv; it('should create a japanese mnemonic (' + i + ')', function() { - var mnemonic = new bcoin.hd.Mnemonic({ + var mnemonic, key; + + mnemonic = new HD.Mnemonic({ language: 'japanese', entropy: entropy, passphrase: passphrase }); + assert.equal(mnemonic.getPhrase(), phrase); assert.equal(mnemonic.toSeed().toString('hex'), seed.toString('hex')); - var key = bcoin.hd.fromMnemonic(mnemonic); + + key = HD.fromMnemonic(mnemonic); assert.equal(key.toBase58(), xpriv); }); }); it('should verify phrase', function() { - var m1 = new bcoin.hd.Mnemonic(); - var m2 = bcoin.hd.Mnemonic.fromPhrase(m1.getPhrase()); + var m1 = new HD.Mnemonic(); + var m2 = HD.Mnemonic.fromPhrase(m1.getPhrase()); assert.deepEqual(m2.getEntropy(), m1.getEntropy()); assert.equal(m2.bits, m1.bits); assert.equal(m2.language, m1.language); diff --git a/test/protocol-test.js b/test/protocol-test.js index 1db1fe2a..29dea34a 100644 --- a/test/protocol-test.js +++ b/test/protocol-test.js @@ -1,24 +1,26 @@ 'use strict'; -var bcoin = require('../').set('main'); -var assert = require('assert'); -var constants = bcoin.constants; -var network = bcoin.network.get(); -var util = bcoin.util; -var crypto = require('../lib/crypto/crypto'); var fs = require('fs'); -var alertData = fs.readFileSync(__dirname + '/data/alertTests.raw'); +var assert = require('assert'); +var constants = require('../lib/protocol/constants'); +var Network = require('../lib/protocol/network'); +var util = require('../lib/utils/util'); +var BufferReader = require('../lib/utils/reader'); +var crypto = require('../lib/crypto/crypto'); var NetworkAddress = require('../lib/primitives/netaddress'); +var TX = require('../lib/primitives/tx'); var Framer = require('../lib/net/framer'); var Parser = require('../lib/net/parser'); var packets = require('../lib/net/packets'); +var network = Network.get('main'); + +var alertData = fs.readFileSync(__dirname + '/data/alertTests.raw'); describe('Protocol', function() { var version = require('../package.json').version; var agent = '/bcoin:' + version + '/'; + var parser, framer, v1, v2, hosts; - var parser; - var framer; beforeEach(function() { parser = new Parser(); framer = new Framer(); @@ -36,10 +38,10 @@ describe('Protocol', function() { }); } - var v1 = packets.VersionPacket.fromOptions({ + v1 = packets.VersionPacket.fromOptions({ version: constants.VERSION, services: constants.LOCAL_SERVICES, - ts: bcoin.now(), + ts: network.now(), remote: new NetworkAddress(), local: new NetworkAddress(), nonce: util.nonce(), @@ -55,10 +57,10 @@ describe('Protocol', function() { assert.equal(payload.relay, false); }); - var v2 = packets.VersionPacket.fromOptions({ + v2 = packets.VersionPacket.fromOptions({ version: constants.VERSION, services: constants.LOCAL_SERVICES, - ts: bcoin.now(), + ts: network.now(), remote: new NetworkAddress(), local: new NetworkAddress(), nonce: util.nonce(), @@ -77,7 +79,7 @@ describe('Protocol', function() { packetTest('verack', new packets.VerackPacket(), function(payload) { }); - var hosts = [ + hosts = [ new NetworkAddress({ services: constants.LOCAL_SERVICES, host: '127.0.0.1', @@ -109,7 +111,9 @@ describe('Protocol', function() { it('should include the raw data of only one transaction in a ' + 'parsed transaction', function() { - var rawTwoTxs = new Buffer( + var tx, rawTwoTxs, rawFirstTx; + + rawTwoTxs = new Buffer( '0100000004b124cca7e9686375380c845d0fd002ed704aef4472f4cc193' + 'fca4aa1b3404da400000000b400493046022100d3c9ba786488323c975f' + 'e61593df6a8041c5442736f361887abfe5c97175c72b022100ca61688f4' + @@ -160,7 +164,8 @@ describe('Protocol', function() { '0001976a9146167aeaeec59836b22447b8af2c5e61fb4f1b7b088ac00a3' + 'dc5c0500000017a9149eb21980dc9d413d8eac27314938b9da920ee53e8' + '700000000', 'hex'); - var rawFirstTx = new Buffer( + + rawFirstTx = new Buffer( '0100000004b124cca7e9686375380c845d0fd002ed704aef4472f4cc193' + 'fca4aa1b3404da400000000b400493046022100d3c9ba786488323c975f' + 'e61593df6a8041c5442736f361887abfe5c97175c72b022100ca61688f4' + @@ -201,19 +206,24 @@ describe('Protocol', function() { 'c9954c44b0ce168bc78efd5f1e1c7db9d6c21b3016599ffffffff01a029' + 'de5c0500000017a9141d9ca71efa36d814424ea6ca1437e67287aebe348' + '700000000', 'hex'); - var tx = bcoin.tx.fromRaw(rawTwoTxs); + + tx = TX.fromRaw(rawTwoTxs); tx._raw = null; + assert.deepEqual(tx.toRaw(), rawFirstTx); }); it('should parse, reserialize, and verify alert packets', function() { - var br = new bcoin.reader(alertData); + var br = new BufferReader(alertData); + var alert, data; + while (br.left()) { - var alert = packets.AlertPacket.fromReader(br); + alert = packets.AlertPacket.fromReader(br); assert(alert.verify(network.alertKey)); alert._payload = null; alert._hash = null; - var data = alert.toRaw(); + + data = alert.toRaw(); alert = packets.AlertPacket.fromRaw(data); assert(alert.verify(network.alertKey)); } diff --git a/test/script-test.js b/test/script-test.js index 8b3d8b16..a91c3a92 100644 --- a/test/script-test.js +++ b/test/script-test.js @@ -164,57 +164,57 @@ describe('Script', function() { }); it('should handle CScriptNums correctly', function() { - var s1, s2, stack; + var input, output, stack; - s1 = new Script([ - new Buffer([0xff, 0xff, 0xff, 0x7f]), + input = new Script([ + new Buffer('ffffff7f', 'hex'), opcodes.OP_NEGATE, opcodes.OP_DUP, opcodes.OP_ADD ]); - s2 = new Script([ - new Buffer([0xfe, 0xff, 0xff, 0xff, 0x80]), + output = new Script([ + new Buffer('feffffff80', 'hex'), opcodes.OP_EQUAL ]); stack = new Stack(); - assert(s1.execute(stack)); - assert(success(s2.execute(stack), stack)); + assert(input.execute(stack)); + assert(success(output.execute(stack), stack)); }); it('should handle CScriptNums correctly', function() { - var s1, s2, stack; + var input, output, stack; - s1 = new Script([ + input = new Script([ opcodes.OP_11, opcodes.OP_10, opcodes.OP_1, opcodes.OP_ADD ]); - s2 = new Script([ + output = new Script([ opcodes.OP_NUMNOTEQUAL, opcodes.OP_NOT ]); stack = new Stack(); - assert(s1.execute(stack)); - assert(success(s2.execute(stack), stack)); + assert(input.execute(stack)); + assert(success(output.execute(stack), stack)); }); it('should handle OP_ROLL correctly', function() { - var s1, s2, stack; + var input, output, stack; - s1 = new Script([ + input = new Script([ new Buffer([0x16]), new Buffer([0x15]), new Buffer([0x14]) ]); - s2 = new Script([ + output = new Script([ opcodes.OP_0, opcodes.OP_ROLL, new Buffer([0x14]), @@ -226,8 +226,8 @@ describe('Script', function() { stack = new Stack(); - assert(s1.execute(stack)); - assert(success(s2.execute(stack), stack)); + assert(input.execute(stack)); + assert(success(output.execute(stack), stack)); }); scripts.forEach(function(data) { @@ -264,8 +264,8 @@ describe('Script', function() { flags = flag; - [false, true].forEach(function(nocache) { - var suffix = nocache ? ' without cache' : ' with cache'; + [false, true].forEach(function(noCache) { + var suffix = noCache ? ' without cache' : ' with cache'; it('should handle script test' + suffix + ': ' + comments, function() { var prev, tx, err, res; @@ -278,7 +278,7 @@ describe('Script', function() { hash: constants.NULL_HASH, index: 0xffffffff }, - script: [Script.array(0), Script.array(0)], + script: new Script([opcodes.OP_0, opcodes.OP_0]), witness: new Witness(), sequence: 0xffffffff }], @@ -309,7 +309,7 @@ describe('Script', function() { locktime: 0 }); - if (nocache) { + if (noCache) { tx._raw = null; tx._size = -1; tx._witnessSize = -1; diff --git a/test/tx-test.js b/test/tx-test.js index 581ca2f4..7b003be5 100644 --- a/test/tx-test.js +++ b/test/tx-test.js @@ -41,8 +41,8 @@ function parseTX(file) { return { tx: tx, view: view }; } -function clearCache(tx, nocache) { - if (!nocache) { +function clearCache(tx, noCache) { + if (!noCache) { assert.equal(tx.hash('hex'), tx.clone().hash('hex')); return; } @@ -77,24 +77,27 @@ function parseTest(data) { flags = flag; coins.forEach(function(data) { - var hash = data[0]; + var hash = util.revHex(data[0]); var index = data[1]; var script = Script.fromString(data[2]); - var value = data[3]; + var amount = data[3] != null ? data[3] : '0'; + var value = parseInt(amount, 10); var coin; + if (index === -1) + return; + coin = new Coin({ version: 1, height: -1, coinbase: false, - hash: util.revHex(hash), + hash: hash, index: index, script: script, - value: value != null ? parseInt(value, 10) : 0 + value: value }); - if (index !== -1) - view.addCoin(coin); + view.addCoin(coin); }); coin = view.getOutput(tx.inputs[0]); @@ -148,12 +151,12 @@ describe('TX', function() { '0f7c00000000001976a91495ad422bb5911c2c9fe6ce4f82a13c85f03d9b' + '2e88ac00000000'; - [false, true].forEach(function(nocache) { - var suffix = nocache ? ' without cache' : ' with cache'; + [false, true].forEach(function(noCache) { + var suffix = noCache ? ' without cache' : ' with cache'; it('should decode/encode with parser/framer' + suffix, function() { var tx = TX.fromRaw(raw, 'hex'); - clearCache(tx, nocache); + clearCache(tx, noCache); assert.equal(tx.toRaw().toString('hex'), raw); }); @@ -163,43 +166,44 @@ describe('TX', function() { var view = new CoinView(); view.addTX(p, -1); - clearCache(tx, nocache); - clearCache(p, nocache); + clearCache(tx, noCache); + clearCache(p, noCache); assert(tx.verify(view)); + util.log(tx.format(view)); }); it('should verify non-minimal output' + suffix, function() { - clearCache(tx1.tx, nocache); + clearCache(tx1.tx, noCache); assert(tx1.tx.verify(tx1.view, constants.flags.VERIFY_P2SH)); }); it('should verify tx.version == 0' + suffix, function() { - clearCache(tx2.tx, nocache); + clearCache(tx2.tx, noCache); assert(tx2.tx.verify(tx2.view, constants.flags.VERIFY_P2SH)); }); it('should verify sighash_single bug w/ findanddelete' + suffix, function() { - clearCache(tx3.tx, nocache); + clearCache(tx3.tx, noCache); assert(tx3.tx.verify(tx3.view, constants.flags.VERIFY_P2SH)); }); it('should verify high S value with only DERSIG enabled' + suffix, function() { var coin = tx4.view.getOutput(tx4.tx.inputs[0]); var flags = constants.flags.VERIFY_P2SH | constants.flags.VERIFY_DERSIG; - clearCache(tx4.tx, nocache); + clearCache(tx4.tx, noCache); assert(tx4.tx.verifyInput(0, coin, flags)); }); it('should verify the coolest tx ever sent' + suffix, function() { - clearCache(coolest.tx, nocache); + clearCache(coolest.tx, noCache); assert(coolest.tx.verify(coolest.view, constants.flags.VERIFY_NONE)); }); it('should parse witness tx properly' + suffix, function() { var raw1, raw2, wtx2; - clearCache(wtx.tx, nocache); + clearCache(wtx.tx, noCache); assert.equal(wtx.tx.inputs.length, 5); assert.equal(wtx.tx.outputs.length, 1980); @@ -218,7 +222,7 @@ describe('TX', function() { assert.deepEqual(raw1, raw2); wtx2 = TX.fromRaw(raw2); - clearCache(wtx2, nocache); + clearCache(wtx2, noCache); assert.equal(wtx.tx.hash('hex'), wtx2.hash('hex')); assert.equal(wtx.tx.witnessHash('hex'), wtx2.witnessHash('hex')); @@ -257,19 +261,19 @@ describe('TX', function() { if (valid) { if (comments.indexOf('Coinbase') === 0) { it('should handle valid coinbase' + suffix + ': ' + comments, function() { - clearCache(tx, nocache); + clearCache(tx, noCache); assert.ok(tx.isSane()); }); return; } it('should handle valid tx test' + suffix + ': ' + comments, function() { - clearCache(tx, nocache); + clearCache(tx, noCache); assert.ok(tx.verify(view, flags)); }); } else { if (comments === 'Duplicate inputs') { it('should handle duplicate input test' + suffix + ': ' + comments, function() { - clearCache(tx, nocache); + clearCache(tx, noCache); assert.ok(tx.verify(view, flags)); assert.ok(!tx.isSane()); }); @@ -277,7 +281,7 @@ describe('TX', function() { } if (comments === 'Negative output') { it('should handle invalid tx (negative)' + suffix + ': ' + comments, function() { - clearCache(tx, nocache); + clearCache(tx, noCache); assert.ok(tx.verify(view, flags)); assert.ok(!tx.isSane()); }); @@ -285,13 +289,13 @@ describe('TX', function() { } if (comments.indexOf('Coinbase') === 0) { it('should handle invalid coinbase' + suffix + ': ' + comments, function() { - clearCache(tx, nocache); + clearCache(tx, noCache); assert.ok(!tx.isSane()); }); return; } it('should handle invalid tx test' + suffix + ': ' + comments, function() { - clearCache(tx, nocache); + clearCache(tx, noCache); assert.ok(!tx.verify(view, flags)); }); } @@ -299,13 +303,14 @@ describe('TX', function() { }); sighash.forEach(function(data) { - var tx, script, index, type, expected, hexType; + var name, tx, script, index; + var type, expected, hexType; if (data.length === 1) return; tx = TX.fromRaw(data[0], 'hex'); - clearCache(tx, nocache); + clearCache(tx, noCache); script = Script.fromRaw(data[1], 'hex'); @@ -322,10 +327,13 @@ describe('TX', function() { if (hexType.length % 2 !== 0) hexType = '0' + hexType; - it('should get signature hash of ' + data[4] + ' (' + hexType + ')' + suffix, function() { + name = 'should get signature hash of ' + + data[4] + ' (' + hexType + ')' + suffix; + + it(name, function() { var subscript = script.getSubscript(0).removeSeparators(); - var hash = tx.signatureHash(index, subscript, 0, type, 0).toString('hex'); - assert.equal(hash, expected); + var hash = tx.signatureHash(index, subscript, 0, type, 0); + assert.equal(hash.toString('hex'), expected); }); }); }); diff --git a/test/utils-test.js b/test/utils-test.js index 5d63b510..a6ac82a5 100644 --- a/test/utils-test.js +++ b/test/utils-test.js @@ -12,7 +12,9 @@ var schnorr = require('../lib/crypto/schnorr'); var Amount = require('../lib/btc/amount'); describe('Utils', function() { - var vectors = [ + var vectors, signed, unsigned; + + vectors = [ ['', ''], ['61', '2g'], ['626262', 'a3gV'], @@ -30,22 +32,28 @@ describe('Utils', function() { it('should encode/decode base58', function() { var arr = new Buffer([ 0, 0, 0, 0xde, 0xad, 0xbe, 0xef ]); var b = base58.encode(arr); + var i, r, b; + assert.equal(b, '1116h8cQN'); assert.deepEqual(base58.decode(b), arr); - for (var i = 0; i < vectors.length; i++) { - var r = new Buffer(vectors[i][0], 'hex'); - var b = vectors[i][1]; + + for (i = 0; i < vectors.length; i++) { + r = new Buffer(vectors[i][0], 'hex'); + b = vectors[i][1]; assert.equal(base58.encode(r), b); assert.deepEqual(base58.decode(b), r); } }); it('should verify proof-of-work', function() { - var hash = new Buffer( + var bits = 0x1900896c; + var hash; + + hash = new Buffer( '672b3f1bb11a994267ea4171069ba0aa4448a840f38e8f340000000000000000', 'hex' ); - var bits = 0x1900896c; + assert(btcutils.verifyPOW(hash, bits)); }); @@ -90,6 +98,8 @@ describe('Utils', function() { }); it('should write/read new varints', function() { + var n, b; + /* * 0: [0x00] 256: [0x81 0x00] * 1: [0x01] 16383: [0xFE 0x7F] @@ -99,71 +109,71 @@ describe('Utils', function() { * 2^32: [0x8E 0xFE 0xFE 0xFF 0x00] */ - var n = 0; - var b = new Buffer(1); + n = 0; + b = new Buffer(1); b.fill(0x00); encoding.writeVarint2(b, 0, 0); assert.equal(encoding.readVarint2(b, 0).value, 0); assert.deepEqual(b, [0]); - var b = new Buffer(1); + b = new Buffer(1); b.fill(0x00); encoding.writeVarint2(b, 1, 0); assert.equal(encoding.readVarint2(b, 0).value, 1); assert.deepEqual(b, [1]); - var b = new Buffer(1); + b = new Buffer(1); b.fill(0x00); encoding.writeVarint2(b, 127, 0); assert.equal(encoding.readVarint2(b, 0).value, 127); assert.deepEqual(b, [0x7f]); - var b = new Buffer(2); + b = new Buffer(2); b.fill(0x00); encoding.writeVarint2(b, 128, 0); assert.equal(encoding.readVarint2(b, 0).value, 128); assert.deepEqual(b, [0x80, 0x00]); - var b = new Buffer(2); + b = new Buffer(2); b.fill(0x00); encoding.writeVarint2(b, 255, 0); assert.equal(encoding.readVarint2(b, 0).value, 255); assert.deepEqual(b, [0x80, 0x7f]); - var b = new Buffer(2); + b = new Buffer(2); b.fill(0x00); encoding.writeVarint2(b, 16383, 0); assert.equal(encoding.readVarint2(b, 0).value, 16383); assert.deepEqual(b, [0xfe, 0x7f]); - var b = new Buffer(2); + b = new Buffer(2); b.fill(0x00); encoding.writeVarint2(b, 16384, 0); assert.equal(encoding.readVarint2(b, 0).value, 16384); assert.deepEqual(b, [0xff, 0x00]); - var b = new Buffer(3); + b = new Buffer(3); b.fill(0x00); encoding.writeVarint2(b, 16511, 0); assert.equal(encoding.readVarint2(b, 0).value, 16511); // assert.deepEqual(b, [0x80, 0xff, 0x7f]); assert.deepEqual(b, [0xff, 0x7f, 0x00]); - var b = new Buffer(3); + b = new Buffer(3); b.fill(0x00); encoding.writeVarint2(b, 65535, 0); assert.equal(encoding.readVarint2(b, 0).value, 65535); // assert.deepEqual(b, [0x82, 0xfd, 0x7f]); assert.deepEqual(b, [0x82, 0xfe, 0x7f]); - var b = new Buffer(5); + b = new Buffer(5); b.fill(0x00); encoding.writeVarint2(b, Math.pow(2, 32), 0); assert.equal(encoding.readVarint2(b, 0).value, Math.pow(2, 32)); assert.deepEqual(b, [0x8e, 0xfe, 0xfe, 0xff, 0x00]); }); - var unsigned = [ + unsigned = [ new BN('ffeeffee'), new BN('001fffeeffeeffee'), new BN('eeffeeff'), @@ -172,7 +182,7 @@ describe('Utils', function() { new BN(1) ]; - var signed = [ + signed = [ new BN('ffeeffee'), new BN('001fffeeffeeffee'), new BN('eeffeeff'), @@ -191,12 +201,16 @@ describe('Utils', function() { var buf1 = new Buffer(8); var buf2 = new Buffer(8); var msg = 'should write+read a ' + num.bitLength() + ' bit unsigned int'; + it(msg, function() { + var n1, n2; + encoding.writeU64BN(buf1, num, 0); encoding.writeU64(buf2, num.toNumber(), 0); assert.deepEqual(buf1, buf2); - var n1 = encoding.readU64BN(buf1, 0); - var n2 = encoding.readU64(buf2, 0); + + n1 = encoding.readU64BN(buf1, 0); + n2 = encoding.readU64(buf2, 0); assert.equal(n1.toNumber(), n2); }); }); @@ -206,27 +220,36 @@ describe('Utils', function() { var buf2 = new Buffer(8); var msg = 'should write+read a ' + num.bitLength() + ' bit ' + (num.isNeg() ? 'negative' : 'positive') + ' int'; + it(msg, function() { + var n1, n2; + encoding.write64BN(buf1, num, 0); encoding.write64(buf2, num.toNumber(), 0); assert.deepEqual(buf1, buf2); - var n1 = encoding.read64BN(buf1, 0); - var n2 = encoding.read64(buf2, 0); + + n1 = encoding.read64BN(buf1, 0); + n2 = encoding.read64(buf2, 0); assert.equal(n1.toNumber(), n2); }); - var msg = 'should write+read a ' + num.bitLength() + + msg = 'should write+read a ' + num.bitLength() + ' bit ' + (num.isNeg() ? 'negative' : 'positive') + ' int as unsigned'; + it(msg, function() { + var n1, n2; + encoding.writeU64BN(buf1, num, 0); encoding.writeU64(buf2, num.toNumber(), 0); assert.deepEqual(buf1, buf2); - var n1 = encoding.readU64BN(buf1, 0); + + n1 = encoding.readU64BN(buf1, 0); if (num.isNeg()) { assert.throws(function() { encoding.readU64(buf2, 0); }); } else { - var n2 = encoding.readU64(buf2, 0); + n2 = encoding.readU64(buf2, 0); assert.equal(n1.toNumber(), n2); } }); @@ -239,46 +262,48 @@ describe('Utils', function() { var salt = '000102030405060708090a0b0c'; var info = 'f0f1f2f3f4f5f6f7f8f9'; var len = 42; + var prkE, okmE, prk, okm, hash; - var prkE = '077709362c2e32df0ddc3f0dc47bba6390b6c73bb50f9c3122ec844ad7c2b3e5'; - var okmE = '3cb25f25faacd57a90434f64d0362f2a2d2d0a90cf1a5a4c5db02d56ecc4c5bf34007208d5b887185865'; + prkE = '077709362c2e32df0ddc3f0dc47bba6390b6c73bb50f9c3122ec844ad7c2b3e5'; + okmE = '3cb25f25faacd57a90434f64d0362f2a2d2d0a90cf1' + + 'a5a4c5db02d56ecc4c5bf34007208d5b887185865'; ikm = new Buffer(ikm, 'hex'); salt = new Buffer(salt, 'hex'); info = new Buffer(info, 'hex'); - var prk = crypto.hkdfExtract(ikm, salt, 'sha256'); - var okm = crypto.hkdfExpand(prk, info, len, 'sha256'); + prk = crypto.hkdfExtract(ikm, salt, 'sha256'); + okm = crypto.hkdfExpand(prk, info, len, 'sha256'); assert.equal(prk.toString('hex'), prkE); assert.equal(okm.toString('hex'), okmE); - var hash = 'sha256'; + hash = 'sha256'; - var ikm = '000102030405060708090a0b0c0d0e0f' + ikm = '000102030405060708090a0b0c0d0e0f' + '101112131415161718191a1b1c1d1e1f' + '202122232425262728292a2b2c2d2e2f' + '303132333435363738393a3b3c3d3e3f' + '404142434445464748494a4b4c4d4e4f'; - var salt = '606162636465666768696a6b6c6d6e6f' + salt = '606162636465666768696a6b6c6d6e6f' + '707172737475767778797a7b7c7d7e7f' + '808182838485868788898a8b8c8d8e8f' + '909192939495969798999a9b9c9d9e9f' + 'a0a1a2a3a4a5a6a7a8a9aaabacadaeaf'; - var info = 'b0b1b2b3b4b5b6b7b8b9babbbcbdbebf' + info = 'b0b1b2b3b4b5b6b7b8b9babbbcbdbebf' + 'c0c1c2c3c4c5c6c7c8c9cacbcccdcecf' + 'd0d1d2d3d4d5d6d7d8d9dadbdcdddedf' + 'e0e1e2e3e4e5e6e7e8e9eaebecedeeef' + 'f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff'; - var len = 82; + len = 82; - var prkE = '06a6b88c5853361a06104c9ceb35b45c' + prkE = '06a6b88c5853361a06104c9ceb35b45c' + 'ef760014904671014a193f40c15fc244'; - var okmE = 'b11e398dc80327a1c8e7f78c596a4934' + okmE = 'b11e398dc80327a1c8e7f78c596a4934' + '4f012eda2d4efad8a050cc4c19afa97c' + '59045a99cac7827271cb41c65e590e09' + 'da3275600c2f09b8367793a9aca3db71' @@ -289,8 +314,8 @@ describe('Utils', function() { salt = new Buffer(salt, 'hex'); info = new Buffer(info, 'hex'); - var prk = crypto.hkdfExtract(ikm, salt, 'sha256'); - var okm = crypto.hkdfExpand(prk, info, len, 'sha256'); + prk = crypto.hkdfExtract(ikm, salt, 'sha256'); + okm = crypto.hkdfExpand(prk, info, len, 'sha256'); assert.equal(prk.toString('hex'), prkE); assert.equal(okm.toString('hex'), okmE); diff --git a/test/wallet-test.js b/test/wallet-test.js index 91521d8d..9a539baf 100644 --- a/test/wallet-test.js +++ b/test/wallet-test.js @@ -1,15 +1,20 @@ 'use strict'; -var BN = require('bn.js'); -var bcoin = require('../').set('main'); -var constants = bcoin.constants; -var network = bcoin.networks; -var util = bcoin.util; +var assert = require('assert'); +var constants = require('../lib/protocol/constants'); +var util = require('../lib/utils/util'); var encoding = require('../lib/utils/encoding'); var crypto = require('../lib/crypto/crypto'); -var assert = require('assert'); -var scriptTypes = constants.scriptTypes; var co = require('../lib/utils/co'); +var WalletDB = require('../lib/wallet/walletdb'); +var Address = require('../lib/primitives/address'); +var MTX = require('../lib/primitives/mtx'); +var Coin = require('../lib/primitives/coin'); +var KeyRing = require('../lib/primitives/keyring'); +var Address = require('../lib/primitives/address'); +var Script = require('../lib/script/script'); +var HD = require('../lib/hd'); +var scriptTypes = constants.scriptTypes; var cob = co.cob; var KEY1 = 'xprv9s21ZrQH143K3Aj6xQBymM31Zb4BVc7wxqfUhMZrzewdDVCt' @@ -54,9 +59,11 @@ function dummy(hash) { } describe('Wallet', function() { - var walletdb, wallet, ewallet, ekey, doubleSpendWallet, doubleSpend; + var walletdb, wallet, ewallet, ekey; + var doubleSpendWallet, doubleSpend; + var p2pkh, multisig; - walletdb = new bcoin.walletdb({ + walletdb = new WalletDB({ name: 'wallet-test', db: 'memory', resolution: true, @@ -74,15 +81,15 @@ describe('Wallet', function() { var w = yield walletdb.create(); var addr = w.getAddress('base58'); assert(addr); - assert(bcoin.address.validate(addr)); + assert(Address.validate(addr)); })); it('should validate existing address', function() { - assert(bcoin.address.validate('1KQ1wMNwXHUYj1nV2xzsRcKUH8gVFpTFUc')); + assert(Address.validate('1KQ1wMNwXHUYj1nV2xzsRcKUH8gVFpTFUc')); }); it('should fail to validate invalid address', function() { - assert(!bcoin.address.validate('1KQ1wMNwXHUYj1nv2xzsRcKUH8gVFpTFUc')); + assert(!Address.validate('1KQ1wMNwXHUYj1nv2xzsRcKUH8gVFpTFUc')); }); it('should create and get wallet', cob(function* () { @@ -96,26 +103,28 @@ describe('Wallet', function() { assert(w1 !== w2); assert(w1.master !== w2.master); assert.equal(w1.master.key.toBase58(), w2.master.key.toBase58()); - assert.equal(w1.account.accountKey.toBase58(), w2.account.accountKey.toBase58()); + assert.equal( + w1.account.accountKey.toBase58(), + w2.account.accountKey.toBase58()); })); - var p2pkh = co(function* p2pkh(witness, bullshitNesting) { - var flags = bcoin.constants.flags.STANDARD_VERIFY_FLAGS; + p2pkh = co(function* p2pkh(witness, bullshitNesting) { + var flags = constants.flags.STANDARD_VERIFY_FLAGS; var w, addr, src, tx; if (witness) - flags |= bcoin.constants.flags.VERIFY_WITNESS; + flags |= constants.flags.VERIFY_WITNESS; w = yield walletdb.create({ witness: witness }); - addr = bcoin.address.fromBase58(w.getAddress('base58')); + addr = Address.fromBase58(w.getAddress('base58')); if (witness) assert.equal(addr.type, scriptTypes.WITNESSPUBKEYHASH); else assert.equal(addr.type, scriptTypes.PUBKEYHASH); - src = bcoin.mtx({ + src = MTX({ outputs: [{ value: 5460 * 2, address: bullshitNesting @@ -123,13 +132,13 @@ describe('Wallet', function() { : w.getAddress() }, { value: 5460 * 2, - address: new bcoin.address() + address: new Address() }] }); src.addInput(dummy()); - tx = bcoin.mtx() + tx = MTX() .addInput(src, 0) .addOutput(w.getAddress(), 5460); @@ -159,7 +168,7 @@ describe('Wallet', function() { n: 2 }); - k = bcoin.hd.fromMnemonic().deriveAccount44(0).toPublic(); + k = HD.fromMnemonic().deriveAccount44(0).toPublic(); yield w.addSharedKey(k); @@ -169,19 +178,19 @@ describe('Wallet', function() { ]; // Input transaction (bare 1-of-2 multisig) - src = bcoin.mtx({ + src = MTX({ outputs: [{ value: 5460 * 2, - script: bcoin.script.fromMultisig(1, 2, keys) + script: Script.fromMultisig(1, 2, keys) }, { value: 5460 * 2, - address: new bcoin.address() + address: new Address() }] }); src.addInput(dummy()); - tx = bcoin.mtx() + tx = MTX() .addInput(src, 0) .addOutput(w.getAddress(), 5460); @@ -201,7 +210,7 @@ describe('Wallet', function() { doubleSpendWallet = w; // Coinbase - t1 = bcoin.mtx() + t1 = MTX() .addOutput(w.getAddress(), 50000) .addOutput(w.getAddress(), 1000); t1.addInput(dummy()); @@ -211,17 +220,17 @@ describe('Wallet', function() { // yield w.sign(t1); t1 = t1.toTX(); - t2 = bcoin.mtx() + t2 = MTX() .addInput(t1, 0) // 50000 .addOutput(w.getAddress(), 24000) .addOutput(w.getAddress(), 24000); - doubleSpend = bcoin.coin.fromTX(t1, 0, -1); + doubleSpend = Coin.fromTX(t1, 0, -1); // balance: 49000 yield w.sign(t2); t2 = t2.toTX(); - t3 = bcoin.mtx() + t3 = MTX() .addInput(t1, 1) // 1000 .addInput(t2, 0) // 24000 .addOutput(w.getAddress(), 23000); @@ -229,7 +238,7 @@ describe('Wallet', function() { // balance: 47000 yield w.sign(t3); t3 = t3.toTX(); - t4 = bcoin.mtx() + t4 = MTX() .addInput(t2, 1) // 24000 .addInput(t3, 0) // 23000 .addOutput(w.getAddress(), 11000) @@ -238,7 +247,7 @@ describe('Wallet', function() { // balance: 22000 yield w.sign(t4); t4 = t4.toTX(); - f1 = bcoin.mtx() + f1 = MTX() .addInput(t4, 1) // 11000 .addOutput(f.getAddress(), 10000); @@ -246,7 +255,7 @@ describe('Wallet', function() { yield w.sign(f1); f1 = f1.toTX(); - fake = bcoin.mtx() + fake = MTX() .addInput(t1, 1) // 1000 (already redeemed) .addOutput(w.getAddress(), 500); @@ -307,7 +316,7 @@ describe('Wallet', function() { var w = doubleSpendWallet; var tx, txs, total, balance; - tx = bcoin.mtx().addOutput(w.getAddress(), 5000); + tx = MTX().addOutput(w.getAddress(), 5000); tx.addInput(doubleSpend); txs = yield w.getHistory(); @@ -341,7 +350,7 @@ describe('Wallet', function() { it('should handle missed txs without resolution', cob(function* () { var walletdb, w, f, t1, t2, t3, t4, f1, fake, balance, txs; - walletdb = new bcoin.walletdb({ + walletdb = new WalletDB({ name: 'wallet-test', db: 'memory', resolution: false, @@ -354,7 +363,7 @@ describe('Wallet', function() { f = yield walletdb.create(); // Coinbase - t1 = bcoin.mtx() + t1 = MTX() .addOutput(w.getAddress(), 50000) .addOutput(w.getAddress(), 1000); t1.addInput(dummy()); @@ -363,7 +372,7 @@ describe('Wallet', function() { // yield w.sign(t1); t1 = t1.toTX(); - t2 = bcoin.mtx() + t2 = MTX() .addInput(t1, 0) // 50000 .addOutput(w.getAddress(), 24000) .addOutput(w.getAddress(), 24000); @@ -371,7 +380,7 @@ describe('Wallet', function() { // balance: 49000 yield w.sign(t2); t2 = t2.toTX(); - t3 = bcoin.mtx() + t3 = MTX() .addInput(t1, 1) // 1000 .addInput(t2, 0) // 24000 .addOutput(w.getAddress(), 23000); @@ -379,7 +388,7 @@ describe('Wallet', function() { // balance: 47000 yield w.sign(t3); t3 = t3.toTX(); - t4 = bcoin.mtx() + t4 = MTX() .addInput(t2, 1) // 24000 .addInput(t3, 0) // 23000 .addOutput(w.getAddress(), 11000) @@ -388,7 +397,7 @@ describe('Wallet', function() { // balance: 22000 yield w.sign(t4); t4 = t4.toTX(); - f1 = bcoin.mtx() + f1 = MTX() .addInput(t4, 1) // 11000 .addOutput(f.getAddress(), 10000); @@ -396,7 +405,7 @@ describe('Wallet', function() { yield w.sign(f1); f1 = f1.toTX(); - // fake = bcoin.mtx() + // fake = MTX() // .addInput(t1, 1) // 1000 (already redeemed) // .addOutput(w.getAddress(), 500); @@ -473,7 +482,7 @@ describe('Wallet', function() { var view, t1, t2, t3, err; // Coinbase - t1 = bcoin.mtx() + t1 = MTX() .addOutput(w1.getAddress(), 5460) .addOutput(w1.getAddress(), 5460) .addOutput(w1.getAddress(), 5460) @@ -485,7 +494,7 @@ describe('Wallet', function() { yield walletdb.addTX(t1); // Create new transaction - t2 = bcoin.mtx().addOutput(w2.getAddress(), 5460); + t2 = MTX().addOutput(w2.getAddress(), 5460); yield w1.fund(t2, { rate: 10000, round: true }); yield w1.sign(t2); view = t2.view; @@ -502,7 +511,7 @@ describe('Wallet', function() { assert.equal(t2.getFee(view), 10000); // minrelay=1000 // Create new transaction - t3 = bcoin.mtx().addOutput(w2.getAddress(), 15000); + t3 = MTX().addOutput(w2.getAddress(), 15000); try { yield w1.fund(t3, { rate: 10000, round: true }); @@ -520,7 +529,7 @@ describe('Wallet', function() { var view, t1, t2, t3, balance, err; // Coinbase - t1 = bcoin.mtx() + t1 = MTX() .addOutput(w1.getAddress(), 5460) .addOutput(w1.getAddress(), 5460) .addOutput(w1.getAddress(), 5460) @@ -532,7 +541,7 @@ describe('Wallet', function() { yield walletdb.addTX(t1); // Create new transaction - t2 = bcoin.mtx().addOutput(w2.getAddress(), 5460); + t2 = MTX().addOutput(w2.getAddress(), 5460); yield w1.fund(t2, { rate: 10000 }); yield w1.sign(t2); @@ -559,7 +568,7 @@ describe('Wallet', function() { yield walletdb.addTX(t2); // Create new transaction - t3 = bcoin.mtx().addOutput(w2.getAddress(), 15000); + t3 = MTX().addOutput(w2.getAddress(), 15000); try { yield w1.fund(t3, { rate: 10000 }); @@ -579,7 +588,7 @@ describe('Wallet', function() { var t1, t2, tx, cost, total, coins1, coins2, left; // Coinbase - t1 = bcoin.mtx() + t1 = MTX() .addOutput(w1.getAddress(), 5460) .addOutput(w1.getAddress(), 5460) .addOutput(w1.getAddress(), 5460) @@ -589,7 +598,7 @@ describe('Wallet', function() { t1 = t1.toTX(); // Coinbase - t2 = bcoin.mtx() + t2 = MTX() .addOutput(w2.getAddress(), 5460) .addOutput(w2.getAddress(), 5460) .addOutput(w2.getAddress(), 5460) @@ -602,7 +611,7 @@ describe('Wallet', function() { yield walletdb.addTX(t2); // Create our tx with an output - tx = bcoin.mtx(); + tx = MTX(); tx.addOutput(to.getAddress(), 5460); cost = tx.getOutputValue(); @@ -655,8 +664,8 @@ describe('Wallet', function() { assert.equal(tx.verify(), true); })); - var multisig = co(function* multisig(witness, bullshitNesting, cb) { - var flags = bcoin.constants.flags.STANDARD_VERIFY_FLAGS; + multisig = co(function* multisig(witness, bullshitNesting, cb) { + var flags = constants.flags.STANDARD_VERIFY_FLAGS; var options, w1, w2, w3, receive, b58, addr, paddr, utx, send, change; var view; @@ -664,7 +673,7 @@ describe('Wallet', function() { var depth = bullshitNesting ? 'nestedDepth' : 'receiveDepth'; if (witness) - flags |= bcoin.constants.flags.VERIFY_WITNESS; + flags |= constants.flags.VERIFY_WITNESS; // Create 3 2-of-3 wallets with our pubkeys as "shared keys" options = { @@ -688,7 +697,7 @@ describe('Wallet', function() { // Our p2sh address b58 = w1.account[rec].getAddress('base58'); - addr = bcoin.address.fromBase58(b58); + addr = Address.fromBase58(b58); if (witness) { if (bullshitNesting) @@ -709,7 +718,7 @@ describe('Wallet', function() { assert.equal(w3.getNested('base58'), paddr); // Add a shared unspent transaction to our wallets - utx = bcoin.mtx(); + utx = MTX(); if (bullshitNesting) utx.addOutput({ address: paddr, value: 5460 * 10 }); else @@ -740,7 +749,7 @@ describe('Wallet', function() { assert.equal(w3.account[rec].getAddress('base58'), b58); // Create a tx requiring 2 signatures - send = bcoin.mtx(); + send = MTX(); send.addOutput({ address: receive.getAddress(), value: 5460 }); assert(!send.verify(flags)); yield w1.fund(send, { rate: 10000, round: true }); @@ -819,7 +828,7 @@ describe('Wallet', function() { rec = account.receive; // Coinbase - t1 = bcoin.mtx() + t1 = MTX() .addOutput(rec.getAddress(), 5460) .addOutput(rec.getAddress(), 5460) .addOutput(rec.getAddress(), 5460) @@ -831,7 +840,7 @@ describe('Wallet', function() { yield walletdb.addTX(t1); // Create new transaction - t2 = bcoin.mtx().addOutput(w2.getAddress(), 5460); + t2 = MTX().addOutput(w2.getAddress(), 5460); yield w1.fund(t2, { rate: 10000, round: true }); yield w1.sign(t2); @@ -846,7 +855,7 @@ describe('Wallet', function() { assert.equal(t2.getFee(), 10000); // minrelay=1000 // Create new transaction - t3 = bcoin.mtx().addOutput(w2.getAddress(), 15000); + t3 = MTX().addOutput(w2.getAddress(), 15000); try { yield w1.fund(t3, { rate: 10000, round: true }); @@ -885,7 +894,7 @@ describe('Wallet', function() { w.account.receive.getAddress('base58')); // Coinbase - t1 = bcoin.mtx() + t1 = MTX() .addOutput(w.getAddress(), 5460) .addOutput(w.getAddress(), 5460) .addOutput(w.getAddress(), 5460) @@ -897,7 +906,7 @@ describe('Wallet', function() { yield walletdb.addTX(t1); // Should fill from `foo` and fail - t2 = bcoin.mtx().addOutput(w.getAddress(), 5460); + t2 = MTX().addOutput(w.getAddress(), 5460); try { yield w.fund(t2, { rate: 10000, round: true, account: 'foo' }); } catch (e) { @@ -906,11 +915,11 @@ describe('Wallet', function() { assert(err); // Should fill from whole wallet and succeed - t2 = bcoin.mtx().addOutput(w.getAddress(), 5460); + t2 = MTX().addOutput(w.getAddress(), 5460); yield w.fund(t2, { rate: 10000, round: true }); // Coinbase - t1 = bcoin.mtx() + t1 = MTX() .addOutput(account.receive.getAddress(), 5460) .addOutput(account.receive.getAddress(), 5460) .addOutput(account.receive.getAddress(), 5460); @@ -921,7 +930,7 @@ describe('Wallet', function() { yield walletdb.addTX(t1); - t2 = bcoin.mtx().addOutput(w.getAddress(), 5460); + t2 = MTX().addOutput(w.getAddress(), 5460); // Should fill from `foo` and succeed yield w.fund(t2, { rate: 10000, round: true, account: 'foo' }); })); @@ -943,7 +952,7 @@ describe('Wallet', function() { w.master.key = null; // Coinbase - t1 = bcoin.mtx() + t1 = MTX() .addOutput(w.getAddress(), 5460) .addOutput(w.getAddress(), 5460) .addOutput(w.getAddress(), 5460) @@ -955,7 +964,7 @@ describe('Wallet', function() { yield walletdb.addTX(t1); // Create new transaction - t2 = bcoin.mtx().addOutput(w.getAddress(), 5460); + t2 = MTX().addOutput(w.getAddress(), 5460); yield w.fund(t2, { rate: 10000, round: true }); // Should fail @@ -979,7 +988,7 @@ describe('Wallet', function() { var t1, t2; // Coinbase - t1 = bcoin.mtx() + t1 = MTX() .addOutput(w1.getAddress(), 5460) .addOutput(w1.getAddress(), 5460) .addOutput(w1.getAddress(), 5460) @@ -991,7 +1000,7 @@ describe('Wallet', function() { yield walletdb.addTX(t1); // Create new transaction - t2 = bcoin.mtx().addOutput(w2.getAddress(), 21840); + t2 = MTX().addOutput(w2.getAddress(), 21840); yield w1.fund(t2, { rate: 10000, round: true, subtractFee: true }); yield w1.sign(t2); @@ -1002,13 +1011,13 @@ describe('Wallet', function() { assert.equal(t2.getFee(), 10000); })); - it('should fill tx with inputs with subtract fee with create tx', cob(function* () { + it('should fill tx with inputs with subtract fee', cob(function* () { var w1 = yield walletdb.create(); var w2 = yield walletdb.create(); var options, t1, t2; // Coinbase - t1 = bcoin.mtx() + t1 = MTX() .addOutput(w1.getAddress(), 5460) .addOutput(w1.getAddress(), 5460) .addOutput(w1.getAddress(), 5460) @@ -1070,7 +1079,7 @@ describe('Wallet', function() { })); it('should import privkey', cob(function* () { - var key = bcoin.keyring.generate(); + var key = KeyRing.generate(); var w = yield walletdb.create({ passphrase: 'test' }); var options, k, t1, t2, tx; @@ -1081,7 +1090,7 @@ describe('Wallet', function() { assert.equal(k.getHash('hex'), key.getHash('hex')); // Coinbase - t1 = bcoin.mtx() + t1 = MTX() .addOutput(key.getAddress(), 5460) .addOutput(key.getAddress(), 5460) .addOutput(key.getAddress(), 5460) @@ -1113,8 +1122,8 @@ describe('Wallet', function() { })); it('should import pubkey', cob(function* () { - var priv = bcoin.keyring.generate(); - var key = new bcoin.keyring(priv.publicKey); + var priv = KeyRing.generate(); + var key = new KeyRing(priv.publicKey); var w = yield walletdb.create({ watchOnly: true }); var options, k, t1, t2, tx; @@ -1129,7 +1138,7 @@ describe('Wallet', function() { })); it('should import address', cob(function* () { - var key = bcoin.keyring.generate(); + var key = KeyRing.generate(); var w = yield walletdb.create({ watchOnly: true }); var options, k, t1, t2, tx; @@ -1158,7 +1167,7 @@ describe('Wallet', function() { assert.equal(details[0].toJSON().id, 'test'); })); - it('should handle changed passphrase with encrypted imports', cob(function* () { + it('should change passphrase with encrypted imports', cob(function* () { var w = ewallet; var addr = ekey.getAddress(); var path, d1, d2, k; @@ -1200,7 +1209,7 @@ describe('Wallet', function() { it('should recover from a missed tx', cob(function* () { var walletdb, alice, addr, bob, t1, t2, t3; - walletdb = new bcoin.walletdb({ + walletdb = new WalletDB({ name: 'wallet-test', db: 'memory', resolution: false, @@ -1214,7 +1223,7 @@ describe('Wallet', function() { addr = alice.getAddress(); // Coinbase - t1 = bcoin.mtx() + t1 = MTX() .addOutput(addr, 50000); t1.addInput(dummy()); @@ -1225,7 +1234,7 @@ describe('Wallet', function() { yield bob.add(t1); // Bob misses this tx! - t2 = bcoin.mtx() + t2 = MTX() .addInput(t1, 0) .addOutput(addr, 24000) .addOutput(addr, 24000); @@ -1240,7 +1249,7 @@ describe('Wallet', function() { (yield bob.getBalance()).unconfirmed); // Bob sees this one. - t3 = bcoin.mtx() + t3 = MTX() .addInput(t2, 0) .addInput(t2, 1) .addOutput(addr, 30000); @@ -1267,7 +1276,7 @@ describe('Wallet', function() { it('should recover from a missed tx and double spend', cob(function* () { var walletdb, alice, addr, bob, t1, t2, t3, t2a; - walletdb = new bcoin.walletdb({ + walletdb = new WalletDB({ name: 'wallet-test', db: 'memory', resolution: false, @@ -1281,7 +1290,7 @@ describe('Wallet', function() { addr = alice.getAddress(); // Coinbase - t1 = bcoin.mtx() + t1 = MTX() .addOutput(addr, 50000); t1.addInput(dummy()); @@ -1292,7 +1301,7 @@ describe('Wallet', function() { yield bob.add(t1); // Bob misses this tx! - t2 = bcoin.mtx() + t2 = MTX() .addInput(t1, 0) .addOutput(addr, 24000) .addOutput(addr, 24000); @@ -1307,7 +1316,7 @@ describe('Wallet', function() { (yield bob.getBalance()).unconfirmed); // Bob doublespends. - t2a = bcoin.mtx() + t2a = MTX() .addInput(t1, 0) .addOutput(addr, 10000) .addOutput(addr, 10000); @@ -1318,7 +1327,7 @@ describe('Wallet', function() { yield bob.add(t2a); // Bob sees this one. - t3 = bcoin.mtx() + t3 = MTX() .addInput(t2, 0) .addInput(t2, 1) .addOutput(addr, 30000);