跳至主要內容
本頁面使用機器翻譯自英語,可能包含錯誤或不清楚的語言。如需最準確的信息,請參閱英文原文。由於更新頻繁,部分內容可能與英文原文有出入。請加入我們在 Crowdin 上的努力,幫助我們改進本頁面的翻譯。 (Crowdin translation page, Contributing guide)

V4 keystore

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

web3py_ext導入extend,將 web3 擴展為 kaia web3

eth_account導入Account

定義 ** keystore v4**,此步驟說明從文件中準備 keystore

從文件中讀取密鑰庫,並使用 "Kaia"對其進行**解密

獲取密鑰存儲的公鑰和私鑰

您可以使用 Account.encrypt 創建一個具有相同地址和不同密碼的新 keystore。 目前 kaia-sdk 僅支持創建 v3 密鑰存儲。

檢查 new_keystore 是否具有與 v4_keystore_str 相同的 publicprivate 密鑰。

keystorev4.js

from web3py_ext import extend
from eth_account import Account
v4_keystore_str = '''{
"version": 4,
"id": "15f20a72-3719-4f10-b63d-2dbd2658a3ca",
"address": "0x17226c9b4e130551c258eb7b1cdc927c13998cd6",
"keyring": [
{
"ciphertext": "07162b797dcb32dd96686be249f9b3c357642bf5f5454d0fcbefedb265667f37",
"cipherparams": {
"iv": "8611384aaa8147ef54cbca175d2e818c"
},
"cipher": "aes-128-ctr",
"kdf": "scrypt",
"kdfparams": {
"dklen": 32,
"salt": "d7cb1f18d9fb7aa81042f61a61de9c456f829f2dc8b46cb6290d1bb9de89b791",
"n": 4096,
"r": 8,
"p": 1
},
"mac": "ed0a57bbb11d512f7e3c3025e6dc497c15a2fa3aad1fe558b4cad53f887e06d8"
},
{
"ciphertext": "394200b0c8abae159e7f19c3eee55409ec789ac77a970e4c3f6bc1839a4afbfc",
"cipherparams": {
"iv": "3013456d71996e32e231923f289f5c78"
},
"cipher": "aes-128-ctr",
"kdf": "scrypt",
"kdfparams": {
"dklen": 32,
"salt": "fca63eb0670a9bffcc67cb77f43525a8c93d49e0b24acbf7d2a98bff8a5a5cf1",
"n": 4096,
"r": 8,
"p": 1
},
"mac": "880fa7e1240c1deb18898edea28a2baeb57a0c250971c5fea333033a92a958f7"
},
{
"ciphertext": "e04e0c8c4eaf5a22a037c0771f41e5e3c6e09cf624832a340d1c962ac5aa03cb",
"cipherparams": {
"iv": "8777c8e5c244190b026c54b4a2474bf0"
},
"cipher": "aes-128-ctr",
"kdf": "scrypt",
"kdfparams": {
"dklen": 32,
"salt": "e4b4d5b664f6722a5ad41a1b46249204326c952b3a4ac255ae4a0b5128e4803a",
"n": 4096,
"r": 8,
"p": 1
},
"mac": "c618020675a344b3e13dfe9c605115faf32dc80a7d24081b264ebf04fe5db1dd"
}
]
}'''
with open('keystore', 'w') as f:
f.write(v4_keystore_str)
with open('keystore') as f:
pk = Account.v4_decrypt(f.read(), "Kaia")
accs = list(map(lambda acc: Account.from_key_pair(acc['address'], acc['private_key']), pk))
print("Original Keystores")
for acc in accs:
print(acc.address, acc.key.hex())
print("New Keystores")
# kaia-sdk only supports encrypting keystore v3
for acc in accs:
new_keystore=Account.encrypt(acc.key.hex(),'password1')
new_pk = Account.decrypt(new_keystore, 'password1')
new_acc = Account.from_key(new_pk)
print(new_acc.address,new_acc.key.hex())

output

❯ python keystore-v4.py
Original Keystores
0x17226c9B4e130551c258Eb7B1Cdc927c13998cd6 0xeaffa1321040c44cfb8ae21cab8377198ef6706fb7b6b2a09db292b5fe2a5e63
0x17226c9B4e130551c258Eb7B1Cdc927c13998cd6 0x3b0ac1c503373e1a8f4c35a71dbce404846d8a6173538680dea1d149fd5e494c
0x17226c9B4e130551c258Eb7B1Cdc927c13998cd6 0x21558e1796ad364d34a0ad618f8b086c9af2694083a3cb129a0af10dc6b6cc9e
New Keystores
0x17226c9B4e130551c258Eb7B1Cdc927c13998cd6 0xeaffa1321040c44cfb8ae21cab8377198ef6706fb7b6b2a09db292b5fe2a5e63
0x354688C7A16eA109Da83e386A86945Abe395cDfC 0x3b0ac1c503373e1a8f4c35a71dbce404846d8a6173538680dea1d149fd5e494c
0x4B53Df387Cb8d256D65a7385c9a5AEF8DbB12265 0x21558e1796ad364d34a0ad618f8b086c9af2694083a3cb129a0af10dc6b6cc9e

讓這個頁面變得更好