Skip to content

Instantly share code, notes, and snippets.

@anupam-io
Created April 5, 2021 19:43
Show Gist options
  • Save anupam-io/c8c47ea96ec579402c988af68c99d4b6 to your computer and use it in GitHub Desktop.
Save anupam-io/c8c47ea96ec579402c988af68c99d4b6 to your computer and use it in GitHub Desktop.
Wallets in web3 on ETH
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