Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/interfaces/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,12 @@ class Wallet
virtual wallet::PlatformKeyResult<wallet::FriendshipXpub> ensureFriendshipReceivingKeychain(
const wallet::FriendshipKeychainRequest& request) = 0;

//! Every credit output of this wallet's asset lock transactions that pays
//! one of keys, derived from the transactions the wallet holds. Deriving
//! the keys needs an unlocked wallet.
virtual wallet::PlatformKeyResult<std::vector<wallet::AssetLockFunding>> listAssetLockFundings(
const std::vector<wallet::AssetLockFundingKey>& keys) = 0;

//! Return whether wallet has private key.
virtual bool isSpendable(const CScript& script) = 0;
virtual bool isSpendable(const CTxDestination& dest) = 0;
Expand Down
5 changes: 5 additions & 0 deletions src/wallet/interfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,11 @@ class WalletImpl : public Wallet
{
return m_wallet->EnsureFriendshipReceivingKeychain(request);
}
wallet::PlatformKeyResult<std::vector<wallet::AssetLockFunding>> listAssetLockFundings(
const std::vector<wallet::AssetLockFundingKey>& keys) override
{
return m_wallet->ListAssetLockFundings(keys);
}
bool isSpendable(const CScript& script) override
{
LOCK(m_wallet->cs_wallet);
Expand Down
17 changes: 17 additions & 0 deletions src/wallet/platformtypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#ifndef BITCOIN_WALLET_PLATFORMTYPES_H
#define BITCOIN_WALLET_PLATFORMTYPES_H

#include <consensus/amount.h>
#include <pubkey.h>
#include <script/standard.h>
#include <support/allocators/secure.h>
Expand Down Expand Up @@ -59,6 +60,22 @@ using PlatformKeyRequest = std::variant<IdentityAuthKey, RegistrationFundingKey,
//! the wallet's seed, so the credits it funds can be recovered.
using AssetLockFundingKey = std::variant<RegistrationFundingKey, TopupFundingKey, InvitationFundingKey>;

enum class AssetLockFundingStatus : uint8_t {
ACTIVE, //!< Not abandoned, and no conflicting transaction is mined.
INACTIVE, //!< Abandoned and not mined. It can still be mined, so it stays resumable.
CONFLICTED, //!< A transaction spending the same coins is mined.
};

//! A credit output of a wallet asset lock that pays one of the wallet's
//! funding keys.
struct AssetLockFunding {
uint256 txid;
uint32_t credit_index{0}; //!< Index into the payload's credit outputs.
AssetLockFundingKey key;
CAmount amount{0};
AssetLockFundingStatus status{AssetLockFundingStatus::ACTIVE};
};

struct FriendshipKeychainRequest {
uint32_t account{0};
uint256 local_id;
Expand Down
70 changes: 70 additions & 0 deletions src/wallet/test/wallet_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,75 @@ BOOST_FIXTURE_TEST_CASE(create_asset_lock_transaction, AssetLockTestingSetup)
BOOST_CHECK(!m_node.mempool->exists((*conflicting)->GetHash()));
BOOST_CHECK(iface->abandonTransaction((*conflicting)->GetHash()));

// The asset locks the wallet holds are its funding inventory: each
// credit output paying a requested key, whatever happened to it since.
const std::vector<AssetLockFundingKey> funding_keys{registration_key, topup_key, TopupFundingKey{1}};
const auto list_fundings{[&](interfaces::Wallet& w) {
const auto fundings{w.listAssetLockFundings(funding_keys)};
BOOST_REQUIRE(fundings);
std::map<uint256, AssetLockFunding> by_txid;
for (const auto& funding : fundings.value) by_txid.emplace(funding.txid, funding);
BOOST_CHECK_EQUAL(by_txid.size(), fundings.value.size());
return by_txid;
}};
// The top-up lock was never committed. The abandoned conflict stays listed.
auto fundings{list_fundings(*iface)};
BOOST_REQUIRE_EQUAL(fundings.size(), 2U);
const AssetLockFunding& registration{fundings.at(tx->GetHash())};
BOOST_CHECK(std::holds_alternative<RegistrationFundingKey>(registration.key));
BOOST_CHECK_EQUAL(registration.credit_index, 0U);
BOOST_CHECK_EQUAL(registration.amount, credit_amount);
BOOST_CHECK(registration.status == AssetLockFundingStatus::ACTIVE);
BOOST_CHECK(fundings.at((*conflicting)->GetHash()).status == AssetLockFundingStatus::INACTIVE);
const auto not_requested{iface->listAssetLockFundings({topup_key})};
BOOST_CHECK(not_requested && not_requested.value.empty());

// Mined, the lock stays active and the abandoned conflict is conflicted.
{
const auto notifications{m_node.chain->handleNotifications(wallet_ref)};
CreateAndProcessBlock({CMutableTransaction{*tx}}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
SyncWithValidationInterfaceQueue();
}
const int tx_height{WITH_LOCK(::cs_main, return m_node.chainman->ActiveChain().Height())};
fundings = list_fundings(*iface);
BOOST_REQUIRE_EQUAL(fundings.size(), 2U);
BOOST_CHECK(fundings.at(tx->GetHash()).status == AssetLockFundingStatus::ACTIVE);
BOOST_CHECK(fundings.at((*conflicting)->GetHash()).status == AssetLockFundingStatus::CONFLICTED);

// A wallet restored from the seed and the funding coins' keys finds the
// mined lock again in the chain. Its rescan does not move the height the
// wallet has processed block notifications to, which here stays two
// blocks below the lock: the lock is still active.
{
SecureString mnemonic, mnemonic_passphrase;
{
LOCK(wallet->cs_wallet);
for (auto* spk_man : wallet->GetActiveScriptPubKeyMans()) {
auto* desc_spk_man{dynamic_cast<DescriptorScriptPubKeyMan*>(spk_man)};
if (desc_spk_man && desc_spk_man->GetMnemonicString(mnemonic, mnemonic_passphrase)) break;
}
}
BOOST_REQUIRE(!mnemonic.empty());
const auto restored{std::make_shared<CWallet>(m_node.chain.get(), "", CreateMockableWalletDatabase())};
BOOST_REQUIRE_EQUAL(restored->LoadWallet(), DBErrors::LOAD_OK);
{
LOCK(restored->cs_wallet);
restored->SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
restored->SetupDescriptorScriptPubKeyMans(mnemonic, mnemonic_passphrase);
const CBlockIndex* lagging{WITH_LOCK(::cs_main, return m_node.chainman->ActiveChain()[tx_height - 2])};
restored->SetLastBlockProcessed(lagging->nHeight, lagging->GetBlockHash());
}
AddKey(*restored, coinbaseKey);
const auto restored_iface{interfaces::MakeWallet(context, restored)};
BOOST_CHECK(list_fundings(*restored_iface).empty());
BOOST_REQUIRE(restored_iface->startRescan(/*from_genesis=*/true) == RescanStatus::SUCCESS);
BOOST_CHECK_EQUAL(WITH_LOCK(restored->cs_wallet, return restored->GetLastBlockHeight()), tx_height - 2);
const auto restored_fundings{list_fundings(*restored_iface)};
BOOST_REQUIRE_EQUAL(restored_fundings.size(), 1U);
BOOST_CHECK(restored_fundings.at(tx->GetHash()).status == AssetLockFundingStatus::ACTIVE);
BOOST_CHECK_EQUAL(restored_fundings.at(tx->GetHash()).amount, credit_amount);
}

// An encrypted wallet builds asset locks only when fully unlocked: not
// while locked, and not while unlocked for mixing only.
const SecureString passphrase{"asset lock"};
Expand All @@ -857,6 +926,7 @@ BOOST_FIXTURE_TEST_CASE(create_asset_lock_transaction, AssetLockTestingSetup)
BOOST_CHECK(!iface->createAssetLockTransaction(credit_amount, registration_key, coin_control, fee));
// A locked wallet can still tell whether it is able to fund an identity.
BOOST_CHECK(iface->getPlatformPubKey(registration_key).status == PlatformKeyStatus::WALLET_LOCKED);
BOOST_CHECK(iface->listAssetLockFundings(funding_keys).status == PlatformKeyStatus::WALLET_LOCKED);
BOOST_CHECK(iface->platformKeySourceStatus() == PlatformKeyStatus::SUCCESS);
BOOST_REQUIRE(wallet->Unlock(passphrase, /*fForMixingOnly=*/true));
BOOST_REQUIRE(wallet->IsLocked() && !wallet->IsLocked(/*fForMixing=*/true));
Expand Down
35 changes: 35 additions & 0 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@
#include <warnings.h>

#include <coinjoin/options.h>
#include <evo/assetlocktx.h>
#include <evo/providertx.h>
#include <evo/specialtx.h>
#include <governance/vote.h>

#include <algorithm>
Expand Down Expand Up @@ -4304,6 +4306,39 @@ PlatformKeyResult<FriendshipXpub> CWallet::EnsureFriendshipReceivingKeychain(con
return result;
}

PlatformKeyResult<std::vector<AssetLockFunding>> CWallet::ListAssetLockFundings(const std::vector<AssetLockFundingKey>& keys) const
{
LOCK(cs_wallet);
PlatformKeyResult<std::vector<AssetLockFunding>> result;
result.status = PlatformKeyStatus::SUCCESS;
std::map<CScript, AssetLockFundingKey> scripts;
for (const auto& key : keys) {
platformkeys::ExtKey256 derived;
result.status = DerivePlatformKey(std::visit([](const auto& k) { return PlatformKeyRequest{k}; }, key), derived);
if (result.status != PlatformKeyStatus::SUCCESS) return result;
scripts.emplace(GetScriptForDestination(PKHash{derived.key.GetPubKey()}), key);
}

for (const auto& [_, wtx] : wtxOrdered) {
if (wtx->tx->nType != TRANSACTION_ASSET_LOCK) continue;
const auto payload{GetTxPayload<CAssetLockPayload>(*wtx->tx)};
if (!payload) continue;
AssetLockFundingStatus status{AssetLockFundingStatus::ACTIVE};
if (wtx->isConflicted()) {
status = AssetLockFundingStatus::CONFLICTED;
} else if (wtx->isAbandoned()) {
status = AssetLockFundingStatus::INACTIVE;
}
const auto& credits{payload->getCreditOutputs()};
for (uint32_t index{0}; index < credits.size(); ++index) {
const auto it{scripts.find(credits[index].scriptPubKey)};
if (it == scripts.end()) continue;
result.value.push_back({wtx->GetHash(), index, it->second, credits[index].nValue, status});
}
}
return result;
}

std::vector<const Governance::Object*> CWallet::GetGovernanceObjects()
{
AssertLockHeld(cs_wallet);
Expand Down
1 change: 1 addition & 0 deletions src/wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -1146,6 +1146,7 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
PlatformKeyResult<SecureVector> PlatformECDHSecret(const IdentityAuthKey& key, const CPubKey& counterparty) const;
PlatformKeyResult<uint32_t> PlatformAccountReference(const IdentityAuthKey& key, const CompactXpub& compact_xpub) const;
PlatformKeyResult<FriendshipXpub> EnsureFriendshipReceivingKeychain(const FriendshipKeychainRequest& request);
PlatformKeyResult<std::vector<AssetLockFunding>> ListAssetLockFundings(const std::vector<AssetLockFundingKey>& keys) const;

/* Returns the time of the first created key or, in case of an import, it could be the time of the first received transaction */
int64_t GetBirthTime() const { return m_birth_time; }
Expand Down
Loading