Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions app/src/test/java/to/bitkit/models/WatchBackupContractTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package to.bitkit.models

import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.jsonPrimitive
import org.junit.Assume.assumeFalse
import org.junit.Assume.assumeTrue
import org.junit.Test
import to.bitkit.data.SettingsData
import to.bitkit.di.json
import to.bitkit.env.Env
import java.io.File
import kotlin.test.assertEquals

/**
* The Wear OS app imports the phone's settings by reading the settings backup from VSS (docs/wear.md, Settings
* sync). `wear/src/test/resources/phone-backup-contract.json` holds what it relies on: the backup's key, six
* settings as this app encodes them, and where each network's backups live. The watch's `PhoneBackupContractTest`
* reads the same file. When a test here fails, the watch stops reading the phone's settings: change the watch's
* reader and the file together with the phone.
*/
class WatchBackupContractTest {
private val contract: JsonObject by lazy {
val file = generateSequence(File("").absoluteFile) { it.parentFile }
.map { File(it, "wear/src/test/resources/phone-backup-contract.json") }
.first { it.isFile }
Json.parseToJsonElement(file.readText()).jsonObject
}

@Test
fun `the settings backup is stored under the key the watch reads`() {
assertEquals(contract.getValue("settingsKey").jsonPrimitive.content, BackupCategory.SETTINGS.name)
}

@Test
fun `the settings backup holds the six settings the watch reads, in the form it reads`() {
val expected = contract.getValue("settingsBackup").jsonObject
val expectedSettings = expected.getValue("settings").jsonObject
val settings = SettingsData(
primaryDisplay = PrimaryDisplay.FIAT,
displayUnit = BitcoinDisplayUnit.CLASSIC,
selectedCurrency = "EUR",
isQuickPayEnabled = true,
quickPayAmount = 10,
quickPayDailyLimitMultiplier = 3,
)

val written = written(settings)

assertEquals(expected.getValue("version"), written.getValue("version"))
val writtenSettings = written.getValue("settings").jsonObject
assertEquals(expectedSettings, JsonObject(writtenSettings.filterKeys { it in expectedSettings.keys }))
}

@Test
fun `default settings are written too, so the watch always finds all six`() {
val keys = contract.getValue("settingsBackup").jsonObject.getValue("settings").jsonObject.keys

val written = written(SettingsData()).getValue("settings").jsonObject

assertEquals(keys, written.keys.intersect(keys))
}

@Test
fun `this network's backups live where the watch looks for them`() {
// An E2E build may point the backups at a local server, which the watch never reads.
assumeFalse(Env.isE2eTest)
val network = contract.getValue("networks").jsonObject[Env.network.name]?.jsonObject
// The watch runs on fewer networks than the phone.
assumeTrue(network != null)

val expected = checkNotNull(network).mapValues { it.value.jsonPrimitive.content }
val actual = mapOf(
"storeIdPrefix" to Env.vssStoreIdPrefix,
"vssServerUrl" to Env.vssServerUrl,
"lnurlAuthServerUrl" to Env.lnurlAuthServerUrl,
)
assertEquals(expected, actual)
}

/** The settings backup of [settings] as this app uploads it. */
private fun written(settings: SettingsData): JsonObject {
val backup = SettingsBackupV1(createdAt = 0, settings = settings)
return json.parseToJsonElement(json.encodeToString(SettingsBackupV1.serializer(), backup)).jsonObject
}
}
246 changes: 219 additions & 27 deletions docs/wear.md

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions journeys/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ journey PR, which is what made this file conflict on every merge.
| Deep links, addresses and invoices handed to the app | `adb shell am start -a android.intent.action.VIEW -d "<uri>"`; `bitkit://` screen and sheet routes sit behind the dev-mode gate — [Running a journey](#running-a-journey), [deeplinks](deeplinks) |
| A watch to run the Wear OS app on | a Wear OS watch emulator (`Wear_Large_Round_API37`, `Wear_Small_Round_API34`), or a physical watch over Wi-Fi debugging — [wear](wear/README.md) |
| Watch gestures: pager swipes, swipe-to-dismiss, rotary crown, ambient mode | `adb shell input swipe`, `adb shell input rotaryencoder scroll --axis SCROLL,<n>` and `KEYCODE_SLEEP`/`KEYCODE_WAKEUP` on the emulator; the real crown, bezel and wrist on a physical watch — [wear](wear/README.md#watch-gestures) |
| The phone's settings on the watch (units, currency, QuickPay) | a phone dev build with the same private settings wallet (the test phrase with a passphrase kept on this machine), never the public test phrase alone, writes them to the hosted staging VSS, and the watch imports them; nothing runs locally — [wear](wear/README.md#phone-settings-on-vss) |
| Typing on the watch keyboard | `adb shell input text` after closing the Wear OS keyboard with Back, which otherwise swallows the input — [wear](wear/README.md#typing) |

## Cross-platform
Expand Down
175 changes: 169 additions & 6 deletions journeys/wear/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,34 @@ the command line. Run `receive-paid.xml` first: it funds the wallet the send jou
| `activity-list.xml` | local | yes, after `receive-paid.xml` and `send-deeplink-bolt11.xml` |
| `receive-paid-ambient.xml` | local | no, it funds the wallet with 1 000 sats |
| `tile-balance.xml` | hosted or local | no |

Paying an LNURL-pay link or a Lightning address has no journey: the local stack's LNURL server serves only the
Lightning addresses its Spark wallets registered, over plain HTTP, and nothing in it registers one. It is covered by
`PaymentsRepoTest.kt` and `SendViewModelTest.kt`, which check the range, the amount entry and the invoice the service
returns.
| `settings-import-units.xml` | local, the private settings wallet, and a phone that wrote its settings | yes, any balance above 0 |
| `settings-swap-survives-import.xml` | as `settings-import-units.xml`, after it | yes, any balance above 0 |
| `quickpay-skips-hold.xml` | local, the private settings wallet, and a phone that wrote its settings | yes, 2 000 sats |
| `quickpay-on-app-start.xml` | as `quickpay-skips-hold.xml`, after it | yes, 2 000 sats |
| `quickpay-over-limit-holds.xml` | local, the private settings wallet, and a phone that wrote its settings | yes, 12 000 sats |
| `quickpay-off-holds.xml` | as `quickpay-skips-hold.xml`, after it | yes, 2 000 sats |

The six settings journeys read the phone's settings backup from Blocktank's hosted staging VSS, which every dev
build uses whatever its Spark network is: a watch on the local `./spark` stack reads the same backup as one on hosted
regtest. Nothing extra runs locally, no VSS and no LNURL-auth server. They need a phone app that wrote the backup
first: see [Phone settings on VSS](#phone-settings-on-vss). They never run on the public test phrase, whose backup
every other journey reads too: see [Private settings wallet](#private-settings-wallet).

The daily QuickPay limit, a failed QuickPay payment that is taken out of the day's count, an amount the wearer
typed, and a missing, unreadable or newer backup have no journey: they depend on the exchange rate of the day, a
payment that fails on demand, an amountless invoice the local stack does not make, or a backup only a broken phone
would write. `QuickPayRepoTest.kt`, `QuickPaySpendStoreTest.kt`, `PaymentsRepoTest.kt`, `SettingsSyncRepoTest.kt`
and `PhoneSettingsParserTest.kt` cover them.

A payment link opened again from Recents, which the watch takes as a plain launch, has no journey either: it was
not tried on a device, and `ReplayedLaunchTest.kt` covers the check. To try it by hand: with the app not running,
open a link as in `send-deeplink-bolt11.xml`, leave the review and Home with Back, open Bitkit from the watch's
Recents and check that Home shows and `WatchSendHold` does not.

Paying an LNURL-pay link or a Lightning address has no journey, under QuickPay or with the hold: the local stack's
LNURL server serves only the Lightning addresses its Spark wallets registered, over plain HTTP, and nothing in it
registers one. It is covered by `PaymentsRepoTest.kt` and `SendViewModelTest.kt`, which check the range, the amount
entry, the invoice the service returns and that only a link with one amount goes out under QuickPay.

### Local `./spark` stack

Expand All @@ -149,7 +172,7 @@ regtest. A local wallet is new to that chain, so it starts at 0.
### Funding the test wallet

On the local stack, `receive-paid.xml` funds the watch through its own receive QR. Otherwise, or for an on-chain
deposit, take the deposit address or invoice from the phone app with the same recovery phrase: restore [the test wallet](#the-test-wallet) in a phone dev build on the same Spark network, read the
deposit, take the deposit address or invoice from the phone app with the same recovery phrase: restore [the test wallet](#the-test-wallet) in a phone dev build on the same Spark network (and change none of its settings there, see [Private settings wallet](#private-settings-wallet)), read the
Spark deposit address at Settings › Advanced › Lightning Connections › Spark (`SparkDepositAddress`), fund it, then
close the phone app before you start the watch again. Two live Spark instances on one seed are unsafe (#2152), so never
keep both open.
Expand Down Expand Up @@ -213,6 +236,146 @@ The test wallet restored with no passphrase, its balance loaded, Bitcoin as the

A wallet restored by an earlier journey can stay: only [Fresh app](#fresh-app) and `remove-wallet.xml` remove it.

The watch imports the settings backup of whatever phrase it holds, so these journeys count on the public phrase's
backup holding the phone's defaults (Bitcoin first, sats, USD, QuickPay off) or not existing. If the balance shows
another currency or eight decimals, a phone app changed that backup: run the journeys with a private 12-word phrase
of your own instead, which they support (see [The test wallet](#the-test-wallet)). That phrase's phone settings must
be at their defaults too, so not the [Private settings wallet](#private-settings-wallet), whose backup holds
whatever the last settings journey set.

### Private settings wallet

Every journey that changes the phone's settings (`settings-*.xml`, `quickpay-*.xml`) runs on a wallet of its own,
never on the public test phrase. That phrase's settings backup is one object on the hosted staging VSS, shared by
hosted regtest and the local `./spark` stack and by everyone who restores the phrase: fiat first, euros or QuickPay
written there would reach every other watch on that phrase and break the journeys that expect the defaults.

The private wallet is the public test phrase with a passphrase made on this machine. With a passphrase the phrase
is another wallet, with a settings backup of its own, so each machine's settings journeys stay apart from everyone
else's.

The passphrase is not a secret. The phrase it is added to is public and the wallet holds regtest coins of no value;
the passphrase only keeps one machine's settings backup apart from another's. It may show in a layout dump or a
screenshot on your own machine. It still must not leave the machine, since anyone who has it can write to the same
backup: never commit it, never put it in a journey file, a pull request's text or media, and never paste it into an
issue. A screenshot or a recording of the phone's restore screen, or of the watch's passphrase screen, is not PR
media.

1. Make the passphrase, once per machine, in a file outside the repository:

```bash
F=~/.config/bitkit-journeys/settings-passphrase
mkdir -p "${F%/*}"
[ -s "$F" ] || LC_ALL=C tr -dc a-z0-9 </dev/urandom | head -c 12 >"$F"
```

It keeps a passphrase that is already there. Twelve lowercase letters and digits are short and plain enough for
`adb shell input text`, which drops characters from long or dotted strings.
2. Restore it in the phone app, a plain dev build (see [Phone settings on VSS](#phone-settings-on-vss), step 1). From
a fresh install go through the onboarding to the Restore wallet screen (`RestoreWallet`, then
`MultipleDevices-button`). For each word, tap its field (`Word-0` to `Word-11`) and run
`adb shell input text <word>`: eleven times `abandon`, then `about`. Tap `AdvancedButton`, tap `PassphraseInput`
and type the passphrase:

```bash
adb shell input text "$(cat ~/.config/bitkit-journeys/settings-passphrase)"
```

[Check it](#checking-the-passphrase), then tap `RestoreButton`.
3. Set the phone's settings to where the journeys start: Default Unit Bitcoin, Denomination Modern, Local Currency
USD and QuickPay off. A new wallet has them already. On a rerun the restore brings back whatever the last run
left, as the currency first, Classic and EUR after a run that stopped at `settings-import-units.xml`: set them
back and keep the app open for 20 s, so it uploads them
([Phone settings on VSS](#phone-settings-on-vss), steps 3 and 4). Then close the phone app
(`adb shell am force-stop to.bitkit.dev`).
4. Restore it on the watch: [Restored wallet](#restored-wallet) steps 1 to 3, then tap `RestorePassphraseAdd`
instead of `RestorePassphraseSkip`, tap `RestorePassphraseInput`, close the keyboard with Back
([Typing](#typing)) and run the same `adb shell input text "$(cat …)"` command. [Check it](#checking-the-passphrase),
tap `RestorePassphraseConfirm` and wait for `WatchHomeBalance`. It reads `0` for a wallet that was never funded;
on a rerun against a local stack that kept its data it reads what the last run left.
5. Fund it before the phone's settings change: on the local `./spark` stack run `receive-paid.xml` on this wallet
while the phone's settings are still the ones of step 3. That journey expects "50 000", which is what a watch
on Bitcoin and Modern shows for a wallet that held nothing; after `settings-import-units.xml` set Classic, the
same payment reads "0.00050000". On a rerun with at least 12 000 sats left, skip this step.
6. Only now set what a journey asks for on the phone: [Phone settings on VSS](#phone-settings-on-vss).

One private wallet serves all six journeys, in the order of the table: the settings journeys first, then the
QuickPay ones, whose preconditions set the units and the currency back. Remove it from the watch
(`remove-wallet.xml` or [Fresh app](#fresh-app)) before the journeys on the public phrase run again.

To start over with an empty backup, delete `~/.config/bitkit-journeys/settings-passphrase` and begin at step 1: a
new passphrase is a new wallet with no backup and no balance.

#### Checking the passphrase

`adb shell input text` can drop a character, and a passphrase that differs by one names another wallet with no
backup. Check the field before you confirm: the layout dump has to hold the passphrase as typed, so this prints `1`
or more.

```bash
android layout | grep -c -F "$(cat ~/.config/bitkit-journeys/settings-passphrase)"
```

On `0`, select all in the field, delete and type again, with the key events of
[Replacing a word](#replacing-a-word), then check again. The proof that phone and watch hold the same wallet comes
with the first import after step 6: the watch logs `Imported the phone's settings` once the phone wrote its backup.
It logs `Found no settings backup of the phone` when the two differ, and also before the phone ever changed a
setting.

### Phone settings on VSS

The watch never writes settings; it imports the ones a phone app backed up for the same recovery phrase
(Settings sync in [docs/wear.md](../../docs/wear.md)). So a phone has to write them first, once per journey that
names this precondition:

1. Stop the watch emulator if this machine runs one emulator at a time, and start a phone emulator or connect a phone
([journeys/README.md](../README.md) has the phone setup). Install a plain dev build of the phone app there with
`just install`: not an E2E build with `E2E_VSS_SERVER_URL` set, which would back up to a local server the watch
never reads.
2. Use the [Private settings wallet](#private-settings-wallet) in the phone app, restored there with its
passphrase. Never restore the public test phrase without that passphrase in a phone app to change its settings.
The recovery phrase and passphrase must be the same on phone and watch, since they name the backup.
3. Set what the journey asks for in the phone's Settings (`HeaderMenu`, `DrawerSettings`, `Tab-general`):
- Default Unit (`UnitSettings`): Bitcoin or the currency first, and the denomination (`DenominationModern`,
`DenominationClassic`).
- Local Currency (`CurrenciesSettings`).
- The QuickPay journeys read sats and dollars, so each of them asks for Default Unit Bitcoin, Denomination Modern
and Local Currency USD besides its QuickPay values: `settings-import-units.xml` leaves the wallet on the
currency, Classic and EUR.
- QuickPay (`QuickpaySettings`, past `QuickpayIntro-button` the first time): the switch (`QuickpayToggle`), the
amount (`QuickpayAmountSlider`, stops $1 to $50) and the daily limit (`QuickpayDailyLimitSlider`, 1× to 50×).
Tap the slider track above a stop's label, as in `settings/quickpay-payment-pin.xml`.
4. Keep the phone app open and online for 20 s: it uploads the settings about 5 s after the last change. Settings ›
Security › Data Backups (`BackupSettings`) lists Settings with the time of its last backup.
5. Close the phone app (`adb shell am force-stop to.bitkit.dev` on the phone). The phone and the watch must never be
open on the same Spark network with one recovery phrase at the same time (#2152).
6. Start the watch again. It imports the settings when its wallet is ready, at every start and every 5 minutes, but
not within a minute of the last import and not in ambient mode; a tap on `WatchSettingsSync` on the Settings page
imports at once, and shows `WatchSettingsSyncFailed` when it could not. Debug builds log
`Imported the phone's settings`:

```bash
adb logcat -d -s BitkitWear | grep "SettingsSyncRepo"
```

**Gotcha:** the public test phrase's backup belongs to everyone who restores it on a dev build, so its settings can
change without notice. That is why no settings journey reads or writes it, and why a phone app that holds the public
phrase without a passphrase, as for [Funding the test wallet](#funding-the-test-wallet), must leave its units, currency and QuickPay at
their defaults.

**Gotcha:** QuickPay pays only a link that the wearer is there for: the send has to be priced within 30 s of the
link, with the watch unlocked, the app on screen and out of ambient mode all that time. A journey that sends its link
to a watch that is asleep, locked or still connecting for longer gets the review and the hold instead. A watch that
is offline gets neither: the flow ends on "Can't pay offline" and the link is dropped (`send-offline-error.xml`).

**Gotcha:** the daily limit is counted per watch and per day on the watch's date, and every run of a paying QuickPay
journey adds to it. After about a dozen runs on one day the next one gets the review and the hold, and
`WatchSendSent` never shows. `remove-wallet.xml` or clearing the watch app's data clears the count; so does the next
day.

**Never install the phone app on the watch or the watch app on the phone:** they share one application id.
`just install` and `just wear-install` each pick a device by `ANDROID_SERIAL`; set it before every install.

### Restart

The journeys restart the app with:
Expand Down
Loading