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: "micro" });
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(null, []);
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`);
706 * Encrypt ALL-the-things!
707 * @param {Object} [opts]
708 * @param {Boolean} opts.rotateKey
709 * @param {Array<RawKey>?} rawKeys
711 async function encryptAll(rawKeys, opts) {
713 rawKeys = await readAllKeys();
715 let date = getFsDateString();
717 console.info(`Encrypting...`);
719 await rawKeys.reduce(async function (promise, key) {
722 if (key.encrypted && !opts?.rotateKey) {
723 console.info(`🙈 ${key.addr} [already encrypted]`);
726 let encWif = await maybeEncrypt(key.wif);
728 Path.join(keysDir, `${key.addr}.wif`),
730 Path.join(keysDir, `${key.addr}.${date}.bak`),
732 console.info(`🔑 ${key.addr}`);
733 }, Promise.resolve());
735 console.info(`Done 🔐`);
740 * Decrypt ALL-the-things!
741 * @param {Array<RawKey>?} rawKeys
743 async function decryptAll(rawKeys) {
745 rawKeys = await readAllKeys();
747 let date = getFsDateString();
750 console.info(`Decrypting...`);
752 await rawKeys.reduce(async function (promise, key) {
755 if (!key.encrypted) {
756 console.info(`📖 ${key.addr} [already decrypted]`);
760 Path.join(keysDir, `${key.addr}.wif`),
762 Path.join(keysDir, `${key.addr}.${date}.bak`),
764 console.info(`🔓 ${key.addr}`);
765 }, Promise.resolve());
767 console.info(`Done ✅`);
771 function getFsDateString() {
772 // YYYY-MM-DD_hh-mm_ss
773 let date = new Date()
777 .replace(/\.\d{3}.*/, "");
782 * @param {String} filepath
783 * @param {String} wif
784 * @param {String} bakpath
786 async function safeSave(filepath, wif, bakpath) {
787 let tmpPath = `${bakpath}.tmp`;
788 await Fs.writeFile(tmpPath, wif, "utf8");
789 let err = await Fs.access(filepath).catch(Object);
791 await Fs.rename(filepath, bakpath);
793 await Fs.rename(tmpPath, filepath);
795 await Fs.unlink(bakpath);
800 * @param {Null} psuedoState
801 * @param {Array<String>} args
803 cmds.getPassphrase = async function (psuedoState, args) {
804 // Three possible states:
805 // 1. no shadow file yet (ask to set one)
806 // 2. empty shadow file (initialized, but not set - don't ask to set one)
807 // 3. encrypted shadow file (initialized, requires passphrase)
808 let needsInit = false;
809 let shadow = await Fs.readFile(shadowPath, "utf8").catch(function (err) {
810 if ("ENOENT" === err.code) {
817 // State 1: not initialized, what does the user want?
820 let no = await Prompt.prompt(
821 "Would you like to set an encryption passphrase? [Y/n]: ",
824 // Set a passphrase and create shadow file
825 if (!no || ["yes", "y"].includes(no.toLowerCase())) {
826 let passphrase = await setPassphrase(
827 { _askPreviousPassphrase: false },
830 cmds._setPassphrase(passphrase);
835 if (!["no", "n"].includes(no.toLowerCase())) {
839 // No passphrase, create empty shadow file
840 await Fs.writeFile(shadowPath, "", "utf8");
845 // State 2: shadow already initialized to empty
846 // (user doesn't want a passphrase)
848 cmds._setPassphrase("");
852 // State 3: passphrase & shadow already in use
854 let passphrase = await Prompt.prompt("Enter (current) passphrase: ", {
857 passphrase = passphrase.trim();
858 if (!passphrase || "q" === passphrase) {
859 console.error("cancel: no passphrase");
864 let match = await Cipher.checkPassphrase(passphrase, shadow);
866 cmds._setPassphrase(passphrase);
871 console.error("incorrect passphrase");
874 throw new Error("SANITY FAIL: unreachable return");
877 cmds._getPassphrase = function () {
882 * @param {String} passphrase
884 cmds._setPassphrase = function (passphrase) {
885 // Look Ma! A private variable!
886 cmds._getPassphrase = function () {
892 * @typedef {Object} RawKey
893 * @property {String} addr
894 * @property {Boolean} encrypted
895 * @property {String} wif
901 async function readAllKeys() {
902 let wifnames = await listManagedKeynames();
904 /** @type Array<RawKey> */
906 await wifnames.reduce(async function (promise, wifname) {
909 let keypath = Path.join(keysDir, wifname);
910 let key = await maybeReadKeyFileRaw(keypath);
915 if (`${key.addr}.wif` !== wifname) {
917 `computed pubkey '${key.addr}' of WIF does not match filename '${keypath}'`,
922 }, Promise.resolve());
928 * @param {String} filepath
929 * @param {Object} [opts]
930 * @param {Boolean} opts.wif
931 * @returns {Promise<String>}
933 async function maybeReadKeyFile(filepath, opts) {
934 let key = await maybeReadKeyFileRaw(filepath, opts);
935 if (false === opts?.wif) {
936 return key?.addr || "";
938 return key?.wif || "";
942 * @param {String} filepath
943 * @param {Object} [opts]
944 * @param {Boolean} opts.wif
945 * @returns {Promise<RawKey?>}
947 async function maybeReadKeyFileRaw(filepath, opts) {
948 let privKey = await Fs.readFile(filepath, "utf8").catch(
949 emptyStringOnErrEnoent,
951 privKey = privKey.trim();
956 let encrypted = false;
957 if (privKey.includes(":")) {
960 if (false !== opts?.wif) {
961 privKey = await decrypt(privKey);
965 console.error(err.message);
966 console.error(`passphrase does not match for key ${filepath}`);
970 if (false === opts?.wif) {
972 addr: Path.basename(filepath, ".wif"),
973 encrypted: encrypted,
978 let pk = new Dashcore.PrivateKey(privKey);
979 let pub = pk.toAddress().toString();
983 encrypted: encrypted,
989 * @param {String} encWif
991 async function decrypt(encWif) {
992 let passphrase = cmds._getPassphrase();
994 passphrase = await cmds.getPassphrase(null, []);
996 let key128 = await Cipher.deriveKey(passphrase);
997 let cipher = Cipher.create(key128);
999 return cipher.decrypt(encWif);
1003 * @param {String} plainWif
1005 async function maybeEncrypt(plainWif) {
1006 let passphrase = cmds._getPassphrase();
1008 passphrase = await cmds.getPassphrase(null, []);
1014 let key128 = await Cipher.deriveKey(passphrase);
1015 let cipher = Cipher.create(key128);
1016 return cipher.encrypt(plainWif);
1021 * @param {Array<String>} args
1023 async function setDefault(_, args) {
1024 let addr = args.shift() || "";
1026 let keyname = await findWif(addr);
1028 console.error(`no key matches '${addr}'`);
1033 let filepath = Path.join(keysDir, keyname);
1034 let wif = await maybeReadKeyFile(filepath);
1035 let pk = new Dashcore.PrivateKey(wif);
1036 let pub = pk.toAddress().toString();
1038 console.info("set", defaultWifPath, pub);
1039 await Fs.writeFile(defaultWifPath, pub, "utf8");
1042 // TODO option to specify config dir
1045 * @param {Object} opts
1046 * @param {any} opts.dashApi - TODO
1047 * @param {Array<String>} args
1049 async function listKeys({ dashApi }, args) {
1050 let wifnames = await listManagedKeynames();
1053 * @type Array<{ node: String, error: Error }>
1057 console.info(`Staking keys: (in ${keysDirRel}/)`);
1059 if (!wifnames.length) {
1060 console.info(` (none)`);
1062 await wifnames.reduce(async function (promise, wifname) {
1065 let wifpath = Path.join(keysDir, wifname);
1066 let addr = await maybeReadKeyFile(wifpath, { wif: false }).catch(function (
1069 warns.push({ node: wifname, error: err });
1077 let pk = new Dashcore.PrivateKey(wif);
1078 let pub = pk.toAddress().toString();
1079 if (`${pub}.wif` !== wifname) {
1084 `computed pubkey '${pub}' of WIF does not match filename '${wifname}'`,
1091 process.stdout.write(` 🪙 ${addr}: `);
1092 let balanceInfo = await dashApi.getInstantBalance(addr);
1093 let balanceDash = toDash(balanceInfo.balanceSat);
1094 console.info(`${balanceInfo.balanceSat} (Đ${balanceDash})`);
1095 }, Promise.resolve());
1099 console.warn(`Warnings:`);
1100 warns.forEach(function (warn) {
1101 console.warn(`${warn.node}: ${warn.error.message}`);
1108 * @param {String} name - ex: Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.wif.enc
1110 function isNamedLikeKey(name) {
1111 // TODO distinguish with .enc extension?
1112 let hasGoodLength = 34 + 4 === name.length || 34 + 4 + 4 === name.length;
1113 let knownExt = name.endsWith(".wif") || name.endsWith(".wif.enc");
1114 let isTmp = name.startsWith(".") || name.startsWith("_");
1115 return hasGoodLength && knownExt && !isTmp;
1119 * @param {Object} opts
1120 * @param {any} opts.dashApi - TODO
1121 * @param {String} opts.defaultAddr
1122 * @param {String} opts.insightBaseUrl
1123 * @param {Array<String>} args
1125 async function removeKey({ dashApi, defaultAddr, insightBaseUrl }, args) {
1126 let [addr, name] = await mustGetAddr({ defaultAddr }, args);
1127 let balanceInfo = await dashApi.getInstantBalance(addr);
1129 let balanceDash = toDash(balanceInfo.balanceSat);
1130 if (balanceInfo.balanceSat) {
1132 console.error(`Error: ${addr}`);
1134 ` still has a balance of ${balanceInfo.balanceSat} (Đ${balanceDash})`,
1136 console.error(` (transfer to another address before deleting)`);
1142 await initCrowdNode(insightBaseUrl);
1143 let crowdNodeBalance = await CrowdNode.http.GetBalance(addr);
1144 if (!crowdNodeBalance) {
1145 // may be janky if not registered
1146 crowdNodeBalance = {};
1148 if (!crowdNodeBalance.TotalBalance) {
1149 //console.log('DEBUG', crowdNodeBalance);
1150 crowdNodeBalance.TotalBalance = 0;
1152 let crowdNodeDash = toDash(crowdNodeBalance.TotalBalance);
1153 if (crowdNodeBalance.TotalBalance) {
1155 console.error(`Error: ${addr}`);
1157 ` still staking ${crowdNodeBalance.TotalBalance} (Đ${crowdNodeDash}) on CrowdNode`,
1160 ` (withdrawal 100.0 and transfer to another address before deleting)`,
1167 let wifname = await findWif(addr);
1168 let filepath = Path.join(keysDir, wifname);
1169 let wif = await maybeReadKeyPaths(name, { wif: true });
1171 await Fs.unlink(filepath).catch(function (err) {
1172 console.error(`could not remove ${filepath}: ${err.message}`);
1176 let wifnames = await listManagedKeynames();
1178 console.info(`No balances found. Removing ${filepath}.`);
1180 console.info(`Backup (just in case):`);
1181 console.info(` ${wif}`);
1183 if (!wifnames.length) {
1184 console.info(`No keys left.`);
1187 let newAddr = wifnames[0];
1188 console.info(`Selected ${newAddr} as new default staking key.`);
1189 await Fs.writeFile(defaultWifPath, addr.replace(".wif", ""), "utf8");
1195 * @param {String} pre
1197 async function findWif(pre) {
1202 let names = await listManagedKeynames();
1203 names = names.filter(function (name) {
1204 return name.startsWith(pre);
1207 if (!names.length) {
1211 if (names.length > 1) {
1212 console.error(`'${pre}' is ambiguous:`, names.join(", "));
1220 async function listManagedKeynames() {
1221 let nodes = await Fs.readdir(keysDir);
1223 return nodes.filter(isNamedLikeKey);
1227 * @param {Object} opts
1228 * @param {String} opts.defaultAddr
1229 * @param {String} opts.insightBaseUrl
1230 * @param {Array<String>} args
1232 async function loadAddr({ defaultAddr, insightBaseUrl }, args) {
1233 let [addr] = await mustGetAddr({ defaultAddr }, args);
1235 let desiredAmountDash = parseFloat(args.shift() || "0");
1236 let desiredAmountDuff = Math.round(desiredAmountDash * DUFFS);
1237 let effectiveDuff = desiredAmountDuff;
1238 let effectiveDash = "";
1239 if (!effectiveDuff) {
1240 effectiveDuff = CrowdNode.stakeMinimum + signupTotal + feeEstimate;
1241 effectiveDash = toDash(effectiveDuff);
1242 // Round to the nearest mDash
1243 // ex: 0.50238108 => 0.50300000
1244 effectiveDuff = toDuff(
1245 (Math.ceil(parseFloat(effectiveDash) * 1000) / 1000).toString(),
1247 effectiveDash = toDash(effectiveDuff);
1251 showQr(addr, effectiveDuff);
1254 `Use the QR Code above to load ${effectiveDuff} (Đ${effectiveDash}) onto your staking key.`,
1257 console.info(`(waiting...)`);
1259 let payment = await Ws.waitForVout(insightBaseUrl, addr, 0);
1260 console.info(`Received ${payment.satoshis}`);
1265 * @param {Object} opts
1266 * @param {String} opts.defaultAddr
1267 * @param {any} opts.dashApi - TODO
1268 * @param {Array<String>} args
1270 async function getBalance({ dashApi, defaultAddr }, args) {
1271 let [addr] = await mustGetAddr({ defaultAddr }, args);
1272 let balanceInfo = await checkBalance({ addr, dashApi });
1273 console.info(balanceInfo);
1279 * @param {Object} opts
1280 * @param {any} opts.dashApi - TODO
1281 * @param {String} opts.defaultAddr
1282 * @param {Boolean} opts.forceConfirm
1283 * @param {String} opts.insightBaseUrl
1284 * @param {any} opts.insightApi
1285 * @param {Array<String>} args
1287 // ex: node ./bin/crowdnode.js transfer ./priv.wif 'pub' 0.01
1288 async function transferBalance(
1289 { dashApi, defaultAddr, forceConfirm, insightBaseUrl, insightApi },
1292 let wif = await mustGetWif({ defaultAddr }, args);
1294 let keyname = args.shift() || "";
1295 let newAddr = await wifFileToAddr(keyname);
1296 let dashAmount = parseFloat(args.shift() || "0");
1297 let duffAmount = Math.round(dashAmount * DUFFS);
1300 tx = await dashApi.createPayment(wif, newAddr, duffAmount);
1302 tx = await dashApi.createBalanceTransfer(wif, newAddr);
1305 let dashAmountStr = toDash(duffAmount);
1307 `Transferring ${duffAmount} (Đ${dashAmountStr}) to ${newAddr}...`,
1310 console.info(`Transferring balance to ${newAddr}...`);
1312 await insightApi.instantSend(tx);
1313 console.info(`Queued...`);
1314 setTimeout(function () {
1315 // TODO take a cleaner approach
1316 // (waitForVout needs a reasonable timeout)
1317 console.error(`Error: Transfer did not complete.`);
1319 console.error(`(using --unconfirmed may lead to rejected double spends)`);
1323 await Ws.waitForVout(insightBaseUrl, newAddr, 0);
1324 console.info(`Accepted!`);
1330 * @param {Object} opts
1331 * @param {any} opts.dashApi - TODO
1332 * @param {String} opts.defaultAddr
1333 * @param {String} opts.insightBaseUrl
1334 * @param {Array<String>} args
1336 async function getStatus({ dashApi, defaultAddr, insightBaseUrl }, args) {
1337 let [addr] = await mustGetAddr({ defaultAddr }, args);
1338 await initCrowdNode(insightBaseUrl);
1339 let hotwallet = CrowdNode.main.hotwallet;
1340 let state = await getCrowdNodeStatus({ addr, hotwallet });
1343 console.info(`API Actions Complete for ${addr}:`);
1344 console.info(` ${state.signup} SignUpForApi`);
1345 console.info(` ${state.accept} AcceptTerms`);
1346 console.info(` ${state.deposit} DepositReceived`);
1348 let crowdNodeBalance = await CrowdNode.http.GetBalance(addr);
1349 // may be unregistered / undefined
1352 * '@odata.context': 'https://app.crowdnode.io/odata/$metadata#Edm.String',
1353 * value: 'Address not found.'
1356 if (!crowdNodeBalance.TotalBalance) {
1357 crowdNodeBalance.TotalBalance = 0;
1359 let crowdNodeDash = toDash(crowdNodeBalance.TotalBalance);
1361 `CrowdNode Stake: ${crowdNodeBalance.TotalBalance} (Đ${crowdNodeDash})`,
1369 * @param {Object} opts
1370 * @param {any} opts.dashApi - TODO
1371 * @param {String} opts.defaultAddr
1372 * @param {String} opts.insightBaseUrl
1373 * @param {Array<String>} args
1375 async function sendSignup({ dashApi, defaultAddr, insightBaseUrl }, args) {
1376 let [addr, name] = await mustGetAddr({ defaultAddr }, args);
1377 await initCrowdNode(insightBaseUrl);
1378 let hotwallet = CrowdNode.main.hotwallet;
1379 let state = await getCrowdNodeStatus({ addr, hotwallet });
1380 let balanceInfo = await checkBalance({ addr, dashApi });
1382 if (state.status?.signup) {
1383 console.info(`${addr} is already signed up. Here's the account status:`);
1384 console.info(` ${state.signup} SignUpForApi`);
1385 console.info(` ${state.accept} AcceptTerms`);
1386 console.info(` ${state.deposit} DepositReceived`);
1391 let hasEnough = balanceInfo.balanceSat > signupOnly + feeEstimate;
1393 await collectSignupFees(insightBaseUrl, addr);
1396 let wif = await maybeReadKeyPaths(name, { wif: true });
1398 console.info("Requesting account...");
1399 await CrowdNode.signup(wif, hotwallet);
1401 console.info(` ${state.signup} SignUpForApi`);
1402 console.info(` ${state.accept} AcceptTerms`);
1408 * @param {Object} opts
1409 * @param {any} opts.dashApi - TODO
1410 * @param {String} opts.defaultAddr
1411 * @param {String} opts.insightBaseUrl
1412 * @param {Array<String>} args
1414 async function acceptTerms({ dashApi, defaultAddr, insightBaseUrl }, args) {
1415 let [addr, name] = await mustGetAddr({ defaultAddr }, args);
1417 await initCrowdNode(insightBaseUrl);
1418 let hotwallet = CrowdNode.main.hotwallet;
1419 let state = await getCrowdNodeStatus({ addr, hotwallet });
1420 let balanceInfo = await dashApi.getInstantBalance(addr);
1422 if (!state.status?.signup) {
1423 console.info(`${addr} is not signed up yet. Here's the account status:`);
1424 console.info(` ${state.signup} SignUpForApi`);
1425 console.info(` ${state.accept} AcceptTerms`);
1430 if (state.status?.accept) {
1431 console.info(`${addr} is already signed up. Here's the account status:`);
1432 console.info(` ${state.signup} SignUpForApi`);
1433 console.info(` ${state.accept} AcceptTerms`);
1434 console.info(` ${state.deposit} DepositReceived`);
1438 let hasEnough = balanceInfo.balanceSat > acceptOnly + feeEstimate;
1440 await collectSignupFees(insightBaseUrl, addr);
1443 let wif = await maybeReadKeyPaths(name, { wif: true });
1445 console.info("Accepting terms...");
1446 await CrowdNode.accept(wif, hotwallet);
1448 console.info(` ${state.signup} SignUpForApi`);
1449 console.info(` ${state.accept} AcceptTerms`);
1450 console.info(` ${state.deposit} DepositReceived`);
1456 * @param {Object} opts
1457 * @param {any} opts.dashApi - TODO
1458 * @param {String} opts.defaultAddr
1459 * @param {String} opts.insightBaseUrl
1460 * @param {Boolean} opts.noReserve
1461 * @param {Array<String>} args
1463 async function depositDash(
1464 { dashApi, defaultAddr, insightBaseUrl, noReserve },
1467 let [addr, name] = await mustGetAddr({ defaultAddr }, args);
1468 await initCrowdNode(insightBaseUrl);
1469 let hotwallet = CrowdNode.main.hotwallet;
1470 let state = await getCrowdNodeStatus({ addr, hotwallet });
1471 let balanceInfo = await dashApi.getInstantBalance(addr);
1473 if (!state.status?.accept) {
1474 console.error(`no account for address ${addr}`);
1479 // this would allow for at least 2 withdrawals costing (21000 + 1000)
1480 let reserve = 50000;
1481 let reserveDash = toDash(reserve);
1484 `reserving ${reserve} (Đ${reserveDash}) for withdrawals (--no-reserve to disable)`,
1490 // TODO if unconfirmed, check utxos instead
1492 // deposit what the user asks, or all that we have,
1493 // or all that the user deposits - but at least 2x the reserve
1494 let desiredAmountDash = parseFloat(args.shift() || "0");
1495 let desiredAmountDuff = Math.round(desiredAmountDash * DUFFS);
1496 let effectiveAmount = desiredAmountDuff;
1497 if (!effectiveAmount) {
1498 effectiveAmount = balanceInfo.balanceSat - reserve;
1500 let needed = Math.max(reserve * 2, effectiveAmount + reserve);
1502 if (balanceInfo.balanceSat < needed) {
1504 if (desiredAmountDuff) {
1505 ask = desiredAmountDuff + reserve + -balanceInfo.balanceSat;
1507 await collectDeposit(insightBaseUrl, addr, ask);
1508 balanceInfo = await dashApi.getInstantBalance(addr);
1509 if (balanceInfo.balanceSat < needed) {
1510 let balanceDash = toDash(balanceInfo.balanceSat);
1512 `Balance is still too small: ${balanceInfo.balanceSat} (Đ${balanceDash})`,
1518 if (!desiredAmountDuff) {
1519 effectiveAmount = balanceInfo.balanceSat - reserve;
1522 let effectiveDash = toDash(effectiveAmount);
1524 `Initiating deposit of ${effectiveAmount} (Đ${effectiveDash})...`,
1527 let wif = await maybeReadKeyPaths(name, { wif: true });
1529 await CrowdNode.deposit(wif, hotwallet, effectiveAmount);
1530 state.deposit = "✅";
1531 console.info(` ${state.deposit} DepositReceived`);
1537 * @param {Object} opts
1538 * @param {any} opts.dashApi - TODO
1539 * @param {String} opts.defaultAddr
1540 * @param {String} opts.insightBaseUrl
1541 * @param {Array<String>} args
1543 async function withdrawalDash({ dashApi, defaultAddr, insightBaseUrl }, args) {
1544 let [addr] = await mustGetAddr({ defaultAddr }, args);
1545 await initCrowdNode(insightBaseUrl);
1546 let hotwallet = CrowdNode.main.hotwallet;
1547 let state = await getCrowdNodeStatus({ addr, hotwallet });
1549 if (!state.status?.accept) {
1550 console.error(`no account for address ${addr}`);
1555 let percentStr = args.shift() || "100.0";
1556 // pass: .1 0.1, 1, 1.0, 10, 10.0, 100, 100.0
1557 // fail: 1000, 10.00
1558 if (!/^1?\d?\d?(\.\d)?$/.test(percentStr)) {
1559 console.error("Error: withdrawal percent must be between 0.1 and 100.0");
1562 let percent = parseFloat(percentStr);
1564 let permil = Math.round(percent * 10);
1565 if (permil <= 0 || permil > 1000) {
1566 console.error("Error: withdrawal percent must be between 0.1 and 100.0");
1570 let realPercentStr = (permil / 10).toFixed(1);
1571 console.info(`Initiating withdrawal of ${realPercentStr}...`);
1573 let wifname = await findWif(addr);
1574 let filepath = Path.join(keysDir, wifname);
1575 let wif = await maybeReadKeyFile(filepath);
1576 let paid = await CrowdNode.withdrawal(wif, hotwallet, permil);
1577 //let paidFloat = (paid.satoshis / DUFFS).toFixed(8);
1578 //let paidInt = paid.satoshis.toString().padStart(9, "0");
1579 console.info(`API Response: ${paid.api}`);
1587 * Convert prefix, addr, keyname, or filepath to pub addr
1588 * @param {String} name
1591 async function wifFileToAddr(name) {
1592 if (34 === name.length) {
1593 // actually payment addr
1599 let wifname = await findWif(name);
1601 let filepath = Path.join(keysDir, wifname);
1602 privKey = await maybeReadKeyFile(filepath);
1605 privKey = await maybeReadKeyFile(name);
1608 throw new Error("bad file path or address");
1611 let pk = new Dashcore.PrivateKey(privKey);
1612 let pub = pk.toPublicKey().toAddress().toString();
1617 * @param {String} insightBaseUrl
1618 * @param {String} addr
1620 async function collectSignupFees(insightBaseUrl, addr) {
1624 let signupTotalDash = toDash(signupTotal);
1625 let signupMsg = `Please send >= ${signupTotal} (Đ${signupTotalDash}) to Sign Up to CrowdNode`;
1626 let msgPad = Math.ceil((qrWidth - signupMsg.length) / 2);
1627 let subMsg = "(plus whatever you'd like to deposit)";
1628 let subMsgPad = Math.ceil((qrWidth - subMsg.length) / 2);
1631 console.info(" ".repeat(msgPad) + signupMsg);
1632 console.info(" ".repeat(subMsgPad) + subMsg);
1636 console.info("(waiting...)");
1638 let payment = await Ws.waitForVout(insightBaseUrl, addr, 0);
1639 console.info(`Received ${payment.satoshis}`);
1643 * @param {String} insightBaseUrl
1644 * @param {String} addr
1645 * @param {Number} duffAmount
1647 async function collectDeposit(insightBaseUrl, addr, duffAmount) {
1649 showQr(addr, duffAmount);
1651 let depositMsg = `Please send what you wish to deposit to ${addr}`;
1653 let dashAmount = toDash(duffAmount);
1654 depositMsg = `Please deposit ${duffAmount} (Đ${dashAmount}) to ${addr}`;
1657 let msgPad = Math.ceil((qrWidth - depositMsg.length) / 2);
1658 msgPad = Math.max(0, msgPad);
1661 console.info(" ".repeat(msgPad) + depositMsg);
1665 console.info("(waiting...)");
1667 let payment = await Ws.waitForVout(insightBaseUrl, addr, 0);
1668 console.info(`Received ${payment.satoshis}`);
1672 * @param {Error & { code: String }} err
1675 function emptyStringOnErrEnoent(err) {
1676 if ("ENOENT" !== err.code) {
1683 * @param {Number} duffs - ex: 00000000
1685 function toDash(duffs) {
1686 return (duffs / DUFFS).toFixed(8);
1690 * @param {String} dash - ex: 0.00000000
1692 function toDuff(dash) {
1693 return Math.round(parseFloat(dash) * DUFFS);
1698 main().catch(function (err) {
1699 console.error("Fail:");
1700 console.error(err.stack || err);