Skip to main content

V3 keystore

This example demonstrates how to encrypt and decrypt keystore V3.

Import extend from web3py_ext to extend web3 to kaia web3

Import Account class from eth_account

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

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

Get the public and private key of the keystore

You can create a new keystore with the same address and different password by using Account.encrypt

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

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

Make this page better