How to tell if an Ethereum address is a contract or a regular account?

If you want to start learning Ethereum DApp development right away, you can visit the excellent online interactive tutorial provided by Huizhi.com:

In Ethereum, whether it is an ordinary account or a contract account, the address looks like a string of hexadecimal code streams. So, how should one distinguish whether a given Ethereum address is a regular account or a contract account?

use web3.js

The web3.eth.getCode() method returns the hexadecimal string of the code on the specified address. Since there is no code at the normal account address, only the hexadecimal prefix will be returned 0x. Using this we can make judgments, for example:

var code = web3.eth.getCode("0xbfb2e296d9cf3e593e79981235aed29ab9984c0f")
if(code === '0x') console.log('普通账户')
else console.log('合约账户')

Implemented in solidity

Inside the contract, the EVM assembly code can be used to get the code size at the specified address, obviously, the normal account address will return 0:

contract EzDemo {
    function isContract(address addr) returns (bool) {
    uint size;
    assembly { size := extcodesize(addr) }
    return size > 0;
  }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324857880&siteId=291194637