Wallet: move all free functions out of Wallet scope
This commit is contained in:
parent
d618aa9822
commit
8b3470e8ca
1 changed files with 68 additions and 69 deletions
137
src/wallet.js
137
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
|
||||
|
|
Loading…
Reference in a new issue