[Ethereum] How to detect if an address is a contract

addressesweb3js

I'm interested in knowing whether any particular address is a contract address or a standard address. There have been similar questions on the topic, but I'm not looking to detect whether an address is valid as this is child's play.

I assume detecting a contract can be done, Etherscan does a good job at detecting addresses vs contracts.

Any ideas?
Can this be done with web3?

Best Answer

you can use getcode function.
if the address is representing an External Owned Account you will get 0x as response otherwise you will get the contract's bytecodeode. for example :

web3.eth.getCode("0xa5Acc472597C1e1651270da9081Cc5a0b38258E3")
"0x"