Browse Source

throw error when unspent output does not have required keys

hk-custom-address
Wei Lu 11 years ago
parent
commit
01dc34d720
  1. 53
      src/wallet.js
  2. 26
      test/wallet.js

53
src/wallet.js

@ -83,20 +83,55 @@ var Wallet = function (seed, options) {
this.setUnspentOutputs = function(utxo) { this.setUnspentOutputs = function(utxo) {
var outputs = {} var outputs = {}
utxo.forEach(function(o){ utxo.forEach(function(uo){
var hash = o.hash || convert.reverseEndian(o.hashLittleEndian) validateUnspentOutput(uo)
var key = hash + ":" + o.outputIndex var o = unspentOutputToOutput(uo)
outputs[key] = { outputs[o.output] = o
output: key,
scriptPubKey: o.scriptPubKey,
address: o.address,
value: o.value
}
}) })
this.outputs = outputs 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 // Processes a transaction object
// If "verified" is true, then we trust the transaction as "final" // If "verified" is true, then we trust the transaction as "final"
this.processTx = function(tx, verified) { this.processTx = function(tx, verified) {

26
test/wallet.js

@ -198,6 +198,32 @@ describe('Wallet', function() {
verifyOutputs() 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() { function verifyOutputs() {
var output = wallet.outputs[expectedOutputKey] var output = wallet.outputs[expectedOutputKey]
assert(output) assert(output)

Loading…
Cancel
Save