簡體   English   中英

"在 web3.py 中的 personal.newAccount 之后訪問私鑰"

[英]Access private key after personal.newAccount in web3.py

我在 python 3.6 中使用web3.py<\/code>創建了一個以太坊帳戶:

web3.personal.newAccount('password')

當您在節點上創建一個帳戶( w3.personal.newAccount() )時,該節點將托管私鑰 ; 不打算直接訪問它。

如果您必須具有對私鑰的本地訪問權限,則可以:

如果節點是geth,則提取密鑰如下所示:

with open('~/.ethereum/keystore/UTC--...4909639D2D17A3F753ce7d93fa0b9aB12E') as keyfile:
    encrypted_key = keyfile.read()
    private_key = w3.eth.account.decrypt(encrypted_key, 'correcthorsebatterystaple')

安全提示 - 請勿將密鑰或密碼保存在任何位置,尤其是共享源文件中

假設您已激活 geth 的個人 rpc<\/strong> ,要以編程方式執行此操作,而無需在python<\/strong>中硬編碼密鑰庫文件目錄路徑<\/strong>,請執行以下操作:

from web3 import Web3
import eth_keys
from eth_account import account

w3 = Web3(Web3.HTTPProvider('http://127.0.0.1'))
address = '0x...'
password = 'password'

wallets_list = w3.geth.personal.list_wallets()
keyfile_path = (wallets_list[list(i['accounts'][0]['address'] for i in wallets_list).index(address)]['url']).replace("keystore://", "").replace("\\", "/")
keyfile = open(keyfile_path)
keyfile_contents = keyfile.read()
keyfile.close()
private_key = eth_keys.keys.PrivateKey(account.Account.decrypt(keyfile_contents, password))
public_key = private_key.public_key

private_key_str = str(private_key)
public_key_str = str(public_key)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM