Created
April 5, 2021 19:43
-
-
Save anupam-io/c8c47ea96ec579402c988af68c99d4b6 to your computer and use it in GitHub Desktop.
Wallets in web3 on ETH
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rpcEndpoint = "YOUR-RPC-URL"; | |
password = "PASSWORD"; | |
const Web3 = require("web3"); | |
const provider = new Web3.providers.HttpProvider(rpcEndpoint); | |
const web3 = new Web3(provider); | |
const assert = require("assert"); | |
async function main() { | |
// Creating a wallet | |
myWallet = await web3.eth.accounts.wallet.create(1, "Beautiful flower"); | |
// Adding some accounts in wallet | |
account1 = await web3.eth.accounts.create(); | |
account2 = await web3.eth.accounts.create(); | |
await myWallet.add(account1); | |
await myWallet.add(account2); | |
// Removing an account from wallet | |
await myWallet.remove(account2); | |
// Storing the wallet as keystore with password | |
keystoreArray = await myWallet.encrypt(password); | |
// Decrypting the keystore file with password | |
myRecoveredWallet = await web3.eth.accounts.wallet.decrypt( | |
keystoreArray, | |
password | |
); | |
assert.strictEqual(myWallet, myRecoveredWallet); | |
// Only for browser | |
// await myWallet.save(password); | |
// await web3.eth.accounts.wallet.load(password); | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment