【问题标题】:How to create a Web3py account using mnemonic phrase如何使用助记词创建 Web3py 帐户
【发布时间】:2021-09-04 02:31:37
【问题描述】:

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

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

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

【问题讨论】:

    标签: python python-3.x web3 web3py


    【解决方案1】:

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

    account.address # The address for the chosen path
    account.key # Private key for that address
    

    【讨论】:

      【解决方案2】:

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

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

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

      • 地址
      • 加密
      • 私钥
      • signHash
      • signTransaction
      • 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
      

      【讨论】:

        猜你喜欢
        • 2022-07-15
        • 1970-01-01
        • 2020-10-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多