Skip to content

Commit

Permalink
remove .init(), move it to constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
matiu committed Mar 29, 2014
1 parent 7504637 commit 9fc2493
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 58 deletions.
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,20 +175,17 @@ 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)
.build();

/* 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
Expand Down
51 changes: 22 additions & 29 deletions TransactionBuilder.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -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;
}

/*
Expand Down Expand Up @@ -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;
Expand Down
9 changes: 3 additions & 6 deletions examples/CreateAndSignTx.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,17 @@ 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)
.build();

/* 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);
*
Expand Down
26 changes: 9 additions & 17 deletions test/test.TransactionBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -117,8 +115,7 @@ describe('TransactionBuilder', function() {
amount: 0.08
}];

return (new TransactionBuilder())
.init(opts)
return new TransactionBuilder(opts)
.setUnspent(testdata.dataUnspent)
.setOutputs(outs);
};
Expand Down Expand Up @@ -146,8 +143,7 @@ describe('TransactionBuilder', function() {
}];

(function() {
(new TransactionBuilder(opts))
.init(opts)
new TransactionBuilder(opts)
.setUnspent(testdata.dataUnspent)
.setOutputs(outs);
}).should.throw();
Expand All @@ -158,17 +154,15 @@ describe('TransactionBuilder', function() {
}];

should.exist(
(new TransactionBuilder(opts))
.init(opts)
new TransactionBuilder(opts)
.setUnspent(testdata.dataUnspent)
.setOutputs(outs2)
);

// do not allow unconfirmed
opts.spendUnconfirmed = false;
(function() {
(new TransactionBuilder(opts))
.init(opts)
new TransactionBuilder(opts)
.setUnspent(testdata.dataUnspent)
.setOutputs(outs2);
}).should.throw();
Expand Down Expand Up @@ -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);
};
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 9fc2493

Please sign in to comment.