3

我正在使用 web3 制作自己的桌面 BSC 钱包。目前我正在使用

private_key = "private key"
account = w3.eth.account.privateKeyToAccount(private_key)

但我想使用“hello john Pizza guitar”之类的助记词创建帐户。我一直在寻找,但我无法实现它。

4

2 回答 2

3

此时请求的功能在 Web3.py 中不稳定

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也匹配 Ethereum 和 BSC。迭代最后一个数字,您将获得下一个帐户。account 对象可以管理一些操作

account.address # The address for the chosen path
account.key # Private key for that address
于 2021-08-24T21:07:48.777 回答
1

如果您不关心使用未经审计的功能,您可以使用:

    w3.eth.account.enable_unaudited_hdwallet_features()
    account = w3.eth.account.from_mnemonic("hello john pizza guitar")
    print(account.address)

我在文档中找不到任何提及未经审计的功能,但只需查看此(帐户)对象的属性,我会发现您具有以下属性:

  • 地址
  • 加密
  • 钥匙
  • 私钥
  • 符号哈希
  • 签署交易
  • sign_message
  • 签名交易

完整列表(包括私有属性):

['__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
于 2021-08-24T21:09:31.820 回答