What is @ethersproject/wallet?
@ethersproject/wallet is a part of the ethers.js library, which provides a complete, tiny, and simple library for interacting with the Ethereum blockchain. The wallet module specifically deals with creating and managing Ethereum wallets, signing transactions, and interacting with the Ethereum network.
What are @ethersproject/wallet's main functionalities?
Creating a Wallet
This feature allows you to create a new Ethereum wallet with a random private key. The code sample demonstrates how to create a new wallet and print its address.
const { Wallet } = require('@ethersproject/wallet');
const wallet = Wallet.createRandom();
console.log(wallet.address);
Importing a Wallet from a Private Key
This feature allows you to import an existing wallet using a private key. The code sample demonstrates how to create a wallet from a given private key and print its address.
const { Wallet } = require('@ethersproject/wallet');
const privateKey = '0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef';
const wallet = new Wallet(privateKey);
console.log(wallet.address);
Signing a Message
This feature allows you to sign a message using the wallet's private key. The code sample demonstrates how to sign a message and print the resulting signature.
const { Wallet } = require('@ethersproject/wallet');
const wallet = Wallet.createRandom();
const message = 'Hello, Ethereum!';
wallet.signMessage(message).then(signature => {
console.log(signature);
});
Sending a Transaction
This feature allows you to send a transaction from the wallet. The code sample demonstrates how to connect the wallet to a provider and send a transaction to a recipient address.
const { Wallet } = require('@ethersproject/wallet');
const { providers } = require('@ethersproject/providers');
const wallet = Wallet.createRandom();
const provider = new providers.JsonRpcProvider('https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID');
const walletWithProvider = wallet.connect(provider);
const tx = {
to: '0xrecipientAddress',
value: ethers.utils.parseEther('0.01')
};
walletWithProvider.sendTransaction(tx).then(transaction => {
console.log(transaction);
});
Other packages similar to @ethersproject/wallet
web3
web3.js is a collection of libraries that allow you to interact with a local or remote Ethereum node using HTTP, IPC, or WebSocket. It provides similar functionalities to @ethersproject/wallet, such as creating and managing wallets, signing transactions, and interacting with the Ethereum network. However, web3.js is generally considered to be more heavyweight and has a steeper learning curve compared to ethers.js.
eth-lightwallet
eth-lightwallet is a lightweight JavaScript library for creating and managing Ethereum wallets. It provides functionalities for generating wallets, encrypting and decrypting private keys, and signing transactions. Compared to @ethersproject/wallet, eth-lightwallet is more focused on lightweight operations and is often used in browser environments.
ethereumjs-wallet
ethereumjs-wallet is a simple library for creating and managing Ethereum wallets. It provides functionalities for generating wallets, importing wallets from private keys, and signing transactions. Compared to @ethersproject/wallet, ethereumjs-wallet is more minimalistic and focuses solely on wallet management without additional features for interacting with the Ethereum network.
Ethereum Wallet
This sub-module is part of the ethers project.
It contains the class to manage a private key and signing for a standard
externally-owned account.
For more information, see the documentation.
Importing
Most users will prefer to use the umbrella package,
but for those with more specific needs, individual components can be imported.
const {
Wallet,
verifyMessage
} = require("@ethersproject/wallet");
License
MIT License