SparkWallet instance.
Method Signature
Parameters
BIP-39 mnemonic phrase or raw seed
Number used to generate multiple identity keys from the same mnemonic
Custom signer implementation for advanced use cases
Wallet configuration options including network selection
Leaf Optimization
Leaf optimization pre-arranges your wallet’s internal structure to enable faster transfers. Without optimization, transfers may require a swap with the SSP (Spark Service Provider), adding latency. With optimization, transfers complete in ~5 seconds.How It Works
Spark creates power-of-2 denomination leaves (1, 2, 4, 8, 16… sats). With one leaf of each denomination, you can transfer any amount without needing an SSP swap. With multiple leaves of each denomination, you can make multiple transfers without swapping.Multiplicity Levels
Themultiplicity setting controls how aggressively to optimize:
| Level | Behavior | Use Case |
|---|---|---|
| 0 | No optimization | Testing only |
| 1 | 1 leaf per denomination | Likely 1 fast transfer before needing a swap |
| 2-4 | Multiple leaves per denomination | Multiple fast transfers |
| 5 | Maximum optimization | Likely 5+ fast transfers without any swaps |
The Tradeoff
For most consumer wallets, fast transfer speeds for 100% of users outweighs unilateral exit costs for a small fraction of users.Auto vs Manual Mode
Auto mode (auto: true, default):
- Optimization runs automatically in the background after sync
- Swaps with SSP when leaves are too far from optimal
- Transfers wait for optimization to complete before sending
- Best for most applications
auto: false):
- You control exactly when optimization runs via
optimizeLeaves() - More aggressive optimization (skips the “is it needed?” check)
- Use when you want maximum optimization regardless of current state
Configuration Examples
Safe to pass on every init. Pass
optimizationOptions every time you initialize (e.g., on app reopen). The SDK only runs optimization if needed and does nothing on wallets with no balance.Returns
The initialized SparkWallet instance
The mnemonic if one was generated (undefined for raw seed)
Example
Multiple SDK Instances
When running multiple wallet instances (e.g., service worker + popup in a browser extension):Multiple instances are safe but may cause temporary claim failures. The SDK handles this automatically—failed claims retry and succeed on subsequent attempts.
- Avoid calling
getStaticDepositAddress()concurrently—this can create duplicate addresses - Let one instance handle background claiming if possible
- Failed claims due to concurrent access are automatically recoverable