Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
bd1e437
feat: add the wearlink module with the phone-watch pairing protocol
jvsena42 Nov 28, 2026
1e256b7
feat: pair the watch with the phone and receive the wallet from it
jvsena42 Nov 28, 2026
5c8cd37
feat: pair a watch from the phone and push settings to it
jvsena42 Nov 28, 2026
9094896
docs: describe wear pairing with its journeys and manual tests
jvsena42 Nov 28, 2026
2e5b776
feat: move the phone's watch pairing into a bottom sheet
jvsena42 Nov 28, 2026
142cad8
fix: guard the strongbox key import by api level, clear lint findings
jvsena42 Nov 28, 2026
425ef13
chore: update dependency verification metadata for wear pairing
jvsena42 Nov 28, 2026
d79c9b3
fix: harden the wear seed transfer, erase ack and pairing cancel
jvsena42 Nov 28, 2026
82e6d7e
fix: confirm the watch pairing code with a text edge button
jvsena42 Nov 28, 2026
d092356
docs: update wear pairing docs and journeys for the review fixes
jvsena42 Nov 28, 2026
28ae3ed
fix: reopen the watch sheet only onto a transfer still under way
jvsena42 Nov 28, 2026
99e0093
docs: update wear docs and journeys for review round 2
jvsena42 Nov 28, 2026
1be05cb
fix: use text edge buttons for the watch's confirm and new invoice
jvsena42 Nov 28, 2026
92c4a99
fix: harden wear pairing confirm, status read, erase, cancel and retry
jvsena42 Nov 28, 2026
a371e56
fix: keep the phone's pair request and clear a stale unpair failure
jvsena42 Nov 29, 2026
c3be448
fix: allow wear screenshots in debug builds as on the phone
jvsena42 Nov 29, 2026
96aecac
fix: wrap a long kdoc line in the wear screenshot block
jvsena42 Nov 29, 2026
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
7 changes: 7 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,13 @@ dependencies {
implementation(libs.camera.view)
// Crypto
implementation(libs.bouncycastle.provider.jdk)
// Watch link: the shared protocol, the Wear Data Layer that carries it, and opening the watch app from here.
implementation(project(":wearlink"))
implementation(libs.play.services.wearable)
implementation(libs.wear.remote.interactions)
// The debug socket carrier and the in-process fakes; no release variant depends on them.
debugImplementation(testFixtures(project(":wearlink")))
testImplementation(testFixtures(project(":wearlink")))
testImplementation(libs.bouncycastle.pkix.jdk) // test certificates
implementation(libs.ldk.node.android) { exclude(group = "net.java.dev.jna", module = "jna") }
val localBitkitCoreAar = providers.gradleProperty("bitkitCoreAar").orNull
Expand Down
42 changes: 42 additions & 0 deletions app/src/debug/java/to/bitkit/di/WatchLinkTransportModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package to.bitkit.di

import android.content.Context
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob
import to.bitkit.utils.Logger
import to.bitkit.watch.WatchDataLayerTransport
import to.bitkit.wearlink.LinkTransport
import to.bitkit.wearlink.testing.SocketLinkTransport
import java.io.File
import javax.inject.Singleton

/**
* A debug build reaches the watch over the Wear Data Layer too, unless the file `files/wearlink-socket` exists in the
* app's data: then it connects to a loopback TCP port instead, which `adb reverse` and `adb forward` bridge to a
* watch emulator. That is how two emulators, which cannot pair over the Data Layer without a Google account, run the
* pairing journeys; `journeys/wear/README.md` has the setup. The socket carrier is not part of a release build.
*/
@Module
@InstallIn(SingletonComponent::class)
object WatchLinkTransportModule {
private const val TAG = "WatchLinkTransportModule"
private const val SOCKET_MARKER = "wearlink-socket"

@Provides
@Singleton
fun provideLinkTransport(
@ApplicationContext context: Context,
@IoDispatcher ioDispatcher: CoroutineDispatcher,
dataLayer: WatchDataLayerTransport,
): LinkTransport {
if (!File(context.filesDir, SOCKET_MARKER).exists()) return dataLayer
Logger.warn("Using the debug socket link transport", context = TAG)
return SocketLinkTransport(isServer = false, scope = CoroutineScope(SupervisorJob() + ioDispatcher))
}
}
18 changes: 18 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,24 @@
android:resource="@xml/ndef_tag_service" />
</service>

<!--
Receives the paired watch's messages and state over the Wear Data Layer, also while the app is closed.
Play services binds it, so it is exported; everything it receives is authenticated by the link itself.
-->
<service
android:name=".watch.WatchLinkListenerService"
android:exported="true">
<intent-filter>
<action android:name="com.google.android.gms.wearable.MESSAGE_RECEIVED" />
<action android:name="com.google.android.gms.wearable.DATA_CHANGED" />

<data
android:host="*"
android:pathPrefix="/bitkit/v1"
android:scheme="wear" />
</intent-filter>
</service>

<service
android:name="androidx.appcompat.app.AppLocalesMetadataHolderService"
android:enabled="false"
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/java/to/bitkit/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import to.bitkit.appwidget.AppWidgetRefreshScheduler
import to.bitkit.env.Env
import to.bitkit.repositories.HwWalletRepo
import to.bitkit.repositories.SparkExitWork
import to.bitkit.repositories.WatchLinkRepo
import to.bitkit.services.BluetoothInit
import to.bitkit.services.PubkyAuthHandlerRegistrar
import to.bitkit.utils.Crypto
Expand Down Expand Up @@ -51,6 +52,10 @@ internal open class App : Application(), Configuration.Provider {
@Inject
lateinit var hwWalletRepo: Lazy<HwWalletRepo>

/** Resolved at startup when pairing is on, so a settings change reaches the paired watch whoever made it. */
@Inject
lateinit var watchLinkRepo: Lazy<WatchLinkRepo>

override val workManagerConfiguration
get() = Configuration.Builder()
.setWorkerFactory(workerFactory)
Expand All @@ -71,6 +76,7 @@ internal open class App : Application(), Configuration.Provider {
pubkyAuthHandlerRegistrar.start()
subscriptionClockOffsetSync.start()
observeAppForeground()
if (Env.isWatchLinkEnabled) watchLinkRepo.get()
}

private fun observeAppForeground() {
Expand Down
73 changes: 73 additions & 0 deletions app/src/main/java/to/bitkit/data/WatchLinkStore.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package to.bitkit.data

import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.dataStore
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map
import kotlinx.serialization.Serializable
import to.bitkit.data.serializers.WatchLinkSerializer
import to.bitkit.wearlink.Handshake
import to.bitkit.wearlink.LinkRecord
import to.bitkit.wearlink.LinkRecordStore
import to.bitkit.wearlink.ThrottleState
import to.bitkit.wearlink.ThrottleStore
import java.security.SecureRandom
import javax.inject.Inject
import javax.inject.Singleton

private val Context.watchLinkDataStore: DataStore<WatchLinkData> by dataStore(
fileName = "watch_link.json",
serializer = WatchLinkSerializer,
)

/**
* The phone's note of its paired watch, this install's random id for pairings, and the count of seed transfers that
* were started and not finished. It belongs to the install and is in no backup: the link keys are in the Keystore
* and cannot be restored, so a restored phone pairs again. Nothing here is secret.
*/
@Singleton
class WatchLinkStore internal constructor(
private val store: DataStore<WatchLinkData>,
) : LinkRecordStore {
@Inject
constructor(@ApplicationContext context: Context) : this(context.watchLinkDataStore)

override val record: Flow<LinkRecord?> = store.data.map { it.record }.distinctUntilChanged()

override suspend fun load(): LinkRecord? = store.data.first().record

override suspend fun update(transform: (LinkRecord?) -> LinkRecord?): LinkRecord? =
store.updateData { it.copy(record = transform(it.record)) }.record

/** The count of unfinished seed transfers, kept across restarts so a restart does not clear it. */
val throttle: ThrottleStore = object : ThrottleStore {
override suspend fun load(): ThrottleState = store.data.first().let {
ThrottleState(it.transferAttempts, it.transferLockedUntilMs)
}

override suspend fun save(state: ThrottleState) {
store.updateData {
it.copy(transferAttempts = state.attempts, transferLockedUntilMs = state.lockedUntilMs)
}
}
}

/** This install's id for pairings, made at first use. */
@OptIn(ExperimentalStdlibApi::class)
suspend fun installId(): ByteArray = store.updateData {
if (it.installId != null) return@updateData it
it.copy(installId = ByteArray(Handshake.ID_BYTES).also(SecureRandom()::nextBytes).toHexString())
}.installId.orEmpty().hexToByteArray()
}

@Serializable
data class WatchLinkData(
val record: LinkRecord? = null,
val installId: String? = null,
val transferAttempts: Int = 0,
val transferLockedUntilMs: Long = 0,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package to.bitkit.data.serializers

import androidx.datastore.core.Serializer
import to.bitkit.data.WatchLinkData
import to.bitkit.di.json
import to.bitkit.utils.Logger
import java.io.InputStream
import java.io.OutputStream

object WatchLinkSerializer : Serializer<WatchLinkData> {
private const val TAG = "WatchLinkSerializer"

override val defaultValue: WatchLinkData = WatchLinkData()

override suspend fun readFrom(input: InputStream): WatchLinkData {
return runCatching {
json.decodeFromString<WatchLinkData>(input.readBytes().decodeToString())
}.getOrElse {
Logger.error("Failed to deserialize WatchLinkData", it, context = TAG)
defaultValue
}
}

override suspend fun writeTo(t: WatchLinkData, output: OutputStream) {
output.write(json.encodeToString(t).encodeToByteArray())
}
}
17 changes: 17 additions & 0 deletions app/src/main/java/to/bitkit/di/WatchLinkModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package to.bitkit.di

import dagger.Binds
import dagger.Module
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import to.bitkit.watch.KeystoreLinkKeys
import to.bitkit.wearlink.LinkKeyStore
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
abstract class WatchLinkModule {
@Binds
@Singleton
abstract fun bindLinkKeyStore(keys: KeystoreLinkKeys): LinkKeyStore
}
6 changes: 6 additions & 0 deletions app/src/main/java/to/bitkit/env/Env.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ internal data class LocalSparkSettings(val configBase64: String, val chainUrl: S

@Suppress("ConstPropertyName", "KotlinConstantConditions", "SimplifyBooleanWithConstants")
internal object Env {
/**
* Whether the phone offers pairing with a Wear OS watch. Off on mainnet for as long as the watch app is: two live
* Spark instances on one seed are not safe until the real-time sync server is on (#2152).
*/
val isWatchLinkEnabled: Boolean get() = network != Network.BITCOIN

val isUsdtEnabled: Boolean get() = (network == Network.BITCOIN || isDebug) &&
usdtRpcUrl != null && usdtBundlerUrl.isNotBlank()
val usdtBundlerUrl: String get() = BuildConfig.USDT_BUNDLER_URL
Expand Down
Loading