Can you convert a bytes20
to address
in assembly (not solidity-typecasting)?
If so, how?
Ethereum Stack Exchange is a question and answer site for users of Ethereum, the decentralized application platform and smart contract enabled blockchain. It only takes a minute to sign up.
Sign up to join this communitySOLUTION:
If the address is in the first bytes, you can do:
assembly {
parsed := shr(96, mload(add(data, 32)))
}
With this, you right-shift 96 bits (or 12 bytes), and then assign to an address
type.
This solution won't work if you assign to a bytes20
because bytes20
grabs the highest bytes while address
the lowest.
Yep, heres an example of what the function might look like:
function convertBytes20ToAddress(bytes bytesInput) internal returns (address addressOutput) {
assembly {
addressOutput := mload(add(bytesInput,20))
}
}