UNPKG

bitcoin-nanopayment

Version:
111 lines (93 loc) 3.37 kB
/** * This file has interactive CLI functions to help developers set up the config files for the tests. */ 'use strict'; // Internal imports var fs = require('fs'); var bitcoinNanopayment = require("bitcoin-nanopayment"); // External imports var prompt = require('prompt'); var sprintf = require("sprintf-js").sprintf; prompt.start(); // Return a boolean, and log whether the config file exists or not. var configFileExists = function(filePath) { try { fs.statSync(filePath); } catch (e) { if (e.errno != -2) { throw e; } console.log(sprintf("Config %s not found.\n", filePath)); return false; } console.log(sprintf("Config %s found.\n", filePath)); return true }; /* * Run the wizard, checking if the file exists, and, if not, prompting the user to fill it out. */ var wizard = function(configFilePath, intro, promptFor) { if (configFileExists(configFilePath)) { return; } console.log(intro + "\n"); prompt.get(promptFor, function (err, result) { var resultPretty = JSON.stringify(result, null, 4) + "\n"; fs.writeFileSync(configFilePath, resultPretty); console.log(sprintf('\nWrote config to %s : %s', configFilePath, resultPretty)); }); }; exports.wizardBitcoinConfig = function() { var configFilePath = "config/bitcoind.json"; var intro = 'This is the configuration for using bitcoind RPC as a backend. For information on how to launch ' + 'Bitcoin with the RPC server running, see here: https://en.bitcoin.it/wiki/Running_Bitcoin\n\nFor these ' + 'tests to run, the configuration in your bitcoin.conf, or on the Bitcoin command line, must match the ' + 'settings here.'; var promptFor = [ { name: 'host', description: 'Please enter the host on which bitcoind RPC is running', default: 'localhost' }, { name: 'port', description: 'Please enter the port on which bitcoind RPC is running', default: 18332 }, { name: 'user', description: 'Please enter the username for bitcoind RPC' }, { name: 'pass', description: 'Please enter the password for bitcoind RPC' } ]; wizard(configFilePath, intro, promptFor); }; exports.wizardTestAccountsConfig = function() { var configFilePath = "config/test-accounts.json"; var intro = sprintf('To run the tests, you must create two test accounts, one to send funds and the other to ' + 'receive them. Both accounts must have %i Satoshis or more.', bitcoinNanopayment.AMOUNT); var promptFor = [ { name: 'sourceAddress', description: 'Please enter the Base58 source address' }, { name: 'sourcePrivateKey', description: 'Please enter the Base58 source private key' }, { name: 'destinationAddress', description: 'Please enter the Base58 destination address' }, { name: 'destinationPrivateKey', description: 'Please enter the Base58 destination private key' } ]; wizard(configFilePath, intro, promptFor); }; exports.wizardBitcoinConfig(); exports.wizardTestAccountsConfig();