throw error when unspent output does not have required keys

This commit is contained in:
Wei Lu 2014-03-22 15:37:03 +08:00
parent 26afbccc98
commit 01dc34d720
2 changed files with 70 additions and 9 deletions

View file

@ -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) {