Browse Source

Merge pull request #214 from dcousens/txtests

Transaction tests
hk-custom-address
Wei Lu 10 years ago
parent
commit
562a492079
  1. 45
      src/transaction.js
  2. 188
      test/fixtures/transaction.json
  3. 316
      test/transaction.js

45
src/transaction.js

@ -9,11 +9,11 @@ var ECKey = require('./eckey')
var ECSignature = require('./ecsignature') var ECSignature = require('./ecsignature')
var Script = require('./script') var Script = require('./script')
var DEFAULT_SEQUENCE = 0xffffffff Transaction.DEFAULT_SEQUENCE = 0xffffffff
var SIGHASH_ALL = 0x01 Transaction.SIGHASH_ALL = 0x01
var SIGHASH_NONE = 0x02 Transaction.SIGHASH_NONE = 0x02
var SIGHASH_SINGLE = 0x03 Transaction.SIGHASH_SINGLE = 0x03
var SIGHASH_ANYONECANPAY = 0x80 Transaction.SIGHASH_ANYONECANPAY = 0x80
function Transaction() { function Transaction() {
this.version = 1 this.version = 1
@ -32,29 +32,33 @@ function Transaction() {
* *
* Note that this method does not sign the created input. * Note that this method does not sign the created input.
*/ */
Transaction.prototype.addInput = function(tx, index) { Transaction.prototype.addInput = function(tx, index, sequence) {
if (sequence == undefined) sequence = Transaction.DEFAULT_SEQUENCE
var hash var hash
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)
// 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 {
hash = tx
} }
assert(Buffer.isBuffer(hash), 'Expected Transaction, txId or txHash, got ' + tx)
assert.equal(hash.length, 32, 'Expected hash length of 32, got ' + hash.length)
assert.equal(typeof index, 'number', 'Expected number index, got ' + index) assert.equal(typeof index, 'number', 'Expected number index, got ' + index)
return (this.ins.push({ return (this.ins.push({
hash: hash, hash: hash,
index: index, index: index,
script: Script.EMPTY, script: Script.EMPTY,
sequence: DEFAULT_SEQUENCE sequence: sequence
}) - 1) }) - 1)
} }
@ -140,7 +144,6 @@ Transaction.prototype.toBuffer = function () {
}) })
writeUInt32(this.locktime) writeUInt32(this.locktime)
assert.equal(offset, buffer.length, 'Invalid transaction object')
return buffer return buffer
} }
@ -172,15 +175,15 @@ Transaction.prototype.hashForSignature = function(prevOutScript, inIndex, hashTy
txTmp.ins[inIndex].script = hashScript txTmp.ins[inIndex].script = hashScript
var hashTypeModifier = hashType & 0x1f var hashTypeModifier = hashType & 0x1f
if (hashTypeModifier === SIGHASH_NONE) { if (hashTypeModifier === Transaction.SIGHASH_NONE) {
assert(false, 'SIGHASH_NONE not yet supported') assert(false, 'SIGHASH_NONE not yet supported')
} else if (hashTypeModifier === SIGHASH_SINGLE) { } else if (hashTypeModifier === Transaction.SIGHASH_SINGLE) {
assert(false, 'SIGHASH_SINGLE not yet supported') assert(false, 'SIGHASH_SINGLE not yet supported')
} }
if (hashType & SIGHASH_ANYONECANPAY) { if (hashType & Transaction.SIGHASH_ANYONECANPAY) {
assert(false, 'SIGHASH_ANYONECANPAY not yet supported') assert(false, 'SIGHASH_ANYONECANPAY not yet supported')
} }
@ -191,8 +194,12 @@ Transaction.prototype.hashForSignature = function(prevOutScript, inIndex, hashTy
return crypto.hash256(buffer) return crypto.hash256(buffer)
} }
Transaction.prototype.getHash = function () {
return crypto.hash256(this.toBuffer())
}
Transaction.prototype.getId = function () { Transaction.prototype.getId = function () {
var buffer = crypto.hash256(this.toBuffer()) var buffer = this.getHash()
// Big-endian is used for TxHash // Big-endian is used for TxHash
Array.prototype.reverse.call(buffer) Array.prototype.reverse.call(buffer)
@ -278,7 +285,7 @@ Transaction.fromBuffer = function(buffer) {
} }
tx.locktime = readUInt32() tx.locktime = readUInt32()
assert.equal(offset, buffer.length, 'Invalid transaction') assert.equal(offset, buffer.length, 'Transaction has unexpected data')
return tx return tx
} }
@ -300,7 +307,7 @@ Transaction.prototype.sign = function(index, privKey, hashType) {
} }
Transaction.prototype.signInput = function(index, prevOutScript, privKey, hashType) { Transaction.prototype.signInput = function(index, prevOutScript, privKey, hashType) {
hashType = hashType || SIGHASH_ALL hashType = hashType || Transaction.SIGHASH_ALL
var hash = this.hashForSignature(prevOutScript, index, hashType) var hash = this.hashForSignature(prevOutScript, index, hashType)
var signature = privKey.sign(hash) var signature = privKey.sign(hash)

188
test/fixtures/transaction.json

File diff suppressed because one or more lines are too long

316
test/transaction.js

@ -5,198 +5,224 @@ 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 fixtureTxes = require('./fixtures/mainnet_tx') var fixtures = require('./fixtures/transaction')
var fixtureTx1Hex = fixtureTxes.prevTx
var fixtureTx2Hex = fixtureTxes.tx
var fixtureTxBigHex = fixtureTxes.bigTx
describe('Transaction', function() { // FIXME: what is a better way to do this, seems a bit odd
describe('toBuffer', function() { fixtures.valid.forEach(function(f) {
it('matches the expected output', function() { var Script = require('../src/script')
var expected = '010000000189632848f99722915727c5c75da8db2dbf194342a0429828f66ff88fab2af7d600000000fd1b0100483045022100e5be20d440b2bbbc886161f9095fa6d0bca749a4e41d30064f30eb97adc7a1f5022061af132890d8e4e90fedff5e9365aeeb77021afd8ef1d5c114d575512e9a130a0147304402205054e38e9d7b5c10481b6b4991fde5704cd94d49e344406e3c2ce4d18a43bf8e022051d7ba8479865b53a48bee0cce86e89a25633af5b2918aa276859489e232f51c014c8752410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b84104c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a52aeffffffff0101000000000000001976a914751e76e8199196d454941c45d1b3a323f1433bd688ac00000000'
var actual = Transaction.fromHex(expected).toHex() f.raw.ins.forEach(function(fin) {
fin.hash = new Buffer(fin.hash, 'hex')
fin.script = Script.fromHex(fin.script)
})
assert.equal(actual, expected) f.raw.outs.forEach(function(fout) {
}) fout.script = Script.fromHex(fout.script)
}) })
})
describe('fromBuffer', function() { describe('Transaction', function() {
var tx, serializedTx describe('fromBuffer/fromHex', function() {
beforeEach(function() { fixtures.valid.forEach(function(f) {
serializedTx = '0100000001344630cbff61fbc362f7e1ff2f11a344c29326e4ee96e787dc0d4e5cc02fd069000000004a493046022100ef89701f460e8660c80808a162bbf2d676f40a331a243592c36d6bd1f81d6bdf022100d29c072f1b18e59caba6e1f0b8cadeb373fd33a25feded746832ec179880c23901ffffffff0100f2052a010000001976a914dd40dedd8f7e37466624c4dacc6362d8e7be23dd88ac00000000' it('imports ' + f.txid + ' correctly', function() {
tx = Transaction.fromHex(serializedTx) var actual = Transaction.fromHex(f.hex)
})
it('does not mutate the input buffer', function() { assert.deepEqual(actual, f.raw)
var buffer = new Buffer(serializedTx, 'hex') })
Transaction.fromBuffer(buffer) })
assert.equal(buffer.toString('hex'), serializedTx) fixtures.invalid.fromBuffer.forEach(function(f) {
it('throws on ' + f.exception, function() {
assert.throws(function() {
Transaction.fromHex(f.hex)
}, new RegExp(f.exception))
})
}) })
})
describe('toBuffer/toHex', function() {
fixtures.valid.forEach(function(f) {
it('exports ' + f.txid + ' correctly', function() {
var actual = Transaction.prototype.toBuffer.call(f.raw)
it('decodes version correctly', function() { assert.equal(actual.toString('hex'), f.hex)
assert.equal(tx.version, 1) })
}) })
})
it('decodes locktime correctly', function() { describe('addInput', function() {
assert.equal(tx.locktime, 0) // FIXME: not as pretty as could be
// Probably a bit representative of the API
var prevTxHash, prevTxId, prevTx
beforeEach(function() {
var f = fixtures.valid[0]
prevTx = Transaction.fromHex(f.hex)
prevTxHash = prevTx.getHash()
prevTxId = prevTx.getId()
}) })
it('decodes inputs correctly', function() { it('accepts a transaction id', function() {
assert.equal(tx.ins.length, 1) var tx = new Transaction()
tx.addInput(prevTxId, 0)
var input = tx.ins[0] assert.deepEqual(tx.ins[0].hash, prevTxHash)
assert.equal(input.sequence, 4294967295) })
assert.equal(input.index, 0) it('accepts a transaction hash', function() {
assert.equal(input.hash.toString('hex'), "344630cbff61fbc362f7e1ff2f11a344c29326e4ee96e787dc0d4e5cc02fd069") var tx = new Transaction()
tx.addInput(prevTxHash, 0)
assert.equal(input.script.toHex(), "493046022100ef89701f460e8660c80808a162bbf2d676f40a331a243592c36d6bd1f81d6bdf022100d29c072f1b18e59caba6e1f0b8cadeb373fd33a25feded746832ec179880c23901") assert.deepEqual(tx.ins[0].hash, prevTxHash)
}) })
it('decodes outputs correctly', function() { it('accepts a Transaction object', function() {
assert.equal(tx.outs.length, 1) var tx = new Transaction()
tx.addInput(prevTx, 0)
var output = tx.outs[0]
assert.equal(output.value, 5000000000) assert.deepEqual(tx.ins[0].hash, prevTxHash)
assert.deepEqual(output.script, Address.fromBase58Check('n1gqLjZbRH1biT5o4qiVMiNig8wcCPQeB9').toOutputScript())
}) })
it('decodes large inputs correctly', function() { it('returns an index', function() {
// transaction has only 1 input
var tx = new Transaction() var tx = new Transaction()
tx.addInput("0cb859105100ebc3344f749c835c7af7d7103ec0d8cbc3d8ccbd5d28c3c36b57", 0) assert.equal(tx.addInput(prevTxHash, 0), 0)
tx.addOutput("15mMHKL96tWAUtqF3tbVf99Z8arcmnJrr3", 100) assert.equal(tx.addInput(prevTxHash, 0), 1)
})
var buffer = tx.toBuffer()
// we're going to replace the 8bit VarInt for tx.ins.length with a stretched 32bit equivalent it('defaults to DEFAULT_SEQUENCE', function() {
var mutated = Buffer.concat([ var tx = new Transaction()
buffer.slice(0, 4), tx.addInput(prevTxHash, 0)
new Buffer([254, 1, 0, 0, 0]),
buffer.slice(5)
])
// the deserialized-serialized transaction should return to its non-mutated state (== tx) assert.equal(tx.ins[0].sequence, Transaction.DEFAULT_SEQUENCE)
var buffer2 = Transaction.fromBuffer(mutated).toBuffer()
assert.deepEqual(buffer, buffer2)
}) })
})
describe('creating a transaction', function() { fixtures.valid.forEach(function(f) {
var tx, prevTx it('should add the inputs for ' + f.txid + ' correctly', function() {
beforeEach(function() { var tx = new Transaction()
prevTx = Transaction.fromHex(fixtureTx1Hex)
tx = new Transaction()
})
describe('addInput', function() { f.raw.ins.forEach(function(txIn, i) {
it('accepts a transaction hash', function() { var j = tx.addInput(txIn.hash, txIn.index, txIn.sequence)
var prevTxHash = prevTx.getId()
tx.addInput(prevTxHash, 0) assert.equal(i, j)
verifyTransactionIn() assert.deepEqual(tx.ins[i].hash, txIn.hash)
}) assert.equal(tx.ins[i].index, txIn.index)
it('accepts a Transaction object', function() { var sequence = txIn.sequence
tx.addInput(prevTx, 0) if (sequence == undefined) sequence = Transaction.DEFAULT_SEQUENCE
verifyTransactionIn() assert.equal(tx.ins[i].sequence, sequence)
})
}) })
})
it('returns an index', function() { fixtures.invalid.addInput.forEach(function(f) {
assert.equal(tx.addInput(prevTx, 0), 0) it('throws on ' + f.exception, function() {
assert.equal(tx.addInput(prevTx, 0), 1) var tx = new Transaction()
}) var hash = new Buffer(f.hash, 'hex')
function verifyTransactionIn() { assert.throws(function() {
assert.equal(tx.ins.length, 1) tx.addInput(hash, f.index)
}, new RegExp(f.exception))
})
})
})
var input = tx.ins[0] describe('addOutput', function() {
assert.equal(input.sequence, 4294967295) // FIXME: not as pretty as could be
// Probably a bit representative of the API
var destAddressB58, destAddress, destScript
beforeEach(function() {
destAddressB58 = '15mMHKL96tWAUtqF3tbVf99Z8arcmnJrr3'
destAddress = Address.fromBase58Check(destAddressB58)
destScript = destAddress.toOutputScript()
})
assert.equal(input.index, 0) it('accepts an address string', function() {
assert.equal(input.hash.toString('hex'), "576bc3c3285dbdccd8c3cbd8c03e10d7f77a5c839c744f34c3eb00511059b80c") var tx = new Transaction()
tx.addOutput(destAddressB58, 40000)
assert.equal(input.script, Script.EMPTY) assert.deepEqual(tx.outs[0].script, destScript)
} assert.equal(tx.outs[0].value, 40000)
}) })
describe('addOutput', function() { it('accepts an Address', function() {
it('accepts an address string', function() { var tx = new Transaction()
var dest = '15mMHKL96tWAUtqF3tbVf99Z8arcmnJrr3' tx.addOutput(destAddress, 40000)
tx.addOutput(dest, 40000)
verifyTransactionOut()
})
it('accepts an Address', function() { assert.deepEqual(tx.outs[0].script, destScript)
var dest = Address.fromBase58Check('15mMHKL96tWAUtqF3tbVf99Z8arcmnJrr3') assert.equal(tx.outs[0].value, 40000)
})
tx.addOutput(dest, 40000) it('accepts a scriptPubKey', function() {
verifyTransactionOut() var tx = new Transaction()
}) tx.addOutput(destScript, 40000)
it('accepts a scriptPubKey', function() { assert.deepEqual(tx.outs[0].script, destScript)
var dest = Address.fromBase58Check('15mMHKL96tWAUtqF3tbVf99Z8arcmnJrr3').toOutputScript() assert.equal(tx.outs[0].value, 40000)
})
tx.addOutput(dest, 40000) it('returns an index', function() {
verifyTransactionOut() var tx = new Transaction()
}) assert.equal(tx.addOutput(destScript, 40000), 0)
assert.equal(tx.addOutput(destScript, 40000), 1)
})
it('returns an index', function() { fixtures.valid.forEach(function(f) {
var dest = '15mMHKL96tWAUtqF3tbVf99Z8arcmnJrr3' it('should add the outputs for ' + f.txid + ' correctly', function() {
var tx = new Transaction()
assert.equal(tx.addOutput(dest, 40000), 0) f.raw.outs.forEach(function(txOut, i) {
assert.equal(tx.addOutput(dest, 40000), 1) var j = tx.addOutput(txOut.script, txOut.value)
})
function verifyTransactionOut() { assert.equal(i, j)
assert.equal(tx.outs.length, 1) })
var output = tx.outs[0] assert.deepEqual(tx.outs, f.raw.outs)
assert.equal(output.value, 40000) })
assert.equal(output.script.toHex(), "76a9143443bc45c560866cfeabf1d52f50a6ed358c69f288ac")
}
}) })
})
describe('sign', function() { describe('clone', function() {
it('works', function() { fixtures.valid.forEach(function(f) {
tx.addInput("0cb859105100ebc3344f749c835c7af7d7103ec0d8cbc3d8ccbd5d28c3c36b57", 0) var expected = Transaction.fromHex(f.hex)
tx.addOutput("15mMHKL96tWAUtqF3tbVf99Z8arcmnJrr3", 40000) var actual = expected.clone()
tx.addOutput("1Bu3bhwRmevHLAy1JrRB6AfcxfgDG2vXRd", 50000)
var key = ECKey.fromWIF('L44f7zxJ5Zw4EK9HZtyAnzCYz2vcZ5wiJf9AuwhJakiV4xVkxBeb')
tx.sign(0, key)
var script = prevTx.outs[0].script it('should have value equality', function() {
var sig = new Buffer(tx.ins[0].script.chunks[0]) assert.deepEqual(actual, expected)
})
assert.equal(tx.validateInput(0, script, key.pub, sig), true) it('should not have reference equality', function() {
assert.notEqual(actual, expected)
}) })
}) })
})
describe('validateInput', function() { describe('getId', function() {
var validTx fixtures.valid.forEach(function(f) {
it('should return the txid for ' + f.txid, function() {
var tx = Transaction.fromHex(f.hex)
var actual = tx.getId()
beforeEach(function() { assert.equal(actual, f.txid)
validTx = Transaction.fromHex(fixtureTx2Hex)
}) })
})
})
it('returns true for valid signature', function() { describe('getHash', function() {
var key = ECKey.fromWIF('L44f7zxJ5Zw4EK9HZtyAnzCYz2vcZ5wiJf9AuwhJakiV4xVkxBeb') fixtures.valid.forEach(function(f) {
var script = prevTx.outs[0].script it('should return the hash for ' + f.txid, function() {
var sig = new Buffer(validTx.ins[0].script.chunks[0]) var tx = Transaction.fromHex(f.hex)
var actual = tx.getHash().toString('hex')
assert.equal(validTx.validateInput(0, script, key.pub, sig), true) assert.equal(actual, f.hash)
}) })
}) })
}) })
describe('signInput', function() { // TODO:
// hashForSignature: [Function],
// FIXME: could be better
describe('signInput/validateInput', function() {
it('works for multi-sig redeem script', function() { it('works for multi-sig redeem script', function() {
var tx = new Transaction() var tx = new Transaction()
tx.addInput('d6f72aab8ff86ff6289842a0424319bf2ddba85dc7c52757912297f948286389', 0) tx.addInput('d6f72aab8ff86ff6289842a0424319bf2ddba85dc7c52757912297f948286389', 0)
@ -227,32 +253,4 @@ describe('Transaction', function() {
assert.equal(tx.toHex(), expected) assert.equal(tx.toHex(), expected)
}) })
}) })
describe('getId', function() {
it('returns the expected txid', function() {
var tx = new Transaction()
tx.addInput('d6f72aab8ff86ff6289842a0424319bf2ddba85dc7c52757912297f948286389', 0)
tx.addOutput('mrCDrCybB6J1vRfbwM5hemdJz73FwDBC8r', 1)
assert.equal(tx.getId(), '7c3275f1212fd1a2add614f47a1f1f7b6d9570a97cb88e0e2664ab1752976e9f')
})
})
describe('clone', function() {
it('creates a new object', function() {
var txA = new Transaction()
txA.addInput('d6f72aab8ff86ff6289842a0424319bf2ddba85dc7c52757912297f948286389', 0)
txA.addOutput('mrCDrCybB6J1vRfbwM5hemdJz73FwDBC8r', 1000)
var txB = txA.clone()
// Enforce value equality
assert.deepEqual(txA, txB)
// Enforce reference inequality
assert.notEqual(txA.ins[0], txB.ins[0])
assert.notEqual(txA.outs[0], txB.outs[0])
})
})
}) })

Loading…
Cancel
Save