6

Good day, I was looking at the way contracts addresses are created and it seems that the process is quite deterministic (depends on the nonce). Is there a way to get the address of a contract that wasn't yet deployed for a specific nonce? something like:

contractAddress = getAddress(nonce1, data, privateKey)

I know there used to be the pyethereum library that offered such tool, but it wasn't updated in a long time and I was hoping to use a more active tool.

As always, your help is appreciated!

  • What is data? It also shouldn't be needed. – eth Nov 3 '16 at 1:31
5

Others can add other answers, but pyethereum is still active and this type code doesn't change:

def mk_contract_address(sender, nonce):
    return sha3(rlp.encode([normalize_address(sender), nonce]))[12:]

from How is the address of an Ethereum contract computed?

The private key is not needed, only the sender address. However if the sender is unknown, from the OP's privateKey, sender is computed by:

def privtoaddr(x, extended=False):
    if len(x) > 32:
        x = decode_hex(x)
    o = sha3(privtopub(x)[1:])[12:]
    return add_checksum(o) if extended else o
2

In golang, use ethereum/go-ethereum/crypto.CreateAddress.

func CreateAddress(creator common.Address, nonce uint64) common.Address

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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