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.
Copy
Ask AI
import { SparkWallet } from "@buildonspark/spark-sdk";// Create a new walletconst { wallet, mnemonic } = await SparkWallet.initialize({ options: { network: "REGTEST" // or "MAINNET" }});console.log("New wallet created!");console.log("Mnemonic:", mnemonic);console.log("Address:", wallet.getSparkAddress());
Account index for generating multiple identity keys from the same mnemonic (default: 0 on REGTEST, 1 on MAINNET). Important: Always specify this explicitly if you use the same mnemonic across networks.
Map of Bech32m token identifiers to token balance and metadata. UserTokenMetadata includes extraMetadata?: Uint8Array for arbitrary issuer-defined bytes.
cleanupConnections()Properly closes all network connections and cleans up resources when you’re done using the wallet.
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.
// Initialize wallet with mnemonic or seedinitialize({ mnemonicOrSeed, signer, options })// Get the identity public keygetIdentityPublicKey()// Get the Spark addressgetSparkAddress()// Clean up connectionscleanupConnections()