From 9fc2493a6dfdac787dfff18f0bae115451abe80c Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Sat, 29 Mar 2014 04:01:32 -0300 Subject: [PATCH] remove .init(), move it to constructor --- README.md | 9 ++---- TransactionBuilder.js | 51 ++++++++++++++------------------- examples/CreateAndSignTx.js | 9 ++---- test/test.TransactionBuilder.js | 26 ++++++----------- 4 files changed, 37 insertions(+), 58 deletions(-) diff --git a/README.md b/README.md index bcbbc5e..f17f1d0 100644 --- a/README.md +++ b/README.md @@ -175,10 +175,9 @@ peerman.on('connect', function() { if (conn) { var outs = [{address:toAddress, amount:amt}]; var opts = {remainderAddress: changeAddressString}; - var builder = new bitcore.TransactionBuilder; + var Builder = bitcore.TransactionBuilder; - var tx = builder - .init(opts) + var tx = new Builder(opts) .setUnspent(Unspent) .setOutputs(outs) .sign(keys) @@ -186,9 +185,7 @@ peerman.on('connect', function() { /* create and signing can be done in multiple steps using: * - * var builder = bitcore - * .TransactionBuilder - * .init(opts) + * var builder = new bitcore.TransactionBuilder(opts) * .setUnspent(utxos) * .setOutputs(outs); * //later diff --git a/TransactionBuilder.js b/TransactionBuilder.js index 86f8a28..083decb 100644 --- a/TransactionBuilder.js +++ b/TransactionBuilder.js @@ -1,13 +1,13 @@ /* - var tx = TransactionBuilder.init(opts) + var tx = (new TransactionBuilder(opts)) .setUnspent(utxos) .setOutputs(outs) .sign(keys) .build(); - var builder = TransactionBuilder.init(opts) + var builder = (new TransactionBuilder(opts)) .setUnspent(spent) .setOutputs(outs); @@ -88,8 +88,26 @@ var PrivateKey = imports.PrivateKey || require('./PrivateKey'); var Transaction = imports.Transaction || require('./Transaction'); var FEE_PER_1000B_SAT = parseInt(0.0001 * util.COIN); -function TransactionBuilder() { - this.txobj = {}; +function TransactionBuilder(opts) { + var opts = opts || {}; + this.txobj = {}; + this.txobj.version = 1; + this.txobj.lock_time = opts.lockTime || 0; + this.txobj.ins = []; + this.txobj.outs = []; + + this.spendUnconfirmed = opts.spendUnconfirmed || false; + + if (opts.fee || opts.feeSat) { + this.givenFeeSat = opts.fee ? opts.fee * util.COIN : opts.feeSat; + } + this.remainderAddress = opts.remainderAddress; + this.signhash = opts.signhash || Transaction.SIGHASH_ALL; + + this.tx = {}; + this.inputsSigned= 0; + + return this; } /* @@ -189,31 +207,6 @@ TransactionBuilder.prototype._selectUnspent = function(neededAmountSat) { return this; }; - -TransactionBuilder.prototype.init = function(opts) { - var opts = opts || {}; - this.txobj = {}; - this.txobj.version = 1; - this.txobj.lock_time = opts.lockTime || 0; - this.txobj.ins = []; - this.txobj.outs = []; - - this.spendUnconfirmed = opts.spendUnconfirmed || false; - - if (opts.fee || opts.feeSat) { - this.givenFeeSat = opts.fee ? opts.fee * util.COIN : opts.feeSat; - } - this.remainderAddress = opts.remainderAddress; - this.signhash = opts.signhash || Transaction.SIGHASH_ALL; - - this.tx = {}; - this.inputsSigned= 0; - - return this; -}; - - - TransactionBuilder.prototype._setInputs = function() { var ins = this.selectedUtxos; var l = ins.length; diff --git a/examples/CreateAndSignTx.js b/examples/CreateAndSignTx.js index 32990e6..1c2273e 100644 --- a/examples/CreateAndSignTx.js +++ b/examples/CreateAndSignTx.js @@ -26,10 +26,9 @@ var run = function() { var outs = [{address:toAddress, amount:amt}]; var keys = [priv]; var opts = {remainderAddress: changeAddressString}; - var builder = new bitcore.TransactionBuilder; + var Builder = bitcore.TransactionBuilder; - var tx = builder - .init(opts) + var tx = new Builder(opts) .setUnspent(utxos) .setOutputs(outs) .sign(keys) @@ -37,9 +36,7 @@ var run = function() { /* create and signing can be done in multiple steps using: * - * var builder = bitcore - * .TransactionBuilder - * .init(opts) + * var builder = new bitcore.TransactionBuilder(opts) * .setUnspent(utxos) * .setOutputs(outs); * diff --git a/test/test.TransactionBuilder.js b/test/test.TransactionBuilder.js index 1f347b4..dd5a472 100644 --- a/test/test.TransactionBuilder.js +++ b/test/test.TransactionBuilder.js @@ -26,9 +26,8 @@ describe('TransactionBuilder', function() { should.exist(t); }); - it('should be able init', function() { - var t = new TransactionBuilder(); - t.init({spendUnconfirmed: true, lockTime: 10}); + it('should be able to create instance with params', function() { + var t = new TransactionBuilder({spendUnconfirmed: true, lockTime: 10}); should.exist(t); should.exist(t.txobj.version); t.spendUnconfirmed.should.equal(true); @@ -37,8 +36,7 @@ describe('TransactionBuilder', function() { var getBuilder = function (spendUnconfirmed) { - var t = new TransactionBuilder(); - t.init( {spendUnconfirmed: spendUnconfirmed}) + var t = new TransactionBuilder({spendUnconfirmed: spendUnconfirmed}) .setUnspent(testdata.dataUnspent); return t; @@ -117,8 +115,7 @@ describe('TransactionBuilder', function() { amount: 0.08 }]; - return (new TransactionBuilder()) - .init(opts) + return new TransactionBuilder(opts) .setUnspent(testdata.dataUnspent) .setOutputs(outs); }; @@ -146,8 +143,7 @@ describe('TransactionBuilder', function() { }]; (function() { - (new TransactionBuilder(opts)) - .init(opts) + new TransactionBuilder(opts) .setUnspent(testdata.dataUnspent) .setOutputs(outs); }).should.throw(); @@ -158,8 +154,7 @@ describe('TransactionBuilder', function() { }]; should.exist( - (new TransactionBuilder(opts)) - .init(opts) + new TransactionBuilder(opts) .setUnspent(testdata.dataUnspent) .setOutputs(outs2) ); @@ -167,8 +162,7 @@ describe('TransactionBuilder', function() { // do not allow unconfirmed opts.spendUnconfirmed = false; (function() { - (new TransactionBuilder(opts)) - .init(opts) + new TransactionBuilder(opts) .setUnspent(testdata.dataUnspent) .setOutputs(outs2); }).should.throw(); @@ -227,8 +221,7 @@ describe('TransactionBuilder', function() { }]; //console.log('[test.TransactionBuilder.js.216:outs:]',outs, outs.length); //TODO - return (new TransactionBuilder()) - .init(opts) + return new TransactionBuilder(opts) .setUnspent(testdata.dataUnspentSign.unspent) .setOutputs(outs); }; @@ -268,8 +261,7 @@ describe('TransactionBuilder', function() { amount: 0.08 }]; - var b = (new TransactionBuilder()) - .init() + var b = new TransactionBuilder() .setUnspent(testdata.dataUnspentSign.unspent) .setOutputs(outs) .sign(keys);