Skip to content

Commit a775f7c

Browse files
committed
Implement Unlock and Encrypt in DescriptorScriptPubKeyMan
1 parent bfdd073 commit a775f7c

File tree

1 file changed

+47
-2
lines changed

1 file changed

+47
-2
lines changed

src/wallet/scriptpubkeyman.cpp

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1553,12 +1553,57 @@ isminetype DescriptorScriptPubKeyMan::IsMine(const CScript& script) const
15531553

15541554
bool DescriptorScriptPubKeyMan::CheckDecryptionKey(const CKeyingMaterial& master_key, bool accept_no_keys)
15551555
{
1556-
return false;
1556+
LOCK(cs_desc_man);
1557+
if (!m_map_keys.empty()) {
1558+
return false;
1559+
}
1560+
1561+
bool keyPass = m_map_crypted_keys.empty(); // Always pass when there are no encrypted keys
1562+
bool keyFail = false;
1563+
for (const auto& mi : m_map_crypted_keys) {
1564+
const CPubKey &pubkey = mi.second.first;
1565+
const std::vector<unsigned char> &crypted_secret = mi.second.second;
1566+
CKey key;
1567+
if (!DecryptKey(master_key, crypted_secret, pubkey, key)) {
1568+
keyFail = true;
1569+
break;
1570+
}
1571+
keyPass = true;
1572+
if (m_decryption_thoroughly_checked)
1573+
break;
1574+
}
1575+
if (keyPass && keyFail) {
1576+
LogPrintf("The wallet is probably corrupted: Some keys decrypt but not all.\n");
1577+
throw std::runtime_error("Error unlocking wallet: some keys decrypt but not all. Your wallet file may be corrupt.");
1578+
}
1579+
if (keyFail || (!keyPass && !accept_no_keys)) {
1580+
return false;
1581+
}
1582+
m_decryption_thoroughly_checked = true;
1583+
return true;
15571584
}
15581585

15591586
bool DescriptorScriptPubKeyMan::Encrypt(const CKeyingMaterial& master_key, WalletBatch* batch)
15601587
{
1561-
return false;
1588+
LOCK(cs_desc_man);
1589+
if (!m_map_crypted_keys.empty()) {
1590+
return false;
1591+
}
1592+
1593+
for (const KeyMap::value_type& key_in : m_map_keys)
1594+
{
1595+
const CKey &key = key_in.second;
1596+
CPubKey pubkey = key.GetPubKey();
1597+
CKeyingMaterial secret(key.begin(), key.end());
1598+
std::vector<unsigned char> crypted_secret;
1599+
if (!EncryptSecret(master_key, secret, pubkey.GetHash(), crypted_secret)) {
1600+
return false;
1601+
}
1602+
m_map_crypted_keys[pubkey.GetID()] = make_pair(pubkey, crypted_secret);
1603+
batch->WriteCryptedDescriptorKey(GetID(), pubkey, crypted_secret);
1604+
}
1605+
m_map_keys.clear();
1606+
return true;
15621607
}
15631608

15641609
bool DescriptorScriptPubKeyMan::GetReservedDestination(const OutputType type, bool internal, CTxDestination& address, int64_t& index, CKeyPool& keypool)

0 commit comments

Comments
 (0)