From 01dc34d720d0bc207eeca83e92c3f2182a7f778d Mon Sep 17 00:00:00 2001 From: Wei Lu Date: Sat, 22 Mar 2014 15:37:03 +0800 Subject: [PATCH] throw error when unspent output does not have required keys --- src/wallet.js | 53 +++++++++++++++++++++++++++++++++++++++++--------- test/wallet.js | 26 +++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 9 deletions(-) diff --git a/src/wallet.js b/src/wallet.js index 44c592e..dcc8801 100644 --- a/src/wallet.js +++ b/src/wallet.js @@ -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) { diff --git a/test/wallet.js b/test/wallet.js index 549cc70..37aa4f7 100644 --- a/test/wallet.js +++ b/test/wallet.js @@ -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)