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

V3キーストア

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

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

プロバイダーをセットアップし、プロバイダーを使用してweb3**インスタンスを定義する。

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

また、**web3.eth.accounts.create().encrypt()**関数でキーを作成することもできる。

encryptedKeyからpasswordaccountを復号化する。

アカウントのアドレスとprivateKeyを確認できます。

アカウントを別のpassword2で暗号化する。 そして、別のencryptedKey2を作成する。

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

なぜなら、account2accountと同じであり、すでにウォレットに存在しているからです。したがって、account2を再暗号化する前に、web3.eth.accounts.wallet.remove()関数で既存のaccountを削除する必要があります。

メイン関数を実行する。

keystoreV3.js

const { Web3 } = require('@kaiachain/web3js-ext')
const provider = new Web3.providers.HttpProvider(
'https://public-en-kairos.node.kaia.io'
)
const web3 = new Web3(provider)
// Web3 V3. web3.eth.accounts.create(1).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() {
web3.eth.accounts.wallet
.decrypt([JSON.parse(encryptedKey)], password)
.then((account) => {
console.log('\ndecrypted address')
console.log(account[0].address)
console.log('\ndecrypted privateKey')
console.log(account[0].privateKey)
web3.eth.accounts.wallet.encrypt(password2).then((encryptedKey2) => {
// Delete account before adding the same account already existing in the wallet.
web3.eth.accounts.wallet.remove(encryptedKey2[0].address)
web3.eth.accounts.wallet
.decrypt(encryptedKey2, password2)
.then((account2) => {
console.log('\ndecrypted address with new password')
console.log(account2[0].address)
console.log('\ndecrypted privateKey with new password')
console.log(account2[0].privateKey)
})
})
})
}
main()

output

❯ node keystoreV3.js
復号化済みアドレス
0x029e786304c1531af3ac7db24a02448e543a099e
復号化済みプライベートキー
0x1b33a48f58d8c85ab142a7375fcf18714d88271f6647cfa6b54f1be66b05a762
復号化済みアドレスwith new password
0x029e786304c1531af3ac7db24a02448e543a099e
decrypted privateKey with new password
0x1b33a48f58d8c85ab142a7375fcf18714d88271f6647cfa6b54f1be66b05a762

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