← Back to Home

Sparkle SDK

Build Lightning-fast NFT applications on Bitcoin

View on GitHub

Quick Start

// Sparkle Protocol integrates with spark.money SDK
import { SparkWallet } from '@buildonspark/spark-sdk';

// Initialize Spark wallet for Lightning payments
const { wallet } = await SparkWallet.initialize({
    options: { network: "MAINNET" }
});

// Inscribe NFT with Sparkle Protocol metadata
const inscription = {
    p: "sparkle",
    v: 4,
    spark: {
        enabled: true,
        address: await wallet.getSparkAddress()
    }
};

// Pay for NFT using Lightning via spark.money
const payment = await wallet.payInvoice({
    encodedInvoice: sellerInvoice,
    amountSatsToSend: 100000,
    preferSpark: true // Use Spark transfer when available
});

console.log('Payment settled:', payment.status);

Installation

Node.js / npm

npm install @sparkle/sdk

Yarn

yarn add @sparkle/sdk

Browser (CDN)

<script src="https://unpkg.com/@sparkle/sdk@/dist/sparkle.min.js"></script>

Core API Reference

SparkleSDK Constructor

new SparkleSDK(config: SparkleConfig)

Creates a new instance of the Sparkle SDK.

Parameters:

  • network - 'mainnet' | 'testnet' | 'regtest'
  • lightning - Lightning configuration object
  • rpcUrl - Bitcoin RPC endpoint (optional)
  • apiKey - API key for premium features (optional)

deploy()

sparkle.deploy(options: DeployOptions): Promise<Collection>

Deploys a new NFT collection with recursive inscription support.

Parameters:

  • name - Collection name
  • supply - Total supply
  • traits - Array of trait layer inscriptions
  • lightning - Enable Lightning trading
  • checkpointWindow - Blocks for checkpointing (default: 72)

mint()

sparkle.mint(options: MintOptions): Promise<NFT>

Mints a new NFT with selected traits.

Parameters:

  • collection - Collection inscription ID
  • traits - Array of trait inscription IDs
  • recipient - Bitcoin address
  • enableLightning - Enable instant trading

transfer()

sparkle.transfer(nftId: string, recipient: string): Promise<Transaction>

Transfers an NFT to a new owner.

checkpoint()

sparkle.checkpoint(nftId: string, state: State): Promise<Checkpoint>

Creates a mandatory state checkpoint on-chain.

Lightning Integration

Opening a Channel

// Open Lightning channel for NFT trading
const channel = await sparkle.lightning.openChannel({
    nodeUri: '03abc...@spark.money:9735',
    localAmount: 1000000, // satoshis
    pushAmount: 0
});

console.log('Channel opened:', channel.channelId);

Instant NFT Trading

// List NFT for instant sale via Lightning
const listing = await sparkle.lightning.list({
    nft: nftId,
    price: 100000, // satoshis
    duration: 86400 // 24 hours
});

// Buy NFT instantly with Lightning payment
const purchase = await sparkle.lightning.buy({
    listing: listing.id,
    invoice: 'lnbc...' // Lightning invoice
});

// Transaction settles in <10ms!

HTLC Management

// Create HTLC for atomic swap
const htlc = await sparkle.lightning.createHTLC({
    amount: 50000,
    paymentHash: sha256(preimage),
    timelock: 144 // blocks
});

// Claim HTLC with preimage
await sparkle.lightning.claimHTLC(htlc.id, preimage);

Complete Examples

Deploy DARKITA Collection

import { SparkleSDK } from '@sparkle/sdk';
import fs from 'fs';

const sparkle = new SparkleSDK({
    network: 'mainnet',
    lightning: { node: 'spark.money' }
});

async function deployDarkita() {
    // First inscribe trait layers
    const traits = [];

    // Inscribe backgrounds
    for (let i = 1; i <= 20; i++) {
        const svg = fs.readFileSync(`./layers/backgrounds/bg_${i}.svg`);
        const inscription = await sparkle.inscribe({
            content: svg,
            contentType: 'image/svg+xml',
            feeRate: 10
        });
        traits.push({
            type: 'background',
            id: inscription.id,
            zIndex: 0
        });
    }

    // Deploy collection
    const collection = await sparkle.deploy({
        name: 'DARKITA',
        symbol: 'DARK',
        supply: 10000,
        traits: traits,
        lightning: true,
        creator: 'bc1p...',
        royalty: 2.5 // 2.5%
    });

    console.log('Collection deployed:', collection.inscriptionId);
    return collection;
}

deployDarkita().catch(console.error);

Marketplace Integration

// Create marketplace with Lightning support
class SparkleMarketplace {
    constructor(sdk) {
        this.sdk = sdk;
        this.listings = new Map();
    }

    async listNFT(nftId, priceInSats) {
        // Create Lightning invoice for payment
        const invoice = await this.sdk.lightning.createInvoice({
            amount: priceInSats,
            description: `NFT Purchase: ${nftId}`
        });

        // Store listing with HTLC
        const listing = {
            nft: nftId,
            price: priceInSats,
            invoice: invoice,
            htlc: await this.sdk.lightning.createHTLC({
                amount: priceInSats,
                paymentHash: invoice.paymentHash
            })
        };

        this.listings.set(nftId, listing);
        return listing;
    }

    async buyNFT(nftId) {
        const listing = this.listings.get(nftId);

        // Pay Lightning invoice
        await this.sdk.lightning.pay(listing.invoice);

        // Transfer NFT atomically
        await this.sdk.transfer(nftId, buyerAddress);

        // Settlement in <10ms!
        return { success: true, txid: transfer.txid };
    }
}

TypeScript Support

Sparkle SDK includes comprehensive TypeScript definitions for type-safe development.

import {
    SparkleSDK,
    Collection,
    NFT,
    LightningChannel,
    SparkleConfig,
    DeployOptions,
    MintOptions
} from '@sparkle/sdk';

// Type-safe configuration
const config: SparkleConfig = {
    network: 'mainnet',
    lightning: {
        node: 'spark.money',
        capacity: 1000000,
        autopilot: true
    }
};

// Async/await with proper types
async function mintNFT(): Promise {
    const sparkle = new SparkleSDK(config);

    const options: MintOptions = {
        collection: 'ord://...',
        traits: ['trait1', 'trait2'],
        recipient: 'bc1p...',
        enableLightning: true
    };

    return await sparkle.mint(options);
}

Type Definitions

interface NFT {
    inscriptionId: string;
    tokenId: number;
    collection: string;
    traits: string[];
    owner: string;
    lightning: {
        enabled: boolean;
        channelId?: string;
        invoice?: string;
    };
    metadata: Record;
}

interface Collection {
    inscriptionId: string;
    name: string;
    symbol: string;
    supply: number;
    minted: number;
    traits: Trait[];
    checkpoint: {
        window: number;
        lastBlock: number;
        merkleRoot: string;
    };
}

SDK Features

⚡ Lightning Fast

Sub-10ms NFT trades through Lightning Network integration

🔐 Cryptographic Security

SHA-256 verification and HTLC atomic swaps

Cost Reduction

Recursive inscriptions minimize on-chain data

🔄 State Determinism

Mandatory checkpointing ensures canonical state

📦 TypeScript Ready

Full type definitions for type-safe development

WARNING: Proposed Specification

Design phase — not yet implemented

Support & Resources