在 web3.py personal.newAccount 之后访问私钥
Access private key after personal.newAccount in web3.py
我在 python 3.6 中使用 web3.py
创建了一个以太坊帐户:
web3.personal.newAccount('password')
如何访问该帐户的私钥?
当你在你的节点上创建一个账户时(w3.personal.newAccount()
),节点hosts the private key;不打算直接访问它。
如果您必须在本地访问私钥,您可以:
如果节点是 geth,提取密钥如下所示:
with open('~/.ethereum/keystore/UTC--...4909639D2D17A3F753ce7d93fa0b9aB12E') as keyfile:
encrypted_key = keyfile.read()
private_key = w3.eth.account.decrypt(encrypted_key, 'correcthorsebatterystaple')
安全提示 -- 不要将密钥或密码保存在任何地方,尤其是在共享源文件中
假设您已激活您的 geth 的个人 rpc,以编程方式执行此操作无需硬编码密钥库文件目录路径 python,执行以下操作:
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)
我在 python 3.6 中使用 web3.py
创建了一个以太坊帐户:
web3.personal.newAccount('password')
如何访问该帐户的私钥?
当你在你的节点上创建一个账户时(w3.personal.newAccount()
),节点hosts the private key;不打算直接访问它。
如果您必须在本地访问私钥,您可以:
如果节点是 geth,提取密钥如下所示:
with open('~/.ethereum/keystore/UTC--...4909639D2D17A3F753ce7d93fa0b9aB12E') as keyfile:
encrypted_key = keyfile.read()
private_key = w3.eth.account.decrypt(encrypted_key, 'correcthorsebatterystaple')
安全提示 -- 不要将密钥或密码保存在任何地方,尤其是在共享源文件中
假设您已激活您的 geth 的个人 rpc,以编程方式执行此操作无需硬编码密钥库文件目录路径 python,执行以下操作:
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)