跳至主要内容
本页面使用机器翻译自英语,可能包含错误或不清楚的语言。如需最准确的信息,请参阅英文原文。由于更新频繁,部分内容可能与英文原文有出入。请加入我们在 Crowdin 上的努力,帮助我们改进本页面的翻译。 (Crowdin translation page, Contributing guide)

V4 keystore single

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

@kaiachain/ethers-ext/v6 软件包中导入钱包类。

定义加密密钥库 ** version 4**

指定密钥存储的当前密码新密码

使用当前密码加载密钥库

解密后,您可以访问公钥私钥

您可以使用解密密钥和新密码生成新的密钥存储。 请注意,新的加密密钥将是 keystore v3。

检查新的密钥库 publicprivate 密钥,它将与原始密钥库不同,因为密钥库 v3 ** 不支持 klaytn 账户系统。

keystoreV4-single.js

const { Wallet } = require("@kaiachain/ethers-ext/v6");
// Klaytn V4 with one key. kcn account new --lightkdf
const encryptedKey = `{
"address":"ec5eaa07b4d3cbafe7bf437a1ea9a898209f617c",
"keyring":[
[
{
"cipher":"aes-128-ctr",
"ciphertext":"0a5aa3749b9e83c2a4238445aeb66f59355c0363a54c163e34e454f76e061e47",
"cipherparams":{"iv":"2a0b2e02a61e0f721bd800ea6e23a588"},
"kdf":"scrypt",
"kdfparams":{"dklen":32,"n":4096,"p":6,"r":8,"salt":"538ead57745bcd946b05fe294de08256628d9a0a393fd29ced933ba5fc045b07"},
"mac":"30b5488bc97165bc7ecac8ff8dfec65a75a8ad206450aecff0ac2dfea6f79b08"
}
]
],
"id":"362c0766-f5e3-4b4d-af22-7e89d5fb613a",
"version":4
}`;
const password = "password";
const newPassword = "newPassword";
async function main() {
const account = Wallet.fromEncryptedJsonSync(encryptedKey, password);
console.log("decrypted (address, privateKey)");
console.log(account.klaytnAddr, ", ", account.privateKey);
const v3encryptedKey = await account.encrypt(newPassword);
const newAccount = Wallet.fromEncryptedJsonSync(v3encryptedKey, newPassword);
console.log("\ndecrypted (address, privateKey) with new password");
console.log(newAccount.address, ", ", newAccount.privateKey);
}
main();

output

❯ node keystoreV4-single.js
decrypted (address, privateKey)
0xec5eaa07b4d3cbafe7bf437a1ea9a898209f617c , 0x4062512193ef1dab8ccf3e3d7a4862e3c740bdf11d852954ed48bc73643e354f
decrypted (address, privateKey) with new password
0xEc5eAa07b4d3CbAfe7bf437a1Ea9A898209F617c , 0x4062512193ef1dab8ccf3e3d7a4862e3c740bdf11d852954ed48bc73643e354f

让这个页面变得更好