Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1501if (!result["balances"].isNull()) {1502result_string += strprintf("%sBalances%s\n", CYAN, RESET);1504size_t max_balance_length{10};1506for (const std::string& wallet : result["balances"].getKeys()) {1507max_balance_length = std::max(result["balances"][wallet].getValStr().length(), max_balance_length);1508}1510for (const std::string& wallet : result["balances"].getKeys()) {1511result_string += strprintf("%*s %s\n",1512max_balance_length,1513result["balances"][wallet].getValStr(),1514wallet.empty() ? "\"\"" : wallet);1515}1516result_string += "\n";1517}1519const std::string warnings{result["warnings"].getValStr()};1520result_string += strprintf("%sWarnings:%s %s", YELLOW, RESET, warnings.empty() ? "(none)" : warnings);1522result.setStr(result_string);1523}1525/**1526* Call RPC getnewaddress.1527* @returns getnewaddress response as a UniValue object.1528*/1529static UniValue GetNewAddress()1530{1531DefaultRequestHandler rh;1532return ConnectAndCallRPC(&rh, "getnewaddress", /* args=*/{}, RpcWalletName(gArgs));1533}1535/**1536* Check bounds and set up args for RPC generatetoaddress params: nblocks, address, maxtries.1537* @param[in] address Reference to const string address to insert into the args.1538* @param args Reference to vector of string args to modify.1539*/1540static void SetGenerateToAddressArgs(const std::string& address, std::vector<std::string>& args)1541{1542if (args.size() > 2) throw std::runtime_error("too many arguments (maximum 2 for nblocks and maxtries)");1543if (args.size() == 0) {1544args.emplace_back(DEFAULT_NBLOCKS);1545} else if (args.at(0) == "0") {1546throw std::runtime_error("the first argument (number of blocks to generate, default: " + DEFAULT_NBLOCKS + ") must be an integer value greater than zero");1547}1548args.emplace(args.begin() + 1, address);1549}1551static int CommandLineRPC(int argc, char *argv[])1552{1553std::string strPrint;1554int nRet = 0;1555try {1556// Skip switches1557while (argc > 1 && IsSwitchChar(argv[1][0])) {1558argc--;1559argv++;1560}1561std::string rpcPass;1562if (gArgs.GetBoolArg("-stdinrpcpass", false)) {1563NO_STDIN_ECHO();1564if (!StdinReady()) {1565fputs("RPC password> ", stderr);1566fflush(stderr);1567}1568if (!std::getline(std::cin, rpcPass)) {1569throw std::runtime_error("-stdinrpcpass specified but failed to read from standard input");1570}1571if (StdinTerminal()) {1572fputc('\n', stdout);1573}1574gArgs.ForceSetArg("-rpcpassword", rpcPass);1575}1576std::vector<std::string> args = std::vector<std::string>(&argv[1], &argv[argc]);1577if (gArgs.GetBoolArg("-stdinwalletpassphrase", false)) {1578NO_STDIN_ECHO();1579std::string walletPass;1580if (args.size() < 1 || !args[0].starts_with("walletpassphrase")) {1581throw std::runtime_error("-stdinwalletpassphrase is only applicable for walletpassphrase(change)");1582}1583if (!StdinReady()) {1584fputs("Wallet passphrase> ", stderr);1585fflush(stderr);1586}1587if (!std::getline(std::cin, walletPass)) {1588throw std::runtime_error("-stdinwalletpassphrase specified but failed to read from standard input");1589}1590if (StdinTerminal()) {1591fputc('\n', stdout);1592}1593args.insert(args.begin() + 1, walletPass);1594}1595if (gArgs.GetBoolArg("-stdin", false)) {1596// Read one arg per line from stdin and append1597std::string line;1598while (std::getline(std::cin, line)) {1599args.push_back(line);1600}1601if (StdinTerminal()) {1602fputc('\n', stdout);1603}1604}1605gArgs.CheckMultipleCLIArgs();1606std::unique_ptr<BaseRequestHandler> rh;1607std::string method;1608if (gArgs.GetBoolArg("-getinfo", false)) {1609rh.reset(new GetinfoRequestHandler());1610} else if (gArgs.GetBoolArg("-netinfo", false)) {1611if (!args.empty() && (args.at(0) == "h" || args.at(0) == "help")) {1612tfm::format(std::cout, "%s\n", NetinfoRequestHandler().m_help_doc);1613return 0;1614}1615rh.reset(new NetinfoRequestHandler());1616} else if (gArgs.GetBoolArg("-generate", false)) {1617const UniValue getnewaddress{GetNewAddress()};1618const UniValue& error{getnewaddress.find_value("error")};1619if (error.isNull()) {1620SetGenerateToAddressArgs(getnewaddress.find_value("result").get_str(), args);1621rh.reset(new GenerateToAddressRequestHandler());1622} else {1623ParseError(error, strPrint, nRet);1624}1625} else if (gArgs.GetBoolArg("-addrinfo", false)) {1626rh.reset(new AddrinfoRequestHandler());1627} else {1628rh.reset(new DefaultRequestHandler());1629if (args.size() < 1) {1630throw std::runtime_error("too few parameters (need at least command)");1631}1632method = args[0];1633args.erase(args.begin()); // Remove trailing method name from arguments vector1634}1635if (nRet == 0) {1636// Perform RPC call1637const std::optional<std::string> wallet_name{RpcWalletName(gArgs)};1638const UniValue reply = ConnectAndCallRPC(rh.get(), method, args, wallet_name);1640// Parse reply1641UniValue result = reply.find_value("result");1642const UniValue& error = reply.find_value("error");1643if (error.isNull()) {1644if (gArgs.GetBoolArg("-getinfo", false)) {1645if (!wallet_name) {1646GetWalletBalances(result); // fetch multiwallet balances and append to result1647}1648ParseGetInfoResult(result);1649}1651ParseResult(result, strPrint);1652} else {1653ParseError(error, strPrint, nRet);1654}1655}1656} catch (const std::exception& e) {1657strPrint = std::string("error: ") + e.what();1658nRet = EXIT_FAILURE;1659} catch (...) {1660PrintExceptionContinue(nullptr, "CommandLineRPC()");1661throw;1662}1664if (strPrint != "") {1665tfm::format(nRet == 0 ? std::cout : std::cerr, "%s\n", strPrint);1666}1667return nRet;1668}1670MAIN_FUNCTION1671{1672SetupEnvironment();1673if (!SetupNetworking()) {1674tfm::format(std::cerr, "Error: Initializing networking failed\n");1675return EXIT_FAILURE;1676}1678try {1679int ret = AppInitRPC(argc, argv);1680if (ret != CONTINUE_EXECUTION)1681return ret;1682}1683catch (const std::exception& e) {1684PrintExceptionContinue(&e, "AppInitRPC()");1685return EXIT_FAILURE;1686} catch (...) {1687PrintExceptionContinue(nullptr, "AppInitRPC()");1688return EXIT_FAILURE;1689}1691int ret = EXIT_FAILURE;1692try {1693ret = CommandLineRPC(argc, argv);1694}1695catch (const std::exception& e) {1696PrintExceptionContinue(&e, "CommandLineRPC()");1697} catch (...) {1698PrintExceptionContinue(nullptr, "CommandLineRPC()");1699}1700return ret;1701}