From ea1d854b8adaf770d29de23ec957aaa3f27b6557 Mon Sep 17 00:00:00 2001 From: Nohyun Nehemiah Kwak Date: Fri, 28 Jun 2024 05:11:13 +0900 Subject: [PATCH 1/2] Update keystore examples --- ethers-ext/example/utils/keystoreV3.js | 20 +++++++--- web3js-ext/example/utils/keystoreV3.js | 55 ++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 5 deletions(-) create mode 100644 web3js-ext/example/utils/keystoreV3.js diff --git a/ethers-ext/example/utils/keystoreV3.js b/ethers-ext/example/utils/keystoreV3.js index a0efebeaf..b6d961d2b 100644 --- a/ethers-ext/example/utils/keystoreV3.js +++ b/ethers-ext/example/utils/keystoreV3.js @@ -1,6 +1,4 @@ -const { - Wallet -} = require("@klaytn/ethers-ext"); +const { Wallet } = require("@klaytn/ethers-ext"); // Eth V3. ethers.Wallet.createRandom().encrypt("password") const encryptedKey = `{ @@ -22,18 +20,30 @@ const encryptedKey = `{ "mac": "d70f83824c2c30dc5cd3a244d87147b6aa713a6000165789a82a467651284ac7" } }`; -const password = "password"; // const address = "0x029e786304c1531aF3aC7db24A02448e543A099E"; // const key = "0x1b33a48f58d8c85ab142a7375fcf18714d88271f6647cfa6b54f1be66b05a762"; +const password = "password"; +const password2 = "password2"; + async function main() { const account = Wallet.fromEncryptedJsonSync(encryptedKey, password); - console.log("decrypted address"); + 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(); \ No newline at end of file diff --git a/web3js-ext/example/utils/keystoreV3.js b/web3js-ext/example/utils/keystoreV3.js new file mode 100644 index 000000000..cb271bb41 --- /dev/null +++ b/web3js-ext/example/utils/keystoreV3.js @@ -0,0 +1,55 @@ +const { Web3 } = require("@klaytn/web3js-ext"); + +const provider = new Web3.providers.HttpProvider("https://public-en-baobab.klaytn.net"); +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(); \ No newline at end of file From 5363da771a56e1fb36f446ab3144d2cd9aaf2bdc Mon Sep 17 00:00:00 2001 From: Nohyun Nehemiah Kwak Date: Wed, 10 Jul 2024 15:20:54 +0900 Subject: [PATCH 2/2] Remove callback function --- ethers-ext/example/utils/keystoreV3.js | 15 +++++----- web3js-ext/example/utils/keystoreV3.js | 40 +++++++++++++------------- 2 files changed, 27 insertions(+), 28 deletions(-) diff --git a/ethers-ext/example/utils/keystoreV3.js b/ethers-ext/example/utils/keystoreV3.js index b6d961d2b..c511b5f08 100644 --- a/ethers-ext/example/utils/keystoreV3.js +++ b/ethers-ext/example/utils/keystoreV3.js @@ -1,4 +1,4 @@ -const { Wallet } = require("@klaytn/ethers-ext"); +const { Wallet, util } = require("@klaytn/ethers-ext"); // Eth V3. ethers.Wallet.createRandom().encrypt("password") const encryptedKey = `{ @@ -35,15 +35,14 @@ async function main() { console.log("\ndecrypted privateKey"); console.log(account.privateKey); - account.encrypt(password2).then((encryptedKey2) => { - const account2 = Wallet.fromEncryptedJsonSync(encryptedKey2, password2); + const encryptedKey2 = await account.encrypt(password2); + const account2 = Wallet.fromEncryptedJsonSync(encryptedKey2, password2); - console.log("\ndecrypted address with new password"); - console.log( account2.address ); + console.log("\ndecrypted address with new password"); + console.log(account2.address); - console.log("\ndecrypted privateKey with new password"); - console.log(account2.privateKey); - }); + console.log("\ndecrypted privateKey with new password"); + console.log(account2.privateKey); } main(); \ No newline at end of file diff --git a/web3js-ext/example/utils/keystoreV3.js b/web3js-ext/example/utils/keystoreV3.js index cb271bb41..185a4fab4 100644 --- a/web3js-ext/example/utils/keystoreV3.js +++ b/web3js-ext/example/utils/keystoreV3.js @@ -30,26 +30,26 @@ 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); - }); - }); - }); + const accounts = await web3.eth.accounts.wallet.decrypt([JSON.parse(encryptedKey)], password); + + console.log("\ndecrypted address"); + console.log(accounts[0].address); + + console.log("\ndecrypted privateKey"); + console.log(accounts[0].privateKey); + + const encryptedKey2 = await web3.eth.accounts.wallet.encrypt(password2); + + // Delete account before adding the same account already existing in the wallet. + web3.eth.accounts.wallet.remove(encryptedKey2[0].address); + + const accounts2 = await web3.eth.accounts.wallet.decrypt(encryptedKey2, password2); + + console.log("\ndecrypted address with new password"); + console.log(accounts2[0].address); + + console.log("\ndecrypted privateKey with new password"); + console.log(accounts2[0].privateKey); } main(); \ No newline at end of file