14

I am building an application that would create a wallet for a user. One option is the web3.personal API in web3.py, which has a newAccount('passphrase') method. The method only returns the address of created account.

What I'm looking for is a function similar to the eth.accounts API in web3.js, which has a create([entropy]) method. It returns an account object with 'address', 'privatekey' and other details.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Saujanya Acharya
  • 143
  • 1
  • 1
  • 6

2 Answers2

17

Edit: I removed the deprecated pyethereum solution, replaced with the better eth-account one.

Setup

At shell: pip install eth_account

Generating Account

The eth-account library will help you create a private key with an attached address:

>>> from eth_account import Account

>>> acct = Account.create('KEYSMASH FJAFJKLDSKF7JKFDJ 1530')
>>> acct.privateKey
b"\xb2\}\xb3\x1f\xee\xd9\x12''\xbf\t9\xdcv\x9a\x96VK-\xe4\xc4rm\x03[6\xec\xf1\xe5\xb3d"
>>> acct.address
'0x5ce9454909639D2D17A3F753ce7d93fa0b9aB12E'

Adding some of your own randomness above helps address potential limitations of os.urandom, which depends on your version of Python, and your operating system. Obviously use a different string of randomness than the 'KEYSMASH...' one from above.

For more information about using the private key, see this doc with common examples, like signing a transaction.


As a side-note, you may find more support at ethereum.stackexchange.com

carver
  • 2,229
  • 12
  • 28
  • Thank you very much for the detailed instructions. However, I wanted to know if this key pair can be used to perform actual transactions (sorry, I'm very new to this field). Also, I wanted to store a WalletID, Private Key, and Password (for the private key, similar to password used in myetherwallet) in my database. Is that possible? – Saujanya Acharya Sep 01 '17 at 18:01
  • This is a broad extension from the focused one you asked before. They would probably be best split into focused questions. Also, you will get more support if you show the work you tried along the way. A site like reddit.com/r/ethdev might be better for open-ended "How do I get started?" questions. – carver Sep 01 '17 at 18:46
0

you can create private/public key pair with pure computations, bypassing the web3.py accounts api.

install requirements: pip install coincurve pysha3

from secrets import token_bytes
from coincurve import PublicKey
from sha3 import keccak_256

private_key = keccak_256(token_bytes(32)).digest()
public_key = PublicKey.from_valid_secret(private_key).format(compressed=False)[1:]
addr = keccak_256(public_key).digest()[-20:]
print('private_key:', private_key.hex())
print('eth addr: 0x' + addr.hex())

reference: https://www.arthurkoziel.com/generating-ethereum-addresses-in-python/

Valentin Kantor
  • 1,799
  • 1
  • 23
  • 27