1

I am trying to convert a 128 characters hex encoded public key to an address in Solidity (0.5).

For example the pubkey would be:

af80b90d25145da28c583359beb47b21796b2fe1a23c1511e443e7a64dfdb27d7434c380f0aa4c500e220aa1a9d068514b1ff4d5019e624e7ba1efe82b340a59

which should generate an address of:

0x627306090abaB3A6e1400e9345bC60c78a8BEf57

How to convert a 64 byte public key to a 20 byte address in Solidity?

eth
  • 85,679
  • 53
  • 285
  • 406
cammellos
  • 13
  • 2
  • Related: https://ethereum.stackexchange.com/questions/40897/get-address-from-public-key-in-solidity – eth Apr 04 '19 at 05:26

1 Answers1

1
function publicKeyToAddress (bytes memory publicKey) public pure returns (bytes20) {
    require (publicKey.length == 64);
    return bytes20 (uint160 (uint256 (keccak256 (publicKey))));
}

In Remix it gives me:

Input:

{
    "bytes publicKey": "0xaf80b90d25145da28c583359beb47b21796b2fe1a23c1511e443e7a64dfdb27d7434c380f0aa4c500e220aa1a9d068514b1ff4d5019e624e7ba1efe82b340a59"
}

Output:

{
    "0": "bytes20: 0x627306090abab3a6e1400e9345bc60c78a8bef57"
}
Mikhail Vladimirov
  • 7,313
  • 1
  • 23
  • 38