diff --git a/src/addresstype.cpp b/src/addresstype.cpp index 5632879471..684bc6eb35 100644 --- a/src/addresstype.cpp +++ b/src/addresstype.cpp @@ -270,3 +270,10 @@ bool ExtractDestination(const COutPoint& prevout, const CScript& scriptPubKey, C } return false; } + +int GetAddressIndexType(const CTxDestination &dest) +{ + if(dest.index() > 1) + return dest.index() - 1; // PubKeyDestination and PKHash are considered the same + return dest.index(); +} diff --git a/src/addresstype.h b/src/addresstype.h index 92379d1cbc..772adf09e1 100644 --- a/src/addresstype.h +++ b/src/addresstype.h @@ -185,4 +185,6 @@ struct DataVisitor bool ExtractDestination(const COutPoint& prevout, const CScript& scriptPubKey, CTxDestination& addressRet, TxoutType* typeRet = NULL); +int GetAddressIndexType(const CTxDestination& dest); + #endif // BITCOIN_ADDRESSTYPE_H diff --git a/src/key_io.cpp b/src/key_io.cpp index 97a65e8c5f..6b34b71317 100644 --- a/src/key_io.cpp +++ b/src/key_io.cpp @@ -358,6 +358,13 @@ bool DecodeIndexKey(const std::string &str, uint256 &hashBytes, int &type) type = 4; return true; } + + if (std::holds_alternative(dest)) { + const WitnessV1Taproot witnessV1Taproot = std::get(dest); + memcpy(hashBytes.data(), &witnessV1Taproot, 32); + type = 5; + return true; + } } return false; diff --git a/src/rpc/node.cpp b/src/rpc/node.cpp index 7134c68135..6c5978012c 100644 --- a/src/rpc/node.cpp +++ b/src/rpc/node.cpp @@ -577,6 +577,10 @@ bool getAddressFromIndex(const int &type, const uint256 &hash, std::string &addr } else if (type == 4) { std::vector addressBytes(hash.begin(), hash.begin() + 20); address = EncodeDestination(WitnessV0KeyHash(uint160(addressBytes))); + } else if (type == 5) { + WitnessV1Taproot tap; + std::copy(hash.begin(), hash.end(), tap.begin()); + address = EncodeDestination(tap); } else { return false; } diff --git a/src/txmempool.cpp b/src/txmempool.cpp index ec7d91b121..cda81ac4e4 100644 --- a/src/txmempool.cpp +++ b/src/txmempool.cpp @@ -1292,7 +1292,8 @@ void CTxMemPool::addAddressIndex(const CTxMemPoolEntry &entry, const CCoinsViewC } valtype addressBytes(32); std::copy(bytesID.begin(), bytesID.end(), addressBytes.begin()); - CMempoolAddressDeltaKey key(dest.index(), uint256(addressBytes), txhash, j, 1); + int addressIndexType = GetAddressIndexType(dest); + CMempoolAddressDeltaKey key(addressIndexType, uint256(addressBytes), txhash, j, 1); CMempoolAddressDelta delta(entry.GetTime().count(), prevout.nValue * -1, input.prevout.hash, input.prevout.n); mapAddress.insert(std::make_pair(key, delta)); inserted.push_back(key); @@ -1310,7 +1311,8 @@ void CTxMemPool::addAddressIndex(const CTxMemPoolEntry &entry, const CCoinsViewC } valtype addressBytes(32); std::copy(bytesID.begin(), bytesID.end(), addressBytes.begin()); - CMempoolAddressDeltaKey key(dest.index(), uint256(addressBytes), txhash, k, 0); + int addressIndexType = GetAddressIndexType(dest); + CMempoolAddressDeltaKey key(addressIndexType, uint256(addressBytes), txhash, k, 0); mapAddress.insert(std::make_pair(key, CMempoolAddressDelta(entry.GetTime().count(), out.nValue))); inserted.push_back(key); } diff --git a/src/validation.cpp b/src/validation.cpp index 6e1a9d754f..91d6adf45d 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -2282,10 +2282,11 @@ DisconnectResult Chainstate::DisconnectBlock(const CBlock& block, const CBlockIn } valtype addressBytes(32); std::copy(bytesID.begin(), bytesID.end(), addressBytes.begin()); + int addressIndexType = GetAddressIndexType(dest); // undo receiving activity - addressIndex.push_back(std::make_pair(CAddressIndexKey(dest.index(), uint256(addressBytes), pindex->nHeight, i, hash, k, false), out.nValue)); + addressIndex.push_back(std::make_pair(CAddressIndexKey(addressIndexType, uint256(addressBytes), pindex->nHeight, i, hash, k, false), out.nValue)); // undo unspent index - addressUnspentIndex.push_back(std::make_pair(CAddressUnspentKey(dest.index(), uint256(addressBytes), hash, k), CAddressUnspentValue())); + addressUnspentIndex.push_back(std::make_pair(CAddressUnspentKey(addressIndexType, uint256(addressBytes), hash, k), CAddressUnspentValue())); } } } @@ -2319,10 +2320,11 @@ DisconnectResult Chainstate::DisconnectBlock(const CBlock& block, const CBlockIn } valtype addressBytes(32); std::copy(bytesID.begin(), bytesID.end(), addressBytes.begin()); + int addressIndexType = GetAddressIndexType(dest); // undo spending activity - addressIndex.push_back(std::make_pair(CAddressIndexKey(dest.index(), uint256(addressBytes), pindex->nHeight, i, hash, j, true), prevout.nValue * -1)); + addressIndex.push_back(std::make_pair(CAddressIndexKey(addressIndexType, uint256(addressBytes), pindex->nHeight, i, hash, j, true), prevout.nValue * -1)); // restore unspent index - addressUnspentIndex.push_back(std::make_pair(CAddressUnspentKey(dest.index(), uint256(addressBytes), input.prevout.hash, input.prevout.n), CAddressUnspentValue(prevout.nValue, prevout.scriptPubKey, undo.nHeight, isTxCoinStake))); + addressUnspentIndex.push_back(std::make_pair(CAddressUnspentKey(addressIndexType, uint256(addressBytes), input.prevout.hash, input.prevout.n), CAddressUnspentValue(prevout.nValue, prevout.scriptPubKey, undo.nHeight, isTxCoinStake))); } } } @@ -3472,11 +3474,12 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state, } valtype addressBytes(32); std::copy(bytesID.begin(), bytesID.end(), addressBytes.begin()); - addressIndex.push_back(std::make_pair(CAddressIndexKey(dest.index(), uint256(addressBytes), pindex->nHeight, i, tx.GetHash(), j, true), prevout.nValue * -1)); + int addressIndexType = GetAddressIndexType(dest); + addressIndex.push_back(std::make_pair(CAddressIndexKey(addressIndexType, uint256(addressBytes), pindex->nHeight, i, tx.GetHash(), j, true), prevout.nValue * -1)); // remove address from unspent index - addressUnspentIndex.push_back(std::make_pair(CAddressUnspentKey(dest.index(), uint256(addressBytes), input.prevout.hash, input.prevout.n), CAddressUnspentValue())); - spentIndex.push_back(std::make_pair(CSpentIndexKey(input.prevout.hash, input.prevout.n), CSpentIndexValue(tx.GetHash(), j, pindex->nHeight, prevout.nValue, dest.index(), uint256(addressBytes)))); + addressUnspentIndex.push_back(std::make_pair(CAddressUnspentKey(addressIndexType, uint256(addressBytes), input.prevout.hash, input.prevout.n), CAddressUnspentValue())); + spentIndex.push_back(std::make_pair(CSpentIndexKey(input.prevout.hash, input.prevout.n), CSpentIndexValue(tx.GetHash(), j, pindex->nHeight, prevout.nValue, addressIndexType, uint256(addressBytes)))); } } } @@ -3697,10 +3700,11 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state, } valtype addressBytes(32); std::copy(bytesID.begin(), bytesID.end(), addressBytes.begin()); + int addressIndexType = GetAddressIndexType(dest); // record receiving activity - addressIndex.push_back(std::make_pair(CAddressIndexKey(dest.index(), uint256(addressBytes), pindex->nHeight, i, tx.GetHash(), k, false), out.nValue)); + addressIndex.push_back(std::make_pair(CAddressIndexKey(addressIndexType, uint256(addressBytes), pindex->nHeight, i, tx.GetHash(), k, false), out.nValue)); // record unspent output - addressUnspentIndex.push_back(std::make_pair(CAddressUnspentKey(dest.index(), uint256(addressBytes), tx.GetHash(), k), CAddressUnspentValue(out.nValue, out.scriptPubKey, pindex->nHeight, isTxCoinStake))); + addressUnspentIndex.push_back(std::make_pair(CAddressUnspentKey(addressIndexType, uint256(addressBytes), tx.GetHash(), k), CAddressUnspentValue(out.nValue, out.scriptPubKey, pindex->nHeight, isTxCoinStake))); } } }