본문으로 건너뛰기

V4 keystore multi

This example demonstrates how to encrypt and decrypt keystore V4 multi.

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

Define an encrypted multi keystore version 4

Specify the current password of the keystore and a new password.

Decrypt the keystore with its current password

After decryped, you can access the public and private key.

You can generate a new keystore using the descrypted key and a new password. Note that the new encrypted key will be keystore v3.

Check the new keystore public and private key, it will differ from original one since keystore v3 does not support klaytn account system.

keystoreV4-multi.js

const {
Wallet
} = require("@kaiachain/ethers-ext/v6");
// 여러 역할 기반 키가 있는 카이아 V4. https://toolkit.klaytn.foundation/misc/generateKeystore
const encryptedKey = `{
"version": 4,
"id":"2d7ad5c1-880f-4920-9b8e-51f852c4802c",
"address":"0x17226c9b4e130551c258eb7b1cdc927c13998cd6",
"keyring":[
[
{
"ciphertext":"eb9bd884ac3cc8bf92e6b0082e9d07198bfc4c1223ccc6e5edf7452ad612b2b5",
"cipherparams":{"iv":"47faf7b0991a051eef698c73fc246f78"},
"cipher":"aes-128-ctr",
"kdf":"scrypt",
"kdfparams":{"dklen":32,"salt":"BA0A3E8DC49A04F8E590F8DF5A590BC6E134B031CE10F46D73D4C459AA4C08F8","N":4096,"R":8,"P":1},
"MAC":"4978D7325E1B9B3EC9FDPD1EC709A5A86FDADE0297EA9DDEEB8C3A7A62AE898"
}
],
[
{
"ciphertext":"1a80c8666bea1a8dfa3082b001ff64c818fb14cf4e02017785e0edcc7a277af4",
"cipherparams":{"iv":"eafbecc65ccc177a5579bf56d5f4ed31"},
"cipher":"aes-128-ctr",
"kdf":"scrypt",
"kdfparams":{"dklen":32,"salt":"6472845219e11e4de094cac8c32a6a4d13e69cd4507780a7a37f5e411e1d895d","n":4096,"r":8,"p":1},
"mac":"86379236d2fd6e9bb3f99f7eebaa3325b51e9fa5ec150ade7a461555c0a14ca3"
},
{
"ciphertext":"0071c41d2956b12be5ebc08a9a5b3a9684b9e410fe2de91d614be977fb2a0bdb",
"cipherparams":{"iv":"1492dfb771030d3d9c9d996c193c03e5"},
"cipher":"aes-128-ctr",
"kdf":"scrypt",
"kdfparams":{"dklen":32,"salt":"f8145aa907a649866e0fbff86011244584ddc86559cf4901f8f69b670c234fd7","n":4096,"r":8,"p":1},
"mac":"eacc58c1ad717ca375697c9fcc80f463a26600f5da1b21327715bf3efa047be5"
}
],
[
{
"ciphertext":"68ffc1e2800a7288ba7baba0f0f8049daeed05379fabfdd3bc017fa85c49ab50",
"cipherparams":{"iv":"17f22d7b8aa1a8a2948fd3629f0b89ed"},
"cipher":"AES-128-CTR",
"KDF":"SCRYPT",
"KDFPARAMS":{"DKLEN":32,"SALT":"FF5E577EC8294320CFE59EF7B1B01EE44D4C9F19C8FBC31F333059C74EB8C6D2","N":4096,"r":8,"p":1},
"mac":"de65d669be044df5e39e678b099424a8692a2da6f3746832862cf2e5d6ada612"
},
{
"ciphertext":"fd4810ee850f0aa5f61a2eafbfc5ca36cfebb42df5c2465cc8ae5188029b188b",
"cipherparams":{"iv":"b00ead13b38e449c268d09fced80ce49"},
"cipher":"aes-128-ctr",
"kdf":"SCRYPT",
"KDFPARAMS":{"DKLEN":32,"SALT":"AF5DBBFB7383045DC7F8A3BFC56CCCFC22A5150A1F87E454d40893A4B6FEA9A1","N":4096,"R":8,"P":1},
"MAC":"6234352852eb18246b94f28f3c3454103289ecf2faaa91115927c53729bb0805"
},
{
"ciphertext":"03b758de6372aa6bedde513ccb282bf8af32bca227c258f3e0fc85ce454d72a4",
"cipherparams":{"IV":"5C20F3E96D0802EAF56670E57FBE3E98"},
"CIPHER":"AES-128-CTR",
"KDF":"SCRYPT",
"KDFPARAMS":{"DKLEN":32,"SALT":"b5ec4e40f5a09a59e90317ce45eb7bcd73a2a9afe70f6f2e32548fd38ed2da3b","n":4096,"r":8,"p":1},
"mac":"99b7f59855f0aa04531cc4a24c7923f75ed8052084de9ec49a2794e3899c3274"
}
]
]
}`;
const password = "password";
const newPassword = "newPassword";
// 복호화된 키스토어 v4 객체를 다시 암호화하는 것이 지원되지 않는지 확인합니다.
// 따라서 이 예제에서는 키스토어 v4를 복호화하여 키소트레 v3로 암호화하는 것만 보여줍니다.
// 키스토어 v4의 각 계정을 키소트레 v3로 암호화할 때,
// 키스토어 v3는 kaia 계정 시스템을 지원하지 않으므로 kaia 주소가 손실되지 않도록 주의하세요.
async function main() {
const accounts = Wallet.fromEncryptedJsonListSync(encryptedKey, password);
console.log("decrypted (address, privateKey)");
for (const account of accounts) {
console.log(account.klaytnAddr, ", ", account.privateKey);
}
console.log("\n새 비밀번호로 (주소, privateKey) 해독했습니다");
for (const account of accounts) {
const v3encryptedKey = await account.encrypt(newPassword);
const newAccount = Wallet.fromEncryptedJsonSync(v3encryptedKey, newPassword);
console.log(newAccount.address, ", ", newAccount.privateKey);
}
}
main();

