27 error =
_(
"Error: Legacy wallets only support the \"legacy\", \"p2sh-segwit\", and \"bech32\" address types");
38 error =
_(
"Error: Keypool ran out, please call keypoolrefill first");
46 typedef std::vector<unsigned char>
valtype;
56 enum class IsMineSigVersion
68 enum class IsMineResult
76 bool PermitsUncompressed(IsMineSigVersion sigversion)
78 return sigversion == IsMineSigVersion::TOP || sigversion == IsMineSigVersion::P2SH;
83 for (
const valtype& pubkey : pubkeys) {
85 if (!keystore.
HaveKey(keyID))
return false;
98 IsMineResult IsMineInner(
const LegacyScriptPubKeyMan& keystore,
const CScript& scriptPubKey, IsMineSigVersion sigversion,
bool recurse_scripthash=
true)
100 IsMineResult ret = IsMineResult::NO;
102 std::vector<valtype> vSolutions;
114 if (!PermitsUncompressed(sigversion) && vSolutions[0].size() != 33) {
115 return IsMineResult::INVALID;
118 ret = std::max(ret, IsMineResult::SPENDABLE);
123 if (sigversion == IsMineSigVersion::WITNESS_V0) {
125 return IsMineResult::INVALID;
138 if (!PermitsUncompressed(sigversion)) {
141 return IsMineResult::INVALID;
145 ret = std::max(ret, IsMineResult::SPENDABLE);
150 if (sigversion != IsMineSigVersion::TOP) {
152 return IsMineResult::INVALID;
156 if (keystore.
GetCScript(scriptID, subscript)) {
157 ret = std::max(ret, recurse_scripthash ? IsMineInner(keystore, subscript, IsMineSigVersion::P2SH) : IsMineResult::SPENDABLE);
163 if (sigversion == IsMineSigVersion::WITNESS_V0) {
165 return IsMineResult::INVALID;
174 if (keystore.
GetCScript(scriptID, subscript)) {
175 ret = std::max(ret, recurse_scripthash ? IsMineInner(keystore, subscript, IsMineSigVersion::WITNESS_V0) : IsMineResult::SPENDABLE);
183 if (sigversion == IsMineSigVersion::TOP) {
192 std::vector<valtype> keys(vSolutions.begin()+1, vSolutions.begin()+vSolutions.size()-1);
193 if (!PermitsUncompressed(sigversion)) {
194 for (
size_t i = 0; i < keys.size(); i++) {
195 if (keys[i].size() != 33) {
196 return IsMineResult::INVALID;
200 if (HaveKeys(keys, keystore)) {
201 ret = std::max(ret, IsMineResult::SPENDABLE);
207 if (ret == IsMineResult::NO && keystore.
HaveWatchOnly(scriptPubKey)) {
208 ret = std::max(ret, IsMineResult::WATCH_ONLY);
217 switch (IsMineInner(*
this, script, IsMineSigVersion::TOP)) {
218 case IsMineResult::INVALID:
219 case IsMineResult::NO:
221 case IsMineResult::WATCH_ONLY:
223 case IsMineResult::SPENDABLE:
235 bool keyPass = mapCryptedKeys.empty();
236 bool keyFail =
false;
237 CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
239 for (; mi != mapCryptedKeys.end(); ++mi)
241 const CPubKey &vchPubKey = (*mi).second.first;
242 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
244 if (!
DecryptKey(master_key, vchCryptedSecret, vchPubKey, key))
257 if (keyPass && keyFail)
259 LogPrintf(
"The wallet is probably corrupted: Some keys decrypt but not all.\n");
260 throw std::runtime_error(
"Error unlocking wallet: some keys decrypt but not all. Your wallet file may be corrupt.");
262 if (keyFail || (!keyPass && !accept_no_keys))
272 encrypted_batch = batch;
273 if (!mapCryptedKeys.empty()) {
274 encrypted_batch =
nullptr;
279 keys_to_encrypt.swap(mapKeys);
280 for (
const KeyMap::value_type& mKey : keys_to_encrypt)
282 const CKey &key = mKey.second;
285 std::vector<unsigned char> vchCryptedSecret;
287 encrypted_batch =
nullptr;
291 encrypted_batch =
nullptr;
295 encrypted_batch =
nullptr;
302 error =
_(
"Error: Legacy wallets only support the \"legacy\", \"p2sh-segwit\", and \"bech32\" address types");
309 error =
_(
"Error: Keypool ran out, please call keypoolrefill first");
314 error =
_(
"Error: Keypool ran out, please call keypoolrefill first");
342 int64_t missing = std::max(target_size - kp_size, (int64_t) 0);
346 for (int64_t i = missing; i > 0; --i) {
361 std::vector<WalletDestination> result;
366 WalletLogPrintf(
"%s: Detected a used keypool key, mark all keypool keys up to this key as used\n", __func__);
371 result.push_back({dest, keypool.fInternal});
376 WalletLogPrintf(
"%s: Topping up keypool failed (locked wallet)\n", __func__);
382 auto it = mapKeyMetadata.find(keyid);
383 if (it != mapKeyMetadata.end()){
407 for (
auto& meta_pair : mapKeyMetadata) {
418 throw std::runtime_error(
"Invalid stored hdKeypath");
427 if (
GetPubKey(meta_pair.first, pubkey)) {
428 batch->WriteKeyMetadata(meta, pubkey,
true);
456 bool keypool_has_keys;
458 keypool_has_keys = setInternalKeyPool.size() > 0;
463 if (!keypool_has_keys) {
466 return keypool_has_keys;
472 bool hd_upgrade =
false;
473 bool split_upgrade =
false;
492 throw std::runtime_error(std::string(__func__) +
": writing chain failed");
503 error =
_(
"Unable to generate keys");
513 return !mapKeys.empty() || !mapCryptedKeys.empty();
519 setInternalKeyPool.clear();
520 setExternalKeyPool.clear();
528 if (setKeyPool.empty()) {
533 int64_t nIndex = *(setKeyPool.begin());
534 if (!batch.
ReadPool(nIndex, keypool)) {
535 throw std::runtime_error(std::string(__func__) +
": read oldest key in keypool failed");
538 return keypool.
nTime;
551 if (!set_pre_split_keypool.empty()) {
562 return setExternalKeyPool.size() + set_pre_split_keypool.size();
568 return setInternalKeyPool.size() + setExternalKeyPool.size() + set_pre_split_keypool.size();
574 return nTimeFirstKey;
579 return std::make_unique<LegacySigningProvider>(*
this);
584 IsMineResult ismine = IsMineInner(*
this, script, IsMineSigVersion::TOP,
false);
585 if (ismine == IsMineResult::SPENDABLE || ismine == IsMineResult::WATCH_ONLY) {
595 bool has_privkeys =
false;
596 for (
const auto& key_sig_pair : sigdata.
signatures) {
597 has_privkeys |=
HaveKey(key_sig_pair.first);
628 for (
unsigned int i = 0; i < psbtx.
tx->vin.size(); ++i) {
629 const CTxIn& txin = psbtx.
tx->vin[i];
655 if (n_signed && (signed_one || !sign)) {
664 for (
unsigned int i = 0; i < psbtx.
tx->vout.size(); ++i) {
677 auto it = mapKeyMetadata.find(key_id);
678 if (it != mapKeyMetadata.end()) {
679 return std::make_unique<CKeyMetadata>(it->second);
684 auto it = m_script_metadata.find(
CScriptID(scriptPubKey));
685 if (it != m_script_metadata.end()) {
686 return std::make_unique<CKeyMetadata>(it->second);
704 if (nCreateTime <= 1) {
708 }
else if (!nTimeFirstKey || nCreateTime < nTimeFirstKey) {
709 nTimeFirstKey = nCreateTime;
735 bool needsDB = !encrypted_batch;
737 encrypted_batch = &batch;
740 if (needsDB) encrypted_batch =
nullptr;
743 if (needsDB) encrypted_batch =
nullptr;
759 mapKeyMetadata[pubkey.
GetID()]);
773 WalletLogPrintf(
"%s: Warning: This wallet contains a redeemScript of size %i which exceeds maximum size %i thus can never be redeemed. Do not use address %s.\n", __func__, redeemScript.
size(),
MAX_SCRIPT_ELEMENT_SIZE, strAddr);
784 mapKeyMetadata[keyID] = meta;
791 m_script_metadata[script_id] = meta;
805 std::vector<unsigned char> vchCryptedSecret;
820 if (!checksum_valid) {
832 mapCryptedKeys[vchPubKey.
GetID()] = make_pair(vchPubKey, vchCryptedSecret);
838 const std::vector<unsigned char> &vchCryptedSecret)
845 return encrypted_batch->WriteCryptedKey(vchPubKey,
847 mapKeyMetadata[vchPubKey.
GetID()]);
851 mapKeyMetadata[vchPubKey.
GetID()]);
858 return setWatchOnly.count(dest) > 0;
864 return (!setWatchOnly.empty());
869 std::vector<std::vector<unsigned char>> solutions;
871 (pubKeyOut =
CPubKey(solutions[0])).IsFullyValid();
878 setWatchOnly.erase(dest);
881 mapWatchKeys.erase(pubKey.
GetID());
903 setWatchOnly.insert(dest);
906 mapWatchKeys[pubKey.
GetID()] = pubKey;
928 m_script_metadata[
CScriptID(dest)].nCreateTime = create_time;
940 m_script_metadata[
CScriptID(dest)].nCreateTime = nCreateTime;
955 throw std::runtime_error(std::string(__func__) +
": writing chain failed");
978 return mapCryptedKeys.count(address) > 0;
988 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
989 if (mi != mapCryptedKeys.end())
991 const CPubKey &vchPubKey = (*mi).second.first;
992 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
1003 auto it = mapKeyMetadata.find(keyID);
1004 if (it != mapKeyMetadata.end()) {
1020 WatchKeyMap::const_iterator it = mapWatchKeys.find(address);
1021 if (it != mapWatchKeys.end()) {
1022 pubkey_out = it->second;
1038 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
1039 if (mi != mapCryptedKeys.end())
1041 vchPubKeyOut = (*mi).second.first;
1058 int64_t nCreationTime =
GetTime();
1076 mapKeyMetadata[pubkey.
GetID()] = metadata;
1080 throw std::runtime_error(std::string(__func__) +
": AddKey failed");
1096 throw std::runtime_error(std::string(__func__) +
": seed not found");
1130 secret = childKey.
key;
1137 throw std::runtime_error(std::string(__func__) +
": writing HD chain model failed");
1144 set_pre_split_keypool.insert(nIndex);
1146 setInternalKeyPool.insert(nIndex);
1148 setExternalKeyPool.insert(nIndex);
1150 m_max_keypool_index = std::max(m_max_keypool_index, nIndex);
1157 if (mapKeyMetadata.count(keyid) == 0)
1178 int64_t nCreationTime =
GetTime();
1194 mapKeyMetadata[seed.
GetID()] = metadata;
1198 throw std::runtime_error(std::string(__func__) +
": AddKeyPubKey failed");
1232 for (
const int64_t nIndex : setInternalKeyPool) {
1235 setInternalKeyPool.clear();
1237 for (
const int64_t nIndex : setExternalKeyPool) {
1240 setExternalKeyPool.clear();
1242 for (
const int64_t nIndex : set_pre_split_keypool) {
1245 set_pre_split_keypool.clear();
1252 WalletLogPrintf(
"LegacyScriptPubKeyMan::NewKeyPool rewrote keypool\n");
1268 unsigned int nTargetSize;
1270 nTargetSize = kpSize;
1276 int64_t missingExternal = std::max(std::max((int64_t) nTargetSize, (int64_t) 1) - (int64_t)setExternalKeyPool.size(), (int64_t) 0);
1277 int64_t missingInternal = std::max(std::max((int64_t) nTargetSize, (int64_t) 1) - (int64_t)setInternalKeyPool.size(), (int64_t) 0);
1282 missingInternal = 0;
1284 bool internal =
false;
1286 for (int64_t i = missingInternal + missingExternal; i--;)
1288 if (i < missingInternal) {
1295 if (missingInternal + missingExternal > 0) {
1296 WalletLogPrintf(
"keypool added %d keys (%d internal), size=%u (%u internal)\n", missingInternal + missingExternal, missingInternal, setInternalKeyPool.size() + setExternalKeyPool.size() + set_pre_split_keypool.size(), setInternalKeyPool.size());
1306 assert(m_max_keypool_index < std::numeric_limits<int64_t>::max());
1307 int64_t index = ++m_max_keypool_index;
1309 throw std::runtime_error(std::string(__func__) +
": writing imported pubkey failed");
1312 setInternalKeyPool.insert(index);
1314 setExternalKeyPool.insert(index);
1339 setInternalKeyPool.insert(nIndex);
1340 }
else if (!set_pre_split_keypool.empty()) {
1341 set_pre_split_keypool.insert(nIndex);
1343 setExternalKeyPool.insert(nIndex);
1383 bool fReturningInternal = fRequestedInternal;
1385 bool use_split_keypool = set_pre_split_keypool.empty();
1386 std::set<int64_t>& setKeyPool = use_split_keypool ? (fReturningInternal ? setInternalKeyPool : setExternalKeyPool) : set_pre_split_keypool;
1389 if (setKeyPool.empty()) {
1395 auto it = setKeyPool.begin();
1397 setKeyPool.erase(it);
1398 if (!batch.
ReadPool(nIndex, keypool)) {
1399 throw std::runtime_error(std::string(__func__) +
": read failed");
1403 throw std::runtime_error(std::string(__func__) +
": unknown key in key pool");
1406 if (use_split_keypool && keypool.
fInternal != fReturningInternal) {
1407 throw std::runtime_error(std::string(__func__) +
": keypool entry misclassified");
1410 throw std::runtime_error(std::string(__func__) +
": keypool entry invalid");
1443 bool internal = setInternalKeyPool.count(keypool_id);
1444 if (!
internal)
assert(setExternalKeyPool.count(keypool_id) || set_pre_split_keypool.count(keypool_id));
1445 std::set<int64_t> *setKeyPool =
internal ? &setInternalKeyPool : (set_pre_split_keypool.empty() ? &setExternalKeyPool : &set_pre_split_keypool);
1446 auto it = setKeyPool->begin();
1448 std::vector<CKeyPool> result;
1450 while (it != std::end(*setKeyPool)) {
1451 const int64_t& index = *(it);
1452 if (index > keypool_id)
break;
1455 if (batch.
ReadPool(index, keypool)) {
1461 it = setKeyPool->erase(it);
1462 result.push_back(std::move(keypool));
1470 std::vector<CScript> dummy;
1473 std::vector<CKeyID> ret;
1474 for (
const auto& entry : out.
pubkeys) {
1475 ret.push_back(entry.first);
1483 for (
auto it = setExternalKeyPool.begin(); it != setExternalKeyPool.end();) {
1484 int64_t index = *it;
1486 if (!batch.
ReadPool(index, keypool)) {
1487 throw std::runtime_error(std::string(__func__) +
": read keypool entry failed");
1491 throw std::runtime_error(std::string(__func__) +
": writing modified keypool entry failed");
1493 set_pre_split_keypool.insert(index);
1494 it = setExternalKeyPool.erase(it);
1519 mapKeyMetadata[pubkey.
GetID()].key_origin.path = info.
path;
1520 mapKeyMetadata[pubkey.
GetID()].has_key_origin =
true;
1528 for (
const auto& entry : scripts) {
1538 if (timestamp > 0) {
1539 m_script_metadata[
CScriptID(entry)].nCreateTime = timestamp;
1542 if (timestamp > 0) {
1552 for (
const auto& entry : privkey_map) {
1553 const CKey& key = entry.second;
1555 const CKeyID&
id = entry.first;
1562 mapKeyMetadata[
id].nCreateTime = timestamp;
1572 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)
1575 for (
const auto& entry : key_origins) {
1578 for (
const CKeyID&
id : ordered_pubkeys) {
1579 auto entry = pubkey_map.find(
id);
1580 if (entry == pubkey_map.end()) {
1583 const CPubKey& pubkey = entry->second;
1593 mapKeyMetadata[
id].nCreateTime = timestamp;
1607 for (
const CScript& script : script_pub_keys) {
1608 if (!have_solving_data || !
IsMine(script)) {
1623 std::set<CKeyID> set_address;
1624 for (
const auto& mi : mapCryptedKeys) {
1625 set_address.insert(mi.first);
1634 error =
_(
"No addresses available");
1639 assert(m_wallet_descriptor.descriptor->IsSingleType());
1640 std::optional<OutputType> desc_addr_type = m_wallet_descriptor.descriptor->GetOutputType();
1642 if (type != *desc_addr_type) {
1643 throw std::runtime_error(std::string(__func__) +
": Types are inconsistent");
1650 std::vector<CScript> scripts_temp;
1653 error =
_(
"Error: Keypool ran out, please call keypoolrefill first");
1656 if (!m_wallet_descriptor.descriptor->ExpandFromCache(m_wallet_descriptor.next_index, m_wallet_descriptor.cache, scripts_temp, out_keys)) {
1658 error =
_(
"Error: Keypool ran out, please call keypoolrefill first");
1662 std::optional<OutputType> out_script_type = m_wallet_descriptor.descriptor->GetOutputType();
1663 if (out_script_type && out_script_type == type) {
1666 throw std::runtime_error(std::string(__func__) +
": Types are inconsistent. Stored type does not match type of newly generated address");
1668 m_wallet_descriptor.next_index++;
1677 if (m_map_script_pub_keys.count(script) > 0) {
1686 if (!m_map_keys.empty()) {
1690 bool keyPass = m_map_crypted_keys.empty();
1691 bool keyFail =
false;
1692 for (
const auto& mi : m_map_crypted_keys) {
1693 const CPubKey &pubkey = mi.second.first;
1694 const std::vector<unsigned char> &crypted_secret = mi.second.second;
1696 if (!
DecryptKey(master_key, crypted_secret, pubkey, key)) {
1704 if (keyPass && keyFail) {
1705 LogPrintf(
"The wallet is probably corrupted: Some keys decrypt but not all.\n");
1706 throw std::runtime_error(
"Error unlocking wallet: some keys decrypt but not all. Your wallet file may be corrupt.");
1708 if (keyFail || (!keyPass && !accept_no_keys)) {
1718 if (!m_map_crypted_keys.empty()) {
1722 for (
const KeyMap::value_type& key_in : m_map_keys)
1724 const CKey &key = key_in.second;
1727 std::vector<unsigned char> crypted_secret;
1731 m_map_crypted_keys[pubkey.
GetID()] = make_pair(pubkey, crypted_secret);
1742 index = m_wallet_descriptor.next_index - 1;
1750 if (m_wallet_descriptor.next_index - 1 == index) {
1751 m_wallet_descriptor.next_index--;
1762 for (
auto key_pair : m_map_crypted_keys) {
1763 const CPubKey& pubkey = key_pair.second.first;
1764 const std::vector<unsigned char>& crypted_secret = key_pair.second.second;
1767 keys[pubkey.
GetID()] = key;
1777 unsigned int target_size;
1785 int32_t new_range_end = std::max(m_wallet_descriptor.next_index + (int32_t)target_size, m_wallet_descriptor.range_end);
1788 if (!m_wallet_descriptor.descriptor->IsRange()) {
1790 m_wallet_descriptor.range_end = 1;
1791 m_wallet_descriptor.range_start = 0;
1801 std::vector<CScript> scripts_temp;
1804 if (!m_wallet_descriptor.descriptor->ExpandFromCache(i, m_wallet_descriptor.cache, scripts_temp, out_keys)) {
1805 if (!m_wallet_descriptor.descriptor->Expand(i, provider, scripts_temp, out_keys, &temp_cache))
return false;
1808 for (
const CScript& script : scripts_temp) {
1809 m_map_script_pub_keys[script] = i;
1811 for (
const auto& pk_pair : out_keys.
pubkeys) {
1812 const CPubKey& pubkey = pk_pair.second;
1813 if (m_map_pubkeys.count(pubkey) != 0) {
1818 m_map_pubkeys[pubkey] = i;
1823 throw std::runtime_error(std::string(__func__) +
": writing cache items failed");
1827 m_wallet_descriptor.range_end = new_range_end;
1840 std::vector<WalletDestination> result;
1842 int32_t index = m_map_script_pub_keys[script];
1843 if (index >= m_wallet_descriptor.next_index) {
1844 WalletLogPrintf(
"%s: Detected a used keypool item at index %d, mark all keypool items up to this item as used\n", __func__, index);
1845 auto out_keys = std::make_unique<FlatSigningProvider>();
1846 std::vector<CScript> scripts_temp;
1847 while (index >= m_wallet_descriptor.next_index) {
1848 if (!m_wallet_descriptor.descriptor->ExpandFromCache(m_wallet_descriptor.next_index, m_wallet_descriptor.cache, scripts_temp, *out_keys)) {
1849 throw std::runtime_error(std::string(__func__) +
": Unable to expand descriptor from cache");
1853 result.push_back({dest, std::nullopt});
1854 m_wallet_descriptor.next_index++;
1858 WalletLogPrintf(
"%s: Topping up keypool failed (locked wallet)\n", __func__);
1870 throw std::runtime_error(std::string(__func__) +
": writing descriptor private key failed");
1880 if (m_map_keys.find(pubkey.
GetID()) != m_map_keys.end() ||
1881 m_map_crypted_keys.find(pubkey.
GetID()) != m_map_crypted_keys.end()) {
1890 std::vector<unsigned char> crypted_secret;
1896 m_map_crypted_keys[pubkey.
GetID()] = make_pair(pubkey, crypted_secret);
1899 m_map_keys[pubkey.
GetID()] = key;
1910 if (m_wallet_descriptor.descriptor) {
1914 int64_t creation_time =
GetTime();
1919 std::string desc_prefix;
1920 std::string desc_suffix =
"/*)";
1921 switch (addr_type) {
1923 desc_prefix =
"pkh(" + xpub +
"/44'";
1927 desc_prefix =
"sh(wpkh(" + xpub +
"/49'";
1932 desc_prefix =
"wpkh(" + xpub +
"/84'";
1936 desc_prefix =
"tr(" + xpub +
"/86'";
1940 assert(!desc_prefix.empty());
1943 if (
Params().IsTestChain()) {
1944 desc_prefix +=
"/1'";
1946 desc_prefix +=
"/0'";
1949 std::string internal_path =
internal ?
"/1" :
"/0";
1950 std::string desc_str = desc_prefix +
"/0'" + internal_path + desc_suffix;
1955 std::unique_ptr<Descriptor> desc =
Parse(desc_str, keys,
error,
false);
1957 m_wallet_descriptor = w_desc;
1962 throw std::runtime_error(std::string(__func__) +
": writing descriptor master private key failed");
1965 throw std::runtime_error(std::string(__func__) +
": writing descriptor failed");
1978 return m_wallet_descriptor.descriptor->IsRange();
1986 return m_wallet_descriptor.descriptor->IsSingleType() &&
1987 m_wallet_descriptor.descriptor->IsRange() &&
1988 (
HavePrivateKeys() || m_wallet_descriptor.next_index < m_wallet_descriptor.range_end);
1994 return m_map_keys.size() > 0 || m_map_crypted_keys.size() > 0;
2000 return std::nullopt;
2007 return m_wallet_descriptor.range_end - m_wallet_descriptor.next_index;
2013 return m_wallet_descriptor.creation_time;
2021 auto it = m_map_script_pub_keys.find(script);
2022 if (it == m_map_script_pub_keys.end()) {
2025 int32_t index = it->second;
2035 auto it = m_map_pubkeys.find(pubkey);
2036 if (it == m_map_pubkeys.end()) {
2039 int32_t index = it->second;
2049 std::unique_ptr<FlatSigningProvider> out_keys = std::make_unique<FlatSigningProvider>();
2050 std::vector<CScript> scripts_temp;
2051 if (!m_wallet_descriptor.descriptor->ExpandFromCache(index, m_wallet_descriptor.cache, scripts_temp, *out_keys))
return nullptr;
2056 m_wallet_descriptor.descriptor->ExpandPrivate(index, master_provider, *out_keys);
2074 std::unique_ptr<FlatSigningProvider> keys = std::make_unique<FlatSigningProvider>();
2075 for (
const auto& coin_pair : coins) {
2076 std::unique_ptr<FlatSigningProvider> coin_keys =
GetSigningProvider(coin_pair.second.out.scriptPubKey,
true);
2080 *keys =
Merge(*keys, *coin_keys);
2094 if (!keys->GetKey(
ToKeyID(pkhash), key)) {
2109 for (
unsigned int i = 0; i < psbtx.
tx->vin.size(); ++i) {
2110 const CTxIn& txin = psbtx.
tx->vin[i];
2138 std::unique_ptr<FlatSigningProvider> keys = std::make_unique<FlatSigningProvider>();
2141 *keys =
Merge(*keys, *script_keys);
2144 script_keys = std::make_unique<FlatSigningProvider>();
2146 const CPubKey& pubkey = pk_pair.first;
2149 *keys =
Merge(*keys, *pk_keys);
2157 if (n_signed && (signed_one || !sign)) {
2166 for (
unsigned int i = 0; i < psbtx.
tx->vout.size(); ++i) {
2183 if (provider->GetKeyOrigin(key_id, orig)) {
2185 std::unique_ptr<CKeyMetadata> meta = std::make_unique<CKeyMetadata>();
2186 meta->key_origin = orig;
2187 meta->has_key_origin =
true;
2188 meta->nCreateTime = m_wallet_descriptor.creation_time;
2198 std::string desc_str = m_wallet_descriptor.descriptor->ToString();
2207 m_wallet_descriptor.cache = cache;
2208 for (int32_t i = m_wallet_descriptor.range_start; i < m_wallet_descriptor.range_end; ++i) {
2210 std::vector<CScript> scripts_temp;
2211 if (!m_wallet_descriptor.descriptor->ExpandFromCache(i, m_wallet_descriptor.cache, scripts_temp, out_keys)) {
2212 throw std::runtime_error(
"Error: Unable to expand wallet descriptor from cache");
2215 for (
const CScript& script : scripts_temp) {
2216 if (m_map_script_pub_keys.count(script) != 0) {
2217 throw std::runtime_error(
strprintf(
"Error: Already loaded script at index %d as being at index %d", i, m_map_script_pub_keys[script]));
2219 m_map_script_pub_keys[script] = i;
2221 for (
const auto& pk_pair : out_keys.
pubkeys) {
2222 const CPubKey& pubkey = pk_pair.second;
2223 if (m_map_pubkeys.count(pubkey) != 0) {
2228 m_map_pubkeys[pubkey] = i;
2237 m_map_keys[key_id] = key;
2244 if (!m_map_keys.empty()) {
2248 m_map_crypted_keys[key_id] = make_pair(pubkey, crypted_key);
2255 return m_wallet_descriptor.descriptor !=
nullptr && desc.
descriptor !=
nullptr && m_wallet_descriptor.descriptor->ToString() == desc.
descriptor->ToString();
2263 throw std::runtime_error(std::string(__func__) +
": writing descriptor failed");
2269 return m_wallet_descriptor;
2275 std::vector<CScript> script_pub_keys;
2276 script_pub_keys.reserve(m_map_script_pub_keys.size());
2278 for (
auto const& script_pub_key: m_map_script_pub_keys) {
2279 script_pub_keys.push_back(script_pub_key.first);
2281 return script_pub_keys;
2295 return m_wallet_descriptor.descriptor->ToPrivateString(provider, out);
2298 return m_wallet_descriptor.descriptor->ToNormalizedString(provider, out, &m_wallet_descriptor.cache);
2309 if (m_wallet_descriptor.cache.GetCachedLastHardenedExtPubKeys().size() > 0) {
2317 std::vector<CScript> scripts_temp;
2319 if (!m_wallet_descriptor.descriptor->Expand(0, provider, scripts_temp, out_keys, &temp_cache)){
2320 throw std::runtime_error(
"Unable to expand descriptor");
2326 throw std::runtime_error(std::string(__func__) +
": writing cache items failed");
2335 throw std::runtime_error(std::string(__func__) +
": " +
error);
2338 m_map_pubkeys.clear();
2339 m_map_script_pub_keys.clear();
2341 m_wallet_descriptor = descriptor;
2348 error =
"can only update matching descriptor";
2352 if (descriptor.
range_start > m_wallet_descriptor.range_start ||
2353 descriptor.
range_end < m_wallet_descriptor.range_end) {
2355 error =
strprintf(
"new range must include current range = [%d,%d]",
2356 m_wallet_descriptor.range_start,
2357 m_wallet_descriptor.range_end - 1);