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
Generating a vanity address is a simple process of trial-and-error, and the same process can be used to search for accounts having some other property.
Contract addresses are determined by the account of the creating account and its nonce - specifically, they're the hash of the RLP encoding of those two values. Thus, you can search for an account that generates a vanity contract address like this:
- Generate a random private key
- Derive its public key and address
- Derive the address of the first contract it would create from its address and a nonce of zero
- Repeat from 1
Here's some simple code that uses pyethereum to do this:
import os
from ethereum.keys import privtoaddr
from ethereum.utils import mk_contract_address
import zlib
smallest = None
keys = {}
while True:
privkey = os.urandom(32)
addr = privtoaddr(privkey)
contractaddr = mk_contract_address(addr, 0).encode('hex')
compressedsize = len(zlib.compress(contractaddr, 9))
if compressedsize <= 42:
print "0x%s: %d" % (contractaddr, compressedsize)
keys[contractaddr] = privkey.encode('hex')
if smallest is None or long(contractaddr, 16) < long(smallest, 16):
smallest = contractaddr
print "0x%s" % smallest
keys[contractaddr] = privkey.encode('hex')
In this case, it looks for addresses with the most leading zeroes, and the keys that compress the smallest - using zlib as a crude estimator of Kolmogorov Complexity, since addresses with many repetitions will compress smaller.
You can replace the fitness function with anything you would like, of course - to generate the address eventually used for the ENS registry, I used a function that rated addresses based on how many strictly incrementing digits they started with.
Best Answer
Take a look at :
https://github.com/kvhnuke/etherwallet
( and mainly, this file, search for generateSingleWallet : https://github.com/kvhnuke/etherwallet/blob/gh-pages/js/source/01_global.js )
It is the source code for the https://www.myetherwallet.com/ that provides in-browser generation of ethereum addresses.
A simple javascript snippet (using CryptoJS and staticJS/01_ethereumjs-accounts.js libraries from the forementioned repository):