From 4bef1a17beed85a118f5c84d61f5f100340b64ca Mon Sep 17 00:00:00 2001 From: joeuhren Date: Sun, 21 Mar 2021 18:20:49 -0600 Subject: [PATCH] Add full zksnarks tx support --- README.md | 2 +- lib/explorer.js | 73 ++++++++++++++++++++++++++++++++---------- lib/settings.js | 3 -- settings.json.template | 3 -- 4 files changed, 57 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index f653416a..b829dc7a 100644 --- a/README.md +++ b/README.md @@ -190,7 +190,7 @@ Table of Contents - **getsupply:** Returns the current money supply - **getnextrewardestimate:** Returns an estimate for the next block reward based on the current state of decentralized voting - **getnextrewardwhenstr:** Returns a string describing how long until the votes are tallied and the next block reward is computed - - Partial Zcash/zk-SNARKs private tx support (90% complete) + - Zcash/zk-SNARKs private tx support ### See it in Action diff --git a/lib/explorer.js b/lib/explorer.js index 7f91e493..b337ce0e 100644 --- a/lib/explorer.js +++ b/lib/explorer.js @@ -1082,26 +1082,65 @@ module.exports = { // check if zksnarks is enabled if (settings.blockchain_specific.zksnarks.enabled == true) { // check for hidden/anonymous outputs - vhidden.forEach(function(vanon, i) { - if (vanon.vpub_old > 0) { - module.exports.convert_to_satoshi(parseFloat(vanon.vpub_old), function(amount_sat) { - arr_vout.push({addresses: 'hidden_address', amount: amount_sat}); - }); - } else { - module.exports.convert_to_satoshi(parseFloat(vanon.vpub_new), function(amount_sat) { - if (vhidden.length > 0 && (!vout || vout.length == 0) && (!vin || vin.length == 0)) { + if (vhidden != null && vhidden.length > 0) { + tx_type = "zksnarks"; + // loop through all hidden/anonymous outputs + module.exports.syncLoop(vhidden.length, function (loop) { + var i = loop.iteration(); + + if (vhidden[i].vpub_old > 0) { + // process vout addresses + processVoutAddresses(['hidden_address'], parseFloat(vhidden[i].vpub_old), arr_vout, function(vout_array) { + // save updated array + arr_vout = vout_array; + // move to next vout + loop.next(); + }); + } else { + if ((!vout || vout.length == 0) && (!vin || vin.length == 0)) { // hidden sender is sending to hidden recipient // the sent and received values are not known in this case. only the fee paid is known and subtracted from the sender. - arr_vout.push({addresses: 'hidden_address', amount: 0}); + // process vout addresses + processVoutAddresses(['hidden_address'], 0, arr_vout, function(vout_array) { + // save updated array + arr_vout = vout_array; + // add a private send address with the known amount sent + module.exports.is_unique(arr_vin, 'hidden_address', function(unique, index) { + if (unique == true) { + module.exports.convert_to_satoshi(parseFloat(vhidden[i].vpub_new), function(amount_sat) { + arr_vin.push({addresses: 'hidden_address', amount: amount_sat}); + // move to next vout + loop.next(); + }); + } else { + module.exports.convert_to_satoshi(parseFloat(vhidden[i].vpub_new), function(amount_sat) { + arr_vin[index].amount = arr_vin[index].amount + amount_sat; + // move to next vout + loop.next(); + }); + } + }); + }); + } else { + module.exports.is_unique(arr_vin, 'hidden_address', function(unique, index) { + if (unique == true) { + module.exports.convert_to_satoshi(parseFloat(vhidden[i].vpub_new), function(amount_sat) { + arr_vin.push({addresses: 'hidden_address', amount: amount_sat}); + // move to next vout + loop.next(); + }); + } else { + module.exports.convert_to_satoshi(parseFloat(vhidden[i].vpub_new), function(amount_sat) { + arr_vin[index].amount = arr_vin[index].amount + amount_sat; + // move to next vout + loop.next(); + }); + } + }); } - - // add a private send address with the known amount sent - arr_vin.push({addresses: 'hidden_address', amount: amount_sat}); - }); - } - - tx_type = "zksnarks"; - }); + } + }); + } } if (typeof vout[0] !== 'undefined' && vout[0].scriptPubKey.type == 'nonstandard') { diff --git a/lib/settings.js b/lib/settings.js index 459a862d..88817865 100644 --- a/lib/settings.js +++ b/lib/settings.js @@ -960,9 +960,6 @@ exports.blockchain_specific = { } }, // zksnarks: A collection of settings that pertain to Zcash zk-SNARKs private transactions - // NOTE: This is only partial zk-SNARKs private tx support (90% complete) - // While the current implementation should be stable, it does not yet work in 100% of the transaction scenarios - // USE THIS FEATURE AT YOUR OWN RISK. FOR TESTING PURPOSES ONLY! "zksnarks": { // enabled: Enable/disable Zcash zk-SNARKs private transaction support (true/false) // If set to false, zk-SNARKs private txs will not be properly read or saved by the explorer diff --git a/settings.json.template b/settings.json.template index 36bbb499..256f41f8 100644 --- a/settings.json.template +++ b/settings.json.template @@ -1077,9 +1077,6 @@ } }, // zksnarks: A collection of settings that pertain to Zcash zk-SNARKs private transactions - // NOTE: This is only partial zk-SNARKs private tx support (90% complete) - // While the current implementation should be stable, it does not yet work in 100% of the transaction scenarios - // USE THIS FEATURE AT YOUR OWN RISK. FOR TESTING PURPOSES ONLY! "zksnarks": { // enabled: Enable/disable Zcash zk-SNARKs private transaction support (true/false) // If set to false, zk-SNARKs private txs will not be properly read or saved by the explorer