3 /*jshint maxcomplexity:25 */
5 require("dotenv").config({ path: ".env" });
6 require("dotenv").config({ path: ".env.secret" });
8 let HOME = process.env.HOME || "";
11 let pkg = require("../package.json");
13 let Fs = require("fs").promises;
14 let Path = require("path");
16 let Cipher = require("./_cipher.js");
17 let CrowdNode = require("../lib/crowdnode.js");
18 let Dash = require("../lib/dash.js");
19 let Insight = require("../lib/insight.js");
20 let Prompt = require("./_prompt.js");
21 let Qr = require("../lib/qr.js");
22 let Ws = require("../lib/ws.js");
24 let Dashcore = require("@dashevo/dashcore-lib");
26 const DUFFS = 100000000;
27 let qrWidth = 2 + 67 + 2;
29 // 0.00236608 // required for signup
30 // 0.00002000 // TX fee estimate
31 // 0.00238608 // minimum recommended amount
34 let signupOnly = CrowdNode.requests.signupForApi + CrowdNode.requests.offset;
35 let acceptOnly = CrowdNode.requests.acceptTerms + CrowdNode.requests.offset;
36 let signupFees = signupOnly + acceptOnly;
37 let feeEstimate = 500;
38 let signupTotal = signupFees + 2 * feeEstimate;
41 let configdir = `.config/crowdnode`;
42 let keysDir = Path.join(HOME, `${configdir}/keys`);
43 let keysDirRel = `~/${configdir}/keys`;
44 let shadowPath = Path.join(HOME, `${configdir}/shadow`);
45 let defaultWifPath = Path.join(HOME, `${configdir}/default`);
47 function showVersion() {
48 console.info(`${pkg.name} v${pkg.version} - ${pkg.description}`);
55 console.info("Usage:");
56 console.info(" crowdnode help");
57 console.info(" crowdnode status [keyfile-or-addr]");
58 console.info(" crowdnode signup [keyfile-or-addr]");
59 console.info(" crowdnode accept [keyfile-or-addr]");
61 " crowdnode deposit [keyfile-or-addr] [dash-amount] [--no-reserve]",
64 " crowdnode withdrawal [keyfile-or-addr] <percent> # 1.0-100.0 (steps by 0.1)",
68 console.info("Helpful Extras:");
69 console.info(" crowdnode balance [keyfile-or-addr]"); // addr
70 console.info(" crowdnode load [keyfile-or-addr] [dash-amount]"); // addr
72 " crowdnode transfer <from-keyfile-or-addr> <to-keyfile-or-addr> [dash-amount]",
76 console.info("Key Management & Encryption:");
77 console.info(" crowdnode generate [--plain-text] [./privkey.wif]");
78 console.info(" crowdnode encrypt"); // TODO allow encrypting one-by-one?
79 console.info(" crowdnode list");
80 console.info(" crowdnode use <addr>");
81 console.info(" crowdnode import <keyfile>");
82 //console.info(" crowdnode import <(dash-cli dumpprivkey <addr>)"); // TODO
83 //console.info(" crowdnode export <addr> <keyfile>"); // TODO
84 console.info(" crowdnode passphrase # set or change passphrase");
85 console.info(" crowdnode decrypt"); // TODO allow decrypting one-by-one?
86 console.info(" crowdnode delete <addr>");
89 console.info("CrowdNode HTTP RPC:");
90 console.info(" crowdnode http FundsOpen <addr>");
91 console.info(" crowdnode http VotingOpen <addr>");
92 console.info(" crowdnode http GetFunds <addr>");
93 console.info(" crowdnode http GetFundsFrom <addr> <seconds-since-epoch>");
94 console.info(" crowdnode http GetBalance <addr>");
95 console.info(" crowdnode http GetMessages <addr>");
96 console.info(" crowdnode http IsAddressInUse <addr>");
97 // TODO create signature rather than requiring it
98 console.info(" crowdnode http SetEmail ./privkey.wif <email> <signature>");
99 console.info(" crowdnode http Vote ./privkey.wif <gobject-hash> ");
100 console.info(" <Yes|No|Abstain|Delegate|DoNothing> <signature>");
102 " crowdnode http SetReferral ./privkey.wif <referral-id> <signature>",
109 async function main() {
110 /*jshint maxcomplexity:40 */
111 /*jshint maxstatements:500 */
114 // crowdnode <subcommand> [flags] <privkey> [options]
116 // crowdnode withdrawal ./Xxxxpubaddr.wif 100.0
118 let args = process.argv.slice(2);
121 let forceConfirm = removeItem(args, "--unconfirmed");
122 let plainText = removeItem(args, "--plain-text");
123 let noReserve = removeItem(args, "--no-reserve");
125 let subcommand = args.shift();
127 if (!subcommand || ["--help", "-h", "help"].includes(subcommand)) {
133 if (["--version", "-V", "version"].includes(subcommand)) {
141 // find addr by name or by file or by string
142 await Fs.mkdir(keysDir, {
146 let defaultAddr = await Fs.readFile(defaultWifPath, "utf8").catch(
147 emptyStringOnErrEnoent,
149 defaultAddr = defaultAddr.trim();
152 process.env.INSIGHT_BASE_URL || "https://insight.dash.org";
153 let insightApi = Insight.create({ baseUrl: insightBaseUrl });
154 let dashApi = Dash.create({ insightApi: insightApi });
156 if ("list" === subcommand) {
157 await listKeys({ dashApi }, args);
161 if ("generate" === subcommand) {
162 await generateKey({ defaultKey: defaultAddr, plainText }, args);
166 if ("passphrase" === subcommand) {
167 await setPassphrase({}, args);
171 if ("import" === subcommand) {
172 importKey(null, args);
176 if ("encrypt" === subcommand) {
177 let addr = args.shift() || "";
183 let keypath = await findWif(addr);
185 console.error(`no managed key matches '${addr}'`);
189 let key = await maybeReadKeyFileRaw(keypath);
191 throw new Error("impossible error");
197 if ("decrypt" === subcommand) {
198 let addr = args.shift() || "";
203 let keypath = await findWif(addr);
205 console.error(`no managed key matches '${addr}'`);
209 let key = await maybeReadKeyFileRaw(keypath);
211 throw new Error("impossible error");
217 // use or select or default... ?
218 if ("use" === subcommand) {
219 await setDefault(null, args);
223 // helper for debugging
224 if ("transfer" === subcommand) {
225 await transferBalance(
226 { dashApi, defaultAddr, forceConfirm, insightBaseUrl, insightApi },
233 if ("http" === subcommand) {
234 rpc = args.shift() || "";
241 let [addr] = await mustGetAddr({ defaultAddr }, args);
243 await initCrowdNode(insightBaseUrl);
244 // ex: http <rpc>(<pub>, ...)
246 let hasRpc = rpc in CrowdNode.http;
248 console.error(`Unrecognized rpc command ${rpc}`);
253 //@ts-ignore - TODO use `switch` or make Record Type
254 let result = await CrowdNode.http[rpc].apply(null, args);
256 console.info(`${rpc} ${addr}:`);
257 if ("string" === typeof result) {
258 console.info(result);
260 console.info(JSON.stringify(result, null, 2));
265 if ("load" === subcommand) {
266 await loadAddr({ defaultAddr, insightBaseUrl }, args);
270 // keeping rm for backwards compat
271 if ("rm" === subcommand || "delete" === subcommand) {
272 await initCrowdNode(insightBaseUrl);
273 await removeKey({ defaultAddr, dashApi, insightBaseUrl }, args);
277 if ("balance" === subcommand) {
278 await getBalance({ dashApi, defaultAddr }, args);
283 if ("status" === subcommand) {
284 await getStatus({ dashApi, defaultAddr, insightBaseUrl }, args);
288 if ("signup" === subcommand) {
289 await sendSignup({ dashApi, defaultAddr, insightBaseUrl }, args);
293 if ("accept" === subcommand) {
294 await acceptTerms({ dashApi, defaultAddr, insightBaseUrl }, args);
298 if ("deposit" === subcommand) {
300 { dashApi, defaultAddr, insightBaseUrl, noReserve },
306 if ("withdrawal" === subcommand) {
307 await withdrawalDash({ dashApi, defaultAddr, insightBaseUrl }, args);
311 console.error(`Unrecognized subcommand ${subcommand}`);
318 * @param {String} insightBaseUrl
320 async function initCrowdNode(insightBaseUrl) {
321 process.stdout.write("Checking CrowdNode API... ");
322 await CrowdNode.init({
323 baseUrl: "https://app.crowdnode.io",
326 console.info(`(hotwallet ${CrowdNode.main.hotwallet})`);
330 * @param {String} addr - Base58Check pubKeyHash address
331 * @param {Number} duffs - 1/100000000 of a DASH
333 function showQr(addr, duffs = 0) {
334 let dashAmount = toDash(duffs);
335 let dashUri = `dash://${addr}`;
337 dashUri += `?amount=${dashAmount}`;
340 let dashQr = Qr.ascii(dashUri, { indent: 4, size: "mini" });
341 let addrPad = Math.ceil((qrWidth - dashUri.length) / 2);
343 console.info(dashQr);
345 console.info(" ".repeat(addrPad) + dashUri);
349 * @param {Array<any>} arr
352 function removeItem(arr, item) {
353 let index = arr.indexOf(item);
355 return arr.splice(index, 1)[0];
361 * @param {Object} opts
362 * @param {String} opts.addr
363 * @param {String} opts.hotwallet
365 async function getCrowdNodeStatus({ addr, hotwallet }) {
377 //@ts-ignore - TODO why warnings?
378 state.status = await CrowdNode.status(addr, hotwallet);
379 if (state.status?.signup) {
382 if (state.status?.accept) {
385 if (state.status?.deposit) {
392 * @param {Object} opts
393 * @param {String} opts.addr
394 * @param {any} opts.dashApi - TODO
396 async function checkBalance({ addr, dashApi }) {
397 // deposit if balance is over 100,000 (0.00100000)
398 process.stdout.write("Checking balance... ");
399 let balanceInfo = await dashApi.getInstantBalance(addr);
400 let balanceDash = toDash(balanceInfo.balanceSat);
401 console.info(`${balanceInfo.balanceSat} (Đ${balanceDash})`);
403 let balanceInfo = await insightApi.getBalance(pub);
404 if (balanceInfo.unconfirmedBalanceSat || balanceInfo.unconfirmedAppearances) {
407 `Error: This address has pending transactions. Please try again in 1-2 minutes or use --unconfirmed.`,
409 console.error(balanceInfo);
410 if ("status" !== subcommand) {
421 * @param {Object} opts
422 * @param {String} opts.defaultAddr
423 * @param {Array<String>} args
424 * @returns {Promise<[String, String]>}
426 async function mustGetAddr({ defaultAddr }, args) {
427 let name = args.shift() ?? "";
428 if (34 === name.length) {
429 // looks like addr already
430 // TODO make function for addr-lookin' check
434 let addr = await maybeReadKeyPaths(name, { wif: false });
436 if (34 === addr.length) {
439 //let pk = new Dashcore.PrivateKey(wif);
440 //let addr = pk.toAddress().toString();
444 let isNum = !isNaN(parseFloat(name));
452 console.error(`could not read '${name}' in ./ or match in ${keysDirRel}/.`);
458 addr = await mustGetDefaultWif(defaultAddr, { wif: false });
460 // TODO we don't need defaultAddr, right? because it could be old?
465 * @param {Object} opts
466 * @param {String} opts.defaultAddr
467 * @param {Array<String>} args
469 async function mustGetWif({ defaultAddr }, args) {
470 let name = args.shift() ?? "";
472 let wif = await maybeReadKeyPaths(name, { wif: true });
477 let isNum = !isNaN(parseFloat(name));
486 `'${name}' does not match a staking key in ./ or ${keysDirRel}/`,
493 wif = await mustGetDefaultWif(defaultAddr);
499 * @param {String} name
500 * @param {Object} opts
501 * @param {Boolean} opts.wif
502 * @returns {Promise<String>} - wif
504 async function maybeReadKeyPaths(name, opts) {
507 // prefix match in .../keys/
508 let wifname = await findWif(name);
513 if (false === opts.wif) {
514 return wifname.slice(0, -".wif".length);
517 let filepath = Path.join(keysDir, wifname);
518 privKey = await maybeReadKeyFile(filepath);
521 privKey = await maybeReadKeyFile(name);
528 * @param {String} defaultAddr
529 * @param {Object} [opts]
530 * @param {Boolean} opts.wif
532 async function mustGetDefaultWif(defaultAddr, opts) {
535 let keyfile = Path.join(keysDir, `${defaultAddr}.wif`);
536 let raw = await maybeReadKeyFileRaw(keyfile, opts);
537 // misnomering wif here a bit
538 defaultWif = raw?.wif || raw?.addr || "";
541 console.info(`selected default staking key ${defaultAddr}`);
546 console.error(`Error: no default staking key selected.`);
548 console.error(`Select a different address:`);
549 console.error(` crowdnode list`);
550 console.error(` crowdnode use <addr>`);
552 console.error(`Or create a new staking key:`);
553 console.error(` crowdnode generate`);
562 * @param {Object} psuedoState
563 * @param {String} psuedoState.defaultKey - addr name of default key
564 * @param {Boolean} psuedoState.plainText - don't encrypt
565 * @param {Array<String>} args
567 async function generateKey({ defaultKey, plainText }, args) {
568 let name = args.shift();
569 //@ts-ignore - TODO submit JSDoc PR for Dashcore
570 let pk = new Dashcore.PrivateKey();
572 let addr = pk.toAddress().toString();
573 let plainWif = pk.toWIF();
577 wif = await maybeEncrypt(plainWif);
580 let filename = `~/${configdir}/keys/${addr}.wif`;
581 let filepath = Path.join(`${keysDir}/${addr}.wif`);
586 note = `\n(for pubkey address ${addr})`;
587 let err = await Fs.access(filepath).catch(Object);
589 console.info(`'${filepath}' already exists (will not overwrite)`);
595 await Fs.writeFile(filepath, wif, "utf8");
596 if (!name && !defaultKey) {
597 await Fs.writeFile(defaultWifPath, addr, "utf8");
601 console.info(`Generated ${filename} ${note}`);
608 * @param {Object} state
609 * @param {Boolean} [state._askPreviousPassphrase] - don't ask for passphrase again
610 * @param {Array<String>} args
612 async function setPassphrase({ _askPreviousPassphrase }, args) {
613 let date = getFsDateString();
615 // get the old passphrase
616 if (false !== _askPreviousPassphrase) {
617 await cmds.getPassphrase({ _rotatePassphrase: true }, []);
620 // get the new passphrase
623 newPassphrase = await Prompt.prompt("Enter (new) passphrase: ", {
626 newPassphrase = newPassphrase.trim();
628 let _newPassphrase = await Prompt.prompt("Enter passphrase again: ", {
631 _newPassphrase = _newPassphrase.trim();
633 let match = Cipher.secureCompare(newPassphrase, _newPassphrase);
638 console.error("passphrases do not match");
640 let curShadow = await Fs.readFile(shadowPath, "utf8").catch(
641 emptyStringOnErrEnoent,
644 let newShadow = await Cipher.shadowPassphrase(newPassphrase);
645 await Fs.writeFile(shadowPath, newShadow, "utf8");
647 let rawKeys = await readAllKeys();
648 let encAddrs = rawKeys
649 .map(function (raw) {
656 // backup all currently encrypted files
658 if (encAddrs.length) {
659 let filepath = Path.join(HOME, `${configdir}/keys.${date}.bak`);
661 console.info(`Backing up previous (encrypted) keys:`);
662 encAddrs.unshift(curShadow);
663 await Fs.writeFile(filepath, encAddrs.join("\n") + "\n", "utf8");
664 console.info(` ~/${configdir}/keys.${date}.bak`);
667 cmds._setPassphrase(newPassphrase);
669 await encryptAll(rawKeys, { rotateKey: true });
671 return newPassphrase;
677 * @param {Array<String>} args
679 async function importKey(_, args) {
680 let keypath = args.shift() || "";
681 let key = await maybeReadKeyFileRaw(keypath);
683 console.error(`no key found for '${keypath}'`);
688 let encWif = await maybeEncrypt(key.wif);
690 if (encWif.includes(":")) {
693 let date = getFsDateString();
696 Path.join(keysDir, `${key.addr}.wif`),
698 Path.join(keysDir, `${key.addr}.${date}.bak`),
701 console.info(`${icon} Imported ${keysDirRel}/${key.addr}.wif`);
708 * @param {Array<String>} args
710 async function importKey(_, args) {
711 let keypath = args.shift() || "";
712 let key = await maybeReadKeyFileRaw(keypath);
714 console.error(`no key found for '${keypath}'`);
719 let encWif = await maybeEncrypt(key.wif);
721 if (encWif.includes(":")) {
724 let date = getFsDateString();
727 Path.join(keysDir, `${key.addr}.wif`),
729 Path.join(keysDir, `${key.addr}.${date}.bak`),
732 console.info(`${icon} Imported ${keysDirRel}/${key.addr}.wif`);
737 * Encrypt ALL-the-things!
738 * @param {Object} [opts]
739 * @param {Boolean} opts.rotateKey
740 * @param {Array<RawKey>?} rawKeys
742 async function encryptAll(rawKeys, opts) {
744 rawKeys = await readAllKeys();
746 let date = getFsDateString();
748 console.info(`Encrypting...`);
750 await rawKeys.reduce(async function (promise, key) {
753 if (key.encrypted && !opts?.rotateKey) {
754 console.info(`🙈 ${key.addr} [already encrypted]`);
757 let encWif = await maybeEncrypt(key.wif);
759 Path.join(keysDir, `${key.addr}.wif`),
761 Path.join(keysDir, `${key.addr}.${date}.bak`),
763 console.info(`🔑 ${key.addr}`);
764 }, Promise.resolve());
766 console.info(`Done 🔐`);
771 * Decrypt ALL-the-things!
772 * @param {Array<RawKey>?} rawKeys
774 async function decryptAll(rawKeys) {
776 rawKeys = await readAllKeys();
778 let date = getFsDateString();
781 console.info(`Decrypting...`);
783 await rawKeys.reduce(async function (promise, key) {
786 if (!key.encrypted) {
787 console.info(`📖 ${key.addr} [already decrypted]`);
791 Path.join(keysDir, `${key.addr}.wif`),
793 Path.join(keysDir, `${key.addr}.${date}.bak`),
795 console.info(`🔓 ${key.addr}`);
796 }, Promise.resolve());
798 console.info(`Done ✅`);
802 function getFsDateString() {
803 // YYYY-MM-DD_hh-mm_ss
804 let date = new Date()
808 .replace(/\.\d{3}.*/, "");
813 * @param {String} filepath
814 * @param {String} wif
815 * @param {String} bakpath
817 async function safeSave(filepath, wif, bakpath) {
818 let tmpPath = `${bakpath}.tmp`;
819 await Fs.writeFile(tmpPath, wif, "utf8");
820 let err = await Fs.access(filepath).catch(Object);
822 await Fs.rename(filepath, bakpath);
824 await Fs.rename(tmpPath, filepath);
826 await Fs.unlink(bakpath);
831 * @param {Object} opts
832 * @param {Boolean} [opts._rotatePassphrase]
833 * @param {Array<String>} args
835 cmds.getPassphrase = async function ({ _rotatePassphrase }, args) {
836 // Three possible states:
837 // 1. no shadow file yet (ask to set one)
838 // 2. empty shadow file (initialized, but not set - don't ask to set one)
839 // 3. encrypted shadow file (initialized, requires passphrase)
840 let needsInit = false;
841 let shadow = await Fs.readFile(shadowPath, "utf8").catch(function (err) {
842 if ("ENOENT" === err.code) {
849 // State 1: not initialized, what does the user want?
852 let no = await Prompt.prompt(
853 "Would you like to set an encryption passphrase? [Y/n]: ",
856 // Set a passphrase and create shadow file
857 if (!no || ["yes", "y"].includes(no.toLowerCase())) {
858 let passphrase = await setPassphrase(
859 { _askPreviousPassphrase: false },
862 cmds._setPassphrase(passphrase);
867 if (!["no", "n"].includes(no.toLowerCase())) {
871 // No passphrase, create empty shadow file
872 await Fs.writeFile(shadowPath, "", "utf8");
877 // State 2: shadow already initialized to empty
878 // (user doesn't want a passphrase)
880 cmds._setPassphrase("");
884 // State 3: passphrase & shadow already in use
886 let prompt = `Enter passphrase: `;
887 if (_rotatePassphrase) {
888 prompt = `Enter (current) passphrase: `;
890 let passphrase = await Prompt.prompt(prompt, {
893 passphrase = passphrase.trim();
894 if (!passphrase || "q" === passphrase) {
895 console.error("cancel: no passphrase");
900 let match = await Cipher.checkPassphrase(passphrase, shadow);
902 cmds._setPassphrase(passphrase);
907 console.error("incorrect passphrase");
910 throw new Error("SANITY FAIL: unreachable return");
913 cmds._getPassphrase = function () {
918 * @param {String} passphrase
920 cmds._setPassphrase = function (passphrase) {
921 // Look Ma! A private variable!
922 cmds._getPassphrase = function () {
928 * @typedef {Object} RawKey
929 * @property {String} addr
930 * @property {Boolean} encrypted
931 * @property {String} wif
937 async function readAllKeys() {
938 let wifnames = await listManagedKeynames();
940 /** @type Array<RawKey> */
942 await wifnames.reduce(async function (promise, wifname) {
945 let keypath = Path.join(keysDir, wifname);
946 let key = await maybeReadKeyFileRaw(keypath);
951 if (`${key.addr}.wif` !== wifname) {
953 `computed pubkey '${key.addr}' of WIF does not match filename '${keypath}'`,
958 }, Promise.resolve());
964 * @param {String} filepath
965 * @param {Object} [opts]
966 * @param {Boolean} opts.wif
967 * @returns {Promise<String>}
969 async function maybeReadKeyFile(filepath, opts) {
970 let key = await maybeReadKeyFileRaw(filepath, opts);
971 if (false === opts?.wif) {
972 return key?.addr || "";
974 return key?.wif || "";
978 * @param {String} filepath
979 * @param {Object} [opts]
980 * @param {Boolean} opts.wif
981 * @returns {Promise<RawKey?>}
983 async function maybeReadKeyFileRaw(filepath, opts) {
984 let privKey = await Fs.readFile(filepath, "utf8").catch(
985 emptyStringOnErrEnoent,
987 privKey = privKey.trim();
992 let encrypted = false;
993 if (privKey.includes(":")) {
996 if (false !== opts?.wif) {
997 privKey = await decrypt(privKey);
1001 console.error(err.message);
1002 console.error(`passphrase does not match for key ${filepath}`);
1006 if (false === opts?.wif) {
1008 addr: Path.basename(filepath, ".wif"),
1009 encrypted: encrypted,
1014 let pk = new Dashcore.PrivateKey(privKey);
1015 let pub = pk.toAddress().toString();
1019 encrypted: encrypted,
1025 * @param {String} encWif
1027 async function decrypt(encWif) {
1028 let passphrase = cmds._getPassphrase();
1030 passphrase = await cmds.getPassphrase({}, []);
1032 let key128 = await Cipher.deriveKey(passphrase);
1033 let cipher = Cipher.create(key128);
1035 return cipher.decrypt(encWif);
1039 * @param {String} plainWif
1041 async function maybeEncrypt(plainWif) {
1042 let passphrase = cmds._getPassphrase();
1044 passphrase = await cmds.getPassphrase({}, []);
1050 let key128 = await Cipher.deriveKey(passphrase);
1051 let cipher = Cipher.create(key128);
1052 return cipher.encrypt(plainWif);
1057 * @param {Array<String>} args
1059 async function setDefault(_, args) {
1060 let addr = args.shift() || "";
1062 let keyname = await findWif(addr);
1064 console.error(`no key matches '${addr}'`);
1069 let filepath = Path.join(keysDir, keyname);
1070 let wif = await maybeReadKeyFile(filepath);
1071 let pk = new Dashcore.PrivateKey(wif);
1072 let pub = pk.toAddress().toString();
1074 console.info("set", defaultWifPath, pub);
1075 await Fs.writeFile(defaultWifPath, pub, "utf8");
1078 // TODO option to specify config dir
1081 * @param {Object} opts
1082 * @param {any} opts.dashApi - TODO
1083 * @param {Array<String>} args
1085 async function listKeys({ dashApi }, args) {
1086 let wifnames = await listManagedKeynames();
1089 * @type Array<{ node: String, error: Error }>
1093 console.info(`Staking keys: (in ${keysDirRel}/)`);
1095 if (!wifnames.length) {
1096 console.info(` (none)`);
1098 await wifnames.reduce(async function (promise, wifname) {
1101 let wifpath = Path.join(keysDir, wifname);
1102 let addr = await maybeReadKeyFile(wifpath, { wif: false }).catch(function (
1105 warns.push({ node: wifname, error: err });
1113 let pk = new Dashcore.PrivateKey(wif);
1114 let pub = pk.toAddress().toString();
1115 if (`${pub}.wif` !== wifname) {
1120 `computed pubkey '${pub}' of WIF does not match filename '${wifname}'`,
1127 process.stdout.write(` 🪙 ${addr}: `);
1128 let balanceInfo = await dashApi.getInstantBalance(addr);
1129 let balanceDash = toDash(balanceInfo.balanceSat);
1130 console.info(`${balanceInfo.balanceSat} (Đ${balanceDash})`);
1131 }, Promise.resolve());
1135 console.warn(`Warnings:`);
1136 warns.forEach(function (warn) {
1137 console.warn(`${warn.node}: ${warn.error.message}`);
1144 * @param {String} name - ex: Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.wif.enc
1146 function isNamedLikeKey(name) {
1147 // TODO distinguish with .enc extension?
1148 let hasGoodLength = 34 + 4 === name.length || 34 + 4 + 4 === name.length;
1149 let knownExt = name.endsWith(".wif") || name.endsWith(".wif.enc");
1150 let isTmp = name.startsWith(".") || name.startsWith("_");
1151 return hasGoodLength && knownExt && !isTmp;
1155 * @param {Object} opts
1156 * @param {any} opts.dashApi - TODO
1157 * @param {String} opts.defaultAddr
1158 * @param {String} opts.insightBaseUrl
1159 * @param {Array<String>} args
1161 async function removeKey({ dashApi, defaultAddr, insightBaseUrl }, args) {
1162 let [addr, name] = await mustGetAddr({ defaultAddr }, args);
1163 let balanceInfo = await dashApi.getInstantBalance(addr);
1165 let balanceDash = toDash(balanceInfo.balanceSat);
1166 if (balanceInfo.balanceSat) {
1168 console.error(`Error: ${addr}`);
1170 ` still has a balance of ${balanceInfo.balanceSat} (Đ${balanceDash})`,
1172 console.error(` (transfer to another address before deleting)`);
1178 await initCrowdNode(insightBaseUrl);
1179 let crowdNodeBalance = await CrowdNode.http.GetBalance(addr);
1180 if (!crowdNodeBalance) {
1181 // may be janky if not registered
1182 crowdNodeBalance = {};
1184 if (!crowdNodeBalance.TotalBalance) {
1185 //console.log('DEBUG', crowdNodeBalance);
1186 crowdNodeBalance.TotalBalance = 0;
1188 let crowdNodeDash = toDash(crowdNodeBalance.TotalBalance);
1189 if (crowdNodeBalance.TotalBalance) {
1191 console.error(`Error: ${addr}`);
1193 ` still staking ${crowdNodeBalance.TotalBalance} (Đ${crowdNodeDash}) on CrowdNode`,
1196 ` (withdrawal 100.0 and transfer to another address before deleting)`,
1203 let wifname = await findWif(addr);
1204 let filepath = Path.join(keysDir, wifname);
1205 let wif = await maybeReadKeyPaths(name, { wif: true });
1207 await Fs.unlink(filepath).catch(function (err) {
1208 console.error(`could not remove ${filepath}: ${err.message}`);
1212 let wifnames = await listManagedKeynames();
1214 console.info(`No balances found. Removing ${filepath}.`);
1216 console.info(`Backup (just in case):`);
1217 console.info(` ${wif}`);
1219 if (!wifnames.length) {
1220 console.info(`No keys left.`);
1223 let newAddr = wifnames[0];
1224 console.info(`Selected ${newAddr} as new default staking key.`);
1225 await Fs.writeFile(defaultWifPath, addr.replace(".wif", ""), "utf8");
1231 * @param {String} pre
1233 async function findWif(pre) {
1238 let names = await listManagedKeynames();
1239 names = names.filter(function (name) {
1240 return name.startsWith(pre);
1243 if (!names.length) {
1247 if (names.length > 1) {
1248 console.error(`'${pre}' is ambiguous:`, names.join(", "));
1256 async function listManagedKeynames() {
1257 let nodes = await Fs.readdir(keysDir);
1259 return nodes.filter(isNamedLikeKey);
1263 * @param {Object} opts
1264 * @param {String} opts.defaultAddr
1265 * @param {String} opts.insightBaseUrl
1266 * @param {Array<String>} args
1268 async function loadAddr({ defaultAddr, insightBaseUrl }, args) {
1269 let [addr] = await mustGetAddr({ defaultAddr }, args);
1271 let desiredAmountDash = parseFloat(args.shift() || "0");
1272 let desiredAmountDuff = Math.round(desiredAmountDash * DUFFS);
1273 let effectiveDuff = desiredAmountDuff;
1274 let effectiveDash = "";
1275 if (!effectiveDuff) {
1276 effectiveDuff = CrowdNode.stakeMinimum + signupTotal + feeEstimate;
1277 effectiveDash = toDash(effectiveDuff);
1278 // Round to the nearest mDash
1279 // ex: 0.50238108 => 0.50300000
1280 effectiveDuff = toDuff(
1281 (Math.ceil(parseFloat(effectiveDash) * 1000) / 1000).toString(),
1283 effectiveDash = toDash(effectiveDuff);
1287 showQr(addr, effectiveDuff);
1290 `Use the QR Code above to load ${effectiveDuff} (Đ${effectiveDash}) onto your staking key.`,
1293 console.info(`(waiting...)`);
1295 let payment = await Ws.waitForVout(insightBaseUrl, addr, 0);
1296 console.info(`Received ${payment.satoshis}`);
1301 * @param {Object} opts
1302 * @param {String} opts.defaultAddr
1303 * @param {any} opts.dashApi - TODO
1304 * @param {Array<String>} args
1306 async function getBalance({ dashApi, defaultAddr }, args) {
1307 let [addr] = await mustGetAddr({ defaultAddr }, args);
1308 let balanceInfo = await checkBalance({ addr, dashApi });
1309 console.info(balanceInfo);
1315 * @param {Object} opts
1316 * @param {any} opts.dashApi - TODO
1317 * @param {String} opts.defaultAddr
1318 * @param {Boolean} opts.forceConfirm
1319 * @param {String} opts.insightBaseUrl
1320 * @param {any} opts.insightApi
1321 * @param {Array<String>} args
1323 // ex: node ./bin/crowdnode.js transfer ./priv.wif 'pub' 0.01
1324 async function transferBalance(
1325 { dashApi, defaultAddr, forceConfirm, insightBaseUrl, insightApi },
1328 let wif = await mustGetWif({ defaultAddr }, args);
1330 let keyname = args.shift() || "";
1331 let newAddr = await wifFileToAddr(keyname);
1332 let dashAmount = parseFloat(args.shift() || "0");
1333 let duffAmount = Math.round(dashAmount * DUFFS);
1336 tx = await dashApi.createPayment(wif, newAddr, duffAmount);
1338 tx = await dashApi.createBalanceTransfer(wif, newAddr);
1341 let dashAmountStr = toDash(duffAmount);
1343 `Transferring ${duffAmount} (Đ${dashAmountStr}) to ${newAddr}...`,
1346 console.info(`Transferring balance to ${newAddr}...`);
1348 await insightApi.instantSend(tx);
1349 console.info(`Queued...`);
1350 setTimeout(function () {
1351 // TODO take a cleaner approach
1352 // (waitForVout needs a reasonable timeout)
1353 console.error(`Error: Transfer did not complete.`);
1355 console.error(`(using --unconfirmed may lead to rejected double spends)`);
1359 await Ws.waitForVout(insightBaseUrl, newAddr, 0);
1360 console.info(`Accepted!`);
1366 * @param {Object} opts
1367 * @param {any} opts.dashApi - TODO
1368 * @param {String} opts.defaultAddr
1369 * @param {String} opts.insightBaseUrl
1370 * @param {Array<String>} args
1372 async function getStatus({ dashApi, defaultAddr, insightBaseUrl }, args) {
1373 let [addr] = await mustGetAddr({ defaultAddr }, args);
1374 await initCrowdNode(insightBaseUrl);
1375 let hotwallet = CrowdNode.main.hotwallet;
1376 let state = await getCrowdNodeStatus({ addr, hotwallet });
1379 console.info(`API Actions Complete for ${addr}:`);
1380 console.info(` ${state.signup} SignUpForApi`);
1381 console.info(` ${state.accept} AcceptTerms`);
1382 console.info(` ${state.deposit} DepositReceived`);
1384 let crowdNodeBalance = await CrowdNode.http.GetBalance(addr);
1385 // may be unregistered / undefined
1388 * '@odata.context': 'https://app.crowdnode.io/odata/$metadata#Edm.String',
1389 * value: 'Address not found.'
1392 if (!crowdNodeBalance.TotalBalance) {
1393 crowdNodeBalance.TotalBalance = 0;
1395 let crowdNodeDash = toDash(crowdNodeBalance.TotalBalance);
1397 `CrowdNode Stake: ${crowdNodeBalance.TotalBalance} (Đ${crowdNodeDash})`,
1405 * @param {Object} opts
1406 * @param {any} opts.dashApi - TODO
1407 * @param {String} opts.defaultAddr
1408 * @param {String} opts.insightBaseUrl
1409 * @param {Array<String>} args
1411 async function sendSignup({ dashApi, defaultAddr, insightBaseUrl }, args) {
1412 let [addr, name] = await mustGetAddr({ defaultAddr }, args);
1413 await initCrowdNode(insightBaseUrl);
1414 let hotwallet = CrowdNode.main.hotwallet;
1415 let state = await getCrowdNodeStatus({ addr, hotwallet });
1416 let balanceInfo = await checkBalance({ addr, dashApi });
1418 if (state.status?.signup) {
1419 console.info(`${addr} is already signed up. Here's the account status:`);
1420 console.info(` ${state.signup} SignUpForApi`);
1421 console.info(` ${state.accept} AcceptTerms`);
1422 console.info(` ${state.deposit} DepositReceived`);
1427 let hasEnough = balanceInfo.balanceSat > signupOnly + feeEstimate;
1429 await collectSignupFees(insightBaseUrl, addr);
1432 let wif = await maybeReadKeyPaths(name, { wif: true });
1434 console.info("Requesting account...");
1435 await CrowdNode.signup(wif, hotwallet);
1437 console.info(` ${state.signup} SignUpForApi`);
1438 console.info(` ${state.accept} AcceptTerms`);
1444 * @param {Object} opts
1445 * @param {any} opts.dashApi - TODO
1446 * @param {String} opts.defaultAddr
1447 * @param {String} opts.insightBaseUrl
1448 * @param {Array<String>} args
1450 async function acceptTerms({ dashApi, defaultAddr, insightBaseUrl }, args) {
1451 let [addr, name] = await mustGetAddr({ defaultAddr }, args);
1453 await initCrowdNode(insightBaseUrl);
1454 let hotwallet = CrowdNode.main.hotwallet;
1455 let state = await getCrowdNodeStatus({ addr, hotwallet });
1456 let balanceInfo = await dashApi.getInstantBalance(addr);
1458 if (!state.status?.signup) {
1459 console.info(`${addr} is not signed up yet. Here's the account status:`);
1460 console.info(` ${state.signup} SignUpForApi`);
1461 console.info(` ${state.accept} AcceptTerms`);
1466 if (state.status?.accept) {
1467 console.info(`${addr} is already signed up. Here's the account status:`);
1468 console.info(` ${state.signup} SignUpForApi`);
1469 console.info(` ${state.accept} AcceptTerms`);
1470 console.info(` ${state.deposit} DepositReceived`);
1474 let hasEnough = balanceInfo.balanceSat > acceptOnly + feeEstimate;
1476 await collectSignupFees(insightBaseUrl, addr);
1479 let wif = await maybeReadKeyPaths(name, { wif: true });
1481 console.info("Accepting terms...");
1482 await CrowdNode.accept(wif, hotwallet);
1484 console.info(` ${state.signup} SignUpForApi`);
1485 console.info(` ${state.accept} AcceptTerms`);
1486 console.info(` ${state.deposit} DepositReceived`);
1492 * @param {Object} opts
1493 * @param {any} opts.dashApi - TODO
1494 * @param {String} opts.defaultAddr
1495 * @param {String} opts.insightBaseUrl
1496 * @param {Boolean} opts.noReserve
1497 * @param {Array<String>} args
1499 async function depositDash(
1500 { dashApi, defaultAddr, insightBaseUrl, noReserve },
1503 let [addr, name] = await mustGetAddr({ defaultAddr }, args);
1504 await initCrowdNode(insightBaseUrl);
1505 let hotwallet = CrowdNode.main.hotwallet;
1506 let state = await getCrowdNodeStatus({ addr, hotwallet });
1507 let balanceInfo = await dashApi.getInstantBalance(addr);
1509 if (!state.status?.accept) {
1510 console.error(`no account for address ${addr}`);
1515 // this would allow for at least 2 withdrawals costing (21000 + 1000)
1516 let reserve = 50000;
1517 let reserveDash = toDash(reserve);
1520 `reserving ${reserve} (Đ${reserveDash}) for withdrawals (--no-reserve to disable)`,
1526 // TODO if unconfirmed, check utxos instead
1528 // deposit what the user asks, or all that we have,
1529 // or all that the user deposits - but at least 2x the reserve
1530 let desiredAmountDash = parseFloat(args.shift() || "0");
1531 let desiredAmountDuff = Math.round(desiredAmountDash * DUFFS);
1532 let effectiveAmount = desiredAmountDuff;
1533 if (!effectiveAmount) {
1534 effectiveAmount = balanceInfo.balanceSat - reserve;
1536 let needed = Math.max(reserve * 2, effectiveAmount + reserve);
1538 if (balanceInfo.balanceSat < needed) {
1540 if (desiredAmountDuff) {
1541 ask = desiredAmountDuff + reserve + -balanceInfo.balanceSat;
1543 await collectDeposit(insightBaseUrl, addr, ask);
1544 balanceInfo = await dashApi.getInstantBalance(addr);
1545 if (balanceInfo.balanceSat < needed) {
1546 let balanceDash = toDash(balanceInfo.balanceSat);
1548 `Balance is still too small: ${balanceInfo.balanceSat} (Đ${balanceDash})`,
1554 if (!desiredAmountDuff) {
1555 effectiveAmount = balanceInfo.balanceSat - reserve;
1558 let effectiveDash = toDash(effectiveAmount);
1560 `Initiating deposit of ${effectiveAmount} (Đ${effectiveDash})...`,
1563 let wif = await maybeReadKeyPaths(name, { wif: true });
1565 await CrowdNode.deposit(wif, hotwallet, effectiveAmount);
1566 state.deposit = "✅";
1567 console.info(` ${state.deposit} DepositReceived`);
1573 * @param {Object} opts
1574 * @param {any} opts.dashApi - TODO
1575 * @param {String} opts.defaultAddr
1576 * @param {String} opts.insightBaseUrl
1577 * @param {Array<String>} args
1579 async function withdrawalDash({ dashApi, defaultAddr, insightBaseUrl }, args) {
1580 let [addr] = await mustGetAddr({ defaultAddr }, args);
1581 await initCrowdNode(insightBaseUrl);
1582 let hotwallet = CrowdNode.main.hotwallet;
1583 let state = await getCrowdNodeStatus({ addr, hotwallet });
1585 if (!state.status?.accept) {
1586 console.error(`no account for address ${addr}`);
1591 let percentStr = args.shift() || "100.0";
1592 // pass: .1 0.1, 1, 1.0, 10, 10.0, 100, 100.0
1593 // fail: 1000, 10.00
1594 if (!/^1?\d?\d?(\.\d)?$/.test(percentStr)) {
1595 console.error("Error: withdrawal percent must be between 0.1 and 100.0");
1598 let percent = parseFloat(percentStr);
1600 let permil = Math.round(percent * 10);
1601 if (permil <= 0 || permil > 1000) {
1602 console.error("Error: withdrawal percent must be between 0.1 and 100.0");
1606 let realPercentStr = (permil / 10).toFixed(1);
1607 console.info(`Initiating withdrawal of ${realPercentStr}...`);
1609 let wifname = await findWif(addr);
1610 let filepath = Path.join(keysDir, wifname);
1611 let wif = await maybeReadKeyFile(filepath);
1612 let paid = await CrowdNode.withdrawal(wif, hotwallet, permil);
1613 //let paidFloat = (paid.satoshis / DUFFS).toFixed(8);
1614 //let paidInt = paid.satoshis.toString().padStart(9, "0");
1615 console.info(`API Response: ${paid.api}`);
1623 * Convert prefix, addr, keyname, or filepath to pub addr
1624 * @param {String} name
1627 async function wifFileToAddr(name) {
1628 if (34 === name.length) {
1629 // actually payment addr
1635 let wifname = await findWif(name);
1637 let filepath = Path.join(keysDir, wifname);
1638 privKey = await maybeReadKeyFile(filepath);
1641 privKey = await maybeReadKeyFile(name);
1644 throw new Error("bad file path or address");
1647 let pk = new Dashcore.PrivateKey(privKey);
1648 let pub = pk.toPublicKey().toAddress().toString();
1653 * @param {String} insightBaseUrl
1654 * @param {String} addr
1656 async function collectSignupFees(insightBaseUrl, addr) {
1660 let signupTotalDash = toDash(signupTotal);
1661 let signupMsg = `Please send >= ${signupTotal} (Đ${signupTotalDash}) to Sign Up to CrowdNode`;
1662 let msgPad = Math.ceil((qrWidth - signupMsg.length) / 2);
1663 let subMsg = "(plus whatever you'd like to deposit)";
1664 let subMsgPad = Math.ceil((qrWidth - subMsg.length) / 2);
1667 console.info(" ".repeat(msgPad) + signupMsg);
1668 console.info(" ".repeat(subMsgPad) + subMsg);
1672 console.info("(waiting...)");
1674 let payment = await Ws.waitForVout(insightBaseUrl, addr, 0);
1675 console.info(`Received ${payment.satoshis}`);
1679 * @param {String} insightBaseUrl
1680 * @param {String} addr
1681 * @param {Number} duffAmount
1683 async function collectDeposit(insightBaseUrl, addr, duffAmount) {
1685 showQr(addr, duffAmount);
1687 let depositMsg = `Please send what you wish to deposit to ${addr}`;
1689 let dashAmount = toDash(duffAmount);
1690 depositMsg = `Please deposit ${duffAmount} (Đ${dashAmount}) to ${addr}`;
1693 let msgPad = Math.ceil((qrWidth - depositMsg.length) / 2);
1694 msgPad = Math.max(0, msgPad);
1697 console.info(" ".repeat(msgPad) + depositMsg);
1701 console.info("(waiting...)");
1703 let payment = await Ws.waitForVout(insightBaseUrl, addr, 0);
1704 console.info(`Received ${payment.satoshis}`);
1708 * @param {Error & { code: String }} err
1711 function emptyStringOnErrEnoent(err) {
1712 if ("ENOENT" !== err.code) {
1719 * @param {Number} duffs - ex: 00000000
1721 function toDash(duffs) {
1722 return (duffs / DUFFS).toFixed(8);
1726 * @param {String} dash - ex: 0.00000000
1728 function toDuff(dash) {
1729 return Math.round(parseFloat(dash) * DUFFS);
1734 main().catch(function (err) {
1735 console.error("Fail:");
1736 console.error(err.stack || err);