본문으로 건너뛰기
This page uses machine translation from English, which may contain errors or unclear language. For the most accurate information, please see the original English version. Some content may be in the original English due to frequent updates. Help us improve this page's translation by joining our effort on Crowdin. (Crowdin translation page, Contributing guide)

V3 keystore

This example demonstrates how to encrypt and decrypt keystore V3.

Import the Wallet class from the @kaiachain/ethers-ext/v5 package.

Encrypted key and password information are declared.

You can also create a encrypted key with the ethers.Wallet.createRandom().encrypt() function.

Decrypt account from the encryptedKey with the password.

You can check address and privateKey of the account.

Encrypt the account with another password password2. And it will make another encryptedKey2.

Re-decrypt the account2 from encryptedKey2 with password2 and check if the address and privateKey of the account2 are same with the info of the account from encryptedKey.

Execute the main function.

keystoreV3.js

const { Wallet } = require('@kaiachain/ethers-ext/v5')
// Eth V3. ethers.Wallet.createRandom().encrypt("password")
const encryptedKey = {
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',
},
}
// const address = "0x029e786304c1531aF3aC7db24A02448e543A099E";
// const key = "0x1b33a48f58d8c85ab142a7375fcf18714d88271f6647cfa6b54f1be66b05a762";
const password = 'password'
const password2 = 'password2'
async function main() {
const account = Wallet.fromEncryptedJsonSync(encryptedKey, password)
console.log('\ndecrypted address')
console.log(account.address)
console.log('\ndecrypted privateKey')
console.log(account.privateKey)
account.encrypt(password2).then((encryptedKey2) => {
const account2 = Wallet.fromEncryptedJsonSync(encryptedKey2, password2)
console.log('\ndecrypted address with new password')
console.log(account2.address)
console.log('\ndecrypted privateKey with new password')
console.log(account2.privateKey)
})
}
main()

output

❯ node keystoreV3.js
decrypted address
0x029e786304c1531af3ac7db24a02448e543a099e
decrypted privateKey
0x1b33a48f58d8c85ab142a7375fcf18714d88271f6647cfa6b54f1be66b05a762
decrypted address with new password
0x029e786304c1531af3ac7db24a02448e543a099e
decrypted privateKey with new password
0x1b33a48f58d8c85ab142a7375fcf18714d88271f6647cfa6b54f1be66b05a762

페이지를 개선해 주세요