From 69f51c749bf1457551099d3eb471958161227334 Mon Sep 17 00:00:00 2001 From: Patrick Nagurny Date: Thu, 16 Jul 2015 16:17:00 -0400 Subject: [PATCH] require coinbaseAddress to build coinbase transaction --- lib/db.js | 7 ++++--- test/db.unit.js | 20 ++++++-------------- 2 files changed, 10 insertions(+), 17 deletions(-) diff --git a/lib/db.js b/lib/db.js index ed24ab50..28a2570d 100644 --- a/lib/db.js +++ b/lib/db.js @@ -20,6 +20,7 @@ function DB(options) { BaseDB.call(this, options); + this.coinbaseAddress = options.coinbaseAddress; this.coinbaseAmount = options.coinbaseAmount || 50 * 1e8; this.Transaction = Transaction; @@ -81,8 +82,8 @@ DB.prototype.buildGenesisData = function() { }; DB.prototype.buildCoinbaseTransaction = function(transactions, data) { - if(!this.wallet) { - throw new Error('Wallet required to build coinbase'); + if(!this.coinbaseAddress) { + throw new Error('coinbaseAddress required to build coinbase'); } if(!data) { @@ -96,7 +97,7 @@ DB.prototype.buildCoinbaseTransaction = function(transactions, data) { } var coinbaseTx = new this.Transaction(); - coinbaseTx.to('1JwSSubhmg6iPtRjtyqhUYYH7bZg3Lfy1T', this.coinbaseAmount + fees); + coinbaseTx.to(this.coinbaseAddress, this.coinbaseAmount + fees); var script = bitcore.Script.buildDataOut(data); diff --git a/test/db.unit.js b/test/db.unit.js index 309515cf..54ca6a82 100644 --- a/test/db.unit.js +++ b/test/db.unit.js @@ -69,9 +69,7 @@ describe('Bitcoin DB', function() { describe('#buildCoinbaseTransaction', function() { it('should correctly build a coinbase transaction with no fees', function() { var db = new DB({path: 'path', store: memdown}); - db.wallet = { - getAddress: sinon.stub().returns('mzso6uXxfDCq4L6xAffUD9BPWo6bdFBZ2L') - }; + db.coinbaseAddress = 'mzso6uXxfDCq4L6xAffUD9BPWo6bdFBZ2L'; db.coinbaseAmount = coinbaseAmount; var coinbaseTx = db.buildCoinbaseTransaction(); coinbaseTx.inputs.length.should.equal(1); @@ -88,9 +86,7 @@ describe('Bitcoin DB', function() { it('should correctly build a coinbase transaction with fees', function() { var db = new DB({path: 'path', store: memdown}); - db.wallet = { - getAddress: sinon.stub().returns('mzso6uXxfDCq4L6xAffUD9BPWo6bdFBZ2L') - }; + db.coinbaseAddress = 'mzso6uXxfDCq4L6xAffUD9BPWo6bdFBZ2L'; db.coinbaseAmount = coinbaseAmount; var transactions = [ { @@ -117,18 +113,16 @@ describe('Bitcoin DB', function() { output.satoshis.should.equal(coinbaseAmount + 2000); }); - it('should throw an error if wallet not included', function() { + it('should throw an error if coinbaseAddress not included', function() { var db = new DB({path: 'path', store: memdown}); (function() { db.buildCoinbaseTransaction(); - }).should.throw('Wallet required to build coinbase'); + }).should.throw('coinbaseAddress required to build coinbase'); }); it('will build a coinbase database with different data', function() { var db = new DB({path: 'path', store: memdown}); - db.wallet = { - getAddress: sinon.stub().returns('mzso6uXxfDCq4L6xAffUD9BPWo6bdFBZ2L') - }; + db.coinbaseAddress = 'mzso6uXxfDCq4L6xAffUD9BPWo6bdFBZ2L'; var tx1 = db.buildCoinbaseTransaction().uncheckedSerialize(); var tx2 = db.buildCoinbaseTransaction().uncheckedSerialize(); tx1.should.not.equal(tx2); @@ -136,9 +130,7 @@ describe('Bitcoin DB', function() { it('can pass in custom data', function() { var db = new DB({path: 'path', store: memdown}); - db.wallet = { - getAddress: sinon.stub().returns('mzso6uXxfDCq4L6xAffUD9BPWo6bdFBZ2L') - }; + db.coinbaseAddress = 'mzso6uXxfDCq4L6xAffUD9BPWo6bdFBZ2L'; var tx1 = db.buildCoinbaseTransaction(null, new Buffer('abcdef', 'hex')); var data = tx1.inputs[0]._script.getData(); data.should.deep.equal(new Buffer('abcdef', 'hex'));