Merge pull request #1345 from fanatid/feature/Transaction.to

allow Transaction.to take array (#1334)
This commit is contained in:
Braydon Fuller 2015-10-13 17:56:29 -04:00
commit abae55595b
2 changed files with 47 additions and 14 deletions

View File

@ -480,6 +480,14 @@ Transaction.prototype._newTransaction = function() {
/* Transaction creation interface */ /* Transaction creation interface */
/**
* @typedef {Object} Transaction~fromObject
* @property {string} prevTxId
* @property {number} outputIndex
* @property {(Buffer|string|Script)} script
* @property {number} satoshis
*/
/** /**
* Add an input to this transaction. This is a high level interface * Add an input to this transaction. This is a high level interface
* to add an input, for more control, use @{link Transaction#addInput}. * to add an input, for more control, use @{link Transaction#addInput}.
@ -517,7 +525,7 @@ Transaction.prototype._newTransaction = function() {
* ['03000...', '02000...'], 2); * ['03000...', '02000...'], 2);
* ``` * ```
* *
* @param {Object} utxo * @param {(Array.<Transaction~fromObject>|Transaction~fromObject)} utxo
* @param {Array=} pubkeys * @param {Array=} pubkeys
* @param {number=} threshold * @param {number=} threshold
*/ */
@ -691,17 +699,31 @@ Transaction.prototype.getChangeOutput = function() {
return null; return null;
}; };
/**
* @typedef {Object} Transaction~toObject
* @property {(string|Address)} address
* @property {number} satoshis
*/
/** /**
* Add an output to the transaction. * Add an output to the transaction.
* *
* Beware that this resets all the signatures for inputs (in further versions, * Beware that this resets all the signatures for inputs (in further versions,
* SIGHASH_SINGLE or SIGHASH_NONE signatures will not be reset). * SIGHASH_SINGLE or SIGHASH_NONE signatures will not be reset).
* *
* @param {string|Address} address * @param {(string|Address|Array.<Transaction~toObject>)} address
* @param {number} amount in satoshis * @param {number} amount in satoshis
* @return {Transaction} this, for chaining * @return {Transaction} this, for chaining
*/ */
Transaction.prototype.to = function(address, amount) { Transaction.prototype.to = function(address, amount) {
if (_.isArray(address)) {
var self = this;
_.each(address, function(to) {
self.to(to.address, to.satoshis);
});
return this;
}
$.checkArgument( $.checkArgument(
JSUtil.isNaturalNumber(amount), JSUtil.isNaturalNumber(amount),
'Amount is expected to be a positive integer' 'Amount is expected to be a positive integer'

View File

@ -41,7 +41,8 @@ describe('Transaction', function() {
'outputIndex': 0, 'outputIndex': 0,
'script': testScript, 'script': testScript,
'satoshis': testAmount 'satoshis': testAmount
}).to('mrU9pEmAx26HcbKVrABvgL7AwA5fjNFoDc', testAmount - 10000); })
.to('mrU9pEmAx26HcbKVrABvgL7AwA5fjNFoDc', testAmount - 10000);
it('can serialize to a plain javascript object', function() { it('can serialize to a plain javascript object', function() {
var object = testTransaction.toObject(); var object = testTransaction.toObject();
@ -168,7 +169,7 @@ describe('Transaction', function() {
it('works for normal p2pkh', function() { it('works for normal p2pkh', function() {
var transaction = new Transaction() var transaction = new Transaction()
.from(simpleUtxoWith100000Satoshis) .from(simpleUtxoWith100000Satoshis)
.to(toAddress, 50000) .to([{address: toAddress, satoshis: 50000}])
.change(changeAddress) .change(changeAddress)
.sign(privateKey); .sign(privateKey);
transaction.isFullySigned().should.equal(true); transaction.isFullySigned().should.equal(true);
@ -506,7 +507,8 @@ describe('Transaction', function() {
'outputIndex': 0, 'outputIndex': 0,
'script': testScript, 'script': testScript,
'satoshis': testAmount 'satoshis': testAmount
}).to('mrU9pEmAx26HcbKVrABvgL7AwA5fjNFoDc', testAmount - 10000); })
.to('mrU9pEmAx26HcbKVrABvgL7AwA5fjNFoDc', testAmount - 10000);
tx.outputs[0]._satoshis = 100; tx.outputs[0]._satoshis = 100;
tx.outputs[0]._satoshisBN = new BN('fffffffffffffff', 16); tx.outputs[0]._satoshisBN = new BN('fffffffffffffff', 16);
@ -521,7 +523,8 @@ describe('Transaction', function() {
'outputIndex': 0, 'outputIndex': 0,
'script': testScript, 'script': testScript,
'satoshis': testAmount 'satoshis': testAmount
}).to('mrU9pEmAx26HcbKVrABvgL7AwA5fjNFoDc', testAmount - 10000); })
.to('mrU9pEmAx26HcbKVrABvgL7AwA5fjNFoDc', testAmount - 10000);
tx.outputs[0]._satoshis = -100; tx.outputs[0]._satoshis = -100;
tx.outputs[0]._satoshisBN = new BN(-100, 10); tx.outputs[0]._satoshisBN = new BN(-100, 10);
@ -537,7 +540,8 @@ describe('Transaction', function() {
'outputIndex': 0, 'outputIndex': 0,
'script': testScript, 'script': testScript,
'satoshis': testAmount 'satoshis': testAmount
}).to('mrU9pEmAx26HcbKVrABvgL7AwA5fjNFoDc', testAmount - 10000); })
.to('mrU9pEmAx26HcbKVrABvgL7AwA5fjNFoDc', testAmount - 10000);
tx.toBuffer = sinon.stub().returns({ tx.toBuffer = sinon.stub().returns({
length: 10000000 length: 10000000
@ -556,7 +560,8 @@ describe('Transaction', function() {
'outputIndex': 0, 'outputIndex': 0,
'script': testScript, 'script': testScript,
'satoshis': testAmount 'satoshis': testAmount
}).to('mrU9pEmAx26HcbKVrABvgL7AwA5fjNFoDc', testAmount - 10000); })
.to('mrU9pEmAx26HcbKVrABvgL7AwA5fjNFoDc', testAmount - 10000);
tx.isCoinbase = sinon.stub().returns(false); tx.isCoinbase = sinon.stub().returns(false);
tx.inputs[0].isNull = sinon.stub().returns(true); tx.inputs[0].isNull = sinon.stub().returns(true);
@ -692,8 +697,10 @@ describe('Transaction', function() {
}); });
it('an output can be removed by index', function() { it('an output can be removed by index', function() {
var transaction = new Transaction() var transaction = new Transaction()
.to(toAddress, 40000000) .to([
.to(toAddress, 40000000); {address: toAddress, satoshis: 40000000},
{address: toAddress, satoshis: 40000000}
])
transaction.outputs.length.should.equal(2); transaction.outputs.length.should.equal(2);
transaction.outputAmount.should.equal(80000000); transaction.outputAmount.should.equal(80000000);
transaction.removeOutput(0); transaction.removeOutput(0);
@ -845,8 +852,10 @@ describe('Transaction', function() {
beforeEach(function() { beforeEach(function() {
transaction = new Transaction() transaction = new Transaction()
.from(simpleUtxoWith1BTC) .from(simpleUtxoWith1BTC)
.to(toAddress, tenth) .to([
.to(toAddress, fourth) {address: toAddress, satoshis: tenth},
{address: toAddress, satoshis: fourth}
])
.to(toAddress, half) .to(toAddress, half)
.change(changeAddress); .change(changeAddress);
out1 = transaction.outputs[0]; out1 = transaction.outputs[0];
@ -905,8 +914,10 @@ describe('Transaction', function() {
var tx = new Transaction() var tx = new Transaction()
.from(simpleUtxoWith1BTC) .from(simpleUtxoWith1BTC)
.to(toAddress, tenth) .to(toAddress, tenth)
.to(toAddress, fourth) .to([
.to(toAddress, half) {address: toAddress, satoshis: fourth},
{address: toAddress, satoshis: half}
])
.change(changeAddress); .change(changeAddress);
tx.clearOutputs(); tx.clearOutputs();
tx.outputs.length.should.equal(1); tx.outputs.length.should.equal(1);