Transaction: optional script for addInput

This commit is contained in:
Daniel Cousens 2014-10-16 15:25:43 +11:00
parent 5d5dcd3d73
commit 062540e3d9
2 changed files with 17 additions and 4 deletions

View file

@ -32,8 +32,9 @@ Transaction.SIGHASH_ANYONECANPAY = 0x80
* *
* Note that this method does not sign the created input. * Note that this method does not sign the created input.
*/ */
Transaction.prototype.addInput = function(hash, index, sequence) { Transaction.prototype.addInput = function(hash, index, sequence, script) {
if (sequence === undefined) sequence = Transaction.DEFAULT_SEQUENCE if (sequence === undefined) sequence = Transaction.DEFAULT_SEQUENCE
script = script || Script.EMPTY
if (typeof hash === 'string') { if (typeof hash === 'string') {
// TxId hex is big-endian, we need little-endian // TxId hex is big-endian, we need little-endian
@ -47,6 +48,7 @@ Transaction.prototype.addInput = function(hash, index, sequence) {
enforceType('Buffer', hash) enforceType('Buffer', hash)
enforceType('Number', index) enforceType('Number', index)
enforceType('Number', sequence) enforceType('Number', sequence)
enforceType(Script, script)
assert.equal(hash.length, 32, 'Expected hash length of 32, got ' + hash.length) assert.equal(hash.length, 32, 'Expected hash length of 32, got ' + hash.length)
@ -54,7 +56,7 @@ Transaction.prototype.addInput = function(hash, index, sequence) {
return (this.ins.push({ return (this.ins.push({
hash: hash, hash: hash,
index: index, index: index,
script: Script.EMPTY, script: script,
sequence: sequence sequence: sequence
}) - 1) }) - 1)
} }

View file

@ -5,6 +5,7 @@ var scripts = require('../src/scripts')
var Address = require('../src/address') var Address = require('../src/address')
var ECKey = require('../src/eckey') var ECKey = require('../src/eckey')
var Transaction = require('../src/transaction') var Transaction = require('../src/transaction')
var Script = require('../src/script')
var fixtures = require('./fixtures/transaction') var fixtures = require('./fixtures/transaction')
@ -96,20 +97,30 @@ describe('Transaction', function() {
assert.equal(tx.ins[0].sequence, Transaction.DEFAULT_SEQUENCE) assert.equal(tx.ins[0].sequence, Transaction.DEFAULT_SEQUENCE)
}) })
it('defaults to empty script', function() {
var tx = new Transaction()
tx.addInput(prevTxHash, 0)
assert.equal(tx.ins[0].script, Script.EMPTY)
})
fixtures.valid.forEach(function(f) { fixtures.valid.forEach(function(f) {
it('should add the inputs for ' + f.txid + ' correctly', function() { it('should add the inputs for ' + f.txid + ' correctly', function() {
var tx = new Transaction() var tx = new Transaction()
f.raw.ins.forEach(function(txIn, i) { f.raw.ins.forEach(function(txIn, i) {
var j = tx.addInput(txIn.hash, txIn.index, txIn.sequence) var script = txIn.script ? Script.fromHex(txIn.script) : Script.EMPTY
var j = tx.addInput(txIn.hash, txIn.index, txIn.sequence, script)
assert.equal(i, j) assert.equal(i, j)
assert.deepEqual(tx.ins[i].hash, txIn.hash) assert.deepEqual(tx.ins[i].hash, txIn.hash)
assert.equal(tx.ins[i].index, txIn.index) assert.equal(tx.ins[i].index, txIn.index)
var sequence = txIn.sequence var sequence = txIn.sequence
if (sequence == undefined) sequence = Transaction.DEFAULT_SEQUENCE if (sequence === undefined) sequence = Transaction.DEFAULT_SEQUENCE
assert.equal(tx.ins[i].sequence, sequence) assert.equal(tx.ins[i].sequence, sequence)
assert.equal(tx.ins[i].script, script)
}) })
}) })
}) })