5

I am trying to visualise transactions for Ethereum, currently, I know there are blockchain explorers such as Etherscan.io and Etherchain.org, however I don't want to keep calling their API to get data, rather use my own full node to get all the data I require since it give me greater control.

So I set up a full node and use the Web3JS library to get blocks and transactions. However, the Web3JS doesn't have the option to get an account via given address (unless I am mistaken). So, once I have the transaction object, I can figure out who the sender and receiver are, but I also want to know whether the sender or receiver is a contract or an account. I know on etherchain's API we can get accounts using the address, is it possible to do the same using geth and Web3Js?

Ethereum's yellow paper mentions that each account has a nonce, balance, storageHash and codeHash, so I thought it would be possible to get an account using addresses from the blockchain. Please help we figure out how to determine whether a given address is a contract or an account, without using blockchain explorers mentioned above?

Lee
  • 8,548
  • 6
  • 46
  • 80
Athaheer
  • 175
  • 7

1 Answers1

5

No, it is not possible to determine from the address alone if it is a contract account or an externally owned account.

See this previous post on how contract addresses are generated:

Moreover it is expected that eventually all accounts will be contracts (this is called account abstraction):

You can use web3.js function web3.eth.getCode to get EVM bytecode associated with the address. It is assumed that for externally owned accounts it will return 0x0.

max taldykin
  • 2,966
  • 19
  • 27
  • Thanks, that was the notation I notice on etherchain, and was planning to do. – Athaheer Mar 02 '17 at 09:47
  • While the concept of an externally owned account exists, it will always return 0x0 for web3.eth.getCode. (But 0x0 may also mean an account that just hasn't had a contract deployed to yet, so this comment is consistent with this answer. So depending on how strict you want to be, checking the return value of getCode could be a sufficient approximation.) – eth Mar 02 '17 at 15:36
  • The previous comment confuses me. If a contract hasn't been deployed yet at an address, then it is not a contract. In this sense, it seems to me that if getCode returns 0x0, then that address is clearly not a contract. Presumably, the OP is asking the question because he/she wants to know if an address that has interacted with his/her dapp is a contract (and perhaps behave differently). If it's already interacted, and getCode returns 0x0, isn't it true that it is not (and never will be) a contract? I'm not saying it is, I just confused by the previous comment. – Thomas Jay Rush Mar 12 '17 at 16:28