Skip to main content

V4 keystore

This example demonstrates how to encrypt and decrypt keystore V4.

Import extend from web3py_ext to extend web3 to kaia web3

Import Account class from eth_account

Define a keystore v4, this step illustrates preparing a keystore from file

Read the keystore from file and decrypt it with 'Kaia'

Get the public and private keys of the keystore

You can create a new keystore with the same address and different password by using Account.encrypt. Currently kaia-sdk only supports creating v3 keystore.

Check if the new_keystore has the same public and private key as v4_keystore_str.

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

Make this page better