Skip to content

Commit

Permalink
Return Eth addresses with EIP-55 checksum applied (#2024)
Browse files Browse the repository at this point in the history
* Return Eth addresses with EIP-55 checksum applied

* Update src/key_io.cpp

* Fix formatting in added comments

---------

Co-authored-by: Prasanna Loganathar <[email protected]>
  • Loading branch information
Bushstar and prasannavl authored Jun 6, 2023
1 parent b44f582 commit 21b837e
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/key_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,22 @@ class DestinationEncoder

std::string operator()(const WitnessV16EthHash& id) const
{
return ETH_ADDR_PREFIX + HexStr(id);
// Raw addr = ETH_ADDR_PREFIX + HexStr(id);
// Produce ETH checksum address: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-55.md
const auto address = id.ToString();
std::vector<unsigned char> input(address.begin(), address.end());
std::vector<unsigned char> output;
sha3(input, output);
const auto hashedAddress = HexStr(output);
std::string result;
for (size_t i{}; i < address.size(); ++i) {
if (std::isdigit(address[i]) || hashedAddress[i] < '8') {
result += address[i];
} else {
result += std::toupper(address[i]);
}
}
return ETH_ADDR_PREFIX + result;
}

std::string operator()(const CNoDestination& no) const { return {}; }
Expand Down

0 comments on commit 21b837e

Please sign in to comment.