Sub topic 15
Wallet security practices
This chapter assumes you know how a private key, public key and address relate, how transactions are signed and broadcast, and why transfers cannot be reversed. It covers the habits that keep keys and funds safe. It builds on the key chapters and on the list of common mistakes.
The security model in one idea
Whoever can produce a valid signature from your private key controls the funds. No bank, support desk or password reset exists on the chain. Every practice below does one of three things:
- It keeps the key from being copied.
- It keeps you from signing something you did not intend.
- It limits how much a single failure can cost.
Threats worth planning for
| Threat | How it happens | Main defence |
|---|---|---|
| Phishing site | A fake wallet or exchange page asks for your seed phrase or a connection | Bookmarks, hardware confirmation, never typing a seed phrase into a website |
| Malicious approval | You sign a token approval that lets another address spend your tokens | Review approvals, set limits, revoke unused ones |
| Address swapping | Malware replaces a copied address in your clipboard | Compare the full address on a trusted screen |
| Fake support | A stranger offers help and asks for your recovery phrase | Treat any request for the phrase as a scam |
| Device compromise | Malware or a stolen device exposes a hot wallet key | Hardware wallet, separate devices, small hot balances |
| Lost backup | Fire, water or forgetting where the phrase is stored | Redundant, durable, private backups |
Protect the seed phrase
Most wallets generate a seed phrase (also called a recovery phrase), usually 12 or 24 words. All your keys derive from it. Anyone who has the phrase has the funds, from any device, forever.
Follow these rules:
- Write the phrase on paper or stamp it into metal. Do this offline.
- Never photograph it, type it into a notes app, email it, or store it in cloud storage or a password manager that syncs.
- Never enter it into a website, a chat, or a "verification" form. Legitimate wallets ask for it only during recovery, inside the wallet app itself.
- Keep at least two copies in separate physical locations, so one disaster does not destroy your only backup.
- Test recovery before you rely on the wallet. Restore the phrase on a spare device with a small balance and confirm that the same addresses appear.
An optional passphrase, sometimes called the 25th word, adds a second secret. A phrase without it opens an empty decoy wallet. The trade-off is that losing the passphrase loses the funds, and the passphrase must be backed up separately from the words.
Hot wallets, cold wallets and hardware wallets
A hot wallet keeps its key on an internet-connected device such as a browser extension or phone app. It is convenient and exposed. A cold wallet keeps the key on a device that does not expose it to the network. A hardware wallet is a dedicated device that stores the key and signs transactions internally. The computer sends it a transaction, and the device shows the details on its own screen. You approve with a button, and only the signature leaves the device.
The key point is that the hardware wallet's screen is the trusted display. If your computer is infected, it can show you a false recipient, but the device screen shows what you are really signing. Always verify the recipient address and amount there.
A practical split:
- A spending wallet (hot) holds an amount you would accept losing, such as the cash in a physical wallet.
- A savings wallet (hardware) holds the rest and moves funds only when needed.
- A throwaway wallet handles unfamiliar apps, mints and airdrops, and never holds valuable assets.
Buy hardware wallets directly from the manufacturer. Initialize the device yourself and generate your own phrase. If a device arrives with a pre-written phrase or a "setup card," do not use it.
Verify before you sign
Most losses come from the signing moment, not from cracking keys. Before approving:
- Check the network. The same address format works on many chains, as the chapter on networks explained.
- Check the full recipient address, not only the first and last few characters. Address-poisoning attacks send you a tiny transaction from a lookalike address so that it appears in your history, hoping you copy it later.
- For a first payment to a new recipient, send a small test amount, confirm it arrived, then send the rest.
- Read what the wallet says the transaction does. A request to "sign a message" or "approve" is not a payment, but it can still give someone access.
Token approvals
An ERC-20 approval lets a spender address move your tokens up to an allowance, without asking again. Many apps request an unlimited allowance for convenience. If that app's contract is later exploited, or the approval was granted to a malicious contract, your tokens can be taken without any new signature from you.
- Approve only the amount you need when the wallet allows editing the allowance.
- Review active approvals periodically with an approvals tool such as the block explorer's token approval checker.
- Revoke approvals you no longer use. A revoke is itself a transaction and costs gas.
The following script reads the allowance that an address has granted to a spender, so you can see what is exposed. It uses ethers v6 and a read-only provider, so no key is needed.
import { JsonRpcProvider, Contract, formatUnits } from "ethers";
const abi = [
"function decimals() view returns (uint8)",
"function allowance(address owner, address spender) view returns (uint256)",
];
const provider = new JsonRpcProvider(process.env.RPC_URL);
const token = new Contract(process.env.TOKEN_ADDRESS, abi, provider);
const decimals = await token.decimals();
const allowance = await token.allowance(
process.env.OWNER_ADDRESS,
process.env.SPENDER_ADDRESS
);
console.log("Allowance:", formatUnits(allowance, decimals));A result near 115792089237316195423570985008687913039486009292023342 followed by more digits (the largest 256-bit number, scaled by decimals) indicates an unlimited approval.
Handling keys in code
If you script transfers, as in the earlier code examples, the key becomes a secret in your software. Common errors:
- Hard-coding a private key in source code, which then lands in version control.
- Printing keys or full wallet objects in logs.
- Using a key with real funds on a development machine or in a shared CI system.
Safer practice:
- Read the key from an environment variable or a secrets manager, never from a committed file. Add any
.envfile to.gitignore. - Use a dedicated key for each script, funded with only what the script needs.
- Develop on a testnet with a throwaway key. Switch to mainnet only after the logic is proven.
- For production systems, keep keys in a hardware security module or a managed signing service, so the application requests signatures and never holds the key.
- If a key ever appears in a repository, even briefly, treat it as compromised. Move the funds to a new key, because deleting the commit does not remove copies.
Recognizing scams
Scams follow a few patterns. The details change, but the pressure and the request stay the same.
- Urgency: "Your wallet is at risk, verify now."
- Impersonation: support staff or project admins who contact you first, often by direct message.
- Too-good offers: guaranteed returns, doubled deposits, surprise airdrops that need a connection or an approval.
- Requests for the phrase or a screen share: real support never needs your seed phrase.
- Unsolicited tokens: an unknown token that appears in your wallet, often with a link to "claim" or sell it. Ignore it. Interacting with it can trigger a malicious approval.
A sound rule is that you start every interaction. Navigate to services through your own bookmarks, not through links in messages or search ads.
What to do after a suspected compromise
Act quickly, because attackers often drain accounts in seconds.
- Stop using the affected device or key.
- From a clean device, create a new wallet with a new phrase.
- Move the remaining assets to the new wallet, highest value first. Remember that the old key still pays for gas on its own account, so the attacker may be watching it. If a sweeper bot drains any native coin you send for fees, consider a service that bundles the transactions.
- Revoke approvals from the old address if you can still afford the gas.
- Do not send funds back to the old address, and do not trust "recovery services" that contact you. They are a common second scam.
Checklist
- Seed phrase written offline, stored in two places, recovery tested.
- Hardware wallet for savings, small hot wallet for spending.
- Recipient, network and amount verified on a trusted screen.
- Test transaction for new recipients.
- Approvals limited and reviewed.
- Keys in code kept out of source control and logs.
- No one else ever sees the phrase.
Exercise
You get a direct message from "wallet support" saying your account has a problem. The message links to a page that asks you to enter your 12-word recovery phrase to "re-sync." What should you do, and why?
Show answer
Do not enter the phrase and do not follow the link. Anyone who has the phrase controls all the funds derived from it, and real support never needs it. Close the page, report the message, and navigate to the service only through your own bookmark. If you already entered the phrase, treat the wallet as compromised and move the assets to a new wallet with a new phrase from a clean device.
Exercise
A decentralized app asks for an unlimited approval on your stablecoin so that future swaps need fewer prompts. Why is this risky, and what can you do?
Show answer
An unlimited allowance lets the spender contract move all your tokens of that type at any time without another signature. If the contract is exploited or malicious, your balance can be drained. Edit the approval to the amount you need, review your approvals regularly, and revoke ones you no longer use. A revoke costs gas, but that is small compared with the risk.