Skip to content

Commit 64dfe3c

Browse files
committed
wallet: Move internal to be per key when importing
Instead of applying internal-ness to all keys being imported at the same time, apply it on a per key basis. So each key that is imported will carry with it whether it is for the change keypool.
1 parent 1692245 commit 64dfe3c

File tree

6 files changed

+16
-14
lines changed

6 files changed

+16
-14
lines changed

src/qt/test/wallettests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ std::shared_ptr<CWallet> SetupLegacyWatchOnlyWallet(interfaces::Node& node, Test
199199
wallet->SetupLegacyScriptPubKeyMan();
200200
// Add watched key
201201
CPubKey pubKey = test.coinbaseKey.GetPubKey();
202-
bool import_keys = wallet->ImportPubKeys({pubKey.GetID()}, {{pubKey.GetID(), pubKey}} , /*key_origins=*/{}, /*add_keypool=*/false, /*internal=*/false, /*timestamp=*/1);
202+
bool import_keys = wallet->ImportPubKeys({{pubKey.GetID(), false}}, {{pubKey.GetID(), pubKey}} , /*key_origins=*/{}, /*add_keypool=*/false, /*timestamp=*/1);
203203
assert(import_keys);
204204
wallet->SetLastBlockProcessed(105, WITH_LOCK(node.context()->chainman->GetMutex(), return node.context()->chainman->ActiveChain().Tip()->GetBlockHash()));
205205
}

src/wallet/rpc/backup.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ RPCHelpMan importpubkey()
471471

472472
pwallet->ImportScriptPubKeys(strLabel, script_pub_keys, /*have_solving_data=*/true, /*apply_label=*/true, /*timestamp=*/1);
473473

