Skip to main content

Utils

Mnemonic, invoice, swap-id, protocol-version, and swap-fee utilities - thin wrappers over the wasm free functions, plus a client-side invoice validator.

Functions​

amountMsatFromInvoice()​

function amountMsatFromInvoice(bolt11: string): bigint;

Extracts the amount from a BOLT11 invoice, in millisatoshis. Throws if the invoice does not specify an amount.

Parameters​

ParameterType
bolt11string

Returns​

bigint


destAsset()​

function destAsset(
info: InfoResponse,
chain: Chain,
asset?: string): {
dest_asset_id: string;
dest_chain: Chain;
};

The dest_chain and dest_asset_id pair naming a swap's on-chain destination.

The two fields have to agree, so they come from one lookup rather than being written out separately. The asset id is read from getInfo because the chains identify their native asset differently - Bitcoin uses the "native" sentinel, Liquid the asset's 32-byte hex, which is network-specific - so resolving it from the server's policy keeps it correct on every network. A Liquid LiquidParams' policyAsset is this id.

Pass asset only to name a non-native asset by its policy label.

Parameters​

ParameterType
infoInfoResponse
chainChain
asset?string

Returns​

{
dest_asset_id: string;
dest_chain: Chain;
}
NameType
dest_asset_idstring
dest_chainChain

expiryAtFromInvoice()​

function expiryAtFromInvoice(bolt11: string): number;

Extracts the invoice expiration time, as seconds since the Unix epoch. Throws if the invoice does not specify an expiry.

Parameters​

ParameterType
bolt11string

Returns​

number


generateMnemonic()​

function generateMnemonic(wordCount: number): string;

Generates a random BIP39 mnemonic. Pass the result to Wallet.fromMnemonic and show it to the user for backup.

Parameters​

ParameterTypeDescription
wordCountnumberPhrase length: 12, 15, 18, 21, or 24. 24 words (256 bits of entropy) is the recommended default.

Returns​

string


networkFromInvoice()​

function networkFromInvoice(bolt11: string): string;

Extracts the network string from a BOLT11 invoice (e.g. "Regtest").

Parameters​

ParameterType
bolt11string

Returns​

string


newSwapId()​

function newSwapId(): string;

Generates a new random swap id: a UUIDv4 as a 36-character hyphenated string. Pass to AnySwapClient.createSwap.

Returns​

string


paymentHashFromInvoice()​

function paymentHashFromInvoice(bolt11: string): string;

Extracts the payment hash from a BOLT11 invoice, as hex.

Parameters​

ParameterType
bolt11string

Returns​

string


protocolVersion()​

function protocolVersion(): string;

The protocol version this SDK speaks: the release's major and minor version. Pass as protocol_version to AnySwapClient.createSwap; a server on another version refuses the swap.

Returns​

string


swapAmounts()​

function swapAmounts(
receiveAmount: bigint,
info: InfoResponse,
source: SwapEndpoint,
dest: SwapEndpoint): {
sendAmount: bigint;
swapFee: bigint;
};

The swap fee and total send amount for a swap delivering receiveAmount from source to dest, with the rates read from getInfo. The base fee is the sum of the on-chain legs (fee_estimates[source].source if the source is on-chain, plus fee_estimates[dest].dest if the destination is on-chain); the service rate comes from the destination (policy.chain[dest].service_ppm, or policy.lightning.service_ppm when the destination is Lightning). Returns satoshis, with sendAmount = receiveAmount + swapFee. This one formula covers a SwapIn (chain -> lightning), SwapOut (lightning -> chain), and SwapChain (chain -> chain).

Parameters​

ParameterType
receiveAmountbigint
infoInfoResponse
sourceSwapEndpoint
destSwapEndpoint

Returns​

{
sendAmount: bigint;
swapFee: bigint;
}
NameType
sendAmountbigint
swapFeebigint

validateInvoice()​

function validateInvoice(
bolt11: string,
info: InfoResponse,
source: SwapEndpoint): void;

Validates a BOLT11 claim invoice for a swap into Lightning (a SwapIn) whose on-chain source, against getInfo: the network matches policy.network (case-insensitively), the amount is a whole number of sats, and the invoice does not expire before the deposit can confirm on the source chain (> (confs + 1) * block_time seconds). The driver's quote checks the amount limits. Throws on failure.

Parameters​

ParameterType
bolt11string
infoInfoResponse
sourceSwapEndpoint

Returns​

void


validateMnemonic()​

function validateMnemonic(mnemonic: string): boolean;

Validates a BIP39 mnemonic. Returns true if valid, false otherwise.

Parameters​

ParameterType
mnemonicstring

Returns​

boolean