Problem
Fixes #29559
The bitcoin-wallet info
and bitcoin-wallet dump
commands currently fail when wallet files are write-protected, throwing “Database opened in readonly mode but read-write permissions are needed” errors. This prevents users from safely inspecting write-protected wallet files, which is important for:
- Security-conscious setups where wallets are stored on read-only filesystems
- Forensic analysis of wallet files without risk of modification
- Backup verification without accidentally corrupting wallet data
- Compliance with principle of least privilege (read-only operations should only require read access)
Solution
This PR adds comprehensive read-only database support to enable bitcoin-wallet info
and dump
commands to work with write-protected wallet files.
Key Changes
Database Layer (src/wallet/db.h
, src/wallet/sqlite.h
, src/wallet/sqlite.cpp
)
- Add
read_only
boolean option toDatabaseOptions
structure - Add
IsReadOnly()
virtual method toWalletDatabase
interface - Modify
SQLiteDatabase
to properly handle read-only mode:- Use
SQLITE_OPEN_READONLY
flag whenread_only=true
- Skip write operations (pragma settings, table creation) in read-only mode
- Maintain full read capability for queries and data retrieval
- Use
Wallet Tool (src/wallet/wallettool.cpp
)
- Update
bitcoin-wallet info
command to useoptions.read_only = true
- Update
bitcoin-wallet dump
command to useoptions.read_only = true
Wallet Loading (src/wallet/walletdb.cpp
)
- Enhance
LoadWallet()
to detect read-only mode viadatabase.IsReadOnly()
- Skip version updates and other write operations when in read-only mode
- Prevent “Wallet corrupted” errors during read-only access
Legacy Support (src/wallet/migrate.h
)
- Add
IsReadOnly()
method toBerkeleyRODatabase
for consistency
Testing
The implementation has been thoroughly tested:
Manual Testing
- ✅ Created test wallets and made them read-only (
chmod 444
) - ✅ Verified
bitcoin-wallet info
works with read-only files - ✅ Verified
bitcoin-wallet dump
works with read-only files - ✅ Confirmed identical output between read-write and read-only access
- ✅ Verified normal read-write operations remain unaffected
Unit Tests
All existing wallet tests continue to pass, confirming no regression in normal wallet operations.
Safety Verification
- File permissions are respected (no writes attempted on read-only files)
- Data integrity maintained (read-only access produces identical results)
- Backwards compatibility preserved (existing workflows unaffected)
Security Considerations
This change improves security by:
- Enabling inspection of wallet files without write access (principle of least privilege)
- Preventing accidental wallet corruption during read-only operations
- Supporting secure forensic analysis workflows
- Reducing attack surface for read-only operations
The implementation is conservative and safe:
- Only affects explicitly read-only operations (
info
anddump
commands) - No changes to consensus or validation logic
- No impact on normal wallet operations
- Uses SQLite’s built-in read-only mode for safety
Backwards Compatibility
Full backwards compatibility is maintained:
- Existing wallet operations work identically
- No API changes for normal use cases
- No changes to wallet file formats
- No impact on other Bitcoin Core components
Related Work
This builds upon existing read-only database patterns in Bitcoin Core, particularly the BerkeleyRODatabase
class used for wallet migration scenarios.