[txpackages] add canonical way to get hash of package

This commit is contained in:
glozow
2024-04-16 11:58:25 +01:00
parent c3c1e15831
commit 092c978a42
3 changed files with 120 additions and 1 deletions

View File

@@ -147,3 +147,21 @@ bool IsChildWithParentsTree(const Package& package)
return true;
});
}
uint256 GetPackageHash(const std::vector<CTransactionRef>& transactions)
{
// Create a vector of the wtxids.
std::vector<Wtxid> wtxids_copy;
std::transform(transactions.cbegin(), transactions.cend(), std::back_inserter(wtxids_copy),
[](const auto& tx){ return tx->GetWitnessHash(); });
// Sort in ascending order
std::sort(wtxids_copy.begin(), wtxids_copy.end(), [](const auto& lhs, const auto& rhs) { return lhs.GetHex() < rhs.GetHex(); });
// Get sha256 hash of the wtxids concatenated in this order
HashWriter hashwriter;
for (const auto& wtxid : wtxids_copy) {
hashwriter << wtxid;
}
return hashwriter.GetSHA256();
}