Skip to content

Commit 062540e

Browse files
committed
Transaction: optional script for addInput
1 parent 5d5dcd3 commit 062540e

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/transaction.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ Transaction.SIGHASH_ANYONECANPAY = 0x80
3232
*
3333
* Note that this method does not sign the created input.
3434
*/
35-
Transaction.prototype.addInput = function(hash, index, sequence) {
35+
Transaction.prototype.addInput = function(hash, index, sequence, script) {
3636
if (sequence === undefined) sequence = Transaction.DEFAULT_SEQUENCE
37+
script = script || Script.EMPTY
3738

3839
if (typeof hash === 'string') {
3940
// TxId hex is big-endian, we need little-endian
@@ -47,14 +48,15 @@ Transaction.prototype.addInput = function(hash, index, sequence) {
4748
enforceType('Buffer', hash)
4849
enforceType('Number', index)
4950
enforceType('Number', sequence)
51+
enforceType(Script, script)
5052

5153
assert.equal(hash.length, 32, 'Expected hash length of 32, got ' + hash.length)
5254

5355
// Add the input and return the input's index
5456
return (this.ins.push({
5557
hash: hash,
5658
index: index,
57-
script: Script.EMPTY,
59+
script: script,
5860
sequence: sequence
5961
}) - 1)
6062
}

test/transaction.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var scripts = require('../src/scripts')
55
var Address = require('../src/address')
66
var ECKey = require('../src/eckey')
77
var Transaction = require('../src/transaction')
8+
var Script = require('../src/script')
89

910
var fixtures = require('./fixtures/transaction')
1011

@@ -96,20 +97,30 @@ describe('Transaction', function() {
9697
assert.equal(tx.ins[0].sequence, Transaction.DEFAULT_SEQUENCE)
9798
})
9899

100+
it('defaults to empty script', function() {
101+
var tx = new Transaction()
102+
tx.addInput(prevTxHash, 0)
103+
104+
assert.equal(tx.ins[0].script, Script.EMPTY)
105+
})
106+
99107
fixtures.valid.forEach(function(f) {
100108
it('should add the inputs for ' + f.txid + ' correctly', function() {
101109
var tx = new Transaction()
102110

103111
f.raw.ins.forEach(function(txIn, i) {
104-
var j = tx.addInput(txIn.hash, txIn.index, txIn.sequence)
112+
var script = txIn.script ? Script.fromHex(txIn.script) : Script.EMPTY
113+
var j = tx.addInput(txIn.hash, txIn.index, txIn.sequence, script)
105114

106115
assert.equal(i, j)
107116
assert.deepEqual(tx.ins[i].hash, txIn.hash)
108117
assert.equal(tx.ins[i].index, txIn.index)
109118

110119
var sequence = txIn.sequence
111-
if (sequence == undefined) sequence = Transaction.DEFAULT_SEQUENCE
120+
if (sequence === undefined) sequence = Transaction.DEFAULT_SEQUENCE
121+
112122
assert.equal(tx.ins[i].sequence, sequence)
123+
assert.equal(tx.ins[i].script, script)
113124
})
114125
})
115126
})

0 commit comments

Comments
 (0)