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?

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"
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.