throw error when unspent output does not have required keys
This commit is contained in:
parent
26afbccc98
commit
01dc34d720
2 changed files with 70 additions and 9 deletions
|
@ -83,20 +83,55 @@ var Wallet = function (seed, options) {
|
|||
this.setUnspentOutputs = function(utxo) {
|
||||
var outputs = {}
|
||||
|
||||
utxo.forEach(function(o){
|
||||
var hash = o.hash || convert.reverseEndian(o.hashLittleEndian)
|
||||
var key = hash + ":" + o.outputIndex
|
||||
outputs[key] = {
|
||||
output: key,
|
||||
scriptPubKey: o.scriptPubKey,
|
||||
address: o.address,
|
||||
value: o.value
|
||||
}
|
||||
utxo.forEach(function(uo){
|
||||
validateUnspentOutput(uo)
|
||||
var o = unspentOutputToOutput(uo)
|
||||
outputs[o.output] = o
|
||||
})
|
||||
|
||||
this.outputs = outputs
|
||||
}
|
||||
|
||||
function unspentOutputToOutput(o) {
|
||||
var hash = o.hash || convert.reverseEndian(o.hashLittleEndian)
|
||||
var key = hash + ":" + o.outputIndex
|
||||
return {
|
||||
output: key,
|
||||
scriptPubKey: o.scriptPubKey,
|
||||
address: o.address,
|
||||
value: o.value
|
||||
}
|
||||
}
|
||||
|
||||
function validateUnspentOutput(uo) {
|
||||
var missingField;
|
||||
|
||||
if(isNullOrUndefined(uo.hash) && isNullOrUndefined(uo.hashLittleEndian)){
|
||||
missingField = "hash(or hashLittleEndian)"
|
||||
}
|
||||
|
||||
var requiredKeys = ['outputIndex', 'scriptPubKey', 'address', 'value']
|
||||
requiredKeys.forEach(function(key){
|
||||
if(isNullOrUndefined(uo[key])){
|
||||
missingField = key
|
||||
}
|
||||
})
|
||||
|
||||
if(missingField) {
|
||||
var message = [
|
||||
'Invalid unspent output: key', field, 'is missing.',
|
||||
'A valid unspent output must contain'
|
||||
]
|
||||
message.push(requiredKeys.join(', '))
|
||||
message.push("and hash(or hashLittleEndian)")
|
||||
throw new Error(message.join(' '))
|
||||
}
|
||||
}
|
||||
|
||||
function isNullOrUndefined(value){
|
||||
return value == undefined
|
||||
}
|
||||
|
||||
// Processes a transaction object
|
||||
// If "verified" is true, then we trust the transaction as "final"
|
||||
this.processTx = function(tx, verified) {
|
||||
|
|
|
@ -198,6 +198,32 @@ describe('Wallet', function() {
|
|||
verifyOutputs()
|
||||
})
|
||||
|
||||
describe('required fields', function(){
|
||||
it("throws an error when hash and hashLittleEndian are both missing", function(){
|
||||
delete utxo[0]['hash']
|
||||
delete utxo[0]['hashLittleEndian']
|
||||
|
||||
var errorMessage = 'Invalid unspent output: key hash(or hashLittleEndian) is missing. ' +
|
||||
'A valid unspent output must contain outputIndex, scriptPubKey, address, value and hash(or hashLittleEndian)'
|
||||
|
||||
assert.throws(function() {
|
||||
wallet.setUnspentOutputs(utxo)
|
||||
}, Error, errorMessage)
|
||||
});
|
||||
|
||||
['outputIndex', 'scriptPubKey', 'address', 'value'].forEach(function(field){
|
||||
it("throws an error when " + field + " is missing", function(){
|
||||
delete utxo[0][field]
|
||||
var errorMessage = 'Invalid unspent output: key ' + field +
|
||||
' is missing. A valid unspent output must contain outputIndex, scriptPubKey, address, value and hash(or hashLittleEndian)'
|
||||
|
||||
assert.throws(function() {
|
||||
wallet.setUnspentOutputs(utxo)
|
||||
}, Error, errorMessage)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
function verifyOutputs() {
|
||||
var output = wallet.outputs[expectedOutputKey]
|
||||
assert(output)
|
||||
|
|
Loading…
Reference in a new issue