Wake the app every few hours
Send a background push from your server to every device about every 4 hours. The wallet app decides locally what is due, so your server never needs the user’s VTXOs. The first pass submits a delegated refresh, and a later pass completes it (complete each refresh on a later wake). At this cadence, both passes run well inside the mainnet default refresh window of 144 blocks, about 24 hours.- iOS
- Android
Send a background notification, which launches or resumes your app in the background without showing anything:
- In Xcode, add the Push Notifications capability.
- Add the Background Modes capability and select Remote notifications.
- Call
registerForRemoteNotifications()at every launch. - Send the device token that iOS returns to your server.
- From your server, send a payload whose
apsdictionary holds onlycontent-available, with the headers below. - Run the pass from
application(_:didReceiveRemoteNotification:fetchCompletionHandler:).
- Priority 5 only: APNs treats priority 10 as an error for a background push. It applies 10 when the header is missing.
- Bundle ID as the topic: A background push must carry your app’s bundle ID in
apns-topic. - Best-effort delivery: iOS can delay, group, throttle, or drop background pushes (catch devices that stop waking covers the fallback).
- Expo push service: Send iOS devices only
datawithcontentAvailable: trueandpriority: "normal". The service defaults iOS to high priority, which maps to APNs priority 10.
- iOS: Enable the
fetchbackground mode, list the task identifier inBGTaskSchedulerPermittedIdentifiers, and submit aBGAppRefreshTaskRequest. iOS picks the launch time and gives the task up to 30 seconds.earliestBeginDateis only a lower bound. The wait can be many hours. - Android: Enqueue periodic WorkManager work. The minimum interval is 15 minutes. While the device is in Doze, the work waits for a maintenance window.
earliestBeginDate 4 hours ahead. On Android, set a 4-hour interval.
Run one pass per wake
Run these steps from every wake handler:- Return early if a pass is already running in your app process. An in-memory flag that the pass sets and clears is enough.
-
Reuse the wallet if your app process already has it open. Bark allows one open wallet per data directory. A second
Wallet.openon the same directory fails at once, from the same process or another one. - Otherwise, read the mnemonic from secure storage (store the mnemonic covers the setting its entry needs).
-
Open the wallet with these arguments:
runDaemon: false: The bark library’s background daemon joins rounds interactively, which needs the process alive for the whole round. Until the daemon runs delegated refreshes itself, your app runs the delegated pass below on each wake.createIfNotExists: false: A wake that finds no wallet data then fails instead of creating a new wallet.- The same
Configand on-chain wallet as your foreground code: Your foreground code reuses this instance when the user opens the app while the process is alive. Opened without the on-chain wallet, the instance cannot fund exits. A differentvtxoRefreshExpiryThresholdchanges which VTXOs the pass refreshes.
-
If
hasPendingExits()returns true, callprogressExits()first. It needs no Ark server, so a live exit keeps moving even when the server is unreachable. -
Read
nextRoundStartTime(). If the last pass saw the same value, call onlysync(), which completes pending refreshes without submitting new ones. -
Otherwise, call
maintenanceDelegated()(Wallet::maintenance_delegated()in Rust). It syncs the wallet’s off-chain state, completes earlier delegated refreshes that are ready, and submits the VTXOs that are due. When something is due, it returns as soon as the Ark server accepts the participation, without waiting for the round. - Report the outcome to your server (catch devices that stop waking covers what the server does with it).
-
Call the completion handler for a background push,
setTaskCompleted(success:)for aBGAppRefreshTask, or return from the worker on Android.
Notifications.registerTaskAsync from expo-notifications:
- Run at most one pass per round: A second pass before the same round submits the waiting VTXOs again. The new participation replaces the waiting one at the current price. The replaced refresh movement ends
failed(plan for the deadline the user still has to meet covers replacement). Step 6 guards against it by comparingnextRoundStartTime()with the value the last pass saw. - Stay within the iOS time limit: iOS terminates an app that has not called the completion handler within 30 seconds. Call it before then, even if the pass has not returned. With
expo-notifications, iOS gets the result when your task returns, so return from the task before then. Add no other sync call to the pass, sincemaintenanceDelegated()already syncs. - Run the pass on foreground opens too: Call the same function when the app comes to the foreground, so one flag covers both paths. To try an interactive refresh, call
maintenance()before that function, and only while the app stays in the foreground. When a VTXO is due,maintenance()waits for the next round to start, up to an hour on Second’s Ark server. It then takes part in that round until it ends, which an app the OS suspends cannot finish. Run the delegated pass after it even when it throws. If the OS suspends the app first, the pass on the next wake is the fallback. - Stop the background daemon when the app leaves the foreground: If your foreground code runs the bark library’s background daemon, call
stopDaemon()when the app moves to the background andrunDaemon()when it returns. While the daemon runs, do not also callmaintenance(), since the daemon already joins each round with the VTXOs that are due. A wake that reuses the wallet then runs only the delegated pass, never an interactive round it cannot finish. There are two exceptions. The pass can still join an interactive refresh your foreground code left pending. Amaintenance()call still waiting for its round joins that round if it starts during the wake. Do this until the daemon runs delegated refreshes itself.
Complete each refresh on a later wake
A delegated refresh needs at least two passes. The first submits it. A later pass completes it once the round transaction has the confirmations set byroundTxRequiredConfirmations in Config, 1 by default (round_tx_required_confirmations in Rust). The wallet app signs the forfeits only then. A pass after the round but before that confirmation only locks the inputs.
Second’s Ark server runs a round every hour. With a wake every 4 hours, the round transaction usually has its confirmation by the next wake. Each pass then completes the refresh the previous pass submitted and submits what is due next.
With a wake every 4 hours, each wake completes the refresh the previous wake submitted and submits what is due next
- Complete it before the old VTXOs expire: The completing pass has to run while an emergency exit on the old VTXOs could still confirm before they expire (plan for the deadline the user still has to meet).
- Complete a scheduled refresh the same way: A scheduled delegated refresh enters the first round after the chain tip reaches its scheduled height. A pass completes it once that round’s transaction confirms, as above.
- Show it as in progress:
pendingRoundStates()lists the participation asdelegated-pending(aRoundFlowKindcase in the native SDK bindings) until a pass completes it. Show the refresh as in progress, not finished, for that time. If it is stilldelegated-pendingwhen the old VTXOs are close to expiry, start an emergency exit on them (plan for the deadline the user still has to meet covers when).
Store the mnemonic so background tasks can read it
Every pass that opens the wallet has to read the mnemonic, because a public key is not enough. A delegated refresh carries one Schnorr attestation per input VTXO, made with that VTXO’s own key, which derives from the mnemonic. A scheduled delegated refresh, signed in the foreground, covers a single future refresh (plan for the deadline the user still has to meet covers what one participation buys). For most wallet apps, store the mnemonic where only your app can read it, with no prompt or password required for a read. Mnemonic storage gives the settings and code for each platform. On iOS, the default keychain class,kSecAttrAccessibleWhenUnlocked, blocks reads whenever the screen is locked, which is when most background wakes arrive. Set the after-first-unlock class instead. Your app can still show a biometric prompt in its UI wherever you want, since that prompt does not depend on how you store the entry.
A wake that fires after a restart, before the user unlocks the device once, can’t read the entry on either platform. Treat it as a missed pass.
If your product does not allow a background-readable mnemonic, use one of two foreground alternatives:
- Visible push notification: Send one before a refresh is due and prompt the user to open the app. A per-use biometric prompt works fine in the foreground.
- Scheduled delegated refreshes: Register the participation during a foreground session for a future block height (schedule a delegated refresh).
Weigh availability against lock-screen protection
A background-readable mnemonic gives up lock-screen protection to get unattended maintenance:- What you give up: After the first unlock, the entry is open to any code running in your app’s context, so the lock screen no longer protects it.
- What you get: Maintenance that runs unattended, which keeps VTXOs from expiring while the app stays closed.
- Grant the minimum that works: The after-first-unlock class is enough for background tasks. Never reach for the deprecated always-accessible class.
- Require biometrics for user actions: Put sends, mnemonic reveal, and settings changes behind a per-use prompt. Only the background read path bypasses it.
Catch devices that stop waking
On some devices, your wallet app stops running passes, whatever your server sends. Give those users a way back before their VTXOs expire:- Schedule a local notification as the floor: Set it for half your refresh threshold before the first VTXO expiry, about 12 hours at the mainnet default. Working passes complete a refresh at least one wake, about 4 hours, before that point, so the notification fires only when passes stop or fall behind. Have it ask the user to open the app. Reschedule it after every pass.
- Count the inputs of a delegated round: Read
getFirstExpiringVtxoBlockheight()and the lowestexpiryHeightamong the VTXOs thatpendingRoundInputVtxos()returns. Use whichever expiry comes first. Convert heights to a time from the current chain tip at about 10 minutes per block (expose the next refresh deadline covers the height helpers). - Alert from your server when reports stop: A device that has not reported a pass for half your refresh threshold, 12 hours at the mainnet default, may hold a VTXO halfway through its refresh window. Send it a visible notification asking the user to open the app.
- Ask for notification permission during setup: On iOS and on Android 13 and later, neither notification shows unless your app has notification permission. Request it when the user creates or restores the wallet.
- Leave the refresh window wide: Do not lower the threshold on mobile (tune the refresh policy). At the mainnet default of 144 blocks, about 24 hours, a VTXO that falls due still gets about six wakes before it expires. A few missed wakes then cost nothing.
- Force-quit on iOS: An app the user swiped away in the App Switcher gets no background push until the user opens it again or restarts the device.
- Force stop on Android: An app the user force-stopped gets no FCM message, visible or silent, until the user opens it again. The system also clears its scheduled jobs and alarms, so neither notification above reaches it.
- Rarely opened apps on Android: Android limits the background jobs and network access of apps the user rarely opens (App Standby Buckets).
- Low Power Mode on iOS: Low Power Mode turns off Background App Refresh. Apple counts that setting and the device’s power state among the factors that decide whether a background push arrives.
- Before the first unlock: After a restart, no pass can read the mnemonic until the user unlocks the device once (store the mnemonic covers why).
How Bark Wallet does it
How Bark Wallet does it
Bark Wallet runs in a browser, where no background wake exists. The web version (WASM) keeps a worker loop running while a tab is open and submits its refreshes as delegated participations. A refresh submitted before the tab closed completes on the next open. While a tab is open, the bark library’s background daemon can also join a round interactively for the VTXOs its maintenance selection finds due. The installed version leaves background upkeep to the
barkd daemon (maintenance covers both).