如何使用助记词创建 Web3py 帐户
How to create a Web3py account using mnemonic phrase
我正在使用 web3 制作我自己的桌面 BSC 钱包。目前我正在使用
private_key = "private key"
account = w3.eth.account.privateKeyToAccount(private_key)
但我想使用助记词创建帐户,例如“hello john pizza guitar”。我一直在寻找,但我无法实现。
目前请求的功能在 Web3.py
中 不稳定
- 选项 1:使用像 Ethereum Mnemonic Utils 这样的库来处理你的种子。
- 选项 2:在 web3py 中启用未经审核的功能
w3 = Web3()
w3.eth.account.enable_unaudited_hdwallet_features()
account = web3.eth.account.from_mnemonic(my_mnemonic, account_path="m/44'/60'/0'/0/0")
注意:默认的account_path
也匹配以太坊和BSC。迭代最后一个数字你会得到下一个账户。帐户对象可以管理一些操作
account.address # The address for the chosen path
account.key # Private key for that address
如果您不关心使用未经审核的功能,您可以使用这个:
w3.eth.account.enable_unaudited_hdwallet_features()
account = w3.eth.account.from_mnemonic("hello john pizza guitar")
print(account.address)
我在文档中找不到任何关于未经审核的功能的提及,但只需查看此(帐户)对象的属性,我就会发现您具有以下属性:
- 地址
- 加密
- 关键
- 私钥
- 符号哈希
- 签署交易
- sign_message
- sign_transaction
完整列表(包括私有属性):
['__abstractmethods__', '__bytes__', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__slots__', '__str__', '__subclasshook__', '__weakref__', '_abc_impl', '_address', '_key_obj', '_private_key', '_publicapi', 'address', 'encrypt', 'key', 'privateKey', 'signHash', 'signTransaction', 'sign_message', 'sign_transaction']
您可能不应该使用此帐户对象来签署交易,因为它没有记录在案,并且在文档的所有示例中,交易通常使用 web3.eth.sign_transaction(txn, key) 使用私钥签署。您将很难找到有关此对象及其功能的信息,由于 vscode 自动完成
,我无意中发现了此功能
相反,使用它来检索私钥并按照文档中的说明使用它
pk = account.privateKey
我正在使用 web3 制作我自己的桌面 BSC 钱包。目前我正在使用
private_key = "private key"
account = w3.eth.account.privateKeyToAccount(private_key)
但我想使用助记词创建帐户,例如“hello john pizza guitar”。我一直在寻找,但我无法实现。
目前请求的功能在 Web3.py
中 不稳定- 选项 1:使用像 Ethereum Mnemonic Utils 这样的库来处理你的种子。
- 选项 2:在 web3py 中启用未经审核的功能
w3 = Web3()
w3.eth.account.enable_unaudited_hdwallet_features()
account = web3.eth.account.from_mnemonic(my_mnemonic, account_path="m/44'/60'/0'/0/0")
注意:默认的account_path
也匹配以太坊和BSC。迭代最后一个数字你会得到下一个账户。帐户对象可以管理一些操作
account.address # The address for the chosen path
account.key # Private key for that address
如果您不关心使用未经审核的功能,您可以使用这个:
w3.eth.account.enable_unaudited_hdwallet_features()
account = w3.eth.account.from_mnemonic("hello john pizza guitar")
print(account.address)
我在文档中找不到任何关于未经审核的功能的提及,但只需查看此(帐户)对象的属性,我就会发现您具有以下属性:
- 地址
- 加密
- 关键
- 私钥
- 符号哈希
- 签署交易
- sign_message
- sign_transaction
完整列表(包括私有属性):
['__abstractmethods__', '__bytes__', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__slots__', '__str__', '__subclasshook__', '__weakref__', '_abc_impl', '_address', '_key_obj', '_private_key', '_publicapi', 'address', 'encrypt', 'key', 'privateKey', 'signHash', 'signTransaction', 'sign_message', 'sign_transaction']
您可能不应该使用此帐户对象来签署交易,因为它没有记录在案,并且在文档的所有示例中,交易通常使用 web3.eth.sign_transaction(txn, key) 使用私钥签署。您将很难找到有关此对象及其功能的信息,由于 vscode 自动完成
,我无意中发现了此功能相反,使用它来检索私钥并按照文档中的说明使用它
pk = account.privateKey