From: AJ ONeal Date: Tue, 21 Jun 2022 07:05:23 +0000 (-0600) Subject: feat(cli): add load subcommand X-Git-Tag: v1.2.0~1 X-Git-Url: https://git.josue.xyz/?a=commitdiff_plain;h=de2d6dcd0d93b014e64bd856ab281a7cd243c868;p=crowdnode.js%2F.git feat(cli): add load subcommand --- diff --git a/bin/crowdnode.js b/bin/crowdnode.js index 60a251f..ddeafe2 100755 --- a/bin/crowdnode.js +++ b/bin/crowdnode.js @@ -68,6 +68,7 @@ function showHelp() { console.info("Helpful Extras:"); console.info(" crowdnode generate [./privkey.wif]"); + console.info(" crowdnode load [./privkey.wif]"); console.info(" crowdnode balance ./privkey.wif"); console.info( " crowdnode transfer ./source.wif [dash-amount]", @@ -136,6 +137,18 @@ async function main() { let insightApi = Insight.create({ baseUrl: insightBaseUrl }); let dashApi = Dash.create({ insightApi: insightApi }); + if ("load" === subcommand) { + await load( + { + dashApi: dashApi, + insightBaseUrl: insightBaseUrl, + insightApi: insightApi, + }, + args, + ); + return; + } + process.stdout.write("Checking CrowdNode API... "); await CrowdNode.init({ baseUrl: "https://app.crowdnode.io", @@ -330,6 +343,57 @@ async function generate(name) { process.exit(0); } +async function load(psuedoState, args) { + let name = args.shift(); + + // TODO factor out the common bits from generate? + let pk = new Dashcore.PrivateKey(); + + let pub = pk.toAddress().toString(); + let wif = pk.toWIF(); + + let filepath = `./${pub}.wif`; + let note = ""; + if (name) { + filepath = name; + note = `\n(for pubkey address ${pub})`; + } + + let err = await Fs.access(filepath).catch(Object); + if (!err) { + console.info(`'${filepath}' already exists (will not overwrite)`); + } else { + await Fs.writeFile(filepath, wif, "utf8"); + console.info(`Generated ${filepath} ${note}`); + } + + let desiredAmountDash = parseFloat(args.shift() || 0); + let desiredAmountDuff = Math.round(desiredAmountDash * DUFFS); + let effectiveDuff = desiredAmountDuff; + let effectiveDash = ""; + if (!effectiveDuff) { + effectiveDuff = CrowdNode.stakeMinimum + signupTotal + feeEstimate; + effectiveDash = toDash(effectiveDuff); + // Round to the nearest mDash + // ex: 0.50238108 => 0.50300000 + effectiveDuff = toDuff(Math.ceil(effectiveDash * 1000) / 1000); + effectiveDash = toDash(effectiveDuff); + } + + console.info(``); + showQr(pub, effectiveDuff); + console.info(``); + console.info( + `Use the QR Code above to load ${effectiveDuff} (Đ${effectiveDash}) onto your staking key.`, + ); + console.info(``); + console.info(`(waiting...)`); + console.info(``); + let payment = await Ws.waitForVout(psuedoState.insightBaseUrl, pub, 0); + console.info(`Received ${payment.satoshis}`); + process.exit(0); +} + async function balance(args, state) { console.info(state.balanceInfo); process.exit(0); @@ -568,6 +632,7 @@ async function wifFileToAddr(keyfile) { } async function collectSignupFees(insightBaseUrl, pub) { + console.info(``); showQr(pub); let signupTotalDash = toDash(signupTotal); @@ -589,6 +654,7 @@ async function collectSignupFees(insightBaseUrl, pub) { } async function collectDeposit(insightBaseUrl, pub, duffAmount) { + console.info(``); showQr(pub, duffAmount); let depositMsg = `Please send what you wish to deposit to ${pub}`; diff --git a/cli/README.md b/cli/README.md index d37e756..2378d72 100644 --- a/cli/README.md +++ b/cli/README.md @@ -91,6 +91,7 @@ Usage: Helpful Extras: crowdnode generate [./privkey.wif] + crowdnode load [./privkey.wif] [dash-amount] crowdnode balance ./privkey.wif crowdnode transfer ./source.wif [dash-amount]