From 8b3470e8cabe982f6fe40faac35e88271d33b9fc Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Wed, 13 Aug 2014 12:03:53 +1000 Subject: [PATCH] Wallet: move all free functions out of Wallet scope --- src/wallet.js | 137 +++++++++++++++++++++++++------------------------- 1 file changed, 68 insertions(+), 69 deletions(-) diff --git a/src/wallet.js b/src/wallet.js index dd492ba..b188808 100644 --- a/src/wallet.js +++ b/src/wallet.js @@ -86,59 +86,6 @@ function Wallet(seed, network) { this.outputs = outputs } - - function outputToUnspentOutput(output){ - var hashAndIndex = output.from.split(":") - - return { - hash: hashAndIndex[0], - outputIndex: parseInt(hashAndIndex[1]), - address: output.address, - value: output.value, - pending: output.pending - } - } - - function unspentOutputToOutput(o) { - var hash = o.hash - var key = hash + ":" + o.outputIndex - return { - from: key, - address: o.address, - value: o.value, - pending: o.pending - } - } - - function validateUnspentOutput(uo) { - var missingField - - if (isNullOrUndefined(uo.hash)) { - missingField = "hash" - } - - var requiredKeys = ['outputIndex', 'address', 'value'] - requiredKeys.forEach(function (key) { - if (isNullOrUndefined(uo[key])){ - missingField = key - } - }) - - if (missingField) { - var message = [ - 'Invalid unspent output: key', missingField, 'is missing.', - 'A valid unspent output must contain' - ] - message.push(requiredKeys.join(', ')) - message.push("and hash") - throw new Error(message.join(' ')) - } - } - - function isNullOrUndefined(value) { - return value == undefined - } - this.processPendingTx = function(tx){ processTx(tx, true) } @@ -193,7 +140,7 @@ function Wallet(seed, network) { this.createTx = function(to, value, fixedFee, changeAddress) { assert(value > network.dustThreshold, value + ' must be above dust threshold (' + network.dustThreshold + ' Satoshis)') - var utxos = getCandidateOutputs(value) + var utxos = getCandidateOutputs(this.outputs, value) var accum = 0 var subTotal = value var addresses = [] @@ -229,21 +176,6 @@ function Wallet(seed, network) { return tx } - function getCandidateOutputs() { - var unspent = [] - - for (var key in me.outputs) { - var output = me.outputs[key] - if (!output.pending) unspent.push(output) - } - - var sortByValueDesc = unspent.sort(function(o1, o2){ - return o2.value - o1.value - }) - - return sortByValueDesc - } - function estimateFeePadChangeOutput(tx) { var tmpTx = tx.clone() tmpTx.addOutput(getChangeAddress(), network.dustSoftThreshold || 0) @@ -305,4 +237,71 @@ function Wallet(seed, network) { } } +function outputToUnspentOutput(output){ + var hashAndIndex = output.from.split(":") + + return { + hash: hashAndIndex[0], + outputIndex: parseInt(hashAndIndex[1]), + address: output.address, + value: output.value, + pending: output.pending + } +} + +function unspentOutputToOutput(o) { + var hash = o.hash + var key = hash + ":" + o.outputIndex + return { + from: key, + address: o.address, + value: o.value, + pending: o.pending + } +} + +function validateUnspentOutput(uo) { + var missingField + + if (isNullOrUndefined(uo.hash)) { + missingField = "hash" + } + + var requiredKeys = ['outputIndex', 'address', 'value'] + requiredKeys.forEach(function (key) { + if (isNullOrUndefined(uo[key])){ + missingField = key + } + }) + + if (missingField) { + var message = [ + 'Invalid unspent output: key', missingField, 'is missing.', + 'A valid unspent output must contain' + ] + message.push(requiredKeys.join(', ')) + message.push("and hash") + throw new Error(message.join(' ')) + } +} + +function isNullOrUndefined(value) { + return value == undefined +} + +function getCandidateOutputs(outputs/*, value*/) { + var unspent = [] + + for (var key in outputs) { + var output = outputs[key] + if (!output.pending) unspent.push(output) + } + + var sortByValueDesc = unspent.sort(function(o1, o2){ + return o2.value - o1.value + }) + + return sortByValueDesc +} + module.exports = Wallet