From b4fa033e7e9398b8bb55fdff3602c17a7cf8ba20 Mon Sep 17 00:00:00 2001 From: AliAshrafD Date: Sun, 25 Apr 2021 01:55:45 +0430 Subject: [PATCH 1/6] Update coin.js --- js/coin.js | 46 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 7 deletions(-) diff --git a/js/coin.js b/js/coin.js index 1ba2682..7d32e97 100644 --- a/js/coin.js +++ b/js/coin.js @@ -746,9 +746,41 @@ r.parse(); return r; } + + coinjs.cpmareUnspents = function( unspent1, unspent2){ + return ( unspent1.amount - unspent2.amount); + } + + coinjs.partition = function(arr, start, end, compare){ + const pivot = arr[end]; + let pIndex = start; + for (let i = start; i < end; i++) { + if (compare(arr[i] , pivot) < 0 ) { + // Swap + [arr[i], arr[pivotIndex]] = [arr[pivotIndex], arr[i]]; + pIndex++; + } + } + + [arr[pIndex], arr[end]] = [arr[end], arr[pIndex]] + return pivotIndex; +}; + +coinjs.qSort = function(arr, start, end, compare) { + // terminating condition + if (start >= end) { + return; + } + let index = partition(arr, start, end, compare); + + // Recursively apply the same logic to the left and right subarrays + qSort(arr, start, index - 1, compare); + qSort(arr, index + 1, end, compare); +} - /* start of transaction functions */ + /* start of transaction functions */ + /* create a new transaction object */ coinjs.transaction = function() { @@ -841,7 +873,7 @@ } /* add unspent to transaction */ - r.addUnspent = function(address, callback, script, sequence){ + r.addUnspent = function(address, amount, callback, script, sequence){ var self = this; this.listUnspent(address, function(data){ @@ -852,9 +884,9 @@ var x = {}; var unspent = JSON.parse(data); - console.log("Unspent is"+unspent); - - for(i=0;i<=unspent.length-1;i++){ + console.log("Unspent is:"+unspent); + qSort(unspent, 0, unspent.length, compareUnspents); + for(i=0;i<=unspent.length-1 && value < amount;i++){ var txhash = unspent[i].txid, n = unspent[i].vout, scr = unspent[i].scriptPubKey, @@ -874,10 +906,10 @@ } /* add unspent and sign */ - r.addUnspentAndSign = function(wif, callback){ + r.addUnspentAndSign = function(wif, amount, callback){ var self = this; var address = coinjs.wif2address(wif); - self.addUnspent(address['address'], function(data){ + self.addUnspent(address['address'], amount, function(data){ self.sign(wif); return callback(data); }); From 5964bff1542fb51a5a61269da6da8cf966c063ad Mon Sep 17 00:00:00 2001 From: AliAshrafD Date: Sun, 25 Apr 2021 13:36:06 +0430 Subject: [PATCH 2/6] Revert "minmize number of inputs " --- js/coin.js | 46 +++++++--------------------------------------- 1 file changed, 7 insertions(+), 39 deletions(-) diff --git a/js/coin.js b/js/coin.js index 7d32e97..1ba2682 100644 --- a/js/coin.js +++ b/js/coin.js @@ -746,41 +746,9 @@ r.parse(); return r; } - - coinjs.cpmareUnspents = function( unspent1, unspent2){ - return ( unspent1.amount - unspent2.amount); - } - - coinjs.partition = function(arr, start, end, compare){ - const pivot = arr[end]; - let pIndex = start; - for (let i = start; i < end; i++) { - if (compare(arr[i] , pivot) < 0 ) { - // Swap - [arr[i], arr[pivotIndex]] = [arr[pivotIndex], arr[i]]; - pIndex++; - } - } - - [arr[pIndex], arr[end]] = [arr[end], arr[pIndex]] - return pivotIndex; -}; - -coinjs.qSort = function(arr, start, end, compare) { - // terminating condition - if (start >= end) { - return; - } - let index = partition(arr, start, end, compare); - - // Recursively apply the same logic to the left and right subarrays - qSort(arr, start, index - 1, compare); - qSort(arr, index + 1, end, compare); -} - /* start of transaction functions */ - + /* create a new transaction object */ coinjs.transaction = function() { @@ -873,7 +841,7 @@ coinjs.qSort = function(arr, start, end, compare) { } /* add unspent to transaction */ - r.addUnspent = function(address, amount, callback, script, sequence){ + r.addUnspent = function(address, callback, script, sequence){ var self = this; this.listUnspent(address, function(data){ @@ -884,9 +852,9 @@ coinjs.qSort = function(arr, start, end, compare) { var x = {}; var unspent = JSON.parse(data); - console.log("Unspent is:"+unspent); - qSort(unspent, 0, unspent.length, compareUnspents); - for(i=0;i<=unspent.length-1 && value < amount;i++){ + console.log("Unspent is"+unspent); + + for(i=0;i<=unspent.length-1;i++){ var txhash = unspent[i].txid, n = unspent[i].vout, scr = unspent[i].scriptPubKey, @@ -906,10 +874,10 @@ coinjs.qSort = function(arr, start, end, compare) { } /* add unspent and sign */ - r.addUnspentAndSign = function(wif, amount, callback){ + r.addUnspentAndSign = function(wif, callback){ var self = this; var address = coinjs.wif2address(wif); - self.addUnspent(address['address'], amount, function(data){ + self.addUnspent(address['address'], function(data){ self.sign(wif); return callback(data); }); From 81c6eb3dd7cd490d60ef0f81ef867184576c3ac1 Mon Sep 17 00:00:00 2001 From: AliAshrafD Date: Sun, 25 Apr 2021 14:01:18 +0430 Subject: [PATCH 3/6] Minimizing inputs Sorting the retrieved UTXO, order by amount, ascending to be used for the selection of the smallest set of inputs for the txn under construction. --- js/coin.js | 46 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 7 deletions(-) diff --git a/js/coin.js b/js/coin.js index 1ba2682..3a10455 100644 --- a/js/coin.js +++ b/js/coin.js @@ -746,9 +746,41 @@ r.parse(); return r; } + + coinjs.cpmareUnspents = function( unspent1, unspent2){ + return ( unspent1.amount - unspent2.amount); + } + + coinjs.partition = function(arr, start, end, compare){ + const pivot = arr[end]; + let pIndex = start; + for (let i = start; i < end; i++) { + if (compare(arr[i] , pivot) < 0 ) { + // Swap + [arr[i], arr[pIndex]] = [arr[pIndex], arr[i]]; + pIndex++; + } + } + + [arr[pIndex], arr[end]] = [arr[end], arr[pIndex]] + return pivotIndex; +}; + +coinjs.qSort = function(arr, start, end, compare) { + // terminating condition + if (start >= end) { + return; + } + let index = partition(arr, start, end, compare); + + // Recursively apply the same logic to the left and right subarrays + qSort(arr, start, index - 1, compare); + qSort(arr, index + 1, end, compare); +} - /* start of transaction functions */ + /* start of transaction functions */ + /* create a new transaction object */ coinjs.transaction = function() { @@ -841,7 +873,7 @@ } /* add unspent to transaction */ - r.addUnspent = function(address, callback, script, sequence){ + r.addUnspent = function(address, amount, callback, script, sequence){ var self = this; this.listUnspent(address, function(data){ @@ -852,9 +884,9 @@ var x = {}; var unspent = JSON.parse(data); - console.log("Unspent is"+unspent); - - for(i=0;i<=unspent.length-1;i++){ + console.log("Unspent is:"+unspent); + qSort(unspent, 0, unspent.length, compareUnspents); + for(i=0;i<=unspent.length-1 && value < amount;i++){ var txhash = unspent[i].txid, n = unspent[i].vout, scr = unspent[i].scriptPubKey, @@ -874,10 +906,10 @@ } /* add unspent and sign */ - r.addUnspentAndSign = function(wif, callback){ + r.addUnspentAndSign = function(wif, amount, callback){ var self = this; var address = coinjs.wif2address(wif); - self.addUnspent(address['address'], function(data){ + self.addUnspent(address['address'], amount, function(data){ self.sign(wif); return callback(data); }); From 424055396c3ea3225324f8af447de60fc6835694 Mon Sep 17 00:00:00 2001 From: AliAshrafD Date: Sun, 25 Apr 2021 14:38:00 +0430 Subject: [PATCH 4/6] Update coin.js --- js/coin.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/coin.js b/js/coin.js index 3a10455..e55d3c9 100644 --- a/js/coin.js +++ b/js/coin.js @@ -763,7 +763,7 @@ } [arr[pIndex], arr[end]] = [arr[end], arr[pIndex]] - return pivotIndex; + return pIndex; }; coinjs.qSort = function(arr, start, end, compare) { From 687d49e6efc58a7cca36996b23ced9e46aab522b Mon Sep 17 00:00:00 2001 From: AliAshrafD Date: Sun, 25 Apr 2021 16:03:06 +0430 Subject: [PATCH 5/6] Update coinbin.js --- js/coinbin.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/coinbin.js b/js/coinbin.js index 99d3f06..286f765 100644 --- a/js/coinbin.js +++ b/js/coinbin.js @@ -118,7 +118,7 @@ console.log("Finished wallet spend"); var sequence = false; - tx.addUnspent($("#walletAddress").html(), function(data){ + tx.addUnspent($("#walletAddress").html(), total, function(data){ console.log("Going into tx.addUnspent"+data.value); var dvalue = (data.value).toFixed(8) * 1; From 1c7c44095fcc25b9e5a4b516300f1d2cfe7fbb45 Mon Sep 17 00:00:00 2001 From: AliAshrafD Date: Sun, 25 Apr 2021 16:37:39 +0430 Subject: [PATCH 6/6] Update coin.js --- js/coin.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/coin.js b/js/coin.js index e55d3c9..c979a4a 100644 --- a/js/coin.js +++ b/js/coin.js @@ -885,7 +885,7 @@ coinjs.qSort = function(arr, start, end, compare) { var unspent = JSON.parse(data); console.log("Unspent is:"+unspent); - qSort(unspent, 0, unspent.length, compareUnspents); + //qSort(unspent, 0, unspent.length, compareUnspents); for(i=0;i<=unspent.length-1 && value < amount;i++){ var txhash = unspent[i].txid, n = unspent[i].vout,