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
239 changes: 217 additions & 22 deletions docs/wear.md

Large diffs are not rendered by default.

106 changes: 106 additions & 0 deletions journeys/wear/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ the command line. Run `receive-paid.xml` first: it funds the wallet the send jou
| `offline-keeps-last-balance.xml` | hosted | yes |
| `background-disconnects-spark.xml` | hosted | yes |
| `nfc-hint-states.xml` | hosted, physical watch only | yes |
| `nfc-pay-background.xml` | local | yes, 2 000 sats |
| `screenshot-blocked.xml` | none | no |
| `receive-qr.xml` | local | no |
| `receive-qr-rests.xml` | local | no |
Expand Down Expand Up @@ -599,3 +600,108 @@ build tools installed:
```bash
AAPT2="$(ls -d "${ANDROID_HOME:-${ANDROID_SDK_ROOT:-$HOME/Android/Sdk}}"/build-tools/*/ | sort -V | tail -n 1)aapt2"
```

### Tags and taps

The emulators read no tag and offer none, so two things stand in for a tap there:

- **A tapped tag.** `nfc-pay-background.xml` sends the intent Android's tag dispatch would send, with the request as
its data. The real intent always carries the tag (`EXTRA_TAG`) and its message (`EXTRA_NDEF_MESSAGES`), which `adb`
cannot build; the app reads the message when it is there and the data otherwise, so the journey takes a path no
real tap takes, and only unit tests cover the message (`LaunchRequestTest`). `adb` also adds
`FLAG_ACTIVITY_NEW_TASK` and names the package; the NFC stack starts the activity without that flag and by the
filter alone, so the journey shows neither where a real tap arrives (`MainActivity` is `singleTask` for that,
manual tests 1e to 1h) nor Android's chooser. The filter's uppercase `BITCOIN` and `LIGHTNING` entries match
only such an `adb` or app intent: Android lowercases a tag record's scheme. The journey passed on the API 37
emulator while `MainActivity` was `singleTop`. The activity is `singleTask` now, which changes how every wear
journey that starts it arrives (`onNewIntent`), so the journeys must be re-run under `singleTask`.
Re-run under `singleTask` at `7ac718aad` on the local stack: passed, along with send-deeplink-bolt11, send-own-invoice-guard, send-offline-error, receive-qr, receive-paid, tile-balance, nfc-required and restore-background-drops-words.

```bash
adb shell am start -a android.nfc.action.NDEF_DISCOVERED -d "lightning:<invoice>" to.bitkit.dev
```

- **The receive tag and its hint.** Without an NFC adapter the receive QR never offers its tag and shows no hint
(`WatchReceiveNfc` and `WatchReceiveNfcOff` are not listed). The offer's lifecycle and the hint are covered by unit
tests and by the `PreviewNfc` and `PreviewNfcOff` previews of `ReceiveQrScreen`; see docs/wear.md › NFC › Checked
where.

### Manual tests on a physical watch

