taler-android

Android apps for GNU Taler (wallet, PoS, cashier)
Log | Files | Refs | README | LICENSE

addglobalcurrencyexchange-null-masterpub.md (6710B)


      1 <!--
      2   This file is part of GNU Taler
      3   (C) 2026 Taler Systems S.A.
      4 
      5   GNU Taler is free software; you can redistribute it and/or modify it under the
      6   terms of the GNU General Public License as published by the Free Software
      7   Foundation; either version 3, or (at your option) any later version.
      8 
      9   GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
     10   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     11   A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
     12 
     13   You should have received a copy of the GNU General Public License along with
     14   GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15 -->
     16 
     17 # "Treat as global" raises WALLET_UNEXPECTED_EXCEPTION on an un-contacted exchange
     18 
     19 Found by `simulator/driver/explore.py` (seed 11, step 34); reproducer in
     20 `repro_global_currency_null_masterpub.py` (4 taps from a fresh profile).
     21 
     22 **Severity: low-moderate.** The menu entry sits inside `if (devMode)` in
     23 `ExchangeListScreen.kt:231`, so only developer mode shows it. What it costs is a
     24 feature that silently does not work plus a spurious
     25 `WALLET_UNEXPECTED_EXCEPTION` in the logs, which is noise when triaging real
     26 problems.
     27 
     28 ## Reproduce by hand
     29 
     30 No local Taler deployment is needed: any payment service the wallet has not
     31 contacted yet will do, and the wallet ships several as built-ins.
     32 
     33 1. Install a debug build of the wallet (`./gradlew :wallet:installFdroidDebug`)
     34    on a device or emulator, or use a release build and skip to step 2 with
     35    developer mode already on.
     36 2. Open the app, go to **Settings** and switch **Developer mode** on. (A fresh
     37    profile always starts with it off, whatever `BuildConfig.DEBUG` is: the
     38    persisted value is pushed into the view model at every start.)
     39 3. Settings → **Payment services**. The list contains built-ins the wallet has
     40    never talked to, for example `exchange.taler-ops.ch`. Do not tap the entry
     41    itself — contacting it would fetch its keys and fill in the missing field.
     42 4. Tap the **⋮ menu** next to `exchange.taler-ops.ch`.
     43 5. Tap **Treat as global**.
     44 
     45 Nothing happens on screen. With `adb logcat | grep addGlobalCurrencyExchange`
     46 running, the request and its error are visible:
     47 
     48 ```
     49 E taler-wallet: got addGlobalCurrencyExchange error:
     50   TalerErrorInfo(code=WALLET_UNEXPECTED_EXCEPTION,
     51   hint=unexpected exception (message: expected string at
     52   (AddGlobalCurrencyExchangeRequest).exchangeMasterPub but got undefined), ...)
     53 ```
     54 
     55 Expected instead: either the entry is not offered for an exchange whose keys
     56 have not been fetched, or tapping it reports a plain, explained failure.
     57 
     58 ## Reproduce with the simulator
     59 
     60 ```bash
     61 ssh root@tripwire.taler.net
     62 cd /opt/taler-sim/driver && python3 ../findings/repro_global_currency_null_masterpub.py
     63 ```
     64 
     65 Exits 0 and prints `reproduced: True` when the internal error appears.
     66 
     67 ## What happens
     68 
     69 Settings → Payment services → ⋮ on an exchange the wallet has never contacted
     70 → **Treat as global**. wallet-core rejects the request:
     71 
     72 ```
     73 code  7001  (WALLET_UNEXPECTED_EXCEPTION)
     74 hint  unexpected exception (message: expected string at
     75       (AddGlobalCurrencyExchangeRequest).exchangeMasterPub but got undefined)
     76 stack at _DecodingError … at decode … at dispatchRequestInternal
     77 ```
     78 
     79 Nothing is shown to the user: the action silently does nothing, and the only
     80 trace is the error in logcat.
     81 
     82 ## Why
     83 
     84 `ExchangeItem.masterPub` is nullable and starts out null — it is only filled in
     85 once the exchange's `/keys` has been fetched (`Exchanges.kt:33`):
     86 
     87 ```kotlin
     88 val masterPub: String? = null,
     89 ```
     90 
     91 `BalanceManager.addGlobalCurrencyExchange` puts it into the request
     92 unconditionally (`BalanceManager.kt:137`):
     93 
     94 ```kotlin
     95 api.request<Unit>("addGlobalCurrencyExchange") {
     96     put("currency", currency)
     97     put("exchangeBaseUrl", exchange.exchangeBaseUrl)
     98     put("exchangeMasterPub", exchange.masterPub)   // null -> key removed
     99 }
    100 ```
    101 
    102 `org.json.JSONObject.put(String, String?)` *removes* the key when the value is
    103 null rather than storing JSON null, so wallet-core receives a request with no
    104 `exchangeMasterPub` at all and fails to decode it. The failure is reported as
    105 an unexpected exception because a missing required field is, from core's point
    106 of view, a client bug.
    107 
    108 Confirmed from the other side, which pins the precondition exactly: with the
    109 exchange **contacted first** -- its terms accepted and "Reload information"
    110 tapped, so `/keys` has been fetched and `masterPub` is set -- the same menu
    111 entry works. The currency scope then forms across both services, and the
    112 "Where to pay" screen that only exists for a scope with more than one shopping
    113 URL renders:
    114 
    115 ```
    116 - the shopping screen: ['Your payment service offers the following website(s) …',
    117                         'http://localhost:8080/config?shop=one',
    118                         'http://localhost:8080/config?shop=two']
    119 - 2 shopping URLs listed
    120 ```
    121 
    122 So this is not "treat as global is broken"; it is "treat as global is broken
    123 for an exchange that has been added but not yet talked to", which is exactly
    124 the state an exchange is in right after being added by URL. That is also why
    125 `balances_two_exchanges` reached nothing for several rounds: it treated both
    126 services as global straight after adding the second one.
    127 
    128 Two further places make it invisible rather than merely broken:
    129 
    130 * `removeGlobalCurrencyExchange` (`BalanceManager.kt:154`) has the identical
    131   null-`masterPub` problem.
    132 * `ExchangeListScreen.kt:356` discards both callbacks
    133   (`onSuccess = {}, onError = {}`), so neither outcome reaches the UI.
    134 
    135 ## Suggested fix
    136 
    137 Treat a null `masterPub` as "not possible yet" rather than passing it on: have
    138 `ExchangeAction.GlobalAdd`/`GlobalDelete` bail out (or reload the exchange
    139 first) when `exchange.masterPub == null`, and hide or disable the menu entry in
    140 that state. Surfacing the error through `onError` would at least make it
    141 visible, but the request should not be sent in the first place.
    142 
    143 A fix in the UI only is enough for the user-visible symptom; the underlying
    144 sharp edge — `JSONObject.put` dropping null-valued keys, turning a nullable
    145 Kotlin field into a missing required field — is worth checking across the other
    146 `api.request { put(...) }` call sites. A sweep of all of them found every other
    147 nullable argument guarded (`restrictAge?.let { put(…) }`,
    148 `status.exchangeBaseUrl ?: error("no exchangeBaseUrl")`), so
    149 `addGlobalCurrencyExchange` and `removeGlobalCurrencyExchange` are the only two
    150 that pass a nullable straight through.
    151 
    152 The same sweep found `ExchangeListScreen.kt:361` and `:369` to be the only two
    153 places in the app that discard an error callback with `onError = {}` — both of
    154 them this feature.