0

I am running into an error for:

TypeError: Type address payable is not implicitly convertible to expected type bytes32.

Due to the fact that I am using my address 0x0D81d9E21BD7C5bB095535624DcB0759E64B3899

I tried web3.utils.padLeft(web3.utils.hexToBytes(yourAddressString, 32); from https://stackoverflow.com/questions/58101282/how-to-convert-address-type-to-bytes32-via-web3 but had no avail. If somebody can convert my address 0x0D81d9E21BD7C5bB095535624DcB0759E64B3899 to bytes32 and tell me how they did it, I would greatly appreciate it. Thank you!

1
  • 1
    But why would you do that? Commented Jul 22, 2021 at 8:13

4 Answers 4

1

Fixed, answer is use padding

example -> 0x0000000000000000000000000D81d9E21BD7C5bB095535624DcB0759E64B3899

Sometimes the simplest answers are right infront of you

1

Here's a Solidity answer to it:

function addressToBytes32(address _addr) internal pure returns (bytes32) {
    return bytes32(uint256(uint160(_addr)));
}

Source: https://github.com/LayerZero-Labs/LayerZero-v2/blob/bf4318b/oapp/contracts/oft/libs/OFTComposeMsgCodec.sol

0

If you read the Pad Left documentation, you'd see that you don't need to do any conversion. You simply should do a web3.utils.padLeft(address, 64). Given that you want bytes32, that is a total of 64 hex digits, you just need to fill the difference in 0s for in order to have 64 digits.

0

If you're using Ethers.js

'0x'.concat(contract.address.slice(2).padStart(64, '0'))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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