remove .init(), move it to constructor

This commit is contained in:
Matias Alejo Garcia 2014-03-29 04:01:32 -03:00
parent 7504637d92
commit 9fc2493a6d
4 changed files with 37 additions and 58 deletions

View File

@ -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

View File

@ -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;

View File

@ -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);
*

View File

@ -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);