Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert address type to bytes32 via web3?

I have contract which require input in bytes32, so I need convert address to bytes32, but no see this method in web3.

like image 620
Руслан Миров Avatar asked Oct 31 '25 09:10

Руслан Миров


2 Answers

Ethereum addresses are 20 bytes, so you convert hex address to bytes and then pad it to 32 bytes from left.

web3.utils.padLeft(web3.utils.hexToBytes(yourAddressString), 32);
like image 131
ferit Avatar answered Nov 02 '25 23:11

ferit


If you read the PadLeft 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.

like image 29
FHannoun Avatar answered Nov 03 '25 00:11

FHannoun