Skip to content

Commit

Permalink
Fix witness v16 endianness (#2369)
Browse files Browse the repository at this point in the history
* Add support for unit testing

* Add erc55 and wpkh key tests

* Rename test py to functional tests

* Add test-cpp and test-rs

* Remove func test, rename to test-units

* Fix test func

* Fix endianness for witnessv16 to LE

* Fix transferdomain rpc

* Fix getaccount RPC for evm account

* Add helper comments for legacy func

* Revert test test_runner

* Fix transferdomain test

* Fix py lint

* Add script to destination key tests

* Encode key ID from serialised block

* Apply missing changes post-merge

---------

Co-authored-by: Bushstar <[email protected]>
Co-authored-by: Prasanna Loganathar <[email protected]>
  • Loading branch information
3 people authored Sep 5, 2023
1 parent 4160975 commit b32e172
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 19 deletions.
8 changes: 5 additions & 3 deletions src/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,12 @@ inline uint160 Hash160(const prevector<N, unsigned char>& vch)
}

inline uint160 EthHash160(const std::vector<unsigned char>& vch) {
std::vector<unsigned char> output;
sha3_256_safe(vch, output);
std::vector<unsigned char> result;
sha3_256_safe(vch, result);
const size_t ADDRESS_OFFSET{12};
return uint160({output.begin() + ADDRESS_OFFSET, output.end()});
std::vector<unsigned char> output(result.begin() + ADDRESS_OFFSET, result.end());
std::reverse(output.begin(), output.end());
return uint160(output);
}

