Class WalletManager
WalletManager provides a high-level interface for wallet lifecycle:
creating new wallets, importing existing wallets from private keys, and
serializing/deserializing wallets to/from encrypted keystore files
(FR-WALLET-02).
Keystore format
Wallets are persisted using a simplified Web3 Secret Storage v3 format: AES-256-CTR encryption with PBKDF2-derived encryption keys. The keystore is a JSON file containing the encrypted private key and all necessary metadata for reconstruction (FR-WALLET-03).
Thread safety
Access to the internal wallet map is synchronized. However, individual
Wallet objects returned by this class are immutable and inherently
thread-safe.
- Since:
- 1.0.0
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Clears all wallets from this manager.Creates a new wallet with a randomly generated key pair.exportKeystore(Wallet wallet, String password) Exports a wallet to an encrypted keystore JSON string.Returns all wallets managed by this manager.Retrieves a wallet by its address.importFromKeystore(String keystoreJson, String password) Imports a wallet from an encrypted keystore JSON string.importWallet(String privateKeyHex) Imports a wallet from a hex-encoded private key.booleanremoveWallet(String address) Removes a wallet from this manager by address.intsize()Returns the number of wallets managed by this manager.
-
Constructor Details
-
WalletManager
public WalletManager()Constructs an empty wallet manager.
-
-
Method Details
-
createWallet
Creates a new wallet with a randomly generated key pair.- Returns:
- a new
Wallet
-
importWallet
Imports a wallet from a hex-encoded private key.- Parameters:
privateKeyHex- lowercase hex-encoded PKCS#8 private key (non-null, non-blank)- Returns:
- the imported
Wallet - Throws:
NullPointerException- if privateKeyHex is nullIllegalArgumentException- if privateKeyHex is blank or invalid
-
getWallet
Retrieves a wallet by its address.- Parameters:
address- the blockchain address to look up (non-null)- Returns:
- an
Optionalcontaining the wallet, or empty if not found - Throws:
NullPointerException- if address is null
-
getAllWallets
Returns all wallets managed by this manager.- Returns:
- unmodifiable collection of wallets
-
size
public int size()Returns the number of wallets managed by this manager.- Returns:
- non-negative integer
-
removeWallet
Removes a wallet from this manager by address.- Parameters:
address- the address to remove (non-null)- Returns:
trueif a wallet was present and removed
-
exportKeystore
Exports a wallet to an encrypted keystore JSON string.The keystore uses Web3 Secret Storage v3 format with AES-256-CTR encryption and a PBKDF2-derived key (FR-WALLET-03).
- Parameters:
wallet- the wallet to export (non-null)password- the encryption password (non-null, non-blank)- Returns:
- JSON keystore string
- Throws:
NullPointerException- if wallet or password is nullIllegalArgumentException- if password is blank
-
importFromKeystore
Imports a wallet from an encrypted keystore JSON string.The keystore must have been created by
exportKeystore(Wallet, String)or be compatible with Web3 Secret Storage v3 format.- Parameters:
keystoreJson- the keystore JSON string (non-null, non-blank)password- the decryption password (non-null, non-blank)- Returns:
- the imported
Wallet - Throws:
NullPointerException- if keystoreJson or password is nullIllegalArgumentException- if keystoreJson or password is blank, or if decryption fails
-
clear
public void clear()Clears all wallets from this manager.
-