11

Disclaimer: this is a rather long question.

To my understanding, contract address and wallet address are two profoundly different concepts, with the only thing in common being the fact that they are both given as the hexadecimal representation of a 160-bit number.

The difference is described below:

When a contract is deployed, it is allocated at a unique address, i.e., no other contract on the chain can be allocated on that address.

Moreover, suppose that the contract holds state (global) variables with a total size of X bytes (including whatever padding added by the Solidity compiler).

The next time a contract is deployed to the chain, it will necessarily be allocated at the address of the previous contract + X, or higher.

So every contract occupies "real" memory.

A wallet address, on the other hand, is "flat".

It does not store any data, so two wallets can technically have two consecutive addresses.

So while the state variables of a contract are allocated "right after" the address of the contract, the money that a wallet holds is not allocated "right after" the address of the wallet, and in fact - is not allocated at all.

Instead, the money that a wallet holds is ascribed to the wallet address in one or more ERC20 contracts.

For example, suppose I have a wallet with 20 CoffeeCoins at address 0x1234....

Then it means that there is some ERC20 contract deployed on the chain, which stores this information:

contract CoffeeCoin is ERC20Basic {
    mapping(address => uint256) balances;
    ...
}

And in that contract, balances[0x1234...] = 20.

A minor implication of the difference described above:

Some wallet address out there can be equal to some contract address out there.

I believe that in the case of block-chain, the word address is probably more suitable to be used alongside contract rather than alongside wallet (while using it for both creates this confusion to begin with).

A wallet address is in fact nothing more than a public key.

Am I correct in each one of the claims made above?

Is my understanding of the difference between contract address and wallet address correct?

I will be happy to get any constructive comment.

Thank you.

1
  • Which are the non-contract based eth wallet or app which have non-contract based wallet. In blockchain eth wallet is contract based wallet or non-contract based wallet?
    – Vinod
    Jul 10, 2019 at 6:03

2 Answers 2

11

An account address - either an externally owned account (what you're calling a wallet), or a contract account - isn't the same thing as a memory address. It's not possible to know what exists at "contract address + X", because account addresses aren't a mapping into a contiguous piece memory.

As a primer, see the big picture (literally) here: https://ethereum.stackexchange.com/a/6413/52

An address acts as a key into the blockchain's state trie. The leaf nodes of this trie are account data structures. The data structure contains:

  • a nonce
  • a balance
  • a storage root
  • a codehash

To my understanding, contract address and wallet address are two profoundly different concepts,

The addresses just provide a key into the state trie, so they act in the same way and should be considered the same.

The main difference is that the account data structure for a contract contains an entry for codehash, meaning there's some executable code that can be run. For an externally owned account, there's just a balance and nonce.

The contract's storage state - i.e. it's variable states, etc. - is stored in another trie, with the root of that trie being the storage root found in the account data structure mentioned earlier. This trie is a key:value mapping, and is probably what you're thinking about when you mention "contract's address + X". Yes, when changes to the contract's storage are made, further memory is required in this trie. It's just we've moved further down the organisational heirarchy, away from the initial use of the address as a primary means to identify an account. These are two different address spaces.

Some wallet address out there can be equal to some contract address out there.

That can't happen. Only one account can exist at a given (account) address on the chain. (What can happen is that many private keys can map to the same address, but that's orthogonal to what you're asking.)

A wallet address is in fact nothing more than a public key.

It's derived from the last 20-bytes of the keccak256() hash of the public key.

See: How are ethereum addresses generated?

1
  • 1
    Thank you very much for the very elaborated answer. I see now that a lot of the claims that I made and the image that I depicted were wrong, because I've been looking at contracts in the "traditional" way of looking at executable images, memory space layout, etc. I suppose that in the implementation level, some of it might actually be correct, but as you said - I was trying to depict things at a higher layer, and on that layer, this kind of depiction is incorrect. Thank you! Jun 20, 2018 at 12:45
7

In Javascript,

EOA(wallet address): '0x' + keccak256(publicKey).slice(-40)

Like above, wallet address can be achieved by public key. 1) hash it with keccak256. 2) slice rightmost 20 bytes(40 words). 3) Add a '0x' prefix

Contract address: '0x' + keccak256(rlp.encode([sender, nonce])).toString('hex').slice(-40)

Like above, contract address can be achieved by sender's nonce and sender's address. 1) encode sender's address and sender's nonce with rlp. 2) hash it with keccak256. 3) slice right most 20 bytes(40 words). 4) Add a '0x prefix.

You can check generating contract address by yourself in this site, https://blockchains.tools/contractAddress?l=ETH

You can check yourself how to generate EOA from public key as well.

1) Get a public key from this site, https://blockchains.tools/key?l=ETH 2) Paste it the public key to keccak256 input. https://blockchains.tools/keccak256?l=GENERAL 3) Check rightmost 20 bytes(40 words) are same with the address generated from step 1

:)

1
  • Looks like the links are dead :/ Otherwise thanks for posting this.
    – sr9yar
    Mar 5, 2021 at 6:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.