remove old methods
This commit is contained in:
parent
471bc7ed97
commit
2501868f52
1 changed files with 16 additions and 80 deletions
|
@ -147,8 +147,6 @@ var Wallet = function (seed, options) {
|
||||||
value: txOut.value,
|
value: txOut.value,
|
||||||
address: address,
|
address: address,
|
||||||
scriptPubKey: txOut.scriptPubKey()
|
scriptPubKey: txOut.scriptPubKey()
|
||||||
// timestamp: new Date().getTime() / 1000,
|
|
||||||
// pending: true
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -158,26 +156,10 @@ var Wallet = function (seed, options) {
|
||||||
var o = me.outputs[op.hash+':'+op.index]
|
var o = me.outputs[op.hash+':'+op.index]
|
||||||
if (o) {
|
if (o) {
|
||||||
o.spend = txhash+':'+i
|
o.spend = txhash+':'+i
|
||||||
// o.spendpending = true
|
|
||||||
// o.timestamp = new Date().getTime() / 1000
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function getCandidateOutputs(value){
|
|
||||||
var unspent = []
|
|
||||||
for (var key in me.outputs){
|
|
||||||
var output = me.outputs[key]
|
|
||||||
if(!output.value.spend) unspent.push(output)
|
|
||||||
}
|
|
||||||
|
|
||||||
var sortByValueDesc = unspent.sort(function(o1, o2){
|
|
||||||
return o2.value - o1.value
|
|
||||||
})
|
|
||||||
|
|
||||||
return sortByValueDesc;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.createTx = function(to, value, fixedFee) {
|
this.createTx = function(to, value, fixedFee) {
|
||||||
checkDust(value)
|
checkDust(value)
|
||||||
|
|
||||||
|
@ -221,11 +203,18 @@ var Wallet = function (seed, options) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkInsufficientFund(totalInValue, value, fee) {
|
function getCandidateOutputs(value){
|
||||||
if(totalInValue < value + fee) {
|
var unspent = []
|
||||||
throw new Error('Not enough money to send funds including transaction fee. Have: ' +
|
for (var key in me.outputs){
|
||||||
totalInValue + ', needed: ' + (value + fee))
|
var output = me.outputs[key]
|
||||||
|
if(!output.value.spend) unspent.push(output)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var sortByValueDesc = unspent.sort(function(o1, o2){
|
||||||
|
return o2.value - o1.value
|
||||||
|
})
|
||||||
|
|
||||||
|
return sortByValueDesc;
|
||||||
}
|
}
|
||||||
|
|
||||||
function estimateFeePadChangeOutput(tx){
|
function estimateFeePadChangeOutput(tx){
|
||||||
|
@ -239,64 +228,11 @@ var Wallet = function (seed, options) {
|
||||||
return me.changeAddresses[me.changeAddresses.length - 1]
|
return me.changeAddresses[me.changeAddresses.length - 1]
|
||||||
}
|
}
|
||||||
|
|
||||||
this.getUtxoToPay = function(value) {
|
function checkInsufficientFund(totalInValue, value, fee) {
|
||||||
var h = []
|
if(totalInValue < value + fee) {
|
||||||
for (var out in this.outputs) h.push(this.outputs[out])
|
throw new Error('Not enough money to send funds including transaction fee. Have: ' +
|
||||||
var utxo = h.filter(function(x) { return !x.spend });
|
totalInValue + ', needed: ' + (value + fee))
|
||||||
var valuecompare = function(a,b) { return a.value > b.value; }
|
|
||||||
var high = utxo.filter(function(o) { return o.value >= value; })
|
|
||||||
.sort(valuecompare);
|
|
||||||
if (high.length > 0) return [high[0]];
|
|
||||||
utxo.sort(valuecompare);
|
|
||||||
var totalval = 0;
|
|
||||||
for (var i = 0; i < utxo.length; i++) {
|
|
||||||
totalval += utxo[i].value;
|
|
||||||
if (totalval >= value) return utxo.slice(0,i+1);
|
|
||||||
}
|
}
|
||||||
throw ("Not enough money to send funds including transaction fee. Have: "
|
|
||||||
+ (totalval / 100000000) + ", needed: " + (value / 100000000));
|
|
||||||
}
|
|
||||||
|
|
||||||
this.mkSend = function(to, value, fee) {
|
|
||||||
var utxo = this.getUtxoToPay(value + fee)
|
|
||||||
var sum = utxo.reduce(function(t,o) { return t + o.value },0),
|
|
||||||
remainder = sum - value - fee
|
|
||||||
if (value < 5430) throw new Error("Amount below dust threshold!")
|
|
||||||
var unspentOuts = 0;
|
|
||||||
for (var o in this.outputs) {
|
|
||||||
if (!this.outputs[o].spend) unspentOuts += 1
|
|
||||||
if (unspentOuts >= 5) return
|
|
||||||
}
|
|
||||||
var change = this.addresses[this.addresses.length - 1]
|
|
||||||
var toOut = { address: to, value: value },
|
|
||||||
changeOut = { address: change, value: remainder }
|
|
||||||
halfChangeOut = { address: change, value: Math.floor(remainder/2) };
|
|
||||||
|
|
||||||
var outs =
|
|
||||||
remainder < 5430 ? [toOut]
|
|
||||||
: remainder < 10860 ? [toOut, changeOut]
|
|
||||||
: unspentOuts == 5 ? [toOut, changeOut]
|
|
||||||
: [toOut, halfChangeOut, halfChangeOut]
|
|
||||||
|
|
||||||
var tx = new Bitcoin.Transaction({
|
|
||||||
ins: utxo.map(function(x) { return x.receive }),
|
|
||||||
outs: outs
|
|
||||||
})
|
|
||||||
this.sign(tx)
|
|
||||||
return tx
|
|
||||||
}
|
|
||||||
|
|
||||||
this.mkSendToOutputs = function(outputs, changeIndex, fee) {
|
|
||||||
var value = outputs.reduce(function(t,o) { return t + o.value },0),
|
|
||||||
utxo = this.getUtxoToPay(value + fee),
|
|
||||||
sum = utxo.reduce(function(t,p) { return t + o.value },0);
|
|
||||||
utxo[changeIndex].value += sum - value - fee;
|
|
||||||
var tx = new Bitcoin.Transaction({
|
|
||||||
ins: utxo.map(function(x) { return x.receive }),
|
|
||||||
outs: outputs
|
|
||||||
})
|
|
||||||
this.sign(tx)
|
|
||||||
return tx
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.sign = function(tx) {
|
this.sign = function(tx) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue