Introduction to Wallets and Signers
A Wallet in Ethereum manages a private/public key pair used to cryptographically sign transactions and prove ownership on the network. Wallets implement the Signer API, making them versatile for various cryptographic operations.
Wallet Class Overview
The Wallet class is central to Ethereum transactions, offering functionalities like key management, transaction signing, and network interactions.
Creating Wallet Instances
new Wallet(privateKey [, provider]): Initializes a wallet from a private key, with an optional provider for network connectivity.Wallet.createRandom([options]): Generates a new random wallet. Caution: Losing the private key means irreversible loss of access.Options:
extraEntropy: Adds entropy to the random source.
Wallet.fromEncryptedJson(json, password [, progressCallback]): Decrypts an encrypted JSON wallet (e.g., from Geth or Parity).Wallet.fromMnemonic(mnemonic [, path [, wordlist]]): Creates a wallet from a BIP-039/BIP-044 mnemonic phrase with optional path and wordlist.
Supported Wordlists
| Language | Node.js Import | Browser File |
|---|---|---|
| English (US) | ethers.wordlists.en | Included |
| Italian | ethers.wordlists.it | dist/wordlist-it.js |
| Japanese | ethers.wordlists.ja | dist/wordlist-ja.js |
| Korean | ethers.wordlists.ko | dist/wordlist-ko.js |
| Chinese (Simplified) | ethers.wordlists.zh_cn | dist/wordlist-zh.js |
Wallet Prototype Methods
prototype.connect(provider): Connects a wallet to a new provider, returning a new instance.prototype.address: Public address of the wallet.prototype.privateKey: Keep this secret.prototype.provider: The connected provider (null if none).prototype.mnemonic: Mnemonic phrase (null if unknown).prototype.path: Mnemonic derivation path (null if unknown).
Signing Transactions and Messages
Transaction Signing
let wallet = new ethers.Wallet(privateKey);
let transaction = {
nonce: 0,
gasLimit: 21000,
to: "0x88a5C2d9919e46F883EB62F7b8Dd9d0CC45bc290",
value: ethers.utils.parseEther("1.0")
};
wallet.sign(transaction).then(signedTx => console.log(signedTx));Message Signing
wallet.signMessage("Hello World!").then(signature => console.log(signature));Binary Data Signing
let hash = "0x3ea2f1d0abf3fc66cf29eebb70cbd4e7fe762ef8a09bcc06c8edf641230afec0";
wallet.signMessage(ethers.utils.arrayify(hash)).then(sig => console.log(sig));Blockchain Operations
getBalance([blockTag]): Returns the wallet balance in wei.getTransactionCount([blockTag]): Fetches the nonce.estimateGas(transaction): Estimates gas cost.sendTransaction(transaction): Sends a transaction, auto-populating missing fields.
Example:
wallet.sendTransaction({
to: "0x88a5C2d9919e46F883EB62F7b8Dd9d0CC45bc290",
value: ethers.utils.parseEther("1.0")
}).then(tx => console.log(tx));Encrypted JSON Wallets
prototype.encrypt(password [, options [, progressCallback]]): Encrypts the wallet into JSON format.- Options:
salt,iv,scryptparameters, etc. - Progress Callback: Tracks encryption progress (0โ1).
- Options:
Example:
wallet.encrypt("password123", (progress) => {
console.log(`Encrypting: ${Math.round(progress * 100)}%`);
}).then(json => console.log(json));Signer API
The Signer API is an abstract class for creating custom signers. Key methods:
getAddress(): Returns the account address.signMessage(message): Signs a message (string or binary).sendTransaction(transaction): Sends and populates transactions.
๐ Explore more about Ethereum wallets
FAQ Section
1. What is the difference between a Wallet and a Signer?
A Wallet is a specific implementation of the Signer API, managing private keys and signing capabilities. All Wallets are Signers, but not all Signers are Wallets (e.g., hardware wallets).
2. How do I recover a lost wallet?
If you lose the private key or mnemonic phrase, recovery is impossible. Always store backups securely.
3. Can I use the same wallet across different networks?
Yes, but ensure the chainId in transactions matches the network to prevent replay attacks.
4. What is the safest way to store a wallet?
Use encrypted JSON wallets with strong passwords and offline storage for private keys/mnemonics.
5. How do I handle gas fees?
Use estimateGas to predict costs, and gasPrice to adjust fees dynamically.
6. Are there alternatives to the Wallet class?
Yes, libraries like Web3.js or hardware wallets (Ledger/Trezor) can also manage keys and sign transactions.
Conclusion
Mastering Ethereum wallets and signers is essential for secure transactions and dApp development. Whether using mnemonics, encrypted JSON, or custom signers, always prioritize security and best practices.