474-
pwallet->ImportPubKeys({pubKey.GetID()}, {{pubKey.GetID(), pubKey}} , /*key_origins=*/{}, /*add_keypool=*/false, /*internal=*/false, /*timestamp=*/1);
474+
pwallet->ImportPubKeys({{pubKey.GetID(), false}}, {{pubKey.GetID(), pubKey}} , /*key_origins=*/{}, /*add_keypool=*/false, /*timestamp=*/1);
475475
}
476476
if (fRescan)
477477
{
@@ -915,7 +915,7 @@ static std::string RecurseImportData(const CScript& script, ImportData& import_d
915915
NONFATAL_UNREACHABLE();
916916
}
917917

918-
static UniValue ProcessImportLegacy(ImportData& import_data, std::map<CKeyID, CPubKey>& pubkey_map, std::map<CKeyID, CKey>& privkey_map, std::set<CScript>& script_pub_keys, bool& have_solving_data, const UniValue& data, std::vector<CKeyID>& ordered_pubkeys)
918+
static UniValue ProcessImportLegacy(ImportData& import_data, std::map<CKeyID, CPubKey>& pubkey_map, std::map<CKeyID, CKey>& privkey_map, std::set<CScript>& script_pub_keys, bool& have_solving_data, const UniValue& data, std::vector<std::pair<CKeyID, bool>>& ordered_pubkeys)
919919
{
920920
UniValue warnings(UniValue::VARR);
921921

@@ -981,7 +981,7 @@ static UniValue ProcessImportLegacy(ImportData& import_data, std::map<CKeyID, CP
981981
for (size_t i = 0; i < pubKeys.size(); ++i) {
982982
CPubKey pubkey = HexToPubKey(pubKeys[i].get_str());
983983
pubkey_map.emplace(pubkey.GetID(), pubkey);
984-
ordered_pubkeys.push_back(pubkey.GetID());
984+
ordered_pubkeys.emplace_back(pubkey.GetID(), internal);
985985
}
986986
for (size_t i = 0; i < keys.size(); ++i) {
987987
const auto& str = keys[i].get_str();
@@ -1054,8 +1054,10 @@ static UniValue ProcessImportLegacy(ImportData& import_data, std::map<CKeyID, CP
10541054
return warnings;
10551055
}
10561056

1057-
static UniValue ProcessImportDescriptor(ImportData& import_data, std::map<CKeyID, CPubKey>& pubkey_map, std::map<CKeyID, CKey>& privkey_map, std::set<CScript>& script_pub_keys, bool& have_solving_data, const UniValue& data, std::vector<CKeyID>& ordered_pubkeys)
1057+
static UniValue ProcessImportDescriptor(ImportData& import_data, std::map<CKeyID, CPubKey>& pubkey_map, std::map<CKeyID, CKey>& privkey_map, std::set<CScript>& script_pub_keys, bool& have_solving_data, const UniValue& data, std::vector<std::pair<CKeyID, bool>>& ordered_pubkeys)
10581058
{
1059+
const bool internal = data.exists("internal") ? data["internal"].get_bool() : false;
1060+
10591061
UniValue warnings(UniValue::VARR);
10601062

10611063
const std::string& descriptor = data["desc"].get_str();
@@ -1092,7 +1094,7 @@ static UniValue ProcessImportDescriptor(ImportData& import_data, std::map<CKeyID
10921094
parsed_desc->Expand(i, keys, scripts_temp, out_keys);
10931095
std::copy(scripts_temp.begin(), scripts_temp.end(), std::inserter(script_pub_keys, script_pub_keys.end()));
10941096
for (const auto& key_pair : out_keys.pubkeys) {
1095-
ordered_pubkeys.push_back(key_pair.first);
1097+
ordered_pubkeys.emplace_back(key_pair.first, internal);
10961098
}
10971099

10981100
for (const auto& x : out_keys.scripts) {
@@ -1167,7 +1169,7 @@ static UniValue ProcessImport(CWallet& wallet, const UniValue& data, const int64
11671169
std::map<CKeyID, CPubKey> pubkey_map;
11681170
std::map<CKeyID, CKey> privkey_map;
11691171
std::set<CScript> script_pub_keys;
1170-
std::vector<CKeyID> ordered_pubkeys;
1172+
std::vector<std::pair<CKeyID, bool>> ordered_pubkeys;
11711173
bool have_solving_data;
11721174

11731175
if (data.exists("scriptPubKey") && data.exists("desc")) {
@@ -1200,7 +1202,7 @@ static UniValue ProcessImport(CWallet& wallet, const UniValue& data, const int64
12001202
if (!wallet.ImportPrivKeys(privkey_map, timestamp)) {
12011203
throw JSONRPCError(RPC_WALLET_ERROR, "Error adding key to wallet");
12021204
}
1203-
if (!wallet.ImportPubKeys(ordered_pubkeys, pubkey_map, import_data.key_origins, add_keypool, internal, timestamp)) {
1205+
if (!wallet.ImportPubKeys(ordered_pubkeys, pubkey_map, import_data.key_origins, add_keypool, timestamp)) {
12041206
throw JSONRPCError(RPC_WALLET_ERROR, "Error adding address to wallet");
12051207
}
12061208
if (!wallet.ImportScriptPubKeys(label, script_pub_keys, have_solving_data, !internal, timestamp)) {

src/wallet/scriptpubkeyman.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1623,13 +1623,13 @@ bool LegacyScriptPubKeyMan::ImportPrivKeys(const std::map<CKeyID, CKey>& privkey
16231623
return true;
16241624
}
16251625

1626-
bool LegacyScriptPubKeyMan::ImportPubKeys(const std::vector<CKeyID>& ordered_pubkeys, const std::map<CKeyID, CPubKey>& pubkey_map, const std::map<CKeyID, std::pair<CPubKey, KeyOriginInfo>>& key_origins, const bool add_keypool, const bool internal, const int64_t timestamp)
1626+
bool LegacyScriptPubKeyMan::ImportPubKeys(const std::vector<std::pair<CKeyID, bool>>& ordered_pubkeys, const std::map<CKeyID, CPubKey>& pubkey_map, const std::map<CKeyID, std::pair<CPubKey, KeyOriginInfo>>& key_origins, const bool add_keypool, const int64_t timestamp)
16271627
{
16281628
WalletBatch batch(m_storage.GetDatabase());
16291629
for (const auto& entry : key_origins) {
16301630
AddKeyOriginWithDB(batch, entry.second.first, entry.second.second);
16311631
}
1632-
for (const CKeyID& id : ordered_pubkeys) {
1632+
for (const auto& [id, internal] : ordered_pubkeys) {
16331633
auto entry = pubkey_map.find(id);
16341634
if (entry == pubkey_map.end()) {
16351635
continue;

src/wallet/scriptpubkeyman.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ class LegacyScriptPubKeyMan : public ScriptPubKeyMan, public FillableSigningProv
484484

485485
bool ImportScripts(const std::set<CScript> scripts, int64_t timestamp) EXCLUSIVE_LOCKS_REQUIRED(cs_KeyStore);
486486
bool ImportPrivKeys(const std::map<CKeyID, CKey>& privkey_map, const int64_t timestamp) EXCLUSIVE_LOCKS_REQUIRED(cs_KeyStore);
487-
bool ImportPubKeys(const std::vector<CKeyID>& ordered_pubkeys, const std::map<CKeyID, CPubKey>& pubkey_map, const std::map<CKeyID, std::pair<CPubKey, KeyOriginInfo>>& key_origins, const bool add_keypool, const bool internal, const int64_t timestamp) EXCLUSIVE_LOCKS_REQUIRED(cs_KeyStore);
487+
bool ImportPubKeys(const std::vector<std::pair<CKeyID, bool>>& ordered_pubkeys, const std::map<CKeyID, CPubKey>& pubkey_map, const std::map<CKeyID, std::pair<CPubKey, KeyOriginInfo>>& key_origins, const bool add_keypool, const int64_t timestamp) EXCLUSIVE_LOCKS_REQUIRED(cs_KeyStore);
488488
bool ImportScriptPubKeys(const std::set<CScript>& script_pub_keys, const bool have_solving_data, const int64_t timestamp) EXCLUSIVE_LOCKS_REQUIRED(cs_KeyStore);
489489

490490
/* Returns true if the wallet can generate new keys */

src/wallet/wallet.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1773,14 +1773,14 @@ bool CWallet::ImportPrivKeys(const std::map<CKeyID, CKey>& privkey_map, const in
17731773
return spk_man->ImportPrivKeys(privkey_map, timestamp);
17741774
}
17751775

1776-
bool CWallet::ImportPubKeys(const std::vector<CKeyID>& ordered_pubkeys, const std::map<CKeyID, CPubKey>& pubkey_map, const std::map<CKeyID, std::pair<CPubKey, KeyOriginInfo>>& key_origins, const bool add_keypool, const bool internal, const int64_t timestamp)
1776+
bool CWallet::ImportPubKeys(const std::vector<std::pair<CKeyID, bool>>& ordered_pubkeys, const std::map<CKeyID, CPubKey>& pubkey_map, const std::map<CKeyID, std::pair<CPubKey, KeyOriginInfo>>& key_origins, const bool add_keypool, const int64_t timestamp)
17771777
{
17781778
auto spk_man = GetLegacyScriptPubKeyMan();
17791779
if (!spk_man) {
17801780
return false;
17811781
}
17821782
LOCK(spk_man->cs_KeyStore);
1783-
return spk_man->ImportPubKeys(ordered_pubkeys, pubkey_map, key_origins, add_keypool, internal, timestamp);
1783+
return spk_man->ImportPubKeys(ordered_pubkeys, pubkey_map, key_origins, add_keypool, timestamp);
17841784
}
17851785

17861786
bool CWallet::ImportScriptPubKeys(const std::string& label, const std::set<CScript>& script_pub_keys, const bool have_solving_data, const bool apply_label, const int64_t timestamp)

src/wallet/wallet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
686686

687687
bool ImportScripts(const std::set<CScript> scripts, int64_t timestamp) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
688688
bool ImportPrivKeys(const std::map<CKeyID, CKey>& privkey_map, const int64_t timestamp) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
689-
bool ImportPubKeys(const std::vector<CKeyID>& ordered_pubkeys, const std::map<CKeyID, CPubKey>& pubkey_map, const std::map<CKeyID, std::pair<CPubKey, KeyOriginInfo>>& key_origins, const bool add_keypool, const bool internal, const int64_t timestamp) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
689+
bool ImportPubKeys(const std::vector<std::pair<CKeyID, bool>>& ordered_pubkeys, const std::map<CKeyID, CPubKey>& pubkey_map, const std::map<CKeyID, std::pair<CPubKey, KeyOriginInfo>>& key_origins, const bool add_keypool, const int64_t timestamp) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
690690
bool ImportScriptPubKeys(const std::string& label, const std::set<CScript>& script_pub_keys, const bool have_solving_data, const bool apply_label, const int64_t timestamp) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
691691

692692
/** Updates wallet birth time if 'time' is below it */

0 commit comments

Comments
 (0)