diff options
author | MarcoFalke <falke.marco@gmail.com> | 2019-07-10 13:51:20 -0400 |
---|---|---|
committer | MarcoFalke <falke.marco@gmail.com> | 2019-07-10 13:51:25 -0400 |
commit | 4fcccdac785e09ad5627b3bf4811dfba353693e8 (patch) | |
tree | 882edf6c5fb72f1f7efeea0071367f3b4ebf1fca /src/wallet/wallet.cpp | |
parent | ff0aad8a40a01da3f9031c7c583cb489cc5c8c57 (diff) | |
parent | 1aecdf2063cbe28d4715ae5ae1a7e51b860c9f4d (diff) |
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/wallet.cpp')
-rw-r--r-- | src/wallet/wallet.cpp | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 5fb883d48b..e53b433ca8 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -160,6 +160,70 @@ std::shared_ptr<CWallet> LoadWallet(interfaces::Chain& chain, const std::string& return LoadWallet(chain, WalletLocation(name), error, warning); } +std::shared_ptr<CWallet> CreateWallet(interfaces::Chain& chain, const std::string& name, std::string& error, std::string& warning, WalletCreationStatus& status, const SecureString& passphrase, uint64_t wallet_creation_flags) +{ + // Indicate that the wallet is actually supposed to be blank and not just blank to make it encrypted + bool create_blank = (wallet_creation_flags & WALLET_FLAG_BLANK_WALLET); + + // Born encrypted wallets need to be created blank first. + if (!passphrase.empty()) { + wallet_creation_flags |= WALLET_FLAG_BLANK_WALLET; + } + + // Check the wallet file location + WalletLocation location(name); + if (location.Exists()) { + error = "Wallet " + location.GetName() + " already exists."; + status = WalletCreationStatus::CREATION_FAILED; + return nullptr; + } + + // Wallet::Verify will check if we're trying to create a wallet with a duplicate name. + std::string wallet_error; + if (!CWallet::Verify(chain, location, false, wallet_error, warning)) { + error = "Wallet file verification failed: " + wallet_error; + status = WalletCreationStatus::CREATION_FAILED; + return nullptr; + } + + // Make the wallet + std::shared_ptr<CWallet> wallet = CWallet::CreateWalletFromFile(chain, location, wallet_creation_flags); + if (!wallet) { + error = "Wallet creation failed"; + status = WalletCreationStatus::CREATION_FAILED; + return nullptr; + } + + // Encrypt the wallet + if (!passphrase.empty() && !(wallet_creation_flags & WALLET_FLAG_DISABLE_PRIVATE_KEYS)) { + if (!wallet->EncryptWallet(passphrase)) { + error = "Error: Wallet created but failed to encrypt."; + status = WalletCreationStatus::ENCRYPTION_FAILED; + return nullptr; + } + if (!create_blank) { + // Unlock the wallet + if (!wallet->Unlock(passphrase)) { + error = "Error: Wallet was encrypted but could not be unlocked"; + status = WalletCreationStatus::ENCRYPTION_FAILED; + return nullptr; + } + + // Set a seed for the wallet + CPubKey master_pub_key = wallet->GenerateNewSeed(); + wallet->SetHDSeed(master_pub_key); + wallet->NewKeyPool(); + + // Relock the wallet + wallet->Lock(); + } + } + AddWallet(wallet); + wallet->postInitProcess(); + status = WalletCreationStatus::SUCCESS; + return wallet; +} + const uint32_t BIP32_HARDENED_KEY_LIMIT = 0x80000000; const uint256 CMerkleTx::ABANDON_HASH(uint256S("0000000000000000000000000000000000000000000000000000000000000001")); |