// Copyright (c) 2009-2010 Satoshi Nakamoto // Copyright (c) 2009-2016 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include "wallet.h" #include "boost/filesystem/operations.hpp" #include "libspark/keys.h" #include "serialize.h" #include "support/allocators/secure.h" #include "sync.h" #include "walletexcept.h" #include "amount.h" #include "base58.h" #include "checkpoints.h" #include "chain.h" #include "wallet/coincontrol.h" #include "wallet/sparkspendbatch.h" #include "consensus/consensus.h" #include "consensus/validation.h" #include "key.h" #include "keystore.h" #include "validation.h" #include "llmq/quorums_instantsend.h" #include "llmq/quorums_chainlocks.h" #include "net.h" #include "policy/policy.h" #include "primitives/block.h" #include "primitives/transaction.h" #include "script/script.h" #include "script/sign.h" #include "timedata.h" #include "txmempool.h" #include "util.h" #include "ui_interface.h" #include "utilmoneystr.h" #include "validation.h" #include "masternode-sync.h" #include "random.h" #include "init.h" #include "rpc/protocol.h" #include "wallet/bip39.h" #include "crypto/hmac_sha512.h" #include "crypto/aes.h" #include "evo/deterministicmns.h" #include #include #include #include #include #include #include #include "bip47/account.h" #include "bip47/paymentcode.h" #include "bip47/bip47utils.h" CWallet* pwalletMain = NULL; /** Transaction fee set by the user */ CFeeRate payTxFee(DEFAULT_TRANSACTION_FEE); unsigned int nTxConfirmTarget = DEFAULT_TX_CONFIRM_TARGET; bool bSpendZeroConfChange = DEFAULT_SPEND_ZEROCONF_CHANGE; bool fSendFreeTransactions = DEFAULT_SEND_FREE_TRANSACTIONS; bool fWalletRbf = DEFAULT_WALLET_RBF; const char * DEFAULT_WALLET_DAT = "wallet.dat"; boost::signals2::signal UnlockWallet; /** * Fees smaller than this (in satoshi) are considered zero fee (for transaction creation) * Override with -mintxfee */ CFeeRate CWallet::minTxFee = CFeeRate(DEFAULT_TRANSACTION_MINFEE); /** * If fee estimation does not have enough data to provide estimates, use this fee instead. * Has no effect if not using fee estimation * Override with -fallbackfee */ CFeeRate CWallet::fallbackFee = CFeeRate(DEFAULT_FALLBACK_FEE); const uint256 CMerkleTx::ABANDON_HASH(uint256S("0000000000000000000000000000000000000000000000000000000000000001")); /** @defgroup mapWallet * * @{ */ struct CompareValueOnly { bool operator()(const std::pair >& t1, const std::pair >& t2) const { return t1.first < t2.first; } }; struct CompareByAmount { bool operator()(const CompactTallyItem& t1, const CompactTallyItem& t2) const { return t1.nAmount > t2.nAmount; } }; static void EnsureSparkWalletAvailable() { if (!pwalletMain || !pwalletMain->sparkWallet) { throw std::logic_error("Spark feature requires HD wallet"); } } std::string COutput::ToString() const { return strprintf("COutput(%s, %d, %d) [%s]", tx->GetHash().ToString(), i, nDepth, FormatMoney(tx->tx->vout[i].nValue)); } class CAffectedKeysVisitor : public boost::static_visitor { private: const CKeyStore &keystore; std::vector &vKeys; public: CAffectedKeysVisitor(const CKeyStore &keystoreIn, std::vector &vKeysIn) : keystore(keystoreIn), vKeys(vKeysIn) {} void Process(const CScript &script) { txnouttype type; std::vector vDest; int nRequired; if (ExtractDestinations(script, type, vDest, nRequired)) { BOOST_FOREACH(const CTxDestination &dest, vDest) boost::apply_visitor(*this, dest); } } void operator()(const CKeyID &keyId) { if (keystore.HaveKey(keyId)) vKeys.push_back(keyId); } void operator()(const CExchangeKeyID &keyId) { if (keystore.HaveKey(keyId)) vKeys.push_back(keyId); } void operator()(const CScriptID &scriptId) { CScript script; if (keystore.GetCScript(scriptId, script)) Process(script); } void operator()(const CNoDestination &none) {} }; const CWalletTx *CWallet::GetWalletTx(const uint256 &hash) const { LOCK(cs_wallet); std::map::const_iterator it = mapWallet.find(hash); if (it == mapWallet.end()) return NULL; return &(it->second); } CPubKey CWallet::GetKeyFromKeypath(uint32_t nChange, uint32_t nChild, CKey& secret) { AssertLockHeld(cs_wallet); // mapKeyMetadata uint32_t nIndex = Params().GetConsensus().IsMain() ? BIP44_FIRO_INDEX : BIP44_TEST_INDEX; // Fail if not using HD wallet (no keypaths) if (hdChain.masterKeyID.IsNull()) throw std::runtime_error(std::string(__func__) + ": Non-HD wallet detected"); // use BIP44 keypath: m / purpose' / coin_type' / account' / change / address_index CKey key; //master key seed (256bit) CExtKey masterKey; //hd master key CExtKey purposeKey; //key at m/44' CExtKey coinTypeKey; //key at m/44'/<1/136>' (Testnet or Firo Coin Type respectively, according to SLIP-0044) CExtKey accountKey; //key at m/44'/<1/136>'/0' CExtKey externalChainChildKey; //key at m/44'/<1/136>'/0'/ (Standard: 0/1, Mints: 2) CExtKey childKey; //key at m/44'/<1/136>'/0'// if(hdChain.nVersion >= CHDChain::VERSION_WITH_BIP39){ MnemonicContainer mContainer = mnemonicContainer; DecryptMnemonicContainer(mContainer); SecureVector seed = mContainer.GetSeed(); masterKey.SetMaster(&seed[0], seed.size()); } else { // try to get the master key if (!GetKey(hdChain.masterKeyID, key)) throw std::runtime_error(std::string(__func__) + ": Master key not found"); masterKey.SetMaster(key.begin(), key.size()); } // derive m/44' // use hardened derivation (child keys >= 0x80000000 are hardened after bip32) masterKey.Derive(purposeKey, BIP44_INDEX | BIP32_HARDENED_KEY_LIMIT); // derive m/44'/136' purposeKey.Derive(coinTypeKey, nIndex | BIP32_HARDENED_KEY_LIMIT); // derive m/44'/136'/0' coinTypeKey.Derive(accountKey, BIP32_HARDENED_KEY_LIMIT); // derive m/44'/136'/0'/ accountKey.Derive(externalChainChildKey, nChange); // derive m/44'/136'/0'// externalChainChildKey.Derive(childKey, nChild); secret = childKey.key; CPubKey pubkey = secret.GetPubKey(); assert(secret.VerifyPubKey(pubkey)); return pubkey; } CPubKey CWallet::GenerateNewKey(uint32_t nChange, bool fWriteChain) { AssertLockHeld(cs_wallet); // mapKeyMetadata bool fCompressed = CanSupportFeature(FEATURE_COMPRPUBKEY); // default to compressed public keys if we want 0.6.0 wallets CKey secret; // Create new metadata int64_t nCreationTime = GetTime(); CKeyMetadata metadata(nCreationTime); metadata.nChange = Component(nChange, false); uint32_t nIndex = Params().GetConsensus().IsMain() ? BIP44_FIRO_INDEX : BIP44_TEST_INDEX; // use HD key derivation if HD was enabled during wallet creation // TODO: change code to foloow bitcoin structure more closely if (IsHDEnabled()) { // use BIP44 keypath: m / purpose' / coin_type' / account' / change / address_index CKey key; //master key seed (256bit) CExtKey masterKey; //hd master key CExtKey purposeKey; //key at m/44' CExtKey coinTypeKey; //key at m/44'/<1/136>' (Testnet or Firo Coin Type respectively, according to SLIP-0044) CExtKey accountKey; //key at m/44'/<1/136>'/0' CExtKey externalChainChildKey; //key at m/44'/<1/136>'/0'/ (Standard: 0/1, Mints: 2) CExtKey childKey; //key at m/44'/<1/136>'/0'// //For bip39 we use it's original way for generating keys to make it compatible with hardware and software wallets if(hdChain.nVersion >= CHDChain::VERSION_WITH_BIP39){ MnemonicContainer mContainer = mnemonicContainer; DecryptMnemonicContainer(mContainer); SecureVector seed = mContainer.GetSeed(); masterKey.SetMaster(&seed[0], seed.size()); } else { // try to get the master key if (!GetKey(hdChain.masterKeyID, key)) throw std::runtime_error(std::string(__func__) + ": Master key not found"); masterKey.SetMaster(key.begin(), key.size()); } // derive m/44' // use hardened derivation (child keys >= 0x80000000 are hardened after bip32) masterKey.Derive(purposeKey, BIP44_INDEX | BIP32_HARDENED_KEY_LIMIT); // derive m/44'/136' purposeKey.Derive(coinTypeKey, nIndex | BIP32_HARDENED_KEY_LIMIT); // derive m/44'/136'/0' coinTypeKey.Derive(accountKey, BIP32_HARDENED_KEY_LIMIT); // derive m/44'/136'/0'/ accountKey.Derive(externalChainChildKey, nChange); // derive child key at next index, skip keys already known to the wallet do { externalChainChildKey.Derive(childKey, hdChain.nExternalChainCounters[nChange]); metadata.hdKeypath = "m/44'/" + std::to_string(nIndex) + "'/0'/" + std::to_string(nChange) + "/" + std::to_string(hdChain.nExternalChainCounters[nChange]); metadata.hdMasterKeyID = hdChain.masterKeyID; metadata.nChild = Component(hdChain.nExternalChainCounters[nChange], false); // increment childkey index hdChain.nExternalChainCounters[nChange]++; } while (HaveKey(childKey.key.GetPubKey().GetID())); secret = childKey.key; // update the chain model in the database if(fWriteChain){ if (!CWalletDB(strWalletFile).WriteHDChain(hdChain)) throw std::runtime_error(std::string(__func__) + ": Writing HD chain model failed"); } /* bitcoin 0.14: if (IsHDEnabled()) { DeriveNewChildKey(metadata, secret); */ } else { secret.MakeNewKey(fCompressed); } // Compressed public keys were introduced in version 0.6.0 if (fCompressed) SetMinVersion(FEATURE_COMPRPUBKEY); CPubKey pubkey = secret.GetPubKey(); assert(secret.VerifyPubKey(pubkey)); mapKeyMetadata[pubkey.GetID()] = metadata; UpdateTimeFirstKey(nCreationTime); if (!AddKeyPubKey(secret, pubkey)) throw std::runtime_error(std::string(__func__) + ": AddKey failed"); return pubkey; } void CWallet::DeriveNewChildKey(CKeyMetadata& metadata, CKey& secret) { // for now we use a fixed keypath scheme of m/0'/0'/k CKey key; //master key seed (256bit) CExtKey masterKey; //hd master key CExtKey accountKey; //key at m/0' CExtKey externalChainChildKey; //key at m/0'/0' CExtKey childKey; //key at m/0'/0'/' // try to get the master key if (!GetKey(hdChain.masterKeyID, key)) throw std::runtime_error(std::string(__func__) + ": Master key not found"); masterKey.SetMaster(key.begin(), key.size()); // derive m/0' // use hardened derivation (child keys >= 0x80000000 are hardened after bip32) masterKey.Derive(accountKey, BIP32_HARDENED_KEY_LIMIT); // derive m/0'/0' accountKey.Derive(externalChainChildKey, BIP32_HARDENED_KEY_LIMIT); // derive child key at next index, skip keys already known to the wallet do { // always derive hardened keys // childIndex | BIP32_HARDENED_KEY_LIMIT = derive childIndex in hardened child-index-range // example: 1 | BIP32_HARDENED_KEY_LIMIT == 0x80000001 == 2147483649 externalChainChildKey.Derive(childKey, hdChain.nExternalChainCounter | BIP32_HARDENED_KEY_LIMIT); metadata.hdKeypath = "m/0'/0'/" + std::to_string(hdChain.nExternalChainCounter) + "'"; metadata.hdMasterKeyID = hdChain.masterKeyID; // increment childkey index hdChain.nExternalChainCounter++; } while (HaveKey(childKey.key.GetPubKey().GetID())); secret = childKey.key; // update the chain model in the database if (!CWalletDB(strWalletFile).WriteHDChain(hdChain)) throw std::runtime_error(std::string(__func__) + ": Writing HD chain model failed"); } bool CWallet::AddKeyPubKey(const CKey& secret, const CPubKey &pubkey) { AssertLockHeld(cs_wallet); // mapKeyMetadata if (!CCryptoKeyStore::AddKeyPubKey(secret, pubkey)) return false; // check if we need to remove from watch-only CScript script; script = GetScriptForDestination(pubkey.GetID()); if (HaveWatchOnly(script)) RemoveWatchOnly(script); script = GetScriptForRawPubKey(pubkey); if (HaveWatchOnly(script)) RemoveWatchOnly(script); if (!fFileBacked) return true; if (!IsCrypted()) { return CWalletDB(strWalletFile).WriteKey(pubkey, secret.GetPrivKey(), mapKeyMetadata[pubkey.GetID()]); } return true; } bool CWallet::AddCryptedKey(const CPubKey &vchPubKey, const std::vector &vchCryptedSecret) { if (!CCryptoKeyStore::AddCryptedKey(vchPubKey, vchCryptedSecret)) return false; if (!fFileBacked) return true; { LOCK(cs_wallet); if (pwalletdbEncryption) return pwalletdbEncryption->WriteCryptedKey(vchPubKey, vchCryptedSecret, mapKeyMetadata[vchPubKey.GetID()]); else return CWalletDB(strWalletFile).WriteCryptedKey(vchPubKey, vchCryptedSecret, mapKeyMetadata[vchPubKey.GetID()]); } return false; } bool CWallet::LoadKeyMetadata(const CTxDestination& keyID, const CKeyMetadata &meta) { AssertLockHeld(cs_wallet); // mapKeyMetadata UpdateTimeFirstKey(meta.nCreateTime); mapKeyMetadata[keyID] = meta; return true; } bool CWallet::LoadCryptedKey(const CPubKey &vchPubKey, const std::vector &vchCryptedSecret) { return CCryptoKeyStore::AddCryptedKey(vchPubKey, vchCryptedSecret); } void CWallet::UpdateTimeFirstKey(int64_t nCreateTime) { AssertLockHeld(cs_wallet); if (nCreateTime <= 1) { // Cannot determine birthday information, so set the wallet birthday to // the beginning of time. nTimeFirstKey = 1; } else if (!nTimeFirstKey || nCreateTime < nTimeFirstKey) { nTimeFirstKey = nCreateTime; } } bool CWallet::AddCScript(const CScript& redeemScript) { if (!CCryptoKeyStore::AddCScript(redeemScript)) return false; if (!fFileBacked) return true; return CWalletDB(strWalletFile).WriteCScript(Hash160(redeemScript), redeemScript); } bool CWallet::LoadCScript(const CScript& redeemScript) { /* A sanity check was added in pull #3843 to avoid adding redeemScripts * that never can be redeemed. However, old wallets may still contain * these. Do not add them to the wallet and warn. */ if (redeemScript.size() > MAX_SCRIPT_ELEMENT_SIZE) { std::string strAddr = CBitcoinAddress(CScriptID(redeemScript)).ToString(); LogPrintf("%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); return true; } return CCryptoKeyStore::AddCScript(redeemScript); } bool CWallet::AddWatchOnly(const CScript& dest) { if (!CCryptoKeyStore::AddWatchOnly(dest)) return false; const CKeyMetadata& meta = mapKeyMetadata[CScriptID(dest)]; UpdateTimeFirstKey(meta.nCreateTime); NotifyWatchonlyChanged(true); if (!fFileBacked) return true; return CWalletDB(strWalletFile).WriteWatchOnly(dest, meta); } bool CWallet::AddWatchOnly(const CScript& dest, int64_t nCreateTime) { mapKeyMetadata[CScriptID(dest)].nCreateTime = nCreateTime; return AddWatchOnly(dest); } bool CWallet::RemoveWatchOnly(const CScript &dest) { AssertLockHeld(cs_wallet); if (!CCryptoKeyStore::RemoveWatchOnly(dest)) return false; if (!HaveWatchOnly()) NotifyWatchonlyChanged(false); if (fFileBacked) if (!CWalletDB(strWalletFile).EraseWatchOnly(dest)) return false; return true; } bool CWallet::LoadWatchOnly(const CScript &dest) { return CCryptoKeyStore::AddWatchOnly(dest); } bool CWallet::Unlock(const SecureString &strWalletPassphrase, const bool& fFirstUnlock) { CCrypter crypter; CKeyingMaterial vMasterKey; { LOCK(cs_wallet); BOOST_FOREACH(const MasterKeyMap::value_type& pMasterKey, mapMasterKeys) { if(!crypter.SetKeyFromPassphrase(strWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod)) return false; if (!crypter.Decrypt(pMasterKey.second.vchCryptedKey, vMasterKey)) continue; // try another master key if (CCryptoKeyStore::Unlock(vMasterKey, fFirstUnlock)) { fUnlockRequested.store(false); return true; } } } return false; } void CWallet::RequestUnlock() { if (!IsLocked()) return; LogPrintf("Requesting wallet unlock\n"); UnlockWallet(this); printf("Please unlock the wallet with your passphrase to allow spark wallet be created\nYou need to do this only one time.\n"); fUnlockRequested.store(true); } bool CWallet::WaitForUnlock() { while (IsLocked()) { MilliSleep(100); if (ShutdownRequested()) return false; } return true; } bool CWallet::ChangeWalletPassphrase(const SecureString& strOldWalletPassphrase, const SecureString& strNewWalletPassphrase) { bool fWasLocked = IsLocked(); { LOCK(cs_wallet); Lock(); CCrypter crypter; CKeyingMaterial vMasterKey; BOOST_FOREACH(MasterKeyMap::value_type& pMasterKey, mapMasterKeys) { if(!crypter.SetKeyFromPassphrase(strOldWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod)) return false; if (!crypter.Decrypt(pMasterKey.second.vchCryptedKey, vMasterKey)) return false; if (CCryptoKeyStore::Unlock(vMasterKey)) { int64_t nStartTime = GetTimeMillis(); crypter.SetKeyFromPassphrase(strNewWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod); pMasterKey.second.nDeriveIterations = pMasterKey.second.nDeriveIterations * (100 / ((double)(GetTimeMillis() - nStartTime))); nStartTime = GetTimeMillis(); crypter.SetKeyFromPassphrase(strNewWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod); pMasterKey.second.nDeriveIterations = (pMasterKey.second.nDeriveIterations + pMasterKey.second.nDeriveIterations * 100 / ((double)(GetTimeMillis() - nStartTime))) / 2; if (pMasterKey.second.nDeriveIterations < 25000) pMasterKey.second.nDeriveIterations = 25000; LogPrintf("Wallet passphrase changed to an nDeriveIterations of %i\n", pMasterKey.second.nDeriveIterations); if (!crypter.SetKeyFromPassphrase(strNewWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod)) return false; if (!crypter.Encrypt(vMasterKey, pMasterKey.second.vchCryptedKey)) return false; CWalletDB(strWalletFile).WriteMasterKey(pMasterKey.first, pMasterKey.second); if (fWasLocked) Lock(); return true; } } } return false; } void CWallet::SetBestChain(const CBlockLocator& loc) { CWalletDB walletdb(strWalletFile); walletdb.WriteBestBlock(loc); } bool CWallet::SetMinVersion(enum WalletFeature nVersion, CWalletDB* pwalletdbIn, bool fExplicit) { LOCK(cs_wallet); // nWalletVersion if (nWalletVersion >= nVersion) return true; // when doing an explicit upgrade, if we pass the max version permitted, upgrade all the way if (fExplicit && nVersion > nWalletMaxVersion) nVersion = FEATURE_LATEST; nWalletVersion = nVersion; if (nVersion > nWalletMaxVersion) nWalletMaxVersion = nVersion; if (fFileBacked) { CWalletDB* pwalletdb = pwalletdbIn ? pwalletdbIn : new CWalletDB(strWalletFile); if (nWalletVersion > 40000) pwalletdb->WriteMinVersion(nWalletVersion); if (!pwalletdbIn) delete pwalletdb; } return true; } bool CWallet::SetMaxVersion(int nVersion) { LOCK(cs_wallet); // nWalletVersion, nWalletMaxVersion // cannot downgrade below current version if (nWalletVersion > nVersion) return false; nWalletMaxVersion = nVersion; return true; } std::set CWallet::GetConflicts(const uint256& txid) const { std::set result; AssertLockHeld(cs_wallet); std::map::const_iterator it = mapWallet.find(txid); if (it == mapWallet.end()) return result; const CWalletTx& wtx = it->second; std::pair range; BOOST_FOREACH(const CTxIn& txin, wtx.tx->vin) { if (mapTxSpends.count(txin.prevout) <= 1) continue; // No conflict if zero or one spends range = mapTxSpends.equal_range(txin.prevout); for (TxSpends::const_iterator _it = range.first; _it != range.second; ++_it) result.insert(_it->second); } return result; } bool CWallet::HasWalletSpend(const uint256& txid) const { AssertLockHeld(cs_wallet); auto iter = mapTxSpends.lower_bound(COutPoint(txid, 0)); return (iter != mapTxSpends.end() && iter->first.hash == txid); } void CWallet::Flush(bool shutdown) { bitdb.Flush(shutdown); } bool CWallet::Verify() { if (GetBoolArg("-disablewallet", DEFAULT_DISABLE_WALLET)) return true; LogPrintf("Using BerkeleyDB version %s\n", DbEnv::version(0, 0, 0)); std::string walletFile = GetArg("-wallet", DEFAULT_WALLET_DAT); LogPrintf("Using wallet %s\n", walletFile); uiInterface.InitMessage(_("Verifying wallet...")); // Wallet file must be a plain filename without a directory if (walletFile != boost::filesystem::path(walletFile).stem().string() + boost::filesystem::path(walletFile).extension().string()) return InitError(strprintf(_("Wallet %s resides outside data directory %s"), walletFile, GetDataDir().string())); if (!bitdb.Open(GetDataDir())) { // try moving the database env out of the way boost::filesystem::path pathDatabase = GetDataDir() / "database"; boost::filesystem::path pathDatabaseBak = GetDataDir() / strprintf("database.%d.bak", GetTime()); try { boost::filesystem::rename(pathDatabase, pathDatabaseBak); LogPrintf("Moved old %s to %s. Retrying.\n", pathDatabase.string(), pathDatabaseBak.string()); } catch (const boost::filesystem::filesystem_error&) { // failure is ok (well, not really, but it's not worse than what we started with) } // try again if (!bitdb.Open(GetDataDir())) { // if it still fails, it probably means we can't even create the database env return InitError(strprintf(_("Error initializing wallet database environment %s!"), GetDataDir())); } } if (GetBoolArg("-salvagewallet", false)) { // Recover readable keypairs: if (!CWalletDB::Recover(bitdb, walletFile, true)) return false; } if (boost::filesystem::exists(GetDataDir() / walletFile)) { CDBEnv::VerifyResult r = bitdb.Verify(walletFile, CWalletDB::Recover); if (r == CDBEnv::RECOVER_OK) { InitWarning(strprintf(_("Warning: Wallet file corrupt, data salvaged!" " Original %s saved as %s in %s; if" " your balance or transactions are incorrect you should" " restore from a backup."), walletFile, "wallet.{timestamp}.bak", GetDataDir())); } if (r == CDBEnv::RECOVER_FAIL) return InitError(strprintf(_("%s corrupt, salvage failed"), walletFile)); } return true; } void CWallet::SyncMetaData(std::pair range) { // We want all the wallet transactions in range to have the same metadata as // the oldest (smallest nOrderPos). // So: find smallest nOrderPos: int nMinOrderPos = std::numeric_limits::max(); const CWalletTx* copyFrom = NULL; for (TxSpends::iterator it = range.first; it != range.second; ++it) { const uint256& hash = it->second; int n = mapWallet[hash].nOrderPos; if (n < nMinOrderPos) { nMinOrderPos = n; copyFrom = &mapWallet[hash]; } } // Now copy data from copyFrom to rest: for (TxSpends::iterator it = range.first; it != range.second; ++it) { const uint256& hash = it->second; CWalletTx* copyTo = &mapWallet[hash]; if (copyFrom == copyTo) continue; if (!copyFrom->IsEquivalentTo(*copyTo)) continue; copyTo->mapValue = copyFrom->mapValue; copyTo->vOrderForm = copyFrom->vOrderForm; // fTimeReceivedIsTxTime not copied on purpose // nTimeReceived not copied on purpose copyTo->nTimeSmart = copyFrom->nTimeSmart; copyTo->fFromMe = copyFrom->fFromMe; copyTo->strFromAccount = copyFrom->strFromAccount; // nOrderPos not copied on purpose // cached members not copied on purpose } } /** * Outpoint is spent if any non-conflicted transaction * spends it: */ bool CWallet::IsSpent(const uint256 &hash, unsigned int n) const { auto tx = GetWalletTx(hash); // Try to handle mint output first. if (tx && tx->tx->vout.size() > n) { LOCK(cs_wallet); auto& script = tx->tx->vout[n].scriptPubKey; if (script.IsZerocoinMint()) { return true; } else if (pwalletMain && script.IsSigmaMint()) { return true; } else if (pwalletMain && (script.IsLelantusMint() || script.IsLelantusJMint())) { return true; } else if (pwalletMain && pwalletMain->sparkWallet && (script.IsSparkMint() || script.IsSparkSMint())) { std::vector serialContext = spark::getSerialContext(*tx->tx); spark::Coin coin(spark::Params::get_default()); try { spark::ParseSparkMintCoin(script, coin); } catch (std::invalid_argument &) { return false; } coin.setSerialContext(serialContext); if (!pwalletMain->sparkWallet) return false; CSparkMintMeta mintMeta; if (pwalletMain->sparkWallet->getMintMeta(coin, mintMeta)) { bool fIsSpent = mintMeta.isUsed; return fIsSpent; } } } // Normal output. const COutPoint outpoint(hash, n); std::pair range; range = mapTxSpends.equal_range(outpoint); for (TxSpends::const_iterator it = range.first; it != range.second; ++it) { const uint256& wtxid = it->second; std::map::const_iterator mit = mapWallet.find(wtxid); if (mit != mapWallet.end()) { int depth = mit->second.GetDepthInMainChain(); if (depth > 0 || (depth == 0 && !mit->second.isAbandoned())) return true; // Spent } } return false; } void CWallet::AddToSpends(const COutPoint& outpoint, const uint256& wtxid) { mapTxSpends.insert(std::make_pair(outpoint, wtxid)); setWalletUTXO.erase(outpoint); std::pair range; range = mapTxSpends.equal_range(outpoint); SyncMetaData(range); } void CWallet::AddToSpends(const uint256& wtxid) { assert(mapWallet.count(wtxid)); CWalletTx& thisTx = mapWallet[wtxid]; if (thisTx.IsCoinBase()) // Coinbases don't spend anything! return; BOOST_FOREACH(const CTxIn& txin, thisTx.tx->vin) { if (!thisTx.tx->HasNoRegularInputs()) { AddToSpends(txin.prevout, wtxid); } } } bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase) { if (IsCrypted()) return false; CKeyingMaterial vMasterKey; vMasterKey.resize(WALLET_CRYPTO_KEY_SIZE); GetStrongRandBytes(&vMasterKey[0], WALLET_CRYPTO_KEY_SIZE); CMasterKey kMasterKey; kMasterKey.vchSalt.resize(WALLET_CRYPTO_SALT_SIZE); GetStrongRandBytes(&kMasterKey.vchSalt[0], WALLET_CRYPTO_SALT_SIZE); CCrypter crypter; int64_t nStartTime = GetTimeMillis(); crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt, 25000, kMasterKey.nDerivationMethod); kMasterKey.nDeriveIterations = 2500000 / ((double)(GetTimeMillis() - nStartTime)); nStartTime = GetTimeMillis(); crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt, kMasterKey.nDeriveIterations, kMasterKey.nDerivationMethod); kMasterKey.nDeriveIterations = (kMasterKey.nDeriveIterations + kMasterKey.nDeriveIterations * 100 / ((double)(GetTimeMillis() - nStartTime))) / 2; if (kMasterKey.nDeriveIterations < 25000) kMasterKey.nDeriveIterations = 25000; LogPrintf("Encrypting Wallet with an nDeriveIterations of %i\n", kMasterKey.nDeriveIterations); if (!crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt, kMasterKey.nDeriveIterations, kMasterKey.nDerivationMethod)) return false; if (!crypter.Encrypt(vMasterKey, kMasterKey.vchCryptedKey)) return false; { LOCK(cs_wallet); mapMasterKeys[++nMasterKeyMaxID] = kMasterKey; if (fFileBacked) { assert(!pwalletdbEncryption); pwalletdbEncryption = new CWalletDB(strWalletFile); if (!pwalletdbEncryption->TxnBegin()) { delete pwalletdbEncryption; pwalletdbEncryption = NULL; return false; } pwalletdbEncryption->WriteMasterKey(nMasterKeyMaxID, kMasterKey); } if (!EncryptKeys(vMasterKey)) { if (fFileBacked) { pwalletdbEncryption->TxnAbort(); delete pwalletdbEncryption; } // We now probably have half of our keys encrypted in memory, and half not... // die and let the user reload the unencrypted wallet. assert(false); } // Encryption was introduced in version 0.4.0 SetMinVersion(FEATURE_WALLETCRYPT, pwalletdbEncryption, true); if (fFileBacked) { if (!pwalletdbEncryption->TxnCommit()) { delete pwalletdbEncryption; // We now have keys encrypted in memory, but not on disk... // die to avoid confusion and let the user reload the unencrypted wallet. assert(false); } delete pwalletdbEncryption; pwalletdbEncryption = NULL; } Lock(); Unlock(strWalletPassphrase, true); if(!mnemonicContainer.IsNull() && hdChain.nVersion >= CHDChain::VERSION_WITH_BIP39) { assert(EncryptMnemonicContainer(vMasterKey)); SetMinVersion(FEATURE_HD); assert(SetMnemonicContainer(mnemonicContainer, false)); TopUpKeyPool(); } Lock(); // Need to completely rewrite the wallet file; if we don't, bdb might keep // bits of the unencrypted private key in slack space in the database file. CDB::Rewrite(strWalletFile); } NotifyStatusChanged(this); return true; } DBErrors CWallet::ReorderTransactions() { LOCK(cs_wallet); CWalletDB walletdb(strWalletFile); // Old wallets didn't have any defined order for transactions // Probably a bad idea to change the output of this // First: get all CWalletTx and CAccountingEntry into a sorted-by-time multimap. typedef std::pair TxPair; typedef std::multimap TxItems; TxItems txByTime; for (std::map::iterator it = mapWallet.begin(); it != mapWallet.end(); ++it) { CWalletTx* wtx = &((*it).second); txByTime.insert(make_pair(wtx->nTimeReceived, TxPair(wtx, (CAccountingEntry*)0))); } std::list acentries; walletdb.ListAccountCreditDebit("", acentries); BOOST_FOREACH(CAccountingEntry& entry, acentries) { txByTime.insert(make_pair(entry.nTime, TxPair((CWalletTx*)0, &entry))); } nOrderPosNext = 0; std::vector nOrderPosOffsets; for (TxItems::iterator it = txByTime.begin(); it != txByTime.end(); ++it) { CWalletTx *const pwtx = (*it).second.first; CAccountingEntry *const pacentry = (*it).second.second; int64_t& nOrderPos = (pwtx != 0) ? pwtx->nOrderPos : pacentry->nOrderPos; if (nOrderPos == -1) { nOrderPos = nOrderPosNext++; nOrderPosOffsets.push_back(nOrderPos); if (pwtx) { if (!walletdb.WriteTx(*pwtx)) return DB_LOAD_FAIL; } else if (!walletdb.WriteAccountingEntry(pacentry->nEntryNo, *pacentry)) return DB_LOAD_FAIL; } else { int64_t nOrderPosOff = 0; BOOST_FOREACH(const int64_t& nOffsetStart, nOrderPosOffsets) { if (nOrderPos >= nOffsetStart) ++nOrderPosOff; } nOrderPos += nOrderPosOff; nOrderPosNext = std::max(nOrderPosNext, nOrderPos + 1); if (!nOrderPosOff) continue; // Since we're changing the order, write it back if (pwtx) { if (!walletdb.WriteTx(*pwtx)) return DB_LOAD_FAIL; } else if (!walletdb.WriteAccountingEntry(pacentry->nEntryNo, *pacentry)) return DB_LOAD_FAIL; } } walletdb.WriteOrderPosNext(nOrderPosNext); return DB_LOAD_OK; } int64_t CWallet::IncOrderPosNext(CWalletDB *pwalletdb) { AssertLockHeld(cs_wallet); // nOrderPosNext int64_t nRet = nOrderPosNext++; if (pwalletdb) { pwalletdb->WriteOrderPosNext(nOrderPosNext); } else { CWalletDB(strWalletFile).WriteOrderPosNext(nOrderPosNext); } return nRet; } bool CWallet::AccountMove(std::string strFrom, std::string strTo, CAmount nAmount, std::string strComment) { CWalletDB walletdb(strWalletFile); if (!walletdb.TxnBegin()) return false; int64_t nNow = GetAdjustedTime(); // Debit CAccountingEntry debit; debit.nOrderPos = IncOrderPosNext(&walletdb); debit.strAccount = strFrom; debit.nCreditDebit = -nAmount; debit.nTime = nNow; debit.strOtherAccount = strTo; debit.strComment = strComment; AddAccountingEntry(debit, &walletdb); // Credit CAccountingEntry credit; credit.nOrderPos = IncOrderPosNext(&walletdb); credit.strAccount = strTo; credit.nCreditDebit = nAmount; credit.nTime = nNow; credit.strOtherAccount = strFrom; credit.strComment = strComment; AddAccountingEntry(credit, &walletdb); if (!walletdb.TxnCommit()) return false; return true; } bool CWallet::GetAccountPubkey(CPubKey &pubKey, std::string strAccount, bool bForceNew) { CWalletDB walletdb(strWalletFile); CAccount account; walletdb.ReadAccount(strAccount, account); if (!bForceNew) { if (!account.vchPubKey.IsValid()) bForceNew = true; else { // Check if the current key has been used CScript scriptPubKey = GetScriptForDestination(account.vchPubKey.GetID()); for (std::map::iterator it = mapWallet.begin(); it != mapWallet.end() && account.vchPubKey.IsValid(); ++it) BOOST_FOREACH(const CTxOut& txout, (*it).second.tx->vout) if (txout.scriptPubKey == scriptPubKey) { bForceNew = true; break; } } } // Generate a new key if (bForceNew) { if (!GetKeyFromPool(account.vchPubKey)) return false; SetAddressBook(account.vchPubKey.GetID(), strAccount, "receive"); walletdb.WriteAccount(strAccount, account); } pubKey = account.vchPubKey; return true; } void CWallet::MarkDirty() { { LOCK(cs_wallet); BOOST_FOREACH(PAIRTYPE(const uint256, CWalletTx)& item, mapWallet) item.second.MarkDirty(); } } namespace { void HandleSecretAddresses(CWallet & wallet, bip47::CAccountReceiver const & receiver) { if (wallet.IsLocked()) { wallet.NotifyBip47KeysChanged(receiver.getAccountNum(), chainActive.Tip()); return; } bip47::utils::AddReceiverSecretAddresses(receiver, wallet); } } bool CWallet::MarkReplaced(const uint256& originalHash, const uint256& newHash) { LOCK(cs_wallet); auto mi = mapWallet.find(originalHash); // There is a bug if MarkReplaced is not called on an existing wallet transaction. assert(mi != mapWallet.end()); CWalletTx& wtx = (*mi).second; // Ensure for now that we're not overwriting data assert(wtx.mapValue.count("replaced_by_txid") == 0); wtx.mapValue["replaced_by_txid"] = newHash.ToString(); CWalletDB walletdb(strWalletFile, "r+"); bool success = true; if (!walletdb.WriteTx(wtx)) { LogPrintf("%s: Updating walletdb tx %s failed", __func__, wtx.GetHash().ToString()); success = false; } NotifyTransactionChanged(this, originalHash, CT_UPDATED); return success; } bool CWallet::AddToWallet(const CWalletTx& wtxIn, bool fFlushOnClose) { LOCK(cs_wallet); CWalletDB walletdb(strWalletFile, "r+", fFlushOnClose); uint256 hash = wtxIn.GetHash(); // Inserts only if not already there, returns tx inserted or tx found std::pair::iterator, bool> ret = mapWallet.insert(std::make_pair(hash, wtxIn)); CWalletTx& wtx = (*ret.first).second; wtx.BindWallet(this); bool fInsertedNew = ret.second; if (fInsertedNew) { wtx.nTimeReceived = GetAdjustedTime(); wtx.nOrderPos = IncOrderPosNext(&walletdb); wtxOrdered.insert(make_pair(wtx.nOrderPos, TxPair(&wtx, (CAccountingEntry*)0))); auto mnList = deterministicMNManager->GetListAtChainTip(); for(unsigned int i = 0; i < wtx.tx->vout.size(); ++i) { if (IsMine(wtx.tx->vout[i], *wtx.tx) && !IsSpent(hash, i)) { setWalletUTXO.insert(COutPoint(hash, i)); if (deterministicMNManager->IsProTxWithCollateral(wtx.tx, i) || mnList.HasMNByCollateral(COutPoint(hash, i))) { LockCoin(COutPoint(hash, i)); } } } wtx.nTimeSmart = wtx.nTimeReceived; if (!wtxIn.hashUnset()) { if (mapBlockIndex.count(wtxIn.hashBlock)) { int64_t latestNow = wtx.nTimeReceived; int64_t latestEntry = 0; { // Tolerate times up to the last timestamp in the wallet not more than 5 minutes into the future int64_t latestTolerated = latestNow + 300; const TxItems & txOrdered = wtxOrdered; for (TxItems::const_reverse_iterator it = txOrdered.rbegin(); it != txOrdered.rend(); ++it) { CWalletTx *const pwtx = (*it).second.first; if (pwtx == &wtx) continue; CAccountingEntry *const pacentry = (*it).second.second; int64_t nSmartTime; if (pwtx) { nSmartTime = pwtx->nTimeSmart; if (!nSmartTime) nSmartTime = pwtx->nTimeReceived; } else nSmartTime = pacentry->nTime; if (nSmartTime <= latestTolerated) { latestEntry = nSmartTime; if (nSmartTime > latestNow) latestNow = nSmartTime; break; } } } int64_t blocktime = mapBlockIndex[wtxIn.hashBlock]->GetBlockTime(); wtx.nTimeSmart = std::max(latestEntry, std::min(blocktime, latestNow)); } else LogPrintf("AddToWallet(): found %s in block %s not in index\n", wtxIn.GetHash().ToString(), wtxIn.hashBlock.ToString()); } AddToSpends(hash); } bool fUpdated = false; if (!fInsertedNew) { // Merge if (!wtxIn.hashUnset() && wtxIn.hashBlock != wtx.hashBlock) { wtx.hashBlock = wtxIn.hashBlock; fUpdated = true; } // If no longer abandoned, update if (wtxIn.hashBlock.IsNull() && wtx.isAbandoned()) { wtx.hashBlock = wtxIn.hashBlock; fUpdated = true; } if (wtxIn.nIndex != -1 && (wtxIn.nIndex != wtx.nIndex)) { wtx.nIndex = wtxIn.nIndex; fUpdated = true; } if (wtxIn.fFromMe && wtxIn.fFromMe != wtx.fFromMe) { wtx.fFromMe = wtxIn.fFromMe; fUpdated = true; } } //// debug print LogPrintf("AddToWallet %s %s%s\n", wtxIn.GetHash().ToString(), (fInsertedNew ? "new" : ""), (fUpdated ? "update" : "")); // Write to disk if (fInsertedNew || fUpdated) if (!walletdb.WriteTx(wtx)) return false; // Handle bip47 notification tx if (fInsertedNew) { HandleBip47Transaction(wtx); HandleSparkTransaction(wtx); } // Break debit/credit balance caches: wtx.MarkDirty(); // Notify UI of new or updated transaction NotifyTransactionChanged(this, hash, fInsertedNew ? CT_NEW : CT_UPDATED); // notify an external script when a wallet transaction comes in or is updated std::string strCmd = GetArg("-walletnotify", ""); if ( !strCmd.empty()) { boost::replace_all(strCmd, "%s", wtxIn.GetHash().GetHex()); boost::replace_all(strCmd, "%t", wtxIn.tx->IsSparkTransaction() ? "spark" : "regular"); boost::thread t(runCommand, strCmd); // thread runs free } return true; } bool CWallet::LoadToWallet(const CWalletTx& wtxIn) { uint256 hash = wtxIn.GetHash(); mapWallet[hash] = wtxIn; CWalletTx& wtx = mapWallet[hash]; wtx.BindWallet(this); wtxOrdered.insert(make_pair(wtx.nOrderPos, TxPair(&wtx, (CAccountingEntry*)0))); AddToSpends(hash); BOOST_FOREACH(const CTxIn& txin, wtx.tx->vin) { if (mapWallet.count(txin.prevout.hash)) { CWalletTx& prevtx = mapWallet[txin.prevout.hash]; if (prevtx.nIndex == -1 && !prevtx.hashUnset()) { MarkConflicted(prevtx.hashBlock, wtx.GetHash()); } } } return true; } /** * Add a transaction to the wallet, or update it. pIndex and posInBlock should * be set when the transaction was known to be included in a block. When * posInBlock = SYNC_TRANSACTION_NOT_IN_BLOCK (-1) , then wallet state is not * updated in AddToWallet, but notifications happen and cached balances are * marked dirty. * If fUpdate is true, existing transactions will be updated. * TODO: One exception to this is that the abandoned state is cleared under the * assumption that any further notification of a transaction that was considered * abandoned is an indication that it is not safe to be considered abandoned. * Abandoned state should probably be more carefuly tracked via different * posInBlock signals or by checking mempool presence when necessary. */ bool CWallet::AddToWalletIfInvolvingMe(const CTransaction& tx, const CBlockIndex* pIndex, int posInBlock, bool fUpdate) { { AssertLockHeld(cs_wallet); if (posInBlock != -1) { if(!(tx.IsCoinBase() || tx.HasNoRegularInputs())) { BOOST_FOREACH(const CTxIn& txin, tx.vin) { std::pair range = mapTxSpends.equal_range(txin.prevout); while (range.first != range.second) { if (range.first->second != tx.GetHash()) { LogPrintf("Transaction %s (in block %s) conflicts with wallet transaction %s (both spend %s:%i)\n", tx.GetHash().ToString(), pIndex->GetBlockHash().ToString(), range.first->second.ToString(), range.first->first.hash.ToString(), range.first->first.n); MarkConflicted(pIndex->GetBlockHash(), range.first->second); } range.first++; } } } } bool fExisted = mapWallet.count(tx.GetHash()) != 0; if (fExisted && !fUpdate) return false; if (fExisted || IsMine(tx) || IsFromMe(tx)) { /* Check if any keys in the wallet keypool that were supposed to be unused * have appeared in a new transaction. If so, remove those keys from the keypool. * This can happen when restoring an old wallet backup that does not contain * the mostly recently created transactions from newer versions of the wallet. */ // loop though all outputs for (const CTxOut& txout: tx.vout) { // extract addresses and check if they match with an unused keypool key std::vector vAffected; CAffectedKeysVisitor(*this, vAffected).Process(txout.scriptPubKey); for (const CKeyID &keyid : vAffected) { std::map::const_iterator mi = m_pool_key_to_index.find(keyid); if (mi != m_pool_key_to_index.end()) { LogPrintf("%s: Detected a used keypool key, mark all keypool key up to this key as used\n", __func__); MarkReserveKeysAsUsed(mi->second); if (!TopUpKeyPool()) { LogPrintf("%s: Topping up keypool failed (locked wallet)\n", __func__); } } } } CWalletTx wtx(this, MakeTransactionRef(tx)); // Get merkle branch if transaction was found in a block if (posInBlock != -1) wtx.SetMerkleBranch(pIndex, posInBlock); return AddToWallet(wtx, false); } } return false; } bool CWallet::AbandonTransaction(const uint256& hashTx) { LOCK2(cs_main, cs_wallet); CWalletDB walletdb(strWalletFile, "r+"); std::set todo; std::set done; // Can't mark abandoned if confirmed or in mempool assert(mapWallet.count(hashTx)); CWalletTx& origtx = mapWallet[hashTx]; if (origtx.GetDepthInMainChain() > 0 || origtx.InMempool() || origtx.InStempool() || origtx.IsLockedByLLMQInstantSend()) { return false; } todo.insert(hashTx); while (!todo.empty()) { uint256 now = *todo.begin(); todo.erase(now); done.insert(now); assert(mapWallet.count(now)); CWalletTx& wtx = mapWallet[now]; int currentconfirm = wtx.GetDepthInMainChain(); // If the orig tx was not in block, none of its spends can be assert(currentconfirm <= 0); // if (currentconfirm < 0) {Tx and spends are already conflicted, no need to abandon} if (currentconfirm == 0 && !wtx.isAbandoned()) { // If the orig tx was not in block/mempool, none of its spends can be in mempool assert(!wtx.InMempool()); assert(!wtx.InStempool()); wtx.nIndex = -1; wtx.setAbandoned(); wtx.MarkDirty(); walletdb.WriteTx(wtx); NotifyTransactionChanged(this, wtx.GetHash(), CT_UPDATED); // Iterate over all its outputs, and mark transactions in the wallet that spend them abandoned too TxSpends::const_iterator iter = mapTxSpends.lower_bound(COutPoint(hashTx, 0)); while (iter != mapTxSpends.end() && iter->first.hash == now) { if (!done.count(iter->second)) { todo.insert(iter->second); } iter++; } // If a transaction changes 'conflicted' state, that changes the balance // available of the outputs it spends. So force those to be recomputed BOOST_FOREACH(const CTxIn& txin, wtx.tx->vin) { if (mapWallet.count(txin.prevout.hash)) mapWallet[txin.prevout.hash].MarkDirty(); } } if (wtx.tx->IsSparkSpend()) { std::vector lTags; try { spark::SpendTransaction spend = spark::ParseSparkSpend(*wtx.tx); lTags = spend.getUsedLTags(); } catch (const std::exception &) { continue; } sparkWallet->AbandonSpends(lTags); } if (wtx.tx->IsSparkTransaction()) { std::vector coins = spark::GetSparkMintCoins(*wtx.tx); sparkWallet->AbandonSparkMints(coins); } } return true; } void CWallet::MarkConflicted(const uint256& hashBlock, const uint256& hashTx) { LOCK2(cs_main, cs_wallet); int conflictconfirms = 0; if (mapBlockIndex.count(hashBlock)) { CBlockIndex* pindex = mapBlockIndex[hashBlock]; if (chainActive.Contains(pindex)) { conflictconfirms = -(chainActive.Height() - pindex->nHeight + 1); } } // If number of conflict confirms cannot be determined, this means // that the block is still unknown or not yet part of the main chain, // for example when loading the wallet during a reindex. Do nothing in that // case. if (conflictconfirms >= 0) return; // Do not flush the wallet here for performance reasons CWalletDB walletdb(strWalletFile, "r+", false); std::set todo; std::set done; todo.insert(hashTx); while (!todo.empty()) { uint256 now = *todo.begin(); todo.erase(now); done.insert(now); assert(mapWallet.count(now)); CWalletTx& wtx = mapWallet[now]; int currentconfirm = wtx.GetDepthInMainChain(); if (conflictconfirms < currentconfirm) { // Block is 'more conflicted' than current confirm; update. // Mark transaction as conflicted with this block. wtx.nIndex = -1; wtx.hashBlock = hashBlock; wtx.MarkDirty(); walletdb.WriteTx(wtx); NotifyTransactionChanged(this, now, CT_UPDATED); // Iterate over all its outputs, and mark transactions in the wallet that spend them conflicted too TxSpends::const_iterator iter = mapTxSpends.lower_bound(COutPoint(now, 0)); while (iter != mapTxSpends.end() && iter->first.hash == now) { if (!done.count(iter->second)) { todo.insert(iter->second); } iter++; } // If a transaction changes 'conflicted' state, that changes the balance // available of the outputs it spends. So force those to be recomputed BOOST_FOREACH(const CTxIn& txin, wtx.tx->vin) { if (mapWallet.count(txin.prevout.hash)) mapWallet[txin.prevout.hash].MarkDirty(); } } } } void CWallet::SyncTransaction(const CTransaction& tx, const CBlockIndex *pindex, int posInBlock) { LOCK2(cs_main, cs_wallet); if (!AddToWalletIfInvolvingMe(tx, pindex, posInBlock, true)) return; // Not one of ours // If a transaction changes 'conflicted' state, that changes the balance // available of the outputs it spends. So force those to be // recomputed, also: BOOST_FOREACH(const CTxIn& txin, tx.vin) { if (mapWallet.count(txin.prevout.hash)) mapWallet[txin.prevout.hash].MarkDirty(); } } isminetype CWallet::IsMine(const CTxIn &txin, const CTransaction& tx) const { LOCK(cs_wallet); if (txin.IsZerocoinSpend() || txin.IsSigmaSpend() || txin.IsLelantusJoinSplit()) { return ISMINE_NO; } else if (txin.IsZerocoinRemint()) { return ISMINE_NO; } else if (tx.IsSparkSpend()) { std::vector lTags; try { spark::SpendTransaction spend = spark::ParseSparkSpend(tx); lTags = spend.getUsedLTags(); } catch (const std::exception &) { return ISMINE_NO; } if (!sparkWallet) return ISMINE_NO; return sparkWallet->isMine(lTags) ? ISMINE_SPENDABLE : ISMINE_NO; } else { std::map::const_iterator mi = mapWallet.find(txin.prevout.hash); if (mi != mapWallet.end()) { const CWalletTx& prev = (*mi).second; if (txin.prevout.n < prev.tx->vout.size()) return IsMine(prev.tx->vout[txin.prevout.n]); } } return ISMINE_NO; } // Note that this function doesn't distinguish between a 0-valued input, // and a not-"is mine" (according to the filter) input. CAmount CWallet::GetDebit(const CTxIn &txin, const CTransaction& tx, const isminefilter& filter) const { LOCK(cs_wallet); if (txin.IsZerocoinSpend() || txin.IsSigmaSpend() || txin.IsLelantusJoinSplit()) { goto end; } else if (txin.IsZerocoinRemint()) { return 0; } else if (tx.IsSparkSpend()) { if (!(filter & ISMINE_SPENDABLE)) { goto end; } std::vector lTags; try { spark::SpendTransaction spend = spark::ParseSparkSpend(tx); lTags = spend.getUsedLTags(); } catch (const std::exception &) { goto end; } if (!sparkWallet) goto end; CAmount amount = sparkWallet->getMySpendAmount(lTags); return amount; } else { std::map::const_iterator mi = mapWallet.find(txin.prevout.hash); if (mi != mapWallet.end()) { const CWalletTx& prev = (*mi).second; if (txin.prevout.n < prev.tx->vout.size()) if (IsMine(prev.tx->vout[txin.prevout.n]) & filter) return prev.tx->vout[txin.prevout.n].nValue; } } end: return 0; } isminetype CWallet::IsMine(const CTxOut &txout) const { LOCK(cs_wallet); if (txout.scriptPubKey.IsSparkMint() || txout.scriptPubKey.IsSparkSMint()) { // The Spark serial context is derived from the containing transaction; // without it in hand, locate the output in the wallet first. for (std::map::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it) { const CWalletTx *pcoin = &(*it).second; for (unsigned int i = 0; i < pcoin->tx->vout.size(); ++i) { if (txout == pcoin->tx->vout[i]) return IsMine(txout, *pcoin->tx); } } return ISMINE_NO; } else { return ::IsMine(*this, txout.scriptPubKey); } } isminetype CWallet::IsMine(const CTxOut &txout, const CTransaction& tx) const { LOCK(cs_wallet); // SMint values are authenticated by the containing Spark spend proof. if (txout.scriptPubKey.IsSparkSMint() && !tx.IsSparkSpend()) return ISMINE_NO; if (txout.scriptPubKey.IsSparkMint() || txout.scriptPubKey.IsSparkSMint()) { std::vector serialContext = spark::getSerialContext(tx); if (serialContext.empty()) return ISMINE_NO; spark::Coin coin(spark::Params::get_default()); try { spark::ParseSparkMintCoin(txout.scriptPubKey, coin); } catch (std::invalid_argument &) { return ISMINE_NO; } coin.setSerialContext(serialContext); if (!sparkWallet) return ISMINE_NO; return sparkWallet->isMine(coin) ? ISMINE_SPENDABLE : ISMINE_NO; } else { return ::IsMine(*this, txout.scriptPubKey); } } CAmount CWallet::GetCredit(const CTxOut& txout, const isminefilter& filter) const { if (!MoneyRange(txout.nValue)) throw std::runtime_error(std::string(__func__) + ": value out of range"); if (txout.scriptPubKey.IsSparkSMint()) { if (!(filter & ISMINE_SPENDABLE)) return 0; LOCK(cs_wallet); // The Spark serial context is derived from the containing transaction; // without it in hand, locate the output in the wallet first. for (std::map::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it) { const CWalletTx *pcoin = &(*it).second; for (unsigned int i = 0; i < pcoin->tx->vout.size(); ++i) { if (txout == pcoin->tx->vout[i]) return GetCredit(txout, *pcoin->tx, filter); } } return 0; } return ((IsMine(txout) & filter) ? txout.nValue : 0); } CAmount CWallet::GetCredit(const CTxOut& txout, const CTransaction& tx, const isminefilter& filter) const { if (!MoneyRange(txout.nValue)) throw std::runtime_error(std::string(__func__) + ": value out of range"); if (txout.scriptPubKey.IsSparkSMint()) { if (!tx.IsSparkSpend()) return 0; if (!(filter & ISMINE_SPENDABLE)) return 0; std::vector serialContext = spark::getSerialContext(tx); if (serialContext.empty()) return 0; spark::Coin coin(spark::Params::get_default()); try { spark::ParseSparkMintCoin(txout.scriptPubKey, coin); } catch (std::invalid_argument &) { return 0; } coin.setSerialContext(serialContext); if (!sparkWallet) return 0; return sparkWallet->getMyCoinV(coin); } return ((IsMine(txout, tx) & filter) ? txout.nValue : 0); } bool CWallet::IsChange(const uint256& tx, const CTxOut &txout) const { auto wtx = GetWalletTx(tx); if (!wtx) { throw std::invalid_argument("The specified transaction hash is not belong to the wallet"); } return wtx->IsChange(txout); } CAmount CWallet::GetChange(const uint256& tx, const CTxOut &txout) const { if (!MoneyRange(txout.nValue)) throw std::runtime_error(std::string(__func__) + ": value out of range"); if (txout.scriptPubKey.IsSparkSMint()) { if (IsChange(tx, txout)) { std::vector serial_context = spark::getSerialContext(*GetWalletTx(tx)->tx); spark::Coin coin(spark::Params::get_default()); try { spark::ParseSparkMintCoin(txout.scriptPubKey, coin); coin.setSerialContext(serial_context); } catch (const std::exception &) { return 0; } return sparkWallet->getMyCoinV(coin); } else return 0; } return (IsChange(tx, txout) ? txout.nValue : 0); } bool CWallet::IsMine(const CTransaction& tx) const { if (tx.IsSparkTransaction()) { if (!sparkWallet) return false; std::vector serialContext = spark::getSerialContext(tx); for (const auto& txout : tx.vout) { if (txout.scriptPubKey.IsSparkSMint() && !tx.IsSparkSpend()) continue; if (txout.scriptPubKey.IsSparkMint() || txout.scriptPubKey.IsSparkSMint()) { spark::Coin coin(spark::Params::get_default()); try { spark::ParseSparkMintCoin(txout.scriptPubKey, coin); } catch (std::invalid_argument &) { return false; } coin.setSerialContext(serialContext); if(sparkWallet->isMine(coin)) return true; } else if (IsMine(txout, tx)) return true; } return false; } BOOST_FOREACH(const CTxOut& txout, tx.vout) if (IsMine(txout, tx)) return true; return false; } bool CWallet::IsFromMe(const CTransaction& tx) const { return (GetDebit(tx, ISMINE_ALL) > 0); } CAmount CWallet::GetDebit(const CTransaction& tx, const isminefilter& filter) const { CAmount nDebit = 0; BOOST_FOREACH(const CTxIn& txin, tx.vin) { nDebit += GetDebit(txin, tx, filter); if (!MoneyRange(nDebit)) throw std::runtime_error(std::string(__func__) + ": value out of range"); } return nDebit; } bool CWallet::IsAllFromMe(const CTransaction& tx, const isminefilter& filter) const { LOCK(cs_wallet); BOOST_FOREACH(const CTxIn& txin, tx.vin) { auto mi = mapWallet.find(txin.prevout.hash); if (mi == mapWallet.end()) return false; // any unknown inputs can't be from us const CWalletTx& prev = (*mi).second; if (txin.prevout.n >= prev.tx->vout.size()) return false; // invalid input! if (!(IsMine(prev.tx->vout[txin.prevout.n]) & filter)) return false; } return true; } CAmount CWallet::GetCredit(const CTransaction& tx, const isminefilter& filter) const { CAmount nCredit = 0; BOOST_FOREACH(const CTxOut& txout, tx.vout) { nCredit += GetCredit(txout, tx, filter); if (!MoneyRange(nCredit)) throw std::runtime_error(std::string(__func__) + ": value out of range"); } return nCredit; } CAmount CWallet::GetChange(const CTransaction& tx) const { CAmount nChange = 0; BOOST_FOREACH(const CTxOut& txout, tx.vout) { nChange += GetChange(tx.GetHash(), txout); if (!MoneyRange(nChange)) throw std::runtime_error(std::string(__func__) + ": value out of range"); } return nChange; } CPubKey CWallet::GenerateNewHDMasterKey() { CKey key; key.MakeNewKey(true); int64_t nCreationTime = GetTime(); CKeyMetadata metadata(nCreationTime); // calculate the pubkey CPubKey pubkey = key.GetPubKey(); assert(key.VerifyPubKey(pubkey)); // set the hd keypath to "m" -> Master, refers the masterkeyid to itself metadata.hdKeypath = "m"; metadata.hdMasterKeyID = pubkey.GetID(); { LOCK(cs_wallet); // mem store the metadata mapKeyMetadata[pubkey.GetID()] = metadata; // write the key&metadata to the database if (!AddKeyPubKey(key, pubkey)) throw std::runtime_error(std::string(__func__) + ": AddKeyPubKey failed"); } return pubkey; } void CWallet::GenerateNewMnemonic() { CHDChain newHdChain; MnemonicContainer mnContainer; std::string strSeed = GetArg("-hdseed", "not hex"); bool isHDSeedSet = strSeed != "not hex"; if(isHDSeedSet && IsHex(strSeed)) { std::vector seed = ParseHex(strSeed); if (!mnContainer.SetSeed(SecureVector(seed.begin(), seed.end()))) throw std::runtime_error(std::string(__func__) + ": SetSeed failed"); newHdChain.masterKeyID = CKeyID(Hash160(seed.begin(), seed.end())); } else { LogPrintf("CWallet::GenerateNewMnemonic -- Generating new MnemonicContainer\n"); std::string mnemonic = GetArg("-mnemonic", ""); std::string mnemonicPassphrase = GetArg("-mnemonicpassphrase", ""); //remove trailing string identifiers boost::algorithm::trim_if(mnemonic, [](char c){return c=='\"' || c=='\'';}); boost::algorithm::trim_if(mnemonicPassphrase, [](char c){return c=='\"' || c=='\'';}); //Use 24 words by default; bool use12Words = GetBoolArg("-use12", false); mnContainer.Set12Words(use12Words); SecureString secureMnemonic(mnemonic.begin(), mnemonic.end()); SecureString securePassphrase(mnemonicPassphrase.begin(), mnemonicPassphrase.end()); if (!mnContainer.SetMnemonic(secureMnemonic, securePassphrase)) throw std::runtime_error(std::string(__func__) + ": SetMnemonic failed"); newHdChain.masterKeyID = CKeyID(Hash160(mnContainer.seed.begin(), mnContainer.seed.end())); } if (!SetHDChain(newHdChain, false)) throw std::runtime_error(std::string(__func__) + ": SetHDChain failed"); if (!SetMnemonicContainer(mnContainer, false)) throw std::runtime_error(std::string(__func__) + ": SetMnemonicContainer failed"); } bool CWallet::SetHDMasterKey(const CPubKey &pubkey, const int cHDChainVersion) { LOCK(cs_wallet); // ensure this wallet.dat can only be opened by clients supporting HD SetMinVersion(FEATURE_HD); // store the keyid (hash160) together with // the child index counter in the database // as a hdchain object CHDChain newHdChain; newHdChain.nVersion = cHDChainVersion; newHdChain.masterKeyID = pubkey.GetID(); SetHDChain(newHdChain, false); return true; } bool CWallet::SetHDChain(const CHDChain &chain, bool memonly, bool& upgradeChain, bool genNewKeyPool) { LOCK(cs_wallet); upgradeChain = (chain.nVersion==CHDChain::VERSION_BASIC); if (upgradeChain && !IsLocked()) { // Upgrade HDChain to latest version CHDChain newChain; newChain.masterKeyID = chain.masterKeyID; newChain.nVersion = CHDChain::VERSION_WITH_BIP44; // old versions cannot use mnemonic // whether to generate the keypool now (conditional as leads to DB deadlock if loading DB simultaneously) if (genNewKeyPool) NewKeyPool(); if (!memonly && !CWalletDB(strWalletFile).WriteHDChain(newChain)) throw std::runtime_error(std::string(__func__) + ": writing chain failed"); hdChain = newChain; } else { if (!memonly && !CWalletDB(strWalletFile).WriteHDChain(chain)) throw std::runtime_error(std::string(__func__) + ": writing chain failed"); hdChain = chain; } return true; } bool CWallet::IsHDEnabled() { return !hdChain.masterKeyID.IsNull(); } bool CWallet::SetMnemonicContainer(const MnemonicContainer& mnContainer, bool memonly) { if (!memonly && !CWalletDB(strWalletFile).WriteMnemonic(mnContainer)) throw std::runtime_error(std::string(__func__) + ": writing chain failed"); mnemonicContainer = mnContainer; return true; } bool CWallet::EncryptMnemonicContainer(const CKeyingMaterial& vMasterKeyIn) { if (!IsCrypted()) return false; if (mnemonicContainer.IsCrypted()) return true; uint256 id = uint256S(hdChain.masterKeyID.GetHex()); std::vector cryptedSeed; if (!EncryptMnemonicSecret(vMasterKeyIn, mnemonicContainer.GetSeed(), id, cryptedSeed)) return false; SecureVector secureCryptedSeed(cryptedSeed.begin(), cryptedSeed.end()); if (!mnemonicContainer.SetSeed(secureCryptedSeed)) return false; SecureString mnemonic; if (mnemonicContainer.GetMnemonic(mnemonic)) { std::vector cryptedMnemonic; SecureVector vectorMnemonic(mnemonic.begin(), mnemonic.end()); if ((!mnemonic.empty() && !EncryptMnemonicSecret(vMasterKeyIn, vectorMnemonic, id, cryptedMnemonic))) return false; SecureVector secureCryptedMnemonic(cryptedMnemonic.begin(), cryptedMnemonic.end()); if (!mnemonicContainer.SetMnemonic(secureCryptedMnemonic)) return false; } mnemonicContainer.SetCrypted(true); return true; } bool CWallet::DecryptMnemonicContainer(MnemonicContainer& mnContainer) { if (!IsCrypted()) return true; if (!mnemonicContainer.IsCrypted()) return false; uint256 id = uint256S(hdChain.masterKeyID.GetHex()); SecureVector seed; SecureVector cryptedSeed = mnemonicContainer.GetSeed(); std::vector vCryptedSeed(cryptedSeed.begin(), cryptedSeed.end()); if (!DecryptMnemonicSecret(vCryptedSeed, id, seed)) return false; mnContainer = mnemonicContainer; if (!mnContainer.SetSeed(seed)) return false; SecureString cryptedMnemonic; if (mnemonicContainer.GetMnemonic(cryptedMnemonic)) { SecureVector vectorMnemonic; std::vector CryptedMnemonic(cryptedMnemonic.begin(), cryptedMnemonic.end()); if (!CryptedMnemonic.empty() && !DecryptMnemonicSecret(CryptedMnemonic, id, vectorMnemonic)) return false; if (!mnContainer.SetMnemonic(vectorMnemonic)) return false; } mnContainer.SetCrypted(false); return true; } int64_t CWalletTx::GetTxTime() const { int64_t n = nTimeSmart; return n ? n : nTimeReceived; } int CWalletTx::GetRequestCount() const { // Returns -1 if it wasn't being tracked int nRequests = -1; { LOCK(pwallet->cs_wallet); if (IsCoinBase()) { // Generated block if (!hashUnset()) { std::map::const_iterator mi = pwallet->mapRequestCount.find(hashBlock); if (mi != pwallet->mapRequestCount.end()) nRequests = (*mi).second; } } else { // Did anyone request this transaction? std::map::const_iterator mi = pwallet->mapRequestCount.find(GetHash()); if (mi != pwallet->mapRequestCount.end()) { nRequests = (*mi).second; // How about the block it's in? if (nRequests == 0 && !hashUnset()) { std::map::const_iterator _mi = pwallet->mapRequestCount.find(hashBlock); if (_mi != pwallet->mapRequestCount.end()) nRequests = (*_mi).second; else nRequests = 1; // If it's in someone else's block it must have got out } } } } return nRequests; } void CWalletTx::GetAmounts(std::list& listReceived, std::list& listSent, CAmount& nFee, std::string& strSentAccount, const isminefilter& filter) const { nFee = 0; listReceived.clear(); listSent.clear(); strSentAccount = strFromAccount; // Compute fee: CAmount nDebit = GetDebit(filter); if (nDebit > 0) // debit>0 means we signed/sent this transaction { if (tx->IsSparkSpend()) { try { nFee = spark::GetSparkSpendFee(*tx); } catch (const std::exception &) { // do nothing } } else { CAmount nValueOut = tx->GetValueOut(); nFee = nDebit - nValueOut; } } // Sent/received. for (unsigned int i = 0; i < tx->vout.size(); ++i) { const CTxOut& txout = tx->vout[i]; isminetype fIsMine = pwallet->IsMine(txout, *tx); // Only need to handle txouts if AT LEAST one of these is true: // 1) they debit from us (sent) // 2) the output is to us (received) if (nDebit > 0) { // Don't report 'change' txouts if (IsChange(static_cast(i))) continue; } else if (!(fIsMine & filter)) continue; // In either case, we need to get the destination address CTxDestination address; if (txout.scriptPubKey.IsZerocoinMint() || txout.scriptPubKey.IsSigmaMint() || txout.scriptPubKey.IsLelantusMint() || txout.scriptPubKey.IsLelantusJMint() || txout.scriptPubKey.IsSparkMint() || txout.scriptPubKey.IsSparkSMint()) { address = CNoDestination(); } else if (!ExtractDestination(txout.scriptPubKey, address) && !txout.scriptPubKey.IsUnspendable()) { LogPrintf("CWalletTx::GetAmounts: Unknown transaction type found, txid %s\n", this->GetHash().ToString()); address = CNoDestination(); } CAmount nValue; if(txout.scriptPubKey.IsSparkSMint()) { LOCK(pwalletMain->cs_wallet); nValue = pwallet->GetCredit(txout, *tx, ISMINE_SPENDABLE); } else { nValue = txout.nValue; } COutputEntry output = {address, nValue, (int)i}; // If we are debited by the transaction, add the output as a "sent" entry if (nDebit > 0) listSent.push_back(output); // If we are receiving the output, add it as a "received" entry if (fIsMine & filter) listReceived.push_back(output); } } static std::time_t parseDate(const std::string& dateStr) { std::tm tm = {}; std::istringstream ss(dateStr); ss >> std::get_time(&tm, "%d-%m-%Y"); if (ss.fail()) { throw std::invalid_argument("Invalid date format: " + dateStr); } return std::mktime(&tm); } CBlockIndex* CWallet::GetBlockByDate(CBlockIndex* pindexStart, const std::string& dateStr) { std::time_t targetTimestamp = parseDate(dateStr); CBlockIndex* pindex = pindexStart; while (pindex) { if (pindex->GetBlockTime() > targetTimestamp) { if (pindex->nHeight >= 200) { return chainActive[pindex->nHeight - 200]; } else { return chainActive[0]; } } pindex = chainActive.Next(pindex); } return chainActive[chainActive.Tip()->nHeight]; } /** * Scan the block chain (starting in pindexStart) for transactions * from or to us. If fUpdate is true, found transactions that already * exist in the wallet will be updated. * * Returns pointer to the first block in the last contiguous range that was * successfully scanned. * */ CBlockIndex* CWallet::ScanForWalletTransactions(CBlockIndex *pindexStart, bool fUpdate, bool fRecoverMnemonic) { CBlockIndex* ret = nullptr; if (GetBoolArg("-newwallet", false)) { LogPrintf("Created new wallet, no need to scan\n"); return ret; } int64_t nNow = GetTime(); const CChainParams& chainParams = Params(); CBlockIndex* pindex = pindexStart; { LOCK2(cs_main, cs_wallet); // No need to read and scan block if block was created before our wallet birthday (as adjusted for block time variability). // If you are recovering wallet with mnemonics, start rescan from the block when mnemonics were implemented in Firo. // If the user provides a date, start scanning from the block that corresponds to that date. // If no date is provided, start scanning from the mnemonic start block. std::string wcdate = GetArg("-wcdate", ""); CBlockIndex* mnemonicStartBlock = chainActive[chainParams.GetConsensus().nMnemonicBlock]; if (mnemonicStartBlock == NULL) mnemonicStartBlock = chainActive.Tip(); if (!wcdate.empty()) { pindex = GetBlockByDate(mnemonicStartBlock, wcdate); if (pindex->nHeight < chainParams.GetConsensus().nMnemonicBlock) { pindex = mnemonicStartBlock; } } else { bool fRescan = GetBoolArg("-rescan", false); if (fRescan || fRecoverMnemonic) { if (mnemonicContainer.IsNull()) pindex = chainActive.Genesis(); else pindex = mnemonicStartBlock; } else while (pindex && nTimeFirstKey && (pindex->GetBlockTime() < (nTimeFirstKey - 7200))) pindex = chainActive.Next(pindex); } if (pindex) LogPrintf("Rescanning last %i blocks (from block %i)...\n", chainActive.Height(), pindex->nHeight); ShowProgress(_("Rescanning..."), 0); // show rescan progress in GUI as dialog or on splashscreen, if -rescan on startup double dProgressStart = GuessVerificationProgress(chainParams.TxData(), pindex); double dProgressTip = GuessVerificationProgress(chainParams.TxData(), chainActive.Tip()); while (pindex) { // A temporary fix for inability to Ctrl-C rescan when restoring a wallet (will be fixed in 0.15.) if (ShutdownRequested()) return nullptr; if (pindex->nHeight % 100 == 0 && dProgressTip - dProgressStart > 0.0) ShowProgress(_("Rescanning..."), std::max(1, std::min(99, (int)((GuessVerificationProgress(chainParams.TxData(), pindex) - dProgressStart) / (dProgressTip - dProgressStart) * 100)))); if (GetTime() >= nNow + 60) { nNow = GetTime(); LogPrintf("Still rescanning. At block %d. Progress=%f\n", pindex->nHeight, GuessVerificationProgress(chainParams.TxData(), pindex)); } CBlock block; if (ReadBlockFromDisk(block, pindex, Params().GetConsensus())) { for (size_t posInBlock = 0; posInBlock < block.vtx.size(); ++posInBlock) { AddToWalletIfInvolvingMe(*block.vtx[posInBlock], pindex, posInBlock, fUpdate); } if (!ret) { ret = pindex; } } else { ret = nullptr; } pindex = chainActive.Next(pindex); } ShowProgress(_("Rescanning..."), 100); // hide progress dialog in GUI } return ret; } void CWallet::ReacceptWalletTransactions() { // If transactions aren't being broadcasted, don't let them into local mempool either if (!fBroadcastTransactions) return; LOCK2(cs_main, cs_wallet); std::map mapSorted; // Sort pending wallet transactions based on their initial wallet insertion order BOOST_FOREACH(PAIRTYPE(const uint256, CWalletTx)& item, mapWallet) { const uint256& wtxid = item.first; CWalletTx& wtx = item.second; assert(wtx.GetHash() == wtxid); int nDepth = wtx.GetDepthInMainChain(); if (!wtx.IsCoinBase() && (nDepth == 0 && !wtx.isAbandoned() && !wtx.IsLockedByLLMQInstantSend())) { mapSorted.insert(std::make_pair(wtx.nOrderPos, &wtx)); } } // Try to add wallet transactions to memory pool BOOST_FOREACH(PAIRTYPE(const int64_t, CWalletTx*)& item, mapSorted) { CWalletTx& wtx = *(item.second); CValidationState state; // the app was closed and re-opened, do NOT check their // serial numbers, and DO NOT try to mark their serial numbers // a second time. We assume those operations were already done. wtx.AcceptToMemoryPool(maxTxFee, state); // If Dandelion enabled, relay transaction once again. if (GetBoolArg("-dandelion", true)) { wtx.RelayWalletTransaction(g_connman.get()); } } } bool CWalletTx::RelayWalletTransaction(CConnman* connman) { assert(pwallet->GetBroadcastTransactions()); if (!IsCoinBase() && !isAbandoned() && GetDepthInMainChain() == 0) { CValidationState state; /* GetDepthInMainChain already catches known conflicts. */ if (InMempool() || InStempool() || AcceptToMemoryPool(maxTxFee, state)) { // If Dandelion enabled, push inventory item to just one destination. if (GetBoolArg("-dandelion", true)) { int64_t nCurrTime = GetTimeMicros(); int64_t nEmbargo = 1000000 * DANDELION_EMBARGO_MINIMUM + PoissonNextSend(nCurrTime, DANDELION_EMBARGO_AVG_ADD); CNode::insertDandelionEmbargo(GetHash(), nEmbargo); CInv inv(MSG_DANDELION_TX, GetHash()); return CNode::localDandelionDestinationPushInventory(inv); } else { // LogPrintf("Relaying wtx %s\n", GetHash().ToString()); if (connman) { connman->RelayTransaction(*this); return true; } } } } return false; } std::set CWalletTx::GetConflicts() const { std::set result; if (pwallet != NULL) { uint256 myHash = GetHash(); result = pwallet->GetConflicts(myHash); result.erase(myHash); } return result; } CAmount CWalletTx::GetDebit(const isminefilter& filter) const { if (tx->vin.empty()) return 0; CAmount debit = 0; if(filter & ISMINE_SPENDABLE) { if (fDebitCached) debit += nDebitCached; else { nDebitCached = pwallet->GetDebit(*this, ISMINE_SPENDABLE); fDebitCached = true; debit += nDebitCached; } } if(filter & ISMINE_WATCH_ONLY) { if(fWatchDebitCached) debit += nWatchDebitCached; else { nWatchDebitCached = pwallet->GetDebit(*this, ISMINE_WATCH_ONLY); fWatchDebitCached = true; debit += nWatchDebitCached; } } return debit; } CAmount CWalletTx::GetCredit(const isminefilter& filter) const { // Must wait until coinbase is safely deep enough in the chain before valuing it if (IsCoinBase() && GetBlocksToMaturity() > 0) return 0; CAmount credit = 0; if (filter & ISMINE_SPENDABLE) { // GetBalance can assume transactions in mapWallet won't change if (fCreditCached) credit += nCreditCached; else { nCreditCached = pwallet->GetCredit(*this, ISMINE_SPENDABLE); fCreditCached = true; credit += nCreditCached; } } if (filter & ISMINE_WATCH_ONLY) { if (fWatchCreditCached) credit += nWatchCreditCached; else { nWatchCreditCached = pwallet->GetCredit(*this, ISMINE_WATCH_ONLY); fWatchCreditCached = true; credit += nWatchCreditCached; } } return credit; } CAmount CWalletTx::GetImmatureCredit(bool fUseCache) const { if (IsCoinBase() && GetBlocksToMaturity() > 0 && IsInMainChain()) { if (fUseCache && fImmatureCreditCached) return nImmatureCreditCached; nImmatureCreditCached = pwallet->GetCredit(*this, ISMINE_SPENDABLE); fImmatureCreditCached = true; return nImmatureCreditCached; } return 0; } CAmount CWalletTx::GetAvailableCredit(bool fUseCache, bool fExcludeLocked) const { if (pwallet == 0) return 0; // Must wait until coinbase is safely deep enough in the chain before valuing it if (IsCoinBase() && GetBlocksToMaturity() > 0) return 0; // We cannot use cache if vout contains mints due to it will not update when it spend if (fUseCache && fAvailableCreditCached && !tx->IsZerocoinMint() && !tx->IsSigmaMint() && !tx->IsLelantusMint() && !tx->IsSparkMint() && !tx->IsSparkSpend() && !fExcludeLocked) return nAvailableCreditCached; CAmount nCredit = 0; uint256 hashTx = GetHash(); for (unsigned int i = 0; i < tx->vout.size(); i++) { const CTxOut &txout = tx->vout[i]; bool isPrivate = txout.scriptPubKey.IsZerocoinMint() || txout.scriptPubKey.IsSigmaMint() || txout.scriptPubKey.IsLelantusMint() || txout.scriptPubKey.IsLelantusJMint() || txout.scriptPubKey.IsSparkMint() || txout.scriptPubKey.IsSparkSMint(); if (isPrivate) continue; if (fExcludeLocked && pwallet->IsLockedCoin(hashTx, i)) continue; if (!pwallet->IsSpent(hashTx, i)) { nCredit += pwallet->GetCredit(txout, *tx, ISMINE_SPENDABLE); if (!MoneyRange(nCredit)) throw std::runtime_error("CWalletTx::GetAvailableCredit() : value out of range"); } } nAvailableCreditCached = nCredit; fAvailableCreditCached = true; if (fExcludeLocked) fAvailableCreditCached = false; return nCredit; } CAmount CWalletTx::GetImmatureWatchOnlyCredit(const bool& fUseCache) const { if (IsCoinBase() && GetBlocksToMaturity() > 0 && IsInMainChain()) { if (fUseCache && fImmatureWatchCreditCached) return nImmatureWatchCreditCached; nImmatureWatchCreditCached = pwallet->GetCredit(*this, ISMINE_WATCH_ONLY); fImmatureWatchCreditCached = true; return nImmatureWatchCreditCached; } return 0; } CAmount CWalletTx::GetAvailableWatchOnlyCredit(const bool& fUseCache) const { if (pwallet == 0) return 0; // Must wait until coinbase is safely deep enough in the chain before valuing it if (IsCoinBase() && GetBlocksToMaturity() > 0) return 0; if (fUseCache && fAvailableWatchCreditCached) return nAvailableWatchCreditCached; CAmount nCredit = 0; for (unsigned int i = 0; i < tx->vout.size(); i++) { if (!pwallet->IsSpent(GetHash(), i)) { const CTxOut &txout = tx->vout[i]; nCredit += pwallet->GetCredit(txout, *tx, ISMINE_WATCH_ONLY); if (!MoneyRange(nCredit)) throw std::runtime_error("CWalletTx::GetAvailableCredit() : value out of range"); } } nAvailableWatchCreditCached = nCredit; fAvailableWatchCreditCached = true; return nCredit; } CAmount CWalletTx::GetChange() const { if (fChangeCached) return nChangeCached; nChangeCached = pwallet->GetChange(*this); fChangeCached = true; return nChangeCached; } bool CWalletTx::InMempool() const { LOCK(mempool.cs); if (mempool.exists(GetHash())) { return true; } return false; } bool CWalletTx::InStempool() const { if (txpools.getStemTxPool().exists(GetHash())) { return true; } return false; } bool CWalletTx::IsTrusted() const { // Quick answer in most cases. // Zerocoin spend is always false due to it use nSequence incorrectly. if (!tx->IsZerocoinSpend() && !CheckFinalTx(*this)) return false; int nDepth = GetDepthInMainChain(); if (nDepth >= 1) return true; if (nDepth < 0) return false; if (IsLockedByLLMQInstantSend()) return true; if (!bSpendZeroConfChange || !IsFromMe(ISMINE_ALL)) // using wtx's cached debit return false; // Don't trust unconfirmed transactions from us unless they are in the mempool. if (!InMempool() && !InStempool()) return false; // Trusted if all inputs are from us and are in the mempool: BOOST_FOREACH(const CTxIn& txin, tx->vin) { if (tx->HasNoRegularInputs()) { if (!(pwallet->IsMine(txin, *tx) & ISMINE_SPENDABLE)) { return false; } } else { // Transactions not sent by us: not trusted const CWalletTx *parent = pwallet->GetWalletTx(txin.prevout.hash); if (parent == NULL) return false; const CTxOut &parentOut = parent->tx->vout[txin.prevout.n]; if (pwallet->IsMine(parentOut) != ISMINE_SPENDABLE) return false; } } return true; } bool CWalletTx::IsChange(uint32_t out) const { if (out >= tx->vout.size()) { throw std::invalid_argument("The specified output index is not valid"); } if (changes.count(out)) { return true; } if (tx->IsSparkSpend()) { std::vector serial_context = spark::getSerialContext(*tx); spark::Coin coin(spark::Params::get_default()); try { spark::ParseSparkMintCoin(tx->vout[out].scriptPubKey, coin); coin.setSerialContext(serial_context); } catch (const std::exception &) { return false; } return pwallet->sparkWallet->getMyCoinIsChange(coin); } // Legacy transaction handling. // Zerocoin spend have one special output mode to spend to yourself with change address, // we don't want to identify that output as change. if (!tx->IsZerocoinSpend() && ::IsMine(*pwallet, tx->vout[out].scriptPubKey)) { CTxDestination address; if (!ExtractDestination(tx->vout[out].scriptPubKey, address)) { return true; } LOCK(pwallet->cs_wallet); if (!pwallet->mapAddressBook.count(address)) { return true; } } return false; } bool CWalletTx::IsChange(const CTxOut& out) const { auto it = std::find(tx->vout.begin(), tx->vout.end(), out); if (it == tx->vout.end()) { throw std::invalid_argument("The specified output does not belong to the transaction"); } return IsChange(it - tx->vout.begin()); } bool CWalletTx::IsEquivalentTo(const CWalletTx& _tx) const { CMutableTransaction tx1 = *this->tx; CMutableTransaction tx2 = *_tx.tx; for (unsigned int i = 0; i < tx1.vin.size(); i++) tx1.vin[i].scriptSig = CScript(); for (unsigned int i = 0; i < tx2.vin.size(); i++) tx2.vin[i].scriptSig = CScript(); return CTransaction(tx1) == CTransaction(tx2); } std::vector CWallet::ResendWalletTransactionsBefore(int64_t nTime, CConnman* connman) { std::vector result; LOCK(cs_wallet); // Sort them in chronological order std::multimap mapSorted; BOOST_FOREACH(PAIRTYPE(const uint256, CWalletTx)& item, mapWallet) { CWalletTx& wtx = item.second; // Don't rebroadcast if newer than nTime: if (wtx.nTimeReceived > nTime) continue; mapSorted.insert(std::make_pair(wtx.nTimeReceived, &wtx)); } BOOST_FOREACH(PAIRTYPE(const unsigned int, CWalletTx*)& item, mapSorted) { CWalletTx& wtx = *item.second; if (wtx.RelayWalletTransaction(connman)) result.push_back(wtx.GetHash()); } return result; } void CWallet::ResendWalletTransactions(int64_t nBestBlockTime, CConnman* connman) { // Do this infrequently and randomly to avoid giving away // that these are our transactions. if (GetTime() < nNextResend || !fBroadcastTransactions) return; bool fFirst = (nNextResend == 0); nNextResend = GetTime() + GetRand(30 * 60); if (fFirst) return; // Only do it if there's been a new block since last time if (nBestBlockTime < nLastResend) return; nLastResend = GetTime(); // Rebroadcast unconfirmed txes older than 5 minutes before the last // block was found: std::vector relayed = ResendWalletTransactionsBefore(nBestBlockTime-5*60, connman); if (!relayed.empty()) LogPrintf("%s: rebroadcast %u unconfirmed transactions\n", __func__, relayed.size()); } /** @} */ // end of mapWallet /** @defgroup Actions * * @{ */ CAmount CWallet::GetBalance(bool fExcludeLocked) const { CAmount nTotal = 0; { LOCK2(cs_main, cs_wallet); for (std::map::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it) { const CWalletTx* pcoin = &(*it).second; if (pcoin->IsTrusted()) nTotal += pcoin->GetAvailableCredit(true, fExcludeLocked); } } return nTotal; } std::list CWallet::GetAvailableSparkCoins(const CCoinControl *coinControl) const { EnsureSparkWalletAvailable(); LOCK2(cs_main, cs_wallet); return sparkWallet->GetAvailableSparkCoins(coinControl); } // Calculate total balance in a different way from GetBalance. The biggest // difference is that GetBalance sums up all unspent TxOuts paying to the // wallet, while this sums up both spent and unspent TxOuts paying to the // wallet, and then subtracts the values of TxIns spending from the wallet. This // also has fewer restrictions on which unconfirmed transactions are considered // trusted. CAmount CWallet::GetLegacyBalance(const isminefilter& filter, int minDepth, const std::string* account, bool fAddLocked) const { LOCK2(cs_main, cs_wallet); CAmount balance = 0; for (const auto& entry : mapWallet) { const CWalletTx& wtx = entry.second; const int depth = wtx.GetDepthInMainChain(); if (depth < 0 || !CheckFinalTx(*wtx.tx) || wtx.GetBlocksToMaturity() > 0) { continue; } // Loop through tx outputs and add incoming payments. For outgoing txs, // treat change outputs specially, as part of the amount debited. CAmount debit = wtx.GetDebit(filter); const bool outgoing = debit > 0; for (const CTxOut& out : wtx.tx->vout) { if (outgoing && IsChange(wtx.tx->GetHash(), out)) { debit -= out.nValue; } else if (IsMine(out, *wtx.tx) & filter && depth >= minDepth && (!account || *account == GetAccountName(out.scriptPubKey))) { balance += out.nValue; } } // For outgoing txs, subtract amount debited. if (outgoing && (!account || *account == wtx.strFromAccount)) { balance -= debit; } } if (account) { balance += CWalletDB(strWalletFile).GetAccountCreditDebit(*account); } return balance; } std::vector GetAESKey(const secp_primitives::GroupElement& pubcoin) { uint32_t keyPath = primitives::GetPubCoinValueHash(pubcoin).GetFirstUint32(); CKey secret; { pwalletMain->GetKeyFromKeypath(BIP44_MINT_VALUE_INDEX, keyPath, secret); } std::vector result(CHMAC_SHA512::OUTPUT_SIZE); CHMAC_SHA512(secret.begin(), secret.size()).Finalize(&result[0]); return result; } std::vector CWallet::EncryptMintAmount(uint64_t amount, const secp_primitives::GroupElement& pubcoin) const { LOCK(cs_wallet); std::vector key = GetAESKey(pubcoin); AES256Encrypt enc(key.data()); std::vector ciphertext(16); std::vector plaintext(16); memcpy(plaintext.data(), &amount, 8); enc.Encrypt(ciphertext.data(), plaintext.data()); return ciphertext; } bool CWallet::DecryptMintAmount(const std::vector& encryptedValue, const secp_primitives::GroupElement& pubcoin, uint64_t& amount) const { if (IsLocked() || hdChain.masterKeyID.IsNull()) { amount = 0; return true; } LOCK(cs_wallet); std::vector key = GetAESKey(pubcoin); AES256Decrypt dec(key.data()); std::vector plaintext(16); dec.Decrypt(plaintext.data(), encryptedValue.data()); memcpy(&amount, plaintext.data(), 8); return true; } template static CAmount CalculateCoinsBalance(Iterator begin, Iterator end) { CAmount balance(0); for (auto start = begin; start != end; start++) { balance += start->get_denomination_value(); } return balance; } bool CWallet::TryGetBalances(CAmount& balance, CAmount& unconfirmedBalance, CAmount& newImmatureBalance, CAmount& mintableBalance) const { TRY_LOCK(cs_main, locked_main); if (!locked_main) { return false; } TRY_LOCK(cs_wallet, locked_wallet); if (!locked_wallet) { return false; } balance = GetBalance(); unconfirmedBalance = GetUnconfirmedBalance(); newImmatureBalance = GetImmatureBalance(); mintableBalance = GetBalance(true); return true; } CAmount CWallet::GetUnconfirmedBalance() const { CAmount nTotal = 0; { LOCK2(cs_main, cs_wallet); for (std::map::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it) { const CWalletTx *pcoin = &(*it).second; if (!pcoin->IsTrusted() && pcoin->GetDepthInMainChain() == 0 && (pcoin->InMempool() || pcoin->InStempool()) && !pcoin->IsLockedByLLMQInstantSend()) nTotal += pcoin->GetAvailableCredit(); } } return nTotal; } CAmount CWallet::GetImmatureBalance() const { CAmount nTotal = 0; { LOCK2(cs_main, cs_wallet); for (std::map::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it) { const CWalletTx *pcoin = &(*it).second; nTotal += pcoin->GetImmatureCredit(); } } return nTotal; } CAmount CWallet::GetWatchOnlyBalance() const { CAmount nTotal = 0; { LOCK2(cs_main, cs_wallet); for (std::map::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it) { const CWalletTx *pcoin = &(*it).second; if (pcoin->IsTrusted()) nTotal += pcoin->GetAvailableWatchOnlyCredit(); } } return nTotal; } CAmount CWallet::GetUnconfirmedWatchOnlyBalance() const { CAmount nTotal = 0; { LOCK2(cs_main, cs_wallet); for (std::map::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it) { const CWalletTx *pcoin = &(*it).second; if (!pcoin->IsTrusted() && pcoin->GetDepthInMainChain() == 0 && (pcoin->InMempool() || pcoin->InStempool()) && !pcoin->IsLockedByLLMQInstantSend()) nTotal += pcoin->GetAvailableWatchOnlyCredit(); } } return nTotal; } CAmount CWallet::GetImmatureWatchOnlyBalance() const { CAmount nTotal = 0; { LOCK2(cs_main, cs_wallet); for (std::map::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it) { const CWalletTx* pcoin = &(*it).second; nTotal += pcoin->GetImmatureWatchOnlyCredit(); } } return nTotal; } void CWallet::AvailableCoins(std::vector &vCoins, bool fOnlyConfirmed, const CCoinControl *coinControl, bool fIncludeZeroValue, bool fUseInstantSend) const { static const int ZNODE_COIN_REQUIRED = 1000; vCoins.clear(); CoinType nCoinType = coinControl ? coinControl->nCoinType : CoinType::ALL_COINS; { LOCK2(cs_main, cs_wallet); int nInstantSendConfirmationsRequired = Params().GetConsensus().nInstantSendConfirmationsRequired; for (std::map::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it) { const uint256& wtxid = it->first; const CWalletTx* pcoin = &(*it).second; if (!CheckFinalTx(*pcoin)) continue; if (fOnlyConfirmed && !pcoin->IsTrusted()) continue; if (pcoin->IsCoinBase() && pcoin->GetBlocksToMaturity() > 0) continue; int nDepth = pcoin->GetDepthInMainChain(false); // do not use IX for inputs that have less then nInstantSendConfirmationsRequired blockchain confirmations if (fUseInstantSend && nDepth < nInstantSendConfirmationsRequired) continue; // We should not consider coins from transactions that are replacing // other transactions. // // Example: There is a transaction A which is replaced by bumpfee // transaction B. In this case, we want to prevent creation of // a transaction B' which spends an output of B. // // Reason: If transaction A were initially confirmed, transactions B // and B' would no longer be valid, so the user would have to create // a new transaction C to replace B'. However, in the case of a // one-block reorg, transactions B' and C might BOTH be accepted, // when the user only wanted one of them. Specifically, there could // be a 1-block reorg away from the chain where transactions A and C // were accepted to another chain where B, B', and C were all // accepted. if (nDepth == 0 && fOnlyConfirmed && pcoin->mapValue.count("replaces_txid")) { continue; } // Similarly, we should not consider coins from transactions that // have been replaced. In the example above, we would want to prevent // creation of a transaction A' spending an output of A, because if // transaction B were initially confirmed, conflicting with A and // A', we wouldn't want to the user to create a transaction D // intending to replace A', but potentially resulting in a scenario // where A, A', and D could all be accepted (instead of just B and // D, or just A and A' like the user would want). if (nDepth == 0 && fOnlyConfirmed && pcoin->mapValue.count("replaced_by_txid")) { continue; } for (unsigned int i = 0; i < pcoin->tx->vout.size(); i++) { bool found = false; if(nCoinType == CoinType::ALL_COINS){ // We are now taking ALL_COINS to mean everything sans mints found = !(pcoin->tx->vout[i].scriptPubKey.IsZerocoinMint() || pcoin->tx->vout[i].scriptPubKey.IsSigmaMint() || pcoin->tx->vout[i].scriptPubKey.IsLelantusMint() || pcoin->tx->vout[i].scriptPubKey.IsLelantusJMint() || pcoin->tx->vout[i].scriptPubKey.IsSparkMint() || pcoin->tx->vout[i].scriptPubKey.IsSparkSMint()) || pcoin->tx->vout[i].scriptPubKey.IsZerocoinRemint(); } else if(nCoinType == CoinType::ONLY_MINTS){ // Do not consider anything other than mints found = (pcoin->tx->vout[i].scriptPubKey.IsZerocoinMint() || pcoin->tx->vout[i].scriptPubKey.IsSigmaMint() || pcoin->tx->vout[i].scriptPubKey.IsZerocoinRemint() || pcoin->tx->vout[i].scriptPubKey.IsLelantusMint() || pcoin->tx->vout[i].scriptPubKey.IsLelantusJMint() || pcoin->tx->vout[i].scriptPubKey.IsSparkMint() || pcoin->tx->vout[i].scriptPubKey.IsSparkSMint()); } else if (nCoinType == CoinType::ONLY_NOT1000IFMN) { found = !(fMasternodeMode && pcoin->tx->vout[i].nValue == ZNODE_COIN_REQUIRED * COIN); } else if (nCoinType == CoinType::ONLY_NONDENOMINATED_NOT1000IFMN) { if (fMasternodeMode) found = pcoin->tx->vout[i].nValue != ZNODE_COIN_REQUIRED * COIN; // do not use Hot MN funds } else if (nCoinType == CoinType::ONLY_1000) { found = pcoin->tx->vout[i].nValue == ZNODE_COIN_REQUIRED * COIN; } else { found = true; } if (!found) continue; isminetype mine = IsMine(pcoin->tx->vout[i], *pcoin->tx); if (!(IsSpent(wtxid, i)) && mine != ISMINE_NO && (!IsLockedCoin((*it).first, i) || nCoinType == CoinType::ONLY_1000) && (pcoin->tx->vout[i].nValue > 0 || fIncludeZeroValue || ((pcoin->tx->vout[i].scriptPubKey.IsSparkSMint()) && GetCredit(pcoin->tx->vout[i], *pcoin->tx, ISMINE_SPENDABLE) > 0)) && (!coinControl || !coinControl->HasSelected() || coinControl->fAllowOtherInputs || coinControl->IsSelected(COutPoint((*it).first, i)))) { vCoins.push_back(COutput(pcoin, i, nDepth, ((mine & ISMINE_SPENDABLE) != ISMINE_NO) || (coinControl && coinControl->fAllowWatchOnly && (mine & ISMINE_WATCH_SOLVABLE) != ISMINE_NO), (mine & (ISMINE_SPENDABLE | ISMINE_WATCH_SOLVABLE)) != ISMINE_NO)); } } } } } std::map> CWallet::GetConsolidationCoins(const CTxDestination& target) const { LOCK2(cs_main, cs_wallet); std::vector available; AvailableCoins(available); const auto mnList = deterministicMNManager ? deterministicMNManager->GetListAtChainTip() : CDeterministicMNList(); std::map> groups; for (const auto& coin : available) { const auto& script = coin.tx->tx->vout[coin.i].scriptPubKey; CTxDestination destination; const COutPoint outpoint(coin.tx->GetHash(), coin.i); // Do not follow change ancestry or combine different scripts that decode // to the same address (e.g. bare public keys and Spark Name fee outputs). if (coin.fSpendable && coin.nDepth > 0 && ExtractDestination(script, destination) && script == GetScriptForDestination(destination) && (boost::get(&target) || target == destination) && (!deterministicMNManager || !deterministicMNManager->IsProTxWithCollateral(coin.tx->tx, coin.i)) && !mnList.HasMNByCollateral(outpoint)) { groups[destination].push_back(outpoint); } } return groups; } std::map CWallet::GetConsolidationPlans(const CTxDestination& target) const { LOCK2(cs_main, cs_wallet); std::map plans; for (const auto& group : GetConsolidationCoins(target)) { auto& plan = plans[group.first]; plan.eligibleCount = group.second.size(); if (plan.eligibleCount < 2) { plan.error = _("There are fewer than two eligible outputs at this address."); continue; } // Cache values before sorting; map lookups do not belong in the comparator. std::vector> coins; coins.reserve(group.second.size()); for (const auto& outpoint : group.second) coins.emplace_back(mapWallet.at(outpoint.hash).tx->vout[outpoint.n].nValue, outpoint); std::sort(coins.begin(), coins.end(), [](const auto& a, const auto& b) { return a.first != b.first ? a.first > b.first : a.second < b.second; }); // Even an unsigned input needs this much weight. Bound the sizing work. coins.resize(std::min(coins.size(), size_t(MAX_NEW_TX_WEIGHT / (WITNESS_SCALE_FACTOR * ::GetSerializeSize(CTxIn(), SER_NETWORK, PROTOCOL_VERSION))))); CMutableTransaction sized; sized.vout.emplace_back(0, GetScriptForDestination(group.first)); CCoinsView emptyView; CCoinsViewCache view(&emptyView); const auto& first = coins.front().second; const auto& firstTx = mapWallet.at(first.hash); sized.vin.emplace_back(first); const std::vector> inputs{{&firstTx, first.n}}; view.AddCoin(first, Coin(firstTx.tx->vout[first.n], 0, false), false); if (!DummySignTx(sized, inputs)) { plan.error = _("Signing transaction failed"); continue; } // Exact script groups share one maximum-size dummy satisfaction. Derive // its cost once, including witness data and the input-count CompactSize. const CTxIn signedInput = sized.vin.front(); const CTransaction singleInput(sized); const size_t witnessBytes = sized.HasWitness() ? ::GetSerializeSize(signedInput.scriptWitness.stack, SER_NETWORK, PROTOCOL_VERSION) : 0; const size_t inputBytes = ::GetSerializeSize(signedInput, SER_NETWORK, PROTOCOL_VERSION) + witnessBytes; const size_t inputWeight = WITNESS_SCALE_FACTOR * (inputBytes - witnessBytes) + witnessBytes; const size_t singleWeight = GetTransactionWeight(singleInput); const int64_t outputSigOps = int64_t(sized.vout[0].scriptPubKey.GetSigOpCount(false)) * WITNESS_SCALE_FACTOR; const int64_t inputSigOps = GetTransactionSigOpCost(singleInput, view, STANDARD_SCRIPT_VERIFY_FLAGS) - outputSigOps; size_t low = 0, high = coins.size(); while (low < high) { const size_t count = low + (high - low + 1) / 2; const size_t weight = singleWeight + (count - 1) * inputWeight + size_t(WITNESS_SCALE_FACTOR) * (GetSizeOfCompactSize(count) - GetSizeOfCompactSize(1)); if (weight < MAX_NEW_TX_WEIGHT && outputSigOps + int64_t(count) * inputSigOps <= MAX_STANDARD_TX_SIGOPS_COST) { low = count; } else { high = count - 1; } } plan.sizeLimited = low < plan.eligibleCount; if (low < 2) { plan.error = _("Too few outputs fit within the transaction limits."); continue; } CAmount total = 0; for (size_t i = 0; i < low; ++i) total += coins[i].first; unsigned int bytes = singleInput.GetTotalSize() + (low - 1) * inputBytes + GetSizeOfCompactSize(low) - GetSizeOfCompactSize(1); CAmount requiredFee = 0; bool feePolicyFailure = false; // Drop the smallest outputs until the batch pays its fee. Update its // serialized size instead of reserializing every remaining input. while (low >= 2) { requiredFee = GetMinimumFee(bytes, nTxConfirmTarget, mempool); feePolicyFailure = requiredFee < ::minRelayTxFee.GetFee(bytes); if (total > requiredFee && !feePolicyFailure) break; bytes -= inputBytes; bytes -= GetSizeOfCompactSize(low) - GetSizeOfCompactSize(low - 1); total -= coins[--low].first; } if (low < 2) { plan.error = feePolicyFailure ? _("Transaction too large for fee policy") : _("The transaction amount is too small to pay the fee"); continue; } sized.vin.assign(low, signedInput); for (size_t i = 0; i < low; ++i) { const auto& outpoint = coins[i].second; sized.vin[i].prevout = outpoint; if (i != 0) view.AddCoin(outpoint, Coin(mapWallet.at(outpoint.hash).tx->vout[outpoint.n], 0, false), false); } const CTransaction candidate(sized); if (candidate.GetTotalSize() != bytes || GetTransactionWeight(candidate) >= MAX_NEW_TX_WEIGHT || GetTransactionSigOpCost(candidate, view, STANDARD_SCRIPT_VERIFY_FLAGS) > MAX_STANDARD_TX_SIGOPS_COST) { plan.error = _("Unable to create a same-address consolidation transaction."); continue; } for (size_t i = 0; i < low; ++i) plan.inputs.push_back(coins[i].second); plan.total = total; plan.fee = requiredFee; plan.signedBytes = bytes; } return plans; } ConsolidationPlan CWallet::GetConsolidationPlan(const CTxDestination& destination) const { auto plans = GetConsolidationPlans(destination); const auto found = plans.find(destination); if (found != plans.end()) return std::move(found->second); ConsolidationPlan plan; plan.error = _("There are fewer than two eligible outputs at this address."); return plan; } bool CWallet::CreateConsolidationTransaction(const CTxDestination& destination, CWalletTx& transaction, CReserveKey& reserveKey, CAmount& fee, std::string& error, ConsolidationPlan* usedPlan) { LOCK2(cs_main, cs_wallet); const auto plan = GetConsolidationPlan(destination); if (!plan.error.empty()) { error = plan.error; return false; } CCoinControl control; control.destChange = destination; for (const auto& input : plan.inputs) control.Select(input); const CScript script = GetScriptForDestination(destination); int changePosition = -1; if (!CreateTransaction({{script, plan.total, true}}, transaction, reserveKey, fee, changePosition, error, &control)) return false; const auto& tx = *transaction.tx; if (tx.vout.size() != 1 || tx.vout[0].nValue <= 0 || tx.vout[0].scriptPubKey != script || tx.vout[0].nValue != plan.total - fee || tx.vin.size() != plan.inputs.size() || !std::all_of(tx.vin.begin(), tx.vin.end(), [&](const CTxIn& input) { return control.IsSelected(input.prevout); })) { error = _("Unable to create a same-address consolidation transaction."); return false; } if (usedPlan) *usedPlan = plan; return true; } void CWallet::AvailableCoinsForLMint(std::vector>>& valueAndUTXO, const CCoinControl *coinControl) const { valueAndUTXO.clear(); std::vector vAvailableCoins; AvailableCoins(vAvailableCoins, true, coinControl); std::map>> mapAddrToUTXO; for(const auto& coin : vAvailableCoins) { CTxDestination address; const auto& scriptPubKey = coin.tx->tx->vout[coin.i].scriptPubKey; if (!ExtractDestination(scriptPubKey, address) && !scriptPubKey.IsUnspendable()) continue; auto& element = mapAddrToUTXO[address]; if(element.second.empty()) element.first = coin.tx->tx->vout[coin.i].nValue; else element.first += coin.tx->tx->vout[coin.i].nValue; element.second.push_back(coin); } valueAndUTXO.reserve(mapAddrToUTXO.size()); for(const auto& element : mapAddrToUTXO) valueAndUTXO.emplace_back(element.second); std::sort(valueAndUTXO.begin(), valueAndUTXO.end(), [](const std::pair> &left, const std::pair> &right) { return left.first > right.first; }); } bool CWallet::GetZnodeVinAndKeys(CTxIn &txinRet, CPubKey &pubKeyRet, CKey &keyRet, std::string strTxHash, std::string strOutputIndex) { // wait for reindex and/or import to finish if (fImporting || fReindex) return false; // Find possible candidates std::vector vPossibleCoins; CCoinControl coinControl; coinControl.nCoinType = CoinType::ONLY_1000; AvailableCoins(vPossibleCoins, true, &coinControl, false); if (vPossibleCoins.empty()) { LogPrintf("CWallet::GetZnodeVinAndKeys -- Could not locate any valid znode vin\n"); return false; } if (strTxHash.empty()) // No output specified, select the first one return GetVinAndKeysFromOutput(vPossibleCoins[0], txinRet, pubKeyRet, keyRet); // Find specific vin uint256 txHash = uint256S(strTxHash); int nOutputIndex = atoi(strOutputIndex.c_str()); BOOST_FOREACH(COutput & out, vPossibleCoins) if (out.tx->GetHash() == txHash && out.i == nOutputIndex) // found it! return GetVinAndKeysFromOutput(out, txinRet, pubKeyRet, keyRet); LogPrintf("CWallet::GetZnodeVinAndKeys -- Could not locate specified znode vin\n"); return false; } bool CWallet::GetVinAndKeysFromOutput(COutput out, CTxIn &txinRet, CPubKey &pubKeyRet, CKey &keyRet) { // wait for reindex and/or import to finish if (fImporting || fReindex) return false; CScript pubScript; txinRet = CTxIn(out.tx->GetHash(), out.i); pubScript = out.tx->tx->vout[out.i].scriptPubKey; // the inputs PubKey CTxDestination address1; ExtractDestination(pubScript, address1); CBitcoinAddress address2(address1); CKeyID keyID; if (!address2.GetKeyID(keyID)) { LogPrintf("CWallet::GetVinAndKeysFromOutput -- Address does not refer to a key\n"); return false; } if (!GetKey(keyID, keyRet)) { LogPrintf("CWallet::GetVinAndKeysFromOutput -- Private key for address is not known\n"); return false; } pubKeyRet = keyRet.GetPubKey(); return true; } static void ApproximateBestSubset(std::vector > >vValue, const CAmount& nTotalLower, const CAmount& nTargetValue, std::vector& vfBest, CAmount& nBest, int iterations = 1000) { std::vector vfIncluded; vfBest.assign(vValue.size(), true); nBest = nTotalLower; FastRandomContext insecure_rand; for (int nRep = 0; nRep < iterations && nBest != nTargetValue; nRep++) { vfIncluded.assign(vValue.size(), false); CAmount nTotal = 0; bool fReachedTarget = false; for (int nPass = 0; nPass < 2 && !fReachedTarget; nPass++) { for (unsigned int i = 0; i < vValue.size(); i++) { //The solver here uses a randomized algorithm, //the randomness serves no real security purpose but is just //needed to prevent degenerate behavior and it is important //that the rng is fast. We do not use a constant random sequence, //because there may be some privacy improvement by making //the selection random. if (nPass == 0 ? insecure_rand.rand32()&1 : !vfIncluded[i]) { nTotal += vValue[i].first; vfIncluded[i] = true; if (nTotal >= nTargetValue) { fReachedTarget = true; if (nTotal < nBest) { nBest = nTotal; vfBest = vfIncluded; } nTotal -= vValue[i].first; vfIncluded[i] = false; } } } } } } bool CWallet::SelectCoinsMinConf(const CAmount& nTargetValue, const int nConfMine, const int nConfTheirs, const uint64_t nMaxAncestors, std::vector vCoins, std::set >& setCoinsRet, CAmount& nValueRet, bool fForUseInInstantSend) const { setCoinsRet.clear(); nValueRet = 0; // List of values less than target std::pair > coinLowestLarger; coinLowestLarger.first = std::numeric_limits::max(); coinLowestLarger.second.first = NULL; std::vector > > vValue; CAmount nTotalLower = 0; Shuffle(vCoins.begin(), vCoins.end(), FastRandomContext()); BOOST_FOREACH(const COutput &output, vCoins) { if (!output.fSpendable) continue; const CWalletTx *pcoin = output.tx; bool fLockedByIS = pcoin->IsLockedByLLMQInstantSend(); if (output.nDepth < ((pcoin->IsFromMe(ISMINE_ALL) || pcoin->tx->IsLelantusMint()) ? nConfMine : nConfTheirs) && !fLockedByIS) continue; if (!mempool.TransactionWithinChainLimit(pcoin->GetHash(), nMaxAncestors)) continue; int i = output.i; CAmount n = pcoin->tx->vout[i].nValue; std::pair > coin = std::make_pair(n,std::make_pair(pcoin, i)); if (n == nTargetValue) { setCoinsRet.insert(coin.second); nValueRet += coin.first; return true; } else if (n < nTargetValue + MIN_CHANGE) { vValue.push_back(coin); nTotalLower += n; } else if (n < coinLowestLarger.first) { coinLowestLarger = coin; } } if (nTotalLower == nTargetValue) { for (unsigned int i = 0; i < vValue.size(); ++i) { setCoinsRet.insert(vValue[i].second); nValueRet += vValue[i].first; } return true; } if (nTotalLower < nTargetValue) { if (coinLowestLarger.second.first == NULL) return false; setCoinsRet.insert(coinLowestLarger.second); nValueRet += coinLowestLarger.first; return true; } // Solve subset sum by stochastic approximation std::sort(vValue.begin(), vValue.end(), CompareValueOnly()); std::reverse(vValue.begin(), vValue.end()); std::vector vfBest; CAmount nBest; ApproximateBestSubset(vValue, nTotalLower, nTargetValue, vfBest, nBest); if (nBest != nTargetValue && nTotalLower >= nTargetValue + MIN_CHANGE) ApproximateBestSubset(vValue, nTotalLower, nTargetValue + MIN_CHANGE, vfBest, nBest); // If we have a bigger coin and (either the stochastic approximation didn't find a good solution, // or the next bigger coin is closer), return the bigger coin if (coinLowestLarger.second.first && ((nBest != nTargetValue && nBest < nTargetValue + MIN_CHANGE) || coinLowestLarger.first <= nBest)) { setCoinsRet.insert(coinLowestLarger.second); nValueRet += coinLowestLarger.first; } else { for (unsigned int i = 0; i < vValue.size(); i++) if (vfBest[i]) { setCoinsRet.insert(vValue[i].second); nValueRet += vValue[i].first; } LogPrint("selectcoins", "SelectCoins() best subset: "); for (unsigned int i = 0; i < vValue.size(); i++) if (vfBest[i]) LogPrint("selectcoins", "%s ", FormatMoney(vValue[i].first)); LogPrint("selectcoins", "total %s\n", FormatMoney(nBest)); } return true; } bool CWallet::SelectCoins(const std::vector& vAvailableCoins, const CAmount& nTargetValue, std::set >& setCoinsRet, CAmount& nValueRet, const CCoinControl* coinControl, bool fForUseInInstantSend) const { std::vector vCoins(vAvailableCoins); FIRO_UNUSED CoinType nCoinType = coinControl ? coinControl->nCoinType : CoinType::ALL_COINS; // coin control -> return all selected outputs (we want all selected to go into the transaction for sure) if (coinControl && coinControl->HasSelected() && !coinControl->fAllowOtherInputs) { BOOST_FOREACH(const COutput& out, vCoins) { if (!out.fSpendable) continue; nValueRet += out.tx->tx->vout[out.i].nValue; setCoinsRet.insert(std::make_pair(out.tx, out.i)); } return (nValueRet >= nTargetValue); } // calculate value from preset inputs and store them std::set > setPresetCoins; CAmount nValueFromPresetInputs = 0; std::vector vPresetInputs; if (coinControl) coinControl->ListSelected(vPresetInputs); BOOST_FOREACH(const COutPoint& outpoint, vPresetInputs) { std::map::const_iterator it = mapWallet.find(outpoint.hash); if (it != mapWallet.end()) { const CWalletTx* pcoin = &it->second; // Clearly invalid input, fail if (pcoin->tx->vout.size() <= outpoint.n) return false; nValueFromPresetInputs += pcoin->tx->vout[outpoint.n].nValue; setPresetCoins.insert(std::make_pair(pcoin, outpoint.n)); } else return false; // TODO: Allow non-wallet inputs } // remove preset inputs from vCoins for (std::vector::iterator it = vCoins.begin(); it != vCoins.end() && coinControl && coinControl->HasSelected();) { if (setPresetCoins.count(std::make_pair(it->tx, it->i))) it = vCoins.erase(it); else ++it; } size_t nMaxChainLength = std::min(GetArg("-limitancestorcount", DEFAULT_ANCESTOR_LIMIT), GetArg("-limitdescendantcount", DEFAULT_DESCENDANT_LIMIT)); bool fRejectLongChains = GetBoolArg("-walletrejectlongchains", DEFAULT_WALLET_REJECT_LONG_CHAINS); bool res = nTargetValue <= nValueFromPresetInputs || SelectCoinsMinConf(nTargetValue - nValueFromPresetInputs, 1, 6, 0, vCoins, setCoinsRet, nValueRet, fForUseInInstantSend) || SelectCoinsMinConf(nTargetValue - nValueFromPresetInputs, 1, 1, 0, vCoins, setCoinsRet, nValueRet, fForUseInInstantSend) || (bSpendZeroConfChange && SelectCoinsMinConf(nTargetValue - nValueFromPresetInputs, 0, 1, 2, vCoins, setCoinsRet, nValueRet, fForUseInInstantSend)) || (bSpendZeroConfChange && SelectCoinsMinConf(nTargetValue - nValueFromPresetInputs, 0, 1, std::min((size_t)4, nMaxChainLength/3), vCoins, setCoinsRet, nValueRet, fForUseInInstantSend)) || (bSpendZeroConfChange && SelectCoinsMinConf(nTargetValue - nValueFromPresetInputs, 0, 1, nMaxChainLength/2, vCoins, setCoinsRet, nValueRet, fForUseInInstantSend)) || (bSpendZeroConfChange && SelectCoinsMinConf(nTargetValue - nValueFromPresetInputs, 0, 1, nMaxChainLength, vCoins, setCoinsRet, nValueRet, fForUseInInstantSend)) || (bSpendZeroConfChange && !fRejectLongChains && SelectCoinsMinConf(nTargetValue - nValueFromPresetInputs, 0, 1, std::numeric_limits::max(), vCoins, setCoinsRet, nValueRet, fForUseInInstantSend)); // because SelectCoinsMinConf clears the setCoinsRet, we now add the possible inputs to the coinset setCoinsRet.insert(setPresetCoins.begin(), setPresetCoins.end()); // add preset inputs to the total value selected nValueRet += nValueFromPresetInputs; return res; } bool CWallet::FundTransaction(CMutableTransaction& tx, CAmount& nFeeRet, bool overrideEstimatedFeeRate, const CFeeRate& specificFeeRate, int& nChangePosInOut, std::string& strFailReason, bool includeWatching, bool lockUnspents, const std::set& setSubtractFeeFromOutputs, bool keepReserveKey, const CTxDestination& destChange) { std::vector vecSend; // Turn the txout set into a CRecipient vector for (size_t idx = 0; idx < tx.vout.size(); idx++) { const CTxOut& txOut = tx.vout[idx]; CRecipient recipient = {txOut.scriptPubKey, txOut.nValue, setSubtractFeeFromOutputs.count(idx) == 1, {}, {}}; vecSend.push_back(recipient); } CCoinControl coinControl; coinControl.destChange = destChange; coinControl.fAllowOtherInputs = true; coinControl.fAllowWatchOnly = includeWatching; coinControl.fOverrideFeeRate = overrideEstimatedFeeRate; coinControl.nFeeRate = specificFeeRate; BOOST_FOREACH(const CTxIn& txin, tx.vin) coinControl.Select(txin.prevout); int nExtraPayloadSize = 0; if (tx.nVersion == 3 && tx.nType != TRANSACTION_NORMAL) nExtraPayloadSize = (int)tx.vExtraPayload.size(); CReserveKey reservekey(this); CWalletTx wtx; if (!CreateTransaction(vecSend, wtx, reservekey, nFeeRet, nChangePosInOut, strFailReason, &coinControl, nExtraPayloadSize)) return false; if (nChangePosInOut != -1) tx.vout.insert(tx.vout.begin() + nChangePosInOut, wtx.tx->vout[nChangePosInOut]); // Copy output sizes from new transaction; they may have had the fee subtracted from them for (unsigned int idx = 0; idx < tx.vout.size(); idx++) tx.vout[idx].nValue = wtx.tx->vout[idx].nValue; // Add new txins (keeping original txin scriptSig/order) BOOST_FOREACH(const CTxIn& txin, wtx.tx->vin) { if (!coinControl.IsSelected(txin.prevout)) { tx.vin.push_back(txin); if (lockUnspents) { LOCK2(cs_main, cs_wallet); LockCoin(txin.prevout); } } } // optionally keep the change output key if (keepReserveKey) reservekey.KeepKey(); return true; } bool CWallet::ConvertList(std::vector vecTxIn, std::vector &vecAmounts) { BOOST_FOREACH(CTxIn txin, vecTxIn) { if (mapWallet.count(txin.prevout.hash)) { CWalletTx &wtx = mapWallet[txin.prevout.hash]; if (txin.prevout.n < wtx.tx->vout.size()) { vecAmounts.push_back(wtx.tx->vout[txin.prevout.n].nValue); } } else { LogPrintf("CWallet::ConvertList -- Couldn't find transaction\n"); } } return true; } bool CWallet::CreateTransaction(const std::vector& vecSend, CWalletTx& wtxNew, CReserveKey& reservekey, CAmount& nFeeRet, int& nChangePosInOut, std::string& strFailReason, const CCoinControl* coinControl, bool sign, int nExtraPayloadSize, bool fUseInstantSend) { CAmount nFeePay = 0; CAmount nValue = 0; int nChangePosRequest = nChangePosInOut; unsigned int nSubtractFeeFromAmount = 0; for (const auto& recipient : vecSend) { if (nValue < 0 || recipient.nAmount < 0) { strFailReason = _("Transaction amounts must not be negative"); return false; } nValue += recipient.nAmount; if (recipient.fSubtractFeeFromAmount) nSubtractFeeFromAmount++; } if (vecSend.empty()) { strFailReason = _("Transaction must have at least one recipient"); return false; } wtxNew.fTimeReceivedIsTxTime = true; wtxNew.BindWallet(this); CMutableTransaction txNew; // Discourage fee sniping. // // For a large miner the value of the transactions in the best block and // the mempool can exceed the cost of deliberately attempting to mine two // blocks to orphan the current best block. By setting nLockTime such that // only the next block can include the transaction, we discourage this // practice as the height restricted and limited blocksize gives miners // considering fee sniping fewer options for pulling off this attack. // // A simple way to think about this is from the wallet's point of view we // always want the blockchain to move forward. By setting nLockTime this // way we're basically making the statement that we only want this // transaction to appear in the next block; we don't want to potentially // encourage reorgs by allowing transactions to appear at lower heights // than the next block in forks of the best chain. // // Of course, the subsidy is high enough, and transaction volume low // enough, that fee sniping isn't a problem yet, but by implementing a fix // now we ensure code won't be written that makes assumptions about // nLockTime that preclude a fix later. int nHeight = 0; { LOCK(cs_main); nHeight = chainActive.Height(); } txNew.nLockTime = nHeight; // Secondly occasionally randomly pick a nLockTime even further back, so // that transactions that are delayed after signing for whatever reason, // e.g. high-latency mix networks and some CoinJoin implementations, have // better privacy. if (GetRandInt(10) == 0) txNew.nLockTime = std::max(0, (int)txNew.nLockTime - GetRandInt(100)); assert(txNew.nLockTime <= static_cast(nHeight)); assert(txNew.nLockTime < LOCKTIME_THRESHOLD); { std::set> setCoins; LOCK2(cs_main, cs_wallet); { std::vector vAvailableCoins; AvailableCoins(vAvailableCoins, true, coinControl, false, fUseInstantSend); FIRO_UNUSED int nInstantSendConfirmationsRequired = Params().GetConsensus().nInstantSendConfirmationsRequired; nFeeRet = 0; if(nFeePay > 0) nFeeRet = nFeePay; double dPriority = 0; // Start with no fee and loop until there is enough fee while (true) { nChangePosInOut = nChangePosRequest; txNew.vin.clear(); txNew.vout.clear(); wtxNew.fFromMe = true; bool fFirst = true; CAmount nValueToSelect = nValue; if (nSubtractFeeFromAmount == 0) nValueToSelect += nFeeRet; // vouts to the payees for (const auto& recipient : vecSend) { CTxOut txout(recipient.nAmount, recipient.scriptPubKey); if (recipient.fSubtractFeeFromAmount) { txout.nValue -= nFeeRet / nSubtractFeeFromAmount; // Subtract fee equally from each selected recipient if (fFirst) // first receiver pays the remainder not divisible by output count { fFirst = false; txout.nValue -= nFeeRet % nSubtractFeeFromAmount; } } if (txout.IsDust(dustRelayFee)) { if (recipient.fSubtractFeeFromAmount && nFeeRet > 0) { if (txout.nValue < 0) strFailReason = _("The transaction amount is too small to pay the fee"); else strFailReason = _("The transaction amount is too small to send after the fee has been deducted"); } else strFailReason = _("Transaction amount too small"); return false; } txNew.vout.push_back(txout); } // Choose coins to use CAmount nValueIn = 0; setCoins.clear(); if (!SelectCoins(vAvailableCoins, nValueToSelect, setCoins, nValueIn, coinControl, fUseInstantSend)) { strFailReason = _("Insufficient funds"); return false; } const CAmount nChange = nValueIn - nValueToSelect; CTxOut newTxOut; if (nChange > 0 && !(coinControl && coinControl->fNoChange)) { // Fill a vout to ourself // TODO: pass in scriptChange instead of reservekey so // change transaction isn't always pay-to-dash-address CScript scriptChange; // coin control: send change to custom address if (coinControl && !boost::get(&coinControl->destChange)) scriptChange = GetScriptForDestination(coinControl->destChange); // no coin control: send change to newly generated address else { // Note: We use a new key here to keep it from being obvious which side is the change. // The drawback is that by not reusing a previous key, the change may be lost if a // backup is restored, if the backup doesn't have the new private key for the change. // If we reused the old key, it would be possible to add code to look for and // rediscover unknown transactions that were written with keys of ours to recover // post-backup change. // Reserve a new key pair from key pool CPubKey vchPubKey; bool ret; ret = reservekey.GetReservedKey(vchPubKey); if (!ret) { strFailReason = _("Keypool ran out, please call keypoolrefill first"); return false; } scriptChange = GetScriptForDestination(vchPubKey.GetID()); } newTxOut = CTxOut(nChange, scriptChange); // We do not move dust-change to fees, because the sender would end up paying more than requested. // This would be against the purpose of the all-inclusive feature. // So instead we raise the change and deduct from the recipient. if (nSubtractFeeFromAmount > 0 && newTxOut.IsDust(dustRelayFee)) { CAmount nDust = newTxOut.GetDustThreshold(dustRelayFee) - newTxOut.nValue; newTxOut.nValue += nDust; // raise change until no more dust for (unsigned int i = 0; i < vecSend.size(); i++) // subtract from first recipient { if (vecSend[i].fSubtractFeeFromAmount) { txNew.vout[i].nValue -= nDust; if (txNew.vout[i].IsDust(dustRelayFee)) { strFailReason = _("The transaction amount is too small to send after the fee has been deducted"); return false; } break; } } } // Never create dust outputs; if we would, just // add the dust to the fee. if (newTxOut.IsDust(dustRelayFee)) { nChangePosInOut = -1; nFeeRet += nChange; reservekey.ReturnKey(); } else { if (nChangePosInOut == -1) { // Insert change txn at random position: nChangePosInOut = GetRandInt(txNew.vout.size()+1); } else if ((unsigned int)nChangePosInOut > txNew.vout.size()) { strFailReason = _("Change index out of range"); return false; } std::vector::iterator position = txNew.vout.begin()+nChangePosInOut; txNew.vout.insert(position, newTxOut); } } else { reservekey.ReturnKey(); nChangePosInOut = -1; } // Fill vin // // Note how the sequence number is set to max()-1 so that the // nLockTime set above actually works. // // BIP125 defines opt-in RBF as any nSequence < maxint-1, so // we use the highest possible value in that range (maxint-2) // to avoid conflicting with other possible uses of nSequence, // and in the spirit of "smallest possible change from prior // behavior." for (const auto& coin : setCoins) txNew.vin.push_back(CTxIn(coin.first->GetHash(),coin.second,CScript(), std::numeric_limits::max() - (fWalletRbf ? 2 : 1))); // Fill in dummy signatures for fee calculation. if (!DummySignTx(txNew, setCoins)) { strFailReason = _("Signing transaction failed"); return false; } unsigned int nBytes = ::GetSerializeSize(txNew, SER_NETWORK, PROTOCOL_VERSION); if (nExtraPayloadSize != 0) { // account for extra payload in fee calculation nBytes += GetSizeOfCompactSize(nExtraPayloadSize) + nExtraPayloadSize; } if (GetTransactionWeight(txNew) >= MAX_NEW_TX_WEIGHT) { // Do not create oversized transactions (bad-txns-oversize). strFailReason = _("Transaction is too large (size limit: 250Kb). Select fewer inputs. If many inputs belong to one transparent address, use File > Consolidate outputs in the GUI or the consolidateaddress RPC, then retry after confirmation."); return false; } CTransaction txNewConst(txNew); dPriority = txNewConst.ComputePriority(dPriority, nBytes); // Remove scriptSigs to eliminate the fee calculation dummy signatures for (auto& vin : txNew.vin) { vin.scriptSig = CScript(); vin.scriptWitness.SetNull(); } // Allow to override the default confirmation target over the CoinControl instance int currentConfirmationTarget = nTxConfirmTarget; if (coinControl && coinControl->nConfirmTarget > 0) currentConfirmationTarget = coinControl->nConfirmTarget; // Can we complete this as a free transaction? if (fSendFreeTransactions && nBytes <= MAX_FREE_TRANSACTION_CREATE_SIZE) { // Not enough fee: enough priority? double dPriorityNeeded = mempool.estimateSmartPriority(currentConfirmationTarget); // Require at least hard-coded AllowFree. if (dPriority >= dPriorityNeeded && AllowFree(dPriority)) break; } CAmount nFeeNeeded = GetMinimumFee(nBytes, currentConfirmationTarget, mempool); if (coinControl && nFeeNeeded > 0 && coinControl->nMinimumTotalFee > nFeeNeeded) { nFeeNeeded = coinControl->nMinimumTotalFee; } if (coinControl && coinControl->fOverrideFeeRate) nFeeNeeded = coinControl->nFeeRate.GetFee(nBytes); // If we made it here and we aren't even able to meet the relay fee on the next pass, give up // because we must be at the maximum allowed fee. if (nFeeNeeded < ::minRelayTxFee.GetFee(nBytes)) { strFailReason = _("Transaction too large for fee policy"); return false; } if (nFeeRet >= nFeeNeeded) { // Reduce fee to only the needed amount if we have change // output to increase. This prevents potential overpayment // in fees if the coins selected to meet nFeeNeeded result // in a transaction that requires less fee than the prior // iteration. // TODO: The case where nSubtractFeeFromAmount > 0 remains // to be addressed because it requires returning the fee to // the payees and not the change output. // TODO: The case where there is no change output remains // to be addressed so we avoid creating too small an output. if (nFeeRet > nFeeNeeded && nChangePosInOut != -1 && nSubtractFeeFromAmount == 0) { CAmount extraFeePaid = nFeeRet - nFeeNeeded; std::vector::iterator change_position = txNew.vout.begin()+nChangePosInOut; change_position->nValue += extraFeePaid; nFeeRet -= extraFeePaid; } break; // Done, enough fee included. } // Try to reduce change to include necessary fee if (nChangePosInOut != -1 && nSubtractFeeFromAmount == 0) { CAmount additionalFeeNeeded = nFeeNeeded - nFeeRet; std::vector::iterator change_position = txNew.vout.begin()+nChangePosInOut; // Only reduce change if remaining amount is still a large enough output. if (change_position->nValue >= MIN_FINAL_CHANGE + additionalFeeNeeded) { change_position->nValue -= additionalFeeNeeded; nFeeRet += additionalFeeNeeded; break; // Done, able to increase fee from change } } // Include more fee and try again. nFeeRet = nFeeNeeded; continue; } } if (sign) { CTransaction txNewConst(txNew); int nIn = 0; for (const auto& coin : setCoins) { const CScript& scriptPubKey = coin.first->tx->vout[coin.second].scriptPubKey; SignatureData sigdata; if (!ProduceSignature(TransactionSignatureCreator(this, &txNewConst, nIn, coin.first->tx->vout[coin.second].nValue, SIGHASH_ALL), scriptPubKey, sigdata)) { strFailReason = _("Signing transaction failed"); return false; } else { UpdateTransaction(txNew, nIn, sigdata); } nIn++; } } // Embed the constructed transaction data in wtxNew. wtxNew.SetTx(MakeTransactionRef(std::move(txNew))); } if (GetBoolArg("-walletrejectlongchains", DEFAULT_WALLET_REJECT_LONG_CHAINS)) { // Lastly, ensure this tx will pass the mempool's chain limits LockPoints lp; CTxMemPoolEntry entry(wtxNew.tx, 0, 0, 0, 0, false, 0, lp); CTxMemPool::setEntries setAncestors; size_t nLimitAncestors = GetArg("-limitancestorcount", DEFAULT_ANCESTOR_LIMIT); size_t nLimitAncestorSize = GetArg("-limitancestorsize", DEFAULT_ANCESTOR_SIZE_LIMIT)*1000; size_t nLimitDescendants = GetArg("-limitdescendantcount", DEFAULT_DESCENDANT_LIMIT); size_t nLimitDescendantSize = GetArg("-limitdescendantsize", DEFAULT_DESCENDANT_SIZE_LIMIT)*1000; std::string errString; if (!mempool.CalculateMemPoolAncestors(entry, setAncestors, nLimitAncestors, nLimitAncestorSize, nLimitDescendants, nLimitDescendantSize, errString)) { strFailReason = _("Transaction has too long of a mempool chain"); return false; } } return true; } static std::atomic g_sparkOutputWriteFailAfter{-1}; void CWallet::SetSparkOutputWriteFailureForTesting(int failAfterWrites) { g_sparkOutputWriteFailAfter.store(failAfterWrites); } static bool WritePendingSparkOutputsAtomically( const std::string& strWalletFile, const std::vector>& records, std::vector& persistedScripts) { persistedScripts.clear(); if (records.empty()) return true; const int failAfter = g_sparkOutputWriteFailAfter.load(); if (failAfter == 0) return false; CWalletDB walletdb(strWalletFile); if (!walletdb.TxnBegin()) return false; int writes = 0; for (const auto& outputRecord : records) { if (!walletdb.WriteSparkOutputTx(outputRecord.first, outputRecord.second)) { walletdb.TxnAbort(); persistedScripts.clear(); return false; } persistedScripts.push_back(outputRecord.first); ++writes; if (failAfter > 0 && writes >= failAfter) { walletdb.TxnAbort(); persistedScripts.clear(); return false; } } if (!walletdb.TxnCommit()) { persistedScripts.clear(); return false; } return true; } static bool EraseSparkOutputScripts( const std::string& strWalletFile, const std::vector& scripts) { if (scripts.empty()) return true; CWalletDB walletdb(strWalletFile); if (!walletdb.TxnBegin()) return false; for (const auto& script : scripts) { if (!walletdb.EraseSparkOutputTx(script)) { walletdb.TxnAbort(); return false; } } return walletdb.TxnCommit(); } /** * Call after CreateTransaction unless you want to abort */ bool CWallet::CommitTransaction(CWalletTx& wtxNew, CReserveKey& reservekey, CConnman* connman, CValidationState& state, bool fCheckTransaction) { { LOCK2(cs_main, cs_wallet); LogPrintf("CommitTransaction:\n%s", wtxNew.tx->ToString()); // Persist Spark output metadata atomically before mempool acceptance. // AcceptToMemoryPool inserts the transaction and updates Spark spend // state, so a later disk-full failure would otherwise be reported as a // failed payment while the transaction remained eligible for mining. std::vector persistedSparkOutputScripts; if (!WritePendingSparkOutputsAtomically( strWalletFile, wtxNew.pendingSparkOutputRecords, persistedSparkOutputScripts)) { LogPrintf("CommitTransaction(): Unable to save Spark transaction output\n"); return state.Error("Unable to save Spark transaction output"); } // When fCheckTransaction is set, validate against the mempool before adding // the transaction to the wallet. If rejected, drop Spark output metadata // written above and return without AddToWallet. if (fCheckTransaction && !wtxNew.AcceptToMemoryPool(maxTxFee, state)) { LogPrintf("CommitTransaction(): Transaction rejected: %s\n", state.GetRejectReason()); if (!EraseSparkOutputScripts(strWalletFile, persistedSparkOutputScripts)) { LogPrintf( "CommitTransaction(): failed to drop Spark output metadata after mempool rejection\n"); } return false; } wtxNew.pendingSparkOutputRecords.clear(); { // Take key pair from key pool so it won't be used again reservekey.KeepKey(); // Add tx to wallet, because if it has change it's also ours, // otherwise just for transaction history. AddToWallet(wtxNew); // Notify that old coins are spent BOOST_FOREACH(const CTxIn& txin, wtxNew.tx->vin) { // Skip inputs of anonymized transactions if (txin.prevout.hash.IsNull()) continue; CWalletTx &coin = mapWallet[txin.prevout.hash]; coin.BindWallet(this); NotifyTransactionChanged(this, coin.GetHash(), CT_UPDATED); } } // Track how many getdata requests our transaction gets mapRequestCount[wtxNew.GetHash()] = 0; // RelayWalletTransaction asserts GetBroadcastTransactions(), so never // relay when -walletbroadcast=0. fCheckTransaction only controls the // earlier mempool acceptance, not inventory push. if (fBroadcastTransactions) { if (fCheckTransaction || wtxNew.AcceptToMemoryPool(maxTxFee, state)) { wtxNew.RelayWalletTransaction(connman); } else { LogPrintf("CommitTransaction(): Transaction cannot be broadcast immediately, %s\n", state.GetRejectReason()); } } } return true; } bool CWallet::EraseFromWallet(uint256 hash) { if (!fFileBacked) return false; { LOCK(cs_wallet); if (mapWallet.erase(hash)) CWalletDB(strWalletFile).EraseTx(hash); } return true; } /** * @brief CWallet::MintAndStoreSpark Create and commit Spark mint transactions. * @param outputs Spark mint outputs to create * @param[out] wtxAndFee Created wallet transactions and their fees * @param subtractFeeFromAmount Whether to subtract fees from output amounts * @param fSplit Whether to split outputs * @param autoMintAll Whether this is an automatic mint-all request * @param fAskFee Whether to ask the user before paying the fee * @param coinControl Optional coin selection control parameters * @return empty string if successful, otherwise an error description */ std::string CWallet::MintAndStoreSpark( const std::vector& outputs, std::vector>& wtxAndFee, bool subtractFeeFromAmount, bool fSplit, bool autoMintAll, bool fAskFee, const CCoinControl *coinControl) { std::string strError; EnsureSparkWalletAvailable(); if (IsLocked()) { strError = _("Error: Wallet locked, unable to create transaction!"); LogPrintf("MintSpark() : %s", strError); return strError; } CAmount value = 0; for (const auto& output : outputs) { if (output.v > static_cast(MAX_MONEY - value)) return _("Spark mint amount out of range"); value += static_cast(output.v); } const CAmount feePerK = payTxFee.GetFeePerK(); if (!MoneyRange(feePerK)) return _("Spark mint amount out of range"); if (value > GetBalance()) return _("Insufficient funds"); LogPrintf("payTxFee.GetFeePerK()=%d\n", payTxFee.GetFeePerK()); int64_t nFeeRequired = 0; int nChangePosRet = -1; std::list reservekeys; if (!sparkWallet->CreateSparkMintTransactions(outputs, wtxAndFee, nFeeRequired, reservekeys, nChangePosRet, subtractFeeFromAmount, strError, fSplit, coinControl, autoMintAll)) { return strError; } if (fAskFee && !uiInterface.ThreadSafeAskFee(nFeeRequired)){ LogPrintf("MintSpark: returning aborted..\n"); return "ABORTED"; } CValidationState state; auto reservekey = reservekeys.begin(); for(size_t i = 0; i < wtxAndFee.size(); i++) { if (!CommitTransaction(wtxAndFee[i].first, *reservekey++, g_connman.get(), state, true)) { if (i > 0) { return strprintf( _("Error: The transaction was rejected after %u of %u mint transactions were already sent. Do not retry the whole mint."), i, wtxAndFee.size()); } return _( "Error: The transaction was rejected! This might happen if some of the coins in your wallet were already spent, such as if you used a copy of wallet.dat and coins were spent in the copy but not marked as spent here."); } else { LogPrintf("CommitTransaction success!\n"); } } return ""; } CWalletTx CWallet::CreateSparkSpendTransaction( const std::vector& recipients, const std::vector>& privateRecipients, CAmount &fee, const CCoinControl *coinControl, int expectedNextBlockHeight, std::vector* recipientAmounts) { // sanity check EnsureSparkWalletAvailable(); if (IsLocked()) { throw std::runtime_error(_("Wallet locked")); } return sparkWallet->CreateSparkSpendTransaction( recipients, privateRecipients, fee, coinControl, 0, uint256(), expectedNextBlockHeight, recipientAmounts); } CWalletTx CWallet::CreateSparkNameTransaction( CSparkNameTxData &sparkNameData, CAmount sparkNameFee, CAmount &txFee, const CCoinControl *coinControl, int expectedNextBlockHeight) { // sanity check EnsureSparkWalletAvailable(); if (IsLocked()) { throw std::runtime_error(_("Wallet locked")); } return sparkWallet->CreateSparkNameTransaction( sparkNameData, sparkNameFee, txFee, coinControl, expectedNextBlockHeight); } CWalletTx CWallet::SpendAndStoreSpark( const std::vector& recipients, const std::vector>& privateRecipients, CAmount &fee, const CCoinControl *coinControl) { // create transaction auto result = CreateSparkSpendTransaction(recipients, privateRecipients, fee, coinControl); // commit try { CValidationState state; CReserveKey reserveKey(this); if (!CommitTransaction( result, reserveKey, g_connman.get(), state, true)) { throw std::runtime_error(state.GetRejectReason()); } } catch (const std::exception &) { auto error = _( "Error: The transaction was rejected! This might happen if some of " "the coins in your wallet were already spent, such as if you used " "a copy of wallet.dat and coins were spent in the copy but not " "marked as spent here." ); std::throw_with_nested(std::runtime_error(error)); } return result; } std::vector CWallet::SpendAndStoreSparkSingleInput( const std::vector& recipients, const std::vector>& privateRecipients, CAmount& totalFee, const CCoinControl* coinControl) { return sparkspendbatch::SpendAndStoreSingleInputBatches( *this, recipients, privateRecipients, totalFee, coinControl); } void CWallet::ListAccountCreditDebit(const std::string& strAccount, std::list& entries) { CWalletDB walletdb(strWalletFile); return walletdb.ListAccountCreditDebit(strAccount, entries); } bool CWallet::AddAccountingEntry(const CAccountingEntry& acentry) { CWalletDB walletdb(strWalletFile); return AddAccountingEntry(acentry, &walletdb); } bool CWallet::AddAccountingEntry(const CAccountingEntry& acentry, CWalletDB *pwalletdb) { if (!pwalletdb->WriteAccountingEntry_Backend(acentry)) return false; laccentries.push_back(acentry); CAccountingEntry & entry = laccentries.back(); wtxOrdered.insert(make_pair(entry.nOrderPos, TxPair((CWalletTx*)0, &entry))); return true; } CAmount CWallet::GetRequiredFee(unsigned int nTxBytes) { return std::max(minTxFee.GetFee(nTxBytes), ::minRelayTxFee.GetFee(nTxBytes)); } CAmount CWallet::GetMinimumFee(unsigned int nTxBytes, unsigned int nConfirmTarget, const CTxMemPool& pool) { // payTxFee is the user-set global for desired feerate return GetMinimumFee(nTxBytes, nConfirmTarget, pool, payTxFee.GetFee(nTxBytes)); } CAmount CWallet::GetMinimumFee( unsigned int nTxBytes, const CCoinControl* coinControl, const CTxMemPool& pool) { const unsigned int confirmationTarget = coinControl && coinControl->nConfirmTarget > 0 ? coinControl->nConfirmTarget : nTxConfirmTarget; CAmount fee = GetMinimumFee(nTxBytes, confirmationTarget, pool); if (coinControl && fee > 0 && coinControl->nMinimumTotalFee > fee) { fee = coinControl->nMinimumTotalFee; } if (coinControl && coinControl->fOverrideFeeRate) { fee = coinControl->nFeeRate.GetFee(nTxBytes); } return fee; } CAmount CWallet::GetMinimumFee(unsigned int nTxBytes, unsigned int nConfirmTarget, const CTxMemPool& pool, CAmount targetFee) { CAmount nFeeNeeded = targetFee; // User didn't set: use -txconfirmtarget to estimate... if (nFeeNeeded == 0) { int estimateFoundTarget = nConfirmTarget; nFeeNeeded = pool.estimateSmartFee(nConfirmTarget, &estimateFoundTarget).GetFee(nTxBytes); // ... unless we don't have enough mempool data for estimatefee, then use fallbackFee if (nFeeNeeded == 0) nFeeNeeded = fallbackFee.GetFee(nTxBytes); } // prevent user from paying a fee below minRelayTxFee or minTxFee nFeeNeeded = std::max(nFeeNeeded, GetRequiredFee(nTxBytes)); // But always obey the maximum if (nFeeNeeded > maxTxFee) nFeeNeeded = maxTxFee; return nFeeNeeded; } DBErrors CWallet::LoadWallet(bool& fFirstRunRet) { MnemonicContainer mContainer = GetMnemonicContainer(); SecureString mnemonic; //Don't dump mnemonic words in case user has set only hd seed during wallet creation if(mContainer.GetMnemonic(mnemonic)) std::cout << "# mnemonic: " << mnemonic << "\n"; if (!fFileBacked) return DB_LOAD_OK; fFirstRunRet = false; DBErrors nLoadWalletRet = CWalletDB(strWalletFile,"cr+").LoadWallet(this); if (nLoadWalletRet == DB_NEED_REWRITE) { if (CDB::Rewrite(strWalletFile, "\x04pool")) { LOCK(cs_wallet); setKeyPool.clear(); m_pool_key_to_index.clear(); // Note: can't top-up keypool here, because wallet is locked. // User will be prompted to unlock wallet the next operation // that requires a new key. } } { LOCK2(cs_main, cs_wallet); for (auto& pair : mapWallet) { for(unsigned int i = 0; i < pair.second.tx->vout.size(); ++i) { if (IsMine(pair.second.tx->vout[i], *pair.second.tx) && !IsSpent(pair.first, i)) { setWalletUTXO.insert(COutPoint(pair.first, i)); } } } } if (nLoadWalletRet != DB_LOAD_OK) return nLoadWalletRet; fFirstRunRet = !vchDefaultKey.IsValid(); uiInterface.LoadWallet(this); return DB_LOAD_OK; } // Goes through all wallet transactions and checks if they are masternode collaterals, in which case these are locked // This avoids accidential spending of collaterals. They can still be unlocked manually if a spend is really intended. void CWallet::AutoLockMasternodeCollaterals() { auto mnList = deterministicMNManager->GetListAtChainTip(); LOCK2(cs_main, cs_wallet); for (const auto& pair : mapWallet) { for (unsigned int i = 0; i < pair.second.tx->vout.size(); ++i) { // Avoid Spark ownership/spent checks for outputs that are not collateral. if (deterministicMNManager->IsProTxWithCollateral(pair.second.tx, i) || mnList.HasMNByCollateral(COutPoint(pair.first, i))) { if (IsMine(pair.second.tx->vout[i], *pair.second.tx) && !IsSpent(pair.first, i)) { LockCoin(COutPoint(pair.first, i)); } } } } } DBErrors CWallet::ZapSelectTx(std::vector& vHashIn, std::vector& vHashOut) { if (!fFileBacked) return DB_LOAD_OK; DBErrors nZapSelectTxRet = CWalletDB(strWalletFile,"cr+").ZapSelectTx(this, vHashIn, vHashOut); if (nZapSelectTxRet == DB_NEED_REWRITE) { if (CDB::Rewrite(strWalletFile, "\x04pool")) { LOCK(cs_wallet); setKeyPool.clear(); m_pool_key_to_index.clear(); // Note: can't top-up keypool here, because wallet is locked. // User will be prompted to unlock wallet the next operation // that requires a new key. } } if (nZapSelectTxRet != DB_LOAD_OK) return nZapSelectTxRet; MarkDirty(); return DB_LOAD_OK; } DBErrors CWallet::ZapWalletTx(std::vector& vWtx) { if (!fFileBacked) return DB_LOAD_OK; DBErrors nZapWalletTxRet = CWalletDB(strWalletFile,"cr+").ZapWalletTx(this, vWtx); if (nZapWalletTxRet == DB_NEED_REWRITE) { if (CDB::Rewrite(strWalletFile, "\x04pool")) { LOCK(cs_wallet); setKeyPool.clear(); m_pool_key_to_index.clear(); // Note: can't top-up keypool here, because wallet is locked. // User will be prompted to unlock wallet the next operation // that requires a new key. } } if (nZapWalletTxRet != DB_LOAD_OK) return nZapWalletTxRet; return DB_LOAD_OK; } DBErrors CWallet::ZapSparkMints() { if (!fFileBacked) return DB_LOAD_OK; DBErrors nZapSparkMintRet = CWalletDB(strWalletFile, "cr+").ZapSparkMints(this); if (nZapSparkMintRet != DB_LOAD_OK){ LogPrintf("Failed to remove spark mints from CWalletDB"); return nZapSparkMintRet; } return DB_LOAD_OK; } bool CWallet::SetAddressBook(const CTxDestination& address, const std::string& strName, const std::string& strPurpose) { bool fUpdated = false; { LOCK(cs_wallet); // mapAddressBook std::map::iterator mi = mapAddressBook.find(address); fUpdated = mi != mapAddressBook.end(); mapAddressBook[address].name = strName; if (!strPurpose.empty()) /* update purpose only if requested */ mapAddressBook[address].purpose = strPurpose; } NotifyAddressBookChanged(this, address, strName, ::IsMine(*this, address) != ISMINE_NO, strPurpose, (fUpdated ? CT_UPDATED : CT_NEW) ); if (!fFileBacked) return false; if (!strPurpose.empty() && !CWalletDB(strWalletFile).WritePurpose(CBitcoinAddress(address).ToString(), strPurpose)) return false; return CWalletDB(strWalletFile).WriteName(CBitcoinAddress(address).ToString(), strName); } bool CWallet::DelAddressBook(const CTxDestination& address) { { LOCK(cs_wallet); // mapAddressBook if(fFileBacked) { // Delete destdata tuples associated with address std::string strAddress = CBitcoinAddress(address).ToString(); BOOST_FOREACH(const PAIRTYPE(std::string, std::string) &item, mapAddressBook[address].destdata) { CWalletDB(strWalletFile).EraseDestData(strAddress, item.first); } } mapAddressBook.erase(address); } NotifyAddressBookChanged(this, address, "", ::IsMine(*this, address) != ISMINE_NO, "", CT_DELETED); if (!fFileBacked) return false; CWalletDB(strWalletFile).ErasePurpose(CBitcoinAddress(address).ToString()); return CWalletDB(strWalletFile).EraseName(CBitcoinAddress(address).ToString()); } const std::string& CWallet::GetAccountName(const CScript& scriptPubKey) const { CTxDestination address; if (ExtractDestination(scriptPubKey, address) && !scriptPubKey.IsUnspendable()) { auto mi = mapAddressBook.find(address); if (mi != mapAddressBook.end()) { return mi->second.name; } } // A scriptPubKey that doesn't have an entry in the address book is // associated with the default account (""). const static std::string DEFAULT_ACCOUNT_NAME; return DEFAULT_ACCOUNT_NAME; } bool CWallet::SetDefaultKey(const CPubKey &vchPubKey) { if (fFileBacked) { if (!CWalletDB(strWalletFile).WriteDefaultKey(vchPubKey)) return false; } vchDefaultKey = vchPubKey; return true; } /** * Mark old keypool keys as used, * and generate all new keys */ bool CWallet::NewKeyPool() { { LOCK(cs_wallet); CWalletDB walletdb(strWalletFile); BOOST_FOREACH(int64_t nIndex, setKeyPool) walletdb.ErasePool(nIndex); setKeyPool.clear(); m_pool_key_to_index.clear(); if (IsLocked()) return false; int64_t nKeys = std::max(GetArg("-keypool", DEFAULT_KEYPOOL_SIZE), (int64_t)0); for (int i = 0; i < nKeys; i++) { int64_t nIndex = i+1; CPubKey pubkey(GenerateNewKey()); walletdb.WritePool(nIndex, CKeyPool(pubkey)); setKeyPool.insert(nIndex); m_pool_key_to_index[pubkey.GetID()] = nIndex; } LogPrintf("CWallet::NewKeyPool wrote %d new keys\n", nKeys); } return true; } bool CWallet::TopUpKeyPool(unsigned int kpSize) { { LOCK(cs_wallet); if (IsLocked()) return false; CWalletDB walletdb(strWalletFile); // Top up key pool unsigned int nTargetSize; if (kpSize > 0) nTargetSize = kpSize; else nTargetSize = std::max(GetArg("-keypool", DEFAULT_KEYPOOL_SIZE), (int64_t) 0); while (setKeyPool.size() < (nTargetSize + 1)) { int64_t nEnd = 1; if (!setKeyPool.empty()) nEnd = *(--setKeyPool.end()) + 1; CPubKey pubkey(GenerateNewKey()); if (!walletdb.WritePool(nEnd, CKeyPool(pubkey))) throw std::runtime_error(std::string(__func__) + ": writing generated key failed"); setKeyPool.insert(nEnd); LogPrintf("keypool added key %d, size=%u\n", nEnd, setKeyPool.size()); m_pool_key_to_index[pubkey.GetID()] = nEnd; } } return true; } void CWallet::ReserveKeyFromKeyPool(int64_t& nIndex, CKeyPool& keypool) { nIndex = -1; keypool.vchPubKey = CPubKey(); { LOCK(cs_wallet); if (!IsLocked()) TopUpKeyPool(); // Get the oldest key if(setKeyPool.empty()) return; CWalletDB walletdb(strWalletFile); nIndex = *(setKeyPool.begin()); setKeyPool.erase(setKeyPool.begin()); if (!walletdb.ReadPool(nIndex, keypool)) throw std::runtime_error(std::string(__func__) + ": read failed"); if (!HaveKey(keypool.vchPubKey.GetID())) throw std::runtime_error(std::string(__func__) + ": unknown key in key pool"); assert(keypool.vchPubKey.IsValid()); m_pool_key_to_index.erase(keypool.vchPubKey.GetID()); LogPrintf("keypool reserve %d\n", nIndex); } } void CWallet::KeepKey(int64_t nIndex) { // Remove from key pool if (fFileBacked) { CWalletDB walletdb(strWalletFile); walletdb.ErasePool(nIndex); } LogPrintf("keypool keep %d\n", nIndex); } void CWallet::ReturnKey(int64_t nIndex, const CPubKey& pubkey) { // Return to key pool { LOCK(cs_wallet); setKeyPool.insert(nIndex); m_pool_key_to_index[pubkey.GetID()] = nIndex; } LogPrintf("keypool return %d\n", nIndex); } bool CWallet::GetKeyFromPool(CPubKey& result) { int64_t nIndex = 0; CKeyPool keypool; { LOCK(cs_wallet); ReserveKeyFromKeyPool(nIndex, keypool); if (nIndex == -1) { if (IsLocked()) return false; result = GenerateNewKey(); return true; } KeepKey(nIndex); result = keypool.vchPubKey; } return true; } int64_t CWallet::GetOldestKeyPoolTime() { LOCK(cs_wallet); // if the keypool is empty, return if (setKeyPool.empty()) return GetTime(); // load oldest key from keypool, get time and return CKeyPool keypool; CWalletDB walletdb(strWalletFile); int64_t nIndex = *(setKeyPool.begin()); if (!walletdb.ReadPool(nIndex, keypool)) throw std::runtime_error(std::string(__func__) + ": read oldest key in keypool failed"); assert(keypool.vchPubKey.IsValid()); return keypool.nTime; } std::map CWallet::GetAddressBalances() { std::map balances; { LOCK(cs_wallet); BOOST_FOREACH(PAIRTYPE(uint256, CWalletTx) walletEntry, mapWallet) { CWalletTx *pcoin = &walletEntry.second; if (!pcoin->IsTrusted()) continue; if (pcoin->IsCoinBase() && pcoin->GetBlocksToMaturity() > 0) continue; int nDepth = pcoin->GetDepthInMainChain(); if ((nDepth < (pcoin->IsFromMe(ISMINE_ALL) ? 0 : 1)) && !pcoin->IsLockedByLLMQInstantSend()) continue; for (unsigned int i = 0; i < pcoin->tx->vout.size(); i++) { CTxDestination addr; if (!IsMine(pcoin->tx->vout[i], *pcoin->tx)) continue; if(!ExtractDestination(pcoin->tx->vout[i].scriptPubKey, addr)) continue; CAmount n = IsSpent(walletEntry.first, i) ? 0 : pcoin->tx->vout[i].nValue; if (!balances.count(addr)) balances[addr] = 0; balances[addr] += n; } } } return balances; } std::set< std::set > CWallet::GetAddressGroupings() { AssertLockHeld(cs_wallet); // mapWallet std::set< std::set > groupings; std::set grouping; BOOST_FOREACH(PAIRTYPE(uint256, CWalletTx) walletEntry, mapWallet) { CWalletTx *pcoin = &walletEntry.second; if (pcoin->tx->vin.size() > 0 && !(pcoin->tx->HasNoRegularInputs())) { /* Spends have no standard input */ bool any_mine = false; // group all input addresses with each other BOOST_FOREACH(CTxIn txin, pcoin->tx->vin) { CTxDestination address; if(!IsMine(txin, *pcoin->tx)) /* If this input isn't mine, ignore it */ continue; if(!ExtractDestination(mapWallet[txin.prevout.hash].tx->vout[txin.prevout.n].scriptPubKey, address)) continue; grouping.insert(address); any_mine = true; } // group change with input addresses if (any_mine) { for (uint32_t i = 0; i < pcoin->tx->vout.size(); i++) { if (pcoin->IsChange(i)) { CTxDestination addr; if (!ExtractDestination(pcoin->tx->vout[i].scriptPubKey, addr)) { continue; } grouping.insert(addr); } } } if (grouping.size() > 0) { groupings.insert(grouping); grouping.clear(); } } // group lone addrs by themselves for (unsigned int i = 0; i < pcoin->tx->vout.size(); i++) if (IsMine(pcoin->tx->vout[i], *pcoin->tx)) { CTxDestination address; if(!ExtractDestination(pcoin->tx->vout[i].scriptPubKey, address)) continue; grouping.insert(address); groupings.insert(grouping); grouping.clear(); } } std::set< std::set* > uniqueGroupings; // a set of pointers to groups of addresses std::map< CTxDestination, std::set* > setmap; // map addresses to the unique group containing it BOOST_FOREACH(std::set _grouping, groupings) { // make a set of all the groups hit by this new group std::set< std::set* > hits; std::map< CTxDestination, std::set* >::iterator it; BOOST_FOREACH(CTxDestination address, _grouping) if ((it = setmap.find(address)) != setmap.end()) hits.insert((*it).second); // merge all hit groups into a new single group and delete old groups std::set* merged = new std::set(_grouping); BOOST_FOREACH(std::set* hit, hits) { merged->insert(hit->begin(), hit->end()); uniqueGroupings.erase(hit); delete hit; } uniqueGroupings.insert(merged); // update setmap BOOST_FOREACH(CTxDestination element, *merged) setmap[element] = merged; } std::set< std::set > ret; BOOST_FOREACH(std::set* uniqueGrouping, uniqueGroupings) { ret.insert(*uniqueGrouping); delete uniqueGrouping; } return ret; } std::set CWallet::GetAccountAddresses(const std::string& strAccount) const { LOCK(cs_wallet); std::set result; BOOST_FOREACH(const PAIRTYPE(CTxDestination, CAddressBookData)& item, mapAddressBook) { const CTxDestination& address = item.first; const std::string& strName = item.second.name; if (strName == strAccount) result.insert(address); } return result; } bool CReserveKey::GetReservedKey(CPubKey& pubkey) { if (nIndex == -1) { CKeyPool keypool; pwallet->ReserveKeyFromKeyPool(nIndex, keypool); if (nIndex != -1) vchPubKey = keypool.vchPubKey; else { return false; } } assert(vchPubKey.IsValid()); pubkey = vchPubKey; return true; } void CReserveKey::KeepKey() { if (nIndex != -1) pwallet->KeepKey(nIndex); nIndex = -1; vchPubKey = CPubKey(); } void CReserveKey::ReturnKey() { if (nIndex != -1) pwallet->ReturnKey(nIndex, vchPubKey); nIndex = -1; vchPubKey = CPubKey(); } void CWallet::MarkReserveKeysAsUsed(int64_t keypool_id) { AssertLockHeld(cs_wallet); auto it = setKeyPool.begin(); CWalletDB walletdb(strWalletFile); while (it != std::end(setKeyPool)) { const int64_t& index = *(it); if (index > keypool_id) break; // set*KeyPool is ordered walletdb.ErasePool(index); it = setKeyPool.erase(it); } } spark::FullViewKey CWallet::GetSparkViewKey() { LOCK(cs_wallet); CWalletDB walletdb(strWalletFile); spark::FullViewKey key; walletdb.readFullViewKey(key); return key; } std::string CWallet::GetSparkViewKeyStr() { spark::FullViewKey key = GetSparkViewKey(); FIRO_UNUSED int size = GetSerializeSize(key, SER_NETWORK, PROTOCOL_VERSION); std::ostringstream keydata; ::Serialize(keydata, key); std::string keydata_hex = HexStr(keydata.str()); return keydata_hex; } bool CWallet::UpdatedTransaction(const uint256 &hashTx) { { LOCK(cs_wallet); // Only notify UI if this transaction is in this wallet std::map::const_iterator mi = mapWallet.find(hashTx); if (mi != mapWallet.end()) { NotifyTransactionChanged(this, hashTx, CT_UPDATED); return true; } } return false; } void CWallet::GetScriptForMining(boost::shared_ptr &script) { boost::shared_ptr rKey(new CReserveKey(this)); CPubKey pubkey; if (!rKey->GetReservedKey(pubkey)) return; script = rKey; script->reserveScript = CScript() << ToByteVector(pubkey) << OP_CHECKSIG; } void CWallet::LockCoin(const COutPoint& output) { AssertLockHeld(cs_wallet); // setLockedCoins setLockedCoins.insert(output); } void CWallet::UnlockCoin(const COutPoint& output) { AssertLockHeld(cs_wallet); // setLockedCoins setLockedCoins.erase(output); } void CWallet::UnlockAllCoins() { AssertLockHeld(cs_wallet); // setLockedCoins setLockedCoins.clear(); } bool CWallet::IsLockedCoin(uint256 hash, unsigned int n) const { AssertLockHeld(cs_wallet); // setLockedCoins COutPoint outpt(hash, n); return (setLockedCoins.count(outpt) > 0); } void CWallet::ListLockedCoins(std::vector& vOutpts) { AssertLockHeld(cs_wallet); // setLockedCoins for (std::set::iterator it = setLockedCoins.begin(); it != setLockedCoins.end(); it++) { COutPoint outpt = (*it); vOutpts.push_back(outpt); } } bool CWallet::HasMasternode(){ auto mnList = deterministicMNManager->GetListForBlock(chainActive.Tip()); AssertLockHeld(cs_wallet); for (const auto &o : setWalletUTXO) { if (mapWallet.count(o.hash)) { const auto &p = mapWallet[o.hash]; if (deterministicMNManager->IsProTxWithCollateral(p.tx, o.n) || mnList.HasMNByCollateral(o)) { return true; } } } return false; } void CWallet::ListProTxCoins(std::vector& vOutpts) { auto mnList = deterministicMNManager->GetListForBlock(chainActive.Tip()); AssertLockHeld(cs_wallet); for (const auto &o : setWalletUTXO) { if (mapWallet.count(o.hash)) { const auto &p = mapWallet[o.hash]; if (deterministicMNManager->IsProTxWithCollateral(p.tx, o.n) || mnList.HasMNByCollateral(o)) { vOutpts.emplace_back(o); } } } } /** @} */ // end of Actions void CWallet::GetKeyBirthTimes(std::map &mapKeyBirth) const { AssertLockHeld(cs_wallet); // mapKeyMetadata mapKeyBirth.clear(); // get birth times for keys with metadata for (const auto& entry : mapKeyMetadata) { if (entry.second.nCreateTime) { mapKeyBirth[entry.first] = entry.second.nCreateTime; } } // map in which we'll infer heights of other keys CBlockIndex *pindexMax = chainActive[std::max(0, chainActive.Height() - 144)]; // the tip can be reorganized; use a 144-block safety margin std::map mapKeyFirstBlock; std::set setKeys; GetKeys(setKeys); BOOST_FOREACH(const CKeyID &keyid, setKeys) { if (mapKeyBirth.count(keyid) == 0) mapKeyFirstBlock[keyid] = pindexMax; } setKeys.clear(); // if there are no such keys, we're done if (mapKeyFirstBlock.empty()) return; // find first block that affects those keys, if there are any left std::vector vAffected; for (std::map::const_iterator it = mapWallet.begin(); it != mapWallet.end(); it++) { // iterate over all wallet transactions... const CWalletTx &wtx = (*it).second; BlockMap::const_iterator blit = mapBlockIndex.find(wtx.hashBlock); if (blit != mapBlockIndex.end() && chainActive.Contains(blit->second)) { // ... which are already in a block int nHeight = blit->second->nHeight; BOOST_FOREACH(const CTxOut &txout, wtx.tx->vout) { // iterate over all their outputs CAffectedKeysVisitor(*this, vAffected).Process(txout.scriptPubKey); BOOST_FOREACH(const CKeyID &keyid, vAffected) { // ... and all their affected keys std::map::iterator rit = mapKeyFirstBlock.find(keyid); if (rit != mapKeyFirstBlock.end() && nHeight < rit->second->nHeight) rit->second = blit->second; } vAffected.clear(); } } } // Extract block timestamps for those keys for (std::map::const_iterator it = mapKeyFirstBlock.begin(); it != mapKeyFirstBlock.end(); it++) mapKeyBirth[it->first] = it->second->GetBlockTime() - 7200; // block times can be 2h off } bool CWallet::AddDestData(const CTxDestination &dest, const std::string &key, const std::string &value) { if (boost::get(&dest)) return false; mapAddressBook[dest].destdata.insert(std::make_pair(key, value)); if (!fFileBacked) return true; return CWalletDB(strWalletFile).WriteDestData(CBitcoinAddress(dest).ToString(), key, value); } bool CWallet::AddDestData(const std::string &dest, const std::string &key, const std::string &value) { if(validateAddress(dest)) { CTxDestination _dest = CBitcoinAddress(dest).Get(); if (boost::get(&_dest)) return false; mapAddressBook[_dest].destdata.insert(std::make_pair(key, value)); } else if (bip47::CPaymentCode::validate(dest)) { mapRAPAddressBook[dest].destdata.insert(std::make_pair(key, value)); } else if (validateSparkAddress(dest)) { mapSparkAddressBook[dest].destdata.insert(std::make_pair(key, value)); } if (!fFileBacked) return true; return CWalletDB(strWalletFile).WriteDestData(dest, key, value); } bool CWallet::EraseDestData(const CTxDestination &dest, const std::string &key) { if (!mapAddressBook[dest].destdata.erase(key)) return false; if (!fFileBacked) return true; return CWalletDB(strWalletFile).EraseDestData(CBitcoinAddress(dest).ToString(), key); } bool CWallet::EraseDestData(const std::string &dest, const std::string &key) { if(validateAddress(dest)) { CTxDestination _dest = CBitcoinAddress(dest).Get(); if (!mapAddressBook[_dest].destdata.erase(key)) return false; } else if (bip47::CPaymentCode::validate(dest)) { if (!mapRAPAddressBook[dest].destdata.erase(key)) return false; } else if (validateSparkAddress(dest)) { if (!mapSparkAddressBook[dest].destdata.erase(key)) return false; } if (!fFileBacked) return true; return CWalletDB(strWalletFile).EraseDestData(dest, key); } bool CWallet::LoadDestData(const CTxDestination &dest, const std::string &key, const std::string &value) { mapAddressBook[dest].destdata.insert(std::make_pair(key, value)); return true; } bool CWallet::LoadDestData(const std::string &dest, const std::string &key, const std::string &value) { if(validateAddress(dest)) { CTxDestination _dest = CBitcoinAddress(dest).Get(); mapAddressBook[_dest].destdata.insert(std::make_pair(key, value)); } else if(bip47::CPaymentCode::validate(dest)) { mapRAPAddressBook[dest].destdata.insert(std::make_pair(key, value)); } else if(validateSparkAddress(dest)) { mapSparkAddressBook[dest].destdata.insert(std::make_pair(key, value)); } return true; } bool CWallet::GetDestData(const CTxDestination &dest, const std::string &key, std::string *value) const { std::map::const_iterator i = mapAddressBook.find(dest); if(i != mapAddressBook.end()) { CAddressBookData::StringMap::const_iterator j = i->second.destdata.find(key); if(j != i->second.destdata.end()) { if(value) *value = j->second; return true; } } return false; } std::string CWallet::GetWalletHelpString(bool showDebug) { std::string strUsage = HelpMessageGroup(_("Wallet options:")); strUsage += HelpMessageOpt("-disablewallet", _("Do not load the wallet and disable wallet RPC calls")); strUsage += HelpMessageOpt("-keypool=", strprintf(_("Set key pool size to (default: %u)"), DEFAULT_KEYPOOL_SIZE)); strUsage += HelpMessageOpt("-fallbackfee=", strprintf(_("A fee rate (in %s/kB) that will be used when fee estimation has insufficient data (default: %s)"), CURRENCY_UNIT, FormatMoney(DEFAULT_FALLBACK_FEE))); strUsage += HelpMessageOpt("-mintxfee=", strprintf(_("Fees (in %s/kB) smaller than this are considered zero fee for transaction creation (default: %s)"), CURRENCY_UNIT, FormatMoney(DEFAULT_TRANSACTION_MINFEE))); strUsage += HelpMessageOpt("-paytxfee=", strprintf(_("Fee (in %s/kB) to add to transactions you send (default: %s)"), CURRENCY_UNIT, FormatMoney(payTxFee.GetFeePerK()))); strUsage += HelpMessageOpt("-rescan", _("Rescan the block chain for missing wallet transactions on startup")); strUsage += HelpMessageOpt("-salvagewallet", _("Attempt to recover private keys from a corrupt wallet on startup")); if (showDebug) strUsage += HelpMessageOpt("-sendfreetransactions", strprintf(_("Send transactions as zero-fee transactions if possible (default: %u)"), DEFAULT_SEND_FREE_TRANSACTIONS)); strUsage += HelpMessageOpt("-spendzeroconfchange", strprintf(_("Spend unconfirmed change when sending transactions (default: %u)"), DEFAULT_SPEND_ZEROCONF_CHANGE)); strUsage += HelpMessageOpt("-txconfirmtarget=", strprintf(_("If paytxfee is not set, include enough fee so transactions begin confirmation on average within n blocks (default: %u)"), DEFAULT_TX_CONFIRM_TARGET)); strUsage += HelpMessageOpt("-usehd", _("Use hierarchical deterministic key generation (HD) after BIP32. Only has effect during wallet creation/first start") + " " + strprintf(_("(default: %u)"), DEFAULT_USE_HD_WALLET)); strUsage += HelpMessageOpt("-usemnemonic", _("Use Mnemonic code for generating deterministic keys. Only has effect during wallet creation/first start") + " " + strprintf(_("(default: %u)"), DEFAULT_USE_MNEMONIC)); strUsage += HelpMessageOpt("-mnemonic=", _("User defined mnemonic for HD wallet (bip39). Only has effect during wallet creation/first start (default: randomly generated)")); strUsage += HelpMessageOpt("-mnemonicpassphrase=", _("User defined mnemonic passphrase for HD wallet (BIP39). Only has effect during wallet creation/first start (default: empty string)")); strUsage += HelpMessageOpt("-hdseed=", _("User defined seed for HD wallet (should be in hex). Only has effect during wallet creation/first start (default: randomly generated)")); strUsage += HelpMessageOpt("-batching", _("In case of sync/reindex verifies privacy (Spark) proofs with batch verification, default: true")); strUsage += HelpMessageOpt("-mobile", _("Use this argument when you want to keep additional data in block index for mobile api, default: false")); strUsage += HelpMessageOpt("-walletrbf", strprintf(_("Send transactions with full-RBF opt-in enabled (default: %u)"), DEFAULT_WALLET_RBF)); strUsage += HelpMessageOpt("-upgradewallet", _("Upgrade wallet to latest format on startup")); strUsage += HelpMessageOpt("-wallet=", _("Specify wallet file (within data directory)") + " " + strprintf(_("(default: %s)"), DEFAULT_WALLET_DAT)); strUsage += HelpMessageOpt("-walletbroadcast", _("Make the wallet broadcast transactions") + " " + strprintf(_("(default: %u)"), DEFAULT_WALLETBROADCAST)); strUsage += HelpMessageOpt("-walletnotify=", _("Execute command when a wallet transaction changes (%s in cmd is replaced by TxID, %t is replaced by transaction type: 'spark' or 'regular')")); strUsage += HelpMessageOpt("-zapwalletmints", _("Delete all Sigma mints and only recover those parts of the blockchain through -reindex on startup")); strUsage += HelpMessageOpt("-zapwallettxes=", _("Delete all wallet transactions and only recover those parts of the blockchain through -rescan on startup") + " " + _("(1 = keep tx meta data e.g. account owner and payment request information, 2 = drop tx meta data)")); if (showDebug) { strUsage += HelpMessageGroup(_("Wallet debugging/testing options:")); strUsage += HelpMessageOpt("-dblogsize=", strprintf("Flush wallet database activity from memory to disk log every megabytes (default: %u)", DEFAULT_WALLET_DBLOGSIZE)); strUsage += HelpMessageOpt("-flushwallet", strprintf("Run a thread to flush wallet periodically (default: %u)", DEFAULT_FLUSHWALLET)); strUsage += HelpMessageOpt("-privdb", strprintf("Sets the DB_PRIVATE flag in the wallet db environment (default: %u)", DEFAULT_WALLET_PRIVDB)); strUsage += HelpMessageOpt("-walletrejectlongchains", strprintf(_("Wallet will not create transactions that violate mempool chain limits (default: %u)"), DEFAULT_WALLET_REJECT_LONG_CHAINS)); } return strUsage; } CWallet* CWallet::CreateWalletFromFile(const std::string walletFile) { if (GetBoolArg("-zapwalletmints", false) || GetBoolArg("-reindex", false)) { uiInterface.InitMessage(_("Zapping all Sigma mints from wallet...")); CWallet *tempWallet = new CWallet(walletFile); DBErrors nZapSparkMintRet = tempWallet->ZapSparkMints(); if (nZapSparkMintRet != DB_LOAD_OK) { InitError(strprintf(_("Error loading %s: Wallet corrupted"), walletFile)); return NULL; } delete tempWallet; tempWallet = NULL; } // needed to restore wallet transaction meta data after -zapwallettxes std::vector vWtx; if (GetBoolArg("-zapwallettxes", false)) { uiInterface.InitMessage(_("Zapping all transactions from wallet...")); CWallet *tempWallet = new CWallet(walletFile); DBErrors nZapWalletRet = tempWallet->ZapWalletTx(vWtx); if (nZapWalletRet != DB_LOAD_OK) { InitError(strprintf(_("Error loading %s: Wallet corrupted"), walletFile)); return NULL; } delete tempWallet; tempWallet = NULL; } uiInterface.InitMessage(_("Loading wallet...")); int64_t nStart = GetTimeMillis(); bool fFirstRun = true; bool fRecoverMnemonic = false; CWallet *walletInstance = new CWallet(walletFile); pwalletMain = walletInstance; DBErrors nLoadWalletRet = walletInstance->LoadWallet(fFirstRun); if (nLoadWalletRet != DB_LOAD_OK) { if (nLoadWalletRet == DB_CORRUPT) { InitError(strprintf(_("Error loading %s: Wallet corrupted"), walletFile)); return NULL; } else if (nLoadWalletRet == DB_NONCRITICAL_ERROR) { InitWarning(strprintf(_("Error reading %s! All keys read correctly, but transaction data" " or address book entries might be missing or incorrect."), walletFile)); } else if (nLoadWalletRet == DB_TOO_NEW) { InitError(strprintf(_("Error loading %s: Wallet requires newer version of %s"), walletFile, _(PACKAGE_NAME))); return NULL; } else if (nLoadWalletRet == DB_NEED_REWRITE) { InitError(strprintf(_("Wallet needed to be rewritten: restart %s to complete"), _(PACKAGE_NAME))); return NULL; } else { InitError(strprintf(_("Error loading %s"), walletFile)); return NULL; } } if (GetBoolArg("-upgradewallet", fFirstRun)) { int nMaxVersion = GetArg("-upgradewallet", 0); if (nMaxVersion == 0) // the -upgradewallet without argument case { LogPrintf("Performing wallet upgrade to %i\n", FEATURE_LATEST); nMaxVersion = CLIENT_VERSION; walletInstance->SetMinVersion(FEATURE_LATEST); // permanently upgrade the wallet immediately } else LogPrintf("Allowing wallet upgrade up to %i\n", nMaxVersion); if (nMaxVersion < walletInstance->GetVersion()) { InitError(_("Cannot downgrade wallet")); return NULL; } walletInstance->SetMaxVersion(nMaxVersion); } if (fFirstRun) { // Create new keyUser and set as default key if (GetBoolArg("-usehd", DEFAULT_USE_HD_WALLET) && !walletInstance->IsHDEnabled()) { if(GetBoolArg("-usemnemonic", DEFAULT_USE_MNEMONIC)) { if (GetArg("-mnemonicpassphrase", "").size() > 256) { throw std::runtime_error(std::string(__func__) + ": Mnemonic passphrase is too long, must be at most 256 characters"); } // generate a new HD chain walletInstance->GenerateNewMnemonic(); walletInstance->SetMinVersion(FEATURE_HD); /* set rescan to true. * if blockchain data is not present it has no effect, but it's needed for a mnemonic restore where chain data is present. */ SoftSetBoolArg("-rescan", true); fRecoverMnemonic = true; }else{ // generate a new master key CPubKey masterPubKey = walletInstance->GenerateNewHDMasterKey(); if (!walletInstance->SetHDMasterKey(masterPubKey, CHDChain().VERSION_WITH_BIP44)) throw std::runtime_error(std::string(__func__) + ": Storing master key failed"); } } CPubKey newDefaultKey; if (walletInstance->GetKeyFromPool(newDefaultKey)) { walletInstance->SetDefaultKey(newDefaultKey); if (!walletInstance->SetAddressBook(walletInstance->vchDefaultKey.GetID(), "", "receive")) { InitError(_("Cannot write default address") += "\n"); return NULL; } } walletInstance->SetBestChain(chainActive.GetLocator()); } else if (IsArgSet("-usehd")) { bool useHD = GetBoolArg("-usehd", DEFAULT_USE_HD_WALLET); if (walletInstance->IsHDEnabled() && !useHD) { InitError(strprintf(_("Error loading %s: You can't disable HD on a already existing HD wallet"), walletFile)); return NULL; } if (!walletInstance->IsHDEnabled() && useHD) { InitError(strprintf(_("Error loading %s: You can't enable HD on a already existing non-HD wallet"), walletFile)); return NULL; } } LogPrintf(" wallet %15dms\n", GetTimeMillis() - nStart); if (pwalletMain->IsHDSeedAvailable()) { // if it is first run, we need to generate the full key set for spark, if not we are loading spark wallet from db walletInstance->sparkWallet = std::make_unique(pwalletMain->strWalletFile); spark::Address address = walletInstance->sparkWallet->getDefaultAddress(); std::string addrStr = address.encode(spark::GetNetworkType()); if (walletInstance->mapSparkAddressBook.count(addrStr) == 0) { if (!walletInstance->SetSparkAddressBook(addrStr, "", "receive")) { InitError(_("Cannot write default spark address") += "\n"); return NULL; } } } walletInstance->bip47wallet = std::make_shared(walletInstance->vchDefaultKey.GetHash()); walletInstance->LoadBip47Wallet(); if (fFirstRun) { int const defaultPcodeNumber = std::stoi(GetArg("-defaultrapaddressnumber", "0")); for(int i = 0; i < defaultPcodeNumber; ++i) walletInstance->GeneratePcode("Autogenerated RAP address " + std::to_string(i)); } RegisterValidationInterface(walletInstance); // Try to top up keypool. No-op if the wallet is locked. walletInstance->TopUpKeyPool(); CBlockIndex *pindexRescan = chainActive.Tip(); if (GetBoolArg("-rescan", false)) pindexRescan = chainActive.Genesis(); else { CWalletDB walletdb(walletFile); CBlockLocator locator; if (walletdb.ReadBestBlock(locator)) pindexRescan = FindForkInGlobalIndex(chainActive, locator); else pindexRescan = chainActive.Genesis(); } if (chainActive.Tip() && chainActive.Tip() != pindexRescan) { //We can't rescan beyond non-pruned blocks, stop and throw an error //this might happen if a user uses a old wallet within a pruned node // or if he ran -disablewallet for a longer time, then decided to re-enable if (fPruneMode) { CBlockIndex *block = chainActive.Tip(); while (block && block->pprev && (block->pprev->nStatus & BLOCK_HAVE_DATA) && block->pprev->nTx > 0 && pindexRescan != block) block = block->pprev; if (pindexRescan != block) { InitError(_("Prune: last wallet synchronisation goes beyond pruned data. You need to -reindex (download the whole blockchain again in case of pruned node)")); return NULL; } } if (!(GetBoolArg("-newwallet", false))) {uiInterface.InitMessage(_("Rescanning..."));} nStart = GetTimeMillis(); walletInstance->ScanForWalletTransactions(pindexRescan, true, fRecoverMnemonic); if (!(GetBoolArg("-newwallet", false))) {LogPrintf(" rescan %15dms\n", GetTimeMillis() - nStart);} walletInstance->SetBestChain(chainActive.GetLocator()); CWalletDB::IncrementUpdateCounter(); // Restore wallet transaction metadata after -zapwallettxes=1 if (GetBoolArg("-zapwallettxes", false)) { std::string zwtValue = GetArg("-zapwallettxes", "1"); if(zwtValue != "2") { CWalletDB walletdb(walletFile); BOOST_FOREACH(const CWalletTx &wtxOld, vWtx) { uint256 hash = wtxOld.GetHash(); std::map::iterator mi = walletInstance->mapWallet.find(hash); if (mi != walletInstance->mapWallet.end()) { const CWalletTx *copyFrom = &wtxOld; CWalletTx *copyTo = &mi->second; copyTo->mapValue = copyFrom->mapValue; copyTo->vOrderForm = copyFrom->vOrderForm; copyTo->nTimeReceived = copyFrom->nTimeReceived; copyTo->nTimeSmart = copyFrom->nTimeSmart; copyTo->fFromMe = copyFrom->fFromMe; copyTo->strFromAccount = copyFrom->strFromAccount; copyTo->nOrderPos = copyFrom->nOrderPos; walletdb.WriteTx(*copyTo); } } } } } // -wcdate is only for the initial restore scan; later rescans must use their own start block. ForceSetArg("-wcdate", ""); // recover addressbook if (fFirstRun) { for (std::map::iterator it = walletInstance->mapWallet.begin(); it != walletInstance->mapWallet.end(); ++it) { for (uint32_t i = 0; i < (*it).second.tx->vout.size(); i++) { const auto& txout = (*it).second.tx->vout[i]; if(txout.scriptPubKey.IsMint() || (*it).second.changes.count(i)) continue; if (!walletInstance->IsMine(txout)) continue; CTxDestination addr; if(!ExtractDestination(txout.scriptPubKey, addr)) continue; if (walletInstance->mapAddressBook.count(addr) == 0) walletInstance->SetAddressBook(addr, "", "receive"); } } } walletInstance->SetBroadcastTransactions(GetBoolArg("-walletbroadcast", DEFAULT_WALLETBROADCAST)); { LOCK(walletInstance->cs_wallet); LogPrintf("setKeyPool.size() = %u\n", walletInstance->GetKeyPoolSize()); LogPrintf("mapWallet.size() = %u\n", walletInstance->mapWallet.size()); LogPrintf("mapAddressBook.size() = %u\n", walletInstance->mapAddressBook.size()); } return walletInstance; } bool CWallet::InitLoadWallet() { if (GetBoolArg("-disablewallet", DEFAULT_DISABLE_WALLET)) { pwalletMain = NULL; LogPrintf("Wallet disabled!\n"); return true; } std::string walletFile = GetArg("-wallet", DEFAULT_WALLET_DAT); if (walletFile.find_first_of("/\\") != std::string::npos) { return InitError(_("-wallet parameter must only specify a filename (not a path)")); } else if (SanitizeString(walletFile, SAFE_CHARS_FILENAME) != walletFile) { return InitError(_("Invalid characters in -wallet filename")); } CWallet * const pwallet = CreateWalletFromFile(walletFile); if (!pwallet) { return false; } pwalletMain = pwallet; return true; } std::atomic CWallet::fFlushThreadRunning(false); void CWallet::postInitProcess(boost::thread_group& threadGroup) { // Add wallet transactions that aren't already in a block to mempool // Do this here as mempool requires genesis block to be loaded ReacceptWalletTransactions(); // Run a thread to flush wallet periodically if (!CWallet::fFlushThreadRunning.exchange(true)) { threadGroup.create_thread(ThreadFlushWalletDB); } } bool CWallet::ParameterInteraction() { if (GetBoolArg("-disablewallet", DEFAULT_DISABLE_WALLET)) return true; if (GetBoolArg("-blocksonly", DEFAULT_BLOCKSONLY) && SoftSetBoolArg("-walletbroadcast", false)) { LogPrintf("%s: parameter interaction: -blocksonly=1 -> setting -walletbroadcast=0\n", __func__); } if (GetBoolArg("-salvagewallet", false) && SoftSetBoolArg("-rescan", true)) { // Rewrite just private keys: rescan to find transactions LogPrintf("%s: parameter interaction: -salvagewallet=1 -> setting -rescan=1\n", __func__); } // -zapwallettx implies a rescan if (GetBoolArg("-zapwallettxes", false) && SoftSetBoolArg("-rescan", true)) { LogPrintf("%s: parameter interaction: -zapwallettxes= -> setting -rescan=1\n", __func__); } if (GetBoolArg("-sysperms", false)) return InitError("-sysperms is not allowed in combination with enabled wallet functionality"); if (GetArg("-prune", 0) && GetBoolArg("-rescan", false)) return InitError(_("Rescans are not possible in pruned mode. You will need to use -reindex which will download the whole blockchain again.")); if (::minRelayTxFee.GetFeePerK() > HIGH_TX_FEE_PER_KB) InitWarning(AmountHighWarn("-minrelaytxfee") + " " + _("The wallet will avoid paying less than the minimum relay fee.")); if (IsArgSet("-mintxfee")) { CAmount n = 0; if (!ParseMoney(GetArg("-mintxfee", ""), n) || 0 == n) return InitError(AmountErrMsg("mintxfee", GetArg("-mintxfee", ""))); if (n > HIGH_TX_FEE_PER_KB) InitWarning(AmountHighWarn("-mintxfee") + " " + _("This is the minimum transaction fee you pay on every transaction.")); CWallet::minTxFee = CFeeRate(n); } if (IsArgSet("-fallbackfee")) { CAmount nFeePerK = 0; if (!ParseMoney(GetArg("-fallbackfee", ""), nFeePerK)) return InitError(strprintf(_("Invalid amount for -fallbackfee=: '%s'"), GetArg("-fallbackfee", ""))); /* if (nFeePerK > HIGH_TX_FEE_PER_KB) InitWarning(AmountHighWarn("-fallbackfee") + " " + _("This is the transaction fee you may pay when fee estimates are not available.")); */ CWallet::fallbackFee = CFeeRate(nFeePerK); } if (IsArgSet("-paytxfee")) { CAmount nFeePerK = 0; if (!ParseMoney(GetArg("-paytxfee", ""), nFeePerK)) return InitError(AmountErrMsg("paytxfee", GetArg("-paytxfee", ""))); /* if (nFeePerK > HIGH_TX_FEE_PER_KB) InitWarning(AmountHighWarn("-paytxfee") + " " + _("This is the transaction fee you will pay if you send a transaction.")); */ payTxFee = CFeeRate(nFeePerK, 1000); if (payTxFee < ::minRelayTxFee) { return InitError(strprintf(_("Invalid amount for -paytxfee=: '%s' (must be at least %s)"), GetArg("-paytxfee", ""), ::minRelayTxFee.ToString())); } } if (IsArgSet("-maxtxfee")) { CAmount nMaxFee = 0; if (!ParseMoney(GetArg("-maxtxfee", ""), nMaxFee)) return InitError(AmountErrMsg("maxtxfee", GetArg("-maxtxfee", ""))); /* if (nMaxFee > HIGH_MAX_TX_FEE) InitWarning(_("-maxtxfee is set very high! Fees this large could be paid on a single transaction.")); */ maxTxFee = nMaxFee; if (CFeeRate(maxTxFee, 1000) < ::minRelayTxFee) { return InitError(strprintf(_("Invalid amount for -maxtxfee=: '%s' (must be at least the minrelay fee of %s to prevent stuck transactions)"), GetArg("-maxtxfee", ""), ::minRelayTxFee.ToString())); } } if (IsArgSet("-mininput")) { if (!ParseMoney(GetArg("-mininput", ""), nMinimumInputValue)) return InitError(strprintf(_("Invalid amount for -mininput=: '%s'"), GetArg("-mininput", "").c_str())); } nTxConfirmTarget = GetArg("-txconfirmtarget", DEFAULT_TX_CONFIRM_TARGET); bSpendZeroConfChange = GetBoolArg("-spendzeroconfchange", DEFAULT_SPEND_ZEROCONF_CHANGE); fSendFreeTransactions = GetBoolArg("-sendfreetransactions", DEFAULT_SEND_FREE_TRANSACTIONS); fWalletRbf = GetBoolArg("-walletrbf", DEFAULT_WALLET_RBF); if (fSendFreeTransactions && GetArg("-limitfreerelay", DEFAULT_LIMITFREERELAY) <= 0) return InitError("Creation of free transactions with their relay disabled is not supported."); return true; } bool CWallet::BackupWallet(const std::string& strDest) { if (!fFileBacked) return false; while (true) { { LOCK(bitdb.cs_db); if (!bitdb.mapFileUseCount.count(strWalletFile) || bitdb.mapFileUseCount[strWalletFile] == 0) { // Flush log data to the dat file bitdb.CloseDb(strWalletFile); bitdb.CheckpointLSN(strWalletFile); bitdb.mapFileUseCount.erase(strWalletFile); // Copy wallet file boost::filesystem::path pathSrc = GetDataDir() / strWalletFile; boost::filesystem::path pathDest(strDest); if (boost::filesystem::is_directory(pathDest)) pathDest /= strWalletFile; try { #if BOOST_VERSION >= 104000 const auto copyOptions = boost::filesystem::copy_options::overwrite_existing; boost::filesystem::copy(pathSrc, pathDest, copyOptions); #else boost::filesystem::copy_file(pathSrc, pathDest); #endif LogPrintf("copied %s to %s\n", strWalletFile, pathDest.string()); return true; } catch (const boost::filesystem::filesystem_error& e) { LogPrintf("error copying %s to %s - %s\n", strWalletFile, pathDest.string(), e.what()); return false; } } } MilliSleep(100); } return false; } bip47::CPaymentCode CWallet::GeneratePcode(std::string const & label) { if (!bip47wallet) throw WalletError("BIP47 wallet was not created during the initialization"); bip47::CAccountReceiver & newAcc = bip47wallet->createReceivingAccount(label); { bip47::MyAddrContT addrs = newAcc.getMyNextAddresses(); LOCK(cs_wallet); for(bip47::MyAddrContT::value_type const & addr : addrs) { AddKey(addr.second); } } CWalletDB(strWalletFile).WriteBip47Account(newAcc); NotifyPcodeCreated(bip47::CPaymentCodeDescription(newAcc.getAccountNum(), newAcc.getMyPcode(), newAcc.getLabel(), newAcc.getMyPcode().getNotificationAddress(), bip47::CPaymentCodeSide::Receiver)); return newAcc.getMyPcode(); } std::vector CWallet::ListPcodes() { std::vector result; if (!bip47wallet) return result; bip47wallet->enumerateReceivers( [&result](bip47::CAccountReceiver const & acc)->bool { result.emplace_back(acc.getAccountNum(), acc.getMyPcode(), acc.getLabel(), acc.getMyNotificationAddress(), bip47::CPaymentCodeSide::Receiver); return true; } ); return result; } bip47::CPaymentChannel & CWallet::SetupPchannel(bip47::CPaymentCode const & theirPcode) { if (!bip47wallet) throw WalletError("BIP47 wallet was not created during the initialization"); bip47::CAccountSender & sender = bip47wallet->provideSendingAccount(theirPcode); CWalletDB(strWalletFile).WriteBip47Account(sender); return sender.getPaymentChannel(); } void CWallet::SetNotificationTxId(bip47::CPaymentCode const & theirPcode, uint256 const & txid) { if (!bip47wallet) throw WalletError("BIP47 wallet was not created during the initialization"); bip47::CAccountSender & sender = bip47wallet->provideSendingAccount(theirPcode); sender.setNotificationTxId(txid); CWalletDB(strWalletFile).WriteBip47Account(sender); } namespace { CBitcoinAddress HandleTheirNextAddress(bip47::CWallet & wallet, std::string const & strWalletFile, bip47::CPaymentCode const & theirPcode, bool storeNextAddress) { boost::optional existingAcc; wallet.enumerateSenders( [&theirPcode, &existingAcc](bip47::CAccountSender & acc)->bool { if(acc.getTheirPcode() == theirPcode) { existingAcc.emplace(&acc); return false; } return true; } ); if(!existingAcc) throw std::runtime_error("There is no account setup for payment code " + theirPcode.toString()); CBitcoinAddress result; if(storeNextAddress) { result = existingAcc.get()->generateTheirNextSecretAddress(); LogBip47("Sending to secret address: %s\n", result.ToString()); } else { result = existingAcc.get()->getTheirNextSecretAddress(); } CWalletDB(strWalletFile).WriteBip47Account(*existingAcc.get()); return result; } } CBitcoinAddress CWallet::GetTheirNextAddress(bip47::CPaymentCode const & theirPcode) const { if (!bip47wallet) throw WalletError("BIP47 wallet was not created during the initialization"); return HandleTheirNextAddress(*bip47wallet, strWalletFile, theirPcode, false); } CBitcoinAddress CWallet::GenerateTheirNextAddress(bip47::CPaymentCode const & theirPcode) { if (!bip47wallet) throw WalletError("BIP47 wallet was not created during the initialization"); return HandleTheirNextAddress(*bip47wallet, strWalletFile, theirPcode, true); } void CWallet::LoadBip47Wallet() { CWalletDB(strWalletFile).LoadBip47Accounts(*bip47wallet); } std::shared_ptr CWallet::GetBip47Wallet() const { return bip47wallet; } boost::optional CWallet::FindPcode(bip47::CPaymentCode const & pcode) const { boost::optional result; if (!bip47wallet) return result; bip47wallet->enumerateReceivers( [&pcode, &result](bip47::CAccountReceiver & rec)->bool { if(rec.getMyPcode() == pcode) { result.emplace(rec.getAccountNum(), rec.getMyPcode(), rec.getLabel(), rec.getMyPcode().getNotificationAddress(), bip47::CPaymentCodeSide::Receiver); return false; } for(bip47::CPaymentChannel const & channel : rec.getPchannels()) { if(channel.getTheirPcode() == pcode) { result.emplace(rec.getAccountNum(), rec.getMyPcode(), rec.getLabel(), rec.getMyPcode().getNotificationAddress(), bip47::CPaymentCodeSide::Receiver); return false; } } return true; } ); bip47wallet->enumerateSenders( [&pcode, &result, this](bip47::CAccountSender & sender)->bool { if(sender.getTheirPcode() == pcode) { std::string label = GetSendingPcodeLabel(sender.getTheirPcode()); result.emplace(sender.getAccountNum(), sender.getTheirPcode(), label, sender.getTheirPcode().getNotificationAddress(), bip47::CPaymentCodeSide::Sender); return false; } return true; } ); return result; } boost::optional CWallet::FindPcode(CBitcoinAddress const & address) const { boost::optional result; if (!bip47wallet) return result; bip47wallet->enumerateReceivers( [&address, &result](bip47::CAccountReceiver & rec)->bool { bip47::MyAddrContT addrs = rec.getMyUsedAddresses(); if (std::find_if(addrs.begin(), addrs.end(), bip47::FindByAddress(address)) != addrs.end()) { result.emplace(rec.getAccountNum(), rec.getMyPcode(), rec.getLabel(), rec.getMyPcode().getNotificationAddress(), bip47::CPaymentCodeSide::Receiver); return false; } addrs = rec.getMyNextAddresses(); if (std::find_if(addrs.begin(), addrs.end(), bip47::FindByAddress(address)) != addrs.end()) { result.emplace(rec.getAccountNum(), rec.getMyPcode(), rec.getLabel(), rec.getMyPcode().getNotificationAddress(), bip47::CPaymentCodeSide::Receiver); return false; } return true; } ); bip47wallet->enumerateSenders( [&address, &result, this](bip47::CAccountSender & sender)->bool { bip47::TheirAddrContT addrs = sender.getTheirUsedAddresses(); if (std::find(addrs.begin(), addrs.end(), address) != addrs.end()) { std::string label = GetSendingPcodeLabel(sender.getTheirPcode()); result.emplace(sender.getAccountNum(), sender.getTheirPcode(), label, sender.getTheirPcode().getNotificationAddress(), bip47::CPaymentCodeSide::Sender); return false; } if (address == sender.getTheirNextSecretAddress() || address == sender.getTheirPcode().getNotificationAddress()) { std::string label = GetSendingPcodeLabel(sender.getTheirPcode()); result.emplace(sender.getAccountNum(), sender.getTheirPcode(), label, sender.getTheirPcode().getNotificationAddress(), bip47::CPaymentCodeSide::Sender); return false; } return true; } ); return result; } bip47::CAccountReceiver const * CWallet::AddressUsed(CBitcoinAddress const & address) { bip47::CAccountReceiver const * result = nullptr; if(!bip47wallet) return result; bip47wallet->enumerateReceivers( [&address, &result](bip47::CAccountReceiver & rec)->bool { bip47::MyAddrContT addrs = rec.getMyNextAddresses(); if (std::find_if(addrs.begin(), addrs.end(), bip47::FindByAddress(address)) != addrs.end()) { rec.addressUsed(address); result = &rec; return false; } return true; } ); if (result) CWalletDB(strWalletFile).WriteBip47Account(*result); return result; } void CWallet::HandleBip47Transaction(CWalletTx const & wtx) { bip47::Bytes masked = bip47::utils::GetMaskedPcode(wtx.tx); CKey key; bip47::CAccountReceiver * accFound = nullptr; int nRequired = 0; std::vector addresses; txnouttype typeRet = TX_NONSTANDARD; std::vector::const_iterator ijsplit; std::vector::const_iterator iregout; bool success = false; if (masked.empty()) goto notifTxExit; ijsplit = std::find_if(wtx.tx->vin.begin(), wtx.tx->vin.end(), [](CTxIn const & in){ return in.scriptSig.IsLelantusJoinSplit(); }); if(ijsplit == wtx.tx->vin.end()) { LogBip47("Joinsplit input was not found in a potential notification tx: %s\n", wtx.tx->GetHash().ToString()); goto notifTxExit; } iregout = std::find_if(wtx.tx->vout.begin(), wtx.tx->vout.end(), [](CTxOut const & out){ return out.scriptPubKey[0] != OP_RETURN && !out.scriptPubKey.IsLelantusJMint(); }); if(iregout == wtx.tx->vout.end()) { LogBip47("Regular out was not found in a potential notification tx: %s\n", wtx.tx->GetHash().ToString()); goto notifTxExit; } if(!ExtractDestinations(iregout->scriptPubKey, typeRet, addresses, nRequired)) { LogBip47("Cannot extract destinations for tx: %s\n", wtx.tx->GetHash().ToString()); goto notifTxExit; } bip47wallet->enumerateReceivers( [&key, &addresses, &accFound](bip47::CAccountReceiver & acc)->bool { for (CBitcoinAddress addr : addresses) { if(acc.getMyNotificationAddress() == addr) { key = acc.getMyNextAddresses()[0].second; accFound = &acc; return false; } } return true; } ); if(!accFound) { LogBip47("There was no account set up to receive payments on address: %s\n", CBitcoinAddress(addresses[0]).ToString()); goto notifTxExit; } if(!accFound->acceptMaskedPayload(masked, *wtx.tx)){ LogBip47("Could not accept this masked payload: %s\n", HexStr(masked)); goto notifTxExit; } success = true; notifTxExit: if (success) { LogBip47("The payment code has been accepted: %s\n", accFound->lastPcode().toString()); HandleSecretAddresses(*this, *accFound); CWalletDB(strWalletFile).WriteBip47Account(*accFound); LockCoin(COutPoint(wtx.tx->GetHash(), std::distance(wtx.tx->vout.begin(), iregout))); //Locking the notif tx output to be spent only manually } else { // Checking if it uses a bip47 address for (CTxOut const & out : wtx.tx->vout) { std::vector addresses; txnouttype typeRet = TX_NONSTANDARD; int nRequired = 0; if (ExtractDestinations(out.scriptPubKey, typeRet, addresses, nRequired)) { for (CBitcoinAddress addr : addresses) { bip47::CAccountReceiver const * rec = AddressUsed(addr); if (rec) { HandleSecretAddresses(*this, *rec); } } } } } } void CWallet::HandleSparkTransaction(CWalletTx const & wtx) { if (!wtx.tx->IsSparkTransaction() || !sparkWallet) return; uint256 txHash = wtx.GetHash(); CWalletDB walletdb(strWalletFile); // get spend linking tags and add to spark wallet if (wtx.tx->IsSparkSpend()) { std::vector lTags; lTags = spark::GetSparkUsedTags(*wtx.tx); for (const auto& lTag : lTags) { sparkWallet->UpdateSpendState(lTag, txHash); } } // get spark coins and add into wallet std::vector coins = spark::GetSparkMintCoins(*wtx.tx); sparkWallet->UpdateMintState(coins, txHash, walletdb); } void CWallet::LabelSendingPcode(bip47::CPaymentCode const & pcode_, std::string const & label, bool remove) { std::string const pcodeLbl = bip47::PcodeLabel() + pcode_.toString(); if (label.empty()) remove = true; CWalletDB walletDb(strWalletFile); if (remove) { walletDb.EraseKV(pcodeLbl); LOCK(cs_wallet); mapCustomKeyValues.erase(pcodeLbl); } else { std::multimap::iterator iter = mapCustomKeyValues.find(pcodeLbl); if (iter == mapCustomKeyValues.end()) { LOCK(cs_wallet); mapCustomKeyValues.insert(std::make_pair(pcodeLbl, label)); } else { if (iter->second == label) return; iter->second = label; } walletDb.EraseKV(pcodeLbl); walletDb.WriteKV(pcodeLbl, label); } NotifyPcodeLabeled(pcode_.toString(), label, remove); } std::string CWallet::GetSendingPcodeLabel(bip47::CPaymentCode const & pcode) const { std::string const pcodeLbl = bip47::PcodeLabel() + pcode.toString(); LOCK(cs_wallet); std::multimap::const_iterator iter = mapCustomKeyValues.find(pcodeLbl); if(iter == mapCustomKeyValues.end()) return ""; return iter->second; } void CWallet::LabelReceivingPcode(bip47::CPaymentCode const & pcode, std::string const & label) { if (!bip47wallet) return; LOCK(cs_wallet); bip47::CAccountReceiver * result = nullptr; bip47wallet->enumerateReceivers( [&result, &pcode](bip47::CAccountReceiver & rec)->bool { if(rec.getMyPcode() == pcode) { result = &rec; return false; } return true; } ); if(!result) return; result->setLabel(label); CWalletDB(strWalletFile).WriteBip47Account(*result); } size_t CWallet::SetUsedAddressNumber(bip47::CPaymentCode const & pcode, size_t number) { boost::optional resultSnd, resutRec; bip47wallet->enumerateSenders( [&pcode, &number, &resultSnd](bip47::CAccountSender & sender)->bool { if(sender.getTheirPcode() == pcode) { resultSnd.emplace(sender.setTheirUsedAddressNumber(number)); return false; } return true; } ); bip47::CAccountReceiver * receiver; bip47wallet->enumerateReceivers( [&pcode, &number, &resutRec, &receiver](bip47::CAccountReceiver & rec)->bool { resutRec = rec.setMyUsedAddressNumber(pcode, number); if(resutRec) { receiver = &rec; return false; } return true; } ); if(resutRec) { HandleSecretAddresses(*this, *receiver); return *resutRec; } if(resultSnd) return *resultSnd; return 0; } void CWallet::NotifyTransactionLock(const CTransaction &tx) { LOCK(cs_wallet); // Only notify UI if this transaction is in this wallet std::map::const_iterator mi = mapWallet.find(tx.GetHash()); if (mi != mapWallet.end()){ NotifyISLockReceived(); } } void CWallet::NotifyChainLock(const CBlockIndex* pindexChainLock) { NotifyChainLockReceived(pindexChainLock->nHeight); } /******************************************************************************/ /* */ /* CKeyPool */ /* */ /******************************************************************************/ CKeyPool::CKeyPool() { nTime = GetTime(); } CKeyPool::CKeyPool(const CPubKey& vchPubKeyIn) { nTime = GetTime(); vchPubKey = vchPubKeyIn; } CWalletKey::CWalletKey(int64_t nExpires) { nTimeCreated = (nExpires ? GetTime() : 0); nTimeExpires = nExpires; } void CMerkleTx::SetMerkleBranch(const CBlockIndex* pindex, int posInBlock) { // Update the tx's hashBlock hashBlock = pindex->GetBlockHash(); // set the position of the transaction in the block nIndex = posInBlock; } int CMerkleTx::GetDepthInMainChain(const CBlockIndex *&pindexRet, bool enableIX) const { int nResult; if (hashUnset()) nResult = 0; else { AssertLockHeld(cs_main); // Find the block it claims to be in BlockMap::iterator mi = mapBlockIndex.find(hashBlock); if (mi == mapBlockIndex.end()) nResult = 0; else { CBlockIndex *pindex = (*mi).second; if (!pindex || !chainActive.Contains(pindex)) nResult = 0; else { pindexRet = pindex; nResult = ((nIndex == -1) ? (-1) : 1) * (chainActive.Height() - pindex->nHeight + 1); if (nResult == 0 && !mempool.exists(GetHash())) return -1; // Not in chain, not in mempool } } } return nResult; } bool CMerkleTx::IsLockedByLLMQInstantSend() const { return llmq::quorumInstantSendManager->IsLocked(GetHash()); } bool CMerkleTx::IsChainLocked() const { AssertLockHeld(cs_main); BlockMap::iterator mi = mapBlockIndex.find(hashBlock); if (mi != mapBlockIndex.end() && mi->second != nullptr) { return llmq::chainLocksHandler->HasChainLock(mi->second->nHeight, hashBlock); } return false; } int CMerkleTx::GetDepthInMainChain(const CBlockIndex* &pindexRet) const { if (hashUnset()) return 0; AssertLockHeld(cs_main); // Find the block it claims to be in BlockMap::iterator mi = mapBlockIndex.find(hashBlock); if (mi == mapBlockIndex.end()) return 0; CBlockIndex* pindex = (*mi).second; if (!pindex || !chainActive.Contains(pindex)) return 0; pindexRet = pindex; return ((nIndex == -1) ? (-1) : 1) * (chainActive.Height() - pindex->nHeight + 1); } int CMerkleTx::GetBlocksToMaturity() const { if (!IsCoinBase()) return 0; return std::max(0, (COINBASE_MATURITY+1) - GetDepthInMainChain()); } bool CMerkleTx::AcceptToMemoryPool(const CAmount &nAbsurdFee, CValidationState &state) { if (GetBoolArg("-dandelion", true)) { bool res = ::AcceptToMemoryPool( txpools.getStemTxPool(), state, tx, false, NULL, /* pfMissingInputs */ NULL, false, /* fOverrideMempoolLimit */ nAbsurdFee, true, false /* markFiroSpendTransactionSerial */ ); if (!res) { LogPrintf( "CMerkleTx::AcceptToMemoryPool, failed to add txn %s to dandelion stempool: %s.\n", GetHash().ToString(), state.GetRejectReason()); } return res; } else { // Changes to mempool should also be made to Dandelion stempool return ::AcceptToMemoryPool( txpools, state, tx, false, NULL, /* pfMissingInputs */ NULL, false, /* fOverrideMempoolLimit */ nAbsurdFee, true, true); } } void ShutdownWallet() { if (pwalletMain) { if (pwalletMain->sparkWallet) { pwalletMain->sparkWallet->FinishTasks(); } } } bool CWallet::CreateSparkMintTransactions( const std::vector& outputs, std::vector>& wtxAndFee, CAmount& nAllFeeRet, std::list& reservekeys, int& nChangePosInOut, bool subtractFeeFromAmount, std::string& strFailReason, bool fSplit, const CCoinControl *coinControl, bool autoMintAll, const std::vector* transparentRecipients) { return sparkWallet->CreateSparkMintTransactions( outputs, wtxAndFee, nAllFeeRet, reservekeys, nChangePosInOut, subtractFeeFromAmount, strFailReason, fSplit, coinControl, autoMintAll, transparentRecipients); } std::pair CWallet::GetSparkBalance() { auto sparkWallet = pwalletMain->sparkWallet.get(); if(!sparkWallet) return {0, 0}; return sparkWallet->getSparkBalance(); } bool CWallet::IsSparkAddressMine(const std::string& address) { return sparkWallet && sparkWallet->isAddressMine(address); } bool CWallet::SetSparkAddressBook(const std::string& address, const std::string& strName, const std::string& strPurpose) { if (!sparkWallet) return false; bool fUpdated = false; { LOCK(cs_wallet); // mapAddressBook std::map::iterator mi = mapSparkAddressBook.find(address); fUpdated = mi != mapSparkAddressBook.end(); mapSparkAddressBook[address].name = strName; if (!strPurpose.empty()) /* update purpose only if requested */ mapSparkAddressBook[address].purpose = strPurpose; } NotifySparkAddressBookChanged(this, address, strName, IsSparkAddressMine(address), strPurpose, (fUpdated ? CT_UPDATED : CT_NEW) ); if (!fFileBacked) return false; if (!strPurpose.empty() && !CWalletDB(strWalletFile).WritePurpose(address, strPurpose)) return false; return CWalletDB(strWalletFile).WriteName(address, strName); } bool CWallet::DelAddressBook(const std::string& address) { bool checkSpark = false; { LOCK(cs_wallet); // mapAddressBook const spark::Params* params = spark::Params::get_default(); unsigned char network = spark::GetNetworkType(); unsigned char coinNetwork = {}; spark::Address addr(params); try { coinNetwork = addr.decode(address); } catch (...) { checkSpark = false; } if(network == coinNetwork){ checkSpark = true; } else { checkSpark = false; } if(fFileBacked) { if(checkSpark){ BOOST_FOREACH(const PAIRTYPE(std::string, std::string) &item, mapSparkAddressBook[address].destdata) { CWalletDB(strWalletFile).EraseDestData(address, item.first); } } else if(bip47::CPaymentCode::validate(address)) { BOOST_FOREACH(const PAIRTYPE(std::string, std::string) &item, mapRAPAddressBook[address].destdata) { CWalletDB(strWalletFile).EraseDestData(address, item.first); } } else if (validateAddress(address)) { BOOST_FOREACH(const PAIRTYPE(std::string, std::string) &item, mapAddressBook[CBitcoinAddress(address).Get()].destdata) { CWalletDB(strWalletFile).EraseDestData(address, item.first); } } } if(checkSpark){ mapSparkAddressBook.erase(address); } else if(bip47::CPaymentCode::validate(address)) { mapRAPAddressBook.erase(address); } else if (validateAddress(address)) { mapAddressBook.erase(CBitcoinAddress(address).Get()); } } if(checkSpark){ NotifySparkAddressBookChanged(this, address, "", IsSparkAddressMine(address), "", CT_DELETED); } else if(bip47::CPaymentCode::validate(address)){ bip47::CPaymentCode pcode(address); boost::optional pcodeDesc; pcodeDesc = FindPcode(pcode); if(pcodeDesc) { NotifyRAPAddressBookChanged(this, address, "", true, "", CT_DELETED); } else { NotifyRAPAddressBookChanged(this, address, "", false, "", CT_DELETED); } } else if(validateAddress(address)){ NotifyAddressBookChanged(this, CBitcoinAddress(address).Get(), "", ::IsMine(*this, CBitcoinAddress(address).Get()) != ISMINE_NO, "", CT_DELETED); } if (!fFileBacked) return false; CWalletDB(strWalletFile).ErasePurpose(address); return CWalletDB(strWalletFile).EraseName(address); } bool CWallet::validateAddress(const std::string& address) { CBitcoinAddress addressParsed(address); return addressParsed.IsValid(); } bool CWallet::validateSparkAddress(const std::string& address) const { const spark::Params* params = spark::Params::get_default(); unsigned char network = spark::GetNetworkType(); unsigned char coinNetwork; spark::Address addr(params); try { coinNetwork = addr.decode(address); } catch (...) { return false; } return network == coinNetwork; } bool CWallet::GetSparkOutputTx(const CScript& scriptPubKey, CSparkOutputTx& output) const { CWalletDB walletdb(strWalletFile); return walletdb.ReadSparkOutputTx(scriptPubKey, output); } bool CWallet::SetRAPAddressBook(const std::string& address, const std::string& strName, const std::string& strPurpose) { bool fUpdated = false; { LOCK(cs_wallet); // mapAddressBook std::map::iterator mi = mapRAPAddressBook.find(address); fUpdated = mi != mapRAPAddressBook.end(); mapRAPAddressBook[address].name = strName; if (!strPurpose.empty()) /* update purpose only if requested */ mapRAPAddressBook[address].purpose = strPurpose; } bip47::CPaymentCode pcode(address); boost::optional pcodeDesc; pcodeDesc = FindPcode(pcode); if(pcodeDesc) { NotifyRAPAddressBookChanged(this, address, strName, true, strPurpose, (fUpdated ? CT_UPDATED : CT_NEW) ); } else { NotifyRAPAddressBookChanged(this, address, strName, false, strPurpose, (fUpdated ? CT_UPDATED : CT_NEW) ); } if (!fFileBacked) return false; if (!strPurpose.empty() && !CWalletDB(strWalletFile).WritePurpose(address, strPurpose)) return false; return CWalletDB(strWalletFile).WriteName(address, strName); }