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
create({ mnemonicOrSeed, signer, options, lrc20WalletApiConfig }: SparkWalletProps)
Creates and initializes a new SparkWallet instance.
Parameters:
props
: Object containing:mnemonicOrSeed
: (Optional) BIP-39 mnemonic phrase or raw seedsigner
: (Optional) Custom signer implementationoptions
: (Optional) Wallet configuration optionslrc20WalletApiConfig
: (Optional) LRC20 wallet configuration
Returns:
- Object containing:
wallet
: The initialized SparkWallet instancemnemonic
: The mnemonic if one was generated (undefined for raw seed)
getIdentityPublicKey()
Gets the identity public key of the wallet.
Returns:
Promise<string>
: The identity public key as a hex string
getSparkAddress()
Gets the Spark Address of the wallet.
Returns:
Promise<string>
: The Spark Address as a hex string
getTransfers(limit?: number, offset?: number)
Gets all transfers for the wallet.
Parameters:
limit
: (Optional, default: 20) Maximum number of transfers to returnoffset
: (Optional, default: 0) Offset for pagination
Returns:
Promise<QueryAllTransfersResponse>
: Response containing the list of transfers
getBalance()
Gets the current balance of the wallet.
Returns:
- Object containing:
balance
: The wallet’s current balance in satoshistokenBalances
: Map of token balances indexed by token public key
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
getUnusedDepositAddresses()
Gets all unused deposit addresses for the wallet.
Returns:
Promise<string[]>
: Array of unused deposit addresses
payLightningInvoice(params: PayLightningInvoiceParams)
Pays a Lightning invoice.
Parameters:
params
: Object containing:invoice
: The BOLT11-encoded Lightning invoice to pay
Returns:
Promise<LightningSendRequest>
: The Lightning payment request details
transferTokens(params)
Transfers tokens to another user.
Parameters:
params
: Object containing:tokenPublicKey
: The public key of the token to transfertokenAmount
: The amount of tokens to transferreceiverSparkAddress
: The recipient’s public keyselectedLeaves
: (Optional) Specific leaves to use for the transfer
Returns:
Promise<string>
: The transaction ID of the token transfer
withdrawTokens(tokenPublicKey: string, receiverPublicKey?: string, leafIds?: string[])
Withdraws tokens from the Spark network.
Parameters:
tokenPublicKey
: The public key of the token to withdrawreceiverPublicKey
: (Optional) The public key of the receiveroutputIds
: (Optional) Specific token output IDs to withdraw
Returns:
Promise<{ txid: string } | undefined>
: The transaction ID of the withdrawal, or undefined if the withdrawal fails
createLightningInvoice()
Creates a Lightning invoice for receiving payments.
Parameters:
amountSats
: Amount in satoshismemo
: Description for the invoice
Returns:
Promise<string>
: BOLT11 encoded invoice
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:
getPendingTransfers()
Gets all pending transfers.
Returns:
Promise<Transfer[]>
: The pending transfers
claimTransfer(transfer: Transfer)
Claims a specific transfer.
Parameters:
transfer
: The transfer to claim
Returns:
Promise<{ nodes: TreeNode[] }>
: The claim result containing the new nodes
claimTransfers()
Claims all pending transfers.
Returns:
Promise<boolean>
: True if any transfers were claimed
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.
TreeNode
(Interface)
Represents a node in the Spark tree.
TransferStatus
(Enum)
The possible states of a transfer.
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.