TxBuilder: use data fixtures for invalid tests
This commit is contained in:
parent
bcbcd58964
commit
36b225a3df
3 changed files with 49 additions and 55 deletions
|
@ -118,6 +118,7 @@ TransactionBuilder.prototype.build = function(allowIncomplete) {
|
||||||
if (!allowIncomplete) {
|
if (!allowIncomplete) {
|
||||||
assert(this.tx.ins.length > 0, 'Transaction has no inputs')
|
assert(this.tx.ins.length > 0, 'Transaction has no inputs')
|
||||||
assert(this.tx.outs.length > 0, 'Transaction has no outputs')
|
assert(this.tx.outs.length > 0, 'Transaction has no outputs')
|
||||||
|
assert(this.signatures.length > 0, 'Transaction has no signatures')
|
||||||
assert.equal(this.signatures.length, this.tx.ins.length, 'Transaction is missing signatures')
|
assert.equal(this.signatures.length, this.tx.ins.length, 'Transaction is missing signatures')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
46
test/fixtures/transaction_builder.json
vendored
46
test/fixtures/transaction_builder.json
vendored
|
@ -42,72 +42,62 @@
|
||||||
"build": [
|
"build": [
|
||||||
{
|
{
|
||||||
"exception": "Transaction has no inputs",
|
"exception": "Transaction has no inputs",
|
||||||
"hex": "",
|
"inputs": [],
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"script": "",
|
"script": "OP_DUP OP_HASH160 aa4d7985c57e011a8b3dd8e0e5a73aaef41629c5 OP_EQUALVERIFY OP_CHECKSIG",
|
||||||
"value": 1000
|
"value": 1000
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"exception": "Transaction has no outputs",
|
"exception": "Transaction has no outputs",
|
||||||
"hex": "",
|
|
||||||
"inputs": [
|
"inputs": [
|
||||||
{
|
{
|
||||||
"hash": "",
|
"index": 0,
|
||||||
"index": 0
|
"prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
|
||||||
|
"privKeys": []
|
||||||
}
|
}
|
||||||
]
|
],
|
||||||
|
"outputs": []
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"exception": "Transaction has no signatures",
|
"exception": "Transaction has no signatures",
|
||||||
"hex": "",
|
|
||||||
"inputs": [
|
"inputs": [
|
||||||
{
|
{
|
||||||
"hash": "",
|
"index": 0,
|
||||||
"index": 0
|
"prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
|
||||||
|
"privKeys": []
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"script": "",
|
"script": "OP_DUP OP_HASH160 aa4d7985c57e011a8b3dd8e0e5a73aaef41629c5 OP_EQUALVERIFY OP_CHECKSIG",
|
||||||
"value": 1000
|
"value": 1000
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"exception": "Transaction is missing signatures",
|
"exception": "Transaction is missing signatures",
|
||||||
"hex": "",
|
|
||||||
"inputs": [
|
"inputs": [
|
||||||
{
|
{
|
||||||
"hash": "",
|
"index": 0,
|
||||||
"index": 0
|
"prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
|
||||||
|
"privKeys": ["KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn"]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"hash": "",
|
"index": 1,
|
||||||
"index": 1
|
"prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
|
||||||
|
"privKeys": []
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"script": "",
|
"script": "OP_DUP OP_HASH160 aa4d7985c57e011a8b3dd8e0e5a73aaef41629c5 OP_EQUALVERIFY OP_CHECKSIG",
|
||||||
"value": 1000
|
"value": 1000
|
||||||
}
|
}
|
||||||
],
|
|
||||||
"signatures": [
|
|
||||||
{
|
|
||||||
"wif": "",
|
|
||||||
"index": 0
|
|
||||||
}
|
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
|
||||||
"fromTransaction": [
|
|
||||||
{
|
|
||||||
"exception": "Transaction contains unsupported script types",
|
|
||||||
"hex": ""
|
|
||||||
}
|
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -189,40 +189,43 @@ describe('TransactionBuilder', function() {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('throws if Transaction has no inputs', function() {
|
fixtures.invalid.build.forEach(function(f) {
|
||||||
txb.addOutput(privScript, value)
|
it('throws on ' + f.exception, function() {
|
||||||
|
f.inputs.forEach(function(input) {
|
||||||
|
var prevTx
|
||||||
|
if (input.prevTx.length === 64) {
|
||||||
|
prevTx = input.prevTx
|
||||||
|
} else {
|
||||||
|
prevTx = Transaction.fromHex(input.prevTx)
|
||||||
|
}
|
||||||
|
|
||||||
|
txb.addInput(prevTx, input.index)
|
||||||
|
})
|
||||||
|
|
||||||
|
f.outputs.forEach(function(output) {
|
||||||
|
var script = Script.fromASM(output.script)
|
||||||
|
|
||||||
|
txb.addOutput(script, output.value)
|
||||||
|
})
|
||||||
|
|
||||||
|
f.inputs.forEach(function(input, index) {
|
||||||
|
var redeemScript
|
||||||
|
|
||||||
|
if (input.redeemScript) {
|
||||||
|
redeemScript = Script.fromASM(input.redeemScript)
|
||||||
|
}
|
||||||
|
|
||||||
|
input.privKeys.forEach(function(wif) {
|
||||||
|
var privKey = ECKey.fromWIF(wif)
|
||||||
|
|
||||||
|
txb.sign(index, privKey, redeemScript)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
assert.throws(function() {
|
assert.throws(function() {
|
||||||
txb.build()
|
txb.build()
|
||||||
}, /Transaction has no inputs/)
|
}, new RegExp(f.exception))
|
||||||
})
|
})
|
||||||
|
|
||||||
it('throws if Transaction has no outputs', function() {
|
|
||||||
txb.addInput(prevTxHash, 0)
|
|
||||||
|
|
||||||
assert.throws(function() {
|
|
||||||
txb.build()
|
|
||||||
}, /Transaction has no outputs/)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('throws if Transaction has no signatures', function() {
|
|
||||||
txb.addInput(prevTxHash, 0)
|
|
||||||
txb.addOutput(privScript, value)
|
|
||||||
|
|
||||||
assert.throws(function() {
|
|
||||||
txb.build()
|
|
||||||
}, /Transaction is missing signatures/)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('throws if Transaction has not enough signatures', function() {
|
|
||||||
txb.addInput(prevTxHash, 0)
|
|
||||||
txb.addInput(prevTxHash, 1)
|
|
||||||
txb.addOutput(privScript, value)
|
|
||||||
txb.sign(0, privKey)
|
|
||||||
|
|
||||||
assert.throws(function() {
|
|
||||||
txb.build()
|
|
||||||
}, /Transaction is missing signatures/)
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Add table
Reference in a new issue