> ## Documentation Index
> Fetch the complete documentation index at: https://docs.spark.money/llms.txt
> Use this file to discover all available pages before exploring further.

# Create a Wallet

export const Method = ({children, className = ""}) => {
  return <code className={`spark-method not-prose inline-block px-2 py-1 mt-12 text-sm font-mono font-semibold rounded-md border ${className}`}>
      {children}
    </code>;
};

Create and initialize Spark wallets with full key control.

<Frame className="chill">
  <img className="block dark:hidden" src="https://mintcdn.com/lightspark/RCkJgy8FQAJRUhsh/images/wallets/create-wallet-light.png?fit=max&auto=format&n=RCkJgy8FQAJRUhsh&q=85&s=faf6742206877d45941f090f65be07bb" alt="Create a Wallet" width="3840" height="2160" data-path="images/wallets/create-wallet-light.png" />

  <img className="hidden dark:block" src="https://mintcdn.com/lightspark/RCkJgy8FQAJRUhsh/images/wallets/create-wallet.png?fit=max&auto=format&n=RCkJgy8FQAJRUhsh&q=85&s=08a6acffb83763e58f6f4bab4b7f9ab2" alt="Create a Wallet" width="3840" height="2160" data-path="images/wallets/create-wallet.png" />
</Frame>

***

## 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.

<CodeGroup>
  ```typescript Create Wallet theme={null}
  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:", await wallet.getSparkAddress());
  ```

  ```typescript Import Wallet theme={null}
  import { SparkWallet } from "@buildonspark/spark-sdk";

  // Restore wallet from existing mnemonic
  const { wallet } = await SparkWallet.initialize({
    mnemonicOrSeed: "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about",
    accountNumber: 0, // Optional: specify account index
    options: {
      network: "REGTEST" // or "MAINNET"
    }
  });

  console.log("Wallet restored from mnemonic!");
  console.log("Address:", await wallet.getSparkAddress());
  ```
</CodeGroup>

<Expandable title="Parameters">
  <ResponseField name="mnemonicOrSeed" type="Uint8Array | string" required={false}>
    BIP-39 mnemonic phrase or raw seed. Leave blank to generate a new wallet.
  </ResponseField>

  <ResponseField name="accountNumber" type="number" required={false}>
    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.
  </ResponseField>

  <ResponseField name="signer" type="SparkSigner" required={false}>
    Custom signer implementation for advanced use cases
  </ResponseField>

  <ResponseField name="options" type="ConfigOptions" required={false}>
    Wallet configuration options including network selection
  </ResponseField>
</Expandable>

<Expandable title="Returns">
  <ResponseField name="wallet" type="SparkWallet" required>
    The initialized SparkWallet instance
  </ResponseField>

  <ResponseField name="mnemonic" type="string" required={false}>
    The 12-word mnemonic seed phrase for wallet recovery (undefined for raw seed)
  </ResponseField>
</Expandable>

***

## Essential Wallet Operations

After creating your wallet, you can perform these essential operations

<Method>getIdentityPublicKey()</Method>

Gets the identity public key of the wallet

```typescript theme={null}
const identityKey = await wallet.getIdentityPublicKey();
console.log("Identity Public Key:", identityKey);
```

<Expandable title="Returns">
  <ResponseField name="identityKey" type="string" required>
    The identity public key as a hex string
  </ResponseField>
</Expandable>

<Method>getSparkAddress()</Method>

Gets the Spark address of the wallet

```typescript theme={null}
const sparkAddress = await wallet.getSparkAddress();
console.log("Spark Address:", sparkAddress);
```

<Expandable title="Returns">
  <ResponseField name="sparkAddress" type="SparkAddressFormat" required>
    The Spark address for receiving Bitcoin and tokens
  </ResponseField>
</Expandable>

<Method>getBalance()</Method>

Gets the current balance of the wallet, including Bitcoin and token balances.

```typescript theme={null}
const balance = await wallet.getBalance();
console.log("Balance:", balance.balance, "sats");
console.log("Token Balances:", balance.tokenBalances);
```

<Expandable title="Returns">
  <ResponseField name="balance" type="bigint" required>
    The wallet's current balance in satoshis
  </ResponseField>

  <ResponseField name="tokenBalances" type="Map<string, { balance: bigint, tokenMetadata: UserTokenMetadata }>" required>
    Map of Bech32m token identifiers to token balance and metadata. `UserTokenMetadata` includes `extraMetadata?: Uint8Array` for arbitrary issuer-defined bytes.
  </ResponseField>
</Expandable>

<Method>cleanupConnections()</Method>

Properly closes all network connections and cleans up resources when you're done using the wallet.

```typescript theme={null}
await wallet.cleanupConnections();
console.log("Wallet connections cleaned up");
```

<Expandable title="Returns">
  <ResponseField name="void" type="Promise<void>" required>
    No return value - cleans up connections and aborts active streams
  </ResponseField>
</Expandable>

***

## Network Configuration

Spark supports both mainnet and regtest networks:

<CodeGroup>
  ```typescript Mainnet theme={null}
  const { wallet } = await SparkWallet.initialize({
    options: {
      network: "MAINNET"
    }
  });
  // Use for production applications
  ```

  ```typescript Regtest theme={null}
  const { wallet } = await SparkWallet.initialize({
    options: {
      network: "REGTEST"
    }
  });
  // Use for development and testing
  ```
</CodeGroup>

<Info>
  Always use REGTEST for development and testing. Only use MAINNET for production applications with real Bitcoin.
</Info>

<Warning>
  **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.
</Warning>

***

## Account Derivation

You can create multiple accounts from the same mnemonic by specifying different account numbers:

```typescript theme={null}
// 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:", await wallet0.getSparkAddress());
console.log("Account 1:", await wallet1.getSparkAddress());
```

***

## API Reference

```typescript theme={null}
SparkWallet.initialize({
  mnemonicOrSeed?: Uint8Array | string, // Optional: existing mnemonic or seed
  accountNumber?: number,               // Optional: account index (default: 0)
  signer?: SparkSigner,                 // Optional: custom signer implementation
  options?: {
    network?: "MAINNET" | "TESTNET" | "SIGNET" | "REGTEST" | "LOCAL"
  }
})

// Returns: { wallet: SparkWallet, mnemonic: string | undefined }
```

**Wallet Methods:**

```typescript theme={null}
// 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()
```
