SparkWallet
TheSparkWallet
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
Methods
initialize({ mnemonicOrSeed, signer, options }: SparkWalletProps)
Creates and initializes a new SparkWallet instance.
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.- 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.
Promise<string>
: The identity public key as a hex string
getSparkAddress()
Gets the Spark Address of the wallet.
Promise<SparkAddressFormat>
: The Spark Address
getTransfers(limit?: number, offset?: number)
Gets all transfers for the wallet.
limit
: (Optional, default: 20) Maximum number of transfers to returnoffset
: (Optional, default: 0) Offset for pagination
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.
- Object containing:
balance
: The wallet’s current balance in satoshistokenBalances
: Map of token public keys to token balances with Bech32m token identifiers
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.
Promise<string>
: A Bitcoin address for depositing funds
getUnusedDepositAddresses()
Gets all unused deposit addresses for the wallet.
Promise<string[]>
: Array of unused deposit addresses
transfer(params)
Transfers Bitcoin to another Spark wallet.
params
: Object containing:receiverSparkAddress
: The recipient’s Spark AddressamountSats
: The amount in satoshis to transfer
Promise<WalletTransfer>
: The completed transfer details
payLightningInvoice(params: PayLightningInvoiceParams)
Pays a Lightning invoice.
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.amountSatsToSend
: (Optional) Amount in satoshis to send. This parameter is only used for zero-amount invoices. For regular invoices with a fixed amount, this parameter is ignored.
Promise<LightningSendRequest>
: The Lightning payment request details
transferTokens(params)
Transfers tokens to another user.
params
: Object containing:tokenIdentifier
: The Bech32m token identifier (eg: btkn1…) of the token to transfertokenAmount
: The amount of tokens to transferreceiverSparkAddress
: The recipient’s Spark AddressselectedOutputs
: (Optional) Specific outputs to use for the transfer
Promise<string>
: The transaction ID of the token transfer
getTokenInfo()
Gets information about tokens owned by the wallet.
Promise<TokenInfo[]>
: Array of token information objects
queryTokenTransactions()
Retrieves token transaction from the network. Can optionally filter by ownerPublicKeys, issuerPublicKeys, tokenTransactionHashes, tokenIdentifiers, or outputIds.
ownerPublicKeys
: (Optional) Array of owner public keys to filter byissuerPublicKeys
: (Optional) Array of issuer public keys to filter bytokenTransactionHashes
: (Optional) Array of token transaction hashes to filter bytokenIdentifiers
: (Optional) Array of Bech32m token identifiers to filter byoutputIds
: (Optional) Array of output IDs to filter by
Promise<TokenTransactionWithStatus[]>
: Array of token transactions with status
getTokenL1Address()
Gets the Layer 1 Bitcoin address for token operations.
Promise<string>
: The Layer 1 Bitcoin address
createLightningInvoice(params)
Creates a Lightning invoice for receiving payments.
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
Promise<LightningReceiveRequest>
: The Lightning receive request details
getLightningReceiveRequest(id: string)
Gets the status of a Lightning receive request (invoice).
id
: The ID of the invoice to check.
Promise<LightningReceiveRequest | null>
: The Lightning receive request details or null if not found.
getLightningSendRequest(id: string)
Gets the status of a Lightning send request.
id
: The ID of the Lightning send request to check.
Promise<LightningSendRequest | null>
: The Lightning send request details or null if not found.
getLightningSendFeeEstimate(params)
Estimates the fee for sending a Lightning payment.
params
: Object containing:encodedInvoice
: The BOLT11-encoded Lightning invoice.
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.
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.feeQuote
: (Required) The fee quote object returned bygetWithdrawalFeeQuote
. Must be used before it expires.deductFeeFromWithdrawalAmount
: (Optional, default:true
) Whentrue
, fees are deducted from the withdrawal amount. Whenfalse
, fees are deducted from the remaining wallet balance.
Promise<CoopExitRequest | null | undefined>
: The withdrawal request details, or null/undefined if the request cannot be completed.
getWithdrawalFeeQuote(params)
Gets a fee quote for a cooperative exit (on-chain withdrawal). The quote includes options for different speeds and an expiry time and must be passed to withdraw
before it expires.
params
: Object containing:amountSats
: The amount in satoshis to withdraw.withdrawalAddress
: The Bitcoin address where the funds should be sent.
Promise<WithdrawalFeeQuote | null>
: A fee quote for the withdrawal, or null if not available.
getCoopExitRequest(id: string)
Gets a cooperative exit request by ID.
id
: The ID of the cooperative exit request.
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.
txId
: The transaction ID of the Bitcoin deposit.
Promise<WalletLeaf[]>
: The wallet leaves resulting from the claim operation.
getStaticDepositAddress()
Returns a Bitcoin address that users can deposit funds to as many times as they like. Currently Spark only supports one static deposit address per wallet. If you try to create a second address, it will return your existing static deposit address.
Promise<string>
: A reusable Bitcoin address for depositing funds
getClaimStaticDepositQuote(txId: string, outputIndex?: number)
Gets a quote for claiming a deposit made to a static deposit address. This method returns the transaction details, output index, credit amount in satoshis, and the SSP signature required for claiming.
txId
: The Bitcoin transaction ID of the deposit transaction.outputIndex
: (Optional) The index of the output
Promise<object>
: Object containing transaction details and claiming information
claimStaticDeposit(params)
Claims a deposit made to a static deposit address using the quote information from getClaimStaticDepositQuote
.
params
: Object containing:transactionId
: The Bitcoin transaction ID from the quotecreditAmountSats
: The amount of sats from the quotesspSignature
: The SSP signature from the quoteoutputIndex
: (Optional) The index of the output
Promise<ClaimStaticDepositOutput | null>
: The claim result or null if the operation fails
refundStaticDeposit(depositTxId: string, destinationAddress: string, feeSats: number)
Refunds a deposit made to a static deposit address back to a specified Bitcoin address. The minimum fee is 300 satoshis.
depositTxId
: The transaction ID of the original depositdestinationAddress
: The Bitcoin address to send the refund tofeeSats
: The fee to pay for the refund transaction (minimum 300 sats)
Promise<string>
: The refund transaction as a hex string that needs to be broadcast
advancedDeposit(txHex: string)
Non-trusty flow for depositing funds to the wallet. Construct the tx spending from an L1 wallet to the Spark address.
txHex
: The hex string of the transaction to deposit.
Promise<TreeNode[]>
: The nodes resulting from the deposit.
getSwapFeeEstimate(amountSats: number)
Gets the estimated fee for a swap of leaves.
amountSats
: The amount of sats to swap.
Promise<LeavesSwapFeeEstimateOutput>
: The estimated fee for the swap.
cleanupConnections()
Cleans up connections and aborts any active streams.
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.
event
: The event name to listen forlistener
: The callback function to execute when the event is emitted
this
: The SparkWallet instance for chaining
once(event: string, listener: Function)
Adds a one-time listener for the specified event.
event
: The event name to listen forlistener
: The callback function to execute when the event is emitted
this
: The SparkWallet instance for chaining
off(event: string, listener: Function)
Removes the specified listener from the specified event.
event
: The event name- **`