Browse Source

Transaction: add hash as addInput parameter

hk-custom-address
Daniel Cousens 11 years ago
parent
commit
1b1b550bd6
  1. 12
      src/transaction.js
  2. 19
      test/transaction.js

12
src/transaction.js

@ -39,15 +39,17 @@ Transaction.prototype.addInput = function(tx, index, sequence) {
if (typeof tx === 'string') { if (typeof tx === 'string') {
hash = new Buffer(tx, 'hex') hash = new Buffer(tx, 'hex')
assert.equal(hash.length, 32, 'Expected Transaction or string, got ' + tx) assert.equal(hash.length, 32, 'Expected Transaction, txId or txHash, got ' + tx)
// TxHash hex is big-endian, we need little-endian // TxId hex is big-endian, we need little-endian
Array.prototype.reverse.call(hash) Array.prototype.reverse.call(hash)
} else { } else if (tx instanceof Transaction) {
assert(tx instanceof Transaction, 'Expected Transaction or string, got ' + tx) hash = tx.getHash()
hash = crypto.hash256(tx.toBuffer())
} else {
assert(Buffer.isBuffer(tx), 'Expected Transaction, txId or txHash, got ' + tx)
hash = tx
} }
assert.equal(typeof index, 'number', 'Expected number index, got ' + index) assert.equal(typeof index, 'number', 'Expected number index, got ' + index)

19
test/transaction.js

@ -69,6 +69,13 @@ describe('Transaction', function() {
assert.deepEqual(tx.ins[0].hash, prevTxHash) assert.deepEqual(tx.ins[0].hash, prevTxHash)
}) })
it('accepts a transaction hash', function() {
var tx = new Transaction()
tx.addInput(prevTxHash, 0)
assert.deepEqual(tx.ins[0].hash, prevTxHash)
})
it('accepts a Transaction object', function() { it('accepts a Transaction object', function() {
var tx = new Transaction() var tx = new Transaction()
tx.addInput(prevTx, 0) tx.addInput(prevTx, 0)
@ -78,13 +85,13 @@ describe('Transaction', function() {
it('returns an index', function() { it('returns an index', function() {
var tx = new Transaction() var tx = new Transaction()
assert.equal(tx.addInput(prevTxId, 0), 0) assert.equal(tx.addInput(prevTxHash, 0), 0)
assert.equal(tx.addInput(prevTxId, 0), 1) assert.equal(tx.addInput(prevTxHash, 0), 1)
}) })
it('defaults to DEFAULT_SEQUENCE', function() { it('defaults to DEFAULT_SEQUENCE', function() {
var tx = new Transaction() var tx = new Transaction()
tx.addInput(prevTxId, 0) tx.addInput(prevTxHash, 0)
assert.equal(tx.ins[0].sequence, 0xffffffff) assert.equal(tx.ins[0].sequence, 0xffffffff)
}) })
@ -94,11 +101,7 @@ describe('Transaction', function() {
var tx = new Transaction() var tx = new Transaction()
f.raw.ins.forEach(function(txIn, i) { f.raw.ins.forEach(function(txIn, i) {
var txInId = new Buffer(txIn.hash) var j = tx.addInput(txIn.hash, txIn.index, txIn.sequence)
Array.prototype.reverse.call(txInId)
txInId = txInId.toString('hex')
var j = tx.addInput(txInId, txIn.index, txIn.sequence)
assert.equal(i, j) assert.equal(i, j)
assert.deepEqual(tx.ins[i].hash, txIn.hash) assert.deepEqual(tx.ins[i].hash, txIn.hash)

Loading…
Cancel
Save