Skip to content

Instantly share code, notes, and snippets.

@drgorillamd
Created November 13, 2021 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drgorillamd/6fbb1e275695be373745619ad72d25d3 to your computer and use it in GitHub Desktop.
Save drgorillamd/6fbb1e275695be373745619ad72d25d3 to your computer and use it in GitHub Desktop.
decompose bytes32,bytes32,bytes16 to address
pragma solidity ^0.8.0;
contract Test {
function a(bytes32 c, bytes32 d, bytes16 e) external view returns(address[4] memory res) {
assembly {
//address will take the right-end 20 bytes -> starting at 0, need to add 8 bytes of padding (already 4 from the function sig) => shift right 64bits
mstore(res, shr(64, calldataload(0))) //res[0] res==keccak256(0)==0 here (slot0)
mstore(add(res, 32), calldataload(12)) // we want to have the offset 12 bytes before the begining of the address -> 4+20 are just before, then -12 = 12
mstore(add(res, 64), calldataload(32))
mstore(add(res, 96), calldataload(52))
}
}
}
/*
c : 0x7130d2a12b9bcbfae4f2634d864a1ee1ce3ead9c0e09fabb73bd3ade0a17ecc3
d : 0x21fd13a19e81ce82 f8a0bf9cf54bb92f17374d9e9a321e6a111a51bd 2170ed08
e : 0x80ac9a755fd29b2688956bd959f933f8
selector : 0x13471064
raw data (remix) : "0x7130d2a12b9bcbfae4f2634d864a1ee1ce3ead9c0e09fabb73bd3ade0a17ecc3","0x21fd13a19e81ce82f8a0bf9cf54bb92f17374d9e9a321e6a111a51bd2170ed08","0x80ac9a755fd29b2688956bd959f933f800000000000000000000000000000000"
payload : 0x134710647130d2a12b9bcbfae4f2634d864a1ee1ce3ead9c0e09fabb73bd3ade0a17ecc321fd13a19e81ce82f8a0bf9cf54bb92f17374d9e9a321e6a111a51bd2170ed0880ac9a755fd29b2688956bd959f933f800000000000000000000000000000000
orig :
0x7130d2a12b9bcbfae4f2634d864a1ee1ce3ead9c
0x0e09fabb73bd3ade0a17ecc321fd13a19e81ce82
0xf8a0bf9cf54bb92f17374d9e9a321e6a111a51bd
0x2170ed0880ac9a755fd29b2688956bd959f933f8
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment