Skip to main content
Create and initialize Spark wallets with full key control.
Create a Wallet

Initialize Wallet

The initialize method is the primary way to create or restore a Spark wallet. Leave mnemonicOrSeed blank to generate a new wallet, or provide an existing mnemonic seed phrase to import an existing wallet.
import { SparkWallet } from "@buildonspark/spark-sdk";

// Create a new wallet
const { wallet, mnemonic } = await SparkWallet.initialize({
  options: {
    network: "REGTEST" // or "MAINNET"
  }
});

console.log("New wallet created!");
console.log("Mnemonic:", mnemonic);
console.log("Address:", wallet.getSparkAddress());

Essential Wallet Operations

After creating your wallet, you can perform these essential operations getIdentityPublicKey() Gets the identity public key of the wallet
const identityKey = await wallet.getIdentityPublicKey();
console.log("Identity Public Key:", identityKey);
getSparkAddress() Gets the Spark address of the wallet
const sparkAddress = await wallet.getSparkAddress();
console.log("Spark Address:", sparkAddress);
getBalance() Gets the current balance of the wallet, including Bitcoin and token balances.
const balance = await wallet.getBalance();
console.log("Balance:", balance.balance, "sats");
console.log("Token Balances:", balance.tokenBalances);
cleanupConnections() Properly closes all network connections and cleans up resources when you’re done using the wallet.
await wallet.cleanupConnections();
console.log("Wallet connections cleaned up");

Network Configuration

Spark supports both mainnet and regtest networks:
const { wallet } = await SparkWallet.initialize({
  options: {
    network: "MAINNET"
  }
});
// Use for production applications
Always use REGTEST for development and testing. Only use MAINNET for production applications with real Bitcoin.
Account number defaults differ by network. REGTEST defaults to account 0, MAINNET defaults to account 1. If you test on REGTEST without specifying accountNumber, then deploy to MAINNET with the same mnemonic, your wallet will be empty because funds are on a different account. Always explicitly set accountNumber for consistent behavior.

Account Derivation

You can create multiple accounts from the same mnemonic by specifying different account numbers:
// Account 0 (default)
const wallet0 = await SparkWallet.initialize({
  mnemonicOrSeed: "your mnemonic here",
  accountNumber: 0
});

// Account 1
const wallet1 = await SparkWallet.initialize({
  mnemonicOrSeed: "your mnemonic here", 
  accountNumber: 1
});

// Each account will have different addresses
console.log("Account 0:", wallet0.getSparkAddress());
console.log("Account 1:", wallet1.getSparkAddress());

API Reference

SparkWallet.initialize({
  mnemonicOrSeed?: string,     // Optional: existing mnemonic or seed
  accountNumber?: number,      // Optional: account index (default: 0)
  signer?: Signer,            // Optional: custom signer implementation
  options: {
    network: "MAINNET" | "REGTEST"
  }
})

// Returns: { wallet: SparkWallet, mnemonic: string }
Wallet Methods:
// Initialize wallet with mnemonic or seed
initialize({ mnemonicOrSeed, signer, options })

// Get the identity public key
getIdentityPublicKey()

// Get the Spark address
getSparkAddress()

// Clean up connections
cleanupConnections()