aboutsummaryrefslogtreecommitdiff
path: root/src/wallet/rpcwallet.cpp
diff options
context:
space:
mode:
authorMarcoFalke <falke.marco@gmail.com>2019-07-10 13:51:20 -0400
committerMarcoFalke <falke.marco@gmail.com>2019-07-10 13:51:25 -0400
commit4fcccdac785e09ad5627b3bf4811dfba353693e8 (patch)
tree882edf6c5fb72f1f7efeea0071367f3b4ebf1fca /src/wallet/rpcwallet.cpp
parentff0aad8a40a01da3f9031c7c583cb489cc5c8c57 (diff)
parent1aecdf2063cbe28d4715ae5ae1a7e51b860c9f4d (diff)
downloadbitcoin-4fcccdac785e09ad5627b3bf4811dfba353693e8.tar.xz
Merge #16244: Move wallet creation out of the createwallet rpc into its own function
1aecdf2063cbe28d4715ae5ae1a7e51b860c9f4d Move wallet creation out of the createwallet rpc into its own function (Andrew Chow) Pull request description: Moves the wallet creation logic from within the `createwallet` rpc and into its own function within wallet.cpp. ACKs for top commit: jnewbery: ACK 1aecdf2063cbe28d4715ae5ae1a7e51b860c9f4d MarcoFalke: ACK 1aecdf2063cbe28d4715ae5ae1a7e51b860c9f4d Sjors: ACK 1aecdf2 with some suggestions for followup. Tree-SHA512: 8d26d7ff48db4f8fac12408a5a294f788b7f50a72e7eb4008fb74ff14d7400eb3970f8038a19f989eff55198fc11c0cf86f52231c62b9015eb777132edc8ea88
Diffstat (limited to 'src/wallet/rpcwallet.cpp')
-rw-r--r--src/wallet/rpcwallet.cpp56
1 files changed, 10 insertions, 46 deletions
diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp
index 27fb009070..87ef58ee96 100644
--- a/src/wallet/rpcwallet.cpp
+++ b/src/wallet/rpcwallet.cpp
@@ -2660,17 +2660,12 @@ static UniValue createwallet(const JSONRPCRequest& request)
},
}.Check(request);
- std::string error;
- std::string warning;
-
uint64_t flags = 0;
if (!request.params[1].isNull() && request.params[1].get_bool()) {
flags |= WALLET_FLAG_DISABLE_PRIVATE_KEYS;
}
- bool create_blank = false; // Indicate that the wallet is actually supposed to be blank and not just blank to make it encrypted
if (!request.params[2].isNull() && request.params[2].get_bool()) {
- create_blank = true;
flags |= WALLET_FLAG_BLANK_WALLET;
}
SecureString passphrase;
@@ -2681,55 +2676,24 @@ static UniValue createwallet(const JSONRPCRequest& request)
// Empty string is invalid
throw JSONRPCError(RPC_WALLET_ENCRYPTION_FAILED, "Cannot encrypt a wallet with a blank password");
}
- // Born encrypted wallets need to be blank first so that wallet creation doesn't make any unencrypted keys
- flags |= WALLET_FLAG_BLANK_WALLET;
}
if (!request.params[4].isNull() && request.params[4].get_bool()) {
flags |= WALLET_FLAG_AVOID_REUSE;
}
- WalletLocation location(request.params[0].get_str());
- if (location.Exists()) {
- throw JSONRPCError(RPC_WALLET_ERROR, "Wallet " + location.GetName() + " already exists.");
- }
-
- // Wallet::Verify will check if we're trying to create a wallet with a duplication name.
- if (!CWallet::Verify(*g_rpc_interfaces->chain, location, false, error, warning)) {
- throw JSONRPCError(RPC_WALLET_ERROR, "Wallet file verification failed: " + error);
- }
-
- std::shared_ptr<CWallet> const wallet = CWallet::CreateWalletFromFile(*g_rpc_interfaces->chain, location, flags);
- if (!wallet) {
- throw JSONRPCError(RPC_WALLET_ERROR, "Wallet creation failed.");
- }
-
- // Encrypt the wallet if there's a passphrase
- if (!passphrase.empty() && !(flags & WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
- if (!wallet->EncryptWallet(passphrase)) {
- throw JSONRPCError(RPC_WALLET_ENCRYPTION_FAILED, "Error: Wallet created but failed to encrypt.");
- }
-
- if (!create_blank) {
- // Unlock the wallet
- if (!wallet->Unlock(passphrase)) {
- throw JSONRPCError(RPC_WALLET_ENCRYPTION_FAILED, "Error: Wallet was encrypted but could not be unlocked");
- }
-
- // Set a seed for the wallet
- CPubKey master_pub_key = wallet->GenerateNewSeed();
- wallet->SetHDSeed(master_pub_key);
- wallet->NewKeyPool();
-
- // Relock the wallet
- wallet->Lock();
- }
+ std::string error;
+ std::string warning;
+ WalletCreationStatus status;
+ std::shared_ptr<CWallet> wallet = CreateWallet(*g_rpc_interfaces->chain, request.params[0].get_str(), error, warning, status, passphrase, flags);
+ if (status == WalletCreationStatus::CREATION_FAILED) {
+ throw JSONRPCError(RPC_WALLET_ERROR, error);
+ } else if (status == WalletCreationStatus::ENCRYPTION_FAILED) {
+ throw JSONRPCError(RPC_WALLET_ENCRYPTION_FAILED, error);
+ } else if (status != WalletCreationStatus::SUCCESS) {
+ throw JSONRPCError(RPC_WALLET_ERROR, "Wallet creation failed");
}
- AddWallet(wallet);
-
- wallet->postInitProcess();
-
UniValue obj(UniValue::VOBJ);
obj.pushKV("name", wallet->GetName());
obj.pushKV("warning", warning);