UNPKG

xud

Version:
105 lines 4.3 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.showSeed = exports.waitForCert = exports.satsToCoinsStr = exports.coinsToSats = exports.generateHeaders = exports.trim = exports.shorten = exports.getDefaultCertPath = void 0; const safe_1 = __importDefault(require("colors/safe")); const fs_1 = require("fs"); const os_1 = __importDefault(require("os")); const path_1 = __importDefault(require("path")); const SATOSHIS_PER_COIN = 10 ** 8; function getDefaultCertPath() { switch (os_1.default.platform()) { case 'win32': { const homeDir = process.env.LOCALAPPDATA; return path_1.default.join(homeDir, 'Xud', 'tls.cert'); } case 'darwin': { const homeDir = process.env.HOME; return path_1.default.join(homeDir, '.xud', 'tls.cert'); } default: { const homeDir = process.env.HOME; return path_1.default.join(homeDir, '.xud', 'tls.cert'); } } } exports.getDefaultCertPath = getDefaultCertPath; function shorten(key, length = 10) { if (key.length <= (length * 2) + 3) { return key; } return `${key.slice(0, length)}...${key.slice(key.length - length)}`; } exports.shorten = shorten; function trim(key, length = 10) { if (key.length <= length + 3) { return ''; } return `${key.slice(0, length)}...`; } exports.trim = trim; exports.generateHeaders = (headers) => { return headers.map((header) => { return safe_1.default.blue(header); }); }; /** Returns a number of coins as an integer number of satoshis. */ exports.coinsToSats = (coinsQuantity) => { return Math.round(coinsQuantity * SATOSHIS_PER_COIN); }; /** Returns a number of satoshis as a string representation of coins with up to 8 decimal places. */ exports.satsToCoinsStr = (satsQuantity) => { return (satsQuantity / SATOSHIS_PER_COIN).toFixed(8).replace(/\.?0+$/, ''); }; /** Waits up to 5 seconds for the tls.cert file to be created in case this is the first time xud has been run. */ exports.waitForCert = (certPath) => { return new Promise((resolve, reject) => { try { fs_1.accessSync(certPath); resolve(); } catch (err) { if (err.code === 'ENOENT') { const certDir = path_1.default.dirname(certPath); const certFilename = path_1.default.basename(certPath); const fsWatcher = fs_1.watch(certDir, (event, filename) => { if (event === 'change' && filename === certFilename) { clearTimeout(timeout); fsWatcher.close(); resolve(); } }); const timeout = setTimeout(() => { fsWatcher.close(); reject(`timed out waiting for cert to be created at ${certPath}`); }, 5000); } else { // we handle errors due to file not existing, otherwise reject reject(err); } } }); }; function showSeed(seedMnemonicList) { const WORDS_PER_ROW = 4; const numberedMnemonic = seedMnemonicList.map((value, index) => { return `${index >= 9 ? '' : ' '}${index + 1}. ${value.padEnd(10)}`; }); console.log('----------------------BEGIN XUD SEED---------------------'); for (let n = 0; n < seedMnemonicList.length / WORDS_PER_ROW; n += 1) { console.log(numberedMnemonic.slice(n * WORDS_PER_ROW, (n + 1) * WORDS_PER_ROW).join(' ')); } console.log('-----------------------END XUD SEED----------------------\n'); console.log(` Please write down your 24 word mnemonic. It will allow you to recover your xud \ node key and on-chain funds for the initialized wallets listed above should you \ forget your password or lose your device. Off-chain funds in channels can NOT \ be recovered with it and must be backed up and recovered separately. Keep it \ somewhere safe, it is your ONLY backup in case of data loss. `); } exports.showSeed = showSeed; //# sourceMappingURL=utils.js.map