Changeset View
Changeset View
Standalone View
Standalone View
src/wallet/rpcwallet.cpp
Show First 20 Lines • Show All 197 Lines • ▼ Show 20 Lines | |||||
static std::string LabelFromValue(const UniValue &value) { | static std::string LabelFromValue(const UniValue &value) { | ||||
std::string label = value.get_str(); | std::string label = value.get_str(); | ||||
if (label == "*") { | if (label == "*") { | ||||
throw JSONRPCError(RPC_WALLET_INVALID_LABEL_NAME, "Invalid label name"); | throw JSONRPCError(RPC_WALLET_INVALID_LABEL_NAME, "Invalid label name"); | ||||
} | } | ||||
return label; | return label; | ||||
} | } | ||||
static void UpdateWalletSetting(interfaces::Chain &chain, | |||||
const std::string &wallet_name, | |||||
const UniValue &load_on_startup, | |||||
std::vector<bilingual_str> &warnings) { | |||||
if (load_on_startup.isTrue() && !AddWalletSetting(chain, wallet_name)) { | |||||
warnings.emplace_back( | |||||
Untranslated("Wallet load on startup setting could not be updated, " | |||||
"so wallet may not be loaded next node startup.")); | |||||
} else if (load_on_startup.isFalse() && | |||||
!RemoveWalletSetting(chain, wallet_name)) { | |||||
warnings.emplace_back( | |||||
Untranslated("Wallet load on startup setting could not be updated, " | |||||
"so wallet may still be loaded next node startup.")); | |||||
} | |||||
} | |||||
static UniValue getnewaddress(const Config &config, | static UniValue getnewaddress(const Config &config, | ||||
const JSONRPCRequest &request) { | const JSONRPCRequest &request) { | ||||
RPCHelpMan{ | RPCHelpMan{ | ||||
"getnewaddress", | "getnewaddress", | ||||
"Returns a new Bitcoin address for receiving payments.\n" | "Returns a new Bitcoin address for receiving payments.\n" | ||||
"If 'label' is specified, it is added to the address book \n" | "If 'label' is specified, it is added to the address book \n" | ||||
"so payments received with the address will be associated with " | "so payments received with the address will be associated with " | ||||
"'label'.\n", | "'label'.\n", | ||||
▲ Show 20 Lines • Show All 2,928 Lines • ▼ Show 20 Lines | if (!location.Exists()) { | ||||
throw JSONRPCError(RPC_WALLET_NOT_FOUND, | throw JSONRPCError(RPC_WALLET_NOT_FOUND, | ||||
"Directory " + location.GetName() + | "Directory " + location.GetName() + | ||||
" does not contain a wallet.dat file."); | " does not contain a wallet.dat file."); | ||||
} | } | ||||
} | } | ||||
bilingual_str error; | bilingual_str error; | ||||
std::vector<bilingual_str> warnings; | std::vector<bilingual_str> warnings; | ||||
std::shared_ptr<CWallet> const wallet = | std::optional<bool> load_on_start = | ||||
LoadWallet(chainParams, *context.chain, location, error, warnings); | request.params[1].isNull() | ||||
? std::nullopt | |||||
: std::optional<bool>(request.params[1].get_bool()); | |||||
Fabien: You should use std::make_optional | |||||
std::shared_ptr<CWallet> const wallet = LoadWallet( | |||||
chainParams, *context.chain, location, load_on_start, error, warnings); | |||||
if (!wallet) { | if (!wallet) { | ||||
throw JSONRPCError(RPC_WALLET_ERROR, error.original); | throw JSONRPCError(RPC_WALLET_ERROR, error.original); | ||||
} | } | ||||
UpdateWalletSetting(*context.chain, location.GetName(), request.params[1], | |||||
warnings); | |||||
UniValue obj(UniValue::VOBJ); | UniValue obj(UniValue::VOBJ); | ||||
obj.pushKV("name", wallet->GetName()); | obj.pushKV("name", wallet->GetName()); | ||||
obj.pushKV("warning", Join(warnings, Untranslated("\n")).original); | obj.pushKV("warning", Join(warnings, Untranslated("\n")).original); | ||||
return obj; | return obj; | ||||
} | } | ||||
static UniValue setwalletflag(const Config &config, | static UniValue setwalletflag(const Config &config, | ||||
▲ Show 20 Lines • Show All 148 Lines • ▼ Show 20 Lines | static UniValue createwallet(const Config &config, | ||||
if (!request.params[5].isNull() && request.params[5].get_bool()) { | if (!request.params[5].isNull() && request.params[5].get_bool()) { | ||||
flags |= WALLET_FLAG_DESCRIPTORS; | flags |= WALLET_FLAG_DESCRIPTORS; | ||||
warnings.emplace_back( | warnings.emplace_back( | ||||
Untranslated("Wallet is an experimental descriptor wallet")); | Untranslated("Wallet is an experimental descriptor wallet")); | ||||
} | } | ||||
bilingual_str error; | bilingual_str error; | ||||
std::shared_ptr<CWallet> wallet; | std::shared_ptr<CWallet> wallet; | ||||
WalletCreationStatus status = | std::optional<bool> load_on_start = | ||||
CreateWallet(config.GetChainParams(), *context.chain, passphrase, flags, | request.params[6].isNull() | ||||
request.params[0].get_str(), error, warnings, wallet); | ? std::nullopt | ||||
: std::optional<bool>(request.params[6].get_bool()); | |||||
WalletCreationStatus status = CreateWallet( | |||||
config.GetChainParams(), *context.chain, passphrase, flags, | |||||
request.params[0].get_str(), load_on_start, error, warnings, wallet); | |||||
switch (status) { | switch (status) { | ||||
case WalletCreationStatus::CREATION_FAILED: | case WalletCreationStatus::CREATION_FAILED: | ||||
throw JSONRPCError(RPC_WALLET_ERROR, error.original); | throw JSONRPCError(RPC_WALLET_ERROR, error.original); | ||||
case WalletCreationStatus::ENCRYPTION_FAILED: | case WalletCreationStatus::ENCRYPTION_FAILED: | ||||
throw JSONRPCError(RPC_WALLET_ENCRYPTION_FAILED, error.original); | throw JSONRPCError(RPC_WALLET_ENCRYPTION_FAILED, error.original); | ||||
case WalletCreationStatus::SUCCESS: | case WalletCreationStatus::SUCCESS: | ||||
break; | break; | ||||
// no default case, so the compiler can warn about missing cases | // no default case, so the compiler can warn about missing cases | ||||
} | } | ||||
UpdateWalletSetting(*context.chain, request.params[0].get_str(), | |||||
request.params[6], warnings); | |||||
UniValue obj(UniValue::VOBJ); | UniValue obj(UniValue::VOBJ); | ||||
obj.pushKV("name", wallet->GetName()); | obj.pushKV("name", wallet->GetName()); | ||||
obj.pushKV("warning", Join(warnings, Untranslated("\n")).original); | obj.pushKV("warning", Join(warnings, Untranslated("\n")).original); | ||||
return obj; | return obj; | ||||
} | } | ||||
static UniValue unloadwallet(const Config &config, | static UniValue unloadwallet(const Config &config, | ||||
Show All 38 Lines | static UniValue unloadwallet(const Config &config, | ||||
if (!wallet) { | if (!wallet) { | ||||
throw JSONRPCError(RPC_WALLET_NOT_FOUND, | throw JSONRPCError(RPC_WALLET_NOT_FOUND, | ||||
"Requested wallet does not exist or is not loaded"); | "Requested wallet does not exist or is not loaded"); | ||||
} | } | ||||
// Release the "main" shared pointer and prevent further notifications. | // Release the "main" shared pointer and prevent further notifications. | ||||
// Note that any attempt to load the same wallet would fail until the wallet | // Note that any attempt to load the same wallet would fail until the wallet | ||||
// is destroyed (see CheckUniqueFileid). | // is destroyed (see CheckUniqueFileid). | ||||
if (!RemoveWallet(wallet)) { | std::vector<bilingual_str> warnings; | ||||
std::optional<bool> load_on_start = | |||||
request.params[1].isNull() | |||||
? std::nullopt | |||||
: std::optional<bool>(request.params[1].get_bool()); | |||||
if (!RemoveWallet(wallet, load_on_start, warnings)) { | |||||
throw JSONRPCError(RPC_MISC_ERROR, "Requested wallet already unloaded"); | throw JSONRPCError(RPC_MISC_ERROR, "Requested wallet already unloaded"); | ||||
} | } | ||||
interfaces::Chain &chain = wallet->chain(); | |||||
std::vector<bilingual_str> warnings; | |||||
UnloadWallet(std::move(wallet)); | UnloadWallet(std::move(wallet)); | ||||
UpdateWalletSetting(chain, wallet_name, request.params[1], warnings); | |||||
UniValue result(UniValue::VOBJ); | UniValue result(UniValue::VOBJ); | ||||
result.pushKV("warning", Join(warnings, Untranslated("\n")).original); | result.pushKV("warning", Join(warnings, Untranslated("\n")).original); | ||||
return result; | return result; | ||||
} | } | ||||
static UniValue listunspent(const Config &config, | static UniValue listunspent(const Config &config, | ||||
const JSONRPCRequest &request) { | const JSONRPCRequest &request) { | ||||
▲ Show 20 Lines • Show All 1,607 Lines • Show Last 20 Lines |
You should use std::make_optional