跳至主要内容
本页面使用机器翻译自英语,可能包含错误或不清楚的语言。如需最准确的信息,请参阅英文原文。由于更新频繁,部分内容可能与英文原文有出入。请加入我们在 Crowdin 上的努力,帮助我们改进本页面的翻译。 (Crowdin translation page, Contributing guide)

V3 keystore

本例演示如何加密和解密 keystore V3

web3py_ext导入extend,将 web3 扩展为 kaia web3

eth_account导入Account

定义 ** keystore v3**,此步骤说明如何从文件中准备 keystore

从文件中读取密钥库并使用 "密码 "解密

获取密钥存储的公钥和私钥

您可以使用 Account.encrypt 创建一个具有相同地址和不同密码的新密钥存储。

检查 new_keystore 是否与 v3_keystore_str 具有相同的 publicprivate 密钥。

keystoreV3.js

from web3py_ext import extend
from eth_account import Account
v3_keystore_str = '''{
"address": "029e786304c1531af3ac7db24a02448e543a099e",
"id": "9d492c95-b9e3-42e3-af73-5c77e932208d",
"version": 3,
"crypto": {
"cipher": "aes-128-ctr",
"cipherparams": {"iv": "bfcb88a1501e2bb1e6694c03da18953d"},
"ciphertext": "076510b4e25d5cfc31239bffcad6036fe543cbbb04b9f3ec719bf4f61b58fc05",
"kdf": "scrypt",
"kdfparams": {
"salt": "79124f05995aae98b3088d8365f59a6dfadd1c9ed249abae3c07733f4cbbee53",
"n": 131072,
"dklen": 32,
"p": 1,
"r": 8
},
"mac": "d70f83824c2c30dc5cd3a244d87147b6aa713a6000165789a82a467651284ac7"
}
}'''
with open('keystore', 'w') as f:
f.write(v3_keystore_str)
with open('keystore') as f:
pk = Account.decrypt(f.read(), 'password')
acc = Account.from_key(pk)
print(acc.address, acc.key.hex())
# create a new keystore with other password
new_keystore=Account.encrypt(acc.key.hex(),'password1')
new_pk = Account.decrypt(new_keystore, 'password1')
acc = Account.from_key(pk)
print("New keystore")
print(acc.address, acc.key.hex())

output

❯ python keystore-v3.py
0x029e786304c1531aF3aC7db24A02448e543A099E 0x1b33a48f58d8c85ab142a7375fcf18714d88271f6647cfa6b54f1be66b05a762
New keystore
0x029e786304c1531aF3aC7db24A02448e543A099E 0x1b33a48f58d8c85ab142a7375fcf18714d88271f6647cfa6b54f1be66b05a762

让这个页面变得更好