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.
251std::string pass;253if (gArgs.GetArg("-rpcpassword", "") == "")254{255std::optional<fs::perms> cookie_perms{std::nullopt};256auto cookie_perms_arg{gArgs.GetArg("-rpccookieperms")};257if (cookie_perms_arg) {258auto perm_opt = InterpretPermString(*cookie_perms_arg);259if (!perm_opt) {260LogError("Invalid -rpccookieperms=%s; must be one of 'owner', 'group', or 'all'.", *cookie_perms_arg);261return false;262}263cookie_perms = *perm_opt;264}266switch (GenerateAuthCookie(cookie_perms, user, pass)) {267case AuthCookieResult::Error:268return false;269case AuthCookieResult::Disabled:270LogInfo("RPC authentication cookie file generation is disabled.");271break;272case AuthCookieResult::Ok:273LogInfo("Using random cookie authentication.");274break;275}276} else {277LogInfo("Using rpcuser/rpcpassword authentication.");278LogWarning("The use of rpcuser/rpcpassword is less secure, because credentials are configured in plain text. It is recommended that locally-run instances switch to cookie-based auth, or otherwise to use hashed rpcauth credentials. See share/rpcauth in the source directory for more information.");279user = gArgs.GetArg("-rpcuser", "");280pass = gArgs.GetArg("-rpcpassword", "");281}283// If there is a plaintext credential, hash it with a random salt before storage.284if (!user.empty() || !pass.empty()) {285// Generate a random 16 byte hex salt.286std::array<unsigned char, 16> raw_salt;287GetStrongRandBytes(raw_salt);288std::string salt = HexStr(raw_salt);290// Compute HMAC.291std::array<unsigned char, CHMAC_SHA256::OUTPUT_SIZE> out;292CHMAC_SHA256(UCharCast(salt.data()), salt.size()).Write(UCharCast(pass.data()), pass.size()).Finalize(out.data());293std::string hash = HexStr(out);295g_rpcauth.push_back({user, salt, hash});296}298if (!gArgs.GetArgs("-rpcauth").empty()) {299LogInfo("Using rpcauth authentication.\n");300for (const std::string& rpcauth : gArgs.GetArgs("-rpcauth")) {301std::vector<std::string> fields{SplitString(rpcauth, ':')};302const std::vector<std::string> salt_hmac{SplitString(fields.back(), '$')};303if (fields.size() == 2 && salt_hmac.size() == 2) {304fields.pop_back();305fields.insert(fields.end(), salt_hmac.begin(), salt_hmac.end());306g_rpcauth.push_back(fields);307} else {308LogWarning("Invalid -rpcauth argument.");309return false;310}311}312}314g_rpc_whitelist_default = gArgs.GetBoolArg("-rpcwhitelistdefault", !gArgs.GetArgs("-rpcwhitelist").empty());315for (const std::string& strRPCWhitelist : gArgs.GetArgs("-rpcwhitelist")) {316auto pos = strRPCWhitelist.find(':');317std::string strUser = strRPCWhitelist.substr(0, pos);318bool intersect = g_rpc_whitelist.contains(strUser);319std::set<std::string>& whitelist = g_rpc_whitelist[strUser];320if (pos != std::string::npos) {321std::string strWhitelist = strRPCWhitelist.substr(pos + 1);322std::vector<std::string> whitelist_split = SplitString(strWhitelist, ", ");323std::set<std::string> new_whitelist{324std::make_move_iterator(whitelist_split.begin()),325std::make_move_iterator(whitelist_split.end())};326if (intersect) {327std::set<std::string> tmp_whitelist;328std::set_intersection(new_whitelist.begin(), new_whitelist.end(),329whitelist.begin(), whitelist.end(), std::inserter(tmp_whitelist, tmp_whitelist.end()));330new_whitelist = std::move(tmp_whitelist);331}332whitelist = std::move(new_whitelist);333}334}336return true;337}339bool StartHTTPRPC(const std::any& context)340{341LogDebug(BCLog::RPC, "Starting HTTP RPC server\n");342if (!InitRPCAuthentication())343return false;345auto handle_rpc = [context](HTTPRequest* req, const std::string&) { return HTTPReq_JSONRPC(context, req); };346RegisterHTTPHandler("/", true, handle_rpc);347if (g_wallet_init_interface.HasWalletSupport()) {348RegisterHTTPHandler("/wallet/", false, handle_rpc);349}350return true;351}353void InterruptHTTPRPC()354{355LogDebug(BCLog::RPC, "Interrupting HTTP RPC server\n");356}358void StopHTTPRPC()359{360LogDebug(BCLog::RPC, "Stopping HTTP RPC server\n");361UnregisterHTTPHandler("/", true);362if (g_wallet_init_interface.HasWalletSupport()) {363UnregisterHTTPHandler("/wallet/", false);364}365}
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.