如何在 Python 中创建以太坊钱包?
How do I create an ethereum wallet in Python?
我正在构建一个可以为用户创建钱包的应用程序。一种选择是 web3.py 中的 web3.personal API,它有一个 newAccount('passphrase')
方法。该方法仅returns创建帐户的地址。
我要找的是类似于web3.js中的eth.accounts API的函数,它有一个create([entropy])
方法。它 returns 具有 'address'、'privatekey' 和其他详细信息的帐户对象。
编辑:我删除了已弃用的 pyethereum
解决方案,替换为更好的 eth-account
解决方案。
设置
在 shell:pip install eth_account
正在生成帐户
eth-account
图书馆将通过附加地址帮助您 create a private key:
>>> 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'
在上面添加一些您自己的随机性有助于解决 os.urandom
的潜在限制,这取决于您的 Python 版本和您的操作系统。显然使用与上面的 'KEYSMASH...'
不同的随机字符串。
有关使用私钥的详细信息,请参阅 this doc with common examples, like signing a transaction。
作为旁注,您可以在 ethereum.stackexchange.com
找到更多支持
您可以通过纯计算创建 private/public 密钥对,绕过 web3.py 个帐户 api。
安装要求: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())
参考:https://www.arthurkoziel.com/generating-ethereum-addresses-in-python/
我正在构建一个可以为用户创建钱包的应用程序。一种选择是 web3.py 中的 web3.personal API,它有一个 newAccount('passphrase')
方法。该方法仅returns创建帐户的地址。
我要找的是类似于web3.js中的eth.accounts API的函数,它有一个create([entropy])
方法。它 returns 具有 'address'、'privatekey' 和其他详细信息的帐户对象。
编辑:我删除了已弃用的 pyethereum
解决方案,替换为更好的 eth-account
解决方案。
设置
在 shell:pip install eth_account
正在生成帐户
eth-account
图书馆将通过附加地址帮助您 create a private key:
>>> 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'
在上面添加一些您自己的随机性有助于解决 os.urandom
的潜在限制,这取决于您的 Python 版本和您的操作系统。显然使用与上面的 'KEYSMASH...'
不同的随机字符串。
有关使用私钥的详细信息,请参阅 this doc with common examples, like signing a transaction。
作为旁注,您可以在 ethereum.stackexchange.com
找到更多支持您可以通过纯计算创建 private/public 密钥对,绕过 web3.py 个帐户 api。
安装要求: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())
参考:https://www.arthurkoziel.com/generating-ethereum-addresses-in-python/