Sending to a Spark Wallet

Here’s a complete example of creating two wallets and transferring BTC between them:

// Initialize both wallets
const senderWallet = new WalletSDK();
const receiverWallet = new WalletSDK();

// Create/setup both wallets and get their public keys
const senderMnemonic = senderWallet.generateMnemonic();
const receiverMnemonic = receiverWallet.generateMnemonic();

const senderPubkey = await senderWallet.createSparkWallet(senderMnemonic);
const receiverPubkey = await receiverWallet.createSparkWallet(receiverMnemonic);

console.log("Sender Pubkey:", senderPubkey);
console.log("Receiver Pubkey:", receiverPubkey);

// deposit some BTC via L1, Lightning, or another Spark wallet

// Send BTC from sender to receiver
const amount = BigInt(100000); // Amount in satoshis
await senderWallet.transfer(amount, receiverPubkey);
console.log("BTC Transfer Success");

// Verify balances
const senderBalance = await senderWallet.getBtcBalance();
console.log("Sender Balance:", senderBalance);
const receiverBalance = await receiverWallet.getBtcBalance();
console.log("Receiver Balance:", receiverBalance);

Receiving from a Spark Wallet

  1. Share Your Public Key

    const pubkey = sparkWallet.getMasterPublicKey();
    console.log("Public Key:", pubkey);
    

    Provide your public key to the sender to receive funds.

  2. Monitor Incoming Transfers

    Whenever you call syncWallet() or getBtcBalance(), your wallet will automatically sync its state with the network and fetch all pending transactions. Transfers that require prior broadcasts or Lightning payments will take around half a minute on regtest so that they can be claimed by the user. All other transfers will be instant.

    await sparkWallet.syncWallet();
    const btcBalance = await sparkWallet.getBtcBalance();
    console.log("BTC Balance:", btcBalance);
    

Best Practices

  • Verify Public Keys: Always verify the recipient’s public key before initiating a transfer.

Next Steps

Move on to Off-ramp to UMA to learn how to off-ramp via UMA.