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

# Restore a wallet

> Restore a wallet app built on Bark from the mnemonic or from a wallet database backup, per surface: what each path brings back and what stays in flight.

Your wallet app has to bring a user's wallet back after a reinstall or a change of device.

## Compare what each restore path brings back

[Backups](https://second.tech/docs/patterns/backups.md) explains how both backups are made. This table is what each one gives back.

| What comes back | Mnemonic only | Wallet database backup |
| - | - | - |
| Ark balance | Yes, with the Ark server's cooperation | Yes, without the server |
| On-chain balance | Yes. Barkd and the CLI scan the chain on create. An SDK app runs the scan itself with `initialScan` | Yes |
| Payment history (movements) | No | Yes |
| In-progress exits | Only once the VTXO's own transaction is on-chain or in the mempool. An exit not broadcast yet comes back as a spendable VTXO and has to be started again | Yes |
| Pending delegated participations | Yes, once the round runs. The Ark server posts a notice to the wallet's mailbox when the round completes. The restored wallet rebuilds the participation from it ([delegated co-signer](https://second.tech/docs/patterns/delegated-cosigner.md) covers the deadline) | Yes |
| Operations in flight when the wallet was lost | Not guaranteed. An unregistered board, an unclaimed Lightning receive, or a Lightning send in flight stays lost. An interactive round the Ark server already issued can come back through the server's xpub lookup, like a delegated participation. If the server never issued the round, its input VTXOs come back as ordinary VTXOs | Only what the backup captured |

A database backup restores the wallet as of its last snapshot:

* **Incoming Ark payments and delegated refresh outputs after the snapshot:** Come back through the regular mailbox sync.
* **Other VTXOs the wallet itself created after the snapshot, such as the change of a send or the output of an interactive refresh (current limitation):** Log them as the wallet creates them. Bring them back after the restore. On the SDK, log their IDs and pass them to `recoverVtxos`. On Barkd, log each VTXO's encoding, which `GET /api/v1/wallet/vtxos/{id}/encoded` returns. After the restore, send it to `POST /api/v1/wallet/import-vtxo`. On the Bark CLI, `bark raw-vtxo <VTXO_ID>` prints that encoding. Pass it to `bark dev vtxo import`. A fresh mnemonic restore also brings them back, at the cost of the history.
* **VTXOs spent by a payment after the snapshot (current limitation):** Still count in the balance. Present the restored balance as the balance at the snapshot ([tell the user what came back](https://second.tech/docs/patterns/restore.md#tell-the-user-what-came-back)).
* **Either path:** Needs a reachable chain source to open.

## Pick the path from what the user still holds

* **The old install is still reachable (a change of device):** Move the wallet with the steps below. Restore from the mnemonic only when your app has no way to import a database, and tell the user what that path does not bring back.
* **The old install is gone (a lost or wiped device):** Restore from the latest continuous backup if the app ran one. Otherwise restore from the mnemonic and tell the user what that path does not bring back.

To move a wallet to a new device:

1. Let the old install finish syncing.
2. Stop its background daemon. On the SDK, await `stopDaemonWait()` (`stop_daemon_wait` in Rust). In a browser that call only signals the stop, so let any sync pass in flight finish before you copy.
3. Copy its wallet database.
4. Delete the old install. On Barkd, `DELETE /api/v1/wallet` deletes it once you pass `dangerous: true` and the wallet's fingerprint.
5. Restore on the new device from that copy. It brings back the history and every in-flight state, and needs nothing from the server.

> [!WARNING]
>
> **Never run two wallet apps on the same mnemonic**
>
> Each instance refreshes and spends VTXOs the other never learns about. Bitcoin the surviving instance cannot see is lost when those VTXOs expire. Delete the old install before restoring on the new one.

## Restore from the mnemonic

Create the wallet from the existing mnemonic instead of a fresh one. That first open starts the recovery scan, which rebuilds the spendable Ark balance. The scan asks the Ark server for every VTXO held by the wallet's keys, then reads the wallet's [mailboxes](https://second.tech/docs/patterns/backups.md#mnemonic-backups).

> [tab] Bark SDK
>
> Open the wallet for the first time with the existing mnemonic, leaving `createIfNotExists` at its default of `true`. In Go, set `CreateIfNotExists: true` explicitly, since an unset `bool` field is `false`. The create-if-missing path is what runs the recovery scan, so restore through `open`, not through a separate `create` (`initWallet` in the native bindings). Leave the open option `skipRecovery` (`skip_recovery` in Rust) at its default of `false`.
>
> Call `initialScan` (`initial_wallet_scan` on the Rust on-chain wallet) once after the restore to bring the on-chain balance back:
>
> * **With a bitcoind chain source:** Pass a birthday height, a block height from before the wallet's first transaction, or the scan starts at the genesis block.
> * **With Esplora:** The scan is a gap-limited full scan and needs no height.
> * **With your own callback on-chain wallet:** Run your own history scan instead of `initialScan`.

> [tab] Bark CLI
>
> Add `--mnemonic "<phrase>"` to your `bark create` command. With a bitcoind chain source, add `--birthday-height <height>`, a block height from before the wallet's first transaction. Esplora ignores it. The CLI scans the chain on create, which brings the on-chain balance back.

> [tab] Barkd
>
> Call `POST /api/v1/wallet/create` with the `mnemonic` field set, and `birthday_height` with a bitcoind chain source. Barkd scans the chain on create, which brings the on-chain balance back.

Raise the key gap limit before the restore if your wallet app hands out many addresses that never receive bitcoin. The server's xpub lookup does not use that limit. The mailbox scans match a VTXO only when its key sits within `vtxoKeyGapLimit` consecutive unused key indices (`vtxo_key_gap_limit` in Rust and `config.toml`, `--gap-limit` on `bark create`, `gap_limit` on `POST /api/v1/wallet/create`). They put a VTXO beyond that window in `foreign`. The limit is 250 by default and 100,000 at most. Every match extends the window.

On the SDK, read `recoveryStatus` once the wallet is open (the Rust crate hands the same `RecoveryStatus` to the `on_recovery_finished` callback of `OpenWalletArgs`). The values below are the TypeScript tags for web apps. The other bindings expose the same four cases in their own casing:

| `recoveryStatus` | What it means | What your wallet app does |
| - | - | - |
| `not-run` | The wallet already existed locally with no scan in progress, or you set `skipRecovery`. | Nothing. |
| `failed` | Bark could not start the scan or read its state back. The status carries the error message. Some bitcoin may be missing. | Tell the user, then call `pendingRecovery()` (`pending_recovery` in Rust). If it returns a report, the next full sync resumes the scan. If it returns nothing on the open that created the wallet, delete this install and restore from the mnemonic again on a fresh wallet database. |
| `in-progress` | The scan stopped before the end, on an error or on VTXOs it could not decide yet. `recoveryReport` holds what it decided so far. Some bitcoin may be missing. | Show the restore as still running. Each full wallet sync resumes the scan: `sync()`, `maintenance()`, `maintenanceDelegated()`, and each start of the background daemon. Between them, call `syncPendingRecovery()` to resume it. `pendingRecovery()` returns the current report, or nothing once the scan is done. |
| `completed` | The scan ran to the end and left no entry in `failed`. `recoveryReport` holds its buckets. | Act on the buckets below. |

`recoveryStatus` and `recoveryReport` describe the scan as the open saw it and do not change afterward. `recoveryReport` is present only when the status is `in-progress` or `completed`, so read the status first. While a scan is in progress, read `pendingRecovery()` after each sync and store its `failed` and `abandoned` IDs, so your app still has them once the scan is done. The buckets:

* **`recovered`:** What came back.
* **`failed`:** Entries the scan could not decide yet. They may still hold bitcoin. Each full sync retries them, so a `completed` report has none.
* **`abandoned`:** Entries the scan gave up on after 40 attempts that the server or the chain answered without a decision. They may still hold bitcoin, so retry them with `recoverVtxos(vtxoIds)`.
* **`foreign`:** Entries whose key sits beyond the key gap limit. Retry them with `recoverVtxos(vtxoIds, gapLimit)` and a wider limit, up to 100,000.
* **`skipped`, `exited`, and `notFound`:** No retry. `skipped` holds VTXOs already spent, unclaimed, or unregistered. `exited` holds VTXOs whose exit transaction is already confirmed, handed to the exit process, so progress and claim them like any other [emergency exit](https://second.tech/docs/patterns/exits.md). `notFound` holds IDs the Ark server does not know.
* **`isComplete`:** `true` only when `failed`, `abandoned`, and `foreign` are all empty.

Barkd and the CLI run the same scan. Read its outcome in their log. A scan that could not start logs an error saying bitcoin may be missing. A scan that stopped before the end logs a warning with the number of VTXOs still missing. On the CLI, any command that syncs resumes it. On Barkd, each start of `barkd` and each `POST /api/v1/wallet/sync` resume it.

## Restore from a database backup

Put the backed-up wallet database back where the wallet reads it, then open with the same mnemonic. The wallet opens as an existing wallet, so no recovery scan runs. Its next sync continues the pending boards, rounds, and Lightning payments from the state in the snapshot.

> [tab] Bark SDK
>
> Restore the persisted store before the first open. That store is `db.sqlite` in the directory you pass as `datadir` on iOS, Android, and desktop, or the IndexedDB data your web app shipped out. Then open the wallet with the mnemonic as usual.

> [tab] Bark CLI
>
> Put `db.sqlite`, `config.toml`, and the `mnemonic` file back in the wallet's data directory, such as `~/.bark-mainnet` (`.bark` in the user's home directory by default, `BARK_DATADIR` or `--datadir` to override), then run any command.

> [tab] Barkd
>
> Stop `barkd`. Put `db.sqlite`, `config.toml`, and the `mnemonic` file back in the data directory, and start `barkd` again. Put `auth_token` back as well, or `barkd` generates a new bearer token on its next start. If you use Nostr Wallet Connect, put `nwc.json` back too. With bark-backupd, put `config.toml`, the `mnemonic` file, and `backup.toml` back first, then run `bark-backupd restore` to fetch `db.sqlite`. [Continuous backups](https://second.tech/docs/patterns/backups.md#continuous-backups) covers the backup side.

## Tell the user what came back

Show the recovered balance as soon as the wallet opens, and be explicit about what the path did not bring back.

* **After a mnemonic restore:** Show the Ark balance right away and the on-chain balance once the chain scan completes. Treat a `failed` or `in-progress` recovery status, and any `failed`, `abandoned`, or `foreign` entry of the report, as bitcoin that may still be missing. Say that the payment history starts at the restore, and name any board or Lightning payment that was in flight instead of promising the amount will appear.
* **After a database restore:** Show the balance and history as they were at the last snapshot. An in-progress exit resumes from the database state. Run the exit automation from [exit handling](https://second.tech/docs/patterns/exits.md) on the restored wallet.

> [accordion] How Bark Wallet does it
>
> * **From the mnemonic:** Bark Wallet's welcome screen offers **Import existing wallet**, which calls `POST /api/v1/wallet/create` ([Restore your wallet](https://second.tech/docs/bark-wallet/restore.md) walks users through it). The web version (WASM) opens the SDK wallet with the mnemonic, calls `initialScan` for the on-chain history, and warns the user if that scan fails.
> * **From a database backup:** Each platform brings the wallet directory or the latest snapshot back its own way ([Restore a continuous backup](https://second.tech/docs/bark-wallet/restore.md#restore-a-continuous-backup) covers Start9, Umbrel, and self-hosted installs).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.