For an ERC20 wallet embedded in a website to be fully non-custodial, the private keys must be generated, stored, and controlled exclusively by the user, never touching the website's servers. Here's how this can be achieved:
Client-Side Key Generation and Storage
The wallet should generate private keys entirely in the user's browser using cryptographically secure random number generation. Libraries like ethers.js or web3.js can handle this client-side. The private key should never be transmitted to any server.
For storage, you have several options:
Browser Local Storage: The private key can be encrypted with a user-provided password and stored in the browser's localStorage or IndexedDB. This keeps the key on the user's device, but it's vulnerable if the device is compromised.
Hardware Wallet Integration: The most secure approach is supporting hardware wallets like Ledger or Trezor through Web3 providers. The private key never leaves the hardware device, and the website only receives signed transactions.
Browser Extension Wallets: Integrating with MetaMask or similar browser extensions means the website never handles private keys directly. Users approve transactions through the extension's interface.
Transaction Signing
All transaction signing must happen client-side. When a user wants to send tokens, the website should:
- Construct the transaction parameters (to, value, data, etc.)
- Sign the transaction locally using the user's private key
- Broadcast only the signed transaction to the Ethereum network
The website should never receive the private key, only the signed transaction data.
Key Recovery and Backup
To maintain non-custodial principles while providing recovery options:
- Generate a mnemonic seed phrase (BIP39) that users must write down and store securely
- Optionally allow encrypted backup files that users download and store themselves
- Never store recovery phrases or backup files on your servers
Security Considerations
To ensure the wallet remains truly non-custodial:
- Use Content Security Policy headers to prevent malicious script injection
- Implement Subresource Integrity (SRI) for all external libraries
- Consider using a separate subdomain for wallet operations to isolate it from other site functionality
- Regularly audit your code and dependencies for vulnerabilities
- Make your wallet code open source so users can verify it doesn't transmit keys
User Experience Trade-offs
Non-custodial wallets place more responsibility on users. They must:
- Securely store their seed phrases or private keys
- Remember their passwords (if using encrypted storage)
- Understand that lost keys mean permanently lost funds
The website should clearly communicate these responsibilities and provide educational resources about key management.
By following these principles, you can create a truly non-custodial wallet where users maintain complete control over their funds, while your website merely provides the interface for interacting with the Ethereum blockchain.