/** A writer stream (for serialization) that computes a 256-bit hash. */
Expand Down
3 changes: 2 additions & 1 deletion src/key_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class DestinationEncoder
{
// Raw addr = ETH_ADDR_PREFIX + HexStr(id);
// Produce ERC55 checksum address: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-55.md
const auto address = HexStr(id);
const auto address = id.GetHex();
std::vector<unsigned char> input(address.begin(), address.end());
std::vector<unsigned char> output;
sha3_256_safe(input, output);
Expand Down Expand Up @@ -93,6 +93,7 @@ CTxDestination DecodeDestination(const std::string& str, const CChainParams& par
return CNoDestination();
}
data = ParseHex(hex);
std::reverse(data.begin(), data.end());
return WitnessV16EthHash(uint160(data));
}
if (DecodeBase58Check(str, data)) {
Expand Down
8 changes: 4 additions & 4 deletions src/masternodes/consensus/xvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,14 +209,14 @@ Res CXVMConsensus::operator()(const CTransferDomainMessage &obj) const {
auto tokenId = dst.amount.nTokenId;
CrossBoundaryResult result;
if (tokenId == DCT_ID{0}) {
evm_unsafe_try_add_balance_in_q(result, evmQueueId, toAddress.ToHexString(), balanceIn, tx.GetHash().GetHex());
evm_unsafe_try_add_balance_in_q(result, evmQueueId, toAddress.GetHex(), balanceIn, tx.GetHash().GetHex());
if (!result.ok) {
return Res::Err("Error bridging DFI: %s", result.reason);
}
}
else {
CrossBoundaryResult result;
evm_try_bridge_dst20(result, evmQueueId, toAddress.ToHexString(), balanceIn, tx.GetHash().GetHex(), tokenId.v, false);
evm_try_bridge_dst20(result, evmQueueId, toAddress.GetHex(), balanceIn, tx.GetHash().GetHex(), tokenId.v, false);
if (!result.ok) {
return Res::Err("Error bridging DST20: %s", result.reason);
}
Expand All @@ -235,7 +235,7 @@ Res CXVMConsensus::operator()(const CTransferDomainMessage &obj) const {
auto tokenId = dst.amount.nTokenId;
if (tokenId == DCT_ID{0}) {
CrossBoundaryResult result;
if (!evm_unsafe_try_sub_balance_in_q(result, evmQueueId, fromAddress.ToHexString(), balanceIn, tx.GetHash().GetHex())) {
if (!evm_unsafe_try_sub_balance_in_q(result, evmQueueId, fromAddress.GetHex(), balanceIn, tx.GetHash().GetHex())) {
return DeFiErrors::TransferDomainNotEnoughBalance(EncodeDestination(dest));
}
if (!result.ok) {
Expand All @@ -244,7 +244,7 @@ Res CXVMConsensus::operator()(const CTransferDomainMessage &obj) const {
}
else {
CrossBoundaryResult result;
evm_try_bridge_dst20(result, evmQueueId, fromAddress.ToHexString(), balanceIn, tx.GetHash().GetHex(), tokenId.v, true);
evm_try_bridge_dst20(result, evmQueueId, fromAddress.GetHex(), balanceIn, tx.GetHash().GetHex(), tokenId.v, true);
if (!result.ok) {
return Res::Err("Error bridging DST20: %s", result.reason);
}
Expand Down
4 changes: 2 additions & 2 deletions src/masternodes/rpc_accounts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ UniValue getaccount(const JSONRPCRequest& request) {
CTxDestination dest;
if (ExtractDestination(reqOwner, dest) && dest.index() == WitV16KeyEthHashType) {
const auto keyID = std::get<WitnessV16EthHash>(dest);
auto r = XResultValue(evm_try_get_balance(result, keyID.ToHexString()));
auto r = XResultValue(evm_try_get_balance(result, keyID.GetHex()));
if (!r) throw JSONRPCError(RPC_MISC_ERROR, r.msg);
if (const auto balance = *r) {
balances[DCT_ID{}] = balance;
Expand Down Expand Up @@ -605,7 +605,7 @@ UniValue gettokenbalances(const JSONRPCRequest& request) {
if (evm_dfi_lookup) {
for (const auto keyID : pwallet->GetKeys()) {
// TODO: Use GetHex when eth key is fixed to be stored in LE
auto res = XResultValue(evm_try_get_balance(result, HexStr(keyID)));
auto res = XResultValue(evm_try_get_balance(result, keyID.GetHex()));
if (res) {
auto evmAmount = *res;
totalBalances.Add({{}, static_cast<CAmount>(evmAmount)});
Expand Down
2 changes: 1 addition & 1 deletion src/masternodes/rpc_evm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ UniValue evmtx(const JSONRPCRequest &request) {
}

const auto toEth = std::get<WitnessV16EthHash>(toDest);
to = toEth.ToHexString();
to = toEth.GetHex();
}

rust::Vec<uint8_t> input{};
Expand Down
3 changes: 1 addition & 2 deletions src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1154,8 +1154,7 @@ Staker::Status Staker::stake(const CChainParams& chainparams, const ThreadStaker
if (pubKey.IsCompressed()) {
pubKey.Decompress();
}
// TODO: Use GetHex when eth key is fixed to be stored in LE
const auto evmBeneficiary = HexStr(pubKey.GetEthID());
const auto evmBeneficiary = pubKey.GetEthID().GetHex();
auto pblocktemplate = BlockAssembler(chainparams).CreateNewBlock(scriptPubKey, blockTime, evmBeneficiary);
if (!pblocktemplate) {
LogPrintf("Error: WalletStaker: Keypool ran out, keypoolrefill and restart required\n");
Expand Down
5 changes: 0 additions & 5 deletions src/script/standard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ WitnessV0KeyHash::WitnessV0KeyHash(const CPubKey& pubkey) : uint160(pubkey.GetID

WitnessV16EthHash::WitnessV16EthHash(const CPubKey& pubkey) : uint160(pubkey.GetEthID()) {}

// TO DO: Remove this temporary fix to return hex string, once EthHash is switched from big endian to little endian.
std::string WitnessV16EthHash::ToHexString() const {
return HexStr(data, data + sizeof(data));
}

const char* GetTxnOutputType(txnouttype t)
{
switch (t)
Expand Down
1 change: 0 additions & 1 deletion src/script/standard.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ struct WitnessV16EthHash : public uint160 {
WitnessV16EthHash() : uint160() {}
explicit WitnessV16EthHash(const uint160& hash) : uint160(hash) {}
explicit WitnessV16EthHash(const CPubKey& pubkey);
std::string ToHexString() const;
using uint160::uint160;
};

Expand Down
1 change: 1 addition & 0 deletions src/test/key_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include <boost/test/unit_test.hpp>

// private keys
static const std::string strSecret1 = "5HxWvvfubhXpYYpS3tJkw6fq9jE9j18THftkZjHHfmFiWtmAbrj";
static const std::string strSecret2 = "5KC4ejrDjv152FGwP386VD1i2NYc5KkfSMyv1nGy1VGDxGHqVY3";
static const std::string strSecret1C = "Kwr371tjA9u2rFSMZjTNun2PXXP3WPZu2afRHTcta6KxEUdm1vEw";
Expand Down

0 comments on commit b32e172

Please sign in to comment.