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
initialize({ mnemonicOrSeed, signer, options }: SparkWalletProps)
Creates and initializes a new SparkWallet instance.
Parameters:
props
: Object containing:mnemonicOrSeed
: (Optional) BIP-39 mnemonic phrase or raw seedaccountNumber
: (Optional) number used to generate multiple identity keys from the same mnemonicsigner
: (Optional) Custom signer implementationoptions
: (Optional) Wallet configuration options
If no account number is provided, our JS-SDK defaults accountNumber
to 1
to support backwards compatability for mainnet wallets created with earlier
versions of the SDK.
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<SparkAddressFormat>
: The Spark Address
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<{ transfers: WalletTransfer[]; offset: number; }>
: Response containing the list of transfers and offset
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.
Returns:
- Object containing:
balance
: The wallet’s current balance in satoshistokenBalances
: Map of token public keys to token balances
getSingleUseDepositAddress()
Generates a new single-use deposit address for receiving bitcoin funds. Note that this function returns a bitcoin address, not a Spark Address. Once used, this address should not be used again. 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
transfer(params)
Transfers Bitcoin to another Spark wallet.
Parameters:
params
: Object containing:receiverSparkAddress
: The recipient’s Spark AddressamountSats
: The amount in satoshis to transfer
Returns:
Promise<WalletTransfer>
: The completed transfer details
payLightningInvoice(params: PayLightningInvoiceParams)
Pays a Lightning invoice.
Parameters:
params
: Object containing:invoice
: The BOLT11-encoded Lightning invoice to pay.maxFeeSats
: Maximum fee in satoshis to pay for the invoice.preferSpark
: (Optional) Boolean that defaults tofalse
. WhenpreferSpark
is set totrue
, Spark wallets will initiate a Spark transfer instead of a Lightning transfer if a valid Spark address is found in the invoice. If not, a regular Lightning payment will occur.
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 Spark AddressselectedOutputs
: (Optional) Specific outputs to use for the transfer
Returns:
Promise<string>
: The transaction ID of the token transfer
getTokenInfo()
Gets information about tokens owned by the wallet.
Returns:
Promise<TokenInfo[]>
: Array of token information objects
queryTokenTransactions(tokenPublicKeys: string[], tokenTransactionHashes?: string[])
Retrieves token transaction history for specified tokens owned by the wallet. Can optionally filter by specific transaction hashes.
Parameters:
tokenPublicKeys
: Array of token public keys to querytokenTransactionHashes
: (Optional) Array of token transaction hashes to filter by
Returns:
Promise<TokenTransactionWithStatus[]>
: Array of token transactions with status
getTokenL1Address()
Gets the Layer 1 Bitcoin address for token operations.
Returns:
Promise<string>
: The Layer 1 Bitcoin address
createLightningInvoice(params)
Creates a Lightning invoice for receiving payments.
Parameters:
params
: Object containing:amountSats
: Amount in satoshismemo
: (Optional) Description for the invoiceexpirySeconds
: (Optional) Expiry time in seconds, defaults to 30 daysincludeSparkAddress
: (Optional) By passing intrue
, a 36-byte string consisting of a recognizable header and a receiver’s compressed identity public keySPK:identitypubkey
will get embedded in the fallback address (f) field of a BOLT11 invoicereceiverIdentityPubkey
: (Optional) To generate an invoice for another Spark user, pass in the 33-byte compressed identity pubkey as a string
Returns:
Promise<LightningReceiveRequest>
: The Lightning receive request details
Examples:
getLightningReceiveRequest(id: string)
Gets the status of a Lightning receive request (invoice).
Parameters:
id
: The ID of the invoice to check.
Returns:
Promise<LightningReceiveRequest | null>
: The Lightning receive request details or null if not found.
getLightningSendRequest(id: string)
Gets the status of a Lightning send request.
Parameters:
id
: The ID of the Lightning send request to check.
Returns:
Promise<LightningSendRequest | null>
: The Lightning send request details or null if not found.
getLightningSendFeeEstimate(params)
Estimates the fee for sending a Lightning payment.
Parameters:
params
: Object containing:encodedInvoice
: The BOLT11-encoded Lightning invoice.
Returns:
Promise<number>
: The estimated fee in satoshis.
withdraw(params)
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 sent.exitSpeed
: (Required) The desired speed of the exit (FAST, MEDIUM, SLOW).amountSats
: (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:
getWithdrawalFeeEstimate(params)
Gets fee estimate for cooperative exit (on-chain withdrawal).
Parameters:
params
: Object containing:amountSats
: The amount in satoshis to withdraw.withdrawalAddress
: The Bitcoin address where the funds should be sent.
Returns:
Promise<CoopExitFeeEstimatesOutput | null>
: Fee estimate for the withdrawal or null if not available.
getCoopExitRequest(id: string)
Gets a cooperative exit request by ID.
Parameters:
id
: The ID of the cooperative exit request.
Returns:
Promise<CoopExitRequest | null>
: The cooperative exit request details or null if not found.
claimDeposit(txId: string)
Claims a Bitcoin deposit made to a single-use deposit address.
Parameters:
txId
: The transaction ID of the Bitcoin deposit.
Returns:
Promise<WalletLeaf[]>
: The wallet leaves resulting from the claim operation.
advancedDeposit(txHex: string)
Non-trusty flow for depositing funds to the wallet. Construct the tx spending from an L1 wallet to the Spark address.
Parameters:
txHex
: The hex string of the transaction to deposit.
Returns:
Promise<TreeNode[]>
: The nodes resulting from the deposit.
getSwapFeeEstimate(amountSats: number)
Gets the estimated fee for a swap of leaves.
Parameters:
amountSats
: The amount of sats to swap.
Returns:
Promise<LeavesSwapFeeEstimateOutput>
: The estimated fee for the swap.
cleanupConnections()
Cleans up connections and aborts any active streams.
Returns:
Promise<void>
Inherited Methods from EventEmitter
SparkWallet extends EventEmitter, so it inherits the following methods:
on(event: string, listener: Function)
Adds a listener for the specified event.
Parameters:
event
: The event name to listen forlistener
: The callback function to execute when the event is emitted
Returns:
this
: The SparkWallet instance for chaining
Example:
once(event: string, listener: Function)
Adds a one-time listener for the specified event.
Parameters:
event
: The event name to listen forlistener
: The callback function to execute when the event is emitted
Returns:
this
: The SparkWallet instance for chaining
off(event: string, listener: Function)
Removes the specified listener from the specified event.
Parameters:
event
: The event namelistener
: The callback function to remove
Returns:
this
: The SparkWallet instance for chaining
removeAllListeners(event?: string)
Removes all listeners, or those of the specified event.
Parameters:
event
: (Optional) The event name
Returns:
this
: The SparkWallet instance for chaining
Events
SparkWallet emits the following events:
transfer:claimed
Emitted when an incoming transfer is successfully claimed.
Callback Parameters:
transferId
: The ID of the claimed transferupdatedBalance
: The new total balance after claiming the transfer
deposit:confirmed
Emitted when a deposit is marked as available.
Callback Parameters:
depositId
: The ID of the confirmed depositupdatedBalance
: The new total balance after confirming the deposit
stream:connected
Emitted when the stream is connected.
stream:disconnected
Emitted when the stream disconnects and fails to reconnect after max attempts.
Callback Parameters:
reason
: The reason for disconnection
stream:reconnecting
Emitted when attempting to reconnect the stream.
Callback Parameters:
attempt
: The current reconnection attempt numbermaxAttempts
: The maximum number of reconnection attemptsdelayMs
: The delay in milliseconds before the next reconnection attempterror
: The error that caused the reconnection attempt
Supporting Types
BitcoinNetwork
(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.
SparkSigner
(Interface)
Interface for a custom signer implementation.
SparkWalletProps
(Interface)
Properties for initializing a SparkWallet
.
WalletLeaf
(Interface)
Represents a leaf in the wallet’s tree structure, as used by the SDK.
This is often a mapped version of the proto TreeNode
.
TransferDirection
(Enum)
Represents the direction of a transfer (SDK specific enum).
TransferStatus
(Enum)
The possible states of a transfer. (Corresponds to proto TransferStatus
)
TransferType
(Enum)
The types of transfers. (Corresponds to proto TransferType
)
WalletTransferLeaf
(Interface)
Represents a leaf within a transfer, as used by the SDK.
WalletTransfer
(Interface)
Represents a transfer between users, as used by the SDK.
ExitSpeed
(Enum)
Represents the desired speed for a cooperative exit.
TokenOutput
(Interface)
Represents a token output. (Corresponds to proto TokenOutput
)
OutputWithPreviousTransactionData
(Interface)
Represents an output with its previous transaction data, used for token transfers. (Corresponds to proto OutputWithPreviousTransactionData
)
TokenInfo
(Interface)
Represents information about a specific token (SDK specific type).
CoopExitRequest
(Type)
The result of a withdrawal request (SDK specific type).
LightningSendRequest
(Type)
The result of a Lightning payment request (SDK specific type).
LightningReceiveRequest
(Type)
The result of a Lightning invoice creation (SDK specific type).
CurrencyAmount
(Type)
Represents an amount in a specific currency (SDK specific type).
CoopExitFeeEstimatesOutput
(Type)
Fee estimates for cooperative exit (SDK specific type).
TokenTransactionStatus
(Enum)
Status of a token transaction. (Corresponds to proto TokenTransactionStatus
)
TokenMintInput
(Interface)
Input for a token mint operation. (Corresponds to proto TokenMintInput
)
TokenOutputToSpend
(Interface)
Specifies a token output to be spent. (Corresponds to proto TokenOutputToSpend
)
TokenTransferInput
(Interface)
Input for a token transfer operation. (Corresponds to proto TokenTransferInput
)
TokenTransaction
(Interface)
Represents a token transaction. (Corresponds to proto TokenTransaction
)
TokenTransactionWithStatus
(Type)
Represents a token transaction with its status. (Corresponds to proto TokenTransactionWithStatus
)
SparkAddressFormat
(Type)
Represents a formatted Spark address.
SparkWalletEvents
(Interface)
Defines the events that can be emitted by a SparkWallet instance.