output

❯ node keystoreV4-multi.js
decrypted (address, privateKey)
0x17226c9b4e130551c258eb7b1cdc927c13998cd6 , 0x278c3d035328daf04ab2597da96dd2d8868fd61a8837030f7d8a85f27b7f1bad
0x17226c9b4e130551c258eb7b1cdc927c13998cd6 , 0xa06d13800719307ea7e2503ea441c2ea49279d0d600a2eec2887b50928869676
0x17226c9b4e130551c258eb7b1cdc927c13998cd6 , 0xc32f4007ffad303db99dee0d79a720e1d70c4b2babf8e33cb28170a16bac467d
0x17226c9b4e130551c258eb7b1cdc927c13998cd6 , 0xc274d13302891d0d91a60891a48fde8c2860018f8dcb6293dcc0b28a238590b0
0x17226c9b4e130551c258eb7b1cdc927c13998cd6 , 0x83c127e5207b70086a702c93f1c9a041f15ce49ee5183ce848f35c64de196eff
0x17226c9b4e130551c258eb7b1cdc927c13998cd6 , 0x48f97204ac4886dfbd819ada04ea31a730c6fc43fcb08900566360ee7402f93b
decrypted (address, privateKey) with new password
0x0cc57a3c4E276A37AB0A98ba6899CAf6037996fB , 0x278c3d035328daf04ab2597da96dd2d8868fd61a8837030f7d8a85f27b7f1bad
0x1F2f81B67d1A718c09221eBeb3F12a7192389663 , 0xa06d13800719307ea7e2503ea441c2ea49279d0d600a2eec2887b50928869676
0xF5D27139C99621859e8D1b0f6Be8BF3b8dAca609 , 0xc32f4007ffad303db99dee0d79a720e1d70c4b2babf8e33cb28170a16bac467d
0x7E39a9097C975E6A63f1e0ade4b7312cF2854F9c , 0xc274d13302891d0d91a60891a48fde8c2860018f8dcb6293dcc0b28a238590b0
0x09859661f2574E80C5a51EA3e0E29cA19D21f513 , 0x83c127e5207b70086a702c93f1c9a041f15ce49ee5183ce848f35c64de196eff
0x3AcFe8529FD4C2028f8A26805F9Bf9bAB2cc41eF , 0x48f97204ac4886dfbd819ada04ea31a730c6fc43fcb08900566360ee7402f93b

페이지를 개선해 주세요