mirror of
https://github.com/dashpay/dash.git
synced 2024-12-25 12:02:48 +01:00
Merge #15226: Allow creating blank (empty) wallets (alternative)
7687f7873 [wallet] Support creating a blank wallet (Andrew Chow) Pull request description: Alternative (kind of) to #14938 This PR adds a `blank` parameter to the `createwallet` RPC to create a wallet that has no private keys initially. `sethdseed` can then be used to make a clean wallet with a custom seed. `encryptwallet` can also be used to make a wallet that is born encrypted. Instead of changing the version number as done in #14938, a wallet flag is used to indicate that the wallet should be blank. This flag is set at creation, and then unset when the wallet is no longer blank. A wallet becomes non-blank when a HD seed is set or anything is imported. The main change to create a blank wallet is primarily taken from #14938. Also with this, the term "blank wallet" is used instead of "empty wallet" to avoid confusion with wallets that have balance which would also be referred to as "empty". This is built on top of #15225 in order to fix GUI issues. Tree-SHA512: 824d685e11ac2259a26b5ece99c67a7bda94a570cd921472c464243ee356b7734595ad35cc439b34357135df041ed9cba951e6edac194935c3a55a1dc4fcbdea
This commit is contained in:
parent
a472a85ab6
commit
254e122217
8
doc/release-notes-15226.md
Normal file
8
doc/release-notes-15226.md
Normal file
@ -0,0 +1,8 @@
|
||||
Miscellaneous RPC changes
|
||||
------------
|
||||
|
||||
- The RPC `createwallet` now has an optional `blank` argument that can be used to create a blank wallet.
|
||||
Blank wallets do not have any keys or HD seed.
|
||||
They cannot be opened in software older than 0.18.
|
||||
Once a blank wallet has a HD seed set (by using `sethdseed`) or private keys, scripts, addresses, and other watch only things have been imported, the wallet is no longer blank and can be opened in 0.17.x.
|
||||
Encrypting a blank wallet will also set a HD seed for it.
|
@ -526,6 +526,7 @@ public:
|
||||
}
|
||||
unsigned int getConfirmTarget() override { return m_wallet->m_confirm_target; }
|
||||
bool hdEnabled() override { return m_wallet->IsHDEnabled(); }
|
||||
bool canGetAddresses() override { return m_wallet->CanGetAddresses(); }
|
||||
bool IsWalletFlagSet(uint64_t flag) override { return m_wallet->IsWalletFlagSet(flag); }
|
||||
CoinJoin::Client& coinJoin() override { return m_coinjoin; }
|
||||
CAmount getDefaultMaxTxFee() override { return m_wallet->m_default_max_tx_fee; }
|
||||
|
@ -266,6 +266,9 @@ public:
|
||||
// Return whether HD enabled.
|
||||
virtual bool hdEnabled() = 0;
|
||||
|
||||
// Return whether the wallet is blank.
|
||||
virtual bool canGetAddresses() = 0;
|
||||
|
||||
// check if a certain wallet flag is set.
|
||||
virtual bool IsWalletFlagSet(uint64_t flag) = 0;
|
||||
|
||||
|
@ -633,12 +633,7 @@ bool WalletModel::privateKeysDisabled() const
|
||||
|
||||
bool WalletModel::canGetAddresses() const
|
||||
{
|
||||
// The wallet can provide a fresh address if:
|
||||
// * hdEnabled(): an HD seed is present; or
|
||||
// * it is a legacy wallet, because:
|
||||
// * !hdEnabled(): an HD seed is not present; and
|
||||
// * !IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS): private keys have not been disabled (which results in hdEnabled() == true)
|
||||
return m_wallet->hdEnabled() || (!m_wallet->hdEnabled() && !m_wallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS));
|
||||
return m_wallet->canGetAddresses();
|
||||
}
|
||||
|
||||
QString WalletModel::getWalletName() const
|
||||
|
@ -192,6 +192,7 @@ static const CRPCConvertParam vRPCConvertParams[] =
|
||||
{ "rescanblockchain", 0, "start_height"},
|
||||
{ "rescanblockchain", 1, "stop_height"},
|
||||
{ "createwallet", 1, "disable_private_keys"},
|
||||
{ "createwallet", 2, "blank"},
|
||||
{ "getnodeaddresses", 0, "count"},
|
||||
{ "stop", 0, "wait" },
|
||||
};
|
||||
|
@ -246,7 +246,7 @@ bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn, bool fForMixin
|
||||
if (!SetCrypted())
|
||||
return false;
|
||||
|
||||
bool keyPass = false;
|
||||
bool keyPass = mapCryptedKeys.empty(); // Always pass when there are no encrypted keys
|
||||
bool keyFail = false;
|
||||
CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
|
||||
for (; mi != mapCryptedKeys.end(); ++mi)
|
||||
|
@ -170,12 +170,17 @@ UniValue getnewaddress(const JSONRPCRequest& request)
|
||||
},
|
||||
}.ToString());
|
||||
|
||||
// Belt and suspenders check for disabled private keys
|
||||
if (pwallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
|
||||
throw JSONRPCError(RPC_WALLET_ERROR, "Error: Private keys are disabled for this wallet");
|
||||
}
|
||||
|
||||
LOCK(pwallet->cs_wallet);
|
||||
|
||||
if (!pwallet->CanGetAddresses()) {
|
||||
throw JSONRPCError(RPC_WALLET_ERROR, "Error: This wallet has no available keys");
|
||||
}
|
||||
|
||||
// Parse the label first so we don't generate a key if there's an error
|
||||
std::string label;
|
||||
if (!request.params[0].isNull())
|
||||
@ -221,12 +226,17 @@ static UniValue getrawchangeaddress(const JSONRPCRequest& request)
|
||||
},
|
||||
}.ToString());
|
||||
|
||||
// Belt and suspenders check for disabled private keys
|
||||
if (pwallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
|
||||
throw JSONRPCError(RPC_WALLET_ERROR, "Error: Private keys are disabled for this wallet");
|
||||
}
|
||||
|
||||
LOCK(pwallet->cs_wallet);
|
||||
|
||||
if (!pwallet->CanGetAddresses(true)) {
|
||||
throw JSONRPCError(RPC_WALLET_ERROR, "Error: This wallet has no available keys");
|
||||
}
|
||||
|
||||
if (!pwallet->IsLocked(true)) {
|
||||
pwallet->TopUpKeyPool();
|
||||
}
|
||||
@ -2912,13 +2922,14 @@ static UniValue loadwallet(const JSONRPCRequest& request)
|
||||
|
||||
static UniValue createwallet(const JSONRPCRequest& request)
|
||||
{
|
||||
if (request.fHelp || request.params.size() < 1 || request.params.size() > 2) {
|
||||
if (request.fHelp || request.params.size() < 1 || request.params.size() > 3) {
|
||||
throw std::runtime_error(
|
||||
RPCHelpMan{"createwallet",
|
||||
"\nCreates and loads a new wallet.\n",
|
||||
{
|
||||
{"wallet_name", RPCArg::Type::STR, RPCArg::Optional::NO, "The name for the new wallet. If this is a path, the wallet will be created at the path location."},
|
||||
{"disable_private_keys", RPCArg::Type::BOOL, /* default */ "false", "Disable the possibility of private keys (only watchonlys are possible in this mode)."},
|
||||
{"blank", RPCArg::Type::BOOL, /* default */ "false", "Create a blank wallet. A blank wallet has no keys or HD seed. One can be set using sethdseed."},
|
||||
},
|
||||
RPCResult{
|
||||
"{\n"
|
||||
@ -2935,9 +2946,13 @@ static UniValue createwallet(const JSONRPCRequest& request)
|
||||
std::string error;
|
||||
std::string warning;
|
||||
|
||||
bool disable_privatekeys = false;
|
||||
if (!request.params[1].isNull()) {
|
||||
disable_privatekeys = request.params[1].get_bool();
|
||||
uint64_t flags = 0;
|
||||
if (!request.params[1].isNull() && request.params[1].get_bool()) {
|
||||
flags |= WALLET_FLAG_DISABLE_PRIVATE_KEYS;
|
||||
}
|
||||
|
||||
if (!request.params[2].isNull() && request.params[2].get_bool()) {
|
||||
flags |= WALLET_FLAG_BLANK_WALLET;
|
||||
}
|
||||
|
||||
WalletLocation location(request.params[0].get_str());
|
||||
@ -2950,7 +2965,7 @@ static UniValue createwallet(const JSONRPCRequest& request)
|
||||
throw JSONRPCError(RPC_WALLET_ERROR, "Wallet file verification failed: " + error);
|
||||
}
|
||||
|
||||
const auto wallet = CWallet::CreateWalletFromFile(*g_rpc_interfaces->chain, location, (disable_privatekeys ? (uint64_t)WALLET_FLAG_DISABLE_PRIVATE_KEYS : 0));
|
||||
const auto wallet = CWallet::CreateWalletFromFile(*g_rpc_interfaces->chain, location, flags);
|
||||
if (!wallet) {
|
||||
throw JSONRPCError(RPC_WALLET_ERROR, "Wallet creation failed.");
|
||||
}
|
||||
@ -4165,7 +4180,7 @@ static const CRPCCommand commands[] =
|
||||
{ "wallet", "abortrescan", &abortrescan, {} },
|
||||
{ "wallet", "addmultisigaddress", &addmultisigaddress, {"nrequired","keys","label"} },
|
||||
{ "wallet", "backupwallet", &backupwallet, {"destination"} },
|
||||
{ "wallet", "createwallet", &createwallet, {"wallet_name", "disable_private_keys"} },
|
||||
{ "wallet", "createwallet", &createwallet, {"wallet_name", "disable_private_keys", "blank"} },
|
||||
{ "wallet", "dumphdinfo", &dumphdinfo, {} },
|
||||
{ "wallet", "dumpprivkey", &dumpprivkey, {"address"} },
|
||||
{ "wallet", "dumpwallet", &dumpwallet, {"filename"} },
|
||||
|
@ -966,6 +966,7 @@ BOOST_FIXTURE_TEST_CASE(wallet_disableprivkeys, TestChain100Setup)
|
||||
{
|
||||
auto chain = interfaces::MakeChain();
|
||||
std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(*chain, WalletLocation(), WalletDatabase::CreateDummy());
|
||||
wallet->SetMinVersion(FEATURE_LATEST);
|
||||
wallet->SetWalletFlag(WALLET_FLAG_DISABLE_PRIVATE_KEYS);
|
||||
BOOST_CHECK(!wallet->TopUpKeyPool(1000));
|
||||
CPubKey pubkey;
|
||||
|
@ -240,6 +240,7 @@ const CWalletTx* CWallet::GetWalletTx(const uint256& hash) const
|
||||
CPubKey CWallet::GenerateNewKey(WalletBatch &batch, uint32_t nAccountIndex, bool fInternal)
|
||||
{
|
||||
assert(!IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS));
|
||||
assert(!IsWalletFlagSet(WALLET_FLAG_BLANK_WALLET));
|
||||
AssertLockHeld(cs_wallet);
|
||||
bool fCompressed = CanSupportFeature(FEATURE_COMPRPUBKEY); // default to compressed public keys if we want 0.6.0 wallets
|
||||
|
||||
@ -250,7 +251,7 @@ CPubKey CWallet::GenerateNewKey(WalletBatch &batch, uint32_t nAccountIndex, bool
|
||||
CKeyMetadata metadata(nCreationTime);
|
||||
|
||||
CPubKey pubkey;
|
||||
// use HD key derivation if HD was enabled during wallet creation
|
||||
// use HD key derivation if HD was enabled during wallet creation and a non-null HD chain is present
|
||||
if (IsHDEnabled()) {
|
||||
DeriveNewChildKey(batch, metadata, secret, nAccountIndex, fInternal);
|
||||
pubkey = secret.GetPubKey();
|
||||
@ -417,7 +418,11 @@ bool CWallet::AddHDPubKey(WalletBatch &batch, const CExtPubKey &extPubKey, bool
|
||||
if (HaveWatchOnly(script))
|
||||
RemoveWatchOnly(script);
|
||||
|
||||
return batch.WriteHDPubKey(hdPubKey, mapKeyMetadata[extPubKey.pubkey.GetID()]);
|
||||
if (!batch.WriteHDPubKey(hdPubKey, mapKeyMetadata[extPubKey.pubkey.GetID()])) {
|
||||
return false;
|
||||
}
|
||||
UnsetWalletFlag(batch, WALLET_FLAG_BLANK_WALLET);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CWallet::AddKeyPubKeyWithDB(WalletBatch& batch, const CKey& secret, const CPubKey& pubkey)
|
||||
@ -455,6 +460,7 @@ bool CWallet::AddKeyPubKeyWithDB(WalletBatch& batch, const CKey& secret, const C
|
||||
secret.GetPrivKey(),
|
||||
mapKeyMetadata[pubkey.GetID()]);
|
||||
}
|
||||
UnsetWalletFlag(batch, WALLET_FLAG_BLANK_WALLET);
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -529,7 +535,12 @@ bool CWallet::AddCScript(const CScript& redeemScript)
|
||||
{
|
||||
if (!CCryptoKeyStore::AddCScript(redeemScript))
|
||||
return false;
|
||||
return WalletBatch(*database).WriteCScript(Hash160(redeemScript), redeemScript);
|
||||
WalletBatch batch(*database);
|
||||
if (batch.WriteCScript(Hash160(redeemScript), redeemScript)) {
|
||||
UnsetWalletFlag(batch, WALLET_FLAG_BLANK_WALLET);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CWallet::LoadCScript(const CScript& redeemScript)
|
||||
@ -554,7 +565,12 @@ bool CWallet::AddWatchOnly(const CScript& dest)
|
||||
const CKeyMetadata& meta = m_script_metadata[CScriptID(dest)];
|
||||
UpdateTimeFirstKey(meta.nCreateTime);
|
||||
NotifyWatchonlyChanged(true);
|
||||
return WalletBatch(*database).WriteWatchOnly(dest, meta);
|
||||
WalletBatch batch(*database);
|
||||
if (batch.WriteWatchOnly(dest, meta)) {
|
||||
UnsetWalletFlag(batch, WALLET_FLAG_BLANK_WALLET);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CWallet::AddWatchOnly(const CScript& dest, int64_t nCreateTime)
|
||||
@ -1807,8 +1823,13 @@ bool CWallet::SetHDChain(WalletBatch &batch, const CHDChain& chain, bool memonly
|
||||
if (!CCryptoKeyStore::SetHDChain(chain))
|
||||
return false;
|
||||
|
||||
if (!memonly && !batch.WriteHDChain(chain))
|
||||
throw std::runtime_error(std::string(__func__) + ": WriteHDChain failed");
|
||||
if (!memonly) {
|
||||
if (!batch.WriteHDChain(chain)) {
|
||||
throw std::runtime_error(std::string(__func__) + ": WriteHDChain failed");
|
||||
}
|
||||
|
||||
UnsetWalletFlag(batch, WALLET_FLAG_BLANK_WALLET);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
@ -1828,6 +1849,7 @@ bool CWallet::SetCryptedHDChain(WalletBatch &batch, const CHDChain& chain, bool
|
||||
if (!batch.WriteCryptedHDChain(chain))
|
||||
throw std::runtime_error(std::string(__func__) + ": WriteCryptedHDChain failed");
|
||||
}
|
||||
UnsetWalletFlag(batch, WALLET_FLAG_BLANK_WALLET);
|
||||
}
|
||||
|
||||
return true;
|
||||
@ -1942,6 +1964,33 @@ CAmount CWallet::GetChange(const CTransaction& tx) const
|
||||
return nChange;
|
||||
}
|
||||
|
||||
bool CWallet::CanGenerateKeys()
|
||||
{
|
||||
LOCK(cs_wallet);
|
||||
if (IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS) || IsWalletFlagSet(WALLET_FLAG_BLANK_WALLET)) {
|
||||
return false;
|
||||
}
|
||||
// A wallet can generate keys if it has a non-null HD chain (IsHDEnabled) or it is a non-HD wallet (pre FEATURE_HD)
|
||||
return IsHDEnabled() || nWalletVersion < FEATURE_HD;
|
||||
}
|
||||
|
||||
bool CWallet::CanGetAddresses(bool internal)
|
||||
{
|
||||
LOCK(cs_wallet);
|
||||
// Check if the keypool has keys
|
||||
bool keypool_has_keys;
|
||||
if (internal && CanSupportFeature(FEATURE_HD)) {
|
||||
keypool_has_keys = setInternalKeyPool.size() > 0;
|
||||
} else {
|
||||
keypool_has_keys = KeypoolCountExternalKeys() > 0;
|
||||
}
|
||||
// If the keypool doesn't have keys, check if we can generate them
|
||||
if (!keypool_has_keys) {
|
||||
return CanGenerateKeys();
|
||||
}
|
||||
return keypool_has_keys;
|
||||
}
|
||||
|
||||
void CWallet::SetWalletFlag(uint64_t flags)
|
||||
{
|
||||
LOCK(cs_wallet);
|
||||
@ -1950,6 +1999,21 @@ void CWallet::SetWalletFlag(uint64_t flags)
|
||||
throw std::runtime_error(std::string(__func__) + ": writing wallet flags failed");
|
||||
}
|
||||
|
||||
void CWallet::UnsetWalletFlag(uint64_t flag)
|
||||
{
|
||||
LOCK(cs_wallet);
|
||||
WalletBatch batch(*database);
|
||||
UnsetWalletFlag(batch, flag);
|
||||
}
|
||||
|
||||
void CWallet::UnsetWalletFlag(WalletBatch& batch, uint64_t flag)
|
||||
{
|
||||
LOCK(cs_wallet);
|
||||
m_wallet_flags &= ~flag;
|
||||
if (!batch.WriteWalletFlags(m_wallet_flags))
|
||||
throw std::runtime_error(std::string(__func__) + ": writing wallet flags failed");
|
||||
}
|
||||
|
||||
bool CWallet::IsWalletFlagSet(uint64_t flag)
|
||||
{
|
||||
return (m_wallet_flags & flag);
|
||||
@ -3983,7 +4047,7 @@ DBErrors CWallet::LoadWallet(bool& fFirstRunRet)
|
||||
{
|
||||
LOCK(cs_KeyStore);
|
||||
// This wallet is in its first run if all of these are empty
|
||||
fFirstRunRet = mapKeys.empty() && mapHdPubKeys.empty() && mapCryptedKeys.empty() && mapWatchKeys.empty() && setWatchOnly.empty() && mapScripts.empty() && !IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS);
|
||||
fFirstRunRet = mapKeys.empty() && mapHdPubKeys.empty() && mapCryptedKeys.empty() && mapWatchKeys.empty() && setWatchOnly.empty() && mapScripts.empty() && !IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS) && !IsWalletFlagSet(WALLET_FLAG_BLANK_WALLET);
|
||||
}
|
||||
|
||||
for (auto& pair : mapWallet) {
|
||||
@ -4200,7 +4264,7 @@ size_t CWallet::KeypoolCountInternalKeys()
|
||||
|
||||
bool CWallet::TopUpKeyPool(unsigned int kpSize)
|
||||
{
|
||||
if (IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
|
||||
if (!CanGenerateKeys()) {
|
||||
return false;
|
||||
}
|
||||
{
|
||||
@ -4341,7 +4405,7 @@ void CWallet::ReturnKey(int64_t nIndex, bool fInternal, const CPubKey& pubkey)
|
||||
|
||||
bool CWallet::GetKeyFromPool(CPubKey& result, bool internal)
|
||||
{
|
||||
if (IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
|
||||
if (!CanGetAddresses(internal)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -4543,6 +4607,10 @@ std::set<CTxDestination> CWallet::GetLabelAddresses(const std::string& label) co
|
||||
|
||||
bool CReserveKey::GetReservedKey(CPubKey& pubkey, bool fInternalIn)
|
||||
{
|
||||
if (!pwallet->CanGetAddresses(fInternalIn)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (nIndex == -1)
|
||||
{
|
||||
CKeyPool keypool;
|
||||
@ -4956,11 +5024,12 @@ std::shared_ptr<CWallet> CWallet::CreateWalletFromFile(interfaces::Chain& chain,
|
||||
if (gArgs.GetBoolArg("-upgradewallet", fFirstRun))
|
||||
{
|
||||
int nMaxVersion = gArgs.GetArg("-upgradewallet", 0);
|
||||
auto nMinVersion = DEFAULT_USE_HD_WALLET ? FEATURE_LATEST : FEATURE_COMPRPUBKEY;
|
||||
if (nMaxVersion == 0) // the -upgradewallet without argument case
|
||||
{
|
||||
walletInstance->WalletLogPrintf("Performing wallet upgrade to %i\n", FEATURE_LATEST);
|
||||
walletInstance->WalletLogPrintf("Performing wallet upgrade to %i\n", nMinVersion);
|
||||
nMaxVersion = FEATURE_LATEST;
|
||||
walletInstance->SetMinVersion(FEATURE_LATEST); // permanently upgrade the wallet immediately
|
||||
walletInstance->SetMinVersion(nMinVersion); // permanently upgrade the wallet immediately
|
||||
}
|
||||
else
|
||||
walletInstance->WalletLogPrintf("Allowing wallet upgrade up to %i\n", nMaxVersion);
|
||||
@ -4973,48 +5042,50 @@ std::shared_ptr<CWallet> CWallet::CreateWalletFromFile(interfaces::Chain& chain,
|
||||
|
||||
if (fFirstRun)
|
||||
{
|
||||
// Create new keyUser and set as default key
|
||||
if (gArgs.GetBoolArg("-usehd", DEFAULT_USE_HD_WALLET) && !walletInstance->IsHDEnabled()) {
|
||||
std::string strSeed = gArgs.GetArg("-hdseed", "not hex");
|
||||
|
||||
if (gArgs.IsArgSet("-hdseed") && IsHex(strSeed)) {
|
||||
CHDChain newHdChain;
|
||||
std::vector<unsigned char> vchSeed = ParseHex(strSeed);
|
||||
if (!newHdChain.SetSeed(SecureVector(vchSeed.begin(), vchSeed.end()), true)) {
|
||||
return error(strprintf(_("%s failed"), "SetSeed"));
|
||||
}
|
||||
if (!walletInstance->SetHDChainSingle(newHdChain, false)) {
|
||||
return error(strprintf(_("%s failed"), "SetHDChainSingle"));
|
||||
}
|
||||
// add default account
|
||||
newHdChain.AddAccount();
|
||||
newHdChain.Debug(__func__);
|
||||
} else {
|
||||
if (gArgs.IsArgSet("-hdseed") && !IsHex(strSeed)) {
|
||||
walletInstance->WalletLogPrintf("%s -- Incorrect seed, generating a random mnemonic instead\n", __func__);
|
||||
}
|
||||
SecureString secureMnemonic = gArgs.GetArg("-mnemonic", "").c_str();
|
||||
SecureString secureMnemonicPassphrase = gArgs.GetArg("-mnemonicpassphrase", "").c_str();
|
||||
walletInstance->GenerateNewHDChain(secureMnemonic, secureMnemonicPassphrase);
|
||||
}
|
||||
|
||||
// ensure this wallet.dat can only be opened by clients supporting HD
|
||||
walletInstance->WalletLogPrintf("Upgrading wallet to HD\n");
|
||||
walletInstance->SetMinVersion(FEATURE_HD);
|
||||
|
||||
// clean up
|
||||
gArgs.ForceRemoveArg("-hdseed");
|
||||
gArgs.ForceRemoveArg("-mnemonic");
|
||||
gArgs.ForceRemoveArg("-mnemonicpassphrase");
|
||||
}
|
||||
|
||||
if ((wallet_creation_flags & WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
|
||||
//selective allow to set flags
|
||||
walletInstance->SetWalletFlag(WALLET_FLAG_DISABLE_PRIVATE_KEYS);
|
||||
}
|
||||
} else if (wallet_creation_flags & WALLET_FLAG_BLANK_WALLET) {
|
||||
walletInstance->SetWalletFlag(WALLET_FLAG_BLANK_WALLET);
|
||||
} else {
|
||||
// Create new HD chain
|
||||
if (gArgs.GetBoolArg("-usehd", DEFAULT_USE_HD_WALLET) && !walletInstance->IsHDEnabled()) {
|
||||
std::string strSeed = gArgs.GetArg("-hdseed", "not hex");
|
||||
|
||||
if (gArgs.IsArgSet("-hdseed") && IsHex(strSeed)) {
|
||||
CHDChain newHdChain;
|
||||
std::vector<unsigned char> vchSeed = ParseHex(strSeed);
|
||||
if (!newHdChain.SetSeed(SecureVector(vchSeed.begin(), vchSeed.end()), true)) {
|
||||
return error(strprintf(_("%s failed"), "SetSeed"));
|
||||
}
|
||||
if (!walletInstance->SetHDChainSingle(newHdChain, false)) {
|
||||
return error(strprintf(_("%s failed"), "SetHDChainSingle"));
|
||||
}
|
||||
// add default account
|
||||
newHdChain.AddAccount();
|
||||
newHdChain.Debug(__func__);
|
||||
} else {
|
||||
if (gArgs.IsArgSet("-hdseed") && !IsHex(strSeed)) {
|
||||
walletInstance->WalletLogPrintf("%s -- Incorrect seed, generating a random mnemonic instead\n", __func__);
|
||||
}
|
||||
SecureString secureMnemonic = gArgs.GetArg("-mnemonic", "").c_str();
|
||||
SecureString secureMnemonicPassphrase = gArgs.GetArg("-mnemonicpassphrase", "").c_str();
|
||||
walletInstance->GenerateNewHDChain(secureMnemonic, secureMnemonicPassphrase);
|
||||
}
|
||||
|
||||
// ensure this wallet.dat can only be opened by clients supporting HD
|
||||
walletInstance->WalletLogPrintf("Upgrading wallet to HD\n");
|
||||
walletInstance->SetMinVersion(FEATURE_HD);
|
||||
|
||||
// clean up
|
||||
gArgs.ForceRemoveArg("-hdseed");
|
||||
gArgs.ForceRemoveArg("-mnemonic");
|
||||
gArgs.ForceRemoveArg("-mnemonicpassphrase");
|
||||
}
|
||||
} // Otherwise, do not create a new HD chain
|
||||
|
||||
// Top up the keypool
|
||||
if (!walletInstance->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS) && !walletInstance->TopUpKeyPool()) {
|
||||
if (walletInstance->CanGenerateKeys() && !walletInstance->TopUpKeyPool()) {
|
||||
return error(_("Unable to generate initial keys"));
|
||||
}
|
||||
|
||||
|
@ -126,9 +126,21 @@ enum WalletFlags : uint64_t {
|
||||
|
||||
// will enforce the rule that the wallet can't contain any private keys (only watch-only/pubkeys)
|
||||
WALLET_FLAG_DISABLE_PRIVATE_KEYS = (1ULL << 32),
|
||||
|
||||
//! Flag set when a wallet contains no HD seed and no private keys, scripts,
|
||||
//! addresses, and other watch only things, and is therefore "blank."
|
||||
//!
|
||||
//! The only function this flag serves is to distinguish a blank wallet from
|
||||
//! a newly created wallet when the wallet database is loaded, to avoid
|
||||
//! initialization that should only happen on first run.
|
||||
//!
|
||||
//! This flag is also a mandatory flag to prevent previous versions of
|
||||
//! bitcoin from opening the wallet, thinking it was newly created, and
|
||||
//! then improperly reinitializing it.
|
||||
WALLET_FLAG_BLANK_WALLET = (1ULL << 33),
|
||||
};
|
||||
|
||||
static constexpr uint64_t g_known_wallet_flags = WALLET_FLAG_DISABLE_PRIVATE_KEYS;
|
||||
static constexpr uint64_t g_known_wallet_flags = WALLET_FLAG_DISABLE_PRIVATE_KEYS | WALLET_FLAG_BLANK_WALLET;
|
||||
|
||||
/** A key pool entry */
|
||||
class CKeyPool
|
||||
@ -1208,6 +1220,13 @@ public:
|
||||
|
||||
/* Returns true if HD is enabled */
|
||||
bool IsHDEnabled() const;
|
||||
|
||||
/* Returns true if the wallet can generate new keys */
|
||||
bool CanGenerateKeys();
|
||||
|
||||
/* Returns true if the wallet can give out new addresses. This means it has keys in the keypool or can generate new keys */
|
||||
bool CanGetAddresses(bool internal = false);
|
||||
|
||||
/* Generates a new HD chain */
|
||||
void GenerateNewHDChain(const SecureString& secureMnemonic, const SecureString& secureMnemonicPassphrase);
|
||||
bool GenerateNewHDChainEncrypted(const SecureString& secureMnemonic, const SecureString& secureMnemonicPassphrase, const SecureString& secureWalletPassphrase);
|
||||
@ -1245,6 +1264,10 @@ public:
|
||||
/** set a single wallet flag */
|
||||
void SetWalletFlag(uint64_t flags);
|
||||
|
||||
/** Unsets a single wallet flag */
|
||||
void UnsetWalletFlag(uint64_t flag);
|
||||
void UnsetWalletFlag(WalletBatch& batch, uint64_t flag);
|
||||
|
||||
/** check if a certain wallet flag is set */
|
||||
bool IsWalletFlagSet(uint64_t flag);
|
||||
|
||||
|
@ -37,7 +37,7 @@ static std::shared_ptr<CWallet> CreateWallet(const std::string& name, const fs::
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
wallet_instance->SetMinVersion(FEATURE_HD);
|
||||
wallet_instance->SetMinVersion(FEATURE_COMPRPUBKEY);
|
||||
|
||||
// generate a new HD seed
|
||||
// NOTE: we do not yet create HD wallets by default
|
||||
|
@ -135,8 +135,8 @@ BASE_SCRIPTS = [
|
||||
'mempool_reorg.py',
|
||||
'mempool_persist.py',
|
||||
'wallet_multiwallet.py',
|
||||
'wallet_disableprivatekeys.py',
|
||||
'wallet_disableprivatekeys.py --usecli',
|
||||
'wallet_createwallet.py',
|
||||
'wallet_createwallet.py --usecli',
|
||||
'interface_http.py',
|
||||
'interface_rpc.py',
|
||||
'rpc_psbt.py',
|
||||
|
100
test/functional/wallet_createwallet.py
Executable file
100
test/functional/wallet_createwallet.py
Executable file
@ -0,0 +1,100 @@
|
||||
#!/usr/bin/env python3
|
||||
# Copyright (c) 2018 The Bitcoin Core developers
|
||||
# Distributed under the MIT software license, see the accompanying
|
||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
"""Test createwallet arguments.
|
||||
"""
|
||||
|
||||
from test_framework.test_framework import BitcoinTestFramework
|
||||
from test_framework.util import (
|
||||
assert_equal,
|
||||
assert_raises_rpc_error,
|
||||
)
|
||||
|
||||
class CreateWalletTest(BitcoinTestFramework):
|
||||
def set_test_params(self):
|
||||
self.setup_clean_chain = False
|
||||
self.num_nodes = 1
|
||||
self.supports_cli = True
|
||||
|
||||
def skip_test_if_missing_module(self):
|
||||
self.skip_if_no_wallet()
|
||||
|
||||
def run_test(self):
|
||||
node = self.nodes[0]
|
||||
node.generate(1) # Leave IBD for sethdseed
|
||||
|
||||
self.nodes[0].createwallet(wallet_name='w0')
|
||||
w0 = node.get_wallet_rpc('w0')
|
||||
address1 = w0.getnewaddress()
|
||||
|
||||
self.log.info("Test disableprivatekeys creation.")
|
||||
self.nodes[0].createwallet(wallet_name='w1', disable_private_keys=True)
|
||||
w1 = node.get_wallet_rpc('w1')
|
||||
assert_raises_rpc_error(-4, "Error: Private keys are disabled for this wallet", w1.getnewaddress)
|
||||
assert_raises_rpc_error(-4, "Error: Private keys are disabled for this wallet", w1.getrawchangeaddress)
|
||||
w1.importpubkey(w0.getaddressinfo(address1)['pubkey'])
|
||||
|
||||
self.log.info('Test that private keys cannot be imported')
|
||||
addr = w0.getnewaddress()
|
||||
privkey = w0.dumpprivkey(addr)
|
||||
assert_raises_rpc_error(-4, 'Cannot import private keys to a wallet with private keys disabled', w1.importprivkey, privkey)
|
||||
result = w1.importmulti([{'scriptPubKey': {'address': addr}, 'timestamp': 'now', 'keys': [privkey]}])
|
||||
assert(not result[0]['success'])
|
||||
assert('warning' not in result[0])
|
||||
assert_equal(result[0]['error']['code'], -4)
|
||||
assert_equal(result[0]['error']['message'], 'Cannot import private keys to a wallet with private keys disabled')
|
||||
|
||||
self.log.info("Test blank creation with private keys disabled.")
|
||||
self.nodes[0].createwallet(wallet_name='w2', disable_private_keys=True, blank=True)
|
||||
w2 = node.get_wallet_rpc('w2')
|
||||
assert_raises_rpc_error(-4, "Error: Private keys are disabled for this wallet", w2.getnewaddress)
|
||||
assert_raises_rpc_error(-4, "Error: Private keys are disabled for this wallet", w2.getrawchangeaddress)
|
||||
w2.importpubkey(w0.getaddressinfo(address1)['pubkey'])
|
||||
|
||||
self.log.info("Test blank creation with private keys enabled.")
|
||||
self.nodes[0].createwallet(wallet_name='w3', disable_private_keys=False, blank=True)
|
||||
w3 = node.get_wallet_rpc('w3')
|
||||
assert_equal(w3.getwalletinfo()['keypoolsize'], 0)
|
||||
assert_raises_rpc_error(-4, "Error: This wallet has no available keys", w3.getnewaddress)
|
||||
assert_raises_rpc_error(-4, "Error: This wallet has no available keys", w3.getrawchangeaddress)
|
||||
# Import private key
|
||||
w3.importprivkey(w0.dumpprivkey(address1))
|
||||
# Imported private keys are currently ignored by the keypool
|
||||
assert_equal(w3.getwalletinfo()['keypoolsize'], 0)
|
||||
assert_raises_rpc_error(-4, "Error: This wallet has no available keys", w3.getnewaddress)
|
||||
# Set the seed
|
||||
w3.upgradetohd()
|
||||
assert_equal(w3.getwalletinfo()['keypoolsize'], 1)
|
||||
w3.getnewaddress()
|
||||
w3.getrawchangeaddress()
|
||||
|
||||
self.log.info("Test blank creation with privkeys enabled and then encryption")
|
||||
self.nodes[0].createwallet(wallet_name='w4', disable_private_keys=False, blank=True)
|
||||
w4 = node.get_wallet_rpc('w4')
|
||||
assert_equal(w4.getwalletinfo()['keypoolsize'], 0)
|
||||
assert_raises_rpc_error(-4, "Error: This wallet has no available keys", w4.getnewaddress)
|
||||
assert_raises_rpc_error(-4, "Error: This wallet has no available keys", w4.getrawchangeaddress)
|
||||
# Encrypt the wallet. Nothing should change about the keypool
|
||||
w4.encryptwallet('pass')
|
||||
assert_raises_rpc_error(-4, "Error: This wallet has no available keys", w4.getnewaddress)
|
||||
assert_raises_rpc_error(-4, "Error: This wallet has no available keys", w4.getrawchangeaddress)
|
||||
# Now set a seed and it should work. Wallet should also be encrypted
|
||||
w4.walletpassphrase('pass', 2)
|
||||
w4.upgradetohd(walletpassphrase='pass')
|
||||
w4.getnewaddress()
|
||||
w4.getrawchangeaddress()
|
||||
|
||||
self.log.info("Test blank creation with privkeys disabled and then encryption")
|
||||
self.nodes[0].createwallet(wallet_name='w5', disable_private_keys=True, blank=True)
|
||||
w5 = node.get_wallet_rpc('w5')
|
||||
assert_equal(w5.getwalletinfo()['keypoolsize'], 0)
|
||||
assert_raises_rpc_error(-4, "Error: Private keys are disabled for this wallet", w5.getnewaddress)
|
||||
assert_raises_rpc_error(-4, "Error: Private keys are disabled for this wallet", w5.getrawchangeaddress)
|
||||
# Encrypt the wallet
|
||||
assert_raises_rpc_error(-16, "Error: wallet does not contain private keys, nothing to encrypt.", w5.encryptwallet, 'pass')
|
||||
assert_raises_rpc_error(-4, "Error: Private keys are disabled for this wallet", w5.getnewaddress)
|
||||
assert_raises_rpc_error(-4, "Error: Private keys are disabled for this wallet", w5.getrawchangeaddress)
|
||||
|
||||
if __name__ == '__main__':
|
||||
CreateWalletTest().main()
|
@ -1,46 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
# Copyright (c) 2018 The Bitcoin Core developers
|
||||
# Distributed under the MIT software license, see the accompanying
|
||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
"""Test disable-privatekeys mode.
|
||||
"""
|
||||
|
||||
from test_framework.test_framework import BitcoinTestFramework
|
||||
from test_framework.util import (
|
||||
assert_equal,
|
||||
assert_raises_rpc_error,
|
||||
)
|
||||
|
||||
|
||||
class DisablePrivateKeysTest(BitcoinTestFramework):
|
||||
def set_test_params(self):
|
||||
self.setup_clean_chain = False
|
||||
self.num_nodes = 1
|
||||
self.supports_cli = True
|
||||
|
||||
def skip_test_if_missing_module(self):
|
||||
self.skip_if_no_wallet()
|
||||
|
||||
def run_test(self):
|
||||
node = self.nodes[0]
|
||||
self.log.info("Test disableprivatekeys creation.")
|
||||
self.nodes[0].createwallet('w1', True)
|
||||
self.nodes[0].createwallet('w2')
|
||||
w1 = node.get_wallet_rpc('w1')
|
||||
w2 = node.get_wallet_rpc('w2')
|
||||
assert_raises_rpc_error(-4,"Error: Private keys are disabled for this wallet", w1.getnewaddress)
|
||||
assert_raises_rpc_error(-4,"Error: Private keys are disabled for this wallet", w1.getrawchangeaddress)
|
||||
w1.importpubkey(w2.getaddressinfo(w2.getnewaddress())['pubkey'])
|
||||
|
||||
self.log.info('Test that private keys cannot be imported')
|
||||
addr = w2.getnewaddress('')
|
||||
privkey = w2.dumpprivkey(addr)
|
||||
assert_raises_rpc_error(-4, 'Cannot import private keys to a wallet with private keys disabled', w1.importprivkey, privkey)
|
||||
result = w1.importmulti([{'scriptPubKey': {'address': addr}, 'timestamp': 'now', 'keys': [privkey]}])
|
||||
assert not result[0]['success']
|
||||
assert 'warning' not in result[0]
|
||||
assert_equal(result[0]['error']['code'], -4)
|
||||
assert_equal(result[0]['error']['message'], 'Cannot import private keys to a wallet with private keys disabled')
|
||||
|
||||
if __name__ == '__main__':
|
||||
DisablePrivateKeysTest().main()
|
Loading…
Reference in New Issue
Block a user