How does Ethereum determine the account type

There are two types of accounts in Ethereum that share the same address space.

  • External accounts, which are controlled by public-private key pairs (humans).
  • Contract accounts, which are controlled by the code stored in the account.

The address of the external account is determined by the public key, and the address of the contract account is determined when the contract is created.

Except for the fact that contract accounts store code, external accounts do not, these two types of accounts are the same to EVM. Each account has a persistent store in the form of key-value. The length of key and value are both 256 bits, and the name is storage.

Additionally, each account has an ether balance (in "Wei") that can be changed by sending it a transaction with ether.

How to determine the account type of an address?

Use the web3.eth.getCode() method to determine whether the account of a given address is an external account or a contract account. This function returns the code of the specified address. Since the external account has no code, it will only return 0x, while the contract account will return 0xthe hexadecimal code string at the beginning. E.g:

var code = web3.eth.getCode("0xd5677cf67b5aa051bb40496e68ad359eb97cfbf8")
if(code === '0x') console.log('外部账户')
else console.log('合约账户')

online tutorial

Guess you like

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