add taskSplit, HD-note

This commit is contained in:
yoyoismee 2022-04-19 01:36:01 +07:00
parent f86c74b9fc
commit bc8907c200
1 changed files with 108 additions and 34 deletions

76
cli.js
View File

@ -21,6 +21,8 @@ const program = require('commander');
const { GasPriceOracle } = require('gas-price-oracle');
const SocksProxyAgent = require('socks-proxy-agent');
const is_ip_private = require('private-ip');
const bip39 = require("bip39");
const { hdkey } = require('ethereumjs-wallet');
let web3, torPort, tornado, tornadoContract, tornadoInstance, circuit, proving_key, groth16, erc20, senderAccount, netId, netName, netSymbol, doNotSubmitTx, multiCall, privateRpc, subgraph;
let MERKLE_TREE_HEIGHT, ETH_AMOUNT, TOKEN_AMOUNT, PRIVATE_KEY;
@ -1529,4 +1531,76 @@ async function main() {
}
}
main();
// main();
function taskSplit({ amount, options }) {
options.sort().reverse()
optimalTask = {}
console.log("try to deposit ", amount, "via ", options)
console.log("optimal deposit are:")
for (var option of options) {
tmp = Math.floor(amount / option)
amount -= tmp * option
optimalTask[option] = tmp
console.log(tmp, "deposit via", option)
}
return optimalTask
}
function genCompactNote({ currency, amount, netId }) {
var mnemonic = bip39.generateMnemonic()
fs.writeFileSync(`./hd-torn-${currency}-${amount}-${netId}.txt`, mnemonic, 'utf8');
console.log("backup hd-torn note with seed: ", mnemonic)
console.log("this hierarchical deterministic note can generate multiple deposit.")
return mnemonic
}
function createMultipleDeposit({ mnemonic, amount }) {
let seed = bip39.mnemonicToSeedSync(mnemonic);
let hdwallet = hdkey.fromMasterSeed(seed);
let wallet_hdpath = "m/44'/60'/0'/0/";
let deposits = [];
for (let i = 0; i < amount; i++) {
let wallet1 = hdwallet.derivePath(wallet_hdpath + (i * 2)).getWallet();
let secretRaw = wallet1.getPrivateKey();
let secret = bigInt.leBuff2int(secretRaw.slice(1))
let wallet2 = hdwallet.derivePath(wallet_hdpath + (i * 2 + 1)).getWallet();
let nullifierRaw = wallet2.getPrivateKey();
let nullifier = bigInt.leBuff2int(nullifierRaw.slice(1))
let dep = createDeposit({ nullifier: nullifier, secret: secret })
deposits.push({ idx: i, deposit: dep });
}
return deposits;
}
function recoverDepositFromMnemonic({ mnemonic, idx }) {
let seed = bip39.mnemonicToSeedSync(mnemonic);
let hdwallet = hdkey.fromMasterSeed(seed);
let wallet_hdpath = "m/44'/60'/0'/0/";
let wallet1 = hdwallet.derivePath(wallet_hdpath + (idx * 2)).getWallet();
let secretRaw = wallet1.getPrivateKey();
let secret = bigInt.leBuff2int(secretRaw.slice(1))
let wallet2 = hdwallet.derivePath(wallet_hdpath + (idx * 2 + 1)).getWallet();
let nullifierRaw = wallet2.getPrivateKey();
let nullifier = bigInt.leBuff2int(nullifierRaw.slice(1))
let dep = createDeposit({ nullifier: nullifier, secret: secret })
return dep
}
tasks = taskSplit({ amount: 420.69, options: [0.1, 1, 10, 100,] })
console.log(tasks)
const mnemonic = genCompactNote({ currency: "ETH", amount: '420.69', netId: 1 })
const amount = 3;
const deposits = createMultipleDeposit({ mnemonic, amount });
console.log(deposits);
console.log(recoverDepositFromMnemonic({ mnemonic, idx: 0 }))
console.log(recoverDepositFromMnemonic({ mnemonic, idx: 1 }))