跳至主要內容
本頁面使用機器翻譯自英語,可能包含錯誤或不清楚的語言。如需最準確的信息,請參閱英文原文。由於更新頻繁,部分內容可能與英文原文有出入。請加入我們在 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

讓這個頁面變得更好