These need a capability the journey environment lacks: an NFC radio on the watch, and a second NFC device to tap.
Run them on a physical watch with NFC on, unlocked and on the wrist, with a dev build
([On a physical watch](#on-a-physical-watch)) and a funded wallet on hosted regtest, and a phone with a dev build of
the phone app on **another** wallet: the two apps do not know each other's invoices before pairing, and with the
same wallet the phone's invoice would be the watch's own. Debug builds log the tag's offer under `BitkitWear`
(`Started the NFC tag`, `Stopped the NFC tag`).

1. **Tap pays from the background.** On the phone open Receive with an amount of 1 000 sats, so that it offers its
NFC tag. On the watch leave Bitkit (press the crown) and keep the screen on. Hold the watch to the back of the
phone.
- Expected: Bitkit opens on the watch on the review with "1 000" and the hold to pay; nothing is paid before the
hold. Holding pays, and the phone shows the payment as received.
- 1a. Keep the watch on the phone for 10 s after the review shows: the review stays, and no second review or
chooser opens.
- 1b. With Bitkit open on the Balance page instead of in the background: the same review opens.
- 1c. With the watch's screen off, hold it to the phone for 5 s: nothing opens, as Android reads no tag with
the screen off. Wake the screen with the watch still on the phone: the review opens once, without lifting the
watch and tapping again.
- 1d. A printed or written NFC tag with one URI record `lightning:<invoice>` gives the same result as 1.
- 1e. One activity, one task: after 1 and again after 1b, run
`adb shell dumpsys activity activities | grep -c 'Hist.*to.bitkit.wear.ui.MainActivity'`. Expected: it prints
1, and the watch's Recents show Bitkit once: the tap reached the running activity and made no second one.
- 1f. Tap again during Sending: start a payment as in 1 with a second tag at hand (another invoice). Lift the
watch off the phone when the review shows and wait 3 s, so the watch reads tags again, then hold to pay and
tap the second tag while the bolt and ring show. Expected: the sending screen stays and ends on "Sent", and
no second review opens; the log lists "Dropped launch request 'Send' during a send", unless the tap came
while the watch still held off repeats of the first tag, when Android sends nothing and nothing is logged.
If "Sent" shows before the second tap, run it again. Lifting and tapping the second tag after the flow
closed opens its review.
- 1g. Tap with Bitkit open from the launcher: open Bitkit from the launcher, open a payment link from the
phone or `adb` so a review shows, then tap a tag with another invoice. Expected: the tag's review replaces
the first in the same activity; Recents shows Bitkit once, and nothing of the first link is paid.
- 1h. A link from another app's task, where the watch has an app that opens a `lightning:` link without a new
task, as a notes or browser app that shows links; skip it otherwise, since `adb` always adds a new task.
With Bitkit running, open the link from that app. Expected: the review opens in Bitkit's own task, and
Recents shows Bitkit once.
- 1i. A tag the watch refuses: write `bitcoin:<address>?req-foo=1` as one URI record to a tag and tap it.
Expected: "Not supported" with OK (`WatchTagRefused`), once, however long the watch stays on the tag; OK
returns to Home.
2. **QuickPay on a tap.** With QuickPay on in the phone settings the watch imported and an amount within its limit
(the private settings wallet, see [Phone settings on VSS](#phone-settings-on-vss)), repeat 1 with a tag of
another wallet.
- Expected: the send goes out without the review and ends on "Sent"; the log lists "Skipped the hold to pay
under QuickPay".
3. **Offline tap.** Turn the watch's Wi-Fi and Bluetooth off, wait for the offline icon on the Balance page, then
tap as in 1.
- Expected: "Can't pay offline" with OK; nothing is paid when the network returns.
4. **The phone pays the watch's receive QR.** On the watch open Receive and the QR. On the phone open Send and its
NFC scanner, and hold the phone to the watch.
- Expected: below the QR the NFC hint animates (`WatchReceiveNfc`); the phone reads the watch's invoice and
opens its amount entry (the invoice is amountless); after the phone pays, the watch shows the check with the
amount.
- 4a. The hint is clear of the QR's white square, and the phone's camera still scans the QR.
- 4b. `adb shell svc nfc disable`: the hint turns dimmed and slashed (`WatchReceiveNfcOff`), the phone reads
nothing, and a tap on the hint opens the NFC settings. Run `adb shell svc nfc enable` and swipe back from the
NFC settings to the QR: the hint animates again and the phone reads the invoice.
- 4c. Leave the QR showing until the watch's screen is off, and wait until the invoice expired (1 h; the log's
"Showing invoice" line dates it). Wake the watch, and open Bitkit if the watch face shows: it returns on the
receive screen. Expected: the loading ring, then a new QR, or the QR-hidden icon that shows the new QR on a
tap (a screen that went off without ambient mode keeps the QR resting); the log lists a second "Showing
invoice" with another invoice; the phone's scanner never reads the old one.
5. **The tag stops with the QR.** With the QR showing, check with the phone's scanner that each of these ends the
tag (the phone reads nothing, and the log lists "Stopped the NFC tag"): swiping back to the Receive page;
pressing the crown; covering the watch until it goes ambient; waiting 2 min until the QR rests; after a payment,
while the check shows.
- Expected: nothing is read in any of them, and tapping the resting screen, waking the watch or tapping the
check for a new invoice brings the tag back with the QR.
- 5a. A tag tapped on the QR: with the QR showing, tap the watch on a payment tag as in 1. Expected: the review
opens above Home in place of the QR, and the log lists "Stopped the NFC tag"; the phone's scanner reads
nothing from the watch while the review shows.
- 5b. Recreation: with the QR showing, change the watch's font size or language in its settings and return.
Expected: the same QR shows, the log lists "Stopped the NFC tag" and then "Started the NFC tag" once each,
and the phone reads the invoice again.
6. **The watch's own tag.** Read the invoice of the watch's receive QR from the log
([Reading the receive invoice](#reading-the-receive-invoice)), write `lightning:<invoice>` as one URI record to a
blank NFC tag with any tag-writing app on the phone, and tap the watch on that tag.
- Expected: "Can't pay own invoice" with OK, and no hold to pay.
32 changes: 32 additions & 0 deletions journeys/wear/nfc-pay-background.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<journey name="wear nfc pay background">
<description>
A tapped NFC payment tag opens the send flow while Bitkit is in the background: Android's tag dispatch starts the
app with an `NDEF_DISCOVERED` intent, the review shows the tag's amount, a repeat of the same tap changes nothing,
and holding the pay button pays. The emulators have no NFC, so no tag can be tapped there; `adb` sends the intent
the dispatch would send, with the request as its data and without the tag and its message. That checks the
manifest filter, `onNewIntent`, the repeat filter and the send flow. The reading of a tag's message is
unit-tested only (`LaunchRequestTest`): a real tap always carries it, and `adb` cannot. `adb` also adds
`FLAG_ACTIVITY_NEW_TASK` and names the package, which the NFC stack does not, so this does not check where a
real tap arrives or that a watch reads a tag at all; the manual tests in journeys/wear/README.md › NFC cover
those on a physical watch. Emulator and physical watch.

Precondition: Restored wallet on the local ./spark stack with at least 2 000 sats (run receive-paid.xml first),
see journeys/wear/README.md › Local ./spark stack, with the app open on the Balance page and the loading ring
stopped, and QuickPay off or never imported (a watch with QuickPay on pays 1 000 sats without the review, as
designed). README › Holding to pay has the hold gesture.
</description>
<actions>
<action>Run `../bitkit-docker/spark invoice 1000` and note the BOLT11 invoice it prints as &lt;invoice&gt;</action>
<action>Run `adb shell input keyevent KEYCODE_HOME` and wait 2 s</action>
<action>Run `adb shell dumpsys activity activities | grep -m1 topResumedActivity` and verify it does not name `to.bitkit.dev`: the app is in the background</action>
<action>Run `adb logcat -c`</action>
<action>Run `adb shell 'am start -a android.nfc.action.NDEF_DISCOVERED -d "lightning:&lt;invoice&gt;" to.bitkit.dev; sleep 1; am start -a android.nfc.action.NDEF_DISCOVERED -d "lightning:&lt;invoice&gt;" to.bitkit.dev'`: one command, so the repeat follows the tap by 1 s however slow the runner is, as when the watch stays on the tag</action>
<action>Wait up to 20 s until "WatchSendHold" is listed: the wallet may connect again first</action>
<action>Verify "WatchSendAmount" reads "1 000"</action>
<action>Run `adb logcat -d -s BitkitWear | grep -c "Dropped a repeat of an NFC tap"` and verify it prints 1: the repeat opened no second flow</action>
<action>Hold "WatchSendHold" for 2 s as in README › Holding to pay</action>
<action>Wait up to 15 s until "WatchSendSent" is listed</action>
<action>Verify "WatchSendResultAmount" reads "1 000"</action>
<action>Wait up to 6 s until "WatchHomeBalance" is listed: the flow closed by itself on the Balance page</action>
</actions>
</journey>
5 changes: 3 additions & 2 deletions journeys/wear/nfc-required.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<journey name="wear nfc required">
<description>
The watch app requires NFC hardware (`android.hardware.nfc`, required), so Play offers it only to watches with NFC.
The watch app requires NFC hardware (`android.hardware.nfc`, required), so Play offers it only to watches with NFC,
and does not require host card emulation (`android.hardware.nfc.hce`), so a watch without it is not filtered out.
`uses-feature` is a store filter, not an install check, so the app still installs on the NFC-less emulators.
`dumpsys package` does not list required features, so the check reads the installed APK with `aapt2` from the
newest SDK build tools installed (journeys/wear/README.md › NFC sets `AAPT2`). Emulator and physical watch.
Expand All @@ -12,6 +13,6 @@
<action>Run `adb pull "$(adb shell pm path to.bitkit.dev | grep base.apk | sed 's/package://')" base.apk`</action>
<action>Run `"$AAPT2" dump badging base.apk | grep uses-feature` and verify it lists `android.hardware.nfc`</action>
<action>Verify the same output lists `android.hardware.type.watch`</action>
<action>Run `"$AAPT2" dump badging base.apk | grep -c nfc.hce` and verify it prints 0: host card emulation is not required yet</action>
<action>Run `"$AAPT2" dump badging base.apk | grep nfc.hce` and verify its one line starts with `uses-feature-not-required`: host card emulation, which the receive tag uses, is declared but not required</action>
</actions>
</journey>
47 changes: 46 additions & 1 deletion wear/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@
android:name="android.hardware.nfc"
android:required="true"
tools:ignore="UnnecessaryRequiredFeature" />
<!-- Offering the receive invoice as a tag needs host card emulation; a watch without it still pays by tap. -->
<uses-feature
android:name="android.hardware.nfc.hce"
android:required="false" />

<!-- NFC payments: the repeat guard after a tapped tag and offering the receive invoice as a tag. -->
<uses-permission android:name="android.permission.NFC" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Expand Down Expand Up @@ -53,10 +59,15 @@
tools:ignore="MissingClass"
tools:node="remove" />

<!--
singleTask, as the phone's MainActivity: a payment link or a tapped tag always reaches the one running
activity in its own task (onNewIntent), also when its sender starts it without NEW_TASK, as Android's
NFC dispatch does. The empty task affinity is what Wear OS asks of an activity for its Recents.
-->
<activity
android:name=".ui.MainActivity"
android:exported="true"
android:launchMode="singleTop"
android:launchMode="singleTask"
android:taskAffinity=""
android:theme="@style/Theme.Wear.Splash">
<intent-filter>
Expand Down Expand Up @@ -84,8 +95,42 @@
<data android:scheme="lnurlp" />
<data android:scheme="spark" />
</intent-filter>
<!-- A tapped NFC tag whose first record is a payment link, also while the app is not in front; the
same schemes as above. LaunchRequest reads it as the phone reads a tag. Android lowercases a
tag record's scheme before it matches, so the uppercase entries only ever match an intent that
another app or adb sends. -->
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />

<category android:name="android.intent.category.DEFAULT" />

<data android:scheme="bitcoin" />
<data
android:scheme="BITCOIN"
tools:ignore="AppLinkUrlError" />
<data android:scheme="lightning" />
<data
android:scheme="LIGHTNING"
tools:ignore="AppLinkUrlError" />
<data android:scheme="lnurl" />
<data android:scheme="lnurlp" />
<data android:scheme="spark" />
</intent-filter>
</activity>

<!-- Offers the receive invoice as an NFC tag while the receive QR shows; only the NFC stack may bind it. -->
<service
android:name=".nfc.NdefTagService"
android:exported="true"
android:permission="android.permission.BIND_NFC_SERVICE">
<intent-filter>
<action android:name="android.nfc.cardemulation.action.HOST_APDU_SERVICE" />
</intent-filter>
<meta-data
android:name="android.nfc.cardemulation.host_apdu_service"
android:resource="@xml/ndef_tag_service" />
</service>

<!-- The balance tile; only the system's tile host may bind it. -->
<service
android:name=".tile.BalanceTileService"
Expand Down
Loading