API Reference
Complete documentation for the Spark Wallet SDK
SparkWallet
The SparkWallet
class is the primary interface for interacting with the Spark network. It provides methods for creating and managing wallets, handling deposits, executing transfers, and interacting with the Lightning Network.
Installation
or npm
Methods
initWallet(mnemonicOrSeed?: Uint8Array | string)
Initializes the wallet using either a mnemonic phrase or a raw seed.
initWallet
will also claim any pending incoming lightning payment, spark transfer, or bitcoin deposit.
Parameters:
mnemonicOrSeed
: (Optional) Either:- A BIP-39 mnemonic phrase as string
- A raw seed as Uint8Array or hex string
- If not provided, generates a new mnemonic and uses it to create a new wallet
Returns:
- Object containing:
mnemonic
: The mnemonic if one was generated (undefined for raw seed)balance
: The wallet’s initial balance in satoshistokenBalance
: Map of token balances and leaf counts
Example:
getBalance()
Gets the current balance of the wallet.
You can use the forceRefetch
option to synchronize your wallet and claim any pending incoming lightning payment, spark transfer, or bitcoin deposit before returning the balance.
Parameters:
forceRefetch
: (Optional) Synchronizes the wallet before returning the balance, which will claim any pending incoming lightning payment, spark transfer, or bitcoin deposit.
Returns:
- Object containing:
balance
: The wallet’s current balance in satoshistokenBalance
: Map of token balances and leaf counts
Example:
getDepositAddress()
Generates a new deposit address for receiving bitcoin funds. Note that this function returns a bitcoin address, not a spark address. For Layer 1 Bitcoin deposits, Spark generates Pay to Taproot (P2TR) addresses. These addresses start with “bc1p” and can be used to receive Bitcoin from any wallet.
Returns:
Promise<string>
: A Bitcoin address for depositing funds
Example:
sendSparkTransfer()
Sends a transfer to another Spark user.
Parameters:
receiverSparkAddress
: The recipient’s Spark addressamountSats
: Amount to send in satoshis
Returns:
Promise<Transfer>
: The completed transfer details
Example:
createLightningInvoice()
Creates a Lightning invoice for receiving payments.
Parameters:
amountSats
: Amount in satoshismemo
: Description for the invoice
Returns:
Promise<string>
: BOLT11 encoded invoice
Example:
payLightningInvoice()
Pays a Lightning invoice.
Parameters:
params
: An object with the following properties:invoice
: (Required) The BOLT11-encoded Lightning invoice to pay
Returns:
Promise<LightningSendRequest>
: The Lightning payment request details
Example:
withdraw()
Initiates a withdrawal to move funds from the Spark network to an on-chain Bitcoin address.
Parameters:
params
: An object with the following properties:onchainAddress
: (Required) The Bitcoin address where the funds should be senttargetAmountSats
: (Optional) The amount in satoshis to withdraw. If not specified, attempts to withdraw all available funds
Returns:
Promise<CoopExitRequest | null | undefined>
: The withdrawal request details, or null/undefined if the request cannot be completed
Example:
Supporting Types
Network
(Enum)
Represents the Bitcoin network to use with the wallet.
CreateLightningInvoiceParams
(Type)
Parameters for creating a Lightning invoice.
PayLightningInvoiceParams
(Type)
Parameters for paying a Lightning invoice.
Transfer
(Interface)
Represents a transfer between users.
TokenBalance
(Type)
Represents a token’s balance and leaf count.
WithdrawParams
(Type)
Parameters for initiating a withdrawal.
CoopExitRequest
(Type)
The result of a withdrawal request.
LightningSendRequest
(Type)
The result of a Lightning payment request.
TokenBalanceMap
(Type)
Map of token balances indexed by token public key.