本文へスキップ
このページは英語からの機械翻訳を使用しており、誤りや不明瞭な表現が含まれている可能性があります。最も正確な情報については、オリジナルの英語版をご覧ください。頻繁な更新のため、一部のコンテンツはオリジナルの英語になっている可能性があります。Crowdinでの取り組みに参加して、このページの翻訳改善にご協力ください。 (Crowdin translation page, Contributing guide)

V3キーストア

この例では、keystore V3を暗号化および復号化する方法を示す。

Walletクラスを**@kaiachain/ethers-ext/v6**パッケージからインポートします。

暗号化キーパスワード情報を宣言する。

また、**ethers.Wallet.createRandom().encrypt()**関数で暗号化キーを作成することもできます。

encryptedKeyからpasswordaccountを復号化する。

accountのアドレスとprivateKeyを確認できます。

accountを別のパスワードpassword2で暗号化する。 そして、別のencryptedKey2を作成する。

encryptedKey2account2password2で復号化し、account2のアドレスとprivateKeyがencryptedKeyaccountの情報と同じかどうかを確認します。

メイン関数を実行する。

keystoreV3.js

const { Wallet } = require('@kaiachain/ethers-ext/v6')
// 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

ページを改善してください。