Browse Source

remove .init(), move it to constructor

patch-2
Matias Alejo Garcia 11 years ago
parent
commit
9fc2493a6d
  1. 9
      README.md
  2. 51
      TransactionBuilder.js
  3. 9
      examples/CreateAndSignTx.js
  4. 26
      test/test.TransactionBuilder.js

9
README.md

@ -175,10 +175,9 @@ peerman.on('connect', function() {
if (conn) { if (conn) {
var outs = [{address:toAddress, amount:amt}]; var outs = [{address:toAddress, amount:amt}];
var opts = {remainderAddress: changeAddressString}; var opts = {remainderAddress: changeAddressString};
var builder = new bitcore.TransactionBuilder; var Builder = bitcore.TransactionBuilder;
var tx = builder var tx = new Builder(opts)
.init(opts)
.setUnspent(Unspent) .setUnspent(Unspent)
.setOutputs(outs) .setOutputs(outs)
.sign(keys) .sign(keys)
@ -186,9 +185,7 @@ peerman.on('connect', function() {
/* create and signing can be done in multiple steps using: /* create and signing can be done in multiple steps using:
* *
* var builder = bitcore * var builder = new bitcore.TransactionBuilder(opts)
* .TransactionBuilder
* .init(opts)
* .setUnspent(utxos) * .setUnspent(utxos)
* .setOutputs(outs); * .setOutputs(outs);
* //later * //later

51
TransactionBuilder.js

@ -1,13 +1,13 @@
/* /*
var tx = TransactionBuilder.init(opts) var tx = (new TransactionBuilder(opts))
.setUnspent(utxos) .setUnspent(utxos)
.setOutputs(outs) .setOutputs(outs)
.sign(keys) .sign(keys)
.build(); .build();
var builder = TransactionBuilder.init(opts) var builder = (new TransactionBuilder(opts))
.setUnspent(spent) .setUnspent(spent)
.setOutputs(outs); .setOutputs(outs);
@ -88,8 +88,26 @@ var PrivateKey = imports.PrivateKey || require('./PrivateKey');
var Transaction = imports.Transaction || require('./Transaction'); var Transaction = imports.Transaction || require('./Transaction');
var FEE_PER_1000B_SAT = parseInt(0.0001 * util.COIN); var FEE_PER_1000B_SAT = parseInt(0.0001 * util.COIN);
function TransactionBuilder() { function TransactionBuilder(opts) {
this.txobj = {}; 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; 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() { TransactionBuilder.prototype._setInputs = function() {
var ins = this.selectedUtxos; var ins = this.selectedUtxos;
var l = ins.length; var l = ins.length;

9
examples/CreateAndSignTx.js

@ -26,10 +26,9 @@ var run = function() {
var outs = [{address:toAddress, amount:amt}]; var outs = [{address:toAddress, amount:amt}];
var keys = [priv]; var keys = [priv];
var opts = {remainderAddress: changeAddressString}; var opts = {remainderAddress: changeAddressString};
var builder = new bitcore.TransactionBuilder; var Builder = bitcore.TransactionBuilder;
var tx = builder var tx = new Builder(opts)
.init(opts)
.setUnspent(utxos) .setUnspent(utxos)
.setOutputs(outs) .setOutputs(outs)
.sign(keys) .sign(keys)
@ -37,9 +36,7 @@ var run = function() {
/* create and signing can be done in multiple steps using: /* create and signing can be done in multiple steps using:
* *
* var builder = bitcore * var builder = new bitcore.TransactionBuilder(opts)
* .TransactionBuilder
* .init(opts)
* .setUnspent(utxos) * .setUnspent(utxos)
* .setOutputs(outs); * .setOutputs(outs);
* *

26
test/test.TransactionBuilder.js

@ -26,9 +26,8 @@ describe('TransactionBuilder', function() {
should.exist(t); should.exist(t);
}); });
it('should be able init', function() { it('should be able to create instance with params', function() {
var t = new TransactionBuilder(); var t = new TransactionBuilder({spendUnconfirmed: true, lockTime: 10});
t.init({spendUnconfirmed: true, lockTime: 10});
should.exist(t); should.exist(t);
should.exist(t.txobj.version); should.exist(t.txobj.version);
t.spendUnconfirmed.should.equal(true); t.spendUnconfirmed.should.equal(true);
@ -37,8 +36,7 @@ describe('TransactionBuilder', function() {
var getBuilder = function (spendUnconfirmed) { var getBuilder = function (spendUnconfirmed) {
var t = new TransactionBuilder(); var t = new TransactionBuilder({spendUnconfirmed: spendUnconfirmed})
t.init( {spendUnconfirmed: spendUnconfirmed})
.setUnspent(testdata.dataUnspent); .setUnspent(testdata.dataUnspent);
return t; return t;
@ -117,8 +115,7 @@ describe('TransactionBuilder', function() {
amount: 0.08 amount: 0.08
}]; }];
return (new TransactionBuilder()) return new TransactionBuilder(opts)
.init(opts)
.setUnspent(testdata.dataUnspent) .setUnspent(testdata.dataUnspent)
.setOutputs(outs); .setOutputs(outs);
}; };
@ -146,8 +143,7 @@ describe('TransactionBuilder', function() {
}]; }];
(function() { (function() {
(new TransactionBuilder(opts)) new TransactionBuilder(opts)
.init(opts)
.setUnspent(testdata.dataUnspent) .setUnspent(testdata.dataUnspent)
.setOutputs(outs); .setOutputs(outs);
}).should.throw(); }).should.throw();
@ -158,8 +154,7 @@ describe('TransactionBuilder', function() {
}]; }];
should.exist( should.exist(
(new TransactionBuilder(opts)) new TransactionBuilder(opts)
.init(opts)
.setUnspent(testdata.dataUnspent) .setUnspent(testdata.dataUnspent)
.setOutputs(outs2) .setOutputs(outs2)
); );
@ -167,8 +162,7 @@ describe('TransactionBuilder', function() {
// do not allow unconfirmed // do not allow unconfirmed
opts.spendUnconfirmed = false; opts.spendUnconfirmed = false;
(function() { (function() {
(new TransactionBuilder(opts)) new TransactionBuilder(opts)
.init(opts)
.setUnspent(testdata.dataUnspent) .setUnspent(testdata.dataUnspent)
.setOutputs(outs2); .setOutputs(outs2);
}).should.throw(); }).should.throw();
@ -227,8 +221,7 @@ describe('TransactionBuilder', function() {
}]; }];
//console.log('[test.TransactionBuilder.js.216:outs:]',outs, outs.length); //TODO //console.log('[test.TransactionBuilder.js.216:outs:]',outs, outs.length); //TODO
return (new TransactionBuilder()) return new TransactionBuilder(opts)
.init(opts)
.setUnspent(testdata.dataUnspentSign.unspent) .setUnspent(testdata.dataUnspentSign.unspent)
.setOutputs(outs); .setOutputs(outs);
}; };
@ -268,8 +261,7 @@ describe('TransactionBuilder', function() {
amount: 0.08 amount: 0.08
}]; }];
var b = (new TransactionBuilder()) var b = new TransactionBuilder()
.init()
.setUnspent(testdata.dataUnspentSign.unspent) .setUnspent(testdata.dataUnspentSign.unspent)
.setOutputs(outs) .setOutputs(outs)
.sign(keys); .sign(keys);

Loading…
Cancel
Save