[英]How do I create an ethereum wallet in Python?
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.一种选择是 web3.py 中的web3.personal API ,它有一个
newAccount('passphrase')
方法。 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.我正在寻找的是一个类似于 web3.js 中的eth.accounts API的函数,它有一个
create([entropy])
方法。 It returns an account object with 'address', ' privatekey ' and other details.它返回一个包含“地址”、“私钥”和其他详细信息的帐户对象。
Edit: I removed the deprecated pyethereum
solution, replaced with the better eth-account
one.编辑:我删除了已弃用的
pyethereum
解决方案,取而代之的是更好的eth-account
解决方案。
At shell: pip install eth_account
在外壳:
pip install eth_account
The eth-account
library will help you create a private key with an attached address: eth-account
库将帮助您创建带有附加地址的私钥:
>>> 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.在上面添加一些您自己的随机性有助于解决
os.urandom
的潜在限制,这取决于您的 Python 版本和您的操作系统。 Obviously use a different string of randomness than the 'KEYSMASH...'
one from above.显然使用与上面的
'KEYSMASH...'
不同的随机字符串。
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作为旁注,您可以在ethereum.stackexchange.com找到更多支持
you can create private/public key pair with pure computations, bypassing the web3.py accounts api.您可以通过纯计算创建私钥/公钥对,绕过 web3.py 帐户 api。
install requirements: pip install coincurve pysha3
安装要求:
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/参考: https ://www.arthurkoziel.com/generating-ethereum-addresses-in-python/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.