From e79e16b572d6e3472ac94500bf812d937bd89135 Mon Sep 17 00:00:00 2001 From: Anant Date: Fri, 25 Sep 2026 21:28:56 +0530 Subject: [PATCH 001/254] fix: synchronize release versions and verify packaged artifacts --- Gemfile.lock | 5 +- android/app/build.gradle | 22 +- android/fastlane/Fastfile | 42 ++-- android/fastlane/README.md | 2 +- android/gradle.properties | 4 - ios/Podfile.lock | 214 +++++++++--------- ios/fastlane/Fastfile | 19 +- ios/hexa_keeper.xcodeproj/project.pbxproj | 16 +- ios/hexa_keeper/Info.plist | 2 +- ios/hexa_keeper_dev-Info.plist | 2 +- .../.openspec.yaml | 2 + .../fix-release-version-consistency/design.md | 46 ++++ .../proposal.md | 33 +++ .../specs/release-version-consistency/spec.md | 76 +++++++ .../fix-release-version-consistency/tasks.md | 35 +++ openspec/config.yaml | 2 +- package.json | 7 +- release/README.md | 40 ++++ release/version.json | 21 ++ scripts/release-version.cjs | 168 ++++++++++++++ scripts/release-version.test.cjs | 80 +++++++ scripts/test_release_artifact.py | 86 +++++++ scripts/verify-release-artifact.py | 112 +++++++++ 23 files changed, 873 insertions(+), 163 deletions(-) create mode 100644 openspec/changes/fix-release-version-consistency/.openspec.yaml create mode 100644 openspec/changes/fix-release-version-consistency/design.md create mode 100644 openspec/changes/fix-release-version-consistency/proposal.md create mode 100644 openspec/changes/fix-release-version-consistency/specs/release-version-consistency/spec.md create mode 100644 openspec/changes/fix-release-version-consistency/tasks.md create mode 100644 release/README.md create mode 100644 release/version.json create mode 100644 scripts/release-version.cjs create mode 100644 scripts/release-version.test.cjs create mode 100644 scripts/test_release_artifact.py create mode 100644 scripts/verify-release-artifact.py diff --git a/Gemfile.lock b/Gemfile.lock index 1dabd56760..c60ef7ba78 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -288,8 +288,11 @@ PLATFORMS ruby DEPENDENCIES - cocoapods (>= 1.11.3) + activesupport (>= 6.1.7.5, != 7.1.0) + cocoapods (>= 1.13, != 1.15.1, != 1.15.0) + concurrent-ruby (< 1.3.4) fastlane (~> 2.225.0) + xcodeproj (< 1.26.0) RUBY VERSION ruby 3.3.0p0 diff --git a/android/app/build.gradle b/android/app/build.gradle index e8ead00b0a..a56f8d534d 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -98,8 +98,8 @@ android { applicationId "io.hexawallet.keeper" minSdkVersion rootProject.ext.minSdkVersion targetSdkVersion rootProject.ext.targetSdkVersion - versionCode 621 - versionName "2.3.15" + versionCode 622 + versionName "2.5.15" missingDimensionStrategy 'react-native-camera', 'general' missingDimensionStrategy 'store', 'play' multiDexEnabled true @@ -115,12 +115,11 @@ android { signingConfigs { release { - if (project.hasProperty('MYAPP_RELEASE_STORE_FILE')) { - storeFile file(MYAPP_RELEASE_STORE_FILE) - storePassword MYAPP_RELEASE_STORE_PASSWORD - keyAlias MYAPP_RELEASE_KEY_ALIAS - keyPassword MYAPP_RELEASE_KEY_PASSWORD - } + // Release credentials belong in the build environment, never in Git. + storeFile file(System.getenv('KEEPER_ANDROID_KEYSTORE') ?: 'release.keystore') + storePassword System.getenv('STORE_PASSWORD') + keyAlias System.getenv('KEY_ALIAS') + keyPassword System.getenv('KEY_PASSWORD') } debug { storeFile file('debug.keystore') @@ -134,13 +133,16 @@ android { signingConfig signingConfigs.debug } release { - signingConfig signingConfigs.release + // Caution! In production, you need to generate your own keystore file. + // see https://reactnative.dev/docs/signed-apk-android. + // signingConfig signingConfigs.debug minifyEnabled enableProguardInReleaseBuilds proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro" } } productFlavors { production { + signingConfig signingConfigs.release minSdkVersion rootProject.ext.minSdkVersion applicationId "io.hexawallet.bitcoinkeeper" targetSdkVersion rootProject.ext.targetSdkVersion @@ -203,4 +205,4 @@ dependencies { implementation 'com.itextpdf:itextg:5.5.10' -} +} \ No newline at end of file diff --git a/android/fastlane/Fastfile b/android/fastlane/Fastfile index 6765414875..cfc909f0db 100644 --- a/android/fastlane/Fastfile +++ b/android/fastlane/Fastfile @@ -47,20 +47,18 @@ platform :android do ) end - desc "Push a new live build to Slack and internal track on Play Store" + desc "Build explicitly versioned production artifacts and upload to Play internal testing" lane :live do - file_name = "../../.env" - file_url = "https://raw.githubusercontent.com/bithyve/bitcoin-keeper-envs/live/app.live.env" - auth_header = "Authorization: token #{ENV["GITHUB_ACCESS_TOKEN"]}" - sh( - "curl -H '#{auth_header}' -o '#{file_name}' -s '#{file_url}'", - step_name: "curl -H '***' -o '#{file_name}' -s '#{file_url}'" - ) + sh("node", File.expand_path("../../scripts/release-version.cjs", __dir__), "release-check") + production_env = File.expand_path("../../.env.production", __dir__) + UI.user_error!("Missing reviewed .env.production") unless File.file?(production_env) + ENV['ENVFILE'] = production_env store_password = ENV["STORE_PASSWORD"] key_password = ENV["KEY_PASSWORD"] key_alias = ENV["KEY_ALIAS"] - releaseFilePath = File.join(Dir.pwd, "../app", "release.keystore") - bump_version_code() + UI.user_error!("Missing Android release signing credentials") if [store_password, key_password, key_alias].any? { |value| value.to_s.empty? } + releaseFilePath = ENV["KEEPER_ANDROID_KEYSTORE"] || File.expand_path("../app/release.keystore", __dir__) + UI.user_error!("Missing Android release keystore") unless File.file?(releaseFilePath) gradle(task: 'clean') gradle( task: 'bundle', @@ -73,9 +71,8 @@ platform :android do "android.injected.signing.key.password" => key_password, } ) - upload_to_play_store( - track: 'internal' - ) + aab_path = Actions.lane_context[SharedValues::GRADLE_AAB_OUTPUT_PATH].to_s + UI.user_error!("Build did not return an AAB path") if aab_path.empty? gradle( task: 'assemble', build_type: 'productionRelease', @@ -87,21 +84,36 @@ platform :android do "android.injected.signing.key.password" => key_password, } ) + apk_path = Actions.lane_context[SharedValues::GRADLE_APK_OUTPUT_PATH].to_s + UI.user_error!("Build did not return an APK path") if apk_path.empty? + verifier = File.expand_path("../../scripts/verify-release-artifact.py", __dir__) + sh("python3", verifier, aab_path) + sh("python3", verifier, apk_path) + upload_to_play_store( + track: 'internal', + aab: aab_path + ) end end after_all do |lane| versionCode = android_get_version_code() - slack(message: "Build success :rocket:\nVersion Code: " + versionCode.to_s) + UI.message("Build success. Version Code: " + versionCode.to_s) + if ENV['KEEPER_NOTIFY_SLACK'] == '1' + slack(message: "Build success :rocket:\nVersion Code: " + versionCode.to_s) end +end - error do |lane, exception| +error do |lane, exception| + UI.error("Fastlane failed: " + exception.message.to_s) + if ENV['KEEPER_NOTIFY_SLACK'] == '1' slack( message: exception.message, success:false ) end +end lane :bump_version_code do path = '../app/build.gradle' diff --git a/android/fastlane/README.md b/android/fastlane/README.md index a0074a4078..2e7a754656 100644 --- a/android/fastlane/README.md +++ b/android/fastlane/README.md @@ -40,7 +40,7 @@ Push a new dev build to Slack [bundle exec] fastlane android live ``` -Push a new live build to Slack and internal track on Play Store +Build explicitly versioned production artifacts and upload to Play internal testing ---- diff --git a/android/gradle.properties b/android/gradle.properties index 4e6e2e1b48..2f15da72f1 100644 --- a/android/gradle.properties +++ b/android/gradle.properties @@ -36,10 +36,6 @@ reactNativeArchitectures=armeabi-v7a,arm64-v8a,x86,x86_64 # are providing them. newArchEnabled=true -MYAPP_RELEASE_STORE_FILE=release.keystore -MYAPP_RELEASE_KEY_ALIAS=hexawallet -MYAPP_RELEASE_STORE_PASSWORD=hexa@2021 -MYAPP_RELEASE_KEY_PASSWORD=hexa@2021 org.gradle.jvmargs=-Xmx4g -XX:MaxMetaspaceSize=2048m -XX:MaxMetaspaceSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 # Use this property to enable or disable the Hermes JS engine. diff --git a/ios/Podfile.lock b/ios/Podfile.lock index 85cbf1ecf8..a5c2f9af0b 100644 --- a/ios/Podfile.lock +++ b/ios/Podfile.lock @@ -131,7 +131,7 @@ PODS: - nanopb/encode (= 3.30910.0) - nanopb/decode (3.30910.0) - nanopb/encode (3.30910.0) - - NitroIap (14.4.18): + - NitroIap (16.5.0): - boost - DoubleConversion - fast_float @@ -139,7 +139,7 @@ PODS: - glog - hermes-engine - NitroModules - - openiap (= 1.2.22) + - openiap (= 3.4.0) - RCT-Folly - RCT-Folly/Fabric - RCTRequired @@ -193,7 +193,7 @@ PODS: - ReactCommon/turbomodule/core - SocketRocket - Yoga - - NitroModules (0.29.6): + - NitroModules (0.35.4): - boost - DoubleConversion - fast_float @@ -222,7 +222,7 @@ PODS: - ReactCommon/turbomodule/core - SocketRocket - Yoga - - openiap (1.2.22) + - openiap (3.4.0) - PDFGenerator (3.1.0) - PromisesObjC (2.4.0) - QRCoder (1.1.0) @@ -3971,7 +3971,7 @@ CHECKOUT OPTIONS: SPEC CHECKSUMS: boost: 7e761d76ca2ce687f7cc98e698152abd03a18f90 - BVLinearGradient: cb006ba232a1f3e4f341bb62c42d1098c284da70 + BVLinearGradient: 880f91a7854faff2df62518f0281afb1c60d49a3 CocoaAsyncSocket: 065fd1e645c7abab64f7a6a2007a48038fdc6a99 DoubleConversion: cb417026b2400c8f53ae97020b2be961b59470cb fast_float: b32c788ed9c6a8c584d114d0047beda9664e7cc6 @@ -3987,16 +3987,16 @@ SPEC CHECKSUMS: GoogleDataTransport: aae35b7ea0c09004c3797d53c8c41f66f219d6a7 GoogleUtilities: 00c88b9a86066ef77f0da2fab05f65d7768ed8e1 hermes-engine: 4ec27e7c84ed7950da6e65008539685455d8bef4 - HtmlToPdf: 6a9c28f54ec810d1d120a698a9b3c83e2bdb1672 + HtmlToPdf: 5c0649e93aa1c6583f400268eedea00f15e0fa5f libportal-ios: d9aa55474e2d5be8e38e96345dd37be34fda45b4 - libportal-react-native: 91b6bec36f7e92a0bcc4e9d51d6ce3ee7c163728 + libportal-react-native: 05778ef04321517c8366dc7173d05aa9fe131ff2 libwebp: 02b23773aedb6ff1fd38cec7a77b81414c6842a8 MMKVCore: 3d16ce9f7d411e135020915fde98a056859a1efa nanopb: fad817b59e0457d11a5dfbde799381cd727c1275 - NitroIap: 4567529625c807049c148f8cecfc0d774d67b584 - NitroMmkv: 320feb7789986e3ba45582d5c630ebcfeca807ae - NitroModules: 7d693306799405ca141ef5c24efc0936f20a09c0 - openiap: a6c203f0e8f4ace4f05477f90a2967327ef3ad70 + NitroIap: 2aab68488ccce3c0d6046f0e896b3ed4a5838314 + NitroMmkv: 1f66a0e71a3a3c5677921aca853aabf7911d92c1 + NitroModules: b632f83915f059473b72b5d6fb90ff2c2ca674c1 + openiap: 253070b2afe71078a2cae63a2b8e282bdd44a059 PDFGenerator: 17d6bc525db0a3fcd156fbf00f9d1b8d5cc75d1e PromisesObjC: f5707f49cb48b9636751c5b2e7d227e43fba9f47 QRCoder: cbd2bee531cc86d286df7942334cfed94c803ae4 @@ -4004,115 +4004,115 @@ SPEC CHECKSUMS: RCTDeprecation: c7d33f6bc08dbb8be7dfa70d219bfb034480a406 RCTRequired: 42b13c9504609bae9a9f65673a6b4fcbd8d07bd1 RCTSwiftUI: cef2010e1e5e699600b2064d3aa702e53c35816c - RCTSwiftUIWrapper: 18f0f0601b556e27593928c36697d5f651db760f + RCTSwiftUIWrapper: dc8026f15059e65ae5f23ce8b7032f52318e04bb RCTTypeSafety: c5fd6a8092bee25eaa31d4cca03794e902b5fdfd React: 6c36d83b2afecc1d45ec2a637158750edad20d87 React-callinvoker: 813372c3165324939d57b849f2db0c99390b9aac - React-Core: b2a189a7e16fc88a768908152201f14379708ac0 - React-CoreModules: 7ff1464d8c75a63c4028571217e4f4d01b008919 - React-cxxreact: 06aed26685cbd29a55d1648a14f13491549cea9d + React-Core: 497da2fa08060213b5b265b38cb64af2e0f0526c + React-CoreModules: 255a3150b3a24c8628ef050e7ae448d198dd003b + React-cxxreact: e6a1142898716d391d27bd7d05091fca0046e2c5 React-debug: eeaaa885aed6766643a249e186cb55910847f32d - React-defaultsnativemodule: 137369659d426f45688b7467775a6dc153abf127 - React-domnativemodule: 5c80b3bd89b4d14d1e57f2c2ac2a8e7a3d2a7dcc - React-Fabric: be18c20ebb56a507b6f04dd701fdc79ab7111763 - React-FabricComponents: 648428bb2ea49ee4db5796d9670994280c93e447 - React-FabricImage: 44232bb0498164e155ffb33621acc1851146d964 - React-featureflags: 840962f151a33d44a93f289d59bcffd166fa8e39 - React-featureflagsnativemodule: 2dac13fa41ee8eff0c45e2a336f5c98291ef9a96 - React-graphics: 8f80ab9311f947fade8d34ddd3cff8870441d000 - React-hermes: f241babdb4e8595933d856ab78f5d5d49218cac0 - React-idlecallbacksnativemodule: 2d0687f8820e232f4686109721ec066c169f9232 - React-ImageManager: cda1751f63b04e8a7df4cb65894ed34c6043fb2e - React-intersectionobservernativemodule: 34d7b434d4df27a113158bed316a4e0dff658d2b - React-jserrorhandler: e8ad51be4b0e29eadfdd7854675cf5087a4ea5f9 - React-jsi: ee1dbc3b2635ce07783e6638656c0b472573bb48 - React-jsiexecutor: 05fe7918c713a64a39474915f725fe0e61d65309 - React-jsinspector: 57db0ee88c1471b7310d3feb5c3c92cec6db4702 - React-jsinspectorcdp: 757575b5a8151ca2f253f21e73ef371bc92482a8 - React-jsinspectornetwork: 443fb13ccad11b861d5810f572e6eceb7dd98e05 - React-jsinspectortracing: 59c88018cbc7855bb26c49d8b3bb5ce0d7a6c00a - React-jsitooling: abc1f89dd35866995bcb873c60a15e8af82d0d96 - React-jsitracing: e8993f012752e5f7a5e749d9348095c30091bb27 - React-logger: cf2064f903777a5bdca904c4265f17396d4d8568 - React-Mapbuffer: c546207c9ac05ba26b90ba26f1288f84c8867f2e - React-microtasksnativemodule: ad2becc1076529952b6db26d0a1476ec489b9067 - React-mutationobservernativemodule: 867e9c215f21b4308fa98473f3c38585b5897e44 - react-native-background-timer: 4638ae3bee00320753647900b21260b10587b6f7 - react-native-biometrics: ecca256a9e1c8b430f78c4e3dcb16b57ea1418e0 - react-native-blob-util: 6dec9023ecc3ee5ac8ad377598e943f9ca63f741 - react-native-change-icon: 2abab62ecabf10adf6e03cc9516484a27385b580 - react-native-config: 8e425892a531627c52db765be3088185cb871e19 - react-native-contacts: 2a259e02b85c9510a5fcbdf06c56c828387d47c1 - react-native-document-picker: dc2d83366e47e89e7c51e8a41eab99c1d54e941c - react-native-get-random-values: e2acf4070fe4b7325705a05af047449637a27a21 - react-native-hce: ad5462e792d84e87924308dda1ee516757f1c0eb - react-native-image-picker: 1f5318beec2ebed6695454ffb9bea8c4152f1598 - react-native-netinfo: cec9c4e86083cb5b6aba0e0711f563e2fbbff187 - react-native-nfc-manager: e5d2ce2f7fee42d1ee0eb6b99f3405d5e330112e - react-native-pdf: 2886351f9ea698a820f53056afdabd6fa0ab726d - react-native-randombytes: 3c8f3e89d12487fd03a2f966c288d495415fc116 - react-native-rsa-native: a7931cdda1f73a8576a46d7f431378c5550f0c38 - react-native-safe-area-context: befb5404eb8a16fdc07fa2bebab3568ecabcbb8a - react-native-tcp-socket: 7c7e53a07f122ecf00fb3626684bc0ca82c4f044 - react-native-worklets-core: aaaac7d17f7e576592369a54f30e96fe4875c983 - React-NativeModulesApple: 519f9f98375db6458a7220becd25db81b3860b76 - React-networking: 2318cba8288ec5df38e899feed3baaac0629b4bd + React-defaultsnativemodule: ff63dc721adb5db6eef0951035d4a2935ac99aa8 + React-domnativemodule: eb73ab018bdd17b950515fc6d1b06fa0ae953110 + React-Fabric: 2815ec8bfd1c71a8696bbee7d3ff2a97af27b1c3 + React-FabricComponents: caf3e972e0d2b7feed1f0003377a74a6d9e12cac + React-FabricImage: 9c0298b399cf042d937f42ffcdde9047637fdc0f + React-featureflags: 7ff07b72680999f92cf82fdf58477b38a876d3b1 + React-featureflagsnativemodule: ad7919cff5b36a45af42433d323b8fd23931dadd + React-graphics: 57c9e72ea57228e83474a27367c4182a79acc803 + React-hermes: 85101b86c76672bdfc78def14feb93c15445f029 + React-idlecallbacksnativemodule: d8402e2ed16af4bc0f506315bfdf96007decd80b + React-ImageManager: f0f9fafd26ff52e16781fbc8844a55bd3ba21513 + React-intersectionobservernativemodule: 5bc884eb7e1fe8fec87faf712c963adcec95da69 + React-jserrorhandler: 0a5be1ec13b7ae0404545422bef137be0d36b38b + React-jsi: abd0559cbfcd75539f61bc6ee3ae1e72c38b8727 + React-jsiexecutor: e96ff30c2e7ab4c1a1652e6a48d44f80b774b411 + React-jsinspector: 0199e40d52099d06060387d5143dc32cce90aa83 + React-jsinspectorcdp: 6ca5a4774f941b0a791790adeb94fa55f5338b88 + React-jsinspectornetwork: 1551f3fb9c1db9e1e178acfca43aec29c99f82a9 + React-jsinspectortracing: ea86019081a9a746470d85307209118daa6aa6cc + React-jsitooling: 3c1dfec708ea756367d36784c158487d149a3b96 + React-jsitracing: ef17de0220ff8f0bbf95b5a41b63b44b81cf5a55 + React-logger: 532b0b1405e20c08146a739fbaf6fd16fd771291 + React-Mapbuffer: a803b9ab86de5b3cacfd1f7e324300a4e932d201 + React-microtasksnativemodule: 5b3728f26cfad14e7d05bba8e36f19ae083a787b + React-mutationobservernativemodule: db471fd2cc188f72dc7d5add41d2bebf5361f0ce + react-native-background-timer: 17ea5e06803401a379ebf1f20505b793ac44d0fe + react-native-biometrics: 3b95f2eb074d537d3a8b05d853c17778f6685c4a + react-native-blob-util: ca455d4d8cc8ed667b6c3dfd7ca00aef09f6ee63 + react-native-change-icon: 5bfde1910111f603e75b77e1bf9feabaf2bb070e + react-native-config: 7cd105e71d903104e8919261480858940a6b9c0e + react-native-contacts: 8787d1e792bae8ba6edd1c7ea2749689440ab4bd + react-native-document-picker: 063f6391c62fafd49f97490a3ebb1255e4e30781 + react-native-get-random-values: 9856c21cab42a6d8db71dcb35c3059fdaeac2b16 + react-native-hce: ae425226cd27e1831b98e5577f15f686a1a0dab8 + react-native-image-picker: 60f4246eb5bb7187fc15638a8c1f13abd3820695 + react-native-netinfo: f0a9899081c185db1de5bb2fdc1c88c202a059ac + react-native-nfc-manager: 1bcdf6f96c9065a8f4c8d9a0c6fb2db625205b2c + react-native-pdf: cd45064d7ef704e69f44c2cee14f0635764c752b + react-native-randombytes: 421f1c7d48c0af8dbcd471b0324393ebf8fe7846 + react-native-rsa-native: 12132eb627797529fdb1f0d22fd0f8f9678df64a + react-native-safe-area-context: 0f4986a88ec555aff660503b483d6e4bd6980a9a + react-native-tcp-socket: c9290d77fea17ee75bee93e599b604bae63166f0 + react-native-worklets-core: 30da6af07bef6fcb77ed2b53787958c3d56048e8 + React-NativeModulesApple: 74bee0c16a7a5426aeadd753179a41d7da413c99 + React-networking: 08d6e4f20fc02f240fb1c7b48bc0d72fdc66b1ea React-oscompat: 4524d8ccb12b7f07beb12e2ea9c5ea55b55d604b - React-perflogger: 2b2a2bc9173796cc58fece00d3df1c99b39c2b8e - React-performancecdpmetrics: 162db18cc31c25256d393383890cb0f3de27cf73 - React-performancetimeline: a3b28cf401e8cb2027fb282a697ea87d97cd320a + React-perflogger: ccc20d34acfe74605f0f2d2a414fddf731b14bd1 + React-performancecdpmetrics: a750a2a3276b32d543542bfe72f7509b65fd8a3a + React-performancetimeline: a7bf3ba360917c9001e26c47e55ccab2f6895d00 React-RCTActionSheet: e1c8afe045a25f3a8d73da52bb28e6186718e206 - React-RCTAnimation: 7681b2367405b53d4e91ac76b36bd8fb1ce50a53 - React-RCTAppDelegate: e8d05c439051bd6860638e59fc8fb81cf3ac0f9b - React-RCTBlob: fa0e5ab89883e4bf5eb05e534b68ce2951ca514b - React-RCTFabric: d505530c38fada112559a19e91d200a6b8381d4f - React-RCTFBReactNativeSpec: 2d7c44c5b4349957cec8c77bb5cd401d9348c4fc - React-RCTImage: 8d32ede0e9f07aa7a446a54e924f23b3347d2960 - React-RCTLinking: ef0c625de1ce2b78189cb65391888628b5d2d6ed - React-RCTNetwork: 5e6a3678c23c0b8c4e2b69e85551215f1fcfe2b2 - React-RCTRuntime: ec85cc9d0ca3bd98bfcc0674c59f011eacc96d91 - React-RCTSettings: c7f2c32bee82c3f4e757d779fff3c2bf47cc7e46 - React-RCTText: cc134029a95773223502a9aeac47300586426ca5 - React-RCTVibration: 9ed48065a50d4a4311aed5c17272061f2cae910b + React-RCTAnimation: f350cac40fbf4d2f7269c50a4809812a51e7143a + React-RCTAppDelegate: c7e4dd9a3a94f348a9c9e3f6ec30a2fa45713d0b + React-RCTBlob: 8215edb879697c37ebc3d93d44210c136b0f8f12 + React-RCTFabric: e2a194c2ae6cba49dc67f72250bd48d5457a0711 + React-RCTFBReactNativeSpec: 77b5b284baa526512b143c24ae44d7e24c688e65 + React-RCTImage: 55ce5873af9d447dde76711c84ea545688eb5e01 + React-RCTLinking: 803e8a4356dc848d1cff0882e70b81c2c28f67e1 + React-RCTNetwork: 5e13fe88a73150e7cd7eff989a55b5c1c7e4b701 + React-RCTRuntime: ab3bddd645d86fd1858d9005fe4943cf333384a2 + React-RCTSettings: 3a6fd39c7169fe408f6cf16d939ba7910b02fe6e + React-RCTText: 5a2c8addcbe6d3d36636407423d9969012fcdaa7 + React-RCTVibration: 460b55fcd5ae2f028e9910ccad192f5ffea1d6b8 React-rendererconsistency: 9262a6a4c4be861ffd7e3149c517bd4076a8851f - React-renderercss: 4840f91b676f4000cc704f9214f950df02be54f1 - React-rendererdebug: 613c8b53e6032d16c4c499d52bbd21c9df6873fa - React-RuntimeApple: d53d701e0ac401fa6caca87d22881559617e5a5a - React-RuntimeCore: 2232f165e559a6edf997a5b18e9669a63e997cba - React-runtimeexecutor: fbe7aa46f3c1233e903fa6758a1c8c372070c33b - React-RuntimeHermes: a5ada5beb920f09b3fb3fce491d8d3d132c6e677 - React-runtimescheduler: 81f357f73c68c93459b714b1f917e30a40f1a1ea - React-timing: 5ace10dca5f52437bb6d4212404898d9ab7df82f - React-utils: 29d70520468725a40da28decabd1c58dda7f6f90 - React-webperformancenativemodule: a5f9909a36ae47ea244e38efd25e479c01d39e14 - ReactAppDependencyProvider: 87593e9dfdba8dac1d7b2af1af05f0eff2a05684 - ReactCodegen: 417334350a76a91784cc20f7aff392327e217dc4 - ReactCommon: 5f2db7556b60816bdc5761a359539bacd2d0250d - RealmJS: 10fb99cdac98cad8a6c4471c967bd6be10307741 - RNCClipboard: adba6334687b7fb2c37760e26dedd550b4846a72 - RNDeviceInfo: f9d6fd102f8b11daa7ee322391b23cebee3bd95f - RNFastImage: 462a183c4b0b6b26fdfd639e1ed6ba37536c3b87 - RNFBApp: 022bf8a1be198c5b8dc8af4dcb656d49f903aa8d - RNFBMessaging: e42061ab9fab61081c38b5661b292c1f295bbc96 - RNFS: 89de7d7f4c0f6bafa05343c578f61118c8282ed8 - RNGestureHandler: be7148bc663eac98c0ad6474b4b105f8cadd56be - RNKeychain: a2c134ab796272c3d605e035ab727591000b30f3 - RNLocalize: 19917c3f32cf6386f7d2957a638a4281669cae57 - RNQrGenerator: af2f0888eb81b8ff99517a3929d6399444dcc56d - RNReanimated: 4e6187f2ba1e30e6ddc7c8fc9f19b2dd2b6b2532 - RNScreens: 7f643ee0fd1407dc5085c7795460bd93da113b8f - RNSentry: 80792b62c5fa701a58e27d022e1eb20d52a796e5 - RNShare: 51d221e8ac06263e91168c604b94274fdcc4784f - RNSVG: 3405336c4507ee0bafb02b08058d57216daa2c50 - RNWorklets: 6daa3975bebe6047822c7485666d501a716674ca + React-renderercss: 1641d6d12b7aa54fda225dd0f1fa24f199d523ae + React-rendererdebug: 92ee565692a4a3e6806e88fa4ee816bda5cee693 + React-RuntimeApple: a4f51fa6ebd62e5ac0d3445e47d2a71efd750939 + React-RuntimeCore: c5f51bd6afd2884341749817c5ec512d0dcbc4a7 + React-runtimeexecutor: 3e96660fb93a17185bc8b23c6bbe10fe25f6d3e2 + React-RuntimeHermes: 8382231d2cdfc51bb67785fbc6cc4218f7e934c4 + React-runtimescheduler: 44aa1cbed3ce2759678a1e544ad27108c934a046 + React-timing: ac50e2e44dc82ec7241557bad16adcae184a693b + React-utils: 493a45d877f410d62476c045fbf220acbffba194 + React-webperformancenativemodule: 9b212313b01f5489f21963fbd4a81ef20f280097 + ReactAppDependencyProvider: 5bf52020fdc0d0a337068ea42d4a6676ce4a04bc + ReactCodegen: 5c808345ff57b4f4f1eab230f541921ad9e37500 + ReactCommon: 3b79ecfd327035654f6c36cf1de8e222d8b835e6 + RealmJS: a2e97359d58d72ca9fc2deb2e14ba34a218652e5 + RNCClipboard: a9d5216cbfaf2f51cfe80bcb132856bb9158fe0d + RNDeviceInfo: 0a7c1d2532aa7691f9b9925a27e43af006db4dae + RNFastImage: 5c9c9fed9c076e521b3f509fe79e790418a544e8 + RNFBApp: bb2b158188e3bd5161cbdcd7e9058c2edfaa8c11 + RNFBMessaging: 994290a71d043bc0837e89514be19ce02805790c + RNFS: 4ac0f0ea233904cb798630b3c077808c06931688 + RNGestureHandler: afae32614741017de0f813f74586eee29773f31a + RNKeychain: 182d986f809280edd9444a557adc21845f7d1e6f + RNLocalize: 95a43f85e41a966be7bc9cff2437128911c52da0 + RNQrGenerator: 36f1af182d7673bc36e317aca0d293d19883d6e3 + RNReanimated: 6b69a94db352e4dae7eb540f6fab60ff05c18508 + RNScreens: 7179cc1ba31b4e18ed29f10abf20c24a7961cf4c + RNSentry: f07853ff3fcbb69df26da1d9744e95ee97f683fd + RNShare: 11923289a9964c6bd5f507eaa7fe61ec51b0adce + RNSVG: 6a91afc99237de70b617c7a84ba84c988ad5573b + RNWorklets: 3f35c2c01e5839d4fee0496d10ada185628ba328 SDWebImage: a7f831e1a65eb5e285e3fb046a23fcfbf08e696d SDWebImageWebPCoder: 908b83b6adda48effe7667cd2b7f78c897e5111d Sentry: 4e85b40e4b0235853f40f959d315a6b28e3148d8 SocketRocket: d4aabe649be1e368d1318fdf28a022d714d65748 - VisionCamera: 0044a94f7489f19e19d5938e97dfc36f4784af3c + VisionCamera: 47f10351a1b6a8d251b90986ae72ca5f52331954 Yoga: e6489bbfdab9b98f8d51993484d6bd40216b7834 ZXingObjC: 8898711ab495761b2dbbdec76d90164a6d7e14c5 PODFILE CHECKSUM: b6c63a86c263fb282b75313bfa3faf16def35484 -COCOAPODS: 1.16.2 +COCOAPODS: 1.15.2 diff --git a/ios/fastlane/Fastfile b/ios/fastlane/Fastfile index 7147a897aa..b231891c82 100644 --- a/ios/fastlane/Fastfile +++ b/ios/fastlane/Fastfile @@ -35,11 +35,7 @@ def resolved_ipa_path! ipa_path = Actions.lane_context[SharedValues::IPA_OUTPUT_PATH].to_s if ipa_path.strip.empty? - ipa_path = Dir["./builds/*.ipa"].sort_by { |path| File.mtime(path) }.last.to_s - end - - if ipa_path.strip.empty? - UI.user_error!("Unable to locate generated .ipa file for TestFlight upload") + UI.user_error!("Current build did not return an IPA path; refusing to reuse an older archive") end ipa_path @@ -108,14 +104,10 @@ platform :ios do desc "Push a new live build to TestFlight" lane :live do - file_name = "../../.env" - file_url = "https://raw.githubusercontent.com/bithyve/bitcoin-keeper-envs/live/app.live.env" - auth_header = "Authorization: token #{ENV["GITHUB_ACCESS_TOKEN"]}" - sh( - "curl -H '#{auth_header}' -o '#{file_name}' -s '#{file_url}'", - step_name: "curl -H '***' -o '#{file_name}' -s '#{file_url}'" - ) - increment_build_number(xcodeproj: "hexa_keeper.xcodeproj") + sh("node", File.expand_path("../../scripts/release-version.cjs", __dir__), "release-check") + production_env = File.expand_path("../../.env.production", __dir__) + UI.user_error!("Missing reviewed .env.production") unless File.file?(production_env) + ENV['ENVFILE'] = production_env get_certificates( # Create or get certificate, and install it output_path: "./builds" # Download certificate in the build folder (you don't need to create the folder) @@ -148,6 +140,7 @@ platform :ios do }, build_path: "./builds", output_directory: "./builds") + sh("python3", File.expand_path("../../scripts/verify-release-artifact.py", __dir__), resolved_ipa_path!) upload_build_to_testflight! end end diff --git a/ios/hexa_keeper.xcodeproj/project.pbxproj b/ios/hexa_keeper.xcodeproj/project.pbxproj index 7f0b8da754..cc49992030 100644 --- a/ios/hexa_keeper.xcodeproj/project.pbxproj +++ b/ios/hexa_keeper.xcodeproj/project.pbxproj @@ -748,7 +748,7 @@ CODE_SIGN_ENTITLEMENTS = hexa_keeper/hexa_keeper.entitlements; CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 614; + CURRENT_PROJECT_VERSION = 615; DEVELOPMENT_TEAM = Y5TCB759QL; ENABLE_BITCODE = NO; HEADER_SEARCH_PATHS = ( @@ -849,7 +849,7 @@ "$(inherited)", "\"$(SRCROOT)\"", ); - MARKETING_VERSION = 2.5.14; + MARKETING_VERSION = 2.5.15; OTHER_LDFLAGS = ( "$(inherited)", "-ObjC", @@ -876,7 +876,7 @@ CODE_SIGN_IDENTITY = "Apple Development"; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Distribution"; CODE_SIGN_STYLE = Manual; - CURRENT_PROJECT_VERSION = 614; + CURRENT_PROJECT_VERSION = 615; DEVELOPMENT_TEAM = Y5TCB759QL; "DEVELOPMENT_TEAM[sdk=iphoneos*]" = Y5TCB759QL; HEADER_SEARCH_PATHS = ( @@ -977,7 +977,7 @@ "$(inherited)", "\"$(SRCROOT)\"", ); - MARKETING_VERSION = 2.5.14; + MARKETING_VERSION = 2.5.15; OTHER_LDFLAGS = ( "$(inherited)", "-ObjC", @@ -1147,7 +1147,7 @@ CODE_SIGN_IDENTITY = "Apple Development"; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Distribution"; CODE_SIGN_STYLE = Manual; - CURRENT_PROJECT_VERSION = 614; + CURRENT_PROJECT_VERSION = 615; DEVELOPMENT_TEAM = Y5TCB759QL; "DEVELOPMENT_TEAM[sdk=iphoneos*]" = Y5TCB759QL; ENABLE_BITCODE = NO; @@ -1250,7 +1250,7 @@ "$(PROJECT_DIR)", "\"$(SRCROOT)\"", ); - MARKETING_VERSION = 2.5.14; + MARKETING_VERSION = 2.5.15; OTHER_LDFLAGS = ( "$(inherited)", "-ObjC", @@ -1278,7 +1278,7 @@ CODE_SIGN_ENTITLEMENTS = hexa_keeper_dev.entitlements; CODE_SIGN_IDENTITY = "Apple Distribution"; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Distribution"; - CURRENT_PROJECT_VERSION = 614; + CURRENT_PROJECT_VERSION = 615; DEVELOPMENT_TEAM = Y5TCB759QL; "DEVELOPMENT_TEAM[sdk=iphoneos*]" = Y5TCB759QL; HEADER_SEARCH_PATHS = ( @@ -1380,7 +1380,7 @@ "$(PROJECT_DIR)", "\"$(SRCROOT)\"", ); - MARKETING_VERSION = 2.5.14; + MARKETING_VERSION = 2.5.15; OTHER_LDFLAGS = ( "$(inherited)", "-ObjC", diff --git a/ios/hexa_keeper/Info.plist b/ios/hexa_keeper/Info.plist index 9e1349847b..43c97bae1e 100644 --- a/ios/hexa_keeper/Info.plist +++ b/ios/hexa_keeper/Info.plist @@ -62,7 +62,7 @@ CFBundleVersion - 614 + $(CURRENT_PROJECT_VERSION) ITSAppUsesNonExemptEncryption LSApplicationQueriesSchemes diff --git a/ios/hexa_keeper_dev-Info.plist b/ios/hexa_keeper_dev-Info.plist index 794948f0ac..ae098530a3 100644 --- a/ios/hexa_keeper_dev-Info.plist +++ b/ios/hexa_keeper_dev-Info.plist @@ -36,7 +36,7 @@ CFBundleVersion - 614 + $(CURRENT_PROJECT_VERSION) ITSAppUsesNonExemptEncryption LSApplicationQueriesSchemes diff --git a/openspec/changes/fix-release-version-consistency/.openspec.yaml b/openspec/changes/fix-release-version-consistency/.openspec.yaml new file mode 100644 index 0000000000..abd7c5ae1c --- /dev/null +++ b/openspec/changes/fix-release-version-consistency/.openspec.yaml @@ -0,0 +1,2 @@ +schema: spec-driven +created: 2026-09-25 diff --git a/openspec/changes/fix-release-version-consistency/design.md b/openspec/changes/fix-release-version-consistency/design.md new file mode 100644 index 0000000000..f8df1e8d18 --- /dev/null +++ b/openspec/changes/fix-release-version-consistency/design.md @@ -0,0 +1,46 @@ +## Context + +T-050 is the selected development item. The user authorized public App Store, Play production and normal APK publication after testing. Google Play API inspection on 2026-09-25 confirmed production package `io.hexawallet.bitcoinkeeper` is `621 (2.3.15)`, with highest returned bundle code 621. Authenticated App Store Connect inspection confirmed 2.5.14/build 612 on September 25, 2026. Local source has package.json 2.5.13, Android 2.5.14/616, iOS 2.5.14/614. Do not mistake the old `io.hexawallet.keeper` Play app for production. + +## Goals / Non-Goals + +**Goals:** One explicit marketing version, independently verified monotonically increasing platform build numbers, and a reproducible manifest of the exact released source/artifacts. Catch the observed 2.5.14 -> 2.3.15 regression before upload. + +**Non-Goals:** No new screen, copy, assets, navigation, React components, wallet behavior, signing implementation, Redux slice/saga, Realm schema, MMKV key or persisted-state migration. No PSBT or hardware interaction. No timestamp fix. Preserve the user's existing IAP fix. + +## Decisions + +- Add `release/version.json` as explicit version intent and `scripts/release-version.cjs` for check/apply commands. Use Node's standard library so preflight does not depend on mobile native build tools. +- Keep native values explicit in Gradle and Xcode for compatibility with existing build tools. An apply command synchronizes them; a check command verifies every Keeper target configuration and package.json. Fail on missing/ambiguous targets rather than silently skipping a platform. +- Store baselines separately from candidate values. Version intent can be drafted while store access is unavailable, but publication preflight must reject an unverified baseline. Numeric build values are not chosen until the relevant store is checked. +- Source preflight verifies semver progression, platform counters and matching source declarations. An artifact verifier separately checks metadata extracted from APK/AAB/IPA. Source checks alone do not prove artifacts were built correctly. +- Packaged feature checks use stable Ask Keeper / Dust route and data markers verified against fresh optimized Hermes bundles. These supplement UI tests. Reject a missing current-build IPA path instead of falling back to an older output. Verify both Android artifacts before the first upload. +- Production Android signing credentials come from the environment and the existing keystore. Remove the committed Gradle credential properties, preserving debug signing. This prevents bringing the newer sprint credential strings into the prepared integration; it does not erase repository history or resolve key-custody review. +- Build both Android outputs from one source/configuration and the same versionCode. Sign with the existing appropriate identities and compare fingerprints to the established release identities; never generate replacement release keys. +- Remove independent Fastlane bump operations for production; use explicit preflight instead. Production lanes must not post Slack messages as a side effect of this task. New public distribution steps require verified source provenance and correct environment configuration. +- Keep draft planning possible with unverified baselines, but make final release checks fail closed. Use the highest verified marketing baseline across platforms (at least 2.5.14), so the Android regression is not treated as the new semantic baseline. +- Supply the reconciled source SHA through `KEEPER_RELEASE_COMMIT`; preflight compares it to HEAD and requires a clean checkout. Keeping the SHA outside the committed manifest avoids a self-referential commit hash. + +## Affected files + +New: `release/version.json`, `scripts/release-version.cjs`, `scripts/release-version.test.cjs`, release documentation and verification evidence. Modified: `package.json`, `android/app/build.gradle`, `ios/hexa_keeper.xcodeproj/project.pbxproj`, `android/fastlane/Fastfile`, `ios/fastlane/Fastfile`, and CI validation workflow as needed. No UI files. Both iOS Info.plist files must use project version variables: a real simulator rebuild demonstrated that hardcoded CFBundleVersion silently overrode the otherwise consistent project settings. + +## Risks / Trade-offs + +- Missing Apple uploaded-build evidence -> keep iOS release blocked and do not invent the next build number. +- Native metadata drift -> check every app target and extracted artifact, not only a root version string. +- Existing Fastlane lanes overwrite `.env` and increment builds -> provide a checked release route before invoking publication; do not invoke legacy lanes blindly. +- Store state changes after checking -> refresh baselines immediately before upload. +- Different branch content -> record the canonical release SHA and verify all three artifacts against it. Resolve release-source reconciliation before publishing. + +## Validation and rollout + +Regression tests must fail on the actual 2.3.15 regression, mismatched package/native versions, missing metadata, reused build codes, and unverified store evidence, and pass a consistent fixture. Run existing startup/settings smoke tests with explicit development app IDs on Android and iOS. User checks: launch/unlock, existing wallet visibility without migration loss, displayed version/build, and upgrade installation for existing production identities. Full wallet/signing testing is outside the version change, but relevant release regressions still gate publication. + +DESIGN.md reviewed: no UI change, so existing typography, colors, components and navigation are preserved. No new screenshots/assets/SVGs or design tokens are needed. + +## Open Questions + +- Candidate selected: 2.5.15, Android 622, iOS 615. Refresh store baselines before upload. +- Canonical release source after reconciling iOS and Android published-build provenance. +- Public APK channel is GitHub Releases with SHA256SUM.asc and KEEPER_DETACHED_SIGN.sign. Existing local keystore certificate matches public APK 2.5.13; PGP signing access remains to be resolved. diff --git a/openspec/changes/fix-release-version-consistency/proposal.md b/openspec/changes/fix-release-version-consistency/proposal.md new file mode 100644 index 0000000000..2cb26b3f55 --- /dev/null +++ b/openspec/changes/fix-release-version-consistency/proposal.md @@ -0,0 +1,33 @@ +## Why + +Tracked task T-050 records a release-version regression: commit `1adf4f66399a2bfac4572d9da94440323bff02f2` changes Android versionName from `2.5.14` to `2.3.15` while increasing versionCode from 616 to 621. The local package version also differs from native version metadata. Release preparation must reconcile actual store uploads and built artifacts so this cannot silently recur. + +## What Changes + +- Establish an explicit release-version manifest and a dependency-free preflight validator for package.json, Android and all Keeper iOS target configurations. +- Record verified published/uploaded baselines, source commit, intended marketing version and platform build numbers before selecting final release values. +- Fail release validation when platform versions differ, the marketing version goes backwards, or a build number is reused against the verified baseline. +- Verify the actual IPA, Android App Bundle and public APK metadata, signing identity and source provenance before publication. +- Prevent Fastlane from independently changing build numbers after validation; preserve existing distribution destinations until explicitly selected. + +## Capabilities + +### New Capabilities + +- `release-version-consistency`: Reconciled source versions and verified release artifact metadata across iOS, Play and APK distribution. + +### Modified Capabilities + +None. + +## Impact + +T-050 under T-053 is the selected first development task. This affects release tooling and version metadata on iOS and Android, with identical marketing version for Mainnet and Testnet builds of the release. No changes to Wallet, Vault, Signer or UTXO behavior, subscription tiers, hardware compatibility, PSBTs, wallet storage, or product UI copy. No new network calls in the app. Release tooling uses authorized store access without embedding credentials in source or logs. Maestro impact: run startup and existing settings smoke checks; no new UI flow is introduced. + +## Non-goals + +Fixing Version History timestamps, unrelated feature development, changing production backend code, rotating signing keys, migrating wallets, or pretending a public listing proves the highest uploaded store build. Local-backend setup remains separate environment work. + +## First milestone scope — 25 September 2026 + +The user requests a small test release with high confidence and explicitly separates security remediation. T-040/T-044 completion and private-advisory sign-in are not prerequisites for this milestone. Make no security-fix claims. The versioning patch introduces no wallet/transaction/storage feature changes. Local Docker packaging and broader feature development remain separate work. Determine regression coverage from the actual selected release source; do not call an unverified sprint-to-production delta version-only. The user reconfirmed public App Store, Play production and the normal public APK channel. diff --git a/openspec/changes/fix-release-version-consistency/specs/release-version-consistency/spec.md b/openspec/changes/fix-release-version-consistency/specs/release-version-consistency/spec.md new file mode 100644 index 0000000000..05498238e0 --- /dev/null +++ b/openspec/changes/fix-release-version-consistency/specs/release-version-consistency/spec.md @@ -0,0 +1,76 @@ +## ADDED Requirements + +### Requirement: Consistent release metadata +The release tooling SHALL require a single marketing version in package.json, Android versionName and every Keeper iOS MARKETING_VERSION. Android versionCode SHALL match the explicit Android release counter and every Keeper iOS CURRENT_PROJECT_VERSION SHALL match the explicit iOS release counter. + +#### Scenario: Matching source metadata +- **GIVEN** verified release intent and matching source versions +- **WHEN** the source preflight runs +- **THEN** it succeeds and prints only non-secret version information. + +#### Scenario: Platform metadata differs +- **GIVEN** one native target or package.json has a different version +- **WHEN** preflight runs +- **THEN** it fails and identifies the mismatched file/field before any build upload. + +### Requirement: Prevent backwards versions and reused builds +The release tooling MUST compare semantic version components numerically and require the new marketing version to exceed the highest verified published marketing baseline. It MUST require platform build counters to exceed the highest verified already-uploaded counter for that platform. Missing or unverified store baselines MUST block publication checks. + +#### Scenario: Observed Android regression +- **GIVEN** a previously released marketing version of 2.5.14 +- **WHEN** a candidate has version 2.3.15 and Android code 621 +- **THEN** the marketing-version check fails even if its numeric Android code increased. + +#### Scenario: Reused Android code +- **GIVEN** the highest verified uploaded Android code is 621 +- **WHEN** the candidate also uses 621 +- **THEN** preflight fails regardless of marketing version. + +#### Scenario: Apple access unavailable +- **GIVEN** the public App Store version is known but uploaded-build evidence is unverified +- **WHEN** publication preflight runs +- **THEN** it fails with an actionable evidence error and does not invent a build number. + +### Requirement: Verify distribution artifacts +The release process MUST verify the packaged IPA version/build and bundle identifier, the AAB/APK versionName/versionCode and production package identifier, and their expected signing identities. The AAB and APK MUST originate from the same release source and carry matching Android version metadata. Source-only checks MUST NOT count as artifact verification. + +#### Scenario: Wrong Android package +- **GIVEN** a build uses `io.hexawallet.keeper` or `.development` instead of `io.hexawallet.bitcoinkeeper` +- **WHEN** production artifact validation runs +- **THEN** it fails before upload. + +#### Scenario: Correct three-platform artifacts +- **GIVEN** the IPA, AAB and APK match the release manifest and verified source/signing identities +- **WHEN** release verification completes +- **THEN** their checksums, versions, build numbers and canonical source SHA are recorded before publication. + +#### Scenario: Stale packaged content despite correct source metadata +- **GIVEN** source declarations match but the packaged binary has an older native counter or lacks required Ask Keeper / Dust markers +- **WHEN** production artifact validation runs +- **THEN** it fails before upload; marker checks use stable route/data strings that survive release compilation. + +#### Scenario: Current iOS build did not return an artifact +- **GIVEN** an earlier IPA remains in the output directory and the current build returns no IPA path +- **WHEN** upload is prepared +- **THEN** the lane fails rather than selecting the older file. + +### Requirement: Preserve app behavior +The change SHALL preserve Wallet, Vault, Signer and UTXO behavior, app storage and existing navigation. It SHALL require startup/settings smoke checks for both native platforms and record actual test outcomes rather than assuming a successful build implies a working app. + +#### Scenario: Development smoke test +- **GIVEN** a development build installed on the intended simulator/emulator +- **WHEN** it launches and the tester navigates to Version History +- **THEN** startup succeeds and the visible installed version/build matches that installed artifact. + +### Requirement: Reconciled feature coverage across platforms +The release candidate SHALL include Ask Keeper and the intended Dust Protection changes together. Both native platforms MUST be built from the same reviewed source commit and checked for the intended feature entry points and behavior. Matching version numbers or a conflict-free Git merge MUST NOT be treated as proof of feature coverage. Historical release-source claims MUST distinguish verified provenance from hypotheses. + +#### Scenario: Reported iOS and Android feature mismatch +- **GIVEN** the prior report of Ask Keeper on Android and Dust with old Concierge on iOS +- **WHEN** the next release candidate is verified +- **THEN** Ask Keeper and Dust Protection, Dust Report and Donate Dust are checked on both platforms and their test results are tied to the common source SHA. + +#### Scenario: Branches merge without textual conflicts +- **GIVEN** a merge preview succeeds without conflicts +- **WHEN** release reconciliation is assessed +- **THEN** feature regression tests and build provenance checks are still required before declaring reconciliation complete. diff --git a/openspec/changes/fix-release-version-consistency/tasks.md b/openspec/changes/fix-release-version-consistency/tasks.md new file mode 100644 index 0000000000..bd3e768fad --- /dev/null +++ b/openspec/changes/fix-release-version-consistency/tasks.md @@ -0,0 +1,35 @@ +> Milestone clarification (25 September 2026): security remediation and Docker contributor packaging are separate from the small test release. Broader Ask/Dust investigation below remains tracked; it does not add feature development to the versioning patch. Apply feature regression gates to actual app-code differences from the chosen release baseline. The user reconfirmed all three public destinations; publication tasks below remain required. + +## 1. Release-source investigation + +- [x] 1.1 Confirm T-050 and inspect the original version-regression commit. +- [x] 1.2 Read the production Play package's tracks and uploaded version codes without publishing changes. +- [ ] 1.3 Verify App Store Connect uploaded-build baseline and reconcile canonical release source. + +- [x] 1.4 Recover the prior Ask Keeper / Dust platform-mismatch context and compare current remote branch tips without modifying any branch. +- [ ] 1.5 Establish the historical build provenance where evidence exists; label the suspected stale Dust iOS source as unconfirmed until artifact/build records support it. +- [ ] 1.6 Prepare one reviewed integration source containing Ask Keeper, Dust Protection and intended fixes; document actual conflicts and preserve unrelated stashes/worktrees. + +## 2. Release metadata and tooling + +- [x] 2.1 Add an explicit manifest and source-version check/apply commands. +- [x] 2.2 Add regression tests for backwards marketing versions, reused builds, metadata drift and missing store evidence. +- [x] 2.3 Add production preflight to Fastlane and remove independent production build-number increments. +- [x] 2.4 Apply final verified version/build numbers consistently across package.json, Android and all Keeper iOS targets. +- [x] 2.5 Reject stale/wrong packaged metadata and missing feature markers before upload; verify APK signing identity and remove the iOS stale-IPA fallback. Validate markers against fresh optimized bundles for both platforms. + +## 3. Native tests and artifacts + +- [x] 3.1 Run automated release-tool tests and OpenSpec validation. +- [x] 3.2 Build and test local development apps on Android and iOS; record startup/settings smoke results. +- [ ] 3.3 Build the production IPA, AAB and APK from reconciled source; verify metadata, signing identities, configuration and checksums. +- [ ] 3.4 Provide and record the user's upgrade and basic app regression test checklist. + +- [ ] 3.5 Verify Ask Keeper entry and Dust Protection/Report/Donate Dust on both platforms from the same candidate source; include dust coin-selection regression tests. UI-only checks do not prove backend AI responses or transaction signing. + +## 4. Publication + +- [ ] 4.1 Refresh store baselines and publish the verified iOS build through App Store review. +- [ ] 4.2 Publish the verified Android bundle to Play production. +- [ ] 4.3 Publish the verified APK through the established public release channel with source tag/checksums/signature as required. +- [ ] 4.4 Record actual submission/publication status separately for all three channels and archive only when all tasks are complete. diff --git a/openspec/config.yaml b/openspec/config.yaml index 9ddaec95ff..86bf7c7fed 100644 --- a/openspec/config.yaml +++ b/openspec/config.yaml @@ -93,7 +93,7 @@ rules: - Confirm the agent implementation checklist in DESIGN.md before finalizing UI design - For UI/user-journey changes, specify affected Maestro flow files under flows/ and whether selectors/assertions need updates tasks: - - Group tasks by layer: UI Components, Business Logic / Hooks, Store (Slice + Saga), Storage, Tests, E2E Flows (Maestro) + - "Group tasks by layer: UI Components, Business Logic / Hooks, Store (Slice + Saga), Storage, Tests, E2E Flows (Maestro)" - Keep each task completable in a single focused session - Add a task for updating the Redux Persist migration version if store shape changes - Add a task for unit/integration tests covering the new behaviour diff --git a/package.json b/package.json index 307289bc12..28f4341087 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,17 @@ { "name": "hexa_keeper", - "version": "2.5.13", + "version": "2.5.15", "private": true, "scripts": { "ios": "npx react-native run-ios --scheme=hexa_keeper_dev --simulator 'iPhone 16' ", "iosProd": "npx react-native run-ios --scheme=hexa_keeper --simulator 'iPhone 16' ", "start": "react-native start", "test": "jest", + "test:release": "node --test scripts/release-version.test.cjs && python3 -B -m unittest discover -s scripts -p 'test_release_artifact.py'", + "version:inspect": "node scripts/release-version.cjs inspect", + "version:check": "node scripts/release-version.cjs check", + "version:apply": "node scripts/release-version.cjs apply", + "release:preflight": "node scripts/release-version.cjs release-check", "lint": "eslint . --ext .js,.jsx,.ts,.tsx", "lint:fix": "npm run lint -- --fix", "prepare": "husky install && chmod +x setup.sh && sh ./setup.sh", diff --git a/release/README.md b/release/README.md new file mode 100644 index 0000000000..215fdfb721 --- /dev/null +++ b/release/README.md @@ -0,0 +1,40 @@ +# Release version verification + +**Current milestone:** a small test release of the version-consistency change. Per the user’s 25 September direction, security remediation is separate and private-advisory sign-in is not a prerequisite. No security fixes are claimed. Docker contributor packaging is separate from the release patch. The user reconfirmed all three public destinations: App Store, Play production and the normal public APK channel. + +T-050 addresses the Android regression from marketing version 2.5.14 to 2.3.15 despite an increased build code. Marketing versions and platform build counters must both move forward. + +`version.json` selects candidate 2.5.15, Android 622 and iOS 615 using authenticated store observations from September 25, 2026. The iOS counter also exceeds local source build 614. This is not publication approval or proof of tested binaries. Do not substitute the public App Store listing for uploaded-build history. + +1. Read all uploaded builds and release tracks for Android package `io.hexawallet.bitcoinkeeper`, and uploaded iOS builds for `io.hexawallet.keeper`. Preserve the timestamp and evidence outside the repository. Refresh before upload; preflight accepts evidence at most 24 hours old. +2. Update the manifest with the highest known marketing version and highest uploaded build per platform. Select one greater marketing version for both stores and greater platform build counters. Check unpublished App Store version records as well. +3. Run `npm run version:apply` to synchronize package.json, Android versionName/versionCode and both Keeper iOS targets. Both Info.plist files must reference `$(CURRENT_PROJECT_VERSION)` and `$(MARKETING_VERSION)`; hardcoded values are rejected. Review the diff, then run `npm run version:check` and `npm run test:release`. +4. Reconcile release source with the published versions, prepare a clean release checkout and set `KEEPER_RELEASE_COMMIT` to its complete verified commit SHA. Run `npm run release:preflight`. The manifest cannot contain its own commit SHA. +5. Build from a reviewed production configuration and existing signing identities. Check extracted IPA, APK and AAB metadata against the manifest, verify signing fingerprints and record checksums. A source preflight does not verify the resulting binaries. +6. Test the signed builds, including upgrades, before public submission. + +Both existing Fastlane `live` lanes run preflight before building and consume `.env.production` without downloading over `.env`. They no longer increment production build numbers. Android notifications require explicit `KEEPER_NOTIFY_SLACK=1`. + +These existing lanes still upload to **TestFlight** and **Play internal testing** respectively. Neither constitutes the requested public release. Public submission must record App Store review/submission state, Play production state and GitHub APK publication separately. + +## Packaged artifact checks + +Run `python3 scripts/verify-release-artifact.py /absolute/path/to/build.ipa` (or `.apk` / `.aab`). The production lanes run this before uploading. Android builds both artifacts and verifies them before its first upload. The iOS lane requires the current build's IPA path and never falls back to an older file in the builds directory. + +The verifier checks the production application ID, marketing version, native counter, and bundled Ask Keeper / Dust route and data markers, then emits artifact and bundle SHA-256 hashes. Marker presence supplements device testing; it does not prove behavior or source provenance. Compiler-generated component names are unsuitable markers because release optimization removes them. + +For Android, set `AAPT` and `APKSIGNER` to SDK build-tool paths (or put them on PATH), `BUNDLETOOL_JAR` to an installed bundletool jar, and `KEEPER_ANDROID_CERT_SHA256` to the certificate fingerprint independently verified from the established public APK. The verifier checks the APK signature and certificate. IPA code signing/provisioning and AAB signing still require separate verification before distribution. The seven packaged-artifact regression tests run with `npm run test:release` alongside the source-version tests; Python 3 is required. + +The normal public APK channel is the repository's GitHub Releases. Publish the APK together with `SHA256SUM.asc` and `KEEPER_DETACHED_SIGN.sign` using the established PGP identity, as documented in the root README. Do not replace release signing keys to work around missing access. + +Android production signing reads `STORE_PASSWORD`, `KEY_PASSWORD`, and `KEY_ALIAS` from the build environment. `KEEPER_ANDROID_KEYSTORE` may select an absolute existing keystore path; otherwise the existing ignored `android/app/release.keystore` is used. Repository Gradle properties no longer contain release credentials. The development debug build continues to use its development keystore. + +## Manual release checks + +- Upgrade an existing installation with a designated test wallet on a physical iPhone and Android device; do not uninstall first. Confirm unlock, wallet visibility and settings survive. +- Check the displayed marketing version and native build counter against the release manifest on both platforms. +- Open Ask Keeper on both platforms and send a non-sensitive sample question to the intended backend. Confirm the response and error/retry behavior; local isolated-backend entry checks do not test AI responses. +- Open Wallet Settings > Dust Report. Check an empty test wallet, then a designated testnet fixture with Do Not Spend coins. Confirm the report, ordinary-spend exclusion, and Donate Dust confirmation/amount/fee behavior. Record separately whether a testnet signing transaction completed; an empty-wallet result cannot verify donation. +- Exercise wallet navigation, receive and a test transaction/signing flow with designated test funds and hardware where applicable. Verify that production build configuration is correct; do not send real funds merely to test this versioning change. +- Install the public APK as an upgrade over the previous public APK and verify its signature/checksum. The APK and AAB must carry the same Android version and code. +- Record each channel's actual submission/publication state and public download links. Do not report a TestFlight/internal upload as a public release. diff --git a/release/version.json b/release/version.json new file mode 100644 index 0000000000..626a58552f --- /dev/null +++ b/release/version.json @@ -0,0 +1,21 @@ +{ + "version": "2.5.15", + "androidVersionCode": 622, + "iosBuildNumber": 615, + "baselines": { + "android": { + "applicationId": "io.hexawallet.bitcoinkeeper", + "verifiedAt": "2026-09-25T13:10:54Z", + "highestUploadedBuild": 621, + "highestMarketingVersion": "2.5.14", + "productionRelease": "621 (2.3.15)" + }, + "ios": { + "applicationId": "io.hexawallet.keeper", + "verifiedAt": "2026-09-25T13:44:11Z", + "highestUploadedBuild": 612, + "highestMarketingVersion": "2.5.14", + "productionRelease": "612 (2.5.14)" + } + } +} diff --git a/scripts/release-version.cjs b/scripts/release-version.cjs new file mode 100644 index 0000000000..c1ba39d0b6 --- /dev/null +++ b/scripts/release-version.cjs @@ -0,0 +1,168 @@ +#!/usr/bin/env node +'use strict'; + +const fs = require('node:fs'); +const path = require('node:path'); +const { execFileSync } = require('node:child_process'); + +const appIds = { android: 'io.hexawallet.bitcoinkeeper', ios: 'io.hexawallet.keeper' }; +const iosIds = new Set([appIds.ios, 'io.hexawallet.hexakeeper.dev']); + +function versionParts(version) { + if (typeof version !== 'string' || !/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$/.test(version)) { + throw new Error(`Invalid marketing version: ${String(version)}`); + } + const parts = version.split('.').map(Number); + if (!parts.every(Number.isSafeInteger)) throw new Error('Marketing version exceeds safe integer range'); + return parts; +} + +function compareVersions(a, b) { + const left = versionParts(a); + const right = versionParts(b); + for (let i = 0; i < 3; i += 1) { + if (left[i] !== right[i]) return left[i] > right[i] ? 1 : -1; + } + return 0; +} + +function validateIntent(manifest, { requireEvidence = true, now = Date.now() } = {}) { + const errors = []; + try { versionParts(manifest.version); } catch (e) { errors.push(e.message); } + for (const [platform, field] of [['android', 'androidVersionCode'], ['ios', 'iosBuildNumber']]) { + const value = manifest[field]; + if (!Number.isSafeInteger(value) || value <= 0 || value > 2100000000) { + errors.push(`${field} must be an explicit positive integer`); + } + const baseline = manifest.baselines?.[platform]; + if (!baseline || baseline.applicationId !== appIds[platform]) { + errors.push(`${platform}: missing baseline or wrong production application ID`); + continue; + } + try { + if (compareVersions(manifest.version, baseline.highestMarketingVersion) <= 0) { + errors.push(`${platform}: marketing version must exceed ${baseline.highestMarketingVersion}`); + } + } catch (e) { errors.push(`${platform}: ${e.message}`); } + if (!Number.isSafeInteger(baseline.highestUploadedBuild) || baseline.highestUploadedBuild < 0) { + errors.push(`${platform}: highest uploaded build has not been verified`); + } else if (Number.isSafeInteger(value) && value <= baseline.highestUploadedBuild) { + errors.push(`${platform}: build ${value} must exceed uploaded build ${baseline.highestUploadedBuild}`); + } + if (requireEvidence) { + const verified = Date.parse(baseline.verifiedAt); + if (!Number.isFinite(verified) || verified > now || now - verified > 24 * 60 * 60 * 1000) { + errors.push(`${platform}: refresh authenticated store baseline (must be within 24 hours)`); + } + } + } + return errors; +} + +function iosConfigurations(text) { + const blocks = [...text.matchAll(/^\t\t[A-F0-9]{24} \/\*.*?\*\/ = \{\n([\s\S]*?)^\t\t\};/gm)]; + return blocks.filter((m) => m[1].includes('isa = XCBuildConfiguration;')).map((m) => { + const value = (name) => m[1].match(new RegExp(`\\b${name} = ([^;]+);`))?.[1]?.replace(/^"|"$/g, ''); + return { block: m[0], id: value('PRODUCT_BUNDLE_IDENTIFIER'), version: value('MARKETING_VERSION'), build: value('CURRENT_PROJECT_VERSION') }; + }).filter((config) => iosIds.has(config.id)); +} + +function readSources(root) { + return { + packageText: fs.readFileSync(path.join(root, 'package.json'), 'utf8'), + android: fs.readFileSync(path.join(root, 'android/app/build.gradle'), 'utf8'), + ios: fs.readFileSync(path.join(root, 'ios/hexa_keeper.xcodeproj/project.pbxproj'), 'utf8'), + iosPlists: ['ios/hexa_keeper/Info.plist', 'ios/hexa_keeper_dev-Info.plist'].map((file) => fs.readFileSync(path.join(root, file), 'utf8')), + }; +} + +function validateSources(manifest, sources) { + const errors = []; + if (JSON.parse(sources.packageText).version !== manifest.version) errors.push('package.json version differs from manifest'); + const names = [...sources.android.matchAll(/^\s*versionName\s+"([^"]+)"/gm)]; + const codes = [...sources.android.matchAll(/^\s*versionCode\s+(\d+)/gm)]; + if (names.length !== 1 || names[0][1] !== manifest.version) errors.push('Android versionName missing, ambiguous or different from manifest'); + if (codes.length !== 1 || Number(codes[0][1]) !== manifest.androidVersionCode) errors.push('Android versionCode missing, ambiguous or different from manifest'); + if (!Array.isArray(sources.iosPlists) || sources.iosPlists.length !== 2) { + errors.push('Expected both Keeper Info.plist files'); + } else { + for (const plist of sources.iosPlists) { + for (const [key, expected] of [['CFBundleVersion', '$(CURRENT_PROJECT_VERSION)'], ['CFBundleShortVersionString', '$(MARKETING_VERSION)']]) { + const matches = [...plist.matchAll(new RegExp(`${key}\\s*([^<]*)`, 'g'))]; + if (matches.length !== 1 || matches[0][1] !== expected) errors.push(`iOS Info.plist ${key} must reference ${expected}`); + } + } + } + const configs = iosConfigurations(sources.ios); + for (const id of iosIds) { + if (configs.filter((c) => c.id === id).length !== 2) errors.push(`Expected Debug and Release configurations for ${id}`); + } + for (const c of configs) { + if (c.version !== manifest.version || c.build !== String(manifest.iosBuildNumber)) errors.push(`iOS ${c.id} version/build differs from manifest`); + } + return errors; +} + +function applyVersions(manifest, sources) { + // Validate structure before writing anything. Existing version differences are + // expected here; missing/ambiguous fields must never be silently ignored. + const configs = iosConfigurations(sources.ios); + if ([...sources.android.matchAll(/^\s*versionName\s+"([^"]+)"/gm)].length !== 1 || + [...sources.android.matchAll(/^\s*versionCode\s+(\d+)/gm)].length !== 1 || + [...iosIds].some((id) => configs.filter((c) => c.id === id).length !== 2)) throw new Error('Unexpected native version structure; no files written'); + const pkg = JSON.parse(sources.packageText); + pkg.version = manifest.version; + const android = sources.android.replace(/(^\s*versionName\s+)"[^"]+"/m, (_match, prefix) => `${prefix}"${manifest.version}"`) + .replace(/(^\s*versionCode\s+)\d+/m, (_match, prefix) => `${prefix}${manifest.androidVersionCode}`); + let ios = sources.ios; + for (const c of iosConfigurations(ios)) { + if (!c.version || !c.build) throw new Error('Missing iOS version fields; no files written'); + const block = c.block.replace(/\bMARKETING_VERSION = [^;]+;/, `MARKETING_VERSION = ${manifest.version};`) + .replace(/\bCURRENT_PROJECT_VERSION = [^;]+;/, `CURRENT_PROJECT_VERSION = ${manifest.iosBuildNumber};`); + ios = ios.replace(c.block, block); + } + const result = { packageText: JSON.stringify(pkg, null, 2) + '\n', android, ios, iosPlists: sources.iosPlists }; + const errors = validateSources(manifest, result); + if (errors.length) throw new Error(errors.join('\n')); + return result; +} + +function main() { + const command = process.argv[2] || 'check'; + if (!['check', 'release-check', 'apply', 'inspect'].includes(command)) throw new Error('Usage: release-version.cjs check|release-check|apply|inspect'); + const root = path.resolve(__dirname, '..'); + const manifest = JSON.parse(fs.readFileSync(path.join(root, 'release/version.json'), 'utf8')); + const sources = readSources(root); + if (command === 'inspect') { + console.log(JSON.stringify({ manifest, packageVersion: JSON.parse(sources.packageText).version, + androidVersion: sources.android.match(/versionName\s+"([^"]+)"/)?.[1], + androidBuild: sources.android.match(/versionCode\s+(\d+)/)?.[1], + ios: iosConfigurations(sources.ios).map(({ id, version, build }) => ({ id, version, build })) }, null, 2)); + return; + } + const errors = validateIntent(manifest); + if (errors.length) throw new Error(errors.join('\n')); + if (command === 'apply') { + const updated = applyVersions(manifest, sources); + fs.writeFileSync(path.join(root, 'package.json'), updated.packageText); + fs.writeFileSync(path.join(root, 'android/app/build.gradle'), updated.android); + fs.writeFileSync(path.join(root, 'ios/hexa_keeper.xcodeproj/project.pbxproj'), updated.ios); + console.log(`Applied ${manifest.version}; Android ${manifest.androidVersionCode}; iOS ${manifest.iosBuildNumber}`); + return; + } + errors.push(...validateSources(manifest, sources)); + const head = execFileSync('git', ['rev-parse', 'HEAD'], { cwd: root, encoding: 'utf8' }).trim(); + if (command === 'release-check') { + const expectedCommit = process.env.KEEPER_RELEASE_COMMIT; + if (!/^[a-f0-9]{40}$/.test(expectedCommit || '') || expectedCommit !== head) errors.push('KEEPER_RELEASE_COMMIT must identify the verified canonical HEAD'); + const dirty = execFileSync('git', ['status', '--porcelain', '--untracked-files=normal'], { cwd: root, encoding: 'utf8' }).trim(); + if (dirty) errors.push('Release checkout must be clean; preserve existing work and prepare an isolated release checkout'); + } + if (errors.length) throw new Error(errors.join('\n')); + console.log(`Release versions verified: ${manifest.version}; Android ${manifest.androidVersionCode}; iOS ${manifest.iosBuildNumber}; source ${head}`); +} + +module.exports = { versionParts, compareVersions, validateIntent, validateSources, applyVersions, iosConfigurations }; +if (require.main === module) { + try { main(); } catch (e) { console.error(`Release preflight failed:\n${e.message}`); process.exitCode = 1; } +} diff --git a/scripts/release-version.test.cjs b/scripts/release-version.test.cjs new file mode 100644 index 0000000000..ca3387c611 --- /dev/null +++ b/scripts/release-version.test.cjs @@ -0,0 +1,80 @@ +const { test } = require('node:test'); +const assert = require('node:assert/strict'); +const { compareVersions, validateIntent, validateSources, applyVersions } = require('./release-version.cjs'); + +const now = Date.parse('2026-09-25T14:00:00Z'); +const intent = () => ({ + version: '2.5.15', androidVersionCode: 622, iosBuildNumber: 622, + baselines: { + android: { applicationId: 'io.hexawallet.bitcoinkeeper', verifiedAt: '2026-09-25T13:00:00Z', highestUploadedBuild: 621, highestMarketingVersion: '2.5.14' }, + ios: { applicationId: 'io.hexawallet.keeper', verifiedAt: '2026-09-25T13:00:00Z', highestUploadedBuild: 615, highestMarketingVersion: '2.5.14' }, + }, +}); +const fixtures = () => ({ + packageText: '{"name":"keeper","version":"2.5.15"}', + iosPlists: Array(2).fill('CFBundleVersion$(CURRENT_PROJECT_VERSION)CFBundleShortVersionString$(MARKETING_VERSION)'), + android: 'android {\n versionName "2.5.15"\n versionCode 622\n}\n', + ios: ['io.hexawallet.keeper', 'io.hexawallet.keeper', 'io.hexawallet.hexakeeper.dev', 'io.hexawallet.hexakeeper.dev'].map((id, i) => + `\t\t${String(i).padStart(24, 'A')} /* Build config */ = {\n\t\t\tisa = XCBuildConfiguration;\n\t\t\tbuildSettings = {\n\t\t\t\tPRODUCT_BUNDLE_IDENTIFIER = ${id};\n\t\t\t\tMARKETING_VERSION = 2.5.15;\n\t\t\t\tCURRENT_PROJECT_VERSION = 622;\n\t\t\t};\n\t\t};\n`).join(''), +}); + +test('consistent versions with authenticated fresh baselines pass', () => { + assert.deepEqual(validateIntent(intent(), { now }), []); + assert.deepEqual(validateSources(intent(), fixtures()), []); +}); +test('rejects the actual 2.5.14 -> 2.3.15 regression even with higher build code', () => { + const m = intent(); m.version = '2.3.15'; + assert.ok(validateIntent(m, { now }).some((e) => e.includes('marketing version must exceed 2.5.14'))); +}); +test('numeric version comparison handles multi-digit segments', () => { + assert.equal(compareVersions('2.10.0', '2.9.99'), 1); + assert.equal(compareVersions('2.5.15', '2.5.15'), 0); + assert.throws(() => compareVersions('2.05.15', '2.5.14')); +}); +test('rejects a reused Android build', () => { + const m = intent(); m.androidVersionCode = 621; + assert.ok(validateIntent(m, { now }).some((e) => e.includes('must exceed uploaded build 621'))); +}); +test('does not turn missing iOS evidence or a public listing into a verified baseline', () => { + const m = intent(); m.iosBuildNumber = null; m.baselines.ios.verifiedAt = null; m.baselines.ios.highestUploadedBuild = null; + assert.ok(validateIntent(m, { now }).some((e) => e.includes('highest uploaded build has not been verified'))); + assert.ok(validateIntent(m, { now }).some((e) => e.includes('refresh authenticated store baseline'))); +}); +test('rejects stale and future-dated evidence', () => { + for (const date of ['2026-09-20T00:00:00Z', '2026-09-26T00:00:00Z']) { + const m = intent(); m.baselines.android.verifiedAt = date; + assert.ok(validateIntent(m, { now }).some((e) => e.includes('refresh authenticated store baseline'))); + } +}); +test('rejects the similarly named non-production Play package', () => { + const m = intent(); m.baselines.android.applicationId = 'io.hexawallet.keeper'; + assert.ok(validateIntent(m, { now }).some((e) => e.includes('wrong production application ID'))); +}); +test('detects package drift and a single stale iOS configuration', () => { + const s = fixtures(); s.packageText = '{"version":"2.5.13"}'; s.ios = s.ios.replace('CURRENT_PROJECT_VERSION = 622;', 'CURRENT_PROJECT_VERSION = 614;'); + const errors = validateSources(intent(), s); + assert.ok(errors.some((e) => e.includes('package.json'))); + assert.ok(errors.some((e) => e.includes('iOS'))); +}); +test('apply synchronizes all version fields while preserving unrelated text', () => { + const s = fixtures(); s.android += '// Preserve existing signing configuration\n'; + const m = intent(); m.version = '2.5.16'; m.androidVersionCode = 623; m.iosBuildNumber = 624; + const updated = applyVersions(m, s); + assert.deepEqual(validateSources(m, updated), []); + assert.ok(updated.android.includes('// Preserve existing signing configuration')); + assert.equal(JSON.parse(updated.packageText).name, 'keeper'); + assert.ok(s.android.includes('versionCode 622')); +}); +test('apply refuses ambiguous Android values and missing iOS targets', () => { + const s = fixtures(); s.android += '\nversionCode 999\n'; + assert.throws(() => applyVersions(intent(), s), /Unexpected native version structure/); + const incomplete = fixtures(); incomplete.ios = ''; + assert.throws(() => applyVersions(intent(), incomplete), /Unexpected native version structure/); +}); + +test('rejects hardcoded plist build numbers even when Xcode project versions agree', () => { + const s = fixtures(); + s.iosPlists[0] = s.iosPlists[0].replace('$(CURRENT_PROJECT_VERSION)', '614'); + assert.ok(validateSources(intent(), s).some((e) => e.includes('CFBundleVersion'))); + assert.throws(() => applyVersions(intent(), s), /CFBundleVersion/); +}); diff --git a/scripts/test_release_artifact.py b/scripts/test_release_artifact.py new file mode 100644 index 0000000000..d105e52dff --- /dev/null +++ b/scripts/test_release_artifact.py @@ -0,0 +1,86 @@ +"""Packaged-build regression tests; no store access or signing keys required.""" +import hashlib +import importlib.util +import os +from pathlib import Path +import plistlib +import tempfile +import unittest +from unittest.mock import patch +import zipfile + +spec = importlib.util.spec_from_file_location("artifact", Path(__file__).with_name("verify-release-artifact.py")) +artifact = importlib.util.module_from_spec(spec) +spec.loader.exec_module(artifact) +MANIFEST = {"version": "2.5.15", "iosBuildNumber": 615, "androidVersionCode": 622} +FEATURES = b"HelpAiEntry\0DustReport\0doNotSpend" + + +class PackagedBuildTests(unittest.TestCase): + def setUp(self): + self.directory = tempfile.TemporaryDirectory() + self.addCleanup(self.directory.cleanup) + + def ipa(self, *, app_id="io.hexawallet.keeper", version="2.5.15", build="615", bundle=FEATURES): + path = Path(self.directory.name) / "Keeper.ipa" + with zipfile.ZipFile(path, "w") as archive: + archive.writestr("Payload/Keeper.app/Info.plist", plistlib.dumps({ + "CFBundleIdentifier": app_id, "CFBundleShortVersionString": version, + "CFBundleVersion": build, + }, fmt=plistlib.FMT_BINARY)) + archive.writestr("Payload/Keeper.app/main.jsbundle", bundle) + return path + + def test_candidate_metadata_and_hashes(self): + path = self.ipa() + result = artifact.inspect(path, MANIFEST) + self.assertEqual(result["sha256"], hashlib.sha256(path.read_bytes()).hexdigest()) + self.assertEqual(result["bundleSha256"], hashlib.sha256(FEATURES).hexdigest()) + self.assertEqual(result["signing"], "separate verification required") + + def test_stale_binary_rejected_even_if_source_manifest_is_correct(self): + for kwargs, message in [({"version": "2.5.14"}, "Marketing version"), + ({"build": "614"}, "Build counter"), + ({"app_id": "io.hexawallet.hexakeeper.dev"}, "Application identifier")]: + with self.subTest(kwargs=kwargs), self.assertRaisesRegex(ValueError, message): + artifact.inspect(self.ipa(**kwargs), MANIFEST) + + def test_missing_feature_rejected_despite_correct_versions(self): + with self.assertRaisesRegex(ValueError, "DustReport"): + artifact.inspect(self.ipa(bundle=b"HelpAiEntry\0doNotSpend"), MANIFEST) + + def test_utf16_feature_table(self): + artifact.verify_features(FEATURES.decode().encode("utf-16-le")) + + def test_missing_bundle_is_not_a_pass(self): + path = self.ipa() + with zipfile.ZipFile(path, "w") as archive: + archive.writestr("Payload/Keeper.app/Info.plist", plistlib.dumps({})) + with self.assertRaises(KeyError): + artifact.inspect(path, MANIFEST) + + def test_apk_must_match_independent_signing_identity(self): + path = Path(self.directory.name) / "Keeper.apk" + with zipfile.ZipFile(path, "w") as archive: + archive.writestr("assets/index.android.bundle", FEATURES) + metadata = "package: name='io.hexawallet.bitcoinkeeper' versionCode='622' versionName='2.5.15'" + for actual, should_pass in [("a" * 64, True), ("b" * 64, False)]: + with self.subTest(actual=actual), patch.dict(os.environ, {"KEEPER_ANDROID_CERT_SHA256": "a" * 64}), \ + patch.object(artifact, "output", side_effect=[metadata, "Signer #1 certificate SHA-256 digest: " + actual]): + if should_pass: + self.assertEqual(artifact.inspect(path, MANIFEST)["signing"], "APK certificate verified") + else: + with self.assertRaisesRegex(ValueError, "signing certificate"): + artifact.inspect(path, MANIFEST) + + def test_aab_reads_base_manifest_and_bundle(self): + path = Path(self.directory.name) / "Keeper.aab" + with zipfile.ZipFile(path, "w") as archive: + archive.writestr("base/assets/index.android.bundle", FEATURES) + xml = '' + with patch.dict(os.environ, {"BUNDLETOOL_JAR": str(path)}), patch.object(artifact, "output", return_value=xml): + self.assertEqual(artifact.inspect(path, MANIFEST)["build"], "622") + + +if __name__ == "__main__": + unittest.main() diff --git a/scripts/verify-release-artifact.py b/scripts/verify-release-artifact.py new file mode 100644 index 0000000000..0de412db51 --- /dev/null +++ b/scripts/verify-release-artifact.py @@ -0,0 +1,112 @@ +#!/usr/bin/env python3 +"""Inspect packaged release metadata and required feature markers without dumping secrets. + +APK: Android SDK aapt and apksigner are required. +AAB: BUNDLETOOL_JAR and Java are required. +IPA: Python standard library; signing is verified separately by codesign/export evidence. +This verifier supplements device tests; bytecode markers do not prove feature behavior. +""" +import argparse +import hashlib +import json +import os +from pathlib import Path +import plistlib +import re +import subprocess +import sys +import zipfile +import xml.etree.ElementTree as ET + +ANDROID_NS = "{http://schemas.android.com/apk/res/android}" + + +def require_equal(actual, expected, label): + if str(actual) != str(expected): + raise ValueError(f"{label}: expected {expected}, got {actual}") + + +def verify_features(bundle): + missing = [] + # Route names and data properties survive Metro/Hermes optimization; + # implementation function names (e.g. DustReportScreen) do not. + for feature in ("HelpAiEntry", "DustReport", "doNotSpend"): + if feature.encode() not in bundle and feature.encode("utf-16-le") not in bundle: + missing.append(feature) + if missing: + raise ValueError("Required packaged feature markers absent: " + ", ".join(missing)) + + +def output(command): + result = subprocess.run(command, capture_output=True, text=True) + if result.returncode: + raise ValueError(f"{Path(command[0]).name} failed; inspect tool/environment configuration") + return result.stdout + + +def inspect(path, manifest): + suffix = path.suffix.lower() + with zipfile.ZipFile(path) as archive: + if suffix == ".ipa": + names = [n for n in archive.namelist() if re.fullmatch(r"Payload/[^/]+\.app/Info\.plist", n)] + if len(names) != 1: + raise ValueError("Expected one main app Info.plist") + info = plistlib.loads(archive.read(names[0])) + app_id, version, build = (info.get(k) for k in ("CFBundleIdentifier", "CFBundleShortVersionString", "CFBundleVersion")) + platform = "ios" + bundle = archive.read(names[0].rsplit("/", 1)[0] + "/main.jsbundle") + elif suffix == ".apk": + text = output([os.environ.get("AAPT", "aapt"), "dump", "badging", str(path)]) + match = re.search(r"package: name='([^']+)' versionCode='([^']+)' versionName='([^']+)'", text) + if not match: + raise ValueError("Cannot read APK package/version metadata") + app_id, build, version = match.groups() + platform = "android" + bundle = archive.read("assets/index.android.bundle") + elif suffix == ".aab": + jar = os.environ.get("BUNDLETOOL_JAR") + if not jar or not Path(jar).is_file(): + raise ValueError("Set BUNDLETOOL_JAR to the installed bundletool jar") + xml = output(["java", "-jar", jar, "dump", "manifest", "--bundle=" + str(path), "--module=base"]) + root = ET.fromstring(xml) + app_id, build, version = root.get("package"), root.get(ANDROID_NS + "versionCode"), root.get(ANDROID_NS + "versionName") + platform = "android" + bundle = archive.read("base/assets/index.android.bundle") + else: + raise ValueError("Expected an IPA, APK or AAB") + expected_id = {"ios": "io.hexawallet.keeper", "android": "io.hexawallet.bitcoinkeeper"}[platform] + require_equal(app_id, expected_id, "Application identifier") + require_equal(version, manifest["version"], "Marketing version") + require_equal(build, manifest["iosBuildNumber" if platform == "ios" else "androidVersionCode"], "Build counter") + verify_features(bundle) + result = {"artifact": path.name, "applicationId": app_id, "version": version, "build": str(build), + "sha256": hashlib.sha256(path.read_bytes()).hexdigest(), + "bundleSha256": hashlib.sha256(bundle).hexdigest(), "featureMarkers": "present", + "signing": "separate verification required"} + if suffix == ".apk": + certs = output([os.environ.get("APKSIGNER", "apksigner"), "verify", "--print-certs", str(path)]) + expected = os.environ.get("KEEPER_ANDROID_CERT_SHA256", "").lower().replace(":", "") + if not re.fullmatch(r"[0-9a-f]{64}", expected): + raise ValueError("Set KEEPER_ANDROID_CERT_SHA256 to the independently verified release certificate") + actual = re.findall(r"certificate SHA-256 digest: ([0-9a-fA-F]+)", certs) + if len(actual) != 1 or actual[0].lower() != expected: + raise ValueError("APK signing certificate differs from verified release identity") + result["signing"] = "APK certificate verified" + return result + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("artifact", type=Path) + parser.add_argument("--manifest", type=Path, default=Path(__file__).resolve().parents[1] / "release/version.json") + args = parser.parse_args() + try: + print(json.dumps(inspect(args.artifact, json.loads(args.manifest.read_text())), indent=2)) + except (ValueError, OSError, KeyError, zipfile.BadZipFile, ET.ParseError) as exc: + print("Artifact verification failed: " + str(exc), file=sys.stderr) + return 1 + return 0 + + +if __name__ == "__main__": + sys.exit(main()) From 30d74df0260d2e86d2f22d0648da408f5a55fbfa Mon Sep 17 00:00:00 2001 From: Anant Date: Mon, 28 Sep 2026 18:23:24 +0530 Subject: [PATCH 002/254] fix: preserve Ask tab navigation for 2.5.16 candidate --- android/app/build.gradle | 4 +- flows/ask-history-navigation.yaml | 51 ++++++++++ flows/ask-tab-navigation.yaml | 46 +++++++++ ios/hexa_keeper.xcodeproj/project.pbxproj | 16 ++-- .../changes/fix-ask-tab-navigation/design.md | 38 ++++++++ .../fix-ask-tab-navigation/proposal.md | 28 ++++++ .../specs/home-navigation/spec.md | 36 +++++++ .../changes/fix-ask-tab-navigation/tasks.md | 25 +++++ .../fix-ask-tab-navigation/verification.md | 96 +++++++++++++++++++ package.json | 2 +- release/README.md | 6 +- release/version.json | 22 ++--- src/components/MenuFooter.tsx | 5 + src/screens/Home/HomeScreen.tsx | 7 +- 14 files changed, 354 insertions(+), 28 deletions(-) create mode 100644 flows/ask-history-navigation.yaml create mode 100644 flows/ask-tab-navigation.yaml create mode 100644 openspec/changes/fix-ask-tab-navigation/design.md create mode 100644 openspec/changes/fix-ask-tab-navigation/proposal.md create mode 100644 openspec/changes/fix-ask-tab-navigation/specs/home-navigation/spec.md create mode 100644 openspec/changes/fix-ask-tab-navigation/tasks.md create mode 100644 openspec/changes/fix-ask-tab-navigation/verification.md diff --git a/android/app/build.gradle b/android/app/build.gradle index a56f8d534d..90d90807c5 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -98,8 +98,8 @@ android { applicationId "io.hexawallet.keeper" minSdkVersion rootProject.ext.minSdkVersion targetSdkVersion rootProject.ext.targetSdkVersion - versionCode 622 - versionName "2.5.15" + versionCode 623 + versionName "2.5.16" missingDimensionStrategy 'react-native-camera', 'general' missingDimensionStrategy 'store', 'play' multiDexEnabled true diff --git a/flows/ask-history-navigation.yaml b/flows/ask-history-navigation.yaml new file mode 100644 index 0000000000..fd8731ee88 --- /dev/null +++ b/flows/ask-history-navigation.yaml @@ -0,0 +1,51 @@ +# Run on an unlocked disposable app with an existing Ask conversation. +appId: ${APP_ID} +--- +- repeat: + times: 3 + commands: + - tapOn: + id: home_tab_ask + - assertVisible: + id: chat_thread_0 + - tapOn: + id: chat_thread_0 + - tapOn: + id: btn_learn_more + - assertVisible: Before you use Ask Keeper + - tapOn: Got it + - tapOn: + id: btn_learn_more + - tapOn: Learn more about Ask Keeper.* + - assertVisible: About Ask Keeper + - tapOn: + id: btn_back + - tapOn: + id: input_message + - assertVisible: + id: btn_back + - tapOn: + id: btn_back + - assertVisible: + id: chat_thread_0 + - assertVisible: + id: home_tab_wallets + - assertVisible: + id: home_tab_keys + - assertVisible: + id: home_tab_ask + - assertVisible: + id: home_tab_more + - tapOn: + id: home_tab_more + - assertVisible: More Options + - assertNotVisible: + id: chat_thread_0 + - tapOn: + id: home_tab_wallets + - assertVisible: + id: wallet_item_.* +- tapOn: + id: home_tab_ask +- assertVisible: + id: chat_thread_0 diff --git a/flows/ask-tab-navigation.yaml b/flows/ask-tab-navigation.yaml new file mode 100644 index 0000000000..59700d5714 --- /dev/null +++ b/flows/ask-tab-navigation.yaml @@ -0,0 +1,46 @@ +# Run on an unlocked disposable app at Home with no Ask conversations. +# maestro test -e APP_ID=io.hexawallet.keeper flows/ask-tab-navigation.yaml +appId: ${APP_ID} +--- +- repeat: + times: 3 + commands: + - tapOn: + id: home_tab_ask + - assertVisible: "Something isn't working" + - tapOn: + id: home_tab_more + - assertVisible: More Options + - assertNotVisible: "Something isn't working" + - assertNotVisible: I have an idea for Keeper + - assertNotVisible: Never share seed words or private keys. + - assertVisible: + id: home_tab_wallets + - assertVisible: + id: home_tab_keys + - assertVisible: + id: home_tab_ask + - assertVisible: + id: home_tab_more + - tapOn: + id: home_tab_ask + - assertVisible: "Something isn't working" + - tapOn: + id: home_tab_wallets + - assertVisible: + id: wallet_item_.* + - assertNotVisible: "Something isn't working" + - tapOn: + id: home_tab_ask + - assertVisible: "Something isn't working" + - tapOn: + id: home_tab_keys + - assertNotVisible: "Something isn't working" + - assertVisible: + id: home_tab_wallets + - assertVisible: + id: home_tab_keys + - assertVisible: + id: home_tab_ask + - assertVisible: + id: home_tab_more diff --git a/ios/hexa_keeper.xcodeproj/project.pbxproj b/ios/hexa_keeper.xcodeproj/project.pbxproj index cc49992030..c77033da6c 100644 --- a/ios/hexa_keeper.xcodeproj/project.pbxproj +++ b/ios/hexa_keeper.xcodeproj/project.pbxproj @@ -748,7 +748,7 @@ CODE_SIGN_ENTITLEMENTS = hexa_keeper/hexa_keeper.entitlements; CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 615; + CURRENT_PROJECT_VERSION = 616; DEVELOPMENT_TEAM = Y5TCB759QL; ENABLE_BITCODE = NO; HEADER_SEARCH_PATHS = ( @@ -849,7 +849,7 @@ "$(inherited)", "\"$(SRCROOT)\"", ); - MARKETING_VERSION = 2.5.15; + MARKETING_VERSION = 2.5.16; OTHER_LDFLAGS = ( "$(inherited)", "-ObjC", @@ -876,7 +876,7 @@ CODE_SIGN_IDENTITY = "Apple Development"; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Distribution"; CODE_SIGN_STYLE = Manual; - CURRENT_PROJECT_VERSION = 615; + CURRENT_PROJECT_VERSION = 616; DEVELOPMENT_TEAM = Y5TCB759QL; "DEVELOPMENT_TEAM[sdk=iphoneos*]" = Y5TCB759QL; HEADER_SEARCH_PATHS = ( @@ -977,7 +977,7 @@ "$(inherited)", "\"$(SRCROOT)\"", ); - MARKETING_VERSION = 2.5.15; + MARKETING_VERSION = 2.5.16; OTHER_LDFLAGS = ( "$(inherited)", "-ObjC", @@ -1147,7 +1147,7 @@ CODE_SIGN_IDENTITY = "Apple Development"; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Distribution"; CODE_SIGN_STYLE = Manual; - CURRENT_PROJECT_VERSION = 615; + CURRENT_PROJECT_VERSION = 616; DEVELOPMENT_TEAM = Y5TCB759QL; "DEVELOPMENT_TEAM[sdk=iphoneos*]" = Y5TCB759QL; ENABLE_BITCODE = NO; @@ -1250,7 +1250,7 @@ "$(PROJECT_DIR)", "\"$(SRCROOT)\"", ); - MARKETING_VERSION = 2.5.15; + MARKETING_VERSION = 2.5.16; OTHER_LDFLAGS = ( "$(inherited)", "-ObjC", @@ -1278,7 +1278,7 @@ CODE_SIGN_ENTITLEMENTS = hexa_keeper_dev.entitlements; CODE_SIGN_IDENTITY = "Apple Distribution"; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Distribution"; - CURRENT_PROJECT_VERSION = 615; + CURRENT_PROJECT_VERSION = 616; DEVELOPMENT_TEAM = Y5TCB759QL; "DEVELOPMENT_TEAM[sdk=iphoneos*]" = Y5TCB759QL; HEADER_SEARCH_PATHS = ( @@ -1380,7 +1380,7 @@ "$(PROJECT_DIR)", "\"$(SRCROOT)\"", ); - MARKETING_VERSION = 2.5.15; + MARKETING_VERSION = 2.5.16; OTHER_LDFLAGS = ( "$(inherited)", "-ObjC", diff --git a/openspec/changes/fix-ask-tab-navigation/design.md b/openspec/changes/fix-ask-tab-navigation/design.md new file mode 100644 index 0000000000..d2f781b63f --- /dev/null +++ b/openspec/changes/fix-ask-tab-navigation/design.md @@ -0,0 +1,38 @@ +## Existing behavior and investigation + +HomeScreen conditionally renders one content subtree inside a layout-only Box, +with MenuFooter as its sibling. Ask's empty and history components use native +Views and an absolutely positioned Fab with zIndex. Fabric can flatten these +layout-only ancestors. The repository also carries prior iOS mounting patches. +Native logs did not +identify the exact failing mount transaction; do not infer a proven upstream +renderer cause from this app-level reproduction alone. + +Verified correction: retain a native, non-collapsing content boundary for the +selected tab, +with explicit tab identity, so outgoing native descendants are removed together +and cannot share the footer's native parent. The same flow failed before and +passed after this change, with the same +disposable wallet state. See verification.md for device results and build limits. + +## Design and data constraints + +Read DESIGN.md: preserve its App shell, Bottom navigation, Spacing and +Accessibility rules. Reuse HomeScreenHeader, MenuFooter, existing hp/wp spacing, +colors and typography. No new visible components, assets, strings or screens. +No Redux slice/saga edits, Realm schema/MMKV keys, or persist version migration. +No PSBT, transaction, recovery or hardware interaction. + +## Affected files and verification + +- src/screens/Home/HomeScreen.tsx: verified native tab boundary. +- src/components/MenuFooter.tsx: stable selectors for the existing four tabs. +- flows/ask-tab-navigation.yaml: repeated empty-state switching. +- flows/ask-history-navigation.yaml: existing-chat/back/tab regression. +- This OpenSpec package: results and remaining device limits. + +Native screenshots and assertions must check both absence of outgoing Ask text +and presence/operability of tabs. A React-only pass is insufficient. Distinguish +the user's physical iPhone 15 Pro Max evidence from simulator and emulator checks. +Test a smaller iPhone and larger text where available. Store only disposable QA +data in evidence; never commit user screenshots or wallet secrets without need. diff --git a/openspec/changes/fix-ask-tab-navigation/proposal.md b/openspec/changes/fix-ask-tab-navigation/proposal.md new file mode 100644 index 0000000000..4a9a5e1c09 --- /dev/null +++ b/openspec/changes/fix-ask-tab-navigation/proposal.md @@ -0,0 +1,28 @@ +## Why + +T-066: the live iOS 2.5.15 app can retain Ask Keeper prompt text over another +Home tab and lose its bottom navigation. The user confirmed Ask → More on an +iPhone 15 Pro Max, with a screenshot; restarting is currently required. An +earlier Release simulator check also lost navigation after returning from chat +to Ask history. These are related symptoms; shared root cause must be verified. + +## What Changes + +- Reproduce empty-state tab switching and the chat/history return separately. +- Correct native view ownership at the Home tab-content boundary, with the + smallest verified change; retain the existing MenuFooter and navigation routes. +- Add repeatable native regression coverage for stale Ask text and reachable tabs. + +## Non-goals + +No new UX or copy, dependency-wide/native-renderer workaround, chat deletion, +Recovery Key fix, wallet changes, platform feature merge, or release submission. +Any subsequent release must use the existing version/artifact checks and a new +approved version/build; never replace the published 2.5.15 tag or artifacts. + +## Impact + +Both mainnet and testnet; all tiers. No Wallet, Vault, Signer or UTXO data changes, +key-material access, storage migrations, new network calls or hardware signer +compatibility changes. Native Maestro coverage is required because mocked React +tests do not validate Fabric mounting or on-screen tab placement. diff --git a/openspec/changes/fix-ask-tab-navigation/specs/home-navigation/spec.md b/openspec/changes/fix-ask-tab-navigation/specs/home-navigation/spec.md new file mode 100644 index 0000000000..063deff016 --- /dev/null +++ b/openspec/changes/fix-ask-tab-navigation/specs/home-navigation/spec.md @@ -0,0 +1,36 @@ +## ADDED Requirements + +### Requirement: Home tab content has an isolated lifecycle +The application SHALL remove the outgoing tab's native content when selecting +another Home tab and SHALL preserve visible, operable bottom navigation. + +#### Scenario: Leave an empty Ask screen +- **GIVEN** Ask Keeper shows its existing suggested prompts and safety warning +- **WHEN** the user selects More, Wallets, or Keys +- **THEN** only the destination's content and header are shown +- **AND** Ask-only prompts and warning text are absent +- **AND** all four existing bottom tabs remain visible and tappable without restart. + +#### Scenario: Repeated tab changes +- **GIVEN** an unlocked app on either supported platform +- **WHEN** the user repeatedly changes Ask → More → Ask → Wallets → Ask → Keys +- **THEN** each selected tab renders correctly and the footer remains available. + +#### Scenario: Return from chat to history +- **GIVEN** an existing disposable Ask conversation +- **WHEN** the user opens it, returns to Ask history, and selects another main tab +- **THEN** history remains saved and only the selected tab is shown +- **AND** navigation remains usable, including subsequent re-entry to Ask. + +#### Scenario: Recovery reminder and larger text +- **GIVEN** the existing Recovery Key reminder is visible or system text is enlarged +- **WHEN** the user switches between Ask and other main tabs +- **THEN** the reminder retains its existing behavior and bottom tabs remain reachable +- **AND** no security-critical copy is truncated to make the layout fit. + +#### Scenario: Return through chat information with keyboard focus +- **GIVEN** an existing disposable Ask conversation +- **WHEN** the user opens and dismisses its disclaimer, opens About Ask Keeper, + returns to chat, focuses the input, then goes back to history and another tab +- **THEN** the modal and keyboard do not cover the destination +- **AND** the saved conversation remains accessible and all Home tabs work. diff --git a/openspec/changes/fix-ask-tab-navigation/tasks.md b/openspec/changes/fix-ask-tab-navigation/tasks.md new file mode 100644 index 0000000000..9469b1333e --- /dev/null +++ b/openspec/changes/fix-ask-tab-navigation/tasks.md @@ -0,0 +1,25 @@ +## 1. Investigation +- [x] Inspect user screenshot and existing T-066 evidence; inspect Home/Ask/footer code. +- [x] Reproduce the empty Ask tab-switch failure in the unmodified release simulator. +- [x] Identify and verify the smallest native-lifecycle correction. + +## 2. UI components +- [x] Implement the verified boundary correction and stable footer selectors. +- [x] Preserve copy, styling, routes and wallet/chat data. + +## 3. Business logic, store and storage +- [x] No changes required; no migrations or signing/recovery changes. + +## 4. Tests and Maestro +- [x] Run the same regression before/after on iOS, retaining failure evidence. +- [x] Verify repeated tab changes and chat-history return on iOS. +- [x] Check Android and smaller-screen/larger-text behavior; record exact limits. +- [x] Run relevant automated checks and strict OpenSpec validation. +- [x] Record the physical-device follow-up under Office, update T-066 and prepare the local review package. + +## 5. T-053 maintenance candidate — authorised 28 September 2026 +- [x] Review the prepared local fix for the selected maintenance batch. +- [ ] Allocate new verified version/build numbers; build and test signed artifacts. +- [ ] Optional physical iPhone follow-up; record simulator coverage separately. + +Fresh release candidates are requested. Preserve published 2.5.15. See verification.md for earlier evidence and test limits; record fresh-build results separately. diff --git a/openspec/changes/fix-ask-tab-navigation/verification.md b/openspec/changes/fix-ask-tab-navigation/verification.md new file mode 100644 index 0000000000..d623061651 --- /dev/null +++ b/openspec/changes/fix-ask-tab-navigation/verification.md @@ -0,0 +1,96 @@ +# Navigation regression verification — 26 September 2026 + +## Status and scope + +Fix prepared locally on `codex/fix-ask-tab-navigation`, based on published source +`e79e16b572d6e3472ac94500bf812d937bd89135`. Not committed, pushed, merged or +released. The user explicitly requested holding this fix for a later grouped +release. No version allocation is needed until that release is prepared; use a +new version/build and the existing release checks then. Published 2.5.15 remains +unchanged. + +Only runtime changes: retain a native, non-collapsing Home content View with the +selected tab as its key; add stable selectors to the existing footer controls. +No wallet, key, transaction, recovery, storage or chat logic changed. + +## Before/after evidence + +The user's physical iPhone 15 Pro Max report was reproduced in the unmodified +2.5.15 Release simulator app on a fresh iPhone 15 Pro Max simulator running +iOS 26.4: empty Ask → More retained Ask prompts/security text and lost the +footer. The same sequence passed after replacing the JS bundle with the fix. + +This verifies the app-level boundary correction. It does not establish the exact +upstream Fabric mounting defect; the native logs did not expose the failing +transaction. The user's physical iOS version is unknown. + +Private evidence directory: +`/Users/devaccount/Workspace/keeper-local-dev/ask-navigation-evidence/` + +- Before: `empty-switch-baseline.log`, `empty-before.png`, `empty-after.png`. +- After: `fixed-ios-retry.log`, `fixed-empty-before.png`, `fixed-empty-after.png`. + +## Test matrix + +| Check | Environment | Result / evidence | +| --- | --- | --- | +| Original empty Ask → More failure | iPhone 15 Pro Max / iOS 26.4 simulator | Reproduced before; passed after | +| Ask → More / Wallets / Keys, three cycles (nine exits) | Same iPhone simulator | PASS, `ios-repeated-tabs-retry.log` | +| Same nine exits with Recovery Key reminder present | iPhone 13 mini / iOS 26.4 simulator | PASS, `small-ios.log` | +| Three exits at extra-extra-extra-large system text size | iPhone 15 Pro Max simulator | PASS, `ios-large-text.log`; restored normal size afterward | +| Nine empty-state exits | Android 17 emulator, disposable user 10 | PASS, `android-tabs-final.log` | +| Existing chat → keyboard focus → back → history → main tabs, three cycles | Android emulator | PASS, `android-history.log` | +| Repeated disclaimer dismiss/reopen, About Ask return, keyboard/back/history/tab switches | Android emulator | PASS, three cycles, `android-info-retry.log` | +| Same information/modal/keyboard/history flow, three cycles | iPhone 15 Pro Max simulator | PASS, `ios-history-final.log` | +| Keys add-sheet and Wallets type-sheet open/cancel, then Ask → More | iPhone 13 mini simulator | PASS, `small-key-modal.log`, `small-wallet-modal.log`; no key or wallet created | +| Relevant existing unit suites | Local Jest | PASS, 27 tests in two suites, `jest.log` | +| Formatting, whitespace, release-version consistency, strict OpenSpec validation | Local checks | PASS | + +The automated flows assert both absence of stale Ask content and presence and +operation of the footer, rather than merely checking that no crash occurred. +Chat checks use disposable conversations with a predefined nonsensitive question. +An iOS response was received; Android AI-provider availability is not an acceptance +claim of these navigation tests. No funds were transferred. + +## Test build limits + +- iOS: existing Release simulator native binary, with a fresh production-mode + Hermes JS bundle containing the fix, locally ad-hoc signed. No new native + dependencies; not a fresh Xcode archive or distributable IPA. +- Android: existing debug native app using the fixed JS through Metro and local + backend configuration. Not a signed production APK/AAB validation. +- Two iPhone device sizes share iOS 26.4. No second iOS-version coverage or + physical-device verification. Enlarged-text checks concern navigation, not a + full accessibility audit. +- The physical iPhone follow-up remains tagged **Office** in T-066. When an + eventual test build is available, repeat the reported exits and history/back + sequence on that iPhone. No user action is needed now. +- Before public release: review/commit the patch, create fresh signed artifacts + with new verified version/build numbers, and rerun these flows on the intended + release builds. Do not overwrite published artifacts or the existing tag. + +## Earlier harness failures, retained for audit + +Initial attempts encountered onboarding/notification dialogs, an overly strict +wallet text selector (iOS merges accessibility labels), and an information-link +selector that omitted its arrow. The info button opens a disclaimer first, then +its Learn more link opens About Ask Keeper. The flow now follows that real path. +These setup/selector failures are retained separately from the reproduced +application failure; they are not counted as successful test runs. + +## Reusable regression rule + +When changing Home content or navigation, exercise both empty Ask and saved-chat +history, with the Recovery Key reminder visible, including keyboard dismissal, +modal/information return and repeated exits to every sibling tab. Check rendered +content and footer reachability on native iOS and Android; mocked React tests do +not validate native view mounting. Preserve an explicit native tab-content +boundary around elevated/absolute-positioned children and keep the footer outside +it. Do not weaken native renderer assertions as a substitute for regression tests. + +## Environment restored + +The Android disposable profile's debug preferences were restored and verified, +the task-specific port 8085 reverse was removed, and the emulator was returned to +its original user 0. Disposable QA data and evidence were retained. iOS system +text size was restored to its original setting. Other Metro servers were untouched. diff --git a/package.json b/package.json index 28f4341087..6d67e4c205 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "hexa_keeper", - "version": "2.5.15", + "version": "2.5.16", "private": true, "scripts": { "ios": "npx react-native run-ios --scheme=hexa_keeper_dev --simulator 'iPhone 16' ", diff --git a/release/README.md b/release/README.md index 215fdfb721..2554527896 100644 --- a/release/README.md +++ b/release/README.md @@ -1,10 +1,8 @@ # Release version verification -**Current milestone:** a small test release of the version-consistency change. Per the user’s 25 September direction, security remediation is separate and private-advisory sign-in is not a prerequisite. No security fixes are claimed. Docker contributor packaging is separate from the release patch. The user reconfirmed all three public destinations: App Store, Play production and the normal public APK channel. +**Current milestone:** T-053 maintenance candidate, authorised 28 September 2026: finish the prepared Ask Keeper navigation correction and regression-test existing Dust/Testnet behaviour. Version 2.5.15 remains untouched. Physical-device follow-up is optional; simulator coverage must be labelled accurately. -T-050 addresses the Android regression from marketing version 2.5.14 to 2.3.15 despite an increased build code. Marketing versions and platform build counters must both move forward. - -`version.json` selects candidate 2.5.15, Android 622 and iOS 615 using authenticated store observations from September 25, 2026. The iOS counter also exceeds local source build 614. This is not publication approval or proof of tested binaries. Do not substitute the public App Store listing for uploaded-build history. +`version.json` selects candidate 2.5.16, Android 623 and iOS 616. Authenticated Google Play tracks/uploaded bundles and App Store Connect upload history were checked on 28 September: Android highest 622 and iOS highest 615, both marketing version 2.5.15. The iOS distribution page shows 2.5.15 Ready for Distribution with no newer iOS draft. Keep private investigation and diagnostics outside public release material. 1. Read all uploaded builds and release tracks for Android package `io.hexawallet.bitcoinkeeper`, and uploaded iOS builds for `io.hexawallet.keeper`. Preserve the timestamp and evidence outside the repository. Refresh before upload; preflight accepts evidence at most 24 hours old. 2. Update the manifest with the highest known marketing version and highest uploaded build per platform. Select one greater marketing version for both stores and greater platform build counters. Check unpublished App Store version records as well. diff --git a/release/version.json b/release/version.json index 626a58552f..c8c776defd 100644 --- a/release/version.json +++ b/release/version.json @@ -1,21 +1,21 @@ { - "version": "2.5.15", - "androidVersionCode": 622, - "iosBuildNumber": 615, + "version": "2.5.16", + "androidVersionCode": 623, + "iosBuildNumber": 616, "baselines": { "android": { "applicationId": "io.hexawallet.bitcoinkeeper", - "verifiedAt": "2026-09-25T13:10:54Z", - "highestUploadedBuild": 621, - "highestMarketingVersion": "2.5.14", - "productionRelease": "621 (2.3.15)" + "verifiedAt": "2026-09-28T12:51:36.298748+00:00", + "highestUploadedBuild": 622, + "highestMarketingVersion": "2.5.15", + "productionRelease": "622 (2.5.15)" }, "ios": { "applicationId": "io.hexawallet.keeper", - "verifiedAt": "2026-09-25T13:44:11Z", - "highestUploadedBuild": 612, - "highestMarketingVersion": "2.5.14", - "productionRelease": "612 (2.5.14)" + "verifiedAt": "2026-09-28T12:51:36.298975+00:00", + "highestUploadedBuild": 615, + "highestMarketingVersion": "2.5.15", + "productionRelease": "615 (2.5.15)" } } } diff --git a/src/components/MenuFooter.tsx b/src/components/MenuFooter.tsx index 6547ec6923..bfdc8d492b 100644 --- a/src/components/MenuFooter.tsx +++ b/src/components/MenuFooter.tsx @@ -20,21 +20,25 @@ const MenuFooter = ({ selectedOption, onOptionChange }) => { const menuOptions = [ { + testID: 'home_tab_wallets', name: walletTranslation.homeWallets, defaultIcon: , selectedIcon: , }, { + testID: 'home_tab_keys', name: walletTranslation.keys, defaultIcon: , selectedIcon: , }, { + testID: 'home_tab_ask', name: askAi.ask, defaultIcon: , selectedIcon: , }, { + testID: 'home_tab_more', name: walletTranslation.more, defaultIcon: , selectedIcon: , @@ -53,6 +57,7 @@ const MenuFooter = ({ selectedOption, onOptionChange }) => { {menuOptions.map((option) => ( onOptionChange(option.name)} style={[styles.menuItem]} > diff --git a/src/screens/Home/HomeScreen.tsx b/src/screens/Home/HomeScreen.tsx index d6846a9e42..9640e147c5 100644 --- a/src/screens/Home/HomeScreen.tsx +++ b/src/screens/Home/HomeScreen.tsx @@ -1,4 +1,4 @@ -import { StyleSheet, TouchableOpacity } from 'react-native'; +import { StyleSheet, TouchableOpacity, View } from 'react-native'; import { Box, useColorMode } from '@gluestack-ui/themed-native-base'; import React, { useContext, useEffect, useRef, useState } from 'react'; import useWallets from 'src/hooks/useWallets'; @@ -268,7 +268,10 @@ function NewHomeScreen({ route }) { )} - {content} + {/* Keep tab descendants (including elevated FABs) out of the footer's native parent. */} + + {content} + { From 7fdd502702a8aa149f5a3ad87aac7a274a61e6e8 Mon Sep 17 00:00:00 2001 From: Anant Date: Mon, 28 Sep 2026 18:49:25 +0530 Subject: [PATCH 003/254] fix: initialize Android screen fragment restoration --- .../app/src/main/java/com/hexa_keeper/MainActivity.kt | 3 +++ openspec/changes/fix-ask-tab-navigation/design.md | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/android/app/src/main/java/com/hexa_keeper/MainActivity.kt b/android/app/src/main/java/com/hexa_keeper/MainActivity.kt index b9dd5e2ac9..c807d76e6e 100644 --- a/android/app/src/main/java/com/hexa_keeper/MainActivity.kt +++ b/android/app/src/main/java/com/hexa_keeper/MainActivity.kt @@ -8,6 +8,7 @@ import com.facebook.react.ReactActivity import com.facebook.react.ReactActivityDelegate import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.fabricEnabled import com.facebook.react.defaults.DefaultReactActivityDelegate +import com.swmansion.rnscreens.fragment.restoration.RNScreensFragmentFactory class MainActivity : ReactActivity() { @@ -17,6 +18,8 @@ class MainActivity : ReactActivity() { DefaultReactActivityDelegate(this, mainComponentName, fabricEnabled) override fun onCreate(savedInstanceState: Bundle?) { + // React Native owns screen state; discard stale native screen fragments on recreation. + supportFragmentManager.fragmentFactory = RNScreensFragmentFactory() super.onCreate(savedInstanceState) window.decorView.post { diff --git a/openspec/changes/fix-ask-tab-navigation/design.md b/openspec/changes/fix-ask-tab-navigation/design.md index d2f781b63f..244040a94b 100644 --- a/openspec/changes/fix-ask-tab-navigation/design.md +++ b/openspec/changes/fix-ask-tab-navigation/design.md @@ -36,3 +36,13 @@ and presence/operability of tabs. A React-only pass is insufficient. Distinguish the user's physical iPhone 15 Pro Max evidence from simulator and emulator checks. Test a smaller iPhone and larger text where available. Store only disposable QA data in evidence; never commit user screenshots or wallet secrets without need. + +## Android activity recreation follow-up + +Fresh release testing on 28 September reproduced a native crash when the system +font scale changed: Android attempted to restore a ScreenStackFragment without +React Native owning its screen state. Register the installed react-native-screens +4.24.0 RNScreensFragmentFactory in MainActivity before super.onCreate, as required +by that version's Android setup instructions. Preserve the existing insets setup. +This is native screen-lifecycle handling only; no wallet storage or authentication +change. Recheck activity recreation, unlock/state retention and Ask navigation. From d9cabbc5de55370f911c12b034b9601caea05033 Mon Sep 17 00:00:00 2001 From: Anant Date: Tue, 29 Sep 2026 18:44:18 +0530 Subject: [PATCH 004/254] Prepare Now 2.5.17 recovery and signing fixes --- android/app/build.gradle | 4 +- ios/Podfile.lock | 8 +- ios/hexa_keeper.xcodeproj/project.pbxproj | 16 +- .../fix-recovery-backup-freshness/design.md | 36 ++ .../evidence/migration-tests.txt | 19 + .../evidence/recovery-tests.txt | 49 ++ .../nfr-benchmark.json | 43 ++ .../nfr-review.md | 50 ++ .../fix-recovery-backup-freshness/proposal.md | 16 + .../specs/assisted-server-backup/spec.md | 65 +++ .../fix-recovery-backup-freshness/tasks.md | 27 ++ .../validation.md | 36 ++ package.json | 5 +- release/README.md | 4 +- release/version.json | 22 +- .../Backup/BackupHealthCheckList.tsx | 3 +- src/components/Modal/PasscodeVerify.tsx | 41 +- src/context/Localization/language/en.json | 47 +- src/context/Localization/language/es.json | 47 +- src/hooks/useSettingKeeper.tsx | 2 +- src/hooks/useUSDTWallets.ts | 52 +- src/navigation/Navigator.tsx | 2 + src/navigation/types.ts | 1 + .../AppSettings/AssistedBackupStatus.tsx | 95 ++++ .../BackupWallet/ViewRecoveryKeyScreen.tsx | 9 +- .../Notifications/NotificationsCenter.tsx | 3 +- src/screens/LoginScreen/CreatePin.tsx | 9 +- src/screens/LoginScreen/Login.tsx | 17 +- src/screens/Recovery/EnterSeedScreen.tsx | 17 +- src/screens/SeedScreens/ExportSeedScreen.tsx | 6 +- src/screens/Vault/ResetEmergencyKey.tsx | 4 +- src/screens/Vault/ResetInheritanceKey.tsx | 3 +- .../Vault/VaultMigrationController.tsx | 15 +- src/services/backend/Relay.ts | 32 +- src/services/backup/image.ts | 269 +++++++++++ src/services/backup/repair.ts | 238 ++++++++++ src/services/backup/restore.ts | 27 ++ src/services/backup/transport.ts | 71 +++ src/services/rest/RestClient.ts | 10 +- .../operations/miniscript/initialTimelock.ts | 20 + src/store/reducers/bhr.ts | 24 + src/store/sagaActions/bhr.ts | 7 +- src/store/sagaActions/upgrade.ts | 4 +- src/store/sagas/bhr.ts | 449 +++++++----------- src/store/sagas/index.ts | 9 +- src/store/sagas/login.ts | 17 +- src/store/sagas/upgrade.ts | 12 +- src/store/sagas/utxos.ts | 39 +- src/store/sagas/wallets.ts | 105 ++-- src/utils/utilities.ts | 39 +- tests/recovery/backupFeedback.test.cjs | 54 +++ tests/recovery/backupFreshness.test.cjs | 169 +++++++ tests/recovery/benchmarks/backup-cost.cjs | 74 +++ tests/recovery/helpers.cjs | 58 +++ tests/recovery/keyRecovery.test.cjs | 280 +++++++++++ tests/recovery/manualCoinChoices.test.cjs | 136 ++++++ tests/recovery/nodeReplacement.test.cjs | 29 ++ tests/recovery/recoveryErrorFeedback.test.cjs | 81 ++++ tests/recovery/repair.test.cjs | 119 +++++ tests/recovery/restoreIntegrity.test.cjs | 203 ++++++++ tests/recovery/sagaHarness.cjs | 290 +++++++++++ tests/recovery/sagaState.test.cjs | 62 +++ tests/recovery/transport.test.cjs | 56 +++ tests/recovery/usdtBackup.test.cjs | 169 +++++++ tests/screens/PinEntry.test.tsx | 155 ++++++ tests/services/initialTimelock.test.ts | 113 +++++ tests/services/resetTimelockBoundary.test.ts | 161 +++++++ tests/services/resetTimelockRoute.test.ts | 53 +++ tests/store/versionHistory.test.ts | 249 ++++++++++ 69 files changed, 4209 insertions(+), 447 deletions(-) create mode 100644 openspec/changes/fix-recovery-backup-freshness/design.md create mode 100644 openspec/changes/fix-recovery-backup-freshness/evidence/migration-tests.txt create mode 100644 openspec/changes/fix-recovery-backup-freshness/evidence/recovery-tests.txt create mode 100644 openspec/changes/fix-recovery-backup-freshness/nfr-benchmark.json create mode 100644 openspec/changes/fix-recovery-backup-freshness/nfr-review.md create mode 100644 openspec/changes/fix-recovery-backup-freshness/proposal.md create mode 100644 openspec/changes/fix-recovery-backup-freshness/specs/assisted-server-backup/spec.md create mode 100644 openspec/changes/fix-recovery-backup-freshness/tasks.md create mode 100644 openspec/changes/fix-recovery-backup-freshness/validation.md create mode 100644 src/screens/AppSettings/AssistedBackupStatus.tsx create mode 100644 src/services/backup/image.ts create mode 100644 src/services/backup/repair.ts create mode 100644 src/services/backup/restore.ts create mode 100644 src/services/backup/transport.ts create mode 100644 src/services/wallets/operations/miniscript/initialTimelock.ts create mode 100644 tests/recovery/backupFeedback.test.cjs create mode 100644 tests/recovery/backupFreshness.test.cjs create mode 100644 tests/recovery/benchmarks/backup-cost.cjs create mode 100644 tests/recovery/helpers.cjs create mode 100644 tests/recovery/keyRecovery.test.cjs create mode 100644 tests/recovery/manualCoinChoices.test.cjs create mode 100644 tests/recovery/nodeReplacement.test.cjs create mode 100644 tests/recovery/recoveryErrorFeedback.test.cjs create mode 100644 tests/recovery/repair.test.cjs create mode 100644 tests/recovery/restoreIntegrity.test.cjs create mode 100644 tests/recovery/sagaHarness.cjs create mode 100644 tests/recovery/sagaState.test.cjs create mode 100644 tests/recovery/transport.test.cjs create mode 100644 tests/recovery/usdtBackup.test.cjs create mode 100644 tests/screens/PinEntry.test.tsx create mode 100644 tests/services/initialTimelock.test.ts create mode 100644 tests/services/resetTimelockBoundary.test.ts create mode 100644 tests/services/resetTimelockRoute.test.ts create mode 100644 tests/store/versionHistory.test.ts diff --git a/android/app/build.gradle b/android/app/build.gradle index 90d90807c5..c2f737d4d3 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -98,8 +98,8 @@ android { applicationId "io.hexawallet.keeper" minSdkVersion rootProject.ext.minSdkVersion targetSdkVersion rootProject.ext.targetSdkVersion - versionCode 623 - versionName "2.5.16" + versionCode 624 + versionName "2.5.17" missingDimensionStrategy 'react-native-camera', 'general' missingDimensionStrategy 'store', 'play' multiDexEnabled true diff --git a/ios/Podfile.lock b/ios/Podfile.lock index a5c2f9af0b..97a5cb82e6 100644 --- a/ios/Podfile.lock +++ b/ios/Podfile.lock @@ -3986,7 +3986,7 @@ SPEC CHECKSUMS: glog: 5683914934d5b6e4240e497e0f4a3b42d1854183 GoogleDataTransport: aae35b7ea0c09004c3797d53c8c41f66f219d6a7 GoogleUtilities: 00c88b9a86066ef77f0da2fab05f65d7768ed8e1 - hermes-engine: 4ec27e7c84ed7950da6e65008539685455d8bef4 + hermes-engine: 497c47216fdf505312e6e9fb53a413e085f8a8e1 HtmlToPdf: 5c0649e93aa1c6583f400268eedea00f15e0fa5f libportal-ios: d9aa55474e2d5be8e38e96345dd37be34fda45b4 libportal-react-native: 05778ef04321517c8366dc7173d05aa9fe131ff2 @@ -4086,7 +4086,7 @@ SPEC CHECKSUMS: React-utils: 493a45d877f410d62476c045fbf220acbffba194 React-webperformancenativemodule: 9b212313b01f5489f21963fbd4a81ef20f280097 ReactAppDependencyProvider: 5bf52020fdc0d0a337068ea42d4a6676ce4a04bc - ReactCodegen: 5c808345ff57b4f4f1eab230f541921ad9e37500 + ReactCodegen: e99a2b210dade5f08acfabb6309357e7f8c22640 ReactCommon: 3b79ecfd327035654f6c36cf1de8e222d8b835e6 RealmJS: a2e97359d58d72ca9fc2deb2e14ba34a218652e5 RNCClipboard: a9d5216cbfaf2f51cfe80bcb132856bb9158fe0d @@ -4099,12 +4099,12 @@ SPEC CHECKSUMS: RNKeychain: 182d986f809280edd9444a557adc21845f7d1e6f RNLocalize: 95a43f85e41a966be7bc9cff2437128911c52da0 RNQrGenerator: 36f1af182d7673bc36e317aca0d293d19883d6e3 - RNReanimated: 6b69a94db352e4dae7eb540f6fab60ff05c18508 + RNReanimated: dd6943b67163a2fa23ecaefc457cf34e7f94df74 RNScreens: 7179cc1ba31b4e18ed29f10abf20c24a7961cf4c RNSentry: f07853ff3fcbb69df26da1d9744e95ee97f683fd RNShare: 11923289a9964c6bd5f507eaa7fe61ec51b0adce RNSVG: 6a91afc99237de70b617c7a84ba84c988ad5573b - RNWorklets: 3f35c2c01e5839d4fee0496d10ada185628ba328 + RNWorklets: 9daaf5ec29337d9b12503e9d2618a4e7d8d91475 SDWebImage: a7f831e1a65eb5e285e3fb046a23fcfbf08e696d SDWebImageWebPCoder: 908b83b6adda48effe7667cd2b7f78c897e5111d Sentry: 4e85b40e4b0235853f40f959d315a6b28e3148d8 diff --git a/ios/hexa_keeper.xcodeproj/project.pbxproj b/ios/hexa_keeper.xcodeproj/project.pbxproj index c77033da6c..88ff8d481f 100644 --- a/ios/hexa_keeper.xcodeproj/project.pbxproj +++ b/ios/hexa_keeper.xcodeproj/project.pbxproj @@ -748,7 +748,7 @@ CODE_SIGN_ENTITLEMENTS = hexa_keeper/hexa_keeper.entitlements; CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 616; + CURRENT_PROJECT_VERSION = 617; DEVELOPMENT_TEAM = Y5TCB759QL; ENABLE_BITCODE = NO; HEADER_SEARCH_PATHS = ( @@ -849,7 +849,7 @@ "$(inherited)", "\"$(SRCROOT)\"", ); - MARKETING_VERSION = 2.5.16; + MARKETING_VERSION = 2.5.17; OTHER_LDFLAGS = ( "$(inherited)", "-ObjC", @@ -876,7 +876,7 @@ CODE_SIGN_IDENTITY = "Apple Development"; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Distribution"; CODE_SIGN_STYLE = Manual; - CURRENT_PROJECT_VERSION = 616; + CURRENT_PROJECT_VERSION = 617; DEVELOPMENT_TEAM = Y5TCB759QL; "DEVELOPMENT_TEAM[sdk=iphoneos*]" = Y5TCB759QL; HEADER_SEARCH_PATHS = ( @@ -977,7 +977,7 @@ "$(inherited)", "\"$(SRCROOT)\"", ); - MARKETING_VERSION = 2.5.16; + MARKETING_VERSION = 2.5.17; OTHER_LDFLAGS = ( "$(inherited)", "-ObjC", @@ -1147,7 +1147,7 @@ CODE_SIGN_IDENTITY = "Apple Development"; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Distribution"; CODE_SIGN_STYLE = Manual; - CURRENT_PROJECT_VERSION = 616; + CURRENT_PROJECT_VERSION = 617; DEVELOPMENT_TEAM = Y5TCB759QL; "DEVELOPMENT_TEAM[sdk=iphoneos*]" = Y5TCB759QL; ENABLE_BITCODE = NO; @@ -1250,7 +1250,7 @@ "$(PROJECT_DIR)", "\"$(SRCROOT)\"", ); - MARKETING_VERSION = 2.5.16; + MARKETING_VERSION = 2.5.17; OTHER_LDFLAGS = ( "$(inherited)", "-ObjC", @@ -1278,7 +1278,7 @@ CODE_SIGN_ENTITLEMENTS = hexa_keeper_dev.entitlements; CODE_SIGN_IDENTITY = "Apple Distribution"; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Distribution"; - CURRENT_PROJECT_VERSION = 616; + CURRENT_PROJECT_VERSION = 617; DEVELOPMENT_TEAM = Y5TCB759QL; "DEVELOPMENT_TEAM[sdk=iphoneos*]" = Y5TCB759QL; HEADER_SEARCH_PATHS = ( @@ -1380,7 +1380,7 @@ "$(PROJECT_DIR)", "\"$(SRCROOT)\"", ); - MARKETING_VERSION = 2.5.16; + MARKETING_VERSION = 2.5.17; OTHER_LDFLAGS = ( "$(inherited)", "-ObjC", diff --git a/openspec/changes/fix-recovery-backup-freshness/design.md b/openspec/changes/fix-recovery-backup-freshness/design.md new file mode 100644 index 0000000000..3ab14e30c9 --- /dev/null +++ b/openspec/changes/fix-recovery-backup-freshness/design.md @@ -0,0 +1,36 @@ +# Design + +## Entry points and user flow +Reuse More → Backup and Recovery → Assisted Server Backup. The row opens `AssistedBackupStatus`; its existing switch retains enable/delete behavior. The existing server-backup notification opens the same status screen. Login dispatches a read-only check after wallet sync for opted-in accounts with pending/unverified status. Login does not await the result. + +The status screen uses ScreenWrapper, WalletHeader, KeeperText, Buttons, themed cards and a ScrollView. Body text explicitly enables system font scaling. Full-width actions are stacked. Back is available throughout and does not cancel or imply rollback. No progress percentages or promises of continued background execution are shown. + +Copy is maintained in `recoveryBackup` in both existing locale JSON files. States: disabled, checking, different, preparing, uploading, verifying, verified, conflict, unverified. Different offers **Back Up Now**; terminal enabled states offer **Check Again**. Slow-stage text appears after ten seconds. Restart maps persisted in-progress phases to unverified until the new check starts. + +## Backup comparison and repair +`src/services/backup/image.ts` compares canonical decrypted recovery data, with deterministic object ordering. Exclude rebuildable chain caches, balance, sync timestamps, receiving-address caches and live node connection flags. Retain user-authored manual UTXO spendability overrides by outpoint, independent of confirmation/cache ordering. Retain keys, derivation, address counters, signer policy, vault policy/archive state, labels and user metadata. Include BTC wallets from both networks, USDT, signers, vaults, nodes and labels. Sanitize local signer/vault secrets using the existing helpers and never mutate Realm during checking. + +Server-only records, differing key identities and server address counters ahead of local data produce conflict with no upload. Invalid/decryption-failed/partial responses produce unverified with no upload. Capture a local snapshot and check it again before upload and completion. Final success requires the uploaded recovery data to match the server readback and current local recovery data. + +A lost upload response has an ambiguous outcome. Attempt readback within the same overall deadline. A matching readback is sufficient for verified success; otherwise keep unverified and let the user check again. No automatic retry loop or repeated full upload on startup. + +## Ordering and persistence +All client backup mutation endpoints use a shared per-account queue in `transport.ts`; full inspection owns the queue through readback. Pending incremental writes increment an account revision and invalidate any in-flight completion claim. Vault `appID` and `appId` payload variants share the correct account queue. A new wallet's incremental write is never replaced with a pre-creation snapshot. + +BHR is separately persisted. `backupRepairStateByAppId` and `backupRepairCompletedByAppId` are durable; `backupRepairRunningByAppId` is blacklisted. Only phase verified sets completion. Old persisted states use nullish defaults. Account changes cannot clear the next account's pending flag or enable its backup setting. No Realm migration is needed. + +## NFR implementation and remaining limits +Transport aborts after 30 seconds without advancing upload/download bytes, with a ten-minute overall attempt deadline. Both are engineering limits requiring native/deployed-backend validation. Progress callbacks reporting the same byte count do not extend the timer. Error messages discard Axios request bodies/headers. No new telemetry is added. + +AES encrypt/decrypt yields between 16 KiB chunks using CryptoJS's existing OpenSSL format. Local JSON text is guarded using a conservative three bytes per UTF-16 code unit against an 8 MiB record budget; encrypted aggregate payloads are limited to 32 MiB. Reject oversized data without truncation or verified success. Realm extraction, JSON serialization/parsing, Base64 conversion and UTF-8 decoding still have synchronous sections; native frame-time and memory budgets are NOT established by these guards or desktop tests. + +## Server contract and release gates +Repair now requires `getBackupSnapshot` and `repairAppBackup` from the paired relay candidate. The snapshot returns a SHA-256 revision over encrypted recovery records. Repair supplies that revision and rechecks it inside a MongoDB snapshot transaction. Vaults, labels, signer maps and the app image update atomically; stale revisions, missing referenced records and attempts to omit existing recovery records fail closed. Existing label IDs update their encrypted content. Archived metadata is preserved. No fallback to the legacy full replacement endpoint is permitted. + +Local database and HTTP tests establish this contract for the new endpoints. Production requires a transaction-capable replica set, compatible deployed request limits and relay deployment before the mobile update. Those deployment facts are not yet verified. + +The legacy full-backup endpoint remains callable by older clients and does not participate in the new revision contract. Mixed-version full replacement remains an unresolved release gate. Normal incremental wallet/signer updates in the relay candidate now set individual records atomically, including a version-only call without `nodes`, but that does not make every legacy mutation transactional. Server-only/conflicting data is preserved pending reconciliation. + +## Restore integrity follow-up + +Actual Recovery Key restoration predecodes the complete server image, verifies referenced vault/label identities, and requires each local persistence operation to succeed. A failed fetch cannot masquerade as an empty backup. Success is exposed only after the expected derived Recovery Key signing material is present; failed records are not skipped. Database writes remain sequential rather than one atomic transaction. A correct Recovery Key with unavailable/corrupt backup receives retry feedback instead of an incorrect-words error. Existing private-material policy is preserved per key type. diff --git a/openspec/changes/fix-recovery-backup-freshness/evidence/migration-tests.txt b/openspec/changes/fix-recovery-backup-freshness/evidence/migration-tests.txt new file mode 100644 index 0000000000..322481c078 --- /dev/null +++ b/openspec/changes/fix-recovery-backup-freshness/evidence/migration-tests.txt @@ -0,0 +1,19 @@ +PASS tests/services/recoveryKeyMigration.test.ts + migration 4: recoveryKeyBackedUpByAppId → recoveryKeyStatusByAppId + ✓ maps Boolean true → "confirmed" (1 ms) + ✓ maps Boolean false → "generated" + ✓ maps missing entry → "generated" (1 ms) + ✓ handles undefined recoveryKeyBackedUpByAppId gracefully + ✓ maps multiple apps correctly + setRecoveryKeyStatus reducer + ✓ sets status to "generated" + ✓ sets status to "viewed" + ✓ sets status to "skipped" + ✓ sets status to "confirmed" and syncs legacy boolean to true + ✓ non-confirmed statuses do not change legacy boolean field + +Test Suites: 1 passed, 1 total +Tests: 10 passed, 10 total +Snapshots: 0 total +Time: 0.199 s, estimated 1 s +Ran all test suites matching /tests\/services\/recoveryKeyMigration.test.ts/i. diff --git a/openspec/changes/fix-recovery-backup-freshness/evidence/recovery-tests.txt b/openspec/changes/fix-recovery-backup-freshness/evidence/recovery-tests.txt new file mode 100644 index 0000000000..7686867b20 --- /dev/null +++ b/openspec/changes/fix-recovery-backup-freshness/evidence/recovery-tests.txt @@ -0,0 +1,49 @@ +✔ BackupHealthCheckList.tsx never enables backup or shows success after failure (33.969708ms) +✔ ExportSeedScreen.tsx never enables backup or shows success after failure (16.320167ms) +✔ ViewRecoveryKeyScreen.tsx never enables backup or shows success after failure (7.27875ms) +✔ wallet added after initial backup survives encrypted restore: MAINNET (132.903666ms) +✔ wallet added after initial backup survives encrypted restore: TESTNET (46.145042ms) +✔ pending repair never suppresses a not-yet-persisted incremental addition (36.970166ms) +✔ disabled backup makes no uploads and preserves local creation (31.466583ms) +✔ backup failure retains wallet and retry: {"online":false} (20.890333ms) +✔ backup failure retains wallet and retry: {"incrementalError":true} (20.1545ms) +✔ recovery restores saved wallets and confirms Recovery Key status: MAINNET (27.787625ms) +✔ recovery restores saved wallets and confirms Recovery Key status: TESTNET (16.323292ms) +✔ recovery restores saved wallets and confirms Recovery Key status: MAINNET,TESTNET (16.082333ms) +✔ relay rejection retains the existing wallet-creation failure contract (15.722292ms) +✔ backup gate never performs an unsolicited full repair: true (13.974042ms) +✔ backup gate never performs an unsolicited full repair: false (13.935875ms) +✔ repair markers are stored in the separately persisted backup slice (12.869875ms) +✔ pending repair does not suppress explicit wallet deletion (14.204791ms) +✔ upgrade check detects omissions without uploading; explicit repair verifies readback (90.5965ms) +✔ matching upgrade only reads and does not reupload (67.046166ms) +✔ readError never claims success (35.042917ms) +✔ reject never claims success (55.039708ms) +✔ corruptWrite never claims success (22.31275ms) +✔ lost upload acknowledgement is resolved by readback without duplicate upload (31.242375ms) +✔ server-only wallet is preserved and blocks replacement (20.713375ms) +✔ same IDs with different recovery keys are a conflict (0.321708ms) +✔ same IDs with changed user metadata require an update (0.939583ms) +✔ canonical comparison ignores object order and chain caches but retains address counters (0.628959ms) +✔ BTC both networks, USDT, vaults, keys, labels and nodes are verified without mutating local data (101.287291ms) +✔ malformed or partial server response is unavailable, never an empty snapshot (40.127625ms) +✔ mutation arriving during upload invalidates completion and remains queued until readback (15.497666ms) +✔ account switch during check cannot upload one account with another account data (11.366333ms) +✔ chunked encryption remains compatible with existing recovery including Unicode (27.616042ms) +✔ oversized record fails without uploading or truncating (13.001333ms) +✔ only verified readback sets durable per-account completion and clears pending (100.407291ms) +✔ offline/unverified remains pending and retryable, never spins indefinitely (34.430583ms) +✔ account switch preserves per-account result without clearing another account pending state (42.929ms) +✔ a later mutation invalidates verified status without affecting a second account (0.530667ms) +✔ stalled transport aborts after 30 seconds and redacts request errors (27.97925ms) +✔ only real progress resets stall clock; active transfer still has overall deadline (17.014792ms) +✔ same-account writes serialize, another account runs independently, rejected queue recovers (10.398833ms) +✔ repeated repair actions share one upload and readback (69.173875ms) +ℹ tests 41 +ℹ suites 0 +ℹ pass 41 +ℹ fail 0 +ℹ cancelled 0 +ℹ skipped 0 +ℹ todo 0 +ℹ duration_ms 868.918958 diff --git a/openspec/changes/fix-recovery-backup-freshness/nfr-benchmark.json b/openspec/changes/fix-recovery-backup-freshness/nfr-benchmark.json new file mode 100644 index 0000000000..d4c357f7d1 --- /dev/null +++ b/openspec/changes/fix-recovery-backup-freshness/nfr-benchmark.json @@ -0,0 +1,43 @@ +{ + "environment": "darwin/arm64 Node v25.9.0", + "samples": "one warm-up and three measured repetitions per case", + "networkTiming": "theoretical payload transfer only; no measured network or protocol latency", + "results": [ + { + "records": 10, + "recordKiB": 50, + "plaintextMiB": 0.49, + "ciphertextMiB": 0.65, + "encryptMs": 19.3, + "decryptAndParseMs": 13.5, + "uploadPlusReadbackSecondsAt1Mbps": 10.9 + }, + { + "records": 100, + "recordKiB": 50, + "plaintextMiB": 4.89, + "ciphertextMiB": 6.52, + "encryptMs": 175.5, + "decryptAndParseMs": 136.2, + "uploadPlusReadbackSecondsAt1Mbps": 109.4 + }, + { + "records": 100, + "recordKiB": 200, + "plaintextMiB": 19.54, + "ciphertextMiB": 26.05, + "encryptMs": 718, + "decryptAndParseMs": 498.4, + "uploadPlusReadbackSecondsAt1Mbps": 437.1 + }, + { + "records": 1, + "recordKiB": 20000, + "plaintextMiB": 19.53, + "ciphertextMiB": 26.04, + "encryptMs": 916.6, + "decryptAndParseMs": 503.8, + "uploadPlusReadbackSecondsAt1Mbps": 436.9 + } + ] +} diff --git a/openspec/changes/fix-recovery-backup-freshness/nfr-review.md b/openspec/changes/fix-recovery-backup-freshness/nfr-review.md new file mode 100644 index 0000000000..ec2645a4ea --- /dev/null +++ b/openspec/changes/fix-recovery-backup-freshness/nfr-review.md @@ -0,0 +1,50 @@ +# Backup comparison and repair: NFR review + +Reviewed 29 September 2026. This audit records the original baseline for the check → prompt → update → verify flow. The flow and safeguards are now implemented locally; see design.md and validation.md for current status. The observations below describe the pre-change candidate, not the final implementation. No approved numerical backup performance budgets were found in the synced product sources or repository OpenSpec documents searched. The thresholds below are proposed engineering targets, subject to native validation. + +## Original observed gaps (addressed unless listed as a remaining gate in design.md) + +- `src/services/rest/RestClient.ts:64`: POST passes headers only to Axios. The backup endpoints supply no explicit request timeout, abort signal, or transfer progress callbacks. No global Axios timeout override was found. A stalled request can leave the loading state pending indefinitely. An existing unrelated call passes `{timeout: 20000}` as the headers argument, so that pattern must not be copied as a timeout implementation. +- `src/store/sagas/bhr.ts`: full backup synchronously serializes/encrypts all wallet, signer, vault, node and label records before posting one large request. Saga orchestration does not move synchronous encryption off the React Native JavaScript thread. One large wallet can stall it even if work yields between smaller records. +- `ActivityIndicatorView` is an overlay modal with a spinner and no stage text or explicit exit/retry control. Existing backup consumers use a global loading flag. It is unsuitable as the entire long-running repair experience. +- Existing `validateServerBackupWorker` compares most collections by IDs/counts, decrypts records synchronously, omits local USDT wallet records from its wallet comparison, and mutates the local node object's `isConnected` field during comparison. It cannot be reused unchanged as read-only semantic verification. +- The generic watcher forks each request, so repeated repair actions can overlap. Full snapshots replace server collections; concurrent mutations need ordering/version checks, not merely button disabling. +- The candidate's repair marker currently means server acknowledgement, not post-upload readback verification. That is insufficient for the proposed final verified-success message. +- Candidate upgrade repair currently uploads automatically for opted-in accounts. Adopting the proposed comparison/prompt flow requires replacing that upgrade behavior; it is not yet done. + +## Measured local baseline + +`node tests/recovery/benchmarks/backup-cost.cjs` exercises production AES encryption/decryption with disposable synthetic records, one warm-up and three measured repetitions. Median measurements on this Mac (Node darwin/arm64), not React Native/device results: + +| Synthetic input | Encrypted payload | Encrypt | Decrypt + parse | Upload + readback at 1 Mbps each way, theoretical minimum | +| --- | --- | --- | --- | --- | +| 10 × 50 KiB records, 0.49 MiB total | 0.65 MiB | 19 ms | 14 ms | 11 s | +| 100 × 50 KiB, 4.89 MiB total | 6.52 MiB | 176 ms | 136 ms | 109 s | +| 100 × 200 KiB, 19.54 MiB total | 26.05 MiB | 718 ms | 498 ms | 437 s | +| One 19.53 MiB record | 26.04 MiB | 917 ms | 504 ms | 437 s | + +Transfer estimates exclude the initial comparison download, protocol overhead, latency, retries and server processing. They assume symmetric sustained 1 Mbps, not typical user throughput. Synthetic sizes are stress cases, not observed user backup sizes or proposed supported limits. JSON request overhead and peak memory were not measured. Local relay source accepts JSON bodies up to 50 MB; deployed proxy/server limits remain unverified. + +## Required behavior + +1. **Startup and interaction:** show the unlocked app without awaiting the upgrade comparison's network result. Schedule the check after essential startup work. Encryption/comparison must not freeze navigation or signing UI. Proposed native target: no backup-induced JavaScript-thread task longer than 50 ms; measure on the slowest supported device, including a single large wallet. Yielding between records alone does not solve large-record stalls. +2. **Progress:** display the real current stage: checking, preparing, uploading, verifying. Proposed response target: visible feedback within 250 ms of the user starting repair. Show explanatory slow-operation status after 10 seconds. Do not invent a percent, remaining time, or completion while bytes/server work are unobserved. +3. **No trapped waiting screen:** allow users to leave the progress view from the outset and expose the operation status from the existing backup area. Leaving the view must not claim that an upload has been undone. OS background suspension/app termination must leave durable pending/unverified state; do not promise continued execution in the background. +4. **Bounded stalls:** implement transport-level cancellation/deadlines, not only a UI timer. Proposed stall threshold: 30 seconds without genuine transfer or stage progress. A healthy large transfer may take minutes, so a blanket 30-second total timeout is inappropriate. Establish a bounded overall attempt budget and supported payload ceiling from native and deployed-backend measurements before release; do not guess those limits from the Mac benchmark. +5. **Ambiguous outcomes and retries:** timeout/cancellation after upload may mean the server already accepted it. Mark the outcome unverified, re-read before retrying, and never treat a retrieval error as mismatch or empty backup. Use bounded retry/backoff with manual retry available; no tight loops or repeated prompts at every unlock. +6. **Correct comparison:** compare canonical recovery-relevant plaintext locally after decryption, not randomized ciphertext, volatile balances or live connection flags. Include all supported wallet types/networks and associated recovery material. Preserve server-only/conflicting records pending explicit reconciliation. Comparison must not mutate live Realm objects or alter keys. +7. **Consistent verification:** freeze/version the intended snapshot; coalesce repair work per account and coordinate full/incremental writes. New wallet activity during repair must remain pending until included and verified. Only a readback matching the intended complete recovery data can produce verified success or permanently clear the reminder. +8. **Privacy and consent:** preserve Assisted Server Backup opt-out. Report diagnostic sizes, durations, stage and error category only; no Recovery Key, private keys, plaintext backup, wallet IDs or addresses in telemetry/logs. Do not add new analytics as part of this review. +9. **Accessibility:** use existing Keeper components; stage/failure text must remain readable at small widths and enlarged system text sizes. Controls remain reachable, progress changes are accessible, and a spinner is not the only status signal. + +## Native acceptance matrix / current status + +**Completed locally:** native small-fixture upgrade and clean recovery on Android/iOS, Android repair/readback, iOS backend failure/retry. These are development/simulator results. + +**Remaining native matrix:** large/single-large-record datasets; many labels/signers; both networks and USDT; normal network, 1 Mbps network, high latency, no response, offline/reconnect; background/foreground, OS kill/relaunch, screen navigation, account switch, repeated taps, and wallet mutation during upload. Measure UI stalls, end-to-end stage durations, peak memory, bytes and retry count. Verify bounded stalled-state exit and uncertain-outcome readback, and that matching backups produce no repair prompt. + +**Infrastructure update:** the shared Docker VM still has storage errors. A separate `keeper-recovery-qa` Colima profile now runs a healthy disposable MongoDB replica set and local relay; database/HTTP contract tests pass. Native performance acceptance is still pending. The Mac benchmark is a finding, not an NFR pass; keep the candidate in Now until applicable release gates pass. + +## Current desktop stress follow-up + +The real production AES/comparison path under Node 22, with mocked Realm/transport, repaired 10 wallets + 10 keys in 487 ms, 100 + 100 in 3,866 ms and 1,000 + 1,000 in 35,314 ms. Comparison took 189 / 2,050 / 20,145 ms; maximum event-loop delays were 36 / 37 / 91 ms on a busy Mac. The largest case exceeds the proposed 50 ms task target even here. This is not native NFR acceptance. Evidence: now-candidate-2026-09-29/evidence/backup-performance.json. diff --git a/openspec/changes/fix-recovery-backup-freshness/proposal.md b/openspec/changes/fix-recovery-backup-freshness/proposal.md new file mode 100644 index 0000000000..c19dfb9f77 --- /dev/null +++ b/openspec/changes/fix-recovery-backup-freshness/proposal.md @@ -0,0 +1,16 @@ +# Recovery Key: keep Assisted Server Backup current + +## Why +T-060 reproduced a later-created Testnet Mobile Wallet missing from an acknowledged server backup and from recovery with the correct Recovery Key. The enabled-backup gate skipped incremental writes when no full backup was pending. Permanent loss of wallet keys or funds was not established. + +## What changes +- Enabled backups execute incremental mutations, including wallets not yet persisted to Realm. Pending historical repair does not suppress them. +- Upgraded opted-in accounts check the server against local recovery content without automatically replacing the backup. A mismatch prompts the user through Notifications and More → Backup and Recovery → Assisted Server Backup. +- Back Up Now checks again, prepares the snapshot, uploads and verifies by readback. Matching backups require no upload. Retrieval failure is not treated as an empty backup. +- Show real stages, slow-operation explanation, an immediate Back action, and explicit conflict/unverified states. Preserve per-account status across restart; active spinner state is transient. +- Serialize app, vault, label and deletion writes per account; coalesce repeated repair actions and invalidate completion when local mutations arrive. +- Bound stalled requests and overall attempts; chunk AES work while preserving the existing backup format. Never truncate large records. +- Use the current Recovery Key confirmation reducer after successful recovery. Fix three existing UI branches that enabled Assisted Server Backup or displayed success after server-backup failure. + +## Status +Implemented locally in the isolated `keeper-recovery-t060` checkout. Not committed, integrated, deployed or release-approved. Native QA and relay concurrency/label-write limitations remain release gates; see validation.md. Existing backups missing data require the original device to still hold it; this change does not reconstruct arbitrarily missing wallets. diff --git a/openspec/changes/fix-recovery-backup-freshness/specs/assisted-server-backup/spec.md b/openspec/changes/fix-recovery-backup-freshness/specs/assisted-server-backup/spec.md new file mode 100644 index 0000000000..6503d510c2 --- /dev/null +++ b/openspec/changes/fix-recovery-backup-freshness/specs/assisted-server-backup/spec.md @@ -0,0 +1,65 @@ +## MODIFIED Requirements + +### Requirement: Enabled Assisted Server Backup includes subsequent mutations +The app SHALL upload enabled incremental backup mutations even when no full backup is pending. It SHALL preserve explicit relay rejection semantics and pending repair after transport failure. + +#### Scenario: A wallet is created after the initial backup +- **WHEN** an opted-in user creates a Bitcoin wallet on either network after backing up +- **THEN** its encrypted incremental payload is sent before local creation completes +- **AND** a pending historical repair does not skip the new wallet's write + +#### Scenario: Backup is disabled +- **WHEN** the user creates or changes a wallet with Assisted Server Backup disabled +- **THEN** the automatic writer and upgrade check make no backup upload + +## ADDED Requirements + +### Requirement: Upgrade repair requires comparison and user action +The app SHALL compare local recovery content with a decrypted server snapshot for an opted-in unverified account. Retrieval or decryption errors SHALL NOT be interpreted as a mismatch or empty backup. + +#### Scenario: Old backup omits local data +- **WHEN** comparison detects recovery data present locally but missing from the backup +- **THEN** show a mismatch and Back Up Now through the existing backup area +- **AND** do not replace the backup until the user starts repair + +#### Scenario: Backups match +- **WHEN** canonical recovery content matches +- **THEN** mark that account verified without uploading + +#### Scenario: Server-only data or conflicting recovery identity +- **WHEN** comparison detects server-only records, different keys or address counters ahead of this device +- **THEN** preserve the server backup and show the conflict state without upload + +### Requirement: Completion means verified recovery content +The app SHALL read back the upload and compare it with the intended snapshot and current local data. It SHALL invalidate completion if a local backup mutation occurs during the operation. A server acknowledgement alone SHALL NOT mark repair complete. + +#### Scenario: An upload response is lost +- **WHEN** the request fails after it may have reached the server +- **THEN** attempt readback within the deadline +- **AND** report verified only if the recovery data matches, otherwise leave unverified + +#### Scenario: Another local wallet is created during repair +- **WHEN** a mutation arrives during full backup +- **THEN** serialize the incremental write and keep repair unverified until a subsequent check includes it + +#### Scenario: The app restarts or changes accounts +- **WHEN** an operation is interrupted or the active account changes +- **THEN** retain unverified state for the original account without clearing another account's pending status + +### Requirement: Repair has bounded, accessible progress +The repair screen SHALL expose real checking, preparing, uploading and verifying stages, a slow-operation explanation after ten seconds, and a Back action throughout. It SHALL NOT claim background execution or rollback on navigation. Stalled transport SHALL be aborted, repeated repair actions coalesced, and oversized records rejected without truncation. + +#### Scenario: The server stops responding +- **WHEN** no upload/download bytes advance for thirty seconds +- **THEN** abort the transport and return an unverified state with manual checking available + +#### Scenario: The user leaves the waiting screen +- **WHEN** Back is selected during repair +- **THEN** navigation remains available and operation status can be revisited in Assisted Server Backup + +### Requirement: Failed server backup is not shown as success +Existing backup screens SHALL NOT enable Assisted Server Backup or show server-backup success following backupAllFailure. Physical Recovery Key confirmation remains a separate state. + +#### Scenario: Server backup fails after Recovery Key confirmation +- **WHEN** the user confirmed the Recovery Key but server verification fails +- **THEN** retain the physical confirmation and show backup-not-verified feedback without falsely enabling the server backup setting diff --git a/openspec/changes/fix-recovery-backup-freshness/tasks.md b/openspec/changes/fix-recovery-backup-freshness/tasks.md new file mode 100644 index 0000000000..94fd60121a --- /dev/null +++ b/openspec/changes/fix-recovery-backup-freshness/tasks.md @@ -0,0 +1,27 @@ +## Implementation +- [x] Fix enabled incremental backup gate and preserve explicit subscription rejection. +- [x] Check upgraded opted-in accounts without an unsolicited replacement upload. +- [x] Implement comparison, prompt, repair stages, readback verification and manual retry. +- [x] Include both Bitcoin networks, USDT, keys, vaults, labels and nodes; protect detected server-only/conflicting data. +- [x] Persist account-specific verification and interruption state separately from transient running state. +- [x] Serialize local backup writes, coalesce repair requests and invalidate completion during mutations/account switch. +- [x] Add transport abort, stall/deadline bounds, slow status, leave-screen affordance and bounded/chunked crypto work. +- [x] Fix recovered Recovery Key confirmation and existing backup UI failure-as-success branches. + +## Verification +- [x] Run targeted production-code recovery/repair/transport/state/UI feedback tests with disposable adapters. +- [x] Run the existing Recovery Key migration/status suite. +- [x] Bundle Android JavaScript and inspect baseline versus candidate TypeScript diagnostics. +- [x] Lint all new modules/screen with the missing Jest plugin supplied to the existing ESLint configuration. +- [x] Bundle iOS JavaScript. +- [x] Run native Android/iOS upgrade and clean recovery using disposable data against a working backend. Final consent follow-up is recorded separately in validation.md. +- [ ] Measure small-screen and increased-text layout, crypto frame time, memory, throughput, foreground/background and kill/restart. +- [ ] Validate deployed request-size limits and approve/tune provisional record, transfer and deadline bounds. + +## Release gates +- [x] Implement and test revision-checked atomic repair on the paired relay. +- [x] Protect repaired backups from legacy full replacement, including transaction retries and unchanged-ciphertext races. +- [ ] Validate production transaction topology and relay-first deployment. +- [x] Fix same-ID label-content upserts in the paired relay candidate; verify against real MongoDB and HTTP. +- [ ] Resolve ordinary CLI lint's inherited Jest environment/plugin error. +- [ ] Review and integrate only after applicable checks pass; commit/push/release require established owner approval. diff --git a/openspec/changes/fix-recovery-backup-freshness/validation.md b/openspec/changes/fix-recovery-backup-freshness/validation.md new file mode 100644 index 0000000000..b2db3ee842 --- /dev/null +++ b/openspec/changes/fix-recovery-backup-freshness/validation.md @@ -0,0 +1,36 @@ +# Recovery candidate validation — 29 September 2026 + +Local, uncommitted public source at `/Users/devaccount/Workspace/keeper-recovery-t060`, based on `7fdd502702a8aa149f5a3ad87aac7a274a61e6e8`. Paired relay source at `/Users/devaccount/Workspace/keeper-recovery-relay`, based on `5551550d7ba0e84ce239a7f154f34613081c5c20`. No production deployment, commit, push or store submission has occurred. T-060 remains Now. + +## Automated evidence + +- 162 Node recovery tests pass against production functions and real AES with disposable storage/transport adapters. Includes wallet/key lifecycle, exact key identity, shared registration updates, USDT metadata, manual coin choices, explicit last-node deletion, strict restore, repair races, readback, failures and transport limits. +- 20 related Jest tests pass: 10 Recovery Key migration/status, 6 Version History and 4 PIN. +- 32 relay tests pass against a disposable real MongoDB replica set, including atomic repair, rollback, ownership, archive state, node replacement, old/new writer races and HTTP failure responses. TypeScript compilation passes under Node 22. +- 3 paired real HTTP tests pass using production encryption and comparison. +- Release tooling: 11 Node plus 7 Python tests pass. Existing source version consistency passes at 2.5.16 / Android 623 / iOS 616; those counters cannot be reused for a new store upload. + +## Native evidence and final follow-up + +Fresh Android development and iOS Debug simulator builds installed over existing disposable QA apps. Android mismatch → Back Up Now → Backup verified passed. Clean native restoration of the same encrypted backup passed on both platforms: one hot wallet, seven signer records and two vault records (active and archived). Native key cards include Mobile Key and five imported Seed Keys; the Recovery Key signer is intentionally hidden. Android retained wallet/key data on upgrade. iOS retained the wallet and showed Recovered Wallet in Version History. PIN creation, mismatch/correction and login checks passed where recorded. + +An actual local database outage produced a retryable iOS recovery failure; the same Recovery Key succeeded after the database restarted. This is evidence for that failure/retry, not the full native interruption matrix. + +A subsequent iOS check found that recovery's transient `seedConfirmed=true` signal could be replayed by the Recovery Key backup page and enable Assisted Server Backup. Recovery now clears this transient signal while retaining persistent confirmed Recovery Key status/history. A regression exercises recovery and the actual health-check effect; 162 tests pass. The refreshed Android QA artifact is 2.5.17-qa.3 (626), signature-verified and installed as an upgrade. Final-source clean iOS recovery passed again; opening Recovery Key retained confirmed status while Assisted Server Backup remained off. Evidence: ios-recovered-consent-preserved.log and evidence/ios-final-recovery.log. + +Evidence root: `/Users/devaccount/Workspace/keeper-local-dev/now-candidate-2026-09-29`. Artifact manifests identify exact source hashes and the distinction between embedded Android JS and iOS Metro JS. Earlier APK results must not be represented as final post-follow-up binaries. + +## Compatibility and recovery guarantees + +Successful revisioned repair marks the backup as requiring the new full-write contract. Legacy full replacement checks that marker inside the transaction, including retries, and returns 409 after repair. Before repair it rejects omissions and preserves archive state. Incremental routes remain compatible; stale same-ID writes by older clients are not universally prevented, and the comparison must detect subsequent differences. + +All recovery records are decoded and checked before persistence. Failed Realm writes and regenerated Recovery Key identity mismatches prevent success. Local Realm writes remain sequential; a failed attempt can leave partial records without completing recovery. Native interrupted-recovery acceptance remains open. + +## Remaining gates + +- Native large/single-large-record performance, enlarged text/accessibility, interruption/relaunch and slow-network acceptance; desktop stress results are not phone performance evidence. +- Production transaction topology, endpoint authorization/request limits and relay-first rollout. +- Final clean reviewed source, refreshed store history/counters, production signed artifacts and artifact-level upgrade/smoke checks. +- Whole-repository lint/typechecking retain inherited failures. Focused new modules were linted via the installed nested Jest plugin; no repository-wide clean pass is claimed. + +No physical tester onboarding or automation programme is required by this candidate; that work is deferred separately by the owner. diff --git a/package.json b/package.json index 6d67e4c205..072f11b411 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "hexa_keeper", - "version": "2.5.16", + "version": "2.5.17", "private": true, "scripts": { "ios": "npx react-native run-ios --scheme=hexa_keeper_dev --simulator 'iPhone 16' ", @@ -28,7 +28,8 @@ "release android prod": "cd android && ENVFILE=.env.production bundle exec fastlane live --env production", "release ios dev": "cd ios && ENVFILE=.env bundle exec fastlane dev --env dev", "release ios prod": "cd ios && ENVFILE=.env.production bundle exec fastlane live --env production", - "maestro:test:regression": "maestro test flows/regression.yaml" + "maestro:test:regression": "maestro test flows/regression.yaml", + "test:recovery": "node --test tests/recovery/*.test.cjs" }, "dependencies": { "@bitcoinerlab/miniscript": "1.4.0", diff --git a/release/README.md b/release/README.md index 2554527896..ea88ab6702 100644 --- a/release/README.md +++ b/release/README.md @@ -1,8 +1,8 @@ # Release version verification -**Current milestone:** T-053 maintenance candidate, authorised 28 September 2026: finish the prepared Ask Keeper navigation correction and regression-test existing Dust/Testnet behaviour. Version 2.5.15 remains untouched. Physical-device follow-up is optional; simulator coverage must be labelled accurately. +**Current milestone:** Now candidate 2.5.17: Recovery Key backup comparison/repair and complete data restore, Version History, PIN handling, and the privately reviewed timelock prevention change. Keep advisory details and private fixtures out of public release material. -`version.json` selects candidate 2.5.16, Android 623 and iOS 616. Authenticated Google Play tracks/uploaded bundles and App Store Connect upload history were checked on 28 September: Android highest 622 and iOS highest 615, both marketing version 2.5.15. The iOS distribution page shows 2.5.15 Ready for Distribution with no newer iOS draft. Keep private investigation and diagnostics outside public release material. +`version.json` selects 2.5.17, Android 624 and iOS 617. Authenticated Google Play evidence on 29 September shows highest uploaded Android code 623; App Store Connect shows latest iOS upload 2.5.16 (616), Complete, and the production version Ready for Distribution. Refresh store baselines again before upload. The paired Recovery relay is deployed and its live encrypted repair/readback smoke passed. Independent security acceptance and final physical testing remain pending. 1. Read all uploaded builds and release tracks for Android package `io.hexawallet.bitcoinkeeper`, and uploaded iOS builds for `io.hexawallet.keeper`. Preserve the timestamp and evidence outside the repository. Refresh before upload; preflight accepts evidence at most 24 hours old. 2. Update the manifest with the highest known marketing version and highest uploaded build per platform. Select one greater marketing version for both stores and greater platform build counters. Check unpublished App Store version records as well. diff --git a/release/version.json b/release/version.json index c8c776defd..9a4e7ebc01 100644 --- a/release/version.json +++ b/release/version.json @@ -1,21 +1,21 @@ { - "version": "2.5.16", - "androidVersionCode": 623, - "iosBuildNumber": 616, + "version": "2.5.17", + "androidVersionCode": 624, + "iosBuildNumber": 617, "baselines": { "android": { "applicationId": "io.hexawallet.bitcoinkeeper", - "verifiedAt": "2026-09-28T12:51:36.298748+00:00", - "highestUploadedBuild": 622, - "highestMarketingVersion": "2.5.15", - "productionRelease": "622 (2.5.15)" + "verifiedAt": "2026-09-29T10:32:44Z", + "highestUploadedBuild": 623, + "highestMarketingVersion": "2.5.16", + "productionRelease": "623 (2.5.16)" }, "ios": { "applicationId": "io.hexawallet.keeper", - "verifiedAt": "2026-09-28T12:51:36.298975+00:00", - "highestUploadedBuild": 615, - "highestMarketingVersion": "2.5.15", - "productionRelease": "615 (2.5.15)" + "verifiedAt": "2026-09-29T12:45:29.673796+00:00", + "highestUploadedBuild": 616, + "highestMarketingVersion": "2.5.16", + "productionRelease": "616 (2.5.16)" } } } diff --git a/src/components/Backup/BackupHealthCheckList.tsx b/src/components/Backup/BackupHealthCheckList.tsx index 1c6fa4f475..f59e845194 100644 --- a/src/components/Backup/BackupHealthCheckList.tsx +++ b/src/components/Backup/BackupHealthCheckList.tsx @@ -105,7 +105,7 @@ function BackupHealthCheckList({ isUaiFlow }) { useEffect(() => { - if (backupAllSuccess || backupAllFailure) { + if (backupAllSuccess) { if (!automaticCloudBackup) setAsbEnabled(true); dispatch(setBackupAllSuccess(false)); dispatch(setBackupAllFailure(false)); @@ -132,6 +132,7 @@ function BackupHealthCheckList({ isUaiFlow }) { useEffect(() => { if (backupAllFailure && isFocused) { + setFailedVerificationModal(true); dispatch(setBackupAllFailure(false)); } }, [backupAllFailure]); diff --git a/src/components/Modal/PasscodeVerify.tsx b/src/components/Modal/PasscodeVerify.tsx index 87d53ba498..246ec585c7 100644 --- a/src/components/Modal/PasscodeVerify.tsx +++ b/src/components/Modal/PasscodeVerify.tsx @@ -76,21 +76,16 @@ function PasscodeVerifyModal({ }; const onPressNumber = (text) => { - let tmpPasscode = passcode; - if (passcode.length < 4) { - if (text !== 'x') { - tmpPasscode += text; - setPasscode(tmpPasscode); - } - } - if (passcode && text === 'x') { - setPasscode(passcode.slice(0, -1)); + if (text === 'x') { + setPasscode((current) => current.slice(0, -1)); setLoginError(false); + } else { + setPasscode((current) => (current.length < 4 ? current + text : current)); } }; - const onDeletePressed = (text) => { - setPasscode(passcode.slice(0, passcode.length - 1)); + const onDeletePressed = () => { + setPasscode((current) => current.slice(0, -1)); }; const disableCTA = () => { @@ -152,19 +147,17 @@ function PasscodeVerifyModal({ )} {/* */} - {passcode.length === 4 && ( - { - setBtnDisable(true); - setLoginError(false); - attemptLogin(passcode); - }} - primaryText={primaryText ? primaryText : common.proceed} - activeOpacity={0.5} - primaryDisable={btnDisable} - fullWidth - /> - )} + { + setBtnDisable(true); + setLoginError(false); + attemptLogin(passcode); + }} + primaryText={primaryText ? primaryText : common.proceed} + activeOpacity={0.5} + primaryDisable={btnDisable || passcode.length !== 4} + fullWidth + /> {/* keyboardview start */} diff --git a/src/context/Localization/language/en.json b/src/context/Localization/language/en.json index 962b29aee2..446ba2d6cb 100644 --- a/src/context/Localization/language/en.json +++ b/src/context/Localization/language/en.json @@ -803,6 +803,7 @@ "keeperVerified": "Keeper Verified Successfully", "SeedErrorToast": "Please fill in all seed word fields", "appRecoveredSuccessfulTitle": "App Recovered Successfully!", + "recoveryIncomplete": "Recovery could not be completed. Your backup may be unavailable or some data could not be restored. Please try again.", "appRecoveredSuccessfulSubTitle": "All your wallets and data about your vault has been recovered", "appRecoveredSuccessfulDesc": "The vault keys always stay within the signing devices and therefore do not have to be recovered", "mobileKey": "Mobile Key", @@ -2217,5 +2218,49 @@ "disclaimerPoint3": "Your messages are processed securely to help Ask Keeper respond.", "disclaimerLearnMore": "Learn more about Ask Keeper", "disclaimerCta": "Got it" + }, + "recoveryBackup": { + "title": "Assisted Server Backup", + "backUp": "Back Up Now", + "check": "Check Again", + "back": "Back", + "slow": "This is taking longer than usual. A slow connection or a large backup can take more time.", + "leave": "You can leave this screen. Return here to check the result. If the app closes, check again when you reopen it.", + "disabled": { + "title": "Backup is off", + "body": "Enable Assisted Server Backup in Backup and Recovery to use this check." + }, + "checking": { + "title": "Checking your backup", + "body": "Comparing your encrypted backup with the recovery data on this device." + }, + "different": { + "title": "Your backup needs updating", + "body": "Your Assisted Server Backup differs from this device. Back up now to update it, then Keeper will check that it matches." + }, + "preparing": { + "title": "Preparing your backup", + "body": "Preparing and encrypting the recovery data on this device." + }, + "uploading": { + "title": "Updating your backup", + "body": "Sending your encrypted backup to Keeper’s servers." + }, + "verifying": { + "title": "Verifying your backup", + "body": "Reading your backup back from the server and checking that the recovery data matches." + }, + "verified": { + "title": "Backup verified", + "body": "Your Assisted Server Backup matches the recovery data checked on this device. Keep your Recovery Key safe; you still need it for recovery." + }, + "conflict": { + "title": "Backup needs review", + "body": "The server backup contains recovery data that is missing or different on this device. Keeper has not replaced it. Check your other Keeper devices before making changes, then check again." + }, + "unverified": { + "title": "Backup not verified", + "body": "Keeper could not confirm that your backup matches. Your connection may have been interrupted, the backup may be too large, or data may have changed during the check. Check again before assuming the backup is complete." } -} \ No newline at end of file +} +} diff --git a/src/context/Localization/language/es.json b/src/context/Localization/language/es.json index a59ae5abda..8e95a7f68a 100644 --- a/src/context/Localization/language/es.json +++ b/src/context/Localization/language/es.json @@ -791,6 +791,7 @@ "keeperVerified": "Keeper Verified Successfully", "SeedErrorToast": "Please fill in all seed word fields", "appRecoveredSuccessfulTitle": "App Recovered Successfully!", + "recoveryIncomplete": "No se pudo completar la recuperación. Es posible que la copia de seguridad no esté disponible o que algunos datos no se hayan podido restaurar. Inténtalo de nuevo.", "appRecoveredSuccessfulSubTitle": "All your wallets and data about your vault has been recovered", "appRecoveredSuccessfulDesc": "The vault keys always stay within the signing devices and therefore do not have to be recovered", "mobileKey": "Mobile Key", @@ -2198,5 +2199,49 @@ "disclaimerPoint3": "Your messages are processed securely to help Ask Keeper respond.", "disclaimerLearnMore": "Learn more about Ask Keeper", "disclaimerCta": "Got it" + }, + "recoveryBackup": { + "title": "Copia de seguridad asistida", + "backUp": "Guardar ahora", + "check": "Volver a comprobar", + "back": "Atrás", + "slow": "Está tardando más de lo habitual. Una conexión lenta o una copia grande puede necesitar más tiempo.", + "leave": "Puedes salir de esta pantalla y volver para consultar el resultado. Si la aplicación se cierra, comprueba de nuevo al abrirla.", + "disabled": { + "title": "Copia desactivada", + "body": "Activa la copia de seguridad asistida en Copia de seguridad y recuperación para usar esta comprobación." + }, + "checking": { + "title": "Comprobando la copia", + "body": "Comparando tu copia cifrada con los datos de recuperación de este dispositivo." + }, + "different": { + "title": "Actualiza tu copia", + "body": "Tu copia de seguridad asistida difiere de este dispositivo. Guarda una copia ahora y Keeper comprobará que coincide." + }, + "preparing": { + "title": "Preparando la copia", + "body": "Preparando y cifrando los datos de recuperación de este dispositivo." + }, + "uploading": { + "title": "Actualizando la copia", + "body": "Enviando tu copia cifrada a los servidores de Keeper." + }, + "verifying": { + "title": "Verificando la copia", + "body": "Leyendo la copia del servidor y comprobando que los datos de recuperación coinciden." + }, + "verified": { + "title": "Copia verificada", + "body": "Tu copia de seguridad asistida coincide con los datos de recuperación comprobados en este dispositivo. Guarda tu clave de recuperación; sigue siendo necesaria para recuperar." + }, + "conflict": { + "title": "Revisa la copia", + "body": "El servidor contiene datos de recuperación que faltan o son diferentes en este dispositivo. Keeper no los ha reemplazado. Revisa tus otros dispositivos Keeper antes de hacer cambios y vuelve a comprobar." + }, + "unverified": { + "title": "Copia sin verificar", + "body": "Keeper no pudo confirmar que tu copia coincida. La conexión puede haberse interrumpido, la copia puede ser demasiado grande o los datos pueden haber cambiado. Comprueba de nuevo antes de dar la copia por completa." } -} \ No newline at end of file +} +} diff --git a/src/hooks/useSettingKeeper.tsx b/src/hooks/useSettingKeeper.tsx index d9242915b3..ae020d4cc8 100644 --- a/src/hooks/useSettingKeeper.tsx +++ b/src/hooks/useSettingKeeper.tsx @@ -262,7 +262,7 @@ export const useSettingKeeper = () => { title: settings.assistedServerBackup, description: settings.assistedServerBackupSubtitle, icon: , - onPress: debouncedAutomaticBackupToggle, + onPress: () => navigation.navigate('AssistedBackupStatus'), rightIcon: isOnL2Above ? ( ) : ( diff --git a/src/hooks/useUSDTWallets.ts b/src/hooks/useUSDTWallets.ts index dba1d6e868..12578e9f78 100644 --- a/src/hooks/useUSDTWallets.ts +++ b/src/hooks/useUSDTWallets.ts @@ -23,6 +23,9 @@ import { useDispatch } from 'react-redux'; import { useAppSelector } from 'src/store/hooks'; import { updateAppImage } from 'src/store/sagaActions/bhr'; import Relay from 'src/services/backend/Relay'; +import { canonical, recoveryContent } from 'src/services/backup/image'; +import { markBackupMutation } from 'src/services/backup/transport'; +import { invalidateBackupRepair, setPendingAllBackup } from 'src/store/reducers/bhr'; export interface UseUSDTWalletsOptions { getAll?: boolean; @@ -171,18 +174,45 @@ export const useUSDTWallets = (options: UseUSDTWalletsOptions = {}): UseUSDTWall /** * Update a wallet in the database */ - const updateWallet = useCallback(async (wallet: USDTWallet): Promise => { - try { - const { id, ...walletUpdateData } = wallet; - await dbManager.updateObjectById(RealmSchema.USDTWallet, wallet.id, walletUpdateData); // Remove the primary key 'id' from the update object to avoid Realm primary key change error + const updateWallet = useCallback( + async (wallet: USDTWallet): Promise => { + let recoveryChanged = false; + try { + const previous = dbManager.getObjectById(RealmSchema.USDTWallet, wallet.id); + if (!previous) throw new Error('Wallet not found'); + const previousWallet = previous.toJSON ? previous.toJSON() : previous; + recoveryChanged = + canonical(recoveryContent('wallets', previousWallet)) !== + canonical(recoveryContent('wallets', wallet)); + const { id, ...walletUpdateData } = wallet; + const updated = await dbManager.updateObjectById( + RealmSchema.USDTWallet, + id, + walletUpdateData + ); // Realm updates must omit the primary key. + if (!updated) throw new Error('Failed to update wallet'); - return true; - } catch (err) { - setError(err.message || 'Failed to update wallet'); - captureError(err); - return false; - } - }, []); + // Metadata and visibility must survive recovery. Balance/account refreshes + // are rebuildable caches and must not cause an upload on every sync. + if (recoveryChanged) + dispatch(updateAppImage({ wallets: [wallet], signers: null, updateNodes: false })); + + return true; + } catch (err) { + // The database helper can fail after partially writing fields. Do not + // upload the requested state or leave a previous verification trusted. + if (recoveryChanged) { + markBackupMutation(appId); + dispatch(invalidateBackupRepair(appId)); + dispatch(setPendingAllBackup(true)); + } + setError(err.message || 'Failed to update wallet'); + captureError(err); + return false; + } + }, + [appId, dispatch] + ); /** * Syncs a single wallet account status with latest data diff --git a/src/navigation/Navigator.tsx b/src/navigation/Navigator.tsx index d5ecfc5bbc..dd9327ee96 100644 --- a/src/navigation/Navigator.tsx +++ b/src/navigation/Navigator.tsx @@ -1,3 +1,4 @@ +import AssistedBackupStatus from 'src/screens/AppSettings/AssistedBackupStatus'; import { DefaultTheme, NavigationContainer } from '@react-navigation/native'; import React, { useContext, useRef } from 'react'; import { createNativeStackNavigator } from '@react-navigation/native-stack'; @@ -263,6 +264,7 @@ function AppStack() { + diff --git a/src/navigation/types.ts b/src/navigation/types.ts index 3f71fa0806..1bf30f6349 100644 --- a/src/navigation/types.ts +++ b/src/navigation/types.ts @@ -155,6 +155,7 @@ export type AppStackParams = { }; ConfirmWalletDetails: undefined; AppBackupSettings: undefined; + AssistedBackupStatus: undefined; BuyBitcoin: undefined; DeleteKeys: undefined; HandleFile: undefined; diff --git a/src/screens/AppSettings/AssistedBackupStatus.tsx b/src/screens/AppSettings/AssistedBackupStatus.tsx new file mode 100644 index 0000000000..cdc54fc004 --- /dev/null +++ b/src/screens/AppSettings/AssistedBackupStatus.tsx @@ -0,0 +1,95 @@ +import React, { useContext, useEffect, useState } from 'react'; +import { ActivityIndicator, StyleSheet } from 'react-native'; +import { Box, ScrollView, useColorMode } from '@gluestack-ui/themed-native-base'; +import { useQuery } from '@realm/react'; +import { useNavigation } from '@react-navigation/native'; +import { RealmSchema } from 'src/storage/realm/enum'; +import { useAppDispatch, useAppSelector } from 'src/store/hooks'; +import { checkBackupFreshness, repairBackup } from 'src/store/sagaActions/bhr'; +import { LocalizationContext } from 'src/context/Localization/LocContext'; +import ScreenWrapper from 'src/components/ScreenWrapper'; +import WalletHeader from 'src/components/WalletHeader'; +import Buttons from 'src/components/Buttons'; +import Text from 'src/components/KeeperText'; +import { hp } from 'src/constants/responsive'; + +export default function AssistedBackupStatus() { + const { colorMode } = useColorMode(); + const navigation = useNavigation(); + const dispatch = useAppDispatch(); + const { recoveryBackup: copy } = useContext(LocalizationContext).translations; + const app = useQuery(RealmSchema.KeeperApp)[0] as any; + const id = app?.id as string; + const { + automaticCloudBackup, + backupRepairStateByAppId = {}, + backupRepairRunningByAppId = {}, + } = useAppSelector((state) => state.bhr); + const phase = backupRepairStateByAppId[id]; + const running = !!backupRepairRunningByAppId[id]; + const [slow, setSlow] = useState(false); + useEffect(() => { + if (automaticCloudBackup) dispatch(checkBackupFreshness()); + }, [id]); + useEffect(() => { + setSlow(false); + if (!running) return undefined; + const timer = setTimeout(() => setSlow(true), 10_000); + return () => clearTimeout(timer); + }, [running, phase]); + const state = !automaticCloudBackup + ? 'disabled' + : !running && ['checking', 'preparing', 'uploading', 'verifying'].includes(phase) + ? 'unverified' + : phase || 'unverified'; + return ( + + + + + + {copy[state].title} + + {running && } + + {copy[state].body} + + {slow && ( + + {copy.slow} + + )} + {running && ( + + {copy.leave} + + )} + + {!running && automaticCloudBackup && ( + + dispatch(state === 'different' ? repairBackup() : checkBackupFreshness()) + } + /> + )} + + navigation.goBack()} + /> + + + + ); +} +const styles = StyleSheet.create({ + content: { paddingVertical: hp(24) }, + card: { padding: 20, borderRadius: 16, marginBottom: hp(24) }, + title: { fontSize: 18, marginBottom: hp(16) }, + body: { fontSize: 14, marginTop: hp(12) }, +}); diff --git a/src/screens/BackupWallet/ViewRecoveryKeyScreen.tsx b/src/screens/BackupWallet/ViewRecoveryKeyScreen.tsx index 7ede2e2cde..f5a9ef668b 100644 --- a/src/screens/BackupWallet/ViewRecoveryKeyScreen.tsx +++ b/src/screens/BackupWallet/ViewRecoveryKeyScreen.tsx @@ -33,7 +33,7 @@ type KeeperApp = { export const ViewRecoveryKeyScreen = ({ navigation }) => { const { colorMode } = useColorMode(); - const { home: homeTxt, common, BackupWallet: backupTxt } = useContext(LocalizationContext).translations; + const { home: homeTxt, common, BackupWallet: backupTxt, recoveryBackup } = useContext(LocalizationContext).translations; const { primaryMnemonic, id: appId } = useQuery(RealmSchema.KeeperApp).map( getJSONFromRealmObject )[0] as KeeperApp; @@ -47,7 +47,7 @@ export const ViewRecoveryKeyScreen = ({ navigation }) => { const { showToast } = useToastMessage(); useEffect(() => { - if (backupAllSuccess || backupAllFailure) { + if (backupAllSuccess) { dispatch(setBackupAllSuccess(false)); dispatch(setBackupAllFailure(false)); dispatch(setAutomaticCloudBackup(true)); @@ -55,6 +55,11 @@ export const ViewRecoveryKeyScreen = ({ navigation }) => { showToast(backupTxt.recoveryKeyBackedUpSuccessfully, ); navigation.goBack(); } + if (backupAllFailure) { + dispatch(setBackupAllFailure(false)); + setLoader(false); + showToast(recoveryBackup.unverified.title); + } }, [backupAllSuccess, backupAllFailure]); const SeedCard = ({ item, index }: { item; index }) => { diff --git a/src/screens/Home/Notifications/NotificationsCenter.tsx b/src/screens/Home/Notifications/NotificationsCenter.tsx index 26abf067b2..efcca03f94 100644 --- a/src/screens/Home/Notifications/NotificationsCenter.tsx +++ b/src/screens/Home/Notifications/NotificationsCenter.tsx @@ -35,7 +35,6 @@ import { useAppSelector } from 'src/store/hooks'; import { cachedTxSnapshot } from 'src/store/reducers/cachedTxn'; import UAIView from '../components/UAIView'; import { setStateFromSnapshot } from 'src/store/reducers/send_and_receive'; -import { backupAllSignersAndVaults } from 'src/store/sagaActions/bhr'; import { LocalizationContext } from 'src/context/Localization/LocContext'; import ThemedSvg from 'src/components/ThemedSvg.tsx/ThemedSvg'; import Fonts from 'src/constants/Fonts'; @@ -298,7 +297,7 @@ const Card = memo(({ uai }: CardProps) => { text: common.View, cta: () => { dispatch(uaiActioned({ uaiId: uai.id, action: false })); - dispatch(backupAllSignersAndVaults()); + navigtaion.navigate('AssistedBackupStatus'); }, }, }, diff --git a/src/screens/LoginScreen/CreatePin.tsx b/src/screens/LoginScreen/CreatePin.tsx index 853336524a..cfa93415a5 100644 --- a/src/screens/LoginScreen/CreatePin.tsx +++ b/src/screens/LoginScreen/CreatePin.tsx @@ -16,7 +16,6 @@ import BiometricIcon from 'src/assets/images/biometric-icon.svg'; import { changeLoginMethod, storeCreds, switchCredsChanged } from 'src/store/sagaActions/login'; import KeeperModal from 'src/components/KeeperModal'; import { setIsInitialLogin } from 'src/store/reducers/login'; -import { throttle } from 'src/utils/utilities'; import Buttons from 'src/components/Buttons'; import PinDotView from 'src/components/AppPinInput/PinDotView'; import ReactNativeBiometrics from 'react-native-biometrics'; @@ -83,7 +82,7 @@ export default function CreatePin(props) { } }, [credsChanged]); - const onPressNumber = throttle((text) => { + const onPressNumber = (text) => { setPinState((currentState) => { const currentLength = currentState.value.length; @@ -104,9 +103,9 @@ export default function CreatePin(props) { return currentState; }); - }, 300); + }; - const onDeletePressed = throttle(() => { + const onDeletePressed = () => { setPinState((currentState) => { const currentLength = currentState.value.length; @@ -123,7 +122,7 @@ export default function CreatePin(props) { stage: currentLength <= 5 ? PasscodeStages.CREATE : PasscodeStages.CONFIRM, }; }); - }, 300); + }; const handleNext = () => { dispatch(setIsInitialLogin(true)); diff --git a/src/screens/LoginScreen/Login.tsx b/src/screens/LoginScreen/Login.tsx index 40353f905d..862b39879d 100644 --- a/src/screens/LoginScreen/Login.tsx +++ b/src/screens/LoginScreen/Login.tsx @@ -209,21 +209,16 @@ function LoginScreen({ navigation, route }) { }; const onPressNumber = (text) => { - let tmpPasscode = passcode; - if (passcode.length < 4) { - if (text !== 'x') { - tmpPasscode += text; - setPasscode(tmpPasscode); - } - } - if (passcode && text === 'x') { - setPasscode(passcode.slice(0, -1)); + if (text === 'x') { + setPasscode((current) => current.slice(0, -1)); setLoginError(false); + } else { + setPasscode((current) => (current.length < 4 ? current + text : current)); } }; - const onDeletePressed = (text) => { - setPasscode(passcode.slice(0, passcode.length - 1)); + const onDeletePressed = () => { + setPasscode((current) => current.slice(0, -1)); }; useEffect(() => { diff --git a/src/screens/Recovery/EnterSeedScreen.tsx b/src/screens/Recovery/EnterSeedScreen.tsx index ecace068df..b133caf23a 100644 --- a/src/screens/Recovery/EnterSeedScreen.tsx +++ b/src/screens/Recovery/EnterSeedScreen.tsx @@ -1,5 +1,12 @@ import * as bip39 from 'bip39'; -import { Box, Input, Pressable, ScrollView, View, useColorMode } from '@gluestack-ui/themed-native-base'; +import { + Box, + Input, + Pressable, + ScrollView, + View, + useColorMode, +} from '@gluestack-ui/themed-native-base'; import { Keyboard, KeyboardAvoidingView, Platform, StyleSheet } from 'react-native'; import React, { useCallback, useContext, useEffect, useRef, useState } from 'react'; import { hp, wp } from 'src/constants/responsive'; @@ -130,7 +137,13 @@ function EnterSeedScreen({ route, navigation }) { dispatch(setAppImageError('')); setRecoveryLoading(false); setShowNetworkModal(true); - } else openInvalidSeedsModal(); + } else if (appImageError === 'Invalid mnemonic') { + openInvalidSeedsModal(); + } else { + setRecoveryLoading(false); + showToast(seed.recoveryIncomplete, ); + dispatch(setAppImageError('')); + } } }, [appImageError]); diff --git a/src/screens/SeedScreens/ExportSeedScreen.tsx b/src/screens/SeedScreens/ExportSeedScreen.tsx index bb2ec226a5..447e6c1741 100644 --- a/src/screens/SeedScreens/ExportSeedScreen.tsx +++ b/src/screens/SeedScreens/ExportSeedScreen.tsx @@ -107,13 +107,17 @@ function ExportSeedScreen({ route, navigation }) { }, [backupMethod]); useEffect(() => { - if (backupAllSuccess || backupAllFailure) { + if (backupAllSuccess) { if (!automaticCloudBackup) setAsbEnabled(true); dispatch(setBackupAllSuccess(false)); dispatch(setBackupAllFailure(false)); dispatch(setAutomaticCloudBackup(true)); setBackupSuccessModal(true); } + if (backupAllFailure) { + dispatch(setBackupAllFailure(false)); + showToast(translations.recoveryBackup.unverified.title); + } }, [backupAllSuccess, backupAllFailure]); diff --git a/src/screens/Vault/ResetEmergencyKey.tsx b/src/screens/Vault/ResetEmergencyKey.tsx index 838e055488..9233607be2 100644 --- a/src/screens/Vault/ResetEmergencyKey.tsx +++ b/src/screens/Vault/ResetEmergencyKey.tsx @@ -33,7 +33,7 @@ import { isVaultUsingBlockHeightTimelock } from 'src/services/wallets/factories/ function ResetEmergencyKey({ route }) { const { inheritanceKeys = [], - initialTimelockDuration = 0, + initialTimelockDuration, vault, }: { inheritanceKeys; initialTimelockDuration; vault: Vault } = route.params; const { colorMode } = useColorMode(); @@ -257,7 +257,7 @@ function ResetEmergencyKey({ route }) { key: signer, duration: selectedOptions[getKeyUID(signer)]?.label, }))} - initialTimelockDuration={initialTimelockDuration ?? 0} + initialTimelockDuration={initialTimelockDuration} currentBlockHeight={currentBlockHeight} miniscriptTypes={vault.scheme.miniscriptScheme.usedMiniscriptTypes} /> diff --git a/src/screens/Vault/ResetInheritanceKey.tsx b/src/screens/Vault/ResetInheritanceKey.tsx index 72e0b55046..23f7fb67a7 100644 --- a/src/screens/Vault/ResetInheritanceKey.tsx +++ b/src/screens/Vault/ResetInheritanceKey.tsx @@ -236,6 +236,7 @@ function ResetInheritanceKey({ route }) { CommonActions.navigate({ name: 'ResetEmergencyKey', params: { + initialTimelockDuration, inheritanceKeys: inheritanceSigners.map((signer) => ({ key: signer, duration: selectedOptions[getKeyUID(signer)]?.label, @@ -265,7 +266,7 @@ function ResetInheritanceKey({ route }) { key: signer, duration: selectedOptions[getKeyUID(signer)]?.label, }))} - initialTimelockDuration={initialTimelockDuration ?? 0} + initialTimelockDuration={initialTimelockDuration} currentBlockHeight={currentBlockHeight} miniscriptTypes={vault.scheme.miniscriptScheme.usedMiniscriptTypes} /> diff --git a/src/screens/Vault/VaultMigrationController.tsx b/src/screens/Vault/VaultMigrationController.tsx index bfb688e194..66736912d3 100644 --- a/src/screens/Vault/VaultMigrationController.tsx +++ b/src/screens/Vault/VaultMigrationController.tsx @@ -1,3 +1,4 @@ +import { resolveInitialTimelock } from 'src/services/wallets/operations/miniscript/initialTimelock'; import { CommonActions, useNavigation, useFocusEffect } from '@react-navigation/native'; import React, { useEffect, useState, useRef, useCallback, useContext } from 'react'; import { @@ -375,7 +376,11 @@ function VaultMigrationController({ return; } - let initialTimelock = 0; + const initialTimelock = resolveInitialTimelock( + miniscriptTypes, + initialTimelockDuration, + (label) => getTimelockDuration(label, bitcoinNetworkType) + ); const multisigScriptType = MultisigScriptType.MINISCRIPT_MULTISIG; // let currentSyncedBlockHeight = currentBlockHeight; @@ -396,14 +401,6 @@ function VaultMigrationController({ // } // } - if (initialTimelockDuration) { - initialTimelock = getTimelockDuration(initialTimelockDuration, bitcoinNetworkType); - if (!initialTimelock) { - showToast('Failed to determine initial timelock duration', ); - return; - } - } - const inheritanceSignerWithTimelocks = []; const emergencySignerWithTimelocks = []; diff --git a/src/services/backend/Relay.ts b/src/services/backend/Relay.ts index 8a35b6ae26..d9a33502ad 100644 --- a/src/services/backend/Relay.ts +++ b/src/services/backend/Relay.ts @@ -1,3 +1,4 @@ +import { backupPost } from '../backup/transport'; import { NetworkType } from 'src/services/wallets/enums'; import { SubScriptionPlan } from 'src/models/interfaces/Subscription'; import { @@ -106,16 +107,25 @@ export default class Relay { return res.data || res.json; }; - public static updateAppImage = async ( - appImage - ): Promise<{ + public static updateAppImage = async (appImage: { + appId: string; + publicId?: string; + walletsObject?: Record; + signersObject?: Record; + networkType?: string; + subscription?: string; + version?: string; + nodes?: string[]; + // Required to clear nodes: older clients send [] for unrelated updates. + replaceNodes?: boolean; + }): Promise<{ status: string; updated: boolean; err?: string; message?: string; }> => { try { - const res = await RestClient.post(`${RELAY}updateAppImage`, appImage); + const res = await backupPost(`${RELAY}updateAppImage`, appImage); const data = res.data || res.json; return data; } catch (err) { @@ -150,7 +160,7 @@ export default class Relay { message?: string; }> => { try { - const res = await RestClient.post(`${RELAY}deleteAppImageEntity`, entityList); + const res = await backupPost(`${RELAY}deleteAppImageEntity`, entityList); const data = res.data || res.json; return data; } catch (err) { @@ -168,7 +178,7 @@ export default class Relay { message?: string; }> => { try { - const res = await RestClient.post(`${RELAY}deleteVaults`, entityList); + const res = await backupPost(`${RELAY}deleteVaults`, entityList); const data = res.data || res.json; return data; } catch (err) { @@ -188,7 +198,7 @@ export default class Relay { message?: string; }> => { try { - const res = await RestClient.post(`${RELAY}updateVaultImage`, vaultData); + const res = await backupPost(`${RELAY}updateVaultImage`, vaultData); const data = res.data || res.json; return data; } catch (err) { @@ -275,7 +285,7 @@ export default class Relay { updated: boolean; }> => { try { - const res = (await RestClient.post(`${RELAY}modifyLabels`, { + const res = (await backupPost(`${RELAY}modifyLabels`, { appId, addLabels, deleteLabels, @@ -429,6 +439,7 @@ export default class Relay { public static backupAllSignersAndVaults = async ( allData ): Promise<{ + updated?: boolean; status?: number; data?: { updated: boolean; @@ -437,7 +448,7 @@ export default class Relay { message?: string; }> => { try { - const res = await RestClient.post(`${RELAY}backupAllSignersAndVaults`, allData); + const res = await backupPost(`${RELAY}backupAllSignersAndVaults`, allData); const data = res.data || res.json; return data; } catch (err) { @@ -482,7 +493,7 @@ export default class Relay { message?: string; }> => { try { - const res = await RestClient.post(`${RELAY}deleteBackup`, body); + const res = await backupPost(`${RELAY}deleteBackup`, body); const data = res.data || res.json; return data; } catch (err) { @@ -804,4 +815,3 @@ export default class Relay { } }; } - diff --git a/src/services/backup/image.ts b/src/services/backup/image.ts new file mode 100644 index 0000000000..1ef9342ddd --- /dev/null +++ b/src/services/backup/image.ts @@ -0,0 +1,269 @@ +/* eslint-disable no-await-in-loop -- Sequential work bounds memory and yields to the mobile UI. */ +import cryptoJS from 'crypto-js'; +import { decrypt, encrypt } from 'src/utils/service-utilities/encryption'; + +export type BackupImage = Record< + 'wallets' | 'signers' | 'vaults' | 'nodes' | 'labels', + Record +>; +export type BackupComparison = 'matched' | 'different' | 'conflict'; +export const pauseBackup = () => + new Promise((resolve) => { + setTimeout(resolve, 0); + }); +// Conservative local guard below the relay's 50 MB body limit. Native capacity +// testing is still required; never truncate a backup to satisfy these guards. +export const MAX_RECORD_BYTES = 8 * 1024 * 1024; +export const MAX_BACKUP_BYTES = 32 * 1024 * 1024; + +export function canonical(value: any): string { + if (value === undefined) return 'null'; + if (value === null || typeof value !== 'object') return JSON.stringify(value); + if (value instanceof Date) return JSON.stringify(value.toISOString()); + if (Array.isArray(value)) return `[${value.map(canonical).join(',')}]`; + return `{${Object.keys(value) + .filter((key) => value[key] !== undefined) + .sort() + .map((key) => `${JSON.stringify(key)}:${canonical(value[key])}`) + .join(',')}}`; +} + +export function recoveryContent(kind: keyof BackupImage, record: any) { + if (kind === 'nodes') return { ...record, isConnected: false }; + if (kind !== 'wallets' && kind !== 'vaults') return record; + // Chain caches can be rebuilt. Keep keys, derivation, counters, policies, + // archive state and user metadata in the comparison. + const result = { ...record }; + delete result.receivingAddress; + delete result.accountStatus; + const specs = { ...record.specs }; + // Manual coin-control choices cannot be reconstructed from the chain. Compare + // them by outpoint, independent of confirmation state or UTXO/cache ordering. + // The encrypted record still retains the original UTXOs for existing restore + // and refresh code to reapply each choice after syncing with the node. + const manualCoinChoices: Record = {}; + for (const utxo of [ + ...(record.specs?.confirmedUTXOs || []), + ...(record.specs?.unconfirmedUTXOs || []), + ]) { + if (utxo.isManualOverride) manualCoinChoices[`${utxo.txId}:${utxo.vout}`] = utxo.spendability; + } + for (const field of [ + 'balances', + 'balance', + 'transactions', + 'confirmedUTXOs', + 'unconfirmedUTXOs', + 'addresses', + 'addressPubs', + 'receivingAddress', + 'hasNewUpdates', + 'lastSynched', + ]) + delete specs[field]; + specs.manualCoinChoices = manualCoinChoices; + return { ...result, specs }; +} + +function identity(kind: keyof BackupImage, record: any) { + if (kind === 'wallets') + return { + networkType: record.networkType, + type: record.type, + entityKind: record.entityKind, + derivationDetails: record.derivationDetails, + scriptType: record.scriptType, + xpub: record.specs?.xpub, + xpriv: record.specs?.xpriv, + address: record.specs?.address, + privateKey: record.specs?.privateKey, + }; + if (kind === 'vaults') + return { + networkType: record.networkType, + scheme: record.scheme, + signers: record.signers, + scriptType: record.scriptType, + }; + if (kind === 'signers') + return { + masterFingerprint: record.masterFingerprint, + signerXpubs: record.signerXpubs, + networkType: record.networkType, + type: record.type, + }; + return null; +} + +export async function compareImages( + local: BackupImage, + remote: BackupImage, + checkpoint = () => {} +): Promise { + let result: BackupComparison = 'matched'; + for (const kind of Object.keys(local) as (keyof BackupImage)[]) { + for (const [id, record] of Object.entries(remote[kind])) { + checkpoint(); + const current = local[kind][id]; + if (!current || canonical(identity(kind, current)) !== canonical(identity(kind, record))) + return 'conflict'; + for (const counter of [ + 'nextFreeAddressIndex', + 'nextFreeChangeAddressIndex', + 'totalExternalAddresses', + ]) { + if ((record.specs?.[counter] || 0) > (current.specs?.[counter] || 0)) return 'conflict'; + } + } + for (const [id, record] of Object.entries(local[kind])) { + checkpoint(); + if ( + !remote[kind][id] || + canonical(recoveryContent(kind, record)) !== + canonical(recoveryContent(kind, remote[kind][id])) + ) + result = 'different'; + await pauseBackup(); + } + } + return result; +} + +// Preserve the existing CryptoJS/OpenSSL wire format. Chunk the expensive AES +// work so a large wallet does not monopolize the JS thread for the whole cipher. +export async function encryptRecord(key: string, record: any): Promise { + const text = JSON.stringify(record); + if (text.length * 3 > MAX_RECORD_BYTES) throw new Error('Backup record exceeds supported size'); + if (text.length < 16_384) { + await pauseBackup(); + return encrypt(key, text); + } + const salt = cryptoJS.lib.WordArray.random(8); + const derived = cryptoJS.kdf.OpenSSL.execute(key, 8, 4, salt); + const cipher = cryptoJS.algo.AES.createEncryptor(derived.key, { iv: derived.iv }); + const output = cryptoJS.lib.WordArray.create(); + for (let offset = 0; offset < text.length; ) { + let end = Math.min(offset + 16_384, text.length); + if (end < text.length && /[\uD800-\uDBFF]/.test(text[end - 1])) end--; + output.concat(cipher.process(text.slice(offset, end))); + offset = end; + await pauseBackup(); + } + output.concat(cipher.finalize()); + return cryptoJS.format.OpenSSL.stringify( + cryptoJS.lib.CipherParams.create({ ciphertext: output, salt }) + ); +} + +export async function decryptRecord(key: string, value: unknown) { + if (typeof value !== 'string' || value.length > MAX_RECORD_BYTES * 2) + throw new Error('Invalid backup record'); + // Parsing/format decoding still require native performance validation at the + // supported size ceiling. Yield before and after each record. + await pauseBackup(); + let text: string; + if (value.length < 16_384) text = decrypt(key, value); + else { + const params = cryptoJS.format.OpenSSL.parse(value); + if (!params.salt) throw new Error('Missing backup encryption salt'); + const derived = cryptoJS.kdf.OpenSSL.execute(key, 8, 4, params.salt); + const decipher = cryptoJS.algo.AES.createDecryptor(derived.key, { iv: derived.iv }); + const output = cryptoJS.lib.WordArray.create(); + const data = params.ciphertext; + for (let offset = 0; offset < data.words.length; offset += 4096) { + const bytes = Math.min(4096 * 4, data.sigBytes - offset * 4); + output.concat( + decipher.process( + cryptoJS.lib.WordArray.create(data.words.slice(offset, offset + 4096), bytes) + ) + ); + await pauseBackup(); + } + output.concat(decipher.finalize()); + text = output.toString(cryptoJS.enc.Utf8); + } + const record = JSON.parse(text); + if (!record || typeof record !== 'object' || Array.isArray(record)) + throw new Error('Invalid backup record'); + await pauseBackup(); + return record; +} + +export async function decodeImage( + key: string, + response: any, + appId: string, + checkpoint = () => {} +): Promise { + const app = response?.appImage; + if ( + !app || + app.appId !== appId || + !app.wallets || + !app.signers || + !Array.isArray(app.nodes) || + !Array.isArray(response.allVaultImages) || + !Array.isArray(response.labels) + ) + throw new Error('Incomplete backup response'); + const checkReferences = (expected: unknown, actual: unknown[]) => { + if (expected === undefined) return; // Legacy backups can omit reference lists. + if ( + !Array.isArray(expected) || + expected.length !== actual.length || + new Set(expected).size !== expected.length || + new Set(actual).size !== actual.length || + actual.some((id) => typeof id !== 'string' || !expected.includes(id)) + ) + throw new Error('Incomplete backup references'); + }; + checkReferences( + app.vaults, + response.allVaultImages.map((record) => record?.vaultId) + ); + checkReferences( + app.labels, + response.labels.map((record) => record?.id) + ); + const image: BackupImage = { wallets: {}, signers: {}, vaults: {}, nodes: {}, labels: {} }; + let totalBytes = 0; + const decode = async (value: unknown) => { + checkpoint(); + if (typeof value !== 'string') throw new Error('Invalid backup record'); + totalBytes += value.length; + if (totalBytes > MAX_BACKUP_BYTES) throw new Error('Backup exceeds supported size'); + return decryptRecord(key, value); + }; + const add = (kind: keyof BackupImage, id: string, record: any) => { + if (!id || image[kind][id]) throw new Error('Duplicate or unidentified backup record'); + image[kind][id] = record; + }; + for (const value of Object.values(app.wallets)) { + const record = await decode(value); + add('wallets', record.id, record); + } + for (const [id, value] of Object.entries(app.signers)) { + const record = await decode(value); + add('signers', id, record); + } + for (const value of response.allVaultImages) { + const record = await decode(value?.vault); + if (value.vaultId !== undefined && value.vaultId !== record.id) + throw new Error('Vault backup identity mismatch'); + add('vaults', record.id, record); + } + for (const value of app.nodes) { + const record = await decode(value); + add('nodes', record.id || canonical(recoveryContent('nodes', record)), record); + } + for (const value of response.labels) { + const record = await decode(value?.content); + add('labels', record.id, record); + } + // A partial relay response must never look like an empty backup. + if (Array.isArray(app.vaults) && app.vaults.length !== Object.keys(image.vaults).length) + throw new Error('Incomplete vault backup'); + if (Array.isArray(app.labels) && app.labels.length !== Object.keys(image.labels).length) + throw new Error('Incomplete label backup'); + return image; +} diff --git a/src/services/backup/repair.ts b/src/services/backup/repair.ts new file mode 100644 index 0000000000..afb8d67947 --- /dev/null +++ b/src/services/backup/repair.ts @@ -0,0 +1,238 @@ +/* eslint-disable no-await-in-loop -- Serialize backup records to bound memory and yield to the UI. */ +import dbManager from 'src/storage/realm/dbManager'; +import { RealmSchema } from 'src/storage/realm/enum'; +import config from 'src/utils/service-utilities/config'; +import { generateEncryptionKey, hash256 } from 'src/utils/service-utilities/encryption'; +import { + getKeyUID, + sanitizeSeedKeyForBackup, + sanitizeVaultSignersForSeedKeyBackup, +} from 'src/utils/utilities'; +import { + BACKUP_DEADLINE_MS, + backupRevision, + boundedBackupPost, + withBackupSession, +} from './transport'; +import { + BackupImage, + canonical, + compareImages, + decodeImage, + encryptRecord, + MAX_BACKUP_BYTES, + MAX_RECORD_BYTES, + pauseBackup, + recoveryContent, +} from './image'; + +export type RepairPhase = + | 'checking' + | 'different' + | 'preparing' + | 'uploading' + | 'verifying' + | 'verified' + | 'conflict' + | 'unverified'; +const inFlight = new Map< + string, + { + repair: boolean; + promise: Promise; + listeners: Set<(phase: RepairPhase) => void>; + phase?: RepairPhase; + } +>(); +const clone = (value: any) => JSON.parse(JSON.stringify(value)); + +export async function captureBackupImage(checkpoint = () => {}): Promise { + const image: BackupImage = { wallets: {}, signers: {}, vaults: {}, nodes: {}, labels: {} }; + const sources: [keyof BackupImage, RealmSchema][] = [ + ['wallets', RealmSchema.Wallet], + ['wallets', RealmSchema.USDTWallet], + ['signers', RealmSchema.Signer], + ['vaults', RealmSchema.Vault], + ['nodes', RealmSchema.NodeConnect], + ['labels', RealmSchema.Tags], + ]; + for (const [kind, schema] of sources) { + const records = dbManager.getObjectByIndex(schema, null, true) as any; + for (const value of records) { + checkpoint(); + const text = JSON.stringify(value); + if (text.length * 3 > MAX_RECORD_BYTES) + throw new Error('Backup record exceeds supported size'); + const raw = JSON.parse(text); + const record = + kind === 'signers' + ? sanitizeSeedKeyForBackup(raw) + : kind === 'vaults' + ? sanitizeVaultSignersForSeedKeyBackup(raw) + : kind === 'nodes' + ? { ...raw, isConnected: false } + : raw; + const id = + kind === 'signers' + ? getKeyUID(record) + : record.id || canonical(recoveryContent(kind, record)); + if (image[kind][id]) throw new Error('Duplicate local backup record'); + image[kind][id] = record; + await pauseBackup(); + } + } + return image; +} + +async function encodeImage(app: any, image: BackupImage, checkpoint = () => {}) { + const key = generateEncryptionKey(app.primarySeed); + const payload = { + appId: app.id, + publicId: app.publicId, + subscription: JSON.stringify(app.subscription), + networkType: app.networkType, + version: app.version, + walletObject: {}, + signersObject: {}, + vaultObject: {}, + nodes: [], + labels: [], + }; + let size = 0; + for (const kind of Object.keys(image) as (keyof BackupImage)[]) { + for (const [id, record] of Object.entries(image[kind])) { + checkpoint(); + const encrypted = await encryptRecord(key, record); + size += encrypted.length; + if (size > MAX_BACKUP_BYTES) throw new Error('Backup exceeds supported size'); + if (kind === 'wallets') payload.walletObject[id] = encrypted; + if (kind === 'signers') payload.signersObject[id] = encrypted; + if (kind === 'vaults') + payload.vaultObject[id] = { + vaultId: id, + vault: encrypted, + isArchived: !!record.archived, + scheme: record.scheme, + signersData: record.signers.map((signer) => ({ + signerId: getKeyUID(signer), + xfpHash: hash256(signer.masterFingerprint), + })), + }; + if (kind === 'nodes') payload.nodes.push(encrypted); + if (kind === 'labels') + payload.labels.push({ id: hash256(hash256(key + id)), content: encrypted }); + } + } + return payload; +} + +// Read-only checks never upload. A repair rechecks immediately before writing. +// Session ownership extends through readback; incremental writes wait, and their +// revision invalidates a concurrent repair's completion claim. +export function inspectBackup( + appId: string, + repair: boolean, + onPhase: (phase: RepairPhase) => void +): Promise { + const running = inFlight.get(appId); + if (running) { + if (repair && !running.repair) + return running.promise.then(() => inspectBackup(appId, true, onPhase)); + running.listeners.add(onPhase); + if (running.phase) onPhase(running.phase); + return running.promise; + } + const listeners = new Set([onPhase]); + const notify = (phase: RepairPhase) => { + const active = inFlight.get(appId); + if (active) active.phase = phase; + listeners.forEach((listener) => listener(phase)); + }; + const result = withBackupSession(appId, async () => { + const deadline = Date.now() + BACKUP_DEADLINE_MS; + const revision = backupRevision(appId); + const assertCurrent = () => { + if ( + Date.now() >= deadline || + (dbManager.getObjectByIndex(RealmSchema.KeeperApp) as any)?.id !== appId || + backupRevision(appId) !== revision + ) + throw new Error('Backup changed during check'); + }; + try { + notify('checking'); + const app = clone(dbManager.getObjectByIndex(RealmSchema.KeeperApp)); + assertCurrent(); + const key = generateEncryptionKey(app.primarySeed); + const local = await captureBackupImage(assertCurrent); + assertCurrent(); + let expectedRevision: string; + const read = async () => { + const response = await boundedBackupPost( + `${config.RELAY}getBackupSnapshot`, + { appId }, + deadline + ); + assertCurrent(); + if (!/^[a-f0-9]{64}$/.test(response.data?.revision)) + throw new Error('Backup snapshot revision unavailable'); + expectedRevision = response.data.revision; + return decodeImage(key, response.data, appId, assertCurrent); + }; + const remote = await read(); + const comparison = await compareImages(local, remote, assertCurrent); + assertCurrent(); + if (comparison === 'conflict') { + notify('conflict'); + return 'conflict'; + } + if (comparison === 'different' && !repair) { + notify('different'); + return 'different'; + } + if (comparison === 'different') { + notify('preparing'); + const payload = await encodeImage(app, local, assertCurrent); + assertCurrent(); + // Local Realm may change without a relay call (e.g. address generation). + if ( + (await compareImages(local, await captureBackupImage(assertCurrent), assertCurrent)) !== + 'matched' + ) + throw new Error('Local backup changed'); + assertCurrent(); + notify('uploading'); + try { + const response = await boundedBackupPost( + `${config.RELAY}repairAppBackup`, + { ...payload, expectedRevision }, + deadline + ); + if (response.data?.updated !== true) throw new Error('Backup rejected'); + } catch { + // A lost response is not proof the upload failed. Always read back. + } + notify('verifying'); + if ((await compareImages(local, await read(), assertCurrent)) !== 'matched') + throw new Error('Backup not verified'); + } + if ( + (await compareImages(local, await captureBackupImage(assertCurrent), assertCurrent)) !== + 'matched' + ) + throw new Error('Local backup changed'); + assertCurrent(); + notify('verified'); + return 'verified'; + } catch { + notify('unverified'); + return 'unverified'; + } + }); + inFlight.set(appId, { repair, promise: result, listeners }); + const clean = () => { + if (inFlight.get(appId)?.promise === result) inFlight.delete(appId); + }; + result.then(clean, clean); + return result; +} diff --git a/src/services/backup/restore.ts b/src/services/backup/restore.ts new file mode 100644 index 0000000000..80a7b8bdf7 --- /dev/null +++ b/src/services/backup/restore.ts @@ -0,0 +1,27 @@ +import { decodeImage } from './image'; + +// Decode every record before changing Realm. Older backups may omit collections +// introduced later; an unreadable response or a missing referenced record is not +// an empty backup and must never trigger successful partial recovery. +export async function prepareRecoveryImage( + encryptionKey: string, + appId: string, + appImage: any, + allVaultImages: any[], + labels: any[] +) { + if (!appImage || appImage.appId !== appId) throw new Error('Recovery data unavailable'); + return decodeImage( + encryptionKey, + { + appImage: { + ...appImage, + signers: appImage.signers === undefined ? {} : appImage.signers, + nodes: appImage.nodes === undefined ? [] : appImage.nodes, + }, + allVaultImages, + labels, + }, + appId + ); +} diff --git a/src/services/backup/transport.ts b/src/services/backup/transport.ts new file mode 100644 index 0000000000..21339041ec --- /dev/null +++ b/src/services/backup/transport.ts @@ -0,0 +1,71 @@ +import RestClient from '../rest/RestClient'; + +// One queue per account covers repair, incremental writes, labels and deletion. +const queues = new Map>(); +const revisions = new Map(); +export const backupRevision = (id: string) => revisions.get(id) || 0; +export const markBackupMutation = (id: string) => revisions.set(id, backupRevision(id) + 1); +export const BACKUP_STALL_MS = 30_000; +export const BACKUP_DEADLINE_MS = 10 * 60_000; + +export function withBackupSession(id: string, work: () => Promise): Promise { + const previous = queues.get(id) || Promise.resolve(); + const result = previous.catch(() => undefined).then(work); + queues.set(id, result); + const clean = () => { + if (queues.get(id) === result) queues.delete(id); + }; + result.then(clean, clean); + return result; +} + +// Abort the actual transport, not just the saga waiting on it. A stalled upload +// has an unknown outcome; callers must read back before deciding to upload again. +export async function boundedBackupPost( + path: string, + body: object, + deadline = Date.now() + BACKUP_DEADLINE_MS +) { + const controller = new AbortController(); + let idle: ReturnType; + let uploaded = 0; + let downloaded = 0; + const resetIdle = () => { + clearTimeout(idle); + idle = setTimeout(() => controller.abort(), BACKUP_STALL_MS); + }; + const remaining = deadline - Date.now(); + if (remaining <= 0) throw new Error('Backup deadline exceeded'); + const total = setTimeout(() => controller.abort(), remaining); + resetIdle(); + try { + return await RestClient.post(path, body, undefined, { + signal: controller.signal, + onUploadProgress: ({ loaded }) => { + if (loaded > uploaded) { + uploaded = loaded; + resetIdle(); + } + }, + onDownloadProgress: ({ loaded }) => { + if (loaded > downloaded) { + downloaded = loaded; + resetIdle(); + } + }, + }); + } catch { + // Do not propagate Axios errors: they can contain request bodies and headers. + throw new Error('Backup request could not be completed'); + } finally { + clearTimeout(idle); + clearTimeout(total); + } +} + +export function backupPost(path: string, body: any) { + const appId = body.appId || body.appID; + if (!appId) return Promise.reject(new Error('Missing backup account')); + markBackupMutation(appId); + return withBackupSession(appId, () => boundedBackupPost(path, body)); +} diff --git a/src/services/rest/RestClient.ts b/src/services/rest/RestClient.ts index 456df553f4..e505ec76f1 100644 --- a/src/services/rest/RestClient.ts +++ b/src/services/rest/RestClient.ts @@ -1,4 +1,4 @@ -import axios, { AxiosResponse } from 'axios'; +import axios, { AxiosRequestConfig, AxiosResponse } from 'axios'; import DeviceInfo from 'react-native-device-info'; import { Platform } from 'react-native'; @@ -61,8 +61,14 @@ class RestClient { return RestClient.torStatus; } - async post(path: string, body: object, headers?: object): Promise { + async post( + path: string, + body: object, + headers?: object, + options?: AxiosRequestConfig + ): Promise { return axios.post(path, body, { + ...options, headers: { ...RestClient.headers, ...headers, diff --git a/src/services/wallets/operations/miniscript/initialTimelock.ts b/src/services/wallets/operations/miniscript/initialTimelock.ts new file mode 100644 index 0000000000..ab742181de --- /dev/null +++ b/src/services/wallets/operations/miniscript/initialTimelock.ts @@ -0,0 +1,20 @@ +import { MiniscriptTypes } from 'src/services/wallets/enums'; + +/** Reject a lost duration before generating any replacement policy. */ +export function resolveInitialTimelock( + types: MiniscriptTypes[], + selectedDuration: string | number | null | undefined, + durationForLabel: (label: string) => number | undefined +): number { + const required = types.includes(MiniscriptTypes.TIMELOCKED); + const absent = selectedDuration == null || selectedDuration === '' || selectedDuration === 0; + if (absent && !required) return 0; + if (absent || typeof selectedDuration !== 'string') { + throw new Error('Failed to determine initial timelock duration'); + } + const duration = durationForLabel(selectedDuration); + if (typeof duration !== 'number' || !Number.isSafeInteger(duration) || duration <= 0) { + throw new Error('Failed to determine initial timelock duration'); + } + return duration; +} diff --git a/src/store/reducers/bhr.ts b/src/store/reducers/bhr.ts index fa6788d752..dcd6edd455 100644 --- a/src/store/reducers/bhr.ts +++ b/src/store/reducers/bhr.ts @@ -1,3 +1,4 @@ +import { RepairPhase } from 'src/services/backup/repair'; import { createSlice, PayloadAction } from '@reduxjs/toolkit'; import { BackupType, homeToastMessageType } from 'src/models/enums/BHR'; import _ from 'lodash'; @@ -43,6 +44,9 @@ const initialState: { backupAllFailure: boolean; pendingAllBackup: boolean; + backupRepairCompletedByAppId: Record; + backupRepairStateByAppId: Record; + backupRepairRunningByAppId: Record; automaticCloudBackup: boolean; @@ -84,6 +88,9 @@ const initialState: { backupAllSuccess: false, pendingAllBackup: false, + backupRepairCompletedByAppId: {}, + backupRepairStateByAppId: {}, + backupRepairRunningByAppId: {}, automaticCloudBackup: false, @@ -226,6 +233,19 @@ const bhrSlice = createSlice({ state.pendingAllBackup = action.payload; }, + setBackupRepairState: (state, action: PayloadAction<{ appId: string; phase: RepairPhase }>) => { + const { appId, phase } = action.payload; + (state.backupRepairStateByAppId ??= {})[appId] = phase; + (state.backupRepairCompletedByAppId ??= {})[appId] = phase === 'verified'; + }, + setBackupRepairRunning: (state, action: PayloadAction<{ appId: string; running: boolean }>) => { + (state.backupRepairRunningByAppId ??= {})[action.payload.appId] = action.payload.running; + }, + invalidateBackupRepair: (state, action: PayloadAction) => { + (state.backupRepairCompletedByAppId ??= {})[action.payload] = false; + if (state.backupRepairStateByAppId?.[action.payload] === 'verified') + state.backupRepairStateByAppId[action.payload] = 'unverified'; + }, setAutomaticCloudBackup: (state, action: PayloadAction) => { state.automaticCloudBackup = action.payload; }, @@ -281,6 +301,9 @@ export const { setBackupAllFailure, setPendingAllBackup, + setBackupRepairState, + setBackupRepairRunning, + invalidateBackupRepair, setAutomaticCloudBackup, @@ -294,6 +317,7 @@ const bhrPersistConfig = { key: 'bhr', storage: reduxStorage, blacklist: [ + 'backupRepairRunningByAppId', 'isBackupError', 'backupError', 'seedConfirmed', diff --git a/src/store/sagaActions/bhr.ts b/src/store/sagaActions/bhr.ts index 39e8eadfd3..24ed5515ad 100644 --- a/src/store/sagaActions/bhr.ts +++ b/src/store/sagaActions/bhr.ts @@ -106,4 +106,9 @@ export const deleteBackup = () => ({ export const validateServerBackup = (callback) => ({ type: VALIDATE_SERVER_BACKUP, callback, -}); \ No newline at end of file +}); + +export const CHECK_BACKUP_FRESHNESS = 'CHECK_BACKUP_FRESHNESS'; +export const REPAIR_BACKUP = 'REPAIR_BACKUP'; +export const checkBackupFreshness = () => ({ type: CHECK_BACKUP_FRESHNESS }); +export const repairBackup = () => ({ type: REPAIR_BACKUP }); diff --git a/src/store/sagaActions/upgrade.ts b/src/store/sagaActions/upgrade.ts index e10adaa871..d6b6a14fc7 100644 --- a/src/store/sagaActions/upgrade.ts +++ b/src/store/sagaActions/upgrade.ts @@ -2,9 +2,9 @@ export const RESET_REDUX_STORE = 'RESET_REDUX_STORE'; export const UPDATE_VERSION_HISTORY = 'UPDATE_VERSION_HISTORY'; export const MIGRATE_LABELS_329 = 'MIGRATE_LABELS_329'; -export const updateVersionHistory = (previousVersion, newVersion) => ({ +export const updateVersionHistory = (previousVersion, newVersion, isRecovery = false) => ({ type: UPDATE_VERSION_HISTORY, - payload: { previousVersion, newVersion }, + payload: { previousVersion, newVersion, isRecovery }, }); export const migrateLabelsToBip329 = () => ({ diff --git a/src/store/sagas/bhr.ts b/src/store/sagas/bhr.ts index 063d9dc263..c60ec711eb 100644 --- a/src/store/sagas/bhr.ts +++ b/src/store/sagas/bhr.ts @@ -1,13 +1,13 @@ +import { buffers, END, eventChannel } from 'redux-saga'; +import { inspectBackup, RepairPhase } from 'src/services/backup/repair'; +import { prepareRecoveryImage } from 'src/services/backup/restore'; +import { BackupImage } from 'src/services/backup/image'; +import { markBackupMutation } from 'src/services/backup/transport'; import * as bip39 from 'bip39'; import { Wallet } from 'src/services/wallets/interfaces/wallet'; -import { call, put, select } from 'redux-saga/effects'; +import { call, put, select, takeMaybe, takeEvery } from 'redux-saga/effects'; import config from 'src/utils/service-utilities/config'; -import { - decrypt, - encrypt, - generateEncryptionKey, - hash256, -} from 'src/utils/service-utilities/encryption'; +import { encrypt, generateEncryptionKey, hash256 } from 'src/utils/service-utilities/encryption'; import BIP85 from 'src/services/wallets/operations/BIP85'; import DeviceInfo from 'react-native-device-info'; import { KeeperApp } from 'src/models/interfaces/KeeperApp'; @@ -51,16 +51,20 @@ import { setBackupAllFailure, setBackupAllLoading, setBackupAllSuccess, + setBackupRepairState, + setBackupRepairRunning, + invalidateBackupRepair, setBackupLoading, setBackupType, setDeleteBackupFailure, setDeleteBackupSuccess, - setHomeToastMessage, setIsCloudBsmsBackupRequired, setPendingAllBackup, setSeedConfirmed, } from '../reducers/bhr'; import { + CHECK_BACKUP_FRESHNESS, + REPAIR_BACKUP, BACKUP_ALL_SIGNERS_AND_VAULTS, BACKUP_BSMS_ON_CLOUD, BSMS_CLOUD_HEALTH_CHECK, @@ -83,14 +87,13 @@ import { RootState } from '../store'; import { setupRecoveryKeySigningKey } from 'src/hardware/signerSetup'; import { addNewWalletsWorker, addSigningDeviceWorker, NewWalletInfo } from './wallets'; import { - areSetsEqual, getKeyUID, sanitizeSeedKeyForBackup, sanitizeVaultSignersForSeedKeyBackup, } from 'src/utils/utilities'; import NetInfo from '@react-native-community/netinfo'; import { addToUaiStackWorker, uaiActionedWorker } from './uai'; -import { addAccount, saveDefaultWalletState, setRecoveryKeyBackedUp } from '../reducers/account'; +import { addAccount, saveDefaultWalletState, setRecoveryKeyStatus } from '../reducers/account'; import { loadConciergeTickets, loadConciergeUser } from '../reducers/concierge'; import { USDTWallet } from 'src/services/wallets/factories/USDTWalletFactory'; @@ -121,7 +124,8 @@ export function* updateAppImageWorker({ const encrytedWallet = encrypt(encryptionKey, JSON.stringify(wallet)); walletsObject[wallet.id] = encrytedWallet; } - } else if (signers) { + } + if (signers) { for (const signer of signers) { const encrytedSigner = encrypt( encryptionKey, @@ -129,12 +133,12 @@ export function* updateAppImageWorker({ ); signersObject[getKeyUID(signer)] = encrytedSigner; } - } else if (updateNodes) { + } + if (updateNodes) { const nodes: NodeDetail[] = yield call(dbManager.getCollection, RealmSchema.NodeConnect); if (nodes && nodes.length > 0) { for (const index in nodes) { - const node = nodes[index]; - node.isConnected = false; + const node = { ...nodes[index], isConnected: false }; const encrytedNode = encrypt(encryptionKey, JSON.stringify(node)); nodesList.push(encrytedNode); } @@ -150,7 +154,9 @@ export function* updateAppImageWorker({ subscription: JSON.stringify(subscription), version, nodes: nodesList, + replaceNodes: !!updateNodes, }); + if (!response?.updated) yield call(setServerBackupFailed); return response; } catch (err) { console.log({ err }); @@ -186,6 +192,7 @@ export function* updateVaultImageWorker({ if (isUpdate) { const response = yield call(Relay.updateVaultImage, { isUpdate, + appId: id, vaultId: vault.id, vault: vaultEncrypted, }); @@ -353,23 +360,11 @@ function* getAppImageWorker({ payload }) { } const encryptionKey = generateEncryptionKey(primarySeed.toString('hex')); - let appImage = { appId: appID, version: null, wallets: {}, signers: {}, nodes: [] }; - let subscription = null; - let labels = []; - let allVaultImages = []; - - try { - const response = yield call(Relay.getAppImage, appID); - if (response) { - appImage = response.appImage || appImage; - subscription = response.subscription || subscription; - labels = response.labels || labels; - allVaultImages = response.allVaultImages || allVaultImages; - } - } catch (err) { - console.log('Error retrieving app image from relay:', err); - // Continue with empty defaults - } + const response = yield call(Relay.getAppImage, appID); + if (!response?.appImage) throw new Error('Recovery data unavailable'); + const { appImage } = response; + const labels = response.labels === undefined ? [] : response.labels; + const allVaultImages = response.allVaultImages === undefined ? [] : response.allVaultImages; const previousVersion = appImage.version; @@ -417,17 +412,39 @@ function* getAppImageWorker({ payload }) { instanceNum: 0, }, }; - yield call(addNewWalletsWorker, { payload: [defaultWallet] }); + const created = yield call(addNewWalletsWorker, { payload: [defaultWallet] }); + if (created !== true) throw new Error('Recovery data unavailable'); } const recoveryKeySigner = setupRecoveryKeySigningKey(primaryMnemonic); yield call(addSigningDeviceWorker, { payload: { signers: [recoveryKeySigner] } }); + const recoveredSigners: Signer[] = yield call(dbManager.getCollection, RealmSchema.Signer); + const recoveredKey = recoveredSigners.find( + (signer) => getKeyUID(signer) === getKeyUID(recoveryKeySigner) + ); + // Key creation/merging can return without throwing after a rejected write. + // Verify the required derived key material before exposing recovery success. + if ( + !recoveredKey || + !Object.values(recoveryKeySigner.signerXpubs).some((entries) => entries.length > 0) || + !Object.entries(recoveryKeySigner.signerXpubs).every(([type, entries]) => + entries.every((expected) => + recoveredKey.signerXpubs[type]?.some( + (actual) => + actual.xpub === expected.xpub && + actual.derivationPath === expected.derivationPath && + actual.xpriv === expected.xpriv + ) + ) + ) + ) + throw new Error('Recovery data unavailable'); // applying the restore upgrade sequence if required const newVersion = DeviceInfo.getVersion(); if (previousVersion && semver.lt(previousVersion, newVersion)) { console.log(`applying restore upgarde sequence - from: ${previousVersion} to ${newVersion}`); - yield call(applyUpgradeSequence, { previousVersion, newVersion }); + yield call(applyUpgradeSequence, { previousVersion, newVersion, isRecovery: true }); try { yield call(Relay.updateAppImage, { appId: appImage.appId, @@ -448,7 +465,8 @@ function* getAppImageWorker({ payload }) { yield put(uaiChecks([uaiType.SECURE_VAULT])); yield put(loadConciergeUser(null)); yield put(loadConciergeTickets([])); - yield put(setRecoveryKeyBackedUp({ appId: appID, status: true })); + yield put(setRecoveryKeyStatus({ appId: appID, status: 'confirmed' })); + yield put(setAppCreated(true)); } catch (err) { yield put(setAppImageError(err.message)); } finally { @@ -468,6 +486,14 @@ function* recoverApp( previousVersion ) { const { bitcoinNetworkType } = yield select((state: RootState) => state.settings); + const recovered: BackupImage = yield call( + prepareRecoveryImage, + encryptionKey, + appID, + appImage, + allVaultImages, + labels + ); const entropy = yield call( BIP85.bip39MnemonicToEntropy, config.BIP85_IMAGE_ENCRYPTIONKEY_DERIVATION_PATH, @@ -495,28 +521,25 @@ function* recoverApp( networkType: bitcoinNetworkType, }; - yield call(dbManager.createObject, RealmSchema.KeeperApp, app); + yield call(writeRecoveredObject, RealmSchema.KeeperApp, app); // Wallet recreation if (appImage.wallets) { - for (const [key, value] of Object.entries(appImage.wallets)) { + for (const decryptedWallet of Object.values(recovered.wallets) as Wallet[]) { try { - const decryptedWallet: Wallet = JSON.parse(decrypt(encryptionKey, value)); if (decryptedWallet.entityKind === EntityKind.USDT_WALLET) - yield call(dbManager.createObject, RealmSchema.USDTWallet, decryptedWallet); - else yield call(dbManager.createObject, RealmSchema.Wallet, decryptedWallet); + yield call(writeRecoveredObject, RealmSchema.USDTWallet, decryptedWallet); + else yield call(writeRecoveredObject, RealmSchema.Wallet, decryptedWallet); } catch (err) { - console.log('Error recovering a wallet: ', err); - continue; + throw new Error('Recovery data unavailable'); } } } // Signers recreatin if (appImage.signers) { - for (const [key, value] of Object.entries(appImage.signers)) { + for (const decrytpedSigner of Object.values(recovered.signers) as Signer[]) { try { - const decrytpedSigner: Signer = JSON.parse(decrypt(encryptionKey, value)); if (!decrytpedSigner?.id) { decrytpedSigner.id = getKeyUID(decrytpedSigner); } @@ -526,20 +549,18 @@ function* recoverApp( ? NetworkType.TESTNET : NetworkType.MAINNET; } - yield call(dbManager.createObject, RealmSchema.Signer, decrytpedSigner); + yield call(writeRecoveredObject, RealmSchema.Signer, decrytpedSigner); } catch (err) { - console.log('Error recovering a signer: ', err); - continue; + throw new Error('Recovery data unavailable'); } } } // Vault recreation if (allVaultImages.length > 0) { - for (const vaultImage of allVaultImages) { + // Legacy vaults carry the pre-key-management signer shape until migration. + for (const vault of Object.values(recovered.vaults)) { try { - const vault = JSON.parse(decrypt(encryptionKey, vaultImage.vault)); - if (semver.lt(previousVersion, KEY_MANAGEMENT_VERSION)) { if (vault?.signers?.length) { vault.signers.forEach((signer, index) => { @@ -590,31 +611,22 @@ function* recoverApp( hidden: false, signerXpubs, }; - yield call(dbManager.createObject, RealmSchema.Signer, signerObject); + yield call(writeRecoveredObject, RealmSchema.Signer, signerObject); } } - - yield call(dbManager.createObject, RealmSchema.Vault, vault); } - yield call(dbManager.createObject, RealmSchema.Vault, vault); + yield call(writeRecoveredObject, RealmSchema.Vault, vault); } catch (err) { - console.log('Error recovering a vault: ', err); - continue; + throw new Error('Recovery data unavailable'); } } } // Labels Restore if (labels) { - const restoredLabels = []; - for (const label of labels) { - try { - restoredLabels.push(JSON.parse(decrypt(encryptionKey, label.content))); - } catch { - console.log('Failed to restore label'); - } + for (const label of Object.values(recovered.labels)) { + yield call(writeRecoveredObject, RealmSchema.Tags, label); } - yield call(dbManager.createObjectBulk, RealmSchema.Tags, restoredLabels); } if (appImage.nodes) { @@ -625,21 +637,24 @@ function* recoverApp( ); for (const node of existingNodes) { if (node && node.id) { - yield call(dbManager.deleteObjectById, RealmSchema.NodeConnect, node.id.toString()); + const deleted = yield call( + dbManager.deleteObjectById, + RealmSchema.NodeConnect, + node.id.toString() + ); + if (deleted !== true) throw new Error('Recovery data unavailable'); } } - for (const node of appImage.nodes) { + for (const decryptedNode of Object.values(recovered.nodes) as NodeDetail[]) { try { - const decryptedNode = JSON.parse(decrypt(encryptionKey, node)); if (!decryptedNode?.networkType) { decryptedNode.networkType = config.isDevMode() ? NetworkType.TESTNET : NetworkType.MAINNET; } - yield call(dbManager.createObject, RealmSchema.NodeConnect, decryptedNode); + yield call(writeRecoveredObject, RealmSchema.NodeConnect, decryptedNode); } catch (err) { - console.log('Error recovering a node: ', err); - continue; + throw new Error('Recovery data unavailable'); } } } @@ -650,35 +665,46 @@ function* recoverApp( const firstNode = savedNodes[0]; firstNode.isConnected = true; - yield call( + const connected = yield call( dbManager.updateObjectById, RealmSchema.NodeConnect, firstNode.id.toString(), firstNode ); + if (connected !== true) throw new Error('Recovery data unavailable'); } // seed confirm for recovery - yield call(dbManager.createObject, RealmSchema.BackupHistory, { + yield call(writeRecoveredObject, RealmSchema.BackupHistory, { title: BackupAction.SEED_BACKUP_CONFIRMED, date: moment().unix(), confirmed: true, subtitle: 'Recovered using backup phrase', }); - yield put(setSeedConfirmed(true)); + // Recovery proves possession of the Recovery Key, but is not consent to + // enable Assisted Server Backup. This transient flag triggers a new upload + // when the health-check screen mounts; persistent key status is set by the + // completed recovery worker instead. + yield put(setSeedConfirmed(false)); yield put(setBackupType(BackupType.SEED)); yield put(uaiActioned({ uaiType: uaiType.RECOVERY_PHRASE_HEALTH_CHECK, action: true })); // create/add restored object for version - yield call(dbManager.createObject, RealmSchema.VersionHistory, { + yield call(writeRecoveredObject, RealmSchema.VersionHistory, { version: `${DeviceInfo.getVersion()}(${DeviceInfo.getBuildNumber()})`, date: new Date().toString(), - title: 'Restored version', + title: 'Recovered Wallet', }); yield put(setAppId(appID)); - yield put(setAppCreated(true)); +} + +function* writeRecoveredObject(schema: RealmSchema, record: any) { + // Realm's adapter returns false/undefined on a rejected write instead of + // throwing. Never count that record as recovered or report partial success. + const written = yield call(dbManager.createObject, schema, record); + if (written !== true) throw new Error('Recovery data unavailable'); } function* healthCheckSatutsUpdateWorker({ @@ -946,120 +972,78 @@ export const deleteAppImageEntityWatcher = createWatcher( DELETE_APP_IMAGE_ENTITY ); -function* backupAllSignersAndVaultsWorker() { - yield put(setBackupAllSuccess(false)); - yield put(setBackupAllFailure(false)); - yield put(setBackupAllLoading(true)); - try { - // clear backup failure notification - const uaiCollection = dbManager.getObjectByField( - RealmSchema.UAI, - uaiType.SERVER_BACKUP_FAILURE, - 'uaiType' +function* runBackupInspection(repair: boolean) { + const { id }: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); + yield put(setBackupRepairRunning({ appId: id, running: true })); + let finalPhase: RepairPhase = 'unverified'; + const updates = eventChannel((emit) => { + inspectBackup(id, repair, (phase) => emit(phase)).then( + () => emit(END), + () => emit(END) ); - for (const uai of uaiCollection) { - if (uai.uaiType === uaiType.SERVER_BACKUP_FAILURE) { - yield call(uaiActionedWorker, { - payload: { uaiId: uai.id, action: false }, - }); - } - } - - const { primarySeed, id, publicId, subscription, networkType, version }: KeeperApp = yield call( - dbManager.getObjectByIndex, - RealmSchema.KeeperApp - ); - const encryptionKey = generateEncryptionKey(primarySeed); - const walletObject = {}; - const signersObject = {}; - const vaultObject = {}; - - // update all wallets and signers - const btcWallets: Wallet[] = yield call(dbManager.getCollection, RealmSchema.Wallet); - const usdtWallets = yield call(dbManager.getObjectByIndex, RealmSchema.USDTWallet, null, true); - const wallets = [...btcWallets, ...usdtWallets]; - - for (const index in wallets) { - const wallet = wallets[index]; - const encrytedWallet = encrypt(encryptionKey, JSON.stringify(wallet)); - walletObject[wallet.id] = encrytedWallet; - } - const signers: Signer[] = yield call(dbManager.getCollection, RealmSchema.Signer); - for (const index in signers) { - const signer = signers[index]; - const encrytedSigner = encrypt( - encryptionKey, - JSON.stringify(sanitizeSeedKeyForBackup(signer)) - ); - signersObject[getKeyUID(signer)] = encrytedSigner; + return () => {}; + }, buffers.expanding()); + try { + while (true) { + const phase = yield takeMaybe(updates); + if (phase === END) break; + finalPhase = phase; + yield put(setBackupRepairState({ appId: id, phase })); } - const vaults: Vault[] = yield call(dbManager.getCollection, RealmSchema.Vault); - for (const index in vaults) { - const vault = vaults[index]; - const vaultEncrypted = encrypt( - encryptionKey, - JSON.stringify(sanitizeVaultSignersForSeedKeyBackup(vault)) - ); - const signersData: Array<{ - signerId: string; - xfpHash: string; - }> = []; - for (const signer of vault.signers) { - signersData.push({ - signerId: getKeyUID(signer), - xfpHash: hash256(signer.masterFingerprint), + const current: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); + if (current?.id === id) { + yield put(setPendingAllBackup(finalPhase !== 'verified')); + if (finalPhase === 'verified') { + const notifications = dbManager.getObjectByField( + RealmSchema.UAI, + uaiType.SERVER_BACKUP_FAILURE, + 'uaiType' + ); + for (const uai of notifications) + yield call(uaiActionedWorker, { payload: { uaiId: uai.id, action: true } }); + } else if ( + !dbManager.getObjectByField(RealmSchema.UAI, uaiType.SERVER_BACKUP_FAILURE, 'uaiType') + ?.length + ) { + yield call(addToUaiStackWorker, { + payload: { + uaiType: uaiType.SERVER_BACKUP_FAILURE, + uaiDetails: { + heading: 'Check Your Backup', + body: 'Your Assisted Server Backup may differ from this device. View details to check it.', + }, + }, }); } - vaultObject[vault.id] = { - vaultId: vault.id, - signersData, - vault: vaultEncrypted, - }; } + return finalPhase === 'verified'; + } finally { + updates.close(); + yield put(setBackupRepairRunning({ appId: id, running: false })); + } +} - const nodes: NodeDetail[] = yield call(dbManager.getCollection, RealmSchema.NodeConnect); - const nodesToUpdate = []; - if (nodes && nodes.length > 0) { - for (const index in nodes) { - const node = nodes[index]; - node.isConnected = false; - const encryptedNode = encrypt(encryptionKey, JSON.stringify(node)); - nodesToUpdate.push(encryptedNode); - } - } +function* backupFreshnessWorker({ type }) { + const { automaticCloudBackup } = yield select((state: RootState) => state.bhr); + if (!automaticCloudBackup) return; + yield call(runBackupInspection, type === REPAIR_BACKUP); +} - const labels = yield call(dbManager.getCollection, RealmSchema.Tags); - const tagsToBackup = labels.map((tag) => ({ - id: hash256(hash256(encryptionKey + tag.id)), - content: encrypt(encryptionKey, JSON.stringify(tag)), - })); +export function* backupFreshnessWatcher() { + yield takeEvery([CHECK_BACKUP_FRESHNESS, REPAIR_BACKUP], backupFreshnessWorker); +} - yield call(Relay.backupAllSignersAndVaults, { - appId: id, - publicId, - walletObject, - signersObject, - vaultObject, - networkType, - subscription: JSON.stringify(subscription), - version, - nodes: nodesToUpdate, - labels: tagsToBackup, - }); - yield put(setBackupAllSuccess(true)); - yield put(setPendingAllBackup(false)); - return true; - } catch (error) { - yield put(setBackupAllFailure(true)); - yield call(setServerBackupFailed); - console.log('🚀 ~ function*backupAllSignersAndVaultsWorker ~ error:', error); - yield put( - setHomeToastMessage({ - message: 'Assisted server backup failed. Please try again later.', - isError: true, - }) - ); - return false; +function* backupAllSignersAndVaultsWorker() { + const { id }: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); + yield put(setBackupAllSuccess(false)); + yield put(setBackupAllFailure(false)); + yield put(setBackupAllLoading(true)); + try { + const verified = yield call(runBackupInspection, true); + const current: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); + if (current?.id === id) + yield put(verified ? setBackupAllSuccess(true) : setBackupAllFailure(true)); + return verified; } finally { yield put(setBackupAllLoading(false)); } @@ -1093,22 +1077,19 @@ function* deleteBackupWorker() { export const deleteBackupWatcher = createWatcher(deleteBackupWorker, DELETE_BACKUP); export function* checkBackupCondition() { - const { pendingAllBackup, automaticCloudBackup } = yield select((state: RootState) => state.bhr); - const { subscription }: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); - // if (automaticCloudBackup && subscription.level === AppSubscriptionLevel.L1 && !pendingAllBackup) { - if (automaticCloudBackup && !pendingAllBackup) { - return true; - } + const { automaticCloudBackup } = yield select((state: RootState) => state.bhr); if (!automaticCloudBackup) return true; + const { id }: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); + markBackupMutation(id); + yield put(invalidateBackupRepair(id)); const netInfo = yield call(NetInfo.fetch); if (!netInfo.isConnected) { yield call(setServerBackupFailed); return true; } - if (pendingAllBackup) { - yield call(backupAllSignersAndVaultsWorker); - return true; - } + // Never suppress this incremental write or replace it with a snapshot: wallet + // creation invokes us before persisting the new wallet to Realm. An old + // mismatch is repaired only after Back Up Now, separately from this mutation. return false; } @@ -1138,84 +1119,14 @@ export function* setServerBackupFailed() { } function* validateServerBackupWorker({ callback }) { - try { - const { primarySeed, id }: KeeperApp = yield call( - dbManager.getObjectByIndex, - RealmSchema.KeeperApp - ); - const encryptionKey = generateEncryptionKey(primarySeed as string); - let { allVaultImages, appImage, labels } = yield call(Relay.getAppImage, id); - const { nodes, wallets, signers = {} } = appImage; - - // Check for vaults - let localVaultIds = yield call(dbManager.getCollection, RealmSchema.Vault); - localVaultIds = localVaultIds.map((item) => item.id); - let decryptedVaultIds = []; - for (const index in allVaultImages) { - decryptedVaultIds.push(JSON.parse(decrypt(encryptionKey, allVaultImages[index].vault)).id); - } - if (!areSetsEqual(new Set(localVaultIds), new Set(decryptedVaultIds))) - return callback({ status: false, message: 'Vaults do not match' }); - - // Check for labels - let localLabelIds: any = yield call(dbManager.getCollection, RealmSchema.Tags); - localLabelIds = localLabelIds.map((item) => item.id); - if (labels.length !== localLabelIds.length) - return callback({ status: false, message: 'Labels do not match' }); - const decryptedLabelIds = []; - for (const index in labels) { - decryptedLabelIds.push(JSON.parse(decrypt(encryptionKey, labels[index].content)).id); - } - if (!areSetsEqual(new Set(localLabelIds), new Set(decryptedLabelIds))) - return callback({ status: false, message: 'Labels do not match' }); - - // Check for Signers - let localSignerIds: any = yield call(dbManager.getCollection, RealmSchema.Signer); - localSignerIds = localSignerIds.map((item) => item.id); - if (Object.keys(signers).length !== localSignerIds.length) - return callback({ status: false, message: 'Signers do not match' }); - let decryptedSignerIds = []; - for (const index in signers) { - decryptedSignerIds.push(JSON.parse(decrypt(encryptionKey, signers[index])).id); - } - if (!areSetsEqual(new Set(localSignerIds), new Set(decryptedSignerIds))) - return callback({ status: false, message: 'Signers do not match' }); - - // Check for Wallets - let localWallets: any = yield call(dbManager.getCollection, RealmSchema.Wallet); - const walletsBackupLength = Array.isArray(wallets) - ? wallets.length - : Object.keys(wallets).length; - if (walletsBackupLength !== localWallets.length) - return callback({ status: false, message: 'Wallets do not match' }); - const localWalletIds = localWallets.map((item) => item.id); - let decryptedWalletIds = []; - for (const index in wallets) { - decryptedWalletIds.push(JSON.parse(decrypt(encryptionKey, wallets[index])).id); - } - if (!areSetsEqual(new Set(localWalletIds), new Set(decryptedWalletIds))) - return callback({ status: false, message: 'Wallets do not match' }); - - // Check for nodes - let localNodes: any = yield call(dbManager.getCollection, RealmSchema.NodeConnect); - localNodes = localNodes.map((node) => { - node.isConnected = false; - return JSON.stringify(node); - }); - if (localNodes.length !== nodes.length) - return callback({ status: false, message: 'Nodes do not match' }); - const decryptedNodes = []; - for (const index in nodes) { - decryptedNodes.push(JSON.stringify(JSON.parse(decrypt(encryptionKey, nodes[index])))); - } - const missingNodes = decryptedNodes.filter((item) => !localNodes.includes(item)); - if (missingNodes.length > 0) return callback({ status: false, message: 'Nodes do not match' }); - - return callback({ status: true, message: 'Backup verified successfully.' }); - } catch (error) { - console.log('🚀 ~ function*validateServerBackupWorker ~ error:', error); - return callback({ status: false, message: 'Backup verification failed.', error: true }); - } + const matched = yield call(runBackupInspection, false); + const { id }: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); + const { backupRepairStateByAppId = {} } = yield select((state: RootState) => state.bhr); + callback({ + status: matched, + error: backupRepairStateByAppId[id] === 'unverified', + message: matched ? 'Backup verified successfully.' : 'Backup could not be verified.', + }); } export const validateSeverBackupWatcher = createWatcher( diff --git a/src/store/sagas/index.ts b/src/store/sagas/index.ts index dc4698337a..80ce4fc59d 100644 --- a/src/store/sagas/index.ts +++ b/src/store/sagas/index.ts @@ -45,6 +45,7 @@ import { backupAllSignersAndVaultsWatcher, deleteBackupWatcher, validateSeverBackupWatcher, + backupFreshnessWatcher, } from './bhr'; import { calculateCustomFeeWatcher, @@ -64,7 +65,12 @@ import { setupKeeperAppWatcher, } from './storage'; import { migrateLablesWatcher, updateVersionHistoryWatcher } from './upgrade'; -import { addLabelsWatcher, bulkUpdateLabelWatcher, importLabelsWatcher, markUTXOSpendabilityWatcher } from './utxos'; +import { + addLabelsWatcher, + bulkUpdateLabelWatcher, + importLabelsWatcher, + markUTXOSpendabilityWatcher, +} from './utxos'; import { connectToNodeWatcher } from './network'; import { loadConciergeUserWatcher, @@ -154,6 +160,7 @@ const rootSaga = function* () { backupAllSignersAndVaultsWatcher, deleteBackupWatcher, validateSeverBackupWatcher, + backupFreshnessWatcher, // upgrade updateVersionHistoryWatcher, diff --git a/src/store/sagas/login.ts b/src/store/sagas/login.ts index 497834d986..1f2c839f9a 100644 --- a/src/store/sagas/login.ts +++ b/src/store/sagas/login.ts @@ -51,7 +51,7 @@ import { createWatcher } from '../utilities'; import { fetchExchangeRates } from '../sagaActions/send_and_receive'; import { setLoginMethod } from '../reducers/settings'; import { setSubscription } from 'src/store/sagaActions/settings'; -import { backupAllSignersAndVaults } from '../sagaActions/bhr'; +import { checkBackupFreshness } from '../sagaActions/bhr'; import { uaiChecks } from '../sagaActions/uai'; import { applyUpgradeSequence } from './upgrade'; import { resetSyncing } from '../reducers/wallets'; @@ -214,7 +214,7 @@ function* credentialsAuthWorker({ payload }) { } if (appId) { try { - const { id, publicId, subscription }: KeeperApp = yield call( + const { id }: KeeperApp = yield call( dbManager.getObjectByIndex, RealmSchema.KeeperApp ); @@ -249,10 +249,15 @@ function* credentialsAuthWorker({ payload }) { yield put(resetSyncing()); - const { pendingAllBackup, automaticCloudBackup } = yield select( - (state: RootState) => state.bhr - ); - if (pendingAllBackup && automaticCloudBackup) yield put(backupAllSignersAndVaults()); + const { + pendingAllBackup, + automaticCloudBackup, + backupRepairCompletedByAppId = {}, + } = yield select((state: RootState) => state.bhr); + // Check without uploading; a mismatch requires Back Up Now. + if (automaticCloudBackup && (pendingAllBackup || !backupRepairCompletedByAppId[id])) { + yield put(checkBackupFreshness()); + } if (!allAccounts.length) { // upgraded app yield put(addAccount(appId)); diff --git a/src/store/sagas/upgrade.ts b/src/store/sagas/upgrade.ts index 133d332277..e7dd01185f 100644 --- a/src/store/sagas/upgrade.ts +++ b/src/store/sagas/upgrade.ts @@ -51,9 +51,11 @@ export const SIGNER_POLICY_MIGRATION_VERSION = '2.1.0'; export function* applyUpgradeSequence({ previousVersion, newVersion, + isRecovery = false, }: { previousVersion: string; newVersion: string; + isRecovery?: boolean; }) { console.log(`applying upgrade sequence - from: ${previousVersion} to ${newVersion}`); @@ -80,15 +82,15 @@ export function* applyUpgradeSequence({ } yield put(setAppVersion(newVersion)); - yield put(updateVersionHistory(previousVersion, newVersion)); + yield put(updateVersionHistory(previousVersion, newVersion, isRecovery)); } -function* updateVersionHistoryWorker({ +export function* updateVersionHistoryWorker({ payload, }: { - payload: { previousVersion: string; newVersion: string }; + payload: { previousVersion: string; newVersion: string; isRecovery?: boolean }; }) { - const { previousVersion, newVersion } = payload; + const { previousVersion, newVersion, isRecovery = false } = payload; try { const app: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); const response = yield call(Relay.updateAppImage, { @@ -98,7 +100,7 @@ function* updateVersionHistoryWorker({ yield call(dbManager.createObject, RealmSchema.VersionHistory, { version: `${newVersion}(${DeviceInfo.getBuildNumber()})`, date: new Date().toString(), - title: `Upgraded from ${previousVersion} to ${newVersion}`, + title: isRecovery ? 'Recovered Wallet' : `Upgraded from ${previousVersion} to ${newVersion}`, }); const firebaseApp = getApp(); const messagingInstance = getMessaging(firebaseApp); diff --git a/src/store/sagas/utxos.ts b/src/store/sagas/utxos.ts index 909c03dec7..7f3d1addec 100644 --- a/src/store/sagas/utxos.ts +++ b/src/store/sagas/utxos.ts @@ -11,9 +11,19 @@ import { KeeperApp } from 'src/models/interfaces/KeeperApp'; import { createWatcher } from '../utilities'; import { getJSONFromRealmObject } from 'src/storage/realm/utils'; -import { ADD_LABELS, BULK_UPDATE_LABELS, IMPORT_LABELS, MARK_UTXO_SPENDABILITY } from '../sagaActions/utxos'; +import { + ADD_LABELS, + BULK_UPDATE_LABELS, + IMPORT_LABELS, + MARK_UTXO_SPENDABILITY, +} from '../sagaActions/utxos'; import { resetState, setSyncingUTXOError, setSyncingUTXOs } from '../reducers/utxos'; -import { checkBackupCondition, setServerBackupFailed } from './bhr'; +import { + checkBackupCondition, + setServerBackupFailed, + updateAppImageWorker, + updateVaultImageWorker, +} from './bhr'; import { encrypt, generateEncryptionKey, hash256 } from 'src/utils/service-utilities/encryption'; export function* addLabelsWorker({ @@ -217,30 +227,47 @@ export function* markUTXOSpendabilityWorker({ try { const { wallet, txId, vout, spendability } = payload; - const schema = - wallet.entityKind === EntityKind.VAULT ? RealmSchema.Vault : RealmSchema.Wallet; + const schema = wallet.entityKind === EntityKind.VAULT ? RealmSchema.Vault : RealmSchema.Wallet; const storedWallet: any = yield call(dbManager.getObjectById, schema, wallet.id); if (!storedWallet) return; // Deep plain-JS copy so Realm embedded lists are proper arrays - const walletJSON = getJSONFromRealmObject(storedWallet); + const walletJSON = getJSONFromRealmObject(storedWallet) as unknown as Wallet | Vault; const specs = walletJSON.specs; const allUTXOArrays: Array<'confirmedUTXOs' | 'unconfirmedUTXOs'> = [ 'confirmedUTXOs', 'unconfirmedUTXOs', ]; + let changed = false; for (const arrayKey of allUTXOArrays) { const utxoArray: any[] = specs[arrayKey] || []; const idx = utxoArray.findIndex((u: any) => u.txId === txId && u.vout === vout); if (idx !== -1) { + if (utxoArray[idx].isManualOverride && utxoArray[idx].spendability === spendability) return; utxoArray[idx] = { ...utxoArray[idx], spendability, isManualOverride: true }; + changed = true; break; } } - yield call(dbManager.updateObjectById, schema, wallet.id, { specs }); + if (!changed) return; + const persisted = yield call(dbManager.updateObjectById, schema, wallet.id, { specs }); + if (persisted !== true) return; + // Persist the user's choice first, then invalidate/refresh Assisted Server + // Backup through its normal opt-in/offline-aware incremental path. + try { + const response = + schema === RealmSchema.Vault + ? yield call(updateVaultImageWorker, { + payload: { vault: walletJSON as Vault, isUpdate: true }, + }) + : yield call(updateAppImageWorker, { payload: { wallets: [walletJSON as Wallet] } }); + if (!response?.updated) yield call(setServerBackupFailed); + } catch { + yield call(setServerBackupFailed); + } } catch (e) { console.log('markUTXOSpendabilityWorker error:', e); } diff --git a/src/store/sagas/wallets.ts b/src/store/sagas/wallets.ts index 4f54a44ec0..d212e033db 100644 --- a/src/store/sagas/wallets.ts +++ b/src/store/sagas/wallets.ts @@ -63,7 +63,12 @@ import ElectrumClient, { import idx from 'idx'; import _ from 'lodash'; import { SyncedWallet } from 'src/services/wallets/interfaces'; -import { checkSignerAccountsMatch, getAccountFromSigner, getKeyUID } from 'src/utils/utilities'; +import { + checkSignerAccountsMatch, + getAccountFromSigner, + getKeyUID, + sanitizeSeedKeyForBackup, +} from 'src/utils/utilities'; import { COLLABORATIVE_SCHEME } from 'src/screens/SigningDevices/SetupCollaborativeWallet'; import { RootState } from '../store'; @@ -109,6 +114,7 @@ import { deleteVaultImageWorker, updateAppImageWorker, updateVaultImageWorker, + setServerBackupFailed, } from './bhr'; import { relaySignersUpdateFail, @@ -435,20 +441,20 @@ export function* addSigningDeviceWorker({ multiSigMatch || taprootMatch; - if (signerMergeCondition) { - const { type, signerName } = [SignerType.UNKOWN_SIGNER, SignerType.OTHER_SD].includes( - existingSigner.type - ) - ? newSigner - : existingSigner; - signersToUpdate.push({ - ...existingSigner, - type, - signerXpubs: _.merge(existingSigner.signerXpubs, newSigner.signerXpubs), - signerName, - }); - continue; - } + if (signerMergeCondition) { + const { type, signerName } = [SignerType.UNKOWN_SIGNER, SignerType.OTHER_SD].includes( + existingSigner.type + ) + ? newSigner + : existingSigner; + signersToUpdate.push({ + ...existingSigner, + type, + signerXpubs: _.merge(existingSigner.signerXpubs, newSigner.signerXpubs), + signerName, + }); + continue; + } const singleSigDifferent = keysDifferent(XpubTypes.P2WPKH, newSigner, existingSigner); const multiSigDifferent = keysDifferent(XpubTypes.P2WSH, newSigner, existingSigner); @@ -725,7 +731,10 @@ function* refreshWalletsWorker({ } } - if (synchedWallet.entityKind === EntityKind.VAULT && synchedWallet.type === VaultType.CANARY) { + if ( + synchedWallet.entityKind === EntityKind.VAULT && + synchedWallet.type === VaultType.CANARY + ) { yield put(uaiChecks([uaiType.CANARAY_WALLET])); } @@ -800,14 +809,13 @@ function* refreshWalletsWorker({ } } - const { taintedAddresses, initialTaintAddresses, dustSpendTxids } = - classifyDustByAddress( - synchedWallet as any, - externalAddresses, - internalAddresses, - manualOverrideAddresses, - options.dustScan ? 'full' : 'current' - ); + const { taintedAddresses, initialTaintAddresses, dustSpendTxids } = classifyDustByAddress( + synchedWallet as any, + externalAddresses, + internalAddresses, + manualOverrideAddresses, + options.dustScan ? 'full' : 'current' + ); // Mark UTXOs — only restore snapshot for soft/hard refresh or when user made a manual override; // always re-run classification otherwise so reclassification is not blocked. @@ -1176,15 +1184,45 @@ function* updateKeyDetailsWorker({ payload }) { if (!updatedFlag) { updatedRegsteredVaults.push(value); } - yield call(dbManager.updateObjectByPrimaryId, RealmSchema.VaultSigner, 'xpub', signer.xpub, { - registeredVaults: updatedRegsteredVaults, - }); - return; + const saved = yield call( + dbManager.updateObjectByPrimaryId, + RealmSchema.VaultSigner, + 'xpub', + signer.xpub, + { registeredVaults: updatedRegsteredVaults } + ); + if (saved !== true) { + yield put(relaySignersUpdateFail('Key registration could not be saved')); + return false; + } + // VaultSigner is shared by every vault using this xpub. Back up each fresh + // containing vault so an older copy cannot overwrite the restored metadata. + try { + const vaults: Vault[] = yield call(dbManager.getCollection, RealmSchema.Vault); + for (const vault of vaults) { + if (vault.signers.some((key) => key.xpub === signer.xpub)) { + const response = yield call(updateVaultImageWorker, { + payload: { vault, isUpdate: true }, + }); + if (!response?.updated) yield call(setServerBackupFailed); + } + } + } catch (error) { + yield call(setServerBackupFailed); + } + return true; } - yield call(dbManager.updateObjectByPrimaryId, RealmSchema.VaultSigner, 'xpub', signer.xpub, { - [key]: value, - }); + const updated = yield call( + dbManager.updateObjectByPrimaryId, + RealmSchema.VaultSigner, + 'xpub', + signer.xpub, + { + [key]: value, + } + ); + return updated === true; } export const updateKeyDetails = createWatcher(updateKeyDetailsWorker, UPDATE_KEY_DETAILS); @@ -1364,7 +1402,10 @@ function* mergeSimilarKeysWorker({ payload }: { payload: { signer: Signer } }) { RealmSchema.KeeperApp ); const encryptionKey = generateEncryptionKey(primarySeed); - const encrytedSigner = encrypt(encryptionKey, JSON.stringify(signer)); + const encrytedSigner = encrypt( + encryptionKey, + JSON.stringify(sanitizeSeedKeyForBackup(signer)) + ); const updated = yield call(Relay.migrateXfp, id, [ { oldSignerId: s.masterFingerprint, diff --git a/src/utils/utilities.ts b/src/utils/utilities.ts index 67d4a549df..de54e775f6 100644 --- a/src/utils/utilities.ts +++ b/src/utils/utilities.ts @@ -714,12 +714,29 @@ export const sanitizeSeedKeyForBackup = (signer: Signer) => { }; export const sanitizeVaultSignersForSeedKeyBackup = (vault: Vault) => { - let updatedVault = _.cloneDeep(vault); - let signer = dbManager.getCollection(RealmSchema.Signer); + const updatedVault = _.cloneDeep(vault); + const signers = dbManager.getCollection(RealmSchema.Signer) as unknown as Signer[]; updatedVault.signers = updatedVault.signers.map((vaultSigner) => { - const selectedSigner = signer.find( - (signer) => signer.masterFingerprint === vaultSigner.masterFingerprint - ); + const matches = signers.filter((signer) => { + if (signer.masterFingerprint !== vaultSigner.masterFingerprint) return false; + // A fingerprint can appear on several accounts/networks. Match the actual + // vault key before deciding whether its private material may be backed up. + const sameIdentity = + signer.type === SignerType.POLICY_SERVER + ? signer.networkType === vault.networkType + : getKeyUID(signer) === getKeyUID(vaultSigner); + return ( + sameIdentity && + Object.values(signer.signerXpubs).some((keys) => + keys.some( + (key) => + key.xpub === vaultSigner.xpub && key.derivationPath === vaultSigner.derivationPath + ) + ) + ); + }); + if (matches.length !== 1) throw new Error('Cannot identify a vault key for backup'); + const selectedSigner = matches[0]; if ( selectedSigner.type === SignerType.SEED_WORDS && selectedSigner.signerName !== RECOVERY_KEY_SIGNER_NAME @@ -870,9 +887,13 @@ export const manipulateBitcoinPrices = (data) => { export const validatePSBT = (unsigned, signed, signer, errorText) => { if ( - [SignerType.TAPSIGNER, SignerType.SATOCHIP, SignerType.SPECTER, SignerType.SEEDSIGNER, SignerType.KRUX].includes( - signer.type - ) + [ + SignerType.TAPSIGNER, + SignerType.SATOCHIP, + SignerType.SPECTER, + SignerType.SEEDSIGNER, + SignerType.KRUX, + ].includes(signer.type) ) return; const unsignedHex = bitcoin.Psbt.fromBase64(unsigned).__CACHE.__TX.toHex(); @@ -931,4 +952,4 @@ export const combineSignedPsbt = (PsbtEnvelops) => { combinedPSBT.combine(bitcoin.Psbt.fromBase64(PsbtEnvelops[i].serializedPSBT)); } return combinedPSBT.toBase64(); -}; \ No newline at end of file +}; diff --git a/tests/recovery/backupFeedback.test.cjs b/tests/recovery/backupFeedback.test.cjs new file mode 100644 index 0000000000..5656622b5a --- /dev/null +++ b/tests/recovery/backupFeedback.test.cjs @@ -0,0 +1,54 @@ +const { test } = require('node:test'); +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const path = require('node:path'); +const vm = require('node:vm'); +const ts = require('typescript'); +const { fixture, wallet, encryption, enums } = require('./sagaHarness.cjs'); + +test('opening Recovery Key after restore does not replay health-check consent or enable backup', async () => { + const f = fixture({ bhr: { automaticCloudBackup: false, seedConfirmed: true } }); + const key = encryption.generateEncryptionKey(f.scope.bip39.mnemonicToSeedSync().toString('hex')); + f.remote.wallets.local = encryption.encrypt(key, JSON.stringify(wallet('local', enums.NetworkType.MAINNET))); + await f.run('getAppImageWorker', { payload: { primaryMnemonic: 'mock fixture only' } }); + assert.equal(f.state.account.recoveryKeyStatusByAppId[f.app.id], 'confirmed'); + const signal = f.actions.filter((a) => a.type === 'setSeedConfirmed').at(-1); + assert.equal(signal?.payload, false, 'restore must clear the transient health-check signal'); + assert.equal(f.state.bhr.automaticCloudBackup, false); + const file = 'src/components/Backup/BackupHealthCheckList.tsx'; + const source = ts.createSourceFile(file, fs.readFileSync(path.join(__dirname, '../..', file), 'utf8'), ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX); + let callback; + function visit(node) { + if (ts.isCallExpression(node) && node.expression.getText(source) === 'useEffect' && node.arguments[0]?.getText(source).includes('if (seedConfirmed)')) callback = node.arguments[0].getText(source); + ts.forEachChild(node, visit); + } + visit(source); + assert.ok(callback); + const actions = []; + vm.runInNewContext(ts.transpileModule(`(${callback})()`, { compilerOptions: { target: ts.ScriptTarget.ES2020 } }).outputText, { + seedConfirmed: signal.payload, automaticCloudBackup: false, + dispatch: (action) => actions.push(action), + }); + assert.deepEqual(actions, [], 'mounting the backup page must not upload or enable backup'); +}); +for (const file of ['src/components/Backup/BackupHealthCheckList.tsx', 'src/screens/SeedScreens/ExportSeedScreen.tsx', 'src/screens/BackupWallet/ViewRecoveryKeyScreen.tsx']) { + test(`${path.basename(file)} never enables backup or shows success after failure`, () => { + const source = ts.createSourceFile(file,fs.readFileSync(path.join(__dirname,'../..',file),'utf8'),ts.ScriptTarget.Latest,true,ts.ScriptKind.TSX); + let callback; + function visit(node) { + if (!callback && ts.isCallExpression(node) && node.expression.getText(source)==='useEffect' && node.arguments[0]?.getText(source).includes('backupAllSuccess')) callback=node.arguments[0].getText(source); + ts.forEachChild(node,visit); + } + visit(source); assert.ok(callback); + const actions=[], ui=[]; + const context={ backupAllSuccess:false,backupAllFailure:true,automaticCloudBackup:false, + dispatch:a=>actions.push(a),setBackupAllSuccess:v=>({type:'success',value:v}),setBackupAllFailure:v=>({type:'failure',value:v}),setAutomaticCloudBackup:v=>({type:'enabled',value:v}), + setAsbEnabled:v=>ui.push(['enabled',v]),setHealthCheckModal:v=>ui.push(['success',v]),setBackupSuccessModal:v=>ui.push(['success',v]), + showToast:()=>ui.push(['toast']),setLoader:()=>{},navigation:{goBack:()=>{}}, + translations:{recoveryBackup:{unverified:{title:'not verified'}}},recoveryBackup:{unverified:{title:'not verified'}}, + }; + const js=ts.transpileModule(`(${callback})()`,{compilerOptions:{target:ts.ScriptTarget.ES2020,jsx:ts.JsxEmit.React}}).outputText; + vm.runInNewContext(js,context); + assert.ok(!actions.some(a=>a.type==='enabled'&&a.value)); assert.ok(!ui.some(a=>(a[0]==='enabled'||a[0]==='success')&&a[1])); + }); +} diff --git a/tests/recovery/backupFreshness.test.cjs b/tests/recovery/backupFreshness.test.cjs new file mode 100644 index 0000000000..03ff4b9ab2 --- /dev/null +++ b/tests/recovery/backupFreshness.test.cjs @@ -0,0 +1,169 @@ +const { test } = require('node:test'); +const assert = require('node:assert/strict'); +const { fixture, enums, encryption, wallet, loadModule } = require('./sagaHarness.cjs'); +const { NetworkType, WalletType } = enums; + +for (const network of [NetworkType.MAINNET, NetworkType.TESTNET]) { + test(`wallet added after initial backup survives encrypted restore: ${network}`, async () => { + const f = fixture(); + const initial = wallet('initial', NetworkType.MAINNET); + const added = wallet('later', network, 1); + f.collections.Wallet.push(initial); + await f.run('updateAppImageWorker', { payload: { wallets: [initial] } }); + assert.equal( + await f.run('addNewWalletsWorker', { + payload: [{ walletType: WalletType.DEFAULT, walletDetails: { fixture: added } }], + }), + true + ); + assert.deepEqual(Object.keys(f.remote.wallets).sort(), ['initial', 'later']); + assert.ok(!f.remote.wallets.later.includes('later-address')); + f.collections.Wallet = []; + await f.run( + 'recoverApp', + 'mock mnemonic', + Buffer.from('fixture'), + encryption.generateEncryptionKey(f.app.primarySeed), + f.app.id, + f.app.subscription, + f.remote, + [], + [], + '2.5.16' + ); + assert.deepEqual(JSON.parse(JSON.stringify(f.collections.Wallet)), [initial, added]); + }); +} + +test('pending repair never suppresses a not-yet-persisted incremental addition', async () => { + const f = fixture({ bhr: { pendingAllBackup: true } }); + f.collections.Wallet.push(wallet('initial', NetworkType.MAINNET)); + await f.run('addNewWalletsWorker', { payload: [{ walletType: WalletType.DEFAULT, + walletDetails: { fixture: wallet('new', NetworkType.TESTNET) } }] }); + assert.ok(f.remote.wallets.new); + assert.deepEqual(f.calls.map(([type]) => type), ['incremental']); + assert.equal(f.state.bhr.pendingAllBackup, true); +}); + +test('disabled backup makes no uploads and preserves local creation', async () => { + const f = fixture({ bhr: { automaticCloudBackup: false, pendingAllBackup: true } }); + await f.run('addNewWalletsWorker', { + payload: [ + { + walletType: WalletType.DEFAULT, + walletDetails: { fixture: wallet('local', NetworkType.MAINNET) }, + }, + ], + }); + assert.equal(f.calls.length, 0); + assert.equal(f.collections.Wallet.length, 1); +}); + +for (const failure of [ + { online: false }, + { incrementalError: true }, +]) { + test(`backup failure retains wallet and retry: ${JSON.stringify(failure)}`, async () => { + const f = fixture(failure); + await f.run('addNewWalletsWorker', { + payload: [ + { + walletType: WalletType.DEFAULT, + walletDetails: { fixture: wallet('local', NetworkType.MAINNET) }, + }, + ], + }); + assert.equal(f.collections.Wallet.length, 1); + assert.equal(f.state.bhr.pendingAllBackup, true); + assert.ok(!f.remote.wallets.local); + assert.ok(!f.actions.some((a) => a.type === 'setBackupAllSuccess' && a.payload === true)); + }); +} + +for (const networks of [ + [NetworkType.MAINNET], + [NetworkType.TESTNET], + [NetworkType.MAINNET, NetworkType.TESTNET], +]) { + test(`recovery restores saved wallets and confirms Recovery Key status: ${networks}`, async () => { + const f = fixture(); + // Recovery orchestration computes its own key from the fixture mnemonic. + const seed = f.scope.bip39.mnemonicToSeedSync().toString('hex'); + const key = encryption.generateEncryptionKey(seed); + networks.forEach((network) => { + const w = wallet(network, network); + f.remote.wallets[w.id] = encryption.encrypt(key, JSON.stringify(w)); + }); + await f.run('getAppImageWorker', { payload: { primaryMnemonic: 'mock fixture only' } }); + assert.deepEqual( + f.actions.filter((a) => a.type === 'setAppImageError' && a.payload), + [] + ); + const id = f.app.id; + assert.equal(f.collections.Wallet.length, networks.length); + assert.equal(f.state.account.recoveryKeyStatusByAppId[id], 'confirmed'); + }); +} + +test('relay rejection retains the existing wallet-creation failure contract', async () => { + const f = fixture({ rejectIncremental: true }); + const result = await f.run('addNewWalletsWorker', { + payload: [ + { + walletType: WalletType.DEFAULT, + walletDetails: { fixture: wallet('rejected', NetworkType.MAINNET) }, + }, + ], + }); + assert.equal(result, false); + assert.equal(f.collections.Wallet.length, 0); + assert.equal(f.state.bhr.pendingAllBackup, true); + assert.ok(f.actions.some((a) => a.type === 'relayWalletUpdateFail' && a.payload === 'rejected')); +}); + +for (const enabled of [true, false]) { + test(`backup gate never performs an unsolicited full repair: ${enabled}`, async () => { + const f = fixture({ bhr: { automaticCloudBackup: enabled, backupRepairCompletedByAppId: {} } }); + f.collections.Wallet.push(wallet('old', NetworkType.MAINNET)); + assert.equal(await f.run('checkBackupCondition'), !enabled); + assert.equal(f.calls.length, 0); + assert.equal(f.state.bhr.backupRepairCompletedByAppId[f.app.id], undefined); + }); +} + +test('repair markers are stored in the separately persisted backup slice', () => { + let config; + const bhr = loadModule('src/store/reducers/bhr.ts', { + 'src/models/enums/BHR': loadModule('src/models/enums/BHR.ts'), + 'src/storage': { reduxStorage: {} }, + 'redux-persist': { + persistReducer: (settings, reducer) => { + config = settings; + return reducer; + }, + }, + }); + let state = bhr.default(undefined, { type: 'init' }); + assert.equal(config.key, 'bhr'); + assert.ok(!config.blacklist.includes('backupRepairCompletedByAppId')); + state = bhr.default(state, bhr.setBackupRepairState({ appId: 'first', phase: 'verified' })); + state = bhr.default(state, bhr.setBackupRepairState({ appId: 'second', phase: 'verified' })); + assert.equal(state.backupRepairCompletedByAppId.first, true); + assert.equal(state.backupRepairCompletedByAppId.second, true); + assert.equal(state.automaticCloudBackup, false); +}); + +test('pending repair does not suppress explicit wallet deletion', async () => { + const f = fixture({ bhr: { pendingAllBackup: true } }); + f.collections.Wallet.push(wallet('deleted', NetworkType.MAINNET)); + const response = await f.run('deleteAppImageEntityWorker', { + payload: { walletIds: ['deleted'] }, + }); + assert.equal(response.updated, true); + assert.deepEqual( + f.calls.map(([type]) => type), + ['delete'] + ); + assert.equal(f.collections.Wallet.length, 0); + assert.equal(Object.keys(f.remote.wallets).length, 0); +}); diff --git a/tests/recovery/benchmarks/backup-cost.cjs b/tests/recovery/benchmarks/backup-cost.cjs new file mode 100644 index 0000000000..eb7b9e44ab --- /dev/null +++ b/tests/recovery/benchmarks/backup-cost.cjs @@ -0,0 +1,74 @@ +// Synthetic local CPU/size baseline. Not a mobile benchmark or network measurement. +const fs = require('node:fs'); +const path = require('node:path'); +const vm = require('node:vm'); +const ts = require('typescript'); +const { performance } = require('node:perf_hooks'); +const file = path.join(__dirname, '../../..', 'src/utils/service-utilities/encryption.ts'); +const mod = { exports: {} }; +vm.runInNewContext( + ts.transpileModule(fs.readFileSync(file, 'utf8'), { + compilerOptions: { + module: ts.ModuleKind.CommonJS, + target: ts.ScriptTarget.ES2020, + esModuleInterop: true, + }, + }).outputText, + { + module: mod, + exports: mod.exports, + require: (name) => (name === 'react-native-rsa-native' ? { RSA: {} } : require(name)), + } +); +const { encrypt, decrypt, generateEncryptionKey } = mod.exports; +const key = generateEncryptionKey('synthetic-benchmark-only'); +const median = (values) => [...values].sort((a, b) => a - b)[Math.floor(values.length / 2)]; +const results = []; +for (const [count, recordKiB] of [ + [10, 50], + [100, 50], + [100, 200], + [1, 20000], +]) { + const records = Array.from({ length: count }, (_, index) => ({ + id: `synthetic-${index}`, + networkType: 'TESTNET', + data: 'x'.repeat(recordKiB * 1024), + })); + const repetitions = []; + let plaintextBytes = 0, + ciphertextBytes = 0; + for (let iteration = 0; iteration < 4; iteration++) { + const start = performance.now(); + const payload = records.map((record) => JSON.stringify(record)); + const ciphertext = payload.map((record) => encrypt(key, record)); + const encrypted = performance.now(); + const ids = ciphertext.map((record) => JSON.parse(decrypt(key, record)).id); + const done = performance.now(); + if (ids.length !== count) throw Error('incomplete synthetic round trip'); + plaintextBytes = payload.reduce((n, v) => n + Buffer.byteLength(v), 0); + ciphertextBytes = ciphertext.reduce((n, v) => n + Buffer.byteLength(v), 0); + if (iteration) repetitions.push({ encrypt: encrypted - start, decrypt: done - encrypted }); + } + results.push({ + records: count, + recordKiB, + plaintextMiB: +(plaintextBytes / 2 ** 20).toFixed(2), + ciphertextMiB: +(ciphertextBytes / 2 ** 20).toFixed(2), + encryptMs: +median(repetitions.map((r) => r.encrypt)).toFixed(1), + decryptAndParseMs: +median(repetitions.map((r) => r.decrypt)).toFixed(1), + uploadPlusReadbackSecondsAt1Mbps: +((2 * ciphertextBytes * 8) / 1e6).toFixed(1), + }); +} +console.log( + JSON.stringify( + { + environment: `${process.platform}/${process.arch} Node ${process.version}`, + samples: 'one warm-up and three measured repetitions per case', + networkTiming: 'theoretical payload transfer only; no measured network or protocol latency', + results, + }, + null, + 2 + ) +); diff --git a/tests/recovery/helpers.cjs b/tests/recovery/helpers.cjs new file mode 100644 index 0000000000..ccc8e1ad5e --- /dev/null +++ b/tests/recovery/helpers.cjs @@ -0,0 +1,58 @@ +const fs = require('node:fs'); +const path = require('node:path'); +const vm = require('node:vm'); +const ts = require('typescript'); +function loadModule(file, mocks = {}, globals = {}) { + const module = { exports: {} }; + const code = ts.transpileModule(fs.readFileSync(path.join(__dirname, '../..', file), 'utf8'), { + compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020, esModuleInterop: true }, + }).outputText; + vm.runInNewContext(code, { module, exports: module.exports, setTimeout, clearTimeout, + AbortController, Date, Buffer, console, + require: (name) => name in mocks ? mocks[name] : require(name), ...globals }); + return module.exports; +} +const encryption = loadModule('src/utils/service-utilities/encryption.ts', { 'react-native-rsa-native': { RSA: {} } }); +const image = loadModule('src/services/backup/image.ts', { 'src/utils/service-utilities/encryption': encryption }); +const makeImage = () => ({ wallets: {}, signers: {}, vaults: {}, nodes: {}, labels: {} }); +function harness(options = {}) { + const app = { id: 'disposable', publicId: 'fixture', primarySeed: 'ab'.repeat(32), subscription: { level: 1 }, version: '2.5.16' }; + const local = { Wallet: [], USDTWallet: [], Signer: [], Vault: [], NodeConnect: [], Tags: [] }; + const remote = { appId: app.id, wallets: {}, signers: {}, nodes: [], vaults: [], labels: [] }; + let vaults = [], labels = []; + const revision = () => require('node:crypto').createHash('sha256').update(JSON.stringify({remote,vaults,labels})).digest('hex'); + const calls = [], phases = []; + const db = { getObjectByIndex: (schema) => schema === 'KeeperApp' ? app : local[schema] }; + const rest = { post: async (path, payload, _headers, config) => { + calls.push(path.replace('fixture/', '')); + if (options.post) await options.post({ path, payload, config, remote, app, local }); + if (options.adapter) return options.adapter(path.replace('fixture/', ''), payload, config); + if (path.endsWith('getBackupSnapshot')) { + if (options.readError) throw Error('offline'); + return { data: options.response || { appImage: remote, allVaultImages: vaults, labels, revision: revision() } }; + } + if (payload.expectedRevision !== revision()) throw Error('Backup changed'); + if (options.reject) return { data: { updated: false } }; + remote.wallets = { ...payload.walletObject }; remote.signers = { ...payload.signersObject }; remote.nodes = payload.nodes; + vaults = Object.values(payload.vaultObject); labels = payload.labels; + remote.vaults = vaults.map(v => v.vaultId); remote.labels = labels.map(v => v.id); + if (options.corruptWrite) remote.wallets = {}; + if (options.lostResponse) throw Error('response lost after commit'); + return { data: { updated: true } }; + } }; + const transport = loadModule('src/services/backup/transport.ts', { '../rest/RestClient': rest }); + const repair = loadModule('src/services/backup/repair.ts', { + 'src/storage/realm/dbManager': db, + 'src/storage/realm/enum': { RealmSchema: new Proxy({}, { get: (_, name) => name }) }, + 'src/utils/service-utilities/config': { RELAY: 'fixture/' }, + 'src/utils/service-utilities/encryption': encryption, + 'src/utils/utilities': { getKeyUID: s => s.id || s.masterFingerprint, sanitizeSeedKeyForBackup: s => s, sanitizeVaultSignersForSeedKeyBackup: s => s }, + './transport': transport, './image': image, + }); + return { app, local, remote, calls, phases, transport, repair, + run: (write = false) => repair.inspectBackup(app.id, write, phase => phases.push(phase)) }; +} +const wallet = (id = 'wallet', networkType = 'MAINNET') => ({ id, networkType, type: 'DEFAULT', + derivationDetails: { instanceNum: 0 }, presentationData: { name: 'Disposable wallet' }, + specs: { xpub: 'disposable-test-key', nextFreeAddressIndex: 1, balances: { confirmed: 0 }, transactions: [] } }); +module.exports = { loadModule, encryption, image, makeImage, harness, wallet }; diff --git a/tests/recovery/keyRecovery.test.cjs b/tests/recovery/keyRecovery.test.cjs new file mode 100644 index 0000000000..905ada0ddd --- /dev/null +++ b/tests/recovery/keyRecovery.test.cjs @@ -0,0 +1,280 @@ +// Key lifecycle coverage uses production backup, key identity, sanitization and +// restore functions with real AES. Native Realm, physical signing and BIP32 key +// derivation remain separate acceptance checks; these are disposable records. +const { test } = require('node:test'); +const assert = require('node:assert/strict'); +const { fixture, enums, encryption, signer, wallet } = require('./sagaHarness.cjs'); +const { NetworkType, SignerType, XpubTypes } = enums; +const plain = (value) => JSON.parse(JSON.stringify(value)); +const keyFor = (f) => encryption.generateEncryptionKey(f.app.primarySeed); + +async function restoreKeys(f) { + f.collections.Signer = []; + await f.run('recoverApp', 'mock mnemonic', Buffer.from('ab'.repeat(32), 'hex'), + keyFor(f), f.app.id, f.app.subscription, f.remote, [], [], '2.5.16'); + return plain(f.collections.Signer); +} + +for (const network of [NetworkType.MAINNET, NetworkType.TESTNET]) { + for (const pendingAllBackup of [false, true]) { + test(`later-added key survives encrypted restore: ${network}, pending=${pendingAllBackup}`, async () => { + const f = fixture({ bhr: { pendingAllBackup } }); + f.state.settings.bitcoinNetworkType = network; + const first = signer('AABBCCDD', network); + const later = signer('11223344', network, SignerType.LEDGER, 2); + later.signerName = 'Travel device'; + later.hidden = true; + await f.run('addSigningDeviceWorker', { payload: { signers: [first] } }); + await f.run('addSigningDeviceWorker', { payload: { signers: [later] } }); + assert.equal(f.calls.length, 2); + assert.equal(f.collections.Signer.length, 2); + const expected = plain(f.collections.Signer); + const id = f.scope.getKeyUID(later); + assert.ok(f.remote.signers[id]); + assert.ok(!f.remote.signers[id].includes('Travel device')); + assert.deepEqual(await restoreKeys(f), expected); + assert.equal(f.state.bhr.pendingAllBackup, pendingAllBackup); + }); + } +} + +test('same fingerprint on different networks and accounts retains three distinct keys', async () => { + const f = fixture(); + for (const [network, account] of [[NetworkType.MAINNET, 0], [NetworkType.TESTNET, 0], [NetworkType.MAINNET, 1]]) { + f.state.settings.bitcoinNetworkType = network; + await f.run('addSigningDeviceWorker', { + payload: { signers: [signer('AABBCCDD', network, SignerType.COLDCARD, account)] }, + }); + } + assert.deepEqual(Object.keys(f.remote.signers).sort(), ['AABBCCDD0M', 'AABBCCDD0T', 'AABBCCDD1M']); + const expected = plain(f.collections.Signer); + assert.deepEqual(await restoreKeys(f), expected); +}); + +test('mixed wallet, signer and node update backs up every requested record', async () => { + const f = fixture(); + const key = signer('AABBCCDD', NetworkType.MAINNET); + key.id = f.scope.getKeyUID(key); + f.collections.NodeConnect.push({ id: 'node-fixture', host: 'fixture.invalid', isConnected: true }); + await f.run('updateAppImageWorker', { + payload: { wallets: [wallet('wallet', NetworkType.MAINNET)], signers: [key], updateNodes: true }, + }); + assert.equal(f.calls.length, 1); + assert.ok(f.remote.wallets.wallet); + assert.ok(f.remote.signers[key.id]); + assert.equal(f.remote.nodes.length, 1); + assert.equal(JSON.parse(encryption.decrypt(keyFor(f), f.remote.nodes[0])).isConnected, false); + assert.deepEqual(await restoreKeys(f), [plain(key)]); +}); + +test('empty wallet array does not suppress signer migration', async () => { + const f = fixture(); + const key = signer('AABBCCDD', NetworkType.MAINNET); + key.id = f.scope.getKeyUID(key); + await f.run('updateAppImageWorker', { payload: { wallets: [], signers: [key] } }); + assert.ok(f.remote.signers[key.id]); +}); + +test('key name, description, archive and health metadata updates survive clean restore', async () => { + const f = fixture(); + await f.run('addSigningDeviceWorker', { payload: { signers: [signer('AABBCCDD', NetworkType.MAINNET)] } }); + const key = f.collections.Signer[0]; + for (const [field, value] of Object.entries({ + signerName: 'Renamed device', signerDescription: 'Recorded on physical device', + archived: true, hidden: true, + healthCheckDetails: [{ type: 'address-verification', actionDate: '2026-09-29T00:00:00.000Z' }], + })) await f.run('updateSignerDetailsWorker', { payload: { signer: key, key: field, value } }); + const expected = plain(f.collections.Signer); + assert.deepEqual(await restoreKeys(f), expected); +}); + +test('seed-key private material is omitted while software and Recovery Key material retain existing policy', async () => { + const f = fixture(); + const ordinary = signer('11111111', NetworkType.MAINNET, SignerType.SEED_WORDS); + const recovery = signer('22222222', NetworkType.MAINNET, SignerType.SEED_WORDS); + recovery.signerName = f.scope.RECOVERY_KEY_SIGNER_NAME; + recovery.hidden = true; + const software = signer('33333333', NetworkType.MAINNET, SignerType.MY_KEEPER); + software.extraData = { instanceNumber: 1 }; + for (const record of [ordinary, recovery, software]) { + record.signerXpubs[XpubTypes.P2WPKH][0].xpriv = `${record.masterFingerprint}-disposable-private`; + await f.run('addSigningDeviceWorker', { payload: { signers: [record] } }); + } + const localBefore = plain(f.collections.Signer); + const expected = plain(f.collections.Signer.map(f.scope.sanitizeSeedKeyForBackup)); + assert.deepEqual(plain(f.collections.Signer), localBefore, 'sanitizing must not erase local signing material'); + const restored = await restoreKeys(f); + assert.deepEqual(restored, expected); + assert.equal(restored[0].signerXpubs[XpubTypes.P2WPKH][0].xpriv, null); + assert.equal(restored[1].signerXpubs[XpubTypes.P2WPKH][0].xpriv, '22222222-disposable-private'); + assert.equal(restored[2].signerXpubs[XpubTypes.P2WPKH][0].xpriv, '33333333-disposable-private'); +}); + +test('server-key policy and software key metadata survive encrypted recovery', async () => { + const f = fixture(); + const key = signer('AABBCCDD', NetworkType.TESTNET, SignerType.POLICY_SERVER); + key.signerXpubs = {}; + key.signerPolicy = { signingDelay: 3600, backupDisabled: false, + verification: { method: 'email', verifier: 'fixture.invalid' }, secondaryVerification: [] }; + key.isBIP85 = true; + key.linkedViaSecondary = true; + f.state.settings.bitcoinNetworkType = NetworkType.TESTNET; + await f.run('addSigningDeviceWorker', { payload: { signers: [key] } }); + assert.ok(f.remote.signers.AABBCCDDT); + const expected = plain(f.collections.Signer); + assert.deepEqual(await restoreKeys(f), expected); + const restored = f.collections.Signer[0]; + assert.deepEqual(plain(restored.signerPolicy), key.signerPolicy); + assert.equal(restored.isBIP85, true); +}); + +test('vault sanitizer excludes ordinary imported seed xpriv without changing source data', () => { + const f = fixture(); + const key = signer('AABBCCDD', NetworkType.MAINNET, SignerType.SEED_WORDS); + const recovery = signer('11223344', NetworkType.MAINNET, SignerType.SEED_WORDS); + recovery.signerName = f.scope.RECOVERY_KEY_SIGNER_NAME; + f.collections.Signer.push(key, recovery); + const vault = { id: 'vault-fixture', signers: [key, recovery].map((record) => ({ + masterFingerprint: record.masterFingerprint, ...record.signerXpubs[XpubTypes.P2WPKH][0], + xpriv: `${record.masterFingerprint}-disposable-private`, + })) }; + const before = plain(vault); + const sanitized = f.scope.sanitizeVaultSignersForSeedKeyBackup(vault); + assert.equal(sanitized.signers[0].xpriv, null); + assert.equal(sanitized.signers[1].xpriv, '11223344-disposable-private'); + assert.deepEqual(plain(vault), before); +}); + +for (const [network, account] of [[NetworkType.MAINNET, 1], [NetworkType.TESTNET, 0]]) { + test(`vault sanitization selects exact key across duplicated fingerprint: ${network}, account=${account}`, () => { + const f = fixture(); + const unrelated = signer('AABBCCDD', NetworkType.MAINNET, SignerType.MY_KEEPER); + const ordinarySeed = signer('AABBCCDD', network, SignerType.SEED_WORDS, account); + f.collections.Signer.push(unrelated, ordinarySeed); + const vault = { id: 'fixture', networkType: network, signers: [{ + masterFingerprint: ordinarySeed.masterFingerprint, + ...ordinarySeed.signerXpubs[XpubTypes.P2WPKH][0], xpriv: 'fixture-private', + }] }; + assert.equal(f.scope.sanitizeVaultSignersForSeedKeyBackup(vault).signers[0].xpriv, null); + assert.equal(vault.signers[0].xpriv, 'fixture-private'); + }); +} + +for (const type of Object.values(SignerType)) { + test(`vault sanitizer preserves existing backup policy for ${type}`, () => { + const f = fixture(); + const record = signer('AABBCCDD', NetworkType.MAINNET, type); + f.collections.Signer.push(record); + const vault = { id: 'fixture', networkType: NetworkType.MAINNET, signers: [{ + masterFingerprint: record.masterFingerprint, ...record.signerXpubs[XpubTypes.P2WPKH][0], + xpriv: type === SignerType.SEED_WORDS || type === SignerType.MY_KEEPER ? 'fixture-private' : null, + }] }; + const before = plain(vault); + const sanitized = f.scope.sanitizeVaultSignersForSeedKeyBackup(vault); + assert.equal(sanitized.signers[0].xpriv, type === SignerType.SEED_WORDS ? null : vault.signers[0].xpriv); + assert.deepEqual(plain(vault), before); + }); +} + +for (const problem of ['missing', 'ambiguous', 'different-public-key', 'different-path', 'server-wrong-network']) { + test(`vault key cannot be silently classified for backup: ${problem}`, () => { + const f = fixture(); + const record = signer('AABBCCDD', NetworkType.MAINNET, + problem === 'server-wrong-network' ? SignerType.POLICY_SERVER : SignerType.SEED_WORDS); + const vault = { id: 'fixture', networkType: NetworkType.MAINNET, signers: [{ + masterFingerprint: record.masterFingerprint, ...record.signerXpubs[XpubTypes.P2WPKH][0], + xpriv: 'fixture-private', + }] }; + if (problem === 'different-public-key') vault.signers[0].xpub = 'different-public-fixture'; + if (problem === 'different-path') vault.signers[0].derivationPath = "m/86'/0'/0'"; + if (problem === 'server-wrong-network') record.networkType = NetworkType.TESTNET; + if (problem !== 'missing') f.collections.Signer.push(record); + if (problem === 'ambiguous') f.collections.Signer.push(plain(record)); + assert.throws(() => f.scope.sanitizeVaultSignersForSeedKeyBackup(vault), /Cannot identify a vault key/); + }); +} + +test('fingerprint migration applies the same seed-secret exclusion as normal backups', async () => { + const f = fixture(); + const old = signer('AAAAAAAA', NetworkType.MAINNET, SignerType.SEED_WORDS); + const updated = plain(old); + updated.masterFingerprint = 'BBBBBBBB'; + updated.signerXpubs[XpubTypes.P2WPKH][0].xpriv = 'disposable-private'; + f.collections.Signer.push(old); + f.scope.dbManager.getObjectByQuery = () => []; + let migrated; + f.scope.Relay.migrateXfp = async (_appId, records) => { migrated = records[0]; return true; }; + assert.equal(await f.run('mergeSimilarKeysWorker', { payload: { signer: updated } }), true); + const restored = JSON.parse(encryption.decrypt(keyFor(f), migrated.newSignerDetails)); + assert.equal(restored.signerXpubs[XpubTypes.P2WPKH][0].xpriv, null); + assert.equal(updated.signerXpubs[XpubTypes.P2WPKH][0].xpriv, 'disposable-private'); + assert.equal(restored.masterFingerprint, 'BBBBBBBB'); +}); + +function registrationFixture(options = {}) { + const f = fixture(options); + const device = signer('AABBCCDD', NetworkType.MAINNET); + device.id = f.scope.getKeyUID(device); + const key = { masterFingerprint: device.masterFingerprint, + ...device.signerXpubs[XpubTypes.P2WPKH][0], registeredVaults: [] }; + f.collections.Signer.push(device); + f.collections.Vault.push(...['vault-a', 'vault-b'].map((id) => ({ id, networkType: NetworkType.MAINNET, + signers: [key], scheme: { m: 1, n: 1 } }))); + f.scope.dbManager.getObjectByPrimaryId = () => ({ toJSON: () => plain(key) }); + f.scope.dbManager.updateObjectByPrimaryId = (_schema, _field, _id, patch) => { + if (options.writeFails) return false; + Object.assign(key, patch); + return true; + }; + const uploaded = []; + f.scope.Relay.updateVaultImage = async (payload) => { + if (options.uploadFails) throw Error('fixture offline'); + uploaded.push(payload); + return { updated: true }; + }; + return { f, key, uploaded }; +} + +test('key registration is backed up after persistence in every containing vault', async () => { + const { f, key, uploaded } = registrationFixture(); + const registration = { vaultId: 'vault-a', registered: true, registrationInfo: 'fixture-info', hmac: 'fixture-hmac' }; + await f.run('updateKeyDetailsWorker', { payload: { signer: key, key: 'registered', value: registration } }); + assert.deepEqual(uploaded.map((payload) => payload.vaultId), ['vault-a', 'vault-b']); + for (const payload of uploaded) { + const restored = JSON.parse(encryption.decrypt(keyFor(f), payload.vault)); + assert.deepEqual(restored.signers[0].registeredVaults, [registration]); + } +}); + +for (const options of [{ online: false }, { uploadFails: true }]) { + test(`key registration survives backup failure with retry pending: ${JSON.stringify(options)}`, async () => { + const { f, key, uploaded } = registrationFixture(options); + await f.run('updateKeyDetailsWorker', { payload: { signer: key, key: 'registered', value: { vaultId: 'vault-a', registered: true } } }); + assert.equal(key.registeredVaults[0].registered, true); + assert.equal(uploaded.length, 0); + assert.equal(f.state.bhr.pendingAllBackup, true); + }); +} + +test('disabled server backup preserves local key registration without uploading', async () => { + const { f, key, uploaded } = registrationFixture({ bhr: { automaticCloudBackup: false } }); + await f.run('updateKeyDetailsWorker', { payload: { signer: key, key: 'registered', value: { vaultId: 'vault-a', registered: true } } }); + assert.equal(key.registeredVaults[0].registered, true); + assert.equal(uploaded.length, 0); + assert.equal(f.state.bhr.pendingAllBackup, false); +}); + +test('failed local key registration write never publishes an unpersisted success', async () => { + const { f, key, uploaded } = registrationFixture({ writeFails: true }); + assert.equal(await f.run('updateKeyDetailsWorker', { payload: { signer: key, key: 'registered', value: { vaultId: 'vault-a', registered: true } } }), false); + assert.equal(uploaded.length, 0); + assert.equal(key.registeredVaults.length, 0); + assert.ok(f.actions.some((action) => action.type === 'relaySignersUpdateFail' && action.payload === 'Key registration could not be saved')); +}); + +test('remembering an imported seed for signing remains local and does not trigger a vault upload', async () => { + const { f, key, uploaded } = registrationFixture(); + await f.run('updateKeyDetailsWorker', { payload: { signer: key, key: 'xpriv', value: 'disposable-local-private' } }); + assert.equal(key.xpriv, 'disposable-local-private'); + assert.equal(uploaded.length, 0); +}); diff --git a/tests/recovery/manualCoinChoices.test.cjs b/tests/recovery/manualCoinChoices.test.cjs new file mode 100644 index 0000000000..2f7a4ae47c --- /dev/null +++ b/tests/recovery/manualCoinChoices.test.cjs @@ -0,0 +1,136 @@ +const { test } = require('node:test'); +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const path = require('node:path'); +const vm = require('node:vm'); +const ts = require('typescript'); +const { image, makeImage, harness } = require('./helpers.cjs'); +const { fixture, enums, encryption, wallet } = require('./sagaHarness.cjs'); +const clone = value => JSON.parse(JSON.stringify(value)); +const coin = (spendability = 'doNotSpend', extra = {}) => ({ + txId: 'disposable-transaction', vout: 0, value: 10000, address: 'disposable-address', + spendability, isManualOverride: true, ...extra, +}); +const withCoins = (kind = 'Wallet') => ({ + ...wallet('manual-choice', enums.NetworkType.MAINNET), + entityKind: kind === 'Vault' ? enums.EntityKind.VAULT : enums.EntityKind.WALLET, + signers: [], scheme: { m: 1, n: 1 }, + specs: { confirmedUTXOs: [coin('spendable', { isManualOverride: false })], + unconfirmedUTXOs: [], transactions: [], addresses: { external: {}, internal: {} } }, +}); +function loadFunctions(scope, file, names) { + const source = ts.createSourceFile(file, fs.readFileSync(path.join(__dirname, '../..', file), 'utf8'), ts.ScriptTarget.Latest, true); + const code = source.statements.filter(node => ts.isFunctionDeclaration(node) && names.includes(node.name?.text)) + .map(node => node.getText(source).replace(/^export /, '')).join('\n'); + assert.equal(source.statements.filter(node => ts.isFunctionDeclaration(node) && names.includes(node.name?.text)).length, names.length); + vm.runInNewContext(ts.transpileModule(code, { compilerOptions: { target: ts.ScriptTarget.ES2020 } }).outputText, scope); +} +function sagaFixture(options = {}) { + const f = fixture(options); + f.scope.getJSONFromRealmObject = clone; + f.scope.captureError = error => { throw error; }; + f.scope.dbManager.getObjectById = (kind, id) => f.collections[kind].find(row => row.id === id); + f.scope.dbManager.updateObjectById = (kind, id, update) => { + if (options.rejectLocalWrite) return false; + Object.assign(f.scope.dbManager.getObjectById(kind, id), clone(update)); + return true; + }; + f.scope.Relay.updateVaultImage = async payload => { + f.calls.push(['vault', payload]); + if (options.incrementalError) throw Error('offline'); + if (options.rejectIncremental) return { updated: false }; + f.remote.vault = payload.vault; + return { updated: true }; + }; + loadFunctions(f.scope, 'src/store/sagas/bhr.ts', ['updateVaultImageWorker']); + loadFunctions(f.scope, 'src/store/sagas/utxos.ts', ['markUTXOSpendabilityWorker']); + return f; +} +for (const kind of ['wallets', 'vaults']) test(`${kind}: compare manual choices but ignore chain-derived UTXO fields and confirmation order`, async () => { + const local = makeImage(), remote = makeImage(); + local[kind].wallet = withCoins(); remote[kind].wallet = clone(local[kind].wallet); + local[kind].wallet.specs.confirmedUTXOs = [coin()]; + assert.equal(await image.compareImages(local, remote), 'different'); + remote[kind].wallet.specs.confirmedUTXOs = []; + remote[kind].wallet.specs.unconfirmedUTXOs = [coin('doNotSpend', { value: 20000, height: 0 })]; + assert.equal(await image.compareImages(local, remote), 'matched'); + remote[kind].wallet.specs.unconfirmedUTXOs[0].spendability = 'spendable'; + assert.equal(await image.compareImages(local, remote), 'different'); +}); + +test('upgrade inspection detects a missing manual choice and repair includes it in encrypted readback', async () => { + const f = harness(); f.local.Wallet.push(withCoins()); + assert.equal(await f.run(true), 'verified'); + f.local.Wallet[0].specs.confirmedUTXOs[0] = coin(); + assert.equal(await f.run(), 'different'); + assert.equal(await f.run(true), 'verified'); + const restored = JSON.parse(encryption.decrypt(encryption.generateEncryptionKey(f.app.primarySeed), f.remote.wallets['manual-choice'])); + assert.equal(restored.specs.confirmedUTXOs[0].spendability, 'doNotSpend'); + assert.equal(restored.specs.confirmedUTXOs[0].isManualOverride, true); +}); + +for (const kind of ['Wallet', 'Vault']) test(`${kind}: manual choice persists before incremental backup and invalidates prior verification`, async () => { + const f = sagaFixture(); const w = withCoins(kind); f.collections[kind].push(w); + const relayName = kind === 'Vault' ? 'updateVaultImage' : 'updateAppImage'; + const original = f.scope.Relay[relayName]; + f.scope.Relay[relayName] = payload => { + assert.equal(f.collections[kind][0].specs.confirmedUTXOs[0].spendability, 'doNotSpend'); + return original(payload); + }; + await f.run('markUTXOSpendabilityWorker', { payload: { wallet: w, txId: coin().txId, vout: 0, spendability: 'doNotSpend' } }); + const encrypted = kind === 'Vault' ? f.remote.vault : f.remote.wallets[w.id]; + const restored = JSON.parse(encryption.decrypt(encryption.generateEncryptionKey(f.app.primarySeed), encrypted)); + assert.equal(restored.specs.confirmedUTXOs[0].spendability, 'doNotSpend'); + assert.ok(f.actions.some(action => action.type === 'invalidateBackupRepair')); +}); + +for (const kind of ['Wallet', 'Vault']) for (const options of [ + { bhr: { automaticCloudBackup: false } }, { online: false }, { rejectIncremental: true }, { incrementalError: true }, +]) test(`${kind}: local manual choice survives backup unavailability ${JSON.stringify(options)}`, async () => { + const f = sagaFixture(options); const w = withCoins(kind); f.collections[kind].push(w); + await f.run('markUTXOSpendabilityWorker', { payload: { wallet: w, txId: coin().txId, vout: 0, spendability: 'doNotSpend' } }); + assert.equal(f.collections[kind][0].specs.confirmedUTXOs[0].spendability, 'doNotSpend'); + if (options.bhr || options.online === false) assert.equal(f.calls.length, 0); + if (!options.bhr) assert.equal(f.state.bhr.pendingAllBackup, true); + assert.equal(f.remote.wallets[w.id], undefined); + assert.equal(f.remote.vault, undefined); +}); + +test('missing outpoint and repeated identical choice do not upload', async () => { + const f = sagaFixture(); const w = withCoins(); w.specs.confirmedUTXOs[0] = coin(); f.collections.Wallet.push(w); + for (const txId of ['missing', coin().txId]) { + await f.run('markUTXOSpendabilityWorker', { payload: { wallet: w, txId, vout: 0, spendability: 'doNotSpend' } }); + } + assert.equal(f.calls.length, 0); +}); + +test('rejected local persistence never uploads an unapplied coin choice', async () => { + const f = sagaFixture({ rejectLocalWrite: true }); const w = withCoins(); f.collections.Wallet.push(w); + await f.run('markUTXOSpendabilityWorker', { payload: { wallet: w, txId: coin().txId, vout: 0, spendability: 'doNotSpend' } }); + assert.equal(f.collections.Wallet[0].specs.confirmedUTXOs[0].spendability, 'spendable'); + assert.equal(f.calls.length, 0); +}); + +for (const kind of ['Wallet', 'Vault']) for (const choice of ['doNotSpend', 'spendable']) test(`${kind}: restored manual ${choice} survives real refresh orchestration`, async () => { + const f = sagaFixture(); const restored = withCoins(kind); restored.specs.confirmedUTXOs = [coin(choice)]; + f.collections[kind].push(restored); + f.scope.ELECTRUM_CLIENT = { isClientConnected: true }; + f.scope.WalletUtilities.getNetworkByType = () => ({}); + f.scope.WalletOperations = { syncWalletsViaElectrumClient: async () => { + const synced = clone(restored); + synced.specs.confirmedUTXOs = [coin('spendable', { isManualOverride: false })]; + return { synchedWallets: [{ synchedWallet: synced, newUTXOs: [] }] }; + } }; + // Oppose the user's choice to prove the production manual-override branch wins. + f.scope.classifyDustByAddress = () => ({ + taintedAddresses: new Set(choice === 'spendable' ? [coin().address] : []), + initialTaintAddresses: new Set(), dustSpendTxids: new Set(), + }); + f.scope.setSyncing = payload => ({ type: 'syncing', payload }); + f.scope.setElectrumNotConnectedErr = error => { throw Error(error); }; + f.scope.ELECTRUM_NOT_CONNECTED_ERR = 'offline'; f.scope.ELECTRUM_NOT_CONNECTED_ERR_TOR = 'tor-offline'; + loadFunctions(f.scope, 'src/store/sagas/wallets.ts', ['refreshWalletsWorker']); + await f.run('refreshWalletsWorker', { payload: { wallets: [restored], options: { hardRefresh: true } } }); + assert.equal(f.collections[kind][0].specs.confirmedUTXOs[0].spendability, choice); + assert.equal(f.collections[kind][0].specs.confirmedUTXOs[0].isManualOverride, true); +}); diff --git a/tests/recovery/nodeReplacement.test.cjs b/tests/recovery/nodeReplacement.test.cjs new file mode 100644 index 0000000000..0d2a6fee98 --- /dev/null +++ b/tests/recovery/nodeReplacement.test.cjs @@ -0,0 +1,29 @@ +const { test } = require('node:test'); +const assert = require('node:assert/strict'); +const { fixture, encryption, wallet, enums } = require('./sagaHarness.cjs'); + +test('removing the final node emits an explicit empty-list replacement', async () => { + const f = fixture(); + await f.run('updateAppImageWorker', { payload: { updateNodes: true } }); + assert.equal(f.calls.length, 1); + assert.equal(f.calls[0][1].replaceNodes, true); + assert.deepEqual(Array.from(f.calls[0][1].nodes), []); +}); + +test('unrelated wallet update never requests node replacement', async () => { + const f = fixture(); + await f.run('updateAppImageWorker', { payload: { wallets: [wallet('one', enums.NetworkType.MAINNET)] } }); + assert.equal(f.calls[0][1].replaceNodes, false); +}); + +test('node-list replacement encrypts nodes without changing local connection state', async () => { + const f = fixture(); + const node = { id: 'disposable', host: 'fixture.invalid', port: 50002, isConnected: true }; + f.collections.NodeConnect.push(node); + await f.run('updateAppImageWorker', { payload: { updateNodes: true } }); + const sent = f.calls[0][1]; + assert.equal(sent.replaceNodes, true); + const restored = JSON.parse(encryption.decrypt(encryption.generateEncryptionKey(f.app.primarySeed), sent.nodes[0])); + assert.deepEqual(restored, { ...node, isConnected: false }); + assert.equal(node.isConnected, true); +}); diff --git a/tests/recovery/recoveryErrorFeedback.test.cjs b/tests/recovery/recoveryErrorFeedback.test.cjs new file mode 100644 index 0000000000..7071af5184 --- /dev/null +++ b/tests/recovery/recoveryErrorFeedback.test.cjs @@ -0,0 +1,81 @@ +const { test } = require('node:test'); +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const path = require('node:path'); +const vm = require('node:vm'); +const ts = require('typescript'); + +const file = 'src/screens/Recovery/EnterSeedScreen.tsx'; +const source = ts.createSourceFile(file, + fs.readFileSync(path.join(__dirname, '../..', file), 'utf8'), + ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX); +let callback; +function visit(node) { + if (ts.isCallExpression(node) && node.expression.getText(source) === 'useEffect' && + node.arguments[1]?.getText(source) === '[appImageError]') + callback = node.arguments[0].getText(source); + ts.forEachChild(node, visit); +} +visit(source); +assert.ok(callback, 'Production recovery error feedback effect exists'); +const code = ts.transpileModule(`(${callback})()`, { + compilerOptions: { target: ts.ScriptTarget.ES2020, jsx: ts.JsxEmit.React }, +}).outputText; +const seed = require('../../src/context/Localization/language/en.json').seed; + +function feedback(appImageError) { + const observed = { loading: true, invalid: false, network: false, messages: [], actions: [] }; + vm.runInNewContext(code, { + appImageError, + seed, + React: { createElement: (type) => ({ type }) }, + ToastErrorIcon: 'error-icon', + dispatch: (action) => observed.actions.push(action), + setAppImageError: (payload) => ({ type: 'setAppImageError', payload }), + setRecoveryLoading: (value) => { observed.loading = value; }, + setShowNetworkModal: (value) => { observed.network = value; }, + openInvalidSeedsModal: () => { observed.loading = false; observed.invalid = true; }, + showToast: (message) => observed.messages.push(message), + }); + return observed; +} + +test('invalid mnemonic retains the invalid Recovery Key feedback', () => { + const result = feedback('Invalid mnemonic'); + assert.equal(result.invalid, true); + assert.equal(result.loading, false); + assert.equal(result.network, false); + assert.deepEqual(result.messages, []); +}); + +test('network error stops recovery and retains the existing network modal', () => { + const result = feedback('Network Error'); + assert.equal(result.network, true); + assert.equal(result.loading, false); + assert.equal(result.invalid, false); + assert.deepEqual(result.messages, []); + assert.deepEqual(result.actions, [{ type: 'setAppImageError', payload: '' }]); +}); + +for (const error of ['Recovery data unavailable', 'Incomplete backup response', + 'Invalid backup record', 'Backup request could not be completed']) { + test(`${error} offers retry without blaming the Recovery Key or clearing entered words`, () => { + const result = feedback(error); + assert.equal(result.loading, false); + assert.equal(result.invalid, false); + assert.equal(result.network, false); + assert.deepEqual(result.messages, [seed.recoveryIncomplete]); + assert.match(result.messages[0], /try again/i); + assert.doesNotMatch(result.messages[0], /invalid (seed|mnemonic|recovery key)/i); + assert.deepEqual(result.actions, [{ type: 'setAppImageError', payload: '' }]); + }); +} + +test('empty recovery error does not interrupt loading or display an error', () => { + const result = feedback(''); + assert.equal(result.loading, true); + assert.equal(result.invalid, false); + assert.equal(result.network, false); + assert.deepEqual(result.messages, []); + assert.deepEqual(result.actions, []); +}); diff --git a/tests/recovery/repair.test.cjs b/tests/recovery/repair.test.cjs new file mode 100644 index 0000000000..c6b7d39151 --- /dev/null +++ b/tests/recovery/repair.test.cjs @@ -0,0 +1,119 @@ +const { test } = require('node:test'); +const assert = require('node:assert/strict'); +const { encryption, image, makeImage, harness, wallet } = require('./helpers.cjs'); + +test('upgrade check detects omissions without uploading; explicit repair verifies readback', async () => { + const f = harness(); f.local.Wallet.push(wallet()); + assert.equal(await f.run(), 'different'); assert.deepEqual(f.calls, ['getBackupSnapshot']); + assert.equal(await f.run(true), 'verified'); + assert.deepEqual(f.phases, ['checking','different','checking','preparing','uploading','verifying','verified']); + assert.deepEqual(JSON.parse(encryption.decrypt(encryption.generateEncryptionKey(f.app.primarySeed), f.remote.wallets.wallet)), wallet()); +}); + +test('matching upgrade only reads and does not reupload', async () => { + const f = harness(); f.local.Wallet.push(wallet()); await f.run(true); f.calls.length = 0; + assert.equal(await f.run(), 'verified'); assert.deepEqual(f.calls, ['getBackupSnapshot']); +}); + +for (const failure of ['readError','reject','corruptWrite']) test(`${failure} never claims success`, async () => { + const f = harness({ [failure]: true }); f.local.Wallet.push(wallet()); + assert.equal(await f.run(true), 'unverified'); assert.ok(!f.phases.includes('verified')); + if (failure === 'readError') assert.deepEqual(f.calls, ['getBackupSnapshot']); +}); + +test('lost upload acknowledgement is resolved by readback without duplicate upload', async () => { + const f = harness({ lostResponse: true }); f.local.Wallet.push(wallet()); + assert.equal(await f.run(true), 'verified'); assert.deepEqual(f.calls, ['getBackupSnapshot','repairAppBackup','getBackupSnapshot']); +}); + +test('server-only wallet is preserved and blocks replacement', async () => { + const f = harness(); f.remote.wallets.other = encryption.encrypt(encryption.generateEncryptionKey(f.app.primarySeed), JSON.stringify(wallet('other'))); + assert.equal(await f.run(true), 'conflict'); assert.deepEqual(f.calls, ['getBackupSnapshot']); assert.ok(f.remote.wallets.other); +}); + +test('same IDs with different recovery keys are a conflict', async () => { + const l = makeImage(), r = makeImage(); l.wallets.a = wallet(); r.wallets.a = wallet(); r.wallets.a.specs.xpub = 'other'; + assert.equal(await image.compareImages(l, r), 'conflict'); +}); + +test('same IDs with changed user metadata require an update', async () => { + const l = makeImage(), r = makeImage(); l.wallets.a = wallet(); r.wallets.a = wallet(); r.wallets.a.presentationData.name = 'Old'; + assert.equal(await image.compareImages(l, r), 'different'); +}); + +test('canonical comparison ignores object order and chain caches but retains address counters', async () => { + const l = makeImage(), r = makeImage(); l.wallets.a = wallet(); r.wallets.a = wallet(); r.wallets.a.specs.balances.confirmed = 100; + assert.equal(await image.compareImages(l, r), 'matched'); r.wallets.a.specs.nextFreeAddressIndex = 2; + assert.equal(await image.compareImages(l, r), 'conflict'); + assert.equal(image.canonical({ b:2,a:1 }), image.canonical({ a:1,b:2 })); +}); + +test('BTC both networks, USDT, vaults, keys, labels and nodes are verified without mutating local data', async () => { + const f = harness(); f.local.Wallet.push(wallet('main'), wallet('test','TESTNET')); + f.local.USDTWallet.push({ ...wallet('usdt'), specs: { privateKey:'fixture-only', address:'fixture-only' } }); + f.local.Signer.push({ id:'signer', masterFingerprint:'fixture', signerXpubs:{} }); + f.local.Vault.push({ ...wallet('vault'), archived:true, signers:[{ masterFingerprint:'fixture' }], scheme:{m:1,n:1} }); + f.local.Tags.push({ id:'tag', label:'label', ref:'tx', type:'TXN' }); f.local.NodeConnect.push({ id:'node', isConnected:true, host:'fixture' }); + const before = JSON.stringify(f.local); + assert.equal(await f.run(true), 'verified'); assert.equal(JSON.stringify(f.local), before); + assert.deepEqual(Object.keys(f.remote.wallets).sort(), ['main','test','usdt']); +}); + +test('malformed or partial server response is unavailable, never an empty snapshot', async () => { + for (const response of [{}, { appImage:{} }, { appImage:{appId:'another',wallets:{},signers:{},nodes:[]}, allVaultImages:[],labels:[] }]) { + const f = harness({ response }); f.local.Wallet.push(wallet()); + assert.equal(await f.run(true), 'unverified'); assert.deepEqual(f.calls, ['getBackupSnapshot']); + } +}); + +test('mutation arriving during upload invalidates completion and remains queued until readback', async () => { + let f; + f = harness({ post: async ({ path }) => { if (path.endsWith('repairAppBackup')) f.transport.markBackupMutation(f.app.id); } }); + f.local.Wallet.push(wallet()); assert.equal(await f.run(true), 'unverified'); assert.ok(!f.phases.includes('verified')); +}); + +test('account switch during check cannot upload one account with another account data', async () => { + const f = harness({ post: async ({ app }) => { app.id = 'second'; } }); f.local.Wallet.push(wallet()); + assert.equal(await f.run(true), 'unverified'); assert.deepEqual(f.calls, ['getBackupSnapshot']); +}); + +test('chunked encryption remains compatible with existing recovery including Unicode', async () => { + const record = { id:'fixture', text: ('A'.repeat(16370) + '🔑বাংলা').repeat(5) }; + let yielded = false; setTimeout(() => { yielded = true; }, 0); + const cipher = await image.encryptRecord('test-only', record); + assert.equal(yielded, true); assert.deepEqual(JSON.parse(encryption.decrypt('test-only',cipher)),record); + assert.deepEqual(JSON.parse(JSON.stringify(await image.decryptRecord('test-only',cipher))), record); +}); + +test('oversized record fails without uploading or truncating', async () => { + const f = harness(); f.local.Wallet.push({ ...wallet(), text:'x'.repeat(image.MAX_RECORD_BYTES) }); + assert.equal(await f.run(true),'unverified'); assert.deepEqual(f.calls,[]); +}); + + +test('server change after read rejects stale upload and preserves remote data', async () => { + const f = harness({ post: async ({ path, remote }) => { + if (path.endsWith('repairAppBackup')) remote.wallets.other = 'new-device-encrypted-data'; + } }); + f.local.Wallet.push(wallet()); + assert.equal(await f.run(true), 'unverified'); + assert.equal(f.remote.wallets.other, 'new-device-encrypted-data'); + assert.equal(f.remote.wallets.wallet, undefined); +}); + +test('relay without revision support fails closed without legacy replacement', async () => { + const f = harness({ response: { appImage:{appId:'disposable',wallets:{},signers:{},nodes:[],vaults:[],labels:[]},allVaultImages:[],labels:[] } }); + f.local.Wallet.push(wallet()); + assert.equal(await f.run(true), 'unverified'); + assert.deepEqual(f.calls, ['getBackupSnapshot']); +}); + +test('archived vault is uploaded with archived server metadata', async () => { + let archived; + const f = harness({ post: async ({ path,payload }) => { + if (path.endsWith('repairAppBackup')) archived=payload.vaultObject.vault.isArchived; + } }); + f.local.Vault.push({ ...wallet('vault'), archived:true,signers:[],scheme:{m:1,n:1} }); + assert.equal(await f.run(true), 'verified'); + assert.equal(archived,true); +}); diff --git a/tests/recovery/restoreIntegrity.test.cjs b/tests/recovery/restoreIntegrity.test.cjs new file mode 100644 index 0000000000..dc2b4f6025 --- /dev/null +++ b/tests/recovery/restoreIntegrity.test.cjs @@ -0,0 +1,203 @@ +const { test } = require('node:test'); +const assert = require('node:assert/strict'); +const { fixture, enums, encryption, wallet, signer } = require('./sagaHarness.cjs'); + +const plain = (value) => JSON.parse(JSON.stringify(value)); +function recoveryFixture() { + const f = fixture(); + const key = encryption.generateEncryptionKey(f.app.primarySeed); + const seal = (record) => encryption.encrypt(key, JSON.stringify(record)); + const mobile = signer('LOCAL001', enums.NetworkType.MAINNET, enums.SignerType.MY_KEEPER); + mobile.id = f.scope.getKeyUID(mobile); + const vault = { + ...wallet('vault', enums.NetworkType.MAINNET), + entityKind: enums.EntityKind.VAULT, + archived: true, + scheme: { m: 1, n: 1 }, + signers: [ + { + masterFingerprint: mobile.masterFingerprint, + xpub: mobile.signerXpubs[enums.XpubTypes.P2WPKH][0].xpub, + derivationPath: mobile.signerXpubs[enums.XpubTypes.P2WPKH][0].derivationPath, + }, + ], + }; + const label = { id: 'label', label: 'Disposable label', ref: 'tx', type: 'TXN' }; + const node = { + id: 42, + networkType: enums.NetworkType.MAINNET, + host: 'fixture', + isConnected: false, + }; + const btc = wallet('btc', enums.NetworkType.MAINNET); + const usdt = { + ...wallet('usdt', enums.NetworkType.MAINNET), + entityKind: enums.EntityKind.USDT_WALLET, + specs: { address: 'fixture', privateKey: 'fixture-only' }, + }; + f.remote.wallets = { btc: seal(btc), usdt: seal(usdt) }; + f.remote.signers = { [f.scope.getKeyUID(mobile)]: seal(mobile) }; + f.remote.nodes = [seal(node)]; + f.remote.vaults = ['vault']; + f.remote.labels = ['encrypted-label-id']; + const response = { + appImage: f.remote, + allVaultImages: [{ vaultId: 'vault', vault: seal(vault) }], + labels: [{ id: 'encrypted-label-id', content: seal(label) }], + }; + f.scope.Relay.getAppImage = async () => response; + const writes = []; + const create = f.scope.dbManager.createObject; + f.scope.dbManager.createObject = (schema, value) => { + writes.push(schema); + return create(schema, value); + }; + return { + ...f, + response, + writes, + expected: { mobile, vault, label, node, btc, usdt }, + restore: () => + f.run('getAppImageWorker', { payload: { primaryMnemonic: 'mock fixture only' } }), + }; +} +const completed = (f) => f.actions.some((a) => a.type === 'setAppCreated' && a.payload === true); +const errors = (f) => f.actions.filter((a) => a.type === 'setAppImageError' && a.payload); + +test('clean recovery recreates keys, BTC/USDT, archived vault, labels and custom nodes', async () => { + const f = recoveryFixture(); + await f.restore(); + assert.deepEqual(errors(f), []); + assert.equal(completed(f), true); + assert.deepEqual(plain(f.collections.Wallet), [f.expected.btc]); + assert.deepEqual(plain(f.collections.USDTWallet), [f.expected.usdt]); + assert.deepEqual( + plain(f.collections.Signer.find((s) => s.masterFingerprint === 'LOCAL001')), + f.expected.mobile + ); + assert.deepEqual(plain(f.collections.Vault), [f.expected.vault]); + assert.deepEqual(plain(f.collections.Tags), [f.expected.label]); + assert.deepEqual(plain(f.collections.NodeConnect), [{ ...f.expected.node, isConnected: true }]); +}); + +for (const kind of ['wallet', 'usdt', 'signer', 'vault', 'label', 'node']) { + test(`corrupt ${kind} prevents every recovery write and success`, async () => { + const f = recoveryFixture(); + if (kind === 'wallet') f.remote.wallets.btc = 'corrupt'; + if (kind === 'usdt') f.remote.wallets.usdt = 'corrupt'; + if (kind === 'signer') f.remote.signers[Object.keys(f.remote.signers)[0]] = 'corrupt'; + if (kind === 'vault') f.response.allVaultImages[0].vault = 'corrupt'; + if (kind === 'label') f.response.labels[0].content = 'corrupt'; + if (kind === 'node') f.remote.nodes[0] = 'corrupt'; + await f.restore(); + assert.equal(errors(f).length, 1); + assert.equal(completed(f), false); + assert.deepEqual(f.writes, []); + assert.equal(f.calls.length, 0, 'failed recovery must not upload an empty replacement'); + }); +} + +for (const schema of [ + 'KeeperApp', + 'Wallet', + 'USDTWallet', + 'Signer', + 'Vault', + 'Tags', + 'NodeConnect', +]) { + for (const failedResult of [false, undefined]) { + test(`${schema} persistence returning ${failedResult} cannot report recovery success`, async () => { + const f = recoveryFixture(); + const create = f.scope.dbManager.createObject; + f.scope.dbManager.createObject = (target, value) => + target === schema ? failedResult : create(target, value); + await f.restore(); + assert.equal(errors(f).length, 1); + assert.equal(completed(f), false); + assert.equal( + f.actions.some( + (a) => a.type === 'setRecoveryKeyStatus' && a.payload?.status === 'confirmed' + ), + false + ); + }); + } +} + +for (const kind of [ + 'offline', + 'missing image', + 'other account', + 'missing vault', + 'missing label', + 'wrong vault reference', + 'wrong label reference', + 'wrong encrypted vault identity', +]) { + test(`${kind} is never treated as a successful empty recovery`, async () => { + const f = recoveryFixture(); + if (kind === 'offline') + f.scope.Relay.getAppImage = async () => { + throw Error('Network Error'); + }; + if (kind === 'missing image') delete f.response.appImage; + if (kind === 'other account') f.remote.appId = 'other'; + if (kind === 'missing vault') f.response.allVaultImages = []; + if (kind === 'missing label') f.response.labels = []; + if (kind === 'wrong vault reference') f.remote.vaults = ['another-vault']; + if (kind === 'wrong label reference') f.remote.labels = ['another-label']; + if (kind === 'wrong encrypted vault identity') { + f.remote.vaults = ['another-vault']; + f.response.allVaultImages[0].vaultId = 'another-vault'; + } + await f.restore(); + assert.equal(errors(f).length, 1); + assert.equal(completed(f), false); + assert.deepEqual(f.writes, []); + }); +} + +test('explicit empty backup keeps the established first-wallet recovery path', async () => { + const f = fixture(); + let recreated = 0; + f.scope.WalletUtilities.getDerivationPath = () => 'fixture'; + f.scope.DerivationPurpose = { BIP84: 'BIP84' }; + f.scope.addNewWalletsWorker = () => { + recreated++; + return true; + }; + await f.run('getAppImageWorker', { payload: { primaryMnemonic: 'mock fixture only' } }); + assert.deepEqual(errors(f), []); + assert.equal(completed(f), true); + assert.equal(recreated, 1); +}); + +test('failed first-wallet recreation cannot complete empty-backup recovery', async () => { + const f = fixture(); + f.scope.WalletUtilities.getDerivationPath = () => 'fixture'; + f.scope.DerivationPurpose = { BIP84: 'BIP84' }; + f.scope.addNewWalletsWorker = () => false; + await f.run('getAppImageWorker', { payload: { primaryMnemonic: 'mock fixture only' } }); + assert.equal(completed(f), false); + assert.equal(errors(f).length, 1); +}); + +for (const failure of ['missing key', 'missing private material']) { + test(`regenerated Recovery Key signer with ${failure} prevents success`, async () => { + const f = recoveryFixture(); + const expected = signer('RECOVERY', enums.NetworkType.MAINNET, enums.SignerType.SEED_WORDS); + expected.signerXpubs[enums.XpubTypes.P2WPKH][0].xpriv = 'fixture-derived-private'; + f.scope.setupRecoveryKeySigningKey = () => expected; + f.scope.addSigningDeviceWorker = () => { + if (failure === 'missing private material') { + const incomplete = plain(expected); + incomplete.signerXpubs[enums.XpubTypes.P2WPKH][0].xpriv = null; + f.collections.Signer.push(incomplete); + } + }; + await f.restore(); + assert.equal(completed(f), false); + assert.equal(errors(f).length, 1); + }); +} diff --git a/tests/recovery/sagaHarness.cjs b/tests/recovery/sagaHarness.cjs new file mode 100644 index 0000000000..3a378a08d9 --- /dev/null +++ b/tests/recovery/sagaHarness.cjs @@ -0,0 +1,290 @@ +// Runs the production saga functions with redux-saga and real AES encryption. +// Realm and the relay are disposable in-memory adapters; no user keys or network calls. +const fs = require('node:fs'); +const path = require('node:path'); +const vm = require('node:vm'); +const ts = require('typescript'); +const { runSaga } = require('redux-saga'); +const effects = require('redux-saga/effects'); + +function functions(file, names) { + const source = ts.createSourceFile( + file, + fs.readFileSync(path.join(__dirname, '../..', file), 'utf8'), + ts.ScriptTarget.Latest, + true + ); + return source.statements + .filter((n) => (ts.isFunctionDeclaration(n) && names.includes(n.name?.text)) || + (ts.isVariableStatement(n) && n.declarationList.declarations.some(d => names.includes(d.name?.text)))) + .map((n) => n.getText(source).replace(/^export /, '').replace(/^const /, 'var ')) + .join('\n'); +} +function loadModule(file, mocks = {}) { + const module = { exports: {} }; + const code = ts.transpileModule(fs.readFileSync(path.join(__dirname, '../..', file), 'utf8'), { + compilerOptions: { + module: ts.ModuleKind.CommonJS, + target: ts.ScriptTarget.ES2020, + esModuleInterop: true, + }, + }).outputText; + vm.runInNewContext(code, { + module, + exports: module.exports, + require: (name) => (name in mocks ? mocks[name] : require(name)), + }); + return module.exports; +} +const enums = loadModule('src/services/wallets/enums/index.ts'); +const { NetworkType, WalletType, EntityKind } = enums; +const encryption = loadModule('src/utils/service-utilities/encryption.ts', { + 'react-native-rsa-native': { RSA: {} }, +}); +const { image } = require('./helpers.cjs'); +const { prepareRecoveryImage } = loadModule('src/services/backup/restore.ts', { './image': image }); +const account = loadModule('src/store/reducers/account.ts', { + 'src/services/wallets/enums': enums, + 'src/models/enums/BHR': loadModule('src/models/enums/BHR.ts'), +}); +const wallet = (id, networkType, instanceNum = 0, type = WalletType.DEFAULT) => ({ + id, + networkType, + type, + entityKind: EntityKind.WALLET, + derivationDetails: { instanceNum, xDerivationPath: "m/84'/0'/0'" }, + presentationData: { name: 'Disposable fixture', visibility: 'DEFAULT' }, + specs: { receivingAddress: `${id}-address`, balances: { confirmed: 10000, unconfirmed: 0 } }, +}); +const signer = (fingerprint, networkType, type = enums.SignerType.COLDCARD, account = 0) => ({ + masterFingerprint: fingerprint, + type, + storageType: type === enums.SignerType.COLDCARD ? enums.SignerStorage.COLD : enums.SignerStorage.WARM, + networkType, + signerName: 'Disposable key', + signerDescription: 'Key lifecycle regression fixture', + signerXpubs: { + [enums.XpubTypes.P2WPKH]: [{ xpub: `${fingerprint}-${networkType}-${account}-public`, + derivationPath: `m/84'/${networkType === NetworkType.MAINNET ? 0 : 1}'/${account}'`, + xpriv: null }], + }, + addedOn: '2026-09-01T00:00:00.000Z', + lastHealthCheck: '2026-09-01T00:00:00.000Z', + healthCheckDetails: [{ type: 'fixture-check', actionDate: '2026-09-01T00:00:00.000Z' }], + hidden: false, + archived: false, +}); +function fixture(options = {}) { + const appId = require('crypto').createHash('sha256').update(Buffer.from('ab'.repeat(32), 'hex')).digest('hex'); + const state = { + bhr: { + automaticCloudBackup: true, + pendingAllBackup: false, + backupRepairCompletedByAppId: { [appId]: true }, + ...options.bhr, + }, + settings: { bitcoinNetworkType: NetworkType.MAINNET }, + account: account.default( + undefined, + account.setTempDetails({ hash: 'fixture', realmId: 'fixture', accountIdentifier: '' }) + ), + }; + const app = { + id: appId, + primarySeed: 'ab'.repeat(32), + publicId: 'fixture', + subscription: { level: 1 }, + networkType: NetworkType.MAINNET, + version: '2.5.16', + }; + const collections = { + Wallet: [], + USDTWallet: [], + Signer: [], + Vault: [], + Tags: [], + NodeConnect: [], + UAI: [], + VersionHistory: [], + BackupHistory: [], + }; + const remote = { appId, wallets: {}, signers: {}, nodes: [], version: '2.5.16' }; + const calls = []; + const actions = []; + const scope = { + ...effects, + markBackupMutation: () => {}, + ...encryption, + ...enums, + ...account, + prepareRecoveryImage, + console: { log() {}, error() {} }, + Buffer, + _: require('lodash'), + // Metro normally transforms idx into guarded property access. The extracted + // functions run in a separate VM realm, so emulate those access semantics. + idx: (value, getter) => { + try { return getter(value); } catch (error) { + if (error.name === 'TypeError') return undefined; + throw error; + } + }, + Realm: { UpdateMode: { Modified: 'modified' } }, + captureError: (error) => { throw error; }, + getJSONFromRealmObject: (object) => object, + RealmSchema: new Proxy({}, { get: (_, name) => name }), + dbManager: { + getObjectByIndex: (schema, _index, all) => + schema === 'KeeperApp' ? app : all ? collections[schema] : collections[schema]?.[0], + getCollection: (schema) => collections[schema] || [], + getObjectByField: (schema) => collections[schema] || [], + createObject: (schema, value) => { + if (schema === 'KeeperApp') Object.assign(app, value); + else { + const rows = (collections[schema] ||= []); + const index = rows.findIndex((row) => row.id && row.id === value.id); + if (index < 0) rows.push(value); + else rows[index] = value; + } + return true; + }, + createObjectBulk: (schema, values) => { + values.forEach((value) => scope.dbManager.createObject(schema, value)); + return true; + }, + deleteObjectById: (schema, id) => { + collections[schema] = collections[schema].filter((row) => String(row.id) !== String(id)); + return true; + }, + updateObjectById: (schema, id, patch) => { + const row = collections[schema].find((record) => String(record.id) === String(id)); + if (!row) return false; + Object.assign(row, patch); + return true; + }, + updateObjectByQuery: (schema, predicate, patch) => { + collections[schema].filter(predicate).forEach((row) => Object.assign(row, patch)); + }, + }, + Relay: { + updateAppImage: async (payload) => { + calls.push(['incremental', payload]); + if (options.incrementalError) throw Error('offline'); + if (options.rejectIncremental) return { updated: false, error: 'rejected' }; + Object.assign(remote.wallets, payload.walletsObject); + Object.assign(remote.signers, payload.signersObject); + if (payload.nodes?.length) remote.nodes = payload.nodes; + return { updated: true, error: '' }; + }, + backupAllSignersAndVaults: async (payload) => { + calls.push(['full', payload]); + if (options.fullError) throw Error('offline'); + if (options.rejectFull) return { updated: false }; + remote.wallets = { ...payload.walletObject }; + return { updated: true, error: '' }; + }, + deleteAppImageEntity: async ({ walletIds }) => { + calls.push(['delete']); + walletIds.forEach((id) => delete remote.wallets[id]); + return { updated: true, error: '' }; + }, + getAppImage: async () => ({ appImage: remote, labels: [], allVaultImages: [] }), + }, + NetInfo: { fetch: async () => ({ isConnected: options.online !== false }) }, + addToUaiStackWorker: () => {}, + uaiActionedWorker: () => {}, + uaiType: { SERVER_BACKUP_FAILURE: 'backup-failed' }, + addNewWallet: (_type, details) => details.fixture, + DeviceInfo: { getVersion: () => '2.5.16', getBuildNumber: () => '624' }, + bip39: { + validateMnemonic: () => true, + mnemonicToSeedSync: () => Buffer.from('ab'.repeat(32), 'hex'), + }, + crypto: require('crypto'), + BIP85: { bip39MnemonicToEntropy: () => Buffer.from('fixture') }, + config: { BIP85_IMAGE_ENCRYPTIONKEY_DERIVATION_PATH: 'fixture', isDevMode: () => false }, + WalletUtilities: { getFingerprintFromSeed: () => 'AAAAAAAA' }, + SubscriptionTier: { L1: 'L1', L3: 'L3' }, + AppSubscriptionLevel: { L1: 1, L3: 3 }, + BackupType: { SEED: 'SEED' }, + BackupAction: { SEED_BACKUP_CONFIRMED: 'confirmed' }, + moment: () => ({ unix: () => 1 }), + semver: require('semver'), + setupRecoveryKeySigningKey: () => signer('RECOVERY', NetworkType.MAINNET, enums.SignerType.SEED_WORDS), + applyUpgradeSequence: () => {}, + }; + const names = [ + 'setBackupRepairCompleted', + 'invalidateBackupRepair', + 'setBackupAllSuccess', + 'setBackupAllFailure', + 'setBackupAllLoading', + 'setPendingAllBackup', + 'setHomeToastMessage', + 'setRelayWalletUpdateLoading', + 'relayWalletUpdateSuccess', + 'relayWalletUpdateFail', + 'setRelaySignersUpdateLoading', + 'relaySignersUpdateSuccess', + 'relaySignersUpdateFail', + 'setAppImageError', + 'setSeedConfirmed', + 'setBackupType', + 'uaiActioned', + 'setAppId', + 'setAppCreated', + 'autoSyncWallets', + 'uaiChecks', + 'loadConciergeUser', + 'loadConciergeTickets', + 'appImagerecoveryRetry', + ]; + names.forEach((name) => { + scope[name] = (payload) => ({ type: name, payload }); + }); + const code = + functions('src/constants/defaultData.tsx', ['RECOVERY_KEY_SIGNER_NAME']) + '\n' + + functions('src/store/sagas/upgrade.ts', ['KEY_MANAGEMENT_VERSION']) + '\n' + + functions('src/utils/utilities.ts', [ + 'getAccountFromSigner', 'getKeyUID', 'checkSignerAccountsMatch', + 'sanitizeSeedKeyForBackup', 'sanitizeVaultSignersForSeedKeyBackup', + ]) + '\n' + + functions('src/store/sagas/bhr.ts', [ + 'deleteAppImageEntityWorker', + 'checkBackupCondition', + 'setServerBackupFailed', + 'updateAppImageWorker', + 'updateVaultImageWorker', + 'recoverApp', + 'writeRecoveredObject', + 'getAppImageWorker', + ]) + + '\n' + + functions('src/store/sagas/wallets.ts', [ + 'addNewWalletsWorker', 'addSigningDeviceWorker', 'mergeSimilarKeysWorker', + 'updateVaultSignerXprivWorker', 'updateSignerDetailsWorker', + 'updateKeyDetailsWorker', + ]); + vm.runInNewContext( + ts.transpileModule(code, { compilerOptions: { target: ts.ScriptTarget.ES2020 } }).outputText, + scope + ); + const run = (name, ...args) => + runSaga( + { + getState: () => state, + dispatch(action) { + actions.push(action); + if (action.type === 'setBackupRepairCompleted') + state.bhr.backupRepairCompletedByAppId[action.payload] = true; + if (action.type === 'setPendingAllBackup') state.bhr.pendingAllBackup = action.payload; + state.account = account.default(state.account, action); + }, + }, + scope[name], + ...args + ).toPromise(); + return { scope, app, collections, remote, calls, actions, state, run }; +} + +module.exports = { fixture, enums, encryption, account, wallet, signer, loadModule, functions }; diff --git a/tests/recovery/sagaState.test.cjs b/tests/recovery/sagaState.test.cjs new file mode 100644 index 0000000000..9285b375c1 --- /dev/null +++ b/tests/recovery/sagaState.test.cjs @@ -0,0 +1,62 @@ +const { test } = require('node:test'); +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const path = require('node:path'); +const vm = require('node:vm'); +const ts = require('typescript'); +const saga = require('redux-saga'); +const effects = require('redux-saga/effects'); +const { harness, wallet, loadModule } = require('./helpers.cjs'); +const bhr = loadModule('src/store/reducers/bhr.ts', { + 'src/models/enums/BHR': loadModule('src/models/enums/BHR.ts'), + 'src/storage': { reduxStorage:{} }, + 'redux-persist': { persistReducer: (_config,reducer) => reducer }, +}); +function runInspection(f, repair) { + const filename = 'src/store/sagas/bhr.ts'; + const source = ts.createSourceFile(filename,fs.readFileSync(path.join(__dirname,'../..',filename),'utf8'),ts.ScriptTarget.Latest,true); + const code = source.statements.filter(n => ts.isFunctionDeclaration(n) && n.name?.text === 'runBackupInspection').map(n => n.getText(source)).join('\n'); + const scope = { ...saga, ...effects, ...bhr, inspectBackup:f.repair.inspectBackup, + RealmSchema:{KeeperApp:'KeeperApp',UAI:'UAI'}, + dbManager:{ getObjectByIndex:()=>f.app, getObjectByField:()=>[] }, + uaiType:{SERVER_BACKUP_FAILURE:'failure'}, uaiActionedWorker:()=>{}, addToUaiStackWorker:()=>{}, + }; + vm.runInNewContext(ts.transpileModule(code,{ compilerOptions:{ target:ts.ScriptTarget.ES2020 }}).outputText,scope); + let state = bhr.default(undefined,{type:'init'}); const actions = []; + const task = saga.runSaga({getState:()=>({bhr:state}),dispatch:action=>{ actions.push(action); state=bhr.default(state,action); }},scope.runBackupInspection,repair).toPromise(); + return {task,get state(){ return state; }, actions}; +} + +test('only verified readback sets durable per-account completion and clears pending', async () => { + const f = harness(); f.local.Wallet.push(wallet()); const run=runInspection(f,true); + assert.equal(await run.task,true); + assert.equal(run.state.backupRepairCompletedByAppId.disposable,true); + assert.equal(run.state.backupRepairStateByAppId.disposable,'verified'); + assert.equal(run.state.backupRepairRunningByAppId.disposable,false); + assert.equal(run.state.pendingAllBackup,false); + const phases=run.actions.filter(a=>a.type==='bhr/setBackupRepairState').map(a=>a.payload.phase); + assert.deepEqual(phases,['checking','preparing','uploading','verifying','verified']); +}); + +test('offline/unverified remains pending and retryable, never spins indefinitely', async () => { + const f=harness({readError:true}); f.local.Wallet.push(wallet()); const run=runInspection(f,true); + assert.equal(await run.task,false); assert.equal(run.state.pendingAllBackup,true); + assert.equal(run.state.backupRepairCompletedByAppId.disposable,false); + assert.equal(run.state.backupRepairRunningByAppId.disposable,false); +}); + +test('account switch preserves per-account result without clearing another account pending state', async () => { + const f=harness({post:async ({app})=>{app.id='other';}}); f.local.Wallet.push(wallet()); + const run=runInspection(f,true); await run.task; + assert.equal(run.state.backupRepairStateByAppId.disposable,'unverified'); + assert.equal(run.state.backupRepairStateByAppId.other,undefined); + assert.ok(!run.actions.some(a=>a.type==='bhr/setPendingAllBackup')); +}); + +test('a later mutation invalidates verified status without affecting a second account', () => { + let state=bhr.default(undefined,bhr.setBackupRepairState({appId:'one',phase:'verified'})); + state=bhr.default(state,bhr.setBackupRepairState({appId:'two',phase:'verified'})); + state=bhr.default(state,bhr.invalidateBackupRepair('one')); + assert.equal(state.backupRepairCompletedByAppId.one,false); assert.equal(state.backupRepairStateByAppId.one,'unverified'); + assert.equal(state.backupRepairCompletedByAppId.two,true); +}); diff --git a/tests/recovery/transport.test.cjs b/tests/recovery/transport.test.cjs new file mode 100644 index 0000000000..de400afaf2 --- /dev/null +++ b/tests/recovery/transport.test.cjs @@ -0,0 +1,56 @@ +const { test } = require('node:test'); +const assert = require('node:assert/strict'); +const { loadModule, harness, wallet } = require('./helpers.cjs'); + +function fakeTransport() { + let now = 0, next = 0, config; + const timers = new Map(); + const module = loadModule('src/services/backup/transport.ts', { + '../rest/RestClient': { post: (_path, _body, _headers, options) => { + config = options; + return new Promise((_, reject) => options.signal.addEventListener('abort', () => reject(new Error('secret request body')))); + } }, + }, { Date: { now: () => now }, setTimeout: (fn, ms) => { const id = ++next; timers.set(id, { at:now+ms, fn }); return id; }, clearTimeout: id => timers.delete(id) }); + const tick = (ms) => { + const until = now+ms; + while (true) { + const first = [...timers.entries()].filter(([,t]) => t.at <= until).sort((a,b) => a[1].at-b[1].at)[0]; + if (!first) break; + now = first[1].at; timers.delete(first[0]); first[1].fn(); + } + now = until; + }; + return { module, tick, get config() { return config; }, timers }; +} + +test('stalled transport aborts after 30 seconds and redacts request errors', async () => { + const f = fakeTransport(); const promise = f.module.boundedBackupPost('fixture', {}); + f.tick(29999); assert.equal(f.config.signal.aborted,false); f.tick(1); + await assert.rejects(promise, /^Error: Backup request could not be completed$/); assert.equal(f.timers.size,0); +}); + +test('only real progress resets stall clock; active transfer still has overall deadline', async () => { + const f = fakeTransport(); const promise = f.module.boundedBackupPost('fixture', {}); + for (let n=1;n<30;n++) { f.tick(20000); f.config.onDownloadProgress({ loaded:n }); } + assert.equal(f.config.signal.aborted,false); f.tick(20000); + assert.equal(f.config.signal.aborted,true); await assert.rejects(promise); assert.equal(f.timers.size,0); + const g = fakeTransport(); const stalled = g.module.boundedBackupPost('fixture', {}); + g.tick(20000); g.config.onUploadProgress({ loaded:0 }); g.tick(10000); await assert.rejects(stalled); +}); + +test('same-account writes serialize, another account runs independently, rejected queue recovers', async () => { + const f = fakeTransport(); const order = []; let release; + const first = f.module.withBackupSession('a', () => new Promise(resolve => { order.push('first'); release=resolve; })); + const next = f.module.withBackupSession('a', async () => { order.push('next'); throw Error('fixture'); }); + const recovered = f.module.withBackupSession('a', async () => order.push('recovered')); + await f.module.withBackupSession('b', async () => order.push('other')); + assert.deepEqual(order,['first','other']); release(); await first; await assert.rejects(next); await recovered; + assert.deepEqual(order,['first','other','next','recovered']); +}); + +test('repeated repair actions share one upload and readback', async () => { + const f = harness(); f.local.Wallet.push(wallet()); + const results = await Promise.all([f.run(true),f.run(true),f.run(true)]); + assert.deepEqual(results,['verified','verified','verified']); + assert.deepEqual(f.calls,['getBackupSnapshot','repairAppBackup','getBackupSnapshot']); +}); diff --git a/tests/recovery/usdtBackup.test.cjs b/tests/recovery/usdtBackup.test.cjs new file mode 100644 index 0000000000..aedac01595 --- /dev/null +++ b/tests/recovery/usdtBackup.test.cjs @@ -0,0 +1,169 @@ +const { test } = require('node:test'); +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const path = require('node:path'); +const vm = require('node:vm'); +const ts = require('typescript'); +const { fixture, enums, encryption, loadModule } = require('./sagaHarness.cjs'); +const { image } = require('./helpers.cjs'); +const { updateAppImage } = loadModule('src/store/sagaActions/bhr.ts'); +const copy = (value) => JSON.parse(JSON.stringify(value)); + +// Run the production hook callback without React/Realm native bindings, then +// drive its dispatched action through the production saga and real AES codec. +function hookFixture(options = {}) { + const f = fixture(options); + const source = ts.createSourceFile('useUSDTWallets.ts', fs.readFileSync( + path.join(__dirname, '../../src/hooks/useUSDTWallets.ts'), 'utf8'), + ts.ScriptTarget.Latest, true); + let callback; + function visit(node) { + if (ts.isVariableDeclaration(node) && node.name.getText(source) === 'updateWallet') + callback = node.initializer.arguments[0].getText(source); + ts.forEachChild(node, visit); + } + visit(source); + assert.ok(callback, 'Production updateWallet callback exists'); + const pending = [], order = [], errors = [], mutations = []; + const dbManager = { + getObjectById: (_schema, id) => { + const stored = f.collections.USDTWallet.find((wallet) => wallet.id === id); + return stored && { toJSON: () => copy(stored) }; + }, + updateObjectById: async (_schema, id, patch) => { + assert.equal('id' in patch, false, 'Realm primary key must not be updated'); + order.push('persist'); + if (options.failPersistence) return false; + const stored = f.collections.USDTWallet.find((wallet) => wallet.id === id); + Object.assign(stored, copy(patch)); + return true; + }, + }; + const updateWallet = vm.runInNewContext(ts.transpileModule(`(${callback})`, { + compilerOptions: { target: ts.ScriptTarget.ES2020 }, + }).outputText, { + dbManager, + RealmSchema: { USDTWallet: 'USDTWallet' }, + canonical: image.canonical, + recoveryContent: image.recoveryContent, + appId: f.app.id, + markBackupMutation: (id) => mutations.push(id), + invalidateBackupRepair: (payload) => ({ type: 'invalidateBackupRepair', payload }), + setPendingAllBackup: (payload) => ({ type: 'setPendingAllBackup', payload }), + updateAppImage, + dispatch(action) { + if (action.type === 'UPDATE_APP_IMAGE') { + order.push('backup'); + assert.deepEqual(copy(f.collections.USDTWallet[0]), copy(action.payload.wallets[0]), + 'Backup must contain the successfully persisted wallet'); + pending.push(f.run('updateAppImageWorker', action)); + } else { + f.actions.push(action); + if (action.type === 'setPendingAllBackup') f.state.bhr.pendingAllBackup = action.payload; + } + }, + setError: (message) => errors.push(message), + captureError: () => {}, + }); + return { ...f, order, errors, mutations, + update: async (wallet) => { + const result = await updateWallet(wallet); + await Promise.all(pending); + return result; + }, + }; +} + +const usdtWallet = (type = 'DEFAULT') => ({ + id: `usdt-${type}`, + entityKind: enums.EntityKind.USDT_WALLET, + type, + networkType: enums.NetworkType.TESTNET, + derivationDetails: { + instanceNum: type === 'DEFAULT' ? 1 : null, + mnemonic: 'disposable fixture mnemonic; not a usable wallet', + xDerivationPath: "m/44'/195'/0'/0/0", + }, + presentationData: { name: 'Before edit', description: 'Before', visibility: 'DEFAULT' }, + specs: { address: 'disposable-tron-address', privateKey: 'fixture-only-private-key', + balance: 10, transactions: [], hasNewUpdates: false, lastSynched: 1 }, + accountStatus: { isActive: true, nextNonce: 0 }, + createdAt: 1, +}); + +for (const type of ['DEFAULT', 'IMPORTED']) { + test(`USDT ${type} metadata and hidden state survive encrypted backup and restore`, async () => { + const f = hookFixture(); + const original = usdtWallet(type); + f.collections.USDTWallet.push(copy(original)); + await f.run('updateAppImageWorker', { payload: { wallets: [original] } }); + const edited = { ...original, + presentationData: { name: 'After edit', description: 'Updated description', visibility: 'HIDDEN' } }; + assert.equal(await f.update(edited), true); + assert.deepEqual(f.order, ['persist', 'backup']); + const key = encryption.generateEncryptionKey(f.app.primarySeed); + const cipher = f.remote.wallets[original.id]; + assert.ok(!cipher.includes(edited.specs.privateKey)); + assert.deepEqual(JSON.parse(encryption.decrypt(key, cipher)), edited); + f.collections.USDTWallet = []; + await f.run('recoverApp', 'mock mnemonic', Buffer.from('fixture'), key, + f.app.id, f.app.subscription, f.remote, [], [], '2.5.16'); + assert.deepEqual(copy(f.collections.USDTWallet), [edited]); + }); +} + +for (const failure of [{ online: false }, { incrementalError: true }, { rejectIncremental: true }]) { + test(`USDT edit remains local and retryable when backup fails: ${JSON.stringify(failure)}`, async () => { + const f = hookFixture(failure); + const original = usdtWallet(); + f.collections.USDTWallet.push(copy(original)); + const edited = { ...original, presentationData: { ...original.presentationData, name: 'Changed' } }; + assert.equal(await f.update(edited), true, 'A remote failure must not discard the saved edit'); + assert.deepEqual(copy(f.collections.USDTWallet), [edited]); + assert.equal(f.state.bhr.pendingAllBackup, true); + assert.ok(f.actions.some((a) => a.type === 'invalidateBackupRepair' && a.payload === f.app.id)); + assert.equal(f.remote.wallets[edited.id], undefined); + assert.ok(!f.actions.some((a) => a.type === 'setBackupAllSuccess' && a.payload)); + }); +} + +test('failed USDT persistence cannot report success or upload an unsaved edit', async () => { + const f = hookFixture({ failPersistence: true }); + const original = usdtWallet(); + f.collections.USDTWallet.push(copy(original)); + const edited = { ...original, presentationData: { ...original.presentationData, name: 'Not saved' } }; + assert.equal(await f.update(edited), false); + assert.deepEqual(f.order, ['persist']); + assert.equal(f.calls.length, 0); + assert.deepEqual(copy(f.collections.USDTWallet), [original]); + assert.equal(f.state.bhr.pendingAllBackup, true, 'Partial-write uncertainty requires inspection'); + assert.deepEqual(f.mutations, [f.app.id]); + assert.ok(f.actions.some((a) => a.type === 'invalidateBackupRepair' && a.payload === f.app.id)); + assert.deepEqual(f.errors, ['Failed to update wallet']); +}); + +test('USDT balance, transaction and account sync avoids redundant backup uploads', async () => { + const f = hookFixture(); + const original = usdtWallet(); + f.collections.USDTWallet.push(copy(original)); + const synced = { ...original, + specs: { ...original.specs, balance: 12, transactions: [{ txId: 'fixture-tx' }], + hasNewUpdates: true, lastSynched: 2 }, + accountStatus: { isActive: true, nextNonce: 1 }, + }; + assert.equal(await f.update(synced), true); + assert.deepEqual(copy(f.collections.USDTWallet), [synced]); + assert.deepEqual(f.order, ['persist']); + assert.equal(f.calls.length, 0); + assert.equal(f.actions.length, 0); +}); + +test('USDT metadata edits respect disabled Assisted Server Backup', async () => { + const f = hookFixture({ bhr: { automaticCloudBackup: false } }); + const original = usdtWallet(); + f.collections.USDTWallet.push(copy(original)); + const edited = { ...original, presentationData: { ...original.presentationData, visibility: 'HIDDEN' } }; + assert.equal(await f.update(edited), true); + assert.deepEqual(copy(f.collections.USDTWallet), [edited]); + assert.equal(f.calls.length, 0); +}); diff --git a/tests/screens/PinEntry.test.tsx b/tests/screens/PinEntry.test.tsx new file mode 100644 index 0000000000..42d7deb06e --- /dev/null +++ b/tests/screens/PinEntry.test.tsx @@ -0,0 +1,155 @@ +import React from 'react'; +import { act, create, ReactTestRenderer } from 'react-test-renderer'; +import { TouchableOpacity } from 'react-native'; +import LoginScreen from 'src/screens/LoginScreen/Login'; +import CreatePin from 'src/screens/LoginScreen/CreatePin'; +import PasscodeVerify from 'src/components/Modal/PasscodeVerify'; +import KeyPadView from 'src/components/AppNumPad/KeyPadView'; +import PinDotView from 'src/components/AppPinInput/PinDotView'; +import PinInputsView from 'src/components/AppPinInput/PinInputsView'; +import Buttons from 'src/components/Buttons'; + +const mockDispatch = jest.fn(); +const mockState = { + login: { hasCreds: false, authenticationFailed: false, isAuthenticated: false }, + settings: { loginMethod: 'PIN' }, + account: { allAccounts: [] }, + storage: { appId: 'disposable-test', failedAttempts: 0, lastLoginFailedAt: 0 }, + notifications: {}, + bhr: {}, +}; +jest.mock('src/store/hooks', () => ({ + useAppDispatch: () => mockDispatch, + useAppSelector: (selector) => selector(mockState), +})); +jest.mock('src/context/Localization/LocContext', () => ({ + LocalizationContext: require('react').createContext({ + translations: { + common: { proceed: 'Proceed', create: 'Create' }, + login: { Incorrect: 'Incorrect PIN' }, + error: {}, + }, + }), +})); +jest.mock('@gluestack-ui/themed-native-base', () => ({ + Box: require('react-native').View, + View: require('react-native').View, + StatusBar: () => null, + useColorMode: () => ({ colorMode: 'light' }), +})); +jest.mock('src/components/KeeperText', () => require('react-native').Text); +jest.mock('src/components/AppNumPad/KeyPadView', () => jest.fn(() => null)); +jest.mock('src/components/AppPinInput/PinDotView', () => jest.fn(() => null)); +jest.mock('src/components/AppPinInput/PinInputsView', () => jest.fn(() => null)); +jest.mock('src/components/KeeperModal', () => () => null); +jest.mock('src/components/ThemedColor/ThemedColor', () => () => '#000'); +jest.mock('src/components/ThemedSvg.tsx/ThemedSvg', () => () => null); +jest.mock('src/components/Loader', () => () => null); +jest.mock('src/components/BounceLoader', () => () => null); +jest.mock('src/components/TestnetIndicator', () => () => null); +jest.mock('src/hooks/useToastMessage', () => () => ({ showToast: jest.fn() })); +jest.mock('src/constants/defaultData', () => ({ getSecurityTip: () => ({}) })); +jest.mock('src/constants/Bitcoin', () => ({ isTestnet: () => false })); +jest.mock('src/services/rest/RestClient', () => ({ + __esModule: true, + default: { getTorStatus: () => 0, subToTorStatus: jest.fn(), unsubscribe: jest.fn() }, + TorStatus: {}, +})); +jest.mock('src/storage/realm/dbManager', () => ({})); +jest.mock('src/services/backend/Relay', () => ({})); +jest.mock('react-native-biometrics', () => jest.fn(() => ({}))); +jest.mock('@react-native-firebase/app', () => ({ getApp: jest.fn() })); +jest.mock('@react-native-firebase/messaging', () => ({ + AuthorizationStatus: { AUTHORIZED: 1, PROVISIONAL: 2 }, + getMessaging: jest.fn(), + getToken: jest.fn(), + requestPermission: jest.fn(async () => 0), +})); + +let screen: ReactTestRenderer; +const keypad = () => screen.root.findByType(KeyPadView).props; +const input = () => screen.root.findByType(PinDotView).props.passCode; +function enter(value: string) { + // Deliberately retain one render's handler and queue taps in one batch. + const press = keypad().onPressNumber; + act(() => [...value].forEach(press)); +} +function erase(count: number) { + const press = keypad().onDeletePressed; + act(() => { + for (let i = 0; i < count; i++) press(); + }); +} +function mount(component: React.ReactElement) { + act(() => { + screen = create(component); + }); +} +beforeEach(() => { + jest.useFakeTimers(); + mockDispatch.mockClear(); +}); +afterEach(() => { + act(() => screen?.unmount()); + jest.clearAllTimers(); + jest.useRealTimers(); +}); + +describe('PIN entry under batched rapid taps', () => { + test('login keeps every digit, caps at four and deletes in order', () => { + mount(); + enter('123456'); + expect(input()).toBe('1234'); + erase(2); + expect(input()).toBe('12'); + enter('98'); + expect(input()).toBe('1298'); + erase(6); + expect(input()).toBe(''); + }); + + test('creation accepts rapid confirmation and repeated digits without throttling', () => { + mount(); + enter('11111111'); + const dots = screen.root.findAllByType(PinDotView); + expect(dots.map((dot) => dot.props.passCode)).toEqual(['1111', '1111']); + expect(screen.root.findByType(Buttons).props.primaryDisable).toBe(false); + enter('9'); + expect(screen.root.findAllByType(PinDotView)[1].props.passCode).toBe('1111'); + erase(5); + expect(input()).toBe('111'); + expect(screen.root.findByType(Buttons).props.primaryDisable).toBe(true); + }); + + test('creation mismatch stays disabled and can be corrected by rapid deletion', () => { + mount(); + enter('12345678'); + expect(screen.root.findByType(Buttons).props.primaryDisable).toBe(true); + erase(4); + enter('1234'); + expect(screen.root.findByType(Buttons).props.primaryDisable).toBe(false); + }); + + test('confirmation keeps Proceed mounted, blocks partial PINs and submits exactly four digits', () => { + mount(); + const button = screen.root.findByType(Buttons); + const nativeButton = () => screen.root.findByType(TouchableOpacity); + expect(button.props.primaryText).toBe('Proceed'); + expect(nativeButton().props.disabled).toBe(true); + act(() => nativeButton().props.onPress()); + expect(mockDispatch).not.toHaveBeenCalled(); + enter('123'); + expect(screen.root.findByType(Buttons)).toBe(button); + expect(nativeButton().props.disabled).toBe(true); + enter('45'); + expect(screen.root.findByType(PinInputsView).props.passCode).toBe('1234'); + expect(screen.root.findByType(Buttons)).toBe(button); + expect(nativeButton().props.disabled).toBe(false); + act(() => nativeButton().props.onPress()); + expect(mockDispatch).toHaveBeenCalledTimes(1); + expect(mockDispatch.mock.calls[0][0].payload.passcode).toBe('1234'); + expect(nativeButton().props.disabled).toBe(true); + erase(2); + expect(screen.root.findByType(PinInputsView).props.passCode).toBe('12'); + }); +}); diff --git a/tests/services/initialTimelock.test.ts b/tests/services/initialTimelock.test.ts new file mode 100644 index 0000000000..d2e1530bcb --- /dev/null +++ b/tests/services/initialTimelock.test.ts @@ -0,0 +1,113 @@ +import { resolveInitialTimelock } from 'src/services/wallets/operations/miniscript/initialTimelock'; +import { MiniscriptTypes } from 'src/services/wallets/enums'; +import { + generateEnhancedVaultElements, + ENHANCED_VAULT_TIMELOCKS_TIMESTAMP_MAINNET, + ENHANCED_VAULT_TIMELOCKS_TIMESTAMP_TESTNET, + ENHANCED_VAULT_TIMELOCKS_BLOCK_HEIGHT_MAINNET, + ENHANCED_VAULT_TIMELOCKS_BLOCK_HEIGHT_TESTNET, +} from 'src/services/wallets/operations/miniscript/default/EnhancedVault'; +import { generateMiniscriptPolicy } from 'src/services/wallets/operations/miniscript/policy-generator'; + +jest.mock('src/utils/utilities', () => ({ getKeyUID: (s) => s.masterFingerprint })); +jest.mock('src/utils/service-utilities/utils', () => ({ + getDerivationPath: (p) => p.replace('m/', ''), +})); +const required = [ + MiniscriptTypes.TIMELOCKED, + MiniscriptTypes.INHERITANCE, + MiniscriptTypes.EMERGENCY, +]; +const mk = (fp) => + ({ + masterFingerprint: fp, + derivationPath: "m/48'/0'/0'/2'", + xpub: `xpub${fp}`, + account: 0, + } as any); + +describe('Timelock generation boundary', () => { + test.each([undefined, null, '', 0, NaN, -1, 123])( + 'rejects missing or malformed required selection %s', + (value) => { + expect(() => resolveInitialTimelock(required, value, () => 100)).toThrow(); + } + ); + test.each([0, -1, NaN, Infinity, 0.5, undefined])( + 'rejects invalid resolved duration %s', + (value) => { + expect(() => resolveInitialTimelock(required, 'invalid', () => value)).toThrow(); + } + ); + test.each([ + [], + [MiniscriptTypes.INHERITANCE], + [MiniscriptTypes.EMERGENCY], + [MiniscriptTypes.INHERITANCE, MiniscriptTypes.EMERGENCY], + ])('allows legitimate no-initial-timelock %j', (...args) => { + const types = args.flat() as MiniscriptTypes[]; + for (const value of [undefined, null, '', 0]) + expect(resolveInitialTimelock(types, value, () => undefined)).toBe(0); + }); + test.each([ + ['mainnet timestamp', ENHANCED_VAULT_TIMELOCKS_TIMESTAMP_MAINNET, 1767225600], + ['testnet timestamp', ENHANCED_VAULT_TIMELOCKS_TIMESTAMP_TESTNET, 1767225600], + ['mainnet height', ENHANCED_VAULT_TIMELOCKS_BLOCK_HEIGHT_MAINNET, 950000], + ['testnet height', ENHANCED_VAULT_TIMELOCKS_BLOCK_HEIGHT_TESTNET, 100000], + ])('preserves all policy activation times for %s', (_name, table: any, base: number) => { + for (const durationLabel of ['MONTHS_3', 'MONTHS_6', 'MONTHS_9', 'MONTHS_12']) { + const duration = resolveInitialTimelock(required, durationLabel, (label) => table[label]); + const elements = generateEnhancedVaultElements( + [mk('AAAAAAAA'), mk('BBBBBBBB'), mk('CCCCCCCC')], + [{ signer: mk('DDDDDDDD'), timelock: base + duration + table.MONTHS_12 }], + [{ signer: mk('EEEEEEEE'), timelock: base + duration + table.MONTHS_36 }], + { m: 2, n: 3 }, + base + duration + ); + const { policy, miniscriptPhases } = generateMiniscriptPolicy(elements); + expect(miniscriptPhases.map((p) => p.timelock)).toEqual([ + base + duration, + base + duration + table.MONTHS_12, + base + duration + table.MONTHS_36, + ]); + for (const phase of miniscriptPhases) expect(policy).toContain(`after(${phase.timelock})`); + } + }); +}); + +describe('Already-affected disposable policy assessment', () => { + test.each([ + ['timestamp', ENHANCED_VAULT_TIMELOCKS_TIMESTAMP_MAINNET, 1767225600], + ['block height', ENHANCED_VAULT_TIMELOCKS_BLOCK_HEIGHT_MAINNET, 950000], + ])( + 'distinguishes an omitted initial lock and shifted fallback schedule (%s)', + (_name, table: any, base: number) => { + const generate = (initial: number) => + generateMiniscriptPolicy( + generateEnhancedVaultElements( + [mk('AAAAAAAA'), mk('BBBBBBBB'), mk('CCCCCCCC')], + [{ signer: mk('DDDDDDDD'), timelock: base + initial + table.MONTHS_12 }], + [{ signer: mk('EEEEEEEE'), timelock: base + initial + table.MONTHS_36 }], + { m: 2, n: 3 }, + initial ? base + initial : 0 + ) + ); + const affected = generate(0); + const intended = generate(table.MONTHS_6); + // Inspect the policy generated from each fixture, not a TIMELOCKED metadata flag. + expect(affected.miniscriptPhases.map((p) => p.timelock)).toEqual([ + 0, + base + table.MONTHS_12, + base + table.MONTHS_36, + ]); + expect(intended.miniscriptPhases.map((p) => p.timelock)).toEqual([ + base + table.MONTHS_6, + base + table.MONTHS_6 + table.MONTHS_12, + base + table.MONTHS_6 + table.MONTHS_36, + ]); + expect(affected.policy).not.toContain(`after(${base + table.MONTHS_6})`); + expect(intended.policy).toContain(`after(${base + table.MONTHS_6})`); + expect(affected.policy).not.toBe(intended.policy); + } + ); +}); diff --git a/tests/services/resetTimelockBoundary.test.ts b/tests/services/resetTimelockBoundary.test.ts new file mode 100644 index 0000000000..f58a48d6a1 --- /dev/null +++ b/tests/services/resetTimelockBoundary.test.ts @@ -0,0 +1,161 @@ +import fs from 'fs'; +import path from 'path'; +import vm from 'vm'; +import ts from 'typescript'; +import { resolveInitialTimelock } from 'src/services/wallets/operations/miniscript/initialTimelock'; +import { + MiniscriptTypes, + VaultType, + MultisigScriptType, + NetworkType, +} from 'src/services/wallets/enums'; + +const source = ts.createSourceFile( + 'VaultMigrationController.tsx', + fs.readFileSync( + path.join(process.cwd(), 'src/screens/Vault/VaultMigrationController.tsx'), + 'utf8' + ), + ts.ScriptTarget.Latest, + true, + ts.ScriptKind.TSX +); +let prepareExpression: ts.Expression; +let initiateExpression: ts.Expression; +function visit(node: ts.Node) { + if (ts.isVariableDeclaration(node) && node.name.getText(source) === 'prepareMiniscriptScheme') + prepareExpression = node.initializer; + if (ts.isVariableDeclaration(node) && node.name.getText(source) === 'initiateNewVault') + initiateExpression = node.initializer; + ts.forEachChild(node, visit); +} +visit(source); +function fixture() { + const generateEnhancedVaultElements = jest.fn(() => ({})); + const scope: any = { + resolveInitialTimelock, + MiniscriptTypes, + VaultType, + MultisigScriptType, + NetworkType, + bitcoinNetworkType: NetworkType.TESTNET, + getTimelockDuration: () => 6, + getCurrentTimeForLock: () => 100000, + generateEnhancedVaultElements, + generateMiniscriptScheme: () => ({}), + getKeyUID: (key) => key.id, + }; + vm.runInNewContext( + ts.transpileModule(`prepare = (${prepareExpression.getText(source)});`, { + fileName: 'boundary.tsx', + compilerOptions: { target: ts.ScriptTarget.ES2019, jsx: ts.JsxEmit.React }, + }).outputText, + scope + ); + return { + scope, + generateEnhancedVaultElements, + info: { + vaultType: VaultType.MINISCRIPT, + vaultScheme: { m: 1, n: 1 }, + vaultSigners: [{ id: 'test' }], + }, + }; +} +describe('Actual migration preparation boundary', () => { + test.each([undefined, null, '', 0])( + 'rejects dropped required value %s before policy construction', + async (value) => { + const { scope, generateEnhancedVaultElements, info } = fixture(); + await expect( + scope.prepare( + info, + [MiniscriptTypes.TIMELOCKED, MiniscriptTypes.EMERGENCY], + [], + [{ key: { id: 'emergency' }, duration: '12 months' }], + value + ) + ).rejects.toThrow('initial timelock'); + expect(generateEnhancedVaultElements).not.toHaveBeenCalled(); + expect(info).not.toHaveProperty('miniscriptElements'); + } + ); + test('uses the selected initial lock in primary and emergency policy inputs', async () => { + const { scope, generateEnhancedVaultElements, info } = fixture(); + await scope.prepare( + info, + [MiniscriptTypes.TIMELOCKED, MiniscriptTypes.EMERGENCY], + [], + [{ key: { id: 'emergency' }, duration: '12 months' }], + '6 months' + ); + const call = generateEnhancedVaultElements.mock.calls[0] as any[]; + expect(call[4]).toBe(100006); + expect(call[2][0].timelock).toBe(100012); + }); + test('still accepts an emergency-only wallet with no initial lock', async () => { + const { scope, generateEnhancedVaultElements, info } = fixture(); + await scope.prepare( + info, + [MiniscriptTypes.EMERGENCY], + [], + [{ key: { id: 'emergency' }, duration: '12 months' }], + undefined + ); + const call = generateEnhancedVaultElements.mock.calls[0] as any[]; + expect(call[4]).toBe(0); + expect(call[2][0].timelock).toBe(100006); + }); +}); + +describe('Migration caller fails closed', () => { + test.each([false, true])( + 'does not dispatch creation or migration on missing required lock (existing=%s)', + async (existing) => { + const { scope, info } = fixture(); + Object.assign(scope, { + vaultType: VaultType.MINISCRIPT, + scheme: info.vaultScheme, + vaultKeys: info.vaultSigners, + name: 'Disposable fixture', + description: '', + miniscriptTypes: [MiniscriptTypes.TIMELOCKED, MiniscriptTypes.EMERGENCY], + inheritanceKeys: [], + emergencyKeys: [{ key: { id: 'emergency' }, duration: '12 months' }], + initialTimelockDuration: undefined, + activeVault: existing ? { id: 'old-wallet', scheme: { miniscriptScheme: {} } } : null, + allVaults: [], + archivedVaults: [], + vaultAlreadyExists: jest.fn(() => false), + dispatch: jest.fn(), + migrateVault: jest.fn(), + addNewVault: jest.fn(), + generateVaultId: jest.fn(), + setGeneratedVaultId: jest.fn(), + setSavedGeneratedVaultId: jest.fn(), + showToast: jest.fn(), + setCreating: jest.fn(), + captureError: jest.fn(), + React: { createElement: () => null }, + ToastErrorIcon: () => null, + prepareMiniscriptScheme: scope.prepare, + }); + vm.runInNewContext( + ts.transpileModule(`initiate = (${initiateExpression.getText(source)});`, { + fileName: 'caller.tsx', + compilerOptions: { target: ts.ScriptTarget.ES2019, jsx: ts.JsxEmit.React }, + }).outputText, + scope + ); + await scope.initiate(); + expect(scope.dispatch).not.toHaveBeenCalled(); + expect(scope.vaultAlreadyExists).not.toHaveBeenCalled(); + expect(scope.generateVaultId).not.toHaveBeenCalled(); + expect(scope.setCreating).toHaveBeenCalledWith(false); + expect(scope.showToast.mock.calls[0][0]).toContain( + 'Failed to prepare enhanced vault: Failed to determine initial timelock duration' + ); + expect(scope.captureError).toHaveBeenCalledTimes(1); + } + ); +}); diff --git a/tests/services/resetTimelockRoute.test.ts b/tests/services/resetTimelockRoute.test.ts new file mode 100644 index 0000000000..524d190b3e --- /dev/null +++ b/tests/services/resetTimelockRoute.test.ts @@ -0,0 +1,53 @@ +import fs from 'fs'; +import path from 'path'; +import vm from 'vm'; +import ts from 'typescript'; + +// Execute the real route-parameter expression, without native UI or wallet secrets. +const source = ts.createSourceFile( + 'ResetInheritanceKey.tsx', + fs.readFileSync(path.join(process.cwd(), 'src/screens/Vault/ResetInheritanceKey.tsx'), 'utf8'), + ts.ScriptTarget.Latest, + true, + ts.ScriptKind.TSX +); +let params: ts.Expression; +function visit(node: ts.Node) { + if (ts.isObjectLiteralExpression(node)) { + const name = node.properties.find( + (p) => ts.isPropertyAssignment(p) && p.name.getText(source) === 'name' + ) as ts.PropertyAssignment; + if (name?.initializer.getText(source) === "'ResetEmergencyKey'") { + params = ( + node.properties.find( + (p) => ts.isPropertyAssignment(p) && p.name.getText(source) === 'params' + ) as ts.PropertyAssignment + ).initializer; + } + } + ts.forEachChild(node, visit); +} +visit(source); + +describe('Reset Keys navigation contract', () => { + test.each(['3 months', '6 months', '9 months', '12 months'])( + 'forwards selected initial duration %s', + (initialTimelockDuration) => { + const vault = { id: 'disposable' }; + const script = ts.transpile(`result = (${params.getText(source)});`, { + target: ts.ScriptTarget.ES2019, + }); + const scope: any = { + initialTimelockDuration, + vault, + inheritanceSigners: [{ id: 'test-key' }], + selectedOptions: { 'test-key': { label: '12 months' } }, + getKeyUID: (s) => s.id, + }; + vm.runInNewContext(script, scope); + expect(scope.result.initialTimelockDuration).toBe(initialTimelockDuration); + expect(scope.result.vault).toBe(vault); + expect(scope.result.inheritanceKeys[0].duration).toBe('12 months'); + } + ); +}); diff --git a/tests/store/versionHistory.test.ts b/tests/store/versionHistory.test.ts new file mode 100644 index 0000000000..2343a8c9db --- /dev/null +++ b/tests/store/versionHistory.test.ts @@ -0,0 +1,249 @@ +import fs from 'fs'; +import path from 'path'; +import { call, put, select } from 'redux-saga/effects'; +import ts from 'typescript'; +import vm from 'vm'; +import semver from 'semver'; +import { applyUpgradeSequence, updateVersionHistoryWorker } from 'src/store/sagas/upgrade'; +import { updateVersionHistory } from 'src/store/sagaActions/upgrade'; +import { RealmSchema } from 'src/storage/realm/enum'; +import dbManager from 'src/storage/realm/dbManager'; +import Relay from 'src/services/backend/Relay'; +import { setAppVersion } from 'src/store/reducers/storage'; + +jest.mock('src/storage/realm/dbManager', () => ({ + getObjectByIndex: jest.fn(), + createObject: jest.fn(), +})); +jest.mock('src/services/backend/Relay', () => ({ updateAppImage: jest.fn() })); +jest.mock('src/services/backend/SigningServer', () => ({})); +jest.mock('src/utils/service-utilities/utils', () => ({})); +jest.mock('src/services/wallets/factories/WalletFactory', () => ({})); +jest.mock('src/utils/service-utilities/encryption', () => ({})); +jest.mock('src/utils/utilities', () => ({})); +jest.mock('src/services/wallets/operations/utils', () => ({})); +jest.mock('src/store/sagas/bhr', () => ({})); +jest.mock('@react-native-firebase/app', () => ({ getApp: jest.fn() })); +jest.mock('@react-native-firebase/messaging', () => ({ + getMessaging: jest.fn(), + subscribeToTopic: jest.fn(), + unsubscribeFromTopic: jest.fn(), +})); +jest.mock('react-native-device-info', () => ({ getBuildNumber: () => '624' })); + +const previousVersion = '2.5.15'; +const newVersion = '2.5.16'; + +function writeHistory(action) { + const worker = updateVersionHistoryWorker(action); + expect(worker.next().value).toEqual(call(dbManager.getObjectByIndex, RealmSchema.KeeperApp)); + expect(worker.next({ id: 'test-app' } as any).value).toEqual( + call(Relay.updateAppImage, { appId: 'test-app', version: newVersion }) + ); + const write = worker.next({} as any).value as any; + expect(write.payload.fn).toBe(dbManager.createObject); + expect(write.payload.args[0]).toBe(RealmSchema.VersionHistory); + expect(worker.next().done).toBe(true); + return write.payload.args[1]; +} + +describe('Version History retains recovery provenance through required upgrades', () => { + test.each([true, false])('upgrade passes recovery=%s to the history writer', (isRecovery) => { + const upgrade = applyUpgradeSequence({ previousVersion, newVersion, isRecovery }); + expect(upgrade.next().value).toEqual(put(setAppVersion(newVersion))); + const history = upgrade.next().value as any; + expect(history).toEqual(put(updateVersionHistory(previousVersion, newVersion, isRecovery))); + const record = writeHistory(history.payload.action); + expect(record.version).toBe('2.5.16(624)'); + expect(record.title).toBe(isRecovery ? 'Recovered Wallet' : 'Upgraded from 2.5.15 to 2.5.16'); + expect(upgrade.next().done).toBe(true); + }); + + test('older callers still record ordinary upgrades by default', () => { + expect(writeHistory({ payload: { previousVersion, newVersion } }).title).toBe( + 'Upgraded from 2.5.15 to 2.5.16' + ); + }); + + test('recovery does not skip any of the legacy migrations', () => { + const ordinary = [...applyUpgradeSequence({ previousVersion: '1.0.4', newVersion })]; + const recovered = [ + ...applyUpgradeSequence({ previousVersion: '1.0.4', newVersion, isRecovery: true }), + ]; + expect(recovered.slice(0, -1)).toEqual(ordinary.slice(0, -1)); + expect(recovered.length).toBeGreaterThan(3); + }); +}); + +// Execute the real recovery orchestration with disposable in-memory dependencies. +// This verifies call-site provenance; it does not claim native Realm or wallet recovery QA. +function recoveryFixture(backupVersion: string) { + const source = ts.createSourceFile( + 'bhr.ts', + fs.readFileSync(path.join(process.cwd(), 'src/store/sagas/bhr.ts'), 'utf8'), + ts.ScriptTarget.Latest, + true + ); + const functions = source.statements.filter( + (node) => + ts.isFunctionDeclaration(node) && + ['getAppImageWorker', 'recoverApp', 'writeRecoveredObject'].includes(node.name?.text) + ); + const writes = new Map(); + const recoveryUpgrades = []; + const errors = []; + const scope: any = { + call, + put, + select, + semver, + console, + RealmSchema, + applyUpgradeSequence, + DeviceInfo: { getVersion: () => newVersion, getBuildNumber: () => '624' }, + bip39: { validateMnemonic: () => true, mnemonicToSeedSync: () => Buffer.from('fixture') }, + crypto: require('crypto'), + BIP85: { bip39MnemonicToEntropy: () => Buffer.from('fixture') }, + config: { BIP85_IMAGE_ENCRYPTIONKEY_DERIVATION_PATH: 'fixture' }, + generateEncryptionKey: () => 'fixture', + decrypt: (_key, content) => content, + prepareRecoveryImage: (_key, _appId, image) => ({ + wallets: Object.fromEntries( + Object.entries(image.wallets).map(([id, content]) => [id, JSON.parse(content as string)]) + ), + signers: {}, + vaults: {}, + nodes: {}, + labels: {}, + }), + WalletUtilities: { getFingerprintFromSeed: () => 'AAAAAAAA' }, + SubscriptionTier: { L1: 'L1', L3: 'L3' }, + AppSubscriptionLevel: { L1: 1, L3: 3 }, + BackupType: { SEED: 'SEED' }, + EntityKind: { USDT_WALLET: 'USDT_WALLET' }, + BackupAction: { SEED_BACKUP_CONFIRMED: 'confirmed' }, + NetworkType: { MAINNET: 'MAINNET', TESTNET: 'TESTNET' }, + uaiType: {}, + moment: () => ({ unix: () => 1 }), + setupRecoveryKeySigningKey: () => ({ + id: 'recovery', + signerXpubs: { + fixture: [ + { + xpub: 'fixture-public', + xpriv: 'fixture-private', + derivationPath: 'fixture-path', + }, + ], + }, + }), + getKeyUID: (signer) => signer.id, + addSigningDeviceWorker: () => {}, + dbManager: { + createObject: (schema, record) => { + if (schema === RealmSchema.VersionHistory) writes.set(record.version, record); + return true; + }, + createObjectBulk: () => {}, + getCollection: (schema) => + schema === RealmSchema.Signer + ? [ + { + id: 'recovery', + signerXpubs: { + fixture: [ + { + xpub: 'fixture-public', + xpriv: 'fixture-private', + derivationPath: 'fixture-path', + }, + ], + }, + }, + ] + : [], + }, + Relay: { + getAppImage: () => ({ + appImage: { + appId: 'disposable', + version: backupVersion, + wallets: { test: JSON.stringify({ id: 'test-wallet' }) }, + signers: {}, + nodes: [], + }, + }), + updateAppImage: () => ({}), + }, + }; + for (const name of [ + 'setAppImageError', + 'setSeedConfirmed', + 'setBackupType', + 'uaiActioned', + 'setAppId', + 'setAppCreated', + 'addAccount', + 'autoSyncWallets', + 'saveDefaultWalletState', + 'uaiChecks', + 'loadConciergeUser', + 'loadConciergeTickets', + 'setRecoveryKeyBackedUp', + 'setRecoveryKeyStatus', + 'appImagerecoveryRetry', + ]) { + scope[name] = (payload) => ({ type: name, payload }); + } + vm.runInNewContext( + ts.transpileModule(functions.map((node) => node.getText(source)).join('\n'), { + compilerOptions: { target: ts.ScriptTarget.ES2019 }, + }).outputText, + scope + ); + function drive(generator) { + let result = generator.next(); + while (!result.done) { + const effect = result.value; + let response; + if (effect.type === 'SELECT') response = { bitcoinNetworkType: 'TESTNET' }; + if (effect.type === 'CALL') { + const { fn, args } = effect.payload; + if (fn === applyUpgradeSequence) recoveryUpgrades.push(args[0]); + response = fn(...args); + if (response?.next) response = drive(response); + } + if (effect.type === 'PUT') { + const action = effect.payload.action; + if (action.type === 'setAppImageError' && action.payload) errors.push(action.payload); + if (action.type === 'UPDATE_VERSION_HISTORY') { + const record = writeHistory(action); + writes.set(record.version, record); + } + } + result = generator.next(response); + } + return result.value; + } + drive(scope.getAppImageWorker({ payload: { primaryMnemonic: 'disposable mocked fixture' } })); + return { writes, recoveryUpgrades, errors }; +} + +test.each(['2.5.15', '2.5.16'])( + 'recovery of backup %s finishes with one recovered entry', + (backupVersion) => { + const { writes, recoveryUpgrades, errors } = recoveryFixture(backupVersion); + expect(errors).toEqual([]); + expect([...writes.values()]).toEqual([ + expect.objectContaining({ + version: '2.5.16(624)', + title: 'Recovered Wallet', + }), + ]); + expect(recoveryUpgrades).toEqual( + backupVersion === '2.5.15' + ? [{ previousVersion: backupVersion, newVersion, isRecovery: true }] + : [] + ); + } +); From 380196e43ff4838094b6167dc7133f3ef238bd6f Mon Sep 17 00:00:00 2001 From: Anant Date: Tue, 29 Sep 2026 19:04:11 +0530 Subject: [PATCH 005/254] Open backup comparison from the home warning --- src/components/HomeScreenHeader.tsx | 3 +-- tests/recovery/backupFeedback.test.cjs | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/components/HomeScreenHeader.tsx b/src/components/HomeScreenHeader.tsx index ac9727a035..6a48c248e1 100644 --- a/src/components/HomeScreenHeader.tsx +++ b/src/components/HomeScreenHeader.tsx @@ -23,7 +23,6 @@ import useToastMessage from 'src/hooks/useToastMessage'; import { SignerType } from 'src/services/wallets/enums'; import useSignerMap from 'src/hooks/useSignerMap'; import { setStateFromSnapshot } from 'src/store/reducers/send_and_receive'; -import { backupAllSignersAndVaults } from 'src/store/sagaActions/bhr'; import Fonts from 'src/constants/Fonts'; import ThemedColor from './ThemedColor/ThemedColor'; @@ -164,7 +163,7 @@ const HomeScreenHeader: React.FC = ({ }, [uaiType.SERVER_BACKUP_FAILURE]: () => { dispatch(uaiActioned({ uaiId: localLatestUnseenUai.id, action: false })); - dispatch(backupAllSignersAndVaults()); + navigation.navigate('AssistedBackupStatus'); }, }; diff --git a/tests/recovery/backupFeedback.test.cjs b/tests/recovery/backupFeedback.test.cjs index 5656622b5a..c94c89b29d 100644 --- a/tests/recovery/backupFeedback.test.cjs +++ b/tests/recovery/backupFeedback.test.cjs @@ -52,3 +52,26 @@ for (const file of ['src/components/Backup/BackupHealthCheckList.tsx', 'src/scre assert.ok(!actions.some(a=>a.type==='enabled'&&a.value)); assert.ok(!ui.some(a=>(a[0]==='enabled'||a[0]==='success')&&a[1])); }); } + + +test('home backup warning opens comparison without starting a backup', () => { + const file = 'src/components/HomeScreenHeader.tsx'; + const source = ts.createSourceFile(file, fs.readFileSync(path.join(__dirname, '../..', file), 'utf8'), ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX); + let callback; + function visit(node) { + if (ts.isPropertyAssignment(node) && node.name.getText(source) === '[uaiType.SERVER_BACKUP_FAILURE]') callback = node.initializer.getText(source); + ts.forEachChild(node, visit); + } + visit(source); + assert.ok(callback); + const actions = [], destinations = []; + vm.runInNewContext(ts.transpileModule(`(${callback})()`, { compilerOptions: { target: ts.ScriptTarget.ES2020 } }).outputText, { + localLatestUnseenUai: { id: 'disposable-backup-warning' }, + dispatch: action => actions.push(action), + uaiActioned: payload => ({ type: 'uaiActioned', payload }), + backupAllSignersAndVaults: () => ({ type: 'backupAllSignersAndVaults' }), + navigation: { navigate: route => destinations.push(route) }, + }); + assert.deepEqual(destinations, ['AssistedBackupStatus']); + assert.ok(!actions.some(action => action.type === 'backupAllSignersAndVaults'), 'view details must not write a backup before the user chooses Back Up Now'); +}); From a224bc9fde6ffaea2dded9df51a11bb8909009da Mon Sep 17 00:00:00 2001 From: Anant Date: Fri, 2 Oct 2026 22:02:45 +0530 Subject: [PATCH 006/254] Add R02 current-state backup verification and faster login --- src/context/Localization/language/en.json | 6 +++--- src/context/Localization/language/es.json | 6 +++--- src/screens/LoginScreen/Login.tsx | 20 +++++++++++++++----- src/services/backup/image.ts | 9 ++++++++- src/services/backup/repair.ts | 5 ++++- src/store/sagas/login.ts | 20 ++++++-------------- tests/recovery/repair.test.cjs | 23 ++++++++++++++++++++--- tests/screens/PinEntry.test.tsx | 21 ++++++++++++++++++++- 8 files changed, 79 insertions(+), 31 deletions(-) diff --git a/src/context/Localization/language/en.json b/src/context/Localization/language/en.json index 446ba2d6cb..3a451be678 100644 --- a/src/context/Localization/language/en.json +++ b/src/context/Localization/language/en.json @@ -223,8 +223,8 @@ "failedToConnectTor": "Failed to connect to tor", "networkCallsAndTor": "Network calls and some functions may work slower when the Tor is enabled", "checkConnection": "Please check your internet connection and try again. If you continue offline, some features may not be available.", - "incorrectPassword": "Incorrect Password", - "youEnteredIncorrectPasscode": "The passcode you entered is incorrect. Please try again. If you’ve forgotten your passcode, you can recover your wallet using the Forgot Passcode flow with your recovery key.", + "incorrectPassword": "Incorrect passcode", + "youEnteredIncorrectPasscode": "The passcode you entered is incorrect. Try again.", "checkinternetConnection": "Please check your internet connection and try again.", "forgotPasscode": "Forgot passcode?", "resetPasscodeTitle": "Reset Passcode", @@ -2236,7 +2236,7 @@ }, "different": { "title": "Your backup needs updating", - "body": "Your Assisted Server Backup differs from this device. Back up now to update it, then Keeper will check that it matches." + "body": "Back Up Now replaces the server backup with this device’s current recovery data, including hidden and archived items. Items absent from this device will be removed from the backup. Check other Keeper devices first. Keeper will then read back and verify the result." }, "preparing": { "title": "Preparing your backup", diff --git a/src/context/Localization/language/es.json b/src/context/Localization/language/es.json index 8e95a7f68a..4670ae75db 100644 --- a/src/context/Localization/language/es.json +++ b/src/context/Localization/language/es.json @@ -223,8 +223,8 @@ "failedToConnectTor": "Failed to connect to tor", "networkCallsAndTor": "Network calls and some functions may work slower when the Tor is enabled", "checkConnection": "Please check your internet connection and try again. If you continue offline, some features may not be available.", - "incorrectPassword": "Incorrect Password", - "youEnteredIncorrectPasscode": "The passcode you entered is incorrect. Please try again. If you’ve forgotten your passcode, you can recover your wallet using the Forgot Passcode flow with your recovery key.", + "incorrectPassword": "Código incorrecto", + "youEnteredIncorrectPasscode": "El código introducido es incorrecto. Inténtalo de nuevo.", "checkinternetConnection": "Please check your internet connection and try again.", "forgotPasscode": "Forgot passcode?", "resetPasscodeTitle": "Reset Passcode", @@ -2217,7 +2217,7 @@ }, "different": { "title": "Actualiza tu copia", - "body": "Tu copia de seguridad asistida difiere de este dispositivo. Guarda una copia ahora y Keeper comprobará que coincide." + "body": "Guardar ahora reemplaza la copia del servidor con los datos de recuperación actuales de este dispositivo, incluidos los elementos ocultos y archivados. Los elementos que no estén en este dispositivo se eliminarán de la copia. Revisa primero tus otros dispositivos Keeper. Después, Keeper volverá a leer y verificar la copia." }, "preparing": { "title": "Preparando la copia", diff --git a/src/screens/LoginScreen/Login.tsx b/src/screens/LoginScreen/Login.tsx index 862b39879d..e28e8922c3 100644 --- a/src/screens/LoginScreen/Login.tsx +++ b/src/screens/LoginScreen/Login.tsx @@ -1,7 +1,7 @@ /* eslint-disable react/no-unstable-nested-components */ import Text from 'src/components/KeeperText'; import { Box, StatusBar, useColorMode } from '@gluestack-ui/themed-native-base'; -import React, { useContext, useEffect, useState, useMemo } from 'react'; +import React, { useContext, useEffect, useState, useMemo, useRef } from 'react'; import { Pressable, StyleSheet } from 'react-native'; import { widthPercentageToDP } from 'react-native-responsive-screen'; import { hp, windowWidth, wp } from 'src/constants/responsive'; @@ -104,6 +104,7 @@ function LoginScreen({ navigation, route }) { const { common } = translations; const { allAccounts, biometricEnabledAppId } = useAppSelector((state) => state.account); const [forgotModal, setForgotModal] = useState(false); + const completedLogin = useRef(false); const onChangeTorStatus = (status: TorStatus) => { settorStatus(status); @@ -262,8 +263,14 @@ function LoginScreen({ navigation, route }) { } }, [isAuthenticated, internalCheck]); + useEffect(() => { + if (isAuthenticated && !internalCheck) void loginModalAction(); + }, [isAuthenticated, internalCheck]); + const loginModalAction = async () => { - if (isAuthenticated) { + if (isAuthenticated && !completedLogin.current) { + completedLogin.current = true; + setLogging(false); setLoginModal(false); if (relogin) { navigation.navigate({ @@ -278,9 +285,12 @@ function LoginScreen({ navigation, route }) { navigation.reset({ index: 0, routes: [{ name: 'NewKeeperApp' }] }); } dispatch(credsAuthenticated(false)); - const res = await Relay.getAccountManagerDetails(appId); - if (res) dispatch(setAccountManagerDetails(res)); - else dispatch(setAccountManagerDetails(null)); + try { + const res = await Relay.getAccountManagerDetails(appId); + dispatch(setAccountManagerDetails(res || null)); + } catch { + dispatch(setAccountManagerDetails(null)); + } } }; diff --git a/src/services/backup/image.ts b/src/services/backup/image.ts index 1ef9342ddd..0a9446967b 100644 --- a/src/services/backup/image.ts +++ b/src/services/backup/image.ts @@ -105,7 +105,14 @@ export async function compareImages( for (const [id, record] of Object.entries(remote[kind])) { checkpoint(); const current = local[kind][id]; - if (!current || canonical(identity(kind, current)) !== canonical(identity(kind, record))) + // A server-only record may have been deliberately removed on this device. + // Surface it as a difference so the user can explicitly replace the + // backup with the current recoverable state. Never upload during a check. + if (!current) { + result = 'different'; + continue; + } + if (canonical(identity(kind, current)) !== canonical(identity(kind, record))) return 'conflict'; for (const counter of [ 'nextFreeAddressIndex', diff --git a/src/services/backup/repair.ts b/src/services/backup/repair.ts index afb8d67947..160ab288cb 100644 --- a/src/services/backup/repair.ts +++ b/src/services/backup/repair.ts @@ -205,7 +205,10 @@ export function inspectBackup( try { const response = await boundedBackupPost( `${config.RELAY}repairAppBackup`, - { ...payload, expectedRevision }, + // This explicit action replaces the server snapshot with the + // current local state, including intentional deletions. The relay + // still checks the revision atomically before accepting it. + { ...payload, expectedRevision, replaceCurrentState: true }, deadline ); if (response.data?.updated !== true) throw new Error('Backup rejected'); diff --git a/src/store/sagas/login.ts b/src/store/sagas/login.ts index 1f2c839f9a..35028a9bc8 100644 --- a/src/store/sagas/login.ts +++ b/src/store/sagas/login.ts @@ -1,4 +1,4 @@ -import { call, delay, put, race, select } from 'redux-saga/effects'; +import { call, put, select } from 'redux-saga/effects'; import { decrypt, encrypt, @@ -55,10 +55,10 @@ import { checkBackupFreshness } from '../sagaActions/bhr'; import { uaiChecks } from '../sagaActions/uai'; import { applyUpgradeSequence } from './upgrade'; import { resetSyncing } from '../reducers/wallets'; +import { autoSyncWallets } from '../sagaActions/wallets'; import { connectToNode } from '../sagaActions/network'; import { fetchDelayedPolicyUpdate, fetchSignedDelayedTransaction } from '../sagaActions/storage'; import { setAutomaticCloudBackup, setBackupType } from '../reducers/bhr'; -import { autoWalletsSyncWorker } from './wallets'; import { addAccount, saveDefaultWalletState, @@ -225,16 +225,10 @@ function* credentialsAuthWorker({ payload }) { yield put(fetchExchangeRates()); yield put(fetchSignedDelayedTransaction()); yield put(fetchDelayedPolicyUpdate()); - yield race({ - sync: call(autoWalletsSyncWorker, { - payload: { - syncAll: false, - hardRefresh: false, - addNotifications: true, - }, - }), - timeout: delay(15000), - }); + // Wallet/node refresh runs through its watcher after local unlock. + // Waiting for network work here can hold the success modal for 15s. + yield put(resetSyncing()); + yield put(autoSyncWallets(false, false, true)); yield put( uaiChecks([ @@ -247,8 +241,6 @@ function* credentialsAuthWorker({ payload }) { ]) ); - yield put(resetSyncing()); - const { pendingAllBackup, automaticCloudBackup, diff --git a/tests/recovery/repair.test.cjs b/tests/recovery/repair.test.cjs index c6b7d39151..f72053092f 100644 --- a/tests/recovery/repair.test.cjs +++ b/tests/recovery/repair.test.cjs @@ -26,9 +26,26 @@ test('lost upload acknowledgement is resolved by readback without duplicate uplo assert.equal(await f.run(true), 'verified'); assert.deepEqual(f.calls, ['getBackupSnapshot','repairAppBackup','getBackupSnapshot']); }); -test('server-only wallet is preserved and blocks replacement', async () => { - const f = harness(); f.remote.wallets.other = encryption.encrypt(encryption.generateEncryptionKey(f.app.primarySeed), JSON.stringify(wallet('other'))); - assert.equal(await f.run(true), 'conflict'); assert.deepEqual(f.calls, ['getBackupSnapshot']); assert.ok(f.remote.wallets.other); +test('server-only wallet needs user action, then repair removes it and verifies readback', async () => { + const f = harness({ post: async ({ path, payload }) => { + if (path.endsWith('repairAppBackup')) assert.equal(payload.replaceCurrentState, true); + } }); f.remote.wallets.other = encryption.encrypt(encryption.generateEncryptionKey(f.app.primarySeed), JSON.stringify(wallet('other'))); + assert.equal(await f.run(), 'different'); assert.deepEqual(f.calls, ['getBackupSnapshot']); assert.ok(f.remote.wallets.other); + assert.equal(await f.run(true), 'verified'); assert.deepEqual(f.calls.slice(1), ['getBackupSnapshot','repairAppBackup','getBackupSnapshot']); + assert.equal(f.remote.wallets.other, undefined); +}); + +test('current-state repair keeps hidden keys and archived wallets while dropping deleted records', async () => { + const f = harness(); + const key = encryption.generateEncryptionKey(f.app.primarySeed); + f.local.Signer.push({ id:'hidden-key', masterFingerprint:'hidden', signerXpubs:{}, hidden:true }); + f.local.Vault.push({ ...wallet('archived'), archived:true, signers:[], scheme:{m:1,n:1} }); + f.remote.wallets.deleted = encryption.encrypt(key, JSON.stringify(wallet('deleted'))); + assert.equal(await f.run(), 'different'); + assert.equal(await f.run(true), 'verified'); + assert.equal(f.remote.wallets.deleted, undefined); + assert.equal(JSON.parse(encryption.decrypt(key, f.remote.signers['hidden-key'])).hidden, true); + assert.deepEqual(f.remote.vaults, ['archived']); }); test('same IDs with different recovery keys are a conflict', async () => { diff --git a/tests/screens/PinEntry.test.tsx b/tests/screens/PinEntry.test.tsx index 42d7deb06e..8c47e37b08 100644 --- a/tests/screens/PinEntry.test.tsx +++ b/tests/screens/PinEntry.test.tsx @@ -56,7 +56,10 @@ jest.mock('src/services/rest/RestClient', () => ({ TorStatus: {}, })); jest.mock('src/storage/realm/dbManager', () => ({})); -jest.mock('src/services/backend/Relay', () => ({})); +jest.mock('src/services/backend/Relay', () => ({ + __esModule: true, + default: { getAccountManagerDetails: jest.fn(async () => null) }, +})); jest.mock('react-native-biometrics', () => jest.fn(() => ({}))); jest.mock('@react-native-firebase/app', () => ({ getApp: jest.fn() })); jest.mock('@react-native-firebase/messaging', () => ({ @@ -88,6 +91,7 @@ function mount(component: React.ReactElement) { beforeEach(() => { jest.useFakeTimers(); mockDispatch.mockClear(); + mockState.login.isAuthenticated = false; }); afterEach(() => { act(() => screen?.unmount()); @@ -96,6 +100,21 @@ afterEach(() => { }); describe('PIN entry under batched rapid taps', () => { + test('successful local authentication enters the app without a second tap', async () => { + const navigation = { replace: jest.fn() }; + const route = { params: {} }; + mount(); + await act(async () => { + mockState.login.isAuthenticated = true; + screen.update(); + }); + expect(navigation.replace).toHaveBeenCalledWith('App'); + await act(async () => { + screen.update(); + }); + expect(navigation.replace).toHaveBeenCalledTimes(1); + }); + test('login keeps every digit, caps at four and deletes in order', () => { mount(); enter('123456'); From 5254796dabb56f56d5c2aeb52e7d79c242112c53 Mon Sep 17 00:00:00 2001 From: Anant Date: Mon, 5 Oct 2026 17:58:48 +0530 Subject: [PATCH 007/254] fix: complete backup and timelock validation for 2.6.2 --- android/app/build.gradle | 4 +- ios/hexa_keeper.xcodeproj/project.pbxproj | 16 +- package.json | 2 +- release/README.md | 10 +- release/version.json | 22 +- src/hooks/useSignerIntel.tsx | 10 +- src/screens/Vault/AddEmergencyKey.tsx | 8 +- src/screens/Vault/AddReserveKey.tsx | 10 +- src/screens/Vault/AddSigningDevice.tsx | 47 +- .../Vault/VaultMigrationController.tsx | 52 +- src/services/backup/image.ts | 51 +- src/services/backup/repair.ts | 51 +- src/store/sagas/bhr.ts | 28 +- tests/recovery/restoreIntegrity.test.cjs | 25 + tests/recovery/sagaHarness.cjs | 1 + tests/recovery/sharedVaultRepair.test.cjs | 96 +++ tests/recovery/signerPersistence.test.cjs | 37 ++ tests/recovery/vaultPersistence.test.cjs | 49 ++ .../addReserveTimelockBoundary.test.ts | 561 ++++++++++++++++++ .../services/addSigningDeviceTimelock.test.ts | 450 ++++++++++++++ 20 files changed, 1449 insertions(+), 81 deletions(-) create mode 100644 tests/recovery/sharedVaultRepair.test.cjs create mode 100644 tests/recovery/signerPersistence.test.cjs create mode 100644 tests/recovery/vaultPersistence.test.cjs create mode 100644 tests/services/addReserveTimelockBoundary.test.ts create mode 100644 tests/services/addSigningDeviceTimelock.test.ts diff --git a/android/app/build.gradle b/android/app/build.gradle index c2f737d4d3..eebb1acfe2 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -98,8 +98,8 @@ android { applicationId "io.hexawallet.keeper" minSdkVersion rootProject.ext.minSdkVersion targetSdkVersion rootProject.ext.targetSdkVersion - versionCode 624 - versionName "2.5.17" + versionCode 625 + versionName "2.6.2" missingDimensionStrategy 'react-native-camera', 'general' missingDimensionStrategy 'store', 'play' multiDexEnabled true diff --git a/ios/hexa_keeper.xcodeproj/project.pbxproj b/ios/hexa_keeper.xcodeproj/project.pbxproj index 88ff8d481f..52aa23ef44 100644 --- a/ios/hexa_keeper.xcodeproj/project.pbxproj +++ b/ios/hexa_keeper.xcodeproj/project.pbxproj @@ -748,7 +748,7 @@ CODE_SIGN_ENTITLEMENTS = hexa_keeper/hexa_keeper.entitlements; CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 617; + CURRENT_PROJECT_VERSION = 618; DEVELOPMENT_TEAM = Y5TCB759QL; ENABLE_BITCODE = NO; HEADER_SEARCH_PATHS = ( @@ -849,7 +849,7 @@ "$(inherited)", "\"$(SRCROOT)\"", ); - MARKETING_VERSION = 2.5.17; + MARKETING_VERSION = 2.6.2; OTHER_LDFLAGS = ( "$(inherited)", "-ObjC", @@ -876,7 +876,7 @@ CODE_SIGN_IDENTITY = "Apple Development"; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Distribution"; CODE_SIGN_STYLE = Manual; - CURRENT_PROJECT_VERSION = 617; + CURRENT_PROJECT_VERSION = 618; DEVELOPMENT_TEAM = Y5TCB759QL; "DEVELOPMENT_TEAM[sdk=iphoneos*]" = Y5TCB759QL; HEADER_SEARCH_PATHS = ( @@ -977,7 +977,7 @@ "$(inherited)", "\"$(SRCROOT)\"", ); - MARKETING_VERSION = 2.5.17; + MARKETING_VERSION = 2.6.2; OTHER_LDFLAGS = ( "$(inherited)", "-ObjC", @@ -1147,7 +1147,7 @@ CODE_SIGN_IDENTITY = "Apple Development"; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Distribution"; CODE_SIGN_STYLE = Manual; - CURRENT_PROJECT_VERSION = 617; + CURRENT_PROJECT_VERSION = 618; DEVELOPMENT_TEAM = Y5TCB759QL; "DEVELOPMENT_TEAM[sdk=iphoneos*]" = Y5TCB759QL; ENABLE_BITCODE = NO; @@ -1250,7 +1250,7 @@ "$(PROJECT_DIR)", "\"$(SRCROOT)\"", ); - MARKETING_VERSION = 2.5.17; + MARKETING_VERSION = 2.6.2; OTHER_LDFLAGS = ( "$(inherited)", "-ObjC", @@ -1278,7 +1278,7 @@ CODE_SIGN_ENTITLEMENTS = hexa_keeper_dev.entitlements; CODE_SIGN_IDENTITY = "Apple Distribution"; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Distribution"; - CURRENT_PROJECT_VERSION = 617; + CURRENT_PROJECT_VERSION = 618; DEVELOPMENT_TEAM = Y5TCB759QL; "DEVELOPMENT_TEAM[sdk=iphoneos*]" = Y5TCB759QL; HEADER_SEARCH_PATHS = ( @@ -1380,7 +1380,7 @@ "$(PROJECT_DIR)", "\"$(SRCROOT)\"", ); - MARKETING_VERSION = 2.5.17; + MARKETING_VERSION = 2.6.2; OTHER_LDFLAGS = ( "$(inherited)", "-ObjC", diff --git a/package.json b/package.json index 072f11b411..dc78f187da 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "hexa_keeper", - "version": "2.5.17", + "version": "2.6.2", "private": true, "scripts": { "ios": "npx react-native run-ios --scheme=hexa_keeper_dev --simulator 'iPhone 16' ", diff --git a/release/README.md b/release/README.md index ea88ab6702..4e484e0251 100644 --- a/release/README.md +++ b/release/README.md @@ -1,15 +1,17 @@ # Release version verification -**Current milestone:** Now candidate 2.5.17: Recovery Key backup comparison/repair and complete data restore, Version History, PIN handling, and the privately reviewed timelock prevention change. Keep advisory details and private fixtures out of public release material. +**Current milestone:** R02 / 2.6.2: faster entry after local authentication, Assisted Server Backup current-state correctness, narrow timelock prevention, and the selected cloud credential follow-ups. Keep private advisory details, credentials and fixtures out of public release material. -`version.json` selects 2.5.17, Android 624 and iOS 617. Authenticated Google Play evidence on 29 September shows highest uploaded Android code 623; App Store Connect shows latest iOS upload 2.5.16 (616), Complete, and the production version Ready for Distribution. Refresh store baselines again before upload. The paired Recovery relay is deployed and its live encrypted repair/readback smoke passed. Independent security acceptance and final physical testing remain pending. +`version.json` selects 2.6.2, Android 625 and iOS 618. Authenticated store checks on 5 October 2026 found highest uploaded Android code 624 and iOS build 617, both at 2.5.17. Refresh these baselines before upload. Development builds and local relay validation are separate from reviewed production builds, deployment and store submission. R02 is not released. + +For R02, the owner accepts real-phone upgrade, Face ID and camera feedback after release. Do not require Developer Mode or physical-device connections to finish preparation. Record simulator, automated and funded testnet evidence accurately; they do not establish physical or hardware-signer acceptance. Already-affected-wallet assessment and consented repair are scheduled for R04 / 2.6.3; R03 is the backend open-source milestone. 1. Read all uploaded builds and release tracks for Android package `io.hexawallet.bitcoinkeeper`, and uploaded iOS builds for `io.hexawallet.keeper`. Preserve the timestamp and evidence outside the repository. Refresh before upload; preflight accepts evidence at most 24 hours old. 2. Update the manifest with the highest known marketing version and highest uploaded build per platform. Select one greater marketing version for both stores and greater platform build counters. Check unpublished App Store version records as well. 3. Run `npm run version:apply` to synchronize package.json, Android versionName/versionCode and both Keeper iOS targets. Both Info.plist files must reference `$(CURRENT_PROJECT_VERSION)` and `$(MARKETING_VERSION)`; hardcoded values are rejected. Review the diff, then run `npm run version:check` and `npm run test:release`. 4. Reconcile release source with the published versions, prepare a clean release checkout and set `KEEPER_RELEASE_COMMIT` to its complete verified commit SHA. Run `npm run release:preflight`. The manifest cannot contain its own commit SHA. 5. Build from a reviewed production configuration and existing signing identities. Check extracted IPA, APK and AAB metadata against the manifest, verify signing fingerprints and record checksums. A source preflight does not verify the resulting binaries. -6. Test the signed builds, including upgrades, before public submission. +6. Test the signed builds before public submission; record the R02 physical-device deferral above separately. Both existing Fastlane `live` lanes run preflight before building and consume `.env.production` without downloading over `.env`. They no longer increment production build numbers. Android notifications require explicit `KEEPER_NOTIFY_SLACK=1`. @@ -29,7 +31,7 @@ Android production signing reads `STORE_PASSWORD`, `KEY_PASSWORD`, and `KEY_ALIA ## Manual release checks -- Upgrade an existing installation with a designated test wallet on a physical iPhone and Android device; do not uninstall first. Confirm unlock, wallet visibility and settings survive. +- Post-release for R02 by owner decision: upgrade an existing installation with a designated test wallet on a physical iPhone and Android device; do not uninstall first. Confirm unlock, wallet visibility and settings survive. - Check the displayed marketing version and native build counter against the release manifest on both platforms. - Open Ask Keeper on both platforms and send a non-sensitive sample question to the intended backend. Confirm the response and error/retry behavior; local isolated-backend entry checks do not test AI responses. - Open Wallet Settings > Dust Report. Check an empty test wallet, then a designated testnet fixture with Do Not Spend coins. Confirm the report, ordinary-spend exclusion, and Donate Dust confirmation/amount/fee behavior. Record separately whether a testnet signing transaction completed; an empty-wallet result cannot verify donation. diff --git a/release/version.json b/release/version.json index 9a4e7ebc01..553cacad87 100644 --- a/release/version.json +++ b/release/version.json @@ -1,21 +1,21 @@ { - "version": "2.5.17", - "androidVersionCode": 624, - "iosBuildNumber": 617, + "version": "2.6.2", + "androidVersionCode": 625, + "iosBuildNumber": 618, "baselines": { "android": { "applicationId": "io.hexawallet.bitcoinkeeper", - "verifiedAt": "2026-09-29T10:32:44Z", - "highestUploadedBuild": 623, - "highestMarketingVersion": "2.5.16", - "productionRelease": "623 (2.5.16)" + "verifiedAt": "2026-10-05T10:04:03Z", + "highestUploadedBuild": 624, + "highestMarketingVersion": "2.5.17", + "productionRelease": "624 (2.5.17)" }, "ios": { "applicationId": "io.hexawallet.keeper", - "verifiedAt": "2026-09-29T12:45:29.673796+00:00", - "highestUploadedBuild": 616, - "highestMarketingVersion": "2.5.16", - "productionRelease": "616 (2.5.16)" + "verifiedAt": "2026-10-05T10:04:36Z", + "highestUploadedBuild": 617, + "highestMarketingVersion": "2.5.17", + "productionRelease": "617 (2.5.17)" } } } diff --git a/src/hooks/useSignerIntel.tsx b/src/hooks/useSignerIntel.tsx index adef347341..e76f725862 100644 --- a/src/hooks/useSignerIntel.tsx +++ b/src/hooks/useSignerIntel.tsx @@ -1,4 +1,4 @@ -import { MiniscriptTypes, SignerType } from 'src/services/wallets/enums'; +import { SignerType } from 'src/services/wallets/enums'; import { getSignerNameFromType, isSignerAMF } from 'src/hardware'; import { SubscriptionTier } from 'src/models/enums/SubscriptionTier'; import { VaultScheme, VaultSigner } from 'src/services/wallets/interfaces/vault'; @@ -64,11 +64,9 @@ const useSignerIntel = ({ areSignersValid = false; } else { const signerCount = Array.from(selectedSigners.keys()).length; - const maxKeys = scheme?.miniscriptScheme?.usedMiniscriptTypes?.includes( - MiniscriptTypes.INHERITANCE - ) - ? scheme.n + 1 - : scheme.n; + // AddSigningDevice selects the primary quorum. Inheritance and Emergency + // Keys are retained/configured separately and must not disable this step. + const maxKeys = scheme.n; areSignersValid = signerCount > 0 && !( diff --git a/src/screens/Vault/AddEmergencyKey.tsx b/src/screens/Vault/AddEmergencyKey.tsx index b13a5a2f10..83a24e8d14 100644 --- a/src/screens/Vault/AddEmergencyKey.tsx +++ b/src/screens/Vault/AddEmergencyKey.tsx @@ -92,7 +92,7 @@ function AddEmergencyKey({ route }) { const isDarkMode = colorMode === 'dark'; useEffect(() => { - if (selectedSigner || keyToRotate) return; + if (selectedSigner) return; if ( !activeVault?.id || @@ -110,7 +110,7 @@ function AddEmergencyKey({ route }) { (key) => key.masterFingerprint === emergencyKeyFingerprint ); - if (emergencyKey) { + if (emergencyKey && (!keyToRotate || getKeyUID(emergencyKey) !== getKeyUID(keyToRotate))) { setSelectedSigner([emergencyKey]); } }, [activeVault?.id, keyToRotate, selectedSigner]); @@ -149,6 +149,7 @@ function AddEmergencyKey({ route }) { isAddInheritanceKey, isAddEmergencyKey, hasInitialTimelock, + initialTimelockDuration, currentBlockHeight, onGoBack: (signer) => setSelectedSigner(signer), }); @@ -161,6 +162,7 @@ function AddEmergencyKey({ route }) { isAddInheritanceKey, isAddEmergencyKey, hasInitialTimelock, + initialTimelockDuration, currentBlockHeight, ]); @@ -302,7 +304,7 @@ function AddEmergencyKey({ route }) { emergencyKeys={ selectedSigner ? [{ key: selectedSigner[0], duration: selectedOption.label }] : [] } - initialTimelockDuration={initialTimelockDuration ?? 0} + initialTimelockDuration={initialTimelockDuration} currentBlockHeight={currentBlockHeight} miniscriptTypes={[ ...(hasInitialTimelock ? [MiniscriptTypes.TIMELOCKED] : []), diff --git a/src/screens/Vault/AddReserveKey.tsx b/src/screens/Vault/AddReserveKey.tsx index 51fa4dd5fd..fdcf2db141 100644 --- a/src/screens/Vault/AddReserveKey.tsx +++ b/src/screens/Vault/AddReserveKey.tsx @@ -98,7 +98,7 @@ function AddReserveKey({ route }) { const isDarkMode = colorMode === 'dark'; useEffect(() => { - if (selectedSigner || keyToRotate) return; + if (selectedSigner) return; if ( !activeVault?.id || @@ -117,7 +117,7 @@ function AddReserveKey({ route }) { (key) => key.masterFingerprint === inheritanceKeyFingerprint ); - if (inheritanceKey) { + if (inheritanceKey && (!keyToRotate || getKeyUID(inheritanceKey) !== getKeyUID(keyToRotate))) { setSelectedSigner([inheritanceKey]); } }, [activeVault?.id, keyToRotate, selectedSigner]); @@ -156,6 +156,7 @@ function AddReserveKey({ route }) { isAddInheritanceKey, isAddEmergencyKey, hasInitialTimelock, + initialTimelockDuration, currentBlockHeight, onGoBack: (signer) => setSelectedSigner(signer), }); @@ -168,6 +169,7 @@ function AddReserveKey({ route }) { isAddInheritanceKey, isAddEmergencyKey, hasInitialTimelock, + initialTimelockDuration, currentBlockHeight, ]); @@ -322,10 +324,10 @@ function AddReserveKey({ route }) { inheritanceKeys={ selectedSigner ? [{ key: selectedSigner[0], duration: selectedOption.label }] : [] } - initialTimelockDuration={initialTimelockDuration ?? 0} + initialTimelockDuration={initialTimelockDuration} currentBlockHeight={currentBlockHeight} miniscriptTypes={[ - ...(initialTimelockDuration ? [MiniscriptTypes.TIMELOCKED] : []), + ...(hasInitialTimelock ? [MiniscriptTypes.TIMELOCKED] : []), MiniscriptTypes.INHERITANCE, ]} setVaultCreatedModalVisible={setVaultCreatedModalVisible} diff --git a/src/screens/Vault/AddSigningDevice.tsx b/src/screens/Vault/AddSigningDevice.tsx index 4143c03f8b..300a814692 100644 --- a/src/screens/Vault/AddSigningDevice.tsx +++ b/src/screens/Vault/AddSigningDevice.tsx @@ -243,8 +243,27 @@ const setInitialKeys = ( keyToRotate ) => { if (activeVault) { - // setting initital keys (update if scheme has changed) - const vaultKeys = activeVault.signers.filter( + const isEnhancedVault = + activeVault.type === VaultType.MINISCRIPT && + activeVault.scheme?.miniscriptScheme?.usedMiniscriptTypes?.some((type) => + [ + MiniscriptTypes.TIMELOCKED, + MiniscriptTypes.INHERITANCE, + MiniscriptTypes.EMERGENCY, + ].includes(type) + ); + // Select the stored primary K roles before limiting the number of keys. + const primaryFingerprints = new Set( + Object.entries( + activeVault.scheme?.miniscriptScheme?.miniscriptElements?.signerFingerprints || {} + ) + .filter(([identifier]) => /^K[1-9]\d*$/.test(identifier)) + .map(([, fingerprint]) => fingerprint) + ); + const existingKeys = isEnhancedVault + ? activeVault.signers.filter((key) => primaryFingerprints.has(key.masterFingerprint)) + : activeVault.signers; + const vaultKeys = existingKeys.filter( (key) => keyToRotate && getKeyUID(key) !== getKeyUID(keyToRotate) ); const isMultisig = @@ -361,6 +380,7 @@ function Footer({ isCollaborativeFlow, isAssistedWalletFlow, hasInitialTimelock, + initialTimelockDuration, isReserveKeyFlow, isEmergencyKeyFlow, isAddInheritanceKey, @@ -409,7 +429,7 @@ function Footer({ primaryLoading={relayVaultUpdateLoading} primaryText={keyToRotate ? 'Replace Key' : common.proceed} primaryCallback={ - !hasInitialTimelock + !hasInitialTimelock || initialTimelockDuration !== undefined ? !isAddInheritanceKey ? !isAddEmergencyKey ? keyToRotate @@ -423,11 +443,13 @@ function Footer({ isHotWallet, vaultType, hasInitialTimelock, + initialTimelockDuration, isAddInheritanceKey, currentBlockHeight, hotWalletInstanceNum, selectedSigners: signersList, vaultId, + keyToRotate, }); } : () => @@ -461,8 +483,10 @@ function Footer({ isAddInheritanceKey, isAddEmergencyKey, hasInitialTimelock, + initialTimelockDuration, currentBlockHeight, selectedSigners: signersList, + keyToRotate, }) ) : () => @@ -489,8 +513,10 @@ function Footer({ isAddInheritanceKey, isAddEmergencyKey, hasInitialTimelock, + initialTimelockDuration, currentBlockHeight, selectedSigners: signersList, + keyToRotate, }) ) : () => @@ -504,8 +530,10 @@ function Footer({ isAddInheritanceKey, isAddEmergencyKey, hasInitialTimelock, + initialTimelockDuration, currentBlockHeight, selectedSigners: signersList, + keyToRotate, }) ) } @@ -1174,6 +1202,7 @@ function AddSigningDevice() { isNewSchemeFlow?: boolean; signerFilters?: SignerType | Array; hasInitialTimelock?: boolean; + initialTimelockDuration?: string; }; }; const { @@ -1191,7 +1220,8 @@ function AddSigningDevice() { selectedSignersFromParams, isAddInheritanceKey: isAddInheritanceKeyParam = false, isAddEmergencyKey: isAddEmergencyKeyParam = false, - hasInitialTimelock, + hasInitialTimelock: hasInitialTimelockParam, + initialTimelockDuration, isNewSchemeFlow = false, currentBlockHeight, signerFilters = [], @@ -1216,6 +1246,13 @@ function AddSigningDevice() { const [vaultKeys, setVaultKeys] = useState([]); const { activeVault, allVaults } = useVault({ vaultId }); const scheme = isNewSchemeFlow ? schemeParam : activeVault ? activeVault.scheme : schemeParam; + // Existing wallets keep their policy requirement even if a caller drops the route flag. + const hasInitialTimelock = isNewSchemeFlow + ? hasInitialTimelockParam + : hasInitialTimelockParam || + activeVault?.scheme?.miniscriptScheme?.usedMiniscriptTypes?.includes( + MiniscriptTypes.TIMELOCKED + ); const isAddInheritanceKey = isNewSchemeFlow ? isAddInheritanceKeyParam : activeVault @@ -1466,6 +1503,7 @@ function AddSigningDevice() { setCreating={setCreating} vaultType={vaultType} currentBlockHeight={currentBlockHeight} + initialTimelockDuration={initialTimelockDuration} miniscriptTypes={[ ...(hasInitialTimelock ? [MiniscriptTypes.TIMELOCKED] : []), ...(isAddInheritanceKey ? [MiniscriptTypes.INHERITANCE] : []), @@ -1519,6 +1557,7 @@ function AddSigningDevice() { isAddInheritanceKey={isAddInheritanceKey} isAddEmergencyKey={isAddEmergencyKey} hasInitialTimelock={hasInitialTimelock} + initialTimelockDuration={initialTimelockDuration} currentBlockHeight={currentBlockHeight} onGoBack={onGoBack} vaultKeys={vaultKeys} diff --git a/src/screens/Vault/VaultMigrationController.tsx b/src/screens/Vault/VaultMigrationController.tsx index 66736912d3..d0ea0f63d7 100644 --- a/src/screens/Vault/VaultMigrationController.tsx +++ b/src/screens/Vault/VaultMigrationController.tsx @@ -305,9 +305,33 @@ function VaultMigrationController({ // The getCurrentTimeForLock and getTimelockDuration control whether // a vault created will use UNIX timestamp or block height based timelocks. - const getCurrentTimeForLock = () => { + const getCurrentTimeForLock = async () => { if (activeVault && isVaultUsingBlockHeightTimelock(activeVault)) { - return currentBlockHeight; + let currentSyncedBlockHeight = currentBlockHeight; + if ( + !Number.isSafeInteger(currentSyncedBlockHeight) || + currentSyncedBlockHeight <= 0 || + currentSyncedBlockHeight >= 500000000 + ) { + try { + currentSyncedBlockHeight = (await WalletUtilities.fetchCurrentBlockHeight()) + .currentBlockHeight; + } catch { + throw new Error( + 'Failed to fetch current chain data, please check your connection and try again' + ); + } + } + if ( + !Number.isSafeInteger(currentSyncedBlockHeight) || + currentSyncedBlockHeight <= 0 || + currentSyncedBlockHeight >= 500000000 + ) { + throw new Error( + 'Failed to fetch current chain data, please check your connection and try again' + ); + } + return currentSyncedBlockHeight; } return Math.floor(Date.now() / 1000); }; @@ -383,23 +407,7 @@ function VaultMigrationController({ ); const multisigScriptType = MultisigScriptType.MINISCRIPT_MULTISIG; - // let currentSyncedBlockHeight = currentBlockHeight; - // if (!currentSyncedBlockHeight) { - // try { - // currentSyncedBlockHeight = (await WalletUtilities.fetchCurrentBlockHeight()) - // .currentBlockHeight; - // } catch (err) { - // console.log('Failed to re-fetch current block height: ' + err); - // } - // if (!currentSyncedBlockHeight) { - // showToast( - // 'Failed to fetch current chain data, please check your connection and try again', - // - // ); - // setCreating(false); - // return; - // } - // } + const currentTimeForLock = await getCurrentTimeForLock(); const inheritanceSignerWithTimelocks = []; const emergencySignerWithTimelocks = []; @@ -413,7 +421,7 @@ function VaultMigrationController({ } inheritanceSignerWithTimelocks.push({ signer: key, - timelock: getCurrentTimeForLock() + initialTimelock + timelock, + timelock: currentTimeForLock + initialTimelock + timelock, }); } } @@ -427,7 +435,7 @@ function VaultMigrationController({ } emergencySignerWithTimelocks.push({ signer: key, - timelock: getCurrentTimeForLock() + initialTimelock + timelock, + timelock: currentTimeForLock + initialTimelock + timelock, }); } } @@ -437,7 +445,7 @@ function VaultMigrationController({ inheritanceSignerWithTimelocks, emergencySignerWithTimelocks, vaultInfo.vaultScheme, - initialTimelock ? getCurrentTimeForLock() + initialTimelock : 0 + initialTimelock ? currentTimeForLock + initialTimelock : 0 ); if (!miniscriptElements) { diff --git a/src/services/backup/image.ts b/src/services/backup/image.ts index 0a9446967b..f4e7ca668a 100644 --- a/src/services/backup/image.ts +++ b/src/services/backup/image.ts @@ -65,6 +65,41 @@ export function recoveryContent(kind: keyof BackupImage, record: any) { return { ...result, specs }; } +function vaultSchemeIdentity(scheme: any) { + const miniscript = scheme?.miniscriptScheme; + const elements = miniscript?.miniscriptElements; + if (!elements) return scheme; + const keyIdentity = (key: any) => ({ + ...key, + uniqueKeyIdentifier: key.uniqueKeyIdentifier ?? null, + }); + // Realm defaults optional key identifiers to null and does not persist the + // compiler's phase weights. Compare the compiled policy/script unchanged; + // only then can these non-persisted weights be excluded from key identity. + const hasCompiledPolicy = !!miniscript.miniscript && !!miniscript.miniscriptPolicy; + return { + ...scheme, + miniscriptScheme: { + ...miniscript, + miniscriptElements: { + ...elements, + keysInfo: elements.keysInfo?.map(keyIdentity), + phases: elements.phases?.map((phase: any) => { + const normalized = { + ...phase, + paths: phase.paths?.map((path: any) => ({ + ...path, + keys: path.keys?.map(keyIdentity), + })), + }; + if (hasCompiledPolicy) delete normalized.probability; + return normalized; + }), + }, + }, + }; +} + function identity(kind: keyof BackupImage, record: any) { if (kind === 'wallets') return { @@ -81,14 +116,26 @@ function identity(kind: keyof BackupImage, record: any) { if (kind === 'vaults') return { networkType: record.networkType, - scheme: record.scheme, + scheme: vaultSchemeIdentity(record.scheme), signers: record.signers, scriptType: record.scriptType, }; if (kind === 'signers') return { masterFingerprint: record.masterFingerprint, - signerXpubs: record.signerXpubs, + // Realm adds empty script-type lists when a newly added signer is saved. + // Incremental backups can contain the pre-persistence form. Empty lists + // do not change key identity; populated lists and private keys still do. + signerXpubs: Object.fromEntries( + Object.entries(record.signerXpubs || {}) + .filter(([, keys]) => !Array.isArray(keys) || keys.length > 0) + .map(([type, keys]) => [ + type, + Array.isArray(keys) + ? keys.map((key) => ({ ...key, xpriv: key.xpriv ?? null })) + : keys, + ]) + ), networkType: record.networkType, type: record.type, }; diff --git a/src/services/backup/repair.ts b/src/services/backup/repair.ts index 160ab288cb..25b543c7a1 100644 --- a/src/services/backup/repair.ts +++ b/src/services/backup/repair.ts @@ -167,6 +167,7 @@ export function inspectBackup( const local = await captureBackupImage(assertCurrent); assertCurrent(); let expectedRevision: string; + let needsVaultRepair = false; const read = async () => { const response = await boundedBackupPost( `${config.RELAY}getBackupSnapshot`, @@ -177,10 +178,50 @@ export function inspectBackup( if (!/^[a-f0-9]{64}$/.test(response.data?.revision)) throw new Error('Backup snapshot revision unavailable'); expectedRevision = response.data.revision; - return decodeImage(key, response.data, appId, assertCurrent); + const unavailable = response.data.unavailableVaultIds ?? []; + if (!Array.isArray(unavailable) || new Set(unavailable).size !== unavailable.length) + throw new Error('Invalid unavailable backup records'); + const references = response.data.appImage?.vaults; + if ( + unavailable.length && + (!Array.isArray(references) || + new Set(references).size !== references.length || + unavailable.some( + (id) => + typeof id !== 'string' || + !id || + !references.includes(id) || + response.data.allVaultImages?.some((record) => record.vaultId === id) + )) + ) + throw new Error('Invalid unavailable backup references'); + // A legacy shared-wallet image may have been overwritten by another + // participant. The relay withholds that participant's ciphertext. Only + // this explicit backup check may compare the remaining records; clean + // restore still rejects the incomplete reference list. + const availableResponse = unavailable.length + ? { + ...response.data, + appImage: { + ...response.data.appImage, + vaults: references.filter((id) => !unavailable.includes(id)), + }, + } + : response.data; + const decoded = await decodeImage(key, availableResponse, appId, assertCurrent); + needsVaultRepair = + unavailable.length > 0 || + response.data.allVaultImages.some( + (record) => + typeof record.isArchived === 'boolean' && + record.isArchived !== !!decoded.vaults[record.vaultId]?.archived + ); + return decoded; }; const remote = await read(); - const comparison = await compareImages(local, remote, assertCurrent); + const contentComparison = await compareImages(local, remote, assertCurrent); + const comparison = + contentComparison === 'matched' && needsVaultRepair ? 'different' : contentComparison; assertCurrent(); if (comparison === 'conflict') { notify('conflict'); @@ -216,7 +257,11 @@ export function inspectBackup( // A lost response is not proof the upload failed. Always read back. } notify('verifying'); - if ((await compareImages(local, await read(), assertCurrent)) !== 'matched') + const verifiedRemote = await read(); + if ( + needsVaultRepair || + (await compareImages(local, verifiedRemote, assertCurrent)) !== 'matched' + ) throw new Error('Backup not verified'); } if ( diff --git a/src/store/sagas/bhr.ts b/src/store/sagas/bhr.ts index c60ec711eb..206aaceb2d 100644 --- a/src/store/sagas/bhr.ts +++ b/src/store/sagas/bhr.ts @@ -1,7 +1,7 @@ import { buffers, END, eventChannel } from 'redux-saga'; import { inspectBackup, RepairPhase } from 'src/services/backup/repair'; import { prepareRecoveryImage } from 'src/services/backup/restore'; -import { BackupImage } from 'src/services/backup/image'; +import { BackupImage, pauseBackup } from 'src/services/backup/image'; import { markBackupMutation } from 'src/services/backup/transport'; import * as bip39 from 'bip39'; import { Wallet } from 'src/services/wallets/interfaces/wallet'; @@ -189,16 +189,6 @@ export function* updateVaultImageWorker({ JSON.stringify(sanitizeVaultSignersForSeedKeyBackup(vault)) ); - if (isUpdate) { - const response = yield call(Relay.updateVaultImage, { - isUpdate, - appId: id, - vaultId: vault.id, - vault: vaultEncrypted, - }); - return response; - } - const signersData: Array<{ signerId: string; xfpHash: string; @@ -211,6 +201,18 @@ export function* updateVaultImageWorker({ }); } + if (isUpdate) { + const response = yield call(Relay.updateVaultImage, { + isUpdate, + signersData, + appId: id, + vaultId: vault.id, + vault: vaultEncrypted, + isArchived: !!vault.archived, + }); + return response; + } + // TODO to be removed const subscriptionStrings = JSON.stringify(subscription); @@ -220,6 +222,7 @@ export function* updateVaultImageWorker({ vaultId: vault.id, signersData, vault: vaultEncrypted, + isArchived: !!vault.archived, subscription: subscriptionStrings, ...(archiveVaultId && { archiveVaultId }), }); @@ -705,6 +708,9 @@ function* writeRecoveredObject(schema: RealmSchema, record: any) { // throwing. Never count that record as recovered or report partial success. const written = yield call(dbManager.createObject, schema, record); if (written !== true) throw new Error('Recovery data unavailable'); + // Realm writes complete synchronously. Break the saga's synchronous effect + // chain between records so a large restore cannot exhaust the JS stack. + yield call(pauseBackup); } function* healthCheckSatutsUpdateWorker({ diff --git a/tests/recovery/restoreIntegrity.test.cjs b/tests/recovery/restoreIntegrity.test.cjs index dc2b4f6025..8ff08e3c1e 100644 --- a/tests/recovery/restoreIntegrity.test.cjs +++ b/tests/recovery/restoreIntegrity.test.cjs @@ -64,6 +64,31 @@ function recoveryFixture() { const completed = (f) => f.actions.some((a) => a.type === 'setAppCreated' && a.payload === true); const errors = (f) => f.actions.filter((a) => a.type === 'setAppImageError' && a.payload); +test( + 'hundreds of synchronous Realm writes complete recovery without stalling', + { timeout: 15000 }, + async () => { + const f = recoveryFixture(); + const key = encryption.generateEncryptionKey(f.app.primarySeed); + const labels = Array.from({ length: 600 }, (_, index) => ({ + id: `label-${index}`, + label: `Saved label ${index}`, + ref: `tx-${index}`, + type: 'TXN', + })); + f.response.labels = labels.map((label) => ({ + id: `encrypted-${label.id}`, + content: encryption.encrypt(key, JSON.stringify(label)), + })); + f.remote.labels = f.response.labels.map((label) => label.id); + await f.restore(); + assert.deepEqual(errors(f), []); + assert.equal(completed(f), true); + assert.deepEqual(plain(f.collections.Tags), labels); + assert.equal(f.actions.at(-1).type, 'appImagerecoveryRetry'); + } +); + test('clean recovery recreates keys, BTC/USDT, archived vault, labels and custom nodes', async () => { const f = recoveryFixture(); await f.restore(); diff --git a/tests/recovery/sagaHarness.cjs b/tests/recovery/sagaHarness.cjs index 3a378a08d9..ab82a1d9c4 100644 --- a/tests/recovery/sagaHarness.cjs +++ b/tests/recovery/sagaHarness.cjs @@ -118,6 +118,7 @@ function fixture(options = {}) { ...enums, ...account, prepareRecoveryImage, + pauseBackup: image.pauseBackup, console: { log() {}, error() {} }, Buffer, _: require('lodash'), diff --git a/tests/recovery/sharedVaultRepair.test.cjs b/tests/recovery/sharedVaultRepair.test.cjs new file mode 100644 index 0000000000..7b9027b846 --- /dev/null +++ b/tests/recovery/sharedVaultRepair.test.cjs @@ -0,0 +1,96 @@ +const { test } = require('node:test'); +const assert = require('node:assert/strict'); +const { encryption, image, harness, wallet, loadModule } = require('./helpers.cjs'); +const { prepareRecoveryImage } = loadModule('src/services/backup/restore.ts', { './image': image }); +const clone = value => JSON.parse(JSON.stringify(value)); +function fixture({ unavailable = ['shared'], leaveUnavailable = false } = {}) { + const state = { + appImage: { appId:'disposable', wallets:{}, signers:{}, nodes:[], vaults:['shared'], labels:[] }, + allVaultImages:[], labels:[], unavailableVaultIds:unavailable, revision:'a'.repeat(64), + }; + let uploads=0; + const f=harness({ adapter:async (endpoint,payload) => { + if(endpoint==='getBackupSnapshot') return {data:clone(state)}; + assert.equal(endpoint,'repairAppBackup'); + assert.equal(payload.expectedRevision,state.revision); + assert.equal(payload.replaceCurrentState,true); + uploads++; + Object.assign(state.appImage, {wallets:payload.walletObject,signers:payload.signersObject, + nodes:payload.nodes,vaults:Object.keys(payload.vaultObject),labels:payload.labels.map(v=>v.id)}); + state.allVaultImages=Object.values(payload.vaultObject); + state.labels=payload.labels; + if (!leaveUnavailable) state.unavailableVaultIds=[]; + state.revision='b'.repeat(64); + return {data:{updated:true}}; + }}); + f.local.Vault.push({...wallet('shared'),signers:[],scheme:{m:1,n:1},archived:false}); + const key=encryption.generateEncryptionKey(f.app.primarySeed); + return {f,state,key,uploads:()=>uploads}; +} +test('unavailable collaborative ciphertext requires user action and cannot be clean-restored',async()=>{ + const {f,state,key,uploads}=fixture(); + assert.equal(await f.run(),'different'); + assert.equal(uploads(),0); + assert.deepEqual(f.calls,['getBackupSnapshot']); + await assert.rejects(prepareRecoveryImage(key,f.app.id,state.appImage,state.allVaultImages,state.labels)); +}); +test('explicit local resubmission replaces missing account copy and verifies clean recovery',async()=>{ + const {f,state,key,uploads}=fixture(); + assert.equal(await f.run(true),'verified'); + assert.equal(uploads(),1); + assert.deepEqual(state.unavailableVaultIds,[]); + const recovered=await prepareRecoveryImage(key,f.app.id,state.appImage,state.allVaultImages,state.labels); + assert.deepEqual(clone(recovered.vaults.shared),f.local.Vault[0]); + assert.deepEqual(f.calls,['getBackupSnapshot','repairAppBackup','getBackupSnapshot']); +}); +test('missing server-only vault cannot look matched; explicit current-state replacement removes it',async()=>{ + const {f,state,uploads}=fixture(); f.local.Vault.length=0; + assert.equal(await f.run(),'different'); assert.equal(uploads(),0); + assert.equal(await f.run(true),'verified'); + assert.deepEqual(state.appImage.vaults,[]); assert.equal(uploads(),1); +}); +for(const problem of ['identity','ahead address counter']) test(`available ${problem} conflict still blocks missing-copy repair`,async()=>{ + const {f,state,key,uploads}=fixture(); + f.local.Wallet.push(wallet('other')); const remote=wallet('other'); + if(problem==='identity') remote.specs.xpub='another-key'; + else remote.specs.nextFreeAddressIndex=2; + state.appImage.wallets.other=encryption.encrypt(key,JSON.stringify(remote)); + assert.equal(await f.run(true),'conflict'); assert.equal(uploads(),0); +}); +for(const unavailable of [['not-referenced'],['shared','shared'],[null],'shared']) test(`malformed missing-copy metadata cannot authorize an upload: ${JSON.stringify(unavailable)}`,async()=>{ + const {f,uploads}=fixture({unavailable}); + assert.equal(await f.run(true),'unverified'); assert.equal(uploads(),0); +}); +test('an acknowledged upload that still has unavailable vaults is never verified',async()=>{ + const {f,uploads}=fixture({leaveUnavailable:true}); + assert.equal(await f.run(true),'unverified'); assert.equal(uploads(),1); +}); +test('archive metadata mismatch is read-only until explicit backup and must match after readback',async()=>{ + const {f,state,key,uploads}=fixture({unavailable:[]}); + f.local.Vault[0].archived=true; + state.allVaultImages=[{vaultId:'shared',vault:encryption.encrypt(key,JSON.stringify(f.local.Vault[0])),isArchived:false}]; + assert.equal(await f.run(),'different'); assert.equal(uploads(),0); + assert.equal(await f.run(true),'verified'); assert.equal(uploads(),1); + assert.equal(state.allVaultImages[0].isArchived,true); +}); + +for(const archived of [true,false]) test(`existing-wallet update carries signer maps and archive state: archived=${archived}`,async()=>{ + const {fixture:sagaFixture,enums,signer}=require('./sagaHarness.cjs'); + const f=sagaFixture(); + const device=signer('AABBCCDD',enums.NetworkType.MAINNET,enums.SignerType.LEDGER,0); + device.id=f.scope.getKeyUID(device); + f.collections.Signer.push(device); + const key={masterFingerprint:device.masterFingerprint,...device.signerXpubs[enums.XpubTypes.P2WPKH][0]}; + const vault={id:'reinstated',networkType:enums.NetworkType.MAINNET,signers:[key],scheme:{m:1,n:1},archived}; + let uploaded; + f.scope.Relay.updateVaultImage=async payload=>{uploaded=payload;return {updated:true};}; + await f.run('updateVaultImageWorker',{payload:{vault,isUpdate:true}}); + assert.equal(uploaded.appId,f.app.id); + assert.equal(uploaded.isArchived,archived); + assert.equal(uploaded.signersData.length,1); + assert.equal(uploaded.signersData[0].signerId,f.scope.getKeyUID(key)); + assert.equal(uploaded.signersData[0].xfpHash,encryption.hash256(key.masterFingerprint)); + const restored=JSON.parse(encryption.decrypt(encryption.generateEncryptionKey(f.app.primarySeed),uploaded.vault)); + assert.equal(restored.archived,uploaded.isArchived); + assert.equal(restored.signers[0].masterFingerprint,key.masterFingerprint); +}); diff --git a/tests/recovery/signerPersistence.test.cjs b/tests/recovery/signerPersistence.test.cjs new file mode 100644 index 0000000000..6924c4b385 --- /dev/null +++ b/tests/recovery/signerPersistence.test.cjs @@ -0,0 +1,37 @@ +const assert = require('node:assert/strict'); +const { test } = require('node:test'); +const { loadModule, image, makeImage } = require('./helpers.cjs'); +const { RealmSchema } = loadModule('src/storage/realm/enum.ts'); +const XpubTypes = Object.fromEntries(['AMF', 'P2PKH', 'P2SH-P2WPKH', 'P2SH-P2WSH', 'P2TR', 'P2WPKH', 'P2WSH'].map(type => [type, type])); +const schemas = loadModule('src/storage/realm/schema/vault.ts', { + '../enum': { RealmSchema }, 'src/services/wallets/enums': { XpubTypes }, +}); +// Use the real storage schema defaults without requiring a native Realm binary +// in CI. The native validation additionally checks an actual Realm round trip. +const emptyScriptLists = Object.fromEntries(Object.keys(schemas.SignerXpubsSchema.properties).map(type => [type, []])); +const signer = { masterFingerprint: '12345678', type: 'MOBILE_KEY', networkType: 'TESTNET', signerXpubs: { + P2WSH: [{ xpub: 'disposable-xpub', derivationPath: "m/48'/1'/0'/2'" }], +}}; +function pair() { + const local = makeImage(), remote = makeImage(); + remote.signers.key = structuredClone(signer); + local.signers.key = { ...structuredClone(signer), signerXpubs: { ...emptyScriptLists, P2WSH: [{ ...signer.signerXpubs.P2WSH[0], xpriv: null }] } }; + return { local, remote }; +} +test('Realm empty script lists and absent optional private key do not block backup repair', async () => { + const { local, remote } = pair(); + assert.equal(await image.compareImages(local, remote), 'different'); + assert.equal(await image.compareImages(local, structuredClone(local)), 'matched'); +}); +for (const field of ['xpub', 'derivationPath', 'xpriv']) { + test(`a changed signer ${field} remains a conflict`, async () => { + const { local, remote } = pair(); + local.signers.key.signerXpubs.P2WSH[0][field] = 'different-key-material'; + assert.equal(await image.compareImages(local, remote), 'conflict'); + }); +} +test('a populated script group must not be discarded as a Realm default', async () => { + const { local, remote } = pair(); + local.signers.key.signerXpubs.P2TR = [{ xpub: 'another-xpub', derivationPath: "m/86'/1'/0'" }]; + assert.equal(await image.compareImages(local, remote), 'conflict'); +}); diff --git a/tests/recovery/vaultPersistence.test.cjs b/tests/recovery/vaultPersistence.test.cjs new file mode 100644 index 0000000000..e0956f7529 --- /dev/null +++ b/tests/recovery/vaultPersistence.test.cjs @@ -0,0 +1,49 @@ +const assert = require('node:assert/strict'); +const { test } = require('node:test'); +const { image, loadModule, makeImage } = require('./helpers.cjs'); +const { RealmSchema } = loadModule('src/storage/realm/enum.ts'); +const schemas = loadModule('src/storage/realm/schema/vault.ts', { + '../enum': { RealmSchema }, 'src/services/wallets/enums': { XpubTypes: {} }, +}); +function pair() { + assert.equal(schemas.MiniscriptKeyInfoSchema.properties.uniqueKeyIdentifier, 'string?'); + assert.equal(schemas.MiniscriptPhaseSchema.properties.probability, undefined); + const key = { identifier: 'K1', descriptor: 'disposable-descriptor' }; + const remote = makeImage(), local = makeImage(); + remote.vaults.vault = { id: 'vault', networkType: 'TESTNET', scriptType: 'P2WSH', signers: [], + scheme: { m: 1, n: 1, miniscriptScheme: { + usedMiniscriptTypes: ['TIMELOCKED'], miniscriptPolicy: 'and(after(155000),pk(K1))', + miniscript: 'and_v(v:pk(K1),after(155000))', + miniscriptElements: { keysInfo: [key], phases: [{ id: 1, timelock: 155000, + probability: 99, requiredPaths: 1, paths: [{ id: 1, threshold: 1, keys: [{ ...key, uniqueKeyIdentifier: 'K1<0;1>' }] }] }] }, + } }, specs: {} }; + local.vaults.vault = structuredClone(remote.vaults.vault); + const elements = local.vaults.vault.scheme.miniscriptScheme.miniscriptElements; + elements.keysInfo[0].uniqueKeyIdentifier = null; + delete elements.phases[0].probability; + return { local, remote }; +} +test('new enhanced vault remains repairable after Realm normalizes policy metadata', async () => { + const { local, remote } = pair(); + assert.equal(await image.compareImages(local, remote), 'different'); + assert.equal(await image.compareImages(local, structuredClone(local)), 'matched'); +}); +for (const [name, mutate] of [ + ['primary timelock', s => { s.miniscriptElements.phases[0].timelock = 0; }], + ['key descriptor', s => { s.miniscriptElements.keysInfo[0].descriptor = 'changed'; }], + ['path key identifier', s => { s.miniscriptElements.phases[0].paths[0].keys[0].uniqueKeyIdentifier = 'K2<0;1>'; }], + ['compiled policy', s => { s.miniscriptPolicy = 'pk(K1)'; }], + ['compiled script', s => { s.miniscript = 'pk(K1)'; }], +]) { + test(`changed ${name} still blocks automatic replacement`, async () => { + const { local, remote } = pair(); + mutate(local.vaults.vault.scheme.miniscriptScheme); + assert.equal(await image.compareImages(local, remote), 'conflict'); + }); +} +test('phase weights remain significant when no compiled policy is available', async () => { + const { local, remote } = pair(); + delete local.vaults.vault.scheme.miniscriptScheme.miniscriptPolicy; + delete remote.vaults.vault.scheme.miniscriptScheme.miniscriptPolicy; + assert.equal(await image.compareImages(local, remote), 'conflict'); +}); diff --git a/tests/services/addReserveTimelockBoundary.test.ts b/tests/services/addReserveTimelockBoundary.test.ts new file mode 100644 index 0000000000..27baf07150 --- /dev/null +++ b/tests/services/addReserveTimelockBoundary.test.ts @@ -0,0 +1,561 @@ +import fs from 'fs'; +import path from 'path'; +import vm from 'vm'; +import ts from 'typescript'; +import idx from 'idx'; +import { resolveInitialTimelock } from 'src/services/wallets/operations/miniscript/initialTimelock'; +import { + MiniscriptTypes, + MultisigScriptType, + NetworkType, + VaultType, +} from 'src/services/wallets/enums'; +import * as durations from 'src/screens/Vault/constants'; +import { + ENHANCED_VAULT_TIMELOCKS_BLOCK_HEIGHT_MAINNET, + ENHANCED_VAULT_TIMELOCKS_BLOCK_HEIGHT_TESTNET, + ENHANCED_VAULT_TIMELOCKS_TIMESTAMP_MAINNET, + ENHANCED_VAULT_TIMELOCKS_TIMESTAMP_TESTNET, + INHERITANCE_KEY_IDENTIFIER, + EMERGENCY_KEY_IDENTIFIER, + generateEnhancedVaultElements, +} from 'src/services/wallets/operations/miniscript/default/EnhancedVault'; +import { generateMiniscriptPolicy } from 'src/services/wallets/operations/miniscript/policy-generator'; +import { generateMiniscript } from 'src/services/wallets/operations/miniscript/miniscript'; + +jest.mock('src/utils/utilities', () => ({ getKeyUID: (s) => s.masterFingerprint })); +jest.mock('src/utils/service-utilities/utils', () => ({ + getDerivationPath: (p) => p.replace('m/', ''), +})); + +// Evaluate the actual TSX expressions and controller/factory functions. This +// exercises the screen boundary without rendering native UI or using wallet data. +function readSource(relativePath: string) { + return ts.createSourceFile( + relativePath, + fs.readFileSync(path.join(process.cwd(), relativePath), 'utf8'), + ts.ScriptTarget.Latest, + true, + ts.ScriptKind.TSX + ); +} + +function variableExpression(source: ts.SourceFile, name: string) { + let expression: ts.Expression; + function visit(node: ts.Node) { + if (ts.isVariableDeclaration(node) && node.name.getText(source) === name) { + expression = node.initializer; + } + ts.forEachChild(node, visit); + } + visit(source); + if (!expression) throw new Error(`Missing production variable ${name}`); + return expression; +} + +function execute(expression: ts.Expression, source: ts.SourceFile, scope: any) { + vm.runInNewContext( + ts.transpileModule(`result = (${expression.getText(source)});`, { + fileName: 'screen-boundary.tsx', + compilerOptions: { target: ts.ScriptTarget.ES2019, jsx: ts.JsxEmit.React }, + }).outputText, + scope + ); + return scope.result; +} + +function migrationProps(source: ts.SourceFile, scope: any) { + let component: ts.JsxOpeningLikeElement; + function visit(node: ts.Node) { + if ( + (ts.isJsxOpeningElement(node) || ts.isJsxSelfClosingElement(node)) && + node.tagName.getText(source) === 'VaultMigrationController' + ) { + component = node; + } + ts.forEachChild(node, visit); + } + visit(source); + if (!component) throw new Error('Missing production VaultMigrationController'); + const props: any = {}; + for (const attribute of component.attributes.properties) { + if (!ts.isJsxAttribute(attribute)) continue; + const name = attribute.name.getText(source); + if ( + !['miniscriptTypes', 'initialTimelockDuration', 'inheritanceKeys', 'emergencyKeys'].includes( + name + ) + ) + continue; + if (!attribute.initializer || !ts.isJsxExpression(attribute.initializer)) continue; + props[name] = execute(attribute.initializer.expression, source, scope); + } + return props; +} + +function proceedCallback(source: ts.SourceFile, scope: any) { + let expression: ts.Expression; + function visit(node: ts.Node) { + if (ts.isJsxAttribute(node) && node.name.getText(source) === 'primaryCallback') { + if (node.initializer && ts.isJsxExpression(node.initializer)) { + expression = node.initializer.expression; + } + } + ts.forEachChild(node, visit); + } + visit(source); + if (!expression) throw new Error('Missing production primaryCallback'); + return execute(expression, source, scope); +} + +const reserveSource = readSource('src/screens/Vault/AddReserveKey.tsx'); +const emergencySource = readSource('src/screens/Vault/AddEmergencyKey.tsx'); +const controllerSource = readSource('src/screens/Vault/VaultMigrationController.tsx'); +const factorySource = readSource('src/services/wallets/factories/VaultFactory.ts'); +const mk = (masterFingerprint: string) => ({ + masterFingerprint, + derivationPath: "m/48'/0'/0'/2'", + xpub: `xpub${masterFingerprint}`, + account: 0, +}); + +function screenScope(hasInitialTimelock: boolean, initialTimelockDuration: any) { + return { + MiniscriptTypes, + hasInitialTimelock, + initialTimelockDuration, + selectedSigner: [mk('DDDDDDDD')], + selectedOption: { label: durations.MONTHS_12 }, + inheritanceKeys: [{ key: mk('DDDDDDDD'), duration: durations.MONTHS_12 }], + }; +} + +function controllerFixture(network: NetworkType, blockHeight: boolean) { + const base = blockHeight ? 950000 : 1767225600; + const scope: any = { + ...durations, + resolveInitialTimelock, + MiniscriptTypes, + VaultType, + NetworkType, + MultisigScriptType, + ENHANCED_VAULT_TIMELOCKS_BLOCK_HEIGHT_MAINNET, + ENHANCED_VAULT_TIMELOCKS_BLOCK_HEIGHT_TESTNET, + ENHANCED_VAULT_TIMELOCKS_TIMESTAMP_MAINNET, + ENHANCED_VAULT_TIMELOCKS_TIMESTAMP_TESTNET, + generateEnhancedVaultElements: jest.fn(generateEnhancedVaultElements), + generateMiniscriptPolicy, + generateMiniscript, + bitcoinNetworkType: network, + currentBlockHeight: base, + activeVault: { + scheme: { miniscriptScheme: { miniscriptElements: { timelocks: [base] } } }, + }, + Date: { now: () => base * 1000 }, + WalletUtilities: { + fetchCurrentBlockHeight: jest.fn().mockRejectedValue(new Error('Disposable offline fixture')), + }, + idx, + getKeyUID: (key) => key.masterFingerprint, + showToast: jest.fn(), + ErrorText: { invalidDuration: 'Invalid duration' }, + React: { createElement: () => null }, + ToastErrorIcon: () => null, + }; + for (const name of ['isVaultUsingBlockHeightTimelock', 'generateMiniscriptScheme']) { + scope[name] = execute(variableExpression(factorySource, name), factorySource, scope); + } + for (const name of ['getCurrentTimeForLock', 'getTimelockDuration', 'prepareMiniscriptScheme']) { + scope[name] = execute(variableExpression(controllerSource, name), controllerSource, scope); + } + const table = blockHeight + ? network === NetworkType.MAINNET + ? ENHANCED_VAULT_TIMELOCKS_BLOCK_HEIGHT_MAINNET + : ENHANCED_VAULT_TIMELOCKS_BLOCK_HEIGHT_TESTNET + : network === NetworkType.MAINNET + ? ENHANCED_VAULT_TIMELOCKS_TIMESTAMP_MAINNET + : ENHANCED_VAULT_TIMELOCKS_TIMESTAMP_TESTNET; + const info: any = { + vaultType: VaultType.MINISCRIPT, + vaultScheme: { m: 2, n: 3 }, + vaultSigners: [mk('AAAAAAAA'), mk('BBBBBBBB'), mk('CCCCCCCC')], + }; + return { scope, info, base, table }; +} + +function migrationCaller(scope: any, info: any, props: any) { + scope.activeVault.id = 'disposable'; + Object.assign(scope, { + vaultType: VaultType.MINISCRIPT, + scheme: info.vaultScheme, + vaultKeys: info.vaultSigners, + name: 'Disposable fixture', + description: '', + miniscriptTypes: props.miniscriptTypes, + inheritanceKeys: props.inheritanceKeys, + emergencyKeys: props.emergencyKeys, + initialTimelockDuration: props.initialTimelockDuration, + allVaults: [], + archivedVaults: [], + vaultAlreadyExists: jest.fn(() => false), + dispatch: jest.fn(), + migrateVault: jest.fn((newVaultInfo, vaultId) => ({ + type: 'DISPOSABLE_MIGRATION_FIXTURE', + newVaultInfo, + vaultId, + })), + addNewVault: jest.fn(), + generateVaultId: jest.fn(), + setGeneratedVaultId: jest.fn(), + setSavedGeneratedVaultId: jest.fn(), + setCreating: jest.fn(), + captureError: jest.fn(), + }); + return execute(variableExpression(controllerSource, 'initiateNewVault'), controllerSource, scope); +} + +const invalidHeights = [null, undefined, 0, -1, NaN, 950000.5, '950000', 500000000]; +const chainDataError = + 'Failed to fetch current chain data, please check your connection and try again'; + +describe('Actual block-height replacement refuses missing or invalid chain data', () => { + test.each(invalidHeights)( + 'preparation rejects invalid supplied and fetched height %s before policy construction', + async (height) => { + const props = migrationProps(reserveSource, screenScope(true, durations.MONTHS_6)); + const { scope, info } = controllerFixture(NetworkType.MAINNET, true); + scope.currentBlockHeight = height; + scope.WalletUtilities.fetchCurrentBlockHeight.mockResolvedValue({ + currentBlockHeight: height, + }); + await expect( + scope.prepareMiniscriptScheme( + info, + props.miniscriptTypes, + props.inheritanceKeys, + props.emergencyKeys, + props.initialTimelockDuration + ) + ).rejects.toThrow(chainDataError); + expect(scope.WalletUtilities.fetchCurrentBlockHeight).toHaveBeenCalledTimes(1); + expect(scope.generateEnhancedVaultElements).not.toHaveBeenCalled(); + expect(info).not.toHaveProperty('miniscriptElements'); + expect(info.vaultScheme).toEqual({ m: 2, n: 3 }); + } + ); + + test.each(invalidHeights)( + 'actual migration caller does not dispatch for current height %s', + async (height) => { + const props = migrationProps(reserveSource, screenScope(true, durations.MONTHS_6)); + const { scope, info } = controllerFixture(NetworkType.MAINNET, true); + scope.currentBlockHeight = height; + const initiate = migrationCaller(scope, info, props); + await initiate(); + expect(scope.WalletUtilities.fetchCurrentBlockHeight).toHaveBeenCalledTimes(1); + expect(scope.dispatch).not.toHaveBeenCalled(); + expect(scope.migrateVault).not.toHaveBeenCalled(); + expect(scope.addNewVault).not.toHaveBeenCalled(); + expect(scope.vaultAlreadyExists).not.toHaveBeenCalled(); + expect(scope.generateEnhancedVaultElements).not.toHaveBeenCalled(); + expect(scope.scheme).toEqual({ m: 2, n: 3 }); + expect(scope.setCreating).toHaveBeenCalledWith(false); + expect(scope.showToast).toHaveBeenCalledWith( + `Failed to prepare enhanced vault: ${chainDataError}`, + null + ); + expect(scope.captureError).toHaveBeenCalledTimes(1); + } + ); + + test.each(invalidHeights)( + 'timestamp wallet timing is unaffected by irrelevant height %s', + async (height) => { + const props = migrationProps(reserveSource, screenScope(true, durations.MONTHS_6)); + const { scope, info, base, table } = controllerFixture(NetworkType.MAINNET, false); + scope.currentBlockHeight = height; + await scope.prepareMiniscriptScheme( + info, + props.miniscriptTypes, + props.inheritanceKeys, + props.emergencyKeys, + props.initialTimelockDuration + ); + expect( + info.vaultScheme.miniscriptScheme.miniscriptElements.phases.map((phase) => phase.timelock) + ).toEqual([base + table.MONTHS_6, base + table.MONTHS_6 + table.MONTHS_12]); + expect(scope.WalletUtilities.fetchCurrentBlockHeight).not.toHaveBeenCalled(); + } + ); + + test('the same migration caller recovers after reconnecting and uses one fetched phase anchor', async () => { + const props = migrationProps(emergencySource, { + ...screenScope(true, durations.MONTHS_6), + selectedSigner: [mk('EEEEEEEE')], + selectedOption: { label: durations.MONTHS_36 }, + }); + const { scope, info, table } = controllerFixture(NetworkType.MAINNET, true); + const fetchedHeight = 975000; + scope.currentBlockHeight = null; + const fetchHeight = scope.WalletUtilities.fetchCurrentBlockHeight; + fetchHeight + .mockRejectedValueOnce(new Error('Disposable offline fixture')) + .mockResolvedValueOnce({ currentBlockHeight: fetchedHeight }); + const initiate = migrationCaller(scope, info, props); + + await initiate(); + expect(scope.dispatch).not.toHaveBeenCalled(); + expect(scope.generateEnhancedVaultElements).not.toHaveBeenCalled(); + expect(scope.scheme).toEqual({ m: 2, n: 3 }); + expect(scope.showToast).toHaveBeenCalledWith( + `Failed to prepare enhanced vault: ${chainDataError}`, + null + ); + expect(scope.setCreating).toHaveBeenCalledWith(false); + + // Repeat the same closure; no screen remount or prop/state change repairs it. + await initiate(); + expect(fetchHeight).toHaveBeenCalledTimes(2); + expect(scope.currentBlockHeight).toBeNull(); + expect(scope.generateEnhancedVaultElements).toHaveBeenCalledTimes(1); + expect(scope.migrateVault).toHaveBeenCalledTimes(1); + expect(scope.dispatch).toHaveBeenCalledTimes(1); + expect(scope.addNewVault).not.toHaveBeenCalled(); + const [replacement, vaultId] = scope.migrateVault.mock.calls[0]; + expect(vaultId).toBe('disposable'); + expect(scope.dispatch).toHaveBeenCalledWith({ + type: 'DISPOSABLE_MIGRATION_FIXTURE', + newVaultInfo: replacement, + vaultId, + }); + const expected = [ + fetchedHeight + table.MONTHS_6, + fetchedHeight + table.MONTHS_6 + table.MONTHS_12, + fetchedHeight + table.MONTHS_6 + table.MONTHS_36, + ]; + const replacementScheme = replacement.vaultScheme.miniscriptScheme; + expect(replacementScheme.miniscriptElements.phases.map((phase) => phase.timelock)).toEqual( + expected + ); + for (const timelock of expected) + expect(replacementScheme.miniscriptPolicy).toContain(`after(${timelock})`); + expect(replacementScheme.usedMiniscriptTypes).toEqual([ + MiniscriptTypes.TIMELOCKED, + MiniscriptTypes.INHERITANCE, + MiniscriptTypes.EMERGENCY, + ]); + expect(scope.captureError).toHaveBeenCalledTimes(1); + }); +}); + +describe('AddReserveKey production migration boundary', () => { + test.each([undefined, null, '', 0, NaN, -1, 'invalid'])( + 'retains required TIMELOCKED and rejects missing/invalid duration %s', + async (duration) => { + const props = migrationProps(reserveSource, screenScope(true, duration)); + expect(props.miniscriptTypes).toContain(MiniscriptTypes.TIMELOCKED); + const { scope, info } = controllerFixture(NetworkType.MAINNET, false); + await expect( + scope.prepareMiniscriptScheme( + info, + props.miniscriptTypes, + props.inheritanceKeys, + props.emergencyKeys, + props.initialTimelockDuration + ) + ).rejects.toThrow('initial timelock duration'); + expect(info).not.toHaveProperty('miniscriptElements'); + expect(info.vaultScheme).not.toHaveProperty('miniscriptScheme'); + } + ); + + test.each([ + ['mainnet timestamp', NetworkType.MAINNET, false], + ['testnet timestamp', NetworkType.TESTNET, false], + ['mainnet height', NetworkType.MAINNET, true], + ['testnet height', NetworkType.TESTNET, true], + ])( + 'preserves primary and inheritance timing through actual policy generation (%s)', + async (_name, network: NetworkType, blockHeight: boolean) => { + const props = migrationProps(reserveSource, screenScope(true, durations.MONTHS_6)); + const { scope, info, base, table } = controllerFixture(network, blockHeight); + await scope.prepareMiniscriptScheme( + info, + props.miniscriptTypes, + props.inheritanceKeys, + props.emergencyKeys, + props.initialTimelockDuration + ); + const scheme = info.vaultScheme.miniscriptScheme; + const expected = [base + table.MONTHS_6, base + table.MONTHS_6 + table.MONTHS_12]; + expect(scheme.miniscriptElements.phases.map((phase) => phase.timelock)).toEqual(expected); + for (const timelock of expected) + expect(scheme.miniscriptPolicy).toContain(`after(${timelock})`); + expect(scheme.usedMiniscriptTypes).toContain(MiniscriptTypes.TIMELOCKED); + } + ); + + test('preserves primary, inheritance and emergency timing across both add-key screens', async () => { + const navigate = jest.fn(); + proceedCallback(reserveSource, { + ...screenScope(true, durations.MONTHS_6), + navigation: { navigate }, + isAddEmergencyKey: true, + isAddInheritanceKey: true, + vaultKeys: [mk('AAAAAAAA'), mk('BBBBBBBB'), mk('CCCCCCCC')], + vaultId: 'disposable', + scheme: { m: 2, n: 3 }, + name: 'Disposable fixture', + description: '', + currentBlockHeight: 950000, + route: { params: { selectedSigners: [] } }, + keyToRotate: mk('FFFFFFFF'), + setCreating: jest.fn(), + VaultType, + })(); + expect(navigate.mock.calls[0][0]).toBe('AddEmergencyKey'); + const nextParams = navigate.mock.calls[0][1]; + expect(nextParams.hasInitialTimelock).toBe(true); + expect(nextParams.initialTimelockDuration).toBe(durations.MONTHS_6); + const props = migrationProps(emergencySource, { + ...nextParams, + MiniscriptTypes, + selectedSigner: [mk('EEEEEEEE')], + selectedOption: { label: durations.MONTHS_36 }, + }); + const { scope, info, base, table } = controllerFixture(NetworkType.MAINNET, false); + await scope.prepareMiniscriptScheme( + info, + props.miniscriptTypes, + props.inheritanceKeys, + props.emergencyKeys, + props.initialTimelockDuration + ); + const scheme = info.vaultScheme.miniscriptScheme; + const expected = [ + base + table.MONTHS_6, + base + table.MONTHS_6 + table.MONTHS_12, + base + table.MONTHS_6 + table.MONTHS_36, + ]; + expect(scheme.miniscriptElements.phases.map((phase) => phase.timelock)).toEqual(expected); + for (const timelock of expected) + expect(scheme.miniscriptPolicy).toContain(`after(${timelock})`); + }); + + test.each([undefined, null, '', 0])( + 'accepts a legitimate no-initial-lock wallet (%s)', + async (duration) => { + const props = migrationProps(reserveSource, screenScope(false, duration)); + expect(props.miniscriptTypes).not.toContain(MiniscriptTypes.TIMELOCKED); + const { scope, info, base, table } = controllerFixture(NetworkType.MAINNET, false); + await scope.prepareMiniscriptScheme( + info, + props.miniscriptTypes, + props.inheritanceKeys, + props.emergencyKeys, + props.initialTimelockDuration + ); + expect( + info.vaultScheme.miniscriptScheme.miniscriptElements.phases.map((phase) => phase.timelock) + ).toEqual([0, base + table.MONTHS_12]); + } + ); +}); + +describe('Add-key screens forward initial duration to AddSigningDevice', () => { + test.each([ + ['AddReserveKey', reserveSource], + ['AddEmergencyKey', emergencySource], + ])( + '%s passes the independent flag and exact selected duration', + (_name, source: ts.SourceFile) => { + const push = jest.fn(); + const scope: any = { + ...screenScope(true, durations.MONTHS_6), + navigation: { push }, + ADDRESERVEKEY: 'AddReserveKey', + ADDEMERGENCYKEY: 'AddEmergencyKey', + vaultKeys: [mk('AAAAAAAA'), mk('BBBBBBBB'), mk('CCCCCCCC')], + route: { params: { selectedSigners: [] } }, + scheme: { m: 2, n: 3 }, + isAddInheritanceKey: true, + isAddEmergencyKey: true, + currentBlockHeight: 950000, + setSelectedSigner: jest.fn(), + }; + const useCallback = variableExpression(source, 'userKeyCallback'); + if (!ts.isCallExpression(useCallback)) throw new Error('Expected production useCallback'); + const callback = execute(useCallback.arguments[0], source, scope); + callback(); + expect(push).toHaveBeenCalledTimes(1); + expect(push.mock.calls[0][0]).toBe('AddSigningDevice'); + expect(push.mock.calls[0][1].hasInitialTimelock).toBe(true); + expect(push.mock.calls[0][1].initialTimelockDuration).toBe(durations.MONTHS_6); + } + ); +}); + +describe('Add-key screens preserve fallback roles while replacing a primary key', () => { + test.each([ + ['AddReserveKey', reserveSource, 'DDDDDDDD'], + ['AddEmergencyKey', emergencySource, 'EEEEEEEE'], + ])( + '%s preloads the existing fallback for a primary rotation', + (_name, source: ts.SourceFile, fingerprint: string) => { + const setSelectedSigner = jest.fn(); + const scope: any = { + selectedSigner: null, + keyToRotate: mk('AAAAAAAA'), + activeVault: { + id: 'disposable', + signers: ['AAAAAAAA', 'BBBBBBBB', 'CCCCCCCC', 'DDDDDDDD', 'EEEEEEEE'].map(mk), + scheme: { + miniscriptScheme: { + miniscriptElements: { + signerFingerprints: { + K1: 'AAAAAAAA', + K2: 'BBBBBBBB', + K3: 'CCCCCCCC', + IK1: 'DDDDDDDD', + EK1: 'EEEEEEEE', + }, + }, + }, + }, + }, + INHERITANCE_KEY_IDENTIFIER, + EMERGENCY_KEY_IDENTIFIER, + getKeyUID: (key) => key.masterFingerprint, + setSelectedSigner, + }; + let expression: ts.Expression; + function visit(node: ts.Node) { + if ( + ts.isCallExpression(node) && + node.expression.getText(source) === 'useEffect' && + node.arguments[0].getText(source).includes('setSelectedSigner(') + ) { + expression = node.arguments[0]; + } + ts.forEachChild(node, visit); + } + visit(source); + if (!expression) throw new Error('Missing production fallback preload effect'); + const effect = execute(expression, source, scope); + effect(); + expect(setSelectedSigner).toHaveBeenCalledWith([mk(fingerprint)]); + + // Replacing the fallback itself must still require choosing its replacement. + setSelectedSigner.mockClear(); + scope.keyToRotate = mk(fingerprint); + effect(); + expect(setSelectedSigner).not.toHaveBeenCalled(); + + // Re-rendering must not overwrite an explicit user selection. + scope.keyToRotate = mk('AAAAAAAA'); + scope.selectedSigner = [mk('FFFFFFFF')]; + effect(); + expect(setSelectedSigner).not.toHaveBeenCalled(); + } + ); +}); diff --git a/tests/services/addSigningDeviceTimelock.test.ts b/tests/services/addSigningDeviceTimelock.test.ts new file mode 100644 index 0000000000..dc658c1f18 --- /dev/null +++ b/tests/services/addSigningDeviceTimelock.test.ts @@ -0,0 +1,450 @@ +import fs from 'fs'; +import path from 'path'; +import vm from 'vm'; +import ts from 'typescript'; +import { MiniscriptTypes, VaultType, XpubTypes } from 'src/services/wallets/enums'; +import { resolveInitialTimelock } from 'src/services/wallets/operations/miniscript/initialTimelock'; + +// Run the production screen's route bindings, controller props and button callback. +// Native rendering, Realm persistence and funded migration remain separate acceptance work. +function sourceFile(name: string, override?: string) { + return ts.createSourceFile( + name, + fs.readFileSync(override || path.join(process.cwd(), `src/screens/Vault/${name}`), 'utf8'), + ts.ScriptTarget.Latest, + true, + ts.ScriptKind.TSX + ); +} + +const signingSource = sourceFile('AddSigningDevice.tsx', process.env.R02_ADD_SIGNING_DEVICE_SOURCE); +const detailsSource = sourceFile( + 'SigningDeviceDetails.tsx', + process.env.R02_SIGNING_DEVICE_DETAILS_SOURCE +); + +function findNode(source: ts.SourceFile, predicate: (node: ts.Node) => boolean): ts.Node { + let result: ts.Node; + function visit(node: ts.Node) { + if (!result && predicate(node)) result = node; + if (!result) ts.forEachChild(node, visit); + } + visit(source); + return result; +} + +function execute(code: string, scope: any = {}) { + vm.runInNewContext( + ts.transpileModule(code, { + fileName: 'screen-contract.tsx', + compilerOptions: { target: ts.ScriptTarget.ES2019, jsx: ts.JsxEmit.React }, + }).outputText, + scope + ); + return scope; +} + +function expressionValue(source: ts.SourceFile, node: ts.Expression, scope: any) { + expect(node).toBeDefined(); + return execute(`result = (${node.getText(source)});`, scope).result; +} + +function componentAttribute(component: string, attribute: string): ts.Expression { + const element = findNode( + signingSource, + (node) => + (ts.isJsxOpeningElement(node) || ts.isJsxSelfClosingElement(node)) && + node.tagName.getText(signingSource) === component + ) as ts.JsxOpeningElement | ts.JsxSelfClosingElement; + const prop = element.attributes.properties.find( + (node) => ts.isJsxAttribute(node) && node.name.getText(signingSource) === attribute + ) as ts.JsxAttribute; + expect(prop).toBeDefined(); + return (prop.initializer as ts.JsxExpression).expression; +} + +function requirementFromStoredWallet( + types: MiniscriptTypes[], + routeFlag?: boolean, + newScheme = false +) { + const declaration = findNode( + signingSource, + (node) => + ts.isVariableDeclaration(node) && node.name.getText(signingSource) === 'hasInitialTimelock' + ) as ts.VariableDeclaration; + expect(declaration).toBeDefined(); + return expressionValue(signingSource, declaration.initializer, { + isNewSchemeFlow: newScheme, + hasInitialTimelockParam: routeFlag, + activeVault: { scheme: { miniscriptScheme: { usedMiniscriptTypes: types } } }, + MiniscriptTypes, + }); +} + +function footerFixture(overrides: any = {}) { + const footer = findNode( + signingSource, + (node) => ts.isFunctionDeclaration(node) && node.name?.text === 'Footer' + ) as ts.FunctionDeclaration; + const navigation = { navigate: jest.fn(), dispatch: jest.fn(), goBack: jest.fn() }; + const setCreating = jest.fn(); + const scope: any = { + React: { createElement: (type, props, ...children) => ({ type, props, children }) }, + Box: 'Box', + Buttons: 'Buttons', + styles: { bottomContainer: {} }, + useNavigation: () => navigation, + useContext: () => ({ translations: { common: { proceed: 'Proceed', confirm: 'Confirm' } } }), + LocalizationContext: {}, + getKeyUID: (key) => key.id, + CommonActions: { navigate: (name, params) => ({ name, params }) }, + INHERITANCE_KEY_IDENTIFIER: 'IK', + EMERGENCY_KEY_IDENTIFIER: 'EK', + }; + execute(`${footer.getText(signingSource)}; result = Footer;`, scope); + const keyToRotate = { id: 'old-primary' }; + const props = { + areSignersValid: true, + colorMode: 'light', + setCreating, + isCollaborativeFlow: false, + isAssistedWalletFlow: false, + hasInitialTimelock: true, + initialTimelockDuration: undefined, + isReserveKeyFlow: false, + isEmergencyKeyFlow: false, + isAddInheritanceKey: false, + isAddEmergencyKey: false, + currentBlockHeight: 100000, + vaultKeys: [{ id: 'new-primary', masterFingerprint: 'AAAAAAAA' }], + selectedSigners: new Map([['new-primary', true]]), + signers: [{ id: 'new-primary' }], + name: 'Disposable fixture', + description: '', + vaultId: 'fixture-wallet', + scheme: { m: 1, n: 1 }, + vaultType: 'MINISCRIPT', + keyToRotate, + activeVault: { + scheme: { miniscriptScheme: { miniscriptElements: { signerFingerprints: {} } } }, + }, + ...overrides, + }; + const element = scope.result(props); + const button = element.children.find((child) => child?.type === 'Buttons'); + expect(button).toBeDefined(); + button.props.primaryCallback(); + return { navigation, setCreating, keyToRotate }; +} + +describe('AddSigningDevice initial timelock contract', () => { + test.each(['3 months', '6 months', '9 months', '12 months'])( + 'reads the selected duration %s from the real route binding', + (duration) => { + const binding = findNode( + signingSource, + (node) => + ts.isVariableDeclaration(node) && + ts.isObjectBindingPattern(node.name) && + node.initializer?.getText(signingSource) === 'route.params' + ) as ts.VariableDeclaration; + const scope = execute( + `const ${binding.getText( + signingSource + )}; result = typeof initialTimelockDuration === 'undefined' ? undefined : initialTimelockDuration;`, + { route: { params: { initialTimelockDuration: duration } } } + ); + expect(scope.result).toBe(duration); + } + ); + + test.each([undefined, false, true])( + 'retains the stored TIMELOCKED requirement when route flag is %s', + (routeFlag) => { + expect(requirementFromStoredWallet([MiniscriptTypes.TIMELOCKED], routeFlag)).toBe(true); + } + ); + + test('respects an explicit new-scheme choice to remove the initial lock', () => { + expect(requirementFromStoredWallet([MiniscriptTypes.TIMELOCKED], false, true)).toBe(false); + }); + + test.each(['VaultMigrationController', 'Footer'])( + 'passes the exact selected duration to %s', + (component) => { + const duration = expressionValue( + signingSource, + componentAttribute(component, 'initialTimelockDuration'), + { + initialTimelockDuration: '9 months', + } + ); + expect(duration).toBe('9 months'); + } + ); + + test.each([undefined, null, '', 0])( + 'keeps the controller requirement independent from invalid duration %s', + (initialTimelockDuration) => { + const hasInitialTimelock = requirementFromStoredWallet([MiniscriptTypes.TIMELOCKED]); + const types = expressionValue( + signingSource, + componentAttribute('VaultMigrationController', 'miniscriptTypes'), + { + hasInitialTimelock, + isAddInheritanceKey: true, + isAddEmergencyKey: true, + MiniscriptTypes, + } + ); + const duration = expressionValue( + signingSource, + componentAttribute('VaultMigrationController', 'initialTimelockDuration'), + { + initialTimelockDuration, + } + ); + expect(types).toContain(MiniscriptTypes.TIMELOCKED); + expect(() => resolveInitialTimelock(types, duration, () => 6)).toThrow('initial timelock'); + } + ); + + test('routes replacement without a duration through the existing timelock selection', () => { + const { navigation, setCreating, keyToRotate } = footerFixture(); + expect(setCreating).not.toHaveBeenCalled(); + expect(navigation.dispatch).toHaveBeenCalledWith( + expect.objectContaining({ + name: 'SelectInitialTimelock', + params: expect.objectContaining({ hasInitialTimelock: true, keyToRotate }), + }) + ); + }); + + test.each([ + ['AddReserveKey', { isAddInheritanceKey: true }], + ['AddEmergencyKey', { isAddEmergencyKey: true }], + ])('forwards an explicit duration and replacement identity to %s', (name, overrides) => { + const { navigation, keyToRotate } = footerFixture({ + initialTimelockDuration: '6 months', + ...overrides, + }); + expect(navigation.dispatch).toHaveBeenCalledWith( + expect.objectContaining({ + name, + params: expect.objectContaining({ + hasInitialTimelock: true, + initialTimelockDuration: '6 months', + keyToRotate, + }), + }) + ); + }); + + test('forwards an explicit duration to final confirmation for a new wallet', () => { + const { navigation } = footerFixture({ + initialTimelockDuration: '12 months', + keyToRotate: undefined, + }); + expect(navigation.navigate).toHaveBeenCalledWith( + 'ConfirmWalletDetails', + expect.objectContaining({ hasInitialTimelock: true, initialTimelockDuration: '12 months' }) + ); + }); + + test('uses an explicit duration for primary-key replacement without reselection', () => { + const { navigation, setCreating } = footerFixture({ initialTimelockDuration: '6 months' }); + expect(setCreating).toHaveBeenCalledWith(true); + expect(navigation.dispatch).not.toHaveBeenCalled(); + }); + + test('preserves ordinary untimelocked key replacement', () => { + const { navigation, setCreating } = footerFixture({ hasInitialTimelock: false }); + expect(setCreating).toHaveBeenCalledWith(true); + expect(navigation.dispatch).not.toHaveBeenCalled(); + }); +}); + +describe('Existing primary Change Key navigation', () => { + test('requires duration selection from stored metadata when the unchanged caller omits it', () => { + const action = findNode( + detailsSource, + (node) => + ts.isObjectLiteralExpression(node) && + node.properties.some( + (prop) => + ts.isPropertyAssignment(prop) && + prop.name.getText(detailsSource) === 'text' && + prop.initializer.getText(detailsSource) === 'vaultText.changeKey' + ) + ) as ts.ObjectLiteralExpression; + const callback = ( + action.properties.find( + (prop) => ts.isPropertyAssignment(prop) && prop.name.getText(detailsSource) === 'onPress' + ) as ts.PropertyAssignment + ).initializer; + const activeVault = { + presentationData: { name: 'Disposable fixture', description: '' }, + scheme: { miniscriptScheme: { usedMiniscriptTypes: [MiniscriptTypes.TIMELOCKED] } }, + }; + const vaultKey = { id: 'old-primary' }; + const entryNavigation = { dispatch: jest.fn() }; + const onPress = expressionValue(detailsSource, callback, { + activeVault, + vaultKey, + vaultId: 'fixture-wallet', + isInheritanceKey: false, + isEmergencyKey: false, + navigation: entryNavigation, + CommonActions: { navigate: (action) => action }, + }); + onPress(); + const entry = entryNavigation.dispatch.mock.calls[0][0]; + expect(entry).toMatchObject({ name: 'AddSigningDevice', params: { keyToRotate: vaultKey } }); + expect(entry.params.hasInitialTimelock).toBeUndefined(); + expect(entry.params.initialTimelockDuration).toBeUndefined(); + const hasInitialTimelock = requirementFromStoredWallet( + activeVault.scheme.miniscriptScheme.usedMiniscriptTypes, + entry.params.hasInitialTimelock + ); + expect(hasInitialTimelock).toBe(true); + const { navigation, setCreating } = footerFixture({ + ...entry.params, + hasInitialTimelock, + initialTimelockDuration: entry.params.initialTimelockDuration, + }); + expect(setCreating).not.toHaveBeenCalled(); + expect(navigation.dispatch).toHaveBeenCalledWith( + expect.objectContaining({ + name: 'SelectInitialTimelock', + params: expect.objectContaining({ hasInitialTimelock: true, keyToRotate: vaultKey }), + }) + ); + expect(navigation.dispatch.mock.calls[0][0].params.initialTimelockDuration).toBeUndefined(); + }); +}); + +describe('AddSigningDevice existing primary-key selection', () => { + function selectionFixture(type: VaultType, n = 3, keyToRotate?: string) { + const declaration = findNode( + signingSource, + (node) => + ts.isVariableDeclaration(node) && node.name.getText(signingSource) === 'setInitialKeys' + ) as ts.VariableDeclaration; + const keys = ['K1', 'K2', 'K3', 'IK1', 'EK1'].map((id) => ({ id, masterFingerprint: id })); + const signerMap = Object.fromEntries( + keys.map((key) => [ + key.id, + { + ...key, + signerXpubs: { + [XpubTypes.P2WSH]: [{}], + [XpubTypes.P2WPKH]: [{}], + [XpubTypes.AMF]: [{}], + }, + }, + ]) + ); + const scope = { + getKeyUID: (key) => key.id, + VaultType, + MiniscriptTypes, + XpubTypes, + isSignerValidForScheme: () => ({ isValid: true }), + WalletUtilities: { getKeyForScheme: (_isMultisig, signer) => ({ id: signer.id }) }, + }; + const setInitialKeys = expressionValue(signingSource, declaration.initializer, scope); + const setVaultKeys = jest.fn(); + const setSelectedSigners = jest.fn(); + const activeVault = { + type, + signers: keys, + scheme: { + miniscriptScheme: { + usedMiniscriptTypes: [ + MiniscriptTypes.TIMELOCKED, + MiniscriptTypes.INHERITANCE, + MiniscriptTypes.EMERGENCY, + ], + miniscriptElements: { + signerFingerprints: Object.fromEntries( + keys.map((key) => [key.id, key.masterFingerprint]) + ), + }, + }, + }, + }; + setInitialKeys( + activeVault, + type, + { m: 2, n }, + signerMap, + setVaultKeys, + setSelectedSigners, + new Map(), + keys.find((key) => key.id === keyToRotate) + ); + return { + vaultKeys: setVaultKeys.mock.calls[0][0], + selected: setSelectedSigners.mock.calls[0][0] as Map, + }; + } + + test('leaves the replacement slot free and excludes IK/EK from primary preselection', () => { + const { vaultKeys, selected } = selectionFixture(VaultType.MINISCRIPT, 3, 'K1'); + expect(vaultKeys).toEqual([{ id: 'K2' }, { id: 'K3' }]); + expect(Array.from(selected.keys())).toEqual(['K2', 'K3']); + expect(selected.size).toBeLessThan(3); + }); + + test('respects the chosen primary count when changing a scheme', () => { + const { vaultKeys, selected } = selectionFixture(VaultType.MINISCRIPT, 1, 'K1'); + expect(vaultKeys).toEqual([{ id: 'K2' }]); + expect(Array.from(selected.keys())).toEqual(['K2']); + }); + + test('preserves a fresh new-scheme selection without a rotation target', () => { + const { vaultKeys, selected } = selectionFixture(VaultType.MINISCRIPT); + expect(vaultKeys).toEqual([]); + expect(selected.size).toBe(0); + }); + + test('preserves ordinary wallet preselection', () => { + const { vaultKeys, selected } = selectionFixture(VaultType.DEFAULT, 3, 'K1'); + expect(vaultKeys).toEqual([{ id: 'K2' }, { id: 'K3' }, { id: 'IK1' }]); + expect(Array.from(selected.keys())).toEqual(['K2', 'K3', 'IK1']); + }); +}); + +describe('primary selection validation with retained fallback keys', () => { + const intelSource = ts.createSourceFile('useSignerIntel.tsx', fs.readFileSync( + path.join(process.cwd(), 'src/hooks/useSignerIntel.tsx'), 'utf8' + ), ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX); + const declaration = (name: string) => findNode(intelSource, node => + ts.isVariableDeclaration(node) && node.name.getText(intelSource) === name + ) as ts.VariableDeclaration; + function validate(n: number, selectedCount: number, types: MiniscriptTypes[]) { + const keys = Array.from({ length: selectedCount }, (_, i) => ({ id: `new-${i}`, xfp: `new-${i}` })); + const selectedSigners = new Map(keys.map(key => [key.id, true])); + const scope = execute(`const areSignersSame = ${declaration('areSignersSame').initializer.getText(intelSource)}; + const useSignerIntel = ${declaration('useSignerIntel').initializer.getText(intelSource)}; + result = useSignerIntel(input);`, { + useSignerMap: () => ({ signerMap: Object.fromEntries(keys.map(key => [key.id, { type: 'MY_KEEPER' }])) }), + usePlan: () => ({ plan: 'L1' }), SubscriptionTier: { L1: 'L1' }, + useContext: () => ({ translations: { signer: {} } }), LocalizationContext: {}, + isSignerAMF: () => false, getKeyUID: key => key.id, + SignerType: { POLICY_SERVER: 'POLICY_SERVER' }, getSignerNameFromType: () => 'Mobile Key', + input: { scheme: { m: 1, n, miniscriptScheme: { usedMiniscriptTypes: types } }, + vaultKeys: keys, selectedSigners, existingKeys: [{ xfp: 'old-primary' }, { xfp: 'retained-inheritance' }] }, + }); + return scope.result.areSignersValid; + } + test.each([ + [1, 1, true], [1, 0, false], [1, 2, false], [3, 3, true], [3, 2, false], [3, 4, false], + ])('requires exactly %i primary keys when %i are selected', (n, selected, valid) => { + expect(validate(n, selected, [MiniscriptTypes.TIMELOCKED, MiniscriptTypes.INHERITANCE])).toBe(valid); + }); + test('Emergency Key also does not increase the primary quorum count', () => { + expect(validate(1, 1, [MiniscriptTypes.TIMELOCKED, MiniscriptTypes.INHERITANCE, MiniscriptTypes.EMERGENCY])).toBe(true); + }); +}); From 5bd4dc6421658cd3dfb03900decfcfff4ff15e35 Mon Sep 17 00:00:00 2001 From: Anant Date: Tue, 6 Oct 2026 19:06:07 +0530 Subject: [PATCH 008/254] Use live Keeper channel and guard mobile release builds --- android/fastlane/Fastfile | 1 + ios/fastlane/Fastfile | 1 + scripts/verify-channel-config.cjs | 20 ++++++++++++++++++++ src/utils/service-utilities/config.ts | 2 +- 4 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 scripts/verify-channel-config.cjs diff --git a/android/fastlane/Fastfile b/android/fastlane/Fastfile index cfc909f0db..9ff016ca39 100644 --- a/android/fastlane/Fastfile +++ b/android/fastlane/Fastfile @@ -52,6 +52,7 @@ platform :android do sh("node", File.expand_path("../../scripts/release-version.cjs", __dir__), "release-check") production_env = File.expand_path("../../.env.production", __dir__) UI.user_error!("Missing reviewed .env.production") unless File.file?(production_env) + sh("node", File.expand_path("../../scripts/verify-channel-config.cjs", __dir__), production_env) ENV['ENVFILE'] = production_env store_password = ENV["STORE_PASSWORD"] key_password = ENV["KEY_PASSWORD"] diff --git a/ios/fastlane/Fastfile b/ios/fastlane/Fastfile index b231891c82..a3dfa9739b 100644 --- a/ios/fastlane/Fastfile +++ b/ios/fastlane/Fastfile @@ -107,6 +107,7 @@ platform :ios do sh("node", File.expand_path("../../scripts/release-version.cjs", __dir__), "release-check") production_env = File.expand_path("../../.env.production", __dir__) UI.user_error!("Missing reviewed .env.production") unless File.file?(production_env) + sh("node", File.expand_path("../../scripts/verify-channel-config.cjs", __dir__), production_env) ENV['ENVFILE'] = production_env get_certificates( # Create or get certificate, and install it diff --git a/scripts/verify-channel-config.cjs b/scripts/verify-channel-config.cjs new file mode 100644 index 0000000000..18ecc31734 --- /dev/null +++ b/scripts/verify-channel-config.cjs @@ -0,0 +1,20 @@ +const fs = require('fs'); + +const expected = 'https://channel.bitcoinkeeper.app/'; +const envFile = process.argv[2]; + +if (!envFile || !fs.existsSync(envFile)) { + throw new Error('Production environment file is missing'); +} + +const matches = fs.readFileSync(envFile, 'utf8') + .split(/\r?\n/) + .map(line => line.match(/^\s*(?:export\s+)?CHANNEL_URL\s*=\s*(.*?)\s*$/)) + .filter(Boolean) + .map(match => match[1].replace(/^['"]|['"]$/g, '')); + +if (matches.length !== 1 || matches[0] !== expected) { + throw new Error(`Production CHANNEL_URL must be ${expected}`); +} + +console.log('Production channel URL verified'); diff --git a/src/utils/service-utilities/config.ts b/src/utils/service-utilities/config.ts index 5344d501b9..999bbc3ae7 100644 --- a/src/utils/service-utilities/config.ts +++ b/src/utils/service-utilities/config.ts @@ -23,7 +23,7 @@ const DEFAULT_CONFIG = { HEXA_ID_MAINNET: 'b01623f1065ba45d68b516efe2873f59bfc9b9b2d8b194f94f989d87d711830a', SENTRY_DNS: 'https://25289533edf7432994f58edeaf6541dc@o1388909.ingest.sentry.io/6711631', ENVIRONMENT: APP_STAGE.DEVELOPMENT, - CHANNEL_URL: 'https://keeper-channel-dev-8d01fa5233d0.herokuapp.com/', + CHANNEL_URL: 'http://localhost:4002/', LETS_EXCHANGE_AFFILIATE_ID: 'G0BiaS34U81NR3ra', }; From cc1ac7ecfbd8ce6016799236e893bb354c927485 Mon Sep 17 00:00:00 2001 From: Anant Date: Tue, 6 Oct 2026 19:40:13 +0530 Subject: [PATCH 009/254] Verify packaged native pairing channel before release --- scripts/verify-release-artifact.py | 37 +++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/scripts/verify-release-artifact.py b/scripts/verify-release-artifact.py index 0de412db51..13e3a94dc1 100644 --- a/scripts/verify-release-artifact.py +++ b/scripts/verify-release-artifact.py @@ -19,6 +19,8 @@ import xml.etree.ElementTree as ET ANDROID_NS = "{http://schemas.android.com/apk/res/android}" +LIVE_CHANNEL = "https://channel.bitcoinkeeper.app/" +RETIRED_CHANNELS = ("https://keeper-channel.herokuapp.com/", "https://keeper-dev-channel.herokuapp.com/") def require_equal(actual, expected, label): @@ -37,6 +39,34 @@ def verify_features(bundle): raise ValueError("Required packaged feature markers absent: " + ", ".join(missing)) +def verify_channel(archive, suffix, info=None, app_prefix=None): + if suffix == ".ipa": + executable = info.get("CFBundleExecutable") + if not executable or "/" in executable: + raise ValueError("Cannot identify main iOS executable for channel verification") + members = [app_prefix + "/" + executable] + elif suffix == ".aab": + members = [name for name in archive.namelist() + if name.startswith("base/") and (name.endswith(".dex") or name == "base/resources.pb")] + else: + members = [name for name in archive.namelist() + if re.fullmatch(r"classes\d*\.dex", name) or name == "resources.arsc"] + if not members: + raise ValueError("No native release payload available for channel verification") + expected = LIVE_CHANNEL.encode() + retired = [url.encode() for url in RETIRED_CHANNELS] + found_live = False + found_retired = False + for member in members: + data = archive.read(member) + found_live |= expected in data + found_retired |= any(url in data for url in retired) + if found_retired: + raise ValueError("Packaged native configuration contains a retired pairing channel") + if not found_live: + raise ValueError("Live pairing channel absent from packaged native configuration") + + def output(command): result = subprocess.run(command, capture_output=True, text=True) if result.returncode: @@ -54,7 +84,9 @@ def inspect(path, manifest): info = plistlib.loads(archive.read(names[0])) app_id, version, build = (info.get(k) for k in ("CFBundleIdentifier", "CFBundleShortVersionString", "CFBundleVersion")) platform = "ios" - bundle = archive.read(names[0].rsplit("/", 1)[0] + "/main.jsbundle") + app_prefix = names[0].rsplit("/", 1)[0] + bundle = archive.read(app_prefix + "/main.jsbundle") + verify_channel(archive, suffix, info=info, app_prefix=app_prefix) elif suffix == ".apk": text = output([os.environ.get("AAPT", "aapt"), "dump", "badging", str(path)]) match = re.search(r"package: name='([^']+)' versionCode='([^']+)' versionName='([^']+)'", text) @@ -63,6 +95,7 @@ def inspect(path, manifest): app_id, build, version = match.groups() platform = "android" bundle = archive.read("assets/index.android.bundle") + verify_channel(archive, suffix) elif suffix == ".aab": jar = os.environ.get("BUNDLETOOL_JAR") if not jar or not Path(jar).is_file(): @@ -72,6 +105,7 @@ def inspect(path, manifest): app_id, build, version = root.get("package"), root.get(ANDROID_NS + "versionCode"), root.get(ANDROID_NS + "versionName") platform = "android" bundle = archive.read("base/assets/index.android.bundle") + verify_channel(archive, suffix) else: raise ValueError("Expected an IPA, APK or AAB") expected_id = {"ios": "io.hexawallet.keeper", "android": "io.hexawallet.bitcoinkeeper"}[platform] @@ -82,6 +116,7 @@ def inspect(path, manifest): result = {"artifact": path.name, "applicationId": app_id, "version": version, "build": str(build), "sha256": hashlib.sha256(path.read_bytes()).hexdigest(), "bundleSha256": hashlib.sha256(bundle).hexdigest(), "featureMarkers": "present", + "channelUrl": "verified", "signing": "separate verification required"} if suffix == ".apk": certs = output([os.environ.get("APKSIGNER", "apksigner"), "verify", "--print-certs", str(path)]) From 859429f7b99b5e30423a018cc5bf7cb57020be23 Mon Sep 17 00:00:00 2001 From: Anant Date: Tue, 6 Oct 2026 19:42:52 +0530 Subject: [PATCH 010/254] Prepare 2.6.3 pairing channel hotfix versions --- android/app/build.gradle | 4 ++-- ios/hexa_keeper.xcodeproj/project.pbxproj | 16 ++++++++-------- package.json | 2 +- release/version.json | 22 +++++++++++----------- 4 files changed, 22 insertions(+), 22 deletions(-) diff --git a/android/app/build.gradle b/android/app/build.gradle index eebb1acfe2..b73b4caad3 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -98,8 +98,8 @@ android { applicationId "io.hexawallet.keeper" minSdkVersion rootProject.ext.minSdkVersion targetSdkVersion rootProject.ext.targetSdkVersion - versionCode 625 - versionName "2.6.2" + versionCode 626 + versionName "2.6.3" missingDimensionStrategy 'react-native-camera', 'general' missingDimensionStrategy 'store', 'play' multiDexEnabled true diff --git a/ios/hexa_keeper.xcodeproj/project.pbxproj b/ios/hexa_keeper.xcodeproj/project.pbxproj index 52aa23ef44..c90117ddd7 100644 --- a/ios/hexa_keeper.xcodeproj/project.pbxproj +++ b/ios/hexa_keeper.xcodeproj/project.pbxproj @@ -748,7 +748,7 @@ CODE_SIGN_ENTITLEMENTS = hexa_keeper/hexa_keeper.entitlements; CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 618; + CURRENT_PROJECT_VERSION = 619; DEVELOPMENT_TEAM = Y5TCB759QL; ENABLE_BITCODE = NO; HEADER_SEARCH_PATHS = ( @@ -849,7 +849,7 @@ "$(inherited)", "\"$(SRCROOT)\"", ); - MARKETING_VERSION = 2.6.2; + MARKETING_VERSION = 2.6.3; OTHER_LDFLAGS = ( "$(inherited)", "-ObjC", @@ -876,7 +876,7 @@ CODE_SIGN_IDENTITY = "Apple Development"; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Distribution"; CODE_SIGN_STYLE = Manual; - CURRENT_PROJECT_VERSION = 618; + CURRENT_PROJECT_VERSION = 619; DEVELOPMENT_TEAM = Y5TCB759QL; "DEVELOPMENT_TEAM[sdk=iphoneos*]" = Y5TCB759QL; HEADER_SEARCH_PATHS = ( @@ -977,7 +977,7 @@ "$(inherited)", "\"$(SRCROOT)\"", ); - MARKETING_VERSION = 2.6.2; + MARKETING_VERSION = 2.6.3; OTHER_LDFLAGS = ( "$(inherited)", "-ObjC", @@ -1147,7 +1147,7 @@ CODE_SIGN_IDENTITY = "Apple Development"; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Distribution"; CODE_SIGN_STYLE = Manual; - CURRENT_PROJECT_VERSION = 618; + CURRENT_PROJECT_VERSION = 619; DEVELOPMENT_TEAM = Y5TCB759QL; "DEVELOPMENT_TEAM[sdk=iphoneos*]" = Y5TCB759QL; ENABLE_BITCODE = NO; @@ -1250,7 +1250,7 @@ "$(PROJECT_DIR)", "\"$(SRCROOT)\"", ); - MARKETING_VERSION = 2.6.2; + MARKETING_VERSION = 2.6.3; OTHER_LDFLAGS = ( "$(inherited)", "-ObjC", @@ -1278,7 +1278,7 @@ CODE_SIGN_ENTITLEMENTS = hexa_keeper_dev.entitlements; CODE_SIGN_IDENTITY = "Apple Distribution"; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Distribution"; - CURRENT_PROJECT_VERSION = 618; + CURRENT_PROJECT_VERSION = 619; DEVELOPMENT_TEAM = Y5TCB759QL; "DEVELOPMENT_TEAM[sdk=iphoneos*]" = Y5TCB759QL; HEADER_SEARCH_PATHS = ( @@ -1380,7 +1380,7 @@ "$(PROJECT_DIR)", "\"$(SRCROOT)\"", ); - MARKETING_VERSION = 2.6.2; + MARKETING_VERSION = 2.6.3; OTHER_LDFLAGS = ( "$(inherited)", "-ObjC", diff --git a/package.json b/package.json index dc78f187da..ad0eeb2c1e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "hexa_keeper", - "version": "2.6.2", + "version": "2.6.3", "private": true, "scripts": { "ios": "npx react-native run-ios --scheme=hexa_keeper_dev --simulator 'iPhone 16' ", diff --git a/release/version.json b/release/version.json index 553cacad87..57bc77e469 100644 --- a/release/version.json +++ b/release/version.json @@ -1,21 +1,21 @@ { - "version": "2.6.2", - "androidVersionCode": 625, - "iosBuildNumber": 618, + "version": "2.6.3", + "androidVersionCode": 626, + "iosBuildNumber": 619, "baselines": { "android": { "applicationId": "io.hexawallet.bitcoinkeeper", - "verifiedAt": "2026-10-05T10:04:03Z", - "highestUploadedBuild": 624, - "highestMarketingVersion": "2.5.17", - "productionRelease": "624 (2.5.17)" + "verifiedAt": "2026-10-06T14:11:41Z", + "highestUploadedBuild": 625, + "highestMarketingVersion": "2.6.2", + "productionRelease": "625 (2.6.2)" }, "ios": { "applicationId": "io.hexawallet.keeper", - "verifiedAt": "2026-10-05T10:04:36Z", - "highestUploadedBuild": 617, - "highestMarketingVersion": "2.5.17", - "productionRelease": "617 (2.5.17)" + "verifiedAt": "2026-10-06T14:12:22Z", + "highestUploadedBuild": 618, + "highestMarketingVersion": "2.6.2", + "productionRelease": "618 (2.6.2)" } } } From 872cbc4b16ddcb0a8f3b5b1d7ebcde28e06ed096 Mon Sep 17 00:00:00 2001 From: Anant Date: Tue, 6 Oct 2026 20:11:52 +0530 Subject: [PATCH 011/254] Use aapt2 for release APK metadata verification --- scripts/verify-release-artifact.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/scripts/verify-release-artifact.py b/scripts/verify-release-artifact.py index 13e3a94dc1..16ebaf120b 100644 --- a/scripts/verify-release-artifact.py +++ b/scripts/verify-release-artifact.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 """Inspect packaged release metadata and required feature markers without dumping secrets. -APK: Android SDK aapt and apksigner are required. +APK: Android SDK aapt2 and apksigner are required. AAB: BUNDLETOOL_JAR and Java are required. IPA: Python standard library; signing is verified separately by codesign/export evidence. This verifier supplements device tests; bytecode markers do not prove feature behavior. @@ -74,6 +74,18 @@ def output(command): return result.stdout +def android_tool(name, override): + if os.environ.get(override): + return os.environ[override] + for variable in ("ANDROID_HOME", "ANDROID_SDK_ROOT"): + sdk = os.environ.get(variable) + if sdk: + candidates = sorted((Path(sdk) / "build-tools").glob("*/" + name), reverse=True) + if candidates: + return str(candidates[0]) + return name + + def inspect(path, manifest): suffix = path.suffix.lower() with zipfile.ZipFile(path) as archive: @@ -88,7 +100,7 @@ def inspect(path, manifest): bundle = archive.read(app_prefix + "/main.jsbundle") verify_channel(archive, suffix, info=info, app_prefix=app_prefix) elif suffix == ".apk": - text = output([os.environ.get("AAPT", "aapt"), "dump", "badging", str(path)]) + text = output([android_tool("aapt2", "AAPT2"), "dump", "badging", str(path)]) match = re.search(r"package: name='([^']+)' versionCode='([^']+)' versionName='([^']+)'", text) if not match: raise ValueError("Cannot read APK package/version metadata") @@ -119,7 +131,7 @@ def inspect(path, manifest): "channelUrl": "verified", "signing": "separate verification required"} if suffix == ".apk": - certs = output([os.environ.get("APKSIGNER", "apksigner"), "verify", "--print-certs", str(path)]) + certs = output([android_tool("apksigner", "APKSIGNER"), "verify", "--print-certs", str(path)]) expected = os.environ.get("KEEPER_ANDROID_CERT_SHA256", "").lower().replace(":", "") if not re.fullmatch(r"[0-9a-f]{64}", expected): raise ValueError("Set KEEPER_ANDROID_CERT_SHA256 to the independently verified release certificate") From 5ff2eb4e968772fa04a0ac6a20f186ed3ffac3fe Mon Sep 17 00:00:00 2001 From: Anant Date: Wed, 7 Oct 2026 01:29:34 +0530 Subject: [PATCH 012/254] Use active channel for Keeper Desktop pairing --- .../__tests__/channelURL.test.ts | 16 ++++++++++++++++ src/utils/service-utilities/channelURL.ts | 16 ++++++++++++++++ src/utils/service-utilities/config.ts | 7 +++---- 3 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 src/utils/service-utilities/__tests__/channelURL.test.ts create mode 100644 src/utils/service-utilities/channelURL.ts diff --git a/src/utils/service-utilities/__tests__/channelURL.test.ts b/src/utils/service-utilities/__tests__/channelURL.test.ts new file mode 100644 index 0000000000..f2cb7390ba --- /dev/null +++ b/src/utils/service-utilities/__tests__/channelURL.test.ts @@ -0,0 +1,16 @@ +import { LIVE_CHANNEL_URL, resolveChannelURL } from '../channelURL'; + +describe('Keeper Desktop channel endpoint', () => { + it.each([ + undefined, + '', + 'https://keeper-channel.herokuapp.com/', + 'https://keeper-channel-dev-8d01fa5233d0.herokuapp.com/', + ])('uses the live channel for an absent or retired override (%s)', (value) => { + expect(resolveChannelURL(value)).toBe(LIVE_CHANNEL_URL); + }); + + it('preserves an explicit local channel for isolated QA', () => { + expect(resolveChannelURL(' http://127.0.0.1:3000/ ')).toBe('http://127.0.0.1:3000/'); + }); +}); diff --git a/src/utils/service-utilities/channelURL.ts b/src/utils/service-utilities/channelURL.ts new file mode 100644 index 0000000000..2e85aa1257 --- /dev/null +++ b/src/utils/service-utilities/channelURL.ts @@ -0,0 +1,16 @@ +// The previous Heroku channel apps have been retired. Keep the known legacy +// overrides here because older private build environments still set them. +export const LIVE_CHANNEL_URL = 'https://channel.bitcoinkeeper.app/'; + +const RETIRED_CHANNEL_URLS = new Set([ + 'https://keeper-channel.herokuapp.com', + 'https://keeper-channel-dev-8d01fa5233d0.herokuapp.com', +]); + +export const resolveChannelURL = (configuredURL?: string): string => { + const candidate = configuredURL?.trim(); + if (!candidate || RETIRED_CHANNEL_URLS.has(candidate.replace(/\/+$/, ''))) { + return LIVE_CHANNEL_URL; + } + return candidate; +}; diff --git a/src/utils/service-utilities/config.ts b/src/utils/service-utilities/config.ts index 5344d501b9..a1db1baa07 100644 --- a/src/utils/service-utilities/config.ts +++ b/src/utils/service-utilities/config.ts @@ -1,4 +1,5 @@ import config from 'react-native-config'; +import { resolveChannelURL } from './channelURL'; import { EntityKind, WalletType } from '../../services/wallets/enums'; import { Address, devAddress, prodAddress } from '../../constants/address'; @@ -23,7 +24,7 @@ const DEFAULT_CONFIG = { HEXA_ID_MAINNET: 'b01623f1065ba45d68b516efe2873f59bfc9b9b2d8b194f94f989d87d711830a', SENTRY_DNS: 'https://25289533edf7432994f58edeaf6541dc@o1388909.ingest.sentry.io/6711631', ENVIRONMENT: APP_STAGE.DEVELOPMENT, - CHANNEL_URL: 'https://keeper-channel-dev-8d01fa5233d0.herokuapp.com/', + CHANNEL_URL: resolveChannelURL(), LETS_EXCHANGE_AFFILIATE_ID: 'G0BiaS34U81NR3ra', }; @@ -80,9 +81,7 @@ class Configuration { public ENVIRONMENT: string; - public CHANNEL_URL: string = config.CHANNEL_URL?.trim() - ? config.CHANNEL_URL.trim() - : DEFAULT_CONFIG.CHANNEL_URL.trim(); + public CHANNEL_URL: string = resolveChannelURL(config.CHANNEL_URL); public GASFREE_API_KEY: string = config.GASFREE_API_KEY?.trim(); public GASFREE_API_SECRET: string = config.GASFREE_API_SECRET?.trim(); From ebea172d8655e1b0af64a90e41fe74d22e7a330e Mon Sep 17 00:00:00 2001 From: Anant Date: Wed, 7 Oct 2026 01:23:41 +0530 Subject: [PATCH 013/254] Add privacy-safe stage diagnostics for backup verification --- src/services/backup/repair.ts | 27 +++++++++++++++++++++++++-- tests/recovery/helpers.cjs | 6 +++--- tests/recovery/repair.test.cjs | 9 +++++++++ 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/src/services/backup/repair.ts b/src/services/backup/repair.ts index 25b543c7a1..176781d031 100644 --- a/src/services/backup/repair.ts +++ b/src/services/backup/repair.ts @@ -35,6 +35,16 @@ export type RepairPhase = | 'verified' | 'conflict' | 'unverified'; +type InspectionStage = + | 'local-capture' + | 'snapshot-request' + | 'snapshot-validation' + | 'snapshot-decryption' + | 'comparison' + | 'repair-preparation' + | 'repair-upload' + | 'repair-readback' + | 'local-recheck'; const inFlight = new Map< string, { @@ -149,6 +159,7 @@ export function inspectBackup( listeners.forEach((listener) => listener(phase)); }; const result = withBackupSession(appId, async () => { + let stage: InspectionStage = 'local-capture'; const deadline = Date.now() + BACKUP_DEADLINE_MS; const revision = backupRevision(appId); const assertCurrent = () => { @@ -168,12 +179,14 @@ export function inspectBackup( assertCurrent(); let expectedRevision: string; let needsVaultRepair = false; - const read = async () => { + const read = async (readback = false) => { + stage = readback ? 'repair-readback' : 'snapshot-request'; const response = await boundedBackupPost( `${config.RELAY}getBackupSnapshot`, { appId }, deadline ); + if (!readback) stage = 'snapshot-validation'; assertCurrent(); if (!/^[a-f0-9]{64}$/.test(response.data?.revision)) throw new Error('Backup snapshot revision unavailable'); @@ -208,6 +221,7 @@ export function inspectBackup( }, } : response.data; + if (!readback) stage = 'snapshot-decryption'; const decoded = await decodeImage(key, availableResponse, appId, assertCurrent); needsVaultRepair = unavailable.length > 0 || @@ -219,6 +233,7 @@ export function inspectBackup( return decoded; }; const remote = await read(); + stage = 'comparison'; const contentComparison = await compareImages(local, remote, assertCurrent); const comparison = contentComparison === 'matched' && needsVaultRepair ? 'different' : contentComparison; @@ -232,6 +247,7 @@ export function inspectBackup( return 'different'; } if (comparison === 'different') { + stage = 'repair-preparation'; notify('preparing'); const payload = await encodeImage(app, local, assertCurrent); assertCurrent(); @@ -243,6 +259,7 @@ export function inspectBackup( throw new Error('Local backup changed'); assertCurrent(); notify('uploading'); + stage = 'repair-upload'; try { const response = await boundedBackupPost( `${config.RELAY}repairAppBackup`, @@ -257,13 +274,16 @@ export function inspectBackup( // A lost response is not proof the upload failed. Always read back. } notify('verifying'); - const verifiedRemote = await read(); + stage = 'repair-readback'; + const verifiedRemote = await read(true); + stage = 'repair-readback'; if ( needsVaultRepair || (await compareImages(local, verifiedRemote, assertCurrent)) !== 'matched' ) throw new Error('Backup not verified'); } + stage = 'local-recheck'; if ( (await compareImages(local, await captureBackupImage(assertCurrent), assertCurrent)) !== 'matched' @@ -273,6 +293,9 @@ export function inspectBackup( notify('verified'); return 'verified'; } catch { + // Only log the stage. Errors and response objects may contain encrypted + // backup data, account identifiers, headers, or request bodies. + console.warn('Assisted Server Backup check failed at stage:', stage); notify('unverified'); return 'unverified'; } diff --git a/tests/recovery/helpers.cjs b/tests/recovery/helpers.cjs index ccc8e1ad5e..c03133b986 100644 --- a/tests/recovery/helpers.cjs +++ b/tests/recovery/helpers.cjs @@ -21,7 +21,7 @@ function harness(options = {}) { const remote = { appId: app.id, wallets: {}, signers: {}, nodes: [], vaults: [], labels: [] }; let vaults = [], labels = []; const revision = () => require('node:crypto').createHash('sha256').update(JSON.stringify({remote,vaults,labels})).digest('hex'); - const calls = [], phases = []; + const calls = [], phases = [], diagnostics = []; const db = { getObjectByIndex: (schema) => schema === 'KeeperApp' ? app : local[schema] }; const rest = { post: async (path, payload, _headers, config) => { calls.push(path.replace('fixture/', '')); @@ -48,8 +48,8 @@ function harness(options = {}) { 'src/utils/service-utilities/encryption': encryption, 'src/utils/utilities': { getKeyUID: s => s.id || s.masterFingerprint, sanitizeSeedKeyForBackup: s => s, sanitizeVaultSignersForSeedKeyBackup: s => s }, './transport': transport, './image': image, - }); - return { app, local, remote, calls, phases, transport, repair, + }, { console: { warn: (...args) => diagnostics.push(args) } }); + return { app, local, remote, calls, phases, diagnostics, transport, repair, run: (write = false) => repair.inspectBackup(app.id, write, phase => phases.push(phase)) }; } const wallet = (id = 'wallet', networkType = 'MAINNET') => ({ id, networkType, type: 'DEFAULT', diff --git a/tests/recovery/repair.test.cjs b/tests/recovery/repair.test.cjs index f72053092f..467ef0ae0c 100644 --- a/tests/recovery/repair.test.cjs +++ b/tests/recovery/repair.test.cjs @@ -21,6 +21,15 @@ for (const failure of ['readError','reject','corruptWrite']) test(`${failure} ne if (failure === 'readError') assert.deepEqual(f.calls, ['getBackupSnapshot']); }); +test('read-only failure identifies its stage without logging error contents or backup identifiers', async () => { + const f = harness({ adapter: async () => { throw Error('private-fixture-identifier'); } }); + f.local.Wallet.push(wallet()); + assert.equal(await f.run(), 'unverified'); + assert.deepEqual(f.diagnostics, [['Assisted Server Backup check failed at stage:', 'snapshot-request']]); + assert.ok(!JSON.stringify(f.diagnostics).includes('private-fixture-identifier')); + assert.deepEqual(f.calls, ['getBackupSnapshot']); +}); + test('lost upload acknowledgement is resolved by readback without duplicate upload', async () => { const f = harness({ lostResponse: true }); f.local.Wallet.push(wallet()); assert.equal(await f.run(true), 'verified'); assert.deepEqual(f.calls, ['getBackupSnapshot','repairAppBackup','getBackupSnapshot']); From a788900e41b61bbe8961a80d7c533fdd8dd87982 Mon Sep 17 00:00:00 2001 From: Anant Date: Wed, 7 Oct 2026 01:32:25 +0530 Subject: [PATCH 014/254] Retry read-only backup check after concurrent write --- src/services/backup/repair.ts | 27 +++++++++++++--- src/services/backup/transport.ts | 6 +++- tests/recovery/helpers.cjs | 1 + tests/recovery/repair.test.cjs | 53 ++++++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+), 5 deletions(-) diff --git a/src/services/backup/repair.ts b/src/services/backup/repair.ts index 176781d031..4385ed3d38 100644 --- a/src/services/backup/repair.ts +++ b/src/services/backup/repair.ts @@ -10,6 +10,7 @@ import { } from 'src/utils/utilities'; import { BACKUP_DEADLINE_MS, + backupQueuedRevision, backupRevision, boundedBackupPost, withBackupSession, @@ -158,17 +159,22 @@ export function inspectBackup( if (active) active.phase = phase; listeners.forEach((listener) => listener(phase)); }; - const result = withBackupSession(appId, async () => { + const inspectOnce = async (allowRetry: boolean): Promise => { let stage: InspectionStage = 'local-capture'; + let changedRevision = 0; const deadline = Date.now() + BACKUP_DEADLINE_MS; const revision = backupRevision(appId); const assertCurrent = () => { if ( Date.now() >= deadline || - (dbManager.getObjectByIndex(RealmSchema.KeeperApp) as any)?.id !== appId || - backupRevision(appId) !== revision + (dbManager.getObjectByIndex(RealmSchema.KeeperApp) as any)?.id !== appId ) throw new Error('Backup changed during check'); + const currentRevision = backupRevision(appId); + if (currentRevision !== revision) { + changedRevision = currentRevision; + throw new Error('Backup changed during check'); + } }; try { notify('checking'); @@ -293,13 +299,26 @@ export function inspectBackup( notify('verified'); return 'verified'; } catch { + // A routine incremental write can be queued while a read-only check is + // running. Retry behind that write once before declaring the check failed. + if ( + changedRevision && + allowRetry && + !repair && + backupQueuedRevision(appId) >= changedRevision + ) + return 'retry'; // Only log the stage. Errors and response objects may contain encrypted // backup data, account identifiers, headers, or request bodies. console.warn('Assisted Server Backup check failed at stage:', stage); notify('unverified'); return 'unverified'; } - }); + }; + const runOnce = (allowRetry: boolean) => withBackupSession(appId, () => inspectOnce(allowRetry)); + const result: Promise = runOnce(true).then(async (phase) => + phase === 'retry' ? ((await runOnce(false)) as RepairPhase) : phase + ); inFlight.set(appId, { repair, promise: result, listeners }); const clean = () => { if (inFlight.get(appId)?.promise === result) inFlight.delete(appId); diff --git a/src/services/backup/transport.ts b/src/services/backup/transport.ts index 21339041ec..c462fb2f94 100644 --- a/src/services/backup/transport.ts +++ b/src/services/backup/transport.ts @@ -3,7 +3,9 @@ import RestClient from '../rest/RestClient'; // One queue per account covers repair, incremental writes, labels and deletion. const queues = new Map>(); const revisions = new Map(); +const queuedMutationRevisions = new Map(); export const backupRevision = (id: string) => revisions.get(id) || 0; +export const backupQueuedRevision = (id: string) => queuedMutationRevisions.get(id) || 0; export const markBackupMutation = (id: string) => revisions.set(id, backupRevision(id) + 1); export const BACKUP_STALL_MS = 30_000; export const BACKUP_DEADLINE_MS = 10 * 60_000; @@ -67,5 +69,7 @@ export function backupPost(path: string, body: any) { const appId = body.appId || body.appID; if (!appId) return Promise.reject(new Error('Missing backup account')); markBackupMutation(appId); - return withBackupSession(appId, () => boundedBackupPost(path, body)); + const result = withBackupSession(appId, () => boundedBackupPost(path, body)); + queuedMutationRevisions.set(appId, backupRevision(appId)); + return result; } diff --git a/tests/recovery/helpers.cjs b/tests/recovery/helpers.cjs index c03133b986..6950fb892f 100644 --- a/tests/recovery/helpers.cjs +++ b/tests/recovery/helpers.cjs @@ -31,6 +31,7 @@ function harness(options = {}) { if (options.readError) throw Error('offline'); return { data: options.response || { appImage: remote, allVaultImages: vaults, labels, revision: revision() } }; } + if (path.endsWith('updateAppImage')) return { data: { updated: true } }; if (payload.expectedRevision !== revision()) throw Error('Backup changed'); if (options.reject) return { data: { updated: false } }; remote.wallets = { ...payload.walletObject }; remote.signers = { ...payload.signersObject }; remote.nodes = payload.nodes; diff --git a/tests/recovery/repair.test.cjs b/tests/recovery/repair.test.cjs index 467ef0ae0c..14367ea19a 100644 --- a/tests/recovery/repair.test.cjs +++ b/tests/recovery/repair.test.cjs @@ -30,6 +30,52 @@ test('read-only failure identifies its stage without logging error contents or b assert.deepEqual(f.calls, ['getBackupSnapshot']); }); +test('read-only check retries once after a concurrent incremental backup changes its revision', async () => { + let f, changed = false, write; + f = harness({ post: async ({ path, remote }) => { + if (path.endsWith('getBackupSnapshot') && !changed) { + changed = true; + write = f.transport.backupPost('fixture/updateAppImage', { appId: f.app.id }); + } + if (path.endsWith('updateAppImage')) { + remote.wallets.wallet = encryption.encrypt( + encryption.generateEncryptionKey(f.app.primarySeed), JSON.stringify(wallet()) + ); + } + } }); + f.local.Wallet.push(wallet()); + assert.equal(await f.run(), 'verified'); + await write; + assert.deepEqual(f.calls, ['getBackupSnapshot', 'updateAppImage', 'getBackupSnapshot']); + assert.deepEqual(f.phases, ['checking', 'checking', 'verified']); + assert.deepEqual(f.diagnostics, []); +}); + +test('early revision change without a queued write stays unverified', async () => { + let f; + f = harness({ post: async ({ path }) => { + if (path.endsWith('getBackupSnapshot')) f.transport.markBackupMutation(f.app.id); + } }); + f.local.Wallet.push(wallet()); + assert.equal(await f.run(), 'unverified'); + assert.deepEqual(f.calls, ['getBackupSnapshot']); + assert.deepEqual(f.phases, ['checking', 'unverified']); +}); + +test('continuous queued writes limit a read-only check to one retry', async () => { + let f; + const writes = []; + f = harness({ post: async ({ path }) => { + if (path.endsWith('getBackupSnapshot')) + writes.push(f.transport.backupPost('fixture/updateAppImage', { appId: f.app.id })); + } }); + f.local.Wallet.push(wallet()); + assert.equal(await f.run(), 'unverified'); + await Promise.all(writes); + assert.equal(f.calls.filter((path) => path === 'getBackupSnapshot').length, 2); + assert.deepEqual(f.phases, ['checking', 'checking', 'unverified']); +}); + test('lost upload acknowledgement is resolved by readback without duplicate upload', async () => { const f = harness({ lostResponse: true }); f.local.Wallet.push(wallet()); assert.equal(await f.run(true), 'verified'); assert.deepEqual(f.calls, ['getBackupSnapshot','repairAppBackup','getBackupSnapshot']); @@ -96,6 +142,7 @@ test('mutation arriving during upload invalidates completion and remains queued let f; f = harness({ post: async ({ path }) => { if (path.endsWith('repairAppBackup')) f.transport.markBackupMutation(f.app.id); } }); f.local.Wallet.push(wallet()); assert.equal(await f.run(true), 'unverified'); assert.ok(!f.phases.includes('verified')); + assert.deepEqual(f.calls, ['getBackupSnapshot', 'repairAppBackup', 'getBackupSnapshot']); }); test('account switch during check cannot upload one account with another account data', async () => { @@ -111,6 +158,12 @@ test('chunked encryption remains compatible with existing recovery including Uni assert.deepEqual(JSON.parse(JSON.stringify(await image.decryptRecord('test-only',cipher))), record); }); +test('existing large CryptoJS ciphertext remains readable by streamed backup verification', async () => { + const record = { id: 'legacy-large', text: 'a'.repeat(100_000) + '🔑বাংলা' }; + const ciphertext = encryption.encrypt('test-only', JSON.stringify(record)); + assert.equal(JSON.stringify(await image.decryptRecord('test-only', ciphertext)), JSON.stringify(record)); +}); + test('oversized record fails without uploading or truncating', async () => { const f = harness(); f.local.Wallet.push({ ...wallet(), text:'x'.repeat(image.MAX_RECORD_BYTES) }); assert.equal(await f.run(true),'unverified'); assert.deepEqual(f.calls,[]); From 3f9744c7d4415204f31a4417be660b0ad62a297b Mon Sep 17 00:00:00 2001 From: Anant Date: Wed, 7 Oct 2026 01:46:51 +0530 Subject: [PATCH 015/254] Keep backup stage diagnostics in production bundle --- src/services/backup/repair.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/services/backup/repair.ts b/src/services/backup/repair.ts index 4385ed3d38..65a1953ffc 100644 --- a/src/services/backup/repair.ts +++ b/src/services/backup/repair.ts @@ -310,7 +310,7 @@ export function inspectBackup( return 'retry'; // Only log the stage. Errors and response objects may contain encrypted // backup data, account identifiers, headers, or request bodies. - console.warn('Assisted Server Backup check failed at stage:', stage); + globalThis.console.warn('Assisted Server Backup check failed at stage:', stage); notify('unverified'); return 'unverified'; } From ef1f561f446f34b7f2202d65f48b5c3698582075 Mon Sep 17 00:00:00 2001 From: Anant Date: Wed, 7 Oct 2026 01:53:55 +0530 Subject: [PATCH 016/254] Classify backup snapshot transport failures without request data --- src/services/backup/repair.ts | 8 +++++--- src/services/backup/transport.ts | 30 +++++++++++++++++++++++++++--- tests/recovery/repair.test.cjs | 14 +++++++++++++- 3 files changed, 45 insertions(+), 7 deletions(-) diff --git a/src/services/backup/repair.ts b/src/services/backup/repair.ts index 65a1953ffc..9d9c2f80c8 100644 --- a/src/services/backup/repair.ts +++ b/src/services/backup/repair.ts @@ -10,6 +10,7 @@ import { } from 'src/utils/utilities'; import { BACKUP_DEADLINE_MS, + BackupRequestError, backupQueuedRevision, backupRevision, boundedBackupPost, @@ -298,7 +299,7 @@ export function inspectBackup( assertCurrent(); notify('verified'); return 'verified'; - } catch { + } catch (error) { // A routine incremental write can be queued while a read-only check is // running. Retry behind that write once before declaring the check failed. if ( @@ -308,9 +309,10 @@ export function inspectBackup( backupQueuedRevision(appId) >= changedRevision ) return 'retry'; - // Only log the stage. Errors and response objects may contain encrypted + // Only log fixed categories. Errors and responses can contain encrypted // backup data, account identifiers, headers, or request bodies. - globalThis.console.warn('Assisted Server Backup check failed at stage:', stage); + const category = error instanceof BackupRequestError ? error.category : 'non-transport'; + globalThis.console.warn('Assisted Server Backup check failed at stage:', stage, category); notify('unverified'); return 'unverified'; } diff --git a/src/services/backup/transport.ts b/src/services/backup/transport.ts index c462fb2f94..6b23798c71 100644 --- a/src/services/backup/transport.ts +++ b/src/services/backup/transport.ts @@ -10,6 +10,30 @@ export const markBackupMutation = (id: string) => revisions.set(id, backupRevisi export const BACKUP_STALL_MS = 30_000; export const BACKUP_DEADLINE_MS = 10 * 60_000; +export type BackupRequestFailure = 'http-4xx' | 'http-5xx' | 'timeout' | 'network' | 'other'; + +export class BackupRequestError extends Error { + constructor(public readonly category: BackupRequestFailure) { + super('Backup request could not be completed'); + } +} + +function requestFailureCategory(error: unknown, aborted: boolean): BackupRequestFailure { + if (aborted) return 'timeout'; + if (!error || typeof error !== 'object') return 'other'; + const failure = error as { response?: { status?: unknown }; status?: unknown; code?: unknown; name?: unknown; message?: unknown }; + const status = failure.response?.status ?? failure.status; + if (typeof status === 'number') { + if (status >= 400 && status < 500) return 'http-4xx'; + if (status >= 500 && status < 600) return 'http-5xx'; + return 'other'; + } + if (failure.code === 'ECONNABORTED' || failure.code === 'ETIMEDOUT' || failure.name === 'AbortError') + return 'timeout'; + if (failure.code === 'ERR_NETWORK' || failure.message === 'Network Error') return 'network'; + return 'other'; +} + export function withBackupSession(id: string, work: () => Promise): Promise { const previous = queues.get(id) || Promise.resolve(); const result = previous.catch(() => undefined).then(work); @@ -37,7 +61,7 @@ export async function boundedBackupPost( idle = setTimeout(() => controller.abort(), BACKUP_STALL_MS); }; const remaining = deadline - Date.now(); - if (remaining <= 0) throw new Error('Backup deadline exceeded'); + if (remaining <= 0) throw new BackupRequestError('timeout'); const total = setTimeout(() => controller.abort(), remaining); resetIdle(); try { @@ -56,9 +80,9 @@ export async function boundedBackupPost( } }, }); - } catch { + } catch (error) { // Do not propagate Axios errors: they can contain request bodies and headers. - throw new Error('Backup request could not be completed'); + throw new BackupRequestError(requestFailureCategory(error, controller.signal.aborted)); } finally { clearTimeout(idle); clearTimeout(total); diff --git a/tests/recovery/repair.test.cjs b/tests/recovery/repair.test.cjs index 14367ea19a..59e97921e3 100644 --- a/tests/recovery/repair.test.cjs +++ b/tests/recovery/repair.test.cjs @@ -25,11 +25,23 @@ test('read-only failure identifies its stage without logging error contents or b const f = harness({ adapter: async () => { throw Error('private-fixture-identifier'); } }); f.local.Wallet.push(wallet()); assert.equal(await f.run(), 'unverified'); - assert.deepEqual(f.diagnostics, [['Assisted Server Backup check failed at stage:', 'snapshot-request']]); + assert.deepEqual(f.diagnostics, [['Assisted Server Backup check failed at stage:', 'snapshot-request', 'other']]); assert.ok(!JSON.stringify(f.diagnostics).includes('private-fixture-identifier')); assert.deepEqual(f.calls, ['getBackupSnapshot']); }); +for (const [error, category] of [ + [{ response: { status: 404, data: 'private-fixture-identifier' }, config: { headers: { Authorization: 'private-fixture-identifier' } } }, 'http-4xx'], + [{ response: { status: 503, data: 'private-fixture-identifier' } }, 'http-5xx'], + [{ code: 'ECONNABORTED', message: 'private-fixture-identifier' }, 'timeout'], + [{ code: 'ERR_NETWORK', message: 'private-fixture-identifier' }, 'network'], +]) test(`snapshot transport diagnostics classify ${category} without request data`, async () => { + const f = harness({ adapter: async () => { throw error; } }); + assert.equal(await f.run(), 'unverified'); + assert.deepEqual(f.diagnostics, [['Assisted Server Backup check failed at stage:', 'snapshot-request', category]]); + assert.ok(!JSON.stringify(f.diagnostics).includes('private-fixture-identifier')); +}); + test('read-only check retries once after a concurrent incremental backup changes its revision', async () => { let f, changed = false, write; f = harness({ post: async ({ path, remote }) => { From 84476d7b17cbd34b0859023052b0d49407e05984 Mon Sep 17 00:00:00 2001 From: antuz123 <43998049+antuz123@users.noreply.github.com> Date: Wed, 7 Oct 2026 02:31:08 +0530 Subject: [PATCH 017/254] ci: test release/2.6.3 pull requests Enable the release branch test trigger and repair the inert recovery provenance fixture exposed by that run. No production app code changes. --- .github/workflows/test.yml | 2 ++ tests/store/versionHistory.test.ts | 1 + 2 files changed, 3 insertions(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index a81eab2bb5..dbff68d1af 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -6,11 +6,13 @@ on: - sprint - dev - main + - release/2.6.3 pull_request: branches: - sprint - dev - main + - release/2.6.3 jobs: test: diff --git a/tests/store/versionHistory.test.ts b/tests/store/versionHistory.test.ts index 2343a8c9db..8526829fc8 100644 --- a/tests/store/versionHistory.test.ts +++ b/tests/store/versionHistory.test.ts @@ -122,6 +122,7 @@ function recoveryFixture(backupVersion: string) { BackupType: { SEED: 'SEED' }, EntityKind: { USDT_WALLET: 'USDT_WALLET' }, BackupAction: { SEED_BACKUP_CONFIRMED: 'confirmed' }, + pauseBackup: () => {}, NetworkType: { MAINNET: 'MAINNET', TESTNET: 'TESTNET' }, uaiType: {}, moment: () => ({ unix: () => 1 }), From ef0d4d89e1879cfdb072008816bb85fb7cdf1e61 Mon Sep 17 00:00:00 2001 From: Anant Date: Wed, 7 Oct 2026 02:05:22 +0530 Subject: [PATCH 018/254] Package public backend contributor setup for sprint (cherry picked from commit f225c1978c254830350068f8a08b659ecdd80fa6) --- .github/pull_request_template.md | 29 ++ .github/workflows/public-backend.yml | 59 ++++ CONTRIBUTING.md | 113 ++++++++ Readme.md | 10 + dev/local-backend/.env.example | 5 + dev/local-backend/.gitignore | 3 + dev/local-backend/ACCESS.md | 24 ++ dev/local-backend/CI.md | 54 ++++ dev/local-backend/HANDOFF.md | 61 ++++ dev/local-backend/README.md | 173 +++++++++++ dev/local-backend/VERIFICATION.md | 273 ++++++++++++++++++ .../adapters/signing/files/.dockerignore | 9 + .../adapters/signing/files/Dockerfile.local | 11 + .../signing/files/scripts/local-entry.cjs | 14 + .../adapters/signing/files/src/config.ts | 100 +++++++ .../adapters/signing/local.patch | 132 +++++++++ dev/local-backend/app.env.local.example | 10 + dev/local-backend/compose.yaml | 98 +++++++ dev/local-backend/dev | 3 + dev/local-backend/keeper.py | 181 ++++++++++++ dev/local-backend/nginx.conf | 22 ++ dev/local-backend/sources.lock.json | 18 ++ dev/local-backend/test_keeper.py | 92 ++++++ dev/local-backend/verify-local.sh | 3 + dev/local-backend/verify.py | 151 ++++++++++ setup.sh | 13 +- 26 files changed, 1653 insertions(+), 8 deletions(-) create mode 100644 .github/pull_request_template.md create mode 100644 .github/workflows/public-backend.yml create mode 100644 CONTRIBUTING.md create mode 100644 dev/local-backend/.env.example create mode 100644 dev/local-backend/.gitignore create mode 100644 dev/local-backend/ACCESS.md create mode 100644 dev/local-backend/CI.md create mode 100644 dev/local-backend/HANDOFF.md create mode 100644 dev/local-backend/README.md create mode 100644 dev/local-backend/VERIFICATION.md create mode 100644 dev/local-backend/adapters/signing/files/.dockerignore create mode 100644 dev/local-backend/adapters/signing/files/Dockerfile.local create mode 100644 dev/local-backend/adapters/signing/files/scripts/local-entry.cjs create mode 100644 dev/local-backend/adapters/signing/files/src/config.ts create mode 100644 dev/local-backend/adapters/signing/local.patch create mode 100644 dev/local-backend/app.env.local.example create mode 100644 dev/local-backend/compose.yaml create mode 100755 dev/local-backend/dev create mode 100644 dev/local-backend/keeper.py create mode 100644 dev/local-backend/nginx.conf create mode 100644 dev/local-backend/sources.lock.json create mode 100644 dev/local-backend/test_keeper.py create mode 100755 dev/local-backend/verify-local.sh create mode 100644 dev/local-backend/verify.py diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md new file mode 100644 index 0000000000..45f51ccc3d --- /dev/null +++ b/.github/pull_request_template.md @@ -0,0 +1,29 @@ +## Problem and change + + + +Issue: +OpenSpec change / archived proposal: +Target branch: + +## Test results + + + +| Check / scenario | Platform | Passed / failed / blocked / not run | Evidence or reason | +| --- | --- | --- | --- | +| | | | | + +## Review considerations + + + +- [ ] The diff excludes local secrets, wallet data and generated build/dependency files. +- [ ] Test failures and untested behavior are recorded above. +- [ ] The issue/spec and implementation describe the same scope. + +Keeper maintainers own security review, acceptance, merging and official releases. +Passing checks does not constitute approval. diff --git a/.github/workflows/public-backend.yml b/.github/workflows/public-backend.yml new file mode 100644 index 0000000000..9212bdce78 --- /dev/null +++ b/.github/workflows/public-backend.yml @@ -0,0 +1,59 @@ +name: Public backend setup + +on: + push: + branches: [sprint, main] + paths: + - 'dev/local-backend/**' + - '.github/workflows/public-backend.yml' + pull_request: + branches: [sprint, main] + paths: + - 'dev/local-backend/**' + - '.github/workflows/public-backend.yml' + workflow_dispatch: + +permissions: + contents: read + +concurrency: + group: public-backend-${{ github.event.pull_request.number || github.ref }} + cancel-in-progress: true + +jobs: + signing: + name: Public SigningServer adapter + runs-on: ubuntu-latest + timeout-minutes: 20 + steps: + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4 + with: + persist-credentials: false + - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4 + with: + node-version: '22' + - run: ./dev/local-backend/dev prepare-signing + - run: npm ci --ignore-scripts + working-directory: dev/local-backend/.sources/signing + - run: npm run compile + working-directory: dev/local-backend/.sources/signing + - run: npm test + working-directory: dev/local-backend/.sources/signing + + backend: + name: Local backend and bootstrap + runs-on: ubuntu-latest + timeout-minutes: 25 + steps: + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4 + with: + persist-credentials: false + - uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5 + with: + python-version: '3.9' + - run: python3 -B -m unittest discover -s dev/local-backend -p 'test_*.py' + - run: ./dev/local-backend/dev up + - run: ./dev/local-backend/verify-local.sh --persistence --boundaries + - name: Stop containers and retain volumes + if: always() + run: ./dev/local-backend/dev stop diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000000..569fc61460 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,113 @@ +# Contributing to Bitcoin Keeper + +Contributions are welcome through forks and pull requests. Keeper +maintainers are responsible for code and security review, assessing privacy risks, +deciding whether to accept changes, and controlling merges and public releases. +Passing automated checks or submitting a working build does not constitute approval. + +## Set up a local workspace + +Fork this repository on GitHub and clone your fork for contributions. You do not +need write access to the Keeper repository or production credentials. Follow the +[local development guide](dev/local-backend/README.md) for tool versions, Docker +backend setup, Android Studio/Xcode builds and troubleshooting. Docker runs the +backend; native SDKs and the mobile app run on your host. + +The [SigningServer source](https://github.com/KeeperCommunity/SigningServer) is +public and can be cloned, installed, built, tested and contributed to on its own. +The full Docker app/backend setup uses the public +[Relay](https://github.com/KeeperCommunity/Relay) and SigningServer sources; +it requires no repository invitation. Optional private development resources +follow a separate [access process](dev/local-backend/ACCESS.md). Access approval +is separate from acceptance of contributed code. + +The guide's [capability table](dev/local-backend/README.md#what-works-locally) lists +supported local flows and unavailable integrations. The +[verification record](dev/local-backend/VERIFICATION.md) distinguishes tested +behavior from failures and untested platforms. Start with disposable testnet +wallets. Store accounts, release signing keys and production backend access are +not prerequisites for contributing. + +## Develop a focused change + +1. Find an existing issue or describe the problem in a new issue. For a suspected + vulnerability, use the private process in [SECURITY.md](SECURITY.md) instead. +2. Create a branch in your fork. Use the base branch associated with the issue; + if none is specified, use the repository's default development branch, + `sprint`. Do not assume a release tag includes every unreleased feature. +3. Capture the change's scope and acceptance criteria using the repository's + OpenSpec workflow. The CLI is included in the locked development dependencies: + + ```sh + yarn openspec new change your-change-name + yarn openspec status --change your-change-name + yarn openspec instructions proposal --change your-change-name + ``` + + Follow the artifact instructions for proposal, specs, design and tasks. Read + [OpenSpec configuration](openspec/config.yaml); for UI changes, also read + [DESIGN.md](DESIGN.md). The repository's + [change-engineer workflow](.github/agents/change-engineer.agent.md) describes + planning, implementation and archiving. Respect any explicit repository-owner + restrictions on committing, pushing or publishing when using coding agents. +4. Implement the agreed scope, reuse existing components and keep unrelated + formatting, dependency upgrades, feature reconciliation and version bumps out + of the change. Maintainers coordinate version numbers and the + [Android](android/fastlane/README.md) and [iOS](ios/fastlane/README.md) release + workflows separately from local development. + +For backend development, bootstrap creates ignored checkouts under +`dev/local-backend/.sources/`. Save backend work in a branch/patch and submit it to +the appropriate backend repository; an app PR does not include those ignored +edits. Changes to packaged adapters must update the corresponding SHA-256 entries +in `sources.lock.json` and be verified from freshly prepared sources. Do not +package a generated checkout, database or signing volume in a PR. + +## Test and report the result + +Run checks relevant to the changed behavior. These commands run from the app root: + +| Change | Checks | +| --- | --- | +| App logic | `yarn test --runInBand` (or the relevant Jest test paths) and `yarn lint` | +| User flow or native code | Build the affected development app and exercise the acceptance scenarios; use/update relevant Maestro flows under `flows/` | +| Local backend/bootstrap | `python3 -B -m unittest discover -s dev/local-backend -p 'test_*.py'` and `./dev/local-backend/dev verify` | +| Persistence or isolation | `./dev/local-backend/verify-local.sh --persistence --boundaries` on a disposable, otherwise idle Compose project | +| OpenSpec | `yarn openspec validate your-change-name --strict` | + +Shared mobile changes should be checked on both Android and iOS. If a platform, +hardware device or integration is unavailable, say so; maintainers decide what +additional testing is required before acceptance. A local backend smoke test does +not validate recovery, transaction signing or every mobile feature. + +Report the commands, commit, platform/device, actual results and relevant redacted +evidence. Keep **passed**, **failed/blocked** and **not run** separate. Describe +existing failures rather than hiding them or reporting an unexecuted check as a +pass. Screenshots can support manual verification but do not turn a failed +automated assertion into a passing automated test. + +## Submit and review + +Open a pull request from your fork and complete the supplied template. Link the +issue and OpenSpec artifacts, explain the behavior change and provide the test +results. Review the diff before submission: exclude local environment files, +generated dependencies, databases, keys, wallet backups and signing credentials. +Use [SECURITY.md](SECURITY.md) for private vulnerability details. + +Keeper maintainers review correctness, product fit, security/privacy, dependencies +and test evidence. They may request revisions or additional tests, and decide +whether a change is accepted. Only authorized maintainers merge and release +official builds. Contributors do not need release permissions to participate. + +Some existing CI checks use maintainer-managed services or secrets. If a check is +unavailable on a fork PR, report it in the PR so maintainers can arrange review; +do not request production secrets or treat an unavailable check as passing. +This document defines review responsibilities; it does not claim that GitHub +branch protection or approval rules have been configured or verified. + +GitHub runs app tests and separate public backend/bootstrap checks. The existing Electrum/wallet integration +tests use public testnet nodes and fee services; an internet connection is needed. +Coverage, where generated, measures the configured Electrum/wallet source +directories, not every app screen. SonarCloud analysis on the development branch +is deferred; passing tests are not a security analysis pass. See [CI notes](dev/local-backend/CI.md) +for the branch-specific checks, deferred analysis work and evidence limits. diff --git a/Readme.md b/Readme.md index 3f1f9d298a..70ed94e5c0 100644 --- a/Readme.md +++ b/Readme.md @@ -60,6 +60,16 @@ Refer to platform-specific documentation for detailed setup requirements. yarn install ``` +## Contributing and local backends + +See [CONTRIBUTING.md](CONTRIBUTING.md) for the contribution process and +[the local development guide](dev/local-backend/README.md) for a reproducible +backend setup using public [Relay](https://github.com/KeeperCommunity/Relay) +and [SigningServer](https://github.com/KeeperCommunity/SigningServer) sources. +The local stack uses disposable testnet fixtures and needs no production +credentials. Keep native app builds on your host; the backend runs in Docker. +Report suspected vulnerabilities through [private reporting](SECURITY.md). + ## Build and Run ### Varients diff --git a/dev/local-backend/.env.example b/dev/local-backend/.env.example new file mode 100644 index 0000000000..ed62df3b1f --- /dev/null +++ b/dev/local-backend/.env.example @@ -0,0 +1,5 @@ +# Copy to .env only when changing the defaults. No secrets required. +COMPOSE_PROJECT_NAME=keeper-contributor +KEEPER_RELAY_PORT=3000 +KEEPER_CHANNEL_PORT=4002 +KEEPER_SIGNING_PORT=3003 diff --git a/dev/local-backend/.gitignore b/dev/local-backend/.gitignore new file mode 100644 index 0000000000..53a25a248d --- /dev/null +++ b/dev/local-backend/.gitignore @@ -0,0 +1,3 @@ +.sources/ +.env +__pycache__/ diff --git a/dev/local-backend/ACCESS.md b/dev/local-backend/ACCESS.md new file mode 100644 index 0000000000..d9012a6548 --- /dev/null +++ b/dev/local-backend/ACCESS.md @@ -0,0 +1,24 @@ +# Optional private development resources + +The public [app](https://github.com/KeeperCommunity/bitcoin-keeper), +[Relay](https://github.com/KeeperCommunity/Relay), and +[SigningServer](https://github.com/KeeperCommunity/SigningServer) can be cloned, +built, tested and contributed to without a Keeper invitation. The local Docker +setup in [README.md](README.md) uses only those public repositories and +disposable development identities. Production credentials are never needed. + +Some assigned work may need separate private repositories, provider dashboards, +or test infrastructure. Ask the Keeper maintainer coordinating that work in your +existing private conversation. Include your GitHub username, task and the exact +resource needed. Do not post credentials or private access requests in a public +issue. If you do not have a maintainer contact, use the +[community contact](../../Readme.md#community) to reach one privately. + +Maintainers review the work and grant only the needed permissions after checking +that the resource is ready to share. Approval covers the development access, not +every local command or proposed code change. Record invitation acceptance and +setup success in the internal development backlog. A contributor must use their +own GitHub identity; maintainers must not share personal tokens. + +Security reports follow [SECURITY.md](../../SECURITY.md). Code acceptance, +merges, production access and releases remain separate maintainer decisions. diff --git a/dev/local-backend/CI.md b/dev/local-backend/CI.md new file mode 100644 index 0000000000..66064cccd7 --- /dev/null +++ b/dev/local-backend/CI.md @@ -0,0 +1,54 @@ +# Contributor checks and analysis + +## Public backend CI + +The app's local backend workflow pins the public +[Relay](https://github.com/KeeperCommunity/Relay) and +[SigningServer](https://github.com/KeeperCommunity/SigningServer). It requires no +private repository token or production credential. The default branch runs +`Public backend setup` for the packaged adapter, bootstrap and disposable API +checks. Earlier Linux backend runs failed while fetching the private Relay; +those historical runs do not establish the result for this public pin. +Code acceptance and releases remain maintainer decisions. + +## Active checks + +The default branch's existing `Run Tests` workflow runs app tests. The new +`Public backend setup` workflow runs a separate public SigningServer adapter +fetch/install/compile/test job and full local backend bootstrap, API, +persistence and network-boundary checks. Neither job uses hosted credentials. +Pull requests are checked against their proposed merge with the target branch. +The development branch's `Contributor checks` workflow additionally publishes +app test coverage; its results are tracked with PR #7014. + +Where generated, Jest's LCOV measures only the +Electrum/wallet directories listed in `jest.config.js`; this is not whole-app +coverage. Test success is evidence about the exercised cases, not a security +clearance. Maintainers retain security review and acceptance responsibility. + +## SonarCloud deferred on 26 September 2026 + +The project owner chose to defer SonarCloud on the development branch. That job +was removed from its active workflow; app tests and an LCOV artifact remain there. `sonar-project.properties` is +retained for future restoration, including the report path and quality-gate wait +setting. No analysis or quality-gate pass is claimed, and no threshold has been +lowered. GitHub protection settings were not changed; if a required Sonar status +blocks merging, a maintainer must reconcile that setting with this decision. + +Before deferral, PR #7014's original and updated scanners failed while retrieving +the pull request, before analysis. Public project metadata lists the repository +as `bithyve/bitcoin-keeper`, while this PR lives in +`KeeperCommunity/bitcoin-keeper`. This suggests a stale binding; an administrator +needs to verify the exact access/configuration problem when analysis is restored. + +Deferred follow-up: inspect the project's Repository binding and GitHub app +access, confirm project/organization keys and the secret, restore a pinned scanner +that consumes LCOV and waits for its quality gate, then review the actual result. +Keep analyzer credentials away from fork code. See Sonar's +[repository binding instructions](https://docs.sonarsource.com/sonarqube-cloud/managing-your-projects/administering-your-projects/changing-binding). + +The last existing `sprint` analysis (17 September, commit `1adf4f663`) reported a +failed quality gate: 0% new-code coverage, reliability rating C, 5.1% duplication +and 0% reviewed security hotspots, using a September 2024 previous-version +baseline. These are historical findings, not this PR's analysis. Deferral does +not resolve them or replace Keeper's review. diff --git a/dev/local-backend/HANDOFF.md b/dev/local-backend/HANDOFF.md new file mode 100644 index 0000000000..8d2fe447e2 --- /dev/null +++ b/dev/local-backend/HANDOFF.md @@ -0,0 +1,61 @@ +# Independent contributor setup check + +Use this full-stack checklist with the public Relay and SigningServer sources. +SigningServer-only work can follow +[its independent setup](README.md#signingserver-without-relay-access). A maintainer's working Mac does not establish that the guide works +on another developer's machine. This check does not authorize code acceptance or +an official release. + +## Start clean + +1. Use an anonymous fresh app clone at the exact commit + supplied by the maintainer. Record `git rev-parse HEAD`. Do not copy another + developer's `.env`, databases, signing volume, account credentials or key files. +2. Follow [README.md](README.md) for the toolchain and a + running Docker engine. If other Keeper projects are running, choose a distinct + Compose project and three unused ports as documented there. +3. Run `./dev/local-backend/dev up`. It must prepare the pinned sources, start the + services and pass the local API checks without production credentials. +4. Run `./dev/local-backend/verify-local.sh --persistence --boundaries` while no + other test is using this project's services. It recreates this project's + containers while retaining its volumes. Both persistence and rejection of + mainnet/hosted database settings must pass. +5. Run `python3 -B -m unittest discover -s dev/local-backend -p 'test_*.py'`. +6. Follow the native development build instructions for the platforms available + on this machine. iOS requires a Mac and Xcode. Record unavailable platforms as + untested, not passed. +7. In the development app, create only a disposable local test fixture, complete + passcode/onboarding, reach Mobile Wallet and More Options, then relaunch and + unlock. Confirm that the app uses the local environment. Do not import a real + wallet or move real funds for this check. +8. Stop the selected stack with `./dev/local-backend/dev stop`. Retain the volumes + for follow-up unless you intentionally choose to discard this test fixture. + +## Send the result privately to the coordinating maintainer + +Copy this summary into the existing private conversation. Maintainers keep it in +the internal development backlog, not a new public administrative issue. + +```text +App commit: +Backend revisions (from dev/local-backend/sources.lock.json): +Host OS and architecture: +Docker/Compose, Node/Yarn, Python, Java and Xcode versions as applicable: +Fresh clone and new project/volumes: yes/no (explain reused state) + +PASS: +FAIL or BLOCKED (exact step and sanitized error): +NOT TESTED: +Guide corrections or undocumented steps needed: +Native platform(s), simulator/emulator and onboarding/relaunch result: +``` + +Do not attach recovery words, private keys, authentication tokens, receipts, +databases, captured emails or unreviewed full logs. Report security findings via +[SECURITY.md](../../SECURITY.md). Ordinary reproducible user-facing defects can +have public product issues after removing private details. + +Passing this checklist proves only the listed local setup and smoke checks. +Purchases, push delivery, scheduled inheritance, hardware wallets, transaction +signing and recovery correctness require their own acceptance evidence; see the +[capability table](README.md#what-works-locally) and [verification record](VERIFICATION.md). diff --git a/dev/local-backend/README.md b/dev/local-backend/README.md new file mode 100644 index 0000000000..0968bf0885 --- /dev/null +++ b/dev/local-backend/README.md @@ -0,0 +1,173 @@ +# Contributor development environment + +Run MongoDB, Keeper relay/channel and the testnet signing backend in Docker. Run Metro, Android Studio and Xcode on your host. Hosted development services have been retired; this setup needs no production backend or release credentials. + +For choosing a change, OpenSpec, test reporting and submitting a pull request, see +[Contributing](../../CONTRIBUTING.md). Keeper maintainers own security review, +acceptance, merges and official releases. + +## Prerequisites + +Both [Relay](https://github.com/KeeperCommunity/Relay) and +[SigningServer](https://github.com/KeeperCommunity/SigningServer) are public. +The full local Docker stack uses pinned revisions of these clean public sources +and needs no repository invitation or production credentials. Optional private +development resources have a separate [access process](ACCESS.md). + +- Git, Python 3.9+, Docker Engine with Compose v2.20+ (or a newer Compose release). On macOS use Docker Desktop or Colima; Linux can use Docker Engine. Start the engine before setup. Reserve about 3–4 GB for Docker and run native builds sequentially on a 16 GB Mac. +- Anonymous GitHub access to both public backends. First setup downloads sources, container images and dependencies. Subsequent starts can reuse them. +- Mobile: Node 20.19.4+ (Node 22 is a suitable supported baseline), Yarn Classic 1.22.22. Use the committed `yarn.lock`. +- Android: Android Studio, JDK 17, SDK platform 36, build tools 35.0.0, NDK 27.1.12297006, CMake 3.22.1 and platform tools. Set `JAVA_HOME` and `ANDROID_HOME`. These versions come from this app's Gradle files. +- iOS: macOS, Xcode with an iOS simulator runtime, Ruby 3.3.0 (`.ruby-version`) and Bundler. Use the root `Gemfile.lock` and `ios/Podfile.lock`. + +Earlier native validation on this Mac used Apple Silicon, Xcode 26.4, Node 25.9.0, Yarn 1.22.22, JDK 17 and Ruby 3.3.0. App tests also passed in GitHub Linux CI on Node 22. Full contributor setup on Intel Macs, Linux and Windows/WSL still needs independent verification; iOS builds require macOS. See [verification](VERIFICATION.md) for actual results and limitations. + +Native tool installation: [React Native 0.83 environment guide](https://reactnative.dev/docs/0.83/set-up-your-environment). Follow the repo-specific Android versions above when they differ from that guide. + +## First start + +Clone the public default branch: + +```sh +git clone https://github.com/KeeperCommunity/bitcoin-keeper.git +cd bitcoin-keeper +``` + +`dev up` fetches only public backend sources. + +From this app checkout's root: + +```sh +./dev/local-backend/dev doctor +./dev/local-backend/dev up +./dev/local-backend/dev env +yarn install --frozen-lockfile --non-interactive +``` + +`up` fetches the exact backend Git revisions, applies the local development adapters, builds containers, waits for readiness and runs real disposable API checks. `env` creates `.env.local` with restrictive permissions; it refuses to overwrite an existing different configuration. Existing `.env` and production files are not used or modified. + +## SigningServer without Relay access + +For SigningServer changes that do not need the mobile app or Relay, use the +[public repository's instructions](https://github.com/KeeperCommunity/SigningServer#install-and-verify): + +```sh +git clone https://github.com/KeeperCommunity/SigningServer.git +cd SigningServer +npm ci --ignore-scripts +npm run compile +npm test +``` + +Use Node 22 and report the exact tested commit. The server's unit tests use +disposable local fixtures; they do not establish full app/backend integration or +live signing behavior. To check this app's local adapter against its pinned +public source, run `./dev/local-backend/dev prepare-signing` from the app clone. +That command checks the adapter digests and applies it under ignored +`.sources/signing/` without fetching Relay or starting Docker. Submit upstream +SigningServer changes to its own repository. Full `dev up`, API checks and native +app setup use the public Relay source. + +The JavaScript install hook only installs the repo's Node compatibility shims. Native dependencies are separate explicit steps below. It no longer runs CocoaPods on Android-only machines or rewrites `android/local.properties` with a Mac-specific path. + +If default ports 3000/4002/3003 are occupied, copy `dev/local-backend/.env.example` to `dev/local-backend/.env`, set distinct ports and a unique project name, then run the commands above. Use the same settings for every invocation. Multiple copies using the same Compose project name share containers and volumes; give each independent environment a unique name. `.env.local` must match those ports. Commands work from any directory except the native/Yarn commands, which assume the app root. + +## Android + +Create and boot an emulator through Android Studio. Use an ARM64 image on Apple Silicon, x86_64 on Intel/Linux. Use `adb devices` to select its serial. In one terminal at the app root: + +```sh +yarn start +``` + +In another terminal: + +```sh +./dev/local-backend/dev android-forward --serial YOUR_SERIAL +ENVFILE=.env.local ./android/gradlew -p android app:assembleDevelopmentDebug -PreactNativeArchitectures=arm64-v8a --max-workers=2 +adb -s YOUR_SERIAL install -r android/app/build/outputs/apk/development/debug/app-development-debug.apk +adb -s YOUR_SERIAL shell am start -n io.hexawallet.keeper.development/io.hexawallet.keeper.MainActivityDefault +``` + +Use `-PreactNativeArchitectures=x86_64` for an x86_64 emulator. `ANDROID_HOME` lets Gradle locate the SDK without a machine-specific committed `local.properties`. Android debug signing uses the disposable debug keystore already in the repository. No release signing key is needed. + +Keep `localhost` in `.env.local`: the app's Android cleartext policy allows it. `android-forward` connects the selected device to Metro and the configured backend ports; rerun after restarting the device. A physical Android device can use the same USB forwarding, but physical-device validation is a separate step. + +## iOS + +Select Ruby 3.3.0 with your Ruby manager, then from the app root: + +```sh +bundle install +(cd ios && BUNDLE_GEMFILE="$PWD/../Gemfile" RCT_NEW_ARCH_ENABLED=1 bundle exec pod install) +xcrun simctl list devices available +``` + +Use the root Gemfile explicitly; the `ios` directory has a different Gemfile. CocoaPods may refresh checkout-path-dependent Hermes podspec checksums on initial installation; review any lockfile diff and do not treat dependency version changes as routine setup. + +Start `yarn start` in a separate terminal. You can inspect the workspace in Xcode (`ios/hexa_keeper.xcworkspace`, scheme `hexa_keeper_dev`). Use the command below for local builds so `ENVFILE` reaches every native build phase; simply clicking Run with the default scheme settings does not select `.env.local`: + +```sh +ENVFILE=.env.local xcodebuild -workspace ios/hexa_keeper.xcworkspace -scheme hexa_keeper_dev -configuration Debug -destination 'platform=iOS Simulator,id=YOUR_SIMULATOR_UDID' -derivedDataPath ios/build/local-dev build +xcrun simctl boot YOUR_SIMULATOR_UDID +xcrun simctl install YOUR_SIMULATOR_UDID ios/build/local-dev/Build/Products/Debug-iphonesimulator/hexa_keeper_dev.app +xcrun simctl launch YOUR_SIMULATOR_UDID io.hexawallet.hexakeeper.dev +``` + +Skip `simctl boot` if already booted. Keep simulator signing enabled so the app retains required entitlements. Store publishing/signing is separate from contributor development; see the release documentation. + +## Daily use and verification + +```sh +./dev/local-backend/dev status +./dev/local-backend/dev verify +./dev/local-backend/dev logs +./dev/local-backend/dev stop +``` + +`stop` removes containers but retains both named volumes. A later `up` restores the environment. To check persistence and configuration boundaries explicitly: + +```sh +./dev/local-backend/verify-local.sh --persistence --boundaries +``` + +This recreates only the selected Compose project's containers, retains its volumes, and compares the same app record and testnet signing public key. It also checks startup rejection of mainnet and hosted databases. Run when no other test is using that project. Ordinary `verify` does not restart containers. Every verify run adds a disposable app/signer fixture to the local database; no testnet coins are needed and no transaction is sent. Tokens and private keys are not printed. + +For native smoke testing: launch the development app, complete onboarding with a disposable wallet, unlock, open Wallets and More Options, and select testnet before testing network functionality. An API smoke pass does not prove wallet backup/recovery or a real signing transaction. + +## What works locally + +| Capability | Scope | +| --- | --- | +| Mongo, relay app records | Local persistence and API create/read checks | +| Channel | Socket.IO connection handshake | +| Server Key | V3 testnet setup, public-key derivation, valid/invalid 2FA and persistence | +| Email | Captured inside signing volume at `/local-data/outbox.jsonl`; never delivered | +| Push, purchases, Ask Keeper AI, faucet, price feeds | Unavailable; relay reports unsupported routes explicitly | +| Scheduled signing / inheritance jobs | Disabled; timer-dependent flows are not covered | +| Blockchain / Electrum | Separate mobile internet connection; not provided by this stack | +| Legacy V2 Server Key | Not supported by this acceptance check; a V2 setup probe terminated the pinned upstream process (see verification notes) | + +The relay health field `signing: false` describes the relay, not the separate signing service. Use each service's health endpoint. No production service deployment is performed. + +## Sources, data and network boundaries + +`sources.lock.json` pins both public backend revisions and the SigningServer adapter SHA-256 checksums. Relay's public source already includes local mode, so it needs no app-side adapter. The Node, Mongo and nginx base images are pinned by digest. Backend dependency installs use frozen Yarn / npm lockfiles; OS packages in the relay build still use Debian repositories, so this is a reproducible development workflow, not a bit-for-bit reproducible image build. + +`adapters/signing/` contains the local-mode modifications for the pinned public SigningServer source. `.sources/` is generated and ignored. Bootstrap never overwrites an existing checkout; reruns preserve local backend edits. After editing a backend, rerun `up` to rebuild. To change pins/adapters, save your backend edits and move the generated checkout aside first. Do not deploy the development adapter to production. + +Only the gateway publishes host ports, all on `127.0.0.1`. Mongo, relay and signing share an internal network with no external egress; the gateway also has a host-access network. Compose waits on service health, following [Docker's startup ordering](https://docs.docker.com/compose/how-tos/startup-order/). Build-time dependency downloads still use the internet. Mobile native integrations are outside Docker; this is not a fully offline mobile mode. + +Signing identities are randomly generated per signing volume and stored with restricted permissions. Use disposable test wallets. Keep the Mongo and signing volumes together: deleting signing keys invalidates their stored Server Key records. There is deliberately no automatic reset/delete command. `docker compose down --volumes` is destructive and not part of normal setup. Do not distribute databases, keys, email captures, `.env` files or native signing credentials with the source. + +## Troubleshooting + +- Docker unavailable: start Docker Desktop or Colima, then rerun `doctor`. +- Backend source fetch failure: check the public pinned revision and network connection; no application secret is needed. +- Stale source/adapter marker: save edits and move the relevant `.sources/relay` or `.sources/signing` directory aside. Bootstrap refuses to overwrite it. +- Health succeeds but a feature fails: consult the capability table; cloud integrations are not restored by local startup. +- App cannot reach local services: check `.env.local`, rebuild with `ENVFILE=.env.local`, and rerun Android port forwarding. Changing an env file requires a native rebuild. +- Xcode reports a missing Node executable: inspect the ignored `ios/.xcode.env.local`; CocoaPods may have cached a version-specific path removed by a Node upgrade. Update its `NODE_BINARY` to your current executable and rebuild. +- If several Metro servers are running, select the one for this checkout on each device. Android emulator defaults can bypass USB forwarding via `10.0.2.2`; set its debug server host to `localhost:8081` when using `adb reverse`. +- iOS pod install fails under system Ruby: select the repo's Ruby version and root Bundler environment. Do not run an unrelated globally installed CocoaPods. +- Memory pressure: build iOS and Android sequentially, limit Gradle workers, and shut down unused simulators without erasing their data. diff --git a/dev/local-backend/VERIFICATION.md b/dev/local-backend/VERIFICATION.md new file mode 100644 index 0000000000..1205569afc --- /dev/null +++ b/dev/local-backend/VERIFICATION.md @@ -0,0 +1,273 @@ +# Contributor setup verification + +## Sprint contributor package — 7 October 2026 + +This focused `sprint` contributor package uses the same public backend pins and +adapter digests as the earlier sprint PR #7014 and main PR #7026. It changes the +developer setup hook and adds contributor/security documentation and a +public-backend CI workflow; it does not modify mobile, dependency or release +files. Backend/bootstrap verification for this exact branch is recorded with +its PR checks. Native app builds must be assessed against the selected app +branch and platform separately. +The dated sections below preserve earlier sprint and public-source evidence; +historical private-source failures are not the current public backend result. + +## Public Relay transition — 7 October 2026 + +- Clean-history [Relay](https://github.com/KeeperCommunity/Relay) is public at + `3e1a01bd2fd5381d8546229922c431ce50bedad4`; the legacy credential-bearing + Relay repository remains private. GitHub private vulnerability reporting is + enabled on the public repository. +- An anonymous Relay clone at that exact commit passed a frozen Node 22 install, + compilation, 104 tests across nine suites with disposable MongoDB, current + source and full-history secret scans, and the runtime dependency audit. A + separate local Compose run served ready/local health, persisted a synthetic + app record and returned 503 for an unavailable hosted integration. Its + disposable containers and volumes were removed. +- Sprint PR #7014's public Relay pin and public SigningServer pin were fetched with + Git credential helpers disabled. Six bootstrap regression tests passed. The + isolated full stack passed health, Socket.IO, unsupported-route behavior, + synthetic app record create/read, and V3 testnet signer authorization and + valid/invalid 2FA. No production credentials or funded wallets were used. +- The optional persistence/boundary rerun stopped during Docker network + recreation with an engine input/output error after the host filled its disk. + That rerun is **not passed**. Free space was recovered, but no further Docker + build is being run while concurrent mobile QA uses the machine. CI on the + updated app commit and an independent developer/machine remain pending. +- No new native mobile build, physical-device check, hosted provider delivery, + real signing transaction, or production deployment is claimed by this + contributor source-pin change. + +## Public SigningServer follow-up — 6 October 2026 + +- The pinned SigningServer source is now the public + `KeeperCommunity/SigningServer` revision + `b82dc4f4a8f75676b70b12545d59c7906556a92b`. Its local adapter checksum + check and patch application pass. With Node 22, the adapted source passes + `npm ci --ignore-scripts`, `npm run compile` and `npm test` (2 suites, 108 tests). + The new independent GitHub SigningServer adapter job passes those same steps. +- SigningServer's previously cancelled current-source readiness job was rerun at + its public head: current-source and history scans both pass on the rerun. + Its own public test workflow also passes. Dependency review remains open; + successful source scans and tests are not security acceptance. +- A separate anonymous app checkout at app commit + `41e482ec97ebffb0beff43ed807b1205fb26ed8e` used an empty home and package + cache, disabled Git credential helpers and prompts, and copied no backend + source or environment files. `dev prepare` stopped at private Relay as expected. + `dev prepare-signing` fetched the public pin and applied its adapter without + Relay. `dev env` created a local-only file with mode `0600`; six bootstrap + regression tests passed. No full Docker stack or native build was possible + without Relay access. +- A separate anonymous SigningServer clone, with an initially empty package + cache and no Git credentials, passed `npm ci --ignore-scripts`, + `npm run compile` and `npm test` (2 suites, 108 tests). The local anonymous app + Yarn install was stopped before completion when this Mac had about 1.5 GB free; + no local anonymous app JavaScript install or Jest pass is claimed. On the same + app commit, GitHub Linux CI passed frozen Yarn install, 17 Jest suites / 183 + tests, strict active OpenSpec validation and LCOV upload. Its full backend job + passed bootstrap tests but failed fetching private Relay; later API/persistence + checks were skipped. These CI results do not establish an independent + full-stack checkout or native app acceptance. +- Relay remains private and its publication is deferred. Safe CI Relay access, + independent full-stack setup, remaining dependency/security review and + maintainer acceptance remain open. No production deployment or credential + change was part of this follow-up. + +## Earlier evidence — 26–28 September 2026 + +## Contributor handoff follow-up — 28 September 2026 + +- Removed the public contributor-access issue template. Access instructions now + use private intake/internal tracking, matching the owner's policy. Added + `HANDOFF.md` for independent-machine setup and separate pass/fail/untested reports. +- Five bootstrap regression tests, strict contributor OpenSpec validation, ten + local documentation links and diff whitespace checks: PASS. +- Restarted this Mac's existing Colima profile and ran the documented `dev up` + against the same pinned sources, existing disposable project/volumes and build + caches. Health, Socket.IO, explicit unsupported-route failure, app create/read + and V3 testnet authorization/2FA: PASS. +- `verify-local.sh --persistence --boundaries`: PASS, including retained app and + signing identity after recreation and rejection of mainnet/hosted databases. + This rerun is not a fresh-machine, native UI or transaction-signing test. +- Published baseline app head `434cd7c405d8723b3c21c7ce6e82745b9b254517`, GitHub + run `36240719417`: app tests/coverage PASS; backend bootstrap unit tests PASS; + backend startup FAIL and subsequent integration checks SKIPPED. CI access is + still unresolved; these CI results apply to that baseline, not a later head. +- Independent-machine verification, supported mobile Server Key acceptance, + backend readiness acceptance and safe CI source access remain open. No new + native build, full UI automation, provider-purchase or security-clearance pass + is claimed by this follow-up. + +## Readiness before private access + +The owner requires the same repository hygiene before private contributor access +as before public publication. Backend cleanup is published in private draft PRs. +This app now pins those updated revisions and its refreshed adapters have passed +a fresh Docker setup on this Mac. Broader backend access still awaits history +hygiene, credential handling, remaining dependency/security review and acceptance. +Detailed findings remain private. + +## Fresh backend readiness integration + +Updated pins: relay `5f59876b43fba1be9091657a6bf3389ce43831d0`, signing +`5ad45119ab014ea58efecabd54702b488d8fd254`. New app worktree, newly fetched +backend sources and new Docker volumes; host image/package caches and existing +GitHub read access reused. Project `keeper-readiness-proof` used ports +23000/24002/23003, preserving other development projects. + +- Source fetch, adapter checksums/application and container builds: PASS. +- All four service health checks, Socket.IO handshake, explicit unsupported-route + response and persisted app record create/read: PASS. +- V3 testnet Server Key setup, authorization and valid/invalid 2FA: PASS. +- Container recreation preserves the same app record and derived signer public + key: PASS. Mainnet/hosted database startup rejection: PASS. +- Five bootstrap regression tests: PASS. Active contributor OpenSpec validation: + PASS. Both backend test jobs and current-source scans pass in their own CI; + their history scans remain failing, tracked privately. +- Updated native development builds: iOS PASS after replacing a stale local + Node path; Android PASS after selecting the installed JDK 17 (976 tasks, 58 + executed, 918 cached). Both builds embed the isolated 23000/24002/23003 backend + configuration. Native app source is the previously verified sprint integration; + this follow-up changes only backend pins/adapters and documentation. +- Relay container compilation initially failed from memory exhaustion. Importing + only the existing Android Publisher v3 client reduced measured local compiler + memory to about 422 MB; the final image builds successfully with a 1 GB Node + heap. This retains the same provider API/library version. +- Final image startup/API/V3/2FA and persistence/configuration-boundary checks: + PASS. The earlier iOS onboarding record survives recreation. No actual store + purchase, signing transaction or backup/recovery correctness test is claimed. +- Native local smoke: iOS passcode/onboarding, Mobile Wallet and More Options + PASS using Maestro steps plus screenshot inspection. Initial Start New selector + failed despite the visible control; a visually guided tap continued the flow. + Relaunch/unlock after final backend restart PASS after handling the notification + prompt and repeated introductory/recovery-backup reminders; More Options assertion + passed. The original uninterrupted automation flows remain recorded as failures. +- Android: disposable emulator user 10 preserves the prior user's app data. Fresh + passcode/onboarding, Mobile Wallet and More Options PASS using ADB assertions + plus a visually guided Next tap. The first attempt used the wrong Metro; after + selecting the matching checkout and resetting only this new user's disposable + fixture, the relay created the native app record with HTTP 200. UIAutomator could + not reach idle during the animated Next screen; that original assertion timed + out, while visual navigation and subsequent assertions passed. An earlier + attempt also overlapped the deliberate backend restart and was retried. +- Native app records are confirmed in local MongoDB. This is onboarding/connectivity + evidence, not real signing, recovery, purchase or cloud-integration acceptance. +- Full Linux Docker CI still lacks private-source access. Independent-machine + setup and Keeper security acceptance remain pending. These commits are draft + review inputs and are not an approval to deploy production backends. + +## Access policy and analysis update + +The owner chose approved contributor access and deferred SonarCloud on 26 +September. [Access instructions](ACCESS.md) and a GitHub request template now +cover profile/context review, approved resource grants and setup confirmation. +Local edits need no individual approval. No invitations or CI credentials were +provisioned by this documentation change. SonarCloud is removed from active CI; +its earlier failures below remain historical evidence, not a passing analysis. +App tests and coverage remain active; private-source access still blocks backend +CI. Independent contributor setup remains pending. + +This documentation/workflow update passed YAML and issue-template parsing, +21 local documentation link checks, all five bootstrap tests and strict validation +of both active OpenSpec changes. Native builds were not repeated for this update; +the existing build results below apply to the unchanged app/native code. + +## Integration onto sprint + +The current PR is being integrated onto `sprint` commit `1adf4f663`; the release +baseline results below are historical and are not a substitute for these checks. +Fresh worktree and node_modules, host SDKs/caches reused: + +- Frozen Yarn installation: PASS. +- Jest: 17 suites / 183 tests PASS; process exits normally after adding test-only + Electrum/HTTP teardown. LCOV is generated (coverage scope is the existing + Electrum/wallet directories). +- Five bootstrap regression tests (including denied source access) and both active + OpenSpec changes: PASS. +- Broad validation of all legacy OpenSpec specs: 17 pre-existing specs failed; + CI validates active changes, not these legacy documents. +- Ruby lockfile constraints synchronized with Gemfile without gem version changes. +- Initial CocoaPods install failed because `sprint` locked openiap 1.2.22 while + its existing react-native-iap 16.5.0 requires 3.4.0. Targeted `pod update openiap` + aligned NitroIap/NitroModules/openiap with existing JavaScript dependencies; + 142 pods installed successfully. The updated lockfile is part of this PR. +- Fresh backend API, V3 authorization/2FA, persistence and configuration boundary + checks: PASS using this Mac's existing backend source access. +- Android development build: PASS (976 tasks, 5m20s). Install over the disposable + development app, local backend configuration and testnet passcode-screen launch: + PASS. Fresh signed iOS Debug simulator build: PASS (142 pods). Simulator install and + launch to the passcode screen: PASS by screenshot inspection. These launch + checks are not complete onboarding, recovery or transaction tests. +- GitHub Linux app job: PASS, 17 suites / 183 tests, active OpenSpec validation and + LCOV artifact upload. Measured line coverage is 57.37% (1451/2529) within the + existing configured source scope; no 80% coverage claim is made. +- GitHub Linux bootstrap unit tests: PASS. Docker startup: FAIL during source + fetch because both backend repositories were private at the time and CI lacked + access. Current full-stack CI still requires private Relay access. This is not + a passing full-stack setup check. +- SonarCloud: both the original and updated scanner failed before analysis; repository binding + still points at the old organization. See [CI notes](CI.md). No analysis pass + or security clearance is claimed. + +This integration preserves `sprint`'s app version fields; the separate 2.5.15 +release-version work is not imported or reverted by this contributor PR. + +## Test environment + +Fresh HTTPS clone of the published app tag `v2.5.15` (`e79e16b572d6e3472ac94500bf812d937bd89135`), plus this contributor setup change. New app dependency directories and newly fetched backend checkouts; no copied backend env files, databases or signing keys. Existing host SDKs, package caches and container image caches were reused. + +Apple Silicon Mac, macOS 26.6.2, Xcode 26.4/iOS 26.4 simulator, Node 25.9.0, Yarn 1.22.22, Python 3.9, Ruby 3.3.0/CocoaPods 1.15.2, JDK 17.0.19. Docker Engine 29.5.2/Compose 5.5.1 on Colima. Independent Compose project `keeper-contributor-clean`, newly generated volumes, loopback ports 13000/14002/13003. Existing developer/release services and data were preserved. + +## Results + +| Check | Result | +| --- | --- | +| Fetch pinned sources and verify/apply packaged adapters | PASS, both backends fetched from GitHub | +| Advertised `dev up`, including API checks | PASS after correcting the smoke fixture to use current V3 Server Key APIs | +| Mongo, relay, signing, gateway readiness | PASS | +| Socket.IO polling handshake | PASS | +| Relay create/read persisted app record | PASS | +| Unsupported relay integration | PASS, explicit HTTP 503 | +| V3 testnet Server Key setup and public-key format | PASS | +| Invalid authorization, valid 2FA, invalid 2FA | PASS | +| Container removal/recreation with retained volumes | PASS, same app record and derived signing public key | +| Mainnet and hosted Mongo configuration rejection | PASS for relay and signing | +| Actual container network/port inspection | PASS, backend network internal, no database port published, gateway bound only to loopback | +| Rerun setup, existing env preservation, adapter tampering, unknown checkout preservation, duplicate ports | PASS, four regression tests plus real repeated bootstrap | +| Fresh `yarn install --frozen-lockfile --non-interactive` | PASS; no tracked dependency version changes | +| Fresh CocoaPods installation | PASS, 142 pods; see path-dependent checksum note below | +| Fresh Android `assembleDevelopmentDebug` | PASS, 976 tasks executed, 5m17s | +| Android installed local configuration | PASS, development environment and alternate local backend ports verified in generated BuildConfig | +| Android fresh install, passcode creation, onboarding, Mobile Wallet and More Options | PASS via ADB UI assertions and observed emulator controls | +| Android local app record creation | PASS, HTTP 200 and database record confirmation | +| Android Maestro driver | UNAVAILABLE, gRPC connection failed twice before flow execution; no Maestro pass claimed | +| Fresh iOS development simulator build | PASS, signed Debug simulator app from fresh Pods/derived data | +| iOS simulator launch, passcode creation/unlock and local relay connection | PASS, dev app launched; local app record created (two native app records total) | +| iOS onboarding through Wallets | PASS by simulator screenshot inspection; Maestro text assertion failed despite the visible Mobile Wallet tile | +| iOS More Options | PASS, simulator navigation and screenshot inspected | +| Node/Mongo/nginx pinned image manifests | PASS, each includes Linux amd64 and arm64 | +| Existing release version consistency | PASS, unchanged 2.5.15 / Android 622 / iOS 615 | +| Strict OpenSpec validation | PASS | + +Native apps use a separate Metro process on port 8082 during this acceptance run, preserving the existing developer's Metro on 8081. Android's dev-server host is set to localhost with device port 8081 forwarded to host 8082. Ordinary single-checkout instructions use port 8081. + +## Findings and limits + +- The pinned relay and signing backends acknowledge creation before their save callbacks complete. The check polls for record visibility for up to five seconds before dependent reads; it does not hide a rejected verification token or an unexpected response. +- An exploratory legacy V2 Server Key setup returned success and then terminated its signing process. Its source uses a structured verifier schema but writes a string. This is an observed limitation of the pinned backend, not a verified production incident. Current app acceptance uses V3. Legacy V2 workflows remain outside the supported acceptance scope; this packaging change does not repair wallet/signing business logic. +- Initial CocoaPods `--deployment` failed because the Hermes podspec embeds an absolute `HERMES_CLI_PATH`. A normal locked `pod install` refreshed only that podspec checksum to the new checkout path. Comparing old/new podspec JSON confirmed the path is the only difference. No dependency version changed. This generated lockfile difference is not part of the contributor source change. +- Initial iOS UI scripts hit modal timing/selector failures, and one automated unlock was rejected. Retrying the same fixture PIN with waits succeeded. The XCUITest driver also needed a simulator restart after a failed connection; app data was preserved. The Mobile Wallet tile and Start New card were visible in screenshots when text assertions could not find them. These are recorded as automation limitations, not passes of the original complete flows; visual checks are explicitly distinguished from automated assertions. +- React Native/third-party dependencies emit existing peer-dependency, export-resolution and native deprecation warnings. Build success is not a claim that those upstream warnings are resolved. +- This proves a fresh checkout on this Mac, not a second physical machine. Intel/Linux/Windows, physical devices, cloud integrations, hardware wallets, real Server Key transactions, recovery correctness and scheduled inheritance flows remain unverified here. Recovery T-060 and store/source reconciliation are separate tasks. +- Backend image digests and source pins constrain inputs; package caches and Debian package repositories prevent claiming a bit-identical image build. + +## Reproduce checks + +```sh +./dev/local-backend/dev up +./dev/local-backend/verify-local.sh --persistence --boundaries +python3 -B -m unittest discover -s dev/local-backend -p test_keeper.py +npx --no-install openspec validate package-contributor-development --strict +``` + +The acceptance logs and emulator fixtures remain on the maintainer's machine outside the source package. Do not include local databases, generated signing identities, recovery material or captured emails in a source review or handoff archive. diff --git a/dev/local-backend/adapters/signing/files/.dockerignore b/dev/local-backend/adapters/signing/files/.dockerignore new file mode 100644 index 0000000000..e8b30bfc29 --- /dev/null +++ b/dev/local-backend/adapters/signing/files/.dockerignore @@ -0,0 +1,9 @@ +.git +.env* +node_modules +dist +**/*.pem +**/*.p12 +**/*.key +logfile.log +coverage diff --git a/dev/local-backend/adapters/signing/files/Dockerfile.local b/dev/local-backend/adapters/signing/files/Dockerfile.local new file mode 100644 index 0000000000..f14a93fd88 --- /dev/null +++ b/dev/local-backend/adapters/signing/files/Dockerfile.local @@ -0,0 +1,11 @@ +FROM node:22-bookworm-slim@sha256:43ac6c60b8f89723f746e8a92ce91abd5017e627ce1ddfe4238355d3a30b772c +WORKDIR /app +COPY package.json package-lock.json ./ +RUN npm ci --ignore-scripts +COPY tsconfig.json ./ +COPY src ./src +COPY scripts/local-entry.cjs ./scripts/local-entry.cjs +RUN npm run compile && mkdir /local-data && chown node:node /local-data /app +ENV NODE_ENV=development +USER node +CMD ["node", "scripts/local-entry.cjs"] diff --git a/dev/local-backend/adapters/signing/files/scripts/local-entry.cjs b/dev/local-backend/adapters/signing/files/scripts/local-entry.cjs new file mode 100644 index 0000000000..2795934ca9 --- /dev/null +++ b/dev/local-backend/adapters/signing/files/scripts/local-entry.cjs @@ -0,0 +1,14 @@ +// Local test keys are created once per Docker volume. Never print their values. +const fs = require('node:fs'); +const crypto = require('node:crypto'); +const bip39 = require('bip39'); +if (process.env.LOCAL_DEV !== 'true' || process.env.ENVIRONMENT !== 'TEST') throw new Error('Local entry requires LOCAL_DEV and TEST'); +const file = '/local-data/secrets.json'; +if (!fs.existsSync(file)) { + const rsa = crypto.generateKeyPairSync('rsa', { modulusLength: 2048, + privateKeyEncoding: { type: 'pkcs1', format: 'pem' }, publicKeyEncoding: { type: 'spki', format: 'pem' } }); + const secrets = { 'local-rsa-private': rsa.privateKey, 'local-rsa-public': rsa.publicKey }; + for (const id of ['local-signer-v2', 'local-signer-v3', 'local-inheritance-v2', 'local-inheritance-v3']) secrets[id] = bip39.generateMnemonic(256); + fs.writeFileSync(file, JSON.stringify(secrets), { mode: 0o600, flag: 'wx' }); +} +require('../dist/index.js'); diff --git a/dev/local-backend/adapters/signing/files/src/config.ts b/dev/local-backend/adapters/signing/files/src/config.ts new file mode 100644 index 0000000000..1df0526542 --- /dev/null +++ b/dev/local-backend/adapters/signing/files/src/config.ts @@ -0,0 +1,100 @@ +import * as bitcoinJS from "bitcoinjs-lib"; +import dotenv from "dotenv"; +import { fetchSecrets } from "./utilities/secretManager"; +import { logger } from "./utilities/logger" +import { WalletCredsVersion } from "./interfaces/wallet"; + +const result = dotenv.config(); + +if (result.error && process.env.LOCAL_DEV !== "true") { + throw result.error; +} + +export enum SERVER_TYPE { + MAIN = "MAIN", + TEST = "TEST" +} + +export enum DATABASE_TYPE { + DEV = "DEV", + PROD = "PROD" +} + +class Config { + public LOCAL_DEV = process.env.LOCAL_DEV === "true"; + public ENVIRONMENT: string = process.env.ENVIRONMENT; + public PORT: string = process.env.PORT || "3003"; + public NETWORK: bitcoinJS.Network; + public HEXA_ID = process.env.HEXA_ID + public RELAY = process.env.RELAY + + public INHERITANCE_KEY_REQUEST_THRESHOLD: number; // 30days(prod) / 5 mins(dev) + public INHERITANCE_KEY_REQUEST_TTL: number; // 3x of request threshold duration + public ADMIN_KEY: string = process.env.BIT_ADMIN_KEY; + public VERSION: string = process.env.VERSION; + + public MAILER_CONFIG = { + SES_REGION: process.env.SES_REGION, + SOURCE_EMAIL: process.env.SES_SOURCE_EMAIL, + ACCESS_KEY_ID: process.env.SES_ACCESS_KEY_ID, + SECRET_ACCESS_KEY: process.env.SES_SECRET_ACCESS_KEY, + } + + public DB_MODE = process.env.DB_MODE; + public DATABASE_NAME_V2: string = process.env.DATABASE_NAME_V2; + public DATABASE_SECRET_IDENTIFIER_V2: string = process.env.DATABASE_SECRET_IDENTIFIER_V2; + public PROJECT_ID: string = process.env.PROJECT_ID; + public MIN_POLICY_UPDATE_DELAY: number; + + public MNEMONIC_IDENTIFIERS = { + [WalletCredsVersion.V2]: process.env.MNEMONIC_IDENTIFIER_V2, + [WalletCredsVersion.V3]: process.env.MNEMONIC_IDENTIFIER_V3, + }; + + public INHERITANCE_MNEMONIC_IDENTIFIERS = { + [WalletCredsVersion.V2]: process.env.INHERITANCE_MNEMONIC_IDENTIFIER_V2, + [WalletCredsVersion.V3]: process.env.INHERITANCE_MNEMONIC_IDENTIFIER_V3, + }; + + public RSA_PRV_KEY_IDENTIFIER: string = process.env.RSA_PRV_KEY_IDENTIFIER; + public RSA_PUB_KEY_IDENTIFIER: string = process.env.RSA_PUB_KEY_IDENTIFIER; + public RSA_PRV_KEY: string; + public RSA_PUB_KEY: string; + + constructor() { + if (this.LOCAL_DEV) { + const db = new URL(process.env.LOCAL_DATABASE_URL || ""); + if (this.ENVIRONMENT !== "TEST" || this.DB_MODE !== "DEV" || + db.protocol !== "mongodb:" || !["mongo", "localhost", "127.0.0.1"].includes(db.hostname) || + db.username || db.password || db.pathname !== "/keeper_signing_local") { + throw new Error("Local signing requires TEST, DEV and the isolated local database"); + } + if (this.HEXA_ID !== "keeper-local-only") throw new Error("Local signing requires its own application credential"); + } + this.configureNetwork(); + } + + public configureNetwork = () => { + if (this.ENVIRONMENT === SERVER_TYPE.MAIN) { + this.NETWORK = bitcoinJS.networks.bitcoin; + this.INHERITANCE_KEY_REQUEST_THRESHOLD = 2592000000 // 30 days in milliseconds + this.MIN_POLICY_UPDATE_DELAY = 1 * 7 * 24 * 60 * 60 * 1000 // 1 week in milliseconds + } else if (this.ENVIRONMENT === SERVER_TYPE.TEST) { + this.NETWORK = bitcoinJS.networks.testnet; + this.INHERITANCE_KEY_REQUEST_THRESHOLD = 300000 // 5 mins in milliseconds + this.MIN_POLICY_UPDATE_DELAY = 1 * 60 * 60 * 1000 // 1 hour in milliseconds + } else { + throw new Error("Please specify an apt environment(MAIN||TEST)"); + } + this.INHERITANCE_KEY_REQUEST_TTL = (this.INHERITANCE_KEY_REQUEST_THRESHOLD / 1000) * 3 + }; + + public initRSAKeys = async () => { + const [privResponse, pubResponse] = await fetchSecrets([this.RSA_PRV_KEY_IDENTIFIER, this.RSA_PUB_KEY_IDENTIFIER]); + this.RSA_PRV_KEY = privResponse[this.RSA_PRV_KEY_IDENTIFIER] + this.RSA_PUB_KEY = pubResponse[this.RSA_PUB_KEY_IDENTIFIER] + logger.info('RSA keys have been initialized') + } +} + +export default new Config(); diff --git a/dev/local-backend/adapters/signing/local.patch b/dev/local-backend/adapters/signing/local.patch new file mode 100644 index 0000000000..d13580b53a --- /dev/null +++ b/dev/local-backend/adapters/signing/local.patch @@ -0,0 +1,132 @@ +diff --git a/src/databases/dbV2.ts b/src/databases/dbV2.ts +index dc661dc..31b47a9 100644 +--- a/src/databases/dbV2.ts ++++ b/src/databases/dbV2.ts +@@ -269,6 +269,13 @@ class DatabaseV2 { + } + + public initializeConnection = async () => { ++ if (config.LOCAL_DEV) { ++ this.connection = mongoose.createConnection(process.env.LOCAL_DATABASE_URL, { ++ dbName: "keeper_signing_local", autoIndex: true, ++ }); ++ await this.connection.asPromise(); ++ return; ++ } + const [response] = await fetchSecrets( + [config.DATABASE_SECRET_IDENTIFIER_V2], + true +@@ -288,6 +295,8 @@ class DatabaseV2 { + }); + }; + ++ public isReady = () => this.connection?.readyState === 1; ++ + public getSignerV2Model = (): mongoose.Model => { + return this.connection.model("Signer", this.signerV2); + }; +diff --git a/src/index.ts b/src/index.ts +index 7e3a325..bee0b07 100644 +--- a/src/index.ts ++++ b/src/index.ts +@@ -3,6 +3,7 @@ import express, { Express } from "express"; + import morgan from "morgan"; + + import config from "./config"; ++import dbV2 from "./databases/dbV2"; + import { logger, unhandledLogger} from "./utilities/logger" + import { initializeCrons } from "./utilities/crons"; + import { initializeRoutes } from "./routes/router"; +@@ -14,7 +15,7 @@ class SigningServer { + this.app = express(); + this.configure(); + this.invoke(); +- initializeCrons(); ++ if (!config.LOCAL_DEV) initializeCrons(); + config.initRSAKeys(); + } + +@@ -24,6 +25,13 @@ class SigningServer { + this.app.use(morgan("combined", { stream: { write: message => logger.info(message.trim()) } })); + this.app.use(bodyParser.json()); + this.app.use(bodyParser.urlencoded({ extended: true })); ++ if (config.LOCAL_DEV) { ++ this.app.get("/health", (_req, res) => { ++ const ready = dbV2.isReady() && !!config.RSA_PUB_KEY; ++ res.status(ready ? 200 : 503).json({ ready, mode: "local", network: "TESTNET", ++ capabilities: { signing: ready, emailDelivery: false, pushDelivery: false, scheduledJobs: false } }); ++ }); ++ } + this.app.use(initializeRoutes(this.app)); + } + +diff --git a/src/services/mailing/mailer.ts b/src/services/mailing/mailer.ts +index 9ab6b1d..3995234 100644 +--- a/src/services/mailing/mailer.ts ++++ b/src/services/mailing/mailer.ts +@@ -1,5 +1,6 @@ + import config from "../../config"; + import moment from "moment"; ++import { appendFileSync } from "fs"; + import AWS from "aws-sdk"; + import { EMAIL_TEMPLATES } from "./templates"; + +@@ -65,7 +66,7 @@ const fetchEmailContentByType = ( + ) + .replace( + "{{server_url}}", +- config.ENVIRONMENT === "MAIN" ++ config.LOCAL_DEV ? "http://localhost:3003" : config.ENVIRONMENT === "MAIN" + ? "https://sign.bithyve.com" + : "https://dev-sign.bithyve.com" + ) +@@ -109,5 +110,9 @@ export const sendEmails = ( + }, + }, + }; ++ if (config.LOCAL_DEV) { ++ appendFileSync("/local-data/outbox.jsonl", JSON.stringify({ capturedAt: new Date().toISOString(), ...params }) + "\n", { mode: 0o600 }); ++ return Promise.resolve({ MessageId: "local-captured-not-delivered" }); ++ } + return AWS_SES.sendEmail(params).promise(); + }; +diff --git a/src/services/notifications/pushNotification.ts b/src/services/notifications/pushNotification.ts +index 87e9d5c..477681d 100644 +--- a/src/services/notifications/pushNotification.ts ++++ b/src/services/notifications/pushNotification.ts +@@ -85,6 +85,7 @@ export const pushIKNotification = async ( + requestAutoApprovesIn?: number; + } + ) => { ++ if (config.LOCAL_DEV) { logger.info("Local mode: push delivery disabled"); return; } + try { + const requestData = { + FCMs, +@@ -118,6 +119,7 @@ export const pushServerKeyNotification = async ( + signedTx?: string; + } + ) => { ++ if (config.LOCAL_DEV) { logger.info("Local mode: push delivery disabled"); return; } + try { + let title; + let body; +diff --git a/src/utilities/secretManager.ts b/src/utilities/secretManager.ts +index e6bfd49..3b3b732 100644 +--- a/src/utilities/secretManager.ts ++++ b/src/utilities/secretManager.ts +@@ -1,7 +1,15 @@ + import { SecretManagerServiceClient } from '@google-cloud/secret-manager'; + import config from '../config'; ++import { readFileSync } from 'fs'; + + export const fetchSecrets = async (secretIdentifiers: string[], shouldParse: Boolean = false) => { ++ if (config.LOCAL_DEV) { ++ const secrets = JSON.parse(readFileSync("/local-data/secrets.json", "utf8")); ++ return secretIdentifiers.map(identifier => { ++ if (!Object.prototype.hasOwnProperty.call(secrets, identifier)) throw new Error("Unknown local secret identifier"); ++ return { [identifier]: shouldParse ? JSON.parse(secrets[identifier]) : secrets[identifier] }; ++ }); ++ } + const client = new SecretManagerServiceClient(); + const projectId = config.PROJECT_ID; + diff --git a/dev/local-backend/app.env.local.example b/dev/local-backend/app.env.local.example new file mode 100644 index 0000000000..0121ab915d --- /dev/null +++ b/dev/local-backend/app.env.local.example @@ -0,0 +1,10 @@ +# Disposable local development only. Copy to app repo .env.local. +ENVIRONMENT=DEVELOPMENT +RELAY=http://localhost:3000/ +CHANNEL_URL=http://localhost:4002/ +SIGNING_SERVER_TESTNET=http://localhost:3003/ +SIGNING_SERVER_MAINNET=http://localhost:3003/mainnet-disabled/ +HEXA_ID_TESTNET=keeper-local-only +HEXA_ID_MAINNET=keeper-local-mainnet-disabled +ENC_KEY_STORAGE_IDENTIFIER=KEEPER-LOCAL-DEV +SENTRY_DNS=http://local@localhost:3000/1 diff --git a/dev/local-backend/compose.yaml b/dev/local-backend/compose.yaml new file mode 100644 index 0000000000..e26754b4c7 --- /dev/null +++ b/dev/local-backend/compose.yaml @@ -0,0 +1,98 @@ +name: ${COMPOSE_PROJECT_NAME:-keeper-contributor} +services: + mongo: + image: mongo:7.0@sha256:9854f7139445d766a9523571d6f047530c45547460ffcf8259eb2bf4264632ca + volumes: + - mongo-data:/data/db + networks: [local-only] + healthcheck: + test: [CMD, mongosh, --quiet, --eval, "quit(db.adminCommand('ping').ok ? 0 : 1)"] + interval: 5s + timeout: 5s + retries: 20 + relay: + build: + context: ./.sources/relay + dockerfile: Dockerfile.local + environment: + LOCAL_DEV: "true" + ENVIRONMENT: DEVELOPMENT + BITCOIN_NETWORK: TESTNET + VERSION: local-development + DATABASE_URL: mongodb://mongo:27017/keeper_local + PORT: "3000" + WS_PORT: "4002" + HEXA_ID: keeper-local-only + ADMIN_KEY: keeper-local-admin-only + CHANNEL_URL: http://relay:4002/ + FAUCET_MNEMONIC: "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about" + networks: [local-only] + depends_on: + mongo: + condition: service_healthy + healthcheck: + test: [CMD, node, -e, "fetch('http://localhost:3000/health').then(r=>process.exit(r.ok?0:1)).catch(()=>process.exit(1))"] + interval: 5s + timeout: 5s + retries: 20 + signing: + build: + context: ./.sources/signing + dockerfile: Dockerfile.local + environment: + LOCAL_DEV: "true" + ENVIRONMENT: TEST + DB_MODE: DEV + PORT: "3003" + VERSION: local-development + HEXA_ID: keeper-local-only + BIT_ADMIN_KEY: keeper-local-admin-only + LOCAL_DATABASE_URL: mongodb://mongo:27017/keeper_signing_local + RELAY: http://relay:3000/ + MNEMONIC_IDENTIFIER_V2: local-signer-v2 + MNEMONIC_IDENTIFIER_V3: local-signer-v3 + INHERITANCE_MNEMONIC_IDENTIFIER_V2: local-inheritance-v2 + INHERITANCE_MNEMONIC_IDENTIFIER_V3: local-inheritance-v3 + RSA_PRV_KEY_IDENTIFIER: local-rsa-private + RSA_PUB_KEY_IDENTIFIER: local-rsa-public + SES_REGION: us-east-1 + SES_SOURCE_EMAIL: local@example.invalid + volumes: + - signing-data:/local-data + networks: [local-only] + depends_on: + mongo: + condition: service_healthy + healthcheck: + test: [CMD, node, -e, "fetch('http://localhost:3003/health').then(r=>process.exit(r.ok?0:1)).catch(()=>process.exit(1))"] + interval: 5s + timeout: 5s + retries: 20 + gateway: + healthcheck: + test: [CMD-SHELL, "wget -q -O /dev/null http://127.0.0.1:3000/health && wget -q -O /dev/null http://127.0.0.1:3003/health"] + interval: 5s + timeout: 5s + retries: 20 + image: nginx:1.28-alpine@sha256:a8b39bd9cf0f83869a2162827a0caf6137ddf759d50a171451b335cecc87d236 + volumes: + - ./nginx.conf:/etc/nginx/conf.d/default.conf:ro + ports: + - "127.0.0.1:${KEEPER_RELAY_PORT:-3000}:3000" + - "127.0.0.1:${KEEPER_CHANNEL_PORT:-4002}:4002" + - "127.0.0.1:${KEEPER_SIGNING_PORT:-3003}:3003" + networks: [local-only, host-access] + depends_on: + relay: + condition: service_healthy + restart: true + signing: + condition: service_healthy + restart: true +networks: + local-only: + internal: true + host-access: +volumes: + mongo-data: + signing-data: diff --git a/dev/local-backend/dev b/dev/local-backend/dev new file mode 100755 index 0000000000..52a7cab783 --- /dev/null +++ b/dev/local-backend/dev @@ -0,0 +1,3 @@ +#!/bin/sh +set -eu +exec python3 "$(dirname "$0")/keeper.py" "$@" diff --git a/dev/local-backend/keeper.py b/dev/local-backend/keeper.py new file mode 100644 index 0000000000..ffed894485 --- /dev/null +++ b/dev/local-backend/keeper.py @@ -0,0 +1,181 @@ +#!/usr/bin/env python3 +"""Contributor setup using only Python's standard library, Git and Compose.""" +import argparse +import hashlib +import json +import os +from pathlib import Path +import re +import shutil +import subprocess +import sys +import tempfile + +HERE = Path(__file__).resolve().parent +APP = HERE.parent.parent + + +def run(args, **kwargs): + return subprocess.run([str(a) for a in args], check=True, **kwargs) + + +def output(args, **kwargs): + return subprocess.check_output([str(a) for a in args], text=True, **kwargs).strip() + + +def settings(): + values = {} + envfile = HERE / '.env' + if envfile.exists(): + for line in envfile.read_text().splitlines(): + if line.strip() and not line.lstrip().startswith('#'): + key, sep, value = line.partition('=') + if not sep: + raise ValueError('Use KEY=value lines in dev/local-backend/.env') + values[key.strip()] = value.strip() + defaults = {'COMPOSE_PROJECT_NAME': 'keeper-contributor', + 'KEEPER_RELAY_PORT': '3000', 'KEEPER_CHANNEL_PORT': '4002', + 'KEEPER_SIGNING_PORT': '3003'} + for key, default in defaults.items(): + values[key] = os.environ.get(key, values.get(key, default)) + if not re.fullmatch(r'[a-z0-9][a-z0-9_-]*', values['COMPOSE_PROJECT_NAME']): + raise ValueError('COMPOSE_PROJECT_NAME must contain lowercase letters, digits, _ or -') + ports = [int(values[k]) for k in defaults if k.endswith('_PORT')] + if len(set(ports)) != 3 or any(p < 1024 or p > 65535 for p in ports): + raise ValueError('Choose three distinct ports between 1024 and 65535') + return {k: values[k] for k in defaults} + + +def compose(*args, **kwargs): + env = dict(os.environ, **settings()) + return run(['docker', 'compose', '--env-file', HERE / '.env.example', + '-f', HERE / 'compose.yaml', *args], env=env, **kwargs) + + +def prepare(only=None): + lock = json.loads((HERE / 'sources.lock.json').read_text()) + sources = HERE / '.sources' + sources.mkdir(exist_ok=True) + for name, spec in lock.items(): + if only is not None and name != only: + continue + adapter = HERE / 'adapters' / name + for asset, digest in spec['assets'].items(): + if hashlib.sha256((adapter / asset).read_bytes()).hexdigest() != digest: + raise ValueError(f'{name}: adapter checksum mismatch: {asset}') + fingerprint = hashlib.sha256(json.dumps(spec, sort_keys=True).encode()).hexdigest() + target = sources / name + marker = target / '.keeper-source.json' + if target.exists(): + if not marker.exists() or json.loads(marker.read_text()) != spec: + raise ValueError(f'{target}: existing checkout does not match this setup. ' + 'Move it aside after saving your edits, then retry; it was not overwritten.') + if output(['git', '-C', target, 'rev-parse', 'HEAD']) != spec['revision']: + raise ValueError(f'{target}: HEAD changed; save your work and move this checkout aside.') + print(f'{name}: already prepared; preserving local backend edits ({fingerprint[:12]})') + continue + with tempfile.TemporaryDirectory(prefix=f'.{name}-', dir=sources) as temp: + checkout = Path(temp) / 'checkout' + run(['git', 'init', '--quiet', checkout]) + run(['git', '-C', checkout, 'remote', 'add', 'origin', spec['url']]) + print(f'{name}: fetching {spec["revision"]}', flush=True) + try: + run(['git', '-C', checkout, 'fetch', '--quiet', '--depth=1', 'origin', spec['revision']]) + except subprocess.CalledProcessError as error: + raise ValueError(f'{name}: cannot fetch the pinned public backend revision. ' + 'Check the revision and network connection. ' + 'No existing checkout was replaced.') from error + run(['git', '-C', checkout, 'checkout', '--quiet', '--detach', spec['revision']]) + if spec['assets']: + run(['git', '-C', checkout, 'apply', '--check', adapter / 'local.patch']) + run(['git', '-C', checkout, 'apply', adapter / 'local.patch']) + for asset in (adapter / 'files').rglob('*'): + if asset.is_file(): + dest = checkout / asset.relative_to(adapter / 'files') + dest.parent.mkdir(parents=True, exist_ok=True) + shutil.copyfile(asset, dest) + (checkout / '.keeper-source.json').write_text(json.dumps(spec, indent=2) + '\n') + checkout.rename(target) + + +def app_env(): + config = settings() + template = (HERE / 'app.env.local.example').read_text() + for port, name in [('3000', 'RELAY'), ('4002', 'CHANNEL'), ('3003', 'SIGNING')]: + template = template.replace(f':{port}/', f':{config[f"KEEPER_{name}_PORT"]}/') + dest = APP / '.env.local' + if dest.exists(): + if dest.read_text() != template: + raise ValueError('.env.local already exists with different settings. Compare it with ' + 'dev/local-backend/app.env.local.example; no file was overwritten.') + print('.env.local already matches this setup') + return + fd = os.open(dest, os.O_CREAT | os.O_EXCL | os.O_WRONLY, 0o600) + with os.fdopen(fd, 'w') as stream: + stream.write(template) + print('Created .env.local (local test settings only)') + + +def doctor(): + failed = False + for label, args in [('Git', ['git', '--version']), ('Docker Engine', ['docker', 'info', '--format', '{{.ServerVersion}}']), + ('Docker Compose', ['docker', 'compose', 'version']), ('Node', ['node', '--version']), + ('Yarn', ['yarn', '--version'])]: + try: + print(f'{label}: {output(args, stderr=subprocess.DEVNULL)}') + except (OSError, subprocess.CalledProcessError): + print(f'MISSING: {label}') + failed = True + print('Python:', sys.version.split()[0]) + print('Android SDK:', os.environ.get('ANDROID_HOME', 'set ANDROID_HOME before Android builds')) + print('JAVA_HOME:', os.environ.get('JAVA_HOME', 'set JAVA_HOME to JDK 17 before Android builds')) + if sys.platform == 'darwin': + for label, args in [('Xcode', ['xcodebuild', '-version']), ('Ruby', ['ruby', '--version']), + ('Bundler', ['bundle', '--version'])]: + try: + print(f'{label}: {output(args, stderr=subprocess.DEVNULL)}') + except (OSError, subprocess.CalledProcessError): + print(f'MISSING for iOS: {label}') + print('See README.md for required native tool versions; this checks availability, not native builds.') + if failed: + raise ValueError('Install/start the missing prerequisites and rerun doctor.') + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('command', choices=['doctor', 'prepare', 'prepare-signing', 'up', 'stop', 'status', 'logs', 'verify', 'env', 'android-forward']) + parser.add_argument('--serial', help='Android emulator/device serial for android-forward') + args = parser.parse_args() + if args.command == 'doctor': + doctor() + elif args.command == 'prepare': + prepare() + elif args.command == 'prepare-signing': + prepare(only='signing') + elif args.command == 'up': + prepare() + compose('up', '-d', '--build', '--wait', '--wait-timeout', '180') + run([sys.executable, HERE / 'verify.py']) + elif args.command == 'stop': + compose('down') # Retain both databases and signing keys. No implicit data reset. + elif args.command == 'status': + compose('ps') + elif args.command == 'logs': + compose('logs', '--tail', '100') + elif args.command == 'verify': + run([sys.executable, HERE / 'verify.py']) + elif args.command == 'env': + app_env() + elif args.command == 'android-forward': + if not args.serial: + parser.error('android-forward requires --serial; use adb devices to choose one') + for port in ['8081'] + [v for k, v in settings().items() if k.endswith('_PORT')]: + run(['adb', '-s', args.serial, 'reverse', f'tcp:{port}', f'tcp:{port}']) + + +if __name__ == '__main__': + try: + main() + except (OSError, ValueError, subprocess.CalledProcessError) as error: + print(f'Setup failed: {error}', file=sys.stderr) + sys.exit(1) diff --git a/dev/local-backend/nginx.conf b/dev/local-backend/nginx.conf new file mode 100644 index 0000000000..bcd9481514 --- /dev/null +++ b/dev/local-backend/nginx.conf @@ -0,0 +1,22 @@ +server { + listen 3000; + client_max_body_size 50m; + location / { + proxy_pass http://relay:3000; + } +} +server { + listen 4002; + location / { + proxy_pass http://relay:4002; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "upgrade"; + } +} +server { + listen 3003; + location / { + proxy_pass http://signing:3003; + } +} diff --git a/dev/local-backend/sources.lock.json b/dev/local-backend/sources.lock.json new file mode 100644 index 0000000000..231f141db1 --- /dev/null +++ b/dev/local-backend/sources.lock.json @@ -0,0 +1,18 @@ +{ + "relay": { + "url": "https://github.com/KeeperCommunity/Relay.git", + "revision": "3e1a01bd2fd5381d8546229922c431ce50bedad4", + "assets": {} + }, + "signing": { + "url": "https://github.com/KeeperCommunity/SigningServer.git", + "revision": "b82dc4f4a8f75676b70b12545d59c7906556a92b", + "assets": { + "files/.dockerignore": "78d9a1e03c57cfc1c7fb79ff2335aa2393bb075d0ac4a23feae81371728190be", + "files/Dockerfile.local": "04a0bfca3903eb1363f8804b7c181424747c949aed543a89fe7a02316ccafa3d", + "files/scripts/local-entry.cjs": "7e0d39965fd8d4a880ab68b5ff9e7b75595c33cbdb106bb5ec0b42d505b1827b", + "files/src/config.ts": "606fdeb4717e98ab95e5a6918a0c8a8b9b3427697d27bba263b9ec566a91438e", + "local.patch": "a592f37900215a5f013a0d349983f7a1d937206fbf1670c07fc9a37e457d743f" + } + } +} diff --git a/dev/local-backend/test_keeper.py b/dev/local-backend/test_keeper.py new file mode 100644 index 0000000000..3c80fd9d0c --- /dev/null +++ b/dev/local-backend/test_keeper.py @@ -0,0 +1,92 @@ +"""Regression checks for setup operations that must preserve developer work.""" +import hashlib +import json +import subprocess +from pathlib import Path +import tempfile +import unittest +from unittest.mock import patch + +import keeper + + +class PreserveDeveloperWork(unittest.TestCase): + def setUp(self): + self.temp = tempfile.TemporaryDirectory() + self.addCleanup(self.temp.cleanup) + self.app = Path(self.temp.name) + self.here = self.app / 'dev/local-backend' + self.here.mkdir(parents=True) + self.contexts = [patch.object(keeper, 'HERE', self.here), patch.object(keeper, 'APP', self.app), + patch.dict('os.environ', {}, clear=True)] + for context in self.contexts: + context.start() + self.addCleanup(context.stop) + + def test_environment_is_exclusive_and_preserves_production(self): + (self.here / 'app.env.local.example').write_text('RELAY=http://localhost:3000/\n') + production = self.app / '.env.production' + production.write_text('existing-production-sentinel\n') + keeper.app_env() + self.assertEqual((self.app / '.env.local').stat().st_mode & 0o777, 0o600) + keeper.app_env() # Same configuration is repeatable. + (self.app / '.env.local').write_text('developer-settings\n') + with self.assertRaisesRegex(ValueError, 'already exists'): + keeper.app_env() + self.assertEqual((self.app / '.env.local').read_text(), 'developer-settings\n') + self.assertEqual(production.read_text(), 'existing-production-sentinel\n') + + def test_adapter_tampering_is_rejected_before_fetch(self): + adapter = self.here / 'adapters/relay' + adapter.mkdir(parents=True) + (adapter / 'local.patch').write_text('changed') + (self.here / 'sources.lock.json').write_text(json.dumps({'relay': { + 'assets': {'local.patch': hashlib.sha256(b'original').hexdigest()}}})) + with patch.object(keeper, 'run') as command: + with self.assertRaisesRegex(ValueError, 'checksum mismatch'): + keeper.prepare() + command.assert_not_called() + + def test_unknown_existing_checkout_is_not_overwritten(self): + source = self.here / '.sources/relay' + source.mkdir(parents=True) + edit = source / 'my-work.ts' + edit.write_text('keep this') + (self.here / 'sources.lock.json').write_text(json.dumps({'relay': {'assets': {}}})) + with patch.object(keeper, 'run') as command: + with self.assertRaisesRegex(ValueError, 'not overwritten'): + keeper.prepare() + command.assert_not_called() + self.assertEqual(edit.read_text(), 'keep this') + + def test_port_collision_is_rejected(self): + (self.here / '.env').write_text('KEEPER_RELAY_PORT=3003\n') + with self.assertRaisesRegex(ValueError, 'distinct ports'): + keeper.settings() + + def test_fetch_failure_explains_public_revision_and_cleans_temporary_checkout(self): + (self.here / 'sources.lock.json').write_text(json.dumps({'relay': { + 'assets': {}, 'url': 'https://github.com/example/private-backend.git', + 'revision': 'a' * 40}})) + with patch.object(keeper, 'run', side_effect=[ + None, None, subprocess.CalledProcessError(128, ['git', 'fetch'])]): + with self.assertRaisesRegex(ValueError, 'pinned public backend revision'): + keeper.prepare() + self.assertEqual(list((self.here / '.sources').iterdir()), []) + + def test_signing_prepare_does_not_fetch_relay(self): + (self.here / 'sources.lock.json').write_text(json.dumps({ + 'relay': {'assets': {}, 'url': 'https://github.com/example/private-relay.git', 'revision': 'a' * 40}, + 'signing': {'assets': {}, 'url': 'https://github.com/example/public-signing.git', 'revision': 'b' * 40}, + })) + with patch.object(keeper, 'run', side_effect=[ + None, None, subprocess.CalledProcessError(128, ['git', 'fetch'])]) as command: + with self.assertRaisesRegex(ValueError, 'pinned public backend revision'): + keeper.prepare(only='signing') + self.assertEqual(command.call_count, 3) + self.assertIn('https://github.com/example/public-signing.git', command.call_args_list[1].args[0]) + self.assertEqual(list((self.here / '.sources').iterdir()), []) + + +if __name__ == '__main__': + unittest.main() diff --git a/dev/local-backend/verify-local.sh b/dev/local-backend/verify-local.sh new file mode 100755 index 0000000000..04dc216dfb --- /dev/null +++ b/dev/local-backend/verify-local.sh @@ -0,0 +1,3 @@ +#!/bin/sh +set -eu +exec python3 "$(dirname "$0")/verify.py" "$@" diff --git a/dev/local-backend/verify.py b/dev/local-backend/verify.py new file mode 100644 index 0000000000..e81932aaf6 --- /dev/null +++ b/dev/local-backend/verify.py @@ -0,0 +1,151 @@ +#!/usr/bin/env python3 +"""Exercise disposable local fixtures, never a hosted backend or real wallet.""" +import argparse +import base64 +import hashlib +import hmac +import json +import struct +import subprocess +import sys +import time +import urllib.error +import urllib.request +import uuid + +from keeper import compose, settings + + +def require(condition, description): + if not condition: + raise RuntimeError(description) + + +class NoRedirect(urllib.request.HTTPRedirectHandler): + def redirect_request(self, *args, **kwargs): + raise RuntimeError('Local check refused an HTTP redirect') + + +def request(port, path, data=None, expected=200): + req = urllib.request.Request(f'http://127.0.0.1:{port}{path}', + data=None if data is None else json.dumps(data).encode(), + headers={'Content-Type': 'application/json', 'appversion': '2.5.15', + 'os': 'ANDROID', 'HEXA-ID': 'keeper-local-only'}) + try: + response = urllib.request.build_opener(urllib.request.ProxyHandler({}), NoRedirect()).open(req, timeout=20) + except urllib.error.HTTPError as error: + response = error + with response: + statuses = expected if isinstance(expected, tuple) else (expected,) + require(response.code in statuses, f'{path}: expected HTTP {expected}, got {response.code}') + body = response.read().decode() + return body if path.startswith('/socket.io/') else json.loads(body) + + +def token(secret): + key = base64.b32decode(secret + '=' * (-len(secret) % 8)) + digest = hmac.new(key, struct.pack('>Q', int(time.time()) // 30), hashlib.sha1).digest() + offset = digest[-1] & 15 + return str((struct.unpack('>I', digest[offset:offset + 4])[0] & 0x7fffffff) % 1000000).zfill(6) + + +def health(relay, signing): + for port in (relay, signing): + result = request(port, '/health') + require(result.get('ready') is True and result.get('mode') == 'local' and + result.get('network') == 'TESTNET', 'Expected healthy local TESTNET service') + require(result['capabilities']['pushDelivery'] is False, 'Push must be disabled') + require(request(signing, '/health')['capabilities']['scheduledJobs'] is False, 'Scheduled jobs must be disabled') + print('PASS local/testnet health and disabled cloud capabilities') + + +def boundaries(): + cases = [('relay', 'BITCOIN_NETWORK=MAINNET', 'LOCAL_DEV requires'), + ('relay', 'DATABASE_URL=mongodb://example.invalid/keeper_local', 'LOCAL_DEV requires'), + ('signing', 'ENVIRONMENT=MAIN', 'Local signing requires'), + ('signing', 'LOCAL_DATABASE_URL=mongodb://example.invalid/keeper_signing_local', 'Local signing requires')] + for service, override, message in cases: + try: + compose('run', '--rm', '--no-deps', '-e', override, service, 'node', '-e', + "require('./dist/config')", capture_output=True, text=True) + except subprocess.CalledProcessError as error: + require(message in error.stdout + error.stderr, f'{service}: failed for an unexpected reason') + else: + raise RuntimeError(f'{service}: unsafe configuration was accepted') + print('PASS mainnet and hosted database configuration rejected') + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--persistence', action='store_true', help='Recreate this Compose stack, retaining volumes, and recheck fixtures') + parser.add_argument('--boundaries', action='store_true', help='Check startup rejection of mainnet and external databases') + args = parser.parse_args() + config = settings() + relay, channel, signing = (config[f'KEEPER_{key}_PORT'] for key in ('RELAY', 'CHANNEL', 'SIGNING')) + health(relay, signing) # Must pass before any mutations. + handshake = request(channel, '/socket.io/?EIO=4&transport=polling') + require(handshake.startswith('0') and json.loads(handshake[1:]).get('sid'), 'Socket.IO handshake failed') + print('PASS Socket.IO handshake') + unavailable = request(relay, '/local-unavailable-probe', {}, expected=503) + require(unavailable.get('error') == 'LOCAL_INTEGRATION_UNAVAILABLE', 'Unavailable route silently succeeded') + print('PASS unsupported integration fails explicitly') + fixture = 'contributor-smoke-' + uuid.uuid4().hex + created = request(relay, '/createNewApp', {'publicId': fixture, 'appID': fixture}) + require(created.get('created') is True, 'App record creation failed') + # Existing relay acknowledges creation before its app save callback completes. + # Poll the non-throwing subscription endpoint before retrieving the app image. + for attempt in range(20): + subscription = request(relay, '/getSubscriptionDetails', {'id': fixture, 'appID': fixture}) + if subscription.get('currentSubscription'): + break + time.sleep(0.25) + else: + raise RuntimeError('Created app did not become readable within 5 seconds') + app = request(relay, '/getAppImage', {'appId': fixture}) + require(app.get('appImage', {}).get('appId') == fixture and + app['appImage'].get('publicId') == fixture and app['appImage'].get('_id'), + 'Persisted app record round trip failed') + print('PASS relay app record create/read') + rejected = request(signing, '/v3/setupSigner', {'HEXA_ID': 'invalid'}, expected=400) + require(rejected.get('err') == 'Unauthorized request', 'Signing authorization failed') + result = request(signing, '/v3/setupSigner', {'HEXA_ID': 'keeper-local-only', 'policy': {'verification': {'method': 'TWO_FA'}, + 'restrictions': {'none': True, 'maxTransactionAmount': None, 'timeWindow': None}, + 'signingDelay': None}}) + require(result.get('setupSuccessful') is True, 'Signer setup failed') + data = result['setupData'] + require(data['bhXpub'].startswith('tpub'), 'Signer must use testnet public key') + secret = data['verification']['verifier'] # Disposable fixture; never print or write to source. + auth = {'HEXA_ID': 'keeper-local-only', 'id': data['id'], 'verificationToken': token(secret)} + # Pinned backend uses a save callback here too; wait only for record visibility. + for attempt in range(20): + result = request(signing, '/v3/validateSingerSetup', auth, expected=(200, 400)) + if result.get('valid') is True: + break + require(result.get('err', '').startswith('Singer not found'), 'Valid 2FA token rejected') + time.sleep(0.25) + auth['verificationToken'] = token(secret) + else: + raise RuntimeError('Created signer did not become readable within 5 seconds') + invalid = dict(auth, verificationToken='abcdef') + request(signing, '/v3/validateSingerSetup', invalid, expected=400) + print('PASS testnet signer setup, authorization and valid/invalid 2FA') + if args.persistence: + compose('down') + compose('up', '-d', '--wait', '--wait-timeout', '180') + health(relay, signing) + require(request(relay, '/getAppImage', {'appId': fixture}) == app, 'App data changed after recreation') + auth['verificationToken'] = token(secret) + restored = request(signing, '/v3/fetchSignerSetup', auth) + require(restored.get('valid') is True and restored.get('xpub') == data['bhXpub'], 'Signing identity changed after recreation') + print('PASS database and signing identity persist after container recreation') + if args.boundaries: + boundaries() + print('Local backend checks passed. Disposable records retained in this project\'s local volumes.') + + +if __name__ == '__main__': + try: + main() + except Exception as error: + print(f'FAIL: {error}', file=sys.stderr) + sys.exit(1) diff --git a/setup.sh b/setup.sh index 2013fb9bd3..3135bb8cbb 100755 --- a/setup.sh +++ b/setup.sh @@ -1,9 +1,6 @@ -# enabling node core modules -# adding node core modules support in react-native -rn-nodeify --install buffer,events,process,stream,inherits,path,assert,crypto --hack --yarn - -# ios dependency installation -cd ios && RCT_NEW_ARCH_ENABLED=1 pod install +#!/bin/sh +set -eu -# android SDK location configuration -cd ../android && touch local.properties && echo "sdk.dir = /Users/$(whoami)/Library/Android/sdk" >local.properties +# Yarn's prepare hook runs on Android-only machines too. Keep it platform neutral; +# CocoaPods and the Android SDK are configured explicitly in dev/local-backend/README.md. +rn-nodeify --install buffer,events,process,stream,inherits,path,assert,crypto --hack --yarn From fb2dc41ea2df5568de5b92606fd484e53858e3c1 Mon Sep 17 00:00:00 2001 From: Anant Date: Wed, 7 Oct 2026 09:00:52 +0530 Subject: [PATCH 019/254] Document shared sprint and release branch flow --- CONTRIBUTING.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 569fc61460..6fff79d405 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -56,6 +56,11 @@ not prerequisites for contributing. [Android](android/fastlane/README.md) and [iOS](ios/fastlane/README.md) release workflows separately from local development. +`sprint` is the shared integration branch. Maintainers may use a temporary +release branch while a specific app version is stabilized. Carry fixes made on +that branch back into `sprint` so the shared development line stays current; +then follow the reviewed `dev` to `main` release promotion used by the project. + For backend development, bootstrap creates ignored checkouts under `dev/local-backend/.sources/`. Save backend work in a branch/patch and submit it to the appropriate backend repository; an app PR does not include those ignored From cc3f16f01cb737b6e28375ffa90ce40a40b84547 Mon Sep 17 00:00:00 2001 From: Anant Date: Wed, 7 Oct 2026 17:10:48 +0530 Subject: [PATCH 020/254] Document Copilot triage and product signoff --- CONTRIBUTING.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 6fff79d405..171c3812fd 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -104,6 +104,13 @@ and test evidence. They may request revisions or additional tests, and decide whether a change is accepted. Only authorized maintainers merge and release official builds. Contributors do not need release permissions to participate. +Copilot code review is requested automatically for pull requests targeting +`sprint`. Read its actual comments and check each claim against the changed code +and relevant tests. Fix valid findings or explain why a comment does not apply +before requesting final human review. A developer's approval remains the code +review gate. The product owner may record product signoff in the PR discussion; +signoff from the PR author does not count as GitHub's required approval. + Some existing CI checks use maintainer-managed services or secrets. If a check is unavailable on a fork PR, report it in the PR so maintainers can arrange review; do not request production secrets or treat an unavailable check as passing. From d3e0b0484741fd7ea533d7285af4e678f61d6c9b Mon Sep 17 00:00:00 2001 From: Anant Date: Wed, 7 Oct 2026 17:16:48 +0530 Subject: [PATCH 021/254] Address Copilot review findings in R3 baseline backmerge --- .github/workflows/test.yml | 6 ++++++ release/README.md | 10 +++++----- scripts/release-version.cjs | 4 +++- scripts/release-version.test.cjs | 7 +++++++ scripts/test_release_artifact.py | 5 ++++- src/store/sagas/upgrade.ts | 14 +++++++++----- tests/store/versionHistory.test.ts | 31 +++++++++++++++++++++--------- 7 files changed, 56 insertions(+), 21 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index dbff68d1af..7b53c20ffd 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -32,3 +32,9 @@ jobs: - name: Run tests run: npm run test + + - name: Run recovery regression tests + run: npm run test:recovery + + - name: Run release and packaged-artifact tests + run: npm run test:release diff --git a/release/README.md b/release/README.md index 4e484e0251..3b9db966fb 100644 --- a/release/README.md +++ b/release/README.md @@ -1,17 +1,17 @@ # Release version verification -**Current milestone:** R02 / 2.6.2: faster entry after local authentication, Assisted Server Backup current-state correctness, narrow timelock prevention, and the selected cloud credential follow-ups. Keep private advisory details, credentials and fixtures out of public release material. +**Current source intent:** 2.6.3 is the pairing-channel hotfix baseline. R3 is a separate internal work bucket; this manifest does not declare an R3 mobile delivery. Keep private advisory details, credentials and fixtures out of public release material. -`version.json` selects 2.6.2, Android 625 and iOS 618. Authenticated store checks on 5 October 2026 found highest uploaded Android code 624 and iOS build 617, both at 2.5.17. Refresh these baselines before upload. Development builds and local relay validation are separate from reviewed production builds, deployment and store submission. R02 is not released. +`version.json` selects 2.6.3, Android 626 and iOS 619. Its recorded authenticated store baselines on 6 October 2026 are Android 625 and iOS 618, both at 2.6.2. Refresh these baselines before upload. Development builds and local relay validation are separate from reviewed production builds, deployment and store submission. -For R02, the owner accepts real-phone upgrade, Face ID and camera feedback after release. Do not require Developer Mode or physical-device connections to finish preparation. Record simulator, automated and funded testnet evidence accurately; they do not establish physical or hardware-signer acceptance. Already-affected-wallet assessment and consented repair are scheduled for R04 / 2.6.3; R03 is the backend open-source milestone. +The owner will perform real-phone and other manual live checks after release. Do not require Developer Mode or physical-device connections to finish preparation. Record simulator, automated and disposable testnet evidence accurately; they do not establish physical or hardware-signer acceptance. 1. Read all uploaded builds and release tracks for Android package `io.hexawallet.bitcoinkeeper`, and uploaded iOS builds for `io.hexawallet.keeper`. Preserve the timestamp and evidence outside the repository. Refresh before upload; preflight accepts evidence at most 24 hours old. 2. Update the manifest with the highest known marketing version and highest uploaded build per platform. Select one greater marketing version for both stores and greater platform build counters. Check unpublished App Store version records as well. 3. Run `npm run version:apply` to synchronize package.json, Android versionName/versionCode and both Keeper iOS targets. Both Info.plist files must reference `$(CURRENT_PROJECT_VERSION)` and `$(MARKETING_VERSION)`; hardcoded values are rejected. Review the diff, then run `npm run version:check` and `npm run test:release`. 4. Reconcile release source with the published versions, prepare a clean release checkout and set `KEEPER_RELEASE_COMMIT` to its complete verified commit SHA. Run `npm run release:preflight`. The manifest cannot contain its own commit SHA. 5. Build from a reviewed production configuration and existing signing identities. Check extracted IPA, APK and AAB metadata against the manifest, verify signing fingerprints and record checksums. A source preflight does not verify the resulting binaries. -6. Test the signed builds before public submission; record the R02 physical-device deferral above separately. +6. Run all feasible automated, simulator/emulator and disposable checks before public submission; record the owner's post-release manual checks separately. Both existing Fastlane `live` lanes run preflight before building and consume `.env.production` without downloading over `.env`. They no longer increment production build numbers. Android notifications require explicit `KEEPER_NOTIFY_SLACK=1`. @@ -23,7 +23,7 @@ Run `python3 scripts/verify-release-artifact.py /absolute/path/to/build.ipa` (or The verifier checks the production application ID, marketing version, native counter, and bundled Ask Keeper / Dust route and data markers, then emits artifact and bundle SHA-256 hashes. Marker presence supplements device testing; it does not prove behavior or source provenance. Compiler-generated component names are unsuitable markers because release optimization removes them. -For Android, set `AAPT` and `APKSIGNER` to SDK build-tool paths (or put them on PATH), `BUNDLETOOL_JAR` to an installed bundletool jar, and `KEEPER_ANDROID_CERT_SHA256` to the certificate fingerprint independently verified from the established public APK. The verifier checks the APK signature and certificate. IPA code signing/provisioning and AAB signing still require separate verification before distribution. The seven packaged-artifact regression tests run with `npm run test:release` alongside the source-version tests; Python 3 is required. +For Android, set `AAPT2` and `APKSIGNER` to SDK build-tool paths (or put them on PATH), `BUNDLETOOL_JAR` to an installed bundletool jar, and `KEEPER_ANDROID_CERT_SHA256` to the certificate fingerprint independently verified from the established public APK. The verifier checks the APK signature and certificate. IPA code signing/provisioning and AAB signing still require separate verification before distribution. The seven packaged-artifact regression tests run with `npm run test:release` alongside the source-version tests; Python 3 is required. The normal public APK channel is the repository's GitHub Releases. Publish the APK together with `SHA256SUM.asc` and `KEEPER_DETACHED_SIGN.sign` using the established PGP identity, as documented in the root README. Do not replace release signing keys to work around missing access. diff --git a/scripts/release-version.cjs b/scripts/release-version.cjs index c1ba39d0b6..de30abd6da 100644 --- a/scripts/release-version.cjs +++ b/scripts/release-version.cjs @@ -140,7 +140,9 @@ function main() { ios: iosConfigurations(sources.ios).map(({ id, version, build }) => ({ id, version, build })) }, null, 2)); return; } - const errors = validateIntent(manifest); + // Store evidence must be fresh for publication, not for draft edits or + // checking that the source and manifest still agree. + const errors = validateIntent(manifest, { requireEvidence: command === 'release-check' }); if (errors.length) throw new Error(errors.join('\n')); if (command === 'apply') { const updated = applyVersions(manifest, sources); diff --git a/scripts/release-version.test.cjs b/scripts/release-version.test.cjs index ca3387c611..c34db5dee9 100644 --- a/scripts/release-version.test.cjs +++ b/scripts/release-version.test.cjs @@ -46,6 +46,13 @@ test('rejects stale and future-dated evidence', () => { assert.ok(validateIntent(m, { now }).some((e) => e.includes('refresh authenticated store baseline'))); } }); + +test('draft validation permits stale evidence while publication validation rejects it', () => { + const m = intent(); + m.baselines.android.verifiedAt = '2026-09-20T00:00:00Z'; + assert.deepEqual(validateIntent(m, { now, requireEvidence: false }), []); + assert.ok(validateIntent(m, { now, requireEvidence: true }).some((e) => e.includes('refresh authenticated store baseline'))); +}); test('rejects the similarly named non-production Play package', () => { const m = intent(); m.baselines.android.applicationId = 'io.hexawallet.keeper'; assert.ok(validateIntent(m, { now }).some((e) => e.includes('wrong production application ID'))); diff --git a/scripts/test_release_artifact.py b/scripts/test_release_artifact.py index d105e52dff..ae6ee89670 100644 --- a/scripts/test_release_artifact.py +++ b/scripts/test_release_artifact.py @@ -26,9 +26,10 @@ def ipa(self, *, app_id="io.hexawallet.keeper", version="2.5.15", build="615", b with zipfile.ZipFile(path, "w") as archive: archive.writestr("Payload/Keeper.app/Info.plist", plistlib.dumps({ "CFBundleIdentifier": app_id, "CFBundleShortVersionString": version, - "CFBundleVersion": build, + "CFBundleVersion": build, "CFBundleExecutable": "Keeper", }, fmt=plistlib.FMT_BINARY)) archive.writestr("Payload/Keeper.app/main.jsbundle", bundle) + archive.writestr("Payload/Keeper.app/Keeper", artifact.LIVE_CHANNEL.encode()) return path def test_candidate_metadata_and_hashes(self): @@ -63,6 +64,7 @@ def test_apk_must_match_independent_signing_identity(self): path = Path(self.directory.name) / "Keeper.apk" with zipfile.ZipFile(path, "w") as archive: archive.writestr("assets/index.android.bundle", FEATURES) + archive.writestr("classes.dex", artifact.LIVE_CHANNEL.encode()) metadata = "package: name='io.hexawallet.bitcoinkeeper' versionCode='622' versionName='2.5.15'" for actual, should_pass in [("a" * 64, True), ("b" * 64, False)]: with self.subTest(actual=actual), patch.dict(os.environ, {"KEEPER_ANDROID_CERT_SHA256": "a" * 64}), \ @@ -77,6 +79,7 @@ def test_aab_reads_base_manifest_and_bundle(self): path = Path(self.directory.name) / "Keeper.aab" with zipfile.ZipFile(path, "w") as archive: archive.writestr("base/assets/index.android.bundle", FEATURES) + archive.writestr("base/classes.dex", artifact.LIVE_CHANNEL.encode()) xml = '' with patch.dict(os.environ, {"BUNDLETOOL_JAR": str(path)}), patch.object(artifact, "output", return_value=xml): self.assertEqual(artifact.inspect(path, MANIFEST)["build"], "622") diff --git a/src/store/sagas/upgrade.ts b/src/store/sagas/upgrade.ts index e7dd01185f..1c8772e532 100644 --- a/src/store/sagas/upgrade.ts +++ b/src/store/sagas/upgrade.ts @@ -97,11 +97,15 @@ export function* updateVersionHistoryWorker({ appId: app.id, version: newVersion, }); - yield call(dbManager.createObject, RealmSchema.VersionHistory, { - version: `${newVersion}(${DeviceInfo.getBuildNumber()})`, - date: new Date().toString(), - title: isRecovery ? 'Recovered Wallet' : `Upgraded from ${previousVersion} to ${newVersion}`, - }); + // recoverApp already records the single "Recovered Wallet" entry. Keep the + // image/version and notification updates here without writing it twice. + if (!isRecovery) { + yield call(dbManager.createObject, RealmSchema.VersionHistory, { + version: `${newVersion}(${DeviceInfo.getBuildNumber()})`, + date: new Date().toString(), + title: `Upgraded from ${previousVersion} to ${newVersion}`, + }); + } const firebaseApp = getApp(); const messagingInstance = getMessaging(firebaseApp); unsubscribeFromTopic(messagingInstance, getReleaseTopic(previousVersion)); diff --git a/tests/store/versionHistory.test.ts b/tests/store/versionHistory.test.ts index 8526829fc8..677ae3bb72 100644 --- a/tests/store/versionHistory.test.ts +++ b/tests/store/versionHistory.test.ts @@ -47,15 +47,28 @@ function writeHistory(action) { return write.payload.args[1]; } +function recoverWithoutDuplicateHistory(action) { + const worker = updateVersionHistoryWorker(action); + expect(worker.next().value).toEqual(call(dbManager.getObjectByIndex, RealmSchema.KeeperApp)); + expect(worker.next({ id: 'test-app' } as any).value).toEqual( + call(Relay.updateAppImage, { appId: 'test-app', version: newVersion }) + ); + expect(worker.next({} as any).done).toBe(true); +} + describe('Version History retains recovery provenance through required upgrades', () => { - test.each([true, false])('upgrade passes recovery=%s to the history writer', (isRecovery) => { + test.each([true, false])('upgrade handles recovery=%s without duplicate history', (isRecovery) => { const upgrade = applyUpgradeSequence({ previousVersion, newVersion, isRecovery }); expect(upgrade.next().value).toEqual(put(setAppVersion(newVersion))); const history = upgrade.next().value as any; expect(history).toEqual(put(updateVersionHistory(previousVersion, newVersion, isRecovery))); - const record = writeHistory(history.payload.action); - expect(record.version).toBe('2.5.16(624)'); - expect(record.title).toBe(isRecovery ? 'Recovered Wallet' : 'Upgraded from 2.5.15 to 2.5.16'); + if (isRecovery) { + recoverWithoutDuplicateHistory(history.payload.action); + } else { + const record = writeHistory(history.payload.action); + expect(record.version).toBe('2.5.16(624)'); + expect(record.title).toBe('Upgraded from 2.5.15 to 2.5.16'); + } expect(upgrade.next().done).toBe(true); }); @@ -89,7 +102,7 @@ function recoveryFixture(backupVersion: string) { ts.isFunctionDeclaration(node) && ['getAppImageWorker', 'recoverApp', 'writeRecoveredObject'].includes(node.name?.text) ); - const writes = new Map(); + const writes = []; const recoveryUpgrades = []; const errors = []; const scope: any = { @@ -142,7 +155,7 @@ function recoveryFixture(backupVersion: string) { addSigningDeviceWorker: () => {}, dbManager: { createObject: (schema, record) => { - if (schema === RealmSchema.VersionHistory) writes.set(record.version, record); + if (schema === RealmSchema.VersionHistory) writes.push(record); return true; }, createObjectBulk: () => {}, @@ -218,8 +231,8 @@ function recoveryFixture(backupVersion: string) { const action = effect.payload.action; if (action.type === 'setAppImageError' && action.payload) errors.push(action.payload); if (action.type === 'UPDATE_VERSION_HISTORY') { - const record = writeHistory(action); - writes.set(record.version, record); + if (action.payload.isRecovery) recoverWithoutDuplicateHistory(action); + else writes.push(writeHistory(action)); } } result = generator.next(response); @@ -235,7 +248,7 @@ test.each(['2.5.15', '2.5.16'])( (backupVersion) => { const { writes, recoveryUpgrades, errors } = recoveryFixture(backupVersion); expect(errors).toEqual([]); - expect([...writes.values()]).toEqual([ + expect(writes).toEqual([ expect.objectContaining({ version: '2.5.16(624)', title: 'Recovered Wallet', From 4b303f7b22baa5174bd7a95cd531e77995ee13c5 Mon Sep 17 00:00:00 2001 From: Anant Date: Wed, 7 Oct 2026 17:21:05 +0530 Subject: [PATCH 022/254] Check backup freshness after wallet sync completes --- src/store/sagaActions/wallets.ts | 6 +++-- src/store/sagas/login.ts | 12 +--------- src/store/sagas/wallets.ts | 31 +++++++++++++++++++++---- tests/recovery/backupFreshness.test.cjs | 27 +++++++++++++++++++++ tests/recovery/sagaHarness.cjs | 10 +++++++- 5 files changed, 68 insertions(+), 18 deletions(-) diff --git a/src/store/sagaActions/wallets.ts b/src/store/sagaActions/wallets.ts index 0508d24152..b9078e1e3e 100644 --- a/src/store/sagaActions/wallets.ts +++ b/src/store/sagaActions/wallets.ts @@ -29,13 +29,15 @@ export const getTestcoins = (testWallet: Wallet) => ({ export const autoSyncWallets = ( syncAll?: boolean, hardRefresh?: boolean, - addNotifications?: boolean + addNotifications?: boolean, + backupCheckAppId?: string ) => ({ type: AUTO_SYNC_WALLETS, payload: { syncAll, hardRefresh, addNotifications, + backupCheckAppId, }, }); @@ -145,4 +147,4 @@ export const generateNewAddress = (wallet: Wallet | Vault) => ({ export const updateVaultSignersXpriv = (signers: Signer[]) => ({ type: UPDATED_VAULT_SIGNERS_XPRIV, signers, -}); \ No newline at end of file +}); diff --git a/src/store/sagas/login.ts b/src/store/sagas/login.ts index 35028a9bc8..93465bf271 100644 --- a/src/store/sagas/login.ts +++ b/src/store/sagas/login.ts @@ -51,7 +51,6 @@ import { createWatcher } from '../utilities'; import { fetchExchangeRates } from '../sagaActions/send_and_receive'; import { setLoginMethod } from '../reducers/settings'; import { setSubscription } from 'src/store/sagaActions/settings'; -import { checkBackupFreshness } from '../sagaActions/bhr'; import { uaiChecks } from '../sagaActions/uai'; import { applyUpgradeSequence } from './upgrade'; import { resetSyncing } from '../reducers/wallets'; @@ -228,7 +227,7 @@ function* credentialsAuthWorker({ payload }) { // Wallet/node refresh runs through its watcher after local unlock. // Waiting for network work here can hold the success modal for 15s. yield put(resetSyncing()); - yield put(autoSyncWallets(false, false, true)); + yield put(autoSyncWallets(false, false, true, id)); yield put( uaiChecks([ @@ -241,15 +240,6 @@ function* credentialsAuthWorker({ payload }) { ]) ); - const { - pendingAllBackup, - automaticCloudBackup, - backupRepairCompletedByAppId = {}, - } = yield select((state: RootState) => state.bhr); - // Check without uploading; a mismatch requires Back Up Now. - if (automaticCloudBackup && (pendingAllBackup || !backupRepairCompletedByAppId[id])) { - yield put(checkBackupFreshness()); - } if (!allAccounts.length) { // upgraded app yield put(addAccount(appId)); diff --git a/src/store/sagas/wallets.ts b/src/store/sagas/wallets.ts index d212e033db..cd3d786253 100644 --- a/src/store/sagas/wallets.ts +++ b/src/store/sagas/wallets.ts @@ -71,6 +71,7 @@ import { } from 'src/utils/utilities'; import { COLLABORATIVE_SCHEME } from 'src/screens/SigningDevices/SetupCollaborativeWallet'; import { RootState } from '../store'; +import { checkBackupFreshness } from '../sagaActions/bhr'; import { initiateVaultMigration, @@ -633,7 +634,7 @@ function* refreshWalletsWorker({ }); try { - if (!wallets || wallets.length === 0) return; + if (!wallets || wallets.length === 0) return true; if (!ELECTRUM_CLIENT.isClientConnected) { ElectrumClient.resetCurrentPeerIndex(); @@ -882,6 +883,7 @@ function* refreshWalletsWorker({ yield put(setPendingDustToast(synchedWallet.id)); } } // end for (synchedWalletWithUTXOs) + return true; } catch (err) { if ([ELECTRUM_NOT_CONNECTED_ERR, ELECTRUM_NOT_CONNECTED_ERR_TOR].includes(err?.message)) { yield put( @@ -897,6 +899,7 @@ function* refreshWalletsWorker({ ); captureError(err); } + return false; } finally { yield put(setSyncing({ wallets, isSyncing: false })); } @@ -907,9 +910,14 @@ export const refreshWalletsWatcher = createWatcher(refreshWalletsWorker, REFRESH export function* autoWalletsSyncWorker({ payload, }: { - payload: { syncAll?: boolean; hardRefresh?: boolean; addNotifications?: boolean }; + payload: { + syncAll?: boolean; + hardRefresh?: boolean; + addNotifications?: boolean; + backupCheckAppId?: string; + }; }) { - const { syncAll, hardRefresh, addNotifications } = payload; + const { syncAll, hardRefresh, addNotifications, backupCheckAppId } = payload; const wallets: Wallet[] = yield call(dbManager.getObjectByIndex, RealmSchema.Wallet, null, true); const vaults: Vault[] = yield call(dbManager.getObjectByIndex, RealmSchema.Vault, null, true); const { bitcoinNetworkType } = yield select((state: RootState) => state.settings); @@ -923,8 +931,9 @@ export function* autoWalletsSyncWorker({ } walletsToSync = walletsToSync.filter((wallet) => wallet.networkType === bitcoinNetworkType); + let refreshSucceeded = true; if (walletsToSync.length) { - yield call(refreshWalletsWorker, { + refreshSucceeded = yield call(refreshWalletsWorker, { payload: { wallets: walletsToSync, options: { @@ -934,6 +943,20 @@ export function* autoWalletsSyncWorker({ }, }); } + // Check backup freshness only after the refreshed wallet specs are persisted. + // A failed refresh or account switch must not verify a stale snapshot. + if (backupCheckAppId && refreshSucceeded) { + const { appId } = yield select((state: RootState) => state.storage); + const { automaticCloudBackup, pendingAllBackup, backupRepairCompletedByAppId = {} } = + yield select((state: RootState) => state.bhr); + if ( + appId === backupCheckAppId && + automaticCloudBackup && + (pendingAllBackup || !backupRepairCompletedByAppId[appId]) + ) { + yield put(checkBackupFreshness()); + } + } } export const autoWalletsSyncWatcher = createWatcher(autoWalletsSyncWorker, AUTO_SYNC_WALLETS); diff --git a/tests/recovery/backupFreshness.test.cjs b/tests/recovery/backupFreshness.test.cjs index 03ff4b9ab2..e03ad47b90 100644 --- a/tests/recovery/backupFreshness.test.cjs +++ b/tests/recovery/backupFreshness.test.cjs @@ -3,6 +3,33 @@ const assert = require('node:assert/strict'); const { fixture, enums, encryption, wallet, loadModule } = require('./sagaHarness.cjs'); const { NetworkType, WalletType } = enums; +test('login backup inspection waits for wallet refresh to finish', async () => { + let finishSync; + const syncGate = new Promise((resolve) => { finishSync = resolve; }); + const f = fixture({ bhr: { pendingAllBackup: true }, syncGate }); + f.collections.Wallet.push(wallet('pending', NetworkType.MAINNET)); + const running = f.run('autoWalletsSyncWorker', { + payload: { syncAll: false, hardRefresh: false, addNotifications: true, backupCheckAppId: f.app.id }, + }); + await new Promise((resolve) => setImmediate(resolve)); + assert.deepEqual(f.calls.map(([type]) => type), ['sync']); + assert.equal(f.actions.some((action) => action.type === 'checkBackupFreshness'), false); + finishSync(); + await running; + assert.equal(f.actions.filter((action) => action.type === 'checkBackupFreshness').length, 1); +}); + +test('failed refresh and account switch do not verify backup freshness', async () => { + for (const option of ['syncFails', 'switchAppDuringSync']) { + const f = fixture({ bhr: { pendingAllBackup: true }, [option]: true }); + f.collections.Wallet.push(wallet('pending', NetworkType.MAINNET)); + await f.run('autoWalletsSyncWorker', { + payload: { backupCheckAppId: f.app.id }, + }); + assert.equal(f.actions.some((action) => action.type === 'checkBackupFreshness'), false); + } +}); + for (const network of [NetworkType.MAINNET, NetworkType.TESTNET]) { test(`wallet added after initial backup survives encrypted restore: ${network}`, async () => { const f = fixture(); diff --git a/tests/recovery/sagaHarness.cjs b/tests/recovery/sagaHarness.cjs index ab82a1d9c4..37cc5412d7 100644 --- a/tests/recovery/sagaHarness.cjs +++ b/tests/recovery/sagaHarness.cjs @@ -77,6 +77,7 @@ const signer = (fingerprint, networkType, type = enums.SignerType.COLDCARD, acco function fixture(options = {}) { const appId = require('crypto').createHash('sha256').update(Buffer.from('ab'.repeat(32), 'hex')).digest('hex'); const state = { + storage: { appId }, bhr: { automaticCloudBackup: true, pendingAllBackup: false, @@ -213,6 +214,12 @@ function fixture(options = {}) { semver: require('semver'), setupRecoveryKeySigningKey: () => signer('RECOVERY', NetworkType.MAINNET, enums.SignerType.SEED_WORDS), applyUpgradeSequence: () => {}, + refreshWalletsWorker: async () => { + calls.push(['sync']); + if (options.syncGate) await options.syncGate; + if (options.switchAppDuringSync) state.storage.appId = 'another-app'; + return options.syncFails !== true; + }, }; const names = [ 'setBackupRepairCompleted', @@ -235,6 +242,7 @@ function fixture(options = {}) { 'setAppId', 'setAppCreated', 'autoSyncWallets', + 'checkBackupFreshness', 'uaiChecks', 'loadConciergeUser', 'loadConciergeTickets', @@ -264,7 +272,7 @@ function fixture(options = {}) { functions('src/store/sagas/wallets.ts', [ 'addNewWalletsWorker', 'addSigningDeviceWorker', 'mergeSimilarKeysWorker', 'updateVaultSignerXprivWorker', 'updateSignerDetailsWorker', - 'updateKeyDetailsWorker', + 'updateKeyDetailsWorker', 'autoWalletsSyncWorker', ]); vm.runInNewContext( ts.transpileModule(code, { compilerOptions: { target: ts.ScriptTarget.ES2020 } }).outputText, From c80b4e747314812616978347c5759ab18ff04a0d Mon Sep 17 00:00:00 2001 From: Anant Date: Wed, 7 Oct 2026 17:25:10 +0530 Subject: [PATCH 023/254] Address Copilot findings in public contributor bootstrap --- dev/local-backend/README.md | 4 ++-- .../adapters/relay/files/Dockerfile.local | 12 +++++++++++ dev/local-backend/keeper.py | 17 ++++++++++++---- dev/local-backend/sources.lock.json | 4 +++- dev/local-backend/test_keeper.py | 20 +++++++++++++++++++ 5 files changed, 50 insertions(+), 7 deletions(-) create mode 100644 dev/local-backend/adapters/relay/files/Dockerfile.local diff --git a/dev/local-backend/README.md b/dev/local-backend/README.md index 0968bf0885..f74eb4c29a 100644 --- a/dev/local-backend/README.md +++ b/dev/local-backend/README.md @@ -152,9 +152,9 @@ The relay health field `signing: false` describes the relay, not the separate si ## Sources, data and network boundaries -`sources.lock.json` pins both public backend revisions and the SigningServer adapter SHA-256 checksums. Relay's public source already includes local mode, so it needs no app-side adapter. The Node, Mongo and nginx base images are pinned by digest. Backend dependency installs use frozen Yarn / npm lockfiles; OS packages in the relay build still use Debian repositories, so this is a reproducible development workflow, not a bit-for-bit reproducible image build. +`sources.lock.json` pins both public backend revisions and all local adapter SHA-256 checksums. Relay's public source includes local mode; its local Dockerfile adapter pins the Node base image. The Node, Mongo and nginx base images are pinned by digest. Backend dependency installs use frozen Yarn / npm lockfiles; OS packages in the relay build still use Debian repositories, so this is a reproducible development workflow, not a bit-for-bit reproducible image build. -`adapters/signing/` contains the local-mode modifications for the pinned public SigningServer source. `.sources/` is generated and ignored. Bootstrap never overwrites an existing checkout; reruns preserve local backend edits. After editing a backend, rerun `up` to rebuild. To change pins/adapters, save your backend edits and move the generated checkout aside first. Do not deploy the development adapter to production. +`adapters/signing/` contains the local-mode modifications for the pinned public SigningServer source; `adapters/relay/` pins its local build image. `.sources/` is generated and ignored. Bootstrap never overwrites an existing checkout; reruns preserve local backend edits. After editing a backend, rerun `up` to rebuild. To change pins/adapters, save your backend edits and move the generated checkout aside first. Do not deploy the development adapters to production. Only the gateway publishes host ports, all on `127.0.0.1`. Mongo, relay and signing share an internal network with no external egress; the gateway also has a host-access network. Compose waits on service health, following [Docker's startup ordering](https://docs.docker.com/compose/how-tos/startup-order/). Build-time dependency downloads still use the internet. Mobile native integrations are outside Docker; this is not a fully offline mobile mode. diff --git a/dev/local-backend/adapters/relay/files/Dockerfile.local b/dev/local-backend/adapters/relay/files/Dockerfile.local new file mode 100644 index 0000000000..285e1e8e18 --- /dev/null +++ b/dev/local-backend/adapters/relay/files/Dockerfile.local @@ -0,0 +1,12 @@ +FROM node:22-bookworm-slim@sha256:43ac6c60b8f89723f746e8a92ce91abd5017e627ce1ddfe4238355d3a30b772c +WORKDIR /app +RUN apt-get update && apt-get install -y --no-install-recommends git ca-certificates python3 make g++ && rm -rf /var/lib/apt/lists/* +COPY package.json yarn.lock ./ +RUN yarn install --frozen-lockfile --non-interactive --ignore-scripts +COPY tsconfig.json ./ +COPY src ./src +COPY public ./public +RUN yarn compile +ENV NODE_ENV=development +USER node +CMD ["node", "dist/index.js"] diff --git a/dev/local-backend/keeper.py b/dev/local-backend/keeper.py index ffed894485..15dd7b245c 100644 --- a/dev/local-backend/keeper.py +++ b/dev/local-backend/keeper.py @@ -60,6 +60,12 @@ def prepare(only=None): if only is not None and name != only: continue adapter = HERE / 'adapters' / name + adapter_files = [path for path in adapter.rglob('*') if path.is_file() or path.is_symlink()] + if any(path.is_symlink() for path in adapter_files): + raise ValueError(f'{name}: adapter symlinks are not allowed') + actual_assets = {path.relative_to(adapter).as_posix() for path in adapter_files} + if actual_assets != set(spec['assets']): + raise ValueError(f'{name}: adapter file set does not match source lock') for asset, digest in spec['assets'].items(): if hashlib.sha256((adapter / asset).read_bytes()).hexdigest() != digest: raise ValueError(f'{name}: adapter checksum mismatch: {asset}') @@ -87,8 +93,9 @@ def prepare(only=None): 'No existing checkout was replaced.') from error run(['git', '-C', checkout, 'checkout', '--quiet', '--detach', spec['revision']]) if spec['assets']: - run(['git', '-C', checkout, 'apply', '--check', adapter / 'local.patch']) - run(['git', '-C', checkout, 'apply', adapter / 'local.patch']) + if (adapter / 'local.patch').exists(): + run(['git', '-C', checkout, 'apply', '--check', adapter / 'local.patch']) + run(['git', '-C', checkout, 'apply', adapter / 'local.patch']) for asset in (adapter / 'files').rglob('*'): if asset.is_file(): dest = checkout / asset.relative_to(adapter / 'files') @@ -101,8 +108,10 @@ def prepare(only=None): def app_env(): config = settings() template = (HERE / 'app.env.local.example').read_text() - for port, name in [('3000', 'RELAY'), ('4002', 'CHANNEL'), ('3003', 'SIGNING')]: - template = template.replace(f':{port}/', f':{config[f"KEEPER_{name}_PORT"]}/') + ports = {'3000': config['KEEPER_RELAY_PORT'], + '4002': config['KEEPER_CHANNEL_PORT'], + '3003': config['KEEPER_SIGNING_PORT']} + template = re.sub(r':(3000|4002|3003)/', lambda match: f':{ports[match.group(1)]}/', template) dest = APP / '.env.local' if dest.exists(): if dest.read_text() != template: diff --git a/dev/local-backend/sources.lock.json b/dev/local-backend/sources.lock.json index 231f141db1..863f3168b1 100644 --- a/dev/local-backend/sources.lock.json +++ b/dev/local-backend/sources.lock.json @@ -2,7 +2,9 @@ "relay": { "url": "https://github.com/KeeperCommunity/Relay.git", "revision": "3e1a01bd2fd5381d8546229922c431ce50bedad4", - "assets": {} + "assets": { + "files/Dockerfile.local": "6dde6c692430b1a91a10cdd82e6f1562023cc5974ffd4d4f58fe57fa1994407a" + } }, "signing": { "url": "https://github.com/KeeperCommunity/SigningServer.git", diff --git a/dev/local-backend/test_keeper.py b/dev/local-backend/test_keeper.py index 3c80fd9d0c..d8874ad721 100644 --- a/dev/local-backend/test_keeper.py +++ b/dev/local-backend/test_keeper.py @@ -36,6 +36,16 @@ def test_environment_is_exclusive_and_preserves_production(self): self.assertEqual((self.app / '.env.local').read_text(), 'developer-settings\n') self.assertEqual(production.read_text(), 'existing-production-sentinel\n') + def test_swapped_ports_do_not_rewrite_each_other(self): + (self.here / 'app.env.local.example').write_text( + 'RELAY=http://localhost:3000/\nCHANNEL=http://localhost:4002/\n' + 'SIGNING=http://localhost:3003/\n') + (self.here / '.env').write_text('KEEPER_RELAY_PORT=4002\nKEEPER_CHANNEL_PORT=3000\n') + keeper.app_env() + self.assertEqual((self.app / '.env.local').read_text(), + 'RELAY=http://localhost:4002/\nCHANNEL=http://localhost:3000/\n' + 'SIGNING=http://localhost:3003/\n') + def test_adapter_tampering_is_rejected_before_fetch(self): adapter = self.here / 'adapters/relay' adapter.mkdir(parents=True) @@ -47,6 +57,16 @@ def test_adapter_tampering_is_rejected_before_fetch(self): keeper.prepare() command.assert_not_called() + def test_unlisted_adapter_file_is_rejected_before_fetch(self): + adapter = self.here / 'adapters/relay/files' + adapter.mkdir(parents=True) + (adapter / 'unlisted.js').write_text('unexpected') + (self.here / 'sources.lock.json').write_text(json.dumps({'relay': {'assets': {}}})) + with patch.object(keeper, 'run') as command: + with self.assertRaisesRegex(ValueError, 'file set does not match'): + keeper.prepare() + command.assert_not_called() + def test_unknown_existing_checkout_is_not_overwritten(self): source = self.here / '.sources/relay' source.mkdir(parents=True) From 5ab398397a8e353d94879065932ca3c146973803 Mon Sep 17 00:00:00 2001 From: Anant Date: Wed, 7 Oct 2026 17:21:05 +0530 Subject: [PATCH 024/254] Check backup freshness after wallet sync completes --- src/store/sagaActions/wallets.ts | 6 +++-- src/store/sagas/login.ts | 12 +--------- src/store/sagas/wallets.ts | 31 +++++++++++++++++++++---- tests/recovery/backupFreshness.test.cjs | 27 +++++++++++++++++++++ tests/recovery/sagaHarness.cjs | 10 +++++++- 5 files changed, 68 insertions(+), 18 deletions(-) diff --git a/src/store/sagaActions/wallets.ts b/src/store/sagaActions/wallets.ts index 0508d24152..b9078e1e3e 100644 --- a/src/store/sagaActions/wallets.ts +++ b/src/store/sagaActions/wallets.ts @@ -29,13 +29,15 @@ export const getTestcoins = (testWallet: Wallet) => ({ export const autoSyncWallets = ( syncAll?: boolean, hardRefresh?: boolean, - addNotifications?: boolean + addNotifications?: boolean, + backupCheckAppId?: string ) => ({ type: AUTO_SYNC_WALLETS, payload: { syncAll, hardRefresh, addNotifications, + backupCheckAppId, }, }); @@ -145,4 +147,4 @@ export const generateNewAddress = (wallet: Wallet | Vault) => ({ export const updateVaultSignersXpriv = (signers: Signer[]) => ({ type: UPDATED_VAULT_SIGNERS_XPRIV, signers, -}); \ No newline at end of file +}); diff --git a/src/store/sagas/login.ts b/src/store/sagas/login.ts index 35028a9bc8..93465bf271 100644 --- a/src/store/sagas/login.ts +++ b/src/store/sagas/login.ts @@ -51,7 +51,6 @@ import { createWatcher } from '../utilities'; import { fetchExchangeRates } from '../sagaActions/send_and_receive'; import { setLoginMethod } from '../reducers/settings'; import { setSubscription } from 'src/store/sagaActions/settings'; -import { checkBackupFreshness } from '../sagaActions/bhr'; import { uaiChecks } from '../sagaActions/uai'; import { applyUpgradeSequence } from './upgrade'; import { resetSyncing } from '../reducers/wallets'; @@ -228,7 +227,7 @@ function* credentialsAuthWorker({ payload }) { // Wallet/node refresh runs through its watcher after local unlock. // Waiting for network work here can hold the success modal for 15s. yield put(resetSyncing()); - yield put(autoSyncWallets(false, false, true)); + yield put(autoSyncWallets(false, false, true, id)); yield put( uaiChecks([ @@ -241,15 +240,6 @@ function* credentialsAuthWorker({ payload }) { ]) ); - const { - pendingAllBackup, - automaticCloudBackup, - backupRepairCompletedByAppId = {}, - } = yield select((state: RootState) => state.bhr); - // Check without uploading; a mismatch requires Back Up Now. - if (automaticCloudBackup && (pendingAllBackup || !backupRepairCompletedByAppId[id])) { - yield put(checkBackupFreshness()); - } if (!allAccounts.length) { // upgraded app yield put(addAccount(appId)); diff --git a/src/store/sagas/wallets.ts b/src/store/sagas/wallets.ts index d212e033db..cd3d786253 100644 --- a/src/store/sagas/wallets.ts +++ b/src/store/sagas/wallets.ts @@ -71,6 +71,7 @@ import { } from 'src/utils/utilities'; import { COLLABORATIVE_SCHEME } from 'src/screens/SigningDevices/SetupCollaborativeWallet'; import { RootState } from '../store'; +import { checkBackupFreshness } from '../sagaActions/bhr'; import { initiateVaultMigration, @@ -633,7 +634,7 @@ function* refreshWalletsWorker({ }); try { - if (!wallets || wallets.length === 0) return; + if (!wallets || wallets.length === 0) return true; if (!ELECTRUM_CLIENT.isClientConnected) { ElectrumClient.resetCurrentPeerIndex(); @@ -882,6 +883,7 @@ function* refreshWalletsWorker({ yield put(setPendingDustToast(synchedWallet.id)); } } // end for (synchedWalletWithUTXOs) + return true; } catch (err) { if ([ELECTRUM_NOT_CONNECTED_ERR, ELECTRUM_NOT_CONNECTED_ERR_TOR].includes(err?.message)) { yield put( @@ -897,6 +899,7 @@ function* refreshWalletsWorker({ ); captureError(err); } + return false; } finally { yield put(setSyncing({ wallets, isSyncing: false })); } @@ -907,9 +910,14 @@ export const refreshWalletsWatcher = createWatcher(refreshWalletsWorker, REFRESH export function* autoWalletsSyncWorker({ payload, }: { - payload: { syncAll?: boolean; hardRefresh?: boolean; addNotifications?: boolean }; + payload: { + syncAll?: boolean; + hardRefresh?: boolean; + addNotifications?: boolean; + backupCheckAppId?: string; + }; }) { - const { syncAll, hardRefresh, addNotifications } = payload; + const { syncAll, hardRefresh, addNotifications, backupCheckAppId } = payload; const wallets: Wallet[] = yield call(dbManager.getObjectByIndex, RealmSchema.Wallet, null, true); const vaults: Vault[] = yield call(dbManager.getObjectByIndex, RealmSchema.Vault, null, true); const { bitcoinNetworkType } = yield select((state: RootState) => state.settings); @@ -923,8 +931,9 @@ export function* autoWalletsSyncWorker({ } walletsToSync = walletsToSync.filter((wallet) => wallet.networkType === bitcoinNetworkType); + let refreshSucceeded = true; if (walletsToSync.length) { - yield call(refreshWalletsWorker, { + refreshSucceeded = yield call(refreshWalletsWorker, { payload: { wallets: walletsToSync, options: { @@ -934,6 +943,20 @@ export function* autoWalletsSyncWorker({ }, }); } + // Check backup freshness only after the refreshed wallet specs are persisted. + // A failed refresh or account switch must not verify a stale snapshot. + if (backupCheckAppId && refreshSucceeded) { + const { appId } = yield select((state: RootState) => state.storage); + const { automaticCloudBackup, pendingAllBackup, backupRepairCompletedByAppId = {} } = + yield select((state: RootState) => state.bhr); + if ( + appId === backupCheckAppId && + automaticCloudBackup && + (pendingAllBackup || !backupRepairCompletedByAppId[appId]) + ) { + yield put(checkBackupFreshness()); + } + } } export const autoWalletsSyncWatcher = createWatcher(autoWalletsSyncWorker, AUTO_SYNC_WALLETS); diff --git a/tests/recovery/backupFreshness.test.cjs b/tests/recovery/backupFreshness.test.cjs index 03ff4b9ab2..e03ad47b90 100644 --- a/tests/recovery/backupFreshness.test.cjs +++ b/tests/recovery/backupFreshness.test.cjs @@ -3,6 +3,33 @@ const assert = require('node:assert/strict'); const { fixture, enums, encryption, wallet, loadModule } = require('./sagaHarness.cjs'); const { NetworkType, WalletType } = enums; +test('login backup inspection waits for wallet refresh to finish', async () => { + let finishSync; + const syncGate = new Promise((resolve) => { finishSync = resolve; }); + const f = fixture({ bhr: { pendingAllBackup: true }, syncGate }); + f.collections.Wallet.push(wallet('pending', NetworkType.MAINNET)); + const running = f.run('autoWalletsSyncWorker', { + payload: { syncAll: false, hardRefresh: false, addNotifications: true, backupCheckAppId: f.app.id }, + }); + await new Promise((resolve) => setImmediate(resolve)); + assert.deepEqual(f.calls.map(([type]) => type), ['sync']); + assert.equal(f.actions.some((action) => action.type === 'checkBackupFreshness'), false); + finishSync(); + await running; + assert.equal(f.actions.filter((action) => action.type === 'checkBackupFreshness').length, 1); +}); + +test('failed refresh and account switch do not verify backup freshness', async () => { + for (const option of ['syncFails', 'switchAppDuringSync']) { + const f = fixture({ bhr: { pendingAllBackup: true }, [option]: true }); + f.collections.Wallet.push(wallet('pending', NetworkType.MAINNET)); + await f.run('autoWalletsSyncWorker', { + payload: { backupCheckAppId: f.app.id }, + }); + assert.equal(f.actions.some((action) => action.type === 'checkBackupFreshness'), false); + } +}); + for (const network of [NetworkType.MAINNET, NetworkType.TESTNET]) { test(`wallet added after initial backup survives encrypted restore: ${network}`, async () => { const f = fixture(); diff --git a/tests/recovery/sagaHarness.cjs b/tests/recovery/sagaHarness.cjs index ab82a1d9c4..37cc5412d7 100644 --- a/tests/recovery/sagaHarness.cjs +++ b/tests/recovery/sagaHarness.cjs @@ -77,6 +77,7 @@ const signer = (fingerprint, networkType, type = enums.SignerType.COLDCARD, acco function fixture(options = {}) { const appId = require('crypto').createHash('sha256').update(Buffer.from('ab'.repeat(32), 'hex')).digest('hex'); const state = { + storage: { appId }, bhr: { automaticCloudBackup: true, pendingAllBackup: false, @@ -213,6 +214,12 @@ function fixture(options = {}) { semver: require('semver'), setupRecoveryKeySigningKey: () => signer('RECOVERY', NetworkType.MAINNET, enums.SignerType.SEED_WORDS), applyUpgradeSequence: () => {}, + refreshWalletsWorker: async () => { + calls.push(['sync']); + if (options.syncGate) await options.syncGate; + if (options.switchAppDuringSync) state.storage.appId = 'another-app'; + return options.syncFails !== true; + }, }; const names = [ 'setBackupRepairCompleted', @@ -235,6 +242,7 @@ function fixture(options = {}) { 'setAppId', 'setAppCreated', 'autoSyncWallets', + 'checkBackupFreshness', 'uaiChecks', 'loadConciergeUser', 'loadConciergeTickets', @@ -264,7 +272,7 @@ function fixture(options = {}) { functions('src/store/sagas/wallets.ts', [ 'addNewWalletsWorker', 'addSigningDeviceWorker', 'mergeSimilarKeysWorker', 'updateVaultSignerXprivWorker', 'updateSignerDetailsWorker', - 'updateKeyDetailsWorker', + 'updateKeyDetailsWorker', 'autoWalletsSyncWorker', ]); vm.runInNewContext( ts.transpileModule(code, { compilerOptions: { target: ts.ScriptTarget.ES2020 } }).outputText, From 2214d589472f9e161f35b24cfb76d45dc208f30b Mon Sep 17 00:00:00 2001 From: Anant Date: Wed, 7 Oct 2026 17:30:40 +0530 Subject: [PATCH 025/254] Record product signoff separately from developer review --- .github/pull_request_template.md | 6 ++++++ CONTRIBUTING.md | 7 ++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index 45f51ccc3d..91cd9f43e4 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -27,3 +27,9 @@ Target branch: Keeper maintainers own security review, acceptance, merging and official releases. Passing checks does not constitute approval. + +## Signoff + +- Copilot review findings: +- Developer approval: +- Product owner signoff: diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 171c3812fd..473f17740f 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -107,9 +107,10 @@ official builds. Contributors do not need release permissions to participate. Copilot code review is requested automatically for pull requests targeting `sprint`. Read its actual comments and check each claim against the changed code and relevant tests. Fix valid findings or explain why a comment does not apply -before requesting final human review. A developer's approval remains the code -review gate. The product owner may record product signoff in the PR discussion; -signoff from the PR author does not count as GitHub's required approval. +before requesting final human review. One developer approval remains the code +review gate. For changes to product behavior, UX, or copy, the product owner +records separate product signoff in the PR discussion. A PR author's product +signoff does not count as GitHub's required developer approval. Some existing CI checks use maintainer-managed services or secrets. If a check is unavailable on a fork PR, report it in the PR so maintainers can arrange review; From 90d2e24ecf6e4216f52db1d10d78a5cf24cf1e8c Mon Sep 17 00:00:00 2001 From: Anant Date: Wed, 7 Oct 2026 17:51:42 +0530 Subject: [PATCH 026/254] Request Ben and Utkarsh as sprint code owners --- .github/CODEOWNERS | 2 ++ CONTRIBUTING.md | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 .github/CODEOWNERS diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS new file mode 100644 index 0000000000..3dbbb97c58 --- /dev/null +++ b/.github/CODEOWNERS @@ -0,0 +1,2 @@ +# Request both developers for changes targeting sprint; one approval is required. +* @ben-kaufman @cakesoft-utkarsh diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 473f17740f..2be9cd61d7 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -108,7 +108,9 @@ Copilot code review is requested automatically for pull requests targeting `sprint`. Read its actual comments and check each claim against the changed code and relevant tests. Fix valid findings or explain why a comment does not apply before requesting final human review. One developer approval remains the code -review gate. For changes to product behavior, UX, or copy, the product owner +review gate. GitHub requests Ben and Utkarsh when a PR is ready for review; +either developer can provide that approval. For changes to product behavior, +UX, or copy, the product owner records separate product signoff in the PR discussion. A PR author's product signoff does not count as GitHub's required developer approval. From 934f71275d5306af3129c483dcc7948ac0271c42 Mon Sep 17 00:00:00 2001 From: Anant Date: Wed, 7 Oct 2026 18:01:33 +0530 Subject: [PATCH 027/254] Include Parsh in developer review requests --- .github/CODEOWNERS | 4 ++-- CONTRIBUTING.md | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 3dbbb97c58..f1f3d0d712 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -1,2 +1,2 @@ -# Request both developers for changes targeting sprint; one approval is required. -* @ben-kaufman @cakesoft-utkarsh +# Request the three developers for changes targeting sprint; one approval is required. +* @ben-kaufman @cakesoft-utkarsh @Parsh diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 2be9cd61d7..d7cec89098 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -108,8 +108,8 @@ Copilot code review is requested automatically for pull requests targeting `sprint`. Read its actual comments and check each claim against the changed code and relevant tests. Fix valid findings or explain why a comment does not apply before requesting final human review. One developer approval remains the code -review gate. GitHub requests Ben and Utkarsh when a PR is ready for review; -either developer can provide that approval. For changes to product behavior, +review gate. GitHub requests Ben, Utkarsh, and Parsh when a PR is ready for review; +any one can provide that approval. For changes to product behavior, UX, or copy, the product owner records separate product signoff in the PR discussion. A PR author's product signoff does not count as GitHub's required developer approval. From 8fa6bf7446bd828b8c446503312eec0566d8b400 Mon Sep 17 00:00:00 2001 From: Anant Date: Wed, 7 Oct 2026 18:20:05 +0530 Subject: [PATCH 028/254] Retry transient read-only backup checks within one deadline --- src/services/backup/repair.ts | 24 ++++++-- tests/recovery/helpers.cjs | 5 +- tests/recovery/repair.test.cjs | 101 ++++++++++++++++++++++++++++++++- 3 files changed, 123 insertions(+), 7 deletions(-) diff --git a/src/services/backup/repair.ts b/src/services/backup/repair.ts index 9d9c2f80c8..865cd213ec 100644 --- a/src/services/backup/repair.ts +++ b/src/services/backup/repair.ts @@ -160,10 +160,12 @@ export function inspectBackup( if (active) active.phase = phase; listeners.forEach((listener) => listener(phase)); }; + let deadline: number; const inspectOnce = async (allowRetry: boolean): Promise => { let stage: InspectionStage = 'local-capture'; let changedRevision = 0; - const deadline = Date.now() + BACKUP_DEADLINE_MS; + // One deadline covers both read-only attempts; a retry must not double it. + deadline ??= Date.now() + BACKUP_DEADLINE_MS; const revision = backupRevision(appId); const assertCurrent = () => { if ( @@ -301,12 +303,26 @@ export function inspectBackup( return 'verified'; } catch (error) { // A routine incremental write can be queued while a read-only check is - // running. Retry behind that write once before declaring the check failed. + // running. A transient snapshot request can also fail while the backup is + // healthy. Retry either case once behind queued writes, within the same + // deadline. Never retry a repair, invalid response, or account switch. + let accountIsCurrent = false; + try { + accountIsCurrent = (dbManager.getObjectByIndex(RealmSchema.KeeperApp) as any)?.id === appId; + } catch { + // Realm may already be closed during an account switch. + } + const queuedWriteChanged = changedRevision && backupQueuedRevision(appId) >= changedRevision; + const transientSnapshotFailure = + stage === 'snapshot-request' && + error instanceof BackupRequestError && + ['http-5xx', 'network', 'timeout'].includes(error.category); if ( - changedRevision && allowRetry && !repair && - backupQueuedRevision(appId) >= changedRevision + accountIsCurrent && + Date.now() < deadline && + (queuedWriteChanged || transientSnapshotFailure) ) return 'retry'; // Only log fixed categories. Errors and responses can contain encrypted diff --git a/tests/recovery/helpers.cjs b/tests/recovery/helpers.cjs index 6950fb892f..99e48d7bf9 100644 --- a/tests/recovery/helpers.cjs +++ b/tests/recovery/helpers.cjs @@ -41,7 +41,8 @@ function harness(options = {}) { if (options.lostResponse) throw Error('response lost after commit'); return { data: { updated: true } }; } }; - const transport = loadModule('src/services/backup/transport.ts', { '../rest/RestClient': rest }); + const clock = options.now ? { Date: { now: options.now } } : {}; + const transport = loadModule('src/services/backup/transport.ts', { '../rest/RestClient': rest }, clock); const repair = loadModule('src/services/backup/repair.ts', { 'src/storage/realm/dbManager': db, 'src/storage/realm/enum': { RealmSchema: new Proxy({}, { get: (_, name) => name }) }, @@ -49,7 +50,7 @@ function harness(options = {}) { 'src/utils/service-utilities/encryption': encryption, 'src/utils/utilities': { getKeyUID: s => s.id || s.masterFingerprint, sanitizeSeedKeyForBackup: s => s, sanitizeVaultSignersForSeedKeyBackup: s => s }, './transport': transport, './image': image, - }, { console: { warn: (...args) => diagnostics.push(args) } }); + }, { ...clock, console: { warn: (...args) => diagnostics.push(args) } }); return { app, local, remote, calls, phases, diagnostics, transport, repair, run: (write = false) => repair.inspectBackup(app.id, write, phase => phases.push(phase)) }; } diff --git a/tests/recovery/repair.test.cjs b/tests/recovery/repair.test.cjs index 59e97921e3..68883e696e 100644 --- a/tests/recovery/repair.test.cjs +++ b/tests/recovery/repair.test.cjs @@ -1,6 +1,6 @@ const { test } = require('node:test'); const assert = require('node:assert/strict'); -const { encryption, image, makeImage, harness, wallet } = require('./helpers.cjs'); +const { encryption, image, makeImage, harness, wallet, loadModule } = require('./helpers.cjs'); test('upgrade check detects omissions without uploading; explicit repair verifies readback', async () => { const f = harness(); f.local.Wallet.push(wallet()); @@ -42,6 +42,105 @@ for (const [error, category] of [ assert.ok(!JSON.stringify(f.diagnostics).includes('private-fixture-identifier')); }); +for (const [error, category] of [ + [{ response: { status: 503, data: 'private-fixture-identifier' } }, 'http-5xx'], + [{ code: 'ERR_NETWORK', message: 'private-fixture-identifier' }, 'network'], + [{ code: 'ECONNABORTED', message: 'private-fixture-identifier' }, 'timeout'], +]) test(`read-only ${category} failure retries once and verifies the existing backup without upload`, async () => { + let calls = 0; + const f = harness({ post: async ({ path }) => { + if (path.endsWith('getBackupSnapshot') && ++calls === 1) throw error; + } }); + assert.equal(await f.run(), 'verified'); + assert.deepEqual(f.calls, ['getBackupSnapshot', 'getBackupSnapshot']); + assert.deepEqual(f.phases, ['checking', 'checking', 'verified']); + assert.deepEqual(f.diagnostics, []); +}); + +for (const os of ['ios', 'android']) test(`${os} RestClient preserves the read-only retry and abortable transport`, async () => { + const requests = []; + const rest = loadModule('src/services/rest/RestClient.ts', { + axios: { post: async (path, body, options) => { + requests.push({ path, body, options }); + if (requests.length === 1) throw { code: 'ERR_NETWORK' }; + return { data: { + appImage: { appId: 'disposable', wallets: {}, signers: {}, nodes: [], vaults: [], labels: [] }, + allVaultImages: [], labels: [], revision: 'a'.repeat(64), + } }; + } }, + 'react-native-device-info': { getVersion: () => 'fixture', getBuildNumber: () => 'fixture' }, + 'react-native': { Platform: { OS: os } }, + 'src/utils/service-utilities/config': { HEXA_ID: 'fixture-only' }, + }).default; + const f = harness({ adapter: (path, body, options) => rest.post(path, body, undefined, options) }); + assert.equal(await f.run(), 'verified'); + assert.deepEqual(requests.map(({ path }) => path), ['getBackupSnapshot', 'getBackupSnapshot']); + for (const { body, options } of requests) { + assert.deepEqual(Object.keys(body), ['appId']); + assert.equal(options.headers.os, os); + assert.equal(options.signal.aborted, false); + assert.equal(typeof options.onDownloadProgress, 'function'); + } +}); + +test('read-only retry shares the first attempt deadline', async () => { + let now = 0, reads = 0; + const f = harness({ now: () => now, post: async ({ path }) => { + if (path.endsWith('getBackupSnapshot')) { + now += 300_000; + if (++reads === 1) throw { code: 'ERR_NETWORK' }; + } + } }); + assert.equal(await f.run(), 'unverified'); + assert.deepEqual(f.calls, ['getBackupSnapshot', 'getBackupSnapshot']); + assert.deepEqual(f.phases, ['checking', 'checking', 'unverified']); +}); + +test('expired inspection makes no transient retry', async () => { + let now = 0; + const f = harness({ now: () => now, post: async () => { + now = 600_000; + throw { code: 'ERR_NETWORK' }; + } }); + assert.equal(await f.run(), 'unverified'); + assert.deepEqual(f.calls, ['getBackupSnapshot']); +}); + +test('persistent network failures stop after one read-only retry', async () => { + const f = harness({ adapter: async () => { throw { code: 'ERR_NETWORK' }; } }); + assert.equal(await f.run(), 'unverified'); + assert.deepEqual(f.calls, ['getBackupSnapshot', 'getBackupSnapshot']); + assert.deepEqual(f.phases, ['checking', 'checking', 'unverified']); + assert.deepEqual(f.diagnostics, [['Assisted Server Backup check failed at stage:', 'snapshot-request', 'network']]); +}); + +test('4xx responses and invalid snapshots never retry', async () => { + for (const options of [ + { adapter: async () => { throw { response: { status: 404 } }; } }, + { response: { revision: 'a'.repeat(64) } }, + ]) { + const f = harness(options); + assert.equal(await f.run(), 'unverified'); + assert.deepEqual(f.calls, ['getBackupSnapshot']); + } +}); + +test('transient snapshot failure after account switch makes no retry or upload', async () => { + const f = harness({ post: async ({ app }) => { + app.id = 'second'; + throw { code: 'ERR_NETWORK' }; + } }); + assert.equal(await f.run(), 'unverified'); + assert.deepEqual(f.calls, ['getBackupSnapshot']); +}); + +test('repair does not retry a failed snapshot request', async () => { + const f = harness({ adapter: async () => { throw { code: 'ERR_NETWORK' }; } }); + f.local.Wallet.push(wallet()); + assert.equal(await f.run(true), 'unverified'); + assert.deepEqual(f.calls, ['getBackupSnapshot']); +}); + test('read-only check retries once after a concurrent incremental backup changes its revision', async () => { let f, changed = false, write; f = harness({ post: async ({ path, remote }) => { From 4c5e3353371b15dca6c1c04aabf6dd17b5f84f5e Mon Sep 17 00:00:00 2001 From: Anant Date: Wed, 7 Oct 2026 18:33:14 +0530 Subject: [PATCH 029/254] Verify revisioned backups with audited public backend sources --- dev/local-backend/README.md | 4 +++- dev/local-backend/compose.yaml | 7 ++++--- dev/local-backend/sources.lock.json | 4 ++-- dev/local-backend/verify.py | 30 +++++++++++++++++++++++++++++ 4 files changed, 39 insertions(+), 6 deletions(-) diff --git a/dev/local-backend/README.md b/dev/local-backend/README.md index f74eb4c29a..a373452171 100644 --- a/dev/local-backend/README.md +++ b/dev/local-backend/README.md @@ -131,7 +131,7 @@ Skip `simctl boot` if already booted. Keep simulator signing enabled so the app ./dev/local-backend/verify-local.sh --persistence --boundaries ``` -This recreates only the selected Compose project's containers, retains its volumes, and compares the same app record and testnet signing public key. It also checks startup rejection of mainnet and hosted databases. Run when no other test is using that project. Ordinary `verify` does not restart containers. Every verify run adds a disposable app/signer fixture to the local database; no testnet coins are needed and no transaction is sent. Tokens and private keys are not printed. +This recreates only the selected Compose project's containers, retains its volumes, and compares the same app record, revisioned backup and testnet signing public key. It verifies backup snapshot reads, transactional repair and rejection of stale revisions, and checks startup rejection of mainnet and hosted databases. Run when no other test is using that project. Ordinary `verify` does not restart containers. Every verify run adds a disposable app/signer fixture and synthetic encrypted backup records to the local database; no testnet coins are needed and no transaction is sent. Tokens and private keys are not printed. For native smoke testing: launch the development app, complete onboarding with a disposable wallet, unlock, open Wallets and More Options, and select testnet before testing network functionality. An API smoke pass does not prove wallet backup/recovery or a real signing transaction. @@ -158,6 +158,8 @@ The relay health field `signing: false` describes the relay, not the separate si Only the gateway publishes host ports, all on `127.0.0.1`. Mongo, relay and signing share an internal network with no external egress; the gateway also has a host-access network. Compose waits on service health, following [Docker's startup ordering](https://docs.docker.com/compose/how-tos/startup-order/). Build-time dependency downloads still use the internet. Mobile native integrations are outside Docker; this is not a fully offline mobile mode. +Mongo starts as the single-member `keeper-local` replica set required by the backup API's transactions. Its idempotent health check initializes a new local volume and waits for a writable primary. Existing local data volumes are retained; a standalone Mongo server cannot exercise transactional backup repair. + Signing identities are randomly generated per signing volume and stored with restricted permissions. Use disposable test wallets. Keep the Mongo and signing volumes together: deleting signing keys invalidates their stored Server Key records. There is deliberately no automatic reset/delete command. `docker compose down --volumes` is destructive and not part of normal setup. Do not distribute databases, keys, email captures, `.env` files or native signing credentials with the source. ## Troubleshooting diff --git a/dev/local-backend/compose.yaml b/dev/local-backend/compose.yaml index e26754b4c7..be79d65d3a 100644 --- a/dev/local-backend/compose.yaml +++ b/dev/local-backend/compose.yaml @@ -2,11 +2,12 @@ name: ${COMPOSE_PROJECT_NAME:-keeper-contributor} services: mongo: image: mongo:7.0@sha256:9854f7139445d766a9523571d6f047530c45547460ffcf8259eb2bf4264632ca + command: [mongod, --replSet, keeper-local, --bind_ip_all] volumes: - mongo-data:/data/db networks: [local-only] healthcheck: - test: [CMD, mongosh, --quiet, --eval, "quit(db.adminCommand('ping').ok ? 0 : 1)"] + test: [CMD, mongosh, --quiet, --eval, "try { rs.status(); } catch (e) { if (e.code !== 94) throw e; rs.initiate({_id: 'keeper-local', members: [{_id: 0, host: 'mongo:27017'}]}); } quit(db.hello().isWritablePrimary ? 0 : 1)"] interval: 5s timeout: 5s retries: 20 @@ -19,7 +20,7 @@ services: ENVIRONMENT: DEVELOPMENT BITCOIN_NETWORK: TESTNET VERSION: local-development - DATABASE_URL: mongodb://mongo:27017/keeper_local + DATABASE_URL: mongodb://mongo:27017/keeper_local?replicaSet=keeper-local PORT: "3000" WS_PORT: "4002" HEXA_ID: keeper-local-only @@ -47,7 +48,7 @@ services: VERSION: local-development HEXA_ID: keeper-local-only BIT_ADMIN_KEY: keeper-local-admin-only - LOCAL_DATABASE_URL: mongodb://mongo:27017/keeper_signing_local + LOCAL_DATABASE_URL: mongodb://mongo:27017/keeper_signing_local?replicaSet=keeper-local RELAY: http://relay:3000/ MNEMONIC_IDENTIFIER_V2: local-signer-v2 MNEMONIC_IDENTIFIER_V3: local-signer-v3 diff --git a/dev/local-backend/sources.lock.json b/dev/local-backend/sources.lock.json index 863f3168b1..f9a926960d 100644 --- a/dev/local-backend/sources.lock.json +++ b/dev/local-backend/sources.lock.json @@ -1,14 +1,14 @@ { "relay": { "url": "https://github.com/KeeperCommunity/Relay.git", - "revision": "3e1a01bd2fd5381d8546229922c431ce50bedad4", + "revision": "af3248acb9548a6e03eb3d1ee13b2f24d8a47651", "assets": { "files/Dockerfile.local": "6dde6c692430b1a91a10cdd82e6f1562023cc5974ffd4d4f58fe57fa1994407a" } }, "signing": { "url": "https://github.com/KeeperCommunity/SigningServer.git", - "revision": "b82dc4f4a8f75676b70b12545d59c7906556a92b", + "revision": "3cc7728c27328e7b0358d1f91a32d8d13feba90a", "assets": { "files/.dockerignore": "78d9a1e03c57cfc1c7fb79ff2335aa2393bb075d0ac4a23feae81371728190be", "files/Dockerfile.local": "04a0bfca3903eb1363f8804b7c181424747c949aed543a89fe7a02316ccafa3d", diff --git a/dev/local-backend/verify.py b/dev/local-backend/verify.py index e81932aaf6..2595ba02eb 100644 --- a/dev/local-backend/verify.py +++ b/dev/local-backend/verify.py @@ -5,6 +5,7 @@ import hashlib import hmac import json +import re import struct import subprocess import sys @@ -75,6 +76,31 @@ def boundaries(): print('PASS mainnet and hosted database configuration rejected') +def backup_roundtrip(relay, fixture): + snapshot = request(relay, '/getBackupSnapshot', {'appId': fixture}) + require(snapshot.get('exists') is True and + re.fullmatch(r'[a-f0-9]{64}', snapshot.get('revision', '')), + 'Created app backup snapshot unavailable') + replacement = {'appId': fixture, 'publicId': fixture, 'version': '2.6.3', + 'expectedRevision': snapshot['revision'], + 'walletObject': {'qa': 'synthetic-encrypted-wallet'}, + 'signersObject': {}, 'vaultObject': {}, 'nodes': [], 'labels': []} + result = request(relay, '/repairAppBackup', replacement) + require(result.get('updated') is True, 'Revisioned backup repair failed') + repaired = request(relay, '/getBackupSnapshot', {'appId': fixture}) + require(repaired.get('revision') != snapshot['revision'] and + repaired.get('appImage', {}).get('wallets', {}).get('qa') == + replacement['walletObject']['qa'], 'Backup repair did not persist its image') + stale = dict(replacement, walletObject={'qa': 'stale-replacement-must-not-persist'}) + result = request(relay, '/repairAppBackup', stale, expected=409) + require(result.get('updated') is False and result.get('error') == 'BACKUP_CHANGED', + 'Stale backup revision was accepted') + require(request(relay, '/getBackupSnapshot', {'appId': fixture}) == repaired, + 'Rejected stale repair changed the backup') + print('PASS revisioned backup create/read and stale-revision preservation') + return repaired + + def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('--persistence', action='store_true', help='Recreate this Compose stack, retaining volumes, and recheck fixtures') @@ -106,6 +132,8 @@ def main(): app['appImage'].get('publicId') == fixture and app['appImage'].get('_id'), 'Persisted app record round trip failed') print('PASS relay app record create/read') + backup = backup_roundtrip(relay, fixture) + app = request(relay, '/getAppImage', {'appId': fixture}) rejected = request(signing, '/v3/setupSigner', {'HEXA_ID': 'invalid'}, expected=400) require(rejected.get('err') == 'Unauthorized request', 'Signing authorization failed') result = request(signing, '/v3/setupSigner', {'HEXA_ID': 'keeper-local-only', 'policy': {'verification': {'method': 'TWO_FA'}, @@ -134,6 +162,8 @@ def main(): compose('up', '-d', '--wait', '--wait-timeout', '180') health(relay, signing) require(request(relay, '/getAppImage', {'appId': fixture}) == app, 'App data changed after recreation') + require(request(relay, '/getBackupSnapshot', {'appId': fixture}) == backup, + 'Revisioned backup changed after recreation') auth['verificationToken'] = token(secret) restored = request(signing, '/v3/fetchSignerSetup', auth) require(restored.get('valid') is True and restored.get('xpub') == data['bhXpub'], 'Signing identity changed after recreation') From d61d60ab1dd9e7e0a46415b990f63028659a9dd7 Mon Sep 17 00:00:00 2001 From: Anant Date: Wed, 7 Oct 2026 18:38:19 +0530 Subject: [PATCH 030/254] Keep Desktop QR scans recoverable and verify channel crypto contract --- src/screens/QRScreens/RegisterWithChannel.tsx | 66 ++++++++++--------- src/screens/QRScreens/SignWithChannel.tsx | 62 ++++++++--------- .../channel/ChannelRequestScanner.tsx | 28 ++++++++ src/services/channel/crypto.ts | 45 +++++++++++++ src/utils/service-utilities/utils.ts | 38 +---------- tests/channel/ChannelRequestScanner.test.tsx | 65 ++++++++++++++++++ tests/channel/crypto.test.ts | 57 ++++++++++++++++ tests/channel/desktop-crypto-vectors.json | 40 +++++++++++ 8 files changed, 301 insertions(+), 100 deletions(-) create mode 100644 src/services/channel/ChannelRequestScanner.tsx create mode 100644 src/services/channel/crypto.ts create mode 100644 tests/channel/ChannelRequestScanner.test.tsx create mode 100644 tests/channel/crypto.test.ts create mode 100644 tests/channel/desktop-crypto-vectors.json diff --git a/src/screens/QRScreens/RegisterWithChannel.tsx b/src/screens/QRScreens/RegisterWithChannel.tsx index f678dadc36..6bd925e779 100644 --- a/src/screens/QRScreens/RegisterWithChannel.tsx +++ b/src/screens/QRScreens/RegisterWithChannel.tsx @@ -22,33 +22,28 @@ import { import useSignerFromKey from 'src/hooks/useSignerFromKey'; import { hcStatusType } from 'src/models/interfaces/HeathCheckTypes'; import { healthCheckStatusUpdate } from 'src/store/sagaActions/bhr'; -import QRScanner from 'src/components/QRScanner'; +import ChannelRequestScanner from 'src/services/channel/ChannelRequestScanner'; import { VaultType } from 'src/services/wallets/enums'; import BackgroundTimer from 'react-native-background-timer'; import { useAppSelector } from 'src/store/hooks'; import WalletHeader from 'src/components/WalletHeader'; import { LocalizationContext } from 'src/context/Localization/LocContext'; +import useToastMessage from 'src/hooks/useToastMessage'; +import ToastErrorIcon from 'src/assets/images/toast_error.svg'; function ScanAndInstruct({ onBarCodeRead }) { const { colorMode } = useColorMode(); - const [channelCreated, setChannelCreated] = useState(false); const { translations } = useContext(LocalizationContext); const { vault: vaultTranslation } = translations; - - const callback = (data) => { - onBarCodeRead(data); - setChannelCreated(true); - }; - return !channelCreated ? ( - - ) : ( - // TODO: Move this to a component - - - {vaultTranslation.registerVaultFromDesktop} - - - + return ( + + + + {vaultTranslation.registerVaultFromDesktop} + + + + ); } @@ -64,7 +59,8 @@ function RegisterWithChannel() { }; const { signer } = useSignerFromKey(vaultKey); const { translations } = useContext(LocalizationContext); - const { choosePlan } = translations; + const { choosePlan, error: errorText } = translations; + const { showToast } = useToastMessage(); const [channel] = useState(io(config.CHANNEL_URL)); const decryptionKey = useRef(); @@ -84,20 +80,26 @@ function RegisterWithChannel() { const firstExtAdd = vault.specs.addresses.external[0]; // for cross validation from desktop app. const onBarCodeRead = (data) => { - decryptionKey.current = data; - const sha = crypto.createHash('sha256'); - sha.update(data); - const room = sha.digest().toString('hex'); - const requestBody = { - action: EMIT_MODES.REGISTER_MULTISIG, - signerType, - descriptorString, - miniscriptPolicy, - walletName, - firstExtAdd, - }; - const requestData = createCipherGcm(JSON.stringify(requestBody), decryptionKey.current); - channel.emit(JOIN_CHANNEL, { room, network: bitcoinNetworkType, requestData }); + try { + decryptionKey.current = data; + const sha = crypto.createHash('sha256'); + sha.update(data); + const room = sha.digest().toString('hex'); + const requestBody = { + action: EMIT_MODES.REGISTER_MULTISIG, + signerType, + descriptorString, + miniscriptPolicy, + walletName, + firstExtAdd, + }; + const requestData = createCipherGcm(JSON.stringify(requestBody), decryptionKey.current); + channel.emit(JOIN_CHANNEL, { room, network: bitcoinNetworkType, requestData }); + return true; + } catch { + showToast(errorText.failedToConnectDesktop, ); + return false; + } }; useEffect(() => { diff --git a/src/screens/QRScreens/SignWithChannel.tsx b/src/screens/QRScreens/SignWithChannel.tsx index 9983e3da1e..356a5322ec 100644 --- a/src/screens/QRScreens/SignWithChannel.tsx +++ b/src/screens/QRScreens/SignWithChannel.tsx @@ -26,7 +26,7 @@ import { import useSignerFromKey from 'src/hooks/useSignerFromKey'; import { getPsbtForHwi } from 'src/hardware'; import { hcStatusType } from 'src/models/interfaces/HeathCheckTypes'; -import QRScanner from 'src/components/QRScanner'; +import ChannelRequestScanner from 'src/services/channel/ChannelRequestScanner'; import { updateKeyDetails } from 'src/store/sagaActions/wallets'; import BackgroundTimer from 'react-native-background-timer'; import WalletHeader from 'src/components/WalletHeader'; @@ -37,23 +37,17 @@ import { validatePSBT } from 'src/utils/utilities'; function ScanAndInstruct({ onBarCodeRead }) { const { colorMode } = useColorMode(); - const [channelCreated, setChannelCreated] = useState(false); const { translations } = useContext(LocalizationContext); const { choosePlan } = translations; - - const callback = (data) => { - onBarCodeRead(data); - setChannelCreated(true); - }; - return !channelCreated ? ( - - ) : ( - - - {choosePlan.continuesigntransWithDesktop} - - - + return ( + + + + {choosePlan.continuesigntransWithDesktop} + + + + ); } @@ -107,21 +101,27 @@ function SignWithChannel() { } const onBarCodeRead = async (data) => { - decryptionKey.current = data; - const sha = crypto.createHash('sha256'); - sha.update(data); - const room = sha.digest().toString('hex'); - const psbt = await getPsbtForHwi(serializedPSBT, activeVault); - const requestBody = { - action: EMIT_MODES.SIGN_TX, - signerType, - psbt, - miniscriptPolicy, - walletName, - hmac, - }; - const requestData = createCipherGcm(JSON.stringify(requestBody), decryptionKey.current); - channel.emit(JOIN_CHANNEL, { room, network: bitcoinNetworkType, requestData }); + try { + decryptionKey.current = data; + const sha = crypto.createHash('sha256'); + sha.update(data); + const room = sha.digest().toString('hex'); + const psbt = await getPsbtForHwi(serializedPSBT, activeVault); + const requestBody = { + action: EMIT_MODES.SIGN_TX, + signerType, + psbt, + miniscriptPolicy, + walletName, + hmac, + }; + const requestData = createCipherGcm(JSON.stringify(requestBody), decryptionKey.current); + channel.emit(JOIN_CHANNEL, { room, network: bitcoinNetworkType, requestData }); + return true; + } catch { + showToast(errorText.failedToConnectDesktop, ); + return false; + } }; useEffect(() => { diff --git a/src/services/channel/ChannelRequestScanner.tsx b/src/services/channel/ChannelRequestScanner.tsx new file mode 100644 index 0000000000..3ea583a540 --- /dev/null +++ b/src/services/channel/ChannelRequestScanner.tsx @@ -0,0 +1,28 @@ +import React, { ReactNode, useRef, useState } from 'react'; +import QRScanner from 'src/components/QRScanner'; + +// Keep the scanner available until the request has actually been prepared and sent. +export default function ChannelRequestScanner({ + onScanCompleted, + children, +}: { + onScanCompleted: (data: string) => boolean | Promise; + children: ReactNode; +}) { + const [requestSent, setRequestSent] = useState(false); + const preparing = useRef(false); + const sent = useRef(false); + const onScan = async (data: string) => { + if (preparing.current || sent.current) return; + preparing.current = true; + try { + if (await onScanCompleted(data)) { + sent.current = true; + setRequestSent(true); + } + } finally { + preparing.current = false; + } + }; + return requestSent ? <>{children} : ; +} diff --git a/src/services/channel/crypto.ts b/src/services/channel/crypto.ts new file mode 100644 index 0000000000..05f3cdc91c --- /dev/null +++ b/src/services/channel/crypto.ts @@ -0,0 +1,45 @@ +// Shared with Keeper Desktop: AES-256-GCM, 12-byte IV and 16-byte authentication tag. +const crypto = require('crypto'); + +const pairingKey = (password: string) => { + if (!/^[a-f0-9]{64}$/i.test(password)) throw new Error('Invalid Desktop pairing code'); + return Buffer.from(password, 'hex'); +}; + +export const createCipherGcm = (data: string, password: string) => { + const algorithm = 'aes-256-gcm'; + const key = pairingKey(password); + const iv = crypto.randomBytes(12); // 12 bytes for GCM + const cipher = crypto.createCipheriv(algorithm, key, iv); + const encrypted = Buffer.concat([cipher.update(data, 'utf8'), cipher.final()]); + const authTag = cipher.getAuthTag(); + return { + iv: iv.toString('hex'), + encryptedData: encrypted.toString('hex'), + authTag: authTag.toString('hex'), + }; +}; + +interface DecryptData { + iv: string; + encryptedData: string; + authTag: string; +} + +export const createDecipherGcm = (data: DecryptData, password: string) => { + const algorithm = 'aes-256-gcm'; + const key = pairingKey(password); + const iv = Buffer.from(data.iv, 'hex'); + const encryptedText = Buffer.from(data.encryptedData, 'hex'); + const authTag = Buffer.from(data.authTag, 'hex'); + const decipher = crypto.createDecipheriv(algorithm, key, iv); + decipher.setAuthTag(authTag); + let decrypted: Buffer; + + try { + decrypted = Buffer.concat([decipher.update(encryptedText), decipher.final()]); + } catch (err) { + throw new Error(`Failed to decrypt data: ${err.message}`); + } + return JSON.parse(decrypted.toString('utf-8')); +}; diff --git a/src/utils/service-utilities/utils.ts b/src/utils/service-utilities/utils.ts index b000165599..6eeffe8538 100644 --- a/src/utils/service-utilities/utils.ts +++ b/src/utils/service-utilities/utils.ts @@ -713,43 +713,7 @@ export const createDecipheriv = (data: { iv: string; encryptedData: string }, pa return JSON.parse(decrypted.toString()); }; -export const createCipherGcm = (data: string, password: string) => { - const algorithm = 'aes-256-gcm'; - const key = Buffer.from(password, 'hex'); - const iv = crypto.randomBytes(12); // 12 bytes for GCM - const cipher = crypto.createCipheriv(algorithm, key, iv); - const encrypted = Buffer.concat([cipher.update(data, 'utf8'), cipher.final()]); - const authTag = cipher.getAuthTag(); - return { - iv: iv.toString('hex'), - encryptedData: encrypted.toString('hex'), - authTag: authTag.toString('hex'), - }; -}; - -interface DecryptData { - iv: string; - encryptedData: string; - authTag: string; -} - -export const createDecipherGcm = (data: DecryptData, password: string) => { - const algorithm = 'aes-256-gcm'; - const key = Buffer.from(password, 'hex'); - const iv = Buffer.from(data.iv, 'hex'); - const encryptedText = Buffer.from(data.encryptedData, 'hex'); - const authTag = Buffer.from(data.authTag, 'hex'); - const decipher = crypto.createDecipheriv(algorithm, key, iv); - decipher.setAuthTag(authTag); - let decrypted: Buffer; - - try { - decrypted = Buffer.concat([decipher.update(encryptedText), decipher.final()]); - } catch (err) { - throw new Error(`Failed to decrypt data: ${err.message}`); - } - return JSON.parse(decrypted.toString('utf-8')); -}; +export { createCipherGcm, createDecipherGcm } from 'src/services/channel/crypto'; export const getArchivedVaults = (allVaults: Vault[], vault: Vault) => { return vault.archived || !vault.archivedId diff --git a/tests/channel/ChannelRequestScanner.test.tsx b/tests/channel/ChannelRequestScanner.test.tsx new file mode 100644 index 0000000000..9925a99c09 --- /dev/null +++ b/tests/channel/ChannelRequestScanner.test.tsx @@ -0,0 +1,65 @@ +import React from 'react'; +import renderer, { act } from 'react-test-renderer'; +import ChannelRequestScanner from '../../src/services/channel/ChannelRequestScanner'; + +jest.mock('src/components/QRScanner', () => 'QRScanner'); + +describe('Desktop request scanner', () => { + it('waits for asynchronous preparation and sends only one request per scan', async () => { + let finish: (sent: boolean) => void; + const onScanCompleted = jest.fn( + () => + new Promise((resolve) => { + finish = resolve; + }) + ); + let screen: renderer.ReactTestRenderer; + act(() => { + screen = renderer.create( + + {React.createElement('Waiting')} + + ); + }); + const scan = screen!.root.findByType('QRScanner' as any).props.onScanCompleted; + let request: Promise; + act(() => { + request = scan('disposable-qr'); + }); + await act(async () => { + await scan('duplicate-qr'); + }); + expect(onScanCompleted).toHaveBeenCalledTimes(1); + expect(screen!.root.findAllByType('Waiting' as any)).toHaveLength(0); + await act(async () => { + finish!(true); + await request; + }); + expect(screen!.root.findAllByType('Waiting' as any)).toHaveLength(1); + expect(screen!.root.findAllByType('QRScanner' as any)).toHaveLength(0); + await act(async () => { + await scan('late-duplicate-qr'); + }); + expect(onScanCompleted).toHaveBeenCalledTimes(1); + }); + + it('keeps the scanner available after failure and accepts the next valid request', async () => { + const onScanCompleted = jest.fn().mockResolvedValueOnce(false).mockResolvedValueOnce(true); + let screen: renderer.ReactTestRenderer; + act(() => { + screen = renderer.create( + + {React.createElement('Waiting')} + + ); + }); + await act(async () => { + await screen!.root.findByType('QRScanner' as any).props.onScanCompleted('invalid-qr'); + }); + expect(screen!.root.findAllByType('Waiting' as any)).toHaveLength(0); + await act(async () => { + await screen!.root.findByType('QRScanner' as any).props.onScanCompleted('valid-qr'); + }); + expect(screen!.root.findAllByType('Waiting' as any)).toHaveLength(1); + }); +}); diff --git a/tests/channel/crypto.test.ts b/tests/channel/crypto.test.ts new file mode 100644 index 0000000000..702dcdda1f --- /dev/null +++ b/tests/channel/crypto.test.ts @@ -0,0 +1,57 @@ +import { createCipherGcm, createDecipherGcm } from '../../src/services/channel/crypto'; +import vectors from './desktop-crypto-vectors.json'; +const crypto = require('crypto'); + +describe('Keeper Desktop AES-GCM wire contract', () => { + afterEach(() => jest.restoreAllMocks()); + + it('encrypts the signing request using the Desktop-compatible fixed vector', () => { + jest + .spyOn(crypto, 'randomBytes') + .mockReturnValue(Buffer.from(vectors.request.ciphertext.iv, 'hex')); + expect(createCipherGcm(JSON.stringify(vectors.request.plaintext), vectors.key)).toEqual( + vectors.request.ciphertext + ); + }); + + it('decrypts the Desktop event/data/responseData response shape', () => { + const response = createDecipherGcm(vectors.response.ciphertext, vectors.key); + expect(response).toEqual(vectors.response.plaintext); + expect(response.data.responseData.data.signedSerializedPSBT).toBe('fixture-only-signed-psbt'); + }); + + it.each(['ADD_DEVICE', 'HEALTH_CHECK', 'REGISTER_MULTISIG', 'VERIFY_ADDRESS', 'SIGN_TX'])( + 'preserves the %s response action and nested data', + (action) => { + const response = { + event: 'CHANNEL_MESSAGE', + data: { responseData: { action, data: { address: 'disposable-testnet-address' } } }, + }; + expect( + createDecipherGcm(createCipherGcm(JSON.stringify(response), vectors.key), vectors.key) + ).toEqual(response); + } + ); + + it.each(['iv', 'encryptedData', 'authTag'])( + 'rejects altered %s before returning signing data', + (field) => { + const encrypted = { ...vectors.response.ciphertext }; + encrypted[field] = (encrypted[field][0] === '0' ? '1' : '0') + encrypted[field].slice(1); + expect(() => createDecipherGcm(encrypted, vectors.key)).toThrow(); + } + ); + + it.each(['invalid-qr', vectors.key + 'invalid', vectors.key.slice(1), '01'.repeat(16)])( + 'rejects malformed pairing codes before sending (%s)', + (key) => { + expect(() => createCipherGcm(JSON.stringify(vectors.request.plaintext), key)).toThrow( + 'Invalid Desktop pairing code' + ); + } + ); + + it('rejects a response encrypted for a different QR session', () => { + expect(() => createDecipherGcm(vectors.response.ciphertext, '01'.repeat(32))).toThrow(); + }); +}); diff --git a/tests/channel/desktop-crypto-vectors.json b/tests/channel/desktop-crypto-vectors.json new file mode 100644 index 0000000000..dba3f21764 --- /dev/null +++ b/tests/channel/desktop-crypto-vectors.json @@ -0,0 +1,40 @@ +{ + "description": "Disposable public AES-GCM protocol vectors; payload strings are not valid or spendable PSBTs.", + "key": "0000000000000000000000000000000000000000000000000000000000000000", + "request": { + "plaintext": { + "action": "SIGN_TX", + "signerType": "LEDGER", + "psbt": { + "serializedPSBT": "fixture-only-psbt" + }, + "miniscriptPolicy": null, + "walletName": "Disposable Testnet Vault", + "hmac": null + }, + "ciphertext": { + "iv": "000102030405060708090a0b", + "encryptedData": "f3e952317494e097e04270b6b444905300348ab69deee691a1ae965ff4456a4dd6d5f0e0082074ae1afceffadddf3a200e8fb58982ffe4bfc090272676dae0e4d416d0f1f52962d833a9ccb89e36d04e52859462da27a96c15bfc60345ade264c870cf71bbdf77b26686255990581a165c29e365bf4ee4c9009b026998837a8f03e20a4039428b0ab3301fc4974fc101d831992717cc61a830fde80893d92c03af43e6f4", + "authTag": "8f6e3b25da67fe3d36a80480936ad896" + } + }, + "response": { + "plaintext": { + "event": "CHANNEL_MESSAGE", + "data": { + "responseData": { + "action": "SIGN_TX", + "data": { + "signedSerializedPSBT": "fixture-only-signed-psbt", + "hmac": null + } + } + } + }, + "ciphertext": { + "iv": "000102030405060708090a0b", + "encryptedData": "f3e956246593fbdbf85a11adbc4d90491833e5dfecceceb18ae9c829e9547b0ececdc7873e02428c57bebeefeadc3a6316ceecd886eef9b7c3977f7930d9fae1ce6bbe8bb16c38c827afc8b7cb239e4416928965ca56b1325eb3c30456bbe546f242ef03ee9178b867d06a45801919541227af7aba4cfee205db1738c0d51cca52fa08523b02dd01e60816ca9e5cd9", + "authTag": "aa74a11d43ab5df18d52ef140b5f92d1" + } + } +} From da8751a92b04492ecce6b483c0f9f4a1de292944 Mon Sep 17 00:00:00 2001 From: Anant Date: Wed, 7 Oct 2026 18:43:27 +0530 Subject: [PATCH 031/254] Address Copilot scanner latch and invalid QR feedback findings --- src/screens/Channel/ConnectChannel.tsx | 8 ++++++-- src/screens/QRScreens/RegisterWithChannel.tsx | 10 ++++++++-- src/screens/QRScreens/SignWithChannel.tsx | 10 ++++++++-- src/services/channel/ChannelRequestScanner.tsx | 6 +++++- src/services/channel/crypto.ts | 8 +++++++- tests/channel/ChannelRequestScanner.test.tsx | 14 +++++++++++++- tests/channel/crypto.test.ts | 6 +++++- 7 files changed, 52 insertions(+), 10 deletions(-) diff --git a/src/screens/Channel/ConnectChannel.tsx b/src/screens/Channel/ConnectChannel.tsx index c4b3dec289..30c1f21fbe 100644 --- a/src/screens/Channel/ConnectChannel.tsx +++ b/src/screens/Channel/ConnectChannel.tsx @@ -1,4 +1,4 @@ -import { ActivityIndicator, StyleSheet , TouchableOpacity} from 'react-native'; +import { ActivityIndicator, StyleSheet, TouchableOpacity } from 'react-native'; import { Box, ScrollView, VStack, useColorMode } from '@gluestack-ui/themed-native-base'; import React, { useContext, useEffect, useRef, useState } from 'react'; import ScreenWrapper from 'src/components/ScreenWrapper'; @@ -38,6 +38,7 @@ import ReceiveAddress from '../Recieve/ReceiveAddress'; import ReceiveQR from '../Recieve/ReceiveQR'; import QRScanner from 'src/components/QRScanner'; import { getUSBSignerDetails } from 'src/hardware/usbSigner'; +import { InvalidChannelQRCodeError } from 'src/services/channel/crypto'; import { SignerType, VaultType } from 'src/services/wallets/enums'; import WalletOperations from 'src/services/wallets/operations'; import { getKeyUID } from 'src/utils/utilities'; @@ -199,7 +200,10 @@ function ConnectChannel() { return true; } catch (error) { console.log('Error in onBarCodeRead:', error); - if (error.message && error.message.includes('TypeError: invalid key length 1')) { + if ( + error instanceof InvalidChannelQRCodeError || + error.message?.includes('TypeError: invalid key length 1') + ) { showToast(errorText.QrScannedDesptopInvalid, ); } else { showToast(errorText.failedToConnectDesktop, ); diff --git a/src/screens/QRScreens/RegisterWithChannel.tsx b/src/screens/QRScreens/RegisterWithChannel.tsx index 6bd925e779..0f98be7318 100644 --- a/src/screens/QRScreens/RegisterWithChannel.tsx +++ b/src/screens/QRScreens/RegisterWithChannel.tsx @@ -23,6 +23,7 @@ import useSignerFromKey from 'src/hooks/useSignerFromKey'; import { hcStatusType } from 'src/models/interfaces/HeathCheckTypes'; import { healthCheckStatusUpdate } from 'src/store/sagaActions/bhr'; import ChannelRequestScanner from 'src/services/channel/ChannelRequestScanner'; +import { InvalidChannelQRCodeError } from 'src/services/channel/crypto'; import { VaultType } from 'src/services/wallets/enums'; import BackgroundTimer from 'react-native-background-timer'; import { useAppSelector } from 'src/store/hooks'; @@ -96,8 +97,13 @@ function RegisterWithChannel() { const requestData = createCipherGcm(JSON.stringify(requestBody), decryptionKey.current); channel.emit(JOIN_CHANNEL, { room, network: bitcoinNetworkType, requestData }); return true; - } catch { - showToast(errorText.failedToConnectDesktop, ); + } catch (error) { + showToast( + error instanceof InvalidChannelQRCodeError + ? errorText.QrScannedDesptopInvalid + : errorText.failedToConnectDesktop, + + ); return false; } }; diff --git a/src/screens/QRScreens/SignWithChannel.tsx b/src/screens/QRScreens/SignWithChannel.tsx index 356a5322ec..675ac4173a 100644 --- a/src/screens/QRScreens/SignWithChannel.tsx +++ b/src/screens/QRScreens/SignWithChannel.tsx @@ -27,6 +27,7 @@ import useSignerFromKey from 'src/hooks/useSignerFromKey'; import { getPsbtForHwi } from 'src/hardware'; import { hcStatusType } from 'src/models/interfaces/HeathCheckTypes'; import ChannelRequestScanner from 'src/services/channel/ChannelRequestScanner'; +import { InvalidChannelQRCodeError } from 'src/services/channel/crypto'; import { updateKeyDetails } from 'src/store/sagaActions/wallets'; import BackgroundTimer from 'react-native-background-timer'; import WalletHeader from 'src/components/WalletHeader'; @@ -118,8 +119,13 @@ function SignWithChannel() { const requestData = createCipherGcm(JSON.stringify(requestBody), decryptionKey.current); channel.emit(JOIN_CHANNEL, { room, network: bitcoinNetworkType, requestData }); return true; - } catch { - showToast(errorText.failedToConnectDesktop, ); + } catch (error) { + showToast( + error instanceof InvalidChannelQRCodeError + ? errorText.QrScannedDesptopInvalid + : errorText.failedToConnectDesktop, + + ); return false; } }; diff --git a/src/services/channel/ChannelRequestScanner.tsx b/src/services/channel/ChannelRequestScanner.tsx index 3ea583a540..e41c6c42d3 100644 --- a/src/services/channel/ChannelRequestScanner.tsx +++ b/src/services/channel/ChannelRequestScanner.tsx @@ -10,6 +10,7 @@ export default function ChannelRequestScanner({ children: ReactNode; }) { const [requestSent, setRequestSent] = useState(false); + const [scanAttempt, setScanAttempt] = useState(0); const preparing = useRef(false); const sent = useRef(false); const onScan = async (data: string) => { @@ -19,10 +20,13 @@ export default function ChannelRequestScanner({ if (await onScanCompleted(data)) { sent.current = true; setRequestSent(true); + } else { + // QRScanner latches after one decoded QR. A new instance can scan again. + setScanAttempt((attempt) => attempt + 1); } } finally { preparing.current = false; } }; - return requestSent ? <>{children} : ; + return requestSent ? <>{children} : ; } diff --git a/src/services/channel/crypto.ts b/src/services/channel/crypto.ts index 05f3cdc91c..dd4842db3e 100644 --- a/src/services/channel/crypto.ts +++ b/src/services/channel/crypto.ts @@ -1,8 +1,14 @@ // Shared with Keeper Desktop: AES-256-GCM, 12-byte IV and 16-byte authentication tag. const crypto = require('crypto'); +export class InvalidChannelQRCodeError extends Error { + constructor() { + super('Invalid Desktop pairing code'); + } +} + const pairingKey = (password: string) => { - if (!/^[a-f0-9]{64}$/i.test(password)) throw new Error('Invalid Desktop pairing code'); + if (!/^[a-f0-9]{64}$/i.test(password)) throw new InvalidChannelQRCodeError(); return Buffer.from(password, 'hex'); }; diff --git a/tests/channel/ChannelRequestScanner.test.tsx b/tests/channel/ChannelRequestScanner.test.tsx index 9925a99c09..73c0441ad9 100644 --- a/tests/channel/ChannelRequestScanner.test.tsx +++ b/tests/channel/ChannelRequestScanner.test.tsx @@ -2,7 +2,19 @@ import React from 'react'; import renderer, { act } from 'react-test-renderer'; import ChannelRequestScanner from '../../src/services/channel/ChannelRequestScanner'; -jest.mock('src/components/QRScanner', () => 'QRScanner'); +jest.mock('src/components/QRScanner', () => { + const React = require('react'); + return function MockQRScanner({ onScanCompleted }) { + const hasScanned = React.useRef(false); + return React.createElement('QRScanner', { + onScanCompleted: (data) => { + if (hasScanned.current) return; + hasScanned.current = true; + return onScanCompleted(data); + }, + }); + }; +}); describe('Desktop request scanner', () => { it('waits for asynchronous preparation and sends only one request per scan', async () => { diff --git a/tests/channel/crypto.test.ts b/tests/channel/crypto.test.ts index 702dcdda1f..f125e49d67 100644 --- a/tests/channel/crypto.test.ts +++ b/tests/channel/crypto.test.ts @@ -1,4 +1,8 @@ -import { createCipherGcm, createDecipherGcm } from '../../src/services/channel/crypto'; +import { + createCipherGcm, + createDecipherGcm, + InvalidChannelQRCodeError, +} from '../../src/services/channel/crypto'; import vectors from './desktop-crypto-vectors.json'; const crypto = require('crypto'); From 19e079ae4486dacf1f79bf5e527384e608872977 Mon Sep 17 00:00:00 2001 From: Anant Date: Wed, 7 Oct 2026 18:50:25 +0530 Subject: [PATCH 032/254] Check stale backup revisions after content restoration --- dev/local-backend/sources.lock.json | 2 +- dev/local-backend/verify.py | 20 +++++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/dev/local-backend/sources.lock.json b/dev/local-backend/sources.lock.json index f9a926960d..5917d3086f 100644 --- a/dev/local-backend/sources.lock.json +++ b/dev/local-backend/sources.lock.json @@ -1,7 +1,7 @@ { "relay": { "url": "https://github.com/KeeperCommunity/Relay.git", - "revision": "af3248acb9548a6e03eb3d1ee13b2f24d8a47651", + "revision": "4c54e28738815546718775dbbdb39f0342c1b646", "assets": { "files/Dockerfile.local": "6dde6c692430b1a91a10cdd82e6f1562023cc5974ffd4d4f58fe57fa1994407a" } diff --git a/dev/local-backend/verify.py b/dev/local-backend/verify.py index 2595ba02eb..21a2c3a03c 100644 --- a/dev/local-backend/verify.py +++ b/dev/local-backend/verify.py @@ -98,7 +98,25 @@ def backup_roundtrip(relay, fixture): require(request(relay, '/getBackupSnapshot', {'appId': fixture}) == repaired, 'Rejected stale repair changed the backup') print('PASS revisioned backup create/read and stale-revision preservation') - return repaired + changed = dict(replacement, expectedRevision=repaired['revision'], + walletObject={'qa': 'second-synthetic-encrypted-wallet'}) + require(request(relay, '/repairAppBackup', changed).get('updated') is True, + 'Second disposable backup image was not saved') + second = request(relay, '/getBackupSnapshot', {'appId': fixture}) + restored = dict(replacement, expectedRevision=second['revision']) + require(request(relay, '/repairAppBackup', restored).get('updated') is True, + 'Restoring disposable backup content failed') + final = request(relay, '/getBackupSnapshot', {'appId': fixture}) + require(len({repaired['revision'], second['revision'], final['revision']}) == 3 and + final['appImage']['wallets'] == repaired['appImage']['wallets'], + 'A backup revision repeated after its content changed and returned') + obsolete = dict(changed, expectedRevision=repaired['revision']) + require(request(relay, '/repairAppBackup', obsolete, expected=409).get('error') == + 'BACKUP_CHANGED', 'An obsolete revision was accepted after content returned') + require(request(relay, '/getBackupSnapshot', {'appId': fixture}) == final, + 'Rejected obsolete repair changed the restored backup') + print('PASS restored content retains a new revision and rejects obsolete repairs') + return final def main(): From 2c5d6ae9ea042ed2c6df799012dc34fce03f626e Mon Sep 17 00:00:00 2001 From: Anant Date: Wed, 7 Oct 2026 18:59:29 +0530 Subject: [PATCH 033/254] Assert stable invalid pairing code error in protocol tests --- tests/channel/crypto.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/channel/crypto.test.ts b/tests/channel/crypto.test.ts index f125e49d67..dcdbf5f840 100644 --- a/tests/channel/crypto.test.ts +++ b/tests/channel/crypto.test.ts @@ -50,7 +50,7 @@ describe('Keeper Desktop AES-GCM wire contract', () => { 'rejects malformed pairing codes before sending (%s)', (key) => { expect(() => createCipherGcm(JSON.stringify(vectors.request.plaintext), key)).toThrow( - 'Invalid Desktop pairing code' + InvalidChannelQRCodeError ); } ); From 1fab43c15ae5a2a3675d0cf5f7605474f25b04f2 Mon Sep 17 00:00:00 2001 From: Anant Date: Wed, 7 Oct 2026 19:03:43 +0530 Subject: [PATCH 034/254] Address contributor review findings and verify 2FA rejection --- .github/CODEOWNERS | 2 +- dev/local-backend/README.md | 2 +- dev/local-backend/VERIFICATION.md | 27 ++++++++++++++++++--------- dev/local-backend/keeper.py | 2 ++ dev/local-backend/test_keeper.py | 19 +++++++++++++++++++ dev/local-backend/verify.py | 11 +++++++++-- 6 files changed, 50 insertions(+), 13 deletions(-) diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index f1f3d0d712..baadc36f3e 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -1,2 +1,2 @@ -# Request the three developers for changes targeting sprint; one approval is required. +# Request the three developers for all changes; one approval is required. * @ben-kaufman @cakesoft-utkarsh @Parsh diff --git a/dev/local-backend/README.md b/dev/local-backend/README.md index a373452171..445707f9d3 100644 --- a/dev/local-backend/README.md +++ b/dev/local-backend/README.md @@ -70,7 +70,7 @@ app setup use the public Relay source. The JavaScript install hook only installs the repo's Node compatibility shims. Native dependencies are separate explicit steps below. It no longer runs CocoaPods on Android-only machines or rewrites `android/local.properties` with a Mac-specific path. -If default ports 3000/4002/3003 are occupied, copy `dev/local-backend/.env.example` to `dev/local-backend/.env`, set distinct ports and a unique project name, then run the commands above. Use the same settings for every invocation. Multiple copies using the same Compose project name share containers and volumes; give each independent environment a unique name. `.env.local` must match those ports. Commands work from any directory except the native/Yarn commands, which assume the app root. +If default ports 3000/4002/3003 are occupied, copy `dev/local-backend/.env.example` to `dev/local-backend/.env`, set distinct ports and a unique project name, then run the commands above. Port 8081 is reserved for Metro and cannot be assigned to a backend service. Use the same settings for every invocation. Multiple copies using the same Compose project name share containers and volumes; give each independent environment a unique name. `.env.local` must match those ports. Commands work from any directory except the native/Yarn commands, which assume the app root. ## Android diff --git a/dev/local-backend/VERIFICATION.md b/dev/local-backend/VERIFICATION.md index 1205569afc..0283aeac80 100644 --- a/dev/local-backend/VERIFICATION.md +++ b/dev/local-backend/VERIFICATION.md @@ -2,15 +2,24 @@ ## Sprint contributor package — 7 October 2026 -This focused `sprint` contributor package uses the same public backend pins and -adapter digests as the earlier sprint PR #7014 and main PR #7026. It changes the -developer setup hook and adds contributor/security documentation and a -public-backend CI workflow; it does not modify mobile, dependency or release -files. Backend/bootstrap verification for this exact branch is recorded with -its PR checks. Native app builds must be assessed against the selected app -branch and platform separately. -The dated sections below preserve earlier sprint and public-source evidence; -historical private-source failures are not the current public backend result. +The current contributor package pins public Relay +`4c54e28738815546718775dbbdb39f0342c1b646` and public SigningServer +`3cc7728c27328e7b0358d1f91a32d8d13feba90a`, with local adapter checksums in +`sources.lock.json`. These are newer revisions than the earlier sprint PR #7014 +and main PR #7026. The source locks include Relay snapshot/repair and revision +guards, compatible SigningServer dependency fixes, and digest-pinned image adapters. + +At app commit `19e079ae4486dacf1f79bf5e527384e608872977`, the +[public-backend CI](https://github.com/KeeperCommunity/bitcoin-keeper/actions/runs/37627727798) +passed both the SigningServer adapter and full local backend/bootstrap jobs. +Those checks used these backend pins and exercised local snapshot/repair, +A → B → A revisions, stale-repair rejection and persistence. Subsequent contributor +changes have their own PR check results. Native app builds must be assessed +against the selected app branch and platform separately. + +All dated sections below are historical evidence for the exact revisions named +in those sections. They do not verify the current pins or attribute earlier +private-source failures to the current public backend. ## Public Relay transition — 7 October 2026 diff --git a/dev/local-backend/keeper.py b/dev/local-backend/keeper.py index 15dd7b245c..fbb93a8dca 100644 --- a/dev/local-backend/keeper.py +++ b/dev/local-backend/keeper.py @@ -41,6 +41,8 @@ def settings(): if not re.fullmatch(r'[a-z0-9][a-z0-9_-]*', values['COMPOSE_PROJECT_NAME']): raise ValueError('COMPOSE_PROJECT_NAME must contain lowercase letters, digits, _ or -') ports = [int(values[k]) for k in defaults if k.endswith('_PORT')] + if 8081 in ports: + raise ValueError('Port 8081 is reserved for Metro; choose another backend port') if len(set(ports)) != 3 or any(p < 1024 or p > 65535 for p in ports): raise ValueError('Choose three distinct ports between 1024 and 65535') return {k: values[k] for k in defaults} diff --git a/dev/local-backend/test_keeper.py b/dev/local-backend/test_keeper.py index d8874ad721..c401eedeed 100644 --- a/dev/local-backend/test_keeper.py +++ b/dev/local-backend/test_keeper.py @@ -8,6 +8,7 @@ from unittest.mock import patch import keeper +import verify class PreserveDeveloperWork(unittest.TestCase): @@ -84,6 +85,24 @@ def test_port_collision_is_rejected(self): with self.assertRaisesRegex(ValueError, 'distinct ports'): keeper.settings() + def test_metro_port_is_reserved_for_each_backend_service(self): + for service in ('RELAY', 'CHANNEL', 'SIGNING'): + with self.subTest(service=service): + (self.here / '.env').write_text(f'KEEPER_{service}_PORT=8081\n') + with self.assertRaisesRegex(ValueError, 'reserved for Metro'): + keeper.settings() + + def test_invalid_two_fa_requires_the_pinned_token_failure(self): + with patch.object(verify, 'request', return_value={ + 'err': 'Validation failed: verification token is either invalid or has expired'}): + verify.verify_invalid_two_fa('3003', {'id': 'disposable-signer'}) + for unrelated in ({'err': 'Unauthorized request'}, {'err': 'Invalid request schema'}, + {'valid': False}, {'valid': True}): + with self.subTest(response=unrelated): + with patch.object(verify, 'request', return_value=unrelated): + with self.assertRaisesRegex(RuntimeError, 'unrelated error'): + verify.verify_invalid_two_fa('3003', {'id': 'disposable-signer'}) + def test_fetch_failure_explains_public_revision_and_cleans_temporary_checkout(self): (self.here / 'sources.lock.json').write_text(json.dumps({'relay': { 'assets': {}, 'url': 'https://github.com/example/private-backend.git', diff --git a/dev/local-backend/verify.py b/dev/local-backend/verify.py index 21a2c3a03c..00922ffea3 100644 --- a/dev/local-backend/verify.py +++ b/dev/local-backend/verify.py @@ -119,6 +119,14 @@ def backup_roundtrip(relay, fixture): return final +def verify_invalid_two_fa(signing, auth): + rejected = request(signing, '/v3/validateSingerSetup', + dict(auth, verificationToken='abcdef'), expected=400) + require(rejected.get('err') == + 'Validation failed: verification token is either invalid or has expired', + 'Invalid 2FA check returned an unrelated error') + + def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('--persistence', action='store_true', help='Recreate this Compose stack, retaining volumes, and recheck fixtures') @@ -172,8 +180,7 @@ def main(): auth['verificationToken'] = token(secret) else: raise RuntimeError('Created signer did not become readable within 5 seconds') - invalid = dict(auth, verificationToken='abcdef') - request(signing, '/v3/validateSingerSetup', invalid, expected=400) + verify_invalid_two_fa(signing, auth) print('PASS testnet signer setup, authorization and valid/invalid 2FA') if args.persistence: compose('down') From 8f0ac38fa6dbdc2681e3f1ce8755e62fdfd7b0b1 Mon Sep 17 00:00:00 2001 From: Anant Date: Wed, 7 Oct 2026 19:06:07 +0530 Subject: [PATCH 035/254] Allow Desktop connect QR retries using shared request scanner --- src/screens/Channel/ConnectChannel.tsx | 46 +++++++++++--------------- 1 file changed, 19 insertions(+), 27 deletions(-) diff --git a/src/screens/Channel/ConnectChannel.tsx b/src/screens/Channel/ConnectChannel.tsx index 30c1f21fbe..9ef7342a43 100644 --- a/src/screens/Channel/ConnectChannel.tsx +++ b/src/screens/Channel/ConnectChannel.tsx @@ -36,7 +36,7 @@ import useVault from 'src/hooks/useVault'; import { updateKeyDetails } from 'src/store/sagaActions/wallets'; import ReceiveAddress from '../Recieve/ReceiveAddress'; import ReceiveQR from '../Recieve/ReceiveQR'; -import QRScanner from 'src/components/QRScanner'; +import ChannelRequestScanner from 'src/services/channel/ChannelRequestScanner'; import { getUSBSignerDetails } from 'src/hardware/usbSigner'; import { InvalidChannelQRCodeError } from 'src/services/channel/crypto'; import { SignerType, VaultType } from 'src/services/wallets/enums'; @@ -53,35 +53,27 @@ import { setShowTipModal } from 'src/store/reducers/settings'; function ScanAndInstruct({ onBarCodeRead, mode, receivingAddress }) { const { colorMode } = useColorMode(); - const [channelCreated, setChannelCreated] = useState(false); const { translations } = useContext(LocalizationContext); const { settings } = translations; - const callback = (data) => { - let success = onBarCodeRead(data); - if (success) { - setChannelCreated(true); - } - }; - - return !channelCreated ? ( - - ) : ( - - {mode === InteracationMode.ADDRESS_VERIFICATION ? ( - - - - - ) : ( - - - {settings.KeeperDesktopApp} - - - - )} - + return ( + + + {mode === InteracationMode.ADDRESS_VERIFICATION ? ( + + + + + ) : ( + + + {settings.KeeperDesktopApp} + + + + )} + + ); } From b39b0a173b9021a31479e8c29fe858d4130beb7b Mon Sep 17 00:00:00 2001 From: Anant Date: Wed, 7 Oct 2026 19:14:25 +0530 Subject: [PATCH 036/254] Complete contributor setup docs and symlink integrity coverage --- Readme.md | 4 +++- dev/local-backend/VERIFICATION.md | 13 +++++++------ dev/local-backend/test_keeper.py | 15 +++++++++++++++ 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/Readme.md b/Readme.md index 70ed94e5c0..0db9aef789 100644 --- a/Readme.md +++ b/Readme.md @@ -55,7 +55,9 @@ Refer to platform-specific documentation for detailed setup requirements. cd bitcoin-keeper ``` 3. Install the project dependencies using Yarn: - The prepare scripts will automatically install pods and nodify crypto-related packages for react-native + The prepare script configures the crypto-related React Native packages. + CocoaPods is an explicit iOS setup step; follow [CONTRIBUTING.md](CONTRIBUTING.md) + after dependency installation. ```shell yarn install ``` diff --git a/dev/local-backend/VERIFICATION.md b/dev/local-backend/VERIFICATION.md index 0283aeac80..559fe2f687 100644 --- a/dev/local-backend/VERIFICATION.md +++ b/dev/local-backend/VERIFICATION.md @@ -89,8 +89,8 @@ private-source failures to the current public backend. - Removed the public contributor-access issue template. Access instructions now use private intake/internal tracking, matching the owner's policy. Added `HANDOFF.md` for independent-machine setup and separate pass/fail/untested reports. -- Five bootstrap regression tests, strict contributor OpenSpec validation, ten - local documentation links and diff whitespace checks: PASS. +- Five bootstrap regression tests, ten local documentation links and diff + whitespace checks: PASS. - Restarted this Mac's existing Colima profile and ran the documented `dev up` against the same pinned sources, existing disposable project/volumes and build caches. Health, Socket.IO, explicit unsupported-route failure, app create/read @@ -130,8 +130,8 @@ GitHub read access reused. Project `keeper-readiness-proof` used ports - V3 testnet Server Key setup, authorization and valid/invalid 2FA: PASS. - Container recreation preserves the same app record and derived signer public key: PASS. Mainnet/hosted database startup rejection: PASS. -- Five bootstrap regression tests: PASS. Active contributor OpenSpec validation: - PASS. Both backend test jobs and current-source scans pass in their own CI; +- Five bootstrap regression tests: PASS. Both backend test jobs and + current-source scans pass in their own CI; their history scans remain failing, tracked privately. - Updated native development builds: iOS PASS after replacing a stale local Node path; Android PASS after selecting the installed JDK 17 (976 tasks, 58 @@ -256,7 +256,6 @@ Apple Silicon Mac, macOS 26.6.2, Xcode 26.4/iOS 26.4 simulator, Node 25.9.0, Yar | iOS More Options | PASS, simulator navigation and screenshot inspected | | Node/Mongo/nginx pinned image manifests | PASS, each includes Linux amd64 and arm64 | | Existing release version consistency | PASS, unchanged 2.5.15 / Android 622 / iOS 615 | -| Strict OpenSpec validation | PASS | Native apps use a separate Metro process on port 8082 during this acceptance run, preserving the existing developer's Metro on 8081. Android's dev-server host is set to localhost with device port 8081 forwarded to host 8082. Ordinary single-checkout instructions use port 8081. @@ -272,11 +271,13 @@ Native apps use a separate Metro process on port 8082 during this acceptance run ## Reproduce checks +The earlier contributor OpenSpec change is not included in this checkout. Its +validation is not a reproducible acceptance check for this source package. + ```sh ./dev/local-backend/dev up ./dev/local-backend/verify-local.sh --persistence --boundaries python3 -B -m unittest discover -s dev/local-backend -p test_keeper.py -npx --no-install openspec validate package-contributor-development --strict ``` The acceptance logs and emulator fixtures remain on the maintainer's machine outside the source package. Do not include local databases, generated signing identities, recovery material or captured emails in a source review or handoff archive. diff --git a/dev/local-backend/test_keeper.py b/dev/local-backend/test_keeper.py index c401eedeed..b5bc059b51 100644 --- a/dev/local-backend/test_keeper.py +++ b/dev/local-backend/test_keeper.py @@ -68,6 +68,21 @@ def test_unlisted_adapter_file_is_rejected_before_fetch(self): keeper.prepare() command.assert_not_called() + def test_adapter_symlinks_are_rejected_before_fetch_even_when_listed(self): + adapter = self.here / 'adapters/relay/files' + adapter.mkdir(parents=True) + target = self.app / 'outside-adapter.js' + target.write_text('fixture') + (adapter / 'link.js').symlink_to(target) + for assets in ({}, {'files/link.js': hashlib.sha256(b'fixture').hexdigest()}): + with self.subTest(listed=bool(assets)): + (self.here / 'sources.lock.json').write_text(json.dumps({ + 'relay': {'assets': assets}})) + with patch.object(keeper, 'run') as command: + with self.assertRaisesRegex(ValueError, 'symlinks are not allowed'): + keeper.prepare() + command.assert_not_called() + def test_unknown_existing_checkout_is_not_overwritten(self): source = self.here / '.sources/relay' source.mkdir(parents=True) From 197feb399458fa71d1ea7286219e46ec34861c79 Mon Sep 17 00:00:00 2001 From: Anant Date: Wed, 7 Oct 2026 23:26:39 +0530 Subject: [PATCH 037/254] fix: serialize signer migration and reject failed refresh writes --- src/services/backend/Relay.ts | 2 +- src/store/sagas/wallets.ts | 16 ++- tests/recovery/backupFreshness.test.cjs | 81 +++++++++++++- tests/recovery/fingerprintTransport.test.cjs | 110 +++++++++++++++++++ 4 files changed, 198 insertions(+), 11 deletions(-) create mode 100644 tests/recovery/fingerprintTransport.test.cjs diff --git a/src/services/backend/Relay.ts b/src/services/backend/Relay.ts index d9a33502ad..9bcdcb3fe4 100644 --- a/src/services/backend/Relay.ts +++ b/src/services/backend/Relay.ts @@ -142,7 +142,7 @@ export default class Relay { err?: string; }> => { try { - const res = await RestClient.post(`${RELAY}migrateXfps`, { appId, signerChanges }); + const res = await backupPost(`${RELAY}migrateXfps`, { appId, signerChanges }); const data = res.data || res.json; return data; } catch (err) { diff --git a/src/store/sagas/wallets.ts b/src/store/sagas/wallets.ts index cd3d786253..839c894ca3 100644 --- a/src/store/sagas/wallets.ts +++ b/src/store/sagas/wallets.ts @@ -869,15 +869,13 @@ function* refreshWalletsWorker({ } // Write updated specs (with spendability) back to Realm - if (synchedWallet.entityKind === EntityKind.VAULT) { - yield call(dbManager.updateObjectById, RealmSchema.Vault, synchedWallet.id, { - specs: synchedWallet.specs, - }); - } else { - yield call(dbManager.updateObjectById, RealmSchema.Wallet, synchedWallet.id, { - specs: synchedWallet.specs, - }); - } + const persisted = yield call( + dbManager.updateObjectById, + synchedWallet.entityKind === EntityKind.VAULT ? RealmSchema.Vault : RealmSchema.Wallet, + synchedWallet.id, + { specs: synchedWallet.specs } + ); + if (!persisted) throw new Error('Failed to persist refreshed wallet'); if (options.addNotifications && newDustUTXOs.length > 0) { yield put(setPendingDustToast(synchedWallet.id)); diff --git a/tests/recovery/backupFreshness.test.cjs b/tests/recovery/backupFreshness.test.cjs index e03ad47b90..4676654e7c 100644 --- a/tests/recovery/backupFreshness.test.cjs +++ b/tests/recovery/backupFreshness.test.cjs @@ -1,8 +1,87 @@ const { test } = require('node:test'); const assert = require('node:assert/strict'); -const { fixture, enums, encryption, wallet, loadModule } = require('./sagaHarness.cjs'); +const vm = require('node:vm'); +const ts = require('typescript'); +const { fixture, enums, encryption, wallet, loadModule, functions } = require('./sagaHarness.cjs'); const { NetworkType, WalletType } = enums; +function refreshFixture(kind, rejectWrite = false) { + const f = fixture({ bhr: { pendingAllBackup: true } }); + const original = { + ...wallet('refresh-persistence', NetworkType.MAINNET), + entityKind: kind === 'Vault' ? enums.EntityKind.VAULT : enums.EntityKind.WALLET, + specs: { + receivingAddress: 'before-refresh', + confirmedUTXOs: [], unconfirmedUTXOs: [], transactions: [], + addresses: { external: {}, internal: {} }, + }, + }; + f.collections[kind].push(original); + f.scope.getJSONFromRealmObject = (value) => JSON.parse(JSON.stringify(value)); + f.scope.ELECTRUM_CLIENT = { isClientConnected: true }; + f.scope.WalletUtilities.getNetworkByType = () => ({}); + f.scope.WalletOperations = { syncWalletsViaElectrumClient: async () => { + const synced = f.scope.getJSONFromRealmObject(original); + synced.specs.receivingAddress = 'after-refresh'; + synced.specs.hasNewUpdates = true; + return { synchedWallets: [{ synchedWallet: synced, newUTXOs: [] }] }; + } }; + f.scope.classifyDustByAddress = () => ({ + taintedAddresses: new Set(), initialTaintAddresses: new Set(), dustSpendTxids: new Set(), + }); + f.scope.setSyncing = (payload) => ({ type: 'setSyncing', payload }); + f.scope.setElectrumNotConnectedErr = (payload) => ({ type: 'setElectrumNotConnectedErr', payload }); + f.scope.ELECTRUM_NOT_CONNECTED_ERR = 'offline'; + f.scope.ELECTRUM_NOT_CONNECTED_ERR_TOR = 'tor-offline'; + f.errors = []; + f.scope.captureError = (error) => f.errors.push(error); + const persist = f.scope.dbManager.updateObjectById; + f.writes = []; + f.scope.dbManager.updateObjectById = (schema, id, patch) => { + f.writes.push(schema); + return rejectWrite ? false : persist(schema, id, patch); + }; + vm.runInNewContext(ts.transpileModule( + functions('src/store/sagas/wallets.ts', ['refreshWalletsWorker']), + { compilerOptions: { target: ts.ScriptTarget.ES2020 } } + ).outputText, f.scope); + return f; +} + +for (const kind of ['Wallet', 'Vault']) { + test(`${kind}: rejected refresh persistence returns failure and clears syncing`, async () => { + const f = refreshFixture(kind, true); + assert.equal(await f.run('refreshWalletsWorker', { + payload: { wallets: f.collections[kind], options: { hardRefresh: true } }, + }), false); + assert.deepEqual(f.writes, [kind]); + assert.equal(f.collections[kind][0].specs.receivingAddress, 'before-refresh'); + assert.deepEqual(f.actions.filter((action) => action.type === 'setSyncing') + .map((action) => action.payload.isSyncing), [true, false]); + assert.equal(f.errors.length, 1); + assert.ok(f.actions.some((action) => action.type === 'setElectrumNotConnectedErr' && + action.payload.includes('Failed to persist refreshed wallet'))); + }); + + test(`${kind}: login never inspects stale specs after rejected refresh persistence`, async () => { + const f = refreshFixture(kind, true); + await f.run('autoWalletsSyncWorker', { payload: { backupCheckAppId: f.app.id } }); + assert.deepEqual(f.writes, [kind]); + assert.equal(f.collections[kind][0].specs.receivingAddress, 'before-refresh'); + assert.equal(f.actions.some((action) => action.type === 'checkBackupFreshness'), false); + assert.equal(f.state.bhr.pendingAllBackup, true); + }); + + test(`${kind}: login inspects backup only after successful refresh persistence`, async () => { + const f = refreshFixture(kind); + await f.run('autoWalletsSyncWorker', { payload: { backupCheckAppId: f.app.id } }); + assert.deepEqual(f.writes, [kind]); + assert.equal(f.collections[kind][0].specs.receivingAddress, 'after-refresh'); + assert.equal(f.actions.filter((action) => action.type === 'checkBackupFreshness').length, 1); + assert.equal(f.errors.length, 0); + }); +} + test('login backup inspection waits for wallet refresh to finish', async () => { let finishSync; const syncGate = new Promise((resolve) => { finishSync = resolve; }); diff --git a/tests/recovery/fingerprintTransport.test.cjs b/tests/recovery/fingerprintTransport.test.cjs new file mode 100644 index 0000000000..8b971e26c0 --- /dev/null +++ b/tests/recovery/fingerprintTransport.test.cjs @@ -0,0 +1,110 @@ +const { test } = require('node:test'); +const assert = require('node:assert/strict'); +const crypto = require('node:crypto'); +const { loadModule, harness, wallet } = require('./helpers.cjs'); + +const signerChanges = [{ + oldSignerId: 'fixture-old', newSignerId: 'fixture-new', newSignerDetails: 'fixture-ciphertext', +}]; + +function relay(transport, rest = { post: () => assert.fail('Migration bypassed backup transport') }, errors = []) { + return loadModule('src/services/backend/Relay.ts', { + '../backup/transport': transport, + '../rest/RestClient': rest, + '../sentry': { captureError: (error) => errors.push(error) }, + 'src/services/wallets/enums': {}, + 'src/utils/service-utilities/config': { RELAY: 'fixture/' }, + 'react-native': { Platform: { OS: 'fixture' } }, + }).default; +} + +for (const repair of [false, true]) { + test(`fingerprint migration waits for ${repair ? 'repair readback' : 'inspection'} and invalidates its completion`, async (t) => { + let armed = false, reads = 0, release, entered; + const readGate = new Promise((resolve) => { release = resolve; }); + const readStarted = new Promise((resolve) => { entered = resolve; }); + t.after(() => release()); + let f; + let vaults = [], labels = []; + const revision = () => crypto.createHash('sha256') + .update(JSON.stringify({ remote: f.remote, vaults, labels })).digest('hex'); + f = harness({ + post: async ({ path }) => { + if (armed && path.endsWith('getBackupSnapshot') && ++reads === (repair ? 2 : 1)) { + entered(); + await readGate; + } + }, + adapter: (path, payload, config) => { + assert.ok(config.signal, 'Every backup request must use bounded transport'); + if (path === 'getBackupSnapshot') return { data: { + appImage: f.remote, allVaultImages: vaults, labels, revision: revision(), + } }; + if (path === 'migrateXfps') { + assert.equal(payload.appId, f.app.id); + assert.deepEqual(JSON.parse(JSON.stringify(payload.signerChanges)), signerChanges); + return { data: { updated: true } }; + } + assert.equal(path, 'repairAppBackup'); + assert.equal(payload.expectedRevision, revision()); + f.remote.wallets = { ...payload.walletObject }; + f.remote.signers = { ...payload.signersObject }; + f.remote.nodes = payload.nodes; + vaults = Object.values(payload.vaultObject); + labels = payload.labels; + f.remote.vaults = vaults.map((record) => record.vaultId); + f.remote.labels = labels.map((record) => record.id); + return { data: { updated: true } }; + }, + }); + f.local.Wallet.push(wallet()); + assert.equal(await f.run(true), 'verified'); + if (repair) f.local.Wallet[0].presentationData.name = 'Updated fixture wallet'; + f.calls.length = 0; + f.phases.length = 0; + armed = true; + const checking = f.run(repair); + await readStarted; + const previousRevision = f.transport.backupRevision(f.app.id); + const migration = relay(f.transport).migrateXfp(f.app.id, signerChanges); + const completed = Promise.allSettled([checking, migration]); + const requestedRevision = f.transport.backupRevision(f.app.id); + const migrationStartedDuringCheck = f.calls.includes('migrateXfps'); + release(); + const [checked, migrated] = await completed; + assert.equal(requestedRevision, previousRevision + 1); + assert.equal(migrationStartedDuringCheck, false); + assert.equal(checked.status, 'fulfilled'); + assert.equal(checked.value, 'unverified'); + assert.equal(f.phases.includes('verified'), false); + assert.equal(migrated.status, 'fulfilled'); + assert.equal(migrated.value.updated, true); + assert.deepEqual(f.calls, repair + ? ['getBackupSnapshot', 'repairAppBackup', 'getBackupSnapshot', 'migrateXfps'] + : ['getBackupSnapshot', 'migrateXfps']); + }); +} + +test('migration rejection and transport failure preserve responses, invalidate checks, and release the queue', async () => { + let fail = false; + const errors = []; + const rest = { post: async (_path, _body, _headers, config) => { + assert.ok(config.signal); + if (fail) throw Error('fixture request body must stay redacted'); + return { json: { updated: false, err: 'fixture-rejection' } }; + } }; + const transport = loadModule('src/services/backup/transport.ts', { '../rest/RestClient': rest }); + const service = relay(transport, rest, errors); + assert.deepEqual(JSON.parse(JSON.stringify(await service.migrateXfp('fixture-account', signerChanges))), + { updated: false, err: 'fixture-rejection' }); + assert.equal(transport.backupRevision('fixture-account'), 1); + fail = true; + await assert.rejects(service.migrateXfp('fixture-account', signerChanges), + /^Error: Failed to do migrate the xfp$/); + assert.equal(errors.length, 1); + assert.equal(errors[0].message, 'Backup request could not be completed'); + assert.equal(transport.backupRevision('fixture-account'), 2); + fail = false; + assert.equal((await service.migrateXfp('fixture-account', signerChanges)).updated, false); + assert.equal(transport.backupRevision('fixture-account'), 3); +}); From eb26eace8134a3101ae13a89566cd162baa546fc Mon Sep 17 00:00:00 2001 From: Anant Date: Wed, 7 Oct 2026 23:26:39 +0530 Subject: [PATCH 038/254] fix: retain backup progress until shared inspections settle --- src/store/sagas/bhr.ts | 32 +++++++---- tests/recovery/sagaState.test.cjs | 90 +++++++++++++++++++++++++++++-- 2 files changed, 107 insertions(+), 15 deletions(-) diff --git a/src/store/sagas/bhr.ts b/src/store/sagas/bhr.ts index 206aaceb2d..ba417ac0e4 100644 --- a/src/store/sagas/bhr.ts +++ b/src/store/sagas/bhr.ts @@ -978,18 +978,24 @@ export const deleteAppImageEntityWatcher = createWatcher( DELETE_APP_IMAGE_ENTITY ); +const backupInspectionOwners = new Map(); + function* runBackupInspection(repair: boolean) { const { id }: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); - yield put(setBackupRepairRunning({ appId: id, running: true })); + backupInspectionOwners.set(id, (backupInspectionOwners.get(id) ?? 0) + 1); let finalPhase: RepairPhase = 'unverified'; - const updates = eventChannel((emit) => { - inspectBackup(id, repair, (phase) => emit(phase)).then( - () => emit(END), - () => emit(END) - ); - return () => {}; - }, buffers.expanding()); + let inspection: Promise | undefined; + let updates: ReturnType> | undefined; try { + yield put(setBackupRepairRunning({ appId: id, running: true })); + updates = eventChannel((emit) => { + inspection = inspectBackup(id, repair, (phase) => emit(phase)); + inspection.then( + () => emit(END), + () => emit(END) + ); + return () => {}; + }, buffers.expanding()); while (true) { const phase = yield takeMaybe(updates); if (phase === END) break; @@ -1024,8 +1030,14 @@ function* runBackupInspection(repair: boolean) { } return finalPhase === 'verified'; } finally { - updates.close(); - yield put(setBackupRepairRunning({ appId: id, running: false })); + updates?.close(); + // Cancellation closes this listener, but does not stop its shared operation. + // Keep ownership until that operation settles, including a queued repair. + if (inspection) yield call(() => inspection.catch(() => undefined)); + const remaining = (backupInspectionOwners.get(id) ?? 1) - 1; + if (remaining) backupInspectionOwners.set(id, remaining); + else backupInspectionOwners.delete(id); + yield put(setBackupRepairRunning({ appId: id, running: remaining > 0 })); } } diff --git a/tests/recovery/sagaState.test.cjs b/tests/recovery/sagaState.test.cjs index 9285b375c1..b1dba4af81 100644 --- a/tests/recovery/sagaState.test.cjs +++ b/tests/recovery/sagaState.test.cjs @@ -12,20 +12,100 @@ const bhr = loadModule('src/store/reducers/bhr.ts', { 'src/storage': { reduxStorage:{} }, 'redux-persist': { persistReducer: (_config,reducer) => reducer }, }); -function runInspection(f, repair) { +function inspectionRunner(f) { const filename = 'src/store/sagas/bhr.ts'; const source = ts.createSourceFile(filename,fs.readFileSync(path.join(__dirname,'../..',filename),'utf8'),ts.ScriptTarget.Latest,true); - const code = source.statements.filter(n => ts.isFunctionDeclaration(n) && n.name?.text === 'runBackupInspection').map(n => n.getText(source)).join('\n'); - const scope = { ...saga, ...effects, ...bhr, inspectBackup:f.repair.inspectBackup, + const code = source.statements.filter(n => + (ts.isFunctionDeclaration(n) && n.name?.text === 'runBackupInspection') || + (ts.isVariableStatement(n) && n.declarationList.declarations.some(d => d.name?.text === 'backupInspectionOwners')) + ).map(n => n.getText(source)).join('\n'); + const operations = []; + const scope = { ...saga, ...effects, ...bhr, inspectBackup:(...args) => { + const operation = f.repair.inspectBackup(...args); + operations.push(operation); + return operation; + }, RealmSchema:{KeeperApp:'KeeperApp',UAI:'UAI'}, dbManager:{ getObjectByIndex:()=>f.app, getObjectByField:()=>[] }, uaiType:{SERVER_BACKUP_FAILURE:'failure'}, uaiActionedWorker:()=>{}, addToUaiStackWorker:()=>{}, }; vm.runInNewContext(ts.transpileModule(code,{ compilerOptions:{ target:ts.ScriptTarget.ES2020 }}).outputText,scope); let state = bhr.default(undefined,{type:'init'}); const actions = []; - const task = saga.runSaga({getState:()=>({bhr:state}),dispatch:action=>{ actions.push(action); state=bhr.default(state,action); }},scope.runBackupInspection,repair).toPromise(); - return {task,get state(){ return state; }, actions}; + return { + start(repair) { + const sagaTask = saga.runSaga({getState:()=>({bhr:state}),dispatch:action=>{ actions.push(action); state=bhr.default(state,action); }},scope.runBackupInspection,repair); + return {task:sagaTask.toPromise(),sagaTask,settled:()=>Promise.all(operations),get state(){ return state; },actions}; + }, + }; } +const runInspection = (f, repair) => inspectionRunner(f).start(repair); + +const gate = () => { + let release; + const promise = new Promise(resolve => { release = resolve; }); + return {promise,release}; +}; + +test('queued repair keeps actions disabled after the read-only wrapper finishes', async (t) => { + const read = gate(), readStarted = gate(), upload = gate(), uploadStarted = gate(); + t.after(() => { read.release(); upload.release(); }); + let firstRead = true; + const f = harness({post:async ({path}) => { + if (path.endsWith('getBackupSnapshot') && firstRead) { + firstRead = false; readStarted.release(); await read.promise; + } + if (path.endsWith('repairAppBackup')) { uploadStarted.release(); await upload.promise; } + }}); + f.local.Wallet.push(wallet()); + const runner = inspectionRunner(f); + const check = runner.start(false); + await readStarted.promise; + const repair = runner.start(true); + read.release(); + assert.equal(await check.task, false); + await uploadStarted.promise; + assert.equal(repair.state.backupRepairRunningByAppId.disposable, true); + assert.equal(repair.actions.some(a => a.type === 'bhr/setBackupRepairRunning' && !a.payload.running), false); + upload.release(); + assert.equal(await repair.task, true); + assert.equal(repair.state.backupRepairRunningByAppId.disposable, false); +}); + +test('cancelling a coalesced wrapper does not clear another running repair', async (t) => { + const upload = gate(), uploadStarted = gate(); + t.after(() => upload.release()); + const f = harness({post:async ({path}) => { + if (path.endsWith('repairAppBackup')) { uploadStarted.release(); await upload.promise; } + }}); + f.local.Wallet.push(wallet()); + const runner = inspectionRunner(f), first = runner.start(true); + await uploadStarted.promise; + const second = runner.start(true); + first.sagaTask.cancel(); + await new Promise(resolve => setImmediate(resolve)); + assert.equal(second.state.backupRepairRunningByAppId.disposable, true); + upload.release(); + await first.task; + assert.equal(await second.task, true); + assert.equal(second.state.backupRepairRunningByAppId.disposable, false); + assert.equal(f.calls.filter(path => path === 'repairAppBackup').length, 1); +}); + +test('cancellation retains progress until its non-cancellable inspection actually settles', async (t) => { + const read = gate(), readStarted = gate(); + t.after(() => read.release()); + const f = harness({post:async () => { readStarted.release(); await read.promise; }}); + const run = runInspection(f, false); + await readStarted.promise; + run.sagaTask.cancel(); + await new Promise(resolve => setImmediate(resolve)); + assert.equal(run.state.backupRepairRunningByAppId.disposable, true); + read.release(); + await run.task; + await run.settled(); + await new Promise(resolve => setImmediate(resolve)); + assert.equal(run.state.backupRepairRunningByAppId.disposable, false); +}); test('only verified readback sets durable per-account completion and clears pending', async () => { const f = harness(); f.local.Wallet.push(wallet()); const run=runInspection(f,true); From 27943db4afda7b5cdf3d66b821bb46df2f1497dc Mon Sep 17 00:00:00 2001 From: Anant Date: Wed, 7 Oct 2026 23:26:39 +0530 Subject: [PATCH 039/254] ci: check release consistency on actual checkout --- .github/workflows/test.yml | 3 +++ openspec/changes/fix-release-version-consistency/design.md | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 900273c88c..8a5680c736 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -60,5 +60,8 @@ jobs: - name: Run recovery regression tests run: npm run test:recovery + - name: Verify checked-out release version consistency + run: npm run version:check + - name: Run release and packaged-artifact tests run: npm run test:release diff --git a/openspec/changes/fix-release-version-consistency/design.md b/openspec/changes/fix-release-version-consistency/design.md index f8df1e8d18..2862d15f90 100644 --- a/openspec/changes/fix-release-version-consistency/design.md +++ b/openspec/changes/fix-release-version-consistency/design.md @@ -41,6 +41,6 @@ DESIGN.md reviewed: no UI change, so existing typography, colors, components and ## Open Questions -- Candidate selected: 2.5.15, Android 622, iOS 615. Refresh store baselines before upload. +- This baseline records 2.6.3, Android 626, iOS 619, matching `release/version.json` and the release guide. R3 is an internal work bucket; any subsequent app delivery needs refreshed store baselines and new version intent before building or uploading. - Canonical release source after reconciling iOS and Android published-build provenance. - Public APK channel is GitHub Releases with SHA256SUM.asc and KEEPER_DETACHED_SIGN.sign. Existing local keystore certificate matches public APK 2.5.13; PGP signing access remains to be resolved. From 8d3e06855472ef65a8026b535fd2e2f033482bcb Mon Sep 17 00:00:00 2001 From: Anant Date: Wed, 7 Oct 2026 23:46:45 +0530 Subject: [PATCH 040/254] test(backup): verify fresh inspection after fingerprint migration --- tests/recovery/fingerprintTransport.test.cjs | 49 +++++++++++++++----- 1 file changed, 37 insertions(+), 12 deletions(-) diff --git a/tests/recovery/fingerprintTransport.test.cjs b/tests/recovery/fingerprintTransport.test.cjs index 8b971e26c0..bb5daab124 100644 --- a/tests/recovery/fingerprintTransport.test.cjs +++ b/tests/recovery/fingerprintTransport.test.cjs @@ -21,6 +21,7 @@ function relay(transport, rest = { post: () => assert.fail('Migration bypassed b for (const repair of [false, true]) { test(`fingerprint migration waits for ${repair ? 'repair readback' : 'inspection'} and invalidates its completion`, async (t) => { let armed = false, reads = 0, release, entered; + const events = []; const readGate = new Promise((resolve) => { release = resolve; }); const readStarted = new Promise((resolve) => { entered = resolve; }); t.after(() => release()); @@ -30,19 +31,26 @@ for (const repair of [false, true]) { .update(JSON.stringify({ remote: f.remote, vaults, labels })).digest('hex'); f = harness({ post: async ({ path }) => { - if (armed && path.endsWith('getBackupSnapshot') && ++reads === (repair ? 2 : 1)) { - entered(); - await readGate; + if (armed && path.endsWith('getBackupSnapshot')) { + events.push(`snapshot-request:${++reads}`); + if (reads === (repair ? 2 : 1)) { + entered(); + await readGate; + } } }, adapter: (path, payload, config) => { assert.ok(config.signal, 'Every backup request must use bounded transport'); - if (path === 'getBackupSnapshot') return { data: { - appImage: f.remote, allVaultImages: vaults, labels, revision: revision(), - } }; + if (path === 'getBackupSnapshot') { + if (armed) events.push(`snapshot-response:${reads}`); + return { data: { + appImage: f.remote, allVaultImages: vaults, labels, revision: revision(), + } }; + } if (path === 'migrateXfps') { assert.equal(payload.appId, f.app.id); assert.deepEqual(JSON.parse(JSON.stringify(payload.signerChanges)), signerChanges); + events.push('migration-acknowledged'); return { data: { updated: true } }; } assert.equal(path, 'repairAppBackup'); @@ -63,7 +71,10 @@ for (const repair of [false, true]) { f.calls.length = 0; f.phases.length = 0; armed = true; - const checking = f.run(repair); + const checking = f.repair.inspectBackup(f.app.id, repair, (phase) => { + f.phases.push(phase); + events.push(`phase:${phase}`); + }); await readStarted; const previousRevision = f.transport.backupRevision(f.app.id); const migration = relay(f.transport).migrateXfp(f.app.id, signerChanges); @@ -75,13 +86,27 @@ for (const repair of [false, true]) { assert.equal(requestedRevision, previousRevision + 1); assert.equal(migrationStartedDuringCheck, false); assert.equal(checked.status, 'fulfilled'); - assert.equal(checked.value, 'unverified'); - assert.equal(f.phases.includes('verified'), false); assert.equal(migrated.status, 'fulfilled'); assert.equal(migrated.value.updated, true); - assert.deepEqual(f.calls, repair - ? ['getBackupSnapshot', 'repairAppBackup', 'getBackupSnapshot', 'migrateXfps'] - : ['getBackupSnapshot', 'migrateXfps']); + if (!repair && checked.value === 'verified') { + // A read-only retry may verify a new snapshot after queued writes drain. + // The invalidated first attempt must never publish verified completion. + assert.deepEqual(f.calls, ['getBackupSnapshot', 'migrateXfps', 'getBackupSnapshot']); + assert.equal(reads, 2); + assert.equal(f.phases.filter((phase) => phase === 'verified').length, 1); + const acknowledged = events.indexOf('migration-acknowledged'); + const freshRequest = events.indexOf('snapshot-request:2'); + const freshResponse = events.indexOf('snapshot-response:2'); + const verified = events.indexOf('phase:verified'); + assert.ok(acknowledged >= 0 && freshRequest > acknowledged); + assert.ok(freshResponse > freshRequest && verified > freshResponse); + } else { + assert.equal(checked.value, 'unverified'); + assert.equal(f.phases.includes('verified'), false); + assert.deepEqual(f.calls, repair + ? ['getBackupSnapshot', 'repairAppBackup', 'getBackupSnapshot', 'migrateXfps'] + : ['getBackupSnapshot', 'migrateXfps']); + } }); } From fee03fab6557595e3180b6fb895becf3a6f85815 Mon Sep 17 00:00:00 2001 From: Anant Date: Wed, 7 Oct 2026 23:47:50 +0530 Subject: [PATCH 041/254] test(backup): require read completion before migration acknowledgement --- tests/recovery/fingerprintTransport.test.cjs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/recovery/fingerprintTransport.test.cjs b/tests/recovery/fingerprintTransport.test.cjs index bb5daab124..4e035df3ba 100644 --- a/tests/recovery/fingerprintTransport.test.cjs +++ b/tests/recovery/fingerprintTransport.test.cjs @@ -88,13 +88,16 @@ for (const repair of [false, true]) { assert.equal(checked.status, 'fulfilled'); assert.equal(migrated.status, 'fulfilled'); assert.equal(migrated.value.updated, true); + const heldResponse = events.indexOf(`snapshot-response:${repair ? 2 : 1}`); + const acknowledged = events.indexOf('migration-acknowledged'); + assert.ok(heldResponse >= 0 && acknowledged > heldResponse, + 'Fingerprint migration must wait for the held backup response'); if (!repair && checked.value === 'verified') { // A read-only retry may verify a new snapshot after queued writes drain. // The invalidated first attempt must never publish verified completion. assert.deepEqual(f.calls, ['getBackupSnapshot', 'migrateXfps', 'getBackupSnapshot']); assert.equal(reads, 2); assert.equal(f.phases.filter((phase) => phase === 'verified').length, 1); - const acknowledged = events.indexOf('migration-acknowledged'); const freshRequest = events.indexOf('snapshot-request:2'); const freshResponse = events.indexOf('snapshot-response:2'); const verified = events.indexOf('phase:verified'); From e1d99db4c4c957f85d317a8be1ff81148b67f24a Mon Sep 17 00:00:00 2001 From: Anant Date: Thu, 8 Oct 2026 00:46:41 +0530 Subject: [PATCH 042/254] build: pin Gradle distribution and stop logging generated env values --- android/gradle/wrapper/gradle-wrapper.properties | 1 + appcenter-pre-build.sh | 3 +-- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/android/gradle/wrapper/gradle-wrapper.properties b/android/gradle/wrapper/gradle-wrapper.properties index d4081da476..a54add3199 100644 --- a/android/gradle/wrapper/gradle-wrapper.properties +++ b/android/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,7 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists distributionUrl=https\://services.gradle.org/distributions/gradle-8.14.3-bin.zip +distributionSha256Sum=bd71102213493060956ec229d946beee57158dbd89d0e62b91bca0fa2c5f3531 networkTimeout=10000 validateDistributionUrl=true zipStoreBase=GRADLE_USER_HOME diff --git a/appcenter-pre-build.sh b/appcenter-pre-build.sh index 6adc321a73..5ea1c45f04 100644 --- a/appcenter-pre-build.sh +++ b/appcenter-pre-build.sh @@ -4,5 +4,4 @@ ENV_WHITELIST=${ENV_WHITELIST:-"^RN_"} printf "Creating an .env file with the following whitelist:\n" printf "%s\n" $ENV_WHITELIST set | egrep -e $ENV_WHITELIST | sed 's/^RN_//g' > .env -printf "\n.env created with contents:\n\n" -cat .env \ No newline at end of file +printf "\n.env created.\n" From a7f5ddd7cdfd17065f2d92cad56cbfc14cede6bc Mon Sep 17 00:00:00 2001 From: Anant Date: Thu, 8 Oct 2026 00:46:46 +0530 Subject: [PATCH 043/254] docs: add Android payload comparison and reproducibility evidence --- reproducibility/README.md | 130 +++++++++++++++++++ scripts/compare-android-apks.py | 178 +++++++++++++++++++++++++++ scripts/test_compare_android_apks.py | 149 ++++++++++++++++++++++ 3 files changed, 457 insertions(+) create mode 100644 reproducibility/README.md create mode 100755 scripts/compare-android-apks.py create mode 100644 scripts/test_compare_android_apks.py diff --git a/reproducibility/README.md b/reproducibility/README.md new file mode 100644 index 0000000000..424766716d --- /dev/null +++ b/reproducibility/README.md @@ -0,0 +1,130 @@ +# Android reproducibility status + +This directory records the inputs needed to compare a Bitcoin Keeper Android +release with a build from its public source. The comparison tool in +[`scripts/compare-android-apks.py`](../scripts/compare-android-apks.py) is an +inspection aid. It is **not** a WalletScrutiny build script, and this repository +does not yet establish a reproducibility verdict for v2.6.3. + +## v2.6.3 reference and measured result + +The public source tag is [`v2.6.3`](https://github.com/KeeperCommunity/bitcoin-keeper/releases/tag/v2.6.3), +commit `872cbc4b16ddcb0a8f3b5b1d7ebcde28e06ed096`. The GitHub release APK +`Bitcoin_Keeper_v2.6.3.apk` has SHA-256 +`a20d934ebd80ece779d3c171c7906bb4aff010337989ec1b826cab55c50eba46`. +Its package is `io.hexawallet.bitcoinkeeper`, version `2.6.3`, version code +`626`. + +An isolated build of that tag with a placeholder production configuration and +the checked-in debug signing key produced a valid 2.6.3 APK. Its SHA-256 was +`cc4102251a11cbbfa104808adc4906a3a0e3b2efd9a14ab0785d66f5c9d5143b`. +Comparing uncompressed APK entries found **1,424 matching payload entries and +192 differences**: one manifest, one baseline profile, one Hermes bundle, five +DEX files, 56 native libraries, and 128 resources. The placeholder +configuration and different build environment can affect these files. This is +a diagnostic mismatch, not evidence that the published binary was built from +different source. The measured result and remaining work are tracked in +[#6097](https://github.com/KeeperCommunity/bitcoin-keeper/issues/6097). + +For an SDK 33, arm64-v8a, xxhdpi, English-only profile, the four official +Play-generated APKs were also retrieved and verified. All 42 corresponding +code entries (six DEX files, Hermes, and 35 arm64 libraries) match the GitHub +release APK byte for byte. This establishes published-channel code parity for +that profile, not a rebuild from public source. A rebuilt AAB and split-set +comparison are still needed. + +| Play split | SHA-256 | +| --- | --- | +| SDK 29+ base | `e384b966359aa4937bf4353316ddda471adf1e17f6bb08fe53f9945e8ea9bc1d` | +| arm64-v8a | `11740257e813455501301d5dc0f9870dc476e15947d912523e051d9a3400ab8c` | +| xxhdpi | `da3b93b89e98cff52bbea6c593fc26193139d53f400139ebd44ac51118f08195` | +| English | `a5cae92f6e9c2d725bcf8fc3ebcfb397573fda38e1413dde7e40c395430f0a73` | + +## Compare two APKs + +Run with Python 3 and no extra packages: + +```sh +python3 scripts/compare-android-apks.py published.apk rebuilt.apk > comparison.json +``` + +Exit code 0 means all **uncompressed ZIP file entries** match after the narrow +signing exceptions below. Exit code 1 means at least one payload difference; +exit code 2 means invalid input. JSON includes whole-APK SHA-256 hashes, entry +counts, and every changed, added, or removed file path. ZIP directory records, +compression, entry order, and APK v2/v3 signing blocks are outside this payload +comparison. The tool assumes its inputs are APKs; verify their signatures, +package names, and versions separately. + +The only exempt entries are root-level JAR/v1 signature material +(`META-INF/MANIFEST.MF`, `META-INF/*.SF`, `*.RSA`, `*.DSA`, `*.EC`) and Play's +root `stamp-cert-sha256`. Their differences still appear in +`ignored_signing_entries`. DEX, Hermes, native libraries, manifests, resource +tables and files, baseline profiles, other assets, and other `META-INF` files +remain comparison-critical. A payload match does not authenticate either +signer or prove that the APK came from the claimed source. + +Synthetic tests cover signing exceptions, each payload category, added and +removed files, duplicate ZIP paths, and CLI exit codes: + +```sh +python3 -m unittest scripts/test_compare_android_apks.py +``` + +Compare one APK with one APK. Google Play delivers APKs generated from an AAB; +a Play base split cannot be equated with a standalone GitHub APK. Play +verification needs the complete delivered split set and corresponding splits +from a rebuilt AAB, with transformations examined separately. + +## Inputs needed for an independent build + +The tagged production lane requires an ignored `.env.production`. Its exact +v2.6.3 contents are not public. Names-only inspection of the published APK +found 13 environment-derived fields in `BuildConfig`. `react-native-config` +embeds supplied environment values in Android build outputs, including fields +outside the app's TypeScript configuration object. Placeholder values cannot +support an exact payload verdict. Some fields appear credential-bearing and +require owner security review before public build inputs are released; do not +commit an unchecked production environment file. + +The source pins Gradle wrapper 8.14.3, Android Gradle Plugin 8.8.0, Android +SDK/target 36, Build Tools 35.0.0, NDK 27.1.12297006, and React Native +0.83.9. The locked Realm dependency requests CMake 3.22.1. The dependency +tree is recorded in `yarn.lock` and checked-in `patch-package` patches. The +successful diagnostic used Node +22.23.3, Yarn 1.22.22, and JDK 17.0.19; these are measured diagnostic inputs, +not a claim about the publisher's complete toolchain. A third-party recipe +still needs a digest-pinned Linux container image, exact JDK/Node/Yarn and SDK +package checksums, a fixed checkout and dependency layout, and verified build +commands. The Gradle wrapper now has a distribution checksum pin, but the +Android project has no dependency lock or verification metadata. The published +Hermes bundle contains a Node 25.9.0 +literal injected by `rn-nodeify` and a path through a sibling +`keeper-release-2.5.15/node_modules` directory. These historical build inputs +must be investigated; copying them into a future release recipe would not by +itself reproduce v2.6.3. Realm and Screens also have native executable/data +differences in the placeholder rebuild. + +For the Android-only path, the diagnostic installed JavaScript packages with +`yarn install --frozen-lockfile --ignore-scripts`, then ran the checked-in +`patch-package` patches and the Android `rn-nodeify` step before invoking +`./gradlew :app:assembleProductionRelease`. The root `setup.sh` cannot serve +as a Linux build recipe unchanged: it runs iOS CocoaPods and writes a macOS SDK +path. A future container recipe needs a Linux `local.properties`, the verified +production environment input, and a disposable signing identity. It should +also run `:app:bundleProductionRelease` for Play split comparisons. These +commands describe the diagnostic path; they are not a complete reproducible +build recipe. + +The release Fastlane lane validates time-sensitive store baselines and uploads +an AAB to Play. An independent verifier should build locally without store +access or upload. It can use a disposable signing key and compare payloads +while explicitly accounting for signing differences. A complete recipe must +then build the production APK and AAB under pinned tools and compare the +actual distributed artifacts. + +[WalletScrutiny's script rules](https://github.com/WalletScrutiny/WalletScrutinyCom/blob/master/docs/script_verifications.md) +require Docker, Podman, or Nix; a script ending in `build.sh`; a `--binary` +argument; and an adjacent `COMPARISON_RESULTS.yaml`. No such script or results +file is included here because the exact production input and complete +third-party build path have not yet been established. diff --git a/scripts/compare-android-apks.py b/scripts/compare-android-apks.py new file mode 100755 index 0000000000..e92f8fa7ca --- /dev/null +++ b/scripts/compare-android-apks.py @@ -0,0 +1,178 @@ +#!/usr/bin/env python3 +"""Compare the uncompressed payload entries of two Android APKs. + +This is an inspection aid, not a WalletScrutiny build script or a Play attestation. +It never extracts archive entries to the filesystem. +""" + +from __future__ import annotations + +import argparse +from collections import Counter +import hashlib +import json +from pathlib import Path +import re +import sys +import zipfile + + +CHUNK_SIZE = 1024 * 1024 +MAX_ENTRIES = 100_000 +MAX_ENTRY_BYTES = 2 * 1024**3 +MAX_TOTAL_BYTES = 4 * 1024**3 + +# APK Signature Scheme v2/v3 blocks are outside ZIP entries. These names cover +# only root-level JAR/v1 signature material plus Play's certificate stamp. +JAR_SIGNATURE = re.compile(r"^META-INF/[^/]+\.(?:SF|RSA|DSA|EC)$") +DEX_FILE = re.compile(r"(?:^|/)classes\d*\.dex$") + + +def is_signing_only(name: str) -> bool: + return ( + name == "META-INF/MANIFEST.MF" + or name == "stamp-cert-sha256" + or JAR_SIGNATURE.fullmatch(name) is not None + ) + + +def category(name: str) -> str: + if DEX_FILE.search(name) or name.endswith(".dex"): + return "dex" + if name.startswith("assets/dexopt/") and name.endswith((".prof", ".profm")): + return "baseline_profile" + if name.startswith("lib/") and name.endswith(".so"): + return "native_library" + if name.endswith((".jsbundle", ".bundle")) or ( + name.startswith("assets/") and name.endswith(".js") + ): + return "javascript_bundle" + if name == "AndroidManifest.xml": + return "manifest" + if name == "resources.arsc" or name.startswith("res/"): + return "resource" + if name.startswith("META-INF/"): + return "metadata" + if name.startswith("assets/"): + return "asset" + return "other" + + +def file_sha256(path: Path) -> str: + digest = hashlib.sha256() + with path.open("rb") as stream: + for chunk in iter(lambda: stream.read(CHUNK_SIZE), b""): + digest.update(chunk) + return digest.hexdigest() + + +def inspect_apk(path: Path) -> dict: + if not path.is_file(): + raise ValueError(f"Not a regular file: {path}") + + with zipfile.ZipFile(path) as archive: + infos = archive.infolist() + if len(infos) > MAX_ENTRIES: + raise ValueError(f"Too many ZIP entries in {path}: {len(infos)}") + + names = [info.filename for info in infos] + duplicates = sorted(name for name, count in Counter(names).items() if count > 1) + if duplicates: + raise ValueError(f"Duplicate ZIP entry names in {path}: {duplicates}") + + files = [info for info in infos if not info.is_dir()] + total_bytes = sum(info.file_size for info in files) + if total_bytes > MAX_TOTAL_BYTES: + raise ValueError(f"Declared uncompressed ZIP size exceeds limit in {path}") + + entries = {} + for info in files: + if info.file_size > MAX_ENTRY_BYTES: + raise ValueError(f"ZIP entry exceeds size limit in {path}: {info.filename}") + digest = hashlib.sha256() + measured = 0 + # Passing ZipInfo selects the exact entry; reading to EOF checks its CRC. + with archive.open(info) as stream: + for chunk in iter(lambda: stream.read(CHUNK_SIZE), b""): + measured += len(chunk) + if measured > MAX_ENTRY_BYTES: + raise ValueError(f"ZIP entry exceeds size limit in {path}: {info.filename}") + digest.update(chunk) + if measured != info.file_size: + raise ValueError(f"Incorrect ZIP entry size in {path}: {info.filename}") + entries[info.filename] = {"sha256": digest.hexdigest(), "size": measured} + + return { + "path": str(path.resolve()), + "apk_sha256": file_sha256(path), + "payload_entry_count": len(entries), + "entries": entries, + } + + +def compare(reference: dict, rebuilt: dict) -> dict: + reference_entries = reference["entries"] + rebuilt_entries = rebuilt["entries"] + differences = [] + ignored_signing_entries = [] + matching_payload_entries = 0 + + for name in sorted(reference_entries.keys() | rebuilt_entries.keys()): + before = reference_entries.get(name) + after = rebuilt_entries.get(name) + if before == after: + if not is_signing_only(name): + matching_payload_entries += 1 + continue + + status = ( + "only_reference" if after is None else + "only_rebuilt" if before is None else + "content_mismatch" + ) + detail = { + "path": name, + "status": status, + "category": "signing_only" if is_signing_only(name) else category(name), + "reference": before, + "rebuilt": after, + } + if is_signing_only(name): + ignored_signing_entries.append(detail) + else: + differences.append(detail) + + return { + "schema_version": 1, + "verdict": "match" if not differences else "mismatch", + "scope": "one APK versus one APK; uncompressed ZIP file entries", + "reference": {key: value for key, value in reference.items() if key != "entries"}, + "rebuilt": {key: value for key, value in rebuilt.items() if key != "entries"}, + "summary": { + "matching_payload_entries": matching_payload_entries, + "payload_differences": len(differences), + "ignored_signing_differences": len(ignored_signing_entries), + }, + "differences": differences, + "ignored_signing_entries": ignored_signing_entries, + } + + +def main() -> int: + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("reference_apk", type=Path, help="Published reference APK") + parser.add_argument("rebuilt_apk", type=Path, help="APK rebuilt from source") + args = parser.parse_args() + + try: + report = compare(inspect_apk(args.reference_apk), inspect_apk(args.rebuilt_apk)) + except (OSError, ValueError, RuntimeError, EOFError, NotImplementedError, zipfile.BadZipFile) as error: + report = {"schema_version": 1, "verdict": "invalid_input", "error": str(error)} + + json.dump(report, sys.stdout, indent=2, sort_keys=True) + sys.stdout.write("\n") + return {"match": 0, "mismatch": 1, "invalid_input": 2}[report["verdict"]] + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/scripts/test_compare_android_apks.py b/scripts/test_compare_android_apks.py new file mode 100644 index 0000000000..8841cf0092 --- /dev/null +++ b/scripts/test_compare_android_apks.py @@ -0,0 +1,149 @@ +"""Synthetic APK tests for the payload comparison aid.""" + +import contextlib +import importlib.util +import io +import json +from pathlib import Path +import sys +import tempfile +import unittest +from unittest.mock import patch +import warnings +import zipfile + + +spec = importlib.util.spec_from_file_location( + "compare_android_apks", Path(__file__).with_name("compare-android-apks.py") +) +comparator = importlib.util.module_from_spec(spec) +spec.loader.exec_module(comparator) + + +class ApkComparisonTests(unittest.TestCase): + def setUp(self): + temporary = tempfile.TemporaryDirectory() + self.addCleanup(temporary.cleanup) + self.directory = Path(temporary.name) + + def apk(self, name, entries, *, compression=zipfile.ZIP_STORED): + path = self.directory / name + with zipfile.ZipFile(path, "w", compression=compression) as archive: + for entry_name, content in entries.items(): + archive.writestr(entry_name, content) + return path + + def result(self, reference, rebuilt): + return comparator.compare( + comparator.inspect_apk(reference), comparator.inspect_apk(rebuilt) + ) + + def test_only_root_signing_material_is_excluded(self): + common = { + "classes.dex": b"same dex", + "META-INF/services/provider": b"same non-signing metadata", + } + reference = self.apk("published.apk", { + **common, + "META-INF/MANIFEST.MF": b"original manifest digest", + "META-INF/CERT.SF": b"original signature file", + "META-INF/CERT.RSA": b"original certificate", + "stamp-cert-sha256": b"original stamp", + }) + rebuilt = self.apk("rebuilt.apk", { + **common, + "META-INF/MANIFEST.MF": b"different manifest digest", + "META-INF/CERT.SF": b"different signature file", + "META-INF/CERT.RSA": b"different certificate", + "stamp-cert-sha256": b"different stamp", + }, compression=zipfile.ZIP_DEFLATED) + + result = self.result(reference, rebuilt) + self.assertEqual(result["verdict"], "match") + self.assertEqual(result["summary"], { + "matching_payload_entries": 2, + "payload_differences": 0, + "ignored_signing_differences": 4, + }) + self.assertNotEqual(result["reference"]["apk_sha256"], result["rebuilt"]["apk_sha256"]) + + def test_every_executable_and_resource_category_remains_critical(self): + entries = { + "AndroidManifest.xml": "manifest", + "resources.arsc": "resource", + "res/drawable/icon.png": "resource", + "classes.dex": "dex", + "assets/index.android.bundle": "javascript_bundle", + "lib/arm64-v8a/librealm.so": "native_library", + "assets/dexopt/baseline.prof": "baseline_profile", + "META-INF/com.android.tools.build.gradle/app-metadata.properties": "metadata", + } + reference = self.apk("published.apk", {name: b"old" for name in entries}) + rebuilt = self.apk("rebuilt.apk", {name: b"new" for name in entries}) + + result = self.result(reference, rebuilt) + self.assertEqual(result["verdict"], "mismatch") + self.assertEqual(result["summary"]["payload_differences"], len(entries)) + self.assertEqual(result["summary"]["ignored_signing_differences"], 0) + self.assertEqual( + {entry["path"]: entry["category"] for entry in result["differences"]}, + entries, + ) + self.assertTrue(all(entry["status"] == "content_mismatch" for entry in result["differences"])) + + def test_added_removed_and_nested_signature_named_entries_are_reported(self): + reference = self.apk("published.apk", { + "classes.dex": b"same", + "assets/META-INF/CERT.RSA": b"old nested asset", + "assets/retired.dat": b"removed", + }) + rebuilt = self.apk("rebuilt.apk", { + "classes.dex": b"same", + "assets/META-INF/CERT.RSA": b"new nested asset", + "assets/added.dat": b"added", + }) + + result = self.result(reference, rebuilt) + self.assertEqual(result["verdict"], "mismatch") + self.assertEqual(result["summary"]["matching_payload_entries"], 1) + self.assertEqual(result["summary"]["payload_differences"], 3) + self.assertEqual(result["ignored_signing_entries"], []) + self.assertEqual( + {entry["path"]: entry["status"] for entry in result["differences"]}, + { + "assets/META-INF/CERT.RSA": "content_mismatch", + "assets/retired.dat": "only_reference", + "assets/added.dat": "only_rebuilt", + }, + ) + + def test_duplicate_zip_paths_are_rejected(self): + path = self.directory / "duplicate.apk" + with warnings.catch_warnings(): + warnings.simplefilter("ignore", UserWarning) + with zipfile.ZipFile(path, "w") as archive: + archive.writestr("classes.dex", b"first") + archive.writestr("classes.dex", b"second") + with self.assertRaisesRegex(ValueError, "Duplicate ZIP entry names"): + comparator.inspect_apk(path) + + def test_cli_exit_codes_and_machine_readable_reports(self): + reference = self.apk("published.apk", {"classes.dex": b"same"}) + rebuilt = self.apk("rebuilt.apk", {"classes.dex": b"different"}) + invalid = self.directory / "invalid.apk" + invalid.write_bytes(b"not an APK") + + for candidate, expected_code, expected_verdict in ( + (reference, 0, "match"), + (rebuilt, 1, "mismatch"), + (invalid, 2, "invalid_input"), + ): + with self.subTest(candidate=candidate.name), patch.object( + sys, "argv", ["compare-android-apks.py", str(reference), str(candidate)] + ), contextlib.redirect_stdout(io.StringIO()) as output: + self.assertEqual(comparator.main(), expected_code) + self.assertEqual(json.loads(output.getvalue())["verdict"], expected_verdict) + + +if __name__ == "__main__": + unittest.main() From 0aa9a6a687f64f28fafe998d0d9c05d871c310a0 Mon Sep 17 00:00:00 2001 From: Anant Date: Thu, 8 Oct 2026 09:27:38 +0530 Subject: [PATCH 044/254] Pause new USDT wallet setup and commerce Keep existing USDT wallets, Send, Receive, history, and backup routes. Remove new wallet creation and import entry points, plus buy, sell, and swap entry points. Leave the legacy GasFree Send implementation intact until a working replacement is verified. --- src/context/Localization/language/en.json | 2 + src/context/Localization/language/es.json | 2 + src/hooks/useUSDTWallets.ts | 7 + src/navigation/Navigator.tsx | 8 - .../Home/components/Wallet/HomeWallet.tsx | 189 +----------------- src/screens/Home/components/buyBtc/BuyBtc.tsx | 104 +--------- src/screens/USDT/UsdtDetails.tsx | 61 +++--- 7 files changed, 54 insertions(+), 319 deletions(-) diff --git a/src/context/Localization/language/en.json b/src/context/Localization/language/en.json index 3a451be678..a865c1c5c6 100644 --- a/src/context/Localization/language/en.json +++ b/src/context/Localization/language/en.json @@ -2153,6 +2153,8 @@ "psbtMissingSignature": "This PSBT does not include a signature for the selected signer" }, "usdtWalletText": { + "pausedTitle": "Some USDT options are paused", + "pausedBody": "New USDT wallets, buying, selling and swapping are unavailable for now. Your existing wallet, address, balance and transaction history remain here.", "pickWalletType": "Pick Your Wallet Type", "selectCurrency": "Select the currency you would like to use", "bitcoinWallet": "Bitcoin Wallet", diff --git a/src/context/Localization/language/es.json b/src/context/Localization/language/es.json index 4670ae75db..6d0f7426d8 100644 --- a/src/context/Localization/language/es.json +++ b/src/context/Localization/language/es.json @@ -2134,6 +2134,8 @@ "psbtMissingSignature": "This PSBT does not include a signature for the selected signer" }, "usdtWalletText": { + "pausedTitle": "Algunas opciones de USDT están pausadas", + "pausedBody": "Por ahora no se pueden crear nuevas carteras de USDT ni comprar, vender o intercambiar USDT. Tu cartera, dirección, saldo e historial de transacciones siguen aquí.", "pickWalletType": "Pick Your Wallet Type", "selectCurrency": "Select the currency you would like to use", "bitcoinWallet": "Bitcoin Wallet", diff --git a/src/hooks/useUSDTWallets.ts b/src/hooks/useUSDTWallets.ts index 12578e9f78..f10f538f58 100644 --- a/src/hooks/useUSDTWallets.ts +++ b/src/hooks/useUSDTWallets.ts @@ -32,6 +32,9 @@ export interface UseUSDTWalletsOptions { includeHidden?: boolean; } +// Existing USDT wallets remain readable, but new wallet creation and import are paused. +export const USDT_WALLET_CREATION_PAUSED = true; + export interface UseUSDTWalletsReturn { usdtWallets: USDTWallet[]; error: string | null; @@ -101,6 +104,10 @@ export const useUSDTWallets = (options: UseUSDTWalletsOptions = {}): UseUSDTWall primaryMnemonic?: string; importDetails?: USDTWalletImportDetails; }): Promise<{ newWallet?: USDTWallet; error?: string }> => { + if (USDT_WALLET_CREATION_PAUSED) { + return { error: 'USDT wallet setup is paused' }; + } + try { setError(null); const walletNetworkType = USDTWalletSupportedNetwork; diff --git a/src/navigation/Navigator.tsx b/src/navigation/Navigator.tsx index dd9327ee96..50fa3849a1 100644 --- a/src/navigation/Navigator.tsx +++ b/src/navigation/Navigator.tsx @@ -154,10 +154,8 @@ import SetupAdditionalServerKey from 'src/screens/SigningDevices/SetupAdditional import { MultiUserScreen } from 'src/screens/AppSettings/MultiUserScreen'; import { AddMultipleXpubFiles } from 'src/screens/AddSigner/AddMultipleXpubFiles'; import { SignMessageScreen } from 'src/screens/WalletDetails/SignMessageScreen'; -import AddUsdtWallet from 'src/screens/USDT/UsdtAddWallet'; import UsdtDetails from 'src/screens/USDT/UsdtDetails'; import RecieveUsdt from 'src/screens/USDT/RecieveUsdt'; -import BuyUstd from 'src/screens/USDT/BuyUstd'; import SendUsdt from 'src/screens/USDT/SendUsdt'; import UsdtAmount from 'src/screens/USDT/UsdtAmount'; import UsdtSendConfirmation from 'src/screens/USDT/UsdtSendConfirmation'; @@ -170,8 +168,6 @@ import KeeperSupport from 'src/screens/KeeperConcierge/KeeperSupport'; import Advisors from 'src/screens/Advisors/Advisors'; import FilterAdvisor from 'src/screens/Advisors/FilterAdvisor'; import AdvisorDetail from 'src/screens/Advisors/AdvisorDetail'; -import { Swaps } from 'src/screens/Home/components/buyBtc/Swap/Swaps'; -import { SwapDetails } from 'src/screens/Home/components/buyBtc/Swap/SwapDetails'; import { SwapHistory } from 'src/screens/Home/components/buyBtc/Swap/SwapHistory'; import { SwapHistoryDetail } from 'src/screens/Home/components/buyBtc/Swap/SwapHistoryDetail'; import { SwapAllHistory } from 'src/screens/Home/components/buyBtc/Swap/SwapAllHistory'; @@ -295,10 +291,8 @@ function AppStack() { - - @@ -384,8 +378,6 @@ function AppStack() { - - diff --git a/src/screens/Home/components/Wallet/HomeWallet.tsx b/src/screens/Home/components/Wallet/HomeWallet.tsx index f277a0b79a..95978a0004 100644 --- a/src/screens/Home/components/Wallet/HomeWallet.tsx +++ b/src/screens/Home/components/Wallet/HomeWallet.tsx @@ -1,7 +1,6 @@ import { Box, useColorMode, View } from '@gluestack-ui/themed-native-base'; import React, { useContext, useState, useEffect } from 'react'; import { FlatList, StyleSheet, TouchableOpacity } from 'react-native'; -import DashedCta from 'src/components/DashedCta'; import WalletCard from './WalletCard'; import Colors from 'src/theme/Colors'; import useWallets from 'src/hooks/useWallets'; @@ -10,11 +9,11 @@ import { Wallet } from 'src/services/wallets/interfaces/wallet'; import { Vault } from 'src/services/wallets/interfaces/vault'; import useWalletAsset from 'src/hooks/useWalletAsset'; -import { EntityKind, VisibilityType, WalletType } from 'src/services/wallets/enums'; -import { useNavigation, useFocusEffect, CommonActions } from '@react-navigation/native'; +import { EntityKind, VisibilityType } from 'src/services/wallets/enums'; +import { useNavigation } from '@react-navigation/native'; import KeeperModal from 'src/components/KeeperModal'; import Text from 'src/components/KeeperText'; -import { hp, windowWidth, wp } from 'src/constants/responsive'; +import { hp, wp } from 'src/constants/responsive'; import NewWalletIcon from 'src/assets/images/wallet-white-small.svg'; import ImportWalletIcon from 'src/assets/images/import.svg'; @@ -23,26 +22,18 @@ import CollaborativeWalletIcon from 'src/assets/images/collaborative_vault_white import { useAppSelector } from 'src/store/hooks'; import { resetCollaborativeSession } from 'src/store/reducers/vaults'; import { useDispatch } from 'react-redux'; -import { autoSyncWallets, refreshWallets } from 'src/store/sagaActions/wallets'; +import { autoSyncWallets } from 'src/store/sagaActions/wallets'; import { RefreshControl } from 'react-native'; import { ELECTRUM_CLIENT } from 'src/services/electrum/client'; import ActivityIndicatorView from 'src/components/AppActivityIndicator/ActivityIndicatorView'; import CircleIconWrapper from 'src/components/CircleIconWrapper'; -import ThemedColor from 'src/components/ThemedColor/ThemedColor'; -import ThemedSvg from 'src/components/ThemedSvg.tsx/ThemedSvg'; import { LocalizationContext } from 'src/context/Localization/LocContext'; -import BitCoinWalletLogo from 'src/assets/images/bitcoin-wallet-logo.svg'; -import UsdtWalletLogo from 'src/assets/images/usdt-wallet-logo.svg'; import { useUSDTWallets } from 'src/hooks/useUSDTWallets'; import { getAvailableBalanceUSDTWallet, USDTWallet, - USDTWalletSupportedNetwork, - USDTWalletType, } from 'src/services/wallets/factories/USDTWalletFactory'; import useToastMessage from 'src/hooks/useToastMessage'; -import TickIcon from 'src/assets/images/icon_tick.svg'; -import ToastErrorIcon from 'src/assets/images/toast_error.svg'; import Fab from 'src/components/Fab'; import AddIcon from 'src/assets/images/add_white.svg'; import { useUTXOSpendability } from 'src/hooks/useUTXOSpendability'; @@ -105,28 +96,24 @@ function WalletCardItem({ const HomeWallet = () => { const { colorMode } = useColorMode(); - const isDarkMode = colorMode === 'dark'; const navigation = useNavigation(); const { wallets } = useWallets({ getAll: true }); const { translations } = useContext(LocalizationContext); - const { wallet: walletText, home, common, usdtWalletText } = translations; + const { wallet: walletText, home, common } = translations; const { getWalletCardGradient, getWalletTags } = useWalletAsset(); const { allVaults } = useVault({ includeArchived: false, getFirst: true, getHiddenWallets: false, }); - const { usdtWallets, createWallet } = useUSDTWallets(); + const { usdtWallets } = useUSDTWallets(); const { collaborativeSession } = useAppSelector((state) => state.vault); - const { bitcoinNetworkType } = useAppSelector((state) => state.settings); const dispatch = useDispatch(); const [showAddWalletModal, setShowAddWalletModal] = useState(false); const [collabSessionExistsModalVisible, setCollabSessionExistsModalVisible] = useState(false); const [pullRefresh, setPullRefresh] = useState(false); const { walletSyncing } = useAppSelector((state) => state.wallet); - const [pickWalletType, setPickWalletType] = useState(false); - const [createUsdtWallet, setCreateUsdtWallet] = useState(false); const syncing = ELECTRUM_CLIENT.isClientConnected && Object.values(walletSyncing).some((isSyncing) => isSyncing); @@ -140,12 +127,6 @@ const HomeWallet = () => { ...usdtWallets, ].filter((item) => item !== null); const [isShowAmount, setIsShowAmount] = useState(false); - const DashedCta_hexagonBackgroundColor = ThemedColor({ - name: 'DashedCta_hexagonBackgroundColor', - }); - const dashed_CTA_background = ThemedColor({ - name: 'dashed_CTA_background', - }); const { showToast } = useToastMessage(); const pendingDustToast = useAppSelector((state: any) => state.utxos.pendingDustToast); @@ -176,35 +157,6 @@ const HomeWallet = () => { setPullRefresh(false); }; - const importUSDTWallet = async (mnemonic) => { - try { - const { newWallet, error } = await createWallet({ - type: USDTWalletType.IMPORTED, - name: 'USDT Wallet', - description: 'Imported USDT Wallet', - importDetails: { - mnemonic, - }, - }); - - if (newWallet) { - showToast('USDT wallet imported successfully!', ); - setTimeout(() => { - navigation.dispatch( - CommonActions.navigate({ - name: 'Home', - params: { selectedOption: 'Wallets' }, - }) - ); - }, 900); - } else { - throw new Error(error); - } - } catch (err) { - showToast(`Failed to import USDT wallet: ${err.message}`, ); - } - }; - const CREATE_WALLET_OPTIONS = [ { title: walletText.createWallet, @@ -234,38 +186,6 @@ const HomeWallet = () => { id: 'collaborativeWallet', }, ]; - const CREATE_USDT_WALLET_OPTIONS = [ - { - title: walletText.createWallet, - subtitle: 'Create a new USDT wallet', - icon: , - onPress: () => { - navigation.navigate('addUsdtWallet'); - setCreateUsdtWallet(false); - }, - id: 'usdtnewWallet', - }, - { - title: home.ImportWallet, - subtitle: walletText.restoreExistingWallet, - icon: , - onPress: () => { - setCreateUsdtWallet(false); - navigation.dispatch( - CommonActions.navigate({ - name: 'EnterSeedScreen', - params: { - isImport: true, - isUSDTWallet: true, - importSeedCta: importUSDTWallet, - }, - }) - ); - }, - id: 'usdtimportWallet', - }, - ]; - const renderWalletCard = ({ item }: { item: Wallet | Vault | USDTWallet }) => ( { } - onPress={() => setPickWalletType(true)} + onPress={() => setShowAddWalletModal(true)} /> { )} /> - setCreateUsdtWallet(false)} - textColor={`${colorMode}.textGreen`} - subTitleColor={`${colorMode}.modalSubtitleBlack`} - showCloseIcon - Content={() => ( - - {CREATE_USDT_WALLET_OPTIONS.map((option, index) => ( - - ))} - - )} - /> setCollabSessionExistsModalVisible(false)} @@ -343,63 +247,6 @@ const HomeWallet = () => { navigation.navigate('SetupCollaborativeWallet'); }} /> - setPickWalletType(false)} - title={usdtWalletText.pickWalletType} - subTitle={usdtWalletText.selectCurrency} - textColor={`${colorMode}.textGreen`} - subTitleColor={`${colorMode}.modalSubtitleBlack`} - modalBackground={`${colorMode}.modalWhiteBackground`} - Content={() => ( - - { - setShowAddWalletModal(true); - setPickWalletType(false); - }} - > - - } - backgroundColor={Colors.BrightOrange} - /> - - {usdtWalletText.bitcoinWallet} - - - - {bitcoinNetworkType === USDTWalletSupportedNetwork ? ( - { - setCreateUsdtWallet(true); - setPickWalletType(false); - }} - > - - } - backgroundColor={Colors.DesaturatedTeal} - /> - - {usdtWalletText.dollarWallet} - - - - ) : null} - - )} - /> ); }; @@ -457,26 +304,4 @@ const styles = StyleSheet.create({ borderRadius: 12, borderWidth: 1, }, - customStyle: { - marginBottom: hp(10), - }, - DashedCtaStyle: { - width: windowWidth * 0.88, - }, - walletTypeContainer: { - gap: wp(15), - marginBottom: hp(10), - flexDirection: 'row', - justifyContent: 'center', - alignContent: 'center', - }, - typeCard: { - width: wp(148), - height: hp(104), - alignItems: 'center', - borderWidth: 1, - justifyContent: 'center', - borderRadius: 12, - gap: wp(10), - }, }); diff --git a/src/screens/Home/components/buyBtc/BuyBtc.tsx b/src/screens/Home/components/buyBtc/BuyBtc.tsx index ecb21234a9..060c20ef4a 100644 --- a/src/screens/Home/components/buyBtc/BuyBtc.tsx +++ b/src/screens/Home/components/buyBtc/BuyBtc.tsx @@ -19,11 +19,11 @@ import useToastMessage from 'src/hooks/useToastMessage'; import ToastErrorIcon from 'src/assets/images/toast_error.svg'; import AcquireCard from './AcquireCard'; import BtcAcquireIcon from 'src/assets/images/bitcoin-acquire-icon.svg'; -import UsdtWalletLogo from 'src/assets/images/usdt-wallet-logo.svg'; -import { useUSDTWallets } from 'src/hooks/useUSDTWallets'; -import { fetchSellBtcLink, fetchSellUsdtLink } from 'src/services/thirdparty/ramp'; -import Buttons from 'src/components/Buttons'; +import { fetchSellBtcLink } from 'src/services/thirdparty/ramp'; import ActivityIndicatorView from 'src/components/AppActivityIndicator/ActivityIndicatorView'; +import { useQuery } from '@realm/react'; +import { RealmSchema } from 'src/storage/realm/enum'; +import { SwapHistory } from './Swap/SwapHistory'; const BuyBtc = () => { const { colorMode } = useColorMode(); @@ -35,21 +35,17 @@ const BuyBtc = () => { const { buyBTC: buyBTCText, common } = translations; const [visibleBuyBtc, setVisibleBuyBtc] = useState(false); const [selectedWallet, setSelectedWallet] = useState(null); - const [selectedUsdtWallet, setSelectedUsdtWallet] = useState(null); const navigation = useNavigation(); const [graphData, setGraphData] = useState([]); const [error, setError] = useState(false); const [stats, setStats] = useState(null); const [visibleSellBtc, setVisibleSellBtc] = useState(false); - const [visibleSellUsdt, setVisibleSellUsdt] = useState(false); - const [visibleBuyUsdt, setVisibleBuyUsdt] = useState(false); const { wallets } = useWallets(); const { allVaults } = useVault({ getHiddenWallets: false }); const { showToast } = useToastMessage(); const allWallets = [...wallets, ...allVaults]; - const { usdtWallets } = useUSDTWallets(); - const [usdtPrice, setUsdtPrice] = useState(null); + const previousSwaps = useQuery(RealmSchema.SwapHistory); const [loading, setLoading] = useState(false); useEffect(() => { @@ -58,13 +54,9 @@ const BuyBtc = () => { const loadBtcPrice = async () => { try { - let [usdtData, btcPrice] = await Promise.all([ - Relay.getUsdtPrice(currencyCode), - Relay.getBtcPrice(currencyCode), - ]); + const btcPrice = await Relay.getBtcPrice(currencyCode); const { dailyPrice, high24h, latestPrice, low24h, percentChange, valueChange } = manipulateBitcoinPrices(btcPrice?.prices); - setUsdtPrice(usdtData[0].current_price.toFixed(2)); setGraphData(dailyPrice); setStats({ high24h, low24h, latestPrice, percentChange, valueChange }); } catch (error) { @@ -106,31 +98,8 @@ const BuyBtc = () => { }} graphContent={} /> - } - amount={`${BtcPrice?.symbol} ${usdtPrice ?? '--'} `} - buyCallback={() => { - if (usdtWallets.length) setVisibleBuyUsdt(true); - else showToast('Please create a USDT wallet to proceed.', ); - }} - sellCallback={() => { - if (usdtWallets.length > 0) { - setVisibleSellUsdt(true); - } else showToast("You don't have USDT yet.", ); - }} - /> + {previousSwaps.length > 0 && } - - { - navigation.dispatch(CommonActions.navigate('Swaps')); - }} - fullWidth - /> - {buyBTCText.transactionOnRamp} @@ -164,30 +133,6 @@ const BuyBtc = () => { ); }} /> - setVisibleBuyUsdt(false)} - title={buyBTCText.selectWallet} - subTitle={buyBTCText.selectWalletDesc} - modalBackground={`${colorMode}.modalWhiteBackground`} - textColor={`${colorMode}.textGreen`} - subTitleColor={`${colorMode}.modalSubtitleBlack`} - Content={() => ( - - )} - buttonText={selectedUsdtWallet ? common.proceed : null} - buttonCallback={() => { - if (!selectedUsdtWallet) return; - setVisibleBuyUsdt(false); - navigation.dispatch( - CommonActions.navigate({ name: 'buyUstd', params: { usdtWallet: selectedUsdtWallet } }) - ); - }} - /> setVisibleSellBtc(false)} @@ -218,36 +163,6 @@ const BuyBtc = () => { } }} /> - setVisibleSellUsdt(false)} - title={buyBTCText.proceedToRamp} - modalBackground={`${colorMode}.modalWhiteBackground`} - textColor={`${colorMode}.textGreen`} - subTitleColor={`${colorMode}.modalSubtitleBlack`} - Content={() => ( - - {buyBTCText.rediredctToRampPage} - - )} - buttonText={common.confirm} - buttonCallback={async () => { - try { - setLoading(true); - setVisibleSellUsdt(false); - const url = await fetchSellUsdtLink(); - Linking.openURL(url.toString()); - } catch (error) { - showToast('Error while fetching ramp url'); - } finally { - setLoading(false); - } - }} - /> ); @@ -296,11 +211,6 @@ const styles = StyleSheet.create({ width: wp(160), borderRadius: 8, }, - button_container: { - width: windowWidth, - marginVertical: hp(10), - paddingHorizontal: wp(12), - }, info_container: { maxWidth: windowWidth, paddingHorizontal: wp(12), diff --git a/src/screens/USDT/UsdtDetails.tsx b/src/screens/USDT/UsdtDetails.tsx index 745964ce3e..1f0d035363 100644 --- a/src/screens/USDT/UsdtDetails.tsx +++ b/src/screens/USDT/UsdtDetails.tsx @@ -13,9 +13,6 @@ import { getAvailableBalanceUSDTWallet } from 'src/services/wallets/factories/US import WalletDetailHeader from '../WalletDetails/components/WalletDetailHeader'; import DetailCards from '../WalletDetails/components/DetailCards'; import ThemedColor from 'src/components/ThemedColor/ThemedColor'; -import MoreCard from '../WalletDetails/components/MoreCard'; -import KeeperModal from 'src/components/KeeperModal'; -import SwapSvg from 'src/assets/images/swap.svg'; import ActivityIndicatorView from 'src/components/AppActivityIndicator/ActivityIndicatorView'; function TransactionsAndUTXOs({ transactions, setPullRefresh, pullRefresh, wallet }) { @@ -39,13 +36,12 @@ const UsdtDetails = ({ route }) => { const { colorMode } = useColorMode(); const navigation = useNavigation(); const { translations } = useContext(LocalizationContext); - const { common } = translations; + const { common, usdtWalletText } = translations; const { getWalletCardGradient, getWalletTags } = useWalletAsset(); const [pullRefresh, setPullRefresh] = useState(false); const { usdtWalletId } = route.params || {}; const { getWalletById } = useUSDTWallets(); const usdtWallet = getWalletById(usdtWalletId); - const [showmore, setShowMore] = useState(false); const viewAll_color = ThemedColor({ name: 'viewAll_color' }); return ( @@ -64,21 +60,28 @@ const UsdtDetails = ({ route }) => { navigation.dispatch(CommonActions.navigate('sendUsdt', { usdtWallet })) } receiveCallback={() => navigation.dispatch(CommonActions.navigate('usdtReceive', { usdtWallet })) } - buyCallback={() => - navigation.dispatch(CommonActions.navigate('buyUstd', { usdtWallet })) - } wallet={usdtWallet} /> + + + {usdtWalletText.pausedTitle} + + + {usdtWalletText.pausedBody} + + {usdtWallet ? ( { )} - setShowMore(false)} - title={common.moreOptions} - subTitleColor={`${colorMode}.modalSubtitleBlack`} - textColor={`${colorMode}.textGreen`} - modalBackground={`${colorMode}.modalWhiteBackground`} - Content={() => { - return ( - - { - setShowMore(false); - }} - Icon={} - /> - - ); - }} - /> ); }; @@ -168,7 +150,7 @@ const styles = StyleSheet.create({ paddingHorizontal: 18, }, walletContainer: { - paddingTop: wp(30), + paddingTop: hp(8), paddingBottom: 20, flex: 1, justifyContent: 'space-between', @@ -227,6 +209,21 @@ const styles = StyleSheet.create({ justifyContent: 'center', marginTop: hp(20), }, + pausedNotice: { + marginHorizontal: wp(22), + marginTop: hp(65), + paddingHorizontal: wp(16), + paddingVertical: hp(12), + borderWidth: 1, + borderRadius: 10, + }, + pausedTitle: { + fontSize: wp(14), + }, + pausedBody: { + fontSize: wp(12), + marginTop: hp(6), + }, detailCardsContainer: { zIndex: 1000, }, From bec7cd9d39026e3351af48a2fd1f2ad794fdb988 Mon Sep 17 00:00:00 2001 From: Anant Date: Thu, 8 Oct 2026 09:36:43 +0530 Subject: [PATCH 045/254] Retire GasFree client and pause legacy USDT sends Keep saved USDT wallets, receive addresses, chain reads, history, and backup recovery. Make Send show a pause message and warn before new deposits. Reject retired GasFree and swap credentials in release inputs and direct Android builds. --- android/app/build.gradle | 9 +- appcenter-pre-build.sh | 15 +- package.json | 2 +- scripts/verify-channel-config.cjs | 7 +- scripts/verify-channel-config.test.cjs | 66 +++ src/context/Localization/language/en.json | 7 +- src/context/Localization/language/es.json | 7 +- src/navigation/Navigator.tsx | 6 - .../buyBtc/Swap/SwapHistoryDetail.tsx | 2 +- .../Home/components/buyBtc/Swap/Swaps.tsx | 34 +- .../buyBtc/Swap/component/CoinLogo.tsx | 36 ++ .../Swap/component/SwapTransactionCard.tsx | 2 +- src/screens/USDT/RecieveUsdt.tsx | 33 +- src/screens/USDT/UsdtDetails.tsx | 17 +- src/services/rest/SwapClient.ts | 30 - .../wallets/factories/USDTWalletFactory.ts | 61 +- .../wallets/operations/dollars/GasFree.ts | 538 +----------------- .../wallets/operations/dollars/USDT.ts | 424 ++------------ src/utils/service-utilities/config.ts | 5 - tests/recovery/usdtBackup.test.cjs | 12 +- tests/services/usdtPause.test.cjs | 136 +++++ 21 files changed, 405 insertions(+), 1044 deletions(-) create mode 100644 scripts/verify-channel-config.test.cjs create mode 100644 src/screens/Home/components/buyBtc/Swap/component/CoinLogo.tsx delete mode 100644 src/services/rest/SwapClient.ts create mode 100644 tests/services/usdtPause.test.cjs diff --git a/android/app/build.gradle b/android/app/build.gradle index b73b4caad3..498a073274 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -2,6 +2,13 @@ apply plugin: "com.android.application" apply plugin: "com.facebook.react" apply plugin: 'com.google.gms.google-services' apply from: project(':react-native-config').projectDir.getPath() + "/dotenv.gradle" +// react-native-config embeds every selected env key, even when JavaScript does not use it. +// Check the resolved env map so direct Gradle builds cannot package retired credentials. +def retiredCredentialKeys = ['GASFREE_API_KEY', 'GASFREE_API_SECRET', + 'LETS_EXCHANGE_API_KEY'].collectMany { [it, "RN_${it}"] } +if (retiredCredentialKeys.any { project.ext.env.containsKey(it) }) { + throw new GradleException('Retired API credentials must not be included in an Android build') +} apply plugin: 'kotlin-android' /** @@ -205,4 +212,4 @@ dependencies { implementation 'com.itextpdf:itextg:5.5.10' -} \ No newline at end of file +} diff --git a/appcenter-pre-build.sh b/appcenter-pre-build.sh index 6adc321a73..3705a26b9a 100644 --- a/appcenter-pre-build.sh +++ b/appcenter-pre-build.sh @@ -1,8 +1,19 @@ #!/usr/bin/env bash # Creates an .env from ENV variables for use with react-native-config +set -eu + +if [ -n "${RN_GASFREE_API_KEY+x}" ] || [ -n "${RN_GASFREE_API_SECRET+x}" ] || [ -n "${RN_LETS_EXCHANGE_API_KEY+x}" ]; then + printf "Retired API credentials must not be included in a mobile build\n" >&2 + exit 1 +fi + ENV_WHITELIST=${ENV_WHITELIST:-"^RN_"} printf "Creating an .env file with the following whitelist:\n" printf "%s\n" $ENV_WHITELIST set | egrep -e $ENV_WHITELIST | sed 's/^RN_//g' > .env -printf "\n.env created with contents:\n\n" -cat .env \ No newline at end of file +if grep -Eq '^(RN_)?(GASFREE_API_(KEY|SECRET)|LETS_EXCHANGE_API_KEY)=' .env; then + rm .env + printf "Retired API credentials must not be included in a mobile build\n" >&2 + exit 1 +fi +printf ".env created\n" diff --git a/package.json b/package.json index ad0eeb2c1e..b0344c35d7 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "iosProd": "npx react-native run-ios --scheme=hexa_keeper --simulator 'iPhone 16' ", "start": "react-native start", "test": "jest", - "test:release": "node --test scripts/release-version.test.cjs && python3 -B -m unittest discover -s scripts -p 'test_release_artifact.py'", + "test:release": "node --test scripts/release-version.test.cjs scripts/verify-channel-config.test.cjs && python3 -B -m unittest discover -s scripts -p 'test_release_artifact.py'", "version:inspect": "node scripts/release-version.cjs inspect", "version:check": "node scripts/release-version.cjs check", "version:apply": "node scripts/release-version.cjs apply", diff --git a/scripts/verify-channel-config.cjs b/scripts/verify-channel-config.cjs index 18ecc31734..401bd7be0e 100644 --- a/scripts/verify-channel-config.cjs +++ b/scripts/verify-channel-config.cjs @@ -7,7 +7,12 @@ if (!envFile || !fs.existsSync(envFile)) { throw new Error('Production environment file is missing'); } -const matches = fs.readFileSync(envFile, 'utf8') +const envContents = fs.readFileSync(envFile, 'utf8'); +if (/^\s*(?:export\s+)?(?:RN_)?(?:GASFREE_API_(?:KEY|SECRET)|LETS_EXCHANGE_API_KEY)\s*=/m.test(envContents)) { + throw new Error('Retired API credentials must not be included in a release environment'); +} + +const matches = envContents .split(/\r?\n/) .map(line => line.match(/^\s*(?:export\s+)?CHANNEL_URL\s*=\s*(.*?)\s*$/)) .filter(Boolean) diff --git a/scripts/verify-channel-config.test.cjs b/scripts/verify-channel-config.test.cjs new file mode 100644 index 0000000000..844253bc9c --- /dev/null +++ b/scripts/verify-channel-config.test.cjs @@ -0,0 +1,66 @@ +const { test } = require('node:test'); +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const os = require('node:os'); +const path = require('node:path'); +const { spawnSync } = require('node:child_process'); + +const verifier = path.join(__dirname, 'verify-channel-config.cjs'); +const prebuild = path.join(__dirname, '..', 'appcenter-pre-build.sh'); +const channel = 'CHANNEL_URL=https://channel.bitcoinkeeper.app/\n'; + +function withTempDirectory(run) { + const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'keeper-usdt-env-')); + try { + return run(directory); + } finally { + fs.rmSync(directory, { recursive: true, force: true }); + } +} + +test('release env accepts reviewed channel without retired credentials', () => + withTempDirectory((directory) => { + const envFile = path.join(directory, '.env.production'); + fs.writeFileSync(envFile, channel); + const result = spawnSync(process.execPath, [verifier, envFile], { encoding: 'utf8' }); + assert.equal(result.status, 0, result.stderr); + })); + +for (const name of ['GASFREE_API_KEY', 'GASFREE_API_SECRET', 'RN_GASFREE_API_KEY', + 'LETS_EXCHANGE_API_KEY', 'RN_LETS_EXCHANGE_API_KEY']) { + test(`release env rejects ${name} without logging its value`, () => + withTempDirectory((directory) => { + const envFile = path.join(directory, '.env.production'); + const secret = 'synthetic-secret-not-for-output'; + fs.writeFileSync(envFile, `${channel}${name}=${secret}\n`); + const result = spawnSync(process.execPath, [verifier, envFile], { encoding: 'utf8' }); + assert.notEqual(result.status, 0); + assert.doesNotMatch(result.stdout + result.stderr, /synthetic-secret-not-for-output/); + })); +} + +test('AppCenter rejects retired credential input before writing .env', () => + withTempDirectory((directory) => { + const secret = 'synthetic-secret-not-for-output'; + const result = spawnSync('bash', [prebuild], { + cwd: directory, + env: { PATH: process.env.PATH, RN_GASFREE_API_SECRET: secret }, + encoding: 'utf8', + }); + assert.notEqual(result.status, 0); + assert.equal(fs.existsSync(path.join(directory, '.env')), false); + assert.doesNotMatch(result.stdout + result.stderr, /synthetic-secret-not-for-output/); + })); + +test('AppCenter rejects retired swap credential input before writing .env', () => + withTempDirectory((directory) => { + const secret = 'synthetic-secret-not-for-output'; + const result = spawnSync('bash', [prebuild], { + cwd: directory, + env: { PATH: process.env.PATH, RN_LETS_EXCHANGE_API_KEY: secret }, + encoding: 'utf8', + }); + assert.notEqual(result.status, 0); + assert.equal(fs.existsSync(path.join(directory, '.env')), false); + assert.doesNotMatch(result.stdout + result.stderr, /synthetic-secret-not-for-output/); + })); diff --git a/src/context/Localization/language/en.json b/src/context/Localization/language/en.json index a865c1c5c6..b93cb5be23 100644 --- a/src/context/Localization/language/en.json +++ b/src/context/Localization/language/en.json @@ -2153,8 +2153,11 @@ "psbtMissingSignature": "This PSBT does not include a signature for the selected signer" }, "usdtWalletText": { - "pausedTitle": "Some USDT options are paused", - "pausedBody": "New USDT wallets, buying, selling and swapping are unavailable for now. Your existing wallet, address, balance and transaction history remain here.", + "pausedTitle": "USDT activity is limited", + "pausedBody": "New USDT wallets, sending, buying, selling and swapping are paused. This wallet, its balance and history remain available.", + "sendingPausedTitle": "USDT sending is paused", + "sendingPausedBody": "Keeper cannot send USDT from this wallet right now. Your wallet details, balance and transaction history remain available.", + "receivePausedWarning": "This is your wallet's saved receive address. Keeper cannot send USDT from this wallet while sending is paused. Funds received here cannot be sent through Keeper during the pause. Do not deposit new USDT unless you understand this limitation.", "pickWalletType": "Pick Your Wallet Type", "selectCurrency": "Select the currency you would like to use", "bitcoinWallet": "Bitcoin Wallet", diff --git a/src/context/Localization/language/es.json b/src/context/Localization/language/es.json index 6d0f7426d8..803c3e55d8 100644 --- a/src/context/Localization/language/es.json +++ b/src/context/Localization/language/es.json @@ -2134,8 +2134,11 @@ "psbtMissingSignature": "This PSBT does not include a signature for the selected signer" }, "usdtWalletText": { - "pausedTitle": "Algunas opciones de USDT están pausadas", - "pausedBody": "Por ahora no se pueden crear nuevas carteras de USDT ni comprar, vender o intercambiar USDT. Tu cartera, dirección, saldo e historial de transacciones siguen aquí.", + "pausedTitle": "La actividad de USDT está limitada", + "pausedBody": "La creación de carteras de USDT, el envío, la compra, la venta y el intercambio están pausados. Esta cartera, su saldo e historial siguen disponibles.", + "sendingPausedTitle": "El envío de USDT está pausado", + "sendingPausedBody": "Keeper no puede enviar USDT desde esta cartera ahora mismo. Los detalles, el saldo y el historial de transacciones de la cartera siguen disponibles.", + "receivePausedWarning": "Esta es la dirección de recepción guardada de tu cartera. Keeper no puede enviar USDT desde esta cartera mientras el envío esté pausado. Los fondos recibidos aquí no se pueden enviar mediante Keeper durante la pausa. No deposites nuevos USDT a menos que comprendas esta limitación.", "pickWalletType": "Pick Your Wallet Type", "selectCurrency": "Select the currency you would like to use", "bitcoinWallet": "Bitcoin Wallet", diff --git a/src/navigation/Navigator.tsx b/src/navigation/Navigator.tsx index 50fa3849a1..b89ab6a517 100644 --- a/src/navigation/Navigator.tsx +++ b/src/navigation/Navigator.tsx @@ -156,9 +156,6 @@ import { AddMultipleXpubFiles } from 'src/screens/AddSigner/AddMultipleXpubFiles import { SignMessageScreen } from 'src/screens/WalletDetails/SignMessageScreen'; import UsdtDetails from 'src/screens/USDT/UsdtDetails'; import RecieveUsdt from 'src/screens/USDT/RecieveUsdt'; -import SendUsdt from 'src/screens/USDT/SendUsdt'; -import UsdtAmount from 'src/screens/USDT/UsdtAmount'; -import UsdtSendConfirmation from 'src/screens/USDT/UsdtSendConfirmation'; import UsdtTransactionDetail from 'src/screens/USDT/UsdtTransactionDetail'; import UsdtTransactionHistory from 'src/screens/USDT/UsdtTransactionHistory'; import Usdtsetting from 'src/screens/USDT/Usdtsetting'; @@ -293,9 +290,6 @@ function AppStack() { - - - diff --git a/src/screens/Home/components/buyBtc/Swap/SwapHistoryDetail.tsx b/src/screens/Home/components/buyBtc/Swap/SwapHistoryDetail.tsx index 9d4683f5d9..d658f6a45e 100644 --- a/src/screens/Home/components/buyBtc/Swap/SwapHistoryDetail.tsx +++ b/src/screens/Home/components/buyBtc/Swap/SwapHistoryDetail.tsx @@ -8,7 +8,7 @@ import useToastMessage from 'src/hooks/useToastMessage'; import ToastErrorIcon from 'src/assets/images/toast_error.svg'; import { useDispatch } from 'react-redux'; import { getTnxDetails } from 'src/store/sagaActions/swap'; -import { CoinLogo } from './Swaps'; +import { CoinLogo } from './component/CoinLogo'; import { StyleSheet } from 'react-native'; import CircleIconWrapper from 'src/components/CircleIconWrapper'; import { hp, wp } from 'src/constants/responsive'; diff --git a/src/screens/Home/components/buyBtc/Swap/Swaps.tsx b/src/screens/Home/components/buyBtc/Swap/Swaps.tsx index 1b382f2676..9bcc6d6ffa 100644 --- a/src/screens/Home/components/buyBtc/Swap/Swaps.tsx +++ b/src/screens/Home/components/buyBtc/Swap/Swaps.tsx @@ -14,8 +14,6 @@ import { useAppSelector } from 'src/store/hooks'; import ToastErrorIcon from 'src/assets/images/toast_error.svg'; import Checked from 'src/assets/images/tick_icon.svg'; import { createSwapTnx, getSwapQuote, loadCoinDetails } from 'src/store/sagaActions/swap'; -import BtcAcquireIcon from 'src/assets/images/bitcoin-acquire-icon.svg'; -import UsdtWalletLogo from 'src/assets/images/usdt-wallet-logo.svg'; import Colors from 'src/theme/Colors'; import { useUSDTWallets } from 'src/hooks/useUSDTWallets'; import useVault from 'src/hooks/useVault'; @@ -23,41 +21,11 @@ import useWallets from 'src/hooks/useWallets'; import KeeperModal from 'src/components/KeeperModal'; import BuyBtcModalContent from '../BuyBtcModalContent'; import { SwapHistory } from './SwapHistory'; -import CircleIconWrapper from 'src/components/CircleIconWrapper'; import ThemedSvg from 'src/components/ThemedSvg.tsx/ThemedSvg'; import { LocalizationContext } from 'src/context/Localization/LocContext'; import { EntityKind } from 'src/services/wallets/enums'; +import { CoinLogo } from './component/CoinLogo'; -export const CoinLogo = ({ - code, - CircleWidth, - logoWidth, - logoHeight, -}: { - code: string; - isLarge?: boolean; - CircleWidth?: number; - logoWidth?: number; - logoHeight?: number; -}) => { - const isBtc = code === 'BTC'; - - return ( - - - ) : ( - - ) - } - backgroundColor={isBtc ? Colors.BrightOrange : Colors.DesaturatedTeal} - width={CircleWidth} - /> - - ); -}; export const SwitchButton = ({ onPress, coinData }) => { const { colorMode } = useColorMode(); diff --git a/src/screens/Home/components/buyBtc/Swap/component/CoinLogo.tsx b/src/screens/Home/components/buyBtc/Swap/component/CoinLogo.tsx new file mode 100644 index 0000000000..a5f4383630 --- /dev/null +++ b/src/screens/Home/components/buyBtc/Swap/component/CoinLogo.tsx @@ -0,0 +1,36 @@ +import { Box } from '@gluestack-ui/themed-native-base'; +import React from 'react'; +import BtcAcquireIcon from 'src/assets/images/bitcoin-acquire-icon.svg'; +import UsdtWalletLogo from 'src/assets/images/usdt-wallet-logo.svg'; +import CircleIconWrapper from 'src/components/CircleIconWrapper'; +import Colors from 'src/theme/Colors'; + +export const CoinLogo = ({ + code, + CircleWidth, + logoWidth, + logoHeight, +}: { + code: string; + CircleWidth?: number; + logoWidth?: number; + logoHeight?: number; +}) => { + const isBtc = code === 'BTC'; + + return ( + + + ) : ( + + ) + } + backgroundColor={isBtc ? Colors.BrightOrange : Colors.DesaturatedTeal} + width={CircleWidth} + /> + + ); +}; diff --git a/src/screens/Home/components/buyBtc/Swap/component/SwapTransactionCard.tsx b/src/screens/Home/components/buyBtc/Swap/component/SwapTransactionCard.tsx index 0cc07b9234..ec600bec8c 100644 --- a/src/screens/Home/components/buyBtc/Swap/component/SwapTransactionCard.tsx +++ b/src/screens/Home/components/buyBtc/Swap/component/SwapTransactionCard.tsx @@ -15,7 +15,7 @@ import SwapConfirmingIcon from '../../../../../../assets/images/swap-confirmint. import SwapProcessingIcon from '../../../../../../assets/images/swap-processing.svg'; import SwapSuccessIcon from '../../../../../../assets/images/swap-success.svg'; import SwapOverDueIcon from '../../../../../../assets/images/swap-overDue.svg'; -import { CoinLogo } from '../Swaps'; +import { CoinLogo } from './CoinLogo'; import SwapPriceArrow from 'src/assets/images/swap-price-arrow.svg'; import SwapPriceArrowWhite from 'src/assets/images/swap-price-arrow-white.svg'; diff --git a/src/screens/USDT/RecieveUsdt.tsx b/src/screens/USDT/RecieveUsdt.tsx index 68cdbd2c1f..b7c0637aa0 100644 --- a/src/screens/USDT/RecieveUsdt.tsx +++ b/src/screens/USDT/RecieveUsdt.tsx @@ -1,6 +1,6 @@ import { Box, useColorMode } from '@gluestack-ui/themed-native-base'; import React, { useContext } from 'react'; -import { StyleSheet } from 'react-native'; +import { ScrollView, StyleSheet } from 'react-native'; import KeeperQRCode from 'src/components/KeeperQRCode'; import Text from 'src/components/KeeperText'; import ScreenWrapper from 'src/components/ScreenWrapper'; @@ -19,9 +19,22 @@ const RecieveUsdt = ({ route }) => { return ( - + + + + {usdtWalletText.sendingPausedTitle} + + + {usdtWalletText.receivePausedWarning} + + + { {usdtWalletText.sendOnlyUsdt} - + ); }; @@ -54,6 +67,20 @@ const RecieveUsdt = ({ route }) => { export default RecieveUsdt; const styles = StyleSheet.create({ + scrollContent: { + flexGrow: 1, + }, + warning: { + borderWidth: 1, + borderRadius: 10, + marginHorizontal: 16, + marginTop: hp(12), + paddingHorizontal: 16, + paddingVertical: hp(12), + }, + warningBody: { + marginTop: hp(6), + }, container: { alignItems: 'center', marginTop: hp(20), diff --git a/src/screens/USDT/UsdtDetails.tsx b/src/screens/USDT/UsdtDetails.tsx index 1f0d035363..c8e751b50e 100644 --- a/src/screens/USDT/UsdtDetails.tsx +++ b/src/screens/USDT/UsdtDetails.tsx @@ -14,6 +14,7 @@ import WalletDetailHeader from '../WalletDetails/components/WalletDetailHeader'; import DetailCards from '../WalletDetails/components/DetailCards'; import ThemedColor from 'src/components/ThemedColor/ThemedColor'; import ActivityIndicatorView from 'src/components/AppActivityIndicator/ActivityIndicatorView'; +import KeeperModal from 'src/components/KeeperModal'; function TransactionsAndUTXOs({ transactions, setPullRefresh, pullRefresh, wallet }) { const [initialLoading, setInitialLoading] = useState(false); @@ -43,6 +44,7 @@ const UsdtDetails = ({ route }) => { const { getWalletById } = useUSDTWallets(); const usdtWallet = getWalletById(usdtWalletId); const viewAll_color = ThemedColor({ name: 'viewAll_color' }); + const [showSendPaused, setShowSendPaused] = useState(false); return ( @@ -60,9 +62,7 @@ const UsdtDetails = ({ route }) => { - navigation.dispatch(CommonActions.navigate('sendUsdt', { usdtWallet })) - } + sendCallback={() => setShowSendPaused(true)} receiveCallback={() => navigation.dispatch(CommonActions.navigate('usdtReceive', { usdtWallet })) } @@ -136,6 +136,17 @@ const UsdtDetails = ({ route }) => { )} + setShowSendPaused(false)} + title={usdtWalletText.sendingPausedTitle} + subTitle={usdtWalletText.sendingPausedBody} + modalBackground={`${colorMode}.modalWhiteBackground`} + textColor={`${colorMode}.textGreen`} + subTitleColor={`${colorMode}.modalSubtitleBlack`} + buttonText={common.ok} + buttonCallback={() => setShowSendPaused(false)} + /> ); }; diff --git a/src/services/rest/SwapClient.ts b/src/services/rest/SwapClient.ts deleted file mode 100644 index 405f6e4605..0000000000 --- a/src/services/rest/SwapClient.ts +++ /dev/null @@ -1,30 +0,0 @@ -import axios from 'axios'; -import config from 'src/utils/service-utilities/config'; - -export const swapApi = axios.create({ - baseURL: config.LETS_EXCHANGE_BASE_URL, -}); - -swapApi.interceptors.request.use( - (req) => { - req.headers.Authorization = `Bearer ${config.LETS_EXCHANGE_API_KEY}`; - if (req.method === 'post') { - req.data = { - ...req.data, - affiliate_id: config.LETS_EXCHANGE_AFFILIATE_ID, - }; - } - - return req; - }, - (error) => { - return Promise.reject(error); - } -); - -export const swapEndpoints = { - coins: 'v1/coins', - coinsInfo: 'v1/info/bulk', - quote: '/v1/info', - tnx: 'v1/transaction', -}; diff --git a/src/services/wallets/factories/USDTWalletFactory.ts b/src/services/wallets/factories/USDTWalletFactory.ts index 2b2744abf8..9543d2b8e8 100644 --- a/src/services/wallets/factories/USDTWalletFactory.ts +++ b/src/services/wallets/factories/USDTWalletFactory.ts @@ -187,22 +187,22 @@ export const checkUSDTWalletExists = (address: string, existingWallets: USDTWall }; /** - * Update USDT wallet specs with latest account status + * Preserve stored account information. The retired provider is no longer + * queried, and the saved GasFree address is needed for chain reads. */ export const updateUSDTWalletAccountStatus = async ( wallet: USDTWallet ): Promise => { - try { - return USDT.getAccountStatus(wallet.specs.address, wallet.networkType); - } catch (error) { - return wallet.accountStatus; - } + return wallet.accountStatus; }; /** * Syncs USDT wallet w/ latest balance */ export const syncUSDTWalletBalance = async (wallet: USDTWallet) => { + if (!wallet.accountStatus?.gasFreeAddress) { + throw new Error('Stored USDT address is unavailable'); + } const balance = await USDT.getUSDTBalance( wallet.accountStatus.gasFreeAddress, wallet.networkType @@ -215,7 +215,9 @@ export const syncUSDTWalletBalance = async (wallet: USDTWallet) => { * Evaluates USDT wallet's available balance */ export const getAvailableBalanceUSDTWallet = (wallet: USDTWallet): number => { - return wallet.specs.balance - wallet.accountStatus.frozen; // frozen amount is currently under process w/ the GasFree service provider(in-process permit transfers) + // Provider-reported frozen funds cannot be refreshed after retirement. + // The on-chain balance is the amount shown for the existing wallet. + return wallet.specs.balance; }; /** @@ -224,51 +226,20 @@ export const getAvailableBalanceUSDTWallet = (wallet: USDTWallet): number => { export const syncUSDTWalletTransactions = async (wallet: USDTWallet) => { const existingTransactions = wallet.specs.transactions || []; - // Step 1: Update existing transactions that have traceId but no txId - const updatedExistingTransactions = await Promise.all( - existingTransactions.map(async (existingTx) => { - // If transaction has traceId but no txId, try to fetch the txId - if (existingTx.traceId && !existingTx.txId) { - try { - const transferStatus = await USDT.getTransferStatus( - existingTx.traceId, - wallet.networkType - ); - // Update transaction with new information if available - if (transferStatus.transactionHash) { - const blockNumber = transferStatus.blockInfo?.blockNumber || existingTx.blockNumber; - const status = blockNumber - ? GasFreeTransferStatus.SUCCEED - : transferStatus.status || existingTx.status; - return { - ...existingTx, - txId: transferStatus.transactionHash || existingTx.txId, - status, - blockNumber: blockNumber, - // Update timestamp if we got block timestamp - timestamp: transferStatus.blockInfo?.blockTimestamp - ? transferStatus.blockInfo.blockTimestamp - : existingTx.timestamp, - }; - } - } catch (error) { - // If fetch fails, keep the original transaction - console.warn(`Failed to update transaction with traceId ${existingTx.traceId}:`, error); - } - } + if (!wallet.accountStatus?.gasFreeAddress) { + throw new Error('Stored USDT address is unavailable'); + } - // Return original transaction if no update needed or failed - return existingTx; - }) - ); + // Preserve trace-only entries without assigning a false failure status. + const updatedExistingTransactions = existingTransactions; - // Step 2: Fetch new transactions from USDT service + // Fetch new transactions directly from TRON. const { transactions: newTransactions } = await USDT.getUSDTTransactions( wallet.accountStatus.gasFreeAddress, wallet.networkType ); - // Step 3: Merge transactions using updated existing transactions + // Merge chain transactions with the stored history. const mergedTransactions = [...updatedExistingTransactions]; // Process each new transaction diff --git a/src/services/wallets/operations/dollars/GasFree.ts b/src/services/wallets/operations/dollars/GasFree.ts index ad41f62fab..3c9507a7aa 100644 --- a/src/services/wallets/operations/dollars/GasFree.ts +++ b/src/services/wallets/operations/dollars/GasFree.ts @@ -1,83 +1,4 @@ -import { AxiosResponse } from 'axios'; -import { NetworkType } from 'src/services/wallets/enums'; -import RestClient from '../../../rest/RestClient'; -import * as crypto from 'crypto'; -import config from '../../../../utils/service-utilities/config'; -import { createTronWeb } from './Tron'; -import idx from 'idx'; - -// GasFree API endpoints -const GASFREE_ENDPOINTS = { - [NetworkType.MAINNET]: 'https://open.gasfree.io/tron/', - [NetworkType.TESTNET]: 'https://open-test.gasfree.io/nile/', -}; - -// Network parameters for signature -const NETWORK_PARAMS = { - [NetworkType.MAINNET]: { - chainId: 728126428, - verifyingContract: 'TFFAMQLZybALaLb4uxHA9RBE7pxhUAjF3U', - }, - [NetworkType.TESTNET]: { - chainId: 3448148188, - verifyingContract: 'THQGuFzL87ZqhxkgqYEryRAd7gqFqL5rdc', - }, -}; - -// GasFree Interfaces -export interface GasFreeToken { - tokenAddress: string; - createdAt: string; - updatedAt: string; - activateFee: number; - transferFee: number; - supported: boolean; - symbol: string; - decimal: number; -} - -export interface GasFreeProvider { - address: string; - name: string; - icon: string; - website: string; - config: { - maxPendingTransfer: number; - minDeadlineDuration: number; - maxDeadlineDuration: number; - defaultDeadlineDuration: number; - }; -} - -export interface GasFreeAccountInfo { - accountAddress: string; - gasFreeAddress: string; - active: boolean; - nonce: number; - allowSubmit: boolean; - assets: Array<{ - tokenAddress: string; - tokenSymbol: string; - activateFee: number; - transferFee: number; - decimal: number; - frozen: number; - }>; -} - -export interface GasFreeTransferRequest { - token: string; - serviceProvider: string; - user: string; - receiver: string; - value: string; - maxFee: string; - deadline: string; - version: number; - nonce: number; - sig: string; -} - +/** Historical transfer states remain in stored USDT transactions. */ export enum GasFreeTransferStatus { WAITING = 'WAITING', INPROGRESS = 'INPROGRESS', @@ -85,460 +6,3 @@ export enum GasFreeTransferStatus { SUCCEED = 'SUCCEED', FAILED = 'FAILED', } - -export interface GasFreeTransferResponse { - id: string; - createdAt: string; - updatedAt: string; - accountAddress: string; - gasFreeAddress: string; - providerAddress: string; - targetAddress: string; - tokenAddress: string; - amount: string; - maxFee: string; - signature: string; - version: number; - nonce: number; - expiredAt: string; - state: GasFreeTransferStatus; - estimatedActivateFee: number; - estimatedTransferFee: number; -} - -export interface GasFreeTransferDetails extends GasFreeTransferResponse { - estimatedTotalFee: number; - estimatedTotalCost: number; - txnHash?: string; - txnBlockNum?: number; - txnBlockTimestamp?: number; - txnState?: 'INIT' | 'NOT_ON_CHAIN' | 'ON_CHAIN' | 'SOLIDITY' | 'ON_CHAIN_FAILED'; - txnActivateFee?: number; - txnTransferFee?: number; - txnTotalFee?: number; - txnAmount?: string; - txnTotalCost?: number; -} - -export interface GasFreeSignaturePayload { - token: string; - serviceProvider: string; - user: string; - receiver: string; - value: string; - maxFee: string; - deadline: string; - version: number; - nonce: number; -} - -export interface GasFreeAPIResponse { - code: number; - reason: string | null; - message: string | null; - data: T; -} - -export interface GasFreeAPICredentials { - apiKey: string; - apiSecret: string; -} - -// TIP-712 Signing Types for GasFree -export interface TIP712Domain { - name: string; - version: string; - chainId: number; - verifyingContract: string; -} - -export interface TIP712TypeProperty { - name: string; - type: string; -} - -export interface TIP712MessageTypes { - PermitTransfer: TIP712TypeProperty[]; -} - -export interface Permit712MessageDomain extends TIP712Domain { - name: 'GasFreeController'; - version: 'V1.0.0'; - chainId: number; // Network specific chain ID in decimal - verifyingContract: string; // GasFreeController contract address -} - -export interface Permit712MessageTypes extends TIP712MessageTypes { - PermitTransfer: [ - { name: 'token'; type: 'address' }, - { name: 'serviceProvider'; type: 'address' }, - { name: 'user'; type: 'address' }, - { name: 'receiver'; type: 'address' }, - { name: 'value'; type: 'uint256' }, - { name: 'maxFee'; type: 'uint256' }, - { name: 'deadline'; type: 'uint256' }, - { name: 'version'; type: 'uint256' }, - { name: 'nonce'; type: 'uint256' } - ]; -} - -export default class GasFree { - private static credentials: GasFreeAPICredentials = { - // Mainnet ONLY – Testnet access is not available to end users - apiKey: config.GASFREE_API_KEY, - apiSecret: config.GASFREE_API_SECRET, - }; - - // TIP-712 Constants - private static readonly TIP712_MESSAGE_TYPES: Permit712MessageTypes = { - PermitTransfer: [ - { name: 'token', type: 'address' }, - { name: 'serviceProvider', type: 'address' }, - { name: 'user', type: 'address' }, - { name: 'receiver', type: 'address' }, - { name: 'value', type: 'uint256' }, - { name: 'maxFee', type: 'uint256' }, - { name: 'deadline', type: 'uint256' }, - { name: 'version', type: 'uint256' }, - { name: 'nonce', type: 'uint256' }, - ], - }; - - // Fixed values as per GasFree documentation - private static readonly TIP712_DOMAIN_NAME = 'GasFreeController'; - private static readonly TIP712_DOMAIN_VERSION = 'V1.0.0'; - private static readonly SIGNATURE_VERSION = 1; - - /** - * Get the appropriate endpoint URL based on network type - */ - private static getEndpointURL(networkType?: NetworkType): string { - const network = networkType || NetworkType.MAINNET; - return GASFREE_ENDPOINTS[network]; - } - - /** - * Get network parameters for signing - */ - private static getNetworkParams(networkType?: NetworkType) { - const network = networkType || NetworkType.MAINNET; - return NETWORK_PARAMS[network]; - } - - /** - * Extract network path from baseURL - * e.g., 'https://open-test.gasfree.io/nile/' -> '/nile/' - * e.g., 'https://open.gasfree.io/tron/' -> '/tron/' - */ - private static extractNetworkPath(baseURL: string): string { - try { - // Remove protocol and domain: 'https://open-test.gasfree.io/nile/' -> '/nile/' - const pathStartIndex = baseURL.indexOf('/', baseURL.indexOf('://') + 3); - if (pathStartIndex === -1) return ''; - - let pathname = baseURL.substring(pathStartIndex); - return pathname; - } catch (error) { - throw new Error('Error extracting network path: ' + error?.message); - } - } - - /** - * Generate authentication headers for API requests - */ - private static generateAuthHeaders(method: string, path: string): Record { - if (!GasFree.credentials) { - throw new Error('GasFree API credentials not set. Call setCredentials() first.'); - } - - const timestamp = Math.floor(Date.now() / 1000); - const message = `${method}${path}${timestamp}`; - const signature = crypto - .createHmac('sha256', GasFree.credentials.apiSecret) - .update(message) - .digest('base64'); - - return { - Timestamp: timestamp.toString(), - Authorization: `ApiKey ${GasFree.credentials.apiKey}:${signature}`, - }; - } - - /** - * Make an authenticated API request - */ - private static async makeAuthenticatedRequest( - method: 'GET' | 'POST', - path: string, - body?: any, - networkType?: NetworkType - ): Promise> { - try { - const baseURL = GasFree.getEndpointURL(networkType); - const url = `${baseURL}${path}`; - const networkPrefix = GasFree.extractNetworkPath(baseURL); - const authHeaders = GasFree.generateAuthHeaders(method, networkPrefix + path); - - let response: AxiosResponse; - - if (method === 'GET') { - response = (await RestClient.get(url, { - ...authHeaders, - })) as AxiosResponse; - } else { - response = (await RestClient.post(url, body, { - ...authHeaders, - })) as AxiosResponse; - } - - return response.data; - } catch (err) { - const errorMessage = idx(err, (_) => _.response.data); - throw new Error(`GasFree API request failed: ${errorMessage || err.message || err}`); - } - } - - /** - * Get all supported tokens - */ - public static async getSupportedTokens(networkType?: NetworkType): Promise { - try { - const response = await GasFree.makeAuthenticatedRequest<{ tokens: GasFreeToken[] }>( - 'GET', - 'api/v1/config/token/all', - undefined, - networkType - ); - - if (response.code !== 200) { - throw new Error(response.message || 'Failed to fetch supported tokens'); - } - - return response.data.tokens; - } catch (err) { - throw new Error(`Failed to fetch supported tokens: ${err.message || err}`); - } - } - - /** - * Get all available service providers - */ - public static async getServiceProviders(networkType?: NetworkType): Promise { - try { - const response = await GasFree.makeAuthenticatedRequest<{ providers: GasFreeProvider[] }>( - 'GET', - 'api/v1/config/provider/all', - undefined, - networkType - ); - - if (response.code !== 200) { - throw new Error(response.message || 'Failed to fetch service providers'); - } - - return response.data.providers; - } catch (err) { - throw new Error(`Failed to fetch service providers: ${err.message || err}`); - } - } - - /** - * Get GasFree account information for a given address - */ - public static async getAccountInfo( - accountAddress: string, - networkType?: NetworkType - ): Promise { - try { - const response = await GasFree.makeAuthenticatedRequest( - 'GET', - `api/v1/address/${accountAddress}`, - undefined, - networkType - ); - - if (response.code !== 200) { - throw new Error(response.message || 'Failed to fetch account information'); - } - - return response.data; - } catch (err) { - throw new Error(`Failed to fetch account info: ${err.message || err}`); - } - } - - /** - * Generate signature payload for TIP-712 compatible signing - */ - public static generateSignaturePayload( - transferRequest: Omit, - networkType?: NetworkType - ): { - domain: Permit712MessageDomain; - types: Permit712MessageTypes; - message: GasFreeSignaturePayload; - } { - const params = GasFree.getNetworkParams(networkType); - - const domain: Permit712MessageDomain = { - name: GasFree.TIP712_DOMAIN_NAME, - version: GasFree.TIP712_DOMAIN_VERSION, - chainId: params.chainId, - verifyingContract: params.verifyingContract, - }; - - const types: Permit712MessageTypes = GasFree.TIP712_MESSAGE_TYPES; - - const message: GasFreeSignaturePayload = { - ...transferRequest, - version: GasFree.SIGNATURE_VERSION, - }; - - return { domain, types, message }; - } - - /** - * Sign GasFree transfer payload using TIP-712 standard - * @param privateKey - Private key in hex format for signing - * @param signaturePayload - Signature payload containing domain, types, and message - * @param networkType - Network type (mainnet/testnet) - * @returns Promise - Signature without 0x prefix - */ - public static async signTransferPayload( - privateKey: string, - signaturePayload: { - domain: Permit712MessageDomain; - types: Permit712MessageTypes; - message: GasFreeSignaturePayload; - }, - networkType: NetworkType - ): Promise { - try { - const tronWeb = createTronWeb(networkType); - tronWeb.setPrivateKey(privateKey); - - // Sign using TronWeb's TIP-712 implementation - const { domain, types, message } = signaturePayload; - const signature = await tronWeb.trx._signTypedData(domain, types, message); - - // Remove 0x prefix if present (as per GasFree documentation) - const cleanSignature = signature.startsWith('0x') ? signature.slice(2) : signature; - return cleanSignature; - } catch (error) { - throw new Error(`Failed to sign transfer payload: ${error.message}`); - } - } - - /** - * Submit a GasFree transfer authorization - */ - public static async submitTransfer( - transferRequest: GasFreeTransferRequest, - networkType?: NetworkType - ): Promise { - try { - const response = await GasFree.makeAuthenticatedRequest( - 'POST', - 'api/v1/gasfree/submit', - transferRequest, - networkType - ); - - if (response.code !== 200) { - throw new Error(response.message || 'Failed to submit transfer'); - } - - return response.data; - } catch (err) { - throw new Error(`Failed to submit transfer: ${err.message || err}`); - } - } - - /** - * Get transfer status by trace ID - */ - public static async getTransferStatus( - traceId: string, - networkType?: NetworkType - ): Promise { - try { - const response = await GasFree.makeAuthenticatedRequest( - 'GET', - `api/v1/gasfree/${traceId}`, - undefined, - networkType - ); - - if (response.code !== 200) { - throw new Error(response.message || 'Failed to fetch transfer status'); - } - - return response.data; - } catch (err) { - throw new Error(`Failed to fetch transfer status: ${err.message || err}`); - } - } - - /** - * Calculate deadline timestamp - */ - public static calculateDeadline(durationInSeconds: number = 180): string { - const deadline = Math.floor(Date.now() / 1000) + durationInSeconds; - return deadline.toString(); - } - - /** - * Format token amount to smallest unit (e.g., USDT to 6 decimal places) - */ - public static formatTokenAmount(amount: number, decimals: number): string { - return Math.ceil(amount * Math.pow(10, decimals)).toString(); - } - - /** - * Parse token amount from smallest unit to readable format - */ - public static parseTokenAmount(amount: string, decimals: number): number { - return parseInt(amount) / Math.pow(10, decimals); - } - - /** - * Monitor transfer until completion or failure - */ - public static async monitorTransfer( - traceId: string, - onStatusUpdate?: (status: GasFreeTransferDetails) => void, - networkType?: NetworkType, - maxAttempts: number = 60, - intervalMs: number = 5000 - ): Promise { - let attempts = 0; - - const poll = async (): Promise => { - try { - const status = await GasFree.getTransferStatus(traceId, networkType); - - if (onStatusUpdate) { - onStatusUpdate(status); - } - - // Check if final state - if (status.state === 'SUCCEED' || status.state === 'FAILED') { - return status; - } - - attempts++; - if (attempts >= maxAttempts) { - throw new Error('Transfer monitoring timeout'); - } - - // Wait and poll again - await new Promise((resolve) => setTimeout(resolve, intervalMs)); - return poll(); - } catch (err) { - throw err; - } - }; - - return poll(); - } -} diff --git a/src/services/wallets/operations/dollars/USDT.ts b/src/services/wallets/operations/dollars/USDT.ts index 85b5d4edd0..6a6085c8ba 100644 --- a/src/services/wallets/operations/dollars/USDT.ts +++ b/src/services/wallets/operations/dollars/USDT.ts @@ -1,28 +1,21 @@ -import GasFree, { - GasFreeProvider, - GasFreeTransferRequest, - GasFreeSignaturePayload, - GasFreeTransferStatus, - Permit712MessageTypes, - Permit712MessageDomain, -} from './GasFree'; +import { GasFreeTransferStatus } from './GasFree'; import { NetworkType } from 'src/services/wallets/enums'; import { isValidTronAddress, getTrc20Balance, getTrc20Transactions } from './Tron'; import { USDTWallet } from '../../factories/USDTWalletFactory'; -// USDT Token addresses +// Keep these addresses for existing wallets' balance and history reads. const USDT_ADDRESSES = { - [NetworkType.MAINNET]: 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t', // USDT TRC-20 on mainnet - [NetworkType.TESTNET]: 'TXYZopYRdj2D9XRtbG411XZZ3kM5VkAeBf', // USDT TRC-20 on Nile testnet + [NetworkType.MAINNET]: 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t', + [NetworkType.TESTNET]: 'TXYZopYRdj2D9XRtbG411XZZ3kM5VkAeBf', }; -// USDT GasFree Fee addresses export const USDT_GASFREE_FEE_ADDRESSES = { - [NetworkType.MAINNET]: 'TLntW9Z59LYY5KEi9cmwk3PKjQga828ird', // GasFree fee address for USDT on mainnet - [NetworkType.TESTNET]: 'TCETRh3aED4kdkaYQY7CcxeTJtrQvwBpNT', // GasFree fee address for USDT on testnet + [NetworkType.MAINNET]: 'TLntW9Z59LYY5KEi9cmwk3PKjQga828ird', + [NetworkType.TESTNET]: 'TCETRh3aED4kdkaYQY7CcxeTJtrQvwBpNT', }; -export const DEFAULT_DEADLINE_SECONDS = 300; // Default deadline for permit transactions (5 minutes) +export const DEFAULT_DEADLINE_SECONDS = 300; +export const USDT_ACTIVITY_PAUSED = 'USDT activity is paused'; export interface USDTTransferOptions { source: USDTWallet; @@ -42,27 +35,25 @@ export interface USDTTransferResult { transaction?: USDTTransaction; } +// Stored account fields are retained for Realm and encrypted backup compatibility. export interface USDTAccountStatus { address: string; gasFreeAddress: string; isActive: boolean; - frozen: number; // Frozen amount in pending transfers + frozen: number; canTransfer: boolean; nextNonce: number; - fees: { - transferFee: number; - activateFee: number; - }; + fees: { transferFee: number; activateFee: number }; } export interface USDTTransaction { - txId?: string; // available post processing - traceId?: string; // available via GasFree API + txId?: string; + traceId?: string; from: string; to: string; amount: string; - transferFee?: number; // Estimated transfer fee - activateFee?: number; // Estimated activation fee + transferFee?: number; + activateFee?: number; fee?: string; status: GasFreeTransferStatus; timestamp: number; @@ -71,392 +62,101 @@ export interface USDTTransaction { } export default class USDT { - /** - * Get USDT token address for the specified network - */ private static getUSDTAddress(networkType?: NetworkType): string { - const network = networkType || NetworkType.MAINNET; - return USDT_ADDRESSES[network]; + return USDT_ADDRESSES[networkType || NetworkType.MAINNET]; } - /** - * Get USDT GasFree fee address for the specified network - */ public static getUSDTGasFreeFeeAddress(networkType?: NetworkType): string { - const network = networkType || NetworkType.MAINNET; - return USDT_GASFREE_FEE_ADDRESSES[network]; + return USDT_GASFREE_FEE_ADDRESSES[networkType || NetworkType.MAINNET]; } - /** - * Check if GasFree supports USDT transfers on the specified network - */ - public static async isGasFreeSupported(networkType?: NetworkType): Promise { - try { - const tokens = await GasFree.getSupportedTokens(networkType); - const usdtAddress = USDT.getUSDTAddress(networkType); - - return tokens.some( - (token) => token.tokenAddress.toLowerCase() === usdtAddress.toLowerCase() && token.supported - ); - } catch (err) { - throw new Error('Failed to check GasFree support for USDT on the specified network'); - } + // Retired provider operations fail closed, including calls from stale routes. + public static async isGasFreeSupported(_networkType?: NetworkType): Promise { + return false; } - /** - * Get available service providers for USDT transfers - */ - public static async getServiceProviders(networkType?: NetworkType): Promise { - try { - return await GasFree.getServiceProviders(networkType); - } catch (err) { - throw new Error('Failed to fetch service providers'); - } + public static async getServiceProviders(_networkType?: NetworkType): Promise { + return []; } - /** - * Get USDT account status and balance information - */ public static async getAccountStatus( - address: string, - networkType?: NetworkType + _address: string, + _networkType?: NetworkType ): Promise { - try { - const accountInfo = await GasFree.getAccountInfo(address, networkType); - const usdtAddress = USDT.getUSDTAddress(networkType); - - // Find USDT asset in account - const usdtAsset = accountInfo.assets.find( - (asset) => asset.tokenAddress.toLowerCase() === usdtAddress.toLowerCase() - ); - - if (!usdtAsset) throw new Error('USDT asset not found in account'); - const frozen = GasFree.parseTokenAmount(usdtAsset.frozen.toString(), usdtAsset.decimal); - - return { - address: accountInfo.accountAddress, - gasFreeAddress: accountInfo.gasFreeAddress, - isActive: accountInfo.active, - frozen, - canTransfer: accountInfo.allowSubmit, - nextNonce: accountInfo.nonce, - fees: { - transferFee: GasFree.parseTokenAmount( - usdtAsset.transferFee.toString(), - usdtAsset.decimal - ), - activateFee: GasFree.parseTokenAmount( - usdtAsset.activateFee.toString(), - usdtAsset.decimal - ), - }, - }; - } catch (err) { - throw err; - } + throw new Error(USDT_ACTIVITY_PAUSED); } - /** - * Prepare USDT transfer parameters and validate - */ - public static async prepareTransfer(options: USDTTransferOptions): Promise<{ + public static async prepareTransfer(_options: USDTTransferOptions): Promise<{ isValid: boolean; error?: string; - signaturePayload?: { - domain: Permit712MessageDomain; - types: Permit712MessageTypes; - message: GasFreeSignaturePayload; - }; - fees?: { - transferFee: number; - activateFee: number; - totalFee: number; - }; + signaturePayload?: unknown; + fees?: { transferFee: number; activateFee: number; totalFee: number }; }> { - try { - const usdtAddress = USDT.getUSDTAddress(options.networkType); - - // Get service providers if not specified - let serviceProviderAddress = options.serviceProviderAddress; - if (!serviceProviderAddress) { - const providers = await GasFree.getServiceProviders(options.networkType); - if (providers.length === 0) { - return { isValid: false, error: 'No service providers available' }; - } - serviceProviderAddress = providers[0].address; - } - - // Calculate fees - const { source } = options; - const fees = USDT.evaluateTransferFee(source.accountStatus); - const maxFee = GasFree.formatTokenAmount(options.maxFeeInUSDT || fees.totalFee, 6); - - // Format amount to smallest unit - const formattedAmount = GasFree.formatTokenAmount(options.amount, 6); - - // Check if account allows submission - if (!source.accountStatus.canTransfer) { - return { - isValid: false, - error: 'Account is not allowed to submit transfers at this time', - }; - } - - // Generate signature payload - const deadline = GasFree.calculateDeadline( - options.deadlineInSeconds || DEFAULT_DEADLINE_SECONDS - ); // Default to 5 minutes - - const transferData = { - token: usdtAddress, - serviceProvider: serviceProviderAddress, - user: options.source.specs.address, - receiver: options.toAddress, - value: formattedAmount, - maxFee, - deadline, - nonce: source.accountStatus.nextNonce, - }; - - const signaturePayload = GasFree.generateSignaturePayload(transferData, options.networkType); - return { - isValid: true, - signaturePayload, - fees: { - transferFee: GasFree.parseTokenAmount(fees.transferFee.toString(), 6), - activateFee: GasFree.parseTokenAmount(fees.activateFee.toString(), 6), - totalFee: GasFree.parseTokenAmount(fees.totalFee.toString(), 6), - }, - }; - } catch (err) { - return { isValid: false, error: err.message || 'Failed to prepare transfer' }; - } + return { isValid: false, error: USDT_ACTIVITY_PAUSED }; } - /** - * Submit USDT transfer with signature - */ public static async submitTransfer( - source: USDTWallet, - signaturePayload: { - domain: Permit712MessageDomain; - types: Permit712MessageTypes; - message: GasFreeSignaturePayload; - } + _source: USDTWallet, + _signaturePayload: unknown ): Promise { - try { - // Create and Sign transfer request - const transferRequest: GasFreeTransferRequest = { - ...signaturePayload.message, - sig: await GasFree.signTransferPayload( - source.specs.privateKey, - signaturePayload, - source.networkType - ), - }; - - // Submit transfer - const response = await GasFree.submitTransfer(transferRequest, source.networkType); - const transferFee = GasFree.parseTokenAmount(response.estimatedTransferFee.toString(), 6); - const activateFee = GasFree.parseTokenAmount(response.estimatedActivateFee.toString(), 6); - const fee = transferFee + activateFee; - const amount = GasFree.parseTokenAmount(response.amount.toString(), 6); - const transaction: USDTTransaction = { - traceId: response.id, - from: transferRequest.user, - to: transferRequest.receiver, - amount: amount.toString(), - transferFee: transferFee, - activateFee: activateFee, - fee: fee.toString(), - status: response.state, - timestamp: Date.now(), - isGasFree: true, - }; - - return { - success: true, - traceId: response.id, - transaction, - }; - } catch (err) { - return { - success: false, - error: err || 'Failed to submit transfer', - }; - } + return { success: false, error: USDT_ACTIVITY_PAUSED }; } - /** - * Track USDT transfer status - */ public static async getTransferStatus( - traceId: string, - networkType?: NetworkType - ): Promise<{ - status: GasFreeTransferStatus; - transactionHash?: string; - amount?: number; - fees?: { - actualTransferFee?: number; - actualActivateFee?: number; - actualTotalFee?: number; - }; - blockInfo?: { - blockNumber?: number; - blockTimestamp?: number; - }; - error?: string; - }> { - try { - const details = await GasFree.getTransferStatus(traceId, networkType); - - return { - status: details.state, - transactionHash: details.txnHash, - amount: details.txnAmount ? GasFree.parseTokenAmount(details.txnAmount, 6) : undefined, - fees: { - actualTransferFee: details.txnTransferFee - ? GasFree.parseTokenAmount(details.txnTransferFee.toString(), 6) - : undefined, - actualActivateFee: details.txnActivateFee - ? GasFree.parseTokenAmount(details.txnActivateFee.toString(), 6) - : undefined, - actualTotalFee: details.txnTotalFee - ? GasFree.parseTokenAmount(details.txnTotalFee.toString(), 6) - : undefined, - }, - blockInfo: { - blockNumber: details.txnBlockNum, - blockTimestamp: details.txnBlockTimestamp, - }, - }; - } catch (err) { - return { - status: GasFreeTransferStatus.FAILED, - error: err.message || 'Failed to fetch transfer status', - }; - } + _traceId: string, + _networkType?: NetworkType + ): Promise { + throw new Error(USDT_ACTIVITY_PAUSED); } - /** - * Monitor USDT transfer until completion - */ public static async monitorTransfer( traceId: string, - onStatusUpdate?: (status: any) => void, - networkType?: NetworkType + _onStatusUpdate?: (status: any) => void, + _networkType?: NetworkType ): Promise { - try { - const pollIn = 15; // Poll every 15 seconds - const maxAttempts = DEFAULT_DEADLINE_SECONDS / 15; // default deadline: 5 mins -- poll every 15 seconds, max attempts is 20 - - const finalStatus = await GasFree.monitorTransfer( - traceId, - (details) => { - if (onStatusUpdate) { - onStatusUpdate({ - status: details.state, - transactionHash: details.txnHash, - amount: details.txnAmount - ? GasFree.parseTokenAmount(details.txnAmount, 6) - : undefined, - }); - } - }, - networkType, - maxAttempts, - pollIn * 1000 // Poll every 15 seconds, in milliseconds - ); - - if (finalStatus.state === GasFreeTransferStatus.SUCCEED) { - return { - success: true, - traceId, - transactionHash: finalStatus.txnHash, - }; - } else { - return { - success: false, - traceId, - error: 'Transfer failed or was rejected', - }; - } - } catch (err) { - return { - success: false, - traceId, - error: err.message || 'Transfer monitoring failed', - }; - } + return { success: false, traceId, error: USDT_ACTIVITY_PAUSED }; } - /** - * Evaluates transfer fees - */ + // Retain old fee information for historical transaction screens. public static evaluateTransferFee(accountStatus: USDTAccountStatus): { transferFee: number; activateFee: number; totalFee: number; } { - try { - const { activateFee, transferFee } = accountStatus.fees; - const activationFee = accountStatus.isActive ? 0 : activateFee; - const totalFee = activationFee + transferFee; - return { - transferFee, - activateFee, - totalFee: totalFee, - }; - } catch (err) { - throw new Error(`Failed to evaluate transfer fees: ${err.message || err}`); - } + const { activateFee, transferFee } = accountStatus.fees; + return { + transferFee, + activateFee, + totalFee: transferFee + (accountStatus.isActive ? 0 : activateFee), + }; } - /** - * Validate address format using TronWeb - */ public static isValidAddress(address: string, networkType: NetworkType): boolean { return isValidTronAddress(address, networkType); } - /** - * Format USDT amount for display - */ public static formatUSDTAmount(amount: number, decimals: number = 2): string { return amount.toFixed(decimals); } - /** - * Parse USDT amount from string input - */ public static parseUSDTAmount(amountString: string): number { const parsed = parseFloat(amountString); - if (isNaN(parsed) || parsed <= 0) { - throw new Error('Invalid USDT amount'); - } + if (isNaN(parsed) || parsed <= 0) throw new Error('Invalid USDT amount'); return parsed; } - /** - * Get USDT balance directly from TRON blockchain - */ public static async getUSDTBalance( address: string, networkType: NetworkType = NetworkType.MAINNET ): Promise { try { - const usdtAddress = USDT.getUSDTAddress(networkType); - const balanceResult = await getTrc20Balance(address, usdtAddress, networkType); - return balanceResult.balance; + const result = await getTrc20Balance(address, USDT.getUSDTAddress(networkType), networkType); + return result.balance; } catch (error) { throw new Error(`Failed to fetch USDT balance: ${error.message || error}`); } } - /** - * Get USDT transactions for a USDT wallet - */ public static async getUSDTTransactions( address: string, networkType: NetworkType, @@ -464,21 +164,11 @@ export default class USDT { fingerprint?: string ): Promise<{ transactions: USDTTransaction[]; - meta: { - fingerprint: string; - hasMore: boolean; - }; + meta: { fingerprint: string; hasMore: boolean }; }> { - const usdtContractAddress = USDT.getUSDTAddress(networkType); - const trc20Transactions = await getTrc20Transactions( - address, - usdtContractAddress, - networkType, - limit, - fingerprint - ); - - const updatedTransactions = trc20Transactions.transactions.map((txn) => ({ + const contract = USDT.getUSDTAddress(networkType); + const result = await getTrc20Transactions(address, contract, networkType, limit, fingerprint); + const transactions = result.transactions.map((txn) => ({ txId: txn.transactionId, from: txn.from, to: txn.to, @@ -486,12 +176,8 @@ export default class USDT { status: txn.blockNumber ? GasFreeTransferStatus.SUCCEED : GasFreeTransferStatus.CONFIRMING, timestamp: txn.blockTimestamp, blockNumber: txn.blockNumber, - isGasFree: txn.to === address ? false : true, // incoming transactions are not GasFree in the similar sense as outgoing + isGasFree: txn.to !== address, })); - - return { - transactions: updatedTransactions, - meta: trc20Transactions.meta, - }; + return { transactions, meta: result.meta }; } } diff --git a/src/utils/service-utilities/config.ts b/src/utils/service-utilities/config.ts index 999bbc3ae7..f6191f3b3a 100644 --- a/src/utils/service-utilities/config.ts +++ b/src/utils/service-utilities/config.ts @@ -84,15 +84,10 @@ class Configuration { ? config.CHANNEL_URL.trim() : DEFAULT_CONFIG.CHANNEL_URL.trim(); - public GASFREE_API_KEY: string = config.GASFREE_API_KEY?.trim(); - public GASFREE_API_SECRET: string = config.GASFREE_API_SECRET?.trim(); - public RENEWAL_WINDOW: number; public LETS_EXCHANGE_BASE_URL: string = config.LETS_EXCHANGE_BASE_URL?.trim(); - public LETS_EXCHANGE_API_KEY: string = config.LETS_EXCHANGE_API_KEY?.trim(); - public LETS_EXCHANGE_AFFILIATE_ID: string = DEFAULT_CONFIG.LETS_EXCHANGE_AFFILIATE_ID; public ADDRESS: Address; diff --git a/tests/recovery/usdtBackup.test.cjs b/tests/recovery/usdtBackup.test.cjs index aedac01595..f328e81cfb 100644 --- a/tests/recovery/usdtBackup.test.cjs +++ b/tests/recovery/usdtBackup.test.cjs @@ -87,7 +87,15 @@ const usdtWallet = (type = 'DEFAULT') => ({ presentationData: { name: 'Before edit', description: 'Before', visibility: 'DEFAULT' }, specs: { address: 'disposable-tron-address', privateKey: 'fixture-only-private-key', balance: 10, transactions: [], hasNewUpdates: false, lastSynched: 1 }, - accountStatus: { isActive: true, nextNonce: 0 }, + accountStatus: { + address: 'disposable-tron-address', + gasFreeAddress: 'saved-fixture-receive-address', + isActive: true, + frozen: 0, + canTransfer: true, + nextNonce: 0, + fees: { transferFee: 1, activateFee: 2 }, + }, createdAt: 1, }); @@ -149,7 +157,7 @@ test('USDT balance, transaction and account sync avoids redundant backup uploads const synced = { ...original, specs: { ...original.specs, balance: 12, transactions: [{ txId: 'fixture-tx' }], hasNewUpdates: true, lastSynched: 2 }, - accountStatus: { isActive: true, nextNonce: 1 }, + accountStatus: { ...original.accountStatus, nextNonce: 1 }, }; assert.equal(await f.update(synced), true); assert.deepEqual(copy(f.collections.USDTWallet), [synced]); diff --git a/tests/services/usdtPause.test.cjs b/tests/services/usdtPause.test.cjs new file mode 100644 index 0000000000..a5ddd3b9eb --- /dev/null +++ b/tests/services/usdtPause.test.cjs @@ -0,0 +1,136 @@ +const { test } = require('node:test'); +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const path = require('node:path'); +const vm = require('node:vm'); +const ts = require('typescript'); + +const root = path.join(__dirname, '../..'); +function loadTypeScript(relativePath, imports = {}) { + const source = fs.readFileSync(path.join(root, relativePath), 'utf8'); + const code = ts.transpileModule(source, { + compilerOptions: { + module: ts.ModuleKind.CommonJS, + target: ts.ScriptTarget.ES2020, + esModuleInterop: true, + }, + }).outputText; + const module = { exports: {} }; + const loadImport = (specifier) => { + if (!(specifier in imports)) throw new Error(`Unexpected import: ${specifier}`); + return imports[specifier]; + }; + const run = vm.runInNewContext( + `(function (require, module, exports) { ${code}\n})`, + { Date, Error, Promise } + ); + run(loadImport, module, module.exports); + return module.exports; +} + +const network = { MAINNET: 'MAINNET', TESTNET: 'TESTNET' }; +const historical = loadTypeScript('src/services/wallets/operations/dollars/GasFree.ts'); +const balanceCalls = []; +const historyCalls = []; +const tron = { + isValidTronAddress: () => true, + getTrc20Balance: async (...args) => { + balanceCalls.push(args); + return { balance: 42, decimals: 6 }; + }, + getTrc20Transactions: async (...args) => { + historyCalls.push(args); + return { + transactions: [{ + transactionId: 'chain-1', + from: 'TSender', + to: 'TStoredGasFreeAddress', + formattedValue: 5, + blockNumber: 1, + blockTimestamp: 100, + }], + meta: { fingerprint: '', hasMore: false }, + }; + }, +}; +const service = loadTypeScript('src/services/wallets/operations/dollars/USDT.ts', { + './GasFree': historical, + 'src/services/wallets/enums': { NetworkType: network }, + './Tron': tron, +}).default; + +const wallet = { + networkType: network.MAINNET, + accountStatus: { + address: 'TOwner', + gasFreeAddress: 'TStoredGasFreeAddress', + frozen: 3, + isActive: true, + canTransfer: true, + nextNonce: 2, + fees: { transferFee: 1, activateFee: 0 }, + }, + specs: { + address: 'TOwner', + privateKey: 'fixture-only', + balance: 10, + transactions: [{ + traceId: 'historical-trace', + from: 'TStoredGasFreeAddress', + to: 'TRecipient', + amount: '2', + status: historical.GasFreeTransferStatus.WAITING, + timestamp: 50, + isGasFree: true, + }], + hasNewUpdates: false, + lastSynched: 1, + }, +}; + +const factory = loadTypeScript('src/services/wallets/factories/USDTWalletFactory.ts', { + 'crypto-js': { SHA256: () => ({ toString: () => 'fixture-id' }) }, + '../enums': { + NetworkType: network, + EntityKind: { USDT_WALLET: 'USDT_WALLET' }, + VisibilityType: { DEFAULT: 'DEFAULT' }, + }, + '../operations/dollars/Tron': {}, + '../operations/dollars/USDT': { __esModule: true, default: service }, + '../operations/BIP85': { __esModule: true, default: {} }, + '../operations/dollars/GasFree': historical, + 'src/utils/service-utilities/config': { + __esModule: true, + default: { isDevMode: () => false }, + }, +}); + +test('retired provider operations fail closed without a provider import', async () => { + assert.equal(await service.isGasFreeSupported(network.MAINNET), false); + assert.equal((await service.getServiceProviders(network.MAINNET)).length, 0); + await assert.rejects(service.getAccountStatus('TOwner'), /USDT activity is paused/); + assert.equal((await service.prepareTransfer({ source: wallet })).isValid, false); + assert.equal((await service.submitTransfer(wallet, {})).success, false); + await assert.rejects(service.getTransferStatus('historical-trace'), /USDT activity is paused/); + assert.equal((await service.monitorTransfer('historical-trace')).success, false); +}); + +test('stored address, chain reads and historical trace survive the pause', async () => { + assert.equal(await factory.updateUSDTWalletAccountStatus(wallet), wallet.accountStatus); + assert.equal(await factory.syncUSDTWalletBalance(wallet), 42); + assert.equal(balanceCalls[0][0], wallet.accountStatus.gasFreeAddress); + assert.equal(factory.getAvailableBalanceUSDTWallet(wallet), 10); + + const transactions = await factory.syncUSDTWalletTransactions(wallet); + assert.equal(historyCalls[0][0], wallet.accountStatus.gasFreeAddress); + assert.equal(transactions.length, 2); + assert.equal(transactions[0].txId, 'chain-1'); + assert.equal(transactions[1].traceId, 'historical-trace'); + assert.equal(transactions[1].status, historical.GasFreeTransferStatus.WAITING); +}); + +test('missing saved address is never replaced by the owner address', async () => { + const missing = { ...wallet, accountStatus: { ...wallet.accountStatus, gasFreeAddress: '' } }; + await assert.rejects(factory.syncUSDTWalletBalance(missing), /Stored USDT address/); + await assert.rejects(factory.syncUSDTWalletTransactions(missing), /Stored USDT address/); +}); From 486594d866a7fe3afdbc006a58f198251bfd6724 Mon Sep 17 00:00:00 2001 From: Anant Date: Thu, 8 Oct 2026 09:46:50 +0530 Subject: [PATCH 046/254] Guard iOS config codegen against retired credentials --- ios/Podfile | 11 +++++ ios/scripts/verify-retired-env.rb | 17 ++++++++ ios/scripts/verify-retired-env.test.cjs | 58 +++++++++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 ios/scripts/verify-retired-env.rb create mode 100644 ios/scripts/verify-retired-env.test.cjs diff --git a/ios/Podfile b/ios/Podfile index b0e96375f5..bbda6b4095 100644 --- a/ios/Podfile +++ b/ios/Podfile @@ -86,6 +86,17 @@ post_install do |installer| # :ccache_enabled => true ) + # react-native-config logs and embeds every key from its selected .env file. + # Run the guard in that pod's own phase before its code generator starts. + config_target = installer.pods_project.targets.find { |target| target.name == 'react-native-config' } + config_codegen = config_target&.shell_script_build_phases&.find { |phase| phase.name == '[CP-User] Config codegen' } + raise 'react-native-config codegen phase not found; release credential guard cannot be installed' unless config_codegen + + config_codegen.shell_script = <<~SCRIPT + config_codegen.shell_script + set -e + ruby "${PODS_ROOT}/../scripts/verify-retired-env.rb" "${PODS_TARGET_SRCROOT}/ios/ReactNativeConfig" "${SRCROOT}/../.." + SCRIPT + # fixes release issue with xCode 16, bitcode is stripped by default. bitcode_strip_path = `xcrun --find bitcode_strip`.chop! def strip_bitcode_from_framework(bitcode_strip_path, framework_relative_path) diff --git a/ios/scripts/verify-retired-env.rb b/ios/scripts/verify-retired-env.rb new file mode 100644 index 0000000000..413497ce62 --- /dev/null +++ b/ios/scripts/verify-retired-env.rb @@ -0,0 +1,17 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +# Use react-native-config's own reader so this checks the same ENVFILE, +# /tmp/envfile override, and .env fallback that its code generator will use. +reader_directory, environment_root = ARGV +abort('Missing react-native-config environment inputs') unless reader_directory && environment_root + +retired_names = %w[GASFREE_API_KEY GASFREE_API_SECRET LETS_EXCHANGE_API_KEY] +retired_names += retired_names.map { |name| "RN_#{name}" } +failure = 'Retired API credentials must not be included in an iOS build' + +abort(failure) if retired_names.any? { |name| ENV.key?(name) } + +require File.join(reader_directory, 'ReadDotEnv') +dotenv, = read_dot_env(environment_root) +abort(failure) if retired_names.any? { |name| dotenv.key?(name) } diff --git a/ios/scripts/verify-retired-env.test.cjs b/ios/scripts/verify-retired-env.test.cjs new file mode 100644 index 0000000000..d25f96564d --- /dev/null +++ b/ios/scripts/verify-retired-env.test.cjs @@ -0,0 +1,58 @@ +const { test } = require('node:test'); +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const os = require('node:os'); +const path = require('node:path'); +const { spawnSync } = require('node:child_process'); + +const verifier = path.join(__dirname, 'verify-retired-env.rb'); +const reader = process.env.KEEPER_TEST_RN_CONFIG_READER || + path.join(__dirname, '..', '..', 'node_modules', 'react-native-config', 'ios', 'ReactNativeConfig'); +const secret = 'synthetic-secret-not-for-output'; +const retiredNames = ['GASFREE_API_KEY', 'GASFREE_API_SECRET', 'LETS_EXCHANGE_API_KEY']; +retiredNames.push(...retiredNames.map(name => `RN_${name}`)); + +function verify(contents, additionalEnv = {}) { + const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'keeper-ios-env-')); + try { + const envFile = path.join(directory, '.env.test'); + fs.writeFileSync(envFile, contents); + return spawnSync('ruby', [verifier, reader, directory], { + cwd: directory, + env: { PATH: process.env.PATH, ENVFILE: envFile, ...additionalEnv }, + encoding: 'utf8', + }); + } finally { + fs.rmSync(directory, { recursive: true, force: true }); + } +} + +test('iOS pod guard accepts config without retired credentials', () => { + assert.ok(fs.existsSync(reader), 'Install react-native-config before running this test'); + const result = verify('CHANNEL_URL=https://channel.bitcoinkeeper.app/\n'); + assert.equal(result.status, 0, result.stderr); +}); + +for (const name of retiredNames) { + test(`iOS pod guard rejects ${name} in selected ENVFILE before logging its value`, + () => { + const result = verify(`CHANNEL_URL=https://channel.bitcoinkeeper.app/\n${name}=${secret}\n`); + assert.notEqual(result.status, 0); + assert.doesNotMatch(result.stdout + result.stderr, /synthetic-secret-not-for-output/); + }); +} + +test('iOS pod guard rejects exported retired credentials', () => { + const result = verify(`export GASFREE_API_KEY=${secret}\n`); + assert.notEqual(result.status, 0); + assert.doesNotMatch(result.stdout + result.stderr, /synthetic-secret-not-for-output/); +}); + +test('iOS pod guard rejects a retired process variable before logging its value', + () => { + const result = verify('CHANNEL_URL=https://channel.bitcoinkeeper.app/\n', { + RN_GASFREE_API_SECRET: secret, + }); + assert.notEqual(result.status, 0); + assert.doesNotMatch(result.stdout + result.stderr, /synthetic-secret-not-for-output/); + }); From 627ba03a132069daa593ec430c421af2da719e2d Mon Sep 17 00:00:00 2001 From: Anant Date: Thu, 8 Oct 2026 09:47:12 +0530 Subject: [PATCH 047/254] Clarify status of historical USDT requests --- src/components/TransactionElement.tsx | 25 +++++++- src/context/Localization/language/en.json | 4 ++ src/context/Localization/language/es.json | 4 ++ src/screens/USDT/UsdtTransactionDetail.tsx | 28 +++++---- src/screens/USDT/components/StatusContent.tsx | 41 ++++++++----- .../wallets/operations/dollars/USDT.ts | 4 ++ tests/services/usdtPause.test.cjs | 59 +++++++++++-------- 7 files changed, 112 insertions(+), 53 deletions(-) diff --git a/src/components/TransactionElement.tsx b/src/components/TransactionElement.tsx index a5adda3fac..411e9b2c4b 100644 --- a/src/components/TransactionElement.tsx +++ b/src/components/TransactionElement.tsx @@ -1,4 +1,4 @@ -import React from 'react'; +import React, { useContext } from 'react'; import { Box, useColorMode } from '@gluestack-ui/themed-native-base'; import { StyleSheet, TouchableOpacity } from 'react-native'; import moment from 'moment'; @@ -21,10 +21,14 @@ import Colors from 'src/theme/Colors'; import useLabelsNew from 'src/hooks/useLabelsNew'; import { Wallet } from 'src/services/wallets/interfaces/wallet'; import { Vault } from 'src/services/wallets/interfaces/vault'; -import { USDTTransaction } from 'src/services/wallets/operations/dollars/USDT'; +import { + isHistoricalUnverifiedUSDTRequest, + USDTTransaction, +} from 'src/services/wallets/operations/dollars/USDT'; import { EntityKind } from 'src/services/wallets/enums'; import { USDTWallet } from 'src/services/wallets/factories/USDTWalletFactory'; import ThemedColor from './ThemedColor/ThemedColor'; +import { LocalizationContext } from 'src/context/Localization/LocContext'; function TransactionElement({ transaction, @@ -44,6 +48,9 @@ function TransactionElement({ let amount: number; let confirmations: number; let transactionType: string; + const legacyUSDTRequest = + wallet.entityKind === EntityKind.USDT_WALLET && + isHistoricalUnverifiedUSDTRequest(transaction as USDTTransaction); if (wallet.entityKind === EntityKind.USDT_WALLET) { transactionId = (transaction as USDTTransaction).txId || (transaction as USDTTransaction).traceId; @@ -65,6 +72,7 @@ function TransactionElement({ const { labels } = useLabelsNew({ txid: transactionId }); const { colorMode } = useColorMode(); + const { translations } = useContext(LocalizationContext); const formattedDate = moment(date)?.format('DD MMM YY . HH:mm A'); const viewAll_color = ThemedColor({ name: 'viewAll_color' }); @@ -82,7 +90,7 @@ function TransactionElement({ > - {confirmations === 0 && !isCached && ( + {confirmations === 0 && !isCached && !legacyUSDTRequest && ( @@ -113,6 +121,11 @@ function TransactionElement({ {formattedDate} + {legacyUSDTRequest && ( + + {translations.usdtWalletText.statusUnavailable} + + )} {(transaction as Transaction).tags?.includes('potential-dust-spend') && ( @@ -167,6 +180,12 @@ const styles = StyleSheet.create({ marginHorizontal: 3, lineHeight: 17, }, + legacyStatus: { + fontSize: 10, + width: wp(125), + marginHorizontal: 3, + lineHeight: 13, + }, transactionIdText: { marginHorizontal: 4, fontSize: 14, diff --git a/src/context/Localization/language/en.json b/src/context/Localization/language/en.json index b93cb5be23..1f0aecf80f 100644 --- a/src/context/Localization/language/en.json +++ b/src/context/Localization/language/en.json @@ -2158,6 +2158,10 @@ "sendingPausedTitle": "USDT sending is paused", "sendingPausedBody": "Keeper cannot send USDT from this wallet right now. Your wallet details, balance and transaction history remain available.", "receivePausedWarning": "This is your wallet's saved receive address. Keeper cannot send USDT from this wallet while sending is paused. Funds received here cannot be sent through Keeper during the pause. Do not deposit new USDT unless you understand this limitation.", + "statusUnavailable": "Status unavailable", + "legacyRequestTitle": "Earlier USDT request", + "legacyRequestNotice": "Keeper can no longer verify this earlier USDT request. If it completed, it may also appear as a separate on-chain transaction.", + "requestedAmount": "Requested Amount", "pickWalletType": "Pick Your Wallet Type", "selectCurrency": "Select the currency you would like to use", "bitcoinWallet": "Bitcoin Wallet", diff --git a/src/context/Localization/language/es.json b/src/context/Localization/language/es.json index 803c3e55d8..1df00a4afa 100644 --- a/src/context/Localization/language/es.json +++ b/src/context/Localization/language/es.json @@ -2139,6 +2139,10 @@ "sendingPausedTitle": "El envío de USDT está pausado", "sendingPausedBody": "Keeper no puede enviar USDT desde esta cartera ahora mismo. Los detalles, el saldo y el historial de transacciones de la cartera siguen disponibles.", "receivePausedWarning": "Esta es la dirección de recepción guardada de tu cartera. Keeper no puede enviar USDT desde esta cartera mientras el envío esté pausado. Los fondos recibidos aquí no se pueden enviar mediante Keeper durante la pausa. No deposites nuevos USDT a menos que comprendas esta limitación.", + "statusUnavailable": "Estado no disponible", + "legacyRequestTitle": "Solicitud anterior de USDT", + "legacyRequestNotice": "Keeper ya no puede verificar esta solicitud de USDT anterior. Si se completó, también puede aparecer como una transacción separada en la cadena.", + "requestedAmount": "Importe solicitado", "pickWalletType": "Pick Your Wallet Type", "selectCurrency": "Select the currency you would like to use", "bitcoinWallet": "Bitcoin Wallet", diff --git a/src/screens/USDT/UsdtTransactionDetail.tsx b/src/screens/USDT/UsdtTransactionDetail.tsx index 521ac6f099..0b6371756e 100644 --- a/src/screens/USDT/UsdtTransactionDetail.tsx +++ b/src/screens/USDT/UsdtTransactionDetail.tsx @@ -18,13 +18,14 @@ import Edit from 'src/assets/images/edit.svg'; import EditDark from 'src/assets/images/edit-white.svg'; import { current } from '@reduxjs/toolkit'; import StatusContent from './components/StatusContent'; -import { USDTTransaction } from 'src/services/wallets/operations/dollars/USDT'; +import { + isHistoricalUnverifiedUSDTRequest, + USDTTransaction, +} from 'src/services/wallets/operations/dollars/USDT'; import { USDTWallet } from 'src/services/wallets/factories/USDTWalletFactory'; import Link from 'src/assets/images/link.svg'; import LinkDark from 'src/assets/images/link-white.svg'; import openLink from 'src/utils/OpenLink'; -import useToastMessage from 'src/hooks/useToastMessage'; -import ToastErrorIcon from 'src/assets/images/toast_error.svg'; export function EditNoteContent({ existingNote, noteRef }: { existingNote: string; noteRef }) { const updateNote = useCallback((text) => { @@ -52,6 +53,9 @@ const UsdtTransactionDetail = ({ route }) => { const transactionId = transaction.txId || transaction.traceId; const date = transaction.timestamp; const amount = parseFloat(transaction.amount); + // A trace without a chain transaction ID is a historical provider request. + // Its final result cannot be established after the provider was retired. + const legacyRequest = isHistoricalUnverifiedUSDTRequest(transaction); const status = transaction.status; let transactionType: string; if (transaction.to === wallet.accountStatus.gasFreeAddress) { @@ -66,7 +70,6 @@ const UsdtTransactionDetail = ({ route }) => { const [visible, setVisible] = useState(false); const [updatingLabel, setUpdatingLabel] = useState(false); const close = () => setVisible(false); - const { showToast } = useToastMessage(); function InfoCard({ title, @@ -182,14 +185,10 @@ const UsdtTransactionDetail = ({ route }) => { */} { if (transaction.txId) { redirectToBlockExplorer(transaction.txId); - } else if (transaction.traceId) { - showToast( - 'Transaction is being processed and does not have a Transaction ID yet.', - - ); } }} > @@ -213,8 +212,15 @@ const UsdtTransactionDetail = ({ route }) => { title={usdtWalletText.status} showIcon={false} letterSpacing={2.4} - Content={() => } + Content={() => } /> + {legacyRequest && ( + + )} {transactionType === 'Sent' && ( { title={ transactionType === 'Received' ? usdtWalletText.recievedAmount + : legacyRequest + ? usdtWalletText.requestedAmount : usdtWalletText.sendingAmount } describtion={`${amount} USDT`} diff --git a/src/screens/USDT/components/StatusContent.tsx b/src/screens/USDT/components/StatusContent.tsx index d20220cf61..26201e079c 100644 --- a/src/screens/USDT/components/StatusContent.tsx +++ b/src/screens/USDT/components/StatusContent.tsx @@ -1,26 +1,30 @@ import { Box, useColorMode } from '@gluestack-ui/themed-native-base'; -import React from 'react'; +import React, { useContext } from 'react'; import { StyleSheet } from 'react-native'; import Text from 'src/components/KeeperText'; import { hp, wp } from 'src/constants/responsive'; import { GasFreeTransferStatus } from 'src/services/wallets/operations/dollars/GasFree'; import Colors from 'src/theme/Colors'; +import { LocalizationContext } from 'src/context/Localization/LocContext'; -const StatusContent = ({ status }) => { +const StatusContent = ({ status, unavailable = false }) => { const { colorMode } = useColorMode(); - const containerbackgroundColor = - status === GasFreeTransferStatus.SUCCEED - ? Colors.PaleTropicalTeal - : status === GasFreeTransferStatus.CONFIRMING - ? Colors.lightOrange - : Colors.lightindigoblue; + const { translations } = useContext(LocalizationContext); + const containerbackgroundColor = unavailable + ? Colors.lightindigoblue + : status === GasFreeTransferStatus.SUCCEED + ? Colors.PaleTropicalTeal + : status === GasFreeTransferStatus.CONFIRMING + ? Colors.lightOrange + : Colors.lightindigoblue; - const textColor = - status === GasFreeTransferStatus.SUCCEED - ? Colors.TropicalTeal - : status === GasFreeTransferStatus.CONFIRMING - ? Colors.darkOrange - : Colors.indigoblue; + const textColor = unavailable + ? Colors.indigoblue + : status === GasFreeTransferStatus.SUCCEED + ? Colors.TropicalTeal + : status === GasFreeTransferStatus.CONFIRMING + ? Colors.darkOrange + : Colors.indigoblue; return ( { style={styles.container} > - {status === GasFreeTransferStatus.SUCCEED ? 'SUCCESS' : status} + {unavailable + ? translations.usdtWalletText.statusUnavailable + : status === GasFreeTransferStatus.SUCCEED + ? 'SUCCESS' + : status} ); @@ -40,7 +48,8 @@ const styles = StyleSheet.create({ container: { borderWidth: 1, borderRadius: 30, - width: wp(80), + minWidth: wp(80), + paddingHorizontal: wp(8), height: hp(20), justifyContent: 'center', alignItems: 'center', diff --git a/src/services/wallets/operations/dollars/USDT.ts b/src/services/wallets/operations/dollars/USDT.ts index 6a6085c8ba..5b490c0a97 100644 --- a/src/services/wallets/operations/dollars/USDT.ts +++ b/src/services/wallets/operations/dollars/USDT.ts @@ -61,6 +61,10 @@ export interface USDTTransaction { isGasFree: boolean; } +export const isHistoricalUnverifiedUSDTRequest = ( + transaction: Pick +): boolean => Boolean(transaction.traceId && !transaction.txId); + export default class USDT { private static getUSDTAddress(networkType?: NetworkType): string { return USDT_ADDRESSES[networkType || NetworkType.MAINNET]; diff --git a/tests/services/usdtPause.test.cjs b/tests/services/usdtPause.test.cjs index a5ddd3b9eb..133372ad7b 100644 --- a/tests/services/usdtPause.test.cjs +++ b/tests/services/usdtPause.test.cjs @@ -20,10 +20,11 @@ function loadTypeScript(relativePath, imports = {}) { if (!(specifier in imports)) throw new Error(`Unexpected import: ${specifier}`); return imports[specifier]; }; - const run = vm.runInNewContext( - `(function (require, module, exports) { ${code}\n})`, - { Date, Error, Promise } - ); + const run = vm.runInNewContext(`(function (require, module, exports) { ${code}\n})`, { + Date, + Error, + Promise, + }); run(loadImport, module, module.exports); return module.exports; } @@ -41,23 +42,26 @@ const tron = { getTrc20Transactions: async (...args) => { historyCalls.push(args); return { - transactions: [{ - transactionId: 'chain-1', - from: 'TSender', - to: 'TStoredGasFreeAddress', - formattedValue: 5, - blockNumber: 1, - blockTimestamp: 100, - }], + transactions: [ + { + transactionId: 'chain-1', + from: 'TStoredGasFreeAddress', + to: 'TRecipient', + formattedValue: 2, + blockNumber: 1, + blockTimestamp: 100, + }, + ], meta: { fingerprint: '', hasMore: false }, }; }, }; -const service = loadTypeScript('src/services/wallets/operations/dollars/USDT.ts', { +const usdtModule = loadTypeScript('src/services/wallets/operations/dollars/USDT.ts', { './GasFree': historical, 'src/services/wallets/enums': { NetworkType: network }, './Tron': tron, -}).default; +}); +const service = usdtModule.default; const wallet = { networkType: network.MAINNET, @@ -74,15 +78,17 @@ const wallet = { address: 'TOwner', privateKey: 'fixture-only', balance: 10, - transactions: [{ - traceId: 'historical-trace', - from: 'TStoredGasFreeAddress', - to: 'TRecipient', - amount: '2', - status: historical.GasFreeTransferStatus.WAITING, - timestamp: 50, - isGasFree: true, - }], + transactions: [ + { + traceId: 'historical-trace', + from: 'TStoredGasFreeAddress', + to: 'TRecipient', + amount: '2', + status: historical.GasFreeTransferStatus.WAITING, + timestamp: 50, + isGasFree: true, + }, + ], hasNewUpdates: false, lastSynched: 1, }, @@ -115,7 +121,7 @@ test('retired provider operations fail closed without a provider import', async assert.equal((await service.monitorTransfer('historical-trace')).success, false); }); -test('stored address, chain reads and historical trace survive the pause', async () => { +test('stored address, chain reads and ambiguous historical request survive the pause', async () => { assert.equal(await factory.updateUSDTWalletAccountStatus(wallet), wallet.accountStatus); assert.equal(await factory.syncUSDTWalletBalance(wallet), 42); assert.equal(balanceCalls[0][0], wallet.accountStatus.gasFreeAddress); @@ -126,6 +132,11 @@ test('stored address, chain reads and historical trace survive the pause', async assert.equal(transactions.length, 2); assert.equal(transactions[0].txId, 'chain-1'); assert.equal(transactions[1].traceId, 'historical-trace'); + assert.equal(transactions[0].to, transactions[1].to); + assert.equal(transactions[0].amount, transactions[1].amount); + assert.equal(transactions[1].txId, undefined); + assert.equal(usdtModule.isHistoricalUnverifiedUSDTRequest(transactions[0]), false); + assert.equal(usdtModule.isHistoricalUnverifiedUSDTRequest(transactions[1]), true); assert.equal(transactions[1].status, historical.GasFreeTransferStatus.WAITING); }); From e6c518e4a4aaf065d291d2d83d70a1cff5370018 Mon Sep 17 00:00:00 2001 From: Anant Date: Thu, 8 Oct 2026 09:48:54 +0530 Subject: [PATCH 048/254] Run USDT pause checks in pull request CI --- .github/workflows/test.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index dbff68d1af..7db798d56b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -30,5 +30,10 @@ jobs: - name: Install dependencies run: yarn install + - name: Check USDT pause and retired build inputs + run: | + node --test tests/services/usdtPause.test.cjs tests/recovery/usdtBackup.test.cjs \ + scripts/verify-channel-config.test.cjs ios/scripts/verify-retired-env.test.cjs + - name: Run tests run: npm run test From 179038185a2f2ccecbbd2c5e4ea56d127c087b1e Mon Sep 17 00:00:00 2001 From: Anant Date: Thu, 8 Oct 2026 12:30:57 +0530 Subject: [PATCH 049/254] build: validate Android reproducibility inputs before release builds --- .github/workflows/test.yml | 3 + reproducibility/README.md | 34 ++++ reproducibility/android-inputs.json | 17 ++ reproducibility/test_verify_android_inputs.py | 74 +++++++ reproducibility/verify-android-inputs.py | 190 ++++++++++++++++++ 5 files changed, 318 insertions(+) create mode 100644 reproducibility/android-inputs.json create mode 100644 reproducibility/test_verify_android_inputs.py create mode 100644 reproducibility/verify-android-inputs.py diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index dbff68d1af..1d56528e0a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -32,3 +32,6 @@ jobs: - name: Run tests run: npm run test + + - name: Test Android reproducibility tools + run: python3 -B -m unittest scripts/test_compare_android_apks.py reproducibility/test_verify_android_inputs.py diff --git a/reproducibility/README.md b/reproducibility/README.md index 424766716d..975a5c613c 100644 --- a/reproducibility/README.md +++ b/reproducibility/README.md @@ -128,3 +128,37 @@ require Docker, Podman, or Nix; a script ending in `build.sh`; a `--binary` argument; and an adjacent `COMPARISON_RESULTS.yaml`. No such script or results file is included here because the exact production input and complete third-party build path have not yet been established. + +## Fail closed on future build inputs + +`android-inputs.json` pins the Node, Yarn, JDK, Gradle wrapper, lockfile, and +Android SDK package versions chosen for the next independent build attempt. +These are **chosen future inputs**, not a reconstruction of the v2.6.3 +publisher's environment. Before any production build, run: + +```sh +python3 reproducibility/verify-android-inputs.py \ + --source-commit \ + --env-file /path/to/reviewed/.env.production \ + --approved-env-names /path/to/reviewed/approved-env-names.txt \ + --android-sdk-root /path/to/android-sdk +``` + +The approved names file contains exactly one reviewed environment key per line +and no values. It must come from the release review. The checker rejects +missing or extra names, a dirty or wrong source checkout, changed dependency +hashes, mismatched tool versions, missing SDK packages, a symlinked +`node_modules` root, and links from dependencies to files outside the checkout. +It does not output environment values. A local dependency install is necessary; +linking to a sibling `node_modules` directory recreates an observed historical +build-path dependency and fails this check. + +This check does not yet pin the container image, Android command-line tools +download, Maven artifacts, or every transitive native input. It does not run +the build or compare a binary and is not a WalletScrutiny `build.sh`. The exact +production environment names and values still need release-owner review before +any public recipe or verdict. Run its focused tests with: + +```sh +python3 -m unittest reproducibility/test_verify_android_inputs.py +``` diff --git a/reproducibility/android-inputs.json b/reproducibility/android-inputs.json new file mode 100644 index 0000000000..ca7f711647 --- /dev/null +++ b/reproducibility/android-inputs.json @@ -0,0 +1,17 @@ +{ + "node": "22.23.3", + "yarn": "1.22.22", + "java": "17.0.19", + "yarn_lock_sha256": "b257e6d81f2850ca1c12ce3ddb559d39cd3113a51c09a9eb71ca11c81bf5a245", + "gradle_wrapper_jar_sha256": "0336f591bc0ec9aa0c9988929b93ecc916b3c1d52aed202c7381db144aa0ef15", + "gradle_distribution_sha256": "bd71102213493060956ec229d946beee57158dbd89d0e62b91bca0fa2c5f3531", + "android_packages": { + "build-tools/35.0.0": "35.0.0", + "ndk/27.1.12297006": "27.1.12297006", + "cmake/3.22.1": "3.22.1" + }, + "android_platform": { + "path": "platforms/android-36", + "revision": "2" + } +} diff --git a/reproducibility/test_verify_android_inputs.py b/reproducibility/test_verify_android_inputs.py new file mode 100644 index 0000000000..35f206ab7b --- /dev/null +++ b/reproducibility/test_verify_android_inputs.py @@ -0,0 +1,74 @@ +import importlib.util +from pathlib import Path +import tempfile +import unittest + + +SCRIPT = Path(__file__).with_name("verify-android-inputs.py") +SPEC = importlib.util.spec_from_file_location("verify_android_inputs", SCRIPT) +MODULE = importlib.util.module_from_spec(SPEC) +SPEC.loader.exec_module(MODULE) + + +class AndroidInputTests(unittest.TestCase): + def test_unapproved_environment_name_is_rejected_without_value(self): + with tempfile.TemporaryDirectory() as temporary: + root = Path(temporary) + env = root / ".env.production" + approved = root / "approved-env-names.txt" + env.write_text("ENVIRONMENT=production\nUNREVIEWED_TOKEN=private-example-value\n") + approved.write_text("ENVIRONMENT\n") + with self.assertRaises(MODULE.InputError) as caught: + MODULE.check_env_names(env, approved) + self.assertIn("UNREVIEWED_TOKEN", str(caught.exception)) + self.assertNotIn("private-example-value", str(caught.exception)) + + def test_missing_or_duplicate_approved_name_is_rejected(self): + with tempfile.TemporaryDirectory() as temporary: + root = Path(temporary) + env = root / ".env.production" + approved = root / "approved-env-names.txt" + env.write_text("ENVIRONMENT=production\n") + approved.write_text("ENVIRONMENT\nRELAY\n") + with self.assertRaisesRegex(MODULE.InputError, "missing environment names: RELAY"): + MODULE.check_env_names(env, approved) + approved.write_text("ENVIRONMENT\nENVIRONMENT\n") + with self.assertRaisesRegex(MODULE.InputError, "duplicate approved name"): + MODULE.check_env_names(env, approved) + + def test_external_or_root_dependency_symlink_is_rejected(self): + with tempfile.TemporaryDirectory() as temporary: + root = Path(temporary) + outside = root / "outside" + outside.mkdir() + dependencies = root / "checkout/node_modules" + dependencies.parent.mkdir() + dependencies.symlink_to(outside, target_is_directory=True) + with self.assertRaisesRegex(MODULE.InputError, "real directory"): + MODULE.check_local_dependencies(dependencies.parent) + dependencies.unlink() + dependencies.mkdir() + (dependencies / "escaped").symlink_to(outside, target_is_directory=True) + with self.assertRaisesRegex(MODULE.InputError, "outside this checkout"): + MODULE.check_local_dependencies(dependencies.parent) + + def test_internal_dependency_symlink_is_allowed(self): + with tempfile.TemporaryDirectory() as temporary: + root = Path(temporary) + dependencies = root / "node_modules" + package = dependencies / "some-package" + package.mkdir(parents=True) + bin_directory = dependencies / ".bin" + bin_directory.mkdir() + (bin_directory / "some-package").symlink_to(package, target_is_directory=True) + MODULE.check_local_dependencies(root) + + def test_sdk_revision_accepts_android_property_spacing(self): + with tempfile.TemporaryDirectory() as temporary: + package = Path(temporary) + (package / "source.properties").write_text("Pkg.Revision = 3.22.1\n") + self.assertEqual(MODULE.sdk_revision(package), "3.22.1") + + +if __name__ == "__main__": + unittest.main() diff --git a/reproducibility/verify-android-inputs.py b/reproducibility/verify-android-inputs.py new file mode 100644 index 0000000000..914acf978f --- /dev/null +++ b/reproducibility/verify-android-inputs.py @@ -0,0 +1,190 @@ +#!/usr/bin/env python3 +"""Fail closed on known path, configuration, and toolchain drift before an Android build. + +This is an input check, not a complete build recipe or reproducibility verdict. +Environment values are never printed or written to an output file. +""" + +import argparse +import hashlib +import json +import os +from pathlib import Path +import re +import subprocess +import sys + + +class InputError(Exception): + pass + + +KEY_RE = re.compile(r"^[A-Za-z_][A-Za-z0-9_]*$") +ASSIGNMENT_RE = re.compile(r"^(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=") + + +def sha256(path): + digest = hashlib.sha256() + with path.open("rb") as source: + for chunk in iter(lambda: source.read(1024 * 1024), b""): + digest.update(chunk) + return digest.hexdigest() + + +def read_env_names(path): + """Read only names; deliberately never retain or show the values.""" + names = set() + for line_number, line in enumerate(path.read_text(encoding="utf-8").splitlines(), 1): + stripped = line.strip() + if not stripped or stripped.startswith("#"): + continue + match = ASSIGNMENT_RE.match(stripped) + if not match: + raise InputError(f"invalid environment assignment at line {line_number}") + name = match.group(1) + if name in names: + raise InputError(f"duplicate environment name: {name}") + names.add(name) + return names + + +def read_approved_names(path): + names = set() + for line_number, line in enumerate(path.read_text(encoding="utf-8").splitlines(), 1): + name = line.strip() + if not name or name.startswith("#"): + continue + if not KEY_RE.fullmatch(name): + raise InputError(f"invalid approved name at line {line_number}") + if name in names: + raise InputError(f"duplicate approved name: {name}") + names.add(name) + if not names: + raise InputError("approved environment name list is empty") + return names + + +def check_env_names(env_file, approved_names_file): + actual = read_env_names(env_file) + approved = read_approved_names(approved_names_file) + unexpected = sorted(actual - approved) + missing = sorted(approved - actual) + if unexpected or missing: + parts = [] + if unexpected: + parts.append("unexpected environment names: " + ", ".join(unexpected)) + if missing: + parts.append("missing environment names: " + ", ".join(missing)) + raise InputError("; ".join(parts)) + + +def check_local_dependencies(repo): + dependencies = repo / "node_modules" + if dependencies.is_symlink() or not dependencies.is_dir(): + raise InputError("node_modules must be a real directory inside this checkout") + root = dependencies.resolve() + for current, directories, files in os.walk(dependencies, followlinks=False): + for name in directories + files: + path = Path(current) / name + if path.is_symlink(): + try: + target = path.resolve(strict=True) + except (OSError, RuntimeError): + raise InputError("node_modules contains a broken or cyclic symlink") from None + if not target.is_relative_to(root): + raise InputError("node_modules contains a symlink outside this checkout") + + +def run(*command, cwd=None): + try: + result = subprocess.run(command, cwd=cwd, check=True, capture_output=True, text=True) + except (OSError, subprocess.CalledProcessError): + raise InputError(f"cannot run required command: {command[0]}") from None + return result.stdout + result.stderr + + +def check_source(repo, commit): + if not re.fullmatch(r"[0-9a-f]{40}", commit): + raise InputError("expected source commit must be a full 40-character SHA") + actual = run("git", "rev-parse", "HEAD", cwd=repo).strip() + if actual != commit: + raise InputError("checkout HEAD differs from expected source commit") + if run("git", "status", "--porcelain=v1", "--untracked-files=normal", cwd=repo).strip(): + raise InputError("source checkout has uncommitted files") + + +def check_hash(path, expected, label): + if not path.is_file() or sha256(path) != expected: + raise InputError(f"{label} differs from the pinned hash") + + +def check_gradle_distribution(repo, expected): + wrapper = (repo / "android/gradle/wrapper/gradle-wrapper.properties").read_text(encoding="utf-8") + if f"distributionSha256Sum={expected}" not in wrapper.splitlines(): + raise InputError("Gradle distribution checksum differs from the pinned hash") + + +def sdk_revision(path): + properties = path / "source.properties" + if not properties.is_file(): + raise InputError(f"Android SDK package missing: {path.name}") + for line in properties.read_text(encoding="utf-8").splitlines(): + match = re.fullmatch(r"Pkg\.Revision\s*=\s*(\S+)\s*", line) + if match: + return match.group(1) + raise InputError(f"Android SDK package has no revision: {path.name}") + + +def check_toolchain(inputs, sdk_root): + node = run("node", "--version").strip().removeprefix("v") + yarn = run("yarn", "--version").strip() + java_output = run("java", "-XshowSettings:properties", "-version") + java_match = re.search(r"^\s*java\.version\s*=\s*(\S+)\s*$", java_output, re.MULTILINE) + if (node, yarn, java_match.group(1) if java_match else None) != ( + inputs["node"], inputs["yarn"], inputs["java"] + ): + raise InputError("Node, Yarn, or JDK version differs from pinned Android inputs") + for package, revision in inputs["android_packages"].items(): + if sdk_revision(sdk_root / package) != revision: + raise InputError(f"Android SDK package revision differs: {package}") + platform = inputs["android_platform"] + if sdk_revision(sdk_root / platform["path"]) != platform["revision"]: + raise InputError("Android platform revision differs from pinned Android inputs") + if not (sdk_root / platform["path"] / "android.jar").is_file(): + raise InputError("pinned Android platform is missing") + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--repo", type=Path, default=Path(__file__).resolve().parent.parent) + parser.add_argument("--source-commit", required=True) + parser.add_argument("--env-file", type=Path, required=True) + parser.add_argument("--approved-env-names", type=Path, required=True) + parser.add_argument("--android-sdk-root", type=Path, default=None) + args = parser.parse_args() + repo = args.repo.resolve() + sdk_root = args.android_sdk_root or os.environ.get("ANDROID_HOME") or os.environ.get("ANDROID_SDK_ROOT") + if not sdk_root: + raise InputError("ANDROID_HOME or ANDROID_SDK_ROOT is required") + sdk_root = Path(sdk_root).resolve() + inputs = json.loads((repo / "reproducibility/android-inputs.json").read_text(encoding="utf-8")) + check_source(repo, args.source_commit) + check_hash(repo / "yarn.lock", inputs["yarn_lock_sha256"], "yarn.lock") + check_hash( + repo / "android/gradle/wrapper/gradle-wrapper.jar", + inputs["gradle_wrapper_jar_sha256"], + "Gradle wrapper JAR", + ) + check_gradle_distribution(repo, inputs["gradle_distribution_sha256"]) + check_local_dependencies(repo) + check_env_names(args.env_file, args.approved_env_names) + check_toolchain(inputs, sdk_root) + print("Android build inputs verified; this is not a reproducibility verdict.") + + +if __name__ == "__main__": + try: + main() + except (InputError, OSError, ValueError, KeyError, json.JSONDecodeError) as error: + print(f"Android input check failed: {error}", file=sys.stderr) + sys.exit(1) From c0418f0ac9587df43107eb126499d0335c3021ce Mon Sep 17 00:00:00 2001 From: Anant Date: Thu, 8 Oct 2026 12:35:41 +0530 Subject: [PATCH 050/254] build: track provisional Android environment name allowlist --- reproducibility/README.md | 8 ++++--- reproducibility/android-inputs.json | 11 ++++++++- reproducibility/test_verify_android_inputs.py | 13 ++++------ reproducibility/verify-android-inputs.py | 24 ++++++++----------- 4 files changed, 29 insertions(+), 27 deletions(-) diff --git a/reproducibility/README.md b/reproducibility/README.md index 975a5c613c..6e92902d53 100644 --- a/reproducibility/README.md +++ b/reproducibility/README.md @@ -140,12 +140,14 @@ publisher's environment. Before any production build, run: python3 reproducibility/verify-android-inputs.py \ --source-commit \ --env-file /path/to/reviewed/.env.production \ - --approved-env-names /path/to/reviewed/approved-env-names.txt \ --android-sdk-root /path/to/android-sdk ``` -The approved names file contains exactly one reviewed environment key per line -and no values. It must come from the release review. The checker rejects +The tracked manifest contains seven source-audited environment names and no +values. This is a **provisional future-release allowlist**: the GasFree pause +in draft PR #7037 and integration checks for four candidate omissions must be +reviewed before using it for a release. Changing the list requires a tracked +source change. The checker rejects missing or extra names, a dirty or wrong source checkout, changed dependency hashes, mismatched tool versions, missing SDK packages, a symlinked `node_modules` root, and links from dependencies to files outside the checkout. diff --git a/reproducibility/android-inputs.json b/reproducibility/android-inputs.json index ca7f711647..14c9bfc290 100644 --- a/reproducibility/android-inputs.json +++ b/reproducibility/android-inputs.json @@ -13,5 +13,14 @@ "android_platform": { "path": "platforms/android-36", "revision": "2" - } + }, + "environment_names": [ + "CHANNEL_URL", + "ENVIRONMENT", + "HEXA_ID_MAINNET", + "HEXA_ID_TESTNET", + "RELAY", + "SIGNING_SERVER_MAINNET", + "SIGNING_SERVER_TESTNET" + ] } diff --git a/reproducibility/test_verify_android_inputs.py b/reproducibility/test_verify_android_inputs.py index 35f206ab7b..ad629ccd4b 100644 --- a/reproducibility/test_verify_android_inputs.py +++ b/reproducibility/test_verify_android_inputs.py @@ -15,11 +15,9 @@ def test_unapproved_environment_name_is_rejected_without_value(self): with tempfile.TemporaryDirectory() as temporary: root = Path(temporary) env = root / ".env.production" - approved = root / "approved-env-names.txt" env.write_text("ENVIRONMENT=production\nUNREVIEWED_TOKEN=private-example-value\n") - approved.write_text("ENVIRONMENT\n") with self.assertRaises(MODULE.InputError) as caught: - MODULE.check_env_names(env, approved) + MODULE.check_env_names(env, ["ENVIRONMENT"]) self.assertIn("UNREVIEWED_TOKEN", str(caught.exception)) self.assertNotIn("private-example-value", str(caught.exception)) @@ -27,14 +25,11 @@ def test_missing_or_duplicate_approved_name_is_rejected(self): with tempfile.TemporaryDirectory() as temporary: root = Path(temporary) env = root / ".env.production" - approved = root / "approved-env-names.txt" env.write_text("ENVIRONMENT=production\n") - approved.write_text("ENVIRONMENT\nRELAY\n") with self.assertRaisesRegex(MODULE.InputError, "missing environment names: RELAY"): - MODULE.check_env_names(env, approved) - approved.write_text("ENVIRONMENT\nENVIRONMENT\n") - with self.assertRaisesRegex(MODULE.InputError, "duplicate approved name"): - MODULE.check_env_names(env, approved) + MODULE.check_env_names(env, ["ENVIRONMENT", "RELAY"]) + with self.assertRaisesRegex(MODULE.InputError, "duplicate pinned environment name"): + MODULE.check_env_names(env, ["ENVIRONMENT", "ENVIRONMENT"]) def test_external_or_root_dependency_symlink_is_rejected(self): with tempfile.TemporaryDirectory() as temporary: diff --git a/reproducibility/verify-android-inputs.py b/reproducibility/verify-android-inputs.py index 914acf978f..08e0fcc775 100644 --- a/reproducibility/verify-android-inputs.py +++ b/reproducibility/verify-android-inputs.py @@ -48,25 +48,22 @@ def read_env_names(path): return names -def read_approved_names(path): +def expected_env_names(expected): + if not isinstance(expected, list) or not expected: + raise InputError("pinned environment name list is missing or empty") names = set() - for line_number, line in enumerate(path.read_text(encoding="utf-8").splitlines(), 1): - name = line.strip() - if not name or name.startswith("#"): - continue - if not KEY_RE.fullmatch(name): - raise InputError(f"invalid approved name at line {line_number}") + for name in expected: + if not isinstance(name, str) or not KEY_RE.fullmatch(name): + raise InputError("invalid pinned environment name") if name in names: - raise InputError(f"duplicate approved name: {name}") + raise InputError(f"duplicate pinned environment name: {name}") names.add(name) - if not names: - raise InputError("approved environment name list is empty") return names -def check_env_names(env_file, approved_names_file): +def check_env_names(env_file, expected_names): actual = read_env_names(env_file) - approved = read_approved_names(approved_names_file) + approved = expected_env_names(expected_names) unexpected = sorted(actual - approved) missing = sorted(approved - actual) if unexpected or missing: @@ -159,7 +156,6 @@ def main(): parser.add_argument("--repo", type=Path, default=Path(__file__).resolve().parent.parent) parser.add_argument("--source-commit", required=True) parser.add_argument("--env-file", type=Path, required=True) - parser.add_argument("--approved-env-names", type=Path, required=True) parser.add_argument("--android-sdk-root", type=Path, default=None) args = parser.parse_args() repo = args.repo.resolve() @@ -177,7 +173,7 @@ def main(): ) check_gradle_distribution(repo, inputs["gradle_distribution_sha256"]) check_local_dependencies(repo) - check_env_names(args.env_file, args.approved_env_names) + check_env_names(args.env_file, inputs["environment_names"]) check_toolchain(inputs, sdk_root) print("Android build inputs verified; this is not a reproducibility verdict.") From d96a8215e670b6fd532bdb3526f7c9ca089dfaea Mon Sep 17 00:00:00 2001 From: Anant Date: Thu, 8 Oct 2026 13:06:40 +0530 Subject: [PATCH 051/254] ci: remove stale SonarCloud workflow from release draft --- .github/workflows/build.yml | 20 -------------------- sonar-project.properties | 8 -------- 2 files changed, 28 deletions(-) delete mode 100644 .github/workflows/build.yml delete mode 100644 sonar-project.properties diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml deleted file mode 100644 index dd2225e91d..0000000000 --- a/.github/workflows/build.yml +++ /dev/null @@ -1,20 +0,0 @@ -name: Build -on: - push: - branches: - - sprint - pull_request: - types: [opened, synchronize, reopened] -jobs: - sonarcloud: - name: SonarCloud - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - with: - fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis - - name: SonarCloud Scan - uses: SonarSource/sonarcloud-github-action@master - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any - SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} diff --git a/sonar-project.properties b/sonar-project.properties deleted file mode 100644 index f7cea55e60..0000000000 --- a/sonar-project.properties +++ /dev/null @@ -1,8 +0,0 @@ -sonar.projectKey=bithyve_bitcoin-keeper -sonar.organization=bithyve - -sonar.coverage.exclusions=**/android/**/*.*,**/ios/**/*.* -sonar.exclusions=**/android/**/*.*,**/ios/**/*.* - -sonar.javascript.file.suffixes=.js,.jsx -sonar.typescript.file.suffixes=.ts,.tsx \ No newline at end of file From 0b5370d8cac2354a42c00179c265d87e66c72c68 Mon Sep 17 00:00:00 2001 From: Anant Date: Thu, 8 Oct 2026 13:07:55 +0530 Subject: [PATCH 052/254] ci: remove stale SonarCloud workflow from USDT draft --- .github/workflows/build.yml | 20 -------------------- sonar-project.properties | 8 -------- 2 files changed, 28 deletions(-) delete mode 100644 .github/workflows/build.yml delete mode 100644 sonar-project.properties diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml deleted file mode 100644 index dd2225e91d..0000000000 --- a/.github/workflows/build.yml +++ /dev/null @@ -1,20 +0,0 @@ -name: Build -on: - push: - branches: - - sprint - pull_request: - types: [opened, synchronize, reopened] -jobs: - sonarcloud: - name: SonarCloud - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - with: - fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis - - name: SonarCloud Scan - uses: SonarSource/sonarcloud-github-action@master - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any - SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} diff --git a/sonar-project.properties b/sonar-project.properties deleted file mode 100644 index f7cea55e60..0000000000 --- a/sonar-project.properties +++ /dev/null @@ -1,8 +0,0 @@ -sonar.projectKey=bithyve_bitcoin-keeper -sonar.organization=bithyve - -sonar.coverage.exclusions=**/android/**/*.*,**/ios/**/*.* -sonar.exclusions=**/android/**/*.*,**/ios/**/*.* - -sonar.javascript.file.suffixes=.js,.jsx -sonar.typescript.file.suffixes=.ts,.tsx \ No newline at end of file From ccfdd3568310171898222da1b02b49a57d4e3e26 Mon Sep 17 00:00:00 2001 From: Anant Date: Thu, 8 Oct 2026 17:09:35 +0530 Subject: [PATCH 053/254] Classify malformed backup snapshots before decryption --- src/services/backup/image.ts | 28 +++++++++----- src/services/backup/repair.ts | 11 +++++- src/services/backup/restore.ts | 6 ++- tests/recovery/repair.test.cjs | 70 ++++++++++++++++++++++++++++++++++ 4 files changed, 103 insertions(+), 12 deletions(-) diff --git a/src/services/backup/image.ts b/src/services/backup/image.ts index f4e7ca668a..c4325c643e 100644 --- a/src/services/backup/image.ts +++ b/src/services/backup/image.ts @@ -16,6 +16,8 @@ export const pauseBackup = () => export const MAX_RECORD_BYTES = 8 * 1024 * 1024; export const MAX_BACKUP_BYTES = 32 * 1024 * 1024; +export class BackupSnapshotValidationError extends Error {} + export function canonical(value: any): string { if (value === undefined) return 'null'; if (value === null || typeof value !== 'object') return JSON.stringify(value); @@ -211,7 +213,7 @@ export async function encryptRecord(key: string, record: any): Promise { export async function decryptRecord(key: string, value: unknown) { if (typeof value !== 'string' || value.length > MAX_RECORD_BYTES * 2) - throw new Error('Invalid backup record'); + throw new BackupSnapshotValidationError('Invalid backup record'); // Parsing/format decoding still require native performance validation at the // supported size ceiling. Yield before and after each record. await pauseBackup(); @@ -238,7 +240,7 @@ export async function decryptRecord(key: string, value: unknown) { } const record = JSON.parse(text); if (!record || typeof record !== 'object' || Array.isArray(record)) - throw new Error('Invalid backup record'); + throw new BackupSnapshotValidationError('Invalid backup record'); await pauseBackup(); return record; } @@ -254,12 +256,16 @@ export async function decodeImage( !app || app.appId !== appId || !app.wallets || + typeof app.wallets !== 'object' || + Array.isArray(app.wallets) || !app.signers || + typeof app.signers !== 'object' || + Array.isArray(app.signers) || !Array.isArray(app.nodes) || !Array.isArray(response.allVaultImages) || !Array.isArray(response.labels) ) - throw new Error('Incomplete backup response'); + throw new BackupSnapshotValidationError('Incomplete backup response'); const checkReferences = (expected: unknown, actual: unknown[]) => { if (expected === undefined) return; // Legacy backups can omit reference lists. if ( @@ -269,7 +275,7 @@ export async function decodeImage( new Set(actual).size !== actual.length || actual.some((id) => typeof id !== 'string' || !expected.includes(id)) ) - throw new Error('Incomplete backup references'); + throw new BackupSnapshotValidationError('Incomplete backup references'); }; checkReferences( app.vaults, @@ -283,13 +289,15 @@ export async function decodeImage( let totalBytes = 0; const decode = async (value: unknown) => { checkpoint(); - if (typeof value !== 'string') throw new Error('Invalid backup record'); + if (typeof value !== 'string') throw new BackupSnapshotValidationError('Invalid backup record'); totalBytes += value.length; - if (totalBytes > MAX_BACKUP_BYTES) throw new Error('Backup exceeds supported size'); + if (totalBytes > MAX_BACKUP_BYTES) + throw new BackupSnapshotValidationError('Backup exceeds supported size'); return decryptRecord(key, value); }; const add = (kind: keyof BackupImage, id: string, record: any) => { - if (!id || image[kind][id]) throw new Error('Duplicate or unidentified backup record'); + if (!id || image[kind][id]) + throw new BackupSnapshotValidationError('Duplicate or unidentified backup record'); image[kind][id] = record; }; for (const value of Object.values(app.wallets)) { @@ -303,7 +311,7 @@ export async function decodeImage( for (const value of response.allVaultImages) { const record = await decode(value?.vault); if (value.vaultId !== undefined && value.vaultId !== record.id) - throw new Error('Vault backup identity mismatch'); + throw new BackupSnapshotValidationError('Vault backup identity mismatch'); add('vaults', record.id, record); } for (const value of app.nodes) { @@ -316,8 +324,8 @@ export async function decodeImage( } // A partial relay response must never look like an empty backup. if (Array.isArray(app.vaults) && app.vaults.length !== Object.keys(image.vaults).length) - throw new Error('Incomplete vault backup'); + throw new BackupSnapshotValidationError('Incomplete vault backup'); if (Array.isArray(app.labels) && app.labels.length !== Object.keys(image.labels).length) - throw new Error('Incomplete label backup'); + throw new BackupSnapshotValidationError('Incomplete label backup'); return image; } diff --git a/src/services/backup/repair.ts b/src/services/backup/repair.ts index 865cd213ec..20024cba02 100644 --- a/src/services/backup/repair.ts +++ b/src/services/backup/repair.ts @@ -18,6 +18,7 @@ import { } from './transport'; import { BackupImage, + BackupSnapshotValidationError, canonical, compareImages, decodeImage, @@ -328,7 +329,15 @@ export function inspectBackup( // Only log fixed categories. Errors and responses can contain encrypted // backup data, account identifiers, headers, or request bodies. const category = error instanceof BackupRequestError ? error.category : 'non-transport'; - globalThis.console.warn('Assisted Server Backup check failed at stage:', stage, category); + const diagnosticStage = + stage === 'snapshot-decryption' && error instanceof BackupSnapshotValidationError + ? 'snapshot-validation' + : stage; + globalThis.console.warn( + 'Assisted Server Backup check failed at stage:', + diagnosticStage, + category + ); notify('unverified'); return 'unverified'; } diff --git a/src/services/backup/restore.ts b/src/services/backup/restore.ts index 80a7b8bdf7..4b521b3cdb 100644 --- a/src/services/backup/restore.ts +++ b/src/services/backup/restore.ts @@ -11,12 +11,16 @@ export async function prepareRecoveryImage( labels: any[] ) { if (!appImage || appImage.appId !== appId) throw new Error('Recovery data unavailable'); + // Older Relay registrations and deletions stored empty maps as empty arrays. + // Keep those recoverable, but let decodeImage reject nonempty malformed arrays. + const legacyEmptyMap = (value: any) => (Array.isArray(value) && !value.length ? {} : value); return decodeImage( encryptionKey, { appImage: { ...appImage, - signers: appImage.signers === undefined ? {} : appImage.signers, + wallets: legacyEmptyMap(appImage.wallets), + signers: legacyEmptyMap(appImage.signers === undefined ? {} : appImage.signers), nodes: appImage.nodes === undefined ? [] : appImage.nodes, }, allVaultImages, diff --git a/tests/recovery/repair.test.cjs b/tests/recovery/repair.test.cjs index 68883e696e..15ad8af328 100644 --- a/tests/recovery/repair.test.cjs +++ b/tests/recovery/repair.test.cjs @@ -125,6 +125,76 @@ test('4xx responses and invalid snapshots never retry', async () => { } }); +for (const [name, change] of [ + ['wrong account', (response) => { response.appImage.appId = 'another-account'; }], + ['invalid wallet collection', (response) => { response.appImage.wallets = 1; }], + ['invalid signer collection', (response) => { response.appImage.signers = []; }], + ['non-string record', (response) => { response.appImage.wallets.wallet = 42; }], + ['missing referenced vault', (response) => { response.appImage.vaults = ['missing']; }], + ['duplicate decrypted IDs', (response, key) => { + const encrypted = encryption.encrypt(key, JSON.stringify(wallet())); + response.appImage.wallets = { first: encrypted, second: encrypted }; + }], + ['vault ID mismatch', (response, key) => { + response.appImage.vaults = ['other']; + response.allVaultImages = [{ vaultId: 'other', vault: encryption.encrypt(key, JSON.stringify({ id: 'vault' })) }]; + }], + ['invalid decrypted record type', (response, key) => { + response.appImage.wallets.wallet = encryption.encrypt(key, '42'); + }], +]) test(`snapshot ${name} reports validation failure without retry or upload`, async () => { + const options = {}; + const f = harness(options); + const response = { + revision: 'a'.repeat(64), + appImage: { appId: f.app.id, wallets: {}, signers: {}, nodes: [], vaults: [], labels: [] }, + allVaultImages: [], labels: [], + }; + change(response, encryption.generateEncryptionKey(f.app.primarySeed)); + options.response = response; + assert.equal(await f.run(), 'unverified'); + assert.deepEqual(f.calls, ['getBackupSnapshot']); + assert.deepEqual(f.diagnostics, [['Assisted Server Backup check failed at stage:', 'snapshot-validation', 'non-transport']]); +}); + +test('invalid decrypted JSON reports decryption failure without retry or upload', async () => { + const options = {}; + const f = harness(options); + options.response = { + revision: 'a'.repeat(64), + appImage: { + appId: f.app.id, + wallets: { wallet: encryption.encrypt(encryption.generateEncryptionKey(f.app.primarySeed), '{') }, + signers: {}, nodes: [], vaults: [], labels: [], + }, + allVaultImages: [], labels: [], + }; + assert.equal(await f.run(), 'unverified'); + assert.deepEqual(f.calls, ['getBackupSnapshot']); + assert.deepEqual(f.diagnostics, [['Assisted Server Backup check failed at stage:', 'snapshot-decryption', 'non-transport']]); +}); + +test('legacy registered empty Relay image remains recoverable, but nonempty arrays are invalid', async () => { + const { prepareRecoveryImage } = loadModule('src/services/backup/restore.ts', { './image': image }); + const f = harness(); + const key = encryption.generateEncryptionKey(f.app.primarySeed); + for (const signers of [undefined, []]) { + const recovered = await prepareRecoveryImage( + key, f.app.id, { appId: f.app.id, wallets: [], signers, nodes: [] }, [], [] + ); + assert.deepEqual(JSON.parse(JSON.stringify(recovered)), makeImage()); + } + for (const malformed of [{ wallets: ['ciphertext'] }, { signers: ['ciphertext'] }]) { + await assert.rejects( + prepareRecoveryImage( + key, f.app.id, + { appId: f.app.id, wallets: [], signers: [], nodes: [], ...malformed }, [], [] + ), + (error) => error instanceof image.BackupSnapshotValidationError + ); + } +}); + test('transient snapshot failure after account switch makes no retry or upload', async () => { const f = harness({ post: async ({ app }) => { app.id = 'second'; From ac868253f3460dd875035e1a4f73fc99ca658c1b Mon Sep 17 00:00:00 2001 From: Anant Date: Thu, 8 Oct 2026 17:17:42 +0530 Subject: [PATCH 054/254] Bind inspected wallet map keys to decrypted IDs --- src/services/backup/image.ts | 9 +++-- src/services/backup/repair.ts | 4 ++- tests/recovery/repair.test.cjs | 66 +++++++++++++++++++++++++++++++--- 3 files changed, 72 insertions(+), 7 deletions(-) diff --git a/src/services/backup/image.ts b/src/services/backup/image.ts index c4325c643e..a98e0dfa9a 100644 --- a/src/services/backup/image.ts +++ b/src/services/backup/image.ts @@ -249,7 +249,8 @@ export async function decodeImage( key: string, response: any, appId: string, - checkpoint = () => {} + checkpoint = () => {}, + verifyWalletKeys = false ): Promise { const app = response?.appImage; if ( @@ -300,8 +301,12 @@ export async function decodeImage( throw new BackupSnapshotValidationError('Duplicate or unidentified backup record'); image[kind][id] = record; }; - for (const value of Object.values(app.wallets)) { + for (const [storedId, value] of Object.entries(app.wallets)) { const record = await decode(value); + if (typeof record.id !== 'string' || !record.id) + throw new BackupSnapshotValidationError('Invalid wallet backup identity'); + if (verifyWalletKeys && storedId !== record.id) + throw new BackupSnapshotValidationError('Wallet backup identity mismatch'); add('wallets', record.id, record); } for (const [id, value] of Object.entries(app.signers)) { diff --git a/src/services/backup/repair.ts b/src/services/backup/repair.ts index 20024cba02..5d35580e08 100644 --- a/src/services/backup/repair.ts +++ b/src/services/backup/repair.ts @@ -232,7 +232,9 @@ export function inspectBackup( } : response.data; if (!readback) stage = 'snapshot-decryption'; - const decoded = await decodeImage(key, availableResponse, appId, assertCurrent); + // Restore can salvage legacy map aliases. Inspection requires the + // stored key to match the ID inside each encrypted wallet record. + const decoded = await decodeImage(key, availableResponse, appId, assertCurrent, true); needsVaultRepair = unavailable.length > 0 || response.data.allVaultImages.some( diff --git a/tests/recovery/repair.test.cjs b/tests/recovery/repair.test.cjs index 15ad8af328..513d538924 100644 --- a/tests/recovery/repair.test.cjs +++ b/tests/recovery/repair.test.cjs @@ -131,10 +131,6 @@ for (const [name, change] of [ ['invalid signer collection', (response) => { response.appImage.signers = []; }], ['non-string record', (response) => { response.appImage.wallets.wallet = 42; }], ['missing referenced vault', (response) => { response.appImage.vaults = ['missing']; }], - ['duplicate decrypted IDs', (response, key) => { - const encrypted = encryption.encrypt(key, JSON.stringify(wallet())); - response.appImage.wallets = { first: encrypted, second: encrypted }; - }], ['vault ID mismatch', (response, key) => { response.appImage.vaults = ['other']; response.allVaultImages = [{ vaultId: 'other', vault: encryption.encrypt(key, JSON.stringify({ id: 'vault' })) }]; @@ -195,6 +191,68 @@ test('legacy registered empty Relay image remains recoverable, but nonempty arra } }); +test('recovery rejects duplicate decrypted wallet IDs despite different stored keys', async () => { + const { prepareRecoveryImage } = loadModule('src/services/backup/restore.ts', { './image': image }); + const f = harness(); + const key = encryption.generateEncryptionKey(f.app.primarySeed); + const encrypted = encryption.encrypt(key, JSON.stringify(wallet())); + await assert.rejects( + prepareRecoveryImage( + key, f.app.id, + { appId: f.app.id, wallets: { first: encrypted, second: encrypted }, signers: {}, nodes: [] }, + [], [] + ), + (error) => error instanceof image.BackupSnapshotValidationError && + error.message === 'Duplicate or unidentified backup record' + ); +}); + +for (const [name, keyedWallets] of [ + ['aliased wallet key', [['old-key', wallet('wallet')]]], + ['swapped wallet keys', [['one', wallet('two')], ['two', wallet('one')]]], +]) test(`inspection rejects ${name} while recovery can still salvage records`, async () => { + const options = {}; + const f = harness(options); + const key = encryption.generateEncryptionKey(f.app.primarySeed); + f.local.Wallet.push(...keyedWallets.map(([, record]) => record)); + const appImage = { + appId: f.app.id, + wallets: Object.fromEntries(keyedWallets.map(([storedId, record]) => + [storedId, encryption.encrypt(key, JSON.stringify(record))] + )), + signers: {}, nodes: [], vaults: [], labels: [], + }; + options.response = { revision: 'a'.repeat(64), appImage, allVaultImages: [], labels: [] }; + assert.equal(await f.run(), 'unverified'); + assert.deepEqual(f.calls, ['getBackupSnapshot']); + assert.deepEqual(f.diagnostics, [['Assisted Server Backup check failed at stage:', 'snapshot-validation', 'non-transport']]); + + const { prepareRecoveryImage } = loadModule('src/services/backup/restore.ts', { './image': image }); + const recovered = await prepareRecoveryImage(key, f.app.id, appImage, [], []); + assert.deepEqual(Object.keys(recovered.wallets).sort(), keyedWallets.map(([, record]) => record.id).sort()); +}); + +test('numeric decrypted wallet ID is invalid for inspection and recovery', async () => { + const options = {}; + const f = harness(options); + const key = encryption.generateEncryptionKey(f.app.primarySeed); + const appImage = { + appId: f.app.id, + wallets: { wallet: encryption.encrypt(key, JSON.stringify({ ...wallet(), id: 42 })) }, + signers: {}, nodes: [], vaults: [], labels: [], + }; + options.response = { revision: 'a'.repeat(64), appImage, allVaultImages: [], labels: [] }; + assert.equal(await f.run(), 'unverified'); + assert.deepEqual(f.calls, ['getBackupSnapshot']); + assert.deepEqual(f.diagnostics, [['Assisted Server Backup check failed at stage:', 'snapshot-validation', 'non-transport']]); + + const { prepareRecoveryImage } = loadModule('src/services/backup/restore.ts', { './image': image }); + await assert.rejects( + prepareRecoveryImage(key, f.app.id, appImage, [], []), + (error) => error instanceof image.BackupSnapshotValidationError + ); +}); + test('transient snapshot failure after account switch makes no retry or upload', async () => { const f = harness({ post: async ({ app }) => { app.id = 'second'; From f4464b4cf8c79681ac30ff0a24eae38dbbc756ad Mon Sep 17 00:00:00 2001 From: Anant Date: Thu, 8 Oct 2026 17:33:33 +0530 Subject: [PATCH 055/254] ci: build diagnostic Android artifacts and clarify USDT history --- .../workflows/android-diagnostic-build.yml | 105 ++++++++++++++++++ reproducibility/README.md | 15 ++- scripts/verify-channel-config.cjs | 12 +- scripts/verify-channel-config.test.cjs | 17 ++- src/components/TransactionElement.tsx | 39 ++++--- src/screens/USDT/UsdtTransactionDetail.tsx | 13 ++- src/screens/USDT/UsdtTransactionHistory.tsx | 27 ++++- .../wallets/factories/USDTWalletFactory.ts | 18 ++- tests/services/usdtPause.test.cjs | 20 +++- 9 files changed, 237 insertions(+), 29 deletions(-) create mode 100644 .github/workflows/android-diagnostic-build.yml diff --git a/.github/workflows/android-diagnostic-build.yml b/.github/workflows/android-diagnostic-build.yml new file mode 100644 index 0000000000..ebac2a51eb --- /dev/null +++ b/.github/workflows/android-diagnostic-build.yml @@ -0,0 +1,105 @@ +name: Android diagnostic build + +on: + pull_request: + branches: + - release/2.6.3 + - sprint + - dev + - main + workflow_dispatch: + +permissions: + contents: read + +concurrency: + group: android-diagnostic-${{ github.event.pull_request.number || github.ref }} + cancel-in-progress: true + +jobs: + build: + runs-on: ubuntu-24.04 + timeout-minutes: 90 + + steps: + - name: Check out source + uses: actions/checkout@v4 + + - name: Set up Node + uses: actions/setup-node@v4 + with: + node-version: 22.23.3 + + - name: Set up Java + uses: actions/setup-java@v4 + with: + distribution: temurin + java-version: '17' + + - name: Set up Android SDK tools + uses: android-actions/setup-android@v4 + with: + packages: '' + + - name: Install Android build packages + run: sdkmanager 'platforms;android-36' 'build-tools;35.0.0' 'ndk;27.1.12297006' 'cmake;3.22.1' + + - name: Install JavaScript dependencies without the iOS setup hook + run: | + npm install --global yarn@1.22.22 + yarn install --frozen-lockfile --ignore-scripts --non-interactive + ./node_modules/.bin/patch-package + ./node_modules/.bin/rn-nodeify --install buffer,events,process,stream,inherits,path,assert,crypto --hack --yarn + + - name: Create diagnostic configuration + run: | + cat > "$RUNNER_TEMP/keeper-diagnostic.env" <<'EOF' + CHANNEL_URL=https://channel.bitcoinkeeper.app/ + ENVIRONMENT=PRODUCTION + HEXA_ID_MAINNET=0000000000000000000000000000000000000000000000000000000000000000 + HEXA_ID_TESTNET=0000000000000000000000000000000000000000000000000000000000000000 + RELAY=https://example.invalid/ + SIGNING_SERVER_MAINNET=https://example.invalid/ + SIGNING_SERVER_TESTNET=https://example.invalid/ + EOF + node scripts/verify-channel-config.cjs "$RUNNER_TEMP/keeper-diagnostic.env" + printf 'sdk.dir=%s\n' "$ANDROID_SDK_ROOT" > android/local.properties + + - name: Confirm Gradle rejects a retired mobile credential + run: | + cp "$RUNNER_TEMP/keeper-diagnostic.env" "$RUNNER_TEMP/keeper-rejected.env" + printf 'GASFREE_API_KEY=diagnostic-only\n' >> "$RUNNER_TEMP/keeper-rejected.env" + if (cd android && ENVFILE="$RUNNER_TEMP/keeper-rejected.env" ./gradlew :app:help --no-daemon --console=plain > "$RUNNER_TEMP/rejected-build.log" 2>&1); then + echo 'Gradle accepted a retired mobile credential' >&2 + exit 1 + fi + if ! grep -q 'Retired API credentials must not be included in an Android build' "$RUNNER_TEMP/rejected-build.log"; then + tail -80 "$RUNNER_TEMP/rejected-build.log" + exit 1 + fi + + - name: Build APK and AAB with the public debug key + env: + STORE_PASSWORD: android + KEY_ALIAS: androiddebugkey + KEY_PASSWORD: android + run: | + export ENVFILE="$RUNNER_TEMP/keeper-diagnostic.env" + export KEEPER_ANDROID_KEYSTORE="$GITHUB_WORKSPACE/android/app/debug.keystore" + cd android + ./gradlew :app:assembleProductionRelease :app:bundleProductionRelease --no-daemon --console=plain + + - name: Check diagnostic artifacts + run: | + apk=android/app/build/outputs/apk/production/release/app-production-release.apk + aab=android/app/build/outputs/bundle/productionRelease/app-production-release.aab + test -s "$apk" + test -s "$aab" + "$ANDROID_SDK_ROOT/build-tools/35.0.0/apksigner" verify "$apk" + badging=$("$ANDROID_SDK_ROOT/build-tools/35.0.0/aapt2" dump badging "$apk") + grep -q "package: name='io.hexawallet.bitcoinkeeper' versionCode='626' versionName='2.6.3'" <<< "$badging" + jarsigner -verify "$aab" >/dev/null + unzip -tq "$apk" >/dev/null + unzip -tq "$aab" >/dev/null + sha256sum "$apk" "$aab" + echo 'Diagnostic APK/AAB built with placeholder config and debug signing; no reproducibility verdict.' diff --git a/reproducibility/README.md b/reproducibility/README.md index 6e92902d53..32e7f0eb42 100644 --- a/reproducibility/README.md +++ b/reproducibility/README.md @@ -6,6 +6,14 @@ release with a build from its public source. The comparison tool in inspection aid. It is **not** a WalletScrutiny build script, and this repository does not yet establish a reproducibility verdict for v2.6.3. +The [Android diagnostic build workflow](../.github/workflows/android-diagnostic-build.yml) +is set up to compile an APK and AAB from the combined draft with the live public +channel URL and `ENVIRONMENT=PRODUCTION`, dummy service endpoints and IDs, and +the checked-in debug key. It also checks that Gradle rejects a retired GasFree +credential. A successful run would verify that the draft compiles in a clean CI +checkout; its outputs cannot be compared to a production release for a +reproducibility verdict. The workflow does not publish the diagnostic artifacts. + ## v2.6.3 reference and measured result The public source tag is [`v2.6.3`](https://github.com/KeeperCommunity/bitcoin-keeper/releases/tag/v2.6.3), @@ -145,9 +153,10 @@ python3 reproducibility/verify-android-inputs.py \ The tracked manifest contains seven source-audited environment names and no values. This is a **provisional future-release allowlist**: the GasFree pause -in draft PR #7037 and integration checks for four candidate omissions must be -reviewed before using it for a release. Changing the list requires a tracked -source change. The checker rejects +is included in [combined draft PR #7035](https://github.com/KeeperCommunity/bitcoin-keeper/pull/7035), +and integration checks for four candidate omissions must be reviewed before +using it for a release. Changing the list requires a tracked source change. +The checker rejects missing or extra names, a dirty or wrong source checkout, changed dependency hashes, mismatched tool versions, missing SDK packages, a symlinked `node_modules` root, and links from dependencies to files outside the checkout. diff --git a/scripts/verify-channel-config.cjs b/scripts/verify-channel-config.cjs index 401bd7be0e..cee7d7ffdc 100644 --- a/scripts/verify-channel-config.cjs +++ b/scripts/verify-channel-config.cjs @@ -22,4 +22,14 @@ if (matches.length !== 1 || matches[0] !== expected) { throw new Error(`Production CHANNEL_URL must be ${expected}`); } -console.log('Production channel URL verified'); +const stages = envContents + .split(/\r?\n/) + .map(line => line.match(/^\s*(?:export\s+)?ENVIRONMENT\s*=\s*(.*?)\s*$/)) + .filter(Boolean) + .map(match => match[1].replace(/^['"]|['"]$/g, '')); + +if (stages.length !== 1 || stages[0] !== 'PRODUCTION') { + throw new Error('Production ENVIRONMENT must be PRODUCTION'); +} + +console.log('Production channel URL and environment verified'); diff --git a/scripts/verify-channel-config.test.cjs b/scripts/verify-channel-config.test.cjs index 844253bc9c..adf9ba3d76 100644 --- a/scripts/verify-channel-config.test.cjs +++ b/scripts/verify-channel-config.test.cjs @@ -7,7 +7,7 @@ const { spawnSync } = require('node:child_process'); const verifier = path.join(__dirname, 'verify-channel-config.cjs'); const prebuild = path.join(__dirname, '..', 'appcenter-pre-build.sh'); -const channel = 'CHANNEL_URL=https://channel.bitcoinkeeper.app/\n'; +const channel = 'CHANNEL_URL=https://channel.bitcoinkeeper.app/\nENVIRONMENT=PRODUCTION\n'; function withTempDirectory(run) { const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'keeper-usdt-env-')); @@ -26,6 +26,21 @@ test('release env accepts reviewed channel without retired credentials', () => assert.equal(result.status, 0, result.stderr); })); +for (const [caseName, stage] of [ + ['missing', ''], + ['development', 'ENVIRONMENT=DEVELOPMENT\n'], + ['duplicate', 'ENVIRONMENT=PRODUCTION\nENVIRONMENT=PRODUCTION\n'], +]) { + test(`release env rejects ${caseName} production stage`, () => + withTempDirectory((directory) => { + const envFile = path.join(directory, '.env.production'); + fs.writeFileSync(envFile, `CHANNEL_URL=https://channel.bitcoinkeeper.app/\n${stage}`); + const result = spawnSync(process.execPath, [verifier, envFile], { encoding: 'utf8' }); + assert.notEqual(result.status, 0); + assert.match(result.stderr, /Production ENVIRONMENT must be PRODUCTION/); + })); +} + for (const name of ['GASFREE_API_KEY', 'GASFREE_API_SECRET', 'RN_GASFREE_API_KEY', 'LETS_EXCHANGE_API_KEY', 'RN_LETS_EXCHANGE_API_KEY']) { test(`release env rejects ${name} without logging its value`, () => diff --git a/src/components/TransactionElement.tsx b/src/components/TransactionElement.tsx index 411e9b2c4b..2f58433746 100644 --- a/src/components/TransactionElement.tsx +++ b/src/components/TransactionElement.tsx @@ -81,6 +81,7 @@ function TransactionElement({ )} - {isCached ? ( + {legacyUSDTRequest ? null : isCached ? ( ) : transactionType === 'Received' ? ( colorMode === 'light' ? ( @@ -112,17 +113,19 @@ function TransactionElement({ - {labels[transactionId]?.[0]?.name || transactionId} + {legacyUSDTRequest + ? translations.usdtWalletText.legacyRequestTitle + : labels[transactionId]?.[0]?.name || transactionId} {formattedDate} {legacyUSDTRequest && ( - + {translations.usdtWalletText.statusUnavailable} )} @@ -136,15 +139,17 @@ function TransactionElement({ - + {!legacyUSDTRequest && ( + + )} {colorMode === 'dark' ? : } @@ -158,11 +163,17 @@ const styles = StyleSheet.create({ container: { flexDirection: 'row', borderRadius: 10, - height: hp(76), justifyContent: 'space-between', alignItems: 'center', borderBottomWidth: 1, }, + standardContainer: { + height: hp(76), + }, + legacyContainer: { + minHeight: hp(76), + paddingVertical: hp(8), + }, rowCenter: { marginHorizontal: wp(10), flexDirection: 'row', diff --git a/src/screens/USDT/UsdtTransactionDetail.tsx b/src/screens/USDT/UsdtTransactionDetail.tsx index 0b6371756e..ca42819ab1 100644 --- a/src/screens/USDT/UsdtTransactionDetail.tsx +++ b/src/screens/USDT/UsdtTransactionDetail.tsx @@ -123,12 +123,14 @@ const UsdtTransactionDetail = ({ route }) => { - {transactionType === 'Received' ? ( + {legacyRequest ? null : transactionType === 'Received' ? ( colorMode === 'dark' ? ( ) : ( @@ -149,6 +151,11 @@ const UsdtTransactionDetail = ({ route }) => { + {legacyRequest && ( + + {usdtWalletText.requestedAmount} + + )} {amount} USDT diff --git a/src/screens/USDT/UsdtTransactionHistory.tsx b/src/screens/USDT/UsdtTransactionHistory.tsx index 88f2f4e8e1..a3830788ed 100644 --- a/src/screens/USDT/UsdtTransactionHistory.tsx +++ b/src/screens/USDT/UsdtTransactionHistory.tsx @@ -1,13 +1,15 @@ -import { Box, FlatList, StatusBar, useColorMode } from '@gluestack-ui/themed-native-base'; +import { Box, StatusBar, useColorMode } from '@gluestack-ui/themed-native-base'; import React, { useContext } from 'react'; -import { StyleSheet } from 'react-native'; +import { SectionList, StyleSheet } from 'react-native'; import EmptyStateView from 'src/components/EmptyView/EmptyStateView'; import TransactionElement from 'src/components/TransactionElement'; +import Text from 'src/components/KeeperText'; import WalletHeader from 'src/components/WalletHeader'; import { hp, wp } from 'src/constants/responsive'; import { LocalizationContext } from 'src/context/Localization/LocContext'; import NoTransactionIcon from 'src/assets/images/noTransaction.svg'; import { USDTTransaction } from 'src/services/wallets/operations/dollars/USDT'; +import { partitionUSDTWalletHistory } from 'src/services/wallets/factories/USDTWalletFactory'; import { CommonActions, useNavigation } from '@react-navigation/native'; const UsdtTransactionHistory = ({ route }) => { @@ -16,6 +18,11 @@ const UsdtTransactionHistory = ({ route }) => { const { translations } = useContext(LocalizationContext); const { common, usdtWalletText } = translations; const navigation = useNavigation(); + const { onChainTransactions, earlierRequests } = partitionUSDTWalletHistory(transactions); + const sections = [ + { title: '', data: onChainTransactions }, + { title: usdtWalletText.legacyRequestTitle, data: earlierRequests }, + ].filter((section) => section.data.length > 0); const renderTransactionElement = ({ item }) => ( { - } - data={transactions} + sections={sections} + renderSectionHeader={({ section }) => + section.title ? ( + + {section.title} + + ) : null + } renderItem={renderTransactionElement} keyExtractor={(item: USDTTransaction) => item.txId || item.traceId} showsVerticalScrollIndicator={false} @@ -84,4 +97,8 @@ const styles = StyleSheet.create({ contentContainerStyle: { paddingBottom: hp(100), }, + sectionTitle: { + paddingTop: hp(18), + paddingBottom: hp(8), + }, }); diff --git a/src/services/wallets/factories/USDTWalletFactory.ts b/src/services/wallets/factories/USDTWalletFactory.ts index 9543d2b8e8..09f72f386f 100644 --- a/src/services/wallets/factories/USDTWalletFactory.ts +++ b/src/services/wallets/factories/USDTWalletFactory.ts @@ -4,7 +4,11 @@ import { createTronWalletFromMnemonic, DEFAULT_TRON_DERIVATION_PATH, } from '../operations/dollars/Tron'; -import USDT, { USDTAccountStatus, USDTTransaction } from '../operations/dollars/USDT'; +import USDT, { + isHistoricalUnverifiedUSDTRequest, + USDTAccountStatus, + USDTTransaction, +} from '../operations/dollars/USDT'; import BIP85 from '../operations/BIP85'; import { BIP85Config } from '../interfaces'; import { GasFreeTransferStatus } from '../operations/dollars/GasFree'; @@ -28,6 +32,18 @@ export interface USDTWalletSpecs { lastSynched: number; // Last sync timestamp } +/** Keep unverified provider requests visible without counting them as chain transfers. */ +export const partitionUSDTWalletHistory = (transactions: USDTTransaction[]) => { + const onChainTransactions: USDTTransaction[] = []; + const earlierRequests: USDTTransaction[] = []; + for (const transaction of transactions) { + (isHistoricalUnverifiedUSDTRequest(transaction) ? earlierRequests : onChainTransactions).push( + transaction + ); + } + return { onChainTransactions, earlierRequests }; +}; + export interface USDTWalletPresentationData { name: string; // Custom wallet name description: string; // Custom description diff --git a/tests/services/usdtPause.test.cjs b/tests/services/usdtPause.test.cjs index 133372ad7b..e74fde1ef7 100644 --- a/tests/services/usdtPause.test.cjs +++ b/tests/services/usdtPause.test.cjs @@ -102,7 +102,11 @@ const factory = loadTypeScript('src/services/wallets/factories/USDTWalletFactory VisibilityType: { DEFAULT: 'DEFAULT' }, }, '../operations/dollars/Tron': {}, - '../operations/dollars/USDT': { __esModule: true, default: service }, + '../operations/dollars/USDT': { + __esModule: true, + default: service, + isHistoricalUnverifiedUSDTRequest: usdtModule.isHistoricalUnverifiedUSDTRequest, + }, '../operations/BIP85': { __esModule: true, default: {} }, '../operations/dollars/GasFree': historical, 'src/utils/service-utilities/config': { @@ -138,6 +142,20 @@ test('stored address, chain reads and ambiguous historical request survive the p assert.equal(usdtModule.isHistoricalUnverifiedUSDTRequest(transactions[0]), false); assert.equal(usdtModule.isHistoricalUnverifiedUSDTRequest(transactions[1]), true); assert.equal(transactions[1].status, historical.GasFreeTransferStatus.WAITING); + const { onChainTransactions, earlierRequests } = factory.partitionUSDTWalletHistory(transactions); + assert.deepEqual(Array.from(onChainTransactions, (transaction) => transaction.txId), ['chain-1']); + assert.deepEqual(Array.from(earlierRequests, (transaction) => transaction.traceId), [ + 'historical-trace', + ]); + assert.equal(onChainTransactions.length + earlierRequests.length, transactions.length); + assert.equal( + usdtModule.isHistoricalUnverifiedUSDTRequest({ + traceId: 'old-request', + status: historical.GasFreeTransferStatus.SUCCEED, + }), + true, + 'a stored provider status is not independent proof of an on-chain transfer' + ); }); test('missing saved address is never replaced by the owner address', async () => { From 9ef26da70748fafae27a65dd431fbf2948ab5d7a Mon Sep 17 00:00:00 2001 From: Anant Date: Thu, 8 Oct 2026 17:35:46 +0530 Subject: [PATCH 056/254] ci: read diagnostic artifact version from release manifest --- .github/workflows/android-diagnostic-build.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/android-diagnostic-build.yml b/.github/workflows/android-diagnostic-build.yml index ebac2a51eb..2099b71658 100644 --- a/.github/workflows/android-diagnostic-build.yml +++ b/.github/workflows/android-diagnostic-build.yml @@ -97,7 +97,9 @@ jobs: test -s "$aab" "$ANDROID_SDK_ROOT/build-tools/35.0.0/apksigner" verify "$apk" badging=$("$ANDROID_SDK_ROOT/build-tools/35.0.0/aapt2" dump badging "$apk") - grep -q "package: name='io.hexawallet.bitcoinkeeper' versionCode='626' versionName='2.6.3'" <<< "$badging" + version=$(node -p 'require("./release/version.json").version') + version_code=$(node -p 'require("./release/version.json").androidVersionCode') + grep -Fq "package: name='io.hexawallet.bitcoinkeeper' versionCode='$version_code' versionName='$version'" <<< "$badging" jarsigner -verify "$aab" >/dev/null unzip -tq "$apk" >/dev/null unzip -tq "$aab" >/dev/null From 468138c45101600ec12e2d2a639e24592ec81c1f Mon Sep 17 00:00:00 2001 From: Anant Date: Thu, 8 Oct 2026 17:40:28 +0530 Subject: [PATCH 057/254] fix: keep USDT request history chronological and avoid unverified fees --- src/screens/USDT/UsdtTransactionDetail.tsx | 4 +-- src/screens/USDT/UsdtTransactionHistory.tsx | 27 ++++--------------- .../wallets/factories/USDTWalletFactory.ts | 18 +------------ tests/services/usdtPause.test.cjs | 12 +-------- 4 files changed, 9 insertions(+), 52 deletions(-) diff --git a/src/screens/USDT/UsdtTransactionDetail.tsx b/src/screens/USDT/UsdtTransactionDetail.tsx index ca42819ab1..2060fd3dc5 100644 --- a/src/screens/USDT/UsdtTransactionDetail.tsx +++ b/src/screens/USDT/UsdtTransactionDetail.tsx @@ -249,7 +249,7 @@ const UsdtTransactionDetail = ({ route }) => { showIcon={false} letterSpacing={2.4} /> - {transaction.transferFee || transaction.fee ? ( + {!legacyRequest && (transaction.transferFee || transaction.fee) ? ( { letterSpacing={2.4} /> ) : null} - {transaction.activateFee ? ( + {!legacyRequest && transaction.activateFee ? ( { @@ -18,11 +16,6 @@ const UsdtTransactionHistory = ({ route }) => { const { translations } = useContext(LocalizationContext); const { common, usdtWalletText } = translations; const navigation = useNavigation(); - const { onChainTransactions, earlierRequests } = partitionUSDTWalletHistory(transactions); - const sections = [ - { title: '', data: onChainTransactions }, - { title: usdtWalletText.legacyRequestTitle, data: earlierRequests }, - ].filter((section) => section.data.length > 0); const renderTransactionElement = ({ item }) => ( { - - section.title ? ( - - {section.title} - - ) : null - } + // refreshControl={} + data={transactions} renderItem={renderTransactionElement} keyExtractor={(item: USDTTransaction) => item.txId || item.traceId} showsVerticalScrollIndicator={false} @@ -97,8 +84,4 @@ const styles = StyleSheet.create({ contentContainerStyle: { paddingBottom: hp(100), }, - sectionTitle: { - paddingTop: hp(18), - paddingBottom: hp(8), - }, }); diff --git a/src/services/wallets/factories/USDTWalletFactory.ts b/src/services/wallets/factories/USDTWalletFactory.ts index 09f72f386f..9543d2b8e8 100644 --- a/src/services/wallets/factories/USDTWalletFactory.ts +++ b/src/services/wallets/factories/USDTWalletFactory.ts @@ -4,11 +4,7 @@ import { createTronWalletFromMnemonic, DEFAULT_TRON_DERIVATION_PATH, } from '../operations/dollars/Tron'; -import USDT, { - isHistoricalUnverifiedUSDTRequest, - USDTAccountStatus, - USDTTransaction, -} from '../operations/dollars/USDT'; +import USDT, { USDTAccountStatus, USDTTransaction } from '../operations/dollars/USDT'; import BIP85 from '../operations/BIP85'; import { BIP85Config } from '../interfaces'; import { GasFreeTransferStatus } from '../operations/dollars/GasFree'; @@ -32,18 +28,6 @@ export interface USDTWalletSpecs { lastSynched: number; // Last sync timestamp } -/** Keep unverified provider requests visible without counting them as chain transfers. */ -export const partitionUSDTWalletHistory = (transactions: USDTTransaction[]) => { - const onChainTransactions: USDTTransaction[] = []; - const earlierRequests: USDTTransaction[] = []; - for (const transaction of transactions) { - (isHistoricalUnverifiedUSDTRequest(transaction) ? earlierRequests : onChainTransactions).push( - transaction - ); - } - return { onChainTransactions, earlierRequests }; -}; - export interface USDTWalletPresentationData { name: string; // Custom wallet name description: string; // Custom description diff --git a/tests/services/usdtPause.test.cjs b/tests/services/usdtPause.test.cjs index e74fde1ef7..a16c7a58c3 100644 --- a/tests/services/usdtPause.test.cjs +++ b/tests/services/usdtPause.test.cjs @@ -102,11 +102,7 @@ const factory = loadTypeScript('src/services/wallets/factories/USDTWalletFactory VisibilityType: { DEFAULT: 'DEFAULT' }, }, '../operations/dollars/Tron': {}, - '../operations/dollars/USDT': { - __esModule: true, - default: service, - isHistoricalUnverifiedUSDTRequest: usdtModule.isHistoricalUnverifiedUSDTRequest, - }, + '../operations/dollars/USDT': { __esModule: true, default: service }, '../operations/BIP85': { __esModule: true, default: {} }, '../operations/dollars/GasFree': historical, 'src/utils/service-utilities/config': { @@ -142,12 +138,6 @@ test('stored address, chain reads and ambiguous historical request survive the p assert.equal(usdtModule.isHistoricalUnverifiedUSDTRequest(transactions[0]), false); assert.equal(usdtModule.isHistoricalUnverifiedUSDTRequest(transactions[1]), true); assert.equal(transactions[1].status, historical.GasFreeTransferStatus.WAITING); - const { onChainTransactions, earlierRequests } = factory.partitionUSDTWalletHistory(transactions); - assert.deepEqual(Array.from(onChainTransactions, (transaction) => transaction.txId), ['chain-1']); - assert.deepEqual(Array.from(earlierRequests, (transaction) => transaction.traceId), [ - 'historical-trace', - ]); - assert.equal(onChainTransactions.length + earlierRequests.length, transactions.length); assert.equal( usdtModule.isHistoricalUnverifiedUSDTRequest({ traceId: 'old-request', From 16a1a3f6c402e437f01e1f2204b3cdf438dd8907 Mon Sep 17 00:00:00 2001 From: Anant Date: Thu, 8 Oct 2026 17:40:58 +0530 Subject: [PATCH 058/254] fix: return scanner child without redundant fragment --- src/services/channel/ChannelRequestScanner.tsx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/services/channel/ChannelRequestScanner.tsx b/src/services/channel/ChannelRequestScanner.tsx index e41c6c42d3..a4b68a9e99 100644 --- a/src/services/channel/ChannelRequestScanner.tsx +++ b/src/services/channel/ChannelRequestScanner.tsx @@ -1,4 +1,4 @@ -import React, { ReactNode, useRef, useState } from 'react'; +import React, { ReactElement, useRef, useState } from 'react'; import QRScanner from 'src/components/QRScanner'; // Keep the scanner available until the request has actually been prepared and sent. @@ -7,7 +7,7 @@ export default function ChannelRequestScanner({ children, }: { onScanCompleted: (data: string) => boolean | Promise; - children: ReactNode; + children: ReactElement; }) { const [requestSent, setRequestSent] = useState(false); const [scanAttempt, setScanAttempt] = useState(0); @@ -28,5 +28,5 @@ export default function ChannelRequestScanner({ preparing.current = false; } }; - return requestSent ? <>{children} : ; + return requestSent ? children : ; } From 1370b99be49e48143ed9a685267ebd8d90cd6327 Mon Sep 17 00:00:00 2001 From: Anant Date: Thu, 8 Oct 2026 17:41:21 +0530 Subject: [PATCH 059/254] fix(backup): repair vault registration metadata from persisted keys --- src/services/backup/image.ts | 12 ++- src/services/backup/repair.ts | 15 +++ tests/recovery/helpers.cjs | 2 +- tests/recovery/vaultRegistration.test.cjs | 112 ++++++++++++++++++++++ 4 files changed, 139 insertions(+), 2 deletions(-) create mode 100644 tests/recovery/vaultRegistration.test.cjs diff --git a/src/services/backup/image.ts b/src/services/backup/image.ts index a98e0dfa9a..cae238c219 100644 --- a/src/services/backup/image.ts +++ b/src/services/backup/image.ts @@ -102,6 +102,12 @@ function vaultSchemeIdentity(scheme: any) { }; } +function vaultSignerIdentity(signer: any) { + if (!signer || typeof signer !== 'object') return signer; + const { registeredVaults, ...key } = signer; + return key; +} + function identity(kind: keyof BackupImage, record: any) { if (kind === 'wallets') return { @@ -119,7 +125,11 @@ function identity(kind: keyof BackupImage, record: any) { return { networkType: record.networkType, scheme: vaultSchemeIdentity(record.scheme), - signers: record.signers, + // Registration receipts and device HMACs can change without changing the + // vault's keys or spending policy. They remain in encrypted recovery data. + signers: Array.isArray(record.signers) + ? record.signers.map(vaultSignerIdentity) + : record.signers, scriptType: record.scriptType, }; if (kind === 'signers') diff --git a/src/services/backup/repair.ts b/src/services/backup/repair.ts index 5d35580e08..2bd77a5e87 100644 --- a/src/services/backup/repair.ts +++ b/src/services/backup/repair.ts @@ -61,6 +61,15 @@ const clone = (value: any) => JSON.parse(JSON.stringify(value)); export async function captureBackupImage(checkpoint = () => {}): Promise { const image: BackupImage = { wallets: {}, signers: {}, vaults: {}, nodes: {}, labels: {} }; + // A VaultSigner is shared across the vaults that use its xpub. Read the + // persisted registration value, including a value whose incremental upload + // failed, and carry it into every affected encrypted vault record. + const registrations = new Map(); + const vaultSigners = dbManager.getObjectByIndex(RealmSchema.VaultSigner, null, true) as any; + for (const value of vaultSigners) { + checkpoint(); + registrations.set(value.xpub, clone(value.registeredVaults)); + } const sources: [keyof BackupImage, RealmSchema][] = [ ['wallets', RealmSchema.Wallet], ['wallets', RealmSchema.USDTWallet], @@ -77,6 +86,12 @@ export async function captureBackupImage(checkpoint = () => {}): Promise MAX_RECORD_BYTES) throw new Error('Backup record exceeds supported size'); const raw = JSON.parse(text); + if (kind === 'vaults') + raw.signers = raw.signers.map((signer) => + registrations.has(signer.xpub) + ? { ...signer, registeredVaults: registrations.get(signer.xpub) } + : signer + ); const record = kind === 'signers' ? sanitizeSeedKeyForBackup(raw) diff --git a/tests/recovery/helpers.cjs b/tests/recovery/helpers.cjs index 99e48d7bf9..6d0b4e8e87 100644 --- a/tests/recovery/helpers.cjs +++ b/tests/recovery/helpers.cjs @@ -17,7 +17,7 @@ const image = loadModule('src/services/backup/image.ts', { 'src/utils/service-ut const makeImage = () => ({ wallets: {}, signers: {}, vaults: {}, nodes: {}, labels: {} }); function harness(options = {}) { const app = { id: 'disposable', publicId: 'fixture', primarySeed: 'ab'.repeat(32), subscription: { level: 1 }, version: '2.5.16' }; - const local = { Wallet: [], USDTWallet: [], Signer: [], Vault: [], NodeConnect: [], Tags: [] }; + const local = { Wallet: [], USDTWallet: [], Signer: [], Vault: [], VaultSigner: [], NodeConnect: [], Tags: [] }; const remote = { appId: app.id, wallets: {}, signers: {}, nodes: [], vaults: [], labels: [] }; let vaults = [], labels = []; const revision = () => require('node:crypto').createHash('sha256').update(JSON.stringify({remote,vaults,labels})).digest('hex'); diff --git a/tests/recovery/vaultRegistration.test.cjs b/tests/recovery/vaultRegistration.test.cjs new file mode 100644 index 0000000000..c31762bddb --- /dev/null +++ b/tests/recovery/vaultRegistration.test.cjs @@ -0,0 +1,112 @@ +const assert = require('node:assert/strict'); +const { test } = require('node:test'); +const { encryption, harness, image, makeImage } = require('./helpers.cjs'); + +const clone = (value) => JSON.parse(JSON.stringify(value)); +const key = { + masterFingerprint: 'AABBCCDD', + xfp: 'AABBCCDD', + xpub: 'disposable-xpub', + derivationPath: "m/48'/1'/0'/2'", + registeredVaults: [{ vaultId: 'first', registered: false }], +}; +const vault = (id) => ({ + id, + networkType: 'TESTNET', + scriptType: 'P2WSH', + scheme: { m: 1, n: 1 }, + signers: [clone(key)], + specs: {}, +}); + +function registrationFixture({ incompleteReadback = false } = {}) { + const state = { + appImage: { + appId: 'disposable', wallets: {}, signers: {}, nodes: [], + vaults: ['first', 'second'], labels: [], + }, + allVaultImages: [], labels: [], revision: 'a'.repeat(64), + }; + let uploads = 0; + const f = harness({ adapter: async (endpoint, payload) => { + if (endpoint === 'getBackupSnapshot') return { data: clone(state) }; + assert.equal(endpoint, 'repairAppBackup'); + assert.equal(payload.expectedRevision, state.revision); + assert.equal(payload.replaceCurrentState, true); + uploads++; + Object.assign(state.appImage, { + wallets: payload.walletObject, + signers: payload.signersObject, + nodes: payload.nodes, + vaults: Object.keys(payload.vaultObject), + labels: payload.labels.map((label) => label.id), + }); + state.allVaultImages = Object.values(payload.vaultObject); + if (incompleteReadback) state.allVaultImages.pop(); + state.labels = payload.labels; + state.revision = 'b'.repeat(64); + return { data: { updated: true } }; + } }); + const cipherKey = encryption.generateEncryptionKey(f.app.primarySeed); + for (const id of state.appImage.vaults) { + const record = vault(id); + f.local.Vault.push(clone(record)); + state.allVaultImages.push({ + vaultId: id, vault: encryption.encrypt(cipherKey, JSON.stringify(record)), + isArchived: false, + }); + } + const registeredVaults = [ + { vaultId: 'first', registered: true, hmac: 'disposable-hmac' }, + { vaultId: 'second', registered: true, registrationInfo: 'disposable-info' }, + ]; + f.local.VaultSigner.push({ ...clone(key), registeredVaults }); + return { f, state, cipherKey, registeredVaults, uploads: () => uploads }; +} + +test('persisted registration repairs every affected vault only after explicit action and readback', async () => { + const { f, state, cipherKey, registeredVaults, uploads } = registrationFixture(); + assert.equal(await f.run(), 'different'); + assert.equal(uploads(), 0); + assert.deepEqual(f.calls, ['getBackupSnapshot']); + + assert.equal(await f.run(true), 'verified'); + assert.equal(uploads(), 1); + assert.deepEqual(f.calls, [ + 'getBackupSnapshot', 'getBackupSnapshot', 'repairAppBackup', 'getBackupSnapshot', + ]); + for (const encrypted of state.allVaultImages) { + const record = JSON.parse(encryption.decrypt(cipherKey, encrypted.vault)); + assert.deepEqual(record.signers[0].registeredVaults, registeredVaults); + assert.equal(record.signers[0].xpub, key.xpub); + } + // Repair uses persisted metadata in its image without mutating vault records. + for (const record of f.local.Vault) + assert.deepEqual(record.signers[0].registeredVaults, key.registeredVaults); +}); + +test('an incomplete registration repair readback cannot be marked verified', async () => { + const { f, uploads } = registrationFixture({ incompleteReadback: true }); + assert.equal(await f.run(true), 'unverified'); + assert.equal(uploads(), 1); + assert.deepEqual(f.calls, ['getBackupSnapshot', 'repairAppBackup', 'getBackupSnapshot']); +}); + +test('registration-only drift is a difference while vault key and policy drift remain conflicts', async () => { + const local = makeImage(), remote = makeImage(); + local.vaults.first = vault('first'); + remote.vaults.first = clone(local.vaults.first); + local.vaults.first.signers[0].registeredVaults[0].registered = true; + assert.equal(await image.compareImages(local, remote), 'different'); + for (const mutate of [ + (record) => { record.signers[0].xpub = 'changed-xpub'; }, + (record) => { record.signers[0].derivationPath = "m/48'/1'/1'/2'"; }, + (record) => { record.networkType = 'MAINNET'; }, + (record) => { record.scriptType = 'P2SH'; }, + (record) => { record.scheme.m = 2; }, + ]) { + const changed = clone(local); + mutate(changed.vaults.first); + assert.equal(await image.compareImages(changed, remote), 'conflict'); + } +}); From a8d1270d3277224a7adb6e6eea35fc6bed79544a Mon Sep 17 00:00:00 2001 From: Anant Date: Thu, 8 Oct 2026 17:47:34 +0530 Subject: [PATCH 060/254] ci: run checks on 2.6.4 release branch --- .github/workflows/codeql.yml | 2 ++ .github/workflows/test.yml | 2 ++ 2 files changed, 4 insertions(+) diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index a9dbb36c04..feee5b9c70 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -6,11 +6,13 @@ on: - sprint - dev - main + - release/2.6.4 pull_request: branches: - sprint - dev - main + - release/2.6.4 permissions: actions: read diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 8a5680c736..5c0e6e2a1b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -7,12 +7,14 @@ on: - dev - main - release/2.6.3 + - release/2.6.4 pull_request: branches: - sprint - dev - main - release/2.6.3 + - release/2.6.4 permissions: contents: read From 2ec20264f5486c7ce4ba657bf8250ce60f458817 Mon Sep 17 00:00:00 2001 From: Anant Date: Thu, 8 Oct 2026 17:47:47 +0530 Subject: [PATCH 061/254] Remove private backend revision IDs from public verification history --- dev/local-backend/VERIFICATION.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/dev/local-backend/VERIFICATION.md b/dev/local-backend/VERIFICATION.md index 559fe2f687..44a0d18ebe 100644 --- a/dev/local-backend/VERIFICATION.md +++ b/dev/local-backend/VERIFICATION.md @@ -118,10 +118,10 @@ Detailed findings remain private. ## Fresh backend readiness integration -Updated pins: relay `5f59876b43fba1be9091657a6bf3389ce43831d0`, signing -`5ad45119ab014ea58efecabd54702b488d8fd254`. New app worktree, newly fetched -backend sources and new Docker volumes; host image/package caches and existing -GitHub read access reused. Project `keeper-readiness-proof` used ports +This earlier private-backend readiness run used private Relay and SigningServer +revisions, separate from the current public source pins. New app worktree, newly +fetched backend sources and new Docker volumes; host image/package caches and +existing GitHub read access reused. Project `keeper-readiness-proof` used ports 23000/24002/23003, preserving other development projects. - Source fetch, adapter checksums/application and container builds: PASS. From 466b94f9eb901b521b2d4501fdc628bfe2beb39b Mon Sep 17 00:00:00 2001 From: Anant Date: Thu, 8 Oct 2026 17:55:10 +0530 Subject: [PATCH 062/254] fix(sync): retain archived vault monitoring and complete refresh feedback --- src/hooks/useWalletRefresh.ts | 59 ++++++ src/screens/Home/HomeScreen.tsx | 15 +- .../Home/components/Wallet/HomeWallet.tsx | 32 +-- src/screens/Vault/AllTransactions.tsx | 19 +- src/screens/Vault/VaultDetails.tsx | 8 +- .../Vault/components/TransactionsAndUTXOs.tsx | 13 +- .../ViewTransactions/TransactionHistory.tsx | 25 ++- src/screens/WalletDetails/WalletDetails.tsx | 14 +- .../WalletDetails/components/Transactions.tsx | 21 +- src/store/reducers/wallets.ts | 23 +++ src/store/sagaActions/wallets.ts | 10 +- src/store/sagas/walletRefreshCoordinator.js | 41 ++++ src/store/sagas/walletSyncSelection.js | 19 ++ src/store/sagas/wallets.ts | 182 ++++++++++++++---- tests/recovery/backupFreshness.test.cjs | 45 ++++- tests/recovery/sagaHarness.cjs | 6 + tests/sync/walletRefresh.test.cjs | 60 ++++++ 17 files changed, 486 insertions(+), 106 deletions(-) create mode 100644 src/hooks/useWalletRefresh.ts create mode 100644 src/store/sagas/walletRefreshCoordinator.js create mode 100644 src/store/sagas/walletSyncSelection.js create mode 100644 tests/sync/walletRefresh.test.cjs diff --git a/src/hooks/useWalletRefresh.ts b/src/hooks/useWalletRefresh.ts new file mode 100644 index 0000000000..f1ba509f7c --- /dev/null +++ b/src/hooks/useWalletRefresh.ts @@ -0,0 +1,59 @@ +import { useCallback, useEffect, useRef, useState } from 'react'; +import { useDispatch } from 'react-redux'; +import { Vault } from 'src/services/wallets/interfaces/vault'; +import { Wallet } from 'src/services/wallets/interfaces/wallet'; +import { useAppSelector } from 'src/store/hooks'; +import { + clearRefreshRequest, + startRefreshRequest, +} from 'src/store/reducers/wallets'; +import { autoSyncWallets, refreshWallets } from 'src/store/sagaActions/wallets'; + +let nextRefreshRequest = 0; + +export const useWalletRefresh = () => { + const dispatch = useDispatch(); + const [requestId, setRequestId] = useState(null); + const inFlight = useRef(null); + const status = useAppSelector((state) => + requestId ? state.wallet.refreshRequests[requestId] : undefined + ); + + useEffect(() => { + if (requestId && status !== 'pending') { + inFlight.current = null; + setRequestId(null); + dispatch(clearRefreshRequest(requestId)); + } + }, [dispatch, requestId, status]); + + useEffect(() => () => { + if (inFlight.current) dispatch(clearRefreshRequest(inFlight.current)); + }, [dispatch]); + + const start = useCallback( + (actionForRequest: (id: string) => { type: string; payload: any }) => { + if (inFlight.current) return; + const id = `wallet-refresh-${Date.now()}-${++nextRefreshRequest}`; + inFlight.current = id; + dispatch(startRefreshRequest(id)); + setRequestId(id); + dispatch(actionForRequest(id)); + }, + [dispatch] + ); + + const refresh = useCallback( + (wallets: (Wallet | Vault)[], options: { hardRefresh?: boolean; dustScan?: boolean }) => { + if (!wallets?.length || wallets.some((wallet) => !wallet)) return; + start((id) => refreshWallets(wallets, options, id)); + }, + [start] + ); + + const autoRefresh = useCallback(() => { + start((id) => autoSyncWallets(false, false, true, undefined, false, id)); + }, [start]); + + return { refreshing: !!requestId && status !== 'success' && status !== 'failure', refresh, autoRefresh }; +}; diff --git a/src/screens/Home/HomeScreen.tsx b/src/screens/Home/HomeScreen.tsx index 9640e147c5..bf3e16d4a4 100644 --- a/src/screens/Home/HomeScreen.tsx +++ b/src/screens/Home/HomeScreen.tsx @@ -1,4 +1,4 @@ -import { StyleSheet, TouchableOpacity, View } from 'react-native'; +import { AppState, StyleSheet, TouchableOpacity, View } from 'react-native'; import { Box, useColorMode } from '@gluestack-ui/themed-native-base'; import React, { useContext, useEffect, useRef, useState } from 'react'; import useWallets from 'src/hooks/useWallets'; @@ -31,6 +31,7 @@ import dbManager from 'src/storage/realm/dbManager'; import { setRecoveryKeyStatus } from 'src/store/reducers/account'; import RecoveryKeyIcon from 'src/assets/images/recover_white.svg'; import HelpAiEntry from '../HelpAi/HelpAiEntry'; +import { autoSyncWallets } from 'src/store/sagaActions/wallets'; function NewHomeScreen({ route }) { const { colorMode } = useColorMode(); @@ -52,6 +53,8 @@ function NewHomeScreen({ route }) { const backupHistory = useQuery(RealmSchema.BackupHistory); const { recoveryKeyStatusByAppId } = useAppSelector((state) => state.account); const { bitcoinNetworkType } = useAppSelector((state) => state.settings); + const isAuthenticated = useAppSelector((state) => state.login.isAuthenticated); + const appState = useRef(AppState.currentState); const { id } = dbManager.getObjectByIndex(RealmSchema.KeeperApp) as any; // 'idle' | 'education' | 'skipWarning' @@ -61,6 +64,16 @@ function NewHomeScreen({ route }) { // Session flag: prevent re-showing the education sheet after the user dismisses it const hasShownEducationSheetRef = useRef(false); + useEffect(() => { + const subscription = AppState.addEventListener('change', (nextState) => { + if (appState.current?.match(/inactive|background/) && nextState === 'active' && isAuthenticated) { + dispatch(autoSyncWallets(false, false, false, undefined, true)); + } + appState.current = nextState; + }); + return () => subscription.remove(); + }, [dispatch, isAuthenticated]); + const recoveryKeyStatus = recoveryKeyStatusByAppId?.[id]; const isConfirmed = recoveryKeyStatus === 'confirmed'; diff --git a/src/screens/Home/components/Wallet/HomeWallet.tsx b/src/screens/Home/components/Wallet/HomeWallet.tsx index f277a0b79a..e359467466 100644 --- a/src/screens/Home/components/Wallet/HomeWallet.tsx +++ b/src/screens/Home/components/Wallet/HomeWallet.tsx @@ -23,7 +23,7 @@ import CollaborativeWalletIcon from 'src/assets/images/collaborative_vault_white import { useAppSelector } from 'src/store/hooks'; import { resetCollaborativeSession } from 'src/store/reducers/vaults'; import { useDispatch } from 'react-redux'; -import { autoSyncWallets, refreshWallets } from 'src/store/sagaActions/wallets'; +import { useWalletRefresh } from 'src/hooks/useWalletRefresh'; import { RefreshControl } from 'react-native'; import { ELECTRUM_CLIENT } from 'src/services/electrum/client'; import ActivityIndicatorView from 'src/components/AppActivityIndicator/ActivityIndicatorView'; @@ -123,7 +123,8 @@ const HomeWallet = () => { const dispatch = useDispatch(); const [showAddWalletModal, setShowAddWalletModal] = useState(false); const [collabSessionExistsModalVisible, setCollabSessionExistsModalVisible] = useState(false); - const [pullRefresh, setPullRefresh] = useState(false); + const [navigateAfterSessionReset, setNavigateAfterSessionReset] = useState(false); + const { refreshing: pullRefresh, autoRefresh } = useWalletRefresh(); const { walletSyncing } = useAppSelector((state) => state.wallet); const [pickWalletType, setPickWalletType] = useState(false); const [createUsdtWallet, setCreateUsdtWallet] = useState(false); @@ -157,23 +158,29 @@ const HomeWallet = () => { } }, [pendingDustToast]); + useEffect(() => { + if (navigateAfterSessionReset && Object.keys(collaborativeSession.signers).length === 0) { + setNavigateAfterSessionReset(false); + navigation.navigate('SetupCollaborativeWallet'); + } + }, [navigateAfterSessionReset, collaborativeSession.signers, navigation]); + + const resetAndStartCollaborativeWallet = () => { + dispatch(resetCollaborativeSession()); + setNavigateAfterSessionReset(true); + }; + const handleCollaborativeWalletCreation = () => { setShowAddWalletModal(false); if (Object.keys(collaborativeSession.signers).length > 0) { setCollabSessionExistsModalVisible(true); } else { - dispatch(resetCollaborativeSession()); - setTimeout(() => { - navigation.navigate('SetupCollaborativeWallet'); - }, 500); // delaying navigation by 0.5 second to ensure collaborative session reset + resetAndStartCollaborativeWallet(); } }; const pullDownRefresh = () => { - setPullRefresh(true); - - dispatch(autoSyncWallets(false, false, true)); - setPullRefresh(false); + autoRefresh(); }; const importUSDTWallet = async (mnemonic) => { @@ -333,10 +340,7 @@ const HomeWallet = () => { secondaryButtonText={common.startNew} secondaryCallback={() => { setCollabSessionExistsModalVisible(false); - dispatch(resetCollaborativeSession()); - setTimeout(() => { - navigation.navigate('SetupCollaborativeWallet'); - }, 500); + resetAndStartCollaborativeWallet(); }} buttonCallback={() => { setCollabSessionExistsModalVisible(false); diff --git a/src/screens/Vault/AllTransactions.tsx b/src/screens/Vault/AllTransactions.tsx index ff3377922b..35638a64ea 100644 --- a/src/screens/Vault/AllTransactions.tsx +++ b/src/screens/Vault/AllTransactions.tsx @@ -1,13 +1,12 @@ import { FlatList, RefreshControl } from 'react-native'; -import React, { useState } from 'react'; +import React from 'react'; import { useColorMode } from '@gluestack-ui/themed-native-base'; import { useQuery } from '@realm/react'; -import { useDispatch } from 'react-redux'; import { RealmSchema } from 'src/storage/realm/enum'; import TransactionElement from 'src/components/TransactionElement'; import { getJSONFromRealmObject } from 'src/storage/realm/utils'; -import { refreshWallets } from 'src/store/sagaActions/wallets'; +import { useWalletRefresh } from 'src/hooks/useWalletRefresh'; import { Wallet } from 'src/services/wallets/interfaces/wallet'; import useVault from 'src/hooks/useVault'; import { EntityKind } from 'src/services/wallets/enums'; @@ -17,7 +16,6 @@ import WalletHeader from 'src/components/WalletHeader'; function AllTransactions({ route }) { const { colorMode } = useColorMode(); - const dispatch = useDispatch(); const { title, entityKind, subtitle, vaultId = '' } = route?.params; const { activeVault: vault } = useVault({ vaultId }); @@ -25,22 +23,17 @@ function AllTransactions({ route }) { .map(getJSONFromRealmObject) .filter((wallet) => !wallet.archived)[0]; - const [pullRefresh, setPullRefresh] = useState(false); + const { refreshing: pullRefresh, refresh } = useWalletRefresh(); const vaultTrans: Transaction[] = vault?.specs?.transactions || []; const walletTrans: Transaction[] = wallet?.specs.transactions || []; + const currentWallet = entityKind === EntityKind.WALLET ? wallet : vault; const renderTransactionElement = ({ item }) => ( - + ); const pullDownRefresh = () => { - setPullRefresh(true); - refreshVault(); - setPullRefresh(false); - }; - - const refreshVault = () => { - dispatch(refreshWallets([vault], { hardRefresh: true })); + refresh([currentWallet], { hardRefresh: true }); }; return ( diff --git a/src/screens/Vault/VaultDetails.tsx b/src/screens/Vault/VaultDetails.tsx index 4ae46546d7..445866cd87 100644 --- a/src/screens/Vault/VaultDetails.tsx +++ b/src/screens/Vault/VaultDetails.tsx @@ -9,7 +9,7 @@ import SignerIcon from 'src/assets/images/keys-icon.svg'; import KeeperModal from 'src/components/KeeperModal'; import TransactionElement from 'src/components/TransactionElement'; import { MiniscriptTypes, VaultType } from 'src/services/wallets/enums'; -import { refreshWallets } from 'src/store/sagaActions/wallets'; +import { useWalletRefresh } from 'src/hooks/useWalletRefresh'; import { setIntroModal } from 'src/store/reducers/vaults'; import { useAppSelector } from 'src/store/hooks'; import { useDispatch } from 'react-redux'; @@ -157,7 +157,7 @@ function VaultDetails({ navigation, route }: ScreenProps) { const dispatch = useDispatch(); const { showToast } = useToastMessage(); const { activeVault: vault } = useVault({ vaultId }); - const [pullRefresh, setPullRefresh] = useState(false); + const { refreshing: pullRefresh, refresh } = useWalletRefresh(); const { vaultSigners: keys } = useSigners(vault.id); const transactions = useMemo( () => @@ -372,9 +372,7 @@ function VaultDetails({ navigation, route }: ScreenProps) { }, [syncing, ELECTRUM_CLIENT]); const syncVault = (hardRefresh) => { - setPullRefresh(true); - dispatch(refreshWallets([vault], { hardRefresh })); - setPullRefresh(false); + refresh([vault], { hardRefresh }); }; const showTimelockModalContent = useCallback(() => { diff --git a/src/screens/Vault/components/TransactionsAndUTXOs.tsx b/src/screens/Vault/components/TransactionsAndUTXOs.tsx index b9d9be1d5b..c5ca3f4e9c 100644 --- a/src/screens/Vault/components/TransactionsAndUTXOs.tsx +++ b/src/screens/Vault/components/TransactionsAndUTXOs.tsx @@ -1,14 +1,13 @@ import { FlatList, RefreshControl, TouchableOpacity } from 'react-native'; -import React, { useContext, useState } from 'react'; +import React, { useContext } from 'react'; import { CommonActions, useNavigation } from '@react-navigation/native'; import TransactionElement from 'src/components/TransactionElement'; import { Vault } from 'src/services/wallets/interfaces/vault'; import { Box, HStack, VStack } from '@gluestack-ui/themed-native-base'; -import { refreshWallets } from 'src/store/sagaActions/wallets'; +import { useWalletRefresh } from 'src/hooks/useWalletRefresh'; import EmptyStateView from 'src/components/EmptyView/EmptyStateView'; import NoVaultTransactionIcon from 'src/assets/images/emptystate.svg'; import IconArrowBlack from 'src/assets/images/icon_arrow_black.svg'; -import { useDispatch } from 'react-redux'; import Text from 'src/components/KeeperText'; import { windowHeight, wp } from 'src/constants/responsive'; import { EntityKind } from 'src/services/wallets/enums'; @@ -24,14 +23,11 @@ function TransactionsAndUTXOs({ autoRefresh: boolean; }) { const { colorMode } = useColorMode(); - const [pullRefresh, setPullRefresh] = useState(false); + const { refreshing: pullRefresh, refresh } = useWalletRefresh(); const { translations } = useContext(LocalizationContext); const { home } = translations; - const dispatch = useDispatch(); const syncVault = () => { - setPullRefresh(true); - dispatch(refreshWallets([vault], { hardRefresh: true })); - setPullRefresh(false); + refresh([vault], { hardRefresh: true }); }; const navigation = useNavigation(); @@ -71,6 +67,7 @@ function TransactionsAndUTXOs({ title: home.vaultTransactions, subtitle: home.incommingAndOutgoing, entityKind: EntityKind.VAULT, + vaultId: vault.id, }) ); }} diff --git a/src/screens/ViewTransactions/TransactionHistory.tsx b/src/screens/ViewTransactions/TransactionHistory.tsx index c546c47ce8..5f2ab91977 100644 --- a/src/screens/ViewTransactions/TransactionHistory.tsx +++ b/src/screens/ViewTransactions/TransactionHistory.tsx @@ -1,28 +1,41 @@ import { FlatList, RefreshControl, StatusBar, StyleSheet } from 'react-native'; import { Box, useColorMode } from '@gluestack-ui/themed-native-base'; -import React, { use, useContext, useMemo, useState } from 'react'; +import React, { useContext, useMemo } from 'react'; import { hp, wp } from 'src/constants/responsive'; import { LocalizationContext } from 'src/context/Localization/LocContext'; import { Wallet } from 'src/services/wallets/interfaces/wallet'; +import { Vault } from 'src/services/wallets/interfaces/vault'; import { CommonActions, useNavigation } from '@react-navigation/native'; import TransactionElement from 'src/components/TransactionElement'; import EmptyStateView from 'src/components/EmptyView/EmptyStateView'; import NoTransactionIcon from 'src/assets/images/noTransaction.svg'; -import { refreshWallets } from 'src/store/sagaActions/wallets'; +import { useWalletRefresh } from 'src/hooks/useWalletRefresh'; import { useDispatch } from 'react-redux'; import { setStateFromSnapshot } from 'src/store/reducers/send_and_receive'; import CurrencyTypeSwitch from 'src/components/Switch/CurrencyTypeSwitch'; import WalletHeader from 'src/components/WalletHeader'; import { useSafeAreaInsets } from 'react-native-safe-area-context'; +import { useQuery } from '@realm/react'; +import { RealmSchema } from 'src/storage/realm/enum'; +import { getJSONFromRealmObject } from 'src/storage/realm/utils'; +import { EntityKind } from 'src/services/wallets/enums'; function TransactionHistory({ route }) { const { colorMode } = useColorMode(); const navigation = useNavigation(); const { translations } = useContext(LocalizationContext); const { common, transactions: transactionsText } = translations; - const { wallet }: { wallet: Wallet } = route.params; - const [pullRefresh, setPullRefresh] = useState(false); + const { wallet: routeWallet }: { wallet: Wallet | Vault } = route.params; + const walletRecord = useQuery(RealmSchema.Wallet, (collection) => + collection.filtered('id == $0', routeWallet.id) + )[0]; + const vaultRecord = useQuery(RealmSchema.Vault, (collection) => + collection.filtered('id == $0', routeWallet.id) + )[0]; + const record = routeWallet.entityKind === EntityKind.VAULT ? vaultRecord : walletRecord; + const wallet = record ? (getJSONFromRealmObject(record) as Wallet | Vault) : routeWallet; + const { refreshing: pullRefresh, refresh } = useWalletRefresh(); const dispatch = useDispatch(); const insets = useSafeAreaInsets(); const transactions = useMemo( @@ -67,9 +80,7 @@ function TransactionHistory({ route }) { ); const pullDownRefresh = () => { - setPullRefresh(true); - dispatch(refreshWallets([wallet], { hardRefresh: true })); - setPullRefresh(false); + refresh([wallet], { hardRefresh: true }); }; return ( diff --git a/src/screens/WalletDetails/WalletDetails.tsx b/src/screens/WalletDetails/WalletDetails.tsx index f5f9422437..ec901ff5ca 100644 --- a/src/screens/WalletDetails/WalletDetails.tsx +++ b/src/screens/WalletDetails/WalletDetails.tsx @@ -7,7 +7,7 @@ import AddWalletIcon from 'src/assets/images/addWallet_illustration.svg'; import TickIcon from 'src/assets/images/icon_tick.svg'; import { hp, wp } from 'src/constants/responsive'; import Text from 'src/components/KeeperText'; -import { refreshWallets } from 'src/store/sagaActions/wallets'; +import { useWalletRefresh } from 'src/hooks/useWalletRefresh'; import { setIntroModal } from 'src/store/reducers/wallets'; import { useAppSelector } from 'src/store/hooks'; import useWallets from 'src/hooks/useWallets'; @@ -27,7 +27,7 @@ import ThemedColor from 'src/components/ThemedColor/ThemedColor'; import { useUTXOSpendability } from 'src/hooks/useUTXOSpendability'; // TODO: add type definitions to all components -function TransactionsAndUTXOs({ transactions, setPullRefresh, pullRefresh, wallet }) { +function TransactionsAndUTXOs({ transactions, pullRefresh, onRefresh, wallet }) { const { walletSyncing } = useAppSelector((state) => state.wallet); const syncing = walletSyncing && wallet ? !!walletSyncing[wallet.id] : false; @@ -36,9 +36,9 @@ function TransactionsAndUTXOs({ transactions, setPullRefresh, pullRefresh, walle ); @@ -69,7 +69,7 @@ function WalletDetails({ route }: ScreenProps) { const { walletSyncing } = useAppSelector((state) => state.wallet); const syncing = walletSyncing && wallet ? !!walletSyncing[wallet.id] : false; const introModal = useAppSelector((state) => state.wallet.introModal) || false; - const [pullRefresh, setPullRefresh] = useState(false); + const { refreshing: pullRefresh, refresh } = useWalletRefresh(); const viewAll_color = ThemedColor({ name: 'viewAll_color' }); useEffect(() => { @@ -113,9 +113,7 @@ function WalletDetails({ route }: ScreenProps) { }, [viewTransaction, wallet, navigation]); const pullDownRefresh = (hardRefresh) => { - setPullRefresh(true); - dispatch(refreshWallets([wallet], { hardRefresh })); - setPullRefresh(false); + refresh([wallet], { hardRefresh }); }; return ( @@ -179,9 +177,9 @@ function WalletDetails({ route }: ScreenProps) { ) : null} pullDownRefresh(true)} /> ) : ( diff --git a/src/screens/WalletDetails/components/Transactions.tsx b/src/screens/WalletDetails/components/Transactions.tsx index 3e83162957..c893ffaf2a 100644 --- a/src/screens/WalletDetails/components/Transactions.tsx +++ b/src/screens/WalletDetails/components/Transactions.tsx @@ -1,7 +1,5 @@ import { FlatList, RefreshControl } from 'react-native'; -import React, { useContext, useEffect, useMemo, useState } from 'react'; -import { useDispatch } from 'react-redux'; -import { refreshWallets } from 'src/store/sagaActions/wallets'; +import React, { useContext, useEffect, useMemo } from 'react'; import EmptyStateView from 'src/components/EmptyView/EmptyStateView'; import NoTransactionIcon from 'src/assets/images/noTransaction.svg'; import TransactionElement from 'src/components/TransactionElement'; @@ -52,8 +50,8 @@ function Transactions({ pullRefresh, currentWallet, setInitialLoading, + onBitcoinRefresh, }) { - const dispatch = useDispatch(); const navigation = useNavigation(); const { translations } = useContext(LocalizationContext); const { common } = translations; @@ -80,16 +78,19 @@ function Transactions({ }, [transactions]); const pullDownRefresh = async () => { - setInitialLoading(true); + if (currentWallet.entityKind !== EntityKind.USDT_WALLET) { + onBitcoinRefresh?.(); + return; + } + setPullRefresh?.(true); + setInitialLoading?.(true); try { - if (currentWallet.entityKind === EntityKind.USDT_WALLET) { - await syncWallet(currentWallet); - } else dispatch(refreshWallets([currentWallet], { hardRefresh: true })); + await syncWallet(currentWallet); } catch (error) { captureError(error); } finally { - setPullRefresh(false); - setInitialLoading(false); + setPullRefresh?.(false); + setInitialLoading?.(false); } }; diff --git a/src/store/reducers/wallets.ts b/src/store/reducers/wallets.ts index 9e370fd404..a1b0c00fbf 100644 --- a/src/store/reducers/wallets.ts +++ b/src/store/reducers/wallets.ts @@ -22,6 +22,7 @@ export type WalletsState = { introModal: boolean; err: string; walletSyncing: {}; + refreshRequests: Record; signerPolicyError?: string; }; @@ -43,6 +44,7 @@ const initialState: WalletsState = { err: '', walletSyncing: {}, + refreshRequests: {}, }; export type syncingPayload = { @@ -94,6 +96,23 @@ const walletSlice = createSlice({ }, resetSyncing: (state) => { state.walletSyncing = {}; + state.refreshRequests = {}; + }, + startRefreshRequest: (state, action: PayloadAction) => { + state.refreshRequests[action.payload] = 'pending'; + }, + finishRefreshRequest: ( + state, + action: PayloadAction<{ requestId: string; succeeded: boolean }> + ) => { + if (state.refreshRequests[action.payload.requestId] === 'pending') { + state.refreshRequests[action.payload.requestId] = action.payload.succeeded + ? 'success' + : 'failure'; + } + }, + clearRefreshRequest: (state, action: PayloadAction) => { + delete state.refreshRequests[action.payload]; }, setSignerPolicyError: (state, action: PayloadAction) => { state.signerPolicyError = action.payload; @@ -120,6 +139,9 @@ export const { resetWalletStateFlags, resetSyncing, setSyncing, + startRefreshRequest, + finishRefreshRequest, + clearRefreshRequest, setSignerPolicyError, } = walletSlice.actions; @@ -134,6 +156,7 @@ const walletPersistConfig = { 'hasNewWalletsGenerationSucceeded', 'isGeneratingNewWallet', 'walletSyncing', + 'refreshRequests', 'setSignerPolicyError', ], }; diff --git a/src/store/sagaActions/wallets.ts b/src/store/sagaActions/wallets.ts index b9078e1e3e..4d2c97a475 100644 --- a/src/store/sagaActions/wallets.ts +++ b/src/store/sagaActions/wallets.ts @@ -30,7 +30,9 @@ export const autoSyncWallets = ( syncAll?: boolean, hardRefresh?: boolean, addNotifications?: boolean, - backupCheckAppId?: string + backupCheckAppId?: string, + archivedOnly?: boolean, + requestId?: string ) => ({ type: AUTO_SYNC_WALLETS, payload: { @@ -38,6 +40,8 @@ export const autoSyncWallets = ( hardRefresh, addNotifications, backupCheckAppId, + archivedOnly, + requestId, }, }); @@ -61,12 +65,14 @@ export const updateSignerPolicy = ( export const refreshWallets = ( wallets: (Wallet | Vault)[], - options: { hardRefresh?: boolean, dustScan?: boolean } + options: { hardRefresh?: boolean; dustScan?: boolean }, + requestId?: string ) => ({ type: REFRESH_WALLETS, payload: { wallets, options, + requestId, }, }); diff --git a/src/store/sagas/walletRefreshCoordinator.js b/src/store/sagas/walletRefreshCoordinator.js new file mode 100644 index 0000000000..0aaa9406e6 --- /dev/null +++ b/src/store/sagas/walletRefreshCoordinator.js @@ -0,0 +1,41 @@ +// The wallet watcher forks each request. Keep overlapping refreshes in order so +// an older snapshot cannot persist after a newer one for the same wallet. +const activeBatches = new Map(); +const walletTails = new Map(); + +export const acquireWalletRefresh = (appId, networkType, wallets, options = {}) => { + const walletKeys = [...new Set(wallets.map((wallet) => + JSON.stringify([appId, networkType, wallet.entityKind, wallet.id]) + ))].sort(); + const batchKey = JSON.stringify([ + walletKeys, + !!options.hardRefresh, + !!options.addNotifications, + !!options.dustScan, + ]); + const existing = activeBatches.get(batchKey); + if (existing) return { owner: false, done: existing.done }; + + let resolveDone; + const done = new Promise((resolve) => { resolveDone = resolve; }); + const predecessors = walletKeys.map((key) => walletTails.get(key)).filter(Boolean); + const ready = Promise.all(predecessors); + let completed = false; + const lease = { + owner: true, + ready, + done, + complete: (succeeded) => { + if (completed) return; + completed = true; + if (activeBatches.get(batchKey) === lease) activeBatches.delete(batchKey); + for (const key of walletKeys) { + if (walletTails.get(key) === done) walletTails.delete(key); + } + resolveDone(succeeded); + }, + }; + activeBatches.set(batchKey, lease); + for (const key of walletKeys) walletTails.set(key, done); + return lease; +}; diff --git a/src/store/sagas/walletSyncSelection.js b/src/store/sagas/walletSyncSelection.js new file mode 100644 index 0000000000..4e27514569 --- /dev/null +++ b/src/store/sagas/walletSyncSelection.js @@ -0,0 +1,19 @@ +// Archived vaults remain in the local Realm even when they have no balance. +// Keep them in the automatic sync set so a later deposit is discovered. +export const selectWalletsForSync = ( + wallets, + vaults, + networkType, + { syncAll = false, archivedOnly = false } = {} +) => { + const onNetwork = (wallet) => wallet.networkType === networkType; + const archived = vaults.filter((vault) => vault.archived && onNetwork(vault)); + const active = archivedOnly + ? [] + : [...wallets, ...vaults.filter((vault) => !vault.archived)].filter( + (wallet) => + onNetwork(wallet) && (syncAll || wallet.presentationData.visibility === 'DEFAULT') + ); + + return { active, archived }; +}; diff --git a/src/store/sagas/wallets.ts b/src/store/sagas/wallets.ts index 839c894ca3..d0c9c9f711 100644 --- a/src/store/sagas/wallets.ts +++ b/src/store/sagas/wallets.ts @@ -33,6 +33,7 @@ import { setTestCoinsReceived, setTestCoinsQuotaReached, setSignerPolicyError, + finishRefreshRequest, } from 'src/store/reducers/wallets'; import { Alert } from 'react-native'; @@ -71,6 +72,8 @@ import { } from 'src/utils/utilities'; import { COLLABORATIVE_SCHEME } from 'src/screens/SigningDevices/SetupCollaborativeWallet'; import { RootState } from '../store'; +import { selectWalletsForSync } from './walletSyncSelection'; +import { acquireWalletRefresh } from './walletRefreshCoordinator'; import { checkBackupFreshness } from '../sagaActions/bhr'; import { @@ -617,12 +620,15 @@ function* refreshWalletsWorker({ payload: { wallets: (Wallet | Vault)[]; options: { hardRefresh?: boolean; addNotifications?: boolean; dustScan?: boolean }; + requestId?: string; }; }) { - let { wallets, options } = payload; + let { wallets, options, requestId } = payload; + let succeeded = false; + let lease; // Filter out pre-mix, post-mix, and bad bank wallets as they are no longer displayed or used. - wallets = wallets.filter((wallet) => { + wallets = (wallets || []).filter((wallet) => { if ( wallet.type === WalletType.PRE_MIX || wallet.type === WalletType.POST_MIX || @@ -634,7 +640,46 @@ function* refreshWalletsWorker({ }); try { - if (!wallets || wallets.length === 0) return true; + const refreshScope = yield select((state: RootState) => ({ + appId: state.storage.appId, + networkType: state.settings.bitcoinNetworkType, + })); + wallets = wallets.filter((wallet) => wallet.networkType === refreshScope.networkType); + if (wallets.length === 0) { + succeeded = true; + return true; + } + + lease = acquireWalletRefresh( + refreshScope.appId, + refreshScope.networkType, + wallets, + options + ); + if (!lease.owner) { + succeeded = yield call(() => lease.done); + return succeeded; + } + yield call(() => lease.ready); + const readyScope = yield select((state: RootState) => ({ + appId: state.storage.appId, + networkType: state.settings.bitcoinNetworkType, + })); + if ( + readyScope.appId !== refreshScope.appId || + readyScope.networkType !== refreshScope.networkType + ) return false; + + // A queued refresh must start from the latest persisted specs, and a + // stale route must never supply a snapshot from another profile. + const currentWallets: (Wallet | Vault)[] = []; + for (const wallet of wallets) { + const schema = wallet.entityKind === EntityKind.VAULT ? RealmSchema.Vault : RealmSchema.Wallet; + const currentWallet = yield call(dbManager.getObjectById, schema, wallet.id); + if (!currentWallet || currentWallet.networkType !== refreshScope.networkType) return false; + currentWallets.push(getJSONFromRealmObject(currentWallet)); + } + wallets = currentWallets; if (!ELECTRUM_CLIENT.isClientConnected) { ElectrumClient.resetCurrentPeerIndex(); @@ -688,6 +733,14 @@ function* refreshWalletsWorker({ labels = yield call(dbManager.getCollection, RealmSchema.Tags); } for (const synchedWalletWithUTXOs of synchedWallets) { + const currentScope = yield select((state: RootState) => ({ + appId: state.storage.appId, + networkType: state.settings.bitcoinNetworkType, + })); + if ( + currentScope.appId !== refreshScope.appId || + currentScope.networkType !== refreshScope.networkType + ) return false; const { synchedWallet } = synchedWalletWithUTXOs; if (!synchedWallet.specs.hasNewUpdates && !options.hardRefresh) continue; // no new updates found @@ -869,6 +922,14 @@ function* refreshWalletsWorker({ } // Write updated specs (with spendability) back to Realm + const writeScope = yield select((state: RootState) => ({ + appId: state.storage.appId, + networkType: state.settings.bitcoinNetworkType, + })); + if ( + writeScope.appId !== refreshScope.appId || + writeScope.networkType !== refreshScope.networkType + ) return false; const persisted = yield call( dbManager.updateObjectById, synchedWallet.entityKind === EntityKind.VAULT ? RealmSchema.Vault : RealmSchema.Wallet, @@ -881,6 +942,7 @@ function* refreshWalletsWorker({ yield put(setPendingDustToast(synchedWallet.id)); } } // end for (synchedWalletWithUTXOs) + succeeded = true; return true; } catch (err) { if ([ELECTRUM_NOT_CONNECTED_ERR, ELECTRUM_NOT_CONNECTED_ERR_TOR].includes(err?.message)) { @@ -899,7 +961,11 @@ function* refreshWalletsWorker({ } return false; } finally { - yield put(setSyncing({ wallets, isSyncing: false })); + if (lease?.owner) { + yield put(setSyncing({ wallets, isSyncing: false })); + lease.complete(succeeded); + } + if (requestId) yield put(finishRefreshRequest({ requestId, succeeded })); } } @@ -913,46 +979,88 @@ export function* autoWalletsSyncWorker({ hardRefresh?: boolean; addNotifications?: boolean; backupCheckAppId?: string; + archivedOnly?: boolean; + requestId?: string; }; }) { - const { syncAll, hardRefresh, addNotifications, backupCheckAppId } = payload; - const wallets: Wallet[] = yield call(dbManager.getObjectByIndex, RealmSchema.Wallet, null, true); - const vaults: Vault[] = yield call(dbManager.getObjectByIndex, RealmSchema.Vault, null, true); - const { bitcoinNetworkType } = yield select((state: RootState) => state.settings); - - let walletsToSync: (Wallet | Vault)[] = []; - for (const wallet of [...wallets, ...vaults]) { - if (syncAll || wallet.presentationData.visibility === VisibilityType.DEFAULT) { - if (wallet.entityKind === EntityKind.VAULT && (wallet as Vault).archived) continue; - walletsToSync.push(getJSONFromRealmObject(wallet)); + const { syncAll, hardRefresh, addNotifications, backupCheckAppId, archivedOnly, requestId } = payload; + let activeSucceeded = true; + let archivedSucceeded = true; + try { + const syncScope = yield select((state: RootState) => ({ + appId: state.storage.appId, + networkType: state.settings.bitcoinNetworkType, + })); + const wallets: Wallet[] = yield call(dbManager.getObjectByIndex, RealmSchema.Wallet, null, true); + const vaults: Vault[] = yield call(dbManager.getObjectByIndex, RealmSchema.Vault, null, true); + const currentAppId = yield select((state: RootState) => state.storage.appId); + const currentNetworkType = yield select((state: RootState) => state.settings.bitcoinNetworkType); + if (currentAppId !== syncScope.appId || currentNetworkType !== syncScope.networkType) { + activeSucceeded = false; + archivedSucceeded = false; + return; } - } - walletsToSync = walletsToSync.filter((wallet) => wallet.networkType === bitcoinNetworkType); + const { active, archived } = selectWalletsForSync( + wallets || [], + vaults || [], + syncScope.networkType, + { syncAll, archivedOnly } + ); - let refreshSucceeded = true; - if (walletsToSync.length) { - refreshSucceeded = yield call(refreshWalletsWorker, { - payload: { - wallets: walletsToSync, - options: { - hardRefresh, - addNotifications, + if (active.length) { + activeSucceeded = yield call(refreshWalletsWorker, { + payload: { + wallets: active.map(getJSONFromRealmObject), + options: { hardRefresh, addNotifications }, }, - }, - }); - } - // Check backup freshness only after the refreshed wallet specs are persisted. - // A failed refresh or account switch must not verify a stale snapshot. - if (backupCheckAppId && refreshSucceeded) { - const { appId } = yield select((state: RootState) => state.storage); - const { automaticCloudBackup, pendingAllBackup, backupRepairCompletedByAppId = {} } = - yield select((state: RootState) => state.bhr); + }); + } + const beforeArchive = yield select((state: RootState) => ({ + appId: state.storage.appId, + networkType: state.settings.bitcoinNetworkType, + })); if ( - appId === backupCheckAppId && - automaticCloudBackup && - (pendingAllBackup || !backupRepairCompletedByAppId[appId]) + beforeArchive.appId !== syncScope.appId || + beforeArchive.networkType !== syncScope.networkType ) { - yield put(checkBackupFreshness()); + activeSucceeded = false; + archivedSucceeded = false; + return; + } + // Retained archived vaults are checked even when empty or hidden. Their + // transactions are persisted, while notification and transfer UX belongs to R09. + if (archived.length) { + archivedSucceeded = yield call(refreshWalletsWorker, { + payload: { + wallets: archived.map(getJSONFromRealmObject), + options: { hardRefresh, addNotifications: false }, + }, + }); + } + + // Check backup freshness only after the active wallet specs are persisted. + if (backupCheckAppId && activeSucceeded) { + const { appId } = yield select((state: RootState) => state.storage); + const { automaticCloudBackup, pendingAllBackup, backupRepairCompletedByAppId = {} } = + yield select((state: RootState) => state.bhr); + if ( + appId === backupCheckAppId && + automaticCloudBackup && + (pendingAllBackup || !backupRepairCompletedByAppId[appId]) + ) { + yield put(checkBackupFreshness()); + } + } + } catch (err) { + activeSucceeded = false; + archivedSucceeded = false; + captureError(err); + } finally { + if (requestId) { + yield put(finishRefreshRequest({ + requestId, + succeeded: activeSucceeded && archivedSucceeded, + })); } } } diff --git a/tests/recovery/backupFreshness.test.cjs b/tests/recovery/backupFreshness.test.cjs index 4676654e7c..416dc57fb6 100644 --- a/tests/recovery/backupFreshness.test.cjs +++ b/tests/recovery/backupFreshness.test.cjs @@ -5,6 +5,35 @@ const ts = require('typescript'); const { fixture, enums, encryption, wallet, loadModule, functions } = require('./sagaHarness.cjs'); const { NetworkType, WalletType } = enums; +test('archived-only resume keeps checking an empty hidden vault without notifications', async () => { + const f = fixture(); + f.collections.Wallet.push(wallet('visible-wallet', NetworkType.MAINNET)); + f.collections.Vault.push({ + ...wallet('empty-archive', NetworkType.MAINNET), + entityKind: enums.EntityKind.VAULT, + archived: true, + presentationData: { visibility: 'HIDDEN' }, + specs: { balances: { confirmed: 0, unconfirmed: 0 } }, + }); + f.collections.Vault.push({ + ...wallet('other-network-archive', NetworkType.TESTNET), + entityKind: enums.EntityKind.VAULT, + archived: true, + }); + const batches = []; + f.scope.refreshWalletsWorker = async ({ payload }) => { + batches.push(payload); + return true; + }; + + await f.run('autoWalletsSyncWorker', { payload: { archivedOnly: true } }); + await f.run('autoWalletsSyncWorker', { payload: { archivedOnly: true } }); + assert.deepEqual(batches.map(({ wallets }) => wallets.map(({ id }) => id)), [ + ['empty-archive'], ['empty-archive'], + ]); + assert.ok(batches.every(({ options }) => options.addNotifications === false)); +}); + function refreshFixture(kind, rejectWrite = false) { const f = fixture({ bhr: { pendingAllBackup: true } }); const original = { @@ -52,7 +81,7 @@ for (const kind of ['Wallet', 'Vault']) { test(`${kind}: rejected refresh persistence returns failure and clears syncing`, async () => { const f = refreshFixture(kind, true); assert.equal(await f.run('refreshWalletsWorker', { - payload: { wallets: f.collections[kind], options: { hardRefresh: true } }, + payload: { wallets: f.collections[kind], options: { hardRefresh: true }, requestId: 'failed-pull' }, }), false); assert.deepEqual(f.writes, [kind]); assert.equal(f.collections[kind][0].specs.receivingAddress, 'before-refresh'); @@ -61,6 +90,9 @@ for (const kind of ['Wallet', 'Vault']) { assert.equal(f.errors.length, 1); assert.ok(f.actions.some((action) => action.type === 'setElectrumNotConnectedErr' && action.payload.includes('Failed to persist refreshed wallet'))); + const finished = f.actions.find((action) => action.type === 'finishRefreshRequest')?.payload; + assert.equal(finished?.requestId, 'failed-pull'); + assert.equal(finished?.succeeded, false); }); test(`${kind}: login never inspects stale specs after rejected refresh persistence`, async () => { @@ -82,6 +114,17 @@ for (const kind of ['Wallet', 'Vault']) { }); } +test('successful pull refresh completes only after refreshed specs are persisted', async () => { + const f = refreshFixture('Wallet'); + assert.equal(await f.run('refreshWalletsWorker', { + payload: { wallets: f.collections.Wallet, options: { hardRefresh: true }, requestId: 'saved-pull' }, + }), true); + assert.equal(f.collections.Wallet[0].specs.receivingAddress, 'after-refresh'); + const finished = f.actions.find((action) => action.type === 'finishRefreshRequest')?.payload; + assert.equal(finished?.requestId, 'saved-pull'); + assert.equal(finished?.succeeded, true); +}); + test('login backup inspection waits for wallet refresh to finish', async () => { let finishSync; const syncGate = new Promise((resolve) => { finishSync = resolve; }); diff --git a/tests/recovery/sagaHarness.cjs b/tests/recovery/sagaHarness.cjs index 37cc5412d7..a7a821f9f0 100644 --- a/tests/recovery/sagaHarness.cjs +++ b/tests/recovery/sagaHarness.cjs @@ -43,6 +43,8 @@ const encryption = loadModule('src/utils/service-utilities/encryption.ts', { }); const { image } = require('./helpers.cjs'); const { prepareRecoveryImage } = loadModule('src/services/backup/restore.ts', { './image': image }); +const { selectWalletsForSync } = loadModule('src/store/sagas/walletSyncSelection.js'); +const { acquireWalletRefresh } = loadModule('src/store/sagas/walletRefreshCoordinator.js'); const account = loadModule('src/store/reducers/account.ts', { 'src/services/wallets/enums': enums, 'src/models/enums/BHR': loadModule('src/models/enums/BHR.ts'), @@ -118,6 +120,9 @@ function fixture(options = {}) { ...encryption, ...enums, ...account, + selectWalletsForSync, + acquireWalletRefresh, + finishRefreshRequest: (payload) => ({ type: 'finishRefreshRequest', payload }), prepareRecoveryImage, pauseBackup: image.pauseBackup, console: { log() {}, error() {} }, @@ -138,6 +143,7 @@ function fixture(options = {}) { dbManager: { getObjectByIndex: (schema, _index, all) => schema === 'KeeperApp' ? app : all ? collections[schema] : collections[schema]?.[0], + getObjectById: (schema, id) => collections[schema]?.find((row) => row.id === id), getCollection: (schema) => collections[schema] || [], getObjectByField: (schema) => collections[schema] || [], createObject: (schema, value) => { diff --git a/tests/sync/walletRefresh.test.cjs b/tests/sync/walletRefresh.test.cjs new file mode 100644 index 0000000000..af5b8e823b --- /dev/null +++ b/tests/sync/walletRefresh.test.cjs @@ -0,0 +1,60 @@ +const { test } = require('node:test'); +const assert = require('node:assert/strict'); +const { selectWalletsForSync } = require('../../src/store/sagas/walletSyncSelection.js'); +const { acquireWalletRefresh } = require('../../src/store/sagas/walletRefreshCoordinator.js'); + +const wallet = (id, networkType, archived = false, visibility = 'DEFAULT') => ({ + id, networkType, archived, entityKind: 'VAULT', presentationData: { visibility }, + specs: { balances: { confirmed: 0, unconfirmed: 0 } }, +}); + +test('automatic sync retains empty and hidden archived vaults on the selected network', () => { + const main = wallet('main', 'MAINNET'); + const emptyArchive = wallet('empty-archive', 'TESTNET', true, 'HIDDEN'); + const otherNetworkArchive = wallet('other-archive', 'MAINNET', true); + const activeTestnet = wallet('active', 'TESTNET'); + const selected = selectWalletsForSync([main], [emptyArchive, otherNetworkArchive, activeTestnet], 'TESTNET'); + assert.deepEqual(selected.active.map((item) => item.id), ['active']); + assert.deepEqual(selected.archived.map((item) => item.id), ['empty-archive']); + + const resumed = selectWalletsForSync([], [emptyArchive, activeTestnet], 'TESTNET', { + archivedOnly: true, + }); + assert.deepEqual(resumed.active, []); + assert.deepEqual(resumed.archived.map((item) => item.id), ['empty-archive']); +}); + +test('identical requests join and overlapping wallet refreshes wait for the first write', async () => { + const current = wallet('dedup', 'TESTNET'); + const first = acquireWalletRefresh('profile-a', 'TESTNET', [current], { hardRefresh: false }); + const duplicate = acquireWalletRefresh('profile-a', 'TESTNET', [current], { hardRefresh: false }); + const harder = acquireWalletRefresh('profile-a', 'TESTNET', [current], { hardRefresh: true }); + assert.equal(first.owner, true); + assert.equal(duplicate.owner, false); + assert.equal(duplicate.done, first.done); + assert.equal(harder.owner, true); + + let harderReady = false; + harder.ready.then(() => { harderReady = true; }); + await Promise.resolve(); + assert.equal(harderReady, false); + first.complete(true); + assert.equal(await duplicate.done, true); + await harder.ready; + assert.equal(harderReady, true); + harder.complete(true); +}); + +test('refresh queues are isolated by profile and network', async () => { + const current = wallet('shared-xpub', 'TESTNET'); + const first = acquireWalletRefresh('profile-a', 'TESTNET', [current]); + const otherProfile = acquireWalletRefresh('profile-b', 'TESTNET', [current]); + const otherNetwork = acquireWalletRefresh('profile-a', 'MAINNET', [current]); + await Promise.all([otherProfile.ready, otherNetwork.ready]); + assert.equal(otherProfile.owner, true); + assert.equal(otherNetwork.owner, true); + otherProfile.complete(true); + otherNetwork.complete(true); + first.complete(false); + assert.equal(await first.done, false); +}); From 941b3fbffd1157ba09ad2e46633d4a21f568daf8 Mon Sep 17 00:00:00 2001 From: Anant Date: Thu, 8 Oct 2026 17:47:12 +0530 Subject: [PATCH 063/254] fix(ask): preserve reading position and retry failed messages --- src/screens/HelpAi/HelpAiChat.tsx | 91 +++++++++++++++++++----- src/screens/HelpAi/helpAiChatBehavior.ts | 23 ++++++ src/store/reducers/helpAi.ts | 12 +++- tests/helpAiChatBehavior.test.ts | 43 +++++++++++ 4 files changed, 149 insertions(+), 20 deletions(-) create mode 100644 src/screens/HelpAi/helpAiChatBehavior.ts create mode 100644 tests/helpAiChatBehavior.test.ts diff --git a/src/screens/HelpAi/HelpAiChat.tsx b/src/screens/HelpAi/HelpAiChat.tsx index afd7bf0d2f..633533f863 100644 --- a/src/screens/HelpAi/HelpAiChat.tsx +++ b/src/screens/HelpAi/HelpAiChat.tsx @@ -11,6 +11,7 @@ import { TextInput, View, } from 'react-native'; +import type { NativeScrollEvent, NativeSyntheticEvent } from 'react-native'; import DeviceInfo from 'react-native-device-info'; import Buttons from 'src/components/Buttons'; import KeeperModal from 'src/components/KeeperModal'; @@ -42,6 +43,7 @@ import { setHelpAiDraft, setHelpAiDraftStatus, setHelpAiEscalationStage, + setHelpAiFailedMessageId, setHelpAiRawMessages, type HelpAiDraftStatus, type HelpAiRenderMessage, @@ -65,6 +67,7 @@ import Fonts from 'src/constants/Fonts'; import ChatIcon from 'src/assets/images/chat.svg'; import { sanitizeHelpAiReplyLinks, sanitizeHelpAiSources } from 'src/utils/helpAiLinkPolicy'; import { detectSensitiveInput, detectSensitiveInDraft } from 'src/utils/helpAiSensitiveData'; +import { findRetryableChatMessage, isNearChatBottom, planChatSend } from './helpAiChatBehavior'; // Enhanced error handler to distinguish between all backend rate limit codes const getHelpAiFriendlyError = (error: any, scope: 'chat' | 'issue'): string => { @@ -139,6 +142,11 @@ const HelpAiChat = ({ navigation, route }) => { const disclaimerPoints = [askAi.disclaimerPoint1, askAi.disclaimerPoint2, askAi.disclaimerPoint3]; const { showToast } = useToastMessage(); const listRef = useRef>(null); + const initialScrollPending = useRef(true); + const readingEarlier = useRef(false); + const userScrolling = useRef(false); + const forceFollow = useRef(false); + const sendingRef = useRef(false); const generatedConversationId = useMemo(() => `conv_${Date.now().toString(36)}`, []); const conversationId = route?.params?.conversationId || generatedConversationId; const initialPrompt = route?.params?.prefillText || ''; @@ -153,6 +161,7 @@ const HelpAiChat = ({ navigation, route }) => { text: greetings.current, }; const messages = persistedThread?.messages?.length ? persistedThread.messages : [defaultIntro]; + const failedMessage = findRetryableChatMessage(messages, persistedThread?.failedMessageId); const rawChatMessages = persistedThread?.rawMessages || []; const draft = persistedThread?.draft || null; const draftStatus: HelpAiDraftStatus = persistedThread?.draftStatus || 'pending_review'; @@ -163,7 +172,6 @@ const HelpAiChat = ({ navigation, route }) => { const [input, setInput] = useState(initialPrompt); const [sending, setSending] = useState(false); const [typing, setTyping] = useState(false); - const [lastFailedText, setLastFailedText] = useState(null); const [showDisclaimerModal, setShowDisclaimerModal] = useState(false); useEffect(() => { @@ -181,16 +189,28 @@ const HelpAiChat = ({ navigation, route }) => { setShowDisclaimerModal(false); }; - const scrollToBottom = () => { - requestAnimationFrame(() => listRef.current?.scrollToEnd({ animated: true })); + const scrollToBottom = (animated: boolean) => { + requestAnimationFrame(() => listRef.current?.scrollToEnd({ animated })); }; - useEffect(() => { - if (!messages.length) return undefined; - // Small delay to let FlatList measure new content before scrolling - const t = setTimeout(scrollToBottom, 150); - return () => clearTimeout(t); - }, [messages.length]); + const updateReadingPosition = (event: NativeSyntheticEvent) => { + if (!userScrolling.current) return; + const { contentOffset, layoutMeasurement, contentSize } = event.nativeEvent; + readingEarlier.current = !isNearChatBottom( + contentOffset.y, + layoutMeasurement.height, + contentSize.height + ); + }; + + const handleContentSizeChange = (_width: number, height: number) => { + if (height <= 0) return; + if (initialScrollPending.current || forceFollow.current || !readingEarlier.current) { + scrollToBottom(!initialScrollPending.current); + } + initialScrollPending.current = false; + forceFollow.current = false; + }; const appendMessage = (message: HelpAiRenderMessage) => { dispatch(appendHelpAiMessage({ conversationId, message })); @@ -214,23 +234,33 @@ const HelpAiChat = ({ navigation, route }) => { } }; - const sendToChat = async (text: string) => { - if (!text.trim()) return; + const sendToChat = async (text: string, retryMessageId?: string) => { + if (sendingRef.current) return; + const retryMessage = findRetryableChatMessage(messages, retryMessageId); + if (retryMessageId && !retryMessage) return; + const { outboundText, appendUserMessage } = planChatSend(text, retryMessage); + if (!outboundText) return; if (!appId) { showToast('Missing app id. Please restart the app and try again.'); return; } - const sensitiveResult = detectSensitiveInput(text.trim()); + const sensitiveResult = detectSensitiveInput(outboundText); if (sensitiveResult) { showToast(sensitiveResult.message); return; } - const outboundText = text.trim(); - setInput(''); + const messageId = retryMessageId || nowId(); + sendingRef.current = true; + forceFollow.current = true; + readingEarlier.current = false; + if (appendUserMessage) { + setInput(''); + dispatch(setHelpAiFailedMessageId({ conversationId, messageId: null })); + appendMessage({ id: messageId, type: 'user', text: outboundText }); + } setSending(true); setTyping(true); - appendMessage({ id: nowId(), type: 'user', text: outboundText }); try { const metadata = chatMeta || (await buildMetadata()); @@ -286,11 +316,12 @@ const HelpAiChat = ({ navigation, route }) => { dispatch(appendHelpAiMessage({ conversationId, message: escalationRenderMsg })); } }); - setLastFailedText(null); + dispatch(setHelpAiFailedMessageId({ conversationId, messageId: null })); } catch (error) { showToast(getHelpAiFriendlyError(error, 'chat')); - setLastFailedText(outboundText); + dispatch(setHelpAiFailedMessageId({ conversationId, messageId })); } finally { + sendingRef.current = false; setSending(false); setTyping(false); } @@ -496,6 +527,24 @@ const HelpAiChat = ({ navigation, route }) => { )} contentContainerStyle={styles.messagesContainer} keyboardShouldPersistTaps="handled" + onContentSizeChange={handleContentSizeChange} + onScrollBeginDrag={() => { + userScrolling.current = true; + forceFollow.current = false; + }} + onScroll={updateReadingPosition} + onScrollEndDrag={(event) => { + updateReadingPosition(event); + userScrolling.current = false; + }} + onMomentumScrollBegin={() => { + userScrolling.current = true; + }} + onMomentumScrollEnd={(event) => { + updateReadingPosition(event); + userScrolling.current = false; + }} + scrollEventThrottle={16} ListFooterComponent={ <> {typing && ( @@ -574,8 +623,12 @@ const HelpAiChat = ({ navigation, route }) => { - {lastFailedText ? ( - sendToChat(lastFailedText)}> + {failedMessage ? ( + sendToChat(failedMessage.text, failedMessage.id)} + disabled={sending} + > Retry last failed message diff --git a/src/screens/HelpAi/helpAiChatBehavior.ts b/src/screens/HelpAi/helpAiChatBehavior.ts new file mode 100644 index 0000000000..4835df279d --- /dev/null +++ b/src/screens/HelpAi/helpAiChatBehavior.ts @@ -0,0 +1,23 @@ +export const HELP_AI_NEAR_BOTTOM_DISTANCE = 80; + +export const isNearChatBottom = ( + offsetY: number, + viewportHeight: number, + contentHeight: number +): boolean => contentHeight - offsetY - viewportHeight <= HELP_AI_NEAR_BOTTOM_DISTANCE; + +export const findRetryableChatMessage = ( + messages: Array<{ id: string; type: string; text?: string }>, + failedMessageId?: string | null +): { id: string; text: string } | undefined => { + if (!failedMessageId) return undefined; + const message = messages.find((item) => item.id === failedMessageId); + return message?.type === 'user' && typeof message.text === 'string' + ? { id: message.id, text: message.text } + : undefined; +}; + +export const planChatSend = (input: string, retryMessage?: { text: string }) => ({ + outboundText: retryMessage ? retryMessage.text : input.trim(), + appendUserMessage: !retryMessage, +}); diff --git a/src/store/reducers/helpAi.ts b/src/store/reducers/helpAi.ts index 78489e77a1..27dbba4649 100644 --- a/src/store/reducers/helpAi.ts +++ b/src/store/reducers/helpAi.ts @@ -34,6 +34,7 @@ export type HelpAiThread = { lastEscalationStage: string; chatMeta: HelpChatMetadata | null; issueCount: number; + failedMessageId: string | null; }; type HelpAiState = { @@ -66,6 +67,7 @@ const makeThread = (conversationId: string): HelpAiThread => ({ lastEscalationStage: 'none', chatMeta: null, issueCount: 1, + failedMessageId: null, }); const findOrCreateThread = (state: HelpAiState, conversationId: string) => { @@ -153,6 +155,13 @@ const helpAiSlice = createSlice({ thread.issueCount += 1; thread.updatedAt = new Date().toISOString(); }, + setHelpAiFailedMessageId: ( + state, + action: PayloadAction<{ conversationId: string; messageId: string | null }> + ) => { + const thread = findOrCreateThread(state, action.payload.conversationId); + thread.failedMessageId = action.payload.messageId; + }, }, }); @@ -166,6 +175,7 @@ export const { setHelpAiEscalationStage, setHelpAiChatMeta, incrementHelpAiIssueCount, + setHelpAiFailedMessageId, } = helpAiSlice.actions; const helpAiPersistConfig = { @@ -174,4 +184,4 @@ const helpAiPersistConfig = { whitelist: ['threads', 'activeConversationId'], }; -export default persistReducer(helpAiPersistConfig, helpAiSlice.reducer); \ No newline at end of file +export default persistReducer(helpAiPersistConfig, helpAiSlice.reducer); diff --git a/tests/helpAiChatBehavior.test.ts b/tests/helpAiChatBehavior.test.ts new file mode 100644 index 0000000000..cbfbac6c8b --- /dev/null +++ b/tests/helpAiChatBehavior.test.ts @@ -0,0 +1,43 @@ +import { + findRetryableChatMessage, + isNearChatBottom, + planChatSend, +} from '../src/screens/HelpAi/helpAiChatBehavior'; + +test('new content follows only while the reader is near the bottom', () => { + expect(isNearChatBottom(200, 400, 680)).toBe(true); + expect(isNearChatBottom(199, 400, 680)).toBe(false); + expect(isNearChatBottom(0, 400, 200)).toBe(true); +}); + +test('a fresh send adds one user message with trimmed text', () => { + expect(planChatSend(' How do I reconnect? ')).toEqual({ + outboundText: 'How do I reconnect?', + appendUserMessage: true, + }); +}); + +test('retry resends the failed message without adding a second user bubble', () => { + const threadAfterReturn: { + failedMessageId: string; + messages: Array<{ id: string; type: string; text: string }>; + } = JSON.parse( + JSON.stringify({ + failedMessageId: 'failed-1', + messages: [ + { id: 'failed-1', type: 'user', text: 'Original failed message' }, + { id: 'ai-1', type: 'ai', text: 'Earlier answer' }, + ], + }) + ); + const failedMessage = findRetryableChatMessage( + threadAfterReturn.messages, + threadAfterReturn.failedMessageId + ); + expect(failedMessage).toEqual({ id: 'failed-1', text: 'Original failed message' }); + expect(planChatSend('draft typed after failure', failedMessage)).toEqual({ + outboundText: 'Original failed message', + appendUserMessage: false, + }); + expect(findRetryableChatMessage(threadAfterReturn.messages, 'ai-1')).toBeUndefined(); +}); From fa748cd97535c66eaa40b710a2b02acf9db17456 Mon Sep 17 00:00:00 2001 From: Anant Date: Thu, 8 Oct 2026 17:49:55 +0530 Subject: [PATCH 064/254] fix: align remote links with iOS association and validate routes --- ios/hexa_keeper/hexa_keeper.entitlements | 3 +- ios/hexa_keeper_dev.entitlements | 3 +- src/navigation/AppStateHandler.tsx | 15 ++++---- src/navigation/remoteKeyDeepLink.ts | 25 ++++++++++++++ src/screens/Home/InititalAppController.tsx | 21 +++++------- src/screens/SigningDevices/RemoteSharing.tsx | 2 +- src/store/reducers/login.ts | 2 +- tests/navigation/remoteKeyDeepLink.test.mjs | 36 ++++++++++++++++++++ 8 files changed, 85 insertions(+), 22 deletions(-) create mode 100644 src/navigation/remoteKeyDeepLink.ts create mode 100644 tests/navigation/remoteKeyDeepLink.test.mjs diff --git a/ios/hexa_keeper/hexa_keeper.entitlements b/ios/hexa_keeper/hexa_keeper.entitlements index 2fba371611..176fe58afc 100644 --- a/ios/hexa_keeper/hexa_keeper.entitlements +++ b/ios/hexa_keeper/hexa_keeper.entitlements @@ -6,7 +6,8 @@ development com.apple.developer.associated-domains - applinks:bitcoinkeeper.app/app/prod + applinks:www.bitcoinkeeper.app + applinks:bitcoinkeeper.app com.apple.developer.icloud-container-identifiers diff --git a/ios/hexa_keeper_dev.entitlements b/ios/hexa_keeper_dev.entitlements index bda8e1cf34..212d9955d9 100644 --- a/ios/hexa_keeper_dev.entitlements +++ b/ios/hexa_keeper_dev.entitlements @@ -6,7 +6,8 @@ development com.apple.developer.associated-domains - applinks:bitcoinkeeper.app/app/dev + applinks:www.bitcoinkeeper.app + applinks:bitcoinkeeper.app com.apple.developer.icloud-container-identifiers diff --git a/src/navigation/AppStateHandler.tsx b/src/navigation/AppStateHandler.tsx index 342f2ab15e..560b474af8 100644 --- a/src/navigation/AppStateHandler.tsx +++ b/src/navigation/AppStateHandler.tsx @@ -1,4 +1,4 @@ -import { useEffect, useState } from 'react'; +import { useEffect, useRef, useState } from 'react'; import { AppState, Linking } from 'react-native'; import { useNavigation } from '@react-navigation/native'; import { useDispatch } from 'react-redux'; @@ -11,14 +11,16 @@ const AppStateHandler = () => { const [appState, setAppState] = useState(AppState.currentState); const [lastBackgroundTime, setLastBackgroundTime] = useState(null); const dispatch = useDispatch(); - let triggeredDeepLink = null; + const triggeredDeepLink = useRef(null); useEffect(() => { - Linking.addEventListener('url', (event) => { - if (event.url) triggeredDeepLink = event.url; + const subscription = Linking.addEventListener('url', (event) => { + if (event.url) triggeredDeepLink.current = event.url; }); - // cleanup is performed in initialAppController for all url events at once. + return () => subscription.remove(); + }, []); + useEffect(() => { const handleAppStateChange = (nextAppState) => { if (appState === 'active' && nextAppState.match(/inactive|background/)) { setLastBackgroundTime(Date.now()); @@ -32,7 +34,8 @@ const AppStateHandler = () => { lastBackgroundTime && Date.now() - lastBackgroundTime > PASSCODE_TIMEOUT ) { - dispatch(setHasDeepLink(triggeredDeepLink)); + dispatch(setHasDeepLink(triggeredDeepLink.current)); + triggeredDeepLink.current = null; navigation.reset({ index: 0, routes: [ diff --git a/src/navigation/remoteKeyDeepLink.ts b/src/navigation/remoteKeyDeepLink.ts new file mode 100644 index 0000000000..7df197c51a --- /dev/null +++ b/src/navigation/remoteKeyDeepLink.ts @@ -0,0 +1,25 @@ +const REMOTE_KEY_PATH = /^\/app\/(dev|prod)\/remote\/([0-9a-f]{24})\/?$/; + +/** Return only links generated for this build's remote-key exchange. */ +export function getRemoteKeyFromDeepLink(rawUrl: string, environment: string): string | null { + const expectedStage = environment === 'PRODUCTION' ? 'prod' : environment === 'DEVELOPMENT' ? 'dev' : null; + if (!expectedStage) return null; + + try { + const url = new URL(rawUrl); + if ( + url.protocol !== 'https:' || + !['bitcoinkeeper.app', 'www.bitcoinkeeper.app'].includes(url.hostname) || + url.port || + url.username || + url.password + ) { + return null; + } + + const match = REMOTE_KEY_PATH.exec(url.pathname); + return match?.[1] === expectedStage ? match[2] : null; + } catch { + return null; + } +} diff --git a/src/screens/Home/InititalAppController.tsx b/src/screens/Home/InititalAppController.tsx index 26d81b9ae1..9837071457 100644 --- a/src/screens/Home/InititalAppController.tsx +++ b/src/screens/Home/InititalAppController.tsx @@ -58,6 +58,7 @@ import ThemeMode from 'src/models/enums/ThemeMode'; import { getString, setItem } from 'src/storage'; import ActivityIndicatorView from 'src/components/AppActivityIndicator/ActivityIndicatorView'; import { setAccountManagerDetails } from 'src/store/reducers/concierge'; +import { getRemoteKeyFromDeepLink } from 'src/navigation/remoteKeyDeepLink'; export const KEEPER_PRIVATE_LINK = 'KEEPER_PRIVATE_LINK'; function InititalAppController({ navigation, electrumErrorVisible, setElectrumErrorVisible }) { @@ -85,8 +86,9 @@ function InititalAppController({ navigation, electrumErrorVisible, setElectrumEr function handleDeepLinkEvent(event) { const { url } = event; if (url) { - if (url.includes('remote/')) { - handleRemoteKeyDeepLink(url); + const encryptionKey = getRemoteKeyFromDeepLink(url, config.ENVIRONMENT); + if (encryptionKey) { + handleRemoteKeyDeepLink(encryptionKey); } else if (url.includes('kp/')) { handleKeeperPrivate(url); } @@ -100,9 +102,8 @@ function InititalAppController({ navigation, electrumErrorVisible, setElectrumEr const { inProgress, start } = useAsync(); - const handleRemoteKeyDeepLink = async (initialUrl: string) => { + const handleRemoteKeyDeepLink = async (encryptionKey: string) => { dispatch(setAppWideLoading(true)); - const encryptionKey = initialUrl.split('remote/')[1]; const hash = getHashFromKey(encryptionKey); if (encryptionKey && hash) { try { @@ -338,21 +339,17 @@ function InititalAppController({ navigation, electrumErrorVisible, setElectrumEr async function handleDeepLinking() { try { const initialUrl = await Linking.getInitialURL(); - if (initialUrl) { - if (initialUrl.includes('remote/')) { - handleRemoteKeyDeepLink(initialUrl); - } - } + if (initialUrl) handleDeepLinkEvent({ url: initialUrl }); } catch (error) { // } } useEffect(() => { - Linking.addEventListener('url', handleDeepLinkEvent); - handleDeepLinking(); + const subscription = Linking.addEventListener('url', handleDeepLinkEvent); + if (!hasDeepLink) handleDeepLinking(); return () => { - Linking.removeAllListeners('url'); + subscription.remove(); }; }, []); diff --git a/src/screens/SigningDevices/RemoteSharing.tsx b/src/screens/SigningDevices/RemoteSharing.tsx index f029090868..1e1064ff6e 100644 --- a/src/screens/SigningDevices/RemoteSharing.tsx +++ b/src/screens/SigningDevices/RemoteSharing.tsx @@ -117,7 +117,7 @@ function RemoteSharing({ route }: ScreenProps) { if (res?.id) { const result = await Share.share({ title: RemoteShareText[mode].msgTitle, - message: `${RemoteShareText[mode].msgDesc}\nhttps://bitcoinkeeper.app/${ + message: `${RemoteShareText[mode].msgDesc}\nhttps://www.bitcoinkeeper.app/${ DeepLinkIdentifier[config.ENVIRONMENT] }/remote/${encryptionKey}`, }); diff --git a/src/store/reducers/login.ts b/src/store/reducers/login.ts index 99e122a497..8916f92a58 100644 --- a/src/store/reducers/login.ts +++ b/src/store/reducers/login.ts @@ -149,7 +149,7 @@ const loginSlice = createSlice({ setOfflineStatus: (state, action: PayloadAction) => { state.isOffline = action.payload; }, - setHasDeepLink: (state, action: PayloadAction) => { + setHasDeepLink: (state, action: PayloadAction) => { state.hasDeepLink = action.payload; }, clearHasCreds: (state) => { diff --git a/tests/navigation/remoteKeyDeepLink.test.mjs b/tests/navigation/remoteKeyDeepLink.test.mjs new file mode 100644 index 0000000000..32819da7e4 --- /dev/null +++ b/tests/navigation/remoteKeyDeepLink.test.mjs @@ -0,0 +1,36 @@ +import assert from 'node:assert/strict'; +import test from 'node:test'; +import { getRemoteKeyFromDeepLink } from '../../src/navigation/remoteKeyDeepLink.ts'; + +const key = '0123456789abcdef01234567'; + +test('accepts generated production and development remote-key exchange links', () => { + assert.equal( + getRemoteKeyFromDeepLink(`https://www.bitcoinkeeper.app/app/prod/remote/${key}`, 'PRODUCTION'), + key + ); + assert.equal( + getRemoteKeyFromDeepLink(`https://bitcoinkeeper.app/app/dev/remote/${key}`, 'DEVELOPMENT'), + key + ); + assert.equal( + getRemoteKeyFromDeepLink(`https://www.bitcoinkeeper.app/app/prod/remote/${key}?source=share`, 'PRODUCTION'), + key + ); +}); + +test('rejects a different stage, site, route, or malformed encryption key before fetching', () => { + for (const url of [ + `https://www.bitcoinkeeper.app/app/dev/remote/${key}`, + `https://bitcoinkeeper.app.evil.test/app/prod/remote/${key}`, + `http://www.bitcoinkeeper.app/app/prod/remote/${key}`, + `https://www.bitcoinkeeper.app.evil.test/app/prod/remote/${key}`, + `https://www.bitcoinkeeper.app/app/prod/remote/${key}/extra`, + `https://www.bitcoinkeeper.app/app/prod/remote/${key.toUpperCase()}`, + `https://www.bitcoinkeeper.app/app/prod/remote/${key.slice(0, -1)}`, + `https://www.bitcoinkeeper.app/app/prod/remote/${key}zz`, + 'not a link', + ]) { + assert.equal(getRemoteKeyFromDeepLink(url, 'PRODUCTION'), null, url); + } +}); From 1250be2eaee647bc73559c02ee739b9bea86b150 Mon Sep 17 00:00:00 2001 From: Anant Date: Thu, 8 Oct 2026 17:55:19 +0530 Subject: [PATCH 065/254] fix(network): keep private nodes isolated and verify faucet visibility --- src/context/Localization/language/en.json | 4 + src/context/Localization/language/es.json | 4 + src/hooks/useTestSats.tsx | 40 +++++++-- src/screens/AppSettings/Node/NodeSettings.tsx | 87 +++++++++---------- src/services/backend/Relay.ts | 39 ++++++--- src/services/electrum/client.ts | 40 +++++---- src/services/electrum/peerSelection.ts | 32 +++++++ src/services/electrum/predefinedNodes.ts | 9 ++ src/services/rest/RestClient.ts | 11 ++- src/store/reducers/network.ts | 13 ++- src/store/reducers/wallets.ts | 10 +++ src/store/sagas/faucetVisibility.ts | 17 ++++ src/store/sagas/network.ts | 60 ++++++++++++- src/store/sagas/wallets.ts | 74 +++++++++++++--- tests/services/faucetRequest.test.ts | 60 +++++++++++++ tests/services/faucetVisibility.test.ts | 22 +++++ tests/services/networkFailover.test.ts | 42 +++++++++ .../services/restClient.requestConfig.test.ts | 53 +++++++++++ 18 files changed, 519 insertions(+), 98 deletions(-) create mode 100644 src/services/electrum/peerSelection.ts create mode 100644 src/store/sagas/faucetVisibility.ts create mode 100644 tests/services/faucetRequest.test.ts create mode 100644 tests/services/faucetVisibility.test.ts create mode 100644 tests/services/networkFailover.test.ts create mode 100644 tests/services/restClient.requestConfig.test.ts diff --git a/src/context/Localization/language/en.json b/src/context/Localization/language/en.json index 3a451be678..02441c17eb 100644 --- a/src/context/Localization/language/en.json +++ b/src/context/Localization/language/en.json @@ -449,6 +449,10 @@ "faucetDailyLimitTitle": "Daily Limit Reached", "faucetDailyLimitBody": "You can request test sats up to 5 times per day. Try again after midnight UTC.", "faucetDailyLimitError": "Daily limit reached. Try again after midnight UTC.", + "faucetPendingTitle": "Test sats are still pending", + "faucetPropagationPending": "The faucet sent test sats, but this wallet has not seen the transaction yet. Your balance may take a moment to update.", + "faucetSyncPending": "The faucet sent test sats, but the wallet could not sync. Check your server connection before requesting again.", + "faucetOutcomeUnknown": "The faucet request timed out. Test sats may already have been sent. Check your balance before requesting again.", "Singlekey": "Single-key", "multikey2of3": "2 of 3 multi-key", "multikey3of5": "3 of 5 multi-key", diff --git a/src/context/Localization/language/es.json b/src/context/Localization/language/es.json index 4670ae75db..00dd56df1c 100644 --- a/src/context/Localization/language/es.json +++ b/src/context/Localization/language/es.json @@ -437,6 +437,10 @@ "faucetDailyLimitTitle": "Daily Limit Reached", "faucetDailyLimitBody": "You can request test sats up to 5 times per day. Try again after midnight UTC.", "faucetDailyLimitError": "Daily limit reached. Try again after midnight UTC.", + "faucetPendingTitle": "Los sats de prueba siguen pendientes", + "faucetPropagationPending": "El grifo envió sats de prueba, pero la cartera aún no ve la transacción. El saldo puede tardar en actualizarse.", + "faucetSyncPending": "El grifo envió sats de prueba, pero la cartera no pudo sincronizarse. Comprueba la conexión al servidor antes de volver a solicitarlos.", + "faucetOutcomeUnknown": "La solicitud al grifo agotó el tiempo de espera. Puede que ya se hayan enviado sats de prueba. Comprueba el saldo antes de volver a solicitarlos.", "Singlekey": "Single-key", "multikey2of3": "2 of 3 multi-key", "multikey3of5": "3 of 5 multi-key", diff --git a/src/hooks/useTestSats.tsx b/src/hooks/useTestSats.tsx index a413c2bdde..86910f60ab 100644 --- a/src/hooks/useTestSats.tsx +++ b/src/hooks/useTestSats.tsx @@ -8,6 +8,7 @@ import { setTestCoinsFailed, setTestCoinsReceived, setTestCoinsQuotaReached, + setTestCoinsPending, } from 'src/store/reducers/wallets'; import { LocalizationContext } from 'src/context/Localization/LocContext'; import { useNavigation } from '@react-navigation/native'; @@ -19,9 +20,8 @@ import KeeperModal from 'src/components/KeeperModal'; const useTestSats = ({ wallet }) => { const { setAppLoading, setLoadingContent } = useContext(AppContext); - const { testCoinsReceived, testCoinsFailed, testCoinsQuotaReached } = useAppSelector( - (state) => state.wallet - ); + const { testCoinsReceived, testCoinsFailed, testCoinsQuotaReached, testCoinsPending } = + useAppSelector((state) => state.wallet); const { bitcoinNetworkType } = useAppSelector((state) => state.settings); const { showToast } = useToastMessage(); const dispatch = useDispatch(); @@ -30,21 +30,33 @@ const useTestSats = ({ wallet }) => { const navigation = useNavigation(); const { colorMode } = useColorMode(); const [quotaModalVisible, setQuotaModalVisible] = useState(false); + const [pendingModalKind, setPendingModalKind] = useState(null); useEffect(() => { - setAppLoading(false); if (testCoinsReceived) { + setAppLoading(false); showToast(walletText.recievedSats, ); - setTimeout(() => { + const timer = setTimeout(() => { dispatch(setTestCoinsReceived(false)); navigation.goBack(); }, 3000); + return () => clearTimeout(timer); } else if (testCoinsFailed) { + setAppLoading(false); showToast(errorText.processFailed); dispatch(setTestCoinsFailed(false)); } + return undefined; }, [testCoinsReceived, testCoinsFailed]); + useEffect(() => { + if (testCoinsPending) { + setAppLoading(false); + setPendingModalKind(testCoinsPending); + dispatch(setTestCoinsPending(null)); + } + }, [testCoinsPending]); + useEffect(() => { if (testCoinsQuotaReached) { setAppLoading(false); @@ -105,6 +117,24 @@ const useTestSats = ({ wallet }) => { buttonText={common.ok} buttonCallback={() => setQuotaModalVisible(false)} /> + setPendingModalKind(null)} + title={walletText.faucetPendingTitle} + subTitle={ + pendingModalKind === 'unknown' + ? walletText.faucetOutcomeUnknown + : pendingModalKind === 'sync' + ? walletText.faucetSyncPending + : walletText.faucetPropagationPending + } + modalBackground={`${colorMode}.modalWhiteBackground`} + textColor={`${colorMode}.textGreen`} + subTitleColor={`${colorMode}.modalSubtitleBlack`} + showCloseIcon={false} + buttonText={common.ok} + buttonCallback={() => setPendingModalKind(null)} + /> ); }; diff --git a/src/screens/AppSettings/Node/NodeSettings.tsx b/src/screens/AppSettings/Node/NodeSettings.tsx index dc74de79ca..4e88a8327e 100644 --- a/src/screens/AppSettings/Node/NodeSettings.tsx +++ b/src/screens/AppSettings/Node/NodeSettings.tsx @@ -1,5 +1,5 @@ import { Box, useColorMode } from '@gluestack-ui/themed-native-base'; -import React, { useContext, useEffect, useState } from 'react'; +import React, { useCallback, useContext, useState } from 'react'; import { StyleSheet, FlatList } from 'react-native'; import { hp, wp } from 'src/constants/responsive'; import { LocalizationContext } from 'src/context/Localization/LocContext'; @@ -19,7 +19,7 @@ import DowngradeToPleb from 'src/assets/images/downgradetopleb.svg'; import DowngradeToPlebDark from 'src/assets/images/downgradetoplebDark.svg'; import Buttons from 'src/components/Buttons'; import EmptyListIllustration from 'src/components/EmptyListIllustration'; -import { CommonActions, useNavigation } from '@react-navigation/native'; +import { CommonActions, useFocusEffect, useNavigation } from '@react-navigation/native'; import ServerItem from './components/ServerItem'; import ActivityIndicatorView from 'src/components/AppActivityIndicator/ActivityIndicatorView'; import { updateAppImage } from 'src/store/sagaActions/bhr'; @@ -53,15 +53,14 @@ function NodeSettings() { const [nodeToDisconnect, setNodeToDisconnect] = useState(null); const [nodeToDelete, setNodeToDelete] = useState(null); - const isNodeListEmpty = nodeList.length === 0; const isNoNodeConnected = !ELECTRUM_CLIENT.isClientConnected && nodeList.every((node) => !node.isConnected); - const nodes: NodeDetail[] = Node.getAllNodes(); - - useEffect(() => { - setNodeList(nodes); - }, [nodes.length]); + useFocusEffect( + useCallback(() => { + setNodeList(Node.getAllNodes()); + }, []) + ); const onDelete = async (selectedItem: NodeDetail) => { const isConnected = Node.nodeConnectionStatus(selectedItem); @@ -76,56 +75,54 @@ function NodeSettings() { }; const onConnectToNode = async (selectedNode: NodeDetail) => { - let updatedNodes = Node.getAllNodes(); - - // Disconnect the currently connected node if it's not the selected one - const currentlySelectedNode = updatedNodes.find((node) => node.isConnected); - if (currentlySelectedNode && currentlySelectedNode.id !== selectedNode.id) { - await Node.disconnect(currentlySelectedNode); - currentlySelectedNode.isConnected = false; - Node.update(currentlySelectedNode, { isConnected: currentlySelectedNode.isConnected }); - updatedNodes = updatedNodes.map((node) => - node.id === currentlySelectedNode.id ? { ...currentlySelectedNode } : node - ); - } - dispatch(electrumClientConnectionInitiated()); setLoading(true); - - const { connected, connectedTo, error } = await Node.connectToSelectedNode(selectedNode); - - if (connected) { - selectedNode.isConnected = connected; - Node.update(selectedNode, { isConnected: connected }); - dispatch(electrumClientConnectionExecuted({ successful: connected, connectedTo })); - showToast(`${errorString.ConnectedTo} ${connectedTo}`, ); - } else { - dispatch(electrumClientConnectionExecuted({ successful: connected, error })); + try { + const currentlySelectedNode = Node.getAllNodes().find((node) => node.isConnected); + if (currentlySelectedNode && currentlySelectedNode.id !== selectedNode.id) { + Node.disconnect(currentlySelectedNode); + Node.update(currentlySelectedNode, { isConnected: false }); + } + + const { connected, connectedTo, error } = await Node.connectToSelectedNode(selectedNode); + if (connected) { + Node.update(selectedNode, { isConnected: true }); + dispatch(electrumClientConnectionExecuted({ successful: true, connectedTo })); + showToast(`${errorString.ConnectedTo} ${connectedTo}`, ); + } else { + dispatch(electrumClientConnectionExecuted({ successful: false, error })); + } + } catch (error) { + dispatch( + electrumClientConnectionExecuted({ + successful: false, + error: error?.message || String(error), + }) + ); + } finally { + setLoading(false); + setNodeList(Node.getAllNodes()); } - - updatedNodes = updatedNodes.map((node) => - node.id === selectedNode.id ? { ...selectedNode } : node - ); - setNodeList(updatedNodes); - setLoading(false); }; const onDisconnectToNode = async (selectedNode: NodeDetail) => { try { setLoading(true); Node.disconnect(selectedNode); - selectedNode.isConnected = false; - Node.update(selectedNode, { isConnected: selectedNode.isConnected }); - showToast(`${errorString.disconnectedFrom} ${selectedNode.host}`, ); - - const updatedNodes = nodeList.map((node) => - node.id === selectedNode.id ? { ...selectedNode } : node + Node.update(selectedNode, { isConnected: false }); + dispatch( + electrumClientConnectionExecuted({ + successful: false, + error: 'Disconnected from Electrum server', + }) ); - setNodeList(updatedNodes); - setLoading(false); + showToast(`${errorString.disconnectedFrom} ${selectedNode.host}`, ); } catch (error) { console.error('Error disconnecting electrum client', error); showToast(errorString.failedToDiConnect, ); + } finally { + setLoading(false); + setNodeList(Node.getAllNodes()); } }; diff --git a/src/services/backend/Relay.ts b/src/services/backend/Relay.ts index 9bcdcb3fe4..00df2bc0b1 100644 --- a/src/services/backend/Relay.ts +++ b/src/services/backend/Relay.ts @@ -223,32 +223,47 @@ export default class Relay { public static getTestcoins = async ( recipientAddress: string, - network: any, + network: NetworkType, appId: string ): Promise<{ - txid: any; - funded: any; + txid: string; + funded: boolean; }> => { - if (network === NetworkType.MAINNET) { + if (network !== NetworkType.TESTNET) { throw new Error('Invalid network: failed to fund via testnet'); } try { - const res = await RestClient.post(`${config.RELAY}testnetFaucet`, { - recipientAddress, - appId, - }); - const { txid, funded } = res.data; + const res = await RestClient.post( + `${config.RELAY}testnetFaucet`, + { recipientAddress, appId }, + undefined, + { timeout: 20000 } + ); + const { txid, funded } = res.data || {}; + if (!funded || typeof txid !== 'string' || !/^[0-9a-f]{64}$/i.test(txid)) { + throw new Error('FAUCET_OUTCOME_UNKNOWN'); + } return { txid, funded, }; } catch (err) { + if (err?.message === 'FAUCET_OUTCOME_UNKNOWN') throw err; if (err.response?.status === 429) { throw new Error('FAUCET_DAILY_LIMIT_REACHED'); } - if (err.response) throw new Error(err.response.data.err); - if (err.code) throw new Error(err.code); + if ( + err?.code === 'ECONNABORTED' || + err?.code === 'ETIMEDOUT' || + err?.code === 'ERR_NETWORK' || + err?.response?.status >= 500 + ) { + // The relay may have broadcast before the response was lost. Never retry automatically. + throw new Error('FAUCET_OUTCOME_UNKNOWN'); + } + if (err?.response) throw new Error(err.response.data?.err || 'FAUCET_REQUEST_FAILED'); + throw new Error('FAUCET_REQUEST_FAILED'); } }; @@ -302,7 +317,7 @@ export default class Relay { public static checkTorStatus = async () => { try { - const response = await RestClient.get(TOR_ENDPOINT, { timeout: 20000 }); + const response = await RestClient.get(TOR_ENDPOINT, undefined, { timeout: 20000 }); const data = (response as AxiosResponse).data || (response as any).json; return data.IsTor; } catch (error) { diff --git a/src/services/electrum/client.ts b/src/services/electrum/client.ts index 129db5a320..a9e769b7a3 100644 --- a/src/services/electrum/client.ts +++ b/src/services/electrum/client.ts @@ -7,6 +7,7 @@ import torrific from './torrific'; import RestClient, { TorStatus } from '../rest/RestClient'; import ecc from '../wallets/operations/taproot-utils/noble_ecc'; import { store } from 'src/store/store'; +import { getFailoverPeers } from './peerSelection'; import { classifyElectrumConnectionError, ElectrumConnectionErrorType, @@ -22,14 +23,14 @@ const ELECTRUM_CLIENT_CONFIG: { reconnectDelay: 500, // retry after half a second }; -const ELECTRUM_CLIENT_DEFAULTS = { +const createElectrumClientState = () => ({ electrumClient: null, isClientConnected: false, currentPeerIndex: -1, connectionAttempt: 0, activePeer: null, peers: [], -}; +}); let lastConnectionError: { type: ElectrumConnectionErrorType; @@ -44,7 +45,7 @@ export let ELECTRUM_CLIENT: { connectionAttempt: number; activePeer: NodeDetail; peers: NodeDetail[]; -} = ELECTRUM_CLIENT_DEFAULTS; +} = createElectrumClientState(); export const ELECTRUM_NOT_CONNECTED_ERR = 'Network Error: The current electrum node is not reachable, please try again with a different node'; @@ -73,22 +74,22 @@ export default class ElectrumClient { ELECTRUM_CLIENT.activePeer?.host?.endsWith('.onion') && RestClient?.getTorStatus() === TorStatus.CONNECTED; - ELECTRUM_CLIENT.electrumClient = new ElectrumCli( + const client = new ElectrumCli( ElectrumClient.connectOverTor ? torrific : global.net, global.tls, ELECTRUM_CLIENT.activePeer.port, ELECTRUM_CLIENT.activePeer.host, ELECTRUM_CLIENT.activePeer.useSSL ? 'tls' : 'tcp' ); // tcp or tls + ELECTRUM_CLIENT.electrumClient = client; - ELECTRUM_CLIENT.electrumClient.onError = (error) => { - if (ELECTRUM_CLIENT.isClientConnected) { + client.onError = (error) => { + if (ELECTRUM_CLIENT.electrumClient === client && ELECTRUM_CLIENT.isClientConnected) { console.log('Electrum mainClient.onError():', error?.message || error); - if (ELECTRUM_CLIENT.electrumClient?.close) ELECTRUM_CLIENT.electrumClient.close(); - ELECTRUM_CLIENT.isClientConnected = false; ELECTRUM_CLIENT.activePeer.isConnected = false; + if (client?.close) client.close(); console.log('Error: Close the connection'); // setTimeout( @@ -105,7 +106,7 @@ export default class ElectrumClient { new Promise((resolve) => { timeoutId = setTimeout(() => resolve('timeout'), 20000); }), - ELECTRUM_CLIENT.electrumClient.initElectrum({ + client.initElectrum({ client: 'btc-k', version: '1.4', }), // should resolve within 4 seconds(prior to timeout) @@ -121,6 +122,7 @@ export default class ElectrumClient { lastConnectionError = null; ELECTRUM_CLIENT.isClientConnected = true; ELECTRUM_CLIENT.activePeer.isConnected = true; + ELECTRUM_CLIENT.connectionAttempt = 0; } } catch (error) { ELECTRUM_CLIENT.isClientConnected = false; @@ -176,7 +178,7 @@ export default class ElectrumClient { } ELECTRUM_CLIENT.activePeer = nextPeer; - ELECTRUM_CLIENT.connectionAttempt = 1; + ELECTRUM_CLIENT.connectionAttempt = 0; console.log(`Attempting a connection with next peer: ${nextPeer?.host}`); return ElectrumClient.connect(); } @@ -200,9 +202,9 @@ export default class ElectrumClient { public static forceDisconnect() { if (!ELECTRUM_CLIENT.electrumClient) throw new Error('Electrum client not available'); - if (ELECTRUM_CLIENT.electrumClient?.close) ELECTRUM_CLIENT.electrumClient.close(); ELECTRUM_CLIENT.isClientConnected = false; - ELECTRUM_CLIENT.activePeer.isConnected = false; + if (ELECTRUM_CLIENT.activePeer) ELECTRUM_CLIENT.activePeer.isConnected = false; + if (ELECTRUM_CLIENT.electrumClient?.close) ELECTRUM_CLIENT.electrumClient.close(); } public static async serverFeatures() { @@ -246,20 +248,20 @@ export default class ElectrumClient { // if current peer to use is provided, it will use that peer public static setActivePeer(nodes: NodeDetail[], currentPeerToUse?: NodeDetail) { // close previous connection - if (ELECTRUM_CLIENT.isClientConnected && ELECTRUM_CLIENT.electrumClient?.close) { + ELECTRUM_CLIENT.isClientConnected = false; + if (ELECTRUM_CLIENT.electrumClient?.close) { ELECTRUM_CLIENT.electrumClient.close(); } // set defaults - ELECTRUM_CLIENT = ELECTRUM_CLIENT_DEFAULTS; + ELECTRUM_CLIENT = createElectrumClientState(); + lastConnectionError = null; // set active node - let activeNode = currentPeerToUse || nodes.find((node) => node.isConnected); + const activeNode = currentPeerToUse || nodes.find((node) => node.isConnected); ELECTRUM_CLIENT.activePeer = activeNode; - - if (nodes) { - ELECTRUM_CLIENT.peers = nodes; - } + ELECTRUM_CLIENT.peers = getFailoverPeers(nodes, activeNode); + ELECTRUM_CLIENT.currentPeerIndex = activeNode ? 0 : -1; } public static splitIntoChunks(arr, chunkSize) { diff --git a/src/services/electrum/peerSelection.ts b/src/services/electrum/peerSelection.ts new file mode 100644 index 0000000000..6b706ed61c --- /dev/null +++ b/src/services/electrum/peerSelection.ts @@ -0,0 +1,32 @@ +import { NodeDetail } from 'src/services/wallets/interfaces'; +import { NetworkType } from 'src/services/wallets/enums'; +import { predefinedMainnetNodes, predefinedTestnetNodes } from './predefinedNodes'; + +const isPredefinedNode = (node: NodeDetail): boolean => { + const predefinedNodes = + node.networkType === NetworkType.TESTNET ? predefinedTestnetNodes : predefinedMainnetNodes; + + return predefinedNodes.some( + (candidate) => + candidate.id === node.id && + candidate.host === node.host && + candidate.port === node.port && + candidate.useSSL === node.useSSL + ); +}; + +/** Only a selected public server may fail over to other saved public servers. */ +export const getFailoverPeers = (nodes: NodeDetail[], selectedPeer: NodeDetail): NodeDetail[] => { + if (!selectedPeer) return []; + if (!isPredefinedNode(selectedPeer)) return [selectedPeer]; + + return [ + selectedPeer, + ...nodes.filter( + (node) => + node.networkType === selectedPeer.networkType && + node.id !== selectedPeer.id && + isPredefinedNode(node) + ), + ]; +}; diff --git a/src/services/electrum/predefinedNodes.ts b/src/services/electrum/predefinedNodes.ts index 967ce7fac2..36436b95cf 100644 --- a/src/services/electrum/predefinedNodes.ts +++ b/src/services/electrum/predefinedNodes.ts @@ -11,6 +11,15 @@ export const predefinedTestnetNodes: NodeDetail[] = [ useSSL: true, networkType: NetworkType.TESTNET, }, + { + id: 337, + host: 'bitcoin.stagemole.eu', + port: '5010', + isConnected: false, + useKeeperNode: false, + useSSL: true, + networkType: NetworkType.TESTNET, + }, ]; export const predefinedMainnetNodes: NodeDetail[] = [ diff --git a/src/services/rest/RestClient.ts b/src/services/rest/RestClient.ts index e505ec76f1..8ec9f70ed1 100644 --- a/src/services/rest/RestClient.ts +++ b/src/services/rest/RestClient.ts @@ -65,22 +65,29 @@ class RestClient { path: string, body: object, headers?: object, - options?: AxiosRequestConfig + options: AxiosRequestConfig = {} ): Promise { return axios.post(path, body, { ...options, headers: { ...RestClient.headers, ...headers, + ...options.headers, }, }); } - async get(path: string, headers?: object): Promise { + async get( + path: string, + headers?: object, + options: AxiosRequestConfig = {} + ): Promise { return axios.get(path, { + ...options, headers: { ...RestClient.headers, ...headers, + ...options.headers, }, }); } diff --git a/src/store/reducers/network.ts b/src/store/reducers/network.ts index e037782403..f9ec5bb925 100644 --- a/src/store/reducers/network.ts +++ b/src/store/reducers/network.ts @@ -5,10 +5,12 @@ const initialState: { exchangeRates: ExchangeRates; averageTxFees: AverageTxFeesByNetwork; initialNodesSaved: Boolean; + testnetFallbackNodeAdded: boolean; } = { exchangeRates: null, averageTxFees: null, initialNodesSaved: false, + testnetFallbackNodeAdded: false, }; const networkSlice = createSlice({ @@ -26,9 +28,18 @@ const networkSlice = createSlice({ setInitialNodesSaved: (state, action: PayloadAction) => { state.initialNodesSaved = action.payload; }, + + setTestnetFallbackNodeAdded: (state) => { + state.testnetFallbackNodeAdded = true; + }, }, }); -export const { setExchangeRates, setAverageTxFee, setInitialNodesSaved } = networkSlice.actions; +export const { + setExchangeRates, + setAverageTxFee, + setInitialNodesSaved, + setTestnetFallbackNodeAdded, +} = networkSlice.actions; export default networkSlice.reducer; diff --git a/src/store/reducers/wallets.ts b/src/store/reducers/wallets.ts index a1b0c00fbf..b92b98e642 100644 --- a/src/store/reducers/wallets.ts +++ b/src/store/reducers/wallets.ts @@ -18,6 +18,7 @@ export type WalletsState = { testCoinsReceived: boolean; testCoinsFailed: boolean; testCoinsQuotaReached: boolean; + testCoinsPending: 'propagation' | 'sync' | 'unknown' | null; introModal: boolean; err: string; @@ -39,6 +40,7 @@ const initialState: WalletsState = { testCoinsReceived: false, testCoinsFailed: false, testCoinsQuotaReached: false, + testCoinsPending: null, introModal: true, @@ -68,6 +70,12 @@ const walletSlice = createSlice({ setTestCoinsQuotaReached: (state, action: PayloadAction) => { state.testCoinsQuotaReached = action.payload; }, + setTestCoinsPending: ( + state, + action: PayloadAction<'propagation' | 'sync' | 'unknown' | null> + ) => { + state.testCoinsPending = action.payload; + }, setIntroModal: (state, action: PayloadAction) => { state.introModal = action.payload; }, @@ -133,6 +141,7 @@ export const { setTestCoinsReceived, setTestCoinsFailed, setTestCoinsQuotaReached, + setTestCoinsPending, setIntroModal, walletGenerationFailed, newWalletCreated, @@ -152,6 +161,7 @@ const walletPersistConfig = { 'testCoinsReceived', 'testCoinsFailed', 'testCoinsQuotaReached', + 'testCoinsPending', 'hasNewWalletsGenerationFailed', 'hasNewWalletsGenerationSucceeded', 'isGeneratingNewWallet', diff --git a/src/store/sagas/faucetVisibility.ts b/src/store/sagas/faucetVisibility.ts new file mode 100644 index 0000000000..2c2fae5db6 --- /dev/null +++ b/src/store/sagas/faucetVisibility.ts @@ -0,0 +1,17 @@ +type VisibleWallet = { + specs?: { + confirmedUTXOs?: { txId: string }[]; + unconfirmedUTXOs?: { txId: string }[]; + balances?: { confirmed: number; unconfirmed: number }; + }; +}; + +/** A faucet transfer is visible only after its UTXO and balance were persisted. */ +export const isFaucetTxVisible = (wallet: VisibleWallet, txid: string): boolean => { + const specs = wallet?.specs; + if (!specs?.balances) return false; + + return [...(specs.confirmedUTXOs || []), ...(specs.unconfirmedUTXOs || [])].some( + (utxo) => utxo.txId?.toLowerCase() === txid.toLowerCase() + ); +}; diff --git a/src/store/sagas/network.ts b/src/store/sagas/network.ts index 4b847db49e..f833fd9a82 100644 --- a/src/store/sagas/network.ts +++ b/src/store/sagas/network.ts @@ -8,7 +8,7 @@ import { } from 'src/services/electrum/predefinedNodes'; import ElectrumClient from 'src/services/electrum/client'; import { captureError } from 'src/services/sentry'; -import { setInitialNodesSaved } from '../reducers/network'; +import { setInitialNodesSaved, setTestnetFallbackNodeAdded } from '../reducers/network'; import { RootState } from '../store'; import { electrumClientConnectionExecuted, @@ -41,19 +41,69 @@ export function* connectToNodeWorker() { bitcoinNetworkType === NetworkType.TESTNET ? predefinedTestnetNodes : predefinedMainnetNodes; - dbManager.createObjectBulk(RealmSchema.NodeConnect, hardcodedInitialNodes); + const created = yield call( + dbManager.createObjectBulk, + RealmSchema.NodeConnect, + hardcodedInitialNodes + ); + if (!created) throw new Error('Unable to save initial Electrum servers'); } yield put(setInitialNodesSaved(true)); } + // Existing Testnet installations only saved the first public server. Add the + // second once when that original server is still saved; an explicit deletion + // after this migration must not be undone at every reconnect. + const fallbackNodeAdded = yield select( + (state: RootState) => state.network.testnetFallbackNodeAdded + ); + if (bitcoinNetworkType === NetworkType.TESTNET && !fallbackNodeAdded) { + const savedNodes = yield call(dbManager.getCollection, RealmSchema.NodeConnect); + const originalNode = predefinedTestnetNodes[0]; + const fallbackNode = predefinedTestnetNodes[1]; + if ( + savedNodes.some( + (node) => + node.id === originalNode.id && + node.host === originalNode.host && + node.port === originalNode.port + ) && + !savedNodes.some((node) => node.id === fallbackNode.id) + ) { + const created = yield call(dbManager.createObject, RealmSchema.NodeConnect, fallbackNode); + if (!created) throw new Error('Unable to save Testnet fallback server'); + } + yield put(setTestnetFallbackNodeAdded()); + } + const nodes = (yield call(dbManager.getCollection, RealmSchema.NodeConnect)).filter( (node) => node.networkType === bitcoinNetworkType ); + const selectedNode = nodes.find((node) => node.isConnected); ElectrumClient.setActivePeer(nodes); const { connected, connectedTo, error } = yield call(ElectrumClient.connect); if (connected) { + const connectedNode = ElectrumClient.getActivePeer(); + if (selectedNode && connectedNode?.id !== selectedNode.id) { + yield call( + dbManager.updateObjectById, + RealmSchema.NodeConnect, + selectedNode.id.toString(), + { + isConnected: false, + } + ); + yield call( + dbManager.updateObjectById, + RealmSchema.NodeConnect, + connectedNode.id.toString(), + { + isConnected: true, + } + ); + } yield put(electrumClientConnectionExecuted({ successful: connected, connectedTo })); } else { yield put(electrumClientConnectionExecuted({ successful: connected, error })); @@ -61,6 +111,12 @@ export function* connectToNodeWorker() { yield put(fetchFeeRates()); } catch (err) { captureError(err); + yield put( + electrumClientConnectionExecuted({ + successful: false, + error: err?.message || 'Unable to connect to Electrum server', + }) + ); } } diff --git a/src/store/sagas/wallets.ts b/src/store/sagas/wallets.ts index d0c9c9f711..696be0960f 100644 --- a/src/store/sagas/wallets.ts +++ b/src/store/sagas/wallets.ts @@ -6,6 +6,7 @@ import { EntityKind, MiniscriptTypes, MultisigScriptType, + NetworkType, SignerType, VaultType, VisibilityType, @@ -32,6 +33,7 @@ import { setTestCoinsFailed, setTestCoinsReceived, setTestCoinsQuotaReached, + setTestCoinsPending, setSignerPolicyError, finishRefreshRequest, } from 'src/store/reducers/wallets'; @@ -90,7 +92,6 @@ import { TEST_SATS_RECIEVE, UPDATE_SIGNER_POLICY, UPDATE_WALLET_DETAILS, - refreshWallets, UPDATE_SIGNER_DETAILS, UPDATE_KEY_DETAILS, UPDATE_VAULT_DETAILS, @@ -143,6 +144,7 @@ import { updateDelayedPolicyUpdate } from '../reducers/storage'; import { accountNoFromDerivationPath } from 'src/utils/service-utilities/utils'; import { classifyDustByAddress } from 'src/services/wallets/operations/dustClassification'; import { setPendingDustToast } from '../reducers/utxos'; +import { isFaucetTxVisible } from './faucetVisibility'; export interface NewVaultDetails { name?: string; @@ -1135,27 +1137,75 @@ export const updateSignerPolicyWatcher = createWatcher( UPDATE_SIGNER_POLICY ); -function* testcoinsWorker({ payload }) { +export function* testcoinsWorker({ payload }) { const { wallet } = payload; - const receivingAddress = WalletOperations.getNextFreeAddress(wallet); - const network = WalletUtilities.getNetworkByType(wallet.networkType); - const appId: string = yield select((state: RootState) => state.storage.appId); + yield put(setTestCoinsReceived(false)); + yield put(setTestCoinsFailed(false)); + yield put(setTestCoinsPending(null)); + if (wallet.networkType !== NetworkType.TESTNET) { + yield put(setTestCoinsFailed(true)); + return; + } + + let receivingAddress: string; + let network: ReturnType; + let txid: string; try { - const { txid } = yield call(Relay.getTestcoins, receivingAddress, network, appId); - if (!txid) { - yield put(setTestCoinsFailed(true)); - } else { - yield put(setTestCoinsReceived(true)); - yield put(refreshWallets([wallet], { hardRefresh: true })); - } + receivingAddress = WalletOperations.getNextFreeAddress(wallet); + network = WalletUtilities.getNetworkByType(wallet.networkType); + const appId: string = yield select((state: RootState) => state.storage.appId); + const response = yield call(Relay.getTestcoins, receivingAddress, wallet.networkType, appId); + txid = response.txid; } catch (err) { if (err.message === 'FAUCET_DAILY_LIMIT_REACHED') { yield put(setTestCoinsQuotaReached(true)); + } else if (err.message === 'FAUCET_OUTCOME_UNKNOWN') { + yield put(setTestCoinsPending('unknown')); } else { yield put(setTestCoinsFailed(true)); } + return; + } + + let lastAttemptSynced = false; + for (let attempt = 0; attempt < 8; attempt += 1) { + let attemptSynced = false; + try { + if ( + !ELECTRUM_CLIENT.isClientConnected || + ELECTRUM_CLIENT.activePeer?.networkType !== wallet.networkType + ) { + yield call(connectToNodeWorker); + } + const { txids } = yield call( + [ElectrumClient, ElectrumClient.syncHistoryByAddress], + [receivingAddress], + network + ); + attemptSynced = true; + if (txids.some((seenTxid: string) => seenTxid.toLowerCase() === txid.toLowerCase())) { + const refreshed = yield call(refreshWalletsWorker, { + payload: { wallets: [wallet], options: { hardRefresh: true } }, + }); + if (refreshed) { + const schema = + wallet.entityKind === EntityKind.VAULT ? RealmSchema.Vault : RealmSchema.Wallet; + const storedWallet = yield call(dbManager.getObjectById, schema, wallet.id); + const storedData = storedWallet?.toJSON?.() || storedWallet; + if (isFaucetTxVisible(storedData, txid)) { + yield put(setTestCoinsReceived(true)); + return; + } + } else attemptSynced = false; + } + } catch (err) { + attemptSynced = false; + } + lastAttemptSynced = attemptSynced; + if (attempt < 7) yield delay(4000); } + yield put(setTestCoinsPending(lastAttemptSynced ? 'propagation' : 'sync')); } export const testcoinsWatcher = createWatcher(testcoinsWorker, TEST_SATS_RECIEVE); diff --git a/tests/services/faucetRequest.test.ts b/tests/services/faucetRequest.test.ts new file mode 100644 index 0000000000..88051e36d4 --- /dev/null +++ b/tests/services/faucetRequest.test.ts @@ -0,0 +1,60 @@ +import Relay from '../../src/services/backend/Relay'; +import RestClient from '../../src/services/rest/RestClient'; +import { NetworkType } from '../../src/services/wallets/enums'; + +jest.mock('src/services/rest/RestClient', () => ({ + __esModule: true, + default: { post: jest.fn() }, +})); +jest.mock('src/utils/service-utilities/config', () => ({ + __esModule: true, + default: { RELAY: 'https://relay.example/' }, +})); +jest.mock('src/services/sentry', () => ({ captureError: jest.fn() })); + +const post = RestClient.post as jest.Mock; +const txid = 'a'.repeat(64); + +describe('Testnet faucet request', () => { + beforeEach(() => post.mockReset()); + + it('refuses a non-Testnet wallet before contacting the relay', async () => { + await expect(Relay.getTestcoins('address', NetworkType.MAINNET, 'app')).rejects.toThrow( + 'Invalid network' + ); + expect(post).not.toHaveBeenCalled(); + }); + + it('sends a bounded, single request and returns a valid funded txid', async () => { + post.mockResolvedValue({ data: { txid, funded: true } }); + + await expect(Relay.getTestcoins('address', NetworkType.TESTNET, 'app')).resolves.toEqual({ + txid, + funded: true, + }); + expect(post).toHaveBeenCalledTimes(1); + expect(post).toHaveBeenCalledWith( + 'https://relay.example/testnetFaucet', + { recipientAddress: 'address', appId: 'app' }, + undefined, + { timeout: 20000 } + ); + }); + + it('marks a timed-out response as uncertain without retrying', async () => { + post.mockRejectedValue({ code: 'ECONNABORTED' }); + + await expect(Relay.getTestcoins('address', NetworkType.TESTNET, 'app')).rejects.toThrow( + 'FAUCET_OUTCOME_UNKNOWN' + ); + expect(post).toHaveBeenCalledTimes(1); + }); + + it('keeps the daily quota separate from an uncertain outcome', async () => { + post.mockRejectedValue({ response: { status: 429 } }); + + await expect(Relay.getTestcoins('address', NetworkType.TESTNET, 'app')).rejects.toThrow( + 'FAUCET_DAILY_LIMIT_REACHED' + ); + }); +}); diff --git a/tests/services/faucetVisibility.test.ts b/tests/services/faucetVisibility.test.ts new file mode 100644 index 0000000000..5732b6f106 --- /dev/null +++ b/tests/services/faucetVisibility.test.ts @@ -0,0 +1,22 @@ +import { isFaucetTxVisible } from '../../src/store/sagas/faucetVisibility'; + +const txid = 'a'.repeat(64); + +describe('faucet transaction visibility', () => { + it('requires the funded UTXO in persisted specs', () => { + const wallet = { + specs: { + balances: { confirmed: 0, unconfirmed: 1000 }, + confirmedUTXOs: [], + unconfirmedUTXOs: [{ txId: txid }], + }, + }; + expect(isFaucetTxVisible(wallet, txid)).toBe(true); + expect(isFaucetTxVisible(wallet, 'b'.repeat(64))).toBe(false); + }); + + it('does not report receipt from a txid alone or before balance persistence', () => { + expect(isFaucetTxVisible({}, txid)).toBe(false); + expect(isFaucetTxVisible({ specs: { unconfirmedUTXOs: [{ txId: txid }] } }, txid)).toBe(false); + }); +}); diff --git a/tests/services/networkFailover.test.ts b/tests/services/networkFailover.test.ts new file mode 100644 index 0000000000..a65eefa84a --- /dev/null +++ b/tests/services/networkFailover.test.ts @@ -0,0 +1,42 @@ +import { getFailoverPeers } from '../../src/services/electrum/peerSelection'; +import { + predefinedMainnetNodes, + predefinedTestnetNodes, +} from '../../src/services/electrum/predefinedNodes'; + +describe('Electrum failover peer selection', () => { + it('retries only the selected private node', () => { + const privateNode = { + ...predefinedTestnetNodes[0], + id: 1001, + host: 'private.example', + }; + + expect(getFailoverPeers([predefinedTestnetNodes[0], privateNode], privateNode)).toEqual([ + privateNode, + ]); + }); + + it('uses saved public peers on the same network and never a private node', () => { + const privateNode = { + ...predefinedTestnetNodes[0], + id: 1001, + host: 'private.example', + }; + const peers = getFailoverPeers( + [ + predefinedMainnetNodes[0], + predefinedTestnetNodes[1], + privateNode, + predefinedTestnetNodes[0], + ], + predefinedTestnetNodes[0] + ); + + expect(peers).toEqual(predefinedTestnetNodes); + }); + + it('does not reconnect after an explicit disconnect', () => { + expect(getFailoverPeers(predefinedTestnetNodes, null)).toEqual([]); + }); +}); diff --git a/tests/services/restClient.requestConfig.test.ts b/tests/services/restClient.requestConfig.test.ts new file mode 100644 index 0000000000..565221e15c --- /dev/null +++ b/tests/services/restClient.requestConfig.test.ts @@ -0,0 +1,53 @@ +import axios from 'axios'; +import RestClient from '../../src/services/rest/RestClient'; + +jest.mock('axios'); +jest.mock('react-native-device-info', () => ({ + getVersion: () => 'test', + getBuildNumber: () => '1', +})); +jest.mock('src/utils/service-utilities/config', () => ({ + __esModule: true, + default: { HEXA_ID: 'test-id' }, +})); + +describe('RestClient request options', () => { + beforeEach(() => { + (axios.get as jest.Mock).mockResolvedValue({ data: {} }); + (axios.post as jest.Mock).mockResolvedValue({ data: {} }); + }); + + it('passes GET deadline to Axios instead of sending it as an HTTP header', async () => { + await RestClient.get('https://example.test/status', { Authorization: 'test' }, { timeout: 20 }); + + expect(axios.get).toHaveBeenCalledWith( + 'https://example.test/status', + expect.objectContaining({ + timeout: 20, + headers: expect.objectContaining({ Authorization: 'test' }), + }) + ); + expect((axios.get as jest.Mock).mock.calls[0][1].headers.timeout).toBeUndefined(); + }); + + it('forwards POST cancellation without losing existing headers', async () => { + const controller = new AbortController(); + await RestClient.post( + 'https://example.test/request', + {}, + { Authorization: 'test' }, + { + signal: controller.signal, + } + ); + + expect(axios.post).toHaveBeenCalledWith( + 'https://example.test/request', + {}, + expect.objectContaining({ + signal: controller.signal, + headers: expect.objectContaining({ Authorization: 'test', 'HEXA-ID': 'test-id' }), + }) + ); + }); +}); From 7afb05fbd012e8275fc4949d4839e95c73b84b5c Mon Sep 17 00:00:00 2001 From: Anant Date: Thu, 8 Oct 2026 17:56:43 +0530 Subject: [PATCH 066/254] ci: cover R4 branches and reject unsigned diagnostic bundles --- .github/workflows/android-diagnostic-build.yml | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/.github/workflows/android-diagnostic-build.yml b/.github/workflows/android-diagnostic-build.yml index 2099b71658..a272100d6d 100644 --- a/.github/workflows/android-diagnostic-build.yml +++ b/.github/workflows/android-diagnostic-build.yml @@ -3,7 +3,7 @@ name: Android diagnostic build on: pull_request: branches: - - release/2.6.3 + - 'release/**' - sprint - dev - main @@ -100,7 +100,15 @@ jobs: version=$(node -p 'require("./release/version.json").version') version_code=$(node -p 'require("./release/version.json").androidVersionCode') grep -Fq "package: name='io.hexawallet.bitcoinkeeper' versionCode='$version_code' versionName='$version'" <<< "$badging" - jarsigner -verify "$aab" >/dev/null + if ! jarsigner -verify -verbose -certs "$aab" > "$RUNNER_TEMP/aab-signature.log" 2>&1; then + tail -80 "$RUNNER_TEMP/aab-signature.log" + exit 1 + fi + if ! grep -Fxq 'jar verified.' "$RUNNER_TEMP/aab-signature.log" || grep -Fq '? = unsigned entry' "$RUNNER_TEMP/aab-signature.log"; then + tail -80 "$RUNNER_TEMP/aab-signature.log" + echo 'AAB is unsigned or contains unsigned entries' >&2 + exit 1 + fi unzip -tq "$apk" >/dev/null unzip -tq "$aab" >/dev/null sha256sum "$apk" "$aab" From aa60721c4ed526e129a4760846ba60c9e9d79cc8 Mon Sep 17 00:00:00 2001 From: Anant Date: Thu, 8 Oct 2026 18:05:47 +0530 Subject: [PATCH 067/254] fix(sync): fence account and network scope during refresh --- src/navigation/AppStateHandler.tsx | 19 ++- src/screens/Home/HomeScreen.tsx | 15 +-- src/services/wallets/operations/index.ts | 13 +- src/store/sagas/bhr.ts | 3 +- src/store/sagas/utxos.ts | 47 +++++-- src/store/sagas/wallets.ts | 163 ++++++++++++++++++----- 6 files changed, 192 insertions(+), 68 deletions(-) diff --git a/src/navigation/AppStateHandler.tsx b/src/navigation/AppStateHandler.tsx index 560b474af8..34ef589e62 100644 --- a/src/navigation/AppStateHandler.tsx +++ b/src/navigation/AppStateHandler.tsx @@ -3,6 +3,7 @@ import { AppState, Linking } from 'react-native'; import { useNavigation } from '@react-navigation/native'; import { useDispatch } from 'react-redux'; import { setHasDeepLink } from 'src/store/reducers/login'; +import { autoSyncWallets } from 'src/store/sagaActions/wallets'; const PASSCODE_TIMEOUT = 5 * 60 * 1000; @@ -27,13 +28,11 @@ const AppStateHandler = () => { } else if (appState.match(/inactive|background/) && nextAppState === 'active') { // App is coming to the foreground, check elapsed time const state = navigation.getState(); - const notLoginStack = state?.routes[state.index]?.name !== 'LoginStack'; + const activeRoute = state?.routes[state.index]; + const notLoginStack = activeRoute?.name !== 'LoginStack'; + const timedOut = lastBackgroundTime && Date.now() - lastBackgroundTime > PASSCODE_TIMEOUT; - if ( - notLoginStack && - lastBackgroundTime && - Date.now() - lastBackgroundTime > PASSCODE_TIMEOUT - ) { + if (notLoginStack && timedOut) { dispatch(setHasDeepLink(triggeredDeepLink.current)); triggeredDeepLink.current = null; navigation.reset({ @@ -47,6 +46,14 @@ const AppStateHandler = () => { }, ], }); + } else if (activeRoute?.name === 'App') { + // The transient login.isAuthenticated flag is cleared after unlock. + // Only an active, unlocked app session should poll archived Vaults. + const childState = activeRoute.state; + const childRoute = childState?.routes[childState.index || 0]; + if (childRoute?.name !== 'Login') { + dispatch(autoSyncWallets(false, false, false, undefined, true)); + } } } setAppState(nextAppState); diff --git a/src/screens/Home/HomeScreen.tsx b/src/screens/Home/HomeScreen.tsx index bf3e16d4a4..9640e147c5 100644 --- a/src/screens/Home/HomeScreen.tsx +++ b/src/screens/Home/HomeScreen.tsx @@ -1,4 +1,4 @@ -import { AppState, StyleSheet, TouchableOpacity, View } from 'react-native'; +import { StyleSheet, TouchableOpacity, View } from 'react-native'; import { Box, useColorMode } from '@gluestack-ui/themed-native-base'; import React, { useContext, useEffect, useRef, useState } from 'react'; import useWallets from 'src/hooks/useWallets'; @@ -31,7 +31,6 @@ import dbManager from 'src/storage/realm/dbManager'; import { setRecoveryKeyStatus } from 'src/store/reducers/account'; import RecoveryKeyIcon from 'src/assets/images/recover_white.svg'; import HelpAiEntry from '../HelpAi/HelpAiEntry'; -import { autoSyncWallets } from 'src/store/sagaActions/wallets'; function NewHomeScreen({ route }) { const { colorMode } = useColorMode(); @@ -53,8 +52,6 @@ function NewHomeScreen({ route }) { const backupHistory = useQuery(RealmSchema.BackupHistory); const { recoveryKeyStatusByAppId } = useAppSelector((state) => state.account); const { bitcoinNetworkType } = useAppSelector((state) => state.settings); - const isAuthenticated = useAppSelector((state) => state.login.isAuthenticated); - const appState = useRef(AppState.currentState); const { id } = dbManager.getObjectByIndex(RealmSchema.KeeperApp) as any; // 'idle' | 'education' | 'skipWarning' @@ -64,16 +61,6 @@ function NewHomeScreen({ route }) { // Session flag: prevent re-showing the education sheet after the user dismisses it const hasShownEducationSheetRef = useRef(false); - useEffect(() => { - const subscription = AppState.addEventListener('change', (nextState) => { - if (appState.current?.match(/inactive|background/) && nextState === 'active' && isAuthenticated) { - dispatch(autoSyncWallets(false, false, false, undefined, true)); - } - appState.current = nextState; - }); - return () => subscription.remove(); - }, [dispatch, isAuthenticated]); - const recoveryKeyStatus = recoveryKeyStatusByAppId?.[id]; const isConfirmed = recoveryKeyStatus === 'confirmed'; diff --git a/src/services/wallets/operations/index.ts b/src/services/wallets/operations/index.ts index 7401a3ffcc..db29ee4324 100644 --- a/src/services/wallets/operations/index.ts +++ b/src/services/wallets/operations/index.ts @@ -382,7 +382,8 @@ export default class WalletOperations { addresses: string[], externalAddresses: { [address: string]: number }, internalAddresses: { [address: string]: number }, - network: bitcoinJS.Network + network: bitcoinJS.Network, + assertCurrentScope?: () => void ) => { let transactions = wallet.specs.transactions; let lastUsedAddressIndex = wallet.specs.nextFreeAddressIndex - 1; @@ -395,6 +396,7 @@ export default class WalletOperations { txidToIndex[transaction.txid] = index; } + assertCurrentScope?.(); const { txids, txidToAddress } = await ElectrumClient.syncHistoryByAddress(addresses, network); let newTxids = txids.filter((txid) => { @@ -409,6 +411,7 @@ export default class WalletOperations { ) ); + assertCurrentScope?.(); const txs = await ElectrumClient.getTransactionsById(newTxids); // fetch input transactions(for new ones), in order to construct the inputs @@ -417,6 +420,7 @@ export default class WalletOperations { if (txidToIndex[txid] !== undefined) continue; // transaction is already present(don't need to reconstruct using inputTxids) for (const vin of txs[txid].vin) inputTxIds.push(vin.txid); } + assertCurrentScope?.(); const inputTxs = await ElectrumClient.getTransactionsById(inputTxIds); let hasNewUpdates = false; @@ -483,7 +487,8 @@ export default class WalletOperations { static syncWalletsViaElectrumClient = async ( wallets: (Wallet | Vault)[], network: bitcoinJS.networks.Network, - hardRefresh?: boolean + hardRefresh?: boolean, + assertCurrentScope?: () => void ): Promise<{ synchedWallets: SyncedWallet[]; }> => { @@ -630,6 +635,7 @@ export default class WalletOperations { currentRecheckInternal = wallet.specs.nextFreeChangeAddressIndex + gapLimit; // sync utxos & balances + assertCurrentScope?.(); const utxosByAddress = await ElectrumClient.syncUTXOByAddress( addresses.concat( !hardRefresh && !walletHasNewUpdates @@ -762,7 +768,8 @@ export default class WalletOperations { addresses, externalAddresses, internalAddresses, - network + network, + assertCurrentScope ); walletHasNewUpdates = diff --git a/src/store/sagas/bhr.ts b/src/store/sagas/bhr.ts index ba417ac0e4..996d245f87 100644 --- a/src/store/sagas/bhr.ts +++ b/src/store/sagas/bhr.ts @@ -1094,10 +1094,11 @@ function* deleteBackupWorker() { export const deleteBackupWatcher = createWatcher(deleteBackupWorker, DELETE_BACKUP); -export function* checkBackupCondition() { +export function* checkBackupCondition(expectedAppId?: string) { const { automaticCloudBackup } = yield select((state: RootState) => state.bhr); if (!automaticCloudBackup) return true; const { id }: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); + if (expectedAppId && id !== expectedAppId) return true; markBackupMutation(id); yield put(invalidateBackupRepair(id)); const netInfo = yield call(NetInfo.fetch); diff --git a/src/store/sagas/utxos.ts b/src/store/sagas/utxos.ts index 7f3d1addec..5845863f1c 100644 --- a/src/store/sagas/utxos.ts +++ b/src/store/sagas/utxos.ts @@ -2,7 +2,7 @@ import dbManager from 'src/storage/realm/dbManager'; import { RealmSchema } from 'src/storage/realm/enum'; import { call, delay, fork, put, takeLatest } from 'redux-saga/effects'; import { BIP329Label, UTXO, UTXOSpendability } from 'src/services/wallets/interfaces'; -import { EntityKind, LabelRefType } from 'src/services/wallets/enums'; +import { EntityKind, LabelRefType, NetworkType } from 'src/services/wallets/enums'; import Relay from 'src/services/backend/Relay'; import { Wallet } from 'src/services/wallets/interfaces/wallet'; import { generateAbbreviatedOutputDescriptors } from 'src/utils/service-utilities/utils'; @@ -25,6 +25,28 @@ import { updateVaultImageWorker, } from './bhr'; import { encrypt, generateEncryptionKey, hash256 } from 'src/utils/service-utilities/encryption'; +import { store } from '../store'; + +type LabelSyncScope = { appId: string; networkType: NetworkType }; + +const isCurrentLabelScope = (scope?: LabelSyncScope) => + !scope || + (store.getState().storage.appId === scope.appId && + store.getState().settings.bitcoinNetworkType === scope.networkType); + +// Realm identity and tag writes must happen without a saga yield between them. +const persistScopedLabels = ( + addedTags: BIP329Label[], + deletedTagIds: string[], + scope?: LabelSyncScope +): KeeperApp | null => { + if (!isCurrentLabelScope(scope)) return null; + const keeperApp = dbManager.getObjectByIndex(RealmSchema.KeeperApp) as unknown as KeeperApp; + if (!keeperApp || (scope && keeperApp.id !== scope.appId)) return null; + dbManager.createObjectBulk(RealmSchema.Tags, addedTags); + for (const element of deletedTagIds) dbManager.deleteObjectById(RealmSchema.Tags, element); + return keeperApp; +}; export function* addLabelsWorker({ payload, @@ -93,11 +115,13 @@ export function* bulkUpdateLabelsWorker({ txId?: string; address?: string; wallet: Wallet; + scope?: LabelSyncScope; }; }) { try { yield put(setSyncingUTXOs(true)); - const { labelChanges, wallet, UTXO, txId, address } = payload; + const { labelChanges, wallet, UTXO, txId, address, scope } = payload; + if (scope && (!isCurrentLabelScope(scope) || wallet.networkType !== scope.networkType)) return; const origin = generateAbbreviatedOutputDescriptors(wallet); let addedTags: BIP329Label[] = []; let deletedTagIds: string[] = []; @@ -117,17 +141,18 @@ export function* bulkUpdateLabelsWorker({ if (labelChanges.deleted) { deletedTagIds = labelChanges.deleted.map((label) => `${ref}${label.name}`); } - const { primarySeed, id }: KeeperApp = yield call( - dbManager.getObjectByIndex, - RealmSchema.KeeperApp + const keeperApp: KeeperApp | null = yield call( + persistScopedLabels, + addedTags, + deletedTagIds, + scope ); - yield call(dbManager.createObjectBulk, RealmSchema.Tags, addedTags); - for (const element of deletedTagIds) { - yield call(dbManager.deleteObjectById, RealmSchema.Tags, element); - } + if (!keeperApp) return; + const { primarySeed, id } = keeperApp; try { - const backupResponse = yield call(checkBackupCondition); - if (!backupResponse) { + if (!isCurrentLabelScope(scope)) return; + const backupResponse = yield call(checkBackupCondition, scope?.appId); + if (!backupResponse && isCurrentLabelScope(scope)) { const encryptionKey = generateEncryptionKey(primarySeed); const tagsToBackup = addedTags.map((tag) => ({ id: hash256(hash256(encryptionKey + tag.id)), diff --git a/src/store/sagas/wallets.ts b/src/store/sagas/wallets.ts index 696be0960f..3b383566c5 100644 --- a/src/store/sagas/wallets.ts +++ b/src/store/sagas/wallets.ts @@ -27,7 +27,7 @@ import { WalletImportDetails, WalletPresentationData, } from 'src/services/wallets/interfaces/wallet'; -import { call, delay, fork, put, select } from 'redux-saga/effects'; +import { call, delay, put, select } from 'redux-saga/effects'; import { setSyncing, setTestCoinsFailed, @@ -73,7 +73,7 @@ import { sanitizeSeedKeyForBackup, } from 'src/utils/utilities'; import { COLLABORATIVE_SCHEME } from 'src/screens/SigningDevices/SetupCollaborativeWallet'; -import { RootState } from '../store'; +import { RootState, store } from '../store'; import { selectWalletsForSync } from './walletSyncSelection'; import { acquireWalletRefresh } from './walletRefreshCoordinator'; import { checkBackupFreshness } from '../sagaActions/bhr'; @@ -146,6 +146,8 @@ import { classifyDustByAddress } from 'src/services/wallets/operations/dustClass import { setPendingDustToast } from '../reducers/utxos'; import { isFaucetTxVisible } from './faucetVisibility'; +const WALLET_SYNC_SCOPE_CHANGED = 'WALLET_SYNC_SCOPE_CHANGED'; + export interface NewVaultDetails { name?: string; description?: string; @@ -652,12 +654,7 @@ function* refreshWalletsWorker({ return true; } - lease = acquireWalletRefresh( - refreshScope.appId, - refreshScope.networkType, - wallets, - options - ); + lease = acquireWalletRefresh(refreshScope.appId, refreshScope.networkType, wallets, options); if (!lease.owner) { succeeded = yield call(() => lease.done); return succeeded; @@ -670,24 +667,52 @@ function* refreshWalletsWorker({ if ( readyScope.appId !== refreshScope.appId || readyScope.networkType !== refreshScope.networkType - ) return false; + ) + return false; // A queued refresh must start from the latest persisted specs, and a // stale route must never supply a snapshot from another profile. const currentWallets: (Wallet | Vault)[] = []; for (const wallet of wallets) { - const schema = wallet.entityKind === EntityKind.VAULT ? RealmSchema.Vault : RealmSchema.Wallet; + const schema = + wallet.entityKind === EntityKind.VAULT ? RealmSchema.Vault : RealmSchema.Wallet; const currentWallet = yield call(dbManager.getObjectById, schema, wallet.id); if (!currentWallet || currentWallet.networkType !== refreshScope.networkType) return false; currentWallets.push(getJSONFromRealmObject(currentWallet)); } wallets = currentWallets; - if (!ELECTRUM_CLIENT.isClientConnected) { + let connectionMatchesScope = false; + try { + ElectrumClient.assertConnectionGeneration( + ElectrumClient.getConnectionGeneration(), + refreshScope.networkType + ); + connectionMatchesScope = true; + } catch { + // Reconnect using the current account's saved nodes below. + } + if (!ELECTRUM_CLIENT.isClientConnected || !connectionMatchesScope) { ElectrumClient.resetCurrentPeerIndex(); yield call(connectToNodeWorker); } + const connectionGeneration = ElectrumClient.getConnectionGeneration(); + const assertCurrentScope = () => { + const currentState = store.getState(); + if ( + currentState.storage.appId !== refreshScope.appId || + currentState.settings.bitcoinNetworkType !== refreshScope.networkType + ) + throw new Error(WALLET_SYNC_SCOPE_CHANGED); + try { + ElectrumClient.assertConnectionGeneration(connectionGeneration, refreshScope.networkType); + } catch { + throw new Error(WALLET_SYNC_SCOPE_CHANGED); + } + }; + assertCurrentScope(); + yield put(setSyncing({ wallets, isSyncing: true })); const network = WalletUtilities.getNetworkByType(wallets[0].networkType); @@ -726,8 +751,10 @@ function* refreshWalletsWorker({ WalletOperations.syncWalletsViaElectrumClient, wallets, network, - options.hardRefresh + options.hardRefresh, + assertCurrentScope ); + assertCurrentScope(); let labels: { ref: string; label: string; isSystem: boolean }[]; @@ -742,7 +769,8 @@ function* refreshWalletsWorker({ if ( currentScope.appId !== refreshScope.appId || currentScope.networkType !== refreshScope.networkType - ) return false; + ) + return false; const { synchedWallet } = synchedWalletWithUTXOs; if (!synchedWallet.specs.hasNewUpdates && !options.hardRefresh) continue; // no new updates found @@ -762,9 +790,18 @@ function* refreshWalletsWorker({ ); } - yield fork(bulkUpdateLabelsWorker, { - payload: { labelChanges, UTXO: utxo, wallet: synchedWallet as any }, - }); + if (labelChanges.added.length || labelChanges.deleted.length) { + assertCurrentScope(); + yield call(bulkUpdateLabelsWorker, { + payload: { + labelChanges, + UTXO: utxo, + wallet: synchedWallet as any, + scope: refreshScope, + }, + }); + assertCurrentScope(); + } if (options.addNotifications) { if (synchedWallet.type !== VaultType.CANARY) { @@ -822,6 +859,7 @@ function* refreshWalletsWorker({ if (txsMissingOutputs.length > 0) { const txidsToFetch = txsMissingOutputs.map((tx: any) => tx.txid); try { + assertCurrentScope(); const rawTxs: Record = yield call( [ElectrumClient, ElectrumClient.getTransactionsById], txidsToFetch @@ -931,7 +969,8 @@ function* refreshWalletsWorker({ if ( writeScope.appId !== refreshScope.appId || writeScope.networkType !== refreshScope.networkType - ) return false; + ) + return false; const persisted = yield call( dbManager.updateObjectById, synchedWallet.entityKind === EntityKind.VAULT ? RealmSchema.Vault : RealmSchema.Wallet, @@ -947,6 +986,7 @@ function* refreshWalletsWorker({ succeeded = true; return true; } catch (err) { + if (err?.message === WALLET_SYNC_SCOPE_CHANGED) return false; if ([ELECTRUM_NOT_CONNECTED_ERR, ELECTRUM_NOT_CONNECTED_ERR_TOR].includes(err?.message)) { yield put( setElectrumNotConnectedErr( @@ -985,7 +1025,8 @@ export function* autoWalletsSyncWorker({ requestId?: string; }; }) { - const { syncAll, hardRefresh, addNotifications, backupCheckAppId, archivedOnly, requestId } = payload; + const { syncAll, hardRefresh, addNotifications, backupCheckAppId, archivedOnly, requestId } = + payload; let activeSucceeded = true; let archivedSucceeded = true; try { @@ -993,10 +1034,17 @@ export function* autoWalletsSyncWorker({ appId: state.storage.appId, networkType: state.settings.bitcoinNetworkType, })); - const wallets: Wallet[] = yield call(dbManager.getObjectByIndex, RealmSchema.Wallet, null, true); + const wallets: Wallet[] = yield call( + dbManager.getObjectByIndex, + RealmSchema.Wallet, + null, + true + ); const vaults: Vault[] = yield call(dbManager.getObjectByIndex, RealmSchema.Vault, null, true); const currentAppId = yield select((state: RootState) => state.storage.appId); - const currentNetworkType = yield select((state: RootState) => state.settings.bitcoinNetworkType); + const currentNetworkType = yield select( + (state: RootState) => state.settings.bitcoinNetworkType + ); if (currentAppId !== syncScope.appId || currentNetworkType !== syncScope.networkType) { activeSucceeded = false; archivedSucceeded = false; @@ -1043,8 +1091,11 @@ export function* autoWalletsSyncWorker({ // Check backup freshness only after the active wallet specs are persisted. if (backupCheckAppId && activeSucceeded) { const { appId } = yield select((state: RootState) => state.storage); - const { automaticCloudBackup, pendingAllBackup, backupRepairCompletedByAppId = {} } = - yield select((state: RootState) => state.bhr); + const { + automaticCloudBackup, + pendingAllBackup, + backupRepairCompletedByAppId = {}, + } = yield select((state: RootState) => state.bhr); if ( appId === backupCheckAppId && automaticCloudBackup && @@ -1059,10 +1110,12 @@ export function* autoWalletsSyncWorker({ captureError(err); } finally { if (requestId) { - yield put(finishRefreshRequest({ - requestId, - succeeded: activeSucceeded && archivedSucceeded, - })); + yield put( + finishRefreshRequest({ + requestId, + succeeded: activeSucceeded && archivedSucceeded, + }) + ); } } } @@ -1143,7 +1196,20 @@ export function* testcoinsWorker({ payload }) { yield put(setTestCoinsFailed(false)); yield put(setTestCoinsPending(null)); - if (wallet.networkType !== NetworkType.TESTNET) { + const faucetScope = yield select((state: RootState) => ({ + appId: state.storage.appId, + networkType: state.settings.bitcoinNetworkType, + })); + const isCurrentFaucetScope = () => { + const state = store.getState(); + return ( + state.storage.appId === faucetScope.appId && + state.settings.bitcoinNetworkType === faucetScope.networkType && + faucetScope.networkType === wallet.networkType + ); + }; + + if (wallet.networkType !== NetworkType.TESTNET || !isCurrentFaucetScope()) { yield put(setTestCoinsFailed(true)); return; } @@ -1154,8 +1220,12 @@ export function* testcoinsWorker({ payload }) { try { receivingAddress = WalletOperations.getNextFreeAddress(wallet); network = WalletUtilities.getNetworkByType(wallet.networkType); - const appId: string = yield select((state: RootState) => state.storage.appId); - const response = yield call(Relay.getTestcoins, receivingAddress, wallet.networkType, appId); + const response = yield call( + Relay.getTestcoins, + receivingAddress, + wallet.networkType, + faucetScope.appId + ); txid = response.txid; } catch (err) { if (err.message === 'FAUCET_DAILY_LIMIT_REACHED') { @@ -1168,21 +1238,48 @@ export function* testcoinsWorker({ payload }) { return; } + if (!isCurrentFaucetScope()) { + yield put(setTestCoinsPending('unknown')); + return; + } + let lastAttemptSynced = false; for (let attempt = 0; attempt < 8; attempt += 1) { let attemptSynced = false; try { - if ( - !ELECTRUM_CLIENT.isClientConnected || - ELECTRUM_CLIENT.activePeer?.networkType !== wallet.networkType - ) { + if (!isCurrentFaucetScope()) { + yield put(setTestCoinsPending('unknown')); + return; + } + let connectionMatchesScope = false; + try { + ElectrumClient.assertConnectionGeneration( + ElectrumClient.getConnectionGeneration(), + wallet.networkType + ); + connectionMatchesScope = true; + } catch { + // The old profile or network's connection must not be reused. + } + if (!ELECTRUM_CLIENT.isClientConnected || !connectionMatchesScope) { yield call(connectToNodeWorker); } + if (!isCurrentFaucetScope()) { + yield put(setTestCoinsPending('unknown')); + return; + } + const connectionGeneration = ElectrumClient.getConnectionGeneration(); + ElectrumClient.assertConnectionGeneration(connectionGeneration, wallet.networkType); const { txids } = yield call( [ElectrumClient, ElectrumClient.syncHistoryByAddress], [receivingAddress], network ); + ElectrumClient.assertConnectionGeneration(connectionGeneration, wallet.networkType); + if (!isCurrentFaucetScope()) { + yield put(setTestCoinsPending('unknown')); + return; + } attemptSynced = true; if (txids.some((seenTxid: string) => seenTxid.toLowerCase() === txid.toLowerCase())) { const refreshed = yield call(refreshWalletsWorker, { From 2d7b30f96c66e9a54584f1a751e39a309e2d91c4 Mon Sep 17 00:00:00 2001 From: Anant Date: Thu, 8 Oct 2026 18:06:51 +0530 Subject: [PATCH 068/254] fix(sync): verify active Realm before wallet writes --- src/store/sagas/wallets.ts | 40 +++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/src/store/sagas/wallets.ts b/src/store/sagas/wallets.ts index 3b383566c5..fac97ef003 100644 --- a/src/store/sagas/wallets.ts +++ b/src/store/sagas/wallets.ts @@ -648,6 +648,16 @@ function* refreshWalletsWorker({ appId: state.storage.appId, networkType: state.settings.bitcoinNetworkType, })); + const assertAccountRealmScope = () => { + const currentState = store.getState(); + const keeperApp = dbManager.getObjectByIndex(RealmSchema.KeeperApp) as unknown as KeeperApp; + if ( + currentState.storage.appId !== refreshScope.appId || + currentState.settings.bitcoinNetworkType !== refreshScope.networkType || + keeperApp?.id !== refreshScope.appId + ) + throw new Error(WALLET_SYNC_SCOPE_CHANGED); + }; wallets = wallets.filter((wallet) => wallet.networkType === refreshScope.networkType); if (wallets.length === 0) { succeeded = true; @@ -676,7 +686,10 @@ function* refreshWalletsWorker({ for (const wallet of wallets) { const schema = wallet.entityKind === EntityKind.VAULT ? RealmSchema.Vault : RealmSchema.Wallet; - const currentWallet = yield call(dbManager.getObjectById, schema, wallet.id); + const currentWallet = yield call(() => { + assertAccountRealmScope(); + return dbManager.getObjectById(schema, wallet.id); + }); if (!currentWallet || currentWallet.networkType !== refreshScope.networkType) return false; currentWallets.push(getJSONFromRealmObject(currentWallet)); } @@ -699,12 +712,7 @@ function* refreshWalletsWorker({ const connectionGeneration = ElectrumClient.getConnectionGeneration(); const assertCurrentScope = () => { - const currentState = store.getState(); - if ( - currentState.storage.appId !== refreshScope.appId || - currentState.settings.bitcoinNetworkType !== refreshScope.networkType - ) - throw new Error(WALLET_SYNC_SCOPE_CHANGED); + assertAccountRealmScope(); try { ElectrumClient.assertConnectionGeneration(connectionGeneration, refreshScope.networkType); } catch { @@ -971,12 +979,14 @@ function* refreshWalletsWorker({ writeScope.networkType !== refreshScope.networkType ) return false; - const persisted = yield call( - dbManager.updateObjectById, - synchedWallet.entityKind === EntityKind.VAULT ? RealmSchema.Vault : RealmSchema.Wallet, - synchedWallet.id, - { specs: synchedWallet.specs } - ); + const persisted = yield call(() => { + assertCurrentScope(); + return dbManager.updateObjectById( + synchedWallet.entityKind === EntityKind.VAULT ? RealmSchema.Vault : RealmSchema.Wallet, + synchedWallet.id, + { specs: synchedWallet.specs } + ); + }); if (!persisted) throw new Error('Failed to persist refreshed wallet'); if (options.addNotifications && newDustUTXOs.length > 0) { @@ -1202,10 +1212,12 @@ export function* testcoinsWorker({ payload }) { })); const isCurrentFaucetScope = () => { const state = store.getState(); + const keeperApp = dbManager.getObjectByIndex(RealmSchema.KeeperApp) as unknown as KeeperApp; return ( state.storage.appId === faucetScope.appId && state.settings.bitcoinNetworkType === faucetScope.networkType && - faucetScope.networkType === wallet.networkType + faucetScope.networkType === wallet.networkType && + keeperApp?.id === faucetScope.appId ); }; From 8aab57eef7e23e9e4f9f0462118d0a46143d807c Mon Sep 17 00:00:00 2001 From: Anant Date: Thu, 8 Oct 2026 18:01:45 +0530 Subject: [PATCH 069/254] fix: verify www remote links in Android activity --- android/app/src/debug/AndroidManifest.xml | 9 --------- android/app/src/main/AndroidManifest.xml | 12 +++++++++++- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/android/app/src/debug/AndroidManifest.xml b/android/app/src/debug/AndroidManifest.xml index 594604506c..fdb0c092e5 100644 --- a/android/app/src/debug/AndroidManifest.xml +++ b/android/app/src/debug/AndroidManifest.xml @@ -52,15 +52,6 @@ - - - - - - - - - diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index de838aa93e..1b21a28af9 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -60,9 +60,19 @@ - + + + + + + + + + + + From c5932d75d547cd8d1d786cbdf7fdea16bdb72bd3 Mon Sep 17 00:00:00 2001 From: Anant Date: Thu, 8 Oct 2026 18:00:27 +0530 Subject: [PATCH 070/254] fix(network): retain public failover after manual node selection --- src/services/electrum/client.ts | 7 ++++ src/services/electrum/node.tsx | 8 +++- tests/services/manualNodeFailover.test.ts | 50 +++++++++++++++++++++++ 3 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 tests/services/manualNodeFailover.test.ts diff --git a/src/services/electrum/client.ts b/src/services/electrum/client.ts index a9e769b7a3..b40ce2f5e4 100644 --- a/src/services/electrum/client.ts +++ b/src/services/electrum/client.ts @@ -264,6 +264,13 @@ export default class ElectrumClient { ELECTRUM_CLIENT.currentPeerIndex = activeNode ? 0 : -1; } + /** Extend a successful manual connection with eligible saved peers for later reconnects. */ + public static setFailoverPeers(nodes: NodeDetail[]) { + const activePeer = ELECTRUM_CLIENT.activePeer; + ELECTRUM_CLIENT.peers = getFailoverPeers(nodes, activePeer); + ELECTRUM_CLIENT.currentPeerIndex = activePeer ? 0 : -1; + } + public static splitIntoChunks(arr, chunkSize) { const groups = []; for (let itr = 0; itr < arr.length; itr += chunkSize) { diff --git a/src/services/electrum/node.tsx b/src/services/electrum/node.tsx index 1b09f62e35..00c5a15e81 100644 --- a/src/services/electrum/node.tsx +++ b/src/services/electrum/node.tsx @@ -91,9 +91,15 @@ export default class Node { } public static async connectToSelectedNode(selectedNode: NodeDetail) { - // connects to the selected node + // The tapped node must connect on its own; only then enable eligible saved + // public peers for a later connection loss. ElectrumClient.setActivePeer([], selectedNode); const { connected, connectedTo, error } = await ElectrumClient.connect(); + if (connected) { + ElectrumClient.setFailoverPeers( + this.getAllNodes().filter((node) => node.networkType === selectedNode.networkType) + ); + } return { connected, connectedTo, error }; } diff --git a/tests/services/manualNodeFailover.test.ts b/tests/services/manualNodeFailover.test.ts new file mode 100644 index 0000000000..4ed670acc0 --- /dev/null +++ b/tests/services/manualNodeFailover.test.ts @@ -0,0 +1,50 @@ +import Node from '../../src/services/electrum/node'; +import ElectrumClient from '../../src/services/electrum/client'; +import { + predefinedMainnetNodes, + predefinedTestnetNodes, +} from '../../src/services/electrum/predefinedNodes'; + +jest.mock('src/services/electrum/client', () => ({ + __esModule: true, + default: { + setActivePeer: jest.fn(), + connect: jest.fn(), + setFailoverPeers: jest.fn(), + }, +})); +jest.mock('src/storage/realm/dbManager', () => ({ __esModule: true, default: {} })); +jest.mock('src/store/store', () => ({ store: { getState: jest.fn() } })); + +describe('manual Electrum selection', () => { + afterEach(() => { + jest.restoreAllMocks(); + jest.clearAllMocks(); + }); + + it('connects the tapped node first, then equips only same-network saved peers', async () => { + const selected = predefinedTestnetNodes[0]; + const saved = [...predefinedTestnetNodes, predefinedMainnetNodes[0]]; + jest.spyOn(Node, 'getAllNodes').mockReturnValue(saved); + (ElectrumClient.connect as jest.Mock).mockResolvedValue({ + connected: true, + connectedTo: selected.host, + }); + + await Node.connectToSelectedNode(selected); + + expect(ElectrumClient.setActivePeer).toHaveBeenCalledWith([], selected); + expect(ElectrumClient.setFailoverPeers).toHaveBeenCalledWith(predefinedTestnetNodes); + }); + + it('does not equip fallback peers when the tapped node never connects', async () => { + (ElectrumClient.connect as jest.Mock).mockResolvedValue({ + connected: false, + error: 'unreachable', + }); + + await Node.connectToSelectedNode(predefinedTestnetNodes[0]); + + expect(ElectrumClient.setFailoverPeers).not.toHaveBeenCalled(); + }); +}); From 54ec852a0b8f026185c43062fa28140f610bea3e Mon Sep 17 00:00:00 2001 From: Anant Date: Thu, 8 Oct 2026 18:08:13 +0530 Subject: [PATCH 071/254] fix(ask): require resolving failed send before new message --- src/screens/HelpAi/HelpAiChat.tsx | 66 +++++++++++++++---- src/store/reducers/helpAi.ts | 18 +++++ .../HelpAi => utils}/helpAiChatBehavior.ts | 10 +++ tests/helpAiChatBehavior.test.ts | 20 +++++- 4 files changed, 100 insertions(+), 14 deletions(-) rename src/{screens/HelpAi => utils}/helpAiChatBehavior.ts (71%) diff --git a/src/screens/HelpAi/HelpAiChat.tsx b/src/screens/HelpAi/HelpAiChat.tsx index 633533f863..df90b185e5 100644 --- a/src/screens/HelpAi/HelpAiChat.tsx +++ b/src/screens/HelpAi/HelpAiChat.tsx @@ -38,6 +38,7 @@ import { useAppDispatch, useAppSelector } from 'src/store/hooks'; import { appendHelpAiMessage, createHelpAiThread, + discardHelpAiFailedMessage, incrementHelpAiIssueCount, setHelpAiChatMeta, setHelpAiDraft, @@ -67,7 +68,12 @@ import Fonts from 'src/constants/Fonts'; import ChatIcon from 'src/assets/images/chat.svg'; import { sanitizeHelpAiReplyLinks, sanitizeHelpAiSources } from 'src/utils/helpAiLinkPolicy'; import { detectSensitiveInput, detectSensitiveInDraft } from 'src/utils/helpAiSensitiveData'; -import { findRetryableChatMessage, isNearChatBottom, planChatSend } from './helpAiChatBehavior'; +import { + canSendChatMessage, + findRetryableChatMessage, + isNearChatBottom, + planChatSend, +} from 'src/utils/helpAiChatBehavior'; // Enhanced error handler to distinguish between all backend rate limit codes const getHelpAiFriendlyError = (error: any, scope: 'chat' | 'issue'): string => { @@ -147,6 +153,7 @@ const HelpAiChat = ({ navigation, route }) => { const userScrolling = useRef(false); const forceFollow = useRef(false); const sendingRef = useRef(false); + const discardedFailedMessageId = useRef(null); const generatedConversationId = useMemo(() => `conv_${Date.now().toString(36)}`, []); const conversationId = route?.params?.conversationId || generatedConversationId; const initialPrompt = route?.params?.prefillText || ''; @@ -236,6 +243,11 @@ const HelpAiChat = ({ navigation, route }) => { const sendToChat = async (text: string, retryMessageId?: string) => { if (sendingRef.current) return; + if (retryMessageId && retryMessageId === discardedFailedMessageId.current) return; + if (!canSendChatMessage(failedMessage?.id, retryMessageId)) { + showToast('Retry or discard the unsent message first.'); + return; + } const retryMessage = findRetryableChatMessage(messages, retryMessageId); if (retryMessageId && !retryMessage) return; const { outboundText, appendUserMessage } = planChatSend(text, retryMessage); @@ -327,6 +339,12 @@ const HelpAiChat = ({ navigation, route }) => { } }; + const discardFailedMessage = () => { + if (!failedMessage || sendingRef.current) return; + discardedFailedMessageId.current = failedMessage.id; + dispatch(discardHelpAiFailedMessage({ conversationId })); + }; + const submitDraftIssue = async () => { if (!draft || !chatMeta) return; if (!appId) { @@ -607,12 +625,12 @@ const HelpAiChat = ({ navigation, route }) => { style={[ styles.sendBtn, { - opacity: sending ? 0.6 : 1, - backgroundColor: Colors.primaryGreen, - }, + opacity: sending || failedMessage ? 0.6 : 1, + backgroundColor: Colors.primaryGreen, + }, ]} onPress={() => sendToChat(input)} - disabled={sending} + disabled={sending || !!failedMessage} > {isDarkMode ? ( @@ -624,15 +642,31 @@ const HelpAiChat = ({ navigation, route }) => { {failedMessage ? ( - sendToChat(failedMessage.text, failedMessage.id)} - disabled={sending} - > - - Retry last failed message + + + Message not sent. Retry or discard it before sending another. - + + sendToChat(failedMessage.text, failedMessage.id)} + disabled={sending} + > + + Retry + + + + + Discard + + + + ) : null} ) => { + const thread = state.threads.find( + (item) => item.conversationId === action.payload.conversationId + ); + if (!thread?.failedMessageId) return; + thread.messages = removeFailedChatMessage(thread.messages, thread.failedMessageId); + thread.failedMessageId = null; + const latestMessage = thread.messages[thread.messages.length - 1]; + thread.lastMessage = latestMessage ? getMessagePreview(latestMessage) : ''; + const firstUserMessage = thread.messages.find((message) => message.type === 'user'); + thread.title = + firstUserMessage?.type === 'user' + ? firstUserMessage.text.trim().slice(0, 60) || 'New chat' + : 'New chat'; + thread.updatedAt = new Date().toISOString(); + }, }, }); @@ -176,6 +193,7 @@ export const { setHelpAiChatMeta, incrementHelpAiIssueCount, setHelpAiFailedMessageId, + discardHelpAiFailedMessage, } = helpAiSlice.actions; const helpAiPersistConfig = { diff --git a/src/screens/HelpAi/helpAiChatBehavior.ts b/src/utils/helpAiChatBehavior.ts similarity index 71% rename from src/screens/HelpAi/helpAiChatBehavior.ts rename to src/utils/helpAiChatBehavior.ts index 4835df279d..caa67bf999 100644 --- a/src/screens/HelpAi/helpAiChatBehavior.ts +++ b/src/utils/helpAiChatBehavior.ts @@ -17,6 +17,16 @@ export const findRetryableChatMessage = ( : undefined; }; +export const canSendChatMessage = ( + failedMessageId?: string | null, + retryMessageId?: string +): boolean => !failedMessageId || failedMessageId === retryMessageId; + +export const removeFailedChatMessage = ( + messages: T[], + failedMessageId: string +): T[] => messages.filter((message) => message.id !== failedMessageId); + export const planChatSend = (input: string, retryMessage?: { text: string }) => ({ outboundText: retryMessage ? retryMessage.text : input.trim(), appendUserMessage: !retryMessage, diff --git a/tests/helpAiChatBehavior.test.ts b/tests/helpAiChatBehavior.test.ts index cbfbac6c8b..20712e7b82 100644 --- a/tests/helpAiChatBehavior.test.ts +++ b/tests/helpAiChatBehavior.test.ts @@ -1,8 +1,10 @@ import { + canSendChatMessage, findRetryableChatMessage, isNearChatBottom, planChatSend, -} from '../src/screens/HelpAi/helpAiChatBehavior'; + removeFailedChatMessage, +} from '../src/utils/helpAiChatBehavior'; test('new content follows only while the reader is near the bottom', () => { expect(isNearChatBottom(200, 400, 680)).toBe(true); @@ -41,3 +43,19 @@ test('retry resends the failed message without adding a second user bubble', () }); expect(findRetryableChatMessage(threadAfterReturn.messages, 'ai-1')).toBeUndefined(); }); + +test('a failed bubble must be retried or discarded before sending a different message', () => { + const messages = [ + { id: 'question-1', type: 'user', text: 'First question' }, + { id: 'reply-1', type: 'ai', text: 'First answer' }, + { id: 'failed-1', type: 'user', text: 'Unsent question' }, + ]; + + expect(canSendChatMessage('failed-1')).toBe(false); + expect(canSendChatMessage('failed-1', 'failed-1')).toBe(true); + expect(canSendChatMessage('failed-1', 'question-1')).toBe(false); + + const afterDiscard = removeFailedChatMessage(messages, 'failed-1'); + expect(afterDiscard.map((message) => message.id)).toEqual(['question-1', 'reply-1']); + expect(canSendChatMessage(null)).toBe(true); +}); From 4fd0a71dd3e917646e0bb5b8cf499d0c780c3a8f Mon Sep 17 00:00:00 2001 From: Anant Date: Thu, 8 Oct 2026 18:08:45 +0530 Subject: [PATCH 072/254] Bind assisted backup to account origin and consent --- .../Backup/BackupHealthCheckList.tsx | 42 +-- src/components/ImportExportLabels/index.tsx | 6 +- src/hooks/useSettingKeeper.tsx | 36 +- src/hooks/useUSDTWallets.ts | 79 ++++- .../AppSettings/AssistedBackupStatus.tsx | 8 +- .../AppSettings/Node/NodeSelection.tsx | 17 +- src/screens/AppSettings/Node/NodeSettings.tsx | 13 +- .../BackupWallet/ViewRecoveryKeyScreen.tsx | 36 +- .../Notifications/NotificationsCenter.tsx | 4 +- .../components/Settings/keeperSettings.tsx | 4 +- src/screens/LoginScreen/Login.tsx | 14 +- src/screens/Recieve/ReceiveScreen.tsx | 6 +- src/screens/SeedScreens/ExportSeedScreen.tsx | 36 +- src/screens/Send/SendConfirmation.tsx | 8 +- .../SignTransaction/SignTransactionScreen.tsx | 4 +- src/screens/UTXOManagement/UTXOLabeling.tsx | 12 +- .../components/LabelsEditor.tsx | 6 +- src/screens/Vault/ChoosePolicyNew.tsx | 6 +- src/screens/Vault/VaultDetails.tsx | 4 +- .../ViewTransactions/TransactionDetails.tsx | 8 +- src/services/backend/Relay.ts | 4 +- src/services/backup/repair.ts | 9 +- src/services/backup/transport.ts | 17 +- src/services/electrum/node.tsx | 20 +- src/store/reducers/bhr.ts | 120 +++++-- src/store/reducers/storage.ts | 12 +- src/store/sagaActions/bhr.ts | 16 +- src/store/sagaActions/send_and_receive.ts | 11 +- src/store/sagaActions/upgrade.ts | 6 +- src/store/sagaActions/utxos.ts | 12 +- src/store/sagaActions/wallets.ts | 12 +- src/store/sagas/bhr.ts | 232 +++++++++--- src/store/sagas/login.ts | 32 +- src/store/sagas/send_and_receive.ts | 20 +- src/store/sagas/upgrade.ts | 68 +++- src/store/sagas/utxos.ts | 140 +++++--- src/store/sagas/wallets.ts | 335 +++++++++++++----- src/store/store.ts | 80 ++++- tests/recovery/accountBinding.test.cjs | 256 +++++++++++++ tests/recovery/backupFeedback.test.cjs | 7 +- tests/recovery/backupFreshness.test.cjs | 49 ++- tests/recovery/helpers.cjs | 1 + tests/recovery/labelAccount.test.cjs | 138 ++++++++ tests/recovery/manualCoinChoices.test.cjs | 14 +- tests/recovery/sagaHarness.cjs | 41 ++- tests/recovery/sagaState.test.cjs | 7 +- tests/recovery/sendLabelOrigin.test.cjs | 97 +++++ tests/recovery/usdtAccountDelete.test.cjs | 136 +++++++ tests/recovery/usdtBackup.test.cjs | 6 +- 49 files changed, 1814 insertions(+), 433 deletions(-) create mode 100644 tests/recovery/accountBinding.test.cjs create mode 100644 tests/recovery/labelAccount.test.cjs create mode 100644 tests/recovery/sendLabelOrigin.test.cjs create mode 100644 tests/recovery/usdtAccountDelete.test.cjs diff --git a/src/components/Backup/BackupHealthCheckList.tsx b/src/components/Backup/BackupHealthCheckList.tsx index f59e845194..35f43e341b 100644 --- a/src/components/Backup/BackupHealthCheckList.tsx +++ b/src/components/Backup/BackupHealthCheckList.tsx @@ -17,6 +17,7 @@ import { setBackupAllFailure, setBackupAllSuccess, setSeedConfirmed, + isAutomaticCloudBackupEnabled, } from 'src/store/reducers/bhr'; import { CommonActions, useIsFocused, useNavigation } from '@react-navigation/native'; import { useQuery } from '@realm/react'; @@ -41,7 +42,7 @@ const ContentType = { mismatch: 'mismatch', healthCheckSuccessful: 'healthCheckSuccessful', }; -function Content({ contentType, asbEnabled }: { contentType: string; asbEnabled: boolean }) { +function Content({ contentType, asbEnabled }: { contentType: string; asbEnabled?: boolean }) { const { BackupWallet } = useContext(LocalizationContext).translations; const illustrations = { [ContentType.verifying]: ( @@ -74,17 +75,21 @@ function BackupHealthCheckList({ isUaiFlow }) { const { translations } = useContext(LocalizationContext); const { BackupWallet, vault: vaultText, common } = translations; const dispatch = useAppDispatch(); - const { primaryMnemonic, backup, id } = useQuery(RealmSchema.KeeperApp).map( + const { primaryMnemonic, id } = useQuery(RealmSchema.KeeperApp).map( getJSONFromRealmObject - )[0]; + )[0] as { primaryMnemonic: string; id: string }; const { backupMethod, seedConfirmed, - automaticCloudBackup, - backupAllLoading, - backupAllFailure, - backupAllSuccess, + automaticCloudBackupByAppId, + backupAllLoadingByAppId, + backupAllFailureByAppId, + backupAllSuccessByAppId, } = useAppSelector((state) => state.bhr); + const automaticCloudBackup = isAutomaticCloudBackupEnabled({ automaticCloudBackupByAppId }, id); + const backupAllLoading = !!backupAllLoadingByAppId?.[id]; + const backupAllFailure = !!backupAllFailureByAppId?.[id]; + const backupAllSuccess = !!backupAllSuccessByAppId?.[id]; const [healthCheckModal, setHealthCheckModal] = useState(false); const [showConfirmSeedModal, setShowConfirmSeedModal] = useState(isUaiFlow); const [verificationModal, setVerificationModal] = useState(false); @@ -107,9 +112,9 @@ function BackupHealthCheckList({ isUaiFlow }) { useEffect(() => { if (backupAllSuccess) { if (!automaticCloudBackup) setAsbEnabled(true); - dispatch(setBackupAllSuccess(false)); - dispatch(setBackupAllFailure(false)); - dispatch(setAutomaticCloudBackup(true)); + dispatch(setBackupAllSuccess({ appId: id, status: false })); + dispatch(setBackupAllFailure({ appId: id, status: false })); + dispatch(setAutomaticCloudBackup({ appId: id, enabled: true })); setHealthCheckModal(true); } }, [backupAllSuccess, backupAllFailure]); @@ -118,12 +123,9 @@ function BackupHealthCheckList({ isUaiFlow }) { if (seedConfirmed) { setShowConfirmSeedModal(false); dispatch(setRecoveryKeyBackedUp({ appId: id as string, status: true })); - if (!automaticCloudBackup) dispatch(backupAllSignersAndVaults()); - else { - setTimeout(() => { - setHealthCheckModal(true); - }, 100); - } + setTimeout(() => { + setHealthCheckModal(true); + }, 100); } return () => { dispatch(setSeedConfirmed(false)); @@ -133,13 +135,13 @@ function BackupHealthCheckList({ isUaiFlow }) { useEffect(() => { if (backupAllFailure && isFocused) { setFailedVerificationModal(true); - dispatch(setBackupAllFailure(false)); + dispatch(setBackupAllFailure({ appId: id, status: false })); } }, [backupAllFailure]); useEffect(() => { if (backupAllSuccess && isFocused) { - dispatch(setBackupAllSuccess(false)); + dispatch(setBackupAllSuccess({ appId: id, status: false })); dispatch(seedBackedConfirmed(true)); } }, [backupAllSuccess]); @@ -192,7 +194,7 @@ function BackupHealthCheckList({ isUaiFlow }) { } else { setBackupMismatchModal(true); } - }) + }, id) ); } else { dispatch(seedBackedConfirmed(true)); @@ -288,7 +290,7 @@ function BackupHealthCheckList({ isUaiFlow }) { buttonBackground={`${colorMode}.pantoneGreen`} buttonText={'Backup Now'} buttonCallback={() => { - dispatch(backupAllSignersAndVaults()); + dispatch(backupAllSignersAndVaults(id)); setBackupMismatchModal(false); }} secondaryButtonText="Home" diff --git a/src/components/ImportExportLabels/index.tsx b/src/components/ImportExportLabels/index.tsx index 72074f47ae..91d07f13d3 100644 --- a/src/components/ImportExportLabels/index.tsx +++ b/src/components/ImportExportLabels/index.tsx @@ -1,4 +1,4 @@ -import React, { useState } from 'react'; +import React, { useRef, useState } from 'react'; import { Box, useColorMode } from '@gluestack-ui/themed-native-base'; import { StyleSheet, TouchableOpacity } from 'react-native'; import { useDispatch } from 'react-redux'; @@ -28,6 +28,8 @@ const ImportExportLabels: React.FC = ({ }) => { const { colorMode } = useColorMode(); const dispatch = useDispatch(); + const activeAppId = useAppSelector((state) => state.storage.appId); + const originAppId = useRef(activeAppId).current; const { vault: vaultText } = translations; const { syncingUTXOs } = useAppSelector((state) => state.utxos); const [isSyncingLabels, setIsSyncingLabels] = useState(false); @@ -101,7 +103,7 @@ const ImportExportLabels: React.FC = ({ origin: standardizeDescriptor(label.origin), })); - dispatch(importLabels({ labels: standardizedLabels })); + dispatch(importLabels({ labels: standardizedLabels }, originAppId)); onSuccess(`Imported ${walletLabels.length} labels successfully`); } catch (parseError) { diff --git a/src/hooks/useSettingKeeper.tsx b/src/hooks/useSettingKeeper.tsx index ae020d4cc8..b597869295 100644 --- a/src/hooks/useSettingKeeper.tsx +++ b/src/hooks/useSettingKeeper.tsx @@ -51,6 +51,7 @@ import { setAutomaticCloudBackup, setDeleteBackupSuccess, setDeleteBackupFailure, + isAutomaticCloudBackupEnabled, } from 'src/store/reducers/bhr'; import useToastMessage from './useToastMessage'; import ToastErrorIcon from 'src/assets/images/toast_error.svg'; @@ -92,12 +93,17 @@ export const useSettingKeeper = () => { }); const { id }: KeeperApp = useQuery(RealmSchema.KeeperApp).map(getJSONFromRealmObject)[0]; const { - backupAllFailure, - backupAllSuccess, - automaticCloudBackup, - deleteBackupSuccess, - deleteBackupFailure, + backupAllFailureByAppId, + backupAllSuccessByAppId, + automaticCloudBackupByAppId, + deleteBackupSuccessByAppId, + deleteBackupFailureByAppId, } = useAppSelector((state) => state.bhr); + const automaticCloudBackup = isAutomaticCloudBackupEnabled({ automaticCloudBackupByAppId }, id); + const backupAllFailure = !!backupAllFailureByAppId?.[id]; + const backupAllSuccess = !!backupAllSuccessByAppId?.[id]; + const deleteBackupSuccess = !!deleteBackupSuccessByAppId?.[id]; + const deleteBackupFailure = !!deleteBackupFailureByAppId?.[id]; useEffect(() => { if (isOnL4) { @@ -142,8 +148,8 @@ export const useSettingKeeper = () => { useEffect(() => { if (backupAllSuccess && isFocused) { - dispatch(setBackupAllSuccess(false)); - dispatch(setAutomaticCloudBackup(true)); + dispatch(setBackupAllSuccess({ appId: id, status: false })); + dispatch(setAutomaticCloudBackup({ appId: id, enabled: true })); setTimeout(() => { dispatch(setShowTipModal({ status: true, address: config.ADDRESS.assistServer })); }, 1000); @@ -152,28 +158,28 @@ export const useSettingKeeper = () => { useEffect(() => { if (deleteBackupSuccess && isFocused) { - dispatch(setDeleteBackupSuccess(false)); - dispatch(setAutomaticCloudBackup(false)); + dispatch(setDeleteBackupSuccess({ appId: id, status: false })); + dispatch(setAutomaticCloudBackup({ appId: id, enabled: false })); } }, [deleteBackupSuccess]); useEffect(() => { if (deleteBackupFailure && isFocused) { - dispatch(setDeleteBackupFailure(false)); + dispatch(setDeleteBackupFailure({ appId: id, status: false })); showToast(errorText.unableToDeleteAssistedServer, ); } }, [deleteBackupFailure]); useEffect(() => { if (backupAllFailure && isFocused) { - dispatch(setBackupAllFailure(false)); + dispatch(setBackupAllFailure({ appId: id, status: false })); } }, [backupAllFailure]); const toggleAutomaticBackupMode = useCallback(() => { - if (!automaticCloudBackup) dispatch(backupAllSignersAndVaults()); + if (!automaticCloudBackup) dispatch(backupAllSignersAndVaults(id)); else setShowDeleteBackup(true); - }, [automaticCloudBackup, dispatch]); + }, [automaticCloudBackup, dispatch, id]); const debouncedAutomaticBackupToggle = useCallback(() => { if (backupToggleTimeoutRef.current) { @@ -471,11 +477,11 @@ export const useSettingKeeper = () => { primaryText={settings.assistedServerDeleteBackupModalCTA} primaryCallback={() => { setShowDeleteBackup(false); - dispatch(deleteBackup()); + dispatch(deleteBackup(id)); }} secondaryCallback={() => { setShowDeleteBackup(false); - dispatch(setAutomaticCloudBackup(false)); + dispatch(setAutomaticCloudBackup({ appId: id, enabled: false })); }} secondaryText={settings.assistedServerDeleteBackupModalSecondaryCTA} /> diff --git a/src/hooks/useUSDTWallets.ts b/src/hooks/useUSDTWallets.ts index 12578e9f78..53abd4d944 100644 --- a/src/hooks/useUSDTWallets.ts +++ b/src/hooks/useUSDTWallets.ts @@ -19,13 +19,17 @@ import USDT, { DEFAULT_DEADLINE_SECONDS, USDTTransferOptions, } from '../services/wallets/operations/dollars/USDT'; -import { useDispatch } from 'react-redux'; +import { useDispatch, useStore } from 'react-redux'; import { useAppSelector } from 'src/store/hooks'; import { updateAppImage } from 'src/store/sagaActions/bhr'; import Relay from 'src/services/backend/Relay'; import { canonical, recoveryContent } from 'src/services/backup/image'; import { markBackupMutation } from 'src/services/backup/transport'; -import { invalidateBackupRepair, setPendingAllBackup } from 'src/store/reducers/bhr'; +import { + invalidateBackupRepair, + isAutomaticCloudBackupEnabled, + setPendingAllBackup, +} from 'src/store/reducers/bhr'; export interface UseUSDTWalletsOptions { getAll?: boolean; @@ -64,7 +68,14 @@ export const useUSDTWallets = (options: UseUSDTWalletsOptions = {}): UseUSDTWall const allWallets = useQuery(RealmSchema.USDTWallet); const [error, setError] = useState(null); const dispatch = useDispatch(); + const reduxStore = useStore(); const { id: appId }: any = dbManager.getObjectByIndex(RealmSchema.KeeperApp); + const isOriginCurrent = useCallback( + () => + (dbManager.getObjectByIndex(RealmSchema.KeeperApp) as any)?.id === appId && + reduxStore.getState().storage.appId === appId, + [appId, reduxStore] + ); const filterByNetwork = (wallets: USDTWallet[]) => { if (bitcoinNetworkType) @@ -101,7 +112,9 @@ export const useUSDTWallets = (options: UseUSDTWalletsOptions = {}): UseUSDTWall primaryMnemonic?: string; importDetails?: USDTWalletImportDetails; }): Promise<{ newWallet?: USDTWallet; error?: string }> => { + let beganLocalWrite = false; try { + if (!isOriginCurrent()) throw new Error('Account changed'); setError(null); const walletNetworkType = USDTWalletSupportedNetwork; const allUSDTWallets: USDTWallet[] = (await dbManager.getObjectByIndex( @@ -110,6 +123,7 @@ export const useUSDTWallets = (options: UseUSDTWalletsOptions = {}): UseUSDTWall null, true // get all wallets )) as any; + if (!isOriginCurrent()) throw new Error('Account changed'); let lastInstanceNum = -1; allUSDTWallets.forEach((wallet) => { @@ -128,6 +142,7 @@ export const useUSDTWallets = (options: UseUSDTWalletsOptions = {}): UseUSDTWall instanceNum: params.type === USDTWalletType.DEFAULT ? lastInstanceNum + 1 : null, importDetails: params.importDetails, }); + if (!isOriginCurrent()) throw new Error('Account changed'); // check if a USDT wallet already exists with the same mnemonic(especially for imported wallets) const existingWallet = allUSDTWallets.find((wallet) => wallet.id === newWallet.id); @@ -135,41 +150,67 @@ export const useUSDTWallets = (options: UseUSDTWalletsOptions = {}): UseUSDTWall throw new Error('USDT wallet already exists with the same ID'); } - await dbManager.createObject(RealmSchema.USDTWallet, newWallet); + beganLocalWrite = true; + if ((await dbManager.createObject(RealmSchema.USDTWallet, newWallet)) !== true) + throw new Error('Failed to create wallet'); + if (!isOriginCurrent()) throw new Error('Account changed'); // Create usdt wallet backup - dispatch(updateAppImage({ wallets: [newWallet], signers: null, updateNodes: false })); + dispatch(updateAppImage({ wallets: [newWallet], signers: null, updateNodes: false }, appId)); return { newWallet }; } catch (err) { + if (beganLocalWrite && isAutomaticCloudBackupEnabled(reduxStore.getState().bhr, appId)) { + markBackupMutation(appId); + dispatch(invalidateBackupRepair(appId)); + dispatch(setPendingAllBackup({ appId, pending: true })); + } setError(err.message || 'Failed to create wallet'); captureError(err); return { error: err.message || 'Failed to create wallet' }; } }, - [] + [appId, dispatch, isOriginCurrent, reduxStore] ); /** * Delete a wallet */ const deleteWallet = useCallback(async (walletId: string): Promise => { + let remoteDeleted = false; + const markIncompleteDeletion = () => { + if (!remoteDeleted) return; + markBackupMutation(appId); + dispatch(invalidateBackupRepair(appId)); + dispatch(setPendingAllBackup({ appId, pending: true })); + }; try { - const response = await Relay.deleteAppImageEntity({ - appId, - signers: null, - walletIds: [walletId], - }); - if (!response.updated) throw new Error('Failed to delete wallet'); - - await dbManager.deleteObjectById(RealmSchema.USDTWallet, walletId); - return true; + if (!isOriginCurrent()) return false; + // An account without Assisted Server Backup consent still owns its local + // wallet. Only the opted-in account needs a corresponding relay delete. + if (isAutomaticCloudBackupEnabled(reduxStore.getState().bhr, appId)) { + const response = await Relay.deleteAppImageEntity({ + appId, + signers: null, + walletIds: [walletId], + }); + if (!response.updated) throw new Error('Failed to delete wallet'); + remoteDeleted = true; + } + if (!isOriginCurrent()) { + markIncompleteDeletion(); + return false; + } + const deleted = dbManager.deleteObjectById(RealmSchema.USDTWallet, walletId) === true; + if (!deleted) markIncompleteDeletion(); + return deleted; } catch (err) { + markIncompleteDeletion(); setError(err.message || 'Failed to delete wallet'); captureError(err); return false; } - }, []); + }, [appId, dispatch, isOriginCurrent, reduxStore]); /** * Update a wallet in the database @@ -178,6 +219,7 @@ export const useUSDTWallets = (options: UseUSDTWalletsOptions = {}): UseUSDTWall async (wallet: USDTWallet): Promise => { let recoveryChanged = false; try { + if (!isOriginCurrent()) return false; const previous = dbManager.getObjectById(RealmSchema.USDTWallet, wallet.id); if (!previous) throw new Error('Wallet not found'); const previousWallet = previous.toJSON ? previous.toJSON() : previous; @@ -191,11 +233,12 @@ export const useUSDTWallets = (options: UseUSDTWalletsOptions = {}): UseUSDTWall walletUpdateData ); // Realm updates must omit the primary key. if (!updated) throw new Error('Failed to update wallet'); + if (!isOriginCurrent()) throw new Error('Account changed'); // Metadata and visibility must survive recovery. Balance/account refreshes // are rebuildable caches and must not cause an upload on every sync. if (recoveryChanged) - dispatch(updateAppImage({ wallets: [wallet], signers: null, updateNodes: false })); + dispatch(updateAppImage({ wallets: [wallet], signers: null, updateNodes: false }, appId)); return true; } catch (err) { @@ -204,14 +247,14 @@ export const useUSDTWallets = (options: UseUSDTWalletsOptions = {}): UseUSDTWall if (recoveryChanged) { markBackupMutation(appId); dispatch(invalidateBackupRepair(appId)); - dispatch(setPendingAllBackup(true)); + dispatch(setPendingAllBackup({ appId, pending: true })); } setError(err.message || 'Failed to update wallet'); captureError(err); return false; } }, - [appId, dispatch] + [appId, dispatch, isOriginCurrent] ); /** diff --git a/src/screens/AppSettings/AssistedBackupStatus.tsx b/src/screens/AppSettings/AssistedBackupStatus.tsx index cdc54fc004..5c8d7811ac 100644 --- a/src/screens/AppSettings/AssistedBackupStatus.tsx +++ b/src/screens/AppSettings/AssistedBackupStatus.tsx @@ -6,6 +6,7 @@ import { useNavigation } from '@react-navigation/native'; import { RealmSchema } from 'src/storage/realm/enum'; import { useAppDispatch, useAppSelector } from 'src/store/hooks'; import { checkBackupFreshness, repairBackup } from 'src/store/sagaActions/bhr'; +import { isAutomaticCloudBackupEnabled } from 'src/store/reducers/bhr'; import { LocalizationContext } from 'src/context/Localization/LocContext'; import ScreenWrapper from 'src/components/ScreenWrapper'; import WalletHeader from 'src/components/WalletHeader'; @@ -21,15 +22,16 @@ export default function AssistedBackupStatus() { const app = useQuery(RealmSchema.KeeperApp)[0] as any; const id = app?.id as string; const { - automaticCloudBackup, + automaticCloudBackupByAppId, backupRepairStateByAppId = {}, backupRepairRunningByAppId = {}, } = useAppSelector((state) => state.bhr); + const automaticCloudBackup = isAutomaticCloudBackupEnabled({ automaticCloudBackupByAppId }, id); const phase = backupRepairStateByAppId[id]; const running = !!backupRepairRunningByAppId[id]; const [slow, setSlow] = useState(false); useEffect(() => { - if (automaticCloudBackup) dispatch(checkBackupFreshness()); + if (automaticCloudBackup) dispatch(checkBackupFreshness(id)); }, [id]); useEffect(() => { setSlow(false); @@ -70,7 +72,7 @@ export default function AssistedBackupStatus() { fullWidth primaryText={state === 'different' ? copy.backUp : copy.check} primaryCallback={() => - dispatch(state === 'different' ? repairBackup() : checkBackupFreshness()) + dispatch(state === 'different' ? repairBackup(id) : checkBackupFreshness(id)) } /> )} diff --git a/src/screens/AppSettings/Node/NodeSelection.tsx b/src/screens/AppSettings/Node/NodeSelection.tsx index 71f7579a97..bf62444d53 100644 --- a/src/screens/AppSettings/Node/NodeSelection.tsx +++ b/src/screens/AppSettings/Node/NodeSelection.tsx @@ -96,6 +96,8 @@ const NodeSelection = () => { }, [nodes.length]); const onSaveCallback = async (nodeDetail: NodeDetail) => { + const originAppId = Node.currentAccountId(); + if (!originAppId) return; setSaveLoading(true); // Sanitize host @@ -110,11 +112,15 @@ const NodeSelection = () => { } const nodeToSave = { ...nodeDetail }; - const { saved, connectionError: nodeConnectionError } = await Node.save(nodeToSave, nodeList); + const { saved, connectionError: nodeConnectionError } = await Node.save(nodeToSave, nodeList, originAppId); + if (!Node.isAccountActive(originAppId)) { + setSaveLoading(false); + return; + } if (saved) { const updatedNodeList = Node.getAllNodes(); setNodeList(updatedNodeList); - dispatch(updateAppImage({ wallets: null, signers: null, updateNodes: true })); + dispatch(updateAppImage({ wallets: null, signers: null, updateNodes: true }, originAppId)); const newNode = updatedNodeList.find( (node) => node.host === nodeToSave.host && node.port === nodeToSave.port ); @@ -144,6 +150,8 @@ const NodeSelection = () => { }; const onConnectToNode = async (selectedNode: NodeDetail) => { + const originAppId = Node.currentAccountId(); + if (!originAppId) return; let nodes = Node.getAllNodes(); if ( currentlySelectedNode && @@ -152,6 +160,7 @@ const NodeSelection = () => { ) { // disconnect currently selected node(if connected) await Node.disconnect(currentlySelectedNode); + if (!Node.isAccountActive(originAppId)) return; currentlySelectedNode.isConnected = false; Node.update(currentlySelectedNode, { isConnected: currentlySelectedNode.isConnected }); @@ -168,6 +177,10 @@ const NodeSelection = () => { const node = { ...selectedNode }; const { connected, connectedTo, error } = await Node.connectToSelectedNode(node); + if (!Node.isAccountActive(originAppId)) { + setLoading(false); + return; + } if (connected) { node.isConnected = connected; diff --git a/src/screens/AppSettings/Node/NodeSettings.tsx b/src/screens/AppSettings/Node/NodeSettings.tsx index dc74de79ca..7ca1441fc1 100644 --- a/src/screens/AppSettings/Node/NodeSettings.tsx +++ b/src/screens/AppSettings/Node/NodeSettings.tsx @@ -64,24 +64,31 @@ function NodeSettings() { }, [nodes.length]); const onDelete = async (selectedItem: NodeDetail) => { + const originAppId = Node.currentAccountId(); + if (!originAppId) return; const isConnected = Node.nodeConnectionStatus(selectedItem); if (isConnected) await Node.disconnect(selectedItem); + if (!Node.isAccountActive(originAppId)) return; + const status = Node.delete(selectedItem); if (status) { const updatedNodes = Node.getAllNodes(); setNodeList(updatedNodes); - dispatch(updateAppImage({ wallets: null, signers: null, updateNodes: true })); + dispatch(updateAppImage({ wallets: null, signers: null, updateNodes: true }, originAppId)); } }; const onConnectToNode = async (selectedNode: NodeDetail) => { + const originAppId = Node.currentAccountId(); + if (!originAppId) return; let updatedNodes = Node.getAllNodes(); // Disconnect the currently connected node if it's not the selected one const currentlySelectedNode = updatedNodes.find((node) => node.isConnected); if (currentlySelectedNode && currentlySelectedNode.id !== selectedNode.id) { await Node.disconnect(currentlySelectedNode); + if (!Node.isAccountActive(originAppId)) return; currentlySelectedNode.isConnected = false; Node.update(currentlySelectedNode, { isConnected: currentlySelectedNode.isConnected }); updatedNodes = updatedNodes.map((node) => @@ -93,6 +100,10 @@ function NodeSettings() { setLoading(true); const { connected, connectedTo, error } = await Node.connectToSelectedNode(selectedNode); + if (!Node.isAccountActive(originAppId)) { + setLoading(false); + return; + } if (connected) { selectedNode.isConnected = connected; diff --git a/src/screens/BackupWallet/ViewRecoveryKeyScreen.tsx b/src/screens/BackupWallet/ViewRecoveryKeyScreen.tsx index f5a9ef668b..38988f8ebd 100644 --- a/src/screens/BackupWallet/ViewRecoveryKeyScreen.tsx +++ b/src/screens/BackupWallet/ViewRecoveryKeyScreen.tsx @@ -1,4 +1,4 @@ -import React, { useContext, useEffect, useState } from 'react'; +import React, { useContext, useState } from 'react'; import { Box, useColorMode } from '@gluestack-ui/themed-native-base'; import { LocalizationContext } from 'src/context/Localization/LocContext'; import ScreenWrapper from 'src/components/ScreenWrapper'; @@ -13,18 +13,11 @@ import ThemedColor from 'src/components/ThemedColor/ThemedColor'; import Buttons from 'src/components/Buttons'; import ModalWrapper from 'src/components/Modal/ModalWrapper'; import ConfirmSeedWord from 'src/components/SeedWordBackup/ConfirmSeedWord'; -import ActivityIndicatorView from 'src/components/AppActivityIndicator/ActivityIndicatorView'; -import { backupAllSignersAndVaults, seedBackedUp } from 'src/store/sagaActions/bhr'; +import { seedBackedUp } from 'src/store/sagaActions/bhr'; import { useDispatch } from 'react-redux'; import { setRecoveryKeyStatus } from 'src/store/reducers/account'; -import { useAppSelector } from 'src/store/hooks'; import useToastMessage from 'src/hooks/useToastMessage'; import TickIcon from 'src/assets/images/icon_tick.svg'; -import { - setAutomaticCloudBackup, - setBackupAllFailure, - setBackupAllSuccess, -} from 'src/store/reducers/bhr'; type KeeperApp = { id: string; @@ -33,7 +26,7 @@ type KeeperApp = { export const ViewRecoveryKeyScreen = ({ navigation }) => { const { colorMode } = useColorMode(); - const { home: homeTxt, common, BackupWallet: backupTxt, recoveryBackup } = useContext(LocalizationContext).translations; + const { home: homeTxt, common, BackupWallet: backupTxt } = useContext(LocalizationContext).translations; const { primaryMnemonic, id: appId } = useQuery(RealmSchema.KeeperApp).map( getJSONFromRealmObject )[0] as KeeperApp; @@ -41,27 +34,9 @@ export const ViewRecoveryKeyScreen = ({ navigation }) => { const [showWordIndex, setShowWordIndex] = useState(''); const seedTextColor = ThemedColor({ name: 'seedTextColor' }); const [confirmSeedModal, setConfirmSeedModal] = useState(false); - const [loader, setLoader] = useState(false); const dispatch = useDispatch(); - const { backupAllFailure, backupAllSuccess } = useAppSelector((state) => state.bhr); const { showToast } = useToastMessage(); - useEffect(() => { - if (backupAllSuccess) { - dispatch(setBackupAllSuccess(false)); - dispatch(setBackupAllFailure(false)); - dispatch(setAutomaticCloudBackup(true)); - setLoader(false); - showToast(backupTxt.recoveryKeyBackedUpSuccessfully, ); - navigation.goBack(); - } - if (backupAllFailure) { - dispatch(setBackupAllFailure(false)); - setLoader(false); - showToast(recoveryBackup.unverified.title); - } - }, [backupAllSuccess, backupAllFailure]); - const SeedCard = ({ item, index }: { item; index }) => { return ( { words={words} confirmBtnPress={() => { setConfirmSeedModal(false); - setLoader(true); dispatch(seedBackedUp()); dispatch(setRecoveryKeyStatus({ appId, status: 'confirmed' })); - dispatch(backupAllSignersAndVaults()); + showToast(backupTxt.recoveryKeyBackedUpSuccessfully, ); + navigation.goBack(); }} /> - ); diff --git a/src/screens/Home/Notifications/NotificationsCenter.tsx b/src/screens/Home/Notifications/NotificationsCenter.tsx index efcca03f94..d2ee6454de 100644 --- a/src/screens/Home/Notifications/NotificationsCenter.tsx +++ b/src/screens/Home/Notifications/NotificationsCenter.tsx @@ -90,7 +90,9 @@ const Card = memo(({ uai }: CardProps) => { const [insightModal, setInsightModal] = useState(false); const { signerMap } = useSignerMap(); const snapshots = useAppSelector((state) => state.cachedTxn.snapshots); - const { backupAllLoading } = useAppSelector((state) => state.bhr); + const backupAllLoading = useAppSelector( + (state) => !!state.bhr.backupAllLoadingByAppId?.[state.storage.appId] + ); const { translations } = useContext(LocalizationContext); const { common, notification, error: errorTranslation } = translations; diff --git a/src/screens/Home/components/Settings/keeperSettings.tsx b/src/screens/Home/components/Settings/keeperSettings.tsx index 65b0c28432..0152255a72 100644 --- a/src/screens/Home/components/Settings/keeperSettings.tsx +++ b/src/screens/Home/components/Settings/keeperSettings.tsx @@ -51,7 +51,9 @@ const KeeperSettings = ({ route }) => { }; }, []); // Empty dependency array means this runs once on mount - const { backupAllLoading } = useAppSelector((state) => state.bhr); + const backupAllLoading = useAppSelector( + (state) => !!state.bhr.backupAllLoadingByAppId?.[state.storage.appId] + ); const dispatch = useDispatch(); return ( diff --git a/src/screens/LoginScreen/Login.tsx b/src/screens/LoginScreen/Login.tsx index e28e8922c3..b6970cef1a 100644 --- a/src/screens/LoginScreen/Login.tsx +++ b/src/screens/LoginScreen/Login.tsx @@ -49,7 +49,11 @@ import BounceLoader from 'src/components/BounceLoader'; import { formatCoolDownTime, PasswordTimeout } from 'src/utils/PasswordTimeout'; import Buttons from 'src/components/Buttons'; import PinDotView from 'src/components/AppPinInput/PinDotView'; -import { setAutomaticCloudBackup, setBackupType } from 'src/store/reducers/bhr'; +import { + setAutomaticCloudBackup, + setBackupType, + isAutomaticCloudBackupEnabled, +} from 'src/store/reducers/bhr'; import Relay from 'src/services/backend/Relay'; import { setAccountManagerDetails } from 'src/store/reducers/concierge'; import Fonts from 'src/constants/Fonts'; @@ -84,7 +88,9 @@ function LoginScreen({ navigation, route }) { // useAppSelector((state) => state.settings.subscription) === SubscriptionTier.L4; const isOnPleb = false; const isKeeperPrivate = false; - const { automaticCloudBackup } = useAppSelector((state) => state.bhr); + const automaticCloudBackup = useAppSelector((state) => + isAutomaticCloudBackupEnabled(state.bhr, appId) + ); const login_button_backGround = ThemedColor({ name: 'login_button_backGround' }); const slider_background = ThemedColor({ name: 'slider_background' }); @@ -413,9 +419,9 @@ function LoginScreen({ navigation, route }) { dispatch(setSubscription(updatedSubscription.name)); dispatch(setOfflineStatus(true)); // disable assisted server backup for pleb - dispatch(setAutomaticCloudBackup(false)); + dispatch(setAutomaticCloudBackup({ appId: app.id, enabled: false })); dispatch(setPlebDueToOffline(true)); - dispatch(setAutoUpdateEnabledBeforeDowngrade(automaticCloudBackup)); + dispatch(setAutoUpdateEnabledBeforeDowngrade({ appId: app.id, enabled: automaticCloudBackup })); navigation.replace('App'); } diff --git a/src/screens/Recieve/ReceiveScreen.tsx b/src/screens/Recieve/ReceiveScreen.tsx index 6befa1e08d..ab38812359 100644 --- a/src/screens/Recieve/ReceiveScreen.tsx +++ b/src/screens/Recieve/ReceiveScreen.tsx @@ -3,7 +3,7 @@ import Text from 'src/components/KeeperText'; import { Box, useColorMode, Pressable, HStack } from '@gluestack-ui/themed-native-base'; import { ScrollView, StyleSheet, Vibration, TouchableOpacity, TextInput } from 'react-native'; -import React, { useContext, useEffect, useState } from 'react'; +import React, { useContext, useEffect, useRef, useState } from 'react'; import Buttons from 'src/components/Buttons'; import ScreenWrapper from 'src/components/ScreenWrapper'; @@ -96,6 +96,8 @@ function ReceiveScreen({ route }: { route }) { const [addressUsed, setAddressUsed] = useState(false); const dispatch = useAppDispatch(); + const selectedAppId = useAppSelector((state) => state.storage.appId); + const originAppId = useRef(selectedAppId).current; const { showToast } = useToastMessage(); @@ -111,7 +113,7 @@ function ReceiveScreen({ route }: { route }) { const [localCurrencyKind, setLocalCurrencyKind] = useState(currentCurrency); const generateNewReceiveAddress = () => { - dispatch(generateNewAddress(wallet)); + dispatch(generateNewAddress(wallet, originAppId)); Vibration.vibrate(50); const newTotalAddressesCount = totalAddressesCount + 1; setTotalAddressesCount(newTotalAddressesCount); diff --git a/src/screens/SeedScreens/ExportSeedScreen.tsx b/src/screens/SeedScreens/ExportSeedScreen.tsx index 447e6c1741..213e674a35 100644 --- a/src/screens/SeedScreens/ExportSeedScreen.tsx +++ b/src/screens/SeedScreens/ExportSeedScreen.tsx @@ -7,7 +7,6 @@ import ConfirmSeedWord from 'src/components/SeedWordBackup/ConfirmSeedWord'; import { LocalizationContext } from 'src/context/Localization/LocContext'; import ModalWrapper from 'src/components/Modal/ModalWrapper'; import { - backupAllSignersAndVaults, healthCheckStatusUpdate, seedBackedUp, } from 'src/store/sagaActions/bhr'; @@ -39,12 +38,6 @@ import { RealmSchema } from 'src/storage/realm/enum'; import { setRecoveryKeyBackedUp, updateOneTimeBackupStatus } from 'src/store/reducers/account'; import { setShowTipModal } from 'src/store/reducers/settings'; import config from 'src/utils/service-utilities/config'; -import { - setAutomaticCloudBackup, - setBackupAllFailure, - setBackupAllSuccess, -} from 'src/store/reducers/bhr'; -import ActivityIndicatorView from 'src/components/AppActivityIndicator/ActivityIndicatorView'; function ExportSeedScreen({ route, navigation }) { const { colorMode } = useColorMode(); @@ -88,37 +81,15 @@ function ExportSeedScreen({ route, navigation }) { const [backupSuccessModal, setBackupSuccessModal] = useState(false); const [showQRVisible, setShowQRVisible] = useState(false); const [showWordIndex, setShowWordIndex] = useState(''); - const { - backupMethod, - automaticCloudBackup, - backupAllLoading, - backupAllFailure, - backupAllSuccess, - } = useAppSelector((state) => state.bhr); + const { backupMethod } = useAppSelector((state) => state.bhr); const isChangePassword = parentScreen === PRIVACYANDDISPLAY; const seedTextColor = ThemedColor({ name: 'seedTextColor' }); const { id: appId } = dbManager.getObjectByIndex(RealmSchema.KeeperApp); - const [asbEnabled, setAsbEnabled] = useState(false); useEffect(() => { if (backupMethod !== null && next && !isHealthCheck && !isInheritancePlaning) { - if (asbEnabled) dispatch(backupAllSignersAndVaults()); - else setBackupSuccessModal(true); - } - }, [backupMethod]); - - useEffect(() => { - if (backupAllSuccess) { - if (!automaticCloudBackup) setAsbEnabled(true); - dispatch(setBackupAllSuccess(false)); - dispatch(setBackupAllFailure(false)); - dispatch(setAutomaticCloudBackup(true)); setBackupSuccessModal(true); } - if (backupAllFailure) { - dispatch(setBackupAllFailure(false)); - showToast(translations.recoveryBackup.unverified.title); - } - }, [backupAllSuccess, backupAllFailure]); + }, [backupMethod]); const getNoteSubtitle = () => { @@ -328,7 +299,6 @@ function ExportSeedScreen({ route, navigation }) { }) ); } else { - if (!automaticCloudBackup) setAsbEnabled(true); dispatch(seedBackedUp()); dispatch(setRecoveryKeyBackedUp({ appId, status: true })); } @@ -361,7 +331,6 @@ function ExportSeedScreen({ route, navigation }) { {BackupWallet.backupSuccessParagraph} - {asbEnabled && {BackupWallet.assistedServerBackupEnabled}} )} @@ -387,7 +356,6 @@ function ExportSeedScreen({ route, navigation }) { )} /> - ); } diff --git a/src/screens/Send/SendConfirmation.tsx b/src/screens/Send/SendConfirmation.tsx index c142e84d6d..9b15883a9d 100644 --- a/src/screens/Send/SendConfirmation.tsx +++ b/src/screens/Send/SendConfirmation.tsx @@ -2,7 +2,7 @@ import { StyleSheet, TouchableOpacity, ScrollView } from 'react-native'; import Text from 'src/components/KeeperText'; import { Box, useColorMode } from '@gluestack-ui/themed-native-base'; import { CommonActions, useIsFocused, useNavigation } from '@react-navigation/native'; -import React, { useCallback, useContext, useEffect, useState } from 'react'; +import React, { useCallback, useContext, useEffect, useRef, useState } from 'react'; import { sendPhaseTwo } from 'src/store/sagaActions/send_and_receive'; import { hp, wp } from 'src/constants/responsive'; import Share from 'react-native-share'; @@ -100,6 +100,8 @@ function SendConfirmation({ route }) { const { colorMode } = useColorMode(); const { showToast } = useToastMessage(); const dispatch = useDispatch(); + const activeAppId = useAppSelector((state) => state.storage.appId); + const originAppId = useRef(activeAppId).current; const isFocused = useIsFocused(); const { sender, @@ -328,7 +330,7 @@ function SendConfirmation({ route }) { ), }, note, - }) + }, originAppId) ); }, 200); }) @@ -1058,4 +1060,4 @@ function updateSatsAmountInMsg(message: string, newAmount) { console.log('🚀 ~ updateSatsAmountInMsg ~ error:', error); return message; } -} \ No newline at end of file +} diff --git a/src/screens/SignTransaction/SignTransactionScreen.tsx b/src/screens/SignTransaction/SignTransactionScreen.tsx index eae120b884..10e80c129f 100644 --- a/src/screens/SignTransaction/SignTransactionScreen.tsx +++ b/src/screens/SignTransaction/SignTransactionScreen.tsx @@ -172,6 +172,8 @@ function SignTransactionScreen() { const card = useRef(new CKTapCard()).current; const satochipCard = useRef(new SatochipCard()).current; const dispatch = useDispatch(); + const activeAppId = useAppSelector((state) => state.storage.appId); + const originAppId = useRef(activeAppId).current; const cachedTxn = useAppSelector((state) => state.cachedTxn); const cachedTxid = useAppSelector((state) => state.sendAndReceive.sendPhaseTwo.cachedTxid); @@ -808,7 +810,7 @@ function SignTransactionScreen() { txnPriority: tnxDetails.transactionPriority, note, miniscriptTxElements, - }) + }, originAppId) ); } else { showToast(errorText.notEnoughtSignature); diff --git a/src/screens/UTXOManagement/UTXOLabeling.tsx b/src/screens/UTXOManagement/UTXOLabeling.tsx index 2b137dd6a2..95bbe6e382 100644 --- a/src/screens/UTXOManagement/UTXOLabeling.tsx +++ b/src/screens/UTXOManagement/UTXOLabeling.tsx @@ -54,6 +54,8 @@ function UTXOLabeling() { const { transactions: txTranslations, wallet: walletTranslations, common } = translations; const dispatch = useDispatch(); + const activeAppId = useAppSelector((state) => state.storage.appId); + const originAppId = useRef(activeAppId).current; // Live reactive wallet from Realm so spendability updates immediately after saga writes const liveWalletResult = useWallets({ walletIds: [wallet.id] }).wallets[0]; const liveVaultResult = useVault({ vaultId: wallet.id }).activeVault; @@ -127,7 +129,7 @@ function UTXOLabeling() { const finalLabels = [{ name: noteRef.current, isSystem: false }]; if (txNoteLabels[utxo.txId][0]?.name) { const labelChanges = getLabelChanges(txNoteLabels[utxo.txId], finalLabels); - dispatch(bulkUpdateLabels({ labelChanges, txId: utxo.txId, wallet })); + dispatch(bulkUpdateLabels({ labelChanges, txId: utxo.txId, wallet }, originAppId)); } else { dispatch( addLabels({ @@ -135,13 +137,13 @@ function UTXOLabeling() { txId: utxo.txId, wallet, type: LabelRefType.TXN, - }) + }, originAppId) ); } } else { if (txNoteLabels[utxo.txId][0]?.name) { const labelChanges = getLabelChanges(txNoteLabels[utxo.txId], []); - dispatch(bulkUpdateLabels({ labelChanges, txId: utxo.txId, wallet })); + dispatch(bulkUpdateLabels({ labelChanges, txId: utxo.txId, wallet }, originAppId)); } } } @@ -251,7 +253,7 @@ function UTXOLabeling() { { - dispatch(markUTXOSpendability({ wallet, txId: utxo.txId, vout: utxo.vout, spendability: 'spendable' })); + dispatch(markUTXOSpendability({ wallet, txId: utxo.txId, vout: utxo.vout, spendability: 'spendable' }, originAppId)); showToast('Coin marked spendable', ); }} > @@ -264,7 +266,7 @@ function UTXOLabeling() { { - dispatch(markUTXOSpendability({ wallet, txId: utxo.txId, vout: utxo.vout, spendability: 'doNotSpend' })); + dispatch(markUTXOSpendability({ wallet, txId: utxo.txId, vout: utxo.vout, spendability: 'doNotSpend' }, originAppId)); showToast('Coin marked Do Not Spend', ); }} > diff --git a/src/screens/UTXOManagement/components/LabelsEditor.tsx b/src/screens/UTXOManagement/components/LabelsEditor.tsx index 58db542da5..3f703b0a96 100644 --- a/src/screens/UTXOManagement/components/LabelsEditor.tsx +++ b/src/screens/UTXOManagement/components/LabelsEditor.tsx @@ -28,6 +28,8 @@ function LabelsEditor({ utxo = null, address = null, wallet, onLabelsSaved, read const { showToast } = useToastMessage(); const processDispatched = useRef(false); const dispatch = useDispatch(); + const activeAppId = useAppSelector((state) => state.storage.appId); + const originAppId = useRef(activeAppId).current; const labelsKey = address ? address : `${utxo.txId}:${utxo.vout}`; const getSortedNames = (labels) => @@ -106,9 +108,9 @@ function LabelsEditor({ utxo = null, address = null, wallet, onLabelsSaved, read const labelChanges = getLabelChanges(initialLabels, finalLabels); processDispatched.current = true; if (address) { - dispatch(bulkUpdateLabels({ labelChanges, address, wallet })); + dispatch(bulkUpdateLabels({ labelChanges, address, wallet }, originAppId)); } else { - dispatch(bulkUpdateLabels({ labelChanges, UTXO: utxo, wallet })); + dispatch(bulkUpdateLabels({ labelChanges, UTXO: utxo, wallet }, originAppId)); } }; diff --git a/src/screens/Vault/ChoosePolicyNew.tsx b/src/screens/Vault/ChoosePolicyNew.tsx index 390114b990..9d6bec45a8 100644 --- a/src/screens/Vault/ChoosePolicyNew.tsx +++ b/src/screens/Vault/ChoosePolicyNew.tsx @@ -1,7 +1,7 @@ import Text from 'src/components/KeeperText'; import { Box, useColorMode } from '@gluestack-ui/themed-native-base'; import { StyleSheet, TouchableOpacity } from 'react-native'; -import React, { useCallback, useContext, useEffect, useState } from 'react'; +import React, { useCallback, useContext, useEffect, useRef, useState } from 'react'; import { DelayedPolicyUpdate, SignerPolicy, @@ -46,6 +46,8 @@ import ThemedSvg from 'src/components/ThemedSvg.tsx/ThemedSvg'; import ThemedColor from 'src/components/ThemedColor/ThemedColor'; function ChoosePolicyNew({ navigation, route }) { + const selectedAppId = useAppSelector((state) => state.storage.appId); + const originAppId = useRef(selectedAppId).current; const { colorMode } = useColorMode(); const isDarkMode = colorMode === 'dark'; const { showToast } = useToastMessage(); @@ -193,7 +195,7 @@ function ChoosePolicyNew({ navigation, route }) { restrictions: newPolicy.restrictions, signingDelay: newPolicy.signingDelay, }; - dispatch(updateSignerPolicy(signer, route.params.vaultKey, policyUpdates, verificationToken)); + dispatch(updateSignerPolicy(signer, route.params.vaultKey, policyUpdates, verificationToken, originAppId)); }; useEffect(() => { diff --git a/src/screens/Vault/VaultDetails.tsx b/src/screens/Vault/VaultDetails.tsx index 4ae46546d7..8b8dd6ff0d 100644 --- a/src/screens/Vault/VaultDetails.tsx +++ b/src/screens/Vault/VaultDetails.tsx @@ -155,6 +155,8 @@ function VaultDetails({ navigation, route }: ScreenProps) { viewTransaction = null, } = route.params || {}; const dispatch = useDispatch(); + const activeAppId = useAppSelector((state) => state.storage.appId); + const originAppId = useRef(activeAppId).current; const { showToast } = useToastMessage(); const { activeVault: vault } = useVault({ vaultId }); const [pullRefresh, setPullRefresh] = useState(false); @@ -442,7 +444,7 @@ function VaultDetails({ navigation, route }: ScreenProps) { const txid = tnx?.potentialTxId || getTnxIdFromCachedTnx(tnx); for (const broadcastedTnx of transactions) { if (broadcastedTnx.txid === txid) { - dispatch(discardBroadcastedTnx({ cachedTxid: tnx.txid, vault })); + dispatch(discardBroadcastedTnx({ cachedTxid: tnx.txid, vault }, originAppId)); } } } diff --git a/src/screens/ViewTransactions/TransactionDetails.tsx b/src/screens/ViewTransactions/TransactionDetails.tsx index 6d0bd4220a..5782fad77d 100644 --- a/src/screens/ViewTransactions/TransactionDetails.tsx +++ b/src/screens/ViewTransactions/TransactionDetails.tsx @@ -71,6 +71,8 @@ function TransactionDetails({ route }) { const close = () => setVisible(false); const noteRef = useRef(); const dispatch = useDispatch(); + const activeAppId = useAppSelector((state) => state.storage.appId); + const originAppId = useRef(activeAppId).current; const [updatingLabel, setUpdatingLabel] = React.useState(false); const { bitcoinNetworkType } = useAppSelector((state) => state.settings); const insets = useSafeAreaInsets(); @@ -88,7 +90,7 @@ function TransactionDetails({ route }) { const finalLabels = [{ name: noteRef.current, isSystem: false }]; if (labels[transaction.txid][0]?.name) { const labelChanges = getLabelChanges(labels[transaction.txid], finalLabels); - dispatch(bulkUpdateLabels({ labelChanges, txId: transaction.txid, wallet })); + dispatch(bulkUpdateLabels({ labelChanges, txId: transaction.txid, wallet }, originAppId)); } else { dispatch( addLabels({ @@ -96,13 +98,13 @@ function TransactionDetails({ route }) { txId: transaction.txid, wallet, type: LabelRefType.TXN, - }) + }, originAppId) ); } } else { if (labels[transaction.txid][0]?.name) { const labelChanges = getLabelChanges(labels[transaction.txid], []); - dispatch(bulkUpdateLabels({ labelChanges, txId: transaction.txid, wallet })); + dispatch(bulkUpdateLabels({ labelChanges, txId: transaction.txid, wallet }, originAppId)); } } } diff --git a/src/services/backend/Relay.ts b/src/services/backend/Relay.ts index d9a33502ad..24839a7fed 100644 --- a/src/services/backend/Relay.ts +++ b/src/services/backend/Relay.ts @@ -142,7 +142,7 @@ export default class Relay { err?: string; }> => { try { - const res = await RestClient.post(`${RELAY}migrateXfps`, { appId, signerChanges }); + const res = await backupPost(`${RELAY}migrateXfps`, { appId, signerChanges }); const data = res.data || res.json; return data; } catch (err) { @@ -493,7 +493,7 @@ export default class Relay { message?: string; }> => { try { - const res = await backupPost(`${RELAY}deleteBackup`, body); + const res = await backupPost(`${RELAY}deleteBackup`, body, true); const data = res.data || res.json; return data; } catch (err) { diff --git a/src/services/backup/repair.ts b/src/services/backup/repair.ts index 2bd77a5e87..3ba77bcce6 100644 --- a/src/services/backup/repair.ts +++ b/src/services/backup/repair.ts @@ -14,6 +14,7 @@ import { backupQueuedRevision, backupRevision, boundedBackupPost, + canAccessBackupAccount, withBackupSession, } from './transport'; import { @@ -160,12 +161,13 @@ async function encodeImage(app: any, image: BackupImage, checkpoint = () => {}) export function inspectBackup( appId: string, repair: boolean, - onPhase: (phase: RepairPhase) => void + onPhase: (phase: RepairPhase) => void, + explicitChoice = false ): Promise { const running = inFlight.get(appId); if (running) { if (repair && !running.repair) - return running.promise.then(() => inspectBackup(appId, true, onPhase)); + return running.promise.then(() => inspectBackup(appId, true, onPhase, explicitChoice)); running.listeners.add(onPhase); if (running.phase) onPhase(running.phase); return running.promise; @@ -186,7 +188,8 @@ export function inspectBackup( const assertCurrent = () => { if ( Date.now() >= deadline || - (dbManager.getObjectByIndex(RealmSchema.KeeperApp) as any)?.id !== appId + (dbManager.getObjectByIndex(RealmSchema.KeeperApp) as any)?.id !== appId || + !canAccessBackupAccount(appId, explicitChoice) ) throw new Error('Backup changed during check'); const currentRevision = backupRevision(appId); diff --git a/src/services/backup/transport.ts b/src/services/backup/transport.ts index 6b23798c71..04e6e46c7b 100644 --- a/src/services/backup/transport.ts +++ b/src/services/backup/transport.ts @@ -4,6 +4,12 @@ import RestClient from '../rest/RestClient'; const queues = new Map>(); const revisions = new Map(); const queuedMutationRevisions = new Map(); +let mayUploadForAccount: (appId: string, explicitChoice: boolean) => boolean = () => false; +export const setBackupUploadGuard = (guard: (appId: string, explicitChoice: boolean) => boolean) => { + mayUploadForAccount = guard; +}; +export const canAccessBackupAccount = (appId: string, explicitChoice = false) => + mayUploadForAccount(appId, explicitChoice); export const backupRevision = (id: string) => revisions.get(id) || 0; export const backupQueuedRevision = (id: string) => queuedMutationRevisions.get(id) || 0; export const markBackupMutation = (id: string) => revisions.set(id, backupRevision(id) + 1); @@ -89,11 +95,18 @@ export async function boundedBackupPost( } } -export function backupPost(path: string, body: any) { +export function backupPost(path: string, body: any, explicitChoice = false) { const appId = body.appId || body.appID; if (!appId) return Promise.reject(new Error('Missing backup account')); + if (!canAccessBackupAccount(appId, explicitChoice)) + return Promise.reject(new Error('Backup account is not active or opted in')); markBackupMutation(appId); - const result = withBackupSession(appId, () => boundedBackupPost(path, body)); + const result = withBackupSession(appId, () => { + // The queued write may execute after an account switch or consent change. + if (!canAccessBackupAccount(appId, explicitChoice)) + throw new Error('Backup account is not active or opted in'); + return boundedBackupPost(path, body); + }); queuedMutationRevisions.set(appId, backupRevision(appId)); return result; } diff --git a/src/services/electrum/node.tsx b/src/services/electrum/node.tsx index 1b09f62e35..70b93b3f75 100644 --- a/src/services/electrum/node.tsx +++ b/src/services/electrum/node.tsx @@ -7,7 +7,23 @@ import { predefinedMainnetNodes, predefinedTestnetNodes } from './predefinedNode import { store } from 'src/store/store'; export default class Node { - public static async save(nodeDetail: NodeDetail, nodeList: NodeDetail[]) { + public static currentAccountId(): string | undefined { + const appId = store.getState().storage.appId; + return this.isAccountActive(appId) ? appId : undefined; + } + + public static isAccountActive(appId?: string): boolean { + try { + return !!appId && store.getState().storage.appId === appId && + (dbManager.getObjectByIndex(RealmSchema.KeeperApp) as any)?.id === appId; + } catch { + return false; + } + } + + public static async save(nodeDetail: NodeDetail, nodeList: NodeDetail[], originAppId = this.currentAccountId()) { + if (!this.isAccountActive(originAppId)) + return { saved: false, connectionError: 'Account changed' }; const { bitcoinNetworkType } = store.getState().settings; if (!nodeDetail.host || !nodeDetail.port) { return { saved: false, connectionError: 'Missing node host URL or port' }; @@ -19,6 +35,8 @@ export default class Node { if (!isConnectable) { return { saved: false, connectionError }; } + if (!this.isAccountActive(originAppId)) + return { saved: false, connectionError: 'Account changed' }; const node = { ...nodeDetail }; diff --git a/src/store/reducers/bhr.ts b/src/store/reducers/bhr.ts index dcd6edd455..1effed9274 100644 --- a/src/store/reducers/bhr.ts +++ b/src/store/reducers/bhr.ts @@ -42,16 +42,24 @@ const initialState: { backupAllLoading: boolean; backupAllSuccess: boolean; backupAllFailure: boolean; + backupAllLoadingByAppId: Record; + backupAllSuccessByAppId: Record; + backupAllFailureByAppId: Record; pendingAllBackup: boolean; + pendingAllBackupByAppId: Record; backupRepairCompletedByAppId: Record; backupRepairStateByAppId: Record; backupRepairRunningByAppId: Record; + // The legacy flag is retained only until the first authenticated migration. automaticCloudBackup: boolean; + automaticCloudBackupByAppId: Record; deleteBackupSuccess: boolean; deleteBackupFailure: boolean; + deleteBackupSuccessByAppId: Record; + deleteBackupFailureByAppId: Record; homeToastMessage: homeToastMessageType; } = { backupMethod: null, @@ -86,20 +94,32 @@ const initialState: { backupAllLoading: false, backupAllFailure: false, backupAllSuccess: false, + backupAllLoadingByAppId: {}, + backupAllSuccessByAppId: {}, + backupAllFailureByAppId: {}, pendingAllBackup: false, + pendingAllBackupByAppId: {}, backupRepairCompletedByAppId: {}, backupRepairStateByAppId: {}, backupRepairRunningByAppId: {}, automaticCloudBackup: false, + automaticCloudBackupByAppId: {}, deleteBackupSuccess: false, deleteBackupFailure: false, + deleteBackupSuccessByAppId: {}, + deleteBackupFailureByAppId: {}, homeToastMessage: { message: null, isError: false }, }; +export const isAutomaticCloudBackupEnabled = ( + backup: { automaticCloudBackupByAppId?: Record }, + appId?: string +): boolean => !!appId && backup.automaticCloudBackupByAppId?.[appId] === true; + const bhrSlice = createSlice({ name: 'bhr', initialState, @@ -218,19 +238,40 @@ const bhrSlice = createSlice({ resetSeedWords: (state) => { state.seedWords = []; }, - setBackupAllLoading: (state, action: PayloadAction) => { - state.backupAllLoading = action.payload; - }, - setBackupAllSuccess: (state, action: PayloadAction) => { - state.backupAllSuccess = action.payload; - state.backupAllLoading = false; - }, - setBackupAllFailure: (state, action: PayloadAction) => { - state.backupAllFailure = action.payload; - state.backupAllLoading = false; - }, - setPendingAllBackup: (state, action: PayloadAction) => { - state.pendingAllBackup = action.payload; + setBackupAllLoading: ( + state, + action: PayloadAction<{ appId: string; status: boolean }> + ) => { + const { appId, status } = action.payload; + if (!appId) return; + (state.backupAllLoadingByAppId ??= {})[appId] = status; + }, + setBackupAllSuccess: ( + state, + action: PayloadAction<{ appId: string; status: boolean }> + ) => { + const { appId, status } = action.payload; + if (!appId) return; + (state.backupAllSuccessByAppId ??= {})[appId] = status; + (state.backupAllLoadingByAppId ??= {})[appId] = false; + }, + setBackupAllFailure: ( + state, + action: PayloadAction<{ appId: string; status: boolean }> + ) => { + const { appId, status } = action.payload; + if (!appId) return; + (state.backupAllFailureByAppId ??= {})[appId] = status; + (state.backupAllLoadingByAppId ??= {})[appId] = false; + }, + setPendingAllBackup: ( + state, + action: PayloadAction<{ appId: string; pending: boolean }> + ) => { + const { appId, pending } = action.payload; + if (!appId) return; + (state.pendingAllBackupByAppId ??= {})[appId] = pending; + state.pendingAllBackup = false; }, setBackupRepairState: (state, action: PayloadAction<{ appId: string; phase: RepairPhase }>) => { @@ -246,14 +287,45 @@ const bhrSlice = createSlice({ if (state.backupRepairStateByAppId?.[action.payload] === 'verified') state.backupRepairStateByAppId[action.payload] = 'unverified'; }, - setAutomaticCloudBackup: (state, action: PayloadAction) => { - state.automaticCloudBackup = action.payload; - }, - setDeleteBackupSuccess: (state, action: PayloadAction) => { - state.deleteBackupSuccess = action.payload; - }, - setDeleteBackupFailure: (state, action: PayloadAction) => { - state.deleteBackupFailure = action.payload; + setAutomaticCloudBackup: ( + state, + action: PayloadAction<{ appId: string; enabled: boolean }> + ) => { + const { appId, enabled } = action.payload; + if (!appId) return; + (state.automaticCloudBackupByAppId ??= {})[appId] = enabled; + if (!enabled) (state.backupAllSuccessByAppId ??= {})[appId] = false; + state.automaticCloudBackup = false; + }, + migrateLegacyAutomaticCloudBackup: ( + state, + action: PayloadAction<{ appId: string; canAttributeConsent: boolean }> + ) => { + const { appId, canAttributeConsent } = action.payload; + if (state.automaticCloudBackup && appId && canAttributeConsent) { + const byAppId = (state.automaticCloudBackupByAppId ??= {}); + if (byAppId[appId] === undefined) byAppId[appId] = true; + (state.pendingAllBackupByAppId ??= {})[appId] = true; + } + // An unbound legacy flag must never authorize uploads for another account. + state.automaticCloudBackup = false; + state.pendingAllBackup = false; + }, + setDeleteBackupSuccess: ( + state, + action: PayloadAction<{ appId: string; status: boolean }> + ) => { + const { appId, status } = action.payload; + if (!appId) return; + (state.deleteBackupSuccessByAppId ??= {})[appId] = status; + }, + setDeleteBackupFailure: ( + state, + action: PayloadAction<{ appId: string; status: boolean }> + ) => { + const { appId, status } = action.payload; + if (!appId) return; + (state.deleteBackupFailureByAppId ??= {})[appId] = status; }, setHomeToastMessage: (state, action: PayloadAction) => { state.homeToastMessage = action.payload; @@ -306,6 +378,7 @@ export const { invalidateBackupRepair, setAutomaticCloudBackup, + migrateLegacyAutomaticCloudBackup, setDeleteBackupSuccess, setDeleteBackupFailure, @@ -346,9 +419,14 @@ const bhrPersistConfig = { 'backupAllLoading', 'backupAllFailure', 'backupAllSuccess', + 'backupAllLoadingByAppId', + 'backupAllFailureByAppId', + 'backupAllSuccessByAppId', 'deleteBackupSuccess', 'deleteBackupFailure', + 'deleteBackupSuccessByAppId', + 'deleteBackupFailureByAppId', 'homeToastMessage', ], diff --git a/src/store/reducers/storage.ts b/src/store/reducers/storage.ts index ab7363fd2c..2545477d70 100644 --- a/src/store/reducers/storage.ts +++ b/src/store/reducers/storage.ts @@ -40,6 +40,7 @@ const initialState: { delayedPolicyUpdate: { [policyId: string]: DelayedPolicyUpdate }; // contains a single policy update at a time plebDueToOffline: boolean; // app downgraded to pleb due to internet issue wasAutoUpdateEnabledBeforeDowngrade: boolean; + wasAutoUpdateEnabledBeforeDowngradeByAppId: Record; defaultWalletCreated: { [NetworkType.MAINNET]: boolean; [NetworkType.TESTNET]: boolean; @@ -78,6 +79,7 @@ const initialState: { delayedPolicyUpdate: {}, plebDueToOffline: false, wasAutoUpdateEnabledBeforeDowngrade: false, + wasAutoUpdateEnabledBeforeDowngradeByAppId: {}, defaultWalletCreated: { [NetworkType.MAINNET]: false, [NetworkType.TESTNET]: false, @@ -145,8 +147,14 @@ const storageSlice = createSlice({ setPlebDueToOffline: (state, action: PayloadAction) => { state.plebDueToOffline = action.payload; }, - setAutoUpdateEnabledBeforeDowngrade: (state, action: PayloadAction) => { - state.wasAutoUpdateEnabledBeforeDowngrade = action.payload; + setAutoUpdateEnabledBeforeDowngrade: ( + state, + action: PayloadAction<{ appId: string; enabled: boolean }> + ) => { + const { appId, enabled } = action.payload; + if (!appId) return; + (state.wasAutoUpdateEnabledBeforeDowngradeByAppId ??= {})[appId] = enabled; + state.wasAutoUpdateEnabledBeforeDowngrade = false; }, setDefaultWalletCreated: ( state, diff --git a/src/store/sagaActions/bhr.ts b/src/store/sagaActions/bhr.ts index 24ed5515ad..876d8f34d9 100644 --- a/src/store/sagaActions/bhr.ts +++ b/src/store/sagaActions/bhr.ts @@ -15,8 +15,9 @@ export const SET_AUTOMATIC_CLOUD_BACKUP = 'SET_AUTOMATIC_CLOUD_BACKUP'; export const DELETE_BACKUP = 'DELETE_BACKUP'; export const VALIDATE_SERVER_BACKUP = 'VALIDATE_SERVER_BACKUP'; -export const updateAppImage = ({ wallets, signers, updateNodes }) => ({ +export const updateAppImage = ({ wallets, signers, updateNodes }, originAppId?: string) => ({ type: UPDATE_APP_IMAGE, + ...(originAppId ? { originAppId } : {}), payload: { wallets, signers, @@ -90,8 +91,9 @@ export const bsmsCloudHealthCheck = () => ({ type: BSMS_CLOUD_HEALTH_CHECK, }); -export const backupAllSignersAndVaults = () => ({ +export const backupAllSignersAndVaults = (appId: string) => ({ type: BACKUP_ALL_SIGNERS_AND_VAULTS, + appId, }); export const setAutomaticCloudBackup = (payload) => ({ @@ -99,16 +101,18 @@ export const setAutomaticCloudBackup = (payload) => ({ payload, }); -export const deleteBackup = () => ({ +export const deleteBackup = (appId: string) => ({ type: DELETE_BACKUP, + appId, }); -export const validateServerBackup = (callback) => ({ +export const validateServerBackup = (callback, originAppId: string) => ({ type: VALIDATE_SERVER_BACKUP, callback, + originAppId, }); export const CHECK_BACKUP_FRESHNESS = 'CHECK_BACKUP_FRESHNESS'; export const REPAIR_BACKUP = 'REPAIR_BACKUP'; -export const checkBackupFreshness = () => ({ type: CHECK_BACKUP_FRESHNESS }); -export const repairBackup = () => ({ type: REPAIR_BACKUP }); +export const checkBackupFreshness = (appId: string) => ({ type: CHECK_BACKUP_FRESHNESS, appId }); +export const repairBackup = (appId: string) => ({ type: REPAIR_BACKUP, appId }); diff --git a/src/store/sagaActions/send_and_receive.ts b/src/store/sagaActions/send_and_receive.ts index cb18d384af..9b475b67e2 100644 --- a/src/store/sagaActions/send_and_receive.ts +++ b/src/store/sagaActions/send_and_receive.ts @@ -54,6 +54,7 @@ export const sendPhaseOne = (payload: { export interface SendPhaseTwoAction extends Action { type: typeof SEND_PHASE_TWO; + originAppId?: string; payload: { wallet: Wallet | Vault; currentBlockHeight: number; @@ -75,13 +76,15 @@ export const sendPhaseTwo = (payload: { selectedPhase: number; selectedPaths: number[]; }; -}): SendPhaseTwoAction => ({ +}, originAppId?: string): SendPhaseTwoAction => ({ type: SEND_PHASE_TWO, payload, + originAppId, }); export interface SendPhaseThreeAction extends Action { type: typeof SEND_PHASE_THREE; + originAppId?: string; payload: { wallet: Wallet | Vault; txnPriority: TxPriority; @@ -101,9 +104,10 @@ export const sendPhaseThree = (payload: { selectedPhase: number; selectedPaths: number[]; }; -}): SendPhaseThreeAction => ({ +}, originAppId?: string): SendPhaseThreeAction => ({ type: SEND_PHASE_THREE, payload, + originAppId, }); export interface CalculateSendMaxFeeAction extends Action { @@ -164,7 +168,8 @@ export const calculateCustomFee = (payload: { payload, }); -export const discardBroadcastedTnx = ({ cachedTxid, vault }) => ({ +export const discardBroadcastedTnx = ({ cachedTxid, vault }, originAppId?: string) => ({ type: DISCARD_BROADCASTED_TNX, payload: { cachedTxid, vault }, + originAppId, }); diff --git a/src/store/sagaActions/upgrade.ts b/src/store/sagaActions/upgrade.ts index d6b6a14fc7..c6546a99ab 100644 --- a/src/store/sagaActions/upgrade.ts +++ b/src/store/sagaActions/upgrade.ts @@ -2,11 +2,13 @@ export const RESET_REDUX_STORE = 'RESET_REDUX_STORE'; export const UPDATE_VERSION_HISTORY = 'UPDATE_VERSION_HISTORY'; export const MIGRATE_LABELS_329 = 'MIGRATE_LABELS_329'; -export const updateVersionHistory = (previousVersion, newVersion, isRecovery = false) => ({ +export const updateVersionHistory = (previousVersion, newVersion, isRecovery = false, originAppId?: string) => ({ type: UPDATE_VERSION_HISTORY, payload: { previousVersion, newVersion, isRecovery }, + originAppId, }); -export const migrateLabelsToBip329 = () => ({ +export const migrateLabelsToBip329 = (originAppId?: string) => ({ type: MIGRATE_LABELS_329, + originAppId, }); diff --git a/src/store/sagaActions/utxos.ts b/src/store/sagaActions/utxos.ts index 3307b81f54..0ae52ccfac 100644 --- a/src/store/sagaActions/utxos.ts +++ b/src/store/sagaActions/utxos.ts @@ -14,9 +14,10 @@ export const addLabels = (payload: { wallet: Wallet | Vault; labels: { name: string; isSystem: boolean }[]; type; -}) => ({ +}, originAppId?: string) => ({ type: ADD_LABELS, payload, + originAppId, }); export const bulkUpdateLabels = (payload: { @@ -28,9 +29,10 @@ export const bulkUpdateLabels = (payload: { txId?: string; address?: string; wallet: Wallet; -}) => ({ +}, originAppId?: string) => ({ type: BULK_UPDATE_LABELS, payload, + originAppId, }); export const importLabels = (payload: { @@ -42,9 +44,10 @@ export const importLabels = (payload: { origin: string; } ]; -}) => ({ +}, originAppId?: string) => ({ type: IMPORT_LABELS, payload, + originAppId, }); export const markUTXOSpendability = (payload: { @@ -52,7 +55,8 @@ export const markUTXOSpendability = (payload: { txId: string; vout: number; spendability: UTXOSpendability; -}) => ({ +}, originAppId?: string) => ({ type: MARK_UTXO_SPENDABILITY, payload, + originAppId, }); diff --git a/src/store/sagaActions/wallets.ts b/src/store/sagaActions/wallets.ts index b9078e1e3e..1e19a5adcd 100644 --- a/src/store/sagaActions/wallets.ts +++ b/src/store/sagaActions/wallets.ts @@ -30,14 +30,15 @@ export const autoSyncWallets = ( syncAll?: boolean, hardRefresh?: boolean, addNotifications?: boolean, - backupCheckAppId?: string + originAppId?: string ) => ({ type: AUTO_SYNC_WALLETS, + ...(originAppId ? { originAppId } : {}), payload: { syncAll, hardRefresh, addNotifications, - backupCheckAppId, + backupCheckAppId: originAppId, }, }); @@ -48,9 +49,11 @@ export const updateSignerPolicy = ( restrictions: SignerRestriction; signingDelay: number; }, - verificationToken + verificationToken, + originAppId?: string ) => ({ type: UPDATE_SIGNER_POLICY, + originAppId, payload: { signer, signingKey, @@ -139,8 +142,9 @@ export const updateKeyDetails = (signer: VaultSigner, key: string, value: any) = }, }); -export const generateNewAddress = (wallet: Wallet | Vault) => ({ +export const generateNewAddress = (wallet: Wallet | Vault, originAppId?: string) => ({ type: GENERATE_NEW_ADDRESS, + originAppId, payload: { wallet }, }); diff --git a/src/store/sagas/bhr.ts b/src/store/sagas/bhr.ts index 206aaceb2d..152ac95bcf 100644 --- a/src/store/sagas/bhr.ts +++ b/src/store/sagas/bhr.ts @@ -48,6 +48,7 @@ import { appImagerecoveryRetry, setAppImageError, setAutomaticCloudBackup, + isAutomaticCloudBackupEnabled, setBackupAllFailure, setBackupAllLoading, setBackupAllSuccess, @@ -83,7 +84,7 @@ import { import { uaiActioned, uaiChecks } from '../sagaActions/uai'; import { setAppCreated, setAppId, setDefaultWalletCreated } from '../reducers/storage'; import { applyUpgradeSequence, KEY_MANAGEMENT_VERSION } from './upgrade'; -import { RootState } from '../store'; +import { RootState, store } from '../store'; import { setupRecoveryKeySigningKey } from 'src/hardware/signerSetup'; import { addNewWalletsWorker, addSigningDeviceWorker, NewWalletInfo } from './wallets'; import { @@ -97,17 +98,33 @@ import { addAccount, saveDefaultWalletState, setRecoveryKeyStatus } from '../red import { loadConciergeTickets, loadConciergeUser } from '../reducers/concierge'; import { USDTWallet } from 'src/services/wallets/factories/USDTWalletFactory'; +function currentBackupAppId(): string | undefined { + try { + const id = (dbManager.getObjectByIndex(RealmSchema.KeeperApp) as unknown as KeeperApp)?.id; + return id && store.getState().storage.appId === id ? id : undefined; + } catch { + return undefined; + } +} + export function* updateAppImageWorker({ payload, + originAppId, }: { + originAppId?: string; payload: { wallets?: Wallet[] | USDTWallet[]; signers?: Signer[]; updateNodes?: boolean; }; }) { + const appId = originAppId || currentBackupAppId(); + if (!appId || currentBackupAppId() !== appId) + return { updated: false, error: 'Backup account changed' }; try { - const backupResponse = yield call(checkBackupCondition); + const backupResponse = yield call(checkBackupCondition, appId); + if (currentBackupAppId() !== appId) + return { updated: false, error: 'Backup account changed' }; if (backupResponse) return { updated: true, error: '' }; const { wallets, signers, updateNodes } = payload; @@ -115,6 +132,7 @@ export function* updateAppImageWorker({ dbManager.getObjectByIndex, RealmSchema.KeeperApp ); + if (id !== appId) return { updated: false, error: 'Backup account changed' }; const walletsObject = {}; const signersObject = {}; const nodesList = []; @@ -145,6 +163,9 @@ export function* updateAppImageWorker({ } } + if (currentBackupAppId() !== appId) + return { updated: false, error: 'Backup account changed' }; + const response = yield call(Relay.updateAppImage, { appId: id, publicId, @@ -156,26 +177,33 @@ export function* updateAppImageWorker({ nodes: nodesList, replaceNodes: !!updateNodes, }); - if (!response?.updated) yield call(setServerBackupFailed); + if (!response?.updated) yield call(setServerBackupFailed, appId); return response; } catch (err) { console.log({ err }); console.error('App image update failed', err); - yield call(setServerBackupFailed); + yield call(setServerBackupFailed, appId); return { updated: true, error: '' }; } } export function* updateVaultImageWorker({ payload, + originAppId, }: { + originAppId?: string; payload: { vault: Vault; archiveVaultId?: string; isUpdate?: boolean; }; }) { - const backupResponse = yield call(checkBackupCondition); + const appId = originAppId || currentBackupAppId(); + if (!appId || currentBackupAppId() !== appId) + return { updated: false, error: 'Backup account changed' }; + const backupResponse = yield call(checkBackupCondition, appId); + if (currentBackupAppId() !== appId) + return { updated: false, error: 'Backup account changed' }; if (backupResponse) return { updated: true, error: '' }; const { vault, archiveVaultId, isUpdate } = payload; @@ -183,6 +211,7 @@ export function* updateVaultImageWorker({ dbManager.getObjectByIndex, RealmSchema.KeeperApp ); + if (id !== appId) return { updated: false, error: 'Backup account changed' }; const encryptionKey = generateEncryptionKey(primarySeed); const vaultEncrypted = encrypt( encryptionKey, @@ -200,6 +229,8 @@ export function* updateVaultImageWorker({ xfpHash: hash256(signer.masterFingerprint), }); } + if (currentBackupAppId() !== appId) + return { updated: false, error: 'Backup account changed' }; if (isUpdate) { const response = yield call(Relay.updateVaultImage, { @@ -229,7 +260,7 @@ export function* updateVaultImageWorker({ return response; } catch (err) { captureError(err); - yield call(setServerBackupFailed); + yield call(setServerBackupFailed, appId); return { updated: true, error: '' }; } } @@ -237,18 +268,26 @@ export function* updateVaultImageWorker({ // TODO: Other functions here only handle Relay backup, but this also updates local DB, should move that part out export function* deleteAppImageEntityWorker({ payload, + originAppId, }: { + originAppId?: string; payload: { signerIds?: string[]; walletIds?: string[]; }; }) { + const appId = originAppId || currentBackupAppId(); + if (!appId || currentBackupAppId() !== appId) + return { updated: false, error: 'Backup account changed' }; try { const { signerIds, walletIds } = payload; const { id }: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); + if (id !== appId) return { updated: false, error: 'Backup account changed' }; let response; - const backupResponse = yield call(checkBackupCondition); + const backupResponse = yield call(checkBackupCondition, appId); + if (currentBackupAppId() !== appId) + return { updated: false, error: 'Backup account changed' }; if (backupResponse) response = { updated: true, error: '' }; else { response = yield call(Relay.deleteAppImageEntity, { @@ -258,14 +297,46 @@ export function* deleteAppImageEntityWorker({ }); } + if (currentBackupAppId() !== appId) + return { updated: false, error: 'Backup account changed' }; + if (response?.updated !== true) { + yield put(setPendingAllBackup({ appId, pending: true })); + yield call(setServerBackupFailed, appId); + return response || { updated: false, error: 'Backup deletion not confirmed' }; + } + const remoteDeleteSucceeded = !backupResponse; + if (walletIds?.length > 0) { for (const walletId of walletIds) { - yield call(dbManager.deleteObjectById, RealmSchema.Wallet, walletId); + if (currentBackupAppId() !== appId) + return { updated: false, error: 'Backup account changed' }; + const deleted = yield call(dbManager.deleteObjectById, RealmSchema.Wallet, walletId); + if (deleted !== true) { + if (remoteDeleteSucceeded) { + yield put(setPendingAllBackup({ appId, pending: true })); + yield call(setServerBackupFailed, appId); + } + return { updated: false, error: 'Local wallet deletion failed' }; + } } } if (signerIds?.length > 0) { for (const signerId of signerIds) { - yield call(dbManager.deleteObjectByPrimaryKey, RealmSchema.Signer, 'id', signerId); + if (currentBackupAppId() !== appId) + return { updated: false, error: 'Backup account changed' }; + const deleted = yield call( + dbManager.deleteObjectByPrimaryKey, + RealmSchema.Signer, + 'id', + signerId + ); + if (deleted !== true) { + if (remoteDeleteSucceeded) { + yield put(setPendingAllBackup({ appId, pending: true })); + yield call(setServerBackupFailed, appId); + } + return { updated: false, error: 'Local signer deletion failed' }; + } } } return response; @@ -277,16 +348,24 @@ export function* deleteAppImageEntityWorker({ export function* deleteVaultImageWorker({ payload, + originAppId, }: { + originAppId?: string; payload: { vaultIds: string[]; }; }) { + const appId = originAppId || currentBackupAppId(); + if (!appId || currentBackupAppId() !== appId) + return { updated: false, error: 'Backup account changed' }; try { - const backupResponse = yield call(checkBackupCondition); + const backupResponse = yield call(checkBackupCondition, appId); + if (currentBackupAppId() !== appId) + return { updated: false, error: 'Backup account changed' }; if (backupResponse) return { updated: true, error: '' }; const { vaultIds } = payload; const { id }: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); + if (id !== appId) return { updated: false, error: 'Backup account changed' }; const response = yield call(Relay.deleteVaultImage, { appId: id, vaults: vaultIds, @@ -458,7 +537,7 @@ function* getAppImageWorker({ payload }) { } } yield put(addAccount(appID)); - yield put(autoSyncWallets(true, true, false)); + yield put(autoSyncWallets(true, true, false, appID)); yield put( saveDefaultWalletState({ appId: appID, @@ -525,6 +604,7 @@ function* recoverApp( }; yield call(writeRecoveredObject, RealmSchema.KeeperApp, app); + yield put(setAppId(appID)); // Wallet recreation if (appImage.wallets) { @@ -700,7 +780,6 @@ function* recoverApp( title: 'Recovered Wallet', }); - yield put(setAppId(appID)); } function* writeRecoveredObject(schema: RealmSchema, record: any) { @@ -978,12 +1057,13 @@ export const deleteAppImageEntityWatcher = createWatcher( DELETE_APP_IMAGE_ENTITY ); -function* runBackupInspection(repair: boolean) { +function* runBackupInspection(repair: boolean, expectedAppId?: string, explicitChoice = false) { const { id }: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); + if (expectedAppId && id !== expectedAppId) return false; yield put(setBackupRepairRunning({ appId: id, running: true })); let finalPhase: RepairPhase = 'unverified'; const updates = eventChannel((emit) => { - inspectBackup(id, repair, (phase) => emit(phase)).then( + inspectBackup(id, repair, (phase) => emit(phase), explicitChoice).then( () => emit(END), () => emit(END) ); @@ -997,20 +1077,26 @@ function* runBackupInspection(repair: boolean) { yield put(setBackupRepairState({ appId: id, phase })); } const current: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); - if (current?.id === id) { - yield put(setPendingAllBackup(finalPhase !== 'verified')); + const ownerIsActive = yield select((state: RootState) => state.storage.appId === id); + if (current?.id === id && ownerIsActive) { + yield put(setPendingAllBackup({ appId: id, pending: finalPhase !== 'verified' })); if (finalPhase === 'verified') { const notifications = dbManager.getObjectByField( RealmSchema.UAI, uaiType.SERVER_BACKUP_FAILURE, 'uaiType' - ); - for (const uai of notifications) + ) as unknown as Array<{ id: string }>; + for (const uai of notifications) { + const stillActive = yield select((state: RootState) => state.storage.appId === id); + if (!stillActive || currentBackupAppId() !== id) break; yield call(uaiActionedWorker, { payload: { uaiId: uai.id, action: true } }); + } } else if ( !dbManager.getObjectByField(RealmSchema.UAI, uaiType.SERVER_BACKUP_FAILURE, 'uaiType') ?.length ) { + const stillActive = yield select((state: RootState) => state.storage.appId === id); + if (!stillActive || currentBackupAppId() !== id) return false; yield call(addToUaiStackWorker, { payload: { uaiType: uaiType.SERVER_BACKUP_FAILURE, @@ -1029,29 +1115,40 @@ function* runBackupInspection(repair: boolean) { } } -function* backupFreshnessWorker({ type }) { - const { automaticCloudBackup } = yield select((state: RootState) => state.bhr); - if (!automaticCloudBackup) return; - yield call(runBackupInspection, type === REPAIR_BACKUP); +function* backupFreshnessWorker({ type, appId }) { + const { id }: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); + if (!appId || id !== appId) return; + const enabled = yield select((state: RootState) => + state.storage.appId === appId && isAutomaticCloudBackupEnabled(state.bhr, appId) + ); + if (!enabled) return; + yield call(runBackupInspection, type === REPAIR_BACKUP, appId); } export function* backupFreshnessWatcher() { yield takeEvery([CHECK_BACKUP_FRESHNESS, REPAIR_BACKUP], backupFreshnessWorker); } -function* backupAllSignersAndVaultsWorker() { +function* backupAllSignersAndVaultsWorker({ appId }: { appId: string }) { const { id }: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); - yield put(setBackupAllSuccess(false)); - yield put(setBackupAllFailure(false)); - yield put(setBackupAllLoading(true)); + if (!appId || appId !== id) return false; + yield put(setBackupAllSuccess({ appId, status: false })); + yield put(setBackupAllFailure({ appId, status: false })); + yield put(setBackupAllLoading({ appId, status: true })); try { - const verified = yield call(runBackupInspection, true); + const verified = yield call(runBackupInspection, true, appId, true); const current: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); - if (current?.id === id) - yield put(verified ? setBackupAllSuccess(true) : setBackupAllFailure(true)); + if (current?.id === id) { + if (verified) yield put(setAutomaticCloudBackup({ appId, enabled: true })); + yield put( + verified + ? setBackupAllSuccess({ appId, status: true }) + : setBackupAllFailure({ appId, status: true }) + ); + } return verified; } finally { - yield put(setBackupAllLoading(false)); + yield put(setBackupAllLoading({ appId, status: false })); } } @@ -1060,37 +1157,53 @@ export const backupAllSignersAndVaultsWatcher = createWatcher( BACKUP_ALL_SIGNERS_AND_VAULTS ); -function* deleteBackupWorker() { - yield put(setBackupAllLoading(true)); +function* deleteBackupWorker({ appId }: { appId: string }) { + const { id }: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); + if (!appId || appId !== id) return false; + yield put(setBackupAllLoading({ appId, status: true })); try { - const { id }: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); - - yield call(Relay.deleteBackup, { - appId: id, + const current: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); + if (current?.id !== appId) return false; + const response = yield call(Relay.deleteBackup, { + appId, }); - yield put(setDeleteBackupSuccess(true)); - yield put(setPendingAllBackup(false)); + if (response?.updated !== true && response?.data?.updated !== true) + throw new Error('Backup deletion not confirmed'); + const afterDelete: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); + if (afterDelete?.id !== appId) return false; + yield put(setAutomaticCloudBackup({ appId, enabled: false })); + yield put(setDeleteBackupSuccess({ appId, status: true })); + yield put(setPendingAllBackup({ appId, pending: false })); return true; } catch (error) { - yield put(setDeleteBackupFailure(true)); + yield put(setDeleteBackupFailure({ appId, status: true })); console.log('🚀 ~ deleteBackupWorker ~ error:', error); return false; } finally { - yield put(setBackupAllLoading(false)); + yield put(setBackupAllLoading({ appId, status: false })); } } export const deleteBackupWatcher = createWatcher(deleteBackupWorker, DELETE_BACKUP); -export function* checkBackupCondition() { - const { automaticCloudBackup } = yield select((state: RootState) => state.bhr); - if (!automaticCloudBackup) return true; +export function* checkBackupCondition(expectedAppId?: string) { const { id }: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); + if (expectedAppId && expectedAppId !== id) return true; + const enabled = yield select((state: RootState) => + state.storage.appId === id && isAutomaticCloudBackupEnabled(state.bhr, id) + ); + if (!enabled) return true; markBackupMutation(id); yield put(invalidateBackupRepair(id)); const netInfo = yield call(NetInfo.fetch); + const current: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); + const stillEnabled = yield select((state: RootState) => + state.storage.appId === id && isAutomaticCloudBackupEnabled(state.bhr, id) + ); + if (current?.id !== id || (expectedAppId && current.id !== expectedAppId) || !stillEnabled) + return true; if (!netInfo.isConnected) { - yield call(setServerBackupFailed); + yield call(setServerBackupFailed, id); return true; } // Never suppress this incremental write or replace it with a snapshot: wallet @@ -1099,19 +1212,27 @@ export function* checkBackupCondition() { return false; } -export function* setServerBackupFailed() { +export function* setServerBackupFailed(expectedAppId?: string) { + const { id: appId }: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); + if (expectedAppId && expectedAppId !== appId) return; + const ownerIsActive = yield select((state: RootState) => state.storage.appId === appId); + if (!ownerIsActive || currentBackupAppId() !== appId) return; const uaiCollection = dbManager.getObjectByField( RealmSchema.UAI, uaiType.SERVER_BACKUP_FAILURE, 'uaiType' - ); + ) as unknown as Array<{ id: string; uaiType: uaiType }>; for (const uai of uaiCollection) { if (uai.uaiType === uaiType.SERVER_BACKUP_FAILURE) { + const stillActive = yield select((state: RootState) => state.storage.appId === appId); + if (!stillActive || currentBackupAppId() !== appId) return; yield call(uaiActionedWorker, { payload: { uaiId: uai.id, action: false }, }); } } + const stillActive = yield select((state: RootState) => state.storage.appId === appId); + if (!stillActive || currentBackupAppId() !== appId) return; yield call(addToUaiStackWorker, { payload: { uaiType: uaiType.SERVER_BACKUP_FAILURE, @@ -1121,16 +1242,27 @@ export function* setServerBackupFailed() { }, }, }); - yield put(setPendingAllBackup(true)); + const activeAfterNotice = yield select((state: RootState) => state.storage.appId === appId); + if (!activeAfterNotice || currentBackupAppId() !== appId) return; + yield put(setPendingAllBackup({ appId, pending: true })); } -function* validateServerBackupWorker({ callback }) { - const matched = yield call(runBackupInspection, false); +function* validateServerBackupWorker({ callback, originAppId }) { const { id }: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); + if (!originAppId || originAppId !== id) { + callback({ status: false, error: true, message: 'Backup account changed.' }); + return; + } + const matched = yield call(runBackupInspection, false, originAppId); + const stillActive = yield select((state: RootState) => state.storage.appId === originAppId); + if (!stillActive || currentBackupAppId() !== originAppId) { + callback({ status: false, error: true, message: 'Backup account changed.' }); + return; + } const { backupRepairStateByAppId = {} } = yield select((state: RootState) => state.bhr); callback({ status: matched, - error: backupRepairStateByAppId[id] === 'unverified', + error: backupRepairStateByAppId[originAppId] === 'unverified', message: matched ? 'Backup verified successfully.' : 'Backup could not be verified.', }); } diff --git a/src/store/sagas/login.ts b/src/store/sagas/login.ts index 93465bf271..60e95eb749 100644 --- a/src/store/sagas/login.ts +++ b/src/store/sagas/login.ts @@ -57,7 +57,11 @@ import { resetSyncing } from '../reducers/wallets'; import { autoSyncWallets } from '../sagaActions/wallets'; import { connectToNode } from '../sagaActions/network'; import { fetchDelayedPolicyUpdate, fetchSignedDelayedTransaction } from '../sagaActions/storage'; -import { setAutomaticCloudBackup, setBackupType } from '../reducers/bhr'; +import { + setAutomaticCloudBackup, + setBackupType, + migrateLegacyAutomaticCloudBackup, +} from '../reducers/bhr'; import { addAccount, saveDefaultWalletState, @@ -185,11 +189,21 @@ function* credentialsAuthWorker({ payload }) { } const previousVersion = yield select((state) => state.storage.appVersion); - const { plebDueToOffline, wasAutoUpdateEnabledBeforeDowngrade, defaultWalletCreated } = - yield select((state) => state.storage); + const { defaultWalletCreated } = yield select((state) => state.storage); // setting correct app id from realm at login const keeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); + if (keeperApp?.id) { + const onlyKnownAccount = + allAccounts.length <= 1 && + (!allAccounts.length || allAccounts[0].appId === keeperApp.id); + yield put( + migrateLegacyAutomaticCloudBackup({ + appId: keeperApp.id, + canAttributeConsent: onlyKnownAccount && appId === keeperApp.id, + }) + ); + } if (keeperApp?.id) yield put(setAppId(keeperApp.id)); // Store temporary account details @@ -292,14 +306,16 @@ async function downgradeToPleb() { subscription: updatedSubscription, }); store.dispatch(setSubscription(updatedSubscription.name)); - store.dispatch(setAutomaticCloudBackup(false)); + store.dispatch(setAutomaticCloudBackup({ appId: app.id, enabled: false })); await Relay.updateSubscription(app.id, app.publicId, { productId: SubscriptionTier.L1.toLowerCase(), }); } -async function updateSubscriptionFromRelayData(data, wasAutoUpdateEnabledBeforeDowngrade) { +async function updateSubscriptionFromRelayData(data) { const app: KeeperApp = await dbManager.getObjectByIndex(RealmSchema.KeeperApp); + const wasAutoUpdateEnabledBeforeDowngrade = + store.getState().storage.wasAutoUpdateEnabledBeforeDowngradeByAppId?.[app.id] === true; const isBtcPayment = data?.paymentType == 'btc_payment'; let updatedSubscription: SubScription; if (isBtcPayment) { @@ -322,9 +338,11 @@ async function updateSubscriptionFromRelayData(data, wasAutoUpdateEnabledBeforeD subscription: updatedSubscription, }); store.dispatch(setSubscription(updatedSubscription.name)); - store.dispatch(setAutomaticCloudBackup(wasAutoUpdateEnabledBeforeDowngrade)); + store.dispatch( + setAutomaticCloudBackup({ appId: app.id, enabled: wasAutoUpdateEnabledBeforeDowngrade }) + ); store.dispatch(setPlebDueToOffline(false)); - store.dispatch(setAutoUpdateEnabledBeforeDowngrade(false)); + store.dispatch(setAutoUpdateEnabledBeforeDowngrade({ appId: app.id, enabled: false })); } export const credentialsAuthWatcher = createWatcher(credentialsAuthWorker, CREDS_AUTH); diff --git a/src/store/sagas/send_and_receive.ts b/src/store/sagas/send_and_receive.ts index f9c988a13b..12b95f0eb8 100644 --- a/src/store/sagas/send_and_receive.ts +++ b/src/store/sagas/send_and_receive.ts @@ -129,7 +129,7 @@ function* sendPhaseOneWorker({ payload }: SendPhaseOneAction) { export const sendPhaseOneWatcher = createWatcher(sendPhaseOneWorker, SEND_PHASE_ONE); -function* sendPhaseTwoWorker({ payload }: SendPhaseTwoAction) { +function* sendPhaseTwoWorker({ payload, originAppId }: SendPhaseTwoAction & { originAppId?: string }) { if (!ELECTRUM_CLIENT.isClientConnected) { ElectrumClient.resetCurrentPeerIndex(); yield call(connectToNodeWorker); @@ -170,6 +170,7 @@ function* sendPhaseTwoWorker({ payload }: SendPhaseTwoAction) { if (!txid) throw new Error('Send failed: unable to generate txid'); if (note) { yield call(addLabelsWorker, { + originAppId, payload: { txId: txid, wallet, @@ -205,7 +206,7 @@ function* sendPhaseTwoWorker({ payload }: SendPhaseTwoAction) { } if (finalOutputs) { - yield call(handleChangeOutputLabels, finalOutputs, inputs, wallet, txid); + yield call(handleChangeOutputLabels, finalOutputs, inputs, wallet, txid, originAppId); } } catch (err) { if ([ELECTRUM_NOT_CONNECTED_ERR, ELECTRUM_NOT_CONNECTED_ERR_TOR].includes(err?.message)) { @@ -223,7 +224,7 @@ function* sendPhaseTwoWorker({ payload }: SendPhaseTwoAction) { export const sendPhaseTwoWatcher = createWatcher(sendPhaseTwoWorker, SEND_PHASE_TWO); -function* sendPhaseThreeWorker({ payload }: SendPhaseThreeAction) { +function* sendPhaseThreeWorker({ payload, originAppId }: SendPhaseThreeAction & { originAppId?: string }) { if (!ELECTRUM_CLIENT.isClientConnected) { ElectrumClient.resetCurrentPeerIndex(); yield call(connectToNodeWorker); @@ -276,6 +277,7 @@ function* sendPhaseThreeWorker({ payload }: SendPhaseThreeAction) { if (note) { yield call(addLabelsWorker, { + originAppId, payload: { txId: txid, wallet, @@ -286,7 +288,7 @@ function* sendPhaseThreeWorker({ payload }: SendPhaseThreeAction) { } if (finalOutputs) { - yield call(handleChangeOutputLabels, finalOutputs, inputs, wallet, txid); + yield call(handleChangeOutputLabels, finalOutputs, inputs, wallet, txid, originAppId); } } catch (err) { if ([ELECTRUM_NOT_CONNECTED_ERR, ELECTRUM_NOT_CONNECTED_ERR_TOR].includes(err?.message)) { @@ -304,7 +306,7 @@ function* sendPhaseThreeWorker({ payload }: SendPhaseThreeAction) { export const sendPhaseThreeWatcher = createWatcher(sendPhaseThreeWorker, SEND_PHASE_THREE); -function* handleChangeOutputLabels(finalOutputs, inputs, wallet, txid) { +function* handleChangeOutputLabels(finalOutputs, inputs, wallet, txid, originAppId?: string) { const changeOutputIndex = finalOutputs.findIndex((output) => Object.values(wallet.specs.addresses.internal).includes(output.address) ); @@ -340,6 +342,7 @@ function* handleChangeOutputLabels(finalOutputs, inputs, wallet, txid) { } yield fork(bulkUpdateLabelsWorker, { + originAppId, payload: { labelChanges, UTXO: { txId: txid, vout: changeOutputIndex }, @@ -463,7 +466,7 @@ function* calculateCustomFee({ payload }: CalculateCustomFeeAction) { export const calculateCustomFeeWatcher = createWatcher(calculateCustomFee, CALCULATE_CUSTOM_FEE); -function* discardBroadcastedTnxWorker({ payload }) { +function* discardBroadcastedTnxWorker({ payload, originAppId }) { const { cachedTxid, vault } = payload; const snapshots = yield select((state) => state.cachedTxn.snapshots); if (snapshots[cachedTxid]) { @@ -476,9 +479,10 @@ function* discardBroadcastedTnxWorker({ payload }) { }); const potentialTxId = snapshots[cachedTxid].potentialTxId; const note = snapshots[cachedTxid].routeParams.note; - yield call(handleChangeOutputLabels, finalOutputs, inputs, vault, potentialTxId); + yield call(handleChangeOutputLabels, finalOutputs, inputs, vault, potentialTxId, originAppId); if (note.length) { yield call(addLabelsWorker, { + originAppId, payload: { txId: potentialTxId, wallet: vault, @@ -512,4 +516,4 @@ const reformatExchangeRates = (rawExchangeRates) => { } }); return formattedExchangeRates; -}; \ No newline at end of file +}; diff --git a/src/store/sagas/upgrade.ts b/src/store/sagas/upgrade.ts index e7dd01185f..27055ddbfc 100644 --- a/src/store/sagas/upgrade.ts +++ b/src/store/sagas/upgrade.ts @@ -38,8 +38,8 @@ import { import { updateAppImageWorker, updateVaultImageWorker } from './bhr'; import { createWatcher } from '../utilities'; import { setAppVersion } from '../reducers/storage'; -import { setPendingAllBackup } from '../reducers/bhr'; -import { RootState } from '../store'; +import { isAutomaticCloudBackupEnabled, setPendingAllBackup } from '../reducers/bhr'; +import { RootState, store } from '../store'; export const LABELS_INTRODUCTION_VERSION = '1.0.4'; export const BIP329_INTRODUCTION_VERSION = '1.0.7'; @@ -48,6 +48,10 @@ export const APP_KEY_UPGRADE_VERSION = '1.1.12'; export const HEALTH_CHECK_TIMELINE_MIGRATION_VERSION = '1.2.6'; export const SIGNER_POLICY_MIGRATION_VERSION = '2.1.0'; +const upgradeAccountIsActive = (appId?: string) => + !!appId && store.getState().storage.appId === appId && + (dbManager.getObjectByIndex(RealmSchema.KeeperApp) as unknown as KeeperApp)?.id === appId; + export function* applyUpgradeSequence({ previousVersion, newVersion, @@ -58,19 +62,22 @@ export function* applyUpgradeSequence({ isRecovery?: boolean; }) { console.log(`applying upgrade sequence - from: ${previousVersion} to ${newVersion}`); + const { id: originAppId }: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); + if (!originAppId) return; if ( semver.gte(previousVersion, LABELS_INTRODUCTION_VERSION) && semver.lt(previousVersion, BIP329_INTRODUCTION_VERSION) ) { - yield put(migrateLabelsToBip329()); + yield put(migrateLabelsToBip329(originAppId)); } if (semver.lt(previousVersion, KEY_MANAGEMENT_VERSION)) { - yield call(migrateStructureforSignersInAppImage); - yield call(migrateStructureforVaultInAppImage); + yield call(migrateStructureforSignersInAppImage, originAppId); + yield call(migrateStructureforVaultInAppImage, originAppId); } - if (semver.lt(previousVersion, APP_KEY_UPGRADE_VERSION)) yield call(updateAppKeysToEnableSigning); + if (semver.lt(previousVersion, APP_KEY_UPGRADE_VERSION)) + yield call(updateAppKeysToEnableSigning, originAppId); if (semver.lt(previousVersion, HEALTH_CHECK_TIMELINE_MIGRATION_VERSION)) { yield call(healthCheckTimelineMigration); @@ -78,25 +85,36 @@ export function* applyUpgradeSequence({ if (semver.lt(previousVersion, SIGNER_POLICY_MIGRATION_VERSION)) { yield call(migrateServerKeyPolicy); - yield put(setPendingAllBackup(true)); + const app: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); + if (app?.id === originAppId) yield put(setPendingAllBackup({ appId: originAppId, pending: true })); } + if (!upgradeAccountIsActive(originAppId)) return; yield put(setAppVersion(newVersion)); - yield put(updateVersionHistory(previousVersion, newVersion, isRecovery)); + yield put(updateVersionHistory(previousVersion, newVersion, isRecovery, originAppId)); } export function* updateVersionHistoryWorker({ payload, + originAppId, }: { payload: { previousVersion: string; newVersion: string; isRecovery?: boolean }; + originAppId?: string; }) { const { previousVersion, newVersion, isRecovery = false } = payload; try { const app: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); - const response = yield call(Relay.updateAppImage, { - appId: app.id, - version: newVersion, - }); + if (!originAppId || app?.id !== originAppId) return; + const enabled = yield select((state: RootState) => + state.storage.appId === originAppId && isAutomaticCloudBackupEnabled(state.bhr, originAppId) + ); + if (enabled) { + yield call(Relay.updateAppImage, { + appId: originAppId, + version: newVersion, + }); + } + if (!upgradeAccountIsActive(originAppId)) return; yield call(dbManager.createObject, RealmSchema.VersionHistory, { version: `${newVersion}(${DeviceInfo.getBuildNumber()})`, date: new Date().toString(), @@ -116,8 +134,9 @@ export const updateVersionHistoryWatcher = createWatcher( UPDATE_VERSION_HISTORY ); -function* migrateLablesWorker() { +function* migrateLablesWorker({ originAppId }: { originAppId?: string }) { try { + if (!upgradeAccountIsActive(originAppId)) return; const UTXOLabels: UTXOInfo[] = yield call(dbManager.getCollection, RealmSchema.UTXOInfo); const tags = []; const wallets: Wallet[] = yield call(dbManager.getCollection, RealmSchema.Wallet); @@ -143,18 +162,24 @@ function* migrateLablesWorker() { }); if (tags.length) { + if (!upgradeAccountIsActive(originAppId)) return; yield call(dbManager.createObjectBulk, RealmSchema.Tags, tags); const { id, primarySeed }: KeeperApp = yield call( dbManager.getObjectByIndex, RealmSchema.KeeperApp ); + if (id !== originAppId) return; const encryptionKey = generateEncryptionKey(primarySeed); const tagsToBackup = tags.map((tag) => ({ id: hash256(hash256(encryptionKey + tag.id)), content: encrypt(encryptionKey, JSON.stringify(tag)), })); - yield call(Relay.modifyLabels, id, tagsToBackup.length ? tagsToBackup : [], []); + if (!upgradeAccountIsActive(originAppId)) return; + const enabled = yield select((state: RootState) => + state.storage.appId === originAppId && isAutomaticCloudBackupEnabled(state.bhr, originAppId) + ); + if (enabled) yield call(Relay.modifyLabels, id, tagsToBackup, []); } } catch (error) { console.log({ error }); @@ -163,11 +188,12 @@ function* migrateLablesWorker() { export const migrateLablesWatcher = createWatcher(migrateLablesWorker, MIGRATE_LABELS_329); -function* migrateStructureforSignersInAppImage() { +function* migrateStructureforSignersInAppImage(originAppId: string) { try { + if (!upgradeAccountIsActive(originAppId)) return; const wallets = yield call(dbManager.getCollection, RealmSchema.Wallet); const signers = yield call(dbManager.getCollection, RealmSchema.Signer); - const response = yield call(updateAppImageWorker, { payload: { wallets, signers } }); + const response = yield call(updateAppImageWorker, { payload: { wallets, signers }, originAppId }); if (response.updated) { console.log('Updated the Signers in app image'); } else { @@ -176,13 +202,15 @@ function* migrateStructureforSignersInAppImage() { } catch (err) {} } -function* migrateStructureforVaultInAppImage() { +function* migrateStructureforVaultInAppImage(originAppId: string) { try { + if (!upgradeAccountIsActive(originAppId)) return; const vaults: Vault[] = yield call(dbManager.getCollection, RealmSchema.Vault); const activeVault: Vault = vaults.filter((vault) => !vault.archived)[0] || null; yield call(updateVaultImageWorker, { payload: { isUpdate: true, vault: activeVault }, + originAppId, }); } catch (err) { console.log('Something went wrong in updating the vault image', err); @@ -190,8 +218,9 @@ function* migrateStructureforVaultInAppImage() { } // This function updates app keys/mobile keys to enable signing by generating and associating extended keys. -function* updateAppKeysToEnableSigning() { +function* updateAppKeysToEnableSigning(originAppId: string) { try { + if (!upgradeAccountIsActive(originAppId)) return; const wallets = yield call(dbManager.getCollection, RealmSchema.Wallet); const signers = yield call(dbManager.getCollection, RealmSchema.Signer); const keeperSigners = signers.filter( @@ -202,9 +231,10 @@ function* updateAppKeysToEnableSigning() { ); const { appKeyWalletMap, myAppKeySigners } = mapAppKeysToWallets(wallets, keeperSigners); const extendedKeyMap = generateExtendedKeysForSigners(myAppKeySigners, appKeyWalletMap); + if (!upgradeAccountIsActive(originAppId)) return; updateVaultSigners(extendedKeyMap, signers); updateSignerDetails(myAppKeySigners, extendedKeyMap); - const response = yield call(updateAppImageWorker, { payload: { signers } }); + const response = yield call(updateAppImageWorker, { payload: { signers }, originAppId }); if (response.updated) { console.log('Updated the Signers in app image'); } else { diff --git a/src/store/sagas/utxos.ts b/src/store/sagas/utxos.ts index 7f3d1addec..e08a8c83a0 100644 --- a/src/store/sagas/utxos.ts +++ b/src/store/sagas/utxos.ts @@ -1,7 +1,7 @@ import dbManager from 'src/storage/realm/dbManager'; import { RealmSchema } from 'src/storage/realm/enum'; -import { call, delay, fork, put, takeLatest } from 'redux-saga/effects'; -import { BIP329Label, UTXO, UTXOSpendability } from 'src/services/wallets/interfaces'; +import { call, delay, put } from 'redux-saga/effects'; +import { BIP329Label, UTXOSpendability } from 'src/services/wallets/interfaces'; import { EntityKind, LabelRefType } from 'src/services/wallets/enums'; import Relay from 'src/services/backend/Relay'; import { Wallet } from 'src/services/wallets/interfaces/wallet'; @@ -10,6 +10,7 @@ import { Vault } from 'src/services/wallets/interfaces/vault'; import { KeeperApp } from 'src/models/interfaces/KeeperApp'; import { createWatcher } from '../utilities'; import { getJSONFromRealmObject } from 'src/storage/realm/utils'; +import { store } from '../store'; import { ADD_LABELS, @@ -26,8 +27,39 @@ import { } from './bhr'; import { encrypt, generateEncryptionKey, hash256 } from 'src/utils/service-utilities/encryption'; +function currentOriginApp(originAppId?: string): KeeperApp | null { + if (!originAppId || store.getState().storage.appId !== originAppId) return null; + try { + const app = dbManager.getObjectByIndex(RealmSchema.KeeperApp) as unknown as KeeperApp; + return app?.id === originAppId ? app : null; + } catch { + return null; + } +} + +// Keep the ownership check and local writes in one synchronous turn. An action +// created for A must never mutate B's Realm after a network or saga wait. +function persistTagsForOrigin(originAppId: string, added: BIP329Label[], deleted: string[] = []) { + if (!currentOriginApp(originAppId)) return false; + if (added.length && dbManager.createObjectBulk(RealmSchema.Tags, added) !== true) return false; + for (const id of deleted) { + if (dbManager.deleteObjectById(RealmSchema.Tags, id) !== true) return false; + } + return true; +} + +function persistSpendabilityForOrigin(originAppId: string, schema: RealmSchema, id: string, specs: any) { + if (!currentOriginApp(originAppId)) return false; + return dbManager.updateObjectById(schema, id, { specs }) === true; +} + +function originUiAction(action: T, originAppId: string): T & { originAppId: string } { + return { ...action, originAppId }; +} + export function* addLabelsWorker({ payload, + originAppId, }: { payload: { txId: string; @@ -36,9 +68,14 @@ export function* addLabelsWorker({ labels: { name: string; isSystem: boolean }[]; type; }; + originAppId?: string; }) { try { - yield put(setSyncingUTXOs(true)); + if (!originAppId) return; + const app = currentOriginApp(originAppId); + if (!app) return; + const primarySeed = app.primarySeed; + yield put(originUiAction(setSyncingUTXOs(true), originAppId)); const { txId, vout, wallet, labels, type } = payload; const origin = generateAbbreviatedOutputDescriptors(wallet); const tags = []; @@ -54,35 +91,32 @@ export function* addLabelsWorker({ }; tags.push(tag); }); - const { id, primarySeed }: KeeperApp = yield call( - dbManager.getObjectByIndex, - RealmSchema.KeeperApp - ); - yield call(dbManager.createObjectBulk, RealmSchema.Tags, tags); + if (!(yield call(persistTagsForOrigin, originAppId, tags))) return; try { - const backupResponse = yield call(checkBackupCondition); - if (!backupResponse) { + const backupResponse = yield call(checkBackupCondition, originAppId); + if (!backupResponse && currentOriginApp(originAppId)) { const encryptionKey = generateEncryptionKey(primarySeed); const tagsToBackup = tags.map((tag) => ({ id: hash256(hash256(encryptionKey + tag.id)), content: encrypt(encryptionKey, JSON.stringify(tag)), })); - yield call(Relay.modifyLabels, id, tagsToBackup, []); + yield call(Relay.modifyLabels, originAppId, tagsToBackup, []); } else yield delay(100); } catch (error) { console.log('🚀 ~ addLabelsWorker error:', error); - yield call(setServerBackupFailed); + yield call(setServerBackupFailed, originAppId); } - yield put(resetState()); + yield put(originUiAction(resetState(), originAppId)); } catch (e) { - yield put(setSyncingUTXOError(e)); + if (originAppId) yield put(originUiAction(setSyncingUTXOError(e), originAppId)); } finally { - yield put(setSyncingUTXOs(false)); + if (originAppId) yield put(originUiAction(setSyncingUTXOs(false), originAppId)); } } export function* bulkUpdateLabelsWorker({ payload, + originAppId, }: { payload: { labelChanges: { @@ -94,9 +128,14 @@ export function* bulkUpdateLabelsWorker({ address?: string; wallet: Wallet; }; + originAppId?: string; }) { try { - yield put(setSyncingUTXOs(true)); + if (!originAppId) return; + const app = currentOriginApp(originAppId); + if (!app) return; + const primarySeed = app.primarySeed; + yield put(originUiAction(setSyncingUTXOs(true), originAppId)); const { labelChanges, wallet, UTXO, txId, address } = payload; const origin = generateAbbreviatedOutputDescriptors(wallet); let addedTags: BIP329Label[] = []; @@ -117,43 +156,37 @@ export function* bulkUpdateLabelsWorker({ if (labelChanges.deleted) { deletedTagIds = labelChanges.deleted.map((label) => `${ref}${label.name}`); } - const { primarySeed, id }: KeeperApp = yield call( - dbManager.getObjectByIndex, - RealmSchema.KeeperApp - ); - yield call(dbManager.createObjectBulk, RealmSchema.Tags, addedTags); - for (const element of deletedTagIds) { - yield call(dbManager.deleteObjectById, RealmSchema.Tags, element); - } + if (!(yield call(persistTagsForOrigin, originAppId, addedTags, deletedTagIds))) return; try { - const backupResponse = yield call(checkBackupCondition); - if (!backupResponse) { + const backupResponse = yield call(checkBackupCondition, originAppId); + if (!backupResponse && currentOriginApp(originAppId)) { const encryptionKey = generateEncryptionKey(primarySeed); const tagsToBackup = addedTags.map((tag) => ({ id: hash256(hash256(encryptionKey + tag.id)), content: encrypt(encryptionKey, JSON.stringify(tag)), })); const tagsToDelete = deletedTagIds.map((tag) => hash256(hash256(encryptionKey + tag))); - yield fork( + yield call( Relay.modifyLabels, - id, + originAppId, tagsToBackup.length ? tagsToBackup : [], tagsToDelete.length ? tagsToDelete : [] ); } else yield delay(100); } catch (error) { - yield call(setServerBackupFailed); + yield call(setServerBackupFailed, originAppId); } - yield put(resetState()); + yield put(originUiAction(resetState(), originAppId)); } catch (e) { - yield put(setSyncingUTXOError(e)); + if (originAppId) yield put(originUiAction(setSyncingUTXOError(e), originAppId)); } finally { - yield put(setSyncingUTXOs(false)); + if (originAppId) yield put(originUiAction(setSyncingUTXOs(false), originAppId)); } } export function* importLabelsWorker({ payload, + originAppId, }: { payload: { labels: [ @@ -165,9 +198,14 @@ export function* importLabelsWorker({ } ]; }; + originAppId?: string; }) { try { - yield put(setSyncingUTXOs(true)); + if (!originAppId) return; + const app = currentOriginApp(originAppId); + if (!app) return; + const primarySeed = app.primarySeed; + yield put(originUiAction(setSyncingUTXOs(true), originAppId)); const { labels } = payload; let addedTags: BIP329Label[] = []; if (labels) { @@ -183,30 +221,26 @@ export function* importLabelsWorker({ })); } - const { primarySeed, id }: KeeperApp = yield call( - dbManager.getObjectByIndex, - RealmSchema.KeeperApp - ); - yield call(dbManager.createObjectBulk, RealmSchema.Tags, addedTags); + if (!(yield call(persistTagsForOrigin, originAppId, addedTags))) return; try { - const backupResponse = yield call(checkBackupCondition); - if (!backupResponse) { + const backupResponse = yield call(checkBackupCondition, originAppId); + if (!backupResponse && currentOriginApp(originAppId)) { const encryptionKey = generateEncryptionKey(primarySeed); const tagsToBackup = addedTags.map((tag) => ({ id: hash256(hash256(encryptionKey + tag.id)), content: encrypt(encryptionKey, JSON.stringify(tag)), })); - yield fork(Relay.modifyLabels, id, tagsToBackup.length ? tagsToBackup : [], []); + yield call(Relay.modifyLabels, originAppId, tagsToBackup.length ? tagsToBackup : [], []); } else yield delay(100); } catch (error) { - yield call(setServerBackupFailed); + yield call(setServerBackupFailed, originAppId); } - yield put(resetState()); + yield put(originUiAction(resetState(), originAppId)); } catch (e) { - yield put(setSyncingUTXOError(e)); + if (originAppId) yield put(originUiAction(setSyncingUTXOError(e), originAppId)); } finally { - yield put(setSyncingUTXOs(false)); + if (originAppId) yield put(originUiAction(setSyncingUTXOs(false), originAppId)); } } @@ -216,6 +250,7 @@ export const importLabelsWatcher = createWatcher(importLabelsWorker, IMPORT_LABE export function* markUTXOSpendabilityWorker({ payload, + originAppId, }: { payload: { wallet: any; @@ -223,8 +258,11 @@ export function* markUTXOSpendabilityWorker({ vout: number; spendability: UTXOSpendability; }; + originAppId?: string; }) { try { + if (!originAppId) return; + if (!currentOriginApp(originAppId)) return; const { wallet, txId, vout, spendability } = payload; const schema = wallet.entityKind === EntityKind.VAULT ? RealmSchema.Vault : RealmSchema.Wallet; @@ -253,7 +291,7 @@ export function* markUTXOSpendabilityWorker({ } if (!changed) return; - const persisted = yield call(dbManager.updateObjectById, schema, wallet.id, { specs }); + const persisted = yield call(persistSpendabilityForOrigin, originAppId, schema, wallet.id, specs); if (persisted !== true) return; // Persist the user's choice first, then invalidate/refresh Assisted Server // Backup through its normal opt-in/offline-aware incremental path. @@ -262,11 +300,15 @@ export function* markUTXOSpendabilityWorker({ schema === RealmSchema.Vault ? yield call(updateVaultImageWorker, { payload: { vault: walletJSON as Vault, isUpdate: true }, + originAppId, }) - : yield call(updateAppImageWorker, { payload: { wallets: [walletJSON as Wallet] } }); - if (!response?.updated) yield call(setServerBackupFailed); + : yield call(updateAppImageWorker, { + payload: { wallets: [walletJSON as Wallet] }, + originAppId, + }); + if (!response?.updated) yield call(setServerBackupFailed, originAppId); } catch { - yield call(setServerBackupFailed); + yield call(setServerBackupFailed, originAppId); } } catch (e) { console.log('markUTXOSpendabilityWorker error:', e); diff --git a/src/store/sagas/wallets.ts b/src/store/sagas/wallets.ts index cd3d786253..c420b1bcb8 100644 --- a/src/store/sagas/wallets.ts +++ b/src/store/sagas/wallets.ts @@ -70,7 +70,7 @@ import { sanitizeSeedKeyForBackup, } from 'src/utils/utilities'; import { COLLABORATIVE_SCHEME } from 'src/screens/SigningDevices/SetupCollaborativeWallet'; -import { RootState } from '../store'; +import { RootState, store } from '../store'; import { checkBackupFreshness } from '../sagaActions/bhr'; import { @@ -131,6 +131,7 @@ import { showDeletingKeyModal, hideDeletingKeyModal, showKeyDeletedSuccessModal, + isAutomaticCloudBackupEnabled, } from '../reducers/bhr'; import { setElectrumNotConnectedErr } from '../reducers/login'; import { connectToNodeWorker } from './network'; @@ -141,6 +142,21 @@ import { accountNoFromDerivationPath } from 'src/utils/service-utilities/utils'; import { classifyDustByAddress } from 'src/services/wallets/operations/dustClassification'; import { setPendingDustToast } from '../reducers/utxos'; +const currentWalletAccountId = (): string | undefined => { + try { + return (dbManager.getObjectByIndex(RealmSchema.KeeperApp) as unknown as KeeperApp)?.id; + } catch { + return undefined; + } +}; +const assertWalletAccount = (appId: string) => { + if ( + !appId || + currentWalletAccountId() !== appId || + store.getState().storage.appId !== appId + ) throw new Error('Account changed'); +}; + export interface NewVaultDetails { name?: string; description?: string; @@ -210,10 +226,18 @@ function* addNewWallet( } } -export function* addNewWalletsWorker({ payload: newWalletInfo }: { payload: NewWalletInfo[] }) { +export function* addNewWalletsWorker({ + payload: newWalletInfo, + originAppId, +}: { + payload: NewWalletInfo[]; + originAppId?: string; +}) { try { const wallets: Wallet[] = []; const app: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); + const sourceAppId = originAppId || app?.id; + assertWalletAccount(sourceAppId); for (const { walletType, walletDetails, importDetails } of newWalletInfo) { const wallet: Wallet = yield call( @@ -228,8 +252,12 @@ export function* addNewWalletsWorker({ payload: newWalletInfo }: { payload: NewW if (wallets.length > 0) { yield put(setRelayWalletUpdateLoading(true)); - const response = yield call(updateAppImageWorker, { payload: { wallets } }); + const response = yield call(updateAppImageWorker, { + payload: { wallets }, + originAppId: sourceAppId, + }); if (response.updated) { + assertWalletAccount(sourceAppId); yield call(dbManager.createObjectBulk, RealmSchema.Wallet, wallets); yield put(relayWalletUpdateSuccess()); return true; @@ -260,7 +288,9 @@ export interface NewVaultInfo { export function* addNewVaultWorker({ payload, + originAppId, }: { + originAppId?: string; payload: { newVaultInfo?: NewVaultInfo; isMigrated?: boolean; @@ -268,6 +298,8 @@ export function* addNewVaultWorker({ }; }) { try { + const sourceAppId = originAppId || currentWalletAccountId(); + assertWalletAccount(sourceAppId); const { newVaultInfo, isMigrated, oldVaultId } = payload; const { bitcoinNetworkType: networkType } = yield select((state: RootState) => state.settings); @@ -297,8 +329,12 @@ export function* addNewVaultWorker({ } yield put(setRelayVaultUpdateLoading(true)); - const newVaultResponse = yield call(updateVaultImageWorker, { payload: { vault } }); + const newVaultResponse = yield call(updateVaultImageWorker, { + payload: { vault }, + originAppId: sourceAppId, + }); if (newVaultResponse.updated) { + assertWalletAccount(sourceAppId); yield call(dbManager.createObject, RealmSchema.Vault, vault); yield put(uaiChecks([uaiType.SECURE_VAULT])); @@ -312,6 +348,7 @@ export function* addNewVaultWorker({ archivedId: oldVault.archivedId ? oldVault.archivedId : oldVault.id, }; const archivedVaultresponse = yield call(updateVaultImageWorker, { + originAppId: sourceAppId, payload: { vault: { ...oldVault, @@ -320,6 +357,7 @@ export function* addNewVaultWorker({ }, }); if (archivedVaultresponse.updated) { + assertWalletAccount(sourceAppId); yield call(dbManager.updateObjectById, RealmSchema.Vault, oldVaultId, updatedParams); yield put(initiateVaultMigration({ intrimVault: vault })); } @@ -355,14 +393,18 @@ export const addNewVaultWatcher = createWatcher(addNewVaultWorker, ADD_NEW_VAULT export function* addSigningDeviceWorker({ payload: { signers }, callback, + originAppId, }: { payload: { signers: Signer[] }; callback?: () => void; + originAppId?: string; }) { if (!signers.length) return; + const sourceAppId = originAppId || currentWalletAccountId(); + assertWalletAccount(sourceAppId); for (let i = 0; i < signers.length; i++) { const signer = signers[i]; - yield call(mergeSimilarKeysWorker, { payload: { signer } }); + yield call(mergeSimilarKeysWorker, { payload: { signer }, originAppId: sourceAppId }); } try { const { bitcoinNetworkType } = yield select((state: RootState) => state.settings); @@ -484,8 +526,12 @@ export function* addSigningDeviceWorker({ id: getKeyUID(signer), })); yield put(setRelaySignersUpdateLoading(true)); - const response = yield call(updateAppImageWorker, { payload: { signers: signersToUpdate } }); + const response = yield call(updateAppImageWorker, { + payload: { signers: signersToUpdate }, + originAppId: sourceAppId, + }); if (response.updated) { + assertWalletAccount(sourceAppId); yield call( dbManager.createObjectBulk, RealmSchema.Signer, @@ -499,7 +545,7 @@ export function* addSigningDeviceWorker({ (signersToUpdate[0]?.extraData?.instanceNumber !== 1 && !signersToUpdate[0]?.hidden) ) ); - yield call(updateVaultSignerXprivWorker, { signers }); + yield call(updateVaultSignerXprivWorker, { signers, originAppId: sourceAppId }); } else { const errorMsg = response.error?.message ? response.error.message.toString() @@ -518,29 +564,46 @@ export function* addSigningDeviceWorker({ export const addSigningDeviceWatcher = createWatcher(addSigningDeviceWorker, ADD_SIGINING_DEVICE); -function* deleteSigningDeviceWorker({ payload: { signers } }: { payload: { signers: Signer[] } }) { +function* deleteSigningDeviceWorker({ + payload: { signers }, + originAppId, +}: { + payload: { signers: Signer[] }; + originAppId?: string; +}) { + const sourceAppId = originAppId || currentWalletAccountId(); try { + assertWalletAccount(sourceAppId); if (signers.length) { yield put(showDeletingKeyModal()); const signersToDeleteIds = []; for (const signer of signers) { signersToDeleteIds.push(getKeyUID(signer)); } - for (let i = 0; i < signers.length; i++) { - yield call(deleteAppImageEntityWorker, { - payload: { - signerIds: signersToDeleteIds, - }, - }); + const response = yield call(deleteAppImageEntityWorker, { + originAppId: sourceAppId, + payload: { signerIds: signersToDeleteIds }, + }); + if (!response?.updated) { + assertWalletAccount(sourceAppId); + yield put(hideDeletingKeyModal()); + yield put(relaySignersUpdateFail('An error occurred while deleting signers.')); + return false; } + assertWalletAccount(sourceAppId); yield put(uaiChecks([uaiType.SIGNING_DEVICES_HEALTH_CHECK])); yield put(hideDeletingKeyModal()); yield put(showKeyDeletedSuccessModal()); + return true; } + return false; } catch (error) { captureError(error); - yield put(hideDeletingKeyModal()); - yield put(relaySignersUpdateFail('An error occurred while deleting signers.')); + if (currentWalletAccountId() === sourceAppId && store.getState().storage.appId === sourceAppId) { + yield put(hideDeletingKeyModal()); + yield put(relaySignersUpdateFail('An error occurred while deleting signers.')); + } + return false; } } @@ -549,8 +612,16 @@ export const deleteSigningDeviceWatcher = createWatcher( DELETE_SIGINING_DEVICE ); -function* archiveSigningDeviceWorker({ payload: { signers } }: { payload: { signers: Signer[] } }) { +function* archiveSigningDeviceWorker({ + payload: { signers }, + originAppId, +}: { + payload: { signers: Signer[] }; + originAppId?: string; +}) { try { + const sourceAppId = originAppId || currentWalletAccountId(); + assertWalletAccount(sourceAppId); const signersToArchiveIds = []; for (const signer of signers) { signersToArchiveIds.push(getKeyUID(signer)); @@ -558,6 +629,7 @@ function* archiveSigningDeviceWorker({ payload: { signers } }: { payload: { sign if (signers.length) { for (let i = 0; i < signers.length; i++) { yield call(updateSignerDetailsWorker, { + originAppId: sourceAppId, payload: { signer: signers[i], key: 'archived', @@ -580,12 +652,17 @@ export const archiveSigningDeviceWatcher = createWatcher( function* migrateVaultWorker({ payload, + originAppId, }: { payload: { newVaultInfo: NewVaultInfo; oldVaultId: string }; + originAppId?: string; }) { try { + const sourceAppId = originAppId || currentWalletAccountId(); + assertWalletAccount(sourceAppId); const { newVaultInfo, oldVaultId } = payload; const migrated = yield call(addNewVaultWorker, { + originAppId: sourceAppId, payload: { newVaultInfo, isMigrated: true, oldVaultId }, }); const migratedVault = yield select((state: RootState) => state.vault.intrimVault); @@ -613,12 +690,15 @@ export const migrateVaultWatcher = createWatcher(migrateVaultWorker, MIGRATE_VAU function* refreshWalletsWorker({ payload, + originAppId, }: { + originAppId?: string; payload: { wallets: (Wallet | Vault)[]; options: { hardRefresh?: boolean; addNotifications?: boolean; dustScan?: boolean }; }; }) { + const sourceAppId = originAppId || currentWalletAccountId(); let { wallets, options } = payload; // Filter out pre-mix, post-mix, and bad bank wallets as they are no longer displayed or used. @@ -634,6 +714,7 @@ function* refreshWalletsWorker({ }); try { + assertWalletAccount(sourceAppId); if (!wallets || wallets.length === 0) return true; if (!ELECTRUM_CLIENT.isClientConnected) { @@ -708,6 +789,7 @@ function* refreshWalletsWorker({ } yield fork(bulkUpdateLabelsWorker, { + originAppId: sourceAppId, payload: { labelChanges, UTXO: utxo, wallet: synchedWallet as any }, }); @@ -869,6 +951,7 @@ function* refreshWalletsWorker({ } // Write updated specs (with spendability) back to Realm + assertWalletAccount(sourceAppId); if (synchedWallet.entityKind === EntityKind.VAULT) { yield call(dbManager.updateObjectById, RealmSchema.Vault, synchedWallet.id, { specs: synchedWallet.specs, @@ -909,7 +992,9 @@ export const refreshWalletsWatcher = createWatcher(refreshWalletsWorker, REFRESH export function* autoWalletsSyncWorker({ payload, + originAppId, }: { + originAppId?: string; payload: { syncAll?: boolean; hardRefresh?: boolean; @@ -917,6 +1002,8 @@ export function* autoWalletsSyncWorker({ backupCheckAppId?: string; }; }) { + const sourceAppId = originAppId || currentWalletAccountId(); + assertWalletAccount(sourceAppId); const { syncAll, hardRefresh, addNotifications, backupCheckAppId } = payload; const wallets: Wallet[] = yield call(dbManager.getObjectByIndex, RealmSchema.Wallet, null, true); const vaults: Vault[] = yield call(dbManager.getObjectByIndex, RealmSchema.Vault, null, true); @@ -934,6 +1021,7 @@ export function* autoWalletsSyncWorker({ let refreshSucceeded = true; if (walletsToSync.length) { refreshSucceeded = yield call(refreshWalletsWorker, { + originAppId: sourceAppId, payload: { wallets: walletsToSync, options: { @@ -945,16 +1033,25 @@ export function* autoWalletsSyncWorker({ } // Check backup freshness only after the refreshed wallet specs are persisted. // A failed refresh or account switch must not verify a stale snapshot. - if (backupCheckAppId && refreshSucceeded) { + if ( + backupCheckAppId === sourceAppId && + refreshSucceeded && + currentWalletAccountId() === sourceAppId && + store.getState().storage.appId === sourceAppId + ) { const { appId } = yield select((state: RootState) => state.storage); - const { automaticCloudBackup, pendingAllBackup, backupRepairCompletedByAppId = {} } = + const { + automaticCloudBackupByAppId, + pendingAllBackupByAppId, + backupRepairCompletedByAppId = {}, + } = yield select((state: RootState) => state.bhr); if ( appId === backupCheckAppId && - automaticCloudBackup && - (pendingAllBackup || !backupRepairCompletedByAppId[appId]) + isAutomaticCloudBackupEnabled({ automaticCloudBackupByAppId }, appId) && + (pendingAllBackupByAppId?.[appId] || !backupRepairCompletedByAppId[appId]) ) { - yield put(checkBackupFreshness()); + yield put(checkBackupFreshness(appId)); } } } @@ -963,6 +1060,7 @@ export const autoWalletsSyncWatcher = createWatcher(autoWalletsSyncWorker, AUTO_ export function* updateSignerPolicyWorker({ payload, + originAppId, }: { payload: { signer: Signer; @@ -973,12 +1071,14 @@ export function* updateSignerPolicyWorker({ }; verificationToken: number; }; + originAppId?: string; }) { - const fcmToken = yield select((state: RootState) => state.notifications.fcmToken); - const { bitcoinNetwork } = yield select((state: RootState) => state.settings); - + const sourceAppId = originAppId || currentWalletAccountId(); const { signer, signingKey, updates, verificationToken } = payload; try { + assertWalletAccount(sourceAppId); + const fcmToken = yield select((state: RootState) => state.notifications.fcmToken); + const { bitcoinNetwork } = yield select((state: RootState) => state.settings); const signerId = signingKey?.xfp || WalletUtilities.getFingerprintFromExtendedKey( @@ -996,6 +1096,7 @@ export function* updateSignerPolicyWorker({ updates, fcmToken ); + assertWalletAccount(sourceAppId); if (delayedPolicyUpdate) { yield put(updateDelayedPolicyUpdate(delayedPolicyUpdate)); @@ -1018,9 +1119,11 @@ export function* updateSignerPolicyWorker({ } ); } + assertWalletAccount(sourceAppId); yield put(setSignerPolicyError('success')); } catch (err) { - yield put(setSignerPolicyError('failure')); + if (currentWalletAccountId() === sourceAppId && store.getState().storage.appId === sourceAppId) + yield put(setSignerPolicyError('failure')); } } @@ -1054,7 +1157,8 @@ function* testcoinsWorker({ payload }) { export const testcoinsWatcher = createWatcher(testcoinsWorker, TEST_SATS_RECIEVE); -function* updateWalletDetailsWorker({ payload }) { +function* updateWalletDetailsWorker({ payload, originAppId }) { + const sourceAppId = originAppId || currentWalletAccountId(); const { wallet, details, @@ -1066,6 +1170,7 @@ function* updateWalletDetailsWorker({ payload }) { }; } = payload; try { + assertWalletAccount(sourceAppId); const presentationData: WalletPresentationData = { name: details.name, description: details.description, @@ -1074,8 +1179,12 @@ function* updateWalletDetailsWorker({ payload }) { wallet.presentationData = presentationData; yield put(setRelayWalletUpdateLoading(true)); - const response = yield call(updateAppImageWorker, { payload: { wallets: [wallet] } }); + const response = yield call(updateAppImageWorker, { + payload: { wallets: [wallet] }, + originAppId: sourceAppId, + }); if (response.updated) { + assertWalletAccount(sourceAppId); yield call(dbManager.updateObjectById, RealmSchema.Wallet, wallet.id, { presentationData, }); @@ -1096,7 +1205,8 @@ export const updateWalletDetailWatcher = createWatcher( UPDATE_WALLET_DETAILS ); -function* updateVaultDetailsWorker({ payload }) { +function* updateVaultDetailsWorker({ payload, originAppId }) { + const sourceAppId = originAppId || currentWalletAccountId(); const { vault, details, @@ -1108,6 +1218,7 @@ function* updateVaultDetailsWorker({ payload }) { }; } = payload; try { + assertWalletAccount(sourceAppId); const presentationData: VaultPresentationData = { name: details.name, description: details.description, @@ -1119,8 +1230,10 @@ function* updateVaultDetailsWorker({ payload }) { const response = yield call(updateVaultImageWorker, { payload: { vault }, + originAppId: sourceAppId, }); if (response.updated) { + assertWalletAccount(sourceAppId); yield call(dbManager.updateObjectById, RealmSchema.Vault, vault.id, { presentationData, }); @@ -1142,7 +1255,8 @@ export const updateVaultDetailsWatcher = createWatcher( UPDATE_VAULT_DETAILS ); -export function* updateSignerDetailsWorker({ payload }) { +export function* updateSignerDetailsWorker({ payload, originAppId }) { + const sourceAppId = originAppId || currentWalletAccountId(); const { signer, key, @@ -1154,9 +1268,14 @@ export function* updateSignerDetailsWorker({ payload }) { } = payload; yield put(setRelaySignersUpdateLoading(true)); try { + assertWalletAccount(sourceAppId); signer[key] = value; - const response = yield call(updateAppImageWorker, { payload: { signers: [signer] } }); + const response = yield call(updateAppImageWorker, { + payload: { signers: [signer] }, + originAppId: sourceAppId, + }); if (response.updated) { + assertWalletAccount(sourceAppId); const signerKeyUID = getKeyUID(signer); yield call( dbManager.updateObjectByQuery, @@ -1181,7 +1300,9 @@ export function* updateSignerDetailsWorker({ payload }) { export const updateSignerDetails = createWatcher(updateSignerDetailsWorker, UPDATE_SIGNER_DETAILS); -function* updateKeyDetailsWorker({ payload }) { +function* updateKeyDetailsWorker({ payload, originAppId }) { + const sourceAppId = originAppId || currentWalletAccountId(); + assertWalletAccount(sourceAppId); const { signer, key, @@ -1207,13 +1328,15 @@ function* updateKeyDetailsWorker({ payload }) { if (!updatedFlag) { updatedRegsteredVaults.push(value); } - const saved = yield call( - dbManager.updateObjectByPrimaryId, - RealmSchema.VaultSigner, - 'xpub', - signer.xpub, - { registeredVaults: updatedRegsteredVaults } - ); + const saved = yield call(() => { + assertWalletAccount(sourceAppId); + return dbManager.updateObjectByPrimaryId( + RealmSchema.VaultSigner, + 'xpub', + signer.xpub, + { registeredVaults: updatedRegsteredVaults } + ); + }); if (saved !== true) { yield put(relaySignersUpdateFail('Key registration could not be saved')); return false; @@ -1226,37 +1349,49 @@ function* updateKeyDetailsWorker({ payload }) { if (vault.signers.some((key) => key.xpub === signer.xpub)) { const response = yield call(updateVaultImageWorker, { payload: { vault, isUpdate: true }, + originAppId: sourceAppId, }); - if (!response?.updated) yield call(setServerBackupFailed); + if (!response?.updated) yield call(setServerBackupFailed, sourceAppId); } } } catch (error) { - yield call(setServerBackupFailed); + yield call(setServerBackupFailed, sourceAppId); } return true; } - const updated = yield call( - dbManager.updateObjectByPrimaryId, - RealmSchema.VaultSigner, - 'xpub', - signer.xpub, - { + const updated = yield call(() => { + assertWalletAccount(sourceAppId); + return dbManager.updateObjectByPrimaryId(RealmSchema.VaultSigner, 'xpub', signer.xpub, { [key]: value, - } - ); + }); + }); return updated === true; } export const updateKeyDetails = createWatcher(updateKeyDetailsWorker, UPDATE_KEY_DETAILS); -function* deleteVaultWorker({ payload }) { +function* deleteVaultWorker({ payload, originAppId }) { + const sourceAppId = originAppId || currentWalletAccountId(); const { vaultId } = payload; try { + assertWalletAccount(sourceAppId); yield put(setRelayVaultUpdateLoading(true)); - const response = yield call(deleteVaultImageWorker, { payload: { vaultIds: [vaultId] } }); + const backupEnabled = yield select((state: RootState) => + isAutomaticCloudBackupEnabled(state.bhr, sourceAppId) + ); + const response = yield call(deleteVaultImageWorker, { + payload: { vaultIds: [vaultId] }, + originAppId: sourceAppId, + }); if (response.updated) { - yield call(dbManager.deleteObjectById, RealmSchema.Vault, vaultId); + assertWalletAccount(sourceAppId); + const deleted = yield call(dbManager.deleteObjectById, RealmSchema.Vault, vaultId); + if (deleted !== true) { + if (backupEnabled) yield call(setServerBackupFailed, sourceAppId); + throw new Error('Local vault deletion failed'); + } + assertWalletAccount(sourceAppId); yield put(relayVaultUpdateSuccess()); } else { const errorMsg = response.error?.message @@ -1265,15 +1400,18 @@ function* deleteVaultWorker({ payload }) { yield put(relayVaultUpdateFail(errorMsg)); } } catch (err) { - yield put(relayVaultUpdateFail('Something went wrong while deleting the vault!')); + if (currentWalletAccountId() === sourceAppId && store.getState().storage.appId === sourceAppId) + yield put(relayVaultUpdateFail('Something went wrong while deleting the vault!')); } } export const deleteVaultyWatcher = createWatcher(deleteVaultWorker, DELETE_VAULT); -function* reinstateVaultWorker({ payload }) { +function* reinstateVaultWorker({ payload, originAppId }) { + const sourceAppId = originAppId || currentWalletAccountId(); const { vaultId } = payload; try { + assertWalletAccount(sourceAppId); yield put(setRelayVaultUpdateLoading(true)); const vault: Vault = dbManager.getObjectById(RealmSchema.Vault, vaultId).toJSON(); const updatedParams = { @@ -1286,6 +1424,7 @@ function* reinstateVaultWorker({ payload }) { }, }; const response = yield call(updateVaultImageWorker, { + originAppId: sourceAppId, payload: { vault: { ...vault, @@ -1299,9 +1438,11 @@ function* reinstateVaultWorker({ payload }) { signingDevices.forEach((signer) => (signerMap[getKeyUID(signer)] = signer)); if (response.updated) { + assertWalletAccount(sourceAppId); yield call(dbManager.updateObjectById, RealmSchema.Vault, vaultId, updatedParams); for (let i = 0; i < vault.signers.length; i++) { yield call(updateSignerDetailsWorker, { + originAppId: sourceAppId, payload: { signer: signerMap[getKeyUID(vault.signers[i])], key: 'archived', @@ -1323,9 +1464,11 @@ function* reinstateVaultWorker({ payload }) { export const reinstateVaultWatcher = createWatcher(reinstateVaultWorker, REINSTATE_VAULT); -function* refillMobileKeyWorker({ payload }) { +function* refillMobileKeyWorker({ payload, originAppId }) { + const sourceAppId = originAppId || currentWalletAccountId(); const { vaultKey } = payload; try { + assertWalletAccount(sourceAppId); const { xpriv } = vaultKey; if (!xpriv) { const signer: Signer = dbManager @@ -1343,6 +1486,7 @@ function* refillMobileKeyWorker({ payload }) { if (xpub === signerXpub) { const { xpriv } = details.xpubDetails[XpubTypes.P2WSH]; yield call(updateKeyDetailsWorker, { + originAppId: sourceAppId, payload: { signer: signer.signerXpubs[XpubTypes.P2WSH][0], key: 'xpriv', value: xpriv }, }); } @@ -1354,12 +1498,15 @@ function* refillMobileKeyWorker({ payload }) { export const refillMobileKeyWatcher = createWatcher(refillMobileKeyWorker, REFILL_MOBILEKEY); -function* refreshCanaryWalletsWorker() { +function* refreshCanaryWalletsWorker({ originAppId }: { originAppId?: string } = {}) { try { + const sourceAppId = originAppId || currentWalletAccountId(); + assertWalletAccount(sourceAppId); const vaults: Vault[] = yield call(dbManager.getCollection, RealmSchema.Vault); const canaryWallets = vaults.filter((vault) => vault.type === VaultType.CANARY); if (canaryWallets.length) { yield call(refreshWalletsWorker, { + originAppId: sourceAppId, payload: { wallets: canaryWallets, options: { @@ -1378,8 +1525,16 @@ export const refreshCanaryWalletsWatcher = createWatcher( REFRESH_CANARY_VAULT ); -function* mergeSimilarKeysWorker({ payload }: { payload: { signer: Signer } }) { +function* mergeSimilarKeysWorker({ + payload, + originAppId, +}: { + payload: { signer: Signer }; + originAppId?: string; +}) { try { + const sourceAppId = originAppId || currentWalletAccountId(); + assertWalletAccount(sourceAppId); const { signer } = payload; const signers: Signer[] = yield call(dbManager.getCollection, RealmSchema.Signer); for (let i = 0; i < signers.length; i++) { @@ -1394,14 +1549,14 @@ function* mergeSimilarKeysWorker({ payload }: { payload: { signer: Signer } }) { s.masterFingerprint !== signer.masterFingerprint ) { const signerKeyUID = getKeyUID(s); - yield call( - dbManager.updateObjectByQuery, - RealmSchema.Signer, - (realmSigner) => getKeyUID(realmSigner) === signerKeyUID, - { - masterFingerprint: signer.masterFingerprint, - } - ); + yield call(() => { + assertWalletAccount(sourceAppId); + return dbManager.updateObjectByQuery( + RealmSchema.Signer, + (realmSigner) => getKeyUID(realmSigner) === signerKeyUID, + { masterFingerprint: signer.masterFingerprint } + ); + }); // get all keys that have the same masterFingerprint const keys = yield call( dbManager.getObjectByQuery, @@ -1410,6 +1565,7 @@ function* mergeSimilarKeysWorker({ payload }: { payload: { signer: Signer } }) { true // get all matching objects ); for (let i = 0; i < keys.length; i++) { + assertWalletAccount(sourceAppId); yield call( dbManager.updateObjectByPrimaryId, RealmSchema.VaultSigner, @@ -1424,6 +1580,8 @@ function* mergeSimilarKeysWorker({ payload }: { payload: { signer: Signer } }) { dbManager.getObjectByIndex, RealmSchema.KeeperApp ); + assertWalletAccount(sourceAppId); + if (id !== sourceAppId) throw new Error('Account changed'); const encryptionKey = generateEncryptionKey(primarySeed); const encrytedSigner = encrypt( encryptionKey, @@ -1454,23 +1612,30 @@ export const mergeSimilarKeysWatcher = createWatcher(mergeSimilarKeysWorker, MER function* generateNewExternalAddressWorker({ payload, + originAppId, }: { payload: { wallet: Wallet | Vault; }; + originAppId?: string; }) { + const sourceAppId = originAppId || currentWalletAccountId(); + if ( + !sourceAppId || + currentWalletAccountId() !== sourceAppId || + store.getState().storage.appId !== sourceAppId + ) return false; const { wallet } = payload; - wallet.specs.totalExternalAddresses += 1; - - if (wallet.entityKind === EntityKind.VAULT) { - yield call(dbManager.updateObjectById, RealmSchema.Vault, wallet.id, { - specs: wallet.specs, - }); - } else { - yield call(dbManager.updateObjectById, RealmSchema.Wallet, wallet.id, { - specs: wallet.specs, - }); - } + const specs = { ...wallet.specs, totalExternalAddresses: wallet.specs.totalExternalAddresses + 1 }; + + return yield call(() => { + assertWalletAccount(sourceAppId); + return dbManager.updateObjectById( + wallet.entityKind === EntityKind.VAULT ? RealmSchema.Vault : RealmSchema.Wallet, + wallet.id, + { specs } + ); + }); } export const generateNewExternalAddressWatcher = createWatcher( @@ -1550,7 +1715,9 @@ export const fetchCollaborativeChannelWatcher = createWatcher( FETCH_COLLABORATIVE_CHANNEL ); -function* updateVaultSignerXprivWorker({ signers }) { +function* updateVaultSignerXprivWorker({ signers, originAppId }) { + const sourceAppId = originAppId || currentWalletAccountId(); + assertWalletAccount(sourceAppId); const { bitcoinNetworkType } = yield select((state: RootState) => state.settings); for (let i = 0; i < signers.length; i++) { const signer = signers[i]; @@ -1583,12 +1750,14 @@ function* updateVaultSignerXprivWorker({ signers }) { ? tXpriv : null; yield call( - dbManager.updateObjectByPrimaryId, - RealmSchema.VaultSigner, - 'xpub', - vaultSigner.xpub, - { - xpriv: uptXpriv, + () => { + assertWalletAccount(sourceAppId); + return dbManager.updateObjectByPrimaryId( + RealmSchema.VaultSigner, + 'xpub', + vaultSigner.xpub, + { xpriv: uptXpriv } + ); } ); } diff --git a/src/store/store.ts b/src/store/store.ts index bf33c248fc..9a94620c5d 100644 --- a/src/store/store.ts +++ b/src/store/store.ts @@ -25,6 +25,11 @@ import helpAiReducer from './reducers/helpAi'; import { RESET_REDUX_STORE } from './sagaActions/upgrade'; import reduxPersistMigrations from './migrations'; +import dbManager from 'src/storage/realm/dbManager'; +import { RealmSchema } from 'src/storage/realm/enum'; +import { setBackupUploadGuard } from 'src/services/backup/transport'; +import { isAutomaticCloudBackupEnabled } from './reducers/bhr'; +import { KeeperApp } from 'src/models/interfaces/KeeperApp'; const appReducer = combineReducers({ settings: settingsReducer, @@ -70,7 +75,66 @@ const persistConfig = { const persistedReducer = persistReducer(persistConfig, rootReducer); const sagaMiddleware = createSagaMiddleware(); -const middlewars = [sagaMiddleware]; +// Keep the account present at dispatch time even if a saga runs after a switch. +const accountOwnedActions = new Set([ + 'UPDATE_APP_IMAGE', + 'UPDATE_VAULT_IMAGE', + 'DELETE_APP_IMAGE_ENTITY', + 'ADD_NEW_WALLETS', + 'ADD_NEW_VAULT', + 'ADD_SIGINING_DEVICE', + 'DELETE_SIGINING_DEVICE', + 'ARCHIVE_SIGINING_DEVICE', + 'MIGRATE_VAULT', + 'DELETE_VAULT', + 'REINSTATE_VAULT', + 'REFILL_MOBILEKEY', + 'MERGER_SIMILAR_KEYS', + 'REFRESH_WALLETS', + 'AUTO_SYNC_WALLETS', + 'REFRESH_CANARY_VAULT', + 'UPDATE_WALLET_DETAILS', + 'UPDATE_VAULT_DETAILS', + 'UPDATE_SIGNER_DETAILS', + 'UPDATE_KEY_DETAILS', + 'UPDATED_VAULT_SIGNERS_XPRIV', + 'ADD_LABELS', + 'BULK_UPDATE_LABELS', + 'IMPORT_LABELS', + 'MARK_UTXO_SPENDABILITY', + 'SEND_PHASE_TWO', + 'SEND_PHASE_THREE', + 'DISCARD_BROADCASTED_TNX', + 'GENERATE_NEW_ADDRESS', + 'UPDATE_SIGNER_POLICY', +]); +const accountOwnedUtxoUiActions = new Set([ + 'utxos/setSyncingUTXOs', + 'utxos/setSyncingUTXOError', + 'utxos/resetState', +]); +const bindActionOrigin = (api) => (next) => (action) => { + const originAppId = api.getState()?.storage?.appId; + if (action?.originAppId && accountOwnedUtxoUiActions.has(action.type)) { + try { + if ( + action.originAppId !== originAppId || + (dbManager.getObjectByIndex(RealmSchema.KeeperApp) as unknown as KeeperApp)?.id !== originAppId + ) return action; + } catch { + return action; + } + } + if ( + originAppId && + action && + accountOwnedActions.has(action.type) && + !('originAppId' in action) + ) + return next({ ...action, originAppId }); + return next(action); +}; +const middlewars = [bindActionOrigin, sagaMiddleware]; if (__DEV__) { // const createDebugger = require('redux-flipper').default; @@ -85,5 +149,19 @@ export const store = configureStore({ }).concat(middlewars), }); +setBackupUploadGuard((appId, explicitChoice) => { + const state = store.getState(); + if ( + state.storage.appId !== appId || + (!explicitChoice && !isAutomaticCloudBackupEnabled(state.bhr, appId)) + ) + return false; + try { + return (dbManager.getObjectByIndex(RealmSchema.KeeperApp) as unknown as KeeperApp)?.id === appId; + } catch { + return false; + } +}); + sagaMiddleware.run(rootSaga); export const persistor = persistStore(store); diff --git a/tests/recovery/accountBinding.test.cjs b/tests/recovery/accountBinding.test.cjs new file mode 100644 index 0000000000..b17dd2c3f2 --- /dev/null +++ b/tests/recovery/accountBinding.test.cjs @@ -0,0 +1,256 @@ +const { test } = require('node:test'); +const assert = require('node:assert/strict'); +const { fixture, wallet, signer, enums, loadModule } = require('./sagaHarness.cjs'); +const { harness } = require('./helpers.cjs'); + +function switchToOptedInB(f) { + f.app.id = 'B'; + f.app.primarySeed = 'cd'.repeat(32); + f.state.storage.appId = 'B'; + f.state.bhr.automaticCloudBackupByAppId.B = true; +} + +test('A wallet and vault actions queued before B login never use B key or destination', async () => { + const f = fixture(); + const originAppId = f.app.id; + const aWallet = wallet('A-wallet', enums.NetworkType.MAINNET); + switchToOptedInB(f); + const walletResult = await f.run('updateAppImageWorker', { + originAppId, + payload: { wallets: [aWallet], updateNodes: true }, + }); + const vaultResult = await f.run('updateVaultImageWorker', { + originAppId, + payload: { vault: { id: 'A-vault', signers: [], archived: false } }, + }); + const createResult = await f.run('addNewWalletsWorker', { + originAppId, + payload: [{ walletType: enums.WalletType.DEFAULT, walletDetails: { fixture: aWallet } }], + }); + assert.equal(walletResult.updated, false); + assert.equal(vaultResult.updated, false); + assert.equal(createResult, false); + assert.equal(f.calls.length, 0, 'B must receive no A ciphertext or appId'); + assert.equal(f.collections.Wallet.length, 0, 'A wallet must not be persisted in B Realm'); +}); + +test('selected B blocks stale A writes even before Realm finishes switching', async () => { + const f = fixture(); + const originAppId = f.app.id; + const aWallet = wallet('same-wallet', enums.NetworkType.MAINNET); + f.collections.Wallet.push({ ...aWallet, specs: { totalExternalAddresses: 4 } }); + f.state.storage.appId = 'B'; + f.state.bhr.automaticCloudBackupByAppId.B = true; + const createResult = await f.run('addNewWalletsWorker', { + originAppId, + payload: [{ walletType: enums.WalletType.DEFAULT, walletDetails: { fixture: aWallet } }], + }); + const backupResult = await f.run('updateAppImageWorker', { + originAppId, + payload: { wallets: [aWallet] }, + }); + const addressResult = await f.run('generateNewExternalAddressWorker', { + originAppId, + payload: { wallet: { ...aWallet, specs: { totalExternalAddresses: 7 } } }, + }); + assert.equal(createResult, false); + assert.equal(backupResult.updated, false); + assert.equal(addressResult, false); + assert.equal(f.calls.length, 0); + assert.equal(f.collections.Wallet.length, 1); + assert.equal(f.collections.Wallet[0].specs.totalExternalAddresses, 4); +}); + +test('A to opted-in B during connectivity wait blocks upload and preserves B local wallet', async () => { + const f = fixture(); + const originAppId = f.app.id; + const sharedId = 'same-id-in-both-accounts'; + f.collections.Wallet.push(wallet(sharedId, enums.NetworkType.MAINNET)); + let resumeConnectivity; + f.scope.NetInfo.fetch = () => new Promise((resolve) => { resumeConnectivity = resolve; }); + const deleting = f.run('deleteAppImageEntityWorker', { + originAppId, + payload: { walletIds: [sharedId] }, + }); + for (let i = 0; i < 20 && !resumeConnectivity; i++) await new Promise(setImmediate); + assert.ok(resumeConnectivity, 'test must switch while the original account is waiting'); + switchToOptedInB(f); + resumeConnectivity({ isConnected: true }); + const result = await deleting; + assert.equal(result.updated, false); + assert.equal(f.calls.length, 0, 'no remote delete may target either account'); + assert.deepEqual(f.collections.Wallet.map(({ id }) => id), [sharedId]); + + let resumeSecondCheck; + f.scope.NetInfo.fetch = () => new Promise((resolve) => { resumeSecondCheck = resolve; }); + f.app.id = originAppId; + f.app.primarySeed = 'ab'.repeat(32); + f.state.storage.appId = originAppId; + const updating = f.run('updateAppImageWorker', { + originAppId, + payload: { wallets: [wallet('A-wallet', enums.NetworkType.MAINNET)], updateNodes: true }, + }); + for (let i = 0; i < 20 && !resumeSecondCheck; i++) await new Promise(setImmediate); + assert.ok(resumeSecondCheck); + switchToOptedInB(f); + resumeSecondCheck({ isConnected: true }); + assert.equal((await updating).updated, false); + assert.equal(f.calls.length, 0, 'A payload must never be encrypted with B seed or sent to B'); +}); + +test('queued A transport cannot execute after opted-in B becomes active', async () => { + const calls = []; + const transport = loadModule('src/services/backup/transport.ts', { + '../rest/RestClient': { post: async (_path, body) => { calls.push(body); return { data: { updated: true } }; } }, + }); + let active = 'A'; + const consent = { A: true, B: true }; + transport.setBackupUploadGuard((id, explicitChoice) => id === active && (consent[id] || explicitChoice)); + let release; + const busy = transport.withBackupSession('A', () => new Promise((resolve) => { release = resolve; })); + for (let i = 0; i < 20 && !release; i++) await new Promise(setImmediate); + assert.ok(release); + const pending = transport.backupPost('fixture/updateAppImage', { appId: 'A', walletsObject: { secret: 'A-ciphertext' } }); + active = 'B'; + release(); + await busy; + await assert.rejects(pending, /not active or opted in/); + assert.deepEqual(calls, [], 'the queued request must never reach the network'); +}); + +test('per-account consent starts off, survives switches, and legacy consent needs attribution', () => { + const reducer = loadModule('src/store/reducers/bhr.ts', { + 'src/models/enums/BHR': loadModule('src/models/enums/BHR.ts'), + 'src/storage': { reduxStorage: {} }, + 'redux-persist': { persistReducer: (_settings, fn) => fn }, + }); + let state = reducer.default(undefined, { type: 'init' }); + assert.equal(reducer.isAutomaticCloudBackupEnabled(state, 'A'), false); + assert.equal(reducer.isAutomaticCloudBackupEnabled(state, 'B'), false); + state = reducer.default(state, reducer.setAutomaticCloudBackup({ appId: 'A', enabled: true })); + assert.equal(reducer.isAutomaticCloudBackupEnabled(state, 'A'), true); + assert.equal(reducer.isAutomaticCloudBackupEnabled(state, 'B'), false); + state = reducer.default(state, reducer.setBackupAllSuccess({ appId: 'A', status: true })); + state = reducer.default(state, reducer.setAutomaticCloudBackup({ appId: 'A', enabled: false })); + assert.equal(state.backupAllSuccessByAppId.A, false, 'stale UI success cannot reenable A'); + assert.equal(reducer.isAutomaticCloudBackupEnabled(state, 'A'), false); + const legacy = { ...state, automaticCloudBackup: true, automaticCloudBackupByAppId: {} }; + const ambiguous = reducer.default(legacy, reducer.migrateLegacyAutomaticCloudBackup({ appId: 'A', canAttributeConsent: false })); + assert.equal(reducer.isAutomaticCloudBackupEnabled(ambiguous, 'A'), false); + assert.equal(ambiguous.automaticCloudBackup, false); + const attributable = reducer.default(legacy, reducer.migrateLegacyAutomaticCloudBackup({ appId: 'A', canAttributeConsent: true })); + assert.equal(reducer.isAutomaticCloudBackupEnabled(attributable, 'A'), true); + assert.equal(reducer.isAutomaticCloudBackupEnabled(attributable, 'B'), false); +}); + +test('explicit backup can repair an off account only while that account stays active', async () => { + const f = harness(); + f.local.Wallet.push(wallet('explicit', enums.NetworkType.MAINNET)); + f.transport.setBackupUploadGuard((id, explicitChoice) => id === f.app.id && explicitChoice); + assert.equal(await f.repair.inspectBackup(f.app.id, true, () => {}, true), 'verified'); + assert.deepEqual(f.calls, ['getBackupSnapshot', 'repairAppBackup', 'getBackupSnapshot']); +}); + +test('unconfirmed server deletion retains account consent and reports failure', async () => { + const f = fixture(); + f.scope.Relay.deleteBackup = async () => ({ updated: false }); + assert.equal(await f.run('deleteBackupWorker', { appId: f.app.id }), false); + assert.equal(f.state.bhr.automaticCloudBackupByAppId[f.app.id], true); + assert.ok(f.actions.some((action) => action.type === 'setDeleteBackupFailure' && + action.payload.appId === f.app.id && action.payload.status === true)); + assert.ok(!f.actions.some((action) => action.type === 'setAutomaticCloudBackup' && + action.payload.enabled === false)); +}); + +test('rejected entity deletion retains local wallet and queues an account repair', async () => { + const f = fixture(); + const id = f.app.id; + f.collections.Wallet.push(wallet('keep', enums.NetworkType.MAINNET)); + f.scope.Relay.deleteAppImageEntity = async () => ({ updated: false, error: 'rejected' }); + const result = await f.run('deleteAppImageEntityWorker', { + originAppId: id, + payload: { walletIds: ['keep'] }, + }); + assert.equal(result.updated, false); + assert.deepEqual(f.collections.Wallet.map((w) => w.id), ['keep']); + assert.equal(f.state.bhr.pendingAllBackupByAppId[id], true); +}); + +test('remote entity deletion followed by local failure reports partial deletion', async () => { + const f = fixture(); + const id = f.app.id; + f.collections.Wallet.push(wallet('keep', enums.NetworkType.MAINNET)); + f.scope.dbManager.deleteObjectById = () => false; + const result = await f.run('deleteAppImageEntityWorker', { + originAppId: id, + payload: { walletIds: ['keep'] }, + }); + assert.equal(result.updated, false); + assert.deepEqual(f.collections.Wallet.map((w) => w.id), ['keep']); + assert.equal(f.state.bhr.pendingAllBackupByAppId[id], true); +}); + +test('signer delete reports failure and never shows success after rejected remote delete', async () => { + const f = fixture(); + const key = signer('AABBCCDD', enums.NetworkType.MAINNET); + f.scope.deleteAppImageEntityWorker = function* () { return { updated: false, error: 'rejected' }; }; + const result = await f.run('deleteSigningDeviceWorker', { + originAppId: f.app.id, + payload: { signers: [key] }, + }); + assert.equal(result, false); + assert.ok(f.actions.some((action) => action.type === 'relaySignersUpdateFail')); + assert.ok(!f.actions.some((action) => action.type === 'showKeyDeletedSuccessModal')); +}); + +test('remote vault deletion followed by failed local delete reports failure and pending repair', async () => { + const f = fixture(); + f.scope.deleteVaultImageWorker = function* () { return { updated: true }; }; + f.scope.dbManager.deleteObjectById = () => false; + await f.run('deleteVaultWorker', { originAppId: f.app.id, payload: { vaultId: 'keep-vault' } }); + assert.ok(f.actions.some((action) => action.type === 'relayVaultUpdateFail')); + assert.ok(!f.actions.some((action) => action.type === 'relayVaultUpdateSuccess')); + assert.equal(f.state.bhr.pendingAllBackupByAppId[f.app.id], true); +}); + +test('queued A address action cannot change B wallet with the same ID', async () => { + const f = fixture(); + const originAppId = f.app.id; + const aWallet = { ...wallet('same-wallet', enums.NetworkType.MAINNET), + specs: { totalExternalAddresses: 7 } }; + f.collections.Wallet.push({ ...wallet('same-wallet', enums.NetworkType.MAINNET), + specs: { totalExternalAddresses: 4 } }); + switchToOptedInB(f); + assert.equal(await f.run('generateNewExternalAddressWorker', { + originAppId, payload: { wallet: aWallet }, + }), false); + assert.equal(f.collections.Wallet[0].specs.totalExternalAddresses, 4); + assert.equal(aWallet.specs.totalExternalAddresses, 7); +}); + +test('A policy response after B login cannot change B signer or policy UI state', async () => { + const f = fixture(); + const originAppId = f.app.id; + let completePolicy; + f.scope.SigningServer.updatePolicy = () => new Promise((resolve) => { completePolicy = resolve; }); + let writes = 0; + f.scope.dbManager.updateObjectByPrimaryId = () => { writes++; return true; }; + const policy = f.run('updateSignerPolicyWorker', { + originAppId, + payload: { + signer: { masterFingerprint: 'AABBCCDD', signerPolicy: {} }, + signingKey: { xfp: 'AABBCCDD' }, + updates: { restrictions: {}, signingDelay: 0 }, + verificationToken: 123456, + }, + }); + for (let i = 0; i < 20 && !completePolicy; i++) await new Promise(setImmediate); + assert.ok(completePolicy); + switchToOptedInB(f); + completePolicy({ updated: true, delayedPolicyUpdate: { id: 'A-policy' } }); + await policy; + assert.equal(writes, 0); + assert.ok(!f.actions.some((action) => + action.type === 'updateDelayedPolicyUpdate' || + (action.type === 'setSignerPolicyError' && action.payload === 'success'))); +}); diff --git a/tests/recovery/backupFeedback.test.cjs b/tests/recovery/backupFeedback.test.cjs index c94c89b29d..3deccd825e 100644 --- a/tests/recovery/backupFeedback.test.cjs +++ b/tests/recovery/backupFeedback.test.cjs @@ -39,7 +39,12 @@ for (const file of ['src/components/Backup/BackupHealthCheckList.tsx', 'src/scre if (!callback && ts.isCallExpression(node) && node.expression.getText(source)==='useEffect' && node.arguments[0]?.getText(source).includes('backupAllSuccess')) callback=node.arguments[0].getText(source); ts.forEachChild(node,visit); } - visit(source); assert.ok(callback); + visit(source); + if (!callback) { + assert.ok(!source.getFullText().includes('backupAllSignersAndVaults('), + 'Recovery Key confirmation must not start Assisted Server Backup'); + return; + } const actions=[], ui=[]; const context={ backupAllSuccess:false,backupAllFailure:true,automaticCloudBackup:false, dispatch:a=>actions.push(a),setBackupAllSuccess:v=>({type:'success',value:v}),setBackupAllFailure:v=>({type:'failure',value:v}),setAutomaticCloudBackup:v=>({type:'enabled',value:v}), diff --git a/tests/recovery/backupFreshness.test.cjs b/tests/recovery/backupFreshness.test.cjs index e03ad47b90..90b856f479 100644 --- a/tests/recovery/backupFreshness.test.cjs +++ b/tests/recovery/backupFreshness.test.cjs @@ -1,12 +1,58 @@ const { test } = require('node:test'); const assert = require('node:assert/strict'); -const { fixture, enums, encryption, wallet, loadModule } = require('./sagaHarness.cjs'); +const { fixture, enums, encryption, wallet, loadModule, functions } = require('./sagaHarness.cjs'); +const vm = require('node:vm'); +const ts = require('typescript'); const { NetworkType, WalletType } = enums; +test('login freshness runs after successful sync only for the opted-in source account', async () => { + const scenarios = [ + { enabled: true, pending: true, success: true, switched: false, expected: true }, + { enabled: false, pending: true, success: true, switched: false, expected: false }, + { enabled: true, pending: false, verified: true, success: true, switched: false, expected: false }, + { enabled: true, pending: true, success: false, switched: false, expected: false }, + { enabled: true, pending: true, success: true, switched: true, expected: false }, + ]; + for (const scenario of scenarios) { + const f = fixture(); + const sourceAppId = f.app.id; + f.collections.Wallet.push(wallet('after-sync', NetworkType.MAINNET)); + f.state.bhr.automaticCloudBackupByAppId[sourceAppId] = scenario.enabled; + f.state.bhr.pendingAllBackupByAppId[sourceAppId] = scenario.pending; + f.state.bhr.backupRepairCompletedByAppId[sourceAppId] = !!scenario.verified; + let synced = false; + f.scope.refreshWalletsWorker = function* () { + synced = true; + if (scenario.switched) { + f.app.id = 'B'; + f.state.storage.appId = 'B'; + f.state.bhr.automaticCloudBackupByAppId.B = true; + } + return scenario.success; + }; + f.scope.checkBackupFreshness = (id) => { + assert.equal(synced, true, 'freshness cannot start before sync completes'); + return { type: 'checkBackupFreshness', appId: id }; + }; + vm.runInNewContext(ts.transpileModule( + functions('src/store/sagas/wallets.ts', ['autoWalletsSyncWorker']), + { compilerOptions: { target: ts.ScriptTarget.ES2020 } } + ).outputText, f.scope); + await f.run('autoWalletsSyncWorker', { + originAppId: sourceAppId, + payload: { syncAll: true, backupCheckAppId: sourceAppId }, + }); + const freshness = f.actions.filter((action) => action.type === 'checkBackupFreshness'); + assert.equal(freshness.length, scenario.expected ? 1 : 0, JSON.stringify(scenario)); + if (scenario.expected) assert.equal(freshness[0].appId, sourceAppId); + } +}); + test('login backup inspection waits for wallet refresh to finish', async () => { let finishSync; const syncGate = new Promise((resolve) => { finishSync = resolve; }); const f = fixture({ bhr: { pendingAllBackup: true }, syncGate }); + f.state.bhr.pendingAllBackupByAppId[f.app.id] = true; f.collections.Wallet.push(wallet('pending', NetworkType.MAINNET)); const running = f.run('autoWalletsSyncWorker', { payload: { syncAll: false, hardRefresh: false, addNotifications: true, backupCheckAppId: f.app.id }, @@ -22,6 +68,7 @@ test('login backup inspection waits for wallet refresh to finish', async () => { test('failed refresh and account switch do not verify backup freshness', async () => { for (const option of ['syncFails', 'switchAppDuringSync']) { const f = fixture({ bhr: { pendingAllBackup: true }, [option]: true }); + f.state.bhr.pendingAllBackupByAppId[f.app.id] = true; f.collections.Wallet.push(wallet('pending', NetworkType.MAINNET)); await f.run('autoWalletsSyncWorker', { payload: { backupCheckAppId: f.app.id }, diff --git a/tests/recovery/helpers.cjs b/tests/recovery/helpers.cjs index 6d0b4e8e87..c8e03d888b 100644 --- a/tests/recovery/helpers.cjs +++ b/tests/recovery/helpers.cjs @@ -43,6 +43,7 @@ function harness(options = {}) { } }; const clock = options.now ? { Date: { now: options.now } } : {}; const transport = loadModule('src/services/backup/transport.ts', { '../rest/RestClient': rest }, clock); + transport.setBackupUploadGuard(id => app.id === id); const repair = loadModule('src/services/backup/repair.ts', { 'src/storage/realm/dbManager': db, 'src/storage/realm/enum': { RealmSchema: new Proxy({}, { get: (_, name) => name }) }, diff --git a/tests/recovery/labelAccount.test.cjs b/tests/recovery/labelAccount.test.cjs new file mode 100644 index 0000000000..c6801b3a0e --- /dev/null +++ b/tests/recovery/labelAccount.test.cjs @@ -0,0 +1,138 @@ +const assert = require('node:assert/strict'); +const { test } = require('node:test'); +const fs = require('node:fs'); +const path = require('node:path'); +const vm = require('node:vm'); +const ts = require('typescript'); +const { runSaga } = require('redux-saga'); +const { call, delay, put } = require('redux-saga/effects'); + +const file = 'src/store/sagas/utxos.ts'; +const source = ts.createSourceFile( + file, fs.readFileSync(path.join(__dirname, '../..', file), 'utf8'), ts.ScriptTarget.Latest, true +); +const names = new Set([ + 'currentOriginApp', 'persistTagsForOrigin', 'persistSpendabilityForOrigin', + 'originUiAction', + 'addLabelsWorker', 'bulkUpdateLabelsWorker', 'importLabelsWorker', 'markUTXOSpendabilityWorker', +]); +const extracted = source.statements + .filter((node) => ts.isFunctionDeclaration(node) && names.has(node.name?.text)) + .map((node) => node.getText(source).replace(/^export /, '')) + .join('\n'); +const code = ts.transpileModule(extracted, { + compilerOptions: { target: ts.ScriptTarget.ES2020 }, +}).outputText; + +function fixture({ consent = true, switchDuringCheck = false, switchDuringWalletRead = false } = {}) { + const state = { active: 'A', reduxActive: 'A', saved: [], deleted: [], uploaded: [], uiActions: [], failed: 0, consent }; + const dbManager = { + getObjectByIndex: () => ({ id: state.active, primarySeed: `seed-${state.active}` }), + createObjectBulk: (_schema, tags) => { + state.saved.push({ appId: state.active, tags }); + return true; + }, + deleteObjectById: (_schema, id) => { + state.deleted.push({ appId: state.active, id }); + return true; + }, + getObjectById: () => { + if (switchDuringWalletRead) state.active = 'B'; + return { id: 'wallet', specs: { + confirmedUTXOs: [{ txId: 'tx', vout: 0, spendability: 'spendable' }], + } }; + }, + updateObjectById: () => { + state.saved.push({ appId: state.active, walletUpdated: true }); + return true; + }, + }; + const scope = { + call, delay, put, dbManager, console, + store: { getState: () => ({ storage: { appId: state.reduxActive } }) }, + RealmSchema: { KeeperApp: 'KeeperApp', Tags: 'Tags', Wallet: 'Wallet', Vault: 'Vault' }, + LabelRefType: { TXN: 'TXN', ADDR: 'ADDR', OUTPUT: 'OUTPUT' }, + EntityKind: { VAULT: 'VAULT' }, + generateAbbreviatedOutputDescriptors: () => 'disposable-origin', + generateEncryptionKey: (seed) => seed, + hash256: (value) => `hash(${value})`, + encrypt: (_key, value) => value, + getJSONFromRealmObject: (value) => value, + Relay: { modifyLabels: async (appId, added, removed) => { + state.uploaded.push({ appId, added, removed }); + return { updated: true }; + } }, + checkBackupCondition: function* (appId) { + assert.equal(appId, 'A'); + if (switchDuringCheck) { + state.active = 'B'; + state.reduxActive = 'B'; + } + return !state.consent; + }, + setServerBackupFailed: function* (appId) { + assert.equal(appId, 'A'); + state.failed++; + }, + updateAppImageWorker: function* () { throw Error('unexpected backup'); }, + updateVaultImageWorker: function* () { throw Error('unexpected backup'); }, + setSyncingUTXOs: (value) => ({ type: 'sync', value }), + setSyncingUTXOError: (value) => ({ type: 'error', value }), + resetState: () => ({ type: 'reset' }), + }; + vm.runInNewContext(code, scope); + const run = (name, payload, originAppId = 'A') => runSaga( + { dispatch: (action) => state.uiActions.push({ ...action, active: state.active }), + getState: () => ({}) }, scope[name], { payload, originAppId } + ).toPromise(); + return { state, run }; +} + +const wallet = { id: 'wallet', entityKind: 'WALLET' }; +const add = { txId: 'tx', wallet, labels: [{ name: 'note', isSystem: false }], type: 'TXN' }; + +test('an A label action starting after switch to B writes neither Realm nor relay', async () => { + const f = fixture(); + f.state.active = 'B'; + f.state.reduxActive = 'B'; + await f.run('addLabelsWorker', add); + assert.deepEqual(f.state.saved, []); + assert.deepEqual(f.state.uploaded, []); +}); + +test('Redux account B with A Realm still open cannot persist A labels', async () => { + const f = fixture(); + f.state.reduxActive = 'B'; + await f.run('addLabelsWorker', add); + assert.deepEqual(f.state.saved, []); + assert.deepEqual(f.state.uploaded, []); +}); + +test('switch during connectivity check leaves A local label and makes no B upload', async () => { + const f = fixture({ switchDuringCheck: true }); + await f.run('addLabelsWorker', add); + assert.equal(f.state.saved.length, 1); + assert.equal(f.state.saved[0].appId, 'A'); + assert.deepEqual(f.state.uploaded, []); + assert.equal(f.state.failed, 0); + assert.ok(f.state.uiActions.filter((action) => action.active === 'B').every( + (action) => action.originAppId === 'A' + )); +}); + +test('unconsented A can label locally with zero backup upload', async () => { + const f = fixture({ consent: false }); + await f.run('importLabelsWorker', { labels: [{ type: 'TX', ref: 'tx', label: 'note', origin: 'fixture' }] }); + assert.equal(f.state.saved[0].appId, 'A'); + assert.deepEqual(f.state.uploaded, []); +}); + +test('UTXO spendability cannot persist into B after an A action waits for Realm', async () => { + const f = fixture({ switchDuringWalletRead: true }); + await f.run('markUTXOSpendabilityWorker', { + wallet, txId: 'tx', vout: 0, spendability: 'doNotSpend', + }); + assert.equal(f.state.active, 'B'); + assert.deepEqual(f.state.saved, []); + assert.deepEqual(f.state.uploaded, []); +}); diff --git a/tests/recovery/manualCoinChoices.test.cjs b/tests/recovery/manualCoinChoices.test.cjs index 2f7a4ae47c..7d68e4a032 100644 --- a/tests/recovery/manualCoinChoices.test.cjs +++ b/tests/recovery/manualCoinChoices.test.cjs @@ -43,7 +43,9 @@ function sagaFixture(options = {}) { return { updated: true }; }; loadFunctions(f.scope, 'src/store/sagas/bhr.ts', ['updateVaultImageWorker']); - loadFunctions(f.scope, 'src/store/sagas/utxos.ts', ['markUTXOSpendabilityWorker']); + loadFunctions(f.scope, 'src/store/sagas/utxos.ts', [ + 'currentOriginApp', 'persistSpendabilityForOrigin', 'markUTXOSpendabilityWorker', + ]); return f; } for (const kind of ['wallets', 'vaults']) test(`${kind}: compare manual choices but ignore chain-derived UTXO fields and confirmation order`, async () => { @@ -77,7 +79,7 @@ for (const kind of ['Wallet', 'Vault']) test(`${kind}: manual choice persists be assert.equal(f.collections[kind][0].specs.confirmedUTXOs[0].spendability, 'doNotSpend'); return original(payload); }; - await f.run('markUTXOSpendabilityWorker', { payload: { wallet: w, txId: coin().txId, vout: 0, spendability: 'doNotSpend' } }); + await f.run('markUTXOSpendabilityWorker', { originAppId: f.app.id, payload: { wallet: w, txId: coin().txId, vout: 0, spendability: 'doNotSpend' } }); const encrypted = kind === 'Vault' ? f.remote.vault : f.remote.wallets[w.id]; const restored = JSON.parse(encryption.decrypt(encryption.generateEncryptionKey(f.app.primarySeed), encrypted)); assert.equal(restored.specs.confirmedUTXOs[0].spendability, 'doNotSpend'); @@ -88,10 +90,10 @@ for (const kind of ['Wallet', 'Vault']) for (const options of [ { bhr: { automaticCloudBackup: false } }, { online: false }, { rejectIncremental: true }, { incrementalError: true }, ]) test(`${kind}: local manual choice survives backup unavailability ${JSON.stringify(options)}`, async () => { const f = sagaFixture(options); const w = withCoins(kind); f.collections[kind].push(w); - await f.run('markUTXOSpendabilityWorker', { payload: { wallet: w, txId: coin().txId, vout: 0, spendability: 'doNotSpend' } }); + await f.run('markUTXOSpendabilityWorker', { originAppId: f.app.id, payload: { wallet: w, txId: coin().txId, vout: 0, spendability: 'doNotSpend' } }); assert.equal(f.collections[kind][0].specs.confirmedUTXOs[0].spendability, 'doNotSpend'); if (options.bhr || options.online === false) assert.equal(f.calls.length, 0); - if (!options.bhr) assert.equal(f.state.bhr.pendingAllBackup, true); + if (!options.bhr) assert.equal(f.state.bhr.pendingAllBackupByAppId[f.app.id], true); assert.equal(f.remote.wallets[w.id], undefined); assert.equal(f.remote.vault, undefined); }); @@ -99,14 +101,14 @@ for (const kind of ['Wallet', 'Vault']) for (const options of [ test('missing outpoint and repeated identical choice do not upload', async () => { const f = sagaFixture(); const w = withCoins(); w.specs.confirmedUTXOs[0] = coin(); f.collections.Wallet.push(w); for (const txId of ['missing', coin().txId]) { - await f.run('markUTXOSpendabilityWorker', { payload: { wallet: w, txId, vout: 0, spendability: 'doNotSpend' } }); + await f.run('markUTXOSpendabilityWorker', { originAppId: f.app.id, payload: { wallet: w, txId, vout: 0, spendability: 'doNotSpend' } }); } assert.equal(f.calls.length, 0); }); test('rejected local persistence never uploads an unapplied coin choice', async () => { const f = sagaFixture({ rejectLocalWrite: true }); const w = withCoins(); f.collections.Wallet.push(w); - await f.run('markUTXOSpendabilityWorker', { payload: { wallet: w, txId: coin().txId, vout: 0, spendability: 'doNotSpend' } }); + await f.run('markUTXOSpendabilityWorker', { originAppId: f.app.id, payload: { wallet: w, txId: coin().txId, vout: 0, spendability: 'doNotSpend' } }); assert.equal(f.collections.Wallet[0].specs.confirmedUTXOs[0].spendability, 'spendable'); assert.equal(f.calls.length, 0); }); diff --git a/tests/recovery/sagaHarness.cjs b/tests/recovery/sagaHarness.cjs index 37cc5412d7..9e99f72a2a 100644 --- a/tests/recovery/sagaHarness.cjs +++ b/tests/recovery/sagaHarness.cjs @@ -79,12 +79,18 @@ function fixture(options = {}) { const state = { storage: { appId }, bhr: { - automaticCloudBackup: true, + automaticCloudBackup: false, + automaticCloudBackupByAppId: { + [appId]: options.bhr?.automaticCloudBackup !== false, + ...options.bhr?.automaticCloudBackupByAppId, + }, pendingAllBackup: false, + pendingAllBackupByAppId: {}, backupRepairCompletedByAppId: { [appId]: true }, ...options.bhr, }, - settings: { bitcoinNetworkType: NetworkType.MAINNET }, + settings: { bitcoinNetworkType: NetworkType.MAINNET, bitcoinNetwork: NetworkType.MAINNET }, + notifications: { fcmToken: 'disposable-token' }, account: account.default( undefined, account.setTempDetails({ hash: 'fixture', realmId: 'fixture', accountIdentifier: '' }) @@ -115,6 +121,8 @@ function fixture(options = {}) { const scope = { ...effects, markBackupMutation: () => {}, + store: { getState: () => state }, + isAutomaticCloudBackupEnabled: (backup, id) => !!backup.automaticCloudBackupByAppId?.[id], ...encryption, ...enums, ...account, @@ -158,6 +166,10 @@ function fixture(options = {}) { collections[schema] = collections[schema].filter((row) => String(row.id) !== String(id)); return true; }, + deleteObjectByPrimaryKey: (schema, key, value) => { + collections[schema] = collections[schema].filter((row) => String(row[key]) !== String(value)); + return true; + }, updateObjectById: (schema, id, patch) => { const row = collections[schema].find((record) => String(record.id) === String(id)); if (!row) return false; @@ -207,6 +219,8 @@ function fixture(options = {}) { config: { BIP85_IMAGE_ENCRYPTIONKEY_DERIVATION_PATH: 'fixture', isDevMode: () => false }, WalletUtilities: { getFingerprintFromSeed: () => 'AAAAAAAA' }, SubscriptionTier: { L1: 'L1', L3: 'L3' }, + SigningServer: { updatePolicy: async () => ({ updated: true }) }, + Alert: { alert: () => {} }, AppSubscriptionLevel: { L1: 1, L3: 3 }, BackupType: { SEED: 'SEED' }, BackupAction: { SEED_BACKUP_CONFIRMED: 'confirmed' }, @@ -227,7 +241,10 @@ function fixture(options = {}) { 'setBackupAllSuccess', 'setBackupAllFailure', 'setBackupAllLoading', + 'setDeleteBackupSuccess', + 'setDeleteBackupFailure', 'setPendingAllBackup', + 'setAutomaticCloudBackup', 'setHomeToastMessage', 'setRelayWalletUpdateLoading', 'relayWalletUpdateSuccess', @@ -235,6 +252,14 @@ function fixture(options = {}) { 'setRelaySignersUpdateLoading', 'relaySignersUpdateSuccess', 'relaySignersUpdateFail', + 'hideDeletingKeyModal', + 'showDeletingKeyModal', + 'showKeyDeletedSuccessModal', + 'setSignerPolicyError', + 'updateDelayedPolicyUpdate', + 'setRelayVaultUpdateLoading', + 'relayVaultUpdateSuccess', + 'relayVaultUpdateFail', 'setAppImageError', 'setSeedConfirmed', 'setBackupType', @@ -259,7 +284,10 @@ function fixture(options = {}) { 'sanitizeSeedKeyForBackup', 'sanitizeVaultSignersForSeedKeyBackup', ]) + '\n' + functions('src/store/sagas/bhr.ts', [ + 'currentBackupAppId', 'deleteAppImageEntityWorker', + 'deleteVaultImageWorker', + 'deleteBackupWorker', 'checkBackupCondition', 'setServerBackupFailed', 'updateAppImageWorker', @@ -270,7 +298,10 @@ function fixture(options = {}) { ]) + '\n' + functions('src/store/sagas/wallets.ts', [ + 'currentWalletAccountId', 'assertWalletAccount', 'addNewWalletsWorker', 'addSigningDeviceWorker', 'mergeSimilarKeysWorker', + 'deleteSigningDeviceWorker', 'deleteVaultWorker', + 'generateNewExternalAddressWorker', 'updateSignerPolicyWorker', 'updateVaultSignerXprivWorker', 'updateSignerDetailsWorker', 'updateKeyDetailsWorker', 'autoWalletsSyncWorker', ]); @@ -286,7 +317,11 @@ function fixture(options = {}) { actions.push(action); if (action.type === 'setBackupRepairCompleted') state.bhr.backupRepairCompletedByAppId[action.payload] = true; - if (action.type === 'setPendingAllBackup') state.bhr.pendingAllBackup = action.payload; + if (action.type === 'setPendingAllBackup') { + const { appId: pendingAppId, pending } = action.payload; + state.bhr.pendingAllBackupByAppId[pendingAppId] = pending; + state.bhr.pendingAllBackup = pending; + } state.account = account.default(state.account, action); }, }, diff --git a/tests/recovery/sagaState.test.cjs b/tests/recovery/sagaState.test.cjs index 9285b375c1..86b9bd33a6 100644 --- a/tests/recovery/sagaState.test.cjs +++ b/tests/recovery/sagaState.test.cjs @@ -19,11 +19,12 @@ function runInspection(f, repair) { const scope = { ...saga, ...effects, ...bhr, inspectBackup:f.repair.inspectBackup, RealmSchema:{KeeperApp:'KeeperApp',UAI:'UAI'}, dbManager:{ getObjectByIndex:()=>f.app, getObjectByField:()=>[] }, + currentBackupAppId:()=>f.app.id, uaiType:{SERVER_BACKUP_FAILURE:'failure'}, uaiActionedWorker:()=>{}, addToUaiStackWorker:()=>{}, }; vm.runInNewContext(ts.transpileModule(code,{ compilerOptions:{ target:ts.ScriptTarget.ES2020 }}).outputText,scope); let state = bhr.default(undefined,{type:'init'}); const actions = []; - const task = saga.runSaga({getState:()=>({bhr:state}),dispatch:action=>{ actions.push(action); state=bhr.default(state,action); }},scope.runBackupInspection,repair).toPromise(); + const task = saga.runSaga({getState:()=>({bhr:state,storage:{appId:f.app.id}}),dispatch:action=>{ actions.push(action); state=bhr.default(state,action); }},scope.runBackupInspection,repair).toPromise(); return {task,get state(){ return state; }, actions}; } @@ -33,14 +34,14 @@ test('only verified readback sets durable per-account completion and clears pend assert.equal(run.state.backupRepairCompletedByAppId.disposable,true); assert.equal(run.state.backupRepairStateByAppId.disposable,'verified'); assert.equal(run.state.backupRepairRunningByAppId.disposable,false); - assert.equal(run.state.pendingAllBackup,false); + assert.equal(run.state.pendingAllBackupByAppId.disposable,false); const phases=run.actions.filter(a=>a.type==='bhr/setBackupRepairState').map(a=>a.payload.phase); assert.deepEqual(phases,['checking','preparing','uploading','verifying','verified']); }); test('offline/unverified remains pending and retryable, never spins indefinitely', async () => { const f=harness({readError:true}); f.local.Wallet.push(wallet()); const run=runInspection(f,true); - assert.equal(await run.task,false); assert.equal(run.state.pendingAllBackup,true); + assert.equal(await run.task,false); assert.equal(run.state.pendingAllBackupByAppId.disposable,true); assert.equal(run.state.backupRepairCompletedByAppId.disposable,false); assert.equal(run.state.backupRepairRunningByAppId.disposable,false); }); diff --git a/tests/recovery/sendLabelOrigin.test.cjs b/tests/recovery/sendLabelOrigin.test.cjs new file mode 100644 index 0000000000..de3ad46a25 --- /dev/null +++ b/tests/recovery/sendLabelOrigin.test.cjs @@ -0,0 +1,97 @@ +const assert = require('node:assert/strict'); +const { test } = require('node:test'); +const fs = require('node:fs'); +const path = require('node:path'); +const vm = require('node:vm'); +const ts = require('typescript'); + +const root = path.join(__dirname, '../..'); +function sourceFile(file) { + return ts.createSourceFile(file, fs.readFileSync(path.join(root, file), 'utf8'), + ts.ScriptTarget.Latest, true, file.endsWith('.tsx') ? ts.ScriptKind.TSX : ts.ScriptKind.TS); +} +function declarations(file, names) { + const source = sourceFile(file); + const matches = []; + const walk = (node) => { + if (ts.isVariableDeclaration(node) && names.includes(node.name.getText(source))) + matches.push(`const ${node.getText(source)};`); + ts.forEachChild(node, walk); + }; + walk(source); + return matches; +} + +const code = ts.transpileModule([ + ...declarations('src/store/store.ts', [ + 'accountOwnedActions', 'accountOwnedUtxoUiActions', 'bindActionOrigin', + ]), + ...declarations('src/store/sagaActions/send_and_receive.ts', [ + 'SEND_PHASE_TWO', 'SEND_PHASE_THREE', 'DISCARD_BROADCASTED_TNX', + 'sendPhaseTwo', 'sendPhaseThree', 'discardBroadcastedTnx', + ]), + ...declarations('src/store/sagaActions/utxos.ts', ['IMPORT_LABELS', 'importLabels']), + ...declarations('src/components/ImportExportLabels/index.tsx', ['handleImportLabels']), + 'globalThis.actions = { bindActionOrigin, sendPhaseTwo, sendPhaseThree, discardBroadcastedTnx, importLabels, handleImportLabels };', +].join('\n'), { compilerOptions: { target: ts.ScriptTarget.ES2020 } }).outputText; +const scope = { + Set, + dbManager: { getObjectByIndex: () => ({ id: 'B' }) }, + RealmSchema: { KeeperApp: 'KeeperApp' }, + originAppId: 'A', + vault: { id: 'A-vault' }, + generateAbbreviatedOutputDescriptors: () => 'descriptor', + setIsSyncingLabels: () => {}, + onError: (error) => { throw Error(error); }, + onSuccess: () => {}, + console, +}; +vm.runInNewContext(code, scope); + +test('a delayed A send action keeps A as origin when dispatched on B', () => { + const { bindActionOrigin, sendPhaseTwo, sendPhaseThree, discardBroadcastedTnx } = scope.actions; + const middleware = bindActionOrigin({ getState: () => ({ storage: { appId: 'B' } }) }); + const seen = []; + const dispatch = middleware((action) => { seen.push(action); return action; }); + dispatch(sendPhaseTwo({ wallet: { id: 'A-wallet' } }, 'A')); + dispatch(sendPhaseThree({ wallet: { id: 'A-vault' } }, 'A')); + dispatch(discardBroadcastedTnx({ cachedTxid: 'tx', vault: { id: 'A-vault' } }, 'A')); + dispatch({ type: 'utxos/setSyncingUTXOs', payload: false, originAppId: 'A' }); + assert.equal(seen.length, 3); + assert.ok(seen.every((action) => action.originAppId === 'A')); +}); + +test('send screens pass their captured owner to label-bearing actions', () => { + for (const [file, actionName] of [ + ['src/screens/Send/SendConfirmation.tsx', 'sendPhaseTwo'], + ['src/screens/SignTransaction/SignTransactionScreen.tsx', 'sendPhaseThree'], + ['src/screens/Vault/VaultDetails.tsx', 'discardBroadcastedTnx'], + ]) { + const source = sourceFile(file); + const calls = []; + const walk = (node) => { + if (ts.isCallExpression(node) && node.expression.getText(source) === actionName) + calls.push(node); + ts.forEachChild(node, walk); + }; + walk(source); + assert.ok(calls.length, `${file} should dispatch ${actionName}`); + assert.ok(calls.every((call) => call.arguments[1]?.getText(source) === 'originAppId'), + `${file} must forward its captured owner`); + } +}); + +test('a file import callback from A keeps A after the active account switches to B', async () => { + const seen = []; + const middleware = scope.actions.bindActionOrigin({ + getState: () => ({ storage: { appId: 'B' } }), + }); + scope.dispatch = middleware((action) => { seen.push(action); return action; }); + scope.importFile = async (onFile) => onFile(JSON.stringify({ + type: 'tx', ref: 'txid', label: 'note', origin: 'descriptor', + })); + await scope.actions.handleImportLabels(); + assert.equal(seen.length, 1); + assert.equal(seen[0].type, 'IMPORT_LABELS'); + assert.equal(seen[0].originAppId, 'A'); +}); diff --git a/tests/recovery/usdtAccountDelete.test.cjs b/tests/recovery/usdtAccountDelete.test.cjs new file mode 100644 index 0000000000..72ed256c75 --- /dev/null +++ b/tests/recovery/usdtAccountDelete.test.cjs @@ -0,0 +1,136 @@ +const assert = require('node:assert/strict'); +const { test } = require('node:test'); +const fs = require('node:fs'); +const path = require('node:path'); +const vm = require('node:vm'); +const ts = require('typescript'); + +const file = 'src/hooks/useUSDTWallets.ts'; +const source = ts.createSourceFile( + file, fs.readFileSync(path.join(__dirname, '../..', file), 'utf8'), ts.ScriptTarget.Latest, true +); +const declarations = new Map(); +function find(node) { + if (ts.isVariableDeclaration(node) && + ['isOriginCurrent', 'createWallet', 'deleteWallet', 'updateWallet'].includes(node.name.getText(source))) + declarations.set(node.name.getText(source), node); + ts.forEachChild(node, find); +} +find(source); +for (const name of ['isOriginCurrent', 'createWallet', 'deleteWallet', 'updateWallet']) + assert.ok(declarations.has(name), `production USDT ${name} callback must exist`); +const code = ts.transpileModule( + [...declarations.values()].map((declaration) => `const ${declaration.getText(source)};`).join('\n'), { + compilerOptions: { target: ts.ScriptTarget.ES2020 }, +}).outputText; + +function fixture({ consent = true, switchDuringRemote = false, switchDuringGeneration = false, + switchDuringUpdate = false } = {}) { + const state = { active: 'A', remote: [], local: [], errors: [], dispatched: [], invalidated: [] }; + const scope = { + appId: 'A', + useCallback: (callback) => callback, + RealmSchema: { KeeperApp: 'KeeperApp', USDTWallet: 'USDTWallet' }, + dbManager: { + getObjectByIndex: (schema) => schema === 'KeeperApp' ? { id: state.active } : [], + getObjectById: () => ({ id: 'wallet', name: 'old' }), + createObject: async (_schema, wallet) => { + state.local.push({ appId: state.active, wallet }); + return true; + }, + updateObjectById: async (_schema, _id, update) => { + state.local.push({ appId: state.active, update }); + if (switchDuringUpdate) state.active = 'B'; + return true; + }, + deleteObjectById: (_schema, walletId) => { + state.local.push({ appId: state.active, walletId }); + return true; + }, + }, + reduxStore: { getState: () => ({ + storage: { appId: state.active }, + bhr: { automaticCloudBackupByAppId: { A: consent } }, + }) }, + isAutomaticCloudBackupEnabled: (backup, appId) => backup.automaticCloudBackupByAppId[appId], + USDTWalletType: { DEFAULT: 'default' }, + USDTWalletSupportedNetwork: 'mainnet', + generateUSDTWallet: async () => { + if (switchDuringGeneration) state.active = 'B'; + return { id: 'wallet', name: 'new' }; + }, + canonical: JSON.stringify, + recoveryContent: (_kind, wallet) => wallet, + updateAppImage: (payload, originAppId) => ({ type: 'UPDATE_APP_IMAGE', payload, originAppId }), + invalidateBackupRepair: (appId) => ({ type: 'INVALIDATE_BACKUP_REPAIR', appId }), + setPendingAllBackup: (payload) => ({ type: 'SET_PENDING_ALL_BACKUP', payload }), + markBackupMutation: (appId) => state.invalidated.push(appId), + dispatch: (action) => state.dispatched.push(action), + Relay: { deleteAppImageEntity: async (body) => { + state.remote.push(body); + if (switchDuringRemote) state.active = 'B'; + return { updated: true }; + } }, + setError: (error) => state.errors.push(error), + captureError: () => {}, + }; + vm.runInNewContext(`${code}\nglobalThis.callbacks = { createWallet, deleteWallet, updateWallet };`, scope); + return { state, ...scope.callbacks }; +} + +test('unconsented USDT wallet deletes locally without a relay write', async () => { + const f = fixture({ consent: false }); + assert.equal(await f.deleteWallet('wallet'), true); + assert.deepEqual(f.state.remote, []); + assert.equal(f.state.local[0].appId, 'A'); +}); + +test('remote response after account switch cannot delete B local wallet', async () => { + const f = fixture({ switchDuringRemote: true }); + assert.equal(await f.deleteWallet('wallet'), false); + assert.equal(f.state.remote.length, 1); + assert.equal(f.state.remote[0].appId, 'A'); + assert.deepEqual(f.state.local, []); + assert.deepEqual(f.state.invalidated, ['A']); + assert.equal(f.state.dispatched.find((action) => action.type === 'SET_PENDING_ALL_BACKUP').payload.appId, 'A'); +}); + +test('a stale A callback invoked while B is active makes no write', async () => { + const f = fixture(); + f.state.active = 'B'; + assert.equal(await f.deleteWallet('wallet'), false); + assert.deepEqual(f.state.remote, []); + assert.deepEqual(f.state.local, []); +}); + +test('wallet generation after account switch cannot persist to B or dispatch backup', async () => { + const f = fixture({ switchDuringGeneration: true }); + const result = await f.createWallet({ type: 'default', name: 'new', description: '' }); + assert.equal(result.error, 'Account changed'); + assert.deepEqual(f.state.local, []); + assert.deepEqual(f.state.dispatched, []); +}); + +test('wallet create dispatches backup with captured originating account', async () => { + const f = fixture(); + const result = await f.createWallet({ type: 'default', name: 'new', description: '' }); + assert.equal(result.newWallet.id, 'wallet'); + assert.equal(f.state.local[0].appId, 'A'); + assert.equal(f.state.dispatched[0].originAppId, 'A'); +}); + +test('wallet update after account switch cannot dispatch backup for B', async () => { + const f = fixture({ switchDuringUpdate: true }); + assert.equal(await f.updateWallet({ id: 'wallet', name: 'new' }), false); + assert.equal(f.state.local[0].appId, 'A'); + assert.equal(f.state.dispatched.some((action) => action.type === 'UPDATE_APP_IMAGE'), false); + assert.deepEqual(f.state.invalidated, ['A']); +}); + +test('a stale wallet update invoked while B is active makes no write', async () => { + const f = fixture(); + f.state.active = 'B'; + assert.equal(await f.updateWallet({ id: 'wallet', name: 'new' }), false); + assert.deepEqual(f.state.local, []); + assert.deepEqual(f.state.dispatched, []); +}); diff --git a/tests/recovery/usdtBackup.test.cjs b/tests/recovery/usdtBackup.test.cjs index aedac01595..8041d14d2f 100644 --- a/tests/recovery/usdtBackup.test.cjs +++ b/tests/recovery/usdtBackup.test.cjs @@ -47,6 +47,7 @@ function hookFixture(options = {}) { canonical: image.canonical, recoveryContent: image.recoveryContent, appId: f.app.id, + isOriginCurrent: () => f.app.id === f.state.storage.appId, markBackupMutation: (id) => mutations.push(id), invalidateBackupRepair: (payload) => ({ type: 'invalidateBackupRepair', payload }), setPendingAllBackup: (payload) => ({ type: 'setPendingAllBackup', payload }), @@ -59,7 +60,10 @@ function hookFixture(options = {}) { pending.push(f.run('updateAppImageWorker', action)); } else { f.actions.push(action); - if (action.type === 'setPendingAllBackup') f.state.bhr.pendingAllBackup = action.payload; + if (action.type === 'setPendingAllBackup') { + f.state.bhr.pendingAllBackupByAppId[action.payload.appId] = action.payload.pending; + f.state.bhr.pendingAllBackup = action.payload.pending; + } } }, setError: (message) => errors.push(message), From b848cd414382ab331d475303487ec4674aba6058 Mon Sep 17 00:00:00 2001 From: Anant Date: Thu, 8 Oct 2026 18:12:53 +0530 Subject: [PATCH 073/254] fix(faucet): drop stale profile outcomes --- src/store/sagas/wallets.ts | 61 +++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/src/store/sagas/wallets.ts b/src/store/sagas/wallets.ts index fac97ef003..832339e2c4 100644 --- a/src/store/sagas/wallets.ts +++ b/src/store/sagas/wallets.ts @@ -1201,11 +1201,7 @@ export const updateSignerPolicyWatcher = createWatcher( ); export function* testcoinsWorker({ payload }) { - const { wallet } = payload; - yield put(setTestCoinsReceived(false)); - yield put(setTestCoinsFailed(false)); - yield put(setTestCoinsPending(null)); - + let { wallet } = payload; const faucetScope = yield select((state: RootState) => ({ appId: state.storage.appId, networkType: state.settings.bitcoinNetworkType, @@ -1221,7 +1217,26 @@ export function* testcoinsWorker({ payload }) { ); }; - if (wallet.networkType !== NetworkType.TESTNET || !isCurrentFaucetScope()) { + if (!isCurrentFaucetScope()) return; + const currentWallet = yield call(() => { + if (!isCurrentFaucetScope()) return null; + const schema = wallet.entityKind === EntityKind.VAULT ? RealmSchema.Vault : RealmSchema.Wallet; + const stored = dbManager.getObjectById(schema, wallet.id); + if ( + !stored || + stored.networkType !== wallet.networkType || + stored.entityKind !== wallet.entityKind + ) + return null; + return getJSONFromRealmObject(stored); + }); + if (!currentWallet || !isCurrentFaucetScope()) return; + wallet = currentWallet; + yield put(setTestCoinsReceived(false)); + yield put(setTestCoinsFailed(false)); + yield put(setTestCoinsPending(null)); + + if (wallet.networkType !== NetworkType.TESTNET) { yield put(setTestCoinsFailed(true)); return; } @@ -1232,14 +1247,14 @@ export function* testcoinsWorker({ payload }) { try { receivingAddress = WalletOperations.getNextFreeAddress(wallet); network = WalletUtilities.getNetworkByType(wallet.networkType); - const response = yield call( - Relay.getTestcoins, - receivingAddress, - wallet.networkType, - faucetScope.appId - ); + const response = yield call(() => { + if (!isCurrentFaucetScope()) return null; + return Relay.getTestcoins(receivingAddress, wallet.networkType, faucetScope.appId); + }); + if (!response) return; txid = response.txid; } catch (err) { + if (!isCurrentFaucetScope()) return; if (err.message === 'FAUCET_DAILY_LIMIT_REACHED') { yield put(setTestCoinsQuotaReached(true)); } else if (err.message === 'FAUCET_OUTCOME_UNKNOWN') { @@ -1250,19 +1265,13 @@ export function* testcoinsWorker({ payload }) { return; } - if (!isCurrentFaucetScope()) { - yield put(setTestCoinsPending('unknown')); - return; - } + if (!isCurrentFaucetScope()) return; let lastAttemptSynced = false; for (let attempt = 0; attempt < 8; attempt += 1) { let attemptSynced = false; try { - if (!isCurrentFaucetScope()) { - yield put(setTestCoinsPending('unknown')); - return; - } + if (!isCurrentFaucetScope()) return; let connectionMatchesScope = false; try { ElectrumClient.assertConnectionGeneration( @@ -1276,10 +1285,7 @@ export function* testcoinsWorker({ payload }) { if (!ELECTRUM_CLIENT.isClientConnected || !connectionMatchesScope) { yield call(connectToNodeWorker); } - if (!isCurrentFaucetScope()) { - yield put(setTestCoinsPending('unknown')); - return; - } + if (!isCurrentFaucetScope()) return; const connectionGeneration = ElectrumClient.getConnectionGeneration(); ElectrumClient.assertConnectionGeneration(connectionGeneration, wallet.networkType); const { txids } = yield call( @@ -1288,10 +1294,7 @@ export function* testcoinsWorker({ payload }) { network ); ElectrumClient.assertConnectionGeneration(connectionGeneration, wallet.networkType); - if (!isCurrentFaucetScope()) { - yield put(setTestCoinsPending('unknown')); - return; - } + if (!isCurrentFaucetScope()) return; attemptSynced = true; if (txids.some((seenTxid: string) => seenTxid.toLowerCase() === txid.toLowerCase())) { const refreshed = yield call(refreshWalletsWorker, { @@ -1303,6 +1306,7 @@ export function* testcoinsWorker({ payload }) { const storedWallet = yield call(dbManager.getObjectById, schema, wallet.id); const storedData = storedWallet?.toJSON?.() || storedWallet; if (isFaucetTxVisible(storedData, txid)) { + if (!isCurrentFaucetScope()) return; yield put(setTestCoinsReceived(true)); return; } @@ -1314,6 +1318,7 @@ export function* testcoinsWorker({ payload }) { lastAttemptSynced = attemptSynced; if (attempt < 7) yield delay(4000); } + if (!isCurrentFaucetScope()) return; yield put(setTestCoinsPending(lastAttemptSynced ? 'propagation' : 'sync')); } From d488d5f97130557a6a9fd796e5f817edb6799a63 Mon Sep 17 00:00:00 2001 From: Anant Date: Thu, 8 Oct 2026 18:12:51 +0530 Subject: [PATCH 074/254] fix(network): fence stale Electrum work by connection generation --- src/services/electrum/client.ts | 332 +++++++++++++----- src/services/electrum/node.tsx | 5 +- src/store/reducers/network.ts | 10 +- src/store/sagas/network.ts | 53 ++- .../electrumConnectionGeneration.test.ts | 189 ++++++++++ tests/services/manualNodeFailover.test.ts | 3 +- tests/services/networkConnectRace.test.ts | 117 ++++++ .../services/testnetFallbackMigration.test.ts | 15 + 8 files changed, 632 insertions(+), 92 deletions(-) create mode 100644 tests/services/electrumConnectionGeneration.test.ts create mode 100644 tests/services/networkConnectRace.test.ts create mode 100644 tests/services/testnetFallbackMigration.test.ts diff --git a/src/services/electrum/client.ts b/src/services/electrum/client.ts index b40ce2f5e4..1c4b80f64e 100644 --- a/src/services/electrum/client.ts +++ b/src/services/electrum/client.ts @@ -2,6 +2,10 @@ import ElectrumCli from 'electrum-client'; import reverse from 'buffer-reverse'; import * as bitcoinJS from 'bitcoinjs-lib'; import { NodeDetail } from 'src/services/wallets/interfaces'; +import { NetworkType } from 'src/services/wallets/enums'; +import dbManager from 'src/storage/realm/dbManager'; +import { RealmSchema } from 'src/storage/realm/enum'; +import { electrumClientConnectionExecuted } from 'src/store/reducers/login'; import { ElectrumTransaction, ElectrumUTXO } from './interface'; import torrific from './torrific'; import RestClient, { TorStatus } from '../rest/RestClient'; @@ -23,29 +27,63 @@ const ELECTRUM_CLIENT_CONFIG: { reconnectDelay: 500, // retry after half a second }; -const createElectrumClientState = () => ({ +type ElectrumClientState = { + electrumClient: any; + isClientConnected: boolean; + currentPeerIndex: number; + connectionAttempt: number; + activePeer: NodeDetail | null; + peers: NodeDetail[]; + appId: string | null; + runtimeReconnectOrigin: NodeDetail | null; +}; + +const createElectrumClientState = (): ElectrumClientState => ({ electrumClient: null, isClientConnected: false, currentPeerIndex: -1, connectionAttempt: 0, activePeer: null, peers: [], + appId: null, + runtimeReconnectOrigin: null, }); +let connectionGeneration = 0; + +export const ELECTRUM_CONNECTION_CHANGED_ERR = 'Electrum connection changed during request'; + +const staleConnectionResult = () => ({ + connected: false, + error: ELECTRUM_CONNECTION_CHANGED_ERR, + errorType: 'network' as ElectrumConnectionErrorType, +}); + +const selectionMatches = (state: ElectrumClientState): boolean => { + const current = store.getState(); + const selectedNetwork = current.settings?.bitcoinNetworkType; + return ( + state.appId === (current.storage?.appId ?? null) && + (!selectedNetwork || !state.activePeer || state.activePeer.networkType === selectedNetwork) + ); +}; + +const stateIsCurrent = (state: ElectrumClientState, generation: number): boolean => + ELECTRUM_CLIENT === state && connectionGeneration === generation && selectionMatches(state); + +const networkTypeFor = (network: bitcoinJS.Network): NetworkType | null => { + if (network?.bech32 === bitcoinJS.networks.bitcoin.bech32) return NetworkType.MAINNET; + if (network?.bech32 === bitcoinJS.networks.testnet.bech32) return NetworkType.TESTNET; + return null; +}; + let lastConnectionError: { type: ElectrumConnectionErrorType; message: string; } | null = null; // eslint-disable-next-line import/no-mutable-exports -export let ELECTRUM_CLIENT: { - electrumClient: any; - isClientConnected: boolean; - currentPeerIndex: number; - connectionAttempt: number; - activePeer: NodeDetail; - peers: NodeDetail[]; -} = createElectrumClientState(); +export let ELECTRUM_CLIENT: ElectrumClientState = createElectrumClientState(); export const ELECTRUM_NOT_CONNECTED_ERR = 'Network Error: The current electrum node is not reachable, please try again with a different node'; @@ -56,47 +94,54 @@ export const ELECTRUM_NOT_CONNECTED_ERR_TOR = export default class ElectrumClient { public static connectOverTor = false; - public static async connect() { + public static async connect(expectedGeneration = connectionGeneration) { + if (expectedGeneration !== connectionGeneration) return staleConnectionResult(); + const state = ELECTRUM_CLIENT; + const peer = state.activePeer; + if (!selectionMatches(state)) return staleConnectionResult(); + + if (!peer) { + return { + connected: false, + error: 'Unable to connect to any electrum server. Please switch network and try again!', + generation: connectionGeneration, + }; + } + + const generation = ++connectionGeneration; let timeoutId = null; + let client: ElectrumCli | null = null; try { - if (!ELECTRUM_CLIENT.activePeer) { - console.log( - 'Unable to connect to any electrum server. Please switch network and try again!' - ); - return { - connected: ELECTRUM_CLIENT.isClientConnected, - error: 'Unable to connect to any electrum server. Please switch network and try again!', - }; - } + state.isClientConnected = false; + if (state.electrumClient?.close) state.electrumClient.close(); ElectrumClient.connectOverTor = - ELECTRUM_CLIENT.activePeer?.host?.endsWith('.onion') && - RestClient?.getTorStatus() === TorStatus.CONNECTED; + peer.host?.endsWith('.onion') && RestClient?.getTorStatus() === TorStatus.CONNECTED; - const client = new ElectrumCli( + client = new ElectrumCli( ElectrumClient.connectOverTor ? torrific : global.net, global.tls, - ELECTRUM_CLIENT.activePeer.port, - ELECTRUM_CLIENT.activePeer.host, - ELECTRUM_CLIENT.activePeer.useSSL ? 'tls' : 'tcp' + peer.port, + peer.host, + peer.useSSL ? 'tls' : 'tcp' ); // tcp or tls - ELECTRUM_CLIENT.electrumClient = client; + state.electrumClient = client; client.onError = (error) => { - if (ELECTRUM_CLIENT.electrumClient === client && ELECTRUM_CLIENT.isClientConnected) { + if ( + stateIsCurrent(state, generation) && + state.electrumClient === client && + state.isClientConnected + ) { console.log('Electrum mainClient.onError():', error?.message || error); - ELECTRUM_CLIENT.isClientConnected = false; - ELECTRUM_CLIENT.activePeer.isConnected = false; + state.runtimeReconnectOrigin = state.runtimeReconnectOrigin || peer; + state.isClientConnected = false; + peer.isConnected = false; if (client?.close) client.close(); console.log('Error: Close the connection'); - - // setTimeout( - // ElectrumClient.connect, - // ELECTRUM_CLIENT.activePeer?.host?.endsWith('.onion') ? 4000 : 500 - // ); - return ElectrumClient.reconnect(); + return ElectrumClient.reconnect(generation); } }; @@ -111,22 +156,39 @@ export default class ElectrumClient { version: '1.4', }), // should resolve within 4 seconds(prior to timeout) ]); + if (!stateIsCurrent(state, generation) || state.electrumClient !== client) { + if (client?.close) client.close(); + return staleConnectionResult(); + } if (ver === 'timeout') throw new Error('Connection time-out'); if (ver && ver[0]) { console.log('Connection to electrum server is established', { ver, - node: ELECTRUM_CLIENT.activePeer.host, + node: peer.host, }); lastConnectionError = null; - ELECTRUM_CLIENT.isClientConnected = true; - ELECTRUM_CLIENT.activePeer.isConnected = true; - ELECTRUM_CLIENT.connectionAttempt = 0; + state.isClientConnected = true; + peer.isConnected = true; + state.connectionAttempt = 0; + if (state.runtimeReconnectOrigin) { + ElectrumClient.recordRuntimeRotation( + state, + state.runtimeReconnectOrigin, + peer, + generation + ); + state.runtimeReconnectOrigin = null; + } } } catch (error) { - ELECTRUM_CLIENT.isClientConnected = false; - if (ELECTRUM_CLIENT.activePeer) ELECTRUM_CLIENT.activePeer.isConnected = false; + if (!stateIsCurrent(state, generation) || (client && state.electrumClient !== client)) { + if (client?.close) client.close(); + return staleConnectionResult(); + } + state.isClientConnected = false; + peer.isConnected = false; const errorType = classifyElectrumConnectionError(error); const errorMessage = error?.message || String(error); @@ -134,7 +196,7 @@ export default class ElectrumClient { type: errorType, message: errorMessage, }; - console.log('Bad connection:', JSON.stringify(ELECTRUM_CLIENT.activePeer), { + console.log('Bad connection:', JSON.stringify(peer), { errorType, message: errorMessage, }); @@ -142,22 +204,31 @@ export default class ElectrumClient { if (timeoutId) clearTimeout(timeoutId); } - if (ELECTRUM_CLIENT.isClientConnected) { + if (!stateIsCurrent(state, generation) || state.electrumClient !== client) { + return staleConnectionResult(); + } + if (state.isClientConnected) { return { - connected: ELECTRUM_CLIENT.isClientConnected, - connectedTo: ELECTRUM_CLIENT.activePeer.host, + connected: true, + connectedTo: peer.host, + generation, }; } - return ElectrumClient.reconnect(); + return ElectrumClient.reconnect(generation); } - public static async reconnect() { - ELECTRUM_CLIENT.connectionAttempt += 1; + public static async reconnect(expectedGeneration = connectionGeneration) { + if (expectedGeneration !== connectionGeneration) return staleConnectionResult(); + const state = ELECTRUM_CLIENT; + if (!selectionMatches(state)) return staleConnectionResult(); + const generation = ++connectionGeneration; + state.connectionAttempt += 1; + state.isClientConnected = false; // close the connection before attempting again - if (ELECTRUM_CLIENT.electrumClient?.close) ELECTRUM_CLIENT.electrumClient.close(); + if (state.electrumClient?.close) state.electrumClient.close(); - if (ELECTRUM_CLIENT.connectionAttempt >= ELECTRUM_CLIENT_CONFIG.maxConnectionAttempt) { + if (state.connectionAttempt >= ELECTRUM_CLIENT_CONFIG.maxConnectionAttempt) { const nextPeer = ElectrumClient.getNextPeer(); if (!nextPeer) { const fallbackError = @@ -170,23 +241,81 @@ export default class ElectrumClient { error, }); - return { - connected: ELECTRUM_CLIENT.isClientConnected, - error, - errorType, - }; + if (state.runtimeReconnectOrigin && stateIsCurrent(state, generation)) { + store.dispatch(electrumClientConnectionExecuted({ successful: false, error })); + state.runtimeReconnectOrigin = null; + } + return { connected: false, error, errorType, generation }; } - ELECTRUM_CLIENT.activePeer = nextPeer; - ELECTRUM_CLIENT.connectionAttempt = 0; + state.activePeer = nextPeer; + state.connectionAttempt = 0; console.log(`Attempting a connection with next peer: ${nextPeer?.host}`); - return ElectrumClient.connect(); + return ElectrumClient.connect(generation); } - console.log(`Reconnection attempt #${ELECTRUM_CLIENT.connectionAttempt}`); + console.log(`Reconnection attempt #${state.connectionAttempt}`); await new Promise((resolve) => { setTimeout(resolve, ELECTRUM_CLIENT_CONFIG.reconnectDelay); // attempts reconnection after 1 second }); - return ElectrumClient.connect(); + if (!stateIsCurrent(state, generation)) return staleConnectionResult(); + return ElectrumClient.connect(generation); + } + + private static recordRuntimeRotation( + state: ElectrumClientState, + previousPeer: NodeDetail, + connectedPeer: NodeDetail, + generation: number + ) { + if (!stateIsCurrent(state, generation)) return; + if (previousPeer.id !== connectedPeer.id) { + try { + const previousSaved = dbManager.updateObjectById( + RealmSchema.NodeConnect, + previousPeer.id.toString(), + { + isConnected: false, + } + ); + const connectedSaved = dbManager.updateObjectById( + RealmSchema.NodeConnect, + connectedPeer.id.toString(), + { + isConnected: true, + } + ); + if (!previousSaved || !connectedSaved) { + console.warn('Unable to save active Electrum server after failover'); + } + } catch (error) { + console.warn('Unable to save active Electrum server after failover', error); + } + } + store.dispatch( + electrumClientConnectionExecuted({ successful: true, connectedTo: connectedPeer.host }) + ); + } + + public static getConnectionGeneration(): number { + return connectionGeneration; + } + + public static assertConnectionGeneration( + expected: number, + expectedNetworkType?: NetworkType + ): void { + const state = ELECTRUM_CLIENT; + const selectedNetwork = store.getState().settings?.bitcoinNetworkType; + if ( + !stateIsCurrent(state, expected) || + !state.isClientConnected || + !state.electrumClient || + (expectedNetworkType && + (state.activePeer?.networkType !== expectedNetworkType || + (selectedNetwork && selectedNetwork !== expectedNetworkType))) + ) { + throw new Error(ELECTRUM_CONNECTION_CHANGED_ERR); + } } public static getNextPeer() { @@ -202,22 +331,33 @@ export default class ElectrumClient { public static forceDisconnect() { if (!ELECTRUM_CLIENT.electrumClient) throw new Error('Electrum client not available'); + connectionGeneration += 1; ELECTRUM_CLIENT.isClientConnected = false; if (ELECTRUM_CLIENT.activePeer) ELECTRUM_CLIENT.activePeer.isConnected = false; + ELECTRUM_CLIENT.runtimeReconnectOrigin = null; if (ELECTRUM_CLIENT.electrumClient?.close) ELECTRUM_CLIENT.electrumClient.close(); } public static async serverFeatures() { ElectrumClient.checkConnection(); - - return ELECTRUM_CLIENT.electrumClient.server_features(); + const generation = connectionGeneration; + const client = ELECTRUM_CLIENT.electrumClient; + const features = await client.server_features(); + ElectrumClient.assertConnectionGeneration(generation); + return features; } - public static getBlockchainHeaders = async (): Promise<{ height: number; hex: string }> => - ELECTRUM_CLIENT.electrumClient.blockchainHeaders_subscribe(); + public static getBlockchainHeaders = async (): Promise<{ height: number; hex: string }> => { + ElectrumClient.checkConnection(); + const generation = connectionGeneration; + const client = ELECTRUM_CLIENT.electrumClient; + const headers = await client.blockchainHeaders_subscribe(); + ElectrumClient.assertConnectionGeneration(generation); + return headers; + }; public static checkConnection() { - if (!ELECTRUM_CLIENT.isClientConnected) { + if (!ELECTRUM_CLIENT.isClientConnected || !selectionMatches(ELECTRUM_CLIENT)) { const connectionError = ElectrumClient.connectOverTor ? ELECTRUM_NOT_CONNECTED_ERR_TOR : ELECTRUM_NOT_CONNECTED_ERR; @@ -227,9 +367,12 @@ export default class ElectrumClient { public static async ping() { if (!ELECTRUM_CLIENT.electrumClient) throw new Error('Electrum client not available'); - + const generation = connectionGeneration; + const client = ELECTRUM_CLIENT.electrumClient; try { - await ELECTRUM_CLIENT.electrumClient.server_ping(); + ElectrumClient.assertConnectionGeneration(generation); + await client.server_ping(); + ElectrumClient.assertConnectionGeneration(generation); } catch (_) { return false; } @@ -248,6 +391,7 @@ export default class ElectrumClient { // if current peer to use is provided, it will use that peer public static setActivePeer(nodes: NodeDetail[], currentPeerToUse?: NodeDetail) { // close previous connection + connectionGeneration += 1; ELECTRUM_CLIENT.isClientConnected = false; if (ELECTRUM_CLIENT.electrumClient?.close) { ELECTRUM_CLIENT.electrumClient.close(); @@ -255,6 +399,7 @@ export default class ElectrumClient { // set defaults ELECTRUM_CLIENT = createElectrumClientState(); + ELECTRUM_CLIENT.appId = store.getState().storage?.appId ?? null; lastConnectionError = null; // set active node @@ -265,7 +410,13 @@ export default class ElectrumClient { } /** Extend a successful manual connection with eligible saved peers for later reconnects. */ - public static setFailoverPeers(nodes: NodeDetail[]) { + public static setFailoverPeers(nodes: NodeDetail[], expectedGeneration?: number) { + if ( + !ELECTRUM_CLIENT.isClientConnected || + !selectionMatches(ELECTRUM_CLIENT) || + (expectedGeneration !== undefined && expectedGeneration !== connectionGeneration) + ) + return; const activePeer = ELECTRUM_CLIENT.activePeer; ELECTRUM_CLIENT.peers = getFailoverPeers(nodes, activePeer); ELECTRUM_CLIENT.currentPeerIndex = activePeer ? 0 : -1; @@ -286,9 +437,15 @@ export default class ElectrumClient { ): Promise<{ [address: string]: ElectrumUTXO[] }> { ElectrumClient.checkConnection(); if (!network) network = store.getState().settings.bitcoinNetwork; + const generation = connectionGeneration; + const client = ELECTRUM_CLIENT.electrumClient; + const expectedNetworkType = networkTypeFor(network); + if (!expectedNetworkType) throw new Error('Unsupported Bitcoin network'); + ElectrumClient.assertConnectionGeneration(generation, expectedNetworkType); const res = {}; const chunks = ElectrumClient.splitIntoChunks(addresses, batchsize); for (let itr = 0; itr < chunks.length; itr += 1) { + ElectrumClient.assertConnectionGeneration(generation, expectedNetworkType); const chunk = chunks[itr]; const scripthashes = []; const scripthash2addr = {}; @@ -304,9 +461,8 @@ export default class ElectrumClient { } // eslint-disable-next-line no-await-in-loop - const results = await ELECTRUM_CLIENT.electrumClient.blockchainScripthash_listunspentBatch( - scripthashes - ); + const results = await client.blockchainScripthash_listunspentBatch(scripthashes); + ElectrumClient.assertConnectionGeneration(generation, expectedNetworkType); for (let index = 0; index < results.length; index += 1) { const utxos = results[index]; @@ -324,6 +480,7 @@ export default class ElectrumClient { } } + ElectrumClient.assertConnectionGeneration(generation, expectedNetworkType); return res; } @@ -341,6 +498,11 @@ export default class ElectrumClient { } if (!network) network = store.getState().settings.bitcoinNetwork; ElectrumClient.checkConnection(); + const generation = connectionGeneration; + const client = ELECTRUM_CLIENT.electrumClient; + const expectedNetworkType = networkTypeFor(network); + if (!expectedNetworkType) throw new Error('Unsupported Bitcoin network'); + ElectrumClient.assertConnectionGeneration(generation, expectedNetworkType); const historyByAddress = {}; const txids = []; @@ -348,6 +510,7 @@ export default class ElectrumClient { const chunks = ElectrumClient.splitIntoChunks(addresses, batchsize); for (let itr = 0; itr < chunks.length; itr += 1) { + ElectrumClient.assertConnectionGeneration(generation, expectedNetworkType); const chunk = chunks[itr]; const scripthashes = []; const scripthash2addr = {}; @@ -363,9 +526,8 @@ export default class ElectrumClient { } // eslint-disable-next-line no-await-in-loop - const results = await ELECTRUM_CLIENT.electrumClient.blockchainScripthash_getHistoryBatch( - scripthashes - ); + const results = await client.blockchainScripthash_getHistoryBatch(scripthashes); + ElectrumClient.assertConnectionGeneration(generation, expectedNetworkType); for (let index = 0; index < results.length; index += 1) { const history = results[index]; @@ -382,6 +544,7 @@ export default class ElectrumClient { } } + ElectrumClient.assertConnectionGeneration(generation, expectedNetworkType); return { historyByAddress, txids, txidToAddress }; } @@ -395,6 +558,9 @@ export default class ElectrumClient { return {}; } ElectrumClient.checkConnection(); + const generation = connectionGeneration; + const client = ELECTRUM_CLIENT.electrumClient; + ElectrumClient.assertConnectionGeneration(generation); const res = {}; txids = [...new Set(txids)]; // remove duplicates, if any @@ -402,10 +568,12 @@ export default class ElectrumClient { // lets try cache first const chunks = ElectrumClient.splitIntoChunks(txids, batchsize); for (const chunk of chunks) { + ElectrumClient.assertConnectionGeneration(generation); let results = []; // eslint-disable-next-line no-await-in-loop - results = await ELECTRUM_CLIENT.electrumClient.blockchainTransaction_getBatch(chunk, verbose); + results = await client.blockchainTransaction_getBatch(chunk, verbose); + ElectrumClient.assertConnectionGeneration(generation); for (const txdata of results) { if (txdata.error && txdata.error.code === -32600) { @@ -427,21 +595,27 @@ export default class ElectrumClient { } } + ElectrumClient.assertConnectionGeneration(generation); return res; } public static async estimateFee(numberOfBlocks: number = 1) { ElectrumClient.checkConnection(); - - const feePerKB = await ELECTRUM_CLIENT.electrumClient.blockchainEstimatefee(numberOfBlocks); // in bitcoin + const generation = connectionGeneration; + const client = ELECTRUM_CLIENT.electrumClient; + const feePerKB = await client.blockchainEstimatefee(numberOfBlocks); // in bitcoin + ElectrumClient.assertConnectionGeneration(generation); if (feePerKB === -1) return 1; return Math.round((feePerKB / 1024) * 1e8); // feePerByte(sats) } public static async broadcast(txHex: string) { ElectrumClient.checkConnection(); - - return ELECTRUM_CLIENT.electrumClient.blockchainTransaction_broadcast(txHex); + const generation = connectionGeneration; + const client = ELECTRUM_CLIENT.electrumClient; + const txid = await client.blockchainTransaction_broadcast(txHex); + ElectrumClient.assertConnectionGeneration(generation); + return txid; } public static async testConnection(node: NodeDetail) { diff --git a/src/services/electrum/node.tsx b/src/services/electrum/node.tsx index 00c5a15e81..29007cd312 100644 --- a/src/services/electrum/node.tsx +++ b/src/services/electrum/node.tsx @@ -94,10 +94,11 @@ export default class Node { // The tapped node must connect on its own; only then enable eligible saved // public peers for a later connection loss. ElectrumClient.setActivePeer([], selectedNode); - const { connected, connectedTo, error } = await ElectrumClient.connect(); + const { connected, connectedTo, error, generation } = await ElectrumClient.connect(); if (connected) { ElectrumClient.setFailoverPeers( - this.getAllNodes().filter((node) => node.networkType === selectedNode.networkType) + this.getAllNodes().filter((node) => node.networkType === selectedNode.networkType), + generation ); } return { connected, connectedTo, error }; diff --git a/src/store/reducers/network.ts b/src/store/reducers/network.ts index f9ec5bb925..19e9e174d9 100644 --- a/src/store/reducers/network.ts +++ b/src/store/reducers/network.ts @@ -5,12 +5,12 @@ const initialState: { exchangeRates: ExchangeRates; averageTxFees: AverageTxFeesByNetwork; initialNodesSaved: Boolean; - testnetFallbackNodeAdded: boolean; + testnetFallbackNodeAddedByAppId: { [appId: string]: boolean }; } = { exchangeRates: null, averageTxFees: null, initialNodesSaved: false, - testnetFallbackNodeAdded: false, + testnetFallbackNodeAddedByAppId: {}, }; const networkSlice = createSlice({ @@ -29,8 +29,10 @@ const networkSlice = createSlice({ state.initialNodesSaved = action.payload; }, - setTestnetFallbackNodeAdded: (state) => { - state.testnetFallbackNodeAdded = true; + setTestnetFallbackNodeAdded: (state, action: PayloadAction) => { + // Older persisted network state may not have the per-account map yet. + if (!state.testnetFallbackNodeAddedByAppId) state.testnetFallbackNodeAddedByAppId = {}; + state.testnetFallbackNodeAddedByAppId[action.payload] = true; }, }, }); diff --git a/src/store/sagas/network.ts b/src/store/sagas/network.ts index f833fd9a82..bc3ae3bf45 100644 --- a/src/store/sagas/network.ts +++ b/src/store/sagas/network.ts @@ -9,7 +9,7 @@ import { import ElectrumClient from 'src/services/electrum/client'; import { captureError } from 'src/services/sentry'; import { setInitialNodesSaved, setTestnetFallbackNodeAdded } from '../reducers/network'; -import { RootState } from '../store'; +import { RootState, store } from '../store'; import { electrumClientConnectionExecuted, electrumClientConnectionInitiated, @@ -18,9 +18,25 @@ import { createWatcher } from '../utilities'; import { fetchFeeRates } from '../sagaActions/send_and_receive'; import { CONNECT_TO_NODE } from '../sagaActions/network'; +let latestConnectWorker = 0; + export function* connectToNodeWorker() { + const workerId = ++latestConnectWorker; + let appId: string; + let bitcoinNetworkType: NetworkType; + let responseGeneration: number; + const contextIsCurrent = () => { + const current = store.getState(); + return ( + workerId === latestConnectWorker && + current.storage.appId === appId && + current.settings.bitcoinNetworkType === bitcoinNetworkType + ); + }; try { - const { bitcoinNetworkType } = yield select((state: RootState) => state.settings); + bitcoinNetworkType = yield select((state: RootState) => state.settings.bitcoinNetworkType); + appId = yield select((state: RootState) => state.storage.appId); + if (!contextIsCurrent()) return; console.log('Connecting to node...'); yield put(electrumClientConnectionInitiated()); @@ -31,6 +47,7 @@ export function* connectToNodeWorker() { if (!areInitialNodesSaved) { const currentNodes = yield call(dbManager.getCollection, RealmSchema.NodeConnect); const defaultNodes = yield call(dbManager.getCollection, RealmSchema.DefaultNodeConnect); + if (!contextIsCurrent()) return; let addInitialNode = defaultNodes && defaultNodes.length != 0; if (!addInitialNode && currentNodes.length == 0) { addInitialNode = true; @@ -46,9 +63,11 @@ export function* connectToNodeWorker() { RealmSchema.NodeConnect, hardcodedInitialNodes ); + if (!contextIsCurrent()) return; if (!created) throw new Error('Unable to save initial Electrum servers'); } + if (!contextIsCurrent()) return; yield put(setInitialNodesSaved(true)); } @@ -56,10 +75,11 @@ export function* connectToNodeWorker() { // second once when that original server is still saved; an explicit deletion // after this migration must not be undone at every reconnect. const fallbackNodeAdded = yield select( - (state: RootState) => state.network.testnetFallbackNodeAdded + (state: RootState) => state.network.testnetFallbackNodeAddedByAppId?.[appId] ); - if (bitcoinNetworkType === NetworkType.TESTNET && !fallbackNodeAdded) { + if (bitcoinNetworkType === NetworkType.TESTNET && appId && !fallbackNodeAdded) { const savedNodes = yield call(dbManager.getCollection, RealmSchema.NodeConnect); + if (!contextIsCurrent()) return; const originalNode = predefinedTestnetNodes[0]; const fallbackNode = predefinedTestnetNodes[1]; if ( @@ -72,21 +92,32 @@ export function* connectToNodeWorker() { !savedNodes.some((node) => node.id === fallbackNode.id) ) { const created = yield call(dbManager.createObject, RealmSchema.NodeConnect, fallbackNode); + if (!contextIsCurrent()) return; if (!created) throw new Error('Unable to save Testnet fallback server'); } - yield put(setTestnetFallbackNodeAdded()); + if (!contextIsCurrent()) return; + yield put(setTestnetFallbackNodeAdded(appId)); } const nodes = (yield call(dbManager.getCollection, RealmSchema.NodeConnect)).filter( (node) => node.networkType === bitcoinNetworkType ); + if (!contextIsCurrent()) return; const selectedNode = nodes.find((node) => node.isConnected); ElectrumClient.setActivePeer(nodes); - const { connected, connectedTo, error } = yield call(ElectrumClient.connect); + const { connected, connectedTo, error, generation } = yield call(ElectrumClient.connect); + responseGeneration = generation; + const connectionIsCurrent = () => { + return contextIsCurrent() && generation === ElectrumClient.getConnectionGeneration(); + }; + // Another login/network request may replace this connection while the worker + // is waiting. Its result must not overwrite the newer connection's status. + if (!connectionIsCurrent()) return; if (connected) { const connectedNode = ElectrumClient.getActivePeer(); if (selectedNode && connectedNode?.id !== selectedNode.id) { + if (!connectionIsCurrent()) return; yield call( dbManager.updateObjectById, RealmSchema.NodeConnect, @@ -95,6 +126,7 @@ export function* connectToNodeWorker() { isConnected: false, } ); + if (!connectionIsCurrent()) return; yield call( dbManager.updateObjectById, RealmSchema.NodeConnect, @@ -104,12 +136,21 @@ export function* connectToNodeWorker() { } ); } + if (!connectionIsCurrent()) return; yield put(electrumClientConnectionExecuted({ successful: connected, connectedTo })); } else { + if (!connectionIsCurrent()) return; yield put(electrumClientConnectionExecuted({ successful: connected, error })); } + if (!connectionIsCurrent()) return; yield put(fetchFeeRates()); } catch (err) { + if ( + !contextIsCurrent() || + (responseGeneration !== undefined && + responseGeneration !== ElectrumClient.getConnectionGeneration()) + ) + return; captureError(err); yield put( electrumClientConnectionExecuted({ diff --git a/tests/services/electrumConnectionGeneration.test.ts b/tests/services/electrumConnectionGeneration.test.ts new file mode 100644 index 0000000000..c23ba711be --- /dev/null +++ b/tests/services/electrumConnectionGeneration.test.ts @@ -0,0 +1,189 @@ +import * as bitcoinJS from 'bitcoinjs-lib'; +import ElectrumClient, { + ELECTRUM_CONNECTION_CHANGED_ERR, +} from '../../src/services/electrum/client'; +import { + predefinedMainnetNodes, + predefinedTestnetNodes, +} from '../../src/services/electrum/predefinedNodes'; +import { NetworkType } from '../../src/services/wallets/enums'; +import dbManager from '../../src/storage/realm/dbManager'; +import { RealmSchema } from '../../src/storage/realm/enum'; +import { store } from '../../src/store/store'; + +const mockClients: any[] = []; +const mockInitReplies: Array<() => Promise> = []; +const mockState = { + storage: { appId: 'account-a' }, + settings: { + bitcoinNetworkType: NetworkType.TESTNET, + bitcoinNetwork: bitcoinJS.networks.testnet, + }, +}; + +jest.mock('electrum-client', () => ({ + __esModule: true, + default: jest.fn().mockImplementation(() => { + const client = { + initElectrum: jest.fn( + () => mockInitReplies.shift()?.() || Promise.resolve(['server', '1.4']) + ), + close: jest.fn(), + onError: null, + blockchainScripthash_listunspentBatch: jest.fn(), + blockchainScripthash_getHistoryBatch: jest.fn(), + blockchainTransaction_getBatch: jest.fn(), + }; + mockClients.push(client); + return client; + }), +})); +jest.mock('src/store/store', () => ({ + store: { getState: jest.fn(() => mockState), dispatch: jest.fn() }, +})); +jest.mock('src/storage/realm/dbManager', () => ({ + __esModule: true, + default: { updateObjectById: jest.fn(() => true) }, +})); +jest.mock('src/services/rest/RestClient', () => ({ + __esModule: true, + default: { getTorStatus: jest.fn() }, + TorStatus: { CONNECTED: 'connected' }, +})); +jest.mock('src/services/electrum/torrific', () => ({ __esModule: true, default: {} })); + +const deferred = () => { + let resolve: (value: any) => void; + const promise = new Promise((done) => { + resolve = done; + }); + return { promise, resolve }; +}; + +const testnetNode = () => ({ ...predefinedTestnetNodes[0], isConnected: false }); +const mainnetNode = () => ({ ...predefinedMainnetNodes[0], isConnected: false }); + +describe('Electrum connection generation', () => { + beforeEach(() => { + mockState.storage.appId = 'account-a'; + mockState.settings.bitcoinNetworkType = NetworkType.TESTNET; + mockState.settings.bitcoinNetwork = bitcoinJS.networks.testnet; + mockClients.length = 0; + mockInitReplies.length = 0; + jest.clearAllMocks(); + }); + + afterEach(() => jest.useRealTimers()); + + it('ignores a late handshake and error callback from a replaced account connection', async () => { + const firstHandshake = deferred(); + mockInitReplies.push( + () => firstHandshake.promise, + () => Promise.resolve(['server', '1.4']) + ); + ElectrumClient.setActivePeer([testnetNode()], testnetNode()); + const firstConnect = ElectrumClient.connect(); + const firstClient = mockClients[0]; + + mockState.storage.appId = 'account-b'; + ElectrumClient.setActivePeer([testnetNode()], testnetNode()); + const currentResult = await ElectrumClient.connect(); + const currentGeneration = ElectrumClient.getConnectionGeneration(); + + firstHandshake.resolve(['server', '1.4']); + await expect(firstConnect).resolves.toMatchObject({ + connected: false, + error: ELECTRUM_CONNECTION_CHANGED_ERR, + }); + firstClient.onError(new Error('old socket closed')); + + expect(currentResult.connected).toBe(true); + expect(ElectrumClient.getConnectionGeneration()).toBe(currentGeneration); + expect(mockClients).toHaveLength(2); + expect(store.dispatch as jest.Mock).not.toHaveBeenCalled(); + }); + + it('stops remaining UTXO batches if the selected network changes mid-request', async () => { + const selected = testnetNode(); + ElectrumClient.setActivePeer([selected], selected); + await ElectrumClient.connect(); + const pendingBatch = deferred(); + mockClients[0].blockchainScripthash_listunspentBatch.mockReturnValue(pendingBatch.promise); + const address = 'tb1qd2u9tvuqzadgeh02vppd33e7u2fatuwrw7h4q5'; + const pendingSync = ElectrumClient.syncUTXOByAddress( + [address, address], + bitcoinJS.networks.testnet, + 1 + ); + + mockState.settings.bitcoinNetworkType = NetworkType.MAINNET; + mockState.settings.bitcoinNetwork = bitcoinJS.networks.bitcoin; + const next = mainnetNode(); + ElectrumClient.setActivePeer([next], next); + pendingBatch.resolve([]); + + await expect(pendingSync).rejects.toThrow(ELECTRUM_CONNECTION_CHANGED_ERR); + expect(mockClients[0].blockchainScripthash_listunspentBatch).toHaveBeenCalledTimes(1); + }); + + it('rejects a Bitcoin network that differs from the active peer before sending', async () => { + const selected = testnetNode(); + ElectrumClient.setActivePeer([selected], selected); + await ElectrumClient.connect(); + + await expect( + ElectrumClient.syncUTXOByAddress(['unused'], bitcoinJS.networks.bitcoin) + ).rejects.toThrow(ELECTRUM_CONNECTION_CHANGED_ERR); + expect(mockClients[0].blockchainScripthash_listunspentBatch).not.toHaveBeenCalled(); + }); + + it('does not fetch another transaction chunk through a replaced connection', async () => { + const selected = testnetNode(); + ElectrumClient.setActivePeer([selected], selected); + await ElectrumClient.connect(); + const pendingBatch = deferred(); + mockClients[0].blockchainTransaction_getBatch.mockReturnValue(pendingBatch.promise); + const pendingFetch = ElectrumClient.getTransactionsById(['first', 'second'], true, 1); + + mockState.storage.appId = 'account-b'; + ElectrumClient.setActivePeer([testnetNode()], testnetNode()); + pendingBatch.resolve([]); + + await expect(pendingFetch).rejects.toThrow(ELECTRUM_CONNECTION_CHANGED_ERR); + expect(mockClients[0].blockchainTransaction_getBatch).toHaveBeenCalledTimes(1); + }); + + it('persists live public rotation and publishes the active server', async () => { + jest.useFakeTimers(); + const first = testnetNode(); + const fallback = { ...predefinedTestnetNodes[1], isConnected: false }; + mockInitReplies.push( + () => Promise.resolve(['server', '1.4']), + () => Promise.reject(new Error('first server unavailable')), + () => Promise.resolve(['server', '1.4']) + ); + ElectrumClient.setActivePeer([first, fallback], first); + await ElectrumClient.connect(); + + const reconnect = mockClients[0].onError(new Error('socket closed')); + await jest.advanceTimersByTimeAsync(500); + await reconnect; + + expect(ElectrumClient.getActivePeer()?.id).toBe(fallback.id); + expect(dbManager.updateObjectById).toHaveBeenCalledWith( + RealmSchema.NodeConnect, + first.id.toString(), + { isConnected: false } + ); + expect(dbManager.updateObjectById).toHaveBeenCalledWith( + RealmSchema.NodeConnect, + fallback.id.toString(), + { isConnected: true } + ); + expect(store.dispatch).toHaveBeenCalledWith( + expect.objectContaining({ + payload: { successful: true, connectedTo: fallback.host }, + }) + ); + }); +}); diff --git a/tests/services/manualNodeFailover.test.ts b/tests/services/manualNodeFailover.test.ts index 4ed670acc0..d6dabc3cbd 100644 --- a/tests/services/manualNodeFailover.test.ts +++ b/tests/services/manualNodeFailover.test.ts @@ -29,12 +29,13 @@ describe('manual Electrum selection', () => { (ElectrumClient.connect as jest.Mock).mockResolvedValue({ connected: true, connectedTo: selected.host, + generation: 2, }); await Node.connectToSelectedNode(selected); expect(ElectrumClient.setActivePeer).toHaveBeenCalledWith([], selected); - expect(ElectrumClient.setFailoverPeers).toHaveBeenCalledWith(predefinedTestnetNodes); + expect(ElectrumClient.setFailoverPeers).toHaveBeenCalledWith(predefinedTestnetNodes, 2); }); it('does not equip fallback peers when the tapped node never connects', async () => { diff --git a/tests/services/networkConnectRace.test.ts b/tests/services/networkConnectRace.test.ts new file mode 100644 index 0000000000..4885b6ecc2 --- /dev/null +++ b/tests/services/networkConnectRace.test.ts @@ -0,0 +1,117 @@ +import { runSaga } from 'redux-saga'; +import ElectrumClient from '../../src/services/electrum/client'; +import { predefinedTestnetNodes } from '../../src/services/electrum/predefinedNodes'; +import { NetworkType } from '../../src/services/wallets/enums'; +import dbManager from '../../src/storage/realm/dbManager'; +import { RealmSchema } from '../../src/storage/realm/enum'; +import { connectToNodeWorker } from '../../src/store/sagas/network'; + +let mockGeneration = 0; +const mockNodes = predefinedTestnetNodes; +const mockState = { + settings: { bitcoinNetworkType: NetworkType.TESTNET }, + storage: { appId: 'account-a' }, + network: { + initialNodesSaved: true, + testnetFallbackNodeAddedByAppId: { 'account-a': true }, + }, +}; + +jest.mock('src/store/store', () => ({ store: { getState: jest.fn(() => mockState) } })); +jest.mock('src/storage/realm/dbManager', () => ({ + __esModule: true, + default: { getCollection: jest.fn(() => mockNodes), createObject: jest.fn(() => true) }, +})); +jest.mock('src/services/electrum/client', () => ({ + __esModule: true, + default: { + setActivePeer: jest.fn(() => { + mockGeneration += 1; + }), + connect: jest.fn(), + getConnectionGeneration: jest.fn(() => mockGeneration), + getActivePeer: jest.fn(() => mockNodes[0]), + }, +})); +jest.mock('src/services/sentry', () => ({ captureError: jest.fn() })); + +const deferred = () => { + let resolve: (value: any) => void; + const promise = new Promise((done) => { + resolve = done; + }); + return { promise, resolve }; +}; + +describe('overlapping Electrum connection workers', () => { + beforeEach(() => { + mockGeneration = 0; + mockState.storage.appId = 'account-a'; + jest.clearAllMocks(); + (ElectrumClient.connect as jest.Mock).mockReset(); + (dbManager.getCollection as jest.Mock).mockReturnValue(mockNodes); + }); + + it('does not publish a late result from the older worker', async () => { + const first = deferred(); + const second = deferred(); + (ElectrumClient.connect as jest.Mock) + .mockImplementationOnce(() => { + mockGeneration += 1; + return first.promise; + }) + .mockImplementationOnce(() => { + mockGeneration += 1; + return second.promise; + }); + const actions: any[] = []; + const environment = { + dispatch: (action) => actions.push(action), + getState: () => mockState, + }; + + const firstTask = runSaga(environment, connectToNodeWorker); + const secondTask = runSaga(environment, connectToNodeWorker); + expect(ElectrumClient.connect).toHaveBeenCalledTimes(2); + + second.resolve({ connected: true, connectedTo: 'winner', generation: mockGeneration }); + await secondTask.toPromise(); + first.resolve({ connected: false, error: 'old failure', generation: mockGeneration - 2 }); + await firstTask.toPromise(); + + const connectionResults = actions.filter( + (action) => action.type === 'login/electrumClientConnectionExecuted' + ); + expect(connectionResults).toEqual([ + expect.objectContaining({ payload: { successful: true, connectedTo: 'winner' } }), + ]); + expect(dbManager.getCollection).toHaveBeenCalled(); + }); + + it('adds the Testnet fallback for a second account despite the first account marker', async () => { + mockState.storage.appId = 'account-b'; + (dbManager.getCollection as jest.Mock).mockReturnValue([mockNodes[0]]); + (ElectrumClient.connect as jest.Mock).mockImplementation(() => { + mockGeneration += 1; + return Promise.resolve({ + connected: true, + connectedTo: mockNodes[0].host, + generation: mockGeneration, + }); + }); + const actions: any[] = []; + + await runSaga( + { dispatch: (action) => actions.push(action), getState: () => mockState }, + connectToNodeWorker + ).toPromise(); + + expect(dbManager.createObject).toHaveBeenCalledWith(RealmSchema.NodeConnect, mockNodes[1]); + expect(actions).toContainEqual( + expect.objectContaining({ + type: 'network/setTestnetFallbackNodeAdded', + payload: 'account-b', + }) + ); + }); +}); diff --git a/tests/services/testnetFallbackMigration.test.ts b/tests/services/testnetFallbackMigration.test.ts new file mode 100644 index 0000000000..e82a9ac446 --- /dev/null +++ b/tests/services/testnetFallbackMigration.test.ts @@ -0,0 +1,15 @@ +import networkReducer, { setTestnetFallbackNodeAdded } from '../../src/store/reducers/network'; + +describe('Testnet fallback migration', () => { + it('tracks each account separately so a second Realm receives its own fallback', () => { + const firstAccount = networkReducer(undefined, setTestnetFallbackNodeAdded('account-a')); + expect(firstAccount.testnetFallbackNodeAddedByAppId['account-a']).toBe(true); + expect(firstAccount.testnetFallbackNodeAddedByAppId['account-b']).toBeUndefined(); + + const secondAccount = networkReducer(firstAccount, setTestnetFallbackNodeAdded('account-b')); + expect(secondAccount.testnetFallbackNodeAddedByAppId).toEqual({ + 'account-a': true, + 'account-b': true, + }); + }); +}); From 0113fd5f23c8d533e24f148477b137d9ddc95641 Mon Sep 17 00:00:00 2001 From: Anant Date: Thu, 8 Oct 2026 18:14:27 +0530 Subject: [PATCH 075/254] test(sync): cover Realm handover during refresh --- tests/recovery/backupFreshness.test.cjs | 26 ++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/tests/recovery/backupFreshness.test.cjs b/tests/recovery/backupFreshness.test.cjs index 416dc57fb6..fb6867c5c2 100644 --- a/tests/recovery/backupFreshness.test.cjs +++ b/tests/recovery/backupFreshness.test.cjs @@ -47,7 +47,20 @@ function refreshFixture(kind, rejectWrite = false) { }; f.collections[kind].push(original); f.scope.getJSONFromRealmObject = (value) => JSON.parse(JSON.stringify(value)); - f.scope.ELECTRUM_CLIENT = { isClientConnected: true }; + f.scope.WALLET_SYNC_SCOPE_CHANGED = 'WALLET_SYNC_SCOPE_CHANGED'; + f.scope.store = { getState: () => f.state }; + f.scope.ELECTRUM_CLIENT = { + isClientConnected: true, + activePeer: { networkType: NetworkType.MAINNET }, + }; + f.scope.ElectrumClient = { + getConnectionGeneration: () => 1, + assertConnectionGeneration: (_generation, networkType) => { + if (networkType !== f.scope.ELECTRUM_CLIENT.activePeer.networkType) { + throw new Error('Electrum network changed'); + } + }, + }; f.scope.WalletUtilities.getNetworkByType = () => ({}); f.scope.WalletOperations = { syncWalletsViaElectrumClient: async () => { const synced = f.scope.getJSONFromRealmObject(original); @@ -125,6 +138,17 @@ test('successful pull refresh completes only after refreshed specs are persisted assert.equal(finished?.succeeded, true); }); +test('refresh does not read or write another Realm during account handover', async () => { + const f = refreshFixture('Wallet'); + f.app.id = 'new-profile'; // Realm B opened while Redux still names account A. + assert.equal(await f.run('refreshWalletsWorker', { + payload: { wallets: f.collections.Wallet, options: { hardRefresh: true } }, + }), false); + assert.deepEqual(f.writes, []); + assert.equal(f.collections.Wallet[0].specs.receivingAddress, 'before-refresh'); + assert.deepEqual(f.errors, []); +}); + test('login backup inspection waits for wallet refresh to finish', async () => { let finishSync; const syncGate = new Promise((resolve) => { finishSync = resolve; }); From ffd459490d0d65f88503166d4ad97b7d0a8e2999 Mon Sep 17 00:00:00 2001 From: Anant Date: Thu, 8 Oct 2026 18:14:41 +0530 Subject: [PATCH 076/254] fix(sync): treat closed Realm as stale scope --- src/store/sagas/wallets.ts | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/store/sagas/wallets.ts b/src/store/sagas/wallets.ts index 832339e2c4..cf6309f92f 100644 --- a/src/store/sagas/wallets.ts +++ b/src/store/sagas/wallets.ts @@ -650,7 +650,12 @@ function* refreshWalletsWorker({ })); const assertAccountRealmScope = () => { const currentState = store.getState(); - const keeperApp = dbManager.getObjectByIndex(RealmSchema.KeeperApp) as unknown as KeeperApp; + let keeperApp: KeeperApp; + try { + keeperApp = dbManager.getObjectByIndex(RealmSchema.KeeperApp) as unknown as KeeperApp; + } catch { + throw new Error(WALLET_SYNC_SCOPE_CHANGED); + } if ( currentState.storage.appId !== refreshScope.appId || currentState.settings.bitcoinNetworkType !== refreshScope.networkType || @@ -1208,13 +1213,17 @@ export function* testcoinsWorker({ payload }) { })); const isCurrentFaucetScope = () => { const state = store.getState(); - const keeperApp = dbManager.getObjectByIndex(RealmSchema.KeeperApp) as unknown as KeeperApp; - return ( - state.storage.appId === faucetScope.appId && - state.settings.bitcoinNetworkType === faucetScope.networkType && - faucetScope.networkType === wallet.networkType && - keeperApp?.id === faucetScope.appId - ); + try { + const keeperApp = dbManager.getObjectByIndex(RealmSchema.KeeperApp) as unknown as KeeperApp; + return ( + state.storage.appId === faucetScope.appId && + state.settings.bitcoinNetworkType === faucetScope.networkType && + faucetScope.networkType === wallet.networkType && + keeperApp?.id === faucetScope.appId + ); + } catch { + return false; + } }; if (!isCurrentFaucetScope()) return; From 615c1103a43a29d28bfa2363a1ee1ff10bff0019 Mon Sep 17 00:00:00 2001 From: Anant Date: Thu, 8 Oct 2026 18:16:23 +0530 Subject: [PATCH 077/254] fix(network): fence Electrum work during Realm account handover --- src/services/electrum/client.ts | 17 +++++++- src/store/sagas/network.ts | 17 ++++++++ .../electrumConnectionGeneration.test.ts | 32 ++++++++++++++- tests/services/networkConnectRace.test.ts | 39 ++++++++++++++++++- 4 files changed, 102 insertions(+), 3 deletions(-) diff --git a/src/services/electrum/client.ts b/src/services/electrum/client.ts index 1c4b80f64e..624e858514 100644 --- a/src/services/electrum/client.ts +++ b/src/services/electrum/client.ts @@ -62,8 +62,20 @@ const staleConnectionResult = () => ({ const selectionMatches = (state: ElectrumClientState): boolean => { const current = store.getState(); const selectedNetwork = current.settings?.bitcoinNetworkType; + let realmMatches = true; + if (state.appId) { + try { + const keeper = dbManager.getObjectByIndex(RealmSchema.KeeperApp) as unknown as { + id?: string; + }; + realmMatches = keeper?.id === state.appId; + } catch (_) { + realmMatches = false; + } + } return ( state.appId === (current.storage?.appId ?? null) && + realmMatches && (!selectedNetwork || !state.activePeer || state.activePeer.networkType === selectedNetwork) ); }; @@ -267,9 +279,10 @@ export default class ElectrumClient { connectedPeer: NodeDetail, generation: number ) { - if (!stateIsCurrent(state, generation)) return; + if (!state.appId || !stateIsCurrent(state, generation)) return; if (previousPeer.id !== connectedPeer.id) { try { + if (!stateIsCurrent(state, generation)) return; const previousSaved = dbManager.updateObjectById( RealmSchema.NodeConnect, previousPeer.id.toString(), @@ -277,6 +290,7 @@ export default class ElectrumClient { isConnected: false, } ); + if (!stateIsCurrent(state, generation)) return; const connectedSaved = dbManager.updateObjectById( RealmSchema.NodeConnect, connectedPeer.id.toString(), @@ -291,6 +305,7 @@ export default class ElectrumClient { console.warn('Unable to save active Electrum server after failover', error); } } + if (!stateIsCurrent(state, generation)) return; store.dispatch( electrumClientConnectionExecuted({ successful: true, connectedTo: connectedPeer.host }) ); diff --git a/src/store/sagas/network.ts b/src/store/sagas/network.ts index bc3ae3bf45..7e5788be8a 100644 --- a/src/store/sagas/network.ts +++ b/src/store/sagas/network.ts @@ -27,8 +27,19 @@ export function* connectToNodeWorker() { let responseGeneration: number; const contextIsCurrent = () => { const current = store.getState(); + let realmAppId: string; + try { + const keeper = dbManager.getObjectByIndex(RealmSchema.KeeperApp) as unknown as { + id?: string; + }; + realmAppId = keeper?.id; + } catch (_) { + return false; + } return ( workerId === latestConnectWorker && + !!appId && + realmAppId === appId && current.storage.appId === appId && current.settings.bitcoinNetworkType === bitcoinNetworkType ); @@ -45,7 +56,9 @@ export function* connectToNodeWorker() { ); if (!areInitialNodesSaved) { + if (!contextIsCurrent()) return; const currentNodes = yield call(dbManager.getCollection, RealmSchema.NodeConnect); + if (!contextIsCurrent()) return; const defaultNodes = yield call(dbManager.getCollection, RealmSchema.DefaultNodeConnect); if (!contextIsCurrent()) return; let addInitialNode = defaultNodes && defaultNodes.length != 0; @@ -58,6 +71,7 @@ export function* connectToNodeWorker() { bitcoinNetworkType === NetworkType.TESTNET ? predefinedTestnetNodes : predefinedMainnetNodes; + if (!contextIsCurrent()) return; const created = yield call( dbManager.createObjectBulk, RealmSchema.NodeConnect, @@ -78,6 +92,7 @@ export function* connectToNodeWorker() { (state: RootState) => state.network.testnetFallbackNodeAddedByAppId?.[appId] ); if (bitcoinNetworkType === NetworkType.TESTNET && appId && !fallbackNodeAdded) { + if (!contextIsCurrent()) return; const savedNodes = yield call(dbManager.getCollection, RealmSchema.NodeConnect); if (!contextIsCurrent()) return; const originalNode = predefinedTestnetNodes[0]; @@ -91,6 +106,7 @@ export function* connectToNodeWorker() { ) && !savedNodes.some((node) => node.id === fallbackNode.id) ) { + if (!contextIsCurrent()) return; const created = yield call(dbManager.createObject, RealmSchema.NodeConnect, fallbackNode); if (!contextIsCurrent()) return; if (!created) throw new Error('Unable to save Testnet fallback server'); @@ -99,6 +115,7 @@ export function* connectToNodeWorker() { yield put(setTestnetFallbackNodeAdded(appId)); } + if (!contextIsCurrent()) return; const nodes = (yield call(dbManager.getCollection, RealmSchema.NodeConnect)).filter( (node) => node.networkType === bitcoinNetworkType ); diff --git a/tests/services/electrumConnectionGeneration.test.ts b/tests/services/electrumConnectionGeneration.test.ts index c23ba711be..ba7ce5c351 100644 --- a/tests/services/electrumConnectionGeneration.test.ts +++ b/tests/services/electrumConnectionGeneration.test.ts @@ -13,6 +13,7 @@ import { store } from '../../src/store/store'; const mockClients: any[] = []; const mockInitReplies: Array<() => Promise> = []; +let mockRealmAppId = 'account-a'; const mockState = { storage: { appId: 'account-a' }, settings: { @@ -43,7 +44,10 @@ jest.mock('src/store/store', () => ({ })); jest.mock('src/storage/realm/dbManager', () => ({ __esModule: true, - default: { updateObjectById: jest.fn(() => true) }, + default: { + getObjectByIndex: jest.fn(() => ({ id: mockRealmAppId })), + updateObjectById: jest.fn(() => true), + }, })); jest.mock('src/services/rest/RestClient', () => ({ __esModule: true, @@ -65,6 +69,7 @@ const mainnetNode = () => ({ ...predefinedMainnetNodes[0], isConnected: false }) describe('Electrum connection generation', () => { beforeEach(() => { + mockRealmAppId = 'account-a'; mockState.storage.appId = 'account-a'; mockState.settings.bitcoinNetworkType = NetworkType.TESTNET; mockState.settings.bitcoinNetwork = bitcoinJS.networks.testnet; @@ -86,6 +91,7 @@ describe('Electrum connection generation', () => { const firstClient = mockClients[0]; mockState.storage.appId = 'account-b'; + mockRealmAppId = 'account-b'; ElectrumClient.setActivePeer([testnetNode()], testnetNode()); const currentResult = await ElectrumClient.connect(); const currentGeneration = ElectrumClient.getConnectionGeneration(); @@ -146,6 +152,7 @@ describe('Electrum connection generation', () => { const pendingFetch = ElectrumClient.getTransactionsById(['first', 'second'], true, 1); mockState.storage.appId = 'account-b'; + mockRealmAppId = 'account-b'; ElectrumClient.setActivePeer([testnetNode()], testnetNode()); pendingBatch.resolve([]); @@ -186,4 +193,27 @@ describe('Electrum connection generation', () => { }) ); }); + + it('stops reconnect when Realm switches accounts before Redux catches up', async () => { + jest.useFakeTimers(); + const selected = testnetNode(); + ElectrumClient.setActivePeer([selected], selected); + await ElectrumClient.connect(); + const generation = ElectrumClient.getConnectionGeneration(); + const reconnect = mockClients[0].onError(new Error('socket closed')); + + mockRealmAppId = 'account-b'; + expect(() => ElectrumClient.assertConnectionGeneration(generation)).toThrow( + ELECTRUM_CONNECTION_CHANGED_ERR + ); + await jest.advanceTimersByTimeAsync(500); + await expect(reconnect).resolves.toMatchObject({ + connected: false, + error: ELECTRUM_CONNECTION_CHANGED_ERR, + }); + + expect(mockClients).toHaveLength(1); + expect(dbManager.updateObjectById).not.toHaveBeenCalled(); + expect(store.dispatch).not.toHaveBeenCalled(); + }); }); diff --git a/tests/services/networkConnectRace.test.ts b/tests/services/networkConnectRace.test.ts index 4885b6ecc2..d21c012606 100644 --- a/tests/services/networkConnectRace.test.ts +++ b/tests/services/networkConnectRace.test.ts @@ -7,6 +7,7 @@ import { RealmSchema } from '../../src/storage/realm/enum'; import { connectToNodeWorker } from '../../src/store/sagas/network'; let mockGeneration = 0; +let mockRealmAppId = 'account-a'; const mockNodes = predefinedTestnetNodes; const mockState = { settings: { bitcoinNetworkType: NetworkType.TESTNET }, @@ -20,7 +21,12 @@ const mockState = { jest.mock('src/store/store', () => ({ store: { getState: jest.fn(() => mockState) } })); jest.mock('src/storage/realm/dbManager', () => ({ __esModule: true, - default: { getCollection: jest.fn(() => mockNodes), createObject: jest.fn(() => true) }, + default: { + getObjectByIndex: jest.fn(() => ({ id: mockRealmAppId })), + getCollection: jest.fn(() => mockNodes), + createObject: jest.fn(() => true), + updateObjectById: jest.fn(() => true), + }, })); jest.mock('src/services/electrum/client', () => ({ __esModule: true, @@ -46,6 +52,7 @@ const deferred = () => { describe('overlapping Electrum connection workers', () => { beforeEach(() => { mockGeneration = 0; + mockRealmAppId = 'account-a'; mockState.storage.appId = 'account-a'; jest.clearAllMocks(); (ElectrumClient.connect as jest.Mock).mockReset(); @@ -90,6 +97,7 @@ describe('overlapping Electrum connection workers', () => { it('adds the Testnet fallback for a second account despite the first account marker', async () => { mockState.storage.appId = 'account-b'; + mockRealmAppId = 'account-b'; (dbManager.getCollection as jest.Mock).mockReturnValue([mockNodes[0]]); (ElectrumClient.connect as jest.Mock).mockImplementation(() => { mockGeneration += 1; @@ -114,4 +122,33 @@ describe('overlapping Electrum connection workers', () => { }) ); }); + + it('does not write node flags into the newly opened Realm while Redux still names the old app', async () => { + const pendingConnection = deferred(); + (ElectrumClient.connect as jest.Mock).mockImplementation(() => { + mockGeneration += 1; + return pendingConnection.promise; + }); + (ElectrumClient.getActivePeer as jest.Mock).mockReturnValue(mockNodes[1]); + const actions: any[] = []; + const task = runSaga( + { dispatch: (action) => actions.push(action), getState: () => mockState }, + connectToNodeWorker + ); + expect(ElectrumClient.connect).toHaveBeenCalledTimes(1); + + mockRealmAppId = 'account-b'; + pendingConnection.resolve({ + connected: true, + connectedTo: mockNodes[1].host, + generation: mockGeneration, + }); + await task.toPromise(); + + expect(mockState.storage.appId).toBe('account-a'); + expect(dbManager.updateObjectById).not.toHaveBeenCalled(); + expect( + actions.filter((action) => action.type === 'login/electrumClientConnectionExecuted') + ).toEqual([]); + }); }); From 024006e1d848558e096a6ab8589de7bcb02b3735 Mon Sep 17 00:00:00 2001 From: Anant Date: Thu, 8 Oct 2026 18:18:05 +0530 Subject: [PATCH 078/254] fix(sync): report failed node connection on pull refresh --- src/store/sagas/wallets.ts | 5 ++++- tests/recovery/backupFreshness.test.cjs | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/store/sagas/wallets.ts b/src/store/sagas/wallets.ts index cf6309f92f..a11a4bdcda 100644 --- a/src/store/sagas/wallets.ts +++ b/src/store/sagas/wallets.ts @@ -715,13 +715,16 @@ function* refreshWalletsWorker({ yield call(connectToNodeWorker); } + assertAccountRealmScope(); + if (!ELECTRUM_CLIENT.isClientConnected) throw new Error(ELECTRUM_NOT_CONNECTED_ERR); + const connectionGeneration = ElectrumClient.getConnectionGeneration(); const assertCurrentScope = () => { assertAccountRealmScope(); try { ElectrumClient.assertConnectionGeneration(connectionGeneration, refreshScope.networkType); } catch { - throw new Error(WALLET_SYNC_SCOPE_CHANGED); + throw new Error(ELECTRUM_NOT_CONNECTED_ERR); } }; assertCurrentScope(); diff --git a/tests/recovery/backupFreshness.test.cjs b/tests/recovery/backupFreshness.test.cjs index fb6867c5c2..2f5a32c5c0 100644 --- a/tests/recovery/backupFreshness.test.cjs +++ b/tests/recovery/backupFreshness.test.cjs @@ -149,6 +149,26 @@ test('refresh does not read or write another Realm during account handover', asy assert.deepEqual(f.errors, []); }); +test('manual refresh reports a disconnected current node and ends its request', async () => { + const f = refreshFixture('Wallet'); + f.scope.ELECTRUM_CLIENT.isClientConnected = false; + f.scope.ElectrumClient.resetCurrentPeerIndex = () => {}; + f.scope.connectToNodeWorker = () => {}; + assert.equal(await f.run('refreshWalletsWorker', { + payload: { + wallets: f.collections.Wallet, + options: { hardRefresh: true }, + requestId: 'disconnected-pull', + }, + }), false); + assert.deepEqual(f.writes, []); + assert.ok(f.actions.some((action) => + action.type === 'setElectrumNotConnectedErr' && action.payload.includes('Network error'))); + const finished = f.actions.find((action) => action.type === 'finishRefreshRequest')?.payload; + assert.equal(finished?.requestId, 'disconnected-pull'); + assert.equal(finished?.succeeded, false); +}); + test('login backup inspection waits for wallet refresh to finish', async () => { let finishSync; const syncGate = new Promise((resolve) => { finishSync = resolve; }); From 9c8a0119a29d64c7704102589e685f9231ee8512 Mon Sep 17 00:00:00 2001 From: Anant Date: Thu, 8 Oct 2026 18:19:01 +0530 Subject: [PATCH 079/254] perf(wallets): reuse Realm list snapshots across renders --- src/hooks/useVault.ts | 33 ++++-- src/hooks/useWallets.tsx | 49 +++++--- tests/hooks/walletCollectionMemo.test.tsx | 138 ++++++++++++++++++++++ 3 files changed, 190 insertions(+), 30 deletions(-) create mode 100644 tests/hooks/walletCollectionMemo.test.tsx diff --git a/src/hooks/useVault.ts b/src/hooks/useVault.ts index e9a6309e95..d23b09311f 100644 --- a/src/hooks/useVault.ts +++ b/src/hooks/useVault.ts @@ -4,6 +4,7 @@ import { getJSONFromRealmObject } from 'src/storage/realm/utils'; import { useQuery } from '@realm/react'; import { VaultType, VisibilityType } from 'src/services/wallets/enums'; import { useAppSelector } from 'src/store/hooks'; +import { useMemo } from 'react'; type Params = | { @@ -25,19 +26,29 @@ const useVault = ({ getFirst = false, getHiddenWallets = true, }: Params) => { - let allVaults: Vault[] = useQuery(RealmSchema.Vault); - + const realmVaults = useQuery(RealmSchema.Vault) as unknown as Vault[]; const { bitcoinNetworkType } = useAppSelector((state) => state.settings); - allVaults = includeArchived - ? allVaults.map(getJSONFromRealmObject) - : allVaults.filtered('archived != true').map(getJSONFromRealmObject); - // Filter vaults based on network - allVaults = allVaults.filter((wallet) => wallet.networkType === bitcoinNetworkType); + // Select before converting: toJSON traverses address, UTXO and history data. + // The useQuery reference changes on Realm writes, but stays stable across + // unrelated renders of the same screen. + const allVaultsIncludingCanary: Vault[] = useMemo( + () => + realmVaults + .filter( + (vault) => + (includeArchived || !vault.archived) && vault.networkType === bitcoinNetworkType + ) + .map(getJSONFromRealmObject) as unknown as Vault[], + [realmVaults, includeArchived, bitcoinNetworkType] + ); //Filtering Canary Vaults from at all UI level where Vaults are consumed - const allVaultsIncludingCanary = allVaults; - allVaults = allVaults.filter((vault) => vault.type !== VaultType.CANARY); - const allNonHiddenNonArchivedVaults = allVaults.filter( - (vault) => vault.presentationData.visibility === VisibilityType.DEFAULT + const allVaults = useMemo( + () => allVaultsIncludingCanary.filter((vault) => vault.type !== VaultType.CANARY), + [allVaultsIncludingCanary] + ); + const allNonHiddenNonArchivedVaults = useMemo( + () => allVaults.filter((vault) => vault.presentationData.visibility === VisibilityType.DEFAULT), + [allVaults] ); if (!vaultId) { if (getHiddenWallets) { diff --git a/src/hooks/useWallets.tsx b/src/hooks/useWallets.tsx index 79eb7aeee1..eafab61eca 100644 --- a/src/hooks/useWallets.tsx +++ b/src/hooks/useWallets.tsx @@ -4,44 +4,55 @@ import { getJSONFromRealmObject } from 'src/storage/realm/utils'; import { VisibilityType, WalletType } from 'src/services/wallets/enums'; import { useObject, useQuery } from '@realm/react'; import { useAppSelector } from 'src/store/hooks'; +import { useMemo } from 'react'; type useWalletsInterface = ({ getAll, walletIds }?: { getAll?: boolean; walletIds?: string[] }) => { wallets: Wallet[]; }; +const filterByNetwork = (wallets: Wallet[], bitcoinNetworkType?: string): Wallet[] => + bitcoinNetworkType + ? (wallets + .filter((wallet) => wallet.networkType === bitcoinNetworkType) + .map(getJSONFromRealmObject) as unknown as Wallet[]) + : wallets; + const useWallets: useWalletsInterface = ({ walletIds = [], getAll = false } = {}) => { const { bitcoinNetworkType } = useAppSelector((state) => state.settings); - const allWallets: Wallet[] = useQuery(RealmSchema.Wallet).filter((wallet) => - [WalletType.DEFAULT, WalletType.IMPORTED].includes(wallet.type) - ); - - const filterByNetwork = (wallets) => { - if (bitcoinNetworkType) - return wallets - .map(getJSONFromRealmObject) - .filter((wallet) => wallet.networkType === bitcoinNetworkType); - else return wallets; - }; + const realmWallets = useQuery(RealmSchema.Wallet) as unknown as Wallet[]; + walletIds = walletIds?.filter((item) => !!item); + const hasWalletIds = !!walletIds?.length; + + // useQuery changes its collection reference when Realm reports an insert, + // deletion, or modification. Other screen renders can reuse this snapshot. + const listedWallets = useMemo(() => { + if (hasWalletIds && !getAll) return []; + const regularWallets = realmWallets.filter( + (wallet) => wallet.type === WalletType.DEFAULT || wallet.type === WalletType.IMPORTED + ); + const visibleWallets = getAll + ? regularWallets + : regularWallets.filter( + (wallet) => wallet.presentationData.visibility == VisibilityType.DEFAULT + ); + return filterByNetwork(visibleWallets, bitcoinNetworkType); + }, [realmWallets, bitcoinNetworkType, getAll, hasWalletIds]); if (getAll) { - return { wallets: filterByNetwork(allWallets) }; + return { wallets: listedWallets }; } - const allWalletsNonHidden = allWallets.filter( - (wallet) => wallet.presentationData.visibility == VisibilityType.DEFAULT - ); - walletIds = walletIds?.filter((item) => !!item); - if (walletIds && walletIds.length) { + if (hasWalletIds) { const extractedWallets = []; for (let index = 0; index < walletIds.length; index += 1) { const id = walletIds[index]; const wallet: Wallet = useObject(RealmSchema.Wallet, id); if (wallet) extractedWallets.push(wallet); } - return { wallets: filterByNetwork(extractedWallets) }; + return { wallets: filterByNetwork(extractedWallets, bitcoinNetworkType) }; } - return { wallets: filterByNetwork(allWalletsNonHidden) }; + return { wallets: listedWallets }; }; export default useWallets; diff --git a/tests/hooks/walletCollectionMemo.test.tsx b/tests/hooks/walletCollectionMemo.test.tsx new file mode 100644 index 0000000000..ec0964528f --- /dev/null +++ b/tests/hooks/walletCollectionMemo.test.tsx @@ -0,0 +1,138 @@ +import React from 'react'; +import TestRenderer, { act } from 'react-test-renderer'; +import { useObject, useQuery } from '@realm/react'; +import { useAppSelector } from 'src/store/hooks'; +import useWallets from 'src/hooks/useWallets'; +import useVault from 'src/hooks/useVault'; +import { RealmSchema } from 'src/storage/realm/enum'; +import { NetworkType, VaultType, VisibilityType, WalletType } from 'src/services/wallets/enums'; + +jest.mock('src/store/hooks', () => ({ useAppSelector: jest.fn() })); + +const queryMock = useQuery as jest.Mock; +const objectMock = useObject as jest.Mock; +const selectorMock = useAppSelector as jest.Mock; + +const makeManaged = (id: string, overrides: Record = {}) => { + const value = { + id, + type: WalletType.DEFAULT, + networkType: NetworkType.MAINNET, + archived: false, + presentationData: { name: id, visibility: VisibilityType.DEFAULT }, + specs: { balances: { confirmed: 100, unconfirmed: 0 } }, + ...overrides, + }; + return { ...value, toJSON: jest.fn(() => ({ ...value })) }; +}; + +describe('wallet and Vault list snapshots', () => { + let network: NetworkType; + let wallets: any[]; + let vaults: any[]; + + beforeEach(() => { + network = NetworkType.MAINNET; + wallets = []; + vaults = []; + queryMock.mockImplementation((schema) => (schema === RealmSchema.Wallet ? wallets : vaults)); + objectMock.mockReset(); + selectorMock.mockImplementation((selector) => + selector({ settings: { bitcoinNetworkType: network } }) + ); + }); + + afterEach(() => { + jest.clearAllMocks(); + }); + + test('wallets convert only selected network objects and refresh on query or network change', () => { + const main = makeManaged('main'); + const otherNetwork = makeManaged('testnet', { networkType: NetworkType.TESTNET }); + const hidden = makeManaged('hidden', { + presentationData: { name: 'hidden', visibility: VisibilityType.HIDDEN }, + }); + const legacy = makeManaged('legacy', { type: WalletType.PRE_MIX }); + wallets = [main, otherNetwork, hidden, legacy]; + let seen: any[] = []; + const Probe = ({ getAll = false, tick = 0 }) => { + seen = useWallets({ getAll }).wallets; + return <>{tick}; + }; + let tree: TestRenderer.ReactTestRenderer; + act(() => { + tree = TestRenderer.create(); + }); + expect(seen.map((wallet) => wallet.id)).toEqual(['main']); + expect(main.toJSON).toHaveBeenCalledTimes(1); + expect(otherNetwork.toJSON).not.toHaveBeenCalled(); + expect(hidden.toJSON).not.toHaveBeenCalled(); + act(() => tree.update()); + expect(main.toJSON).toHaveBeenCalledTimes(1); + + act(() => tree.update()); + expect(seen.map((wallet) => wallet.id)).toEqual(['main', 'hidden']); + expect(hidden.toJSON).toHaveBeenCalledTimes(1); + + const changed = makeManaged('main', { + specs: { balances: { confirmed: 250, unconfirmed: 0 } }, + }); + wallets = [changed, otherNetwork, hidden, legacy]; + act(() => tree.update()); + expect(seen[0].specs.balances.confirmed).toBe(250); + expect(changed.toJSON).toHaveBeenCalledTimes(1); + + network = NetworkType.TESTNET; + act(() => tree.update()); + expect(seen.map((wallet) => wallet.id)).toEqual(['testnet']); + expect(otherNetwork.toJSON).toHaveBeenCalledTimes(1); + act(() => tree.unmount()); + }); + + test('Vaults preserve archived, hidden and canary selection while avoiding unrelated conversions', () => { + const active = makeManaged('active'); + const archived = makeManaged('archive', { + archived: true, + specs: { balances: { confirmed: 0, unconfirmed: 0 } }, + }); + const hidden = makeManaged('hidden', { + presentationData: { name: 'hidden', visibility: VisibilityType.HIDDEN }, + }); + const canary = makeManaged('canary', { type: VaultType.CANARY }); + const otherNetwork = makeManaged('testnet', { networkType: NetworkType.TESTNET }); + vaults = [active, archived, hidden, canary, otherNetwork]; + let seen: ReturnType; + const Probe = ({ tick = 0, includeArchived = false, vaultId = '' }) => { + seen = useVault({ includeArchived, getHiddenWallets: false, vaultId }); + return <>{tick}; + }; + let tree: TestRenderer.ReactTestRenderer; + act(() => { + tree = TestRenderer.create(); + }); + expect(seen.allVaults.map((vault) => vault.id)).toEqual(['active']); + expect(active.toJSON).toHaveBeenCalledTimes(1); + expect(archived.toJSON).not.toHaveBeenCalled(); + expect(otherNetwork.toJSON).not.toHaveBeenCalled(); + act(() => tree.update()); + expect(active.toJSON).toHaveBeenCalledTimes(1); + + act(() => tree.update()); + expect(seen.activeVault?.id).toBe('canary'); + expect(canary.toJSON).toHaveBeenCalledTimes(1); + expect(active.toJSON).toHaveBeenCalledTimes(1); + + act(() => tree.update()); + expect(seen.activeVault?.id).toBe('archive'); + expect(seen.allVaults.map((vault) => vault.id)).toEqual(['active', 'archive']); + expect((seen.activeVault as any)?.specs.balances.confirmed).toBe(0); + const updatedArchive = makeManaged('archive', { + archived: true, + specs: { balances: { confirmed: 50, unconfirmed: 0 } }, + }); + vaults = [active, updatedArchive, hidden, canary, otherNetwork]; + act(() => tree.update()); + expect((seen.activeVault as any)?.specs.balances.confirmed).toBe(50); + act(() => tree.unmount()); + }); +}); From 7b1e7e1d9cb364a967720ae556b03440767d6d37 Mon Sep 17 00:00:00 2001 From: Anant Date: Thu, 8 Oct 2026 18:21:16 +0530 Subject: [PATCH 080/254] fix(wallets): keep ID lookup within one reactive query --- src/hooks/useWallets.tsx | 26 +++++++++++-------- tests/hooks/walletCollectionMemo.test.tsx | 31 +++++++++++++++++++++++ 2 files changed, 47 insertions(+), 10 deletions(-) diff --git a/src/hooks/useWallets.tsx b/src/hooks/useWallets.tsx index eafab61eca..284e039d1f 100644 --- a/src/hooks/useWallets.tsx +++ b/src/hooks/useWallets.tsx @@ -2,7 +2,7 @@ import { Wallet } from 'src/services/wallets/interfaces/wallet'; import { RealmSchema } from 'src/storage/realm/enum'; import { getJSONFromRealmObject } from 'src/storage/realm/utils'; import { VisibilityType, WalletType } from 'src/services/wallets/enums'; -import { useObject, useQuery } from '@realm/react'; +import { useQuery } from '@realm/react'; import { useAppSelector } from 'src/store/hooks'; import { useMemo } from 'react'; @@ -20,8 +20,9 @@ const filterByNetwork = (wallets: Wallet[], bitcoinNetworkType?: string): Wallet const useWallets: useWalletsInterface = ({ walletIds = [], getAll = false } = {}) => { const { bitcoinNetworkType } = useAppSelector((state) => state.settings); const realmWallets = useQuery(RealmSchema.Wallet) as unknown as Wallet[]; - walletIds = walletIds?.filter((item) => !!item); - const hasWalletIds = !!walletIds?.length; + const validWalletIds = walletIds?.filter((item) => !!item) || []; + const walletIdsKey = JSON.stringify(validWalletIds); + const hasWalletIds = validWalletIds.length > 0; // useQuery changes its collection reference when Realm reports an insert, // deletion, or modification. Other screen renders can reuse this snapshot. @@ -38,18 +39,23 @@ const useWallets: useWalletsInterface = ({ walletIds = [], getAll = false } = {} return filterByNetwork(visibleWallets, bitcoinNetworkType); }, [realmWallets, bitcoinNetworkType, getAll, hasWalletIds]); + // Resolve IDs from the same reactive query. Calling useObject in a loop + // changes the number of hooks when a route gains or loses an ID. + const selectedWallets = useMemo(() => { + if (getAll || !hasWalletIds) return []; + const ids = JSON.parse(walletIdsKey) as string[]; + const matched = ids + .map((id) => realmWallets.find((wallet) => wallet.id === id)) + .filter((wallet): wallet is Wallet => !!wallet); + return filterByNetwork(matched, bitcoinNetworkType); + }, [realmWallets, bitcoinNetworkType, getAll, hasWalletIds, walletIdsKey]); + if (getAll) { return { wallets: listedWallets }; } if (hasWalletIds) { - const extractedWallets = []; - for (let index = 0; index < walletIds.length; index += 1) { - const id = walletIds[index]; - const wallet: Wallet = useObject(RealmSchema.Wallet, id); - if (wallet) extractedWallets.push(wallet); - } - return { wallets: filterByNetwork(extractedWallets, bitcoinNetworkType) }; + return { wallets: selectedWallets }; } return { wallets: listedWallets }; diff --git a/tests/hooks/walletCollectionMemo.test.tsx b/tests/hooks/walletCollectionMemo.test.tsx index ec0964528f..d66a3c4bfa 100644 --- a/tests/hooks/walletCollectionMemo.test.tsx +++ b/tests/hooks/walletCollectionMemo.test.tsx @@ -135,4 +135,35 @@ describe('wallet and Vault list snapshots', () => { expect((seen.activeVault as any)?.specs.balances.confirmed).toBe(50); act(() => tree.unmount()); }); + + test('ID selection keeps order and survives an empty-to-populated route update', () => { + const first = makeManaged('first'); + const hidden = makeManaged('hidden', { + presentationData: { name: 'hidden', visibility: VisibilityType.HIDDEN }, + }); + wallets = [first, hidden]; + let seen: any[] = []; + const Probe = ({ walletIds = [] }: { walletIds?: string[] }) => { + seen = useWallets({ walletIds }).wallets; + return null; + }; + let tree: TestRenderer.ReactTestRenderer; + act(() => { + tree = TestRenderer.create(); + }); + expect(seen.map((wallet) => wallet.id)).toEqual(['first']); + + act(() => tree.update()); + expect(seen.map((wallet) => wallet.id)).toEqual(['hidden', 'first', 'hidden']); + expect(hidden.toJSON).toHaveBeenCalledTimes(2); + act(() => tree.update()); + expect(hidden.toJSON).toHaveBeenCalledTimes(2); + + network = undefined as any; + act(() => tree.update()); + expect(seen[0]).toBe(hidden); + act(() => tree.update()); + expect(seen.map((wallet) => wallet.id)).toEqual(['first']); + act(() => tree.unmount()); + }); }); From 54d53d74a45c02ad88fe9d4eee0a7316faf28fc3 Mon Sep 17 00:00:00 2001 From: Anant Date: Fri, 9 Oct 2026 15:02:56 +0530 Subject: [PATCH 081/254] test(vault): validate passphrase and fixed inheritance policy --- tests/services/passphraseDecayPolicy.test.ts | 85 ++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 tests/services/passphraseDecayPolicy.test.ts diff --git a/tests/services/passphraseDecayPolicy.test.ts b/tests/services/passphraseDecayPolicy.test.ts new file mode 100644 index 0000000000..b40f6e0ddf --- /dev/null +++ b/tests/services/passphraseDecayPolicy.test.ts @@ -0,0 +1,85 @@ +import * as bip39 from 'bip39'; +import { + generateSeedWordsKey, + generateMiniscriptScheme, +} from 'src/services/wallets/factories/VaultFactory'; +import { MiniscriptTypes, NetworkType } from 'src/services/wallets/enums'; +import { + ENHANCED_VAULT_TIMELOCKS_TIMESTAMP_MAINNET, + generateEnhancedVaultElements, +} from 'src/services/wallets/operations/miniscript/default/EnhancedVault'; +import WalletUtilities from 'src/services/wallets/operations/utils'; + +jest.mock('src/store/store', () => ({ + store: { getState: () => ({ settings: { bitcoinNetworkType: 'MAINNET' } }) }, +})); +jest.mock('src/utils/utilities', () => ({ + getKeyUID: (signer) => signer.masterFingerprint, +})); +jest.mock('src/utils/service-utilities/utils', () => ({ + getDerivationPath: (path) => path.replace('m/', ''), +})); + +const mnemonic = bip39.entropyToMnemonic('00000000000000000000000000000000'); +const networkType = NetworkType.MAINNET; +const network = WalletUtilities.getNetworkByType(networkType); +const derivationPath = WalletUtilities.getDerivationPath(true, networkType); + +const externalSigner = (passphrase: string) => { + const masterFingerprint = WalletUtilities.getMasterFingerprintFromMnemonic(mnemonic, passphrase); + return { + masterFingerprint, + xfp: masterFingerprint, + derivationPath, + xpub: WalletUtilities.generateExtendedKey( + mnemonic, + false, + network, + derivationPath, + passphrase + ), + }; +}; + +describe('Disposable passphrase and absolute-lock policy fixture', () => { + it('derives distinct signer keys from the same public test words with and without a passphrase', () => { + const primary = externalSigner('TREZOR'); + const inheritance = externalSigner(''); + const inAppSeedWordsKey = generateSeedWordsKey(mnemonic, networkType, true); + + expect(primary.masterFingerprint).not.toBe(inheritance.masterFingerprint); + expect(primary.xpub).not.toBe(inheritance.xpub); + expect(inAppSeedWordsKey.xpub).toBe(inheritance.xpub); + expect(inAppSeedWordsKey.xpub).not.toBe(primary.xpub); + }); + + it('compiles a one-key inheritance branch at one fixed six-month mainnet timestamp', () => { + const primary = externalSigner('TREZOR'); + const inheritance = externalSigner(''); + const fixedStart = 1800000000; + // Production's MONTHS_6 is a fixed 180-day interval; Testnet uses a shorter fixture interval. + expect(ENHANCED_VAULT_TIMELOCKS_TIMESTAMP_MAINNET.MONTHS_6).toBe(180 * 24 * 60 * 60); + const absoluteUnlock = fixedStart + ENHANCED_VAULT_TIMELOCKS_TIMESTAMP_MAINNET.MONTHS_6; + const elements = generateEnhancedVaultElements( + [primary], + [{ signer: inheritance, timelock: absoluteUnlock }], + [], + { m: 1, n: 1 }, + 0 + ); + const scheme = generateMiniscriptScheme(elements, [MiniscriptTypes.INHERITANCE]); + + expect(scheme.miniscriptPolicy).toContain(`after(${absoluteUnlock})`); + expect(scheme.miniscriptPolicy).toContain('pk(IK1'); + expect(scheme.miniscriptPolicy).toContain('pk(K1'); + expect(scheme.miniscript).toBeTruthy(); + expect(scheme.miniscriptElements.timelocks).toEqual([absoluteUnlock]); + expect(Object.values(scheme.keyInfoMap)).toEqual( + expect.arrayContaining([ + expect.stringContaining(`${primary.xpub}/<`), + expect.stringContaining(`${inheritance.xpub}/<`), + ]) + ); + expect(Object.values(scheme.keyInfoMap).every((key) => key.endsWith('/*'))).toBe(true); + }); +}); From 1e40ce7cffe63a3a568555cce1c3224ccf460251 Mon Sep 17 00:00:00 2001 From: Anant Date: Thu, 8 Oct 2026 22:07:49 +0530 Subject: [PATCH 082/254] test(backup): cover account switch during vault registration repair --- tests/recovery/vaultRegistration.test.cjs | 32 +++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/tests/recovery/vaultRegistration.test.cjs b/tests/recovery/vaultRegistration.test.cjs index c31762bddb..2a7e8f3efa 100644 --- a/tests/recovery/vaultRegistration.test.cjs +++ b/tests/recovery/vaultRegistration.test.cjs @@ -19,7 +19,7 @@ const vault = (id) => ({ specs: {}, }); -function registrationFixture({ incompleteReadback = false } = {}) { +function registrationFixture({ incompleteReadback = false, afterSnapshot, afterUpload } = {}) { const state = { appImage: { appId: 'disposable', wallets: {}, signers: {}, nodes: [], @@ -28,8 +28,13 @@ function registrationFixture({ incompleteReadback = false } = {}) { allVaultImages: [], labels: [], revision: 'a'.repeat(64), }; let uploads = 0; + let snapshotReads = 0; const f = harness({ adapter: async (endpoint, payload) => { - if (endpoint === 'getBackupSnapshot') return { data: clone(state) }; + if (endpoint === 'getBackupSnapshot') { + snapshotReads++; + if (afterSnapshot) await afterSnapshot(snapshotReads); + return { data: clone(state) }; + } assert.equal(endpoint, 'repairAppBackup'); assert.equal(payload.expectedRevision, state.revision); assert.equal(payload.replaceCurrentState, true); @@ -45,6 +50,7 @@ function registrationFixture({ incompleteReadback = false } = {}) { if (incompleteReadback) state.allVaultImages.pop(); state.labels = payload.labels; state.revision = 'b'.repeat(64); + if (afterUpload) await afterUpload(); return { data: { updated: true } }; } }); const cipherKey = encryption.generateEncryptionKey(f.app.primarySeed); @@ -92,6 +98,28 @@ test('an incomplete registration repair readback cannot be marked verified', asy assert.deepEqual(f.calls, ['getBackupSnapshot', 'repairAppBackup', 'getBackupSnapshot']); }); +test('account switch after registration snapshot prevents an upload for the old account', async () => { + let fixture; + fixture = registrationFixture({ afterSnapshot: (read) => { + if (read === 1) fixture.f.app.id = 'other-disposable-account'; + } }); + assert.equal(await fixture.f.run(true), 'unverified'); + assert.equal(fixture.uploads(), 0); + assert.deepEqual(fixture.f.calls, ['getBackupSnapshot']); + assert.ok(!fixture.f.phases.includes('verified')); +}); + +test('account switch after registration upload cannot claim a verified repair', async () => { + let fixture; + fixture = registrationFixture({ afterUpload: () => { + fixture.f.app.id = 'other-disposable-account'; + } }); + assert.equal(await fixture.f.run(true), 'unverified'); + assert.equal(fixture.uploads(), 1, 'the upload was for the original account'); + assert.deepEqual(fixture.f.calls, ['getBackupSnapshot', 'repairAppBackup', 'getBackupSnapshot']); + assert.ok(!fixture.f.phases.includes('verified')); +}); + test('registration-only drift is a difference while vault key and policy drift remain conflicts', async () => { const local = makeImage(), remote = makeImage(); local.vaults.first = vault('first'); From 1bee2e99bd075fcd163c10b665ac84f9e0a510cd Mon Sep 17 00:00:00 2001 From: Anant Date: Fri, 9 Oct 2026 15:03:27 +0530 Subject: [PATCH 083/254] Wait for visible balance before confirming Testnet faucet receipt --- src/store/sagas/faucetVisibility.ts | 2 + .../faucetVisibilityAcceptance.test.cjs | 123 ++++++++++++++++++ tests/services/faucetVisibility.test.ts | 12 ++ 3 files changed, 137 insertions(+) create mode 100644 tests/recovery/faucetVisibilityAcceptance.test.cjs diff --git a/src/store/sagas/faucetVisibility.ts b/src/store/sagas/faucetVisibility.ts index 2c2fae5db6..6352bab485 100644 --- a/src/store/sagas/faucetVisibility.ts +++ b/src/store/sagas/faucetVisibility.ts @@ -10,6 +10,8 @@ type VisibleWallet = { export const isFaucetTxVisible = (wallet: VisibleWallet, txid: string): boolean => { const specs = wallet?.specs; if (!specs?.balances) return false; + // Home displays this sum. Do not announce receipt while it still shows zero. + if (!(specs.balances.confirmed + specs.balances.unconfirmed > 0)) return false; return [...(specs.confirmedUTXOs || []), ...(specs.unconfirmedUTXOs || [])].some( (utxo) => utxo.txId?.toLowerCase() === txid.toLowerCase() diff --git a/tests/recovery/faucetVisibilityAcceptance.test.cjs b/tests/recovery/faucetVisibilityAcceptance.test.cjs new file mode 100644 index 0000000000..bbe8e48d54 --- /dev/null +++ b/tests/recovery/faucetVisibilityAcceptance.test.cjs @@ -0,0 +1,123 @@ +const { test } = require('node:test'); +const assert = require('node:assert/strict'); +const vm = require('node:vm'); +const ts = require('typescript'); +const { fixture, enums, wallet, loadModule, functions } = require('./sagaHarness.cjs'); + +const txid = 'a'.repeat(64); + +function faucetFixture(balanceOnAttempt) { + const f = fixture(); + f.state.settings.bitcoinNetworkType = enums.NetworkType.TESTNET; + f.app.networkType = enums.NetworkType.TESTNET; + const stored = { + ...wallet('disposable-faucet', enums.NetworkType.TESTNET), + specs: { + balances: { confirmed: 0, unconfirmed: 0 }, + confirmedUTXOs: [], + unconfirmedUTXOs: [], + addresses: { external: {}, internal: {} }, + transactions: [], + receivingAddress: 'tb1qdisposable', + }, + }; + f.collections.Wallet.push(stored); + + f.scope.getJSONFromRealmObject = (value) => JSON.parse(JSON.stringify(value)); + f.scope.store = { getState: () => f.state }; + f.scope.WALLET_SYNC_SCOPE_CHANGED = 'WALLET_SYNC_SCOPE_CHANGED'; + f.scope.ELECTRUM_CLIENT = { + isClientConnected: true, + activePeer: { networkType: enums.NetworkType.TESTNET }, + }; + let historyPolls = 0; + f.scope.ElectrumClient = { + getConnectionGeneration: () => 1, + assertConnectionGeneration: (_generation, network) => { + assert.equal(network, enums.NetworkType.TESTNET); + }, + syncHistoryByAddress: async () => { + historyPolls += 1; + return { txids: [txid] }; + }, + }; + f.scope.WalletUtilities.getNetworkByType = () => ({ network: 'testnet4' }); + let refreshes = 0; + f.scope.WalletOperations = { + getNextFreeAddress: () => stored.specs.receivingAddress, + syncWalletsViaElectrumClient: async (wallets) => { + refreshes += 1; + const synced = f.scope.getJSONFromRealmObject(wallets[0]); + synced.specs.unconfirmedUTXOs = [{ + txId: txid, + vout: 0, + address: stored.specs.receivingAddress, + value: 1000, + }]; + synced.specs.balances = { confirmed: 0, unconfirmed: balanceOnAttempt(refreshes) }; + synced.specs.hasNewUpdates = true; + return { synchedWallets: [{ synchedWallet: synced, newUTXOs: [] }] }; + }, + }; + let relayRequests = 0; + f.scope.Relay.getTestcoins = async () => { + relayRequests += 1; + return { txid, funded: true }; + }; + f.scope.isFaucetTxVisible = loadModule('src/store/sagas/faucetVisibility.ts').isFaucetTxVisible; + f.scope.classifyDustByAddress = () => ({ + taintedAddresses: new Set(), initialTaintAddresses: new Set(), dustSpendTxids: new Set(), + }); + for (const name of [ + 'setTestCoinsReceived', 'setTestCoinsFailed', 'setTestCoinsPending', + 'setTestCoinsQuotaReached', 'setSyncing', 'setElectrumNotConnectedErr', + ]) { + f.scope[name] = (payload) => ({ type: name, payload }); + } + f.scope.ELECTRUM_NOT_CONNECTED_ERR = 'offline'; + f.scope.ELECTRUM_NOT_CONNECTED_ERR_TOR = 'tor-offline'; + f.scope.delay = () => Promise.resolve(); + + const writes = []; + const originalUpdate = f.scope.dbManager.updateObjectById; + f.scope.dbManager.updateObjectById = (schema, id, patch) => { + writes.push({ schema, balance: patch.specs.balances.unconfirmed }); + return originalUpdate(schema, id, patch); + }; + vm.runInNewContext(ts.transpileModule( + functions('src/store/sagas/wallets.ts', ['refreshWalletsWorker', 'testcoinsWorker']), + { compilerOptions: { target: ts.ScriptTarget.ES2020 } } + ).outputText, f.scope); + + return { + f, stored, writes, + getCounts: () => ({ relayRequests, historyPolls, refreshes }), + }; +} + +test('faucet receipt waits for a funded UTXO and Home-visible persisted balance', async () => { + const { f, stored, writes, getCounts } = faucetFixture((attempt) => attempt === 1 ? 0 : 1000); + await f.run('testcoinsWorker', { payload: { wallet: stored } }); + + assert.deepEqual(getCounts(), { relayRequests: 1, historyPolls: 2, refreshes: 2 }); + assert.deepEqual(writes, [ + { schema: 'Wallet', balance: 0 }, + { schema: 'Wallet', balance: 1000 }, + ]); + assert.deepEqual(f.actions.filter((action) => action.type === 'setTestCoinsReceived') + .map((action) => action.payload), [false, true]); + assert.equal(stored.specs.balances.unconfirmed, 1000); + assert.equal(stored.specs.unconfirmedUTXOs[0].txId, txid); +}); + +test('faucet reports pending when the funded txid persists but visible balance stays zero', async () => { + const { f, stored, writes, getCounts } = faucetFixture(() => 0); + await f.run('testcoinsWorker', { payload: { wallet: stored } }); + + assert.deepEqual(getCounts(), { relayRequests: 1, historyPolls: 8, refreshes: 8 }); + assert.equal(writes.length, 8); + assert.equal(f.actions.some((action) => + action.type === 'setTestCoinsReceived' && action.payload === true), false); + assert.ok(f.actions.some((action) => + action.type === 'setTestCoinsPending' && action.payload === 'propagation')); +}); diff --git a/tests/services/faucetVisibility.test.ts b/tests/services/faucetVisibility.test.ts index 5732b6f106..9b2c4cfc47 100644 --- a/tests/services/faucetVisibility.test.ts +++ b/tests/services/faucetVisibility.test.ts @@ -19,4 +19,16 @@ describe('faucet transaction visibility', () => { expect(isFaucetTxVisible({}, txid)).toBe(false); expect(isFaucetTxVisible({ specs: { unconfirmedUTXOs: [{ txId: txid }] } }, txid)).toBe(false); }); + + it('waits until the persisted balance shown on Home is positive', () => { + const wallet = { + specs: { + balances: { confirmed: 0, unconfirmed: 0 }, + unconfirmedUTXOs: [{ txId: txid }], + }, + }; + expect(isFaucetTxVisible(wallet, txid)).toBe(false); + wallet.specs.balances.unconfirmed = 1000; + expect(isFaucetTxVisible(wallet, txid)).toBe(true); + }); }); From 9c66b3527fa23b8bf20ea7164b06c771b9ba17a6 Mon Sep 17 00:00:00 2001 From: Anant Date: Fri, 9 Oct 2026 15:04:11 +0530 Subject: [PATCH 084/254] Pin Relay signer-map cleanup for public contributor setup --- dev/local-backend/VERIFICATION.md | 26 +++++++++++++++++++++++--- dev/local-backend/sources.lock.json | 2 +- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/dev/local-backend/VERIFICATION.md b/dev/local-backend/VERIFICATION.md index 44a0d18ebe..7222bc86af 100644 --- a/dev/local-backend/VERIFICATION.md +++ b/dev/local-backend/VERIFICATION.md @@ -1,8 +1,28 @@ # Contributor setup verification +## R4 public Relay repin — 9 October 2026 + +The current source lock pins public Relay +`fb93c887631a6f567155a88f8e93d566f9d32a53` and public SigningServer +`3cc7728c27328e7b0358d1f91a32d8d13feba90a`. Compared with the previous +Relay pin, the follow-up changes signer lookup cleanup and its regression tests; +it does not change the mobile backup request or response shape or the app's local +Dockerfile adapter. Relay's exact-head build, disposable backup tests and source +scans passed in [PR #3](https://github.com/KeeperCommunity/Relay/pull/3). + +In the existing anonymous app checkout, with an isolated HOME, disabled +system/global Git configuration and interactive credential prompts, +`dev prepare` fetched the new Relay commit and applied the unchanged adapter. +It preserved the prepared public +SigningServer checkout. All 11 bootstrap fixtures and Compose configuration +validation passed. No local Docker build or native app run was repeated for this +new pin. The earlier full-stack acceptance in +[app PR #7030](https://github.com/KeeperCommunity/bitcoin-keeper/pull/7030) +used the previous Relay pin and is historical evidence for that revision. + ## Sprint contributor package — 7 October 2026 -The current contributor package pins public Relay +That sprint contributor package pinned public Relay `4c54e28738815546718775dbbdb39f0342c1b646` and public SigningServer `3cc7728c27328e7b0358d1f91a32d8d13feba90a`, with local adapter checksums in `sources.lock.json`. These are newer revisions than the earlier sprint PR #7014 @@ -18,8 +38,8 @@ changes have their own PR check results. Native app builds must be assessed against the selected app branch and platform separately. All dated sections below are historical evidence for the exact revisions named -in those sections. They do not verify the current pins or attribute earlier -private-source failures to the current public backend. +in those sections. They do not verify the 9 October source lock or attribute +earlier private-source failures to the current public backend. ## Public Relay transition — 7 October 2026 diff --git a/dev/local-backend/sources.lock.json b/dev/local-backend/sources.lock.json index 5917d3086f..420e19371b 100644 --- a/dev/local-backend/sources.lock.json +++ b/dev/local-backend/sources.lock.json @@ -1,7 +1,7 @@ { "relay": { "url": "https://github.com/KeeperCommunity/Relay.git", - "revision": "4c54e28738815546718775dbbdb39f0342c1b646", + "revision": "fb93c887631a6f567155a88f8e93d566f9d32a53", "assets": { "files/Dockerfile.local": "6dde6c692430b1a91a10cdd82e6f1562023cc5974ffd4d4f58fe57fa1994407a" } From d280c3b7ce879503998a9625a18d9be0ccd6d7da Mon Sep 17 00:00:00 2001 From: Anant Date: Fri, 9 Oct 2026 15:07:09 +0530 Subject: [PATCH 085/254] test(vault): validate actual descriptor and receive addresses --- tests/services/passphraseDecayPolicy.test.ts | 53 +++++++++++++++++--- 1 file changed, 45 insertions(+), 8 deletions(-) diff --git a/tests/services/passphraseDecayPolicy.test.ts b/tests/services/passphraseDecayPolicy.test.ts index b40f6e0ddf..7b33ab80f5 100644 --- a/tests/services/passphraseDecayPolicy.test.ts +++ b/tests/services/passphraseDecayPolicy.test.ts @@ -2,24 +2,20 @@ import * as bip39 from 'bip39'; import { generateSeedWordsKey, generateMiniscriptScheme, + generateVault, } from 'src/services/wallets/factories/VaultFactory'; -import { MiniscriptTypes, NetworkType } from 'src/services/wallets/enums'; +import { MiniscriptTypes, MultisigScriptType, NetworkType, VaultType } from 'src/services/wallets/enums'; import { ENHANCED_VAULT_TIMELOCKS_TIMESTAMP_MAINNET, generateEnhancedVaultElements, } from 'src/services/wallets/operations/miniscript/default/EnhancedVault'; import WalletUtilities from 'src/services/wallets/operations/utils'; +import WalletOperations from 'src/services/wallets/operations'; +import { generateOutputDescriptors } from 'src/utils/service-utilities/utils'; jest.mock('src/store/store', () => ({ store: { getState: () => ({ settings: { bitcoinNetworkType: 'MAINNET' } }) }, })); -jest.mock('src/utils/utilities', () => ({ - getKeyUID: (signer) => signer.masterFingerprint, -})); -jest.mock('src/utils/service-utilities/utils', () => ({ - getDerivationPath: (path) => path.replace('m/', ''), -})); - const mnemonic = bip39.entropyToMnemonic('00000000000000000000000000000000'); const networkType = NetworkType.MAINNET; const network = WalletUtilities.getNetworkByType(networkType); @@ -82,4 +78,45 @@ describe('Disposable passphrase and absolute-lock policy fixture', () => { ); expect(Object.values(scheme.keyInfoMap).every((key) => key.endsWith('/*'))).toBe(true); }); + + it('derives two receive addresses from a Vault descriptor with the same absolute unlock', async () => { + const primary = externalSigner('TREZOR'); + const inheritance = externalSigner(''); + const absoluteUnlock = 1800000000 + ENHANCED_VAULT_TIMELOCKS_TIMESTAMP_MAINNET.MONTHS_6; + const miniscriptScheme = generateMiniscriptScheme( + generateEnhancedVaultElements( + [primary], + [{ signer: inheritance, timelock: absoluteUnlock }], + [], + { m: 1, n: 1 }, + 0 + ), + [MiniscriptTypes.INHERITANCE] + ); + const vault = await generateVault({ + type: VaultType.MINISCRIPT, + vaultName: 'Disposable policy fixture', + vaultDescription: '', + scheme: { + m: 1, + n: 1, + multisigScriptType: MultisigScriptType.MINISCRIPT_MULTISIG, + miniscriptScheme, + }, + signers: [primary, inheritance], + networkType, + }); + const descriptor = generateOutputDescriptors(vault); + const first = WalletOperations.getExternalInternalAddressAtIdx(vault, 0); + const second = WalletOperations.getExternalInternalAddressAtIdx(vault, 1); + + expect(descriptor).toContain(`after(${absoluteUnlock})`); + expect(descriptor).toContain(`[${primary.masterFingerprint}/48h/0h/0h/2h]`); + expect(descriptor).toContain(`[${inheritance.masterFingerprint}/48h/0h/0h/2h]`); + expect(descriptor).toContain(`${primary.xpub}/<`); + expect(descriptor).toContain(`${inheritance.xpub}/<`); + expect(descriptor).toContain('/*'); + expect(descriptor).toContain('#'); + expect(first).not.toBe(second); + }); }); From 81bc417cb42372a4eb7dc918db073684f8bf2687 Mon Sep 17 00:00:00 2001 From: Anant Date: Fri, 9 Oct 2026 15:09:40 +0530 Subject: [PATCH 086/254] Guard backup consent completion across account switches --- src/store/sagas/bhr.ts | 18 ++++---- src/store/store.ts | 14 ++++++ tests/recovery/accountBinding.test.cjs | 61 +++++++++++++++++++++++++- tests/recovery/sagaHarness.cjs | 1 + 4 files changed, 85 insertions(+), 9 deletions(-) diff --git a/src/store/sagas/bhr.ts b/src/store/sagas/bhr.ts index 152ac95bcf..bf2c651bee 100644 --- a/src/store/sagas/bhr.ts +++ b/src/store/sagas/bhr.ts @@ -1132,20 +1132,22 @@ export function* backupFreshnessWatcher() { function* backupAllSignersAndVaultsWorker({ appId }: { appId: string }) { const { id }: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); if (!appId || appId !== id) return false; + const selected = yield select((state: RootState) => state.storage.appId === appId); + if (!selected) return false; yield put(setBackupAllSuccess({ appId, status: false })); yield put(setBackupAllFailure({ appId, status: false })); yield put(setBackupAllLoading({ appId, status: true })); try { const verified = yield call(runBackupInspection, true, appId, true); const current: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); - if (current?.id === id) { - if (verified) yield put(setAutomaticCloudBackup({ appId, enabled: true })); - yield put( - verified - ? setBackupAllSuccess({ appId, status: true }) - : setBackupAllFailure({ appId, status: true }) - ); - } + const stillSelected = yield select((state: RootState) => state.storage.appId === appId); + if (current?.id !== appId || !stillSelected) return false; + if (verified) yield put(setAutomaticCloudBackup({ appId, enabled: true })); + yield put( + verified + ? setBackupAllSuccess({ appId, status: true }) + : setBackupAllFailure({ appId, status: true }) + ); return verified; } finally { yield put(setBackupAllLoading({ appId, status: false })); diff --git a/src/store/store.ts b/src/store/store.ts index 9a94620c5d..f19aa9f6c4 100644 --- a/src/store/store.ts +++ b/src/store/store.ts @@ -115,6 +115,20 @@ const accountOwnedUtxoUiActions = new Set([ ]); const bindActionOrigin = (api) => (next) => (action) => { const originAppId = api.getState()?.storage?.appId; + const enablesBackup = action?.type === 'bhr/setAutomaticCloudBackup' && + action.payload?.enabled === true; + const reportsBackupSuccess = action?.type === 'bhr/setBackupAllSuccess' && + action.payload?.status === true; + if (enablesBackup || reportsBackupSuccess) { + const appId = action.payload.appId; + if (!appId || originAppId !== appId) return action; + try { + if ((dbManager.getObjectByIndex(RealmSchema.KeeperApp) as unknown as KeeperApp)?.id !== appId) + return action; + } catch { + return action; + } + } if (action?.originAppId && accountOwnedUtxoUiActions.has(action.type)) { try { if ( diff --git a/tests/recovery/accountBinding.test.cjs b/tests/recovery/accountBinding.test.cjs index b17dd2c3f2..7b27b562eb 100644 --- a/tests/recovery/accountBinding.test.cjs +++ b/tests/recovery/accountBinding.test.cjs @@ -1,7 +1,9 @@ const { test } = require('node:test'); const assert = require('node:assert/strict'); -const { fixture, wallet, signer, enums, loadModule } = require('./sagaHarness.cjs'); +const { fixture, wallet, signer, enums, loadModule, functions } = require('./sagaHarness.cjs'); const { harness } = require('./helpers.cjs'); +const vm = require('node:vm'); +const ts = require('typescript'); function switchToOptedInB(f) { f.app.id = 'B'; @@ -61,6 +63,63 @@ test('selected B blocks stale A writes even before Realm finishes switching', as assert.equal(f.collections.Wallet[0].specs.totalExternalAddresses, 4); }); +test('verified A backup completion after selected B switch cannot enable A or report success', async () => { + const f = fixture(); + const originAppId = f.app.id; + f.state.bhr.automaticCloudBackupByAppId[originAppId] = false; + f.scope.runBackupInspection = function* () { + f.state.storage.appId = 'B'; + f.state.bhr.automaticCloudBackupByAppId.B = true; + return true; + }; + const result = await f.run('backupAllSignersAndVaultsWorker', { appId: originAppId }); + assert.equal(result, false); + assert.ok(!f.actions.some((action) => action.type === 'setAutomaticCloudBackup' && + action.payload.enabled === true)); + assert.ok(!f.actions.some((action) => action.type === 'setBackupAllSuccess' && + action.payload.status === true)); +}); + +test('dispatch gate drops stale enable and success actions while preserving account-specific resets', () => { + const scope = { + Set, + RealmSchema: { KeeperApp: 'KeeperApp' }, + dbManager: { getObjectByIndex: () => ({ id: realmAppId }) }, + }; + let selectedAppId = 'B'; + let realmAppId = 'A'; + vm.runInNewContext(ts.transpileModule( + functions('src/store/store.ts', [ + 'accountOwnedActions', 'accountOwnedUtxoUiActions', 'bindActionOrigin', + ]), + { compilerOptions: { target: ts.ScriptTarget.ES2020 } } + ).outputText, scope); + const seen = []; + const middleware = scope.bindActionOrigin({ + getState: () => ({ storage: { appId: selectedAppId } }), + })((action) => { seen.push(action); return action; }); + const enableA = { type: 'bhr/setAutomaticCloudBackup', payload: { appId: 'A', enabled: true } }; + middleware(enableA); + assert.equal(seen.length, 0, 'selected B blocks A enable even while Realm still A'); + middleware({ type: 'bhr/setBackupAllSuccess', payload: { appId: 'A', status: true } }); + assert.equal(seen.length, 0, 'selected B blocks A success even while Realm still A'); + selectedAppId = 'A'; + realmAppId = 'B'; + middleware(enableA); + assert.equal(seen.length, 0, 'Realm B blocks A enable even while Redux still A'); + middleware({ type: 'bhr/setBackupAllSuccess', payload: { appId: 'A', status: true } }); + assert.equal(seen.length, 0, 'Realm B blocks A success even while Redux still A'); + realmAppId = 'A'; + middleware(enableA); + assert.equal(seen.length, 1); + middleware({ type: 'bhr/setBackupAllSuccess', payload: { appId: 'A', status: true } }); + assert.equal(seen.length, 2); + selectedAppId = 'B'; + middleware({ type: 'bhr/setAutomaticCloudBackup', payload: { appId: 'A', enabled: false } }); + middleware({ type: 'bhr/setBackupAllSuccess', payload: { appId: 'A', status: false } }); + assert.equal(seen.length, 4, 'A disable and success reset remain account-specific after switch'); +}); + test('A to opted-in B during connectivity wait blocks upload and preserves B local wallet', async () => { const f = fixture(); const originAppId = f.app.id; diff --git a/tests/recovery/sagaHarness.cjs b/tests/recovery/sagaHarness.cjs index 9e99f72a2a..1e5fd2a8b4 100644 --- a/tests/recovery/sagaHarness.cjs +++ b/tests/recovery/sagaHarness.cjs @@ -288,6 +288,7 @@ function fixture(options = {}) { 'deleteAppImageEntityWorker', 'deleteVaultImageWorker', 'deleteBackupWorker', + 'backupAllSignersAndVaultsWorker', 'checkBackupCondition', 'setServerBackupFailed', 'updateAppImageWorker', From 33592ffe989e2f5f24bbaa78f38ff79cbbd65096 Mon Sep 17 00:00:00 2001 From: Anant Date: Fri, 9 Oct 2026 15:20:14 +0530 Subject: [PATCH 087/254] candidate: stage provisional 2.6.4 version counters --- android/app/build.gradle | 4 ++-- ios/hexa_keeper.xcodeproj/project.pbxproj | 16 ++++++++-------- package.json | 2 +- release/version.json | 6 +++--- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/android/app/build.gradle b/android/app/build.gradle index 498a073274..aa9d6b70e7 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -105,8 +105,8 @@ android { applicationId "io.hexawallet.keeper" minSdkVersion rootProject.ext.minSdkVersion targetSdkVersion rootProject.ext.targetSdkVersion - versionCode 626 - versionName "2.6.3" + versionCode 627 + versionName "2.6.4" missingDimensionStrategy 'react-native-camera', 'general' missingDimensionStrategy 'store', 'play' multiDexEnabled true diff --git a/ios/hexa_keeper.xcodeproj/project.pbxproj b/ios/hexa_keeper.xcodeproj/project.pbxproj index c90117ddd7..1f8616b945 100644 --- a/ios/hexa_keeper.xcodeproj/project.pbxproj +++ b/ios/hexa_keeper.xcodeproj/project.pbxproj @@ -748,7 +748,7 @@ CODE_SIGN_ENTITLEMENTS = hexa_keeper/hexa_keeper.entitlements; CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 619; + CURRENT_PROJECT_VERSION = 620; DEVELOPMENT_TEAM = Y5TCB759QL; ENABLE_BITCODE = NO; HEADER_SEARCH_PATHS = ( @@ -849,7 +849,7 @@ "$(inherited)", "\"$(SRCROOT)\"", ); - MARKETING_VERSION = 2.6.3; + MARKETING_VERSION = 2.6.4; OTHER_LDFLAGS = ( "$(inherited)", "-ObjC", @@ -876,7 +876,7 @@ CODE_SIGN_IDENTITY = "Apple Development"; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Distribution"; CODE_SIGN_STYLE = Manual; - CURRENT_PROJECT_VERSION = 619; + CURRENT_PROJECT_VERSION = 620; DEVELOPMENT_TEAM = Y5TCB759QL; "DEVELOPMENT_TEAM[sdk=iphoneos*]" = Y5TCB759QL; HEADER_SEARCH_PATHS = ( @@ -977,7 +977,7 @@ "$(inherited)", "\"$(SRCROOT)\"", ); - MARKETING_VERSION = 2.6.3; + MARKETING_VERSION = 2.6.4; OTHER_LDFLAGS = ( "$(inherited)", "-ObjC", @@ -1147,7 +1147,7 @@ CODE_SIGN_IDENTITY = "Apple Development"; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Distribution"; CODE_SIGN_STYLE = Manual; - CURRENT_PROJECT_VERSION = 619; + CURRENT_PROJECT_VERSION = 620; DEVELOPMENT_TEAM = Y5TCB759QL; "DEVELOPMENT_TEAM[sdk=iphoneos*]" = Y5TCB759QL; ENABLE_BITCODE = NO; @@ -1250,7 +1250,7 @@ "$(PROJECT_DIR)", "\"$(SRCROOT)\"", ); - MARKETING_VERSION = 2.6.3; + MARKETING_VERSION = 2.6.4; OTHER_LDFLAGS = ( "$(inherited)", "-ObjC", @@ -1278,7 +1278,7 @@ CODE_SIGN_ENTITLEMENTS = hexa_keeper_dev.entitlements; CODE_SIGN_IDENTITY = "Apple Distribution"; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Distribution"; - CURRENT_PROJECT_VERSION = 619; + CURRENT_PROJECT_VERSION = 620; DEVELOPMENT_TEAM = Y5TCB759QL; "DEVELOPMENT_TEAM[sdk=iphoneos*]" = Y5TCB759QL; HEADER_SEARCH_PATHS = ( @@ -1380,7 +1380,7 @@ "$(PROJECT_DIR)", "\"$(SRCROOT)\"", ); - MARKETING_VERSION = 2.6.3; + MARKETING_VERSION = 2.6.4; OTHER_LDFLAGS = ( "$(inherited)", "-ObjC", diff --git a/package.json b/package.json index 93d368c6c5..a26e86c2b7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "hexa_keeper", - "version": "2.6.3", + "version": "2.6.4", "private": true, "scripts": { "ios": "npx react-native run-ios --scheme=hexa_keeper_dev --simulator 'iPhone 16' ", diff --git a/release/version.json b/release/version.json index 57bc77e469..9754ddfac8 100644 --- a/release/version.json +++ b/release/version.json @@ -1,7 +1,7 @@ { - "version": "2.6.3", - "androidVersionCode": 626, - "iosBuildNumber": 619, + "version": "2.6.4", + "androidVersionCode": 627, + "iosBuildNumber": 620, "baselines": { "android": { "applicationId": "io.hexawallet.bitcoinkeeper", From 7c6cb65de25ae7fb8cdf6c72fdaffdf8297bca54 Mon Sep 17 00:00:00 2001 From: Anant Date: Fri, 9 Oct 2026 15:21:11 +0530 Subject: [PATCH 088/254] candidate: reconcile frozen Yarn lock and Android input hash --- reproducibility/android-inputs.json | 2 +- yarn.lock | 184 +++------------------------- 2 files changed, 17 insertions(+), 169 deletions(-) diff --git a/reproducibility/android-inputs.json b/reproducibility/android-inputs.json index 14c9bfc290..a9183f99a6 100644 --- a/reproducibility/android-inputs.json +++ b/reproducibility/android-inputs.json @@ -2,7 +2,7 @@ "node": "22.23.3", "yarn": "1.22.22", "java": "17.0.19", - "yarn_lock_sha256": "b257e6d81f2850ca1c12ce3ddb559d39cd3113a51c09a9eb71ca11c81bf5a245", + "yarn_lock_sha256": "2764cadd01935bfe8542d858a58e9d397ee3479633f3320f8f3dbbff20ae425a", "gradle_wrapper_jar_sha256": "0336f591bc0ec9aa0c9988929b93ecc916b3c1d52aed202c7381db144aa0ef15", "gradle_distribution_sha256": "bd71102213493060956ec229d946beee57158dbd89d0e62b91bca0fa2c5f3531", "android_packages": { diff --git a/yarn.lock b/yarn.lock index d629fa670f..55464dae23 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1296,7 +1296,7 @@ dependencies: eslint-visitor-keys "^3.4.3" -"@eslint-community/regexpp@^4.12.2", "@eslint-community/regexpp@^4.4.0", "@eslint-community/regexpp@^4.6.1": +"@eslint-community/regexpp@^4.12.2", "@eslint-community/regexpp@^4.6.1": version "4.12.2" resolved "https://registry.yarnpkg.com/@eslint-community/regexpp/-/regexpp-4.12.2.tgz#bccdf615bcf7b6e8db830ec0b8d21c9a25de597b" integrity sha512-EriSTlt5OC9/7SXkRSCAhfSxxoSUgBm33OH+IkwbdpgoqsSsUg7y3uh+IICI/Qg4BBWr3U2i39RpmycbxMq4ew== @@ -4411,11 +4411,6 @@ dependencies: csstype "^3.0.2" -"@types/semver@^7.3.12": - version "7.7.1" - resolved "https://registry.yarnpkg.com/@types/semver/-/semver-7.7.1.tgz#3ce3af1a5524ef327d2da9e4fd8b6d95c8d70528" - integrity sha512-FmgJfu+MOcQ370SD0ev7EI8TlCAfKYU+B4m5T3yXc1CiRN94g/SZPtsCkk506aUDtlMnFZvasDwHHUcZUEaYuA== - "@types/stack-utils@^2.0.0": version "2.0.3" resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-2.0.3.tgz#6209321eb2c1712a7e7466422b8cb1fc0d9dd5d8" @@ -4455,22 +4450,6 @@ dependencies: "@types/yargs-parser" "*" -"@typescript-eslint/eslint-plugin@5.62.0": - version "5.62.0" - resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-5.62.0.tgz#aeef0328d172b9e37d9bab6dbc13b87ed88977db" - integrity sha512-TiZzBSJja/LbhNPvk6yc0JrX9XqhQ0hdh6M2svYfsHGejaKFIAGd9MQ+ERIMzLGlN/kZoYIgdxFV0PuljTKXag== - dependencies: - "@eslint-community/regexpp" "^4.4.0" - "@typescript-eslint/scope-manager" "5.62.0" - "@typescript-eslint/type-utils" "5.62.0" - "@typescript-eslint/utils" "5.62.0" - debug "^4.3.4" - graphemer "^1.4.0" - ignore "^5.2.0" - natural-compare-lite "^1.4.0" - semver "^7.3.7" - tsutils "^3.21.0" - "@typescript-eslint/eslint-plugin@^8.36.0": version "8.59.1" resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-8.59.1.tgz#781bc6f9002982cfaf75a185240e24ad7276628a" @@ -4485,16 +4464,6 @@ natural-compare "^1.4.0" ts-api-utils "^2.5.0" -"@typescript-eslint/parser@5.62.0": - version "5.62.0" - resolved "https://registry.yarnpkg.com/@typescript-eslint/parser/-/parser-5.62.0.tgz#1b63d082d849a2fcae8a569248fbe2ee1b8a56c7" - integrity sha512-VlJEV0fOQ7BExOsHYAGrgbEiZoi8D+Bl2+f6V2RrXerRSylnp+ZBHmPvaIa8cz0Ajx7WO7Z5RqfgYg7ED1nRhA== - dependencies: - "@typescript-eslint/scope-manager" "5.62.0" - "@typescript-eslint/types" "5.62.0" - "@typescript-eslint/typescript-estree" "5.62.0" - debug "^4.3.4" - "@typescript-eslint/parser@^8.36.0": version "8.59.1" resolved "https://registry.yarnpkg.com/@typescript-eslint/parser/-/parser-8.59.1.tgz#835d20a62350659a082a1ae2a60b822c40488905" @@ -4515,14 +4484,6 @@ "@typescript-eslint/types" "^8.59.1" debug "^4.4.3" -"@typescript-eslint/scope-manager@5.62.0": - version "5.62.0" - resolved "https://registry.yarnpkg.com/@typescript-eslint/scope-manager/-/scope-manager-5.62.0.tgz#d9457ccc6a0b8d6b37d0eb252a23022478c5460c" - integrity sha512-VXuvVvZeQCQb5Zgf4HAxc04q5j+WrNAtNh9OwCsCgpKqESMTu3tF/jhZ3xG6T4NZwWl65Bg8KuS2uEvhSfLl0w== - dependencies: - "@typescript-eslint/types" "5.62.0" - "@typescript-eslint/visitor-keys" "5.62.0" - "@typescript-eslint/scope-manager@8.59.1": version "8.59.1" resolved "https://registry.yarnpkg.com/@typescript-eslint/scope-manager/-/scope-manager-8.59.1.tgz#ed90d054fc3db2d0c81464db3a953a94fb85bb58" @@ -4536,16 +4497,6 @@ resolved "https://registry.yarnpkg.com/@typescript-eslint/tsconfig-utils/-/tsconfig-utils-8.59.1.tgz#ba2a779a444f1d5cb92a606f9b209d239fd4cab1" integrity sha512-/0nEyPbX7gRsk0Uwfe4ALwwgxuA66d/l2mhRDNlAvaj4U3juhUtJNq0DsY8M2AYwwb9rEq2hrC3IcIcEt++iJA== -"@typescript-eslint/type-utils@5.62.0": - version "5.62.0" - resolved "https://registry.yarnpkg.com/@typescript-eslint/type-utils/-/type-utils-5.62.0.tgz#286f0389c41681376cdad96b309cedd17d70346a" - integrity sha512-xsSQreu+VnfbqQpW5vnCJdq1Z3Q0U31qiWmRhr98ONQmcp/yhiPJFPq8MXiJVLiksmOKSjIldZzkebzHuCGzew== - dependencies: - "@typescript-eslint/typescript-estree" "5.62.0" - "@typescript-eslint/utils" "5.62.0" - debug "^4.3.4" - tsutils "^3.21.0" - "@typescript-eslint/type-utils@8.59.1": version "8.59.1" resolved "https://registry.yarnpkg.com/@typescript-eslint/type-utils/-/type-utils-8.59.1.tgz#9c83d3f2ed9187a815e8120f72c08317e513e409" @@ -4557,29 +4508,11 @@ debug "^4.4.3" ts-api-utils "^2.5.0" -"@typescript-eslint/types@5.62.0": - version "5.62.0" - resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-5.62.0.tgz#258607e60effa309f067608931c3df6fed41fd2f" - integrity sha512-87NVngcbVXUahrRTqIK27gD2t5Cu1yuCXxbLcFtCzZGlfyVWWh8mLHkoxzjsB6DDNnvdL+fW8MiwPEJyGJQDgQ== - "@typescript-eslint/types@8.59.1", "@typescript-eslint/types@^8.59.1": version "8.59.1" resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-8.59.1.tgz#c1d014d3f03a97e0113a8899fc9d4e45a7fb0ca9" integrity sha512-ZDCjgccSdYPw5Bxh+my4Z0lJU96ZDN7jbBzvmEn0FZx3RtU1C7VWl6NbDx94bwY3V5YsgwRzJPOgeY2Q/nLG8A== -"@typescript-eslint/typescript-estree@5.62.0": - version "5.62.0" - resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-5.62.0.tgz#7d17794b77fabcac615d6a48fb143330d962eb9b" - integrity sha512-CmcQ6uY7b9y694lKdRB8FEel7JbU/40iSAPomu++SjLMntB+2Leay2LO6i8VnJk58MtE9/nQSFIH6jpyRWyYzA== - dependencies: - "@typescript-eslint/types" "5.62.0" - "@typescript-eslint/visitor-keys" "5.62.0" - debug "^4.3.4" - globby "^11.1.0" - is-glob "^4.0.3" - semver "^7.3.7" - tsutils "^3.21.0" - "@typescript-eslint/typescript-estree@8.59.1": version "8.59.1" resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-8.59.1.tgz#4391fadf98a22c869c5b6522dbf4e491e53e351a" @@ -4595,20 +4528,6 @@ tinyglobby "^0.2.15" ts-api-utils "^2.5.0" -"@typescript-eslint/utils@5.62.0": - version "5.62.0" - resolved "https://registry.yarnpkg.com/@typescript-eslint/utils/-/utils-5.62.0.tgz#141e809c71636e4a75daa39faed2fb5f4b10df86" - integrity sha512-n8oxjeb5aIbPFEtmQxQYOLI0i9n5ySBEY/ZEHHZqKQSFnxio1rv6dthascc9dLuwrL0RC5mPCxB7vnAVGAYWAQ== - dependencies: - "@eslint-community/eslint-utils" "^4.2.0" - "@types/json-schema" "^7.0.9" - "@types/semver" "^7.3.12" - "@typescript-eslint/scope-manager" "5.62.0" - "@typescript-eslint/types" "5.62.0" - "@typescript-eslint/typescript-estree" "5.62.0" - eslint-scope "^5.1.1" - semver "^7.3.7" - "@typescript-eslint/utils@8.59.1", "@typescript-eslint/utils@^8.0.0": version "8.59.1" resolved "https://registry.yarnpkg.com/@typescript-eslint/utils/-/utils-8.59.1.tgz#cf6204d69701bbbc5b150f98c18aeef0a42c10bd" @@ -4619,14 +4538,6 @@ "@typescript-eslint/types" "8.59.1" "@typescript-eslint/typescript-estree" "8.59.1" -"@typescript-eslint/visitor-keys@5.62.0": - version "5.62.0" - resolved "https://registry.yarnpkg.com/@typescript-eslint/visitor-keys/-/visitor-keys-5.62.0.tgz#2174011917ce582875954ffe2f6912d5931e353e" - integrity sha512-07ny+LHRzQXepkGg6w0mFY41fVUNBrL2Roj/++7V1txKugfjm/Ci/qSND03r2RhlJhJYMcTn9AhhSSqQp0Ysyw== - dependencies: - "@typescript-eslint/types" "5.62.0" - eslint-visitor-keys "^3.3.0" - "@typescript-eslint/visitor-keys@8.59.1": version "8.59.1" resolved "https://registry.yarnpkg.com/@typescript-eslint/visitor-keys/-/visitor-keys-8.59.1.tgz#b5cba576287a3eeb0b400b62813189abcc3f976a" @@ -5014,11 +4925,6 @@ array-includes@^3.1.6, array-includes@^3.1.8: is-string "^1.1.1" math-intrinsics "^1.1.0" -array-union@^2.1.0: - version "2.1.0" - resolved "https://registry.yarnpkg.com/array-union/-/array-union-2.1.0.tgz#b798420adbeb1de828d84acd8a2e23d3efe85e8d" - integrity sha512-HGyxoOTYUyCM6stUe6EJgnd4EoewAI7zMdfqO+kGjnlZmBDz/cR5pf8r/cR4Wq60sL/p0IkcjUEEPwS3GFrIyw== - array.prototype.findlast@^1.2.5: version "1.2.5" resolved "https://registry.yarnpkg.com/array.prototype.findlast/-/array.prototype.findlast-1.2.5.tgz#3e4fbcb30a15a7f5bf64cf2faae22d139c2e4904" @@ -6598,13 +6504,6 @@ dijkstrajs@^1.0.1: resolved "https://registry.yarnpkg.com/dijkstrajs/-/dijkstrajs-1.0.3.tgz#4c8dbdea1f0f6478bff94d9c49c784d623e4fc23" integrity sha512-qiSlmBq9+BCdCA/L46dw8Uy93mloxsPSbwnm5yrKn2vMPiy8KyAskTF6zuV/j5BMsmOGZDPs7KjU+mjb670kfA== -dir-glob@^3.0.1: - version "3.0.1" - resolved "https://registry.yarnpkg.com/dir-glob/-/dir-glob-3.0.1.tgz#56dbf73d992a4a93ba1584f4534063fd2e41717f" - integrity sha512-WkrWp9GR4KXfKGYzOLmTuGVi1UWFfws377n9cc55/tb6DuqyF6pcQ5AbiHEshaDpY9v6oaSr2XCDidGmMwdzIA== - dependencies: - path-type "^4.0.0" - doctrine@^2.1.0: version "2.1.0" resolved "https://registry.yarnpkg.com/doctrine/-/doctrine-2.1.0.tgz#5cd01fc101621b42c4cd7f5d1a66243716d3f39d" @@ -6902,7 +6801,7 @@ es-errors@^1.3.0: resolved "https://registry.yarnpkg.com/es-errors/-/es-errors-1.3.0.tgz#05f75a25dab98e4fb1dcd5e1472c0546d5057c8f" integrity sha512-Zf5H2Kxt2xjTvbJvP2ZWLEICxA6j+hAmMzIlypy4xcBg1vKVnx89Wy0GbS+kf5cwCVFFzdCFh2XSCFNULS6csw== -es-iterator-helpers@^1.1.0, es-iterator-helpers@^1.2.1: +es-iterator-helpers@^1.2.1: version "1.3.2" resolved "https://registry.yarnpkg.com/es-iterator-helpers/-/es-iterator-helpers-1.3.2.tgz#8f4ff1f3603cbd09fbdb72c747a679779a65cc7f" integrity sha512-HVLACW1TppGYjJ8H6/jqH/pqOtKRw6wMlrB23xfExmFWxFquAIWCmwoLsOyN96K4a5KbmOf5At9ZUO3GZbetAw== @@ -7120,11 +7019,6 @@ eslint-plugin-jsx-a11y@6.10.2: safe-regex-test "^1.0.3" string.prototype.includes "^2.0.1" -eslint-plugin-react-hooks@4.6.2: - version "4.6.2" - resolved "https://registry.yarnpkg.com/eslint-plugin-react-hooks/-/eslint-plugin-react-hooks-4.6.2.tgz#c829eb06c0e6f484b3fbb85a97e57784f328c596" - integrity sha512-QzliNJq4GinDBcD8gPB5v0wh6g8q3SUi6EFF0x8N/BL9PoVs0atuGc47ozMRyOWAKdwaZ5OnbOEa3WR+dSGKuQ== - eslint-plugin-react-hooks@^7.0.1: version "7.1.1" resolved "https://registry.yarnpkg.com/eslint-plugin-react-hooks/-/eslint-plugin-react-hooks-7.1.1.tgz#e6742cad75d970c0a3f30d7d3fa80a4784f55927" @@ -7141,38 +7035,21 @@ eslint-plugin-react-native-globals@^0.1.1: resolved "https://registry.yarnpkg.com/eslint-plugin-react-native-globals/-/eslint-plugin-react-native-globals-0.1.2.tgz#ee1348bc2ceb912303ce6bdbd22e2f045ea86ea2" integrity sha512-9aEPf1JEpiTjcFAmmyw8eiIXmcNZOqaZyHO77wgm0/dWfT/oxC1SrIq8ET38pMxHYrcB6Uew+TzUVsBeczF88g== -eslint-plugin-react-native@4.1.0, eslint-plugin-react-native@^4.0.0: +eslint-plugin-react-native@^4.0.0: version "4.1.0" resolved "https://registry.yarnpkg.com/eslint-plugin-react-native/-/eslint-plugin-react-native-4.1.0.tgz#5343acd3b2246bc1b857ac38be708f070d18809f" integrity sha512-QLo7rzTBOl43FvVqDdq5Ql9IoElIuTdjrz9SKAXCvULvBoRZ44JGSkx9z4999ZusCsb4rK3gjS8gOGyeYqZv2Q== dependencies: eslint-plugin-react-native-globals "^0.1.1" -eslint-plugin-react@7.37.2: - version "7.37.2" - resolved "https://registry.yarnpkg.com/eslint-plugin-react/-/eslint-plugin-react-7.37.2.tgz#cd0935987876ba2900df2f58339f6d92305acc7a" - integrity sha512-EsTAnj9fLVr/GZleBLFbj/sSuXeWmp1eXIN60ceYnZveqEaUCyW4X+Vh4WTdUhCkW4xutXYqTXCUSyqD4rB75w== +eslint-plugin-react-native@^5.0.0: + version "5.0.0" + resolved "https://registry.yarnpkg.com/eslint-plugin-react-native/-/eslint-plugin-react-native-5.0.0.tgz#2ee990ba4967c557183b31121578547fb5c02d5d" + integrity sha512-VyWlyCC/7FC/aONibOwLkzmyKg4j9oI8fzrk9WYNs4I8/m436JuOTAFwLvEn1CVvc7La4cPfbCyspP4OYpP52Q== dependencies: - array-includes "^3.1.8" - array.prototype.findlast "^1.2.5" - array.prototype.flatmap "^1.3.2" - array.prototype.tosorted "^1.1.4" - doctrine "^2.1.0" - es-iterator-helpers "^1.1.0" - estraverse "^5.3.0" - hasown "^2.0.2" - jsx-ast-utils "^2.4.1 || ^3.0.0" - minimatch "^3.1.2" - object.entries "^1.1.8" - object.fromentries "^2.0.8" - object.values "^1.2.0" - prop-types "^15.8.1" - resolve "^2.0.0-next.5" - semver "^6.3.1" - string.prototype.matchall "^4.0.11" - string.prototype.repeat "^1.0.0" + eslint-plugin-react-native-globals "^0.1.1" -eslint-plugin-react@^7.30.1: +eslint-plugin-react@^7.30.1, eslint-plugin-react@^7.37.5: version "7.37.5" resolved "https://registry.yarnpkg.com/eslint-plugin-react/-/eslint-plugin-react-7.37.5.tgz#2975511472bdda1b272b34d779335c9b0e877065" integrity sha512-Qteup0SqU15kdocexFNAJMvCJEfa2xUKNV4CC1xsVMrIIqEy3SQ/rqyxCWNzfrd3/ldy6HMlD2e0JDVpDg2qIA== @@ -7196,7 +7073,7 @@ eslint-plugin-react@^7.30.1: string.prototype.matchall "^4.0.12" string.prototype.repeat "^1.0.0" -eslint-scope@5.1.1, eslint-scope@^5.1.1: +eslint-scope@5.1.1: version "5.1.1" resolved "https://registry.yarnpkg.com/eslint-scope/-/eslint-scope-5.1.1.tgz#e786e59a66cb92b3f6c1fb0d508aab174848f48c" integrity sha512-2NxwbF/hZ0KpepYN0cNbo+FN6XoK7GaHlQhgx/hIZl6Va0bF45RQOOwhLIy8lQDbuCiadSLCBnH2CFYquit5bw== @@ -7217,7 +7094,7 @@ eslint-visitor-keys@^2.1.0: resolved "https://registry.yarnpkg.com/eslint-visitor-keys/-/eslint-visitor-keys-2.1.0.tgz#f65328259305927392c938ed44eb0a5c9b2bd303" integrity sha512-0rSmRBzXgDzIsD6mGdJgevzgezI534Cer5L/vyMX0kHzT/jiB43jRhd9YUlMGYLQy2zprNmoT8qasCGtY+QaKw== -eslint-visitor-keys@^3.3.0, eslint-visitor-keys@^3.4.1, eslint-visitor-keys@^3.4.3: +eslint-visitor-keys@^3.4.1, eslint-visitor-keys@^3.4.3: version "3.4.3" resolved "https://registry.yarnpkg.com/eslint-visitor-keys/-/eslint-visitor-keys-3.4.3.tgz#0cd72fe8550e3c2eae156a96a4dddcd1c8ac5800" integrity sha512-wpc+LXeiyiisxPlEkUzU6svyS1frIO3Mgxj1fdy7Pm8Ygzguax2N3Fa/D/ag1WqbOprdI+uY6wMUl8/a2G+iag== @@ -7446,7 +7323,7 @@ fast-deep-equal@^3.1.1, fast-deep-equal@^3.1.3: resolved "https://registry.yarnpkg.com/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz#3a7d56b559d6cbc3eb512325244e619a65c6c525" integrity sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q== -fast-glob@^3.2.9, fast-glob@^3.3.2, fast-glob@^3.3.3: +fast-glob@^3.3.2, fast-glob@^3.3.3: version "3.3.3" resolved "https://registry.yarnpkg.com/fast-glob/-/fast-glob-3.3.3.tgz#d06d585ce8dba90a16b0505c543c3ccfb3aeb818" integrity sha512-7MptL8U0cqcFdzIzwOTHoilX9x5BrNqye7Z/LuC7kCMRio1EMSyqRK3BEAUD7sXRq4iT4AzTVuZdhgQ2TCvYLg== @@ -7946,18 +7823,6 @@ globalthis@^1.0.4: define-properties "^1.2.1" gopd "^1.0.1" -globby@^11.1.0: - version "11.1.0" - resolved "https://registry.yarnpkg.com/globby/-/globby-11.1.0.tgz#bd4be98bb042f83d796f7e3811991fbe82a0d34b" - integrity sha512-jhIXaOzy1sb8IyocaruWSn1TjmnBVs8Ayhcy83rmxNJ8q2uWKCAj3CnJY+KpGSXCueAPc0i05kVvVKtP1t9S3g== - dependencies: - array-union "^2.1.0" - dir-glob "^3.0.1" - fast-glob "^3.2.9" - ignore "^5.2.0" - merge2 "^1.4.1" - slash "^3.0.0" - google-protobuf@3.21.4: version "3.21.4" resolved "https://registry.yarnpkg.com/google-protobuf/-/google-protobuf-3.21.4.tgz#2f933e8b6e5e9f8edde66b7be0024b68f77da6c9" @@ -9517,7 +9382,7 @@ merge-stream@^2.0.0: resolved "https://registry.yarnpkg.com/merge-stream/-/merge-stream-2.0.0.tgz#52823629a14dd00c9770fb6ad47dc6310f2c1f60" integrity sha512-abv/qOcuPfk3URPfDzmZU1LKmuw8kT+0nIHvKrKgFrwifol/doWcdA4ZqsWQ8ENrFKkd67Mfpo/LovbIUsbt3w== -merge2@^1.3.0, merge2@^1.4.1: +merge2@^1.3.0: version "1.4.1" resolved "https://registry.yarnpkg.com/merge2/-/merge2-1.4.1.tgz#4368892f885e907455a6fd7dc55c0c9d404990ae" integrity sha512-8q7VEgMJW4J8tcfVPy8g09NcQwZdbwFEqhe/WZkoIzjn/3TGDwtOCYtXGxA3O8tPzpczCCDgv+P2P5y00ZJOOg== @@ -10082,11 +9947,6 @@ napi-build-utils@^2.0.0: resolved "https://registry.yarnpkg.com/napi-build-utils/-/napi-build-utils-2.0.0.tgz#13c22c0187fcfccce1461844136372a47ddc027e" integrity sha512-GEbrYkbfF7MoNaoh2iGG84Mnf/WZfB0GdGEsM8wz7Expx/LlWf5U8t9nvJKXSp3qr5IsEbK04cBGhol/KwOsWA== -natural-compare-lite@^1.4.0: - version "1.4.0" - resolved "https://registry.yarnpkg.com/natural-compare-lite/-/natural-compare-lite-1.4.0.tgz#17b09581988979fddafe0201e931ba933c96cbb4" - integrity sha512-Tj+HTDSJJKaZnfiuw+iaF9skdPpTo2GtEly5JHnWV/hfv2Qj/9RKsGISQtLh2ox3l5EAGw487hnBee0sIJ6v2g== - natural-compare@^1.4.0: version "1.4.0" resolved "https://registry.yarnpkg.com/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7" @@ -10276,7 +10136,7 @@ object.assign@^4.1.2, object.assign@^4.1.4, object.assign@^4.1.7: has-symbols "^1.1.0" object-keys "^1.1.1" -object.entries@^1.1.5, object.entries@^1.1.8, object.entries@^1.1.9: +object.entries@^1.1.5, object.entries@^1.1.9: version "1.1.9" resolved "https://registry.yarnpkg.com/object.entries/-/object.entries-1.1.9.tgz#e4770a6a1444afb61bd39f984018b5bede25f8b3" integrity sha512-8u/hfXFRBD1O0hPUjioLhoWFHRmt6tKA4/vZPyckBr18l1KE9uHrFaFaUi8MDRTpi4uak2goyPTSNJLXX2k2Hw== @@ -11949,7 +11809,7 @@ semver@^6.3.0, semver@^6.3.1: resolved "https://registry.yarnpkg.com/semver/-/semver-6.3.1.tgz#556d2ef8689146e46dcea4bfdd095f3434dffcb4" integrity sha512-BR7VvDCVHO+q2xBEWskxS6DJE1qRnb7DxzUrogb71CWoSficBxYsiAGd+Kl0mmq/MprG9yArRkyrQxTO6XjMzA== -semver@^7.1.3, semver@^7.3.4, semver@^7.3.5, semver@^7.3.7, semver@^7.5.2, semver@^7.5.3, semver@^7.5.4, semver@^7.6.3, semver@^7.7.3: +semver@^7.1.3, semver@^7.3.4, semver@^7.3.5, semver@^7.5.2, semver@^7.5.3, semver@^7.5.4, semver@^7.6.3, semver@^7.7.3: version "7.7.4" resolved "https://registry.yarnpkg.com/semver/-/semver-7.7.4.tgz#28464e36060e991fa7a11d0279d2d3f3b57a7e8a" integrity sha512-vFKC2IEtQnVhpT78h1Yp8wzwrf8CM+MzKMHGJZfBtzhZNycRFnXsHk6E5TxIkkMsgNS7mdX3AGB7x2QM2di4lA== @@ -12392,7 +12252,7 @@ string.prototype.includes@^2.0.1: define-properties "^1.2.1" es-abstract "^1.23.3" -string.prototype.matchall@^4.0.11, string.prototype.matchall@^4.0.12: +string.prototype.matchall@^4.0.12: version "4.0.12" resolved "https://registry.yarnpkg.com/string.prototype.matchall/-/string.prototype.matchall-4.0.12.tgz#6c88740e49ad4956b1332a911e949583a275d4c0" integrity sha512-6CC9uyBL+/48dYizRf7H7VAYCMCNTBeM78x/VTUe9bFEaxBepPJDa1Ow99LqI/1yF7kuy7Q3cQsYMrcjGUcskA== @@ -12779,23 +12639,11 @@ tslib@2.7.0: resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.7.0.tgz#d9b40c5c40ab59e8738f297df3087bf1a2690c01" integrity sha512-gLXCKdN1/j47AiHiOkJN69hJmcbGTHI0ImLmbYLHykhgeN0jVGola9yVjFgzCUklsZQMW55o+dW7IXv3RCXDzA== -tslib@^1.8.1: - version "1.14.1" - resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.14.1.tgz#cf2d38bdc34a134bcaf1091c41f6619e2f672d00" - integrity sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg== - tslib@^2.0.0, tslib@^2.0.1, tslib@^2.0.3, tslib@^2.1.0, tslib@^2.8.0, tslib@^2.8.1: version "2.8.1" resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.8.1.tgz#612efe4ed235d567e8aba5f2a5fab70280ade83f" integrity sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w== -tsutils@^3.21.0: - version "3.21.0" - resolved "https://registry.yarnpkg.com/tsutils/-/tsutils-3.21.0.tgz#b48717d394cea6c1e096983eed58e9d61715b623" - integrity sha512-mHKK3iUXL+3UF6xL5k0PEhKRUBKPBCv/+RkEOpjRWxxx27KKRBmmA60A9pgOUvMi8GKhRMPEmjBRPzs2W7O1OA== - dependencies: - tslib "^1.8.1" - tunnel-agent@^0.6.0: version "0.6.0" resolved "https://registry.yarnpkg.com/tunnel-agent/-/tunnel-agent-0.6.0.tgz#27a5dea06b36b04a0a9966774b290868f0fc40fd" From 0b0fa2b0360bf7f1314fff7c1901725984e5c551 Mon Sep 17 00:00:00 2001 From: Anant Date: Fri, 9 Oct 2026 15:26:02 +0530 Subject: [PATCH 089/254] candidate: type scoped wallet refresh inputs --- src/store/sagas/wallets.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/store/sagas/wallets.ts b/src/store/sagas/wallets.ts index 590f5cf10a..8a8313ff01 100644 --- a/src/store/sagas/wallets.ts +++ b/src/store/sagas/wallets.ts @@ -783,7 +783,7 @@ function* refreshWalletsWorker({ return dbManager.getObjectById(schema, wallet.id); }); if (!currentWallet || currentWallet.networkType !== refreshScope.networkType) return false; - currentWallets.push(getJSONFromRealmObject(currentWallet)); + currentWallets.push(getJSONFromRealmObject(currentWallet) as unknown as Wallet | Vault); } wallets = currentWallets; @@ -1193,7 +1193,7 @@ export function* autoWalletsSyncWorker({ activeSucceeded = yield call(refreshWalletsWorker, { originAppId: sourceAppId, payload: { - wallets: active.map(getJSONFromRealmObject), + wallets: active.map((wallet) => getJSONFromRealmObject(wallet) as unknown as Wallet | Vault), options: { hardRefresh, addNotifications }, }, }); @@ -1217,7 +1217,9 @@ export function* autoWalletsSyncWorker({ archivedSucceeded = yield call(refreshWalletsWorker, { originAppId: sourceAppId, payload: { - wallets: archived.map(getJSONFromRealmObject), + wallets: archived.map( + (wallet) => getJSONFromRealmObject(wallet) as unknown as Wallet | Vault + ), options: { hardRefresh, addNotifications: false }, }, }); From a4a55f03ecb8ccc690e9c1b627c2185985c3b9e7 Mon Sep 17 00:00:00 2001 From: Anant Date: Fri, 9 Oct 2026 15:42:08 +0530 Subject: [PATCH 090/254] test(electrum): make client facade suite deterministic --- tests/services/client.test.ts | 143 ++++++++++++++++++++++++---------- 1 file changed, 104 insertions(+), 39 deletions(-) diff --git a/tests/services/client.test.ts b/tests/services/client.test.ts index 7b1381530b..38c22dc894 100644 --- a/tests/services/client.test.ts +++ b/tests/services/client.test.ts @@ -3,24 +3,84 @@ import ElectrumClient from '../../src/services/electrum/client'; import { predefinedTestnetNodes } from '../../src/services/electrum/predefinedNodes'; import * as bitcoinJS from 'bitcoinjs-lib'; +const mockClients: any[] = []; +const mockTxid = '24913105c497b4bb4ccf81b03857c8306aba0e58ccde792d981f6c18efdb24b8'; + +// Exercise the real ElectrumClient facade against deterministic protocol replies. +// A public Electrum server is outside the control of CI. +jest.mock('electrum-client', () => ({ + __esModule: true, + default: jest.fn().mockImplementation((_net, _tls, _port, host) => { + const client = { + host, + initElectrum: jest.fn(async () => { + if (host === 'blackie.c3-soft.com') { + throw new Error('connect ECONNREFUSED 127.0.0.1:50002'); + } + return ['fixture-server', '1.4']; + }), + close: jest.fn(), + onError: null, + server_ping: jest.fn(async () => undefined), + server_features: jest.fn(async () => ({ + server_version: 'fixture-server', + protocol_min: '1.4', + protocol_max: '1.4.3', + })), + blockchainScripthash_listunspentBatch: jest.fn(async (hashes) => + hashes.map((param, index) => ({ + param, + result: index === 0 ? [{ tx_hash: mockTxid, tx_pos: 1, value: 12345, height: 100 }] : [], + })) + ), + blockchainScripthash_getHistoryBatch: jest.fn(async (hashes) => + hashes.map((param, index) => ({ + param, + result: index === 0 ? [{ tx_hash: mockTxid, height: 100 }] : [], + })) + ), + blockchainEstimatefee: jest.fn(async () => 0.00002), + blockchainTransaction_getBatch: jest.fn(async (txids) => + txids.map((param) => ({ + param, + result: { + txid: param, + hex: 'fixture-hex', + vout: [{ scriptPubKey: { address: 'tb1qfixture' } }], + }, + })) + ), + }; + mockClients.push(client); + return client; + }), +})); + jest.mock('src/store/store', () => ({ store: { getState: () => ({ - settings: { - bitcoinNetwork: 'testnet', - }, + settings: { bitcoinNetworkType: 'TESTNET' }, }), }, })); +const onlineNode = { + ...predefinedTestnetNodes[1], + isConnected: true, +}; +const offlineNode = { + ...predefinedTestnetNodes[0], + isConnected: true, +}; +const addresses = [ + 'tb1qd2u9tvuqzadgeh02vppd33e7u2fatuwrw7h4q5', + 'tb1qwdm8hdyvv5jn05qq858lgnk50heucvxvqtl4sx', +]; + beforeAll(async () => { - try { - ElectrumClient.setActivePeer(predefinedTestnetNodes); - await ElectrumClient.connect(); - } catch (err) { - console.log('failed to connect to Electrum:', err); - process.exit(1); - } + ElectrumClient.setActivePeer([onlineNode], onlineNode); + const connection = await ElectrumClient.connect(); + assert.ok(connection.connected); }); afterAll(() => { @@ -30,23 +90,29 @@ afterAll(() => { describe('ElectrumClient', () => { describe('Connection Tests', () => { it('should test connection successfully', async () => { - const result = await ElectrumClient.testConnection(predefinedTestnetNodes[0]); + const result = await ElectrumClient.testConnection(onlineNode); assert.ok(result.connected); }); it('should fail connection when node is unreachable', async () => { - const unreachableNode = { ...predefinedTestnetNodes[0], host: 'invalid.host' }; - const result = await ElectrumClient.testConnection(unreachableNode); + const result = await ElectrumClient.testConnection(offlineNode); assert.ok(!result.connected); assert.ok(result.error); + assert.strictEqual(result.errorType, 'network'); }); it('should reconnect to the next peer when connection fails', async () => { - ElectrumClient.setActivePeer(predefinedTestnetNodes); - ElectrumClient.forceDisconnect(); - const result = await ElectrumClient.reconnect(); + ElectrumClient.setActivePeer([offlineNode, onlineNode], offlineNode); + const result = await ElectrumClient.connect(); assert.ok(result.connected); - assert.ok(result.connectedTo); + assert.strictEqual(result.connectedTo, onlineNode.host); + assert.strictEqual(ElectrumClient.getActivePeer()?.host, onlineNode.host); + + ElectrumClient.forceDisconnect(); + const reconnected = await ElectrumClient.reconnect(); + assert.ok(reconnected.connected); + assert.strictEqual(reconnected.connectedTo, onlineNode.host); + assert.ok(mockClients.some((client) => client.host === offlineNode.host)); }); }); @@ -60,51 +126,50 @@ describe('ElectrumClient', () => { describe('Feature Tests', () => { it('should retrieve server features', async () => { const features = await ElectrumClient.serverFeatures(); - assert.ok(features.server_version); - assert.ok(features.protocol_min); - assert.ok(features.protocol_max); + assert.strictEqual(features.server_version, 'fixture-server'); + assert.strictEqual(features.protocol_min, '1.4'); + assert.strictEqual(features.protocol_max, '1.4.3'); }); }); describe('UTXO and History Tests', () => { it('should sync UTXOs by address', async () => { - const addresses = [ - 'tb1qd2u9tvuqzadgeh02vppd33e7u2fatuwrw7h4q5', - 'tb1qwdm8hdyvv5jn05qq858lgnk50heucvxvqtl4sx', - ]; const utxos = await ElectrumClient.syncUTXOByAddress(addresses, bitcoinJS.networks.testnet); - assert.ok(Object.keys(utxos).length > 0); + assert.deepStrictEqual(utxos[addresses[0]], [ + { txId: mockTxid, vout: 1, value: 12345, height: 100, address: addresses[0] }, + ]); + assert.deepStrictEqual(utxos[addresses[1]], []); }); it('should sync history by address', async () => { - const addresses = [ - 'tb1qd2u9tvuqzadgeh02vppd33e7u2fatuwrw7h4q5', - 'tb1qwdm8hdyvv5jn05qq858lgnk50heucvxvqtl4sx', - ]; const history = await ElectrumClient.syncHistoryByAddress( addresses, bitcoinJS.networks.testnet ); - assert.ok(Object.keys(history.historyByAddress).length > 0); - assert.ok(history.txids.length > 0); + assert.deepStrictEqual(history.historyByAddress[addresses[0]], [ + { tx_hash: mockTxid, height: 100 }, + ]); + assert.deepStrictEqual(history.historyByAddress[addresses[1]], []); + assert.deepStrictEqual(history.txids, [mockTxid]); + assert.strictEqual(history.txidToAddress[mockTxid], addresses[0]); }); }); describe('Transaction Tests', () => { it('should estimate fee', async () => { const fee = await ElectrumClient.estimateFee(1); - assert.ok(fee > 0); + assert.strictEqual(fee, 2); }); it('should fetch transactions by ID', async () => { - const txids = [ - '24913105c497b4bb4ccf81b03857c8306aba0e58ccde792d981f6c18efdb24b8', - '9a15008af30580039bc7a07d36c3544902a2e1618ad31f4dda94fe0044a8dcce', - ]; + const txids = [mockTxid, '9a15008af30580039bc7a07d36c3544902a2e1618ad31f4dda94fe0044a8dcce']; const transactions = await ElectrumClient.getTransactionsById(txids); - assert.ok(Object.keys(transactions).length === txids.length); - assert.deepStrictEqual(transactions[txids[0]].txid, txids[0]); - assert.deepStrictEqual(transactions[txids[1]].txid, txids[1]); + assert.deepStrictEqual(Object.keys(transactions), txids); + for (const txid of txids) { + assert.strictEqual(transactions[txid].txid, txid); + assert.strictEqual(transactions[txid].hex, undefined); + assert.deepStrictEqual(transactions[txid].vout[0].scriptPubKey.addresses, ['tb1qfixture']); + } }); }); }); From 7ec939f2be21d5d56d24db8298321f9144f0e29e Mon Sep 17 00:00:00 2001 From: Anant Date: Fri, 9 Oct 2026 15:44:41 +0530 Subject: [PATCH 091/254] test(vault): close Electrum connection after suite --- tests/services/vault.test.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/services/vault.test.ts b/tests/services/vault.test.ts index ed6cf6f13b..ed49d4001d 100644 --- a/tests/services/vault.test.ts +++ b/tests/services/vault.test.ts @@ -67,6 +67,10 @@ const connectToElectrumClient = async () => { } }; +afterAll(() => { + ElectrumClient.forceDisconnect(); +}); + describe('Vault: Single-Sig(1-of-1)', () => { let primaryMnemonic: string; let vault: Vault; From bfb2465e9b323b6dba31e7002e5907c03b3f5c58 Mon Sep 17 00:00:00 2001 From: Anant Date: Fri, 9 Oct 2026 16:09:44 +0530 Subject: [PATCH 092/254] candidate: pin additional Android NDK used by diagnostic build --- .github/workflows/android-diagnostic-build.yml | 2 +- reproducibility/android-inputs.json | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/android-diagnostic-build.yml b/.github/workflows/android-diagnostic-build.yml index a272100d6d..c4e2befa71 100644 --- a/.github/workflows/android-diagnostic-build.yml +++ b/.github/workflows/android-diagnostic-build.yml @@ -42,7 +42,7 @@ jobs: packages: '' - name: Install Android build packages - run: sdkmanager 'platforms;android-36' 'build-tools;35.0.0' 'ndk;27.1.12297006' 'cmake;3.22.1' + run: sdkmanager 'platforms;android-36' 'build-tools;35.0.0' 'ndk;27.0.12077973' 'ndk;27.1.12297006' 'cmake;3.22.1' - name: Install JavaScript dependencies without the iOS setup hook run: | diff --git a/reproducibility/android-inputs.json b/reproducibility/android-inputs.json index a9183f99a6..eac1c1e6a0 100644 --- a/reproducibility/android-inputs.json +++ b/reproducibility/android-inputs.json @@ -7,6 +7,7 @@ "gradle_distribution_sha256": "bd71102213493060956ec229d946beee57158dbd89d0e62b91bca0fa2c5f3531", "android_packages": { "build-tools/35.0.0": "35.0.0", + "ndk/27.0.12077973": "27.0.12077973", "ndk/27.1.12297006": "27.1.12297006", "cmake/3.22.1": "3.22.1" }, From 1703107cf849f28258816ea514e5f56e22776a6f Mon Sep 17 00:00:00 2001 From: Anant Date: Fri, 9 Oct 2026 16:19:22 +0530 Subject: [PATCH 093/254] Pin Android diagnostic inputs and refresh 2.6.4 release guide --- .github/workflows/android-diagnostic-build.yml | 14 +++++++++++++- release/README.md | 6 +++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/.github/workflows/android-diagnostic-build.yml b/.github/workflows/android-diagnostic-build.yml index c4e2befa71..6ce856f760 100644 --- a/.github/workflows/android-diagnostic-build.yml +++ b/.github/workflows/android-diagnostic-build.yml @@ -24,6 +24,9 @@ jobs: steps: - name: Check out source uses: actions/checkout@v4 + with: + ref: ${{ github.event.pull_request.head.sha || github.sha }} + persist-credentials: false - name: Set up Node uses: actions/setup-node@v4 @@ -34,7 +37,7 @@ jobs: uses: actions/setup-java@v4 with: distribution: temurin - java-version: '17' + java-version: '17.0.19' - name: Set up Android SDK tools uses: android-actions/setup-android@v4 @@ -65,6 +68,15 @@ jobs: node scripts/verify-channel-config.cjs "$RUNNER_TEMP/keeper-diagnostic.env" printf 'sdk.dir=%s\n' "$ANDROID_SDK_ROOT" > android/local.properties + - name: Verify pinned Android inputs + env: + SOURCE_COMMIT: ${{ github.event.pull_request.head.sha || github.sha }} + run: | + python3 -B reproducibility/verify-android-inputs.py \ + --source-commit "$SOURCE_COMMIT" \ + --env-file "$RUNNER_TEMP/keeper-diagnostic.env" \ + --android-sdk-root "$ANDROID_SDK_ROOT" + - name: Confirm Gradle rejects a retired mobile credential run: | cp "$RUNNER_TEMP/keeper-diagnostic.env" "$RUNNER_TEMP/keeper-rejected.env" diff --git a/release/README.md b/release/README.md index 3b9db966fb..4934c88c9f 100644 --- a/release/README.md +++ b/release/README.md @@ -1,8 +1,8 @@ # Release version verification -**Current source intent:** 2.6.3 is the pairing-channel hotfix baseline. R3 is a separate internal work bucket; this manifest does not declare an R3 mobile delivery. Keep private advisory details, credentials and fixtures out of public release material. +**Current source intent:** this R4 candidate stages 2.6.4 on the 2.6.3 pairing-channel hotfix baseline. The candidate is still under review. Keep private advisory details, credentials and fixtures out of public release material. -`version.json` selects 2.6.3, Android 626 and iOS 619. Its recorded authenticated store baselines on 6 October 2026 are Android 625 and iOS 618, both at 2.6.2. Refresh these baselines before upload. Development builds and local relay validation are separate from reviewed production builds, deployment and store submission. +`version.json` provisionally selects 2.6.4, Android 627 and iOS 620. Its recorded authenticated store baselines on 6 October 2026 are Android 625 and iOS 618, both at 2.6.2. Refresh these baselines and confirm the counters before signing or upload. Development builds and local relay validation are separate from reviewed production builds, deployment and store submission. The owner will perform real-phone and other manual live checks after release. Do not require Developer Mode or physical-device connections to finish preparation. Record simulator, automated and disposable testnet evidence accurately; they do not establish physical or hardware-signer acceptance. @@ -31,7 +31,7 @@ Android production signing reads `STORE_PASSWORD`, `KEY_PASSWORD`, and `KEY_ALIA ## Manual release checks -- Post-release for R02 by owner decision: upgrade an existing installation with a designated test wallet on a physical iPhone and Android device; do not uninstall first. Confirm unlock, wallet visibility and settings survive. +- Post-release by owner decision: upgrade an existing installation with a designated test wallet on a physical iPhone and Android device; do not uninstall first. Confirm unlock, wallet visibility and settings survive. - Check the displayed marketing version and native build counter against the release manifest on both platforms. - Open Ask Keeper on both platforms and send a non-sensitive sample question to the intended backend. Confirm the response and error/retry behavior; local isolated-backend entry checks do not test AI responses. - Open Wallet Settings > Dust Report. Check an empty test wallet, then a designated testnet fixture with Do Not Spend coins. Confirm the report, ordinary-spend exclusion, and Donate Dust confirmation/amount/fee behavior. Record separately whether a testnet signing transaction completed; an empty-wallet result cannot verify donation. From 1f2325ca9283e628037c70ea40d26346a7dc02fc Mon Sep 17 00:00:00 2001 From: Anant Date: Fri, 9 Oct 2026 16:22:38 +0530 Subject: [PATCH 094/254] fix(r4): remove candidate-only TypeScript diagnostics --- src/components/HomeScreenHeader.tsx | 2 +- src/hooks/useSettingKeeper.tsx | 2 +- src/screens/Home/Notifications/NotificationsCenter.tsx | 2 +- src/screens/ViewTransactions/TransactionHistory.tsx | 2 +- src/screens/WalletDetails/components/Transactions.tsx | 6 +++--- src/services/backup/repair.ts | 9 ++++++--- tests/store/versionHistory.test.ts | 10 +++++++--- tsconfig.json | 2 +- 8 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/components/HomeScreenHeader.tsx b/src/components/HomeScreenHeader.tsx index 6a48c248e1..239ea8af23 100644 --- a/src/components/HomeScreenHeader.tsx +++ b/src/components/HomeScreenHeader.tsx @@ -163,7 +163,7 @@ const HomeScreenHeader: React.FC = ({ }, [uaiType.SERVER_BACKUP_FAILURE]: () => { dispatch(uaiActioned({ uaiId: localLatestUnseenUai.id, action: false })); - navigation.navigate('AssistedBackupStatus'); + navigation.dispatch(CommonActions.navigate('AssistedBackupStatus')); }, }; diff --git a/src/hooks/useSettingKeeper.tsx b/src/hooks/useSettingKeeper.tsx index b597869295..0f6a08f3de 100644 --- a/src/hooks/useSettingKeeper.tsx +++ b/src/hooks/useSettingKeeper.tsx @@ -268,7 +268,7 @@ export const useSettingKeeper = () => { title: settings.assistedServerBackup, description: settings.assistedServerBackupSubtitle, icon: , - onPress: () => navigation.navigate('AssistedBackupStatus'), + onPress: () => navigation.dispatch(CommonActions.navigate('AssistedBackupStatus')), rightIcon: isOnL2Above ? ( ) : ( diff --git a/src/screens/Home/Notifications/NotificationsCenter.tsx b/src/screens/Home/Notifications/NotificationsCenter.tsx index d2ee6454de..16fdfe7ba9 100644 --- a/src/screens/Home/Notifications/NotificationsCenter.tsx +++ b/src/screens/Home/Notifications/NotificationsCenter.tsx @@ -299,7 +299,7 @@ const Card = memo(({ uai }: CardProps) => { text: common.View, cta: () => { dispatch(uaiActioned({ uaiId: uai.id, action: false })); - navigtaion.navigate('AssistedBackupStatus'); + navigtaion.dispatch(CommonActions.navigate('AssistedBackupStatus')); }, }, }, diff --git a/src/screens/ViewTransactions/TransactionHistory.tsx b/src/screens/ViewTransactions/TransactionHistory.tsx index 5f2ab91977..bfd56a6faa 100644 --- a/src/screens/ViewTransactions/TransactionHistory.tsx +++ b/src/screens/ViewTransactions/TransactionHistory.tsx @@ -34,7 +34,7 @@ function TransactionHistory({ route }) { collection.filtered('id == $0', routeWallet.id) )[0]; const record = routeWallet.entityKind === EntityKind.VAULT ? vaultRecord : walletRecord; - const wallet = record ? (getJSONFromRealmObject(record) as Wallet | Vault) : routeWallet; + const wallet = record ? (getJSONFromRealmObject(record) as unknown as Wallet | Vault) : routeWallet; const { refreshing: pullRefresh, refresh } = useWalletRefresh(); const dispatch = useDispatch(); const insets = useSafeAreaInsets(); diff --git a/src/screens/WalletDetails/components/Transactions.tsx b/src/screens/WalletDetails/components/Transactions.tsx index c893ffaf2a..8aa5b810a6 100644 --- a/src/screens/WalletDetails/components/Transactions.tsx +++ b/src/screens/WalletDetails/components/Transactions.tsx @@ -46,11 +46,11 @@ function TransactionItem({ item, wallet, navigation, index }) { function Transactions({ transactions, - setPullRefresh, + setPullRefresh = undefined, pullRefresh, currentWallet, - setInitialLoading, - onBitcoinRefresh, + setInitialLoading = undefined, + onBitcoinRefresh = undefined, }) { const navigation = useNavigation(); const { translations } = useContext(LocalizationContext); diff --git a/src/services/backup/repair.ts b/src/services/backup/repair.ts index 3ba77bcce6..5cd73e655e 100644 --- a/src/services/backup/repair.ts +++ b/src/services/backup/repair.ts @@ -198,6 +198,8 @@ export function inspectBackup( throw new Error('Backup changed during check'); } }; + // The snapshot reader updates this stage inside an async closure. + const stageAtFailure = (): InspectionStage => stage; try { notify('checking'); const app = clone(dbManager.getObjectByIndex(RealmSchema.KeeperApp)); @@ -323,6 +325,7 @@ export function inspectBackup( notify('verified'); return 'verified'; } catch (error) { + const failedStage = stageAtFailure(); // A routine incremental write can be queued while a read-only check is // running. A transient snapshot request can also fail while the backup is // healthy. Retry either case once behind queued writes, within the same @@ -335,7 +338,7 @@ export function inspectBackup( } const queuedWriteChanged = changedRevision && backupQueuedRevision(appId) >= changedRevision; const transientSnapshotFailure = - stage === 'snapshot-request' && + failedStage === 'snapshot-request' && error instanceof BackupRequestError && ['http-5xx', 'network', 'timeout'].includes(error.category); if ( @@ -350,9 +353,9 @@ export function inspectBackup( // backup data, account identifiers, headers, or request bodies. const category = error instanceof BackupRequestError ? error.category : 'non-transport'; const diagnosticStage = - stage === 'snapshot-decryption' && error instanceof BackupSnapshotValidationError + failedStage === 'snapshot-decryption' && error instanceof BackupSnapshotValidationError ? 'snapshot-validation' - : stage; + : failedStage; globalThis.console.warn( 'Assisted Server Backup check failed at stage:', diagnosticStage, diff --git a/tests/store/versionHistory.test.ts b/tests/store/versionHistory.test.ts index 8c24390016..14422f7339 100644 --- a/tests/store/versionHistory.test.ts +++ b/tests/store/versionHistory.test.ts @@ -50,7 +50,7 @@ function setActiveAccount(appId: string, enabled = true) { function writeHistory(action) { const appId = action.originAppId; - const worker = updateVersionHistoryWorker(action); + const worker = updateVersionHistoryWorker(action) as Generator; expect(worker.next().value).toEqual(call(dbManager.getObjectByIndex, RealmSchema.KeeperApp)); const choice = worker.next({ id: appId } as any).value as any; expect(choice.type).toBe('SELECT'); @@ -67,7 +67,7 @@ function writeHistory(action) { function recoverWithoutDuplicateHistory(action) { const appId = action.originAppId; - const worker = updateVersionHistoryWorker(action); + const worker = updateVersionHistoryWorker(action) as Generator; expect(worker.next().value).toEqual(call(dbManager.getObjectByIndex, RealmSchema.KeeperApp)); expect((worker.next({ id: appId } as any).value as any).type).toBe('SELECT'); expect(worker.next(true).value).toEqual( @@ -104,7 +104,11 @@ describe('Version History retains recovery provenance through required upgrades' test('recovery does not skip any of the legacy migrations', () => { const trace = (isRecovery: boolean) => { const yielded: any[] = []; - const upgrade = applyUpgradeSequence({ previousVersion: '1.0.4', newVersion, isRecovery }); + const upgrade = applyUpgradeSequence({ + previousVersion: '1.0.4', + newVersion, + isRecovery, + }) as Generator; let step = upgrade.next(); while (!step.done) { yielded.push(step.value); diff --git a/tsconfig.json b/tsconfig.json index a6af06ae6d..7e143716c9 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -18,5 +18,5 @@ "resolveJsonModule": true /* Allows importing modules with a ‘.json’ extension, which is a common practice in node projects. */, "baseUrl": "." }, - "exclude": ["node_modules", "babel.config.js", "metro.config.js", "jest.config.js"] + "exclude": ["node_modules", "dev/local-backend", "babel.config.js", "metro.config.js", "jest.config.js"] } From 8f0a2a72819bc67f3f20657da1d7bbfa268830d4 Mon Sep 17 00:00:00 2001 From: Anant Date: Fri, 9 Oct 2026 16:25:31 +0530 Subject: [PATCH 095/254] fix(ios): refresh 2.6.4 Podfile lock for guarded config --- ios/Podfile.lock | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/ios/Podfile.lock b/ios/Podfile.lock index 97a5cb82e6..c0321033e6 100644 --- a/ios/Podfile.lock +++ b/ios/Podfile.lock @@ -3986,7 +3986,7 @@ SPEC CHECKSUMS: glog: 5683914934d5b6e4240e497e0f4a3b42d1854183 GoogleDataTransport: aae35b7ea0c09004c3797d53c8c41f66f219d6a7 GoogleUtilities: 00c88b9a86066ef77f0da2fab05f65d7768ed8e1 - hermes-engine: 497c47216fdf505312e6e9fb53a413e085f8a8e1 + hermes-engine: 4ec27e7c84ed7950da6e65008539685455d8bef4 HtmlToPdf: 5c0649e93aa1c6583f400268eedea00f15e0fa5f libportal-ios: d9aa55474e2d5be8e38e96345dd37be34fda45b4 libportal-react-native: 05778ef04321517c8366dc7173d05aa9fe131ff2 @@ -4086,7 +4086,7 @@ SPEC CHECKSUMS: React-utils: 493a45d877f410d62476c045fbf220acbffba194 React-webperformancenativemodule: 9b212313b01f5489f21963fbd4a81ef20f280097 ReactAppDependencyProvider: 5bf52020fdc0d0a337068ea42d4a6676ce4a04bc - ReactCodegen: e99a2b210dade5f08acfabb6309357e7f8c22640 + ReactCodegen: 5c808345ff57b4f4f1eab230f541921ad9e37500 ReactCommon: 3b79ecfd327035654f6c36cf1de8e222d8b835e6 RealmJS: a2e97359d58d72ca9fc2deb2e14ba34a218652e5 RNCClipboard: a9d5216cbfaf2f51cfe80bcb132856bb9158fe0d @@ -4099,12 +4099,12 @@ SPEC CHECKSUMS: RNKeychain: 182d986f809280edd9444a557adc21845f7d1e6f RNLocalize: 95a43f85e41a966be7bc9cff2437128911c52da0 RNQrGenerator: 36f1af182d7673bc36e317aca0d293d19883d6e3 - RNReanimated: dd6943b67163a2fa23ecaefc457cf34e7f94df74 + RNReanimated: 6b69a94db352e4dae7eb540f6fab60ff05c18508 RNScreens: 7179cc1ba31b4e18ed29f10abf20c24a7961cf4c RNSentry: f07853ff3fcbb69df26da1d9744e95ee97f683fd RNShare: 11923289a9964c6bd5f507eaa7fe61ec51b0adce RNSVG: 6a91afc99237de70b617c7a84ba84c988ad5573b - RNWorklets: 9daaf5ec29337d9b12503e9d2618a4e7d8d91475 + RNWorklets: 3f35c2c01e5839d4fee0496d10ada185628ba328 SDWebImage: a7f831e1a65eb5e285e3fb046a23fcfbf08e696d SDWebImageWebPCoder: 908b83b6adda48effe7667cd2b7f78c897e5111d Sentry: 4e85b40e4b0235853f40f959d315a6b28e3148d8 @@ -4113,6 +4113,6 @@ SPEC CHECKSUMS: Yoga: e6489bbfdab9b98f8d51993484d6bd40216b7834 ZXingObjC: 8898711ab495761b2dbbdec76d90164a6d7e14c5 -PODFILE CHECKSUM: b6c63a86c263fb282b75313bfa3faf16def35484 +PODFILE CHECKSUM: 79b67f9496d66b3a6a618591c809ed441d1032f6 COCOAPODS: 1.15.2 From ce2972b85ba8cd405b60ada06de00d6bd800bbef Mon Sep 17 00:00:00 2001 From: Anant Date: Fri, 9 Oct 2026 16:31:53 +0530 Subject: [PATCH 096/254] test: make Electrum suites deterministic for contributors --- dev/local-backend/sources.lock.json | 2 +- tests/services/client.test.ts | 145 +++++++++++++++++------- tests/services/electrumWalletFixture.ts | 79 +++++++++++++ tests/services/vault.test.ts | 9 ++ tests/services/wallet.test.ts | 5 + tsconfig.json | 2 +- 6 files changed, 200 insertions(+), 42 deletions(-) create mode 100644 tests/services/electrumWalletFixture.ts diff --git a/dev/local-backend/sources.lock.json b/dev/local-backend/sources.lock.json index 420e19371b..4d5d02b531 100644 --- a/dev/local-backend/sources.lock.json +++ b/dev/local-backend/sources.lock.json @@ -1,7 +1,7 @@ { "relay": { "url": "https://github.com/KeeperCommunity/Relay.git", - "revision": "fb93c887631a6f567155a88f8e93d566f9d32a53", + "revision": "ee935359f22ff7932ea0a62e9084d4b49801f4aa", "assets": { "files/Dockerfile.local": "6dde6c692430b1a91a10cdd82e6f1562023cc5974ffd4d4f58fe57fa1994407a" } diff --git a/tests/services/client.test.ts b/tests/services/client.test.ts index 7b1381530b..8a41f58689 100644 --- a/tests/services/client.test.ts +++ b/tests/services/client.test.ts @@ -3,24 +3,88 @@ import ElectrumClient from '../../src/services/electrum/client'; import { predefinedTestnetNodes } from '../../src/services/electrum/predefinedNodes'; import * as bitcoinJS from 'bitcoinjs-lib'; +const mockClients: any[] = []; +const mockTxid = '24913105c497b4bb4ccf81b03857c8306aba0e58ccde792d981f6c18efdb24b8'; + +// Exercise the real ElectrumClient facade against deterministic protocol replies. +// A public Electrum server is outside the control of CI. +jest.mock('electrum-client', () => ({ + __esModule: true, + default: jest.fn().mockImplementation((_net, _tls, _port, host) => { + const client = { + host, + initElectrum: jest.fn(async () => { + if (host === 'blackie.c3-soft.com') { + throw new Error('connect ECONNREFUSED 127.0.0.1:50002'); + } + return ['fixture-server', '1.4']; + }), + close: jest.fn(), + onError: null, + server_ping: jest.fn(async () => undefined), + server_features: jest.fn(async () => ({ + server_version: 'fixture-server', + protocol_min: '1.4', + protocol_max: '1.4.3', + })), + blockchainScripthash_listunspentBatch: jest.fn(async (hashes) => + hashes.map((param, index) => ({ + param, + result: index === 0 ? [{ tx_hash: mockTxid, tx_pos: 1, value: 12345, height: 100 }] : [], + })) + ), + blockchainScripthash_getHistoryBatch: jest.fn(async (hashes) => + hashes.map((param, index) => ({ + param, + result: index === 0 ? [{ tx_hash: mockTxid, height: 100 }] : [], + })) + ), + blockchainEstimatefee: jest.fn(async () => 0.00002), + blockchainTransaction_getBatch: jest.fn(async (txids) => + txids.map((param) => ({ + param, + result: { + txid: param, + hex: 'fixture-hex', + vout: [{ scriptPubKey: { address: 'tb1qfixture' } }], + }, + })) + ), + }; + mockClients.push(client); + return client; + }), +})); + jest.mock('src/store/store', () => ({ store: { getState: () => ({ - settings: { - bitcoinNetwork: 'testnet', - }, + settings: { bitcoinNetworkType: 'TESTNET' }, }), }, })); -beforeAll(async () => { - try { - ElectrumClient.setActivePeer(predefinedTestnetNodes); - await ElectrumClient.connect(); - } catch (err) { - console.log('failed to connect to Electrum:', err); - process.exit(1); - } +const onlineNode = { + ...predefinedTestnetNodes[0], + id: 337, + host: 'electrum-fixture.test', + isConnected: true, +}; +const offlineNode = { + ...predefinedTestnetNodes[0], + isConnected: true, +}; +const addresses = [ + 'tb1qd2u9tvuqzadgeh02vppd33e7u2fatuwrw7h4q5', + 'tb1qwdm8hdyvv5jn05qq858lgnk50heucvxvqtl4sx', +]; + +beforeEach(async () => { + mockClients.length = 0; + ElectrumClient.setActivePeer([onlineNode], onlineNode); + ElectrumClient.resetCurrentPeerIndex(); + const connection = await ElectrumClient.connect(); + assert.ok(connection.connected); }); afterAll(() => { @@ -30,23 +94,25 @@ afterAll(() => { describe('ElectrumClient', () => { describe('Connection Tests', () => { it('should test connection successfully', async () => { - const result = await ElectrumClient.testConnection(predefinedTestnetNodes[0]); + const result = await ElectrumClient.testConnection(onlineNode); assert.ok(result.connected); }); it('should fail connection when node is unreachable', async () => { - const unreachableNode = { ...predefinedTestnetNodes[0], host: 'invalid.host' }; - const result = await ElectrumClient.testConnection(unreachableNode); + const result = await ElectrumClient.testConnection(offlineNode); assert.ok(!result.connected); assert.ok(result.error); + assert.strictEqual(result.errorType, 'network'); }); it('should reconnect to the next peer when connection fails', async () => { - ElectrumClient.setActivePeer(predefinedTestnetNodes); - ElectrumClient.forceDisconnect(); - const result = await ElectrumClient.reconnect(); + ElectrumClient.setActivePeer([offlineNode, onlineNode], offlineNode); + ElectrumClient.resetCurrentPeerIndex(); + const result = await ElectrumClient.connect(); assert.ok(result.connected); - assert.ok(result.connectedTo); + assert.strictEqual(result.connectedTo, onlineNode.host); + assert.strictEqual(ElectrumClient.getActivePeer()?.host, onlineNode.host); + assert.ok(mockClients.some((client) => client.host === offlineNode.host)); }); }); @@ -60,51 +126,50 @@ describe('ElectrumClient', () => { describe('Feature Tests', () => { it('should retrieve server features', async () => { const features = await ElectrumClient.serverFeatures(); - assert.ok(features.server_version); - assert.ok(features.protocol_min); - assert.ok(features.protocol_max); + assert.strictEqual(features.server_version, 'fixture-server'); + assert.strictEqual(features.protocol_min, '1.4'); + assert.strictEqual(features.protocol_max, '1.4.3'); }); }); describe('UTXO and History Tests', () => { it('should sync UTXOs by address', async () => { - const addresses = [ - 'tb1qd2u9tvuqzadgeh02vppd33e7u2fatuwrw7h4q5', - 'tb1qwdm8hdyvv5jn05qq858lgnk50heucvxvqtl4sx', - ]; const utxos = await ElectrumClient.syncUTXOByAddress(addresses, bitcoinJS.networks.testnet); - assert.ok(Object.keys(utxos).length > 0); + assert.deepStrictEqual(utxos[addresses[0]], [ + { txId: mockTxid, vout: 1, value: 12345, height: 100, address: addresses[0] }, + ]); + assert.deepStrictEqual(utxos[addresses[1]], []); }); it('should sync history by address', async () => { - const addresses = [ - 'tb1qd2u9tvuqzadgeh02vppd33e7u2fatuwrw7h4q5', - 'tb1qwdm8hdyvv5jn05qq858lgnk50heucvxvqtl4sx', - ]; const history = await ElectrumClient.syncHistoryByAddress( addresses, bitcoinJS.networks.testnet ); - assert.ok(Object.keys(history.historyByAddress).length > 0); - assert.ok(history.txids.length > 0); + assert.deepStrictEqual(history.historyByAddress[addresses[0]], [ + { tx_hash: mockTxid, height: 100 }, + ]); + assert.deepStrictEqual(history.historyByAddress[addresses[1]], []); + assert.deepStrictEqual(history.txids, [mockTxid]); + assert.strictEqual(history.txidToAddress[mockTxid], addresses[0]); }); }); describe('Transaction Tests', () => { it('should estimate fee', async () => { const fee = await ElectrumClient.estimateFee(1); - assert.ok(fee > 0); + assert.strictEqual(fee, 2); }); it('should fetch transactions by ID', async () => { - const txids = [ - '24913105c497b4bb4ccf81b03857c8306aba0e58ccde792d981f6c18efdb24b8', - '9a15008af30580039bc7a07d36c3544902a2e1618ad31f4dda94fe0044a8dcce', - ]; + const txids = [mockTxid, '9a15008af30580039bc7a07d36c3544902a2e1618ad31f4dda94fe0044a8dcce']; const transactions = await ElectrumClient.getTransactionsById(txids); - assert.ok(Object.keys(transactions).length === txids.length); - assert.deepStrictEqual(transactions[txids[0]].txid, txids[0]); - assert.deepStrictEqual(transactions[txids[1]].txid, txids[1]); + assert.deepStrictEqual(Object.keys(transactions), txids); + for (const txid of txids) { + assert.strictEqual(transactions[txid].txid, txid); + assert.strictEqual(transactions[txid].hex, undefined); + assert.deepStrictEqual(transactions[txid].vout[0].scriptPubKey.addresses, ['tb1qfixture']); + } }); }); }); diff --git a/tests/services/electrumWalletFixture.ts b/tests/services/electrumWalletFixture.ts new file mode 100644 index 0000000000..466523c6ed --- /dev/null +++ b/tests/services/electrumWalletFixture.ts @@ -0,0 +1,79 @@ +import ElectrumClient from 'src/services/electrum/client'; +import WalletOperations from 'src/services/wallets/operations'; + +const fundingTxid = '608710eb040d935dedb6c4d6321369382b2bd45533a7442a6fa98add3860d46b'; +const previousTxid = '89b193b61f7681ee588939932219f3d92e54a1873a9fb823647038f3879fba00'; +const externalAddress = 'tb1ql7w62elx9ucw4pj5lgw4l028hmuw80sndtntxt'; + +// The wallet and vault suites exercise address scanning, transaction parsing, coin selection, +// and PSBT signing. Give each generated wallet one funded testnet address without using a +// public Electrum server or a mutable public wallet balance. +export const installElectrumWalletFixture = () => { + let fundedAddress: string | null = null; + const syncWallets = WalletOperations.syncWalletsViaElectrumClient; + + jest.spyOn(WalletOperations, 'syncWalletsViaElectrumClient').mockImplementation((...args) => { + fundedAddress = null; + return syncWallets(...args); + }); + jest.spyOn(WalletOperations, 'fetchFeeRatesByPriority').mockImplementation(async () => + WalletOperations.mockFeeRates() + ); + + jest.spyOn(ElectrumClient, 'setActivePeer').mockImplementation(() => undefined); + jest.spyOn(ElectrumClient, 'connect').mockResolvedValue({ + connected: true, + connectedTo: 'fixture.electrum.test', + }); + jest.spyOn(ElectrumClient, 'forceDisconnect').mockImplementation(() => undefined); + jest.spyOn(ElectrumClient, 'getBlockchainHeaders').mockResolvedValue({ height: 85172, hex: '00' }); + jest.spyOn(ElectrumClient, 'syncUTXOByAddress').mockImplementation(async (addresses) => { + if (!fundedAddress) fundedAddress = addresses[0]; + return Object.fromEntries( + addresses.map((address) => [ + address, + address === fundedAddress + ? [{ txId: fundingTxid, vout: 1, value: 6000, height: 85000, address }] + : [], + ]) + ); + }); + jest.spyOn(ElectrumClient, 'syncHistoryByAddress').mockImplementation(async (addresses) => { + const hasFunding = addresses.includes(fundedAddress); + return { + historyByAddress: Object.fromEntries( + addresses.map((address) => [ + address, + hasFunding && address === fundedAddress ? [{ tx_hash: fundingTxid, height: 85000 }] : [], + ]) + ), + txids: hasFunding ? [fundingTxid] : [], + txidToAddress: hasFunding ? { [fundingTxid]: fundedAddress } : {}, + }; + }); + jest.spyOn(ElectrumClient, 'getTransactionsById').mockImplementation(async (txids) => { + const transactions: Record = {}; + for (const txid of txids) { + if (txid === fundingTxid) { + transactions[txid] = { + txid, + vin: [{ txid: previousTxid, vout: 0 }], + vout: [ + { value: 0.00001, scriptPubKey: { addresses: [externalAddress] } }, + { value: 0.00006, scriptPubKey: { addresses: [fundedAddress] } }, + ], + confirmations: 12, + time: 1700000000, + blocktime: 1700000000, + }; + } else if (txid === previousTxid) { + transactions[txid] = { + txid, + vin: [], + vout: [{ value: 0.0001, scriptPubKey: { addresses: [externalAddress] } }], + }; + } + } + return transactions; + }); +}; diff --git a/tests/services/vault.test.ts b/tests/services/vault.test.ts index ed6cf6f13b..7b102a7917 100644 --- a/tests/services/vault.test.ts +++ b/tests/services/vault.test.ts @@ -43,6 +43,7 @@ import { generateOutputDescriptors, parseTextforVaultConfig, } from 'src/utils/service-utilities/utils'; +import { installElectrumWalletFixture } from './electrumWalletFixture'; jest.mock('src/store/store', () => ({ store: { @@ -56,6 +57,10 @@ jest.mock('src/store/store', () => ({ jest.mock('realm', () => ({})); +beforeAll(() => { + installElectrumWalletFixture(); +}); + const connectToElectrumClient = async () => { try { ElectrumClient.setActivePeer(predefinedTestnetNodes); @@ -67,6 +72,10 @@ const connectToElectrumClient = async () => { } }; +afterAll(() => { + ElectrumClient.forceDisconnect(); +}); + describe('Vault: Single-Sig(1-of-1)', () => { let primaryMnemonic: string; let vault: Vault; diff --git a/tests/services/wallet.test.ts b/tests/services/wallet.test.ts index dc92142d4f..b366993e46 100644 --- a/tests/services/wallet.test.ts +++ b/tests/services/wallet.test.ts @@ -12,6 +12,7 @@ import { TransactionPrerequisite, } from 'src/services/wallets/interfaces'; import { Wallet } from 'src/services/wallets/interfaces/wallet'; +import { installElectrumWalletFixture } from './electrumWalletFixture'; jest.mock('src/store/store', () => ({ store: { @@ -25,6 +26,10 @@ jest.mock('src/store/store', () => ({ jest.mock('realm', () => ({})); +beforeAll(() => { + installElectrumWalletFixture(); +}); + describe('Wallet Functionality Tests', () => { let primaryMnemonic: string; let wallet: Wallet; diff --git a/tsconfig.json b/tsconfig.json index a6af06ae6d..7e143716c9 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -18,5 +18,5 @@ "resolveJsonModule": true /* Allows importing modules with a ‘.json’ extension, which is a common practice in node projects. */, "baseUrl": "." }, - "exclude": ["node_modules", "babel.config.js", "metro.config.js", "jest.config.js"] + "exclude": ["node_modules", "dev/local-backend", "babel.config.js", "metro.config.js", "jest.config.js"] } From 58208450c88c0aabd4a44df52630c35843c16484 Mon Sep 17 00:00:00 2001 From: Anant Date: Fri, 9 Oct 2026 16:34:40 +0530 Subject: [PATCH 097/254] docs: align contributor install with sprint lock state --- dev/local-backend/README.md | 2 +- dev/local-backend/VERIFICATION.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/dev/local-backend/README.md b/dev/local-backend/README.md index 445707f9d3..eb69147c6a 100644 --- a/dev/local-backend/README.md +++ b/dev/local-backend/README.md @@ -41,7 +41,7 @@ From this app checkout's root: ./dev/local-backend/dev doctor ./dev/local-backend/dev up ./dev/local-backend/dev env -yarn install --frozen-lockfile --non-interactive +yarn install --non-interactive ``` `up` fetches the exact backend Git revisions, applies the local development adapters, builds containers, waits for readiness and runs real disposable API checks. `env` creates `.env.local` with restrictive permissions; it refuses to overwrite an existing different configuration. Existing `.env` and production files are not used or modified. diff --git a/dev/local-backend/VERIFICATION.md b/dev/local-backend/VERIFICATION.md index 7222bc86af..3279a7f36f 100644 --- a/dev/local-backend/VERIFICATION.md +++ b/dev/local-backend/VERIFICATION.md @@ -263,7 +263,7 @@ Apple Silicon Mac, macOS 26.6.2, Xcode 26.4/iOS 26.4 simulator, Node 25.9.0, Yar | Mainnet and hosted Mongo configuration rejection | PASS for relay and signing | | Actual container network/port inspection | PASS, backend network internal, no database port published, gateway bound only to loopback | | Rerun setup, existing env preservation, adapter tampering, unknown checkout preservation, duplicate ports | PASS, four regression tests plus real repeated bootstrap | -| Fresh `yarn install --frozen-lockfile --non-interactive` | PASS; no tracked dependency version changes | +| Fresh mobile Yarn install on the historical v2.5.15 acceptance tag | PASS with frozen mode at that source state; current `sprint` has unmatched lint-plugin lock selectors, so the README uses CI's non-frozen install command | | Fresh CocoaPods installation | PASS, 142 pods; see path-dependent checksum note below | | Fresh Android `assembleDevelopmentDebug` | PASS, 976 tasks executed, 5m17s | | Android installed local configuration | PASS, development environment and alternate local backend ports verified in generated BuildConfig | From 9de6fbb32a0aed6119dfb2efabcbad33679ea4a0 Mon Sep 17 00:00:00 2001 From: Anant Date: Fri, 9 Oct 2026 16:43:02 +0530 Subject: [PATCH 098/254] test(recovery): model backup status navigation dispatch --- tests/recovery/backupFeedback.test.cjs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/recovery/backupFeedback.test.cjs b/tests/recovery/backupFeedback.test.cjs index 3deccd825e..1ab803ea17 100644 --- a/tests/recovery/backupFeedback.test.cjs +++ b/tests/recovery/backupFeedback.test.cjs @@ -75,7 +75,8 @@ test('home backup warning opens comparison without starting a backup', () => { dispatch: action => actions.push(action), uaiActioned: payload => ({ type: 'uaiActioned', payload }), backupAllSignersAndVaults: () => ({ type: 'backupAllSignersAndVaults' }), - navigation: { navigate: route => destinations.push(route) }, + CommonActions: { navigate: route => route }, + navigation: { dispatch: route => destinations.push(route) }, }); assert.deepEqual(destinations, ['AssistedBackupStatus']); assert.ok(!actions.some(action => action.type === 'backupAllSignersAndVaults'), 'view details must not write a backup before the user chooses Back Up Now'); From 8026e0bbb4f33477cef3878cd09e3c23839603c5 Mon Sep 17 00:00:00 2001 From: Anant Date: Fri, 9 Oct 2026 16:43:37 +0530 Subject: [PATCH 099/254] docs(contributor): use corrected frozen R4 lock --- dev/local-backend/README.md | 2 +- dev/local-backend/VERIFICATION.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/dev/local-backend/README.md b/dev/local-backend/README.md index eb69147c6a..445707f9d3 100644 --- a/dev/local-backend/README.md +++ b/dev/local-backend/README.md @@ -41,7 +41,7 @@ From this app checkout's root: ./dev/local-backend/dev doctor ./dev/local-backend/dev up ./dev/local-backend/dev env -yarn install --non-interactive +yarn install --frozen-lockfile --non-interactive ``` `up` fetches the exact backend Git revisions, applies the local development adapters, builds containers, waits for readiness and runs real disposable API checks. `env` creates `.env.local` with restrictive permissions; it refuses to overwrite an existing different configuration. Existing `.env` and production files are not used or modified. diff --git a/dev/local-backend/VERIFICATION.md b/dev/local-backend/VERIFICATION.md index 3279a7f36f..a22f83ad33 100644 --- a/dev/local-backend/VERIFICATION.md +++ b/dev/local-backend/VERIFICATION.md @@ -263,7 +263,7 @@ Apple Silicon Mac, macOS 26.6.2, Xcode 26.4/iOS 26.4 simulator, Node 25.9.0, Yar | Mainnet and hosted Mongo configuration rejection | PASS for relay and signing | | Actual container network/port inspection | PASS, backend network internal, no database port published, gateway bound only to loopback | | Rerun setup, existing env preservation, adapter tampering, unknown checkout preservation, duplicate ports | PASS, four regression tests plus real repeated bootstrap | -| Fresh mobile Yarn install on the historical v2.5.15 acceptance tag | PASS with frozen mode at that source state; current `sprint` has unmatched lint-plugin lock selectors, so the README uses CI's non-frozen install command | +| Fresh mobile Yarn install on the historical v2.5.15 acceptance tag | PASS with frozen mode at that source state. The source PR's `sprint` lock had unmatched lint-plugin selectors; the R4 combined candidate includes a corrected lock and uses frozen install, which passed in hosted diagnostic CI. | | Fresh CocoaPods installation | PASS, 142 pods; see path-dependent checksum note below | | Fresh Android `assembleDevelopmentDebug` | PASS, 976 tasks executed, 5m17s | | Android installed local configuration | PASS, development environment and alternate local backend ports verified in generated BuildConfig | From 890c918d516b372cad4e5d815635f3ae86527160 Mon Sep 17 00:00:00 2001 From: Anant Date: Fri, 9 Oct 2026 16:44:05 +0530 Subject: [PATCH 100/254] test(electrum): retain explicit reconnect coverage --- tests/services/client.test.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/services/client.test.ts b/tests/services/client.test.ts index 173052a056..a04a186206 100644 --- a/tests/services/client.test.ts +++ b/tests/services/client.test.ts @@ -108,6 +108,11 @@ describe('ElectrumClient', () => { assert.ok(result.connected); assert.strictEqual(result.connectedTo, onlineNode.host); assert.strictEqual(ElectrumClient.getActivePeer()?.host, onlineNode.host); + + ElectrumClient.forceDisconnect(); + const reconnected = await ElectrumClient.reconnect(); + assert.ok(reconnected.connected); + assert.strictEqual(reconnected.connectedTo, onlineNode.host); assert.ok(mockClients.some((client) => client.host === offlineNode.host)); }); }); From b706475138eee9728672365270759ea7090e3d59 Mon Sep 17 00:00:00 2001 From: Anant Date: Fri, 9 Oct 2026 16:49:46 +0530 Subject: [PATCH 101/254] Upload short-lived diagnostic APK for inspection --- .../workflows/android-diagnostic-build.yml | 24 +++++++++++++++++++ reproducibility/README.md | 12 +++++++--- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/.github/workflows/android-diagnostic-build.yml b/.github/workflows/android-diagnostic-build.yml index 6ce856f760..549cb39f92 100644 --- a/.github/workflows/android-diagnostic-build.yml +++ b/.github/workflows/android-diagnostic-build.yml @@ -125,3 +125,27 @@ jobs: unzip -tq "$aab" >/dev/null sha256sum "$apk" "$aab" echo 'Diagnostic APK/AAB built with placeholder config and debug signing; no reproducibility verdict.' + + - name: Stage diagnostic APK and provenance + env: + SOURCE_COMMIT: ${{ github.event.pull_request.head.sha || github.sha }} + run: | + apk=android/app/build/outputs/apk/production/release/app-production-release.apk + artifact_dir="$RUNNER_TEMP/android-diagnostic-apk" + mkdir -p "$artifact_dir" + cp "$apk" "$artifact_dir/keeper-diagnostic-production-release.apk" + { + printf 'source_commit=%s\n' "$SOURCE_COMMIT" + printf 'workflow_run_id=%s\n' "$GITHUB_RUN_ID" + printf 'apk_sha256=%s\n' "$(sha256sum "$artifact_dir/keeper-diagnostic-production-release.apk" | cut -d ' ' -f1)" + printf 'configuration=placeholder\nsigning=public-debug-key\npurpose=package-inspection-only\n' + } > "$artifact_dir/provenance.txt" + + - name: Upload diagnostic APK for inspection + uses: actions/upload-artifact@v4 + with: + name: android-diagnostic-apk-${{ github.event.pull_request.head.sha || github.sha }} + path: ${{ runner.temp }}/android-diagnostic-apk/ + if-no-files-found: error + retention-days: 7 + compression-level: 0 diff --git a/reproducibility/README.md b/reproducibility/README.md index 32e7f0eb42..e40baa5341 100644 --- a/reproducibility/README.md +++ b/reproducibility/README.md @@ -9,10 +9,16 @@ does not yet establish a reproducibility verdict for v2.6.3. The [Android diagnostic build workflow](../.github/workflows/android-diagnostic-build.yml) is set up to compile an APK and AAB from the combined draft with the live public channel URL and `ENVIRONMENT=PRODUCTION`, dummy service endpoints and IDs, and -the checked-in debug key. It also checks that Gradle rejects a retired GasFree -credential. A successful run would verify that the draft compiles in a clean CI +the checked-in debug key. It verifies the exact PR source and pinned Android +inputs before Gradle, and checks that Gradle rejects a retired GasFree +credential. A successful run verifies that the draft compiles in a clean CI checkout; its outputs cannot be compared to a production release for a -reproducibility verdict. The workflow does not publish the diagnostic artifacts. +reproducibility verdict. After validation, the workflow keeps only the debug +APK and its source/hash provenance as a seven-day CI artifact for package +inspection. Placeholder Relay settings block fresh account setup, so this +artifact alone does not enable native pairing smoke. Pairing QA needs a +separately reviewed disposable configuration or seeded account. The CI artifact +is not a signed production release. ## v2.6.3 reference and measured result From 8bb899d505bd4e7adbfabe5d3dc832232410d9d9 Mon Sep 17 00:00:00 2001 From: Anant Date: Fri, 9 Oct 2026 17:08:00 +0530 Subject: [PATCH 102/254] fix(usdt): keep unconfirmed history pending --- src/components/TransactionElement.tsx | 2 +- .../wallets/factories/USDTWalletFactory.ts | 10 ++-- .../wallets/operations/dollars/USDT.ts | 6 +- tests/services/usdtPause.test.cjs | 60 ++++++++++++++++--- 4 files changed, 62 insertions(+), 16 deletions(-) diff --git a/src/components/TransactionElement.tsx b/src/components/TransactionElement.tsx index 2f58433746..e0c946623f 100644 --- a/src/components/TransactionElement.tsx +++ b/src/components/TransactionElement.tsx @@ -56,7 +56,7 @@ function TransactionElement({ (transaction as USDTTransaction).txId || (transaction as USDTTransaction).traceId; date = (transaction as USDTTransaction).timestamp; amount = parseFloat((transaction as USDTTransaction).amount); - confirmations = (transaction as USDTTransaction).blockNumber ? 6 : 0; // Assuming 6 confirmations for USDT transactions(no. of conf on Tron doesn't really make any finality sense) + confirmations = (transaction as USDTTransaction).blockNumber ? 1 : 0; // Only used for the pending indicator. if ( (transaction as USDTTransaction).to === (wallet as USDTWallet).accountStatus.gasFreeAddress ) { diff --git a/src/services/wallets/factories/USDTWalletFactory.ts b/src/services/wallets/factories/USDTWalletFactory.ts index 9543d2b8e8..e29a9c504d 100644 --- a/src/services/wallets/factories/USDTWalletFactory.ts +++ b/src/services/wallets/factories/USDTWalletFactory.ts @@ -7,7 +7,6 @@ import { import USDT, { USDTAccountStatus, USDTTransaction } from '../operations/dollars/USDT'; import BIP85 from '../operations/BIP85'; import { BIP85Config } from '../interfaces'; -import { GasFreeTransferStatus } from '../operations/dollars/GasFree'; import config from 'src/utils/service-utilities/config'; export const USDTWalletSupportedNetwork = config.isDevMode() @@ -263,9 +262,10 @@ export const syncUSDTWalletTransactions = async (wallet: USDTWallet) => { if (existingTx.amount === newTx.amount) { // there are two transactions for every gas-free transfer, one for the actual transfer and one for paying the fee(both of them have the same txid, skipping the fee transfer) + // Reconcile both upgrades and corrections to a cached status. Older + // versions could mark a timestamped, unconfirmed transfer as complete. const shouldUpdate = - // Update if new transaction has blockNumber but existing doesn't (confirmed) - newTx.blockNumber && !existingTx.blockNumber; + existingTx.status !== newTx.status || existingTx.blockNumber !== newTx.blockNumber; if (shouldUpdate) { mergedTransactions[existingIndex] = { @@ -275,9 +275,7 @@ export const syncUSDTWalletTransactions = async (wallet: USDTWallet) => { to: newTx.to, timestamp: newTx.timestamp, blockNumber: newTx.blockNumber, - status: newTx.blockNumber - ? GasFreeTransferStatus.SUCCEED - : GasFreeTransferStatus.CONFIRMING, + status: newTx.status, }; } } diff --git a/src/services/wallets/operations/dollars/USDT.ts b/src/services/wallets/operations/dollars/USDT.ts index 5b490c0a97..943834537a 100644 --- a/src/services/wallets/operations/dollars/USDT.ts +++ b/src/services/wallets/operations/dollars/USDT.ts @@ -177,9 +177,11 @@ export default class USDT { from: txn.from, to: txn.to, amount: txn.formattedValue.toString(), - status: txn.blockNumber ? GasFreeTransferStatus.SUCCEED : GasFreeTransferStatus.CONFIRMING, + status: txn.confirmed ? GasFreeTransferStatus.SUCCEED : GasFreeTransferStatus.CONFIRMING, timestamp: txn.blockTimestamp, - blockNumber: txn.blockNumber, + // TronGrid can return a timestamp for an unconfirmed transfer. Keep + // the stored confirmation marker clear until the API confirms it. + blockNumber: txn.confirmed ? txn.blockNumber : 0, isGasFree: txn.to !== address, })); return { transactions, meta: result.meta }; diff --git a/tests/services/usdtPause.test.cjs b/tests/services/usdtPause.test.cjs index a16c7a58c3..1e8ac43c5e 100644 --- a/tests/services/usdtPause.test.cjs +++ b/tests/services/usdtPause.test.cjs @@ -48,9 +48,19 @@ const tron = { from: 'TStoredGasFreeAddress', to: 'TRecipient', formattedValue: 2, + confirmed: true, blockNumber: 1, blockTimestamp: 100, }, + { + transactionId: 'chain-pending', + from: 'TStoredGasFreeAddress', + to: 'TRecipient', + formattedValue: 1, + confirmed: false, + blockNumber: 75, + blockTimestamp: 75, + }, ], meta: { fingerprint: '', hasMore: false }, }; @@ -129,15 +139,19 @@ test('stored address, chain reads and ambiguous historical request survive the p const transactions = await factory.syncUSDTWalletTransactions(wallet); assert.equal(historyCalls[0][0], wallet.accountStatus.gasFreeAddress); - assert.equal(transactions.length, 2); + assert.equal(transactions.length, 3); assert.equal(transactions[0].txId, 'chain-1'); - assert.equal(transactions[1].traceId, 'historical-trace'); - assert.equal(transactions[0].to, transactions[1].to); - assert.equal(transactions[0].amount, transactions[1].amount); - assert.equal(transactions[1].txId, undefined); + assert.equal(transactions[0].status, historical.GasFreeTransferStatus.SUCCEED); + assert.equal(transactions[1].txId, 'chain-pending'); + assert.equal(transactions[1].status, historical.GasFreeTransferStatus.CONFIRMING); + assert.equal(transactions[1].blockNumber, 0); + assert.equal(transactions[2].traceId, 'historical-trace'); + assert.equal(transactions[0].to, transactions[2].to); + assert.equal(transactions[0].amount, transactions[2].amount); + assert.equal(transactions[2].txId, undefined); assert.equal(usdtModule.isHistoricalUnverifiedUSDTRequest(transactions[0]), false); - assert.equal(usdtModule.isHistoricalUnverifiedUSDTRequest(transactions[1]), true); - assert.equal(transactions[1].status, historical.GasFreeTransferStatus.WAITING); + assert.equal(usdtModule.isHistoricalUnverifiedUSDTRequest(transactions[2]), true); + assert.equal(transactions[2].status, historical.GasFreeTransferStatus.WAITING); assert.equal( usdtModule.isHistoricalUnverifiedUSDTRequest({ traceId: 'old-request', @@ -148,6 +162,38 @@ test('stored address, chain reads and ambiguous historical request survive the p ); }); +test('refresh corrects a cached false success without dropping its trace', async () => { + const cached = { + ...wallet, + specs: { + ...wallet.specs, + transactions: [ + ...wallet.specs.transactions, + { + txId: 'chain-pending', + traceId: 'saved-provider-trace', + from: 'TStoredGasFreeAddress', + to: 'TRecipient', + amount: '1', + status: historical.GasFreeTransferStatus.SUCCEED, + timestamp: 75, + blockNumber: 75, + isGasFree: true, + }, + ], + }, + }; + const transactions = await factory.syncUSDTWalletTransactions(cached); + const pending = transactions.find((tx) => tx.txId === 'chain-pending'); + assert.equal(pending.status, historical.GasFreeTransferStatus.CONFIRMING); + assert.equal(pending.blockNumber, 0); + assert.equal(pending.traceId, 'saved-provider-trace'); + assert.equal( + transactions.find((tx) => tx.traceId === 'historical-trace').status, + historical.GasFreeTransferStatus.WAITING + ); +}); + test('missing saved address is never replaced by the owner address', async () => { const missing = { ...wallet, accountStatus: { ...wallet.accountStatus, gasFreeAddress: '' } }; await assert.rejects(factory.syncUSDTWalletBalance(missing), /Stored USDT address/); From 9bd85d071389cf315f31708c78a93d1d42ff04d5 Mon Sep 17 00:00:00 2001 From: Anant Date: Fri, 9 Oct 2026 17:09:17 +0530 Subject: [PATCH 103/254] test(usdt): require timestamp for confirmed history marker --- .../wallets/operations/dollars/USDT.ts | 27 ++++++++++--------- tests/services/usdtPause.test.cjs | 14 +++++++++- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/src/services/wallets/operations/dollars/USDT.ts b/src/services/wallets/operations/dollars/USDT.ts index 943834537a..1f4adf8373 100644 --- a/src/services/wallets/operations/dollars/USDT.ts +++ b/src/services/wallets/operations/dollars/USDT.ts @@ -172,18 +172,21 @@ export default class USDT { }> { const contract = USDT.getUSDTAddress(networkType); const result = await getTrc20Transactions(address, contract, networkType, limit, fingerprint); - const transactions = result.transactions.map((txn) => ({ - txId: txn.transactionId, - from: txn.from, - to: txn.to, - amount: txn.formattedValue.toString(), - status: txn.confirmed ? GasFreeTransferStatus.SUCCEED : GasFreeTransferStatus.CONFIRMING, - timestamp: txn.blockTimestamp, - // TronGrid can return a timestamp for an unconfirmed transfer. Keep - // the stored confirmation marker clear until the API confirms it. - blockNumber: txn.confirmed ? txn.blockNumber : 0, - isGasFree: txn.to !== address, - })); + const transactions = result.transactions.map((txn) => { + // TronGrid can return a timestamp for an unconfirmed transfer. Require + // both fields so the stored status and pending indicator agree. + const confirmed = Boolean(txn.confirmed && txn.blockNumber); + return { + txId: txn.transactionId, + from: txn.from, + to: txn.to, + amount: txn.formattedValue.toString(), + status: confirmed ? GasFreeTransferStatus.SUCCEED : GasFreeTransferStatus.CONFIRMING, + timestamp: txn.blockTimestamp, + blockNumber: confirmed ? txn.blockNumber : 0, + isGasFree: txn.to !== address, + }; + }); return { transactions, meta: result.meta }; } } diff --git a/tests/services/usdtPause.test.cjs b/tests/services/usdtPause.test.cjs index 1e8ac43c5e..58e2fbb44b 100644 --- a/tests/services/usdtPause.test.cjs +++ b/tests/services/usdtPause.test.cjs @@ -61,6 +61,15 @@ const tron = { blockNumber: 75, blockTimestamp: 75, }, + { + transactionId: 'chain-no-time', + from: 'TStoredGasFreeAddress', + to: 'TRecipient', + formattedValue: 1, + confirmed: true, + blockNumber: 0, + blockTimestamp: 0, + }, ], meta: { fingerprint: '', hasMore: false }, }; @@ -139,7 +148,7 @@ test('stored address, chain reads and ambiguous historical request survive the p const transactions = await factory.syncUSDTWalletTransactions(wallet); assert.equal(historyCalls[0][0], wallet.accountStatus.gasFreeAddress); - assert.equal(transactions.length, 3); + assert.equal(transactions.length, 4); assert.equal(transactions[0].txId, 'chain-1'); assert.equal(transactions[0].status, historical.GasFreeTransferStatus.SUCCEED); assert.equal(transactions[1].txId, 'chain-pending'); @@ -152,6 +161,9 @@ test('stored address, chain reads and ambiguous historical request survive the p assert.equal(usdtModule.isHistoricalUnverifiedUSDTRequest(transactions[0]), false); assert.equal(usdtModule.isHistoricalUnverifiedUSDTRequest(transactions[2]), true); assert.equal(transactions[2].status, historical.GasFreeTransferStatus.WAITING); + assert.equal(transactions[3].txId, 'chain-no-time'); + assert.equal(transactions[3].status, historical.GasFreeTransferStatus.CONFIRMING); + assert.equal(transactions[3].blockNumber, 0); assert.equal( usdtModule.isHistoricalUnverifiedUSDTRequest({ traceId: 'old-request', From ebf1b7d649dde9bdafbdca36b400a0d6146d7185 Mon Sep 17 00:00:00 2001 From: Anant Date: Fri, 9 Oct 2026 17:15:02 +0530 Subject: [PATCH 104/254] fix(usdt): trust filtered confirmed TRC20 history --- .../wallets/factories/USDTWalletFactory.ts | 3 +- .../wallets/operations/dollars/Tron.ts | 5 +- tests/services/tronConfirmedHistory.test.cjs | 122 ++++++++++++++++++ tests/services/usdtPause.test.cjs | 52 +++----- 4 files changed, 147 insertions(+), 35 deletions(-) create mode 100644 tests/services/tronConfirmedHistory.test.cjs diff --git a/src/services/wallets/factories/USDTWalletFactory.ts b/src/services/wallets/factories/USDTWalletFactory.ts index e29a9c504d..b7439027bb 100644 --- a/src/services/wallets/factories/USDTWalletFactory.ts +++ b/src/services/wallets/factories/USDTWalletFactory.ts @@ -262,8 +262,7 @@ export const syncUSDTWalletTransactions = async (wallet: USDTWallet) => { if (existingTx.amount === newTx.amount) { // there are two transactions for every gas-free transfer, one for the actual transfer and one for paying the fee(both of them have the same txid, skipping the fee transfer) - // Reconcile both upgrades and corrections to a cached status. Older - // versions could mark a timestamped, unconfirmed transfer as complete. + // Reconcile cached status with the matching confirmed chain entry. const shouldUpdate = existingTx.status !== newTx.status || existingTx.blockNumber !== newTx.blockNumber; diff --git a/src/services/wallets/operations/dollars/Tron.ts b/src/services/wallets/operations/dollars/Tron.ts index 08ed3a5f6e..c776e48ccd 100644 --- a/src/services/wallets/operations/dollars/Tron.ts +++ b/src/services/wallets/operations/dollars/Tron.ts @@ -221,6 +221,7 @@ export async function getTrc20Transactions( const params = new URLSearchParams({ limit: limit.toString(), contract_address: tokenContract, + only_confirmed: 'true', }); if (fingerprint) { @@ -262,7 +263,9 @@ export async function getTrc20Transactions( value: rawValue, formattedValue, contractAddress: tx.token_info?.address || tokenContract, - confirmed: tx.confirmed || false, + // The TRC20 response row need not carry a confirmation flag. The + // request filter establishes it unless a row explicitly contradicts it. + confirmed: tx.confirmed !== false, tokenInfo: tx.token_info ? { symbol: tx.token_info.symbol, diff --git a/tests/services/tronConfirmedHistory.test.cjs b/tests/services/tronConfirmedHistory.test.cjs new file mode 100644 index 0000000000..321965b329 --- /dev/null +++ b/tests/services/tronConfirmedHistory.test.cjs @@ -0,0 +1,122 @@ +const { test } = require('node:test'); +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const path = require('node:path'); +const vm = require('node:vm'); +const ts = require('typescript'); + +const source = fs.readFileSync( + path.join(__dirname, '../../src/services/wallets/operations/dollars/Tron.ts'), + 'utf8' +); +const code = ts.transpileModule(source, { + compilerOptions: { + module: ts.ModuleKind.CommonJS, + target: ts.ScriptTarget.ES2020, + esModuleInterop: true, + }, +}).outputText; + +test('TRC20 history requests confirmed rows and keeps the pagination filter', async () => { + const requested = []; + const imports = { + '../../enums': { NetworkType: { MAINNET: 'MAINNET', TESTNET: 'TESTNET' } }, + bip39: {}, + bip32: () => ({}), + '../taproot-utils/noble_ecc': {}, + 'bitcoinjs-lib': {}, + tronweb: { + TronWeb: class { + isAddress() { + return true; + } + }, + }, + }; + const fetch = async (url) => { + requested.push(url); + return { + ok: true, + json: async () => ({ + success: true, + data: [ + { + transaction_id: 'disposable-confirmed', + block_timestamp: 100000, + from: 'TFrom', + to: 'TTo', + type: 'Transfer', + value: '2000000', + token_info: { address: 'TContract', decimals: 6 }, + }, + ], + meta: { fingerprint: 'next-page', page_size: 2 }, + }), + }; + }; + const module = { exports: {} }; + vm.runInNewContext(`(function (require, module, exports) { ${code}\n})`, { + URLSearchParams, + fetch, + })( + (name) => { + if (!(name in imports)) throw new Error(`Unexpected import: ${name}`); + return imports[name]; + }, + module, + module.exports + ); + + const result = await module.exports.getTrc20Transactions( + 'TStoredAddress', + 'TContract', + 'MAINNET', + 2, + 'prior-page' + ); + const url = new URL(requested[0]); + assert.equal(url.searchParams.get('only_confirmed'), 'true'); + assert.equal(url.searchParams.has('only_unconfirmed'), false); + assert.equal(url.searchParams.get('fingerprint'), 'prior-page'); + assert.equal(url.searchParams.get('limit'), '2'); + assert.equal(url.searchParams.get('contract_address'), 'TContract'); + assert.equal(result.transactions[0].confirmed, true); + assert.equal(result.transactions[0].blockNumber, 100); + assert.equal(result.meta.fingerprint, 'next-page'); + assert.equal(result.meta.hasMore, true); + + const usdtSource = fs.readFileSync( + path.join(__dirname, '../../src/services/wallets/operations/dollars/USDT.ts'), + 'utf8' + ); + const usdtCode = ts.transpileModule(usdtSource, { + compilerOptions: { + module: ts.ModuleKind.CommonJS, + target: ts.ScriptTarget.ES2020, + esModuleInterop: true, + }, + }).outputText; + const usdtModule = { exports: {} }; + vm.runInNewContext(`(function (require, module, exports) { ${usdtCode}\n})`)( + (name) => { + if (name === './Tron') return module.exports; + if (name === './GasFree') + return { GasFreeTransferStatus: { SUCCEED: 'SUCCEED', CONFIRMING: 'CONFIRMING' } }; + if (name === 'src/services/wallets/enums') return imports['../../enums']; + throw new Error(`Unexpected import: ${name}`); + }, + usdtModule, + usdtModule.exports + ); + const mapped = await usdtModule.exports.default.getUSDTTransactions( + 'TStoredAddress', + 'MAINNET', + 2, + 'prior-page' + ); + assert.equal(mapped.transactions[0].status, 'SUCCEED'); + assert.equal(mapped.transactions[0].blockNumber, 100); + assert.equal(mapped.meta.fingerprint, 'next-page'); + assert.equal(new URL(requested[1]).searchParams.get('only_confirmed'), 'true'); + assert.equal(new URL(requested[1]).searchParams.get('fingerprint'), 'prior-page'); +}); diff --git a/tests/services/usdtPause.test.cjs b/tests/services/usdtPause.test.cjs index 58e2fbb44b..b9d2fe0ea7 100644 --- a/tests/services/usdtPause.test.cjs +++ b/tests/services/usdtPause.test.cjs @@ -52,15 +52,6 @@ const tron = { blockNumber: 1, blockTimestamp: 100, }, - { - transactionId: 'chain-pending', - from: 'TStoredGasFreeAddress', - to: 'TRecipient', - formattedValue: 1, - confirmed: false, - blockNumber: 75, - blockTimestamp: 75, - }, { transactionId: 'chain-no-time', from: 'TStoredGasFreeAddress', @@ -148,22 +139,19 @@ test('stored address, chain reads and ambiguous historical request survive the p const transactions = await factory.syncUSDTWalletTransactions(wallet); assert.equal(historyCalls[0][0], wallet.accountStatus.gasFreeAddress); - assert.equal(transactions.length, 4); + assert.equal(transactions.length, 3); assert.equal(transactions[0].txId, 'chain-1'); assert.equal(transactions[0].status, historical.GasFreeTransferStatus.SUCCEED); - assert.equal(transactions[1].txId, 'chain-pending'); - assert.equal(transactions[1].status, historical.GasFreeTransferStatus.CONFIRMING); - assert.equal(transactions[1].blockNumber, 0); - assert.equal(transactions[2].traceId, 'historical-trace'); - assert.equal(transactions[0].to, transactions[2].to); - assert.equal(transactions[0].amount, transactions[2].amount); - assert.equal(transactions[2].txId, undefined); + assert.equal(transactions[1].traceId, 'historical-trace'); + assert.equal(transactions[0].to, transactions[1].to); + assert.equal(transactions[0].amount, transactions[1].amount); + assert.equal(transactions[1].txId, undefined); assert.equal(usdtModule.isHistoricalUnverifiedUSDTRequest(transactions[0]), false); - assert.equal(usdtModule.isHistoricalUnverifiedUSDTRequest(transactions[2]), true); - assert.equal(transactions[2].status, historical.GasFreeTransferStatus.WAITING); - assert.equal(transactions[3].txId, 'chain-no-time'); - assert.equal(transactions[3].status, historical.GasFreeTransferStatus.CONFIRMING); - assert.equal(transactions[3].blockNumber, 0); + assert.equal(usdtModule.isHistoricalUnverifiedUSDTRequest(transactions[1]), true); + assert.equal(transactions[1].status, historical.GasFreeTransferStatus.WAITING); + assert.equal(transactions[2].txId, 'chain-no-time'); + assert.equal(transactions[2].status, historical.GasFreeTransferStatus.CONFIRMING); + assert.equal(transactions[2].blockNumber, 0); assert.equal( usdtModule.isHistoricalUnverifiedUSDTRequest({ traceId: 'old-request', @@ -174,7 +162,7 @@ test('stored address, chain reads and ambiguous historical request survive the p ); }); -test('refresh corrects a cached false success without dropping its trace', async () => { +test('refresh upgrades a cached pending entry when its confirmed chain record appears', async () => { const cached = { ...wallet, specs: { @@ -182,24 +170,24 @@ test('refresh corrects a cached false success without dropping its trace', async transactions: [ ...wallet.specs.transactions, { - txId: 'chain-pending', + txId: 'chain-1', traceId: 'saved-provider-trace', from: 'TStoredGasFreeAddress', to: 'TRecipient', - amount: '1', - status: historical.GasFreeTransferStatus.SUCCEED, - timestamp: 75, - blockNumber: 75, + amount: '2', + status: historical.GasFreeTransferStatus.CONFIRMING, + timestamp: 90, + blockNumber: 0, isGasFree: true, }, ], }, }; const transactions = await factory.syncUSDTWalletTransactions(cached); - const pending = transactions.find((tx) => tx.txId === 'chain-pending'); - assert.equal(pending.status, historical.GasFreeTransferStatus.CONFIRMING); - assert.equal(pending.blockNumber, 0); - assert.equal(pending.traceId, 'saved-provider-trace'); + const confirmed = transactions.find((tx) => tx.txId === 'chain-1'); + assert.equal(confirmed.status, historical.GasFreeTransferStatus.SUCCEED); + assert.equal(confirmed.blockNumber, 1); + assert.equal(confirmed.traceId, 'saved-provider-trace'); assert.equal( transactions.find((tx) => tx.traceId === 'historical-trace').status, historical.GasFreeTransferStatus.WAITING From 7784bd03269ec282ffafb9ae24d2f255524a4fe6 Mon Sep 17 00:00:00 2001 From: Anant Date: Fri, 9 Oct 2026 17:23:52 +0530 Subject: [PATCH 105/254] fix(usdt): distinguish confirmed chain history from legacy success --- src/components/TransactionElement.tsx | 14 +- src/screens/USDT/UsdtTransactionDetail.tsx | 4 +- src/screens/USDT/components/StatusContent.tsx | 18 +- .../wallets/factories/USDTWalletFactory.ts | 10 +- .../wallets/operations/dollars/GasFree.ts | 3 + .../wallets/operations/dollars/USDT.ts | 11 +- .../usdtCacheStatusPresentation.test.cjs | 156 ++++++++++++++++++ tests/services/tronConfirmedHistory.test.cjs | 9 +- tests/services/usdtPause.test.cjs | 67 +++++++- 9 files changed, 274 insertions(+), 18 deletions(-) create mode 100644 tests/components/usdtCacheStatusPresentation.test.cjs diff --git a/src/components/TransactionElement.tsx b/src/components/TransactionElement.tsx index e0c946623f..401e0f0121 100644 --- a/src/components/TransactionElement.tsx +++ b/src/components/TransactionElement.tsx @@ -23,8 +23,10 @@ import { Wallet } from 'src/services/wallets/interfaces/wallet'; import { Vault } from 'src/services/wallets/interfaces/vault'; import { isHistoricalUnverifiedUSDTRequest, + isUSDTStatusUnavailable, USDTTransaction, } from 'src/services/wallets/operations/dollars/USDT'; +import { GasFreeTransferStatus } from 'src/services/wallets/operations/dollars/GasFree'; import { EntityKind } from 'src/services/wallets/enums'; import { USDTWallet } from 'src/services/wallets/factories/USDTWalletFactory'; import ThemedColor from './ThemedColor/ThemedColor'; @@ -51,12 +53,16 @@ function TransactionElement({ const legacyUSDTRequest = wallet.entityKind === EntityKind.USDT_WALLET && isHistoricalUnverifiedUSDTRequest(transaction as USDTTransaction); + const unavailableUSDTStatus = + wallet.entityKind === EntityKind.USDT_WALLET && + isUSDTStatusUnavailable(transaction as USDTTransaction); if (wallet.entityKind === EntityKind.USDT_WALLET) { transactionId = (transaction as USDTTransaction).txId || (transaction as USDTTransaction).traceId; date = (transaction as USDTTransaction).timestamp; amount = parseFloat((transaction as USDTTransaction).amount); - confirmations = (transaction as USDTTransaction).blockNumber ? 1 : 0; // Only used for the pending indicator. + confirmations = + (transaction as USDTTransaction).status === GasFreeTransferStatus.CHAIN_CONFIRMED ? 1 : 0; if ( (transaction as USDTTransaction).to === (wallet as USDTWallet).accountStatus.gasFreeAddress ) { @@ -81,7 +87,7 @@ function TransactionElement({ - {confirmations === 0 && !isCached && !legacyUSDTRequest && ( + {confirmations === 0 && !isCached && !unavailableUSDTStatus && ( @@ -124,7 +130,7 @@ function TransactionElement({ {formattedDate} - {legacyUSDTRequest && ( + {unavailableUSDTStatus && ( {translations.usdtWalletText.statusUnavailable} diff --git a/src/screens/USDT/UsdtTransactionDetail.tsx b/src/screens/USDT/UsdtTransactionDetail.tsx index 2060fd3dc5..3b1e0778b6 100644 --- a/src/screens/USDT/UsdtTransactionDetail.tsx +++ b/src/screens/USDT/UsdtTransactionDetail.tsx @@ -20,6 +20,7 @@ import { current } from '@reduxjs/toolkit'; import StatusContent from './components/StatusContent'; import { isHistoricalUnverifiedUSDTRequest, + isUSDTStatusUnavailable, USDTTransaction, } from 'src/services/wallets/operations/dollars/USDT'; import { USDTWallet } from 'src/services/wallets/factories/USDTWalletFactory'; @@ -56,6 +57,7 @@ const UsdtTransactionDetail = ({ route }) => { // A trace without a chain transaction ID is a historical provider request. // Its final result cannot be established after the provider was retired. const legacyRequest = isHistoricalUnverifiedUSDTRequest(transaction); + const unavailableStatus = isUSDTStatusUnavailable(transaction); const status = transaction.status; let transactionType: string; if (transaction.to === wallet.accountStatus.gasFreeAddress) { @@ -219,7 +221,7 @@ const UsdtTransactionDetail = ({ route }) => { title={usdtWalletText.status} showIcon={false} letterSpacing={2.4} - Content={() => } + Content={() => } /> {legacyRequest && ( { const { colorMode } = useColorMode(); const { translations } = useContext(LocalizationContext); - const containerbackgroundColor = unavailable + // A legacy SUCCEED value is not proof of a confirmed chain transaction. + const statusUnavailable = + unavailable || + status === GasFreeTransferStatus.SUCCEED || + status === GasFreeTransferStatus.UNVERIFIED; + const chainConfirmed = status === GasFreeTransferStatus.CHAIN_CONFIRMED; + const containerbackgroundColor = statusUnavailable ? Colors.lightindigoblue - : status === GasFreeTransferStatus.SUCCEED + : chainConfirmed ? Colors.PaleTropicalTeal : status === GasFreeTransferStatus.CONFIRMING ? Colors.lightOrange : Colors.lightindigoblue; - const textColor = unavailable + const textColor = statusUnavailable ? Colors.indigoblue - : status === GasFreeTransferStatus.SUCCEED + : chainConfirmed ? Colors.TropicalTeal : status === GasFreeTransferStatus.CONFIRMING ? Colors.darkOrange @@ -32,9 +38,9 @@ const StatusContent = ({ status, unavailable = false }) => { style={styles.container} > - {unavailable + {statusUnavailable ? translations.usdtWalletText.statusUnavailable - : status === GasFreeTransferStatus.SUCCEED + : chainConfirmed ? 'SUCCESS' : status} diff --git a/src/services/wallets/factories/USDTWalletFactory.ts b/src/services/wallets/factories/USDTWalletFactory.ts index b7439027bb..ffbad27df4 100644 --- a/src/services/wallets/factories/USDTWalletFactory.ts +++ b/src/services/wallets/factories/USDTWalletFactory.ts @@ -5,6 +5,7 @@ import { DEFAULT_TRON_DERIVATION_PATH, } from '../operations/dollars/Tron'; import USDT, { USDTAccountStatus, USDTTransaction } from '../operations/dollars/USDT'; +import { GasFreeTransferStatus } from '../operations/dollars/GasFree'; import BIP85 from '../operations/BIP85'; import { BIP85Config } from '../interfaces'; import config from 'src/utils/service-utilities/config'; @@ -229,8 +230,13 @@ export const syncUSDTWalletTransactions = async (wallet: USDTWallet) => { throw new Error('Stored USDT address is unavailable'); } - // Preserve trace-only entries without assigning a false failure status. - const updatedExistingTransactions = existingTransactions; + // Older builds could mark a provider-reported transfer successful before it + // appeared in confirmed chain history. Keep the record but withdraw that claim. + const updatedExistingTransactions = existingTransactions.map((transaction) => + transaction.status === GasFreeTransferStatus.SUCCEED + ? { ...transaction, status: GasFreeTransferStatus.UNVERIFIED } + : transaction + ); // Fetch new transactions directly from TRON. const { transactions: newTransactions } = await USDT.getUSDTTransactions( diff --git a/src/services/wallets/operations/dollars/GasFree.ts b/src/services/wallets/operations/dollars/GasFree.ts index 3c9507a7aa..7c73a63eae 100644 --- a/src/services/wallets/operations/dollars/GasFree.ts +++ b/src/services/wallets/operations/dollars/GasFree.ts @@ -3,6 +3,9 @@ export enum GasFreeTransferStatus { WAITING = 'WAITING', INPROGRESS = 'INPROGRESS', CONFIRMING = 'CONFIRMING', + // SUCCEED was also written for provider-reported results in older builds. SUCCEED = 'SUCCEED', + UNVERIFIED = 'UNVERIFIED', + CHAIN_CONFIRMED = 'CONFIRMED', FAILED = 'FAILED', } diff --git a/src/services/wallets/operations/dollars/USDT.ts b/src/services/wallets/operations/dollars/USDT.ts index 1f4adf8373..24c4741be8 100644 --- a/src/services/wallets/operations/dollars/USDT.ts +++ b/src/services/wallets/operations/dollars/USDT.ts @@ -65,6 +65,13 @@ export const isHistoricalUnverifiedUSDTRequest = ( transaction: Pick ): boolean => Boolean(transaction.traceId && !transaction.txId); +export const isUSDTStatusUnavailable = ( + transaction: Pick +): boolean => + isHistoricalUnverifiedUSDTRequest(transaction) || + transaction.status === GasFreeTransferStatus.SUCCEED || + transaction.status === GasFreeTransferStatus.UNVERIFIED; + export default class USDT { private static getUSDTAddress(networkType?: NetworkType): string { return USDT_ADDRESSES[networkType || NetworkType.MAINNET]; @@ -181,7 +188,9 @@ export default class USDT { from: txn.from, to: txn.to, amount: txn.formattedValue.toString(), - status: confirmed ? GasFreeTransferStatus.SUCCEED : GasFreeTransferStatus.CONFIRMING, + status: confirmed + ? GasFreeTransferStatus.CHAIN_CONFIRMED + : GasFreeTransferStatus.CONFIRMING, timestamp: txn.blockTimestamp, blockNumber: confirmed ? txn.blockNumber : 0, isGasFree: txn.to !== address, diff --git a/tests/components/usdtCacheStatusPresentation.test.cjs b/tests/components/usdtCacheStatusPresentation.test.cjs new file mode 100644 index 0000000000..69578d7aea --- /dev/null +++ b/tests/components/usdtCacheStatusPresentation.test.cjs @@ -0,0 +1,156 @@ +const { test } = require('node:test'); +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const path = require('node:path'); +const vm = require('node:vm'); +const ts = require('typescript'); + +const root = path.join(__dirname, '../..'); +const translations = { usdtWalletText: { statusUnavailable: 'Status unavailable' } }; +const React = { + createElement: (type, props, ...children) => ({ type, props, children }), + useContext: () => ({ translations }), +}; + +function loadSource(file, imports) { + const source = fs.readFileSync(path.join(root, file), 'utf8'); + const code = ts.transpileModule(source, { + compilerOptions: { + module: ts.ModuleKind.CommonJS, + target: ts.ScriptTarget.ES2020, + jsx: ts.JsxEmit.React, + esModuleInterop: true, + }, + }).outputText; + const module = { exports: {} }; + vm.runInNewContext(`(function (require, module, exports) { ${code}\n})`)( + (specifier) => { + if (!(specifier in imports)) throw new Error(`Unexpected import: ${specifier}`); + return imports[specifier]; + }, + module, + module.exports + ); + return module.exports; +} + +function nodes(tree) { + if (tree == null || tree === false) return []; + if (Array.isArray(tree)) return tree.flatMap(nodes); + if (typeof tree !== 'object') return [tree]; + return [tree, ...nodes(tree.children)]; +} + +const statuses = loadSource('src/services/wallets/operations/dollars/GasFree.ts', {}); +const { GasFreeTransferStatus } = statuses; +const usdt = loadSource('src/services/wallets/operations/dollars/USDT.ts', { + './GasFree': statuses, + 'src/services/wallets/enums': { NetworkType: { MAINNET: 'MAINNET' } }, + './Tron': {}, +}); + +const uiImports = { + react: React, + '@gluestack-ui/themed-native-base': { + Box: 'Box', + useColorMode: () => ({ colorMode: 'light' }), + }, + 'react-native': { + StyleSheet: { create: (styles) => styles }, + TouchableOpacity: 'TouchableOpacity', + }, + 'src/constants/responsive': { hp: (value) => value, wp: (value) => value }, + 'src/components/KeeperText': 'Text', + 'src/theme/Colors': {}, + 'src/context/Localization/LocContext': { LocalizationContext: {} }, + 'src/services/wallets/operations/dollars/GasFree': statuses, + 'src/services/wallets/operations/dollars/USDT': usdt, +}; + +const StatusContent = loadSource( + 'src/screens/USDT/components/StatusContent.tsx', + uiImports +).default; + +const rowImports = { + ...uiImports, + moment: () => ({ format: () => 'saved date' }), + 'src/screens/Home/components/CurrencyInfo': 'CurrencyInfo', + 'src/hooks/useLabelsNew': () => ({ labels: {} }), + 'src/services/wallets/enums': { EntityKind: { USDT_WALLET: 'USDT_WALLET' } }, + './ThemedColor/ThemedColor': () => 'color', +}; +for (const icon of [ + 'icon_sent_red', + 'icon_recieved_red', + 'icon_recieved_dark', + 'transaction_pending', + 'cache_icon', + 'icon_arrow_grey', + 'icon_arrow_white', +]) { + rowImports[`src/assets/images/${icon}.svg`] = icon; +} +const TransactionElement = loadSource('src/components/TransactionElement.tsx', rowImports).default; + +const wallet = { + entityKind: 'USDT_WALLET', + accountStatus: { gasFreeAddress: 'TStoredGasFreeAddress' }, +}; +const storedTransaction = { + txId: 'saved-chain-id', + traceId: 'saved-provider-trace', + from: 'TStoredGasFreeAddress', + to: 'TRecipient', + amount: '2', + timestamp: 100, + blockNumber: 100, + status: GasFreeTransferStatus.SUCCEED, + isGasFree: true, +}; + +test('offline legacy success with txId shows unavailable in the row and detail badge', () => { + assert.equal(usdt.isHistoricalUnverifiedUSDTRequest(storedTransaction), false); + assert.equal(usdt.isUSDTStatusUnavailable(storedTransaction), true); + + const row = nodes( + TransactionElement({ + transaction: storedTransaction, + wallet, + isCached: false, + }) + ); + assert.ok(row.includes('Status unavailable')); + assert.equal( + row.some((node) => node.type === 'transaction_pending'), + false + ); + + // The badge stays safe even if a caller forgets to pass `unavailable`. + const badge = nodes(StatusContent({ status: storedTransaction.status })); + assert.ok(badge.includes('Status unavailable')); + assert.equal(badge.includes('SUCCESS'), false); +}); + +test('only a confirmed chain status shows SUCCESS; actual pending rows keep the icon', () => { + const confirmed = { ...storedTransaction, status: GasFreeTransferStatus.CHAIN_CONFIRMED }; + assert.equal(usdt.isUSDTStatusUnavailable(confirmed), false); + const row = nodes(TransactionElement({ transaction: confirmed, wallet, isCached: false })); + assert.equal(row.includes('Status unavailable'), false); + assert.equal( + row.some((node) => node.type === 'transaction_pending'), + false + ); + assert.ok(nodes(StatusContent({ status: confirmed.status })).includes('SUCCESS')); + + const pending = { + ...storedTransaction, + status: GasFreeTransferStatus.CONFIRMING, + blockNumber: 0, + }; + const pendingRow = nodes(TransactionElement({ transaction: pending, wallet, isCached: false })); + assert.equal( + pendingRow.some((node) => node.type === 'transaction_pending'), + true + ); +}); diff --git a/tests/services/tronConfirmedHistory.test.cjs b/tests/services/tronConfirmedHistory.test.cjs index 321965b329..ffcf754af0 100644 --- a/tests/services/tronConfirmedHistory.test.cjs +++ b/tests/services/tronConfirmedHistory.test.cjs @@ -101,7 +101,12 @@ test('TRC20 history requests confirmed rows and keeps the pagination filter', as (name) => { if (name === './Tron') return module.exports; if (name === './GasFree') - return { GasFreeTransferStatus: { SUCCEED: 'SUCCEED', CONFIRMING: 'CONFIRMING' } }; + return { + GasFreeTransferStatus: { + CHAIN_CONFIRMED: 'CONFIRMED', + CONFIRMING: 'CONFIRMING', + }, + }; if (name === 'src/services/wallets/enums') return imports['../../enums']; throw new Error(`Unexpected import: ${name}`); }, @@ -114,7 +119,7 @@ test('TRC20 history requests confirmed rows and keeps the pagination filter', as 2, 'prior-page' ); - assert.equal(mapped.transactions[0].status, 'SUCCEED'); + assert.equal(mapped.transactions[0].status, 'CONFIRMED'); assert.equal(mapped.transactions[0].blockNumber, 100); assert.equal(mapped.meta.fingerprint, 'next-page'); assert.equal(new URL(requested[1]).searchParams.get('only_confirmed'), 'true'); diff --git a/tests/services/usdtPause.test.cjs b/tests/services/usdtPause.test.cjs index b9d2fe0ea7..ff9f2c12a1 100644 --- a/tests/services/usdtPause.test.cjs +++ b/tests/services/usdtPause.test.cjs @@ -141,7 +141,7 @@ test('stored address, chain reads and ambiguous historical request survive the p assert.equal(historyCalls[0][0], wallet.accountStatus.gasFreeAddress); assert.equal(transactions.length, 3); assert.equal(transactions[0].txId, 'chain-1'); - assert.equal(transactions[0].status, historical.GasFreeTransferStatus.SUCCEED); + assert.equal(transactions[0].status, historical.GasFreeTransferStatus.CHAIN_CONFIRMED); assert.equal(transactions[1].traceId, 'historical-trace'); assert.equal(transactions[0].to, transactions[1].to); assert.equal(transactions[0].amount, transactions[1].amount); @@ -152,6 +152,8 @@ test('stored address, chain reads and ambiguous historical request survive the p assert.equal(transactions[2].txId, 'chain-no-time'); assert.equal(transactions[2].status, historical.GasFreeTransferStatus.CONFIRMING); assert.equal(transactions[2].blockNumber, 0); + assert.equal(usdtModule.isUSDTStatusUnavailable(transactions[0]), false); + assert.equal(usdtModule.isUSDTStatusUnavailable(transactions[1]), true); assert.equal( usdtModule.isHistoricalUnverifiedUSDTRequest({ traceId: 'old-request', @@ -185,7 +187,7 @@ test('refresh upgrades a cached pending entry when its confirmed chain record ap }; const transactions = await factory.syncUSDTWalletTransactions(cached); const confirmed = transactions.find((tx) => tx.txId === 'chain-1'); - assert.equal(confirmed.status, historical.GasFreeTransferStatus.SUCCEED); + assert.equal(confirmed.status, historical.GasFreeTransferStatus.CHAIN_CONFIRMED); assert.equal(confirmed.blockNumber, 1); assert.equal(confirmed.traceId, 'saved-provider-trace'); assert.equal( @@ -194,6 +196,67 @@ test('refresh upgrades a cached pending entry when its confirmed chain record ap ); }); +test('an unmatched legacy success stays in history but loses its success claim', async () => { + const legacySuccess = { + txId: 'older-than-first-page', + traceId: 'saved-provider-trace', + from: 'TStoredGasFreeAddress', + to: 'TRecipient', + amount: '3', + status: historical.GasFreeTransferStatus.SUCCEED, + timestamp: 20, + blockNumber: 20, + isGasFree: true, + }; + const cached = { + ...wallet, + specs: { ...wallet.specs, transactions: [...wallet.specs.transactions, legacySuccess] }, + }; + + // The UI must be truthful before refresh too, including when offline. + assert.equal(usdtModule.isUSDTStatusUnavailable(legacySuccess), true); + const transactions = await factory.syncUSDTWalletTransactions(cached); + const retained = transactions.find((tx) => tx.txId === legacySuccess.txId); + assert.equal(transactions.length, 4); + assert.equal(retained.status, historical.GasFreeTransferStatus.UNVERIFIED); + assert.equal(retained.traceId, legacySuccess.traceId); + assert.equal(retained.timestamp, legacySuccess.timestamp); + assert.equal(retained.blockNumber, legacySuccess.blockNumber); + assert.equal(usdtModule.isUSDTStatusUnavailable(retained), true); + assert.equal(legacySuccess.status, historical.GasFreeTransferStatus.SUCCEED); +}); + +test('matching confirmed history corrects a cached legacy success without losing its trace', async () => { + const cached = { + ...wallet, + specs: { + ...wallet.specs, + transactions: [ + ...wallet.specs.transactions, + { + txId: 'chain-1', + traceId: 'saved-provider-trace', + from: 'TStoredGasFreeAddress', + to: 'TRecipient', + amount: '2', + status: historical.GasFreeTransferStatus.SUCCEED, + timestamp: 80, + blockNumber: 1, + isGasFree: true, + }, + ], + }, + }; + + const transactions = await factory.syncUSDTWalletTransactions(cached); + const confirmed = transactions.filter((tx) => tx.txId === 'chain-1'); + assert.equal(confirmed.length, 1); + assert.equal(confirmed[0].status, historical.GasFreeTransferStatus.CHAIN_CONFIRMED); + assert.equal(confirmed[0].traceId, 'saved-provider-trace'); + assert.equal(confirmed[0].blockNumber, 1); + assert.equal(usdtModule.isUSDTStatusUnavailable(confirmed[0]), false); +}); + test('missing saved address is never replaced by the owner address', async () => { const missing = { ...wallet, accountStatus: { ...wallet.accountStatus, gasFreeAddress: '' } }; await assert.rejects(factory.syncUSDTWalletBalance(missing), /Stored USDT address/); From 22c4c68be17ec19bf9fc91f3bdd4b2fe0246d716 Mon Sep 17 00:00:00 2001 From: Anant Date: Fri, 9 Oct 2026 17:29:26 +0530 Subject: [PATCH 106/254] test: accept wrong-key AES decode failure --- tests/services/encryption.test.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/services/encryption.test.ts b/tests/services/encryption.test.ts index 24a89e6a57..2c395c4ae2 100644 --- a/tests/services/encryption.test.ts +++ b/tests/services/encryption.test.ts @@ -82,7 +82,13 @@ describe('Testing cryptographic primitives', () => { test('Decryption::Incorrect key', () => { const incorrectKey = 'a'.repeat(64); - const decrypted = decrypt(incorrectKey, encrypted); + let decrypted: string; + try { + decrypted = decrypt(incorrectKey, encrypt(key, message)); + } catch (error) { + expect(error).toEqual(new Error('Malformed UTF-8 data')); + return; + } expect(decrypted).not.toBe(message); }); }); From d59d620c542a6d9e13cdbc31c9f5f5a038cf065c Mon Sep 17 00:00:00 2001 From: Anant Date: Fri, 9 Oct 2026 17:35:59 +0530 Subject: [PATCH 107/254] fix(android): validate direct production release environment --- package.json | 2 +- scripts/verify-channel-config.test.cjs | 38 ++++++++++++++++++++++++-- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index a26e86c2b7..08bacbae78 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ "androidDevelopmentDebug": "ENVFILE=.env react-native run-android --mode=developmentDebug --appIdSuffix=development", "androidDevelopmentRelease": "ENVFILE=.env react-native run-android --mode=developmentRelease --appIdSuffix=development", "androidProductionDebug": "ENVFILE=.env react-native run-android --mode=productionDebug", - "androidProductionRelease": "ENVFILE=.env react-native run-android --mode=productionRelease", + "androidProductionRelease": "node scripts/verify-channel-config.cjs .env.production && ENVFILE=.env.production react-native run-android --mode=productionRelease", "uninstall ios": "xcrun simctl uninstall booted io.hexawallet.keeper", "uninstall ios:dev": "xcrun simctl uninstall booted io.hexawallet.hexakeeper.dev", "uninstall android": "adb uninstall io.hexawallet.bitcoinkeeper", diff --git a/scripts/verify-channel-config.test.cjs b/scripts/verify-channel-config.test.cjs index adf9ba3d76..e9863c25d5 100644 --- a/scripts/verify-channel-config.test.cjs +++ b/scripts/verify-channel-config.test.cjs @@ -26,6 +26,35 @@ test('release env accepts reviewed channel without retired credentials', () => assert.equal(result.status, 0, result.stderr); })); +test('direct Android production release requires and selects the reviewed file', () => + withTempDirectory((directory) => { + fs.mkdirSync(path.join(directory, 'scripts')); + fs.copyFileSync(verifier, path.join(directory, 'scripts', 'verify-channel-config.cjs')); + const bin = path.join(directory, 'bin'); + fs.mkdirSync(bin); + const launcher = path.join(bin, 'react-native'); + fs.writeFileSync(launcher, '#!/bin/sh\nprintf "%s|%s" "$ENVFILE" "$*" > selected-env\n'); + fs.chmodSync(launcher, 0o755); + fs.writeFileSync(path.join(directory, '.env'), channel); + const command = require('../package.json').scripts.androidProductionRelease; + const options = { + cwd: directory, + env: { ...process.env, PATH: `${bin}:${process.env.PATH}` }, + encoding: 'utf8', + }; + const missing = spawnSync('sh', ['-c', command], options); + assert.notEqual(missing.status, 0); + assert.equal(fs.existsSync(path.join(directory, 'selected-env')), false); + + fs.writeFileSync(path.join(directory, '.env.production'), channel); + const valid = spawnSync('sh', ['-c', command], options); + assert.equal(valid.status, 0, valid.stderr); + assert.equal( + fs.readFileSync(path.join(directory, 'selected-env'), 'utf8'), + '.env.production|run-android --mode=productionRelease' + ); + })); + for (const [caseName, stage] of [ ['missing', ''], ['development', 'ENVIRONMENT=DEVELOPMENT\n'], @@ -41,8 +70,13 @@ for (const [caseName, stage] of [ })); } -for (const name of ['GASFREE_API_KEY', 'GASFREE_API_SECRET', 'RN_GASFREE_API_KEY', - 'LETS_EXCHANGE_API_KEY', 'RN_LETS_EXCHANGE_API_KEY']) { +for (const name of [ + 'GASFREE_API_KEY', + 'GASFREE_API_SECRET', + 'RN_GASFREE_API_KEY', + 'LETS_EXCHANGE_API_KEY', + 'RN_LETS_EXCHANGE_API_KEY', +]) { test(`release env rejects ${name} without logging its value`, () => withTempDirectory((directory) => { const envFile = path.join(directory, '.env.production'); From 966cd0e00f6af714aa53e5459246c0f1664d258d Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Mon, 12 Oct 2026 06:11:15 +0000 Subject: [PATCH 108/254] ci: check release trigger PR on its stacked base --- .github/workflows/codeql.yml | 1 + .github/workflows/test.yml | 1 + 2 files changed, 2 insertions(+) diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index feee5b9c70..645d24fbd7 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -10,6 +10,7 @@ on: pull_request: branches: - sprint + - codex/r3-release-baseline-to-sprint - dev - main - release/2.6.4 diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 5c0e6e2a1b..a9fcff27c9 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -11,6 +11,7 @@ on: pull_request: branches: - sprint + - codex/r3-release-baseline-to-sprint - dev - main - release/2.6.3 From 3592ede8832a3b2986fee01339edee717c11c4cd Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Mon, 12 Oct 2026 06:15:57 +0000 Subject: [PATCH 109/254] test: make standalone USDT candidate Electrum checks deterministic --- tests/services/client.test.ts | 149 ++++++++++++++++++------ tests/services/electrumWalletFixture.ts | 79 +++++++++++++ tests/services/vault.test.ts | 9 ++ tests/services/wallet.test.ts | 5 + 4 files changed, 204 insertions(+), 38 deletions(-) create mode 100644 tests/services/electrumWalletFixture.ts diff --git a/tests/services/client.test.ts b/tests/services/client.test.ts index 7b1381530b..a9b548153e 100644 --- a/tests/services/client.test.ts +++ b/tests/services/client.test.ts @@ -3,24 +3,88 @@ import ElectrumClient from '../../src/services/electrum/client'; import { predefinedTestnetNodes } from '../../src/services/electrum/predefinedNodes'; import * as bitcoinJS from 'bitcoinjs-lib'; +const mockClients: any[] = []; +const mockTxid = '24913105c497b4bb4ccf81b03857c8306aba0e58ccde792d981f6c18efdb24b8'; + +// Exercise the real ElectrumClient facade against deterministic protocol replies. +// A public Electrum server is outside the control of CI. +jest.mock('electrum-client', () => ({ + __esModule: true, + default: jest.fn().mockImplementation((_net, _tls, _port, host) => { + const client = { + host, + initElectrum: jest.fn(async () => { + if (host === 'blackie.c3-soft.com') { + throw new Error('connect ECONNREFUSED 127.0.0.1:50002'); + } + return ['fixture-server', '1.4']; + }), + close: jest.fn(), + onError: null, + server_ping: jest.fn(async () => undefined), + server_features: jest.fn(async () => ({ + server_version: 'fixture-server', + protocol_min: '1.4', + protocol_max: '1.4.3', + })), + blockchainScripthash_listunspentBatch: jest.fn(async (hashes) => + hashes.map((param, index) => ({ + param, + result: index === 0 ? [{ tx_hash: mockTxid, tx_pos: 1, value: 12345, height: 100 }] : [], + })) + ), + blockchainScripthash_getHistoryBatch: jest.fn(async (hashes) => + hashes.map((param, index) => ({ + param, + result: index === 0 ? [{ tx_hash: mockTxid, height: 100 }] : [], + })) + ), + blockchainEstimatefee: jest.fn(async () => 0.00002), + blockchainTransaction_getBatch: jest.fn(async (txids) => + txids.map((param) => ({ + param, + result: { + txid: param, + hex: 'fixture-hex', + vout: [{ scriptPubKey: { address: 'tb1qfixture' } }], + }, + })) + ), + }; + mockClients.push(client); + return client; + }), +})); + jest.mock('src/store/store', () => ({ store: { getState: () => ({ - settings: { - bitcoinNetwork: 'testnet', - }, + settings: { bitcoinNetworkType: 'TESTNET' }, }), }, })); -beforeAll(async () => { - try { - ElectrumClient.setActivePeer(predefinedTestnetNodes); - await ElectrumClient.connect(); - } catch (err) { - console.log('failed to connect to Electrum:', err); - process.exit(1); - } +const onlineNode = { + ...predefinedTestnetNodes[0], + id: 337, + host: 'electrum-fixture.test', + isConnected: true, +}; +const offlineNode = { + ...predefinedTestnetNodes[0], + isConnected: true, +}; +const addresses = [ + 'tb1qd2u9tvuqzadgeh02vppd33e7u2fatuwrw7h4q5', + 'tb1qwdm8hdyvv5jn05qq858lgnk50heucvxvqtl4sx', +]; + +beforeEach(async () => { + mockClients.length = 0; + ElectrumClient.setActivePeer([onlineNode], onlineNode); + ElectrumClient.resetCurrentPeerIndex(); + const connection = await ElectrumClient.connect(); + assert.ok(connection.connected); }); afterAll(() => { @@ -30,23 +94,33 @@ afterAll(() => { describe('ElectrumClient', () => { describe('Connection Tests', () => { it('should test connection successfully', async () => { - const result = await ElectrumClient.testConnection(predefinedTestnetNodes[0]); + const result = await ElectrumClient.testConnection(onlineNode); assert.ok(result.connected); }); it('should fail connection when node is unreachable', async () => { - const unreachableNode = { ...predefinedTestnetNodes[0], host: 'invalid.host' }; - const result = await ElectrumClient.testConnection(unreachableNode); + const result = await ElectrumClient.testConnection(offlineNode); assert.ok(!result.connected); assert.ok(result.error); + assert.strictEqual(result.errorType, 'network'); }); it('should reconnect to the next peer when connection fails', async () => { - ElectrumClient.setActivePeer(predefinedTestnetNodes); + ElectrumClient.setActivePeer([offlineNode, onlineNode], offlineNode); + ElectrumClient.resetCurrentPeerIndex(); + const result = await ElectrumClient.connect(); + assert.ok(result.connected); + assert.strictEqual(result.connectedTo, onlineNode.host); + assert.strictEqual(ElectrumClient.getActivePeer()?.host, onlineNode.host); + + assert.ok(mockClients.some((client) => client.host === offlineNode.host)); + }); + + it('should reconnect after explicit disconnection', async () => { ElectrumClient.forceDisconnect(); const result = await ElectrumClient.reconnect(); assert.ok(result.connected); - assert.ok(result.connectedTo); + assert.strictEqual(result.connectedTo, onlineNode.host); }); }); @@ -60,51 +134,50 @@ describe('ElectrumClient', () => { describe('Feature Tests', () => { it('should retrieve server features', async () => { const features = await ElectrumClient.serverFeatures(); - assert.ok(features.server_version); - assert.ok(features.protocol_min); - assert.ok(features.protocol_max); + assert.strictEqual(features.server_version, 'fixture-server'); + assert.strictEqual(features.protocol_min, '1.4'); + assert.strictEqual(features.protocol_max, '1.4.3'); }); }); describe('UTXO and History Tests', () => { it('should sync UTXOs by address', async () => { - const addresses = [ - 'tb1qd2u9tvuqzadgeh02vppd33e7u2fatuwrw7h4q5', - 'tb1qwdm8hdyvv5jn05qq858lgnk50heucvxvqtl4sx', - ]; const utxos = await ElectrumClient.syncUTXOByAddress(addresses, bitcoinJS.networks.testnet); - assert.ok(Object.keys(utxos).length > 0); + assert.deepStrictEqual(utxos[addresses[0]], [ + { txId: mockTxid, vout: 1, value: 12345, height: 100, address: addresses[0] }, + ]); + assert.deepStrictEqual(utxos[addresses[1]], []); }); it('should sync history by address', async () => { - const addresses = [ - 'tb1qd2u9tvuqzadgeh02vppd33e7u2fatuwrw7h4q5', - 'tb1qwdm8hdyvv5jn05qq858lgnk50heucvxvqtl4sx', - ]; const history = await ElectrumClient.syncHistoryByAddress( addresses, bitcoinJS.networks.testnet ); - assert.ok(Object.keys(history.historyByAddress).length > 0); - assert.ok(history.txids.length > 0); + assert.deepStrictEqual(history.historyByAddress[addresses[0]], [ + { tx_hash: mockTxid, height: 100 }, + ]); + assert.deepStrictEqual(history.historyByAddress[addresses[1]], []); + assert.deepStrictEqual(history.txids, [mockTxid]); + assert.strictEqual(history.txidToAddress[mockTxid], addresses[0]); }); }); describe('Transaction Tests', () => { it('should estimate fee', async () => { const fee = await ElectrumClient.estimateFee(1); - assert.ok(fee > 0); + assert.strictEqual(fee, 2); }); it('should fetch transactions by ID', async () => { - const txids = [ - '24913105c497b4bb4ccf81b03857c8306aba0e58ccde792d981f6c18efdb24b8', - '9a15008af30580039bc7a07d36c3544902a2e1618ad31f4dda94fe0044a8dcce', - ]; + const txids = [mockTxid, '9a15008af30580039bc7a07d36c3544902a2e1618ad31f4dda94fe0044a8dcce']; const transactions = await ElectrumClient.getTransactionsById(txids); - assert.ok(Object.keys(transactions).length === txids.length); - assert.deepStrictEqual(transactions[txids[0]].txid, txids[0]); - assert.deepStrictEqual(transactions[txids[1]].txid, txids[1]); + assert.deepStrictEqual(Object.keys(transactions), txids); + for (const txid of txids) { + assert.strictEqual(transactions[txid].txid, txid); + assert.strictEqual(transactions[txid].hex, undefined); + assert.deepStrictEqual(transactions[txid].vout[0].scriptPubKey.addresses, ['tb1qfixture']); + } }); }); }); diff --git a/tests/services/electrumWalletFixture.ts b/tests/services/electrumWalletFixture.ts new file mode 100644 index 0000000000..466523c6ed --- /dev/null +++ b/tests/services/electrumWalletFixture.ts @@ -0,0 +1,79 @@ +import ElectrumClient from 'src/services/electrum/client'; +import WalletOperations from 'src/services/wallets/operations'; + +const fundingTxid = '608710eb040d935dedb6c4d6321369382b2bd45533a7442a6fa98add3860d46b'; +const previousTxid = '89b193b61f7681ee588939932219f3d92e54a1873a9fb823647038f3879fba00'; +const externalAddress = 'tb1ql7w62elx9ucw4pj5lgw4l028hmuw80sndtntxt'; + +// The wallet and vault suites exercise address scanning, transaction parsing, coin selection, +// and PSBT signing. Give each generated wallet one funded testnet address without using a +// public Electrum server or a mutable public wallet balance. +export const installElectrumWalletFixture = () => { + let fundedAddress: string | null = null; + const syncWallets = WalletOperations.syncWalletsViaElectrumClient; + + jest.spyOn(WalletOperations, 'syncWalletsViaElectrumClient').mockImplementation((...args) => { + fundedAddress = null; + return syncWallets(...args); + }); + jest.spyOn(WalletOperations, 'fetchFeeRatesByPriority').mockImplementation(async () => + WalletOperations.mockFeeRates() + ); + + jest.spyOn(ElectrumClient, 'setActivePeer').mockImplementation(() => undefined); + jest.spyOn(ElectrumClient, 'connect').mockResolvedValue({ + connected: true, + connectedTo: 'fixture.electrum.test', + }); + jest.spyOn(ElectrumClient, 'forceDisconnect').mockImplementation(() => undefined); + jest.spyOn(ElectrumClient, 'getBlockchainHeaders').mockResolvedValue({ height: 85172, hex: '00' }); + jest.spyOn(ElectrumClient, 'syncUTXOByAddress').mockImplementation(async (addresses) => { + if (!fundedAddress) fundedAddress = addresses[0]; + return Object.fromEntries( + addresses.map((address) => [ + address, + address === fundedAddress + ? [{ txId: fundingTxid, vout: 1, value: 6000, height: 85000, address }] + : [], + ]) + ); + }); + jest.spyOn(ElectrumClient, 'syncHistoryByAddress').mockImplementation(async (addresses) => { + const hasFunding = addresses.includes(fundedAddress); + return { + historyByAddress: Object.fromEntries( + addresses.map((address) => [ + address, + hasFunding && address === fundedAddress ? [{ tx_hash: fundingTxid, height: 85000 }] : [], + ]) + ), + txids: hasFunding ? [fundingTxid] : [], + txidToAddress: hasFunding ? { [fundingTxid]: fundedAddress } : {}, + }; + }); + jest.spyOn(ElectrumClient, 'getTransactionsById').mockImplementation(async (txids) => { + const transactions: Record = {}; + for (const txid of txids) { + if (txid === fundingTxid) { + transactions[txid] = { + txid, + vin: [{ txid: previousTxid, vout: 0 }], + vout: [ + { value: 0.00001, scriptPubKey: { addresses: [externalAddress] } }, + { value: 0.00006, scriptPubKey: { addresses: [fundedAddress] } }, + ], + confirmations: 12, + time: 1700000000, + blocktime: 1700000000, + }; + } else if (txid === previousTxid) { + transactions[txid] = { + txid, + vin: [], + vout: [{ value: 0.0001, scriptPubKey: { addresses: [externalAddress] } }], + }; + } + } + return transactions; + }); +}; diff --git a/tests/services/vault.test.ts b/tests/services/vault.test.ts index ed6cf6f13b..7b102a7917 100644 --- a/tests/services/vault.test.ts +++ b/tests/services/vault.test.ts @@ -43,6 +43,7 @@ import { generateOutputDescriptors, parseTextforVaultConfig, } from 'src/utils/service-utilities/utils'; +import { installElectrumWalletFixture } from './electrumWalletFixture'; jest.mock('src/store/store', () => ({ store: { @@ -56,6 +57,10 @@ jest.mock('src/store/store', () => ({ jest.mock('realm', () => ({})); +beforeAll(() => { + installElectrumWalletFixture(); +}); + const connectToElectrumClient = async () => { try { ElectrumClient.setActivePeer(predefinedTestnetNodes); @@ -67,6 +72,10 @@ const connectToElectrumClient = async () => { } }; +afterAll(() => { + ElectrumClient.forceDisconnect(); +}); + describe('Vault: Single-Sig(1-of-1)', () => { let primaryMnemonic: string; let vault: Vault; diff --git a/tests/services/wallet.test.ts b/tests/services/wallet.test.ts index dc92142d4f..b366993e46 100644 --- a/tests/services/wallet.test.ts +++ b/tests/services/wallet.test.ts @@ -12,6 +12,7 @@ import { TransactionPrerequisite, } from 'src/services/wallets/interfaces'; import { Wallet } from 'src/services/wallets/interfaces/wallet'; +import { installElectrumWalletFixture } from './electrumWalletFixture'; jest.mock('src/store/store', () => ({ store: { @@ -25,6 +26,10 @@ jest.mock('src/store/store', () => ({ jest.mock('realm', () => ({})); +beforeAll(() => { + installElectrumWalletFixture(); +}); + describe('Wallet Functionality Tests', () => { let primaryMnemonic: string; let wallet: Wallet; From a6b1a7b6f685fee84a52e9f01a4a250c1050aa71 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Mon, 12 Oct 2026 06:17:56 +0000 Subject: [PATCH 110/254] fix(inheritance): preserve entry selection and clarify fixed activation --- src/context/Localization/language/en.json | 1 + src/context/Localization/language/es.json | 1 + src/screens/AddWalletScreen/AddNewWallet.tsx | 4 +- src/screens/Vault/AddReserveKey.tsx | 2 +- tests/services/inheritanceEntryRoute.test.ts | 121 +++++++++++++++++++ tests/services/passphraseDecayPolicy.test.ts | 36 ++++-- 6 files changed, 153 insertions(+), 12 deletions(-) create mode 100644 tests/services/inheritanceEntryRoute.test.ts diff --git a/src/context/Localization/language/en.json b/src/context/Localization/language/en.json index bac88415a4..713747f3f8 100644 --- a/src/context/Localization/language/en.json +++ b/src/context/Localization/language/en.json @@ -671,6 +671,7 @@ "addEmergencyKey": "Add Emergency Key", "availableAfterDelay": "Key will be available after time-delay", "inheritanceKeyActivation": "Inheritance Key Activation", + "inheritanceActivationNote": "Activation is fixed when this wallet version is created, with any Wallet Timelock added to the selected delay. Receiving or spending funds does not restart it. Resetting requires re-vaulting and transferring funds.", "emergencyKeyActivation": "Emergency Key Activation", "changeKey": "Change Key", "selectActivationTime": "Select activation time", diff --git a/src/context/Localization/language/es.json b/src/context/Localization/language/es.json index 3e3daafd0a..464f4f0fda 100644 --- a/src/context/Localization/language/es.json +++ b/src/context/Localization/language/es.json @@ -659,6 +659,7 @@ "addEmergencyKey": "Add Emergency Key", "availableAfterDelay": "Key will be available after time-delay", "inheritanceKeyActivation": "Inheritance Key Activation", + "inheritanceActivationNote": "La activación se fija al crear esta versión de la billetera; cualquier bloqueo temporal de la billetera se suma al plazo seleccionado. Recibir o gastar fondos no reinicia el plazo. Para reiniciarlo hay que crear una nueva versión de la billetera y transferir los fondos.", "emergencyKeyActivation": "Emergency Key Activation", "changeKey": "Change Key", "selectActivationTime": "Select activation time", diff --git a/src/screens/AddWalletScreen/AddNewWallet.tsx b/src/screens/AddWalletScreen/AddNewWallet.tsx index 7f622ca730..6af1196c41 100644 --- a/src/screens/AddWalletScreen/AddNewWallet.tsx +++ b/src/screens/AddWalletScreen/AddNewWallet.tsx @@ -69,7 +69,7 @@ function AddNewWallet({ navigation, route }) { const [selectedWalletType, setSelectedWalletType] = useState(''); const [customConfigModalVisible, setCustomConfigModalVisible] = useState(false); const [showEnhancedOptionsModal, setShowEnhancedOptionsModal] = useState(false); - const { vaultId } = route.params || {}; + const { vaultId, isAddInheritanceKeyFromParams } = route.params || {}; const DashedCtaTextColor = ThemedColor({ name: 'DashedCtaTextColor' }); const DashedCtaBorderColor = ThemedColor({ name: 'DashedCtaBorderColor' }); const dashed_CTA_background = ThemedColor({ name: 'dashed_CTA_background' }); @@ -81,7 +81,7 @@ function AddNewWallet({ navigation, route }) { activeVault?.scheme?.miniscriptScheme?.usedMiniscriptTypes?.includes( MiniscriptTypes.INHERITANCE ) || - route.isAddInheritanceKeyFromParams || + isAddInheritanceKeyFromParams || false ); diff --git a/src/screens/Vault/AddReserveKey.tsx b/src/screens/Vault/AddReserveKey.tsx index fdcf2db141..5960a974a5 100644 --- a/src/screens/Vault/AddReserveKey.tsx +++ b/src/screens/Vault/AddReserveKey.tsx @@ -212,7 +212,7 @@ function AddReserveKey({ route }) { {vaultTranslations.inheritanceKeyActivation} - {vaultTranslations.availableAfterDelay} + {vaultTranslations.inheritanceActivationNote} diff --git a/tests/services/inheritanceEntryRoute.test.ts b/tests/services/inheritanceEntryRoute.test.ts new file mode 100644 index 0000000000..ae0691d30b --- /dev/null +++ b/tests/services/inheritanceEntryRoute.test.ts @@ -0,0 +1,121 @@ +import fs from 'fs'; +import path from 'path'; +import vm from 'vm'; +import ts from 'typescript'; +import { MiniscriptTypes } from 'src/services/wallets/enums'; + +function readSource(relativePath: string) { + return ts.createSourceFile( + relativePath, + fs.readFileSync(path.join(process.cwd(), relativePath), 'utf8'), + ts.ScriptTarget.Latest, + true, + ts.ScriptKind.TSX + ); +} + +function execute(expression: ts.Expression, source: ts.SourceFile, scope: any, declarations = '') { + vm.runInNewContext( + ts.transpileModule(`${declarations}\nresult = (${expression.getText(source)});`, { + compilerOptions: { target: ts.ScriptTarget.ES2019 }, + }).outputText, + scope + ); + return scope.result; +} + +const walletSource = readSource('src/screens/AddWalletScreen/AddNewWallet.tsx'); +const entrySource = readSource('src/screens/InheritanceToolsAndTips/components/AssistedKeys.tsx'); + +function inheritanceRouteBinding() { + let declaration: ts.VariableStatement; + function visit(node: ts.Node) { + if ( + ts.isVariableDeclaration(node) && + ts.isObjectBindingPattern(node.name) && + node.name.elements.some( + (element) => element.name.getText(walletSource) === 'isAddInheritanceKeyFromParams' + ) + ) { + declaration = node.parent.parent as ts.VariableStatement; + } + ts.forEachChild(node, visit); + } + visit(walletSource); + if (!declaration) throw new Error('Missing inheritance route binding'); + return declaration.getText(walletSource); +} + +function inheritanceInitializer() { + let expression: ts.Expression; + function visit(node: ts.Node) { + if ( + ts.isVariableDeclaration(node) && + ts.isArrayBindingPattern(node.name) && + node.name.elements[0]?.getText(walletSource) === 'inheritanceKeySelected' + ) { + expression = node.initializer; + } + ts.forEachChild(node, visit); + } + visit(walletSource); + if (!expression) throw new Error('Missing inheritance state initializer'); + return expression; +} + +function inheritanceEntryNavigation() { + let expression: ts.Expression; + function visit(node: ts.Node) { + if ( + ts.isCallExpression(node) && + node.expression.getText(entrySource) === 'CommonActions.navigate' + ) { + expression = node; + } + ts.forEachChild(node, visit); + } + visit(entrySource); + if (!expression) throw new Error('Missing inheritance entry navigation'); + return execute(expression, entrySource, { + CommonActions: { navigate: (action) => action }, + }); +} + +function initialSelection(route: any, activeVault?: any) { + return execute( + inheritanceInitializer(), + walletSource, + { + route, + activeVault, + MiniscriptTypes, + useState: (value) => value, + }, + inheritanceRouteBinding() + ); +} + +describe('Inheritance Planning entry into wallet creation', () => { + it('passes its actual navigation params into the screen inheritance selection', () => { + const action = inheritanceEntryNavigation(); + expect(action.name).toBe('AddNewWallet'); + expect(initialSelection({ params: action.params })).toBe(true); + }); + + it('leaves ordinary new-wallet creation unselected without route params', () => { + expect(initialSelection({})).toBe(false); + expect(initialSelection({ params: {} })).toBe(false); + expect(initialSelection({ params: { isAddInheritanceKeyFromParams: false } })).toBe(false); + }); + + it('retains inheritance selection from an existing wallet policy', () => { + expect( + initialSelection( + {}, + { + scheme: { miniscriptScheme: { usedMiniscriptTypes: [MiniscriptTypes.INHERITANCE] } }, + } + ) + ).toBe(true); + }); +}); diff --git a/tests/services/passphraseDecayPolicy.test.ts b/tests/services/passphraseDecayPolicy.test.ts index 7b33ab80f5..bd6f1a2a7e 100644 --- a/tests/services/passphraseDecayPolicy.test.ts +++ b/tests/services/passphraseDecayPolicy.test.ts @@ -3,8 +3,14 @@ import { generateSeedWordsKey, generateMiniscriptScheme, generateVault, + getAvailableMiniscriptPhase, } from 'src/services/wallets/factories/VaultFactory'; -import { MiniscriptTypes, MultisigScriptType, NetworkType, VaultType } from 'src/services/wallets/enums'; +import { + MiniscriptTypes, + MultisigScriptType, + NetworkType, + VaultType, +} from 'src/services/wallets/enums'; import { ENHANCED_VAULT_TIMELOCKS_TIMESTAMP_MAINNET, generateEnhancedVaultElements, @@ -27,13 +33,7 @@ const externalSigner = (passphrase: string) => { masterFingerprint, xfp: masterFingerprint, derivationPath, - xpub: WalletUtilities.generateExtendedKey( - mnemonic, - false, - network, - derivationPath, - passphrase - ), + xpub: WalletUtilities.generateExtendedKey(mnemonic, false, network, derivationPath, passphrase), }; }; @@ -79,7 +79,7 @@ describe('Disposable passphrase and absolute-lock policy fixture', () => { expect(Object.values(scheme.keyInfoMap).every((key) => key.endsWith('/*'))).toBe(true); }); - it('derives two receive addresses from a Vault descriptor with the same absolute unlock', async () => { + it('keeps the seed-only fallback behind the same absolute unlock across receive addresses', async () => { const primary = externalSigner('TREZOR'); const inheritance = externalSigner(''); const absoluteUnlock = 1800000000 + ENHANCED_VAULT_TIMELOCKS_TIMESTAMP_MAINNET.MONTHS_6; @@ -118,5 +118,23 @@ describe('Disposable passphrase and absolute-lock policy fixture', () => { expect(descriptor).toContain('/*'); expect(descriptor).toContain('#'); expect(first).not.toBe(second); + + const beforeActivation = getAvailableMiniscriptPhase(vault, absoluteUnlock - 1); + expect( + Object.values(beforeActivation.signers).map((signer: any) => signer.masterFingerprint) + ).toEqual([primary.masterFingerprint]); + + const afterActivation = getAvailableMiniscriptPhase(vault, absoluteUnlock + 1); + expect( + Object.values(afterActivation.signers).map((signer: any) => signer.masterFingerprint) + ).toEqual(expect.arrayContaining([primary.masterFingerprint, inheritance.masterFingerprint])); + const fallbackPhase = afterActivation.phases.find((phase) => phase.timelock === absoluteUnlock); + expect(fallbackPhase.paths).toEqual([ + expect.objectContaining({ + threshold: 1, + keys: [expect.objectContaining({ identifier: 'IK1' })], + }), + ]); + expect(generateOutputDescriptors(vault)).toBe(descriptor); }); }); From 6f3f0e2fca210c39efae62f7448e4ba88154850b Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Mon, 12 Oct 2026 06:18:05 +0000 Subject: [PATCH 111/254] Add verified production build-only Fastlane lanes --- android/fastlane/Fastfile | 101 ++++++----- ios/fastlane/Fastfile | 89 ++++++---- release/README.md | 28 ++- scripts/test_fastlane_build_only.rb | 265 ++++++++++++++++++++++++++++ 4 files changed, 399 insertions(+), 84 deletions(-) create mode 100644 scripts/test_fastlane_build_only.rb diff --git a/android/fastlane/Fastfile b/android/fastlane/Fastfile index 9ff016ca39..6bfaee660d 100644 --- a/android/fastlane/Fastfile +++ b/android/fastlane/Fastfile @@ -15,6 +15,54 @@ default_platform(:android) +def build_verified_production_android_artifacts! + sh("node", File.expand_path("../../scripts/release-version.cjs", __dir__), "release-check") + production_env = File.expand_path("../../.env.production", __dir__) + UI.user_error!("Missing reviewed .env.production") unless File.file?(production_env) + sh("node", File.expand_path("../../scripts/verify-channel-config.cjs", __dir__), production_env) + ENV['ENVFILE'] = production_env + store_password = ENV["STORE_PASSWORD"] + key_password = ENV["KEY_PASSWORD"] + key_alias = ENV["KEY_ALIAS"] + UI.user_error!("Missing Android release signing credentials") if [store_password, key_password, key_alias].any? { |value| value.to_s.empty? } + releaseFilePath = ENV["KEEPER_ANDROID_KEYSTORE"] || File.expand_path("../app/release.keystore", __dir__) + UI.user_error!("Missing Android release keystore") unless File.file?(releaseFilePath) + gradle(task: 'clean') + # Gradle must return the artifact path for this invocation. + Actions.lane_context.delete(SharedValues::GRADLE_AAB_OUTPUT_PATH) + gradle( + task: 'bundle', + build_type: 'productionRelease', + print_command: false, + properties: { + "android.injected.signing.store.file" => releaseFilePath, + "android.injected.signing.store.password" => store_password, + "android.injected.signing.key.alias" => key_alias, + "android.injected.signing.key.password" => key_password, + } + ) + aab_path = Actions.lane_context[SharedValues::GRADLE_AAB_OUTPUT_PATH].to_s + UI.user_error!("Build did not return an AAB path") if aab_path.strip.empty? + Actions.lane_context.delete(SharedValues::GRADLE_APK_OUTPUT_PATH) + gradle( + task: 'assemble', + build_type: 'productionRelease', + print_command: false, + properties: { + "android.injected.signing.store.file" => releaseFilePath, + "android.injected.signing.store.password" => store_password, + "android.injected.signing.key.alias" => key_alias, + "android.injected.signing.key.password" => key_password, + } + ) + apk_path = Actions.lane_context[SharedValues::GRADLE_APK_OUTPUT_PATH].to_s + UI.user_error!("Build did not return an APK path") if apk_path.strip.empty? + verifier = File.expand_path("../../scripts/verify-release-artifact.py", __dir__) + sh("python3", verifier, aab_path) + sh("python3", verifier, apk_path) + { aab: aab_path, apk: apk_path } +end + platform :android do desc "Push a new dev build to Slack" lane :dev do @@ -47,52 +95,17 @@ platform :android do ) end + desc "Build and verify production AAB and APK without uploading or notifying Slack" + lane :build_only do + build_verified_production_android_artifacts! + end + desc "Build explicitly versioned production artifacts and upload to Play internal testing" lane :live do - sh("node", File.expand_path("../../scripts/release-version.cjs", __dir__), "release-check") - production_env = File.expand_path("../../.env.production", __dir__) - UI.user_error!("Missing reviewed .env.production") unless File.file?(production_env) - sh("node", File.expand_path("../../scripts/verify-channel-config.cjs", __dir__), production_env) - ENV['ENVFILE'] = production_env - store_password = ENV["STORE_PASSWORD"] - key_password = ENV["KEY_PASSWORD"] - key_alias = ENV["KEY_ALIAS"] - UI.user_error!("Missing Android release signing credentials") if [store_password, key_password, key_alias].any? { |value| value.to_s.empty? } - releaseFilePath = ENV["KEEPER_ANDROID_KEYSTORE"] || File.expand_path("../app/release.keystore", __dir__) - UI.user_error!("Missing Android release keystore") unless File.file?(releaseFilePath) - gradle(task: 'clean') - gradle( - task: 'bundle', - build_type: 'productionRelease', - print_command: false, - properties: { - "android.injected.signing.store.file" => releaseFilePath, - "android.injected.signing.store.password" => store_password, - "android.injected.signing.key.alias" => key_alias, - "android.injected.signing.key.password" => key_password, - } - ) - aab_path = Actions.lane_context[SharedValues::GRADLE_AAB_OUTPUT_PATH].to_s - UI.user_error!("Build did not return an AAB path") if aab_path.empty? - gradle( - task: 'assemble', - build_type: 'productionRelease', - print_command: false, - properties: { - "android.injected.signing.store.file" => releaseFilePath, - "android.injected.signing.store.password" => store_password, - "android.injected.signing.key.alias" => key_alias, - "android.injected.signing.key.password" => key_password, - } - ) - apk_path = Actions.lane_context[SharedValues::GRADLE_APK_OUTPUT_PATH].to_s - UI.user_error!("Build did not return an APK path") if apk_path.empty? - verifier = File.expand_path("../../scripts/verify-release-artifact.py", __dir__) - sh("python3", verifier, aab_path) - sh("python3", verifier, apk_path) + artifacts = build_verified_production_android_artifacts! upload_to_play_store( track: 'internal', - aab: aab_path + aab: artifacts[:aab] ) end end @@ -101,14 +114,14 @@ end after_all do |lane| versionCode = android_get_version_code() UI.message("Build success. Version Code: " + versionCode.to_s) - if ENV['KEEPER_NOTIFY_SLACK'] == '1' + if lane.to_s.split.last != 'build_only' && ENV['KEEPER_NOTIFY_SLACK'] == '1' slack(message: "Build success :rocket:\nVersion Code: " + versionCode.to_s) end end error do |lane, exception| UI.error("Fastlane failed: " + exception.message.to_s) - if ENV['KEEPER_NOTIFY_SLACK'] == '1' + if lane.to_s.split.last != 'build_only' && ENV['KEEPER_NOTIFY_SLACK'] == '1' slack( message: exception.message, success:false diff --git a/ios/fastlane/Fastfile b/ios/fastlane/Fastfile index a3dfa9739b..e67c168463 100644 --- a/ios/fastlane/Fastfile +++ b/ios/fastlane/Fastfile @@ -54,6 +54,51 @@ def upload_build_to_testflight! upload_to_testflight(options) end +def build_verified_production_ipa! + sh("node", File.expand_path("../../scripts/release-version.cjs", __dir__), "release-check") + production_env = File.expand_path("../../.env.production", __dir__) + UI.user_error!("Missing reviewed .env.production") unless File.file?(production_env) + sh("node", File.expand_path("../../scripts/verify-channel-config.cjs", __dir__), production_env) + ENV['ENVFILE'] = production_env + + get_certificates( # Create or get certificate, and install it + output_path: "./builds" # Download certificate in the build folder (you don't need to create the folder) + ) + + get_provisioning_profile( # Create or get provisioning profile + output_path: "./builds", # Download provisioning profile in the build folder + filename: "Keeper_Distribution.mobileprovision" # Rename the local provisioning profile + ) + + update_project_provisioning( # Set the project provisioning profile (related in Xcode to the General > Signing Release section) + xcodeproj: "hexa_keeper.xcodeproj", + target_filter: "hexa_keeper", # Name of your project + profile: "./builds/Keeper_Distribution.mobileprovision", + build_configuration: "Release" + ) + + update_project_team( # Set the right team on your project + teamid: CredentialsManager::AppfileConfig.try_fetch_value(:team_id) + ) + + app_identifier = resolved_app_identifier! + + # Only accept an artifact path produced by this invocation. + Actions.lane_context.delete(SharedValues::IPA_OUTPUT_PATH) + build_app(workspace: "hexa_keeper.xcworkspace", scheme: "hexa_keeper", clean: true, + export_method: "app-store", + export_options: { + provisioningProfiles: { + app_identifier => app_identifier + " AppStore" # Value of this parameter is the name of the Provisioning Profile. By default, it will be "{bundleId} AppStore" + } + }, + build_path: "./builds", + output_directory: "./builds") + ipa_path = resolved_ipa_path! + sh("python3", File.expand_path("../../scripts/verify-release-artifact.py", __dir__), ipa_path) + ipa_path +end + platform :ios do desc "Push a new dev build to TestFlight" lane :dev do @@ -102,46 +147,14 @@ platform :ios do upload_build_to_testflight! end + desc "Build and verify production IPA without uploading" + lane :build_only do + build_verified_production_ipa! + end + desc "Push a new live build to TestFlight" lane :live do - sh("node", File.expand_path("../../scripts/release-version.cjs", __dir__), "release-check") - production_env = File.expand_path("../../.env.production", __dir__) - UI.user_error!("Missing reviewed .env.production") unless File.file?(production_env) - sh("node", File.expand_path("../../scripts/verify-channel-config.cjs", __dir__), production_env) - ENV['ENVFILE'] = production_env - - get_certificates( # Create or get certificate, and install it - output_path: "./builds" # Download certificate in the build folder (you don't need to create the folder) - ) - - get_provisioning_profile( # Create or get provisioning profile - output_path: "./builds", # Download provisioning profile in the build folder - filename: "Keeper_Distribution.mobileprovision" # Rename the local provisioning profile - ) - - update_project_provisioning( # Set the project provisioning profile (related in Xcode to the General > Signing Release section) - xcodeproj: "hexa_keeper.xcodeproj", - target_filter: "hexa_keeper", # Name of your project - profile: "./builds/Keeper_Distribution.mobileprovision", - build_configuration: "Release" - ) - - update_project_team( # Set the right team on your project - teamid: CredentialsManager::AppfileConfig.try_fetch_value(:team_id) - ) - - app_identifier = resolved_app_identifier! - - build_app(workspace: "hexa_keeper.xcworkspace", scheme: "hexa_keeper", clean: true, - export_method: "app-store", - export_options: { - provisioningProfiles: { - app_identifier => app_identifier + " AppStore" # Value of this parameter is the name of the Provisioning Profile. By default, it will be "{bundleId} AppStore" - } - }, - build_path: "./builds", - output_directory: "./builds") - sh("python3", File.expand_path("../../scripts/verify-release-artifact.py", __dir__), resolved_ipa_path!) + build_verified_production_ipa! upload_build_to_testflight! end end diff --git a/release/README.md b/release/README.md index 4934c88c9f..7500d40554 100644 --- a/release/README.md +++ b/release/README.md @@ -13,13 +13,37 @@ The owner will perform real-phone and other manual live checks after release. Do 5. Build from a reviewed production configuration and existing signing identities. Check extracted IPA, APK and AAB metadata against the manifest, verify signing fingerprints and record checksums. A source preflight does not verify the resulting binaries. 6. Run all feasible automated, simulator/emulator and disposable checks before public submission; record the owner's post-release manual checks separately. -Both existing Fastlane `live` lanes run preflight before building and consume `.env.production` without downloading over `.env`. They no longer increment production build numbers. Android notifications require explicit `KEEPER_NOTIFY_SLACK=1`. +Both Fastlane `build_only` and `live` lanes run the same preflight, production build and artifact verification logic and consume `.env.production` without downloading over `.env`. They do not increment production build numbers. `build_only` never uploads and never notifies Slack, including on failures and when `KEEPER_NOTIFY_SLACK=1` is set. Android `live` notifications still require explicit `KEEPER_NOTIFY_SLACK=1`. These existing lanes still upload to **TestFlight** and **Play internal testing** respectively. Neither constitutes the requested public release. Public submission must record App Store review/submission state, Play production state and GitHub APK publication separately. +## Build-only production preparation + +The following commands are for a future **owner-approved production build**. `build_only` still obtains signing assets and creates signed production artifacts using the existing identities. Approval must name the intended commit, platform and production environment before these commands run. Simulator/emulator validation in the Codex Local handoff is separate and does not authorize signing, building production artifacts, upload or distribution. + +Refresh authenticated store evidence, review `.env.production` and provide the existing signing credentials first. Start each platform in its own clean, isolated checkout at the approved commit so one platform's signing/provisioning changes cannot affect the other platform's clean-source preflight. In each checkout, set `KEEPER_RELEASE_COMMIT` to that full verified commit and run `npm run release:preflight` before its command: + +```sh +# In the approved iOS checkout (requires the approved Mac/signing setup): +export KEEPER_RELEASE_COMMIT="$(git rev-parse HEAD)" +npm run release:preflight +(cd ios && ENVFILE=.env.production bundle exec fastlane build_only --env production) +``` + +```sh +# In the approved Android checkout (requires the established release keystore): +export KEEPER_RELEASE_COMMIT="$(git rev-parse HEAD)" +npm run release:preflight +(cd android && ENVFILE=.env.production bundle exec fastlane build_only --env production) +``` + +Record the generated IPA or AAB/APK paths, verifier output and checksums. The lanes require fresh artifact paths from their current build actions; a prior lane-context path cannot satisfy a missing output. Review signing/provisioning changes without committing credentials. Separate IPA/AAB signing verification and all distribution channels still need their own checks and approval. Do not use `live` for build-only preparation because it uploads. + +`ruby scripts/test_fastlane_build_only.rb` executes the real lane bodies and success/error hooks with fake actions in disposable fixtures. It checks preflight/config/artifact failures, rejection of old artifact paths, and absence of upload/Slack calls for `build_only`, while retaining `live` upload behavior. It needs Ruby 3.3 with the bundled Minitest gem; it never invokes Fastlane, native build tools, signing, stores or Slack. Run it through `npm run test:release` with the source and packaged-artifact regression suites. + ## Packaged artifact checks -Run `python3 scripts/verify-release-artifact.py /absolute/path/to/build.ipa` (or `.apk` / `.aab`). The production lanes run this before uploading. Android builds both artifacts and verifies them before its first upload. The iOS lane requires the current build's IPA path and never falls back to an older file in the builds directory. +Run `python3 scripts/verify-release-artifact.py /absolute/path/to/build.ipa` (or `.apk` / `.aab`). Both production lanes run this; `live` does so before uploading. Android builds both artifacts and verifies them before its first upload. Both platforms clear prior artifact paths before their build actions and require the current build's output; the iOS lane never falls back to an older file in the builds directory. The verifier checks the production application ID, marketing version, native counter, and bundled Ask Keeper / Dust route and data markers, then emits artifact and bundle SHA-256 hashes. Marker presence supplements device testing; it does not prove behavior or source provenance. Compiler-generated component names are unsuitable markers because release optimization removes them. diff --git a/scripts/test_fastlane_build_only.rb b/scripts/test_fastlane_build_only.rb new file mode 100644 index 0000000000..667b1139d0 --- /dev/null +++ b/scripts/test_fastlane_build_only.rb @@ -0,0 +1,265 @@ +# Execute the real Fastfile lane bodies with fake actions only. No Fastlane gems, +# shell commands, signing, store access or Slack requests are used by this suite. +require 'minitest/autorun' +require 'tmpdir' +require 'fileutils' + +module SharedValues + IPA_OUTPUT_PATH = :ipa_output_path + GRADLE_AAB_OUTPUT_PATH = :gradle_aab_output_path + GRADLE_APK_OUTPUT_PATH = :gradle_apk_output_path + SIGH_NAME = :sigh_name +end + +module Actions + def self.lane_context + @lane_context ||= {} + end +end + +module CredentialsManager + module AppfileConfig + def self.try_fetch_value(key) + { app_identifier: 'io.hexawallet.keeper', team_id: 'existing-team' }[key] + end + end +end + +module UI + def self.user_error!(message) + raise message + end + + def self.message(_message); end + def self.error(_message); end +end + +class FakeFastlane + attr_reader :events, :root + attr_accessor :failure, :missing_output + + SIGNING_ACTIONS = %i[get_certificates get_provisioning_profile update_project_provisioning update_project_team].freeze + DISTRIBUTION_ACTIONS = %i[upload_to_testflight upload_to_play_store slack].freeze + + def initialize(target_platform, root) + @root = root + @events = [] + @lanes = {} + file = File.expand_path("../#{target_platform}/fastlane/Fastfile", __dir__) + # Keep __dir__ resolution and real File.file? checks within the disposable fixture. + instance_eval(File.read(file), File.join(root, target_platform.to_s, 'fastlane', 'Fastfile')) + end + + def default_platform(_platform); end + def desc(_description); end + def platform(_platform, &block) + instance_eval(&block) + end + + def lane(name, &block) + @lanes[name] = block + end + + def after_all(&block) + @after_all = block + end + + def error(&block) + @error = block + end + + def run(name) + result = @lanes.fetch(name).call + @after_all&.call(name) + result + rescue StandardError => exception + @error&.call(name, exception) + raise + end + + def sh(command, script, *arguments) + kind = case [command, File.basename(script)] + when ['node', 'release-version.cjs'] then :preflight + when ['node', 'verify-channel-config.cjs'] then :config + when ['python3', 'verify-release-artifact.py'] then :artifact + else raise "Unexpected shell action: #{command}" + end + @events << [kind, script, *arguments] + raise "Simulated #{kind} failure" if @failure == kind || @failure == arguments.first + end + + def build_app(options) + @events << [:build_app, options] + emit_artifact(:ipa, SharedValues::IPA_OUTPUT_PATH) + end + + def gradle(options) + @events << [:gradle, options] + case options[:task] + when 'bundle' then emit_artifact(:aab, SharedValues::GRADLE_AAB_OUTPUT_PATH) + when 'assemble' then emit_artifact(:apk, SharedValues::GRADLE_APK_OUTPUT_PATH) + when 'clean' then nil + else raise "Unexpected Gradle task: #{options[:task]}" + end + end + + def get_build_number + 620 + end + + def android_get_version_code + 627 + end + + def method_missing(name, *arguments) + return @events << [name, *arguments] if (SIGNING_ACTIONS + DISTRIBUTION_ACTIONS).include?(name) + + super + end + + def respond_to_missing?(name, include_private = false) + (SIGNING_ACTIONS + DISTRIBUTION_ACTIONS).include?(name) || super + end + + private + + def emit_artifact(extension, key) + return if @missing_output == extension + + path = File.join(@root, "current.#{extension}") + File.write(path, 'synthetic artifact') + Actions.lane_context[key] = path + end +end + +class FastlaneBuildOnlyTest < Minitest::Test + ENV_KEYS = %w[ENVFILE KEEPER_NOTIFY_SLACK KEEPER_ANDROID_KEYSTORE STORE_PASSWORD KEY_PASSWORD KEY_ALIAS].freeze + + def setup + @saved_env = ENV_KEYS.to_h { |key| [key, ENV[key]] } + @root = Dir.mktmpdir('keeper-fastlane-test-') + File.write(File.join(@root, '.env.production'), 'synthetic reviewed config') + FileUtils.mkdir_p(File.join(@root, 'android', 'app')) + File.write(File.join(@root, 'android', 'app', 'release.keystore'), 'synthetic keystore') + ENV['KEEPER_NOTIFY_SLACK'] = '1' # build_only must ignore even the opt-in. + ENV.delete('KEEPER_ANDROID_KEYSTORE') + %w[STORE_PASSWORD KEY_PASSWORD KEY_ALIAS].each { |key| ENV[key] = 'synthetic' } + Actions.lane_context.clear + end + + def teardown + @saved_env.each { |key, value| value.nil? ? ENV.delete(key) : ENV[key] = value } + Actions.lane_context.clear + FileUtils.remove_entry(@root) + end + + def distribution_events(fastlane) + fastlane.events.select { |event| FakeFastlane::DISTRIBUTION_ACTIONS.include?(event.first) } + end + + %i[ios android].each do |platform| + define_method("test_#{platform}_build_only_verifies_current_production_artifacts_without_distribution") do + fastlane = FakeFastlane.new(platform, @root) + fastlane.run(:build_only) + assert_empty distribution_events(fastlane) + assert_equal %i[preflight config], fastlane.events.take(2).map(&:first) + assert_equal ['release-check'], fastlane.events.first.drop(2) + assert_equal File.join(@root, '.env.production'), ENV['ENVFILE'] + artifacts = fastlane.events.select { |event| event.first == :artifact }.map(&:last) + extensions = platform == :ios ? %w[ipa] : %w[aab apk] + assert_equal extensions.map { |ext| File.join(@root, "current.#{ext}") }, artifacts + if platform == :ios + options = fastlane.events.find { |event| event.first == :build_app }.last + assert_equal 'hexa_keeper', options[:scheme] + assert_equal 'app-store', options[:export_method] + assert_equal true, options[:clean] + assert_equal({ 'io.hexawallet.keeper' => 'io.hexawallet.keeper AppStore' }, options[:export_options][:provisioningProfiles]) + else + builds = fastlane.events.select { |event| event.first == :gradle }.map(&:last) + assert_equal %w[clean bundle assemble], builds.map { |options| options[:task] } + assert builds.drop(1).all? { |options| options[:build_type] == 'productionRelease' } + assert builds.drop(1).all? { |options| options[:properties]['android.injected.signing.store.file'] == File.join(@root, 'android', 'app', 'release.keystore') } + end + end + + %i[preflight config artifact].each do |failure| + define_method("test_#{platform}_build_only_stops_on_#{failure}_failure_without_distribution") do + fastlane = FakeFastlane.new(platform, @root) + fastlane.failure = failure + assert_raises(RuntimeError) { fastlane.run(:build_only) } + assert_empty distribution_events(fastlane) + if failure != :artifact + assert_empty fastlane.events.select { |event| (FakeFastlane::SIGNING_ACTIONS + %i[build_app gradle artifact]).include?(event.first) } + end + end + end + + define_method("test_#{platform}_build_only_rejects_missing_reviewed_environment_without_distribution") do + File.delete(File.join(@root, '.env.production')) + fastlane = FakeFastlane.new(platform, @root) + assert_match(/Missing reviewed/, assert_raises(RuntimeError) { fastlane.run(:build_only) }.message) + assert_equal [:preflight], fastlane.events.map(&:first) + assert_empty distribution_events(fastlane) + end + + define_method("test_#{platform}_live_still_uploads_only_after_verification") do + fastlane = FakeFastlane.new(platform, @root) + fastlane.run(:live) + upload = platform == :ios ? :upload_to_testflight : :upload_to_play_store + assert_equal 1, fastlane.events.count { |event| event.first == upload } + artifact_positions = fastlane.events.each_index.select { |index| fastlane.events[index].first == :artifact } + assert artifact_positions.all? { |index| index < fastlane.events.index { |event| event.first == upload } } + options = fastlane.events.find { |event| event.first == upload }.last + assert_equal File.join(@root, platform == :ios ? 'current.ipa' : 'current.aab'), options[platform == :ios ? :ipa : :aab] + assert_equal 'internal', options[:track] if platform == :android + assert_equal platform == :android ? 1 : 0, fastlane.events.count { |event| event.first == :slack } + end + + define_method("test_#{platform}_live_does_not_upload_after_failed_artifact_verification") do + fastlane = FakeFastlane.new(platform, @root) + fastlane.failure = :artifact + assert_raises(RuntimeError) { fastlane.run(:live) } + assert_empty fastlane.events.select { |event| %i[upload_to_testflight upload_to_play_store].include?(event.first) } + end + end + + { ios: { ipa: SharedValues::IPA_OUTPUT_PATH }, android: { aab: SharedValues::GRADLE_AAB_OUTPUT_PATH, apk: SharedValues::GRADLE_APK_OUTPUT_PATH } }.each do |platform, outputs| + outputs.each do |extension, key| + define_method("test_#{platform}_rejects_stale_#{extension}_from_prior_invocation") do + stale = File.join(@root, "stale.#{extension}") + File.write(stale, 'old artifact') + Actions.lane_context[key] = stale + fastlane = FakeFastlane.new(platform, @root) + fastlane.missing_output = extension + assert_match(/path/, assert_raises(RuntimeError) { fastlane.run(:build_only) }.message) + assert_empty distribution_events(fastlane) + assert_empty fastlane.events.select { |event| event.first == :artifact } + refute Actions.lane_context.key?(key) + end + end + end + + def test_android_rejects_missing_signing_credentials_before_building + ENV.delete('STORE_PASSWORD') + fastlane = FakeFastlane.new(:android, @root) + assert_match(/signing credentials/, assert_raises(RuntimeError) { fastlane.run(:build_only) }.message) + assert_empty fastlane.events.select { |event| event.first == :gradle } + assert_empty distribution_events(fastlane) + end + + def test_android_rejects_missing_keystore_before_building + File.delete(File.join(@root, 'android', 'app', 'release.keystore')) + fastlane = FakeFastlane.new(:android, @root) + assert_match(/keystore/, assert_raises(RuntimeError) { fastlane.run(:build_only) }.message) + assert_empty fastlane.events.select { |event| event.first == :gradle } + assert_empty distribution_events(fastlane) + end + + def test_android_build_only_checks_apk_even_after_aab_passes + fastlane = FakeFastlane.new(:android, @root) + fastlane.failure = File.join(@root, 'current.apk') + assert_raises(RuntimeError) { fastlane.run(:build_only) } + assert_equal %w[aab apk], fastlane.events.select { |event| event.first == :artifact }.map { |event| File.extname(event.last).delete_prefix('.') } + assert_empty distribution_events(fastlane) + end +end From 33ac0bc69d5d80e3d35be4dd5cfbc63568699600 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Mon, 12 Oct 2026 06:19:07 +0000 Subject: [PATCH 112/254] ci: exercise production build-only lanes without distribution --- .github/workflows/test.yml | 5 +++++ package.json | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 8b7fbf90b1..6a884149c7 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -70,6 +70,11 @@ jobs: - name: Verify checked-out release version consistency run: npm run version:check + - name: Set up Ruby for build-only release lane regressions + uses: ruby/setup-ruby@v1 + with: + ruby-version: '3.3.8' + - name: Run release and packaged-artifact tests run: npm run test:release diff --git a/package.json b/package.json index 08bacbae78..434d8b3777 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "iosProd": "npx react-native run-ios --scheme=hexa_keeper --simulator 'iPhone 16' ", "start": "react-native start", "test": "jest", - "test:release": "node --test scripts/release-version.test.cjs scripts/verify-channel-config.test.cjs && python3 -B -m unittest discover -s scripts -p 'test_release_artifact.py'", + "test:release": "node --test scripts/release-version.test.cjs scripts/verify-channel-config.test.cjs && python3 -B -m unittest discover -s scripts -p 'test_release_artifact.py' && ruby scripts/test_fastlane_build_only.rb", "version:inspect": "node scripts/release-version.cjs inspect", "version:check": "node scripts/release-version.cjs check", "version:apply": "node scripts/release-version.cjs apply", From 276333c8fcfb644ffb83f001dab5124986d00ff8 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Mon, 12 Oct 2026 06:21:32 +0000 Subject: [PATCH 113/254] docs: prepare inheritance acceptance steps and conditional release copy --- release/r4-inheritance-validation.md | 99 ++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 release/r4-inheritance-validation.md diff --git a/release/r4-inheritance-validation.md b/release/r4-inheritance-validation.md new file mode 100644 index 0000000000..96072ecf3a --- /dev/null +++ b/release/r4-inheritance-validation.md @@ -0,0 +1,99 @@ +# R4 inheritance and affected timelock validation + +This checklist belongs to the R4 / 2.6.4 candidate in PR #7045. Read its latest Codex local handoff for the exact tested source SHA. Native and hardware acceptance remain open. The copy below is conditional on the documented acceptance and approved release; it does not authorize publishing or customer communication. + +## Row 249: existing decaying-passphrase setup + +The live tracker requires the same BIP39 words **with** a passphrase as the primary key and **without** a passphrase as the Inheritance Key in a 1-of-1 Miniscript wallet, with six-month activation. This is an existing inheritance-policy configuration, not a new passphrase-recovery engine. + +### What the source supports + +- `AddNewWallet.tsx` offers Single-key, then Enhanced Security Options → Inheritance Key. Enhanced option selection checks `usePlan().isOnL3Above`; the Inheritance Planning education entry checks `isOnL2Above`. In the current candidate **usePlan hardcodes L3**, as does useSubscriptionLevel, so both checks are presently true regardless of stored subscription. These source conditions do not establish actual store/product entitlement or require a purchase in this candidate. Observe the final build and confirm intended product availability before publishing customer steps; no entitlement logic was changed by this subtask. +- The primary AddSigningDevice step routes to AddReserveKey when inheritance is selected. AddReserveKey supports one inheritance signer and offers 6, 12, 18, …, 60 months; the default is **12 months**, so the user must explicitly choose **6 months**. +- `VaultMigrationController.tsx` computes one fixed activation lock: creation time + optional initial Wallet Timelock duration + selected inheritance delay. New wallets use a UNIX timestamp from `Date.now()`; re-vaulting an existing block-height policy preserves the block-height unit. Policy compilation uses `after(...)`, not a per-UTXO `older(...)` delay. Consensus timestamp maturity uses chain median time, so wall-clock presentation is not a guarantee of spend finality. +- Mainnet MONTHS_6 means a fixed **180 days**, rather than six calendar months. Testnet's identically labelled option is accelerated to **60 minutes** for new timestamp wallets; the legacy height fixture is **6 blocks**. Testnet acceptance must inspect the actual descriptor and must not mistake its accelerated duration for mainnet behavior. +- A 1-of-1 wallet has the primary key available before inheritance activation (unless a separate initial Wallet Timelock is deliberately enabled), and adds a distinct one-key Inheritance path at the fixed activation. The original primary path remains available afterward. For other quorum sizes, an Inheritance Key joins the quorum; generic copy must not promise that it alone spends every wallet. +- `generateSeedWordsKey` and the software seed-key setup derive from words without a passphrase; there is no in-app passphrase field in that path. The passphrase-protected primary key must be derived/exported by a suitable external signer, with its fingerprint/xpub checked against the chosen passphrase. Keeper cannot infer that two xpubs came from the same words. +- The source Miniscript allowlist includes MY_KEEPER, TAPSIGNER, SATOCHIP, BITBOX02, COLDCARD, JADE, KRUX, LEDGER, SPECTER, SEED_WORDS, KEEPER and POLICY_SERVER. This is a selection allowlist, not hardware/firmware proof. The fixture passphrase string `TREZOR` is a public BIP39 test value; it does not establish Trezor compatibility (TREZOR is absent from that allowlist). +- Receiving funds, spending funds, generating more addresses, opening Keeper or entering a passphrase does **not** restart activation. Later deposits to the same wallet descriptor retain its existing fixed date. Resetting uses an explicit new wallet version and fund transfer, with network fees and signer availability; old addresses keep the old policy. +- Wallet Settings → Wallet configuration file offers QR/file/PDF export; it contains the actual policy and is needed alongside the appropriate seed/passphrase recovery material. Wallet Settings has a separate Resetting Inheritance Key → re-vault flow. That flow must not be presented as an automatic inactivity reset. + +### Targeted source changes in this continuation + +AddReserveKey now uses a separate English/Spanish inheritance disclosure explaining the fixed activation, inclusion of any Wallet Timelock, non-resetting deposits/spends, and the need to re-vault and transfer funds to reset. The shared Emergency Key copy remains unchanged. The existing disposable policy test additionally checks that only the passphrase primary is available before activation, the seed-only path has threshold one after activation, and generating addresses/reading availability does not change the descriptor. + +An additional concrete UI finding: the Inheritance Planning CTA passes `params.isAddInheritanceKeyFromParams`, while AddNewWallet previously read `route.isAddInheritanceKeyFromParams`. The scoped fix reads it from the existing `route.params` binding. The regression executes the actual CTA navigation expression, actual route binding and actual React state initializer; it covers the entry, ordinary creation without params, and retained inheritance policy. The main Create Wallet path still requires explicit Enhanced Security selection. Record the final fix SHA and validate this alternative entry separately. + +### Cloud checks in this continuation + +- `npm test -- --runInBand tests/services/passphraseDecayPolicy.test.ts tests/services/inheritanceEntryRoute.test.ts tests/services/initialTimelock.test.ts tests/services/addReserveTimelockBoundary.test.ts tests/services/addSigningDeviceTimelock.test.ts tests/services/resetTimelockBoundary.test.ts tests/services/resetTimelockRoute.test.ts`: **7/7 suites, 118/118 tests passed**. After refining the route read through its existing destructuring, the two directly changed suites were rerun: **2/2 suites, 6/6 tests passed**. +- Test-file Prettier, English/Spanish JSON parsing and disclosure-key presence, and `git diff --check`: **passed**. +- Scoped screen ESLint: **fails with existing screen debt**. Before/after isolated source comparison: AddNewWallet 27→26 errors, unchanged 15 warnings; AddReserveKey unchanged 33 errors/2 warnings. Config ignores test files. No newly introduced screen lint diagnostic; the obsolete root-route property diagnostic disappears. Full JSON Prettier check also fails the existing localization formatting; no whole-file reformat was applied. +- React best-practices review applied to the two narrow TSX edits: no new hooks, async work, components or subscriptions. Native accessibility/text-layout acceptance remains open for the longer disclosure. +- No simulator/emulator, physical signer, fund transfer, customer wallet assessment, production action, customer reply or release was performed by this audit. + +### Exact UI steps to validate before customer use + +These steps are source-derived and remain **draft** until observed on the final iOS and Android builds with a supported signer/firmware: + +1. Use a disposable Testnet account and a signer capable of the generated Miniscript policy. Prepare the same public test seed with a test passphrase and with no passphrase as two distinct signer identities. Import/export the appropriate P2WSH/multisig xpubs and origins; record fingerprints. Do not use real recovery material in screenshots or logs. +2. From Home's wallet area, open the add-wallet control → **Create Wallet** → **Single-key**. Open **Enhanced Security Options**, select **Inheritance Key**, leave **Emergency Key** and **Wallet Timelock** off for this specific setup, then **Save Changes** and **Proceed**. Record whether the final build allows selection; if an actual product gate appears, stop rather than bypassing it. Do not infer paid-plan requirements from the current candidate's hardcoded L3 helper. +3. On key selection, choose the key derived with the passphrase as the sole primary key, then **Proceed**. Do not substitute Keeper's passphrase-free seed-key import for that primary. +4. On **Add Inheritance Key**, add/select the key derived from the same words without the passphrase. Check that its fingerprint/xpub is distinct from the primary. Select **6 months** explicitly; confirm that the fixed-date disclosure is visible and readable, then **Confirm**. +5. Name the wallet and use **Create Your Wallet**. Open its **Wallet Settings** → **Wallet configuration file** and save the disposable descriptor/config. Confirm the primary role is the passphrase key, the inheritance role is the no-passphrase key, and the policy has exactly the expected fixed fallback `after(...)` lock. Confirm no unintended initial primary timelock was introduced. +6. Complete the signing/receive tests below before moving this wording out of draft. For a mainnet customer, include the 180-day duration, fixed-date limitation, supported firmware and recovery/config requirement; do not claim generic passphrase import or unrestricted signer support. + +### Local acceptance checklist (IOS, ANDROID, HARDWARE) + +Record the final full app SHA, app bundle/package/version/build, OS/device, signer model/firmware, and pass/fail/not run per row. Use disposable Testnet funds only. + +| Scenario | Expected evidence | +| --- | --- | +| UI and entitlement | Main Create Wallet path above works; English/Spanish disclosure wraps fully on a small screen and large font size; the 6-month choice is deliberate; disabled/unsupported signers explain the limitation. | +| Inheritance Planning entry | “Create vault with Inheritance Key” reaches creation with inheritance preselected after the scoped route-param fix. Record final availability/product gating; the current helper hardcodes L3 and does not prove subscription enforcement. | +| Two identities from the same seed | Passphrase and no-passphrase fingerprint/xpub differ; primary and inheritance roles match the imported origins. No stored field or heuristic is treated as proof of common seed words. | +| Descriptor and receive addresses | Two receive addresses plus change branch share one fixed fallback lock. Mainnet read-only policy inspection shows 180 days; Testnet actual descriptor shows its accelerated duration. Export/reimport preserves exact policy and origins. | +| Before activation | Supported hardware successfully signs the primary Testnet path. The no-passphrase key alone cannot satisfy/finalize the pre-activation fallback. Record actual transaction/script outcome, not only whether a key appears selectable. | +| After activation | Once chain median time or height is beyond the descriptor's lock, the no-passphrase key alone signs/finalizes a disposable fallback transaction; original primary still works. Record the maturity source and lock value. | +| No reset on use | Receive before and after activation; make a primary spend before activation; refresh/restart. Descriptor and fallback lock remain identical. A later deposit does not acquire a new six-month clock. | +| Recovery | On a separate disposable install, restore from exported wallet configuration with the required signer/seed material. Verify addresses and lock unchanged. Do not overwrite an owner's installed app or wallet data. | +| Re-vault limits | Do not initiate on a real wallet. If authorized for a disposable fixture, separately verify new policy/schedule, explicit transfer/fees, cancel/retry/restart and old-address continuity. This is not required to call the existing fixed-date setup an inactivity reset. | + +Pending validation: exact UI, native builds/launch, hardware signing and fallback maturity, restore, and final release availability. Source fixture checks alone do not establish these. + +### Conditional release and requester copy + +Use only after the final build passes relevant acceptance and is actually released through an owner-approved channel. Confirm precise signer/firmware steps from Local before sending. No message or social post is sent by this Cloud continuation. + +**Release note draft:** “Support for a decaying-passphrase inheritance setup: configure a 1-of-1 Miniscript wallet with the passphrase-protected key as primary and the same seed without its passphrase as the Inheritance Key. The six-month option is a fixed 180-day activation delay on mainnet; activity and later deposits do not restart it. Requires compatible signers and the wallet configuration for recovery.” + +**Social draft:** “A decaying-passphrase inheritance setup is supported in Keeper 2.6.4 with compatible Miniscript signers. Its activation is fixed when the wallet is created: sending, receiving or using the wallet does not restart the delay. See the validated setup and recovery instructions before funding.” Add an actual published documentation/release link only when one exists. + +**Requester reply draft:** “Your same-seed, with-passphrase / without-passphrase setup has been validated in Keeper 2.6.4 using [actual device/firmware and platforms]. Choose Single-key → Enhanced Security Options → Inheritance Key; use the with-passphrase key as primary, the no-passphrase key as Inheritance Key, and select 6 months. On mainnet this is a fixed 180-day delay from wallet creation, not a new six-month timer for each deposit or after activity. Keep the exported wallet configuration and your recovery material. To start a new delay you must explicitly re-vault and transfer funds, including the network fee. [Published release/instructions link].” Replace placeholders only with actual acceptance/release evidence; do not send before then. + +## Row 252: affected Wallet Timelock assessment + +The preserved artifact named by the live tracker is `outputs/r02-timelock-preserved-before-scope-trim-2026-10-02` (earlier pure read-only assessor plus 34 tests). It is **not present in this Cloud workspace or active candidate**. Preserve this missing dependency by name; do not silently replace it with a fresh detector that guesses prior intent. Existing `initialTimelock.test.ts` creates affected/intended disposable policies and compares them; it is not a stored-wallet assessor or repair implementation. + +Current source boundary: + +- `resolveInitialTimelock` rejects malformed or absent selection when TIMELOCKED is required. Current screen/controller tests cover route propagation and generated initial/fallback schedule. These guard future creation and re-vaulting; they do not repair already funded scripts. +- VaultDetails' timelock presentation and VaultSettings' reset entry use retained `usedMiniscriptTypes.TIMELOCKED` and stored timelock arrays. They do not compare the actual compiled primary spending path with independent original-lock intent. +- A retained TIMELOCKED marker with an unlocked primary path can signal a contradiction. When the intent marker is absent, an unlocked primary may be entirely legitimate; inference from inheritance/emergency locks alone cannot recover an original initial-lock duration. +- Existing Resetting Wallet Timelock or Resetting Inheritance Key flows create a new policy/version and transfer funds. They do not rewrite an already funded Bitcoin output script. Their existence is not row 252 assessment acceptance. + +### Safe assessor port requirements, once preserved source is available + +1. Recover the preserved source/tests without overwriting any work, identify original provenance and exact commit/artifact hash, review its assumptions, and port it as a separate read-only deliverable. +2. Accept only stored compiled primary-path evidence plus independent intent evidence (trusted original creation selection/config/owner-confirmed record). Inspect all supported primary spending alternatives and units; metadata alone is insufficient. Never serialize seed/private material or identifiable wallet data to shared logs. +3. Report at least affected / no demonstrated contradiction / unknown with evidence and explicit unknown reason. Absence of evidence must remain unknown, rather than a guessed duration or safety verdict. A matching policy is “not affected by this omission,” not a general wallet-security assessment. +4. Cover retained TIMELOCKED+unlocked primary, omitted metadata but independently attested intent, legitimate no-initial-lock inheritance, timestamp/height policies, contradictory descriptors vs phase metadata, malformed/unsupported policy, multiple primary paths, partial key/origin state and no independent intent. Reuse/extend the preserved 34 tests and record exact final results. +5. Prove inspection leaves wallet, backup, server, UTXO and navigation state unchanged; no sign/sweep/registration/upload side effects. Keep any proposed repair separate and inactive. + +### Later funded-repair acceptance (explicit owner consent required) + +Any repair needs a new wallet script and signed migration/sweep. Before a funded operation, privately review original intent, corrected primary lock and inheritance/emergency schedule, signer/recovery readiness, exact amount/fee and destination; get explicit owner consent. Do not guess the original duration, auto-migrate, shorten fallback schedules or treat a reset button as consent to security-policy repair. + +Disposable repair acceptance must include fee display and consent, cancel without mutation, retry without duplicate sweep, restart/resume, partial/failed transfer, already-locked current wallet behavior, existing archived-wallet visibility/sync, old address receiving after migration, finding and recovering any remaining old-address UTXOs, configuration/recovery continuity and account isolation. Real-wallet repair is outside #7045 authorization. + +Remaining row 252 blockers: preserved assessor/source and tests unavailable; independent per-wallet initial-lock intent evidence; separate assessor implementation/review; native read-only acceptance; and any later explicitly consented repair design/validation. No user wallet is assessed or repaired by this audit. From 0ac33a839846fb0bf62b5c053c267bba69ddbaf3 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Mon, 12 Oct 2026 06:21:50 +0000 Subject: [PATCH 114/254] test: stabilize stacked release CI with deterministic Electrum fixtures --- tests/services/client.test.ts | 149 ++++++++++++++++++------ tests/services/electrumWalletFixture.ts | 79 +++++++++++++ tests/services/vault.test.ts | 9 ++ tests/services/wallet.test.ts | 5 + 4 files changed, 204 insertions(+), 38 deletions(-) create mode 100644 tests/services/electrumWalletFixture.ts diff --git a/tests/services/client.test.ts b/tests/services/client.test.ts index 7b1381530b..a9b548153e 100644 --- a/tests/services/client.test.ts +++ b/tests/services/client.test.ts @@ -3,24 +3,88 @@ import ElectrumClient from '../../src/services/electrum/client'; import { predefinedTestnetNodes } from '../../src/services/electrum/predefinedNodes'; import * as bitcoinJS from 'bitcoinjs-lib'; +const mockClients: any[] = []; +const mockTxid = '24913105c497b4bb4ccf81b03857c8306aba0e58ccde792d981f6c18efdb24b8'; + +// Exercise the real ElectrumClient facade against deterministic protocol replies. +// A public Electrum server is outside the control of CI. +jest.mock('electrum-client', () => ({ + __esModule: true, + default: jest.fn().mockImplementation((_net, _tls, _port, host) => { + const client = { + host, + initElectrum: jest.fn(async () => { + if (host === 'blackie.c3-soft.com') { + throw new Error('connect ECONNREFUSED 127.0.0.1:50002'); + } + return ['fixture-server', '1.4']; + }), + close: jest.fn(), + onError: null, + server_ping: jest.fn(async () => undefined), + server_features: jest.fn(async () => ({ + server_version: 'fixture-server', + protocol_min: '1.4', + protocol_max: '1.4.3', + })), + blockchainScripthash_listunspentBatch: jest.fn(async (hashes) => + hashes.map((param, index) => ({ + param, + result: index === 0 ? [{ tx_hash: mockTxid, tx_pos: 1, value: 12345, height: 100 }] : [], + })) + ), + blockchainScripthash_getHistoryBatch: jest.fn(async (hashes) => + hashes.map((param, index) => ({ + param, + result: index === 0 ? [{ tx_hash: mockTxid, height: 100 }] : [], + })) + ), + blockchainEstimatefee: jest.fn(async () => 0.00002), + blockchainTransaction_getBatch: jest.fn(async (txids) => + txids.map((param) => ({ + param, + result: { + txid: param, + hex: 'fixture-hex', + vout: [{ scriptPubKey: { address: 'tb1qfixture' } }], + }, + })) + ), + }; + mockClients.push(client); + return client; + }), +})); + jest.mock('src/store/store', () => ({ store: { getState: () => ({ - settings: { - bitcoinNetwork: 'testnet', - }, + settings: { bitcoinNetworkType: 'TESTNET' }, }), }, })); -beforeAll(async () => { - try { - ElectrumClient.setActivePeer(predefinedTestnetNodes); - await ElectrumClient.connect(); - } catch (err) { - console.log('failed to connect to Electrum:', err); - process.exit(1); - } +const onlineNode = { + ...predefinedTestnetNodes[0], + id: 337, + host: 'electrum-fixture.test', + isConnected: true, +}; +const offlineNode = { + ...predefinedTestnetNodes[0], + isConnected: true, +}; +const addresses = [ + 'tb1qd2u9tvuqzadgeh02vppd33e7u2fatuwrw7h4q5', + 'tb1qwdm8hdyvv5jn05qq858lgnk50heucvxvqtl4sx', +]; + +beforeEach(async () => { + mockClients.length = 0; + ElectrumClient.setActivePeer([onlineNode], onlineNode); + ElectrumClient.resetCurrentPeerIndex(); + const connection = await ElectrumClient.connect(); + assert.ok(connection.connected); }); afterAll(() => { @@ -30,23 +94,33 @@ afterAll(() => { describe('ElectrumClient', () => { describe('Connection Tests', () => { it('should test connection successfully', async () => { - const result = await ElectrumClient.testConnection(predefinedTestnetNodes[0]); + const result = await ElectrumClient.testConnection(onlineNode); assert.ok(result.connected); }); it('should fail connection when node is unreachable', async () => { - const unreachableNode = { ...predefinedTestnetNodes[0], host: 'invalid.host' }; - const result = await ElectrumClient.testConnection(unreachableNode); + const result = await ElectrumClient.testConnection(offlineNode); assert.ok(!result.connected); assert.ok(result.error); + assert.strictEqual(result.errorType, 'network'); }); it('should reconnect to the next peer when connection fails', async () => { - ElectrumClient.setActivePeer(predefinedTestnetNodes); + ElectrumClient.setActivePeer([offlineNode, onlineNode], offlineNode); + ElectrumClient.resetCurrentPeerIndex(); + const result = await ElectrumClient.connect(); + assert.ok(result.connected); + assert.strictEqual(result.connectedTo, onlineNode.host); + assert.strictEqual(ElectrumClient.getActivePeer()?.host, onlineNode.host); + + assert.ok(mockClients.some((client) => client.host === offlineNode.host)); + }); + + it('should reconnect after explicit disconnection', async () => { ElectrumClient.forceDisconnect(); const result = await ElectrumClient.reconnect(); assert.ok(result.connected); - assert.ok(result.connectedTo); + assert.strictEqual(result.connectedTo, onlineNode.host); }); }); @@ -60,51 +134,50 @@ describe('ElectrumClient', () => { describe('Feature Tests', () => { it('should retrieve server features', async () => { const features = await ElectrumClient.serverFeatures(); - assert.ok(features.server_version); - assert.ok(features.protocol_min); - assert.ok(features.protocol_max); + assert.strictEqual(features.server_version, 'fixture-server'); + assert.strictEqual(features.protocol_min, '1.4'); + assert.strictEqual(features.protocol_max, '1.4.3'); }); }); describe('UTXO and History Tests', () => { it('should sync UTXOs by address', async () => { - const addresses = [ - 'tb1qd2u9tvuqzadgeh02vppd33e7u2fatuwrw7h4q5', - 'tb1qwdm8hdyvv5jn05qq858lgnk50heucvxvqtl4sx', - ]; const utxos = await ElectrumClient.syncUTXOByAddress(addresses, bitcoinJS.networks.testnet); - assert.ok(Object.keys(utxos).length > 0); + assert.deepStrictEqual(utxos[addresses[0]], [ + { txId: mockTxid, vout: 1, value: 12345, height: 100, address: addresses[0] }, + ]); + assert.deepStrictEqual(utxos[addresses[1]], []); }); it('should sync history by address', async () => { - const addresses = [ - 'tb1qd2u9tvuqzadgeh02vppd33e7u2fatuwrw7h4q5', - 'tb1qwdm8hdyvv5jn05qq858lgnk50heucvxvqtl4sx', - ]; const history = await ElectrumClient.syncHistoryByAddress( addresses, bitcoinJS.networks.testnet ); - assert.ok(Object.keys(history.historyByAddress).length > 0); - assert.ok(history.txids.length > 0); + assert.deepStrictEqual(history.historyByAddress[addresses[0]], [ + { tx_hash: mockTxid, height: 100 }, + ]); + assert.deepStrictEqual(history.historyByAddress[addresses[1]], []); + assert.deepStrictEqual(history.txids, [mockTxid]); + assert.strictEqual(history.txidToAddress[mockTxid], addresses[0]); }); }); describe('Transaction Tests', () => { it('should estimate fee', async () => { const fee = await ElectrumClient.estimateFee(1); - assert.ok(fee > 0); + assert.strictEqual(fee, 2); }); it('should fetch transactions by ID', async () => { - const txids = [ - '24913105c497b4bb4ccf81b03857c8306aba0e58ccde792d981f6c18efdb24b8', - '9a15008af30580039bc7a07d36c3544902a2e1618ad31f4dda94fe0044a8dcce', - ]; + const txids = [mockTxid, '9a15008af30580039bc7a07d36c3544902a2e1618ad31f4dda94fe0044a8dcce']; const transactions = await ElectrumClient.getTransactionsById(txids); - assert.ok(Object.keys(transactions).length === txids.length); - assert.deepStrictEqual(transactions[txids[0]].txid, txids[0]); - assert.deepStrictEqual(transactions[txids[1]].txid, txids[1]); + assert.deepStrictEqual(Object.keys(transactions), txids); + for (const txid of txids) { + assert.strictEqual(transactions[txid].txid, txid); + assert.strictEqual(transactions[txid].hex, undefined); + assert.deepStrictEqual(transactions[txid].vout[0].scriptPubKey.addresses, ['tb1qfixture']); + } }); }); }); diff --git a/tests/services/electrumWalletFixture.ts b/tests/services/electrumWalletFixture.ts new file mode 100644 index 0000000000..466523c6ed --- /dev/null +++ b/tests/services/electrumWalletFixture.ts @@ -0,0 +1,79 @@ +import ElectrumClient from 'src/services/electrum/client'; +import WalletOperations from 'src/services/wallets/operations'; + +const fundingTxid = '608710eb040d935dedb6c4d6321369382b2bd45533a7442a6fa98add3860d46b'; +const previousTxid = '89b193b61f7681ee588939932219f3d92e54a1873a9fb823647038f3879fba00'; +const externalAddress = 'tb1ql7w62elx9ucw4pj5lgw4l028hmuw80sndtntxt'; + +// The wallet and vault suites exercise address scanning, transaction parsing, coin selection, +// and PSBT signing. Give each generated wallet one funded testnet address without using a +// public Electrum server or a mutable public wallet balance. +export const installElectrumWalletFixture = () => { + let fundedAddress: string | null = null; + const syncWallets = WalletOperations.syncWalletsViaElectrumClient; + + jest.spyOn(WalletOperations, 'syncWalletsViaElectrumClient').mockImplementation((...args) => { + fundedAddress = null; + return syncWallets(...args); + }); + jest.spyOn(WalletOperations, 'fetchFeeRatesByPriority').mockImplementation(async () => + WalletOperations.mockFeeRates() + ); + + jest.spyOn(ElectrumClient, 'setActivePeer').mockImplementation(() => undefined); + jest.spyOn(ElectrumClient, 'connect').mockResolvedValue({ + connected: true, + connectedTo: 'fixture.electrum.test', + }); + jest.spyOn(ElectrumClient, 'forceDisconnect').mockImplementation(() => undefined); + jest.spyOn(ElectrumClient, 'getBlockchainHeaders').mockResolvedValue({ height: 85172, hex: '00' }); + jest.spyOn(ElectrumClient, 'syncUTXOByAddress').mockImplementation(async (addresses) => { + if (!fundedAddress) fundedAddress = addresses[0]; + return Object.fromEntries( + addresses.map((address) => [ + address, + address === fundedAddress + ? [{ txId: fundingTxid, vout: 1, value: 6000, height: 85000, address }] + : [], + ]) + ); + }); + jest.spyOn(ElectrumClient, 'syncHistoryByAddress').mockImplementation(async (addresses) => { + const hasFunding = addresses.includes(fundedAddress); + return { + historyByAddress: Object.fromEntries( + addresses.map((address) => [ + address, + hasFunding && address === fundedAddress ? [{ tx_hash: fundingTxid, height: 85000 }] : [], + ]) + ), + txids: hasFunding ? [fundingTxid] : [], + txidToAddress: hasFunding ? { [fundingTxid]: fundedAddress } : {}, + }; + }); + jest.spyOn(ElectrumClient, 'getTransactionsById').mockImplementation(async (txids) => { + const transactions: Record = {}; + for (const txid of txids) { + if (txid === fundingTxid) { + transactions[txid] = { + txid, + vin: [{ txid: previousTxid, vout: 0 }], + vout: [ + { value: 0.00001, scriptPubKey: { addresses: [externalAddress] } }, + { value: 0.00006, scriptPubKey: { addresses: [fundedAddress] } }, + ], + confirmations: 12, + time: 1700000000, + blocktime: 1700000000, + }; + } else if (txid === previousTxid) { + transactions[txid] = { + txid, + vin: [], + vout: [{ value: 0.0001, scriptPubKey: { addresses: [externalAddress] } }], + }; + } + } + return transactions; + }); +}; diff --git a/tests/services/vault.test.ts b/tests/services/vault.test.ts index ed6cf6f13b..7b102a7917 100644 --- a/tests/services/vault.test.ts +++ b/tests/services/vault.test.ts @@ -43,6 +43,7 @@ import { generateOutputDescriptors, parseTextforVaultConfig, } from 'src/utils/service-utilities/utils'; +import { installElectrumWalletFixture } from './electrumWalletFixture'; jest.mock('src/store/store', () => ({ store: { @@ -56,6 +57,10 @@ jest.mock('src/store/store', () => ({ jest.mock('realm', () => ({})); +beforeAll(() => { + installElectrumWalletFixture(); +}); + const connectToElectrumClient = async () => { try { ElectrumClient.setActivePeer(predefinedTestnetNodes); @@ -67,6 +72,10 @@ const connectToElectrumClient = async () => { } }; +afterAll(() => { + ElectrumClient.forceDisconnect(); +}); + describe('Vault: Single-Sig(1-of-1)', () => { let primaryMnemonic: string; let vault: Vault; diff --git a/tests/services/wallet.test.ts b/tests/services/wallet.test.ts index dc92142d4f..b366993e46 100644 --- a/tests/services/wallet.test.ts +++ b/tests/services/wallet.test.ts @@ -12,6 +12,7 @@ import { TransactionPrerequisite, } from 'src/services/wallets/interfaces'; import { Wallet } from 'src/services/wallets/interfaces/wallet'; +import { installElectrumWalletFixture } from './electrumWalletFixture'; jest.mock('src/store/store', () => ({ store: { @@ -25,6 +26,10 @@ jest.mock('src/store/store', () => ({ jest.mock('realm', () => ({})); +beforeAll(() => { + installElectrumWalletFixture(); +}); + describe('Wallet Functionality Tests', () => { let primaryMnemonic: string; let wallet: Wallet; From ebda6738c8eedba8ccd07d94c12f9438982cf033 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Mon, 12 Oct 2026 06:24:43 +0000 Subject: [PATCH 115/254] fix(r4): complete fresh wallet setup and isolate faucet refreshes --- src/store/sagas/storage.ts | 106 +++++++-- src/store/sagas/wallets.ts | 21 +- src/store/store.ts | 1 + .../faucetVisibilityAcceptance.test.cjs | 46 ++++ tests/recovery/freshInstallWallet.test.cjs | 220 ++++++++++++++++++ .../recovery/walletRefreshIsolation.test.cjs | 117 ++++++++++ 6 files changed, 488 insertions(+), 23 deletions(-) create mode 100644 tests/recovery/freshInstallWallet.test.cjs create mode 100644 tests/recovery/walletRefreshIsolation.test.cjs diff --git a/src/store/sagas/storage.ts b/src/store/sagas/storage.ts index 99eaa08f39..dad91ea326 100644 --- a/src/store/sagas/storage.ts +++ b/src/store/sagas/storage.ts @@ -10,12 +10,14 @@ import { DerivationPurpose, SignerType, WalletType } from 'src/services/wallets/ import WalletUtilities from 'src/services/wallets/operations/utils'; import crypto from 'crypto'; import dbManager from 'src/storage/realm/dbManager'; +import realm from 'src/storage/realm/realm'; import Relay from 'src/services/backend/Relay'; import config from 'src/utils/service-utilities/config'; import { setupRecoveryKeySigningKey } from 'src/hardware/signerSetup'; import { DelayedPolicyUpdate, DelayedTransaction } from 'src/models/interfaces/AssistedKeys'; import SigningServer from 'src/services/backend/SigningServer'; import { Signer } from 'src/services/wallets/interfaces/vault'; +import { Wallet } from 'src/services/wallets/interfaces/wallet'; import { uaiType } from 'src/models/interfaces/Uai'; import { createWatcher } from '../utilities'; import { @@ -33,7 +35,7 @@ import { import { setAppCreationError } from '../reducers/login'; import { resetRealyWalletState } from '../reducers/bhr'; import { addToUaiStack } from '../sagaActions/uai'; -import { RootState } from '../store'; +import { RootState, store } from '../store'; import { addAccount, setBiometricEnabledAppId, @@ -43,8 +45,14 @@ import { loadConciergeTickets, loadConciergeUser } from '../reducers/concierge'; import LoginMethod from 'src/models/enums/LoginMethod'; export function* setupKeeperAppWorker({ payload }) { + let isCurrentSetupScope = () => true; try { - const { bitcoinNetworkType } = yield select((state: RootState) => state.settings); + const { bitcoinNetworkType, selectedAppId } = yield select((state: RootState) => ({ + bitcoinNetworkType: state.settings.bitcoinNetworkType, + selectedAppId: state.storage.appId, + })); + const sourceRealm = realm.getDatabase(); + let expectedAppId = selectedAppId; const { appName, fcmToken }: { appName: string; fcmToken: string } = payload; let primaryMnemonic; let primarySeed; @@ -52,6 +60,28 @@ export function* setupKeeperAppWorker({ payload }) { let publicId; let imageEncryptionKey; const app: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); + let expectedRealmAppId = app?.id; + isCurrentSetupScope = () => { + try { + const state = store.getState(); + const currentApp = dbManager.getObjectByIndex(RealmSchema.KeeperApp) as unknown as KeeperApp; + return ( + realm.getDatabase() === sourceRealm && + state.storage.appId === expectedAppId && + state.settings.bitcoinNetworkType === bitcoinNetworkType && + currentApp?.id === expectedRealmAppId + ); + } catch { + return false; + } + }; + const assertCurrentSetupScope = () => { + if (!isCurrentSetupScope()) throw new Error('Account changed during app setup'); + }; + const dispatchForSetup = (action) => { + assertCurrentSetupScope(); + store.dispatch(action); + }; if (app) { primaryMnemonic = app.primaryMnemonic; primarySeed = app.primarySeed; @@ -70,7 +100,9 @@ export function* setupKeeperAppWorker({ payload }) { ); imageEncryptionKey = generateEncryptionKey(entropy.toString('hex')); } + assertCurrentSetupScope(); const response = yield call(Relay.createNewApp, publicId, appID, fcmToken); + assertCurrentSetupScope(); if (response && response.created) { const newAPP: KeeperApp = { @@ -91,9 +123,21 @@ export function* setupKeeperAppWorker({ payload }) { networkType: bitcoinNetworkType, enableAnalytics: false, }; - yield call(dbManager.createObject, RealmSchema.KeeperApp, newAPP); - - yield put(addAccount(appID)); + const appPersisted = yield call(() => { + assertCurrentSetupScope(); + return dbManager.createObject(RealmSchema.KeeperApp, newAPP); + }); + if (!appPersisted) throw new Error('Failed to persist new app'); + expectedRealmAppId = appID; + // Scoped wallet/key creation requires Redux and the new Realm to name the + // same account before setup can be marked ready. + yield call(() => { + // Check the original selection again at the publication boundary. + assertCurrentSetupScope(); + expectedAppId = appID; + store.dispatch(setAppId(appID)); + }); + assertCurrentSetupScope(); const defaultWallet: NewWalletInfo = { walletType: WalletType.DEFAULT, @@ -111,28 +155,56 @@ export function* setupKeeperAppWorker({ payload }) { }; const recoveryKeySigner = setupRecoveryKeySigningKey(primaryMnemonic); - yield call(addNewWalletsWorker, { payload: [defaultWallet] }); - yield call(addSigningDeviceWorker, { payload: { signers: [recoveryKeySigner] } }); - yield put( + const existingWallets: Wallet[] = yield call(dbManager.getCollection, RealmSchema.Wallet); + assertCurrentSetupScope(); + // A retry after key setup failed must retain the already-created wallet. + const isDefaultWallet = (wallet: Wallet) => + wallet.type === WalletType.DEFAULT && + wallet.networkType === bitcoinNetworkType && + wallet.derivationDetails.instanceNum === 0; + const hasDefaultWallet = existingWallets.some(isDefaultWallet); + const walletCreated = hasDefaultWallet || (yield call(addNewWalletsWorker, { + originAppId: appID, + payload: [defaultWallet], + })); + assertCurrentSetupScope(); + const createdWallets: Wallet[] = yield call(dbManager.getCollection, RealmSchema.Wallet); + assertCurrentSetupScope(); + if (!walletCreated || !createdWallets.some(isDefaultWallet)) + throw new Error('Failed to create default wallet'); + yield call(addSigningDeviceWorker, { + originAppId: appID, + payload: { signers: [recoveryKeySigner] }, + }); + assertCurrentSetupScope(); + const recoveryKeys: Signer[] = yield call(dbManager.getCollection, RealmSchema.Signer); + assertCurrentSetupScope(); + if (!recoveryKeys.some((signer) => + signer.masterFingerprint === recoveryKeySigner.masterFingerprint && + signer.networkType === recoveryKeySigner.networkType + )) throw new Error('Failed to create Recovery Key'); + yield call(dispatchForSetup, addAccount(appID)); + yield call(dispatchForSetup, updateDefaultWalletCreatedByAppId({ appId: appID, networkType: bitcoinNetworkType }) ); - yield put(setAppId(appID)); - yield put(setAppCreated(true)); - yield put(resetRealyWalletState()); - yield put(loadConciergeUser(null)); - yield put(loadConciergeTickets([])); + yield call(dispatchForSetup, setAppCreated(true)); + yield call(dispatchForSetup, resetRealyWalletState()); + yield call(dispatchForSetup, loadConciergeUser(null)); + yield call(dispatchForSetup, loadConciergeTickets([])); const { loginMethod } = yield select((state: RootState) => state.settings); if (loginMethod === LoginMethod.BIOMETRIC) { const { allAccounts } = yield select((state: RootState) => state.account); - if (allAccounts.length == 1) yield put(setBiometricEnabledAppId(appID)); + if (allAccounts.length == 1) + yield call(dispatchForSetup, setBiometricEnabledAppId(appID)); } } else { - yield put(setAppCreationError(true)); + yield call(dispatchForSetup, setAppCreationError(true)); } } catch (error) { - yield put(setAppCreationError(true)); console.log({ error }); - yield put(setAppCreationError(true)); + yield call(() => { + if (isCurrentSetupScope()) store.dispatch(setAppCreationError(true)); + }); } } diff --git a/src/store/sagas/wallets.ts b/src/store/sagas/wallets.ts index 8a8313ff01..f3cac9a43b 100644 --- a/src/store/sagas/wallets.ts +++ b/src/store/sagas/wallets.ts @@ -240,10 +240,11 @@ export function* addNewWalletsWorker({ payload: NewWalletInfo[]; originAppId?: string; }) { + let sourceAppId = originAppId; try { const wallets: Wallet[] = []; const app: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); - const sourceAppId = originAppId || app?.id; + sourceAppId = originAppId || app?.id; assertWalletAccount(sourceAppId); for (const { walletType, walletDetails, importDetails } of newWalletInfo) { @@ -254,6 +255,7 @@ export function* addNewWalletsWorker({ app, importDetails ); + assertWalletAccount(sourceAppId); wallets.push(wallet); } @@ -277,7 +279,8 @@ export function* addNewWalletsWorker({ } } catch (err) { console.log(err); - yield put(relayWalletUpdateFail(err.message ? err.message : err.toString())); + if (currentWalletAccountId() === sourceAppId && store.getState().storage.appId === sourceAppId) + yield put(relayWalletUpdateFail(err.message ? err.message : err.toString())); return false; } } @@ -710,6 +713,10 @@ function* refreshWalletsWorker({ let { wallets, options, requestId } = payload; let succeeded = false; let lease; + const isSourceAccountCurrent = () => + !!sourceAppId && + currentWalletAccountId() === sourceAppId && + store.getState().storage.appId === sourceAppId; // Filter out pre-mix, post-mix, and bad bank wallets as they are no longer displayed or used. wallets = (wallets || []).filter((wallet) => { @@ -1092,7 +1099,7 @@ function* refreshWalletsWorker({ succeeded = true; return true; } catch (err) { - if (err?.message === WALLET_SYNC_SCOPE_CHANGED) return false; + if (!isSourceAccountCurrent() || err?.message === WALLET_SYNC_SCOPE_CHANGED) return false; if ([ELECTRUM_NOT_CONNECTED_ERR, ELECTRUM_NOT_CONNECTED_ERR_TOR].includes(err?.message)) { yield put( setElectrumNotConnectedErr( @@ -1110,10 +1117,11 @@ function* refreshWalletsWorker({ return false; } finally { if (lease?.owner) { - yield put(setSyncing({ wallets, isSyncing: false })); + if (isSourceAccountCurrent()) yield put(setSyncing({ wallets, isSyncing: false })); lease.complete(succeeded); } - if (requestId) yield put(finishRefreshRequest({ requestId, succeeded })); + if (requestId && isSourceAccountCurrent()) + yield put(finishRefreshRequest({ requestId, succeeded })); } } @@ -1339,7 +1347,7 @@ export const updateSignerPolicyWatcher = createWatcher( UPDATE_SIGNER_POLICY ); -export function* testcoinsWorker({ payload }) { +export function* testcoinsWorker({ payload, originAppId }) { let { wallet } = payload; const faucetScope = yield select((state: RootState) => ({ appId: state.storage.appId, @@ -1350,6 +1358,7 @@ export function* testcoinsWorker({ payload }) { try { const keeperApp = dbManager.getObjectByIndex(RealmSchema.KeeperApp) as unknown as KeeperApp; return ( + (!originAppId || originAppId === faucetScope.appId) && state.storage.appId === faucetScope.appId && state.settings.bitcoinNetworkType === faucetScope.networkType && faucetScope.networkType === wallet.networkType && diff --git a/src/store/store.ts b/src/store/store.ts index f19aa9f6c4..6105bab262 100644 --- a/src/store/store.ts +++ b/src/store/store.ts @@ -91,6 +91,7 @@ const accountOwnedActions = new Set([ 'REFILL_MOBILEKEY', 'MERGER_SIMILAR_KEYS', 'REFRESH_WALLETS', + 'TEST_SATS_RECIEVE', 'AUTO_SYNC_WALLETS', 'REFRESH_CANARY_VAULT', 'UPDATE_WALLET_DETAILS', diff --git a/tests/recovery/faucetVisibilityAcceptance.test.cjs b/tests/recovery/faucetVisibilityAcceptance.test.cjs index bbe8e48d54..fbf68e7de7 100644 --- a/tests/recovery/faucetVisibilityAcceptance.test.cjs +++ b/tests/recovery/faucetVisibilityAcceptance.test.cjs @@ -121,3 +121,49 @@ test('faucet reports pending when the funded txid persists but visible balance s assert.ok(f.actions.some((action) => action.type === 'setTestCoinsPending' && action.payload === 'propagation')); }); + +test('Receive Test Sats dispatch keeps its original account when queued across a switch', () => { + const scope = { Set }; + vm.runInNewContext(ts.transpileModule(functions('src/store/store.ts', [ + 'accountOwnedActions', 'accountOwnedUtxoUiActions', 'bindActionOrigin', + ]), { compilerOptions: { target: ts.ScriptTarget.ES2020 } }).outputText, scope); + let selected = 'A'; + let queued; + const dispatch = scope.bindActionOrigin({ getState: () => ({ storage: { appId: selected } }) })( + (action) => { queued = action; return action; } + ); + dispatch({ type: 'TEST_SATS_RECIEVE', payload: { wallet: { id: 'same-wallet' } } }); + selected = 'B'; + dispatch(queued); + assert.equal(queued.originAppId, 'A'); +}); + +test('a queued A faucet action cannot fund B wallet even when its ID and network match', async () => { + const { f, stored, writes, getCounts } = faucetFixture(() => 1000); + const originAppId = f.app.id; + f.app.id = 'B'; + f.state.storage.appId = 'B'; + await f.run('testcoinsWorker', { originAppId, payload: { wallet: stored } }); + assert.deepEqual(getCounts(), { relayRequests: 0, historyPolls: 0, refreshes: 0 }); + assert.deepEqual(writes, []); + assert.deepEqual(f.actions, []); + assert.equal(stored.specs.balances.unconfirmed, 0); +}); + +test('A faucet response arriving after B login cannot poll, persist or report receipt on B', async () => { + const { f, stored, writes, getCounts } = faucetFixture(() => 1000); + const originAppId = f.app.id; + let finishRequest; + f.scope.Relay.getTestcoins = () => new Promise((resolve) => { finishRequest = resolve; }); + const receive = f.run('testcoinsWorker', { originAppId, payload: { wallet: stored } }); + assert.ok(finishRequest); + f.app.id = 'B'; + f.state.storage.appId = 'B'; + const actionCount = f.actions.length; + finishRequest({ txid, funded: true }); + await receive; + assert.equal(getCounts().historyPolls, 0); + assert.deepEqual(writes, []); + assert.equal(f.actions.length, actionCount); + assert.equal(stored.specs.balances.unconfirmed, 0); +}); diff --git a/tests/recovery/freshInstallWallet.test.cjs b/tests/recovery/freshInstallWallet.test.cjs new file mode 100644 index 0000000000..9e88302b88 --- /dev/null +++ b/tests/recovery/freshInstallWallet.test.cjs @@ -0,0 +1,220 @@ +const { test } = require('node:test'); +const assert = require('node:assert/strict'); +const vm = require('node:vm'); +const ts = require('typescript'); +const { runSaga } = require('redux-saga'); +const { fixture, enums, account, wallet, signer, loadModule, functions } = require('./sagaHarness.cjs'); + +// Disposable adapters only: run the production setup, wallet/key registration, +// faucet and refresh workers without a live relay, Realm file or user seed. +function freshInstallFixture(options = {}) { + const network = options.network || enums.NetworkType.TESTNET; + const f = fixture({ bhr: { automaticCloudBackup: false } }); + f.state.storage.appId = ''; + f.state.settings.bitcoinNetworkType = network; + f.state.settings.loginMethod = 'PIN'; + let activeApp; + let activeRealm = {}; + let rejectKeyWrite = options.rejectKeyWrite; + let generationCount = 0; + let faucetRequests = 0; + const writes = []; + const txid = 'c'.repeat(64); + const originalGet = f.scope.dbManager.getObjectByIndex; + const originalCreate = f.scope.dbManager.createObject; + const originalBulkCreate = f.scope.dbManager.createObjectBulk; + const originalGetCollection = f.scope.dbManager.getCollection; + f.scope.realm = { getDatabase: () => activeRealm }; + f.scope.dbManager.getObjectByIndex = (schema, ...args) => + schema === 'KeeperApp' ? activeApp : originalGet(schema, ...args); + f.scope.dbManager.createObject = (schema, value, ...args) => { + writes.push({ schema, owner: f.state.storage.appId }); + if (schema !== 'KeeperApp') return originalCreate(schema, value, ...args); + if (options.rejectAppWrite) return false; + activeApp = { ...value }; + options.afterAppWrite?.(f); + return true; + }; + f.scope.dbManager.createObjectBulk = (schema, values, ...args) => { + writes.push({ schema, owner: f.state.storage.appId }); + if ((schema === 'Wallet' && options.rejectWalletWrite) || + (schema === 'Signer' && rejectKeyWrite)) return false; + return originalBulkCreate(schema, values, ...args); + }; + f.scope.dbManager.getCollection = (schema) => { + const values = originalGetCollection(schema); + if (schema === 'Signer' && values.length) options.afterRecoveryKeyRead?.(f); + return values; + }; + f.scope.bip39.generateMnemonic = () => 'disposable setup fixture'; + f.scope.WalletUtilities.getDerivationPath = () => "m/84'/1'/0'"; + f.scope.LoginMethod = { BIOMETRIC: 'BIOMETRIC' }; + f.scope.Relay.createNewApp = async () => { + options.onRelay?.(f); + return { created: true }; + }; + f.scope.addNewWallet = async () => { + generationCount += 1; + options.onWalletGeneration?.(f); + return { + ...wallet('disposable-mobile', network), + specs: { + receivingAddress: 'tb1qdisposable', + balances: { confirmed: 0, unconfirmed: 0 }, + confirmedUTXOs: [], unconfirmedUTXOs: [], transactions: [], + addresses: { external: {}, internal: {} }, + }, + }; + }; + f.scope.setupRecoveryKeySigningKey = () => ({ + ...signer('RECOVERY', network, enums.SignerType.SEED_WORDS), hidden: true, + }); + f.scope.setAppCreationError = (payload) => ({ type: 'setAppCreationError', payload }); + f.scope.resetRealyWalletState = () => ({ type: 'resetRealyWalletState' }); + f.scope.getJSONFromRealmObject = (value) => JSON.parse(JSON.stringify(value)); + f.scope.WALLET_SYNC_SCOPE_CHANGED = 'WALLET_SYNC_SCOPE_CHANGED'; + f.scope.ELECTRUM_CLIENT = { isClientConnected: true }; + f.scope.ElectrumClient = { + getConnectionGeneration: () => 1, + assertConnectionGeneration: (_generation, currentNetwork) => assert.equal(currentNetwork, network), + syncHistoryByAddress: async () => ({ txids: [txid] }), + }; + f.scope.WalletUtilities.getNetworkByType = () => ({}); + f.scope.WalletOperations = { + getNextFreeAddress: (w) => w.specs.receivingAddress, + syncWalletsViaElectrumClient: async (wallets) => { + const synced = f.scope.getJSONFromRealmObject(wallets[0]); + synced.specs.unconfirmedUTXOs = [{ txId: txid, vout: 0, value: 1000 }]; + synced.specs.balances = { confirmed: 0, unconfirmed: 1000 }; + synced.specs.hasNewUpdates = true; + return { synchedWallets: [{ synchedWallet: synced, newUTXOs: [] }] }; + }, + }; + f.scope.Relay.getTestcoins = async (_address, requestedNetwork, appId) => { + faucetRequests += 1; + assert.equal(requestedNetwork, enums.NetworkType.TESTNET); + assert.equal(appId, activeApp.id); + return { txid, funded: true }; + }; + f.scope.isFaucetTxVisible = loadModule('src/store/sagas/faucetVisibility.ts').isFaucetTxVisible; + f.scope.classifyDustByAddress = () => ({ + taintedAddresses: new Set(), initialTaintAddresses: new Set(), dustSpendTxids: new Set(), + }); + for (const name of ['setTestCoinsReceived', 'setTestCoinsFailed', 'setTestCoinsPending', + 'setTestCoinsQuotaReached', 'setSyncing', 'setElectrumNotConnectedErr']) { + f.scope[name] = (payload) => ({ type: name, payload }); + } + f.scope.ELECTRUM_NOT_CONNECTED_ERR = 'offline'; + f.scope.ELECTRUM_NOT_CONNECTED_ERR_TOR = 'tor-offline'; + f.scope.delay = () => Promise.resolve(); + const dispatch = (action) => { + f.actions.push(action); + if (action.type === 'setAppId') f.state.storage.appId = action.payload; + if (action.type === 'setAppCreated') f.state.storage.appCreated = action.payload; + f.state.account = account.default(f.state.account, action); + options.onDispatch?.(f, action); + return action; + }; + f.scope.store = { getState: () => f.state, dispatch }; + vm.runInNewContext(ts.transpileModule([ + functions('src/store/sagas/storage.ts', ['setupKeeperAppWorker']), + functions('src/store/sagas/wallets.ts', ['refreshWalletsWorker', 'testcoinsWorker']), + ].join('\n'), { compilerOptions: { target: ts.ScriptTarget.ES2020 } }).outputText, f.scope); + f.run = (name, ...args) => runSaga({ getState: () => f.state, dispatch }, + f.scope[name], ...args).toPromise(); + f.setup = () => f.run('setupKeeperAppWorker', { payload: { appName: 'Disposable', fcmToken: '' } }); + f.switchToB = () => { + activeRealm = {}; + activeApp = { id: 'B' }; + f.state.storage.appId = 'B'; + f.collections.Wallet = [wallet('disposable-mobile', network)]; + f.collections.Signer = []; + f.state.account = { ...f.state.account, allAccounts: [{ appId: 'B' }] }; + f.switchActionIndex = f.actions.length; + f.switchWriteIndex = writes.length; + }; + f.allowKeyWrites = () => { rejectKeyWrite = false; }; + f.counts = () => ({ generationCount, faucetRequests }); + f.writes = writes; + return f; +} + +for (const network of [enums.NetworkType.MAINNET, enums.NetworkType.TESTNET]) { + test(`fresh ${network} setup persists one default wallet and Recovery Key before readiness`, async () => { + const f = freshInstallFixture({ network }); + await f.setup(); + assert.equal(f.collections.Wallet.length, 1); + assert.equal(f.collections.Signer.length, 1); + assert.equal(f.collections.Signer[0].networkType, network); + assert.equal(f.state.storage.appCreated, true); + assert.equal(f.state.account.defaultWalletCreatedByAppId[f.state.storage.appId][network], true); + assert.equal(f.actions.some((action) => action.type === 'setAppCreationError'), false); + assert.equal(f.calls.length, 0, 'fresh setup must keep automatic backup off'); + }); +} + +test('fresh Testnet setup -> one Receive Test Sats request persists a visible balance in the same session', async () => { + const f = freshInstallFixture(); + await f.setup(); + const freshWallet = f.collections.Wallet[0]; + assert.ok(freshWallet, 'setup must produce the wallet before opening its settings'); + await f.run('testcoinsWorker', { + originAppId: f.state.storage.appId, payload: { wallet: freshWallet }, + }); + assert.deepEqual(f.counts(), { generationCount: 1, faucetRequests: 1 }); + assert.equal(f.collections.Wallet.length, 1); + assert.equal(freshWallet.specs.balances.confirmed + freshWallet.specs.balances.unconfirmed, 1000); + assert.ok(f.actions.some((action) => action.type === 'setTestCoinsReceived' && action.payload)); +}); + +for (const failure of ['rejectAppWrite', 'rejectWalletWrite', 'rejectKeyWrite']) { + test(`failed ${failure} never marks fresh setup ready`, async () => { + const f = freshInstallFixture({ [failure]: true }); + await f.setup(); + assert.notEqual(f.state.storage.appCreated, true); + assert.equal(f.state.account.allAccounts.length, 0); + assert.deepEqual(Object.keys(f.state.account.defaultWalletCreatedByAppId), []); + assert.ok(f.actions.some((action) => action.type === 'setAppCreationError' && action.payload)); + }); +} + +test('retry after key persistence failure preserves the one already-funded default wallet', async () => { + const f = freshInstallFixture({ rejectKeyWrite: true }); + await f.setup(); + assert.equal(f.collections.Wallet.length, 1); + const retained = f.collections.Wallet[0]; + retained.specs.balances.unconfirmed = 321; + f.allowKeyWrites(); + await f.setup(); + assert.equal(f.state.storage.appCreated, true); + assert.equal(f.collections.Wallet.length, 1); + assert.equal(f.collections.Wallet[0], retained); + assert.equal(retained.specs.balances.unconfirmed, 321); + assert.equal(f.counts().generationCount, 1); +}); + +for (const boundary of ['onRelay', 'afterAppWrite', 'onWalletGeneration', 'afterRecoveryKeyRead']) { + test(`account switch at ${boundary} cannot publish A setup or mutate B`, async () => { + const f = freshInstallFixture({ [boundary]: (current) => current.switchToB() }); + await f.setup(); + assert.equal(f.state.storage.appId, 'B'); + assert.notEqual(f.state.storage.appCreated, true); + assert.deepEqual(f.state.account.allAccounts, [{ appId: 'B' }]); + assert.deepEqual(Object.keys(f.state.account.defaultWalletCreatedByAppId), []); + assert.equal(f.collections.Signer.length, 0); + assert.equal(f.collections.Wallet[0].specs.balances.confirmed, 10000); + assert.deepEqual(f.writes.slice(f.switchWriteIndex), []); + assert.deepEqual(f.actions.slice(f.switchActionIndex), [], 'no stale setup errors or readiness UI on B'); + }); +} + +test('switch triggered by account registration cannot mark B default wallet or app ready', async () => { + const f = freshInstallFixture({ onDispatch: (current, action) => { + if (action.type === 'account/addAccount') current.switchToB(); + } }); + await f.setup(); + assert.equal(f.state.storage.appId, 'B'); + assert.notEqual(f.state.storage.appCreated, true); + assert.deepEqual(Object.keys(f.state.account.defaultWalletCreatedByAppId), []); + assert.deepEqual(f.actions.slice(f.switchActionIndex), []); +}); diff --git a/tests/recovery/walletRefreshIsolation.test.cjs b/tests/recovery/walletRefreshIsolation.test.cjs new file mode 100644 index 0000000000..3126d2e23e --- /dev/null +++ b/tests/recovery/walletRefreshIsolation.test.cjs @@ -0,0 +1,117 @@ +const { test } = require('node:test'); +const assert = require('node:assert/strict'); +const vm = require('node:vm'); +const ts = require('typescript'); +const { fixture, enums, wallet, loadModule, functions } = require('./sagaHarness.cjs'); +const walletState = loadModule('src/store/reducers/wallets.ts', { + 'src/storage': { reduxStorage: {} }, + '../sagaActions/wallets': { ADD_NEW_WALLETS: 'ADD_NEW_WALLETS' }, + 'redux-persist/es/persistReducer': { __esModule: true, default: (_settings, reducer) => reducer }, +}); + +function refreshFixture(kind, archived = false) { + const f = fixture(); + f.state.settings.bitcoinNetworkType = enums.NetworkType.TESTNET; + const stored = { + ...wallet('same-disposable-id', enums.NetworkType.TESTNET), + entityKind: kind === 'Vault' ? enums.EntityKind.VAULT : enums.EntityKind.WALLET, + archived, + presentationData: { visibility: archived ? 'HIDDEN' : 'DEFAULT' }, + specs: { + balances: { confirmed: 0, unconfirmed: 0 }, + confirmedUTXOs: [], unconfirmedUTXOs: [], transactions: [], + addresses: { external: {}, internal: {} }, + }, + }; + f.collections[kind].push(stored); + f.scope.getJSONFromRealmObject = (value) => JSON.parse(JSON.stringify(value)); + f.scope.WALLET_SYNC_SCOPE_CHANGED = 'WALLET_SYNC_SCOPE_CHANGED'; + f.scope.ELECTRUM_CLIENT = { isClientConnected: true }; + f.scope.ElectrumClient = { + getConnectionGeneration: () => 1, + assertConnectionGeneration: () => {}, + }; + f.scope.WalletUtilities.getNetworkByType = () => ({}); + f.scope.classifyDustByAddress = () => ({ + taintedAddresses: new Set(), initialTaintAddresses: new Set(), dustSpendTxids: new Set(), + }); + f.scope.setSyncing = walletState.setSyncing; + f.scope.ELECTRUM_NOT_CONNECTED_ERR = 'offline'; + f.scope.ELECTRUM_NOT_CONNECTED_ERR_TOR = 'tor-offline'; + let finishSync; + f.scope.WalletOperations = { syncWalletsViaElectrumClient: (wallets) => { + const synced = f.scope.getJSONFromRealmObject(wallets[0]); + synced.specs.balances.unconfirmed = 1000; + synced.specs.hasNewUpdates = true; + return new Promise((resolve) => { + finishSync = () => resolve({ synchedWallets: [{ synchedWallet: synced, newUTXOs: [] }] }); + }); + } }; + const originalWrite = f.scope.dbManager.updateObjectById; + f.writes = []; + f.scope.dbManager.updateObjectById = (...args) => { + f.writes.push(args); + return originalWrite(...args); + }; + vm.runInNewContext(ts.transpileModule(functions('src/store/sagas/wallets.ts', [ + 'refreshWalletsWorker', + ]), { compilerOptions: { target: ts.ScriptTarget.ES2020 } }).outputText, f.scope); + f.refresh = (originAppId = f.app.id) => f.run('refreshWalletsWorker', { + originAppId, + payload: { wallets: [stored], options: { hardRefresh: true } }, + }); + f.waitForSync = async () => { + for (let attempt = 0; attempt < 20 && !finishSync; attempt += 1) + await new Promise(setImmediate); + assert.ok(finishSync, 'refresh must reach the pending Electrum boundary'); + }; + f.complete = () => finishSync(); + return f; +} + +for (const [kind, archived] of [['Wallet', false], ['Vault', false], ['Vault', true]]) { + const description = archived ? 'hidden archived Vault' : kind; + test(`${description}: pending A refresh cannot overwrite B with the same ID`, async () => { + const f = refreshFixture(kind, archived); + const refreshing = f.refresh(); + await f.waitForSync(); + f.app.id = 'B'; + f.state.storage.appId = 'B'; + const b = { ...wallet('same-disposable-id', enums.NetworkType.TESTNET), + entityKind: kind === 'Vault' ? enums.EntityKind.VAULT : enums.EntityKind.WALLET, + specs: { balances: { confirmed: 42, unconfirmed: 0 } } }; + f.collections[kind] = [b]; + const actionCount = f.actions.length; + let bUi = walletState.default(undefined, walletState.setSyncing({ wallets: [b], isSyncing: true })); + f.complete(); + assert.equal(await refreshing, false); + for (const action of f.actions.slice(actionCount)) bUi = walletState.default(bUi, action); + assert.deepEqual(f.writes, []); + assert.equal(b.specs.balances.confirmed, 42); + assert.equal(b.specs.balances.unconfirmed, 0); + assert.equal(bUi.walletSyncing[b.id], true, 'A completion must not clear B pending refresh'); + }); + + test(`${description}: pending Testnet refresh cannot persist after a Mainnet switch`, async () => { + const f = refreshFixture(kind, archived); + const refreshing = f.refresh(); + await f.waitForSync(); + f.state.settings.bitcoinNetworkType = enums.NetworkType.MAINNET; + f.complete(); + assert.equal(await refreshing, false); + assert.deepEqual(f.writes, []); + assert.equal(f.collections[kind][0].specs.balances.unconfirmed, 0); + }); + + test(`${description}: queued A refresh is rejected before syncing B`, async () => { + const f = refreshFixture(kind, archived); + const originAppId = f.app.id; + f.app.id = 'B'; + f.state.storage.appId = 'B'; + let synced = false; + f.scope.WalletOperations.syncWalletsViaElectrumClient = () => { synced = true; }; + assert.equal(await f.refresh(originAppId), false); + assert.equal(synced, false); + assert.deepEqual(f.writes, []); + }); +} From 29192d30faf0491317dca9916ab861ba891c345a Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Mon, 12 Oct 2026 06:26:49 +0000 Subject: [PATCH 116/254] fix: await collaborative modal hide and session readiness --- src/components/KeeperModal.tsx | 31 +- src/hooks/useCollaborativeWalletHandoff.ts | 136 +++++++++ .../Home/components/Wallet/HomeWallet.tsx | 73 ++--- .../screens/HomeWalletCollaborative.test.tsx | 271 ++++++++++++++++++ 4 files changed, 467 insertions(+), 44 deletions(-) create mode 100644 src/hooks/useCollaborativeWalletHandoff.ts create mode 100644 tests/screens/HomeWalletCollaborative.test.tsx diff --git a/src/components/KeeperModal.tsx b/src/components/KeeperModal.tsx index 8f5105c2a8..6aea64e919 100644 --- a/src/components/KeeperModal.tsx +++ b/src/components/KeeperModal.tsx @@ -2,7 +2,7 @@ import { Box, Modal, useColorMode } from '@gluestack-ui/themed-native-base'; import { ScrollView, StyleSheet, TouchableOpacity, useWindowDimensions } from 'react-native'; import { hp, windowWidth, wp } from 'src/constants/responsive'; import CloseGreen from 'src/assets/images/dark-close-icon.svg'; -import React from 'react'; +import React, { useEffect, useLayoutEffect, useRef, useState } from 'react'; import { useSafeAreaInsets } from 'react-native-safe-area-context'; import Text from 'src/components/KeeperText'; import { useKeyboard } from 'src/hooks/useKeyboard'; @@ -17,6 +17,8 @@ import { default as RNModal } from 'react-native-modal'; type ModalProps = { visible: boolean; close: any; + // eslint-disable-next-line react/require-default-props -- Omitted lifecycle callback has no side effect. + onModalHide?: () => void; title?: string; subTitle?: string; subTitleWidth?: number; @@ -52,6 +54,7 @@ function KeeperModal(props: ModalProps) { const { visible, close, + onModalHide, title = '', subTitle = null, subTitleWidth = windowWidth * 0.7, @@ -87,8 +90,23 @@ function KeeperModal(props: ModalProps) { const maxModalHeight = Math.min(availableHeight, screenHeight * 0.85); const { colorMode } = useColorMode(); const isDarKMode = colorMode === 'dark'; + const [keepMountedForHide, setKeepMountedForHide] = useState(visible); + const hidePending = useRef(false); - if (!visible) { + useLayoutEffect(() => { + if (visible) { + hidePending.current = true; + setKeepMountedForHide(true); + } + }, [visible]); + + useEffect(() => () => { + hidePending.current = false; + }, []); + + // Existing callers still unmount immediately. Opted-in callers need RNModal to + // receive isVisible=false and finish its native close before the next screen. + if (!visible && (!onModalHide || !keepMountedForHide)) { return null; } const getCloseIcon = () => (DarkCloseIcon ? : ); @@ -98,10 +116,19 @@ function KeeperModal(props: ModalProps) { { + if (visible || !hidePending.current) return; + hidePending.current = false; + setKeepMountedForHide(false); + onModalHide?.(); + }} style={styles.modalContainer} avoidKeyboard > ; + isComplete: boolean; + lastSynced: number | null; +}; +type Scope = { appId: string; network: string }; +type Pending = Scope & { + stage: 'add-hide' | 'session-hide' | 'reset'; + action?: 'continue' | 'new'; +}; + +/** Only hand off after the native modal has hidden, within the originating account/network. */ +export function useCollaborativeWalletHandoff({ + appId, + network, + session, + resetSession, + navigate, +}: Scope & { session: Session; resetSession: () => void; navigate: () => void }) { + const [addVisible, setAddVisible] = useState(false); + const [sessionVisible, setSessionVisible] = useState(false); + const [stage, setStage] = useState(null); + const scope = useRef(null); + const latestSession = useRef(session); + const pending = useRef(null); + + useLayoutEffect(() => { + scope.current = { appId, network }; + pending.current = null; + setStage(null); + setAddVisible(false); + setSessionVisible(false); + return () => { + scope.current = null; + pending.current = null; + }; + }, [appId, network]); + + useLayoutEffect(() => { + latestSession.current = session; + }, [session]); + + const isCurrent = useCallback( + (request: Pending) => + !!scope.current?.appId && + scope.current.appId === request.appId && + scope.current.network === request.network, + [] + ); + + const beginReset = useCallback( + (request: Pending) => { + if (!isCurrent(request)) return; + pending.current = { ...request, stage: 'reset' }; + setStage('reset'); + resetSession(); + }, + [isCurrent, resetSession] + ); + + useEffect(() => { + const request = pending.current; + if ( + stage === 'reset' && + request?.stage === 'reset' && + isCurrent(request) && + Object.keys(session.signers).length === 0 && + !session.isComplete && + session.lastSynced === null + ) { + pending.current = null; + setStage(null); + navigate(); + } + }, [stage, isCurrent, navigate, session]); + + const cancel = useCallback(() => { + pending.current = null; + setStage(null); + }, []); + + return { + addVisible, + sessionVisible, + waitForAddHide: stage === 'add-hide', + waitForSessionHide: stage === 'session-hide', + openAdd: () => { + if (!pending.current && !sessionVisible) setAddVisible(true); + }, + closeAdd: () => { + cancel(); + setAddVisible(false); + }, + closeSession: () => { + cancel(); + setSessionVisible(false); + }, + selectCollaborative: () => { + if (!addVisible || pending.current || !scope.current?.appId) return; + pending.current = { ...scope.current, stage: 'add-hide' }; + setStage('add-hide'); + setAddVisible(false); + }, + onAddHide: () => { + const request = pending.current; + if (request?.stage !== 'add-hide' || !isCurrent(request)) return; + // Consume the hide once before opening another modal or dispatching a reset. + pending.current = null; + setStage(null); + if (Object.keys(latestSession.current.signers).length > 0) { + setSessionVisible(true); + } else { + beginReset(request); + } + }, + finishSession: (action: 'continue' | 'new') => { + if (!sessionVisible || pending.current || !scope.current?.appId) return; + pending.current = { ...scope.current, stage: 'session-hide', action }; + setStage('session-hide'); + setSessionVisible(false); + }, + onSessionHide: () => { + const request = pending.current; + if (request?.stage !== 'session-hide' || !isCurrent(request)) return; + pending.current = null; + setStage(null); + if (request.action === 'continue') { + if (Object.keys(latestSession.current.signers).length > 0) navigate(); + } else { + beginReset(request); + } + }, + }; +} diff --git a/src/screens/Home/components/Wallet/HomeWallet.tsx b/src/screens/Home/components/Wallet/HomeWallet.tsx index 91878b9414..4bcb0469ad 100644 --- a/src/screens/Home/components/Wallet/HomeWallet.tsx +++ b/src/screens/Home/components/Wallet/HomeWallet.tsx @@ -1,7 +1,6 @@ import { Box, useColorMode, View } from '@gluestack-ui/themed-native-base'; -import React, { useContext, useState, useEffect } from 'react'; +import React, { useCallback, useContext, useState, useEffect } from 'react'; import { FlatList, StyleSheet, TouchableOpacity } from 'react-native'; -import WalletCard from './WalletCard'; import Colors from 'src/theme/Colors'; import useWallets from 'src/hooks/useWallets'; import useVault from 'src/hooks/useVault'; @@ -38,6 +37,8 @@ import Fab from 'src/components/Fab'; import AddIcon from 'src/assets/images/add_white.svg'; import { useUTXOSpendability } from 'src/hooks/useUTXOSpendability'; import { clearDustToast } from 'src/store/reducers/utxos'; +import { useCollaborativeWalletHandoff } from 'src/hooks/useCollaborativeWalletHandoff'; +import WalletCard from './WalletCard'; function WalletCardItem({ item, @@ -108,11 +109,22 @@ const HomeWallet = () => { }); const { usdtWallets } = useUSDTWallets(); const { collaborativeSession } = useAppSelector((state) => state.vault); + const { appId } = useAppSelector((state) => state.storage); + const { bitcoinNetworkType } = useAppSelector((state) => state.settings); const dispatch = useDispatch(); - const [showAddWalletModal, setShowAddWalletModal] = useState(false); - const [collabSessionExistsModalVisible, setCollabSessionExistsModalVisible] = useState(false); - const [navigateAfterSessionReset, setNavigateAfterSessionReset] = useState(false); + const resetSession = useCallback(() => dispatch(resetCollaborativeSession()), [dispatch]); + const navigateToCollaborativeWallet = useCallback( + () => navigation.navigate('SetupCollaborativeWallet'), + [navigation] + ); + const collaborativeHandoff = useCollaborativeWalletHandoff({ + appId, + network: bitcoinNetworkType, + session: collaborativeSession, + resetSession, + navigate: navigateToCollaborativeWallet, + }); const { refreshing: pullRefresh, autoRefresh } = useWalletRefresh(); const { walletSyncing } = useAppSelector((state) => state.wallet); const syncing = @@ -139,27 +151,6 @@ const HomeWallet = () => { } }, [pendingDustToast]); - useEffect(() => { - if (navigateAfterSessionReset && Object.keys(collaborativeSession.signers).length === 0) { - setNavigateAfterSessionReset(false); - navigation.navigate('SetupCollaborativeWallet'); - } - }, [navigateAfterSessionReset, collaborativeSession.signers, navigation]); - - const resetAndStartCollaborativeWallet = () => { - dispatch(resetCollaborativeSession()); - setNavigateAfterSessionReset(true); - }; - - const handleCollaborativeWalletCreation = () => { - setShowAddWalletModal(false); - if (Object.keys(collaborativeSession.signers).length > 0) { - setCollabSessionExistsModalVisible(true); - } else { - resetAndStartCollaborativeWallet(); - } - }; - const pullDownRefresh = () => { autoRefresh(); }; @@ -170,7 +161,7 @@ const HomeWallet = () => { subtitle: walletText.createWalletDesc, icon: , onPress: () => { - setShowAddWalletModal(false); + collaborativeHandoff.closeAdd(); navigation.navigate('AddNewWallet'); }, id: 'newWallet', @@ -180,7 +171,7 @@ const HomeWallet = () => { subtitle: walletText.restoreExistingWallet, icon: , onPress: () => { - setShowAddWalletModal(false); + collaborativeHandoff.closeAdd(); navigation.navigate('VaultConfigurationCreation'); }, id: 'importWallet', @@ -189,7 +180,7 @@ const HomeWallet = () => { title: common.collaborativeWallet, subtitle: walletText.walletWithFamily, icon: , - onPress: handleCollaborativeWalletCreation, + onPress: collaborativeHandoff.selectCollaborative, id: 'collaborativeWallet', }, ]; @@ -209,7 +200,7 @@ const HomeWallet = () => { } - onPress={() => setShowAddWalletModal(true)} + onPress={collaborativeHandoff.openAdd} /> { ItemSeparatorComponent={() => } /> setShowAddWalletModal(false)} + close={collaborativeHandoff.closeAdd} + onModalHide={collaborativeHandoff.waitForAddHide ? collaborativeHandoff.onAddHide : undefined} textColor={`${colorMode}.textGreen`} subTitleColor={`${colorMode}.modalSubtitleBlack`} showCloseIcon @@ -236,20 +228,17 @@ const HomeWallet = () => { )} /> setCollabSessionExistsModalVisible(false)} + visible={collaborativeHandoff.sessionVisible} + close={collaborativeHandoff.closeSession} + onModalHide={ + collaborativeHandoff.waitForSessionHide ? collaborativeHandoff.onSessionHide : undefined + } title={walletText.collaborativeSessionExists} subTitle={walletText.collaborativeSessionExistsDesc} buttonText={common.continueSession} secondaryButtonText={common.startNew} - secondaryCallback={() => { - setCollabSessionExistsModalVisible(false); - resetAndStartCollaborativeWallet(); - }} - buttonCallback={() => { - setCollabSessionExistsModalVisible(false); - navigation.navigate('SetupCollaborativeWallet'); - }} + secondaryCallback={() => collaborativeHandoff.finishSession('new')} + buttonCallback={() => collaborativeHandoff.finishSession('continue')} /> ); diff --git a/tests/screens/HomeWalletCollaborative.test.tsx b/tests/screens/HomeWalletCollaborative.test.tsx new file mode 100644 index 0000000000..42ef0b7558 --- /dev/null +++ b/tests/screens/HomeWalletCollaborative.test.tsx @@ -0,0 +1,271 @@ +import React from 'react'; +import { act, create, ReactTestRenderer } from 'react-test-renderer'; +import { Dimensions, TouchableOpacity, Text } from 'react-native'; +import HomeWallet from 'src/screens/Home/components/Wallet/HomeWallet'; +import KeeperModal from 'src/components/KeeperModal'; + +const mockNavigate = jest.fn(); +const mockNavigation = { navigate: mockNavigate }; +const mockDispatch = jest.fn(); +const emptySession = () => ({ signers: {}, isComplete: false, lastSynced: null }); +const existingSession = () => ({ + signers: { disposable: { keyDescriptor: 'fixture', keyAES: 'fixture' } }, + isComplete: true, + lastSynced: 123, +}); +let mockState: any; + +jest.mock('@react-navigation/native', () => ({ useNavigation: () => mockNavigation })); +jest.mock('react-redux', () => ({ useDispatch: () => mockDispatch })); +jest.mock('src/store/hooks', () => ({ useAppSelector: (selector) => selector(mockState) })); +jest.mock('src/store/reducers/vaults', () => ({ + resetCollaborativeSession: () => ({ type: 'vault/resetCollaborativeSession' }), +})); +jest.mock('src/store/reducers/utxos', () => ({ clearDustToast: jest.fn() })); +jest.mock('src/hooks/useWallets', () => () => ({ wallets: [] })); +jest.mock('src/hooks/useVault', () => () => ({ allVaults: [] })); +jest.mock('src/hooks/useUSDTWallets', () => ({ useUSDTWallets: () => ({ usdtWallets: [] }) })); +jest.mock('src/hooks/useWalletAsset', () => () => ({ + getWalletCardGradient: jest.fn(), + getWalletTags: jest.fn(), +})); +jest.mock('src/hooks/useWalletRefresh', () => ({ + useWalletRefresh: () => ({ refreshing: false, autoRefresh: jest.fn() }), +})); +jest.mock('src/hooks/useUTXOSpendability', () => ({ useUTXOSpendability: () => ({}) })); +jest.mock('src/hooks/useToastMessage', () => () => ({ showToast: jest.fn() })); +jest.mock('src/services/electrum/client', () => ({ ELECTRUM_CLIENT: {} })); +jest.mock('src/services/wallets/factories/USDTWalletFactory', () => ({})); +jest.mock('src/screens/Home/components/Wallet/WalletCard', () => () => null); +jest.mock('src/components/AppActivityIndicator/ActivityIndicatorView', () => () => null); +jest.mock('src/components/CircleIconWrapper', () => () => null); +jest.mock('src/components/Fab', () => (props) => require('react').createElement('Fab', props)); +jest.mock('src/components/KeeperText', () => require('react-native').Text); +jest.mock('src/components/Buttons', () => (props) => require('react').createElement('Buttons', props)); +jest.mock('src/components/Switch/CurrencyTypeSwitch', () => () => null); +jest.mock('src/components/ThemedSvg.tsx/ThemedSvg', () => () => null); +jest.mock('src/hooks/useKeyboard', () => ({ useKeyboard: () => false })); +jest.mock('src/constants/responsive', () => ({ hp: (n) => n, wp: (n) => n, windowWidth: 414 })); +jest.mock('react-native-safe-area-context', () => ({ useSafeAreaInsets: () => ({ bottom: 0 }) })); +jest.mock('react-native-modal', () => (props) => + require('react').createElement('NativeModal', props, props.children) +); +jest.mock('@gluestack-ui/themed-native-base', () => ({ + Box: require('react-native').View, + View: require('react-native').View, + Modal: { Header: require('react-native').View, Body: require('react-native').View }, + useColorMode: () => ({ colorMode: 'light' }), +})); +jest.mock('src/context/Localization/LocContext', () => ({ + LocalizationContext: require('react').createContext({ + translations: { + wallet: { + addNewWallet: 'Add wallet', + collaborativeSessionExists: 'Existing session', + createWallet: 'New wallet', + }, + home: { ImportWallet: 'Import wallet' }, + common: { collaborativeWallet: 'Collaborative', continueSession: 'Continue', startNew: 'New' }, + }, + }), +})); +jest.mock('src/assets/images/wallet-white-small.svg', () => () => null); +jest.mock('src/assets/images/import.svg', () => () => null); +jest.mock('src/assets/images/collaborative_vault_white.svg', () => () => null); +jest.mock('src/assets/images/add_white.svg', () => () => null); +jest.mock('src/assets/images/dark-close-icon.svg', () => () => null); +jest.mock('src/assets/images/info_icon.svg', () => () => null); +jest.mock('src/assets/images/info-Dark-icon.svg', () => () => null); + +let screen: ReactTestRenderer; +const addModal = () => screen.root.findAllByType(KeeperModal)[0]; +const sessionModal = () => screen.root.findAllByType(KeeperModal)[1]; +const update = () => act(() => screen.update()); + +function mount(session = emptySession()) { + mockState.vault.collaborativeSession = session; + act(() => { + screen = create(); + }); +} + +function selectCollaborative() { + act(() => screen.root.findByType('Fab' as any).props.onPress()); + const option = addModal().findAllByType(TouchableOpacity).find((press) => + press.findAllByType(Text).some((text) => text.props.children === 'Collaborative') + ); + act(() => option!.props.onPress()); +} + +function hide(modal: ReturnType) { + const onHide = modal.findByType('NativeModal' as any).props.onModalHide; + act(() => onHide()); + return onHide; +} + +beforeEach(() => { + jest.useFakeTimers(); + // The repository-wide Dimensions mock does not return a removable subscription. + jest.spyOn(Dimensions, 'addEventListener').mockReturnValue({ + remove: jest.fn(), + } as ReturnType); + mockNavigate.mockClear(); + mockDispatch.mockClear(); + mockState = { + storage: { appId: 'disposable-account-a' }, + settings: { bitcoinNetworkType: 'TESTNET' }, + vault: { collaborativeSession: emptySession() }, + wallet: { walletSyncing: {} }, + utxos: {}, + }; +}); +afterEach(() => { + act(() => screen?.unmount()); + jest.clearAllTimers(); + jest.useRealTimers(); +}); + +describe('Home collaborative wallet native modal handoff', () => { + test.each([0, 60000])('waits for actual add-modal hide after %i ms, then navigates once', (delay) => { + mount(); + selectCollaborative(); + expect(addModal().props.visible).toBe(false); + expect(addModal().findByType('NativeModal' as any).props.isVisible).toBe(false); + act(() => jest.advanceTimersByTime(delay)); + expect(mockDispatch).not.toHaveBeenCalled(); + expect(mockNavigate).not.toHaveBeenCalled(); + expect(sessionModal().props.visible).toBe(false); + const duplicateHide = hide(addModal()); + act(() => duplicateHide()); + expect(mockDispatch).toHaveBeenCalledTimes(1); + expect(mockNavigate).toHaveBeenCalledTimes(1); + expect(mockNavigate).toHaveBeenCalledWith('SetupCollaborativeWallet'); + }); + + test('opens existing-session choices only after add-modal hide and continues after the second hide', () => { + mount(existingSession()); + selectCollaborative(); + expect(sessionModal().props.visible).toBe(false); + const duplicateAddHide = hide(addModal()); + expect(sessionModal().props.visible).toBe(true); + expect(addModal().findAllByType('NativeModal' as any)).toHaveLength(0); + act(() => duplicateAddHide()); + act(() => sessionModal().props.buttonCallback()); + act(() => jest.advanceTimersByTime(60000)); + expect(mockNavigate).not.toHaveBeenCalled(); + expect(mockDispatch).not.toHaveBeenCalled(); + const duplicateSessionHide = hide(sessionModal()); + act(() => duplicateSessionHide()); + expect(mockNavigate).toHaveBeenCalledTimes(1); + expect(mockState.vault.collaborativeSession.signers).toHaveProperty('disposable'); + }); + + test('new session waits for both native hide and fully observed reset readiness', () => { + mount(existingSession()); + selectCollaborative(); + hide(addModal()); + act(() => sessionModal().props.secondaryCallback()); + expect(mockDispatch).not.toHaveBeenCalled(); + hide(sessionModal()); + expect(mockDispatch).toHaveBeenCalledWith({ type: 'vault/resetCollaborativeSession' }); + expect(mockNavigate).not.toHaveBeenCalled(); + // A partially cleared session is not readiness to start a new one. + mockState.vault.collaborativeSession = { ...existingSession(), signers: {} }; + update(); + expect(mockNavigate).not.toHaveBeenCalled(); + mockState.vault.collaborativeSession = emptySession(); + update(); + update(); + expect(mockNavigate).toHaveBeenCalledTimes(1); + }); + + test.each(['account', 'network'])('cancels add-modal handoff on %s switch', (switchType) => { + mount(existingSession()); + selectCollaborative(); + const lateHide = addModal().findByType('NativeModal' as any).props.onModalHide; + if (switchType === 'account') mockState.storage.appId = 'disposable-account-b'; + else mockState.settings.bitcoinNetworkType = 'MAINNET'; + update(); + act(() => lateHide()); + expect(sessionModal().props.visible).toBe(false); + expect(mockDispatch).not.toHaveBeenCalled(); + expect(mockNavigate).not.toHaveBeenCalled(); + }); + + test.each(['account', 'network'])('cancels pending reset on %s switch before reset settles', (switchType) => { + mount(existingSession()); + selectCollaborative(); + hide(addModal()); + act(() => sessionModal().props.secondaryCallback()); + hide(sessionModal()); + if (switchType === 'account') mockState.storage.appId = 'disposable-account-b'; + else mockState.settings.bitcoinNetworkType = 'MAINNET'; + mockState.vault.collaborativeSession = emptySession(); + update(); + expect(mockNavigate).not.toHaveBeenCalled(); + }); + + test('cancels continuation if account changes while its modal is closing', () => { + mount(existingSession()); + selectCollaborative(); + hide(addModal()); + act(() => sessionModal().props.buttonCallback()); + const lateHide = sessionModal().findByType('NativeModal' as any).props.onModalHide; + mockState.storage.appId = 'disposable-account-b'; + update(); + act(() => lateHide()); + expect(mockNavigate).not.toHaveBeenCalled(); + expect(mockDispatch).not.toHaveBeenCalled(); + }); + + test('does not continue a session that was reset while its modal was closing', () => { + mount(existingSession()); + selectCollaborative(); + hide(addModal()); + act(() => sessionModal().props.buttonCallback()); + mockState.vault.collaborativeSession = emptySession(); + update(); + hide(sessionModal()); + expect(mockNavigate).not.toHaveBeenCalled(); + expect(mockDispatch).not.toHaveBeenCalled(); + }); + + test('dismissed session choices and a late unmounted close cannot navigate', () => { + mount(existingSession()); + selectCollaborative(); + hide(addModal()); + act(() => sessionModal().props.close()); + expect(sessionModal().findAllByType('NativeModal' as any)).toHaveLength(0); + expect(mockNavigate).not.toHaveBeenCalled(); + selectCollaborative(); + const lateHide = addModal().findByType('NativeModal' as any).props.onModalHide; + act(() => screen.unmount()); + act(() => lateHide()); + expect(mockNavigate).not.toHaveBeenCalled(); + expect(mockDispatch).not.toHaveBeenCalled(); + }); + + test.each([ + ['New wallet', 'AddNewWallet'], + ['Import wallet', 'VaultConfigurationCreation'], + ])('preserves immediate unrelated %s navigation and modal unmount', (title, destination) => { + mount(); + act(() => screen.root.findByType('Fab' as any).props.onPress()); + const option = addModal().findAllByType(TouchableOpacity).find((press) => + press.findAllByType(Text).some((text) => text.props.children === title) + ); + act(() => option!.props.onPress()); + expect(mockNavigate).toHaveBeenCalledWith(destination); + expect(addModal().findAllByType('NativeModal' as any)).toHaveLength(0); + expect(mockDispatch).not.toHaveBeenCalled(); + }); + + test('callers without a hide callback retain immediate unmount behavior', () => { + act(() => { + screen = create(); + }); + expect(screen.root.findAllByType('NativeModal' as any)).toHaveLength(1); + act(() => screen.update()); + expect(screen.root.findAllByType('NativeModal' as any)).toHaveLength(0); + }); +}); From d0bf05801eeee3c35a120d84998ca94b98ef1bf4 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Mon, 12 Oct 2026 06:28:12 +0000 Subject: [PATCH 117/254] test: type deliberate native modal boundary subscription --- tests/screens/HomeWalletCollaborative.test.tsx | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/screens/HomeWalletCollaborative.test.tsx b/tests/screens/HomeWalletCollaborative.test.tsx index 42ef0b7558..0bd49a9a3f 100644 --- a/tests/screens/HomeWalletCollaborative.test.tsx +++ b/tests/screens/HomeWalletCollaborative.test.tsx @@ -105,10 +105,11 @@ function hide(modal: ReturnType) { beforeEach(() => { jest.useFakeTimers(); - // The repository-wide Dimensions mock does not return a removable subscription. + // The global mock lacks a subscription. This native-boundary fake models only + // remove(), the API that useWindowDimensions calls during component teardown. jest.spyOn(Dimensions, 'addEventListener').mockReturnValue({ remove: jest.fn(), - } as ReturnType); + } as unknown as ReturnType); mockNavigate.mockClear(); mockDispatch.mockClear(); mockState = { From c3b9b37ece19e18ea3198e3bf6aaaf20b05660ab Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Mon, 12 Oct 2026 06:30:05 +0000 Subject: [PATCH 118/254] test: accept wrong-key AES UTF-8 rejection --- tests/services/encryption.test.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/services/encryption.test.ts b/tests/services/encryption.test.ts index 24a89e6a57..2c395c4ae2 100644 --- a/tests/services/encryption.test.ts +++ b/tests/services/encryption.test.ts @@ -82,7 +82,13 @@ describe('Testing cryptographic primitives', () => { test('Decryption::Incorrect key', () => { const incorrectKey = 'a'.repeat(64); - const decrypted = decrypt(incorrectKey, encrypted); + let decrypted: string; + try { + decrypted = decrypt(incorrectKey, encrypt(key, message)); + } catch (error) { + expect(error).toEqual(new Error('Malformed UTF-8 data')); + return; + } expect(decrypted).not.toBe(message); }); }); From f8ca5a843838079cf2d653b036dd8617107aa388 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Mon, 12 Oct 2026 06:30:05 +0000 Subject: [PATCH 119/254] test: accept wrong-key AES UTF-8 rejection --- tests/services/encryption.test.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/services/encryption.test.ts b/tests/services/encryption.test.ts index 24a89e6a57..2c395c4ae2 100644 --- a/tests/services/encryption.test.ts +++ b/tests/services/encryption.test.ts @@ -82,7 +82,13 @@ describe('Testing cryptographic primitives', () => { test('Decryption::Incorrect key', () => { const incorrectKey = 'a'.repeat(64); - const decrypted = decrypt(incorrectKey, encrypted); + let decrypted: string; + try { + decrypted = decrypt(incorrectKey, encrypt(key, message)); + } catch (error) { + expect(error).toEqual(new Error('Malformed UTF-8 data')); + return; + } expect(decrypted).not.toBe(message); }); }); From b78b480eb4223390b2d17073387f9887d2e35f95 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Mon, 12 Oct 2026 06:54:39 +0000 Subject: [PATCH 120/254] docs: clarify inheritance maturity and isolated iOS validation inputs --- dev/local-backend/README.md | 2 ++ release/r4-inheritance-validation.md | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/dev/local-backend/README.md b/dev/local-backend/README.md index 445707f9d3..07d2fb2f5e 100644 --- a/dev/local-backend/README.md +++ b/dev/local-backend/README.md @@ -105,6 +105,8 @@ xcrun simctl list devices available Use the root Gemfile explicitly; the `ios` directory has a different Gemfile. CocoaPods may refresh checkout-path-dependent Hermes podspec checksums on initial installation; review any lockfile diff and do not treat dependency version changes as routine setup. +For exact-source validation, use `pod install --deployment` and report a checksum mismatch as blocked instead of changing the lock incidentally. Before building, check for the shared `/tmp/envfile` selector: react-native-config gives it precedence over `ENVFILE`. Preserve any existing selector, coordinate with other builds, and proceed only when the effective file is this checkout's `.env.local`. Do not print configuration contents or delete another build's selector. + Start `yarn start` in a separate terminal. You can inspect the workspace in Xcode (`ios/hexa_keeper.xcworkspace`, scheme `hexa_keeper_dev`). Use the command below for local builds so `ENVFILE` reaches every native build phase; simply clicking Run with the default scheme settings does not select `.env.local`: ```sh diff --git a/release/r4-inheritance-validation.md b/release/r4-inheritance-validation.md index 96072ecf3a..284fb78858 100644 --- a/release/r4-inheritance-validation.md +++ b/release/r4-inheritance-validation.md @@ -53,11 +53,11 @@ Record the final full app SHA, app bundle/package/version/build, OS/device, sign | Inheritance Planning entry | “Create vault with Inheritance Key” reaches creation with inheritance preselected after the scoped route-param fix. Record final availability/product gating; the current helper hardcodes L3 and does not prove subscription enforcement. | | Two identities from the same seed | Passphrase and no-passphrase fingerprint/xpub differ; primary and inheritance roles match the imported origins. No stored field or heuristic is treated as proof of common seed words. | | Descriptor and receive addresses | Two receive addresses plus change branch share one fixed fallback lock. Mainnet read-only policy inspection shows 180 days; Testnet actual descriptor shows its accelerated duration. Export/reimport preserves exact policy and origins. | -| Before activation | Supported hardware successfully signs the primary Testnet path. The no-passphrase key alone cannot satisfy/finalize the pre-activation fallback. Record actual transaction/script outcome, not only whether a key appears selectable. | +| Before activation | Supported hardware successfully signs the primary Testnet path. The no-passphrase key must not produce a currently spendable fallback before the descriptor lock matures. A signature or finalized future-dated PSBT alone is not maturity evidence; verify policy and chain finality in disposable fixtures. | | After activation | Once chain median time or height is beyond the descriptor's lock, the no-passphrase key alone signs/finalizes a disposable fallback transaction; original primary still works. Record the maturity source and lock value. | | No reset on use | Receive before and after activation; make a primary spend before activation; refresh/restart. Descriptor and fallback lock remain identical. A later deposit does not acquire a new six-month clock. | | Recovery | On a separate disposable install, restore from exported wallet configuration with the required signer/seed material. Verify addresses and lock unchanged. Do not overwrite an owner's installed app or wallet data. | -| Re-vault limits | Do not initiate on a real wallet. If authorized for a disposable fixture, separately verify new policy/schedule, explicit transfer/fees, cancel/retry/restart and old-address continuity. This is not required to call the existing fixed-date setup an inactivity reset. | +| Re-vault limits | Do not initiate on a real wallet. If authorized for a disposable fixture, separately verify new policy/schedule, explicit transfer/fees, cancel/retry/restart and old-address continuity. Activation remains absolute; re-vaulting is a separate manual operation. | Pending validation: exact UI, native builds/launch, hardware signing and fallback maturity, restore, and final release availability. Source fixture checks alone do not establish these. From b46825b979a21badef07e913744a0029ada99f56 Mon Sep 17 00:00:00 2001 From: Anant Date: Mon, 12 Oct 2026 20:20:37 +0530 Subject: [PATCH 121/254] fix: require complete production service config and iOS env selection --- ios/fastlane/Fastfile | 9 +++ ios/scripts/verify-retired-env.rb | 13 +++- ios/scripts/verify-retired-env.test.cjs | 42 +++++++++++++ scripts/test_fastlane_build_only.rb | 17 ++++++ scripts/verify-channel-config.cjs | 56 ++++++++++++++++- scripts/verify-channel-config.test.cjs | 81 ++++++++++++++++++++++++- 6 files changed, 213 insertions(+), 5 deletions(-) diff --git a/ios/fastlane/Fastfile b/ios/fastlane/Fastfile index e67c168463..29d1c67ee1 100644 --- a/ios/fastlane/Fastfile +++ b/ios/fastlane/Fastfile @@ -54,11 +54,20 @@ def upload_build_to_testflight! upload_to_testflight(options) end +def verify_envfile_selector!(production_env, selector = "/tmp/envfile") + return unless File.exist?(selector) + + if File.read(selector).strip != production_env + UI.user_error!("/tmp/envfile would override the reviewed .env.production") + end +end + def build_verified_production_ipa! sh("node", File.expand_path("../../scripts/release-version.cjs", __dir__), "release-check") production_env = File.expand_path("../../.env.production", __dir__) UI.user_error!("Missing reviewed .env.production") unless File.file?(production_env) sh("node", File.expand_path("../../scripts/verify-channel-config.cjs", __dir__), production_env) + verify_envfile_selector!(production_env) ENV['ENVFILE'] = production_env get_certificates( # Create or get certificate, and install it diff --git a/ios/scripts/verify-retired-env.rb b/ios/scripts/verify-retired-env.rb index 413497ce62..ff11a414fc 100644 --- a/ios/scripts/verify-retired-env.rb +++ b/ios/scripts/verify-retired-env.rb @@ -3,8 +3,19 @@ # Use react-native-config's own reader so this checks the same ENVFILE, # /tmp/envfile override, and .env fallback that its code generator will use. -reader_directory, environment_root = ARGV +reader_directory, environment_root, selector_path = ARGV abort('Missing react-native-config environment inputs') unless reader_directory && environment_root +selector_path ||= '/tmp/envfile' + +# react-native-config prefers the shared selector over ENVFILE. Recheck it in +# the pod codegen phase, after the release lane's earlier preflight check. +if File.basename(ENV['ENVFILE'].to_s) == '.env.production' + production_env = File.expand_path(ENV['ENVFILE']) + abort('Reviewed production environment is missing during iOS codegen') unless File.file?(production_env) + if File.exist?(selector_path) && File.read(selector_path).strip != production_env + abort('iOS codegen environment differs from the reviewed .env.production') + end +end retired_names = %w[GASFREE_API_KEY GASFREE_API_SECRET LETS_EXCHANGE_API_KEY] retired_names += retired_names.map { |name| "RN_#{name}" } diff --git a/ios/scripts/verify-retired-env.test.cjs b/ios/scripts/verify-retired-env.test.cjs index d25f96564d..4e496e11e0 100644 --- a/ios/scripts/verify-retired-env.test.cjs +++ b/ios/scripts/verify-retired-env.test.cjs @@ -56,3 +56,45 @@ test('iOS pod guard rejects a retired process variable before logging its value' assert.notEqual(result.status, 0); assert.doesNotMatch(result.stdout + result.stderr, /synthetic-secret-not-for-output/); }); + +test('iOS production codegen rejects a conflicting envfile selector', () => { + assert.ok(fs.existsSync(reader), 'Install react-native-config before running this test'); + const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'keeper-ios-production-env-')); + try { + const production = path.join(directory, '.env.production'); + const selector = path.join(directory, 'envfile-selector'); + fs.writeFileSync(production, 'CHANNEL_URL=https://channel.bitcoinkeeper.app/\n'); + fs.writeFileSync(selector, path.join(directory, 'unreviewed.env')); + const result = spawnSync('ruby', [verifier, reader, directory, selector], { + cwd: directory, + env: { PATH: process.env.PATH, ENVFILE: production }, + encoding: 'utf8', + }); + assert.notEqual(result.status, 0); + assert.match(result.stderr, /environment differs from the reviewed/); + } finally { + fs.rmSync(directory, { recursive: true, force: true }); + } +}); + +test('iOS production codegen accepts matching or absent envfile selector', () => { + assert.ok(fs.existsSync(reader), 'Install react-native-config before running this test'); + const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'keeper-ios-production-env-')); + try { + const production = path.join(directory, '.env.production'); + const selector = path.join(directory, 'envfile-selector'); + fs.writeFileSync(production, 'CHANNEL_URL=https://channel.bitcoinkeeper.app/\n'); + for (const selectorContents of [null, production]) { + if (selectorContents === null) fs.rmSync(selector, { force: true }); + else fs.writeFileSync(selector, selectorContents); + const result = spawnSync('ruby', [verifier, reader, directory, selector], { + cwd: directory, + env: { PATH: process.env.PATH, ENVFILE: production }, + encoding: 'utf8', + }); + assert.equal(result.status, 0, result.stderr); + } + } finally { + fs.rmSync(directory, { recursive: true, force: true }); + } +}); diff --git a/scripts/test_fastlane_build_only.rb b/scripts/test_fastlane_build_only.rb index 667b1139d0..5ecfe8f73c 100644 --- a/scripts/test_fastlane_build_only.rb +++ b/scripts/test_fastlane_build_only.rb @@ -48,6 +48,12 @@ def initialize(target_platform, root) file = File.expand_path("../#{target_platform}/fastlane/Fastfile", __dir__) # Keep __dir__ resolution and real File.file? checks within the disposable fixture. instance_eval(File.read(file), File.join(root, target_platform.to_s, 'fastlane', 'Fastfile')) + if target_platform == :ios + real_selector_check = method(:verify_envfile_selector!) + define_singleton_method(:verify_envfile_selector!) do |production_env, selector = File.join(@root, 'absent-selector')| + real_selector_check.call(production_env, selector) + end + end end def default_platform(_platform); end @@ -157,6 +163,17 @@ def distribution_events(fastlane) fastlane.events.select { |event| FakeFastlane::DISTRIBUTION_ACTIONS.include?(event.first) } end + def test_ios_selector_rejects_an_unreviewed_environment + fastlane = FakeFastlane.new(:ios, @root) + selector = File.join(@root, 'envfile-selector') + File.write(selector, File.join(@root, 'other.env')) + assert_match(/override the reviewed/, assert_raises(RuntimeError) { + fastlane.verify_envfile_selector!(File.join(@root, '.env.production'), selector) + }.message) + File.write(selector, File.join(@root, '.env.production')) + fastlane.verify_envfile_selector!(File.join(@root, '.env.production'), selector) + end + %i[ios android].each do |platform| define_method("test_#{platform}_build_only_verifies_current_production_artifacts_without_distribution") do fastlane = FakeFastlane.new(platform, @root) diff --git a/scripts/verify-channel-config.cjs b/scripts/verify-channel-config.cjs index cee7d7ffdc..6c9d85a987 100644 --- a/scripts/verify-channel-config.cjs +++ b/scripts/verify-channel-config.cjs @@ -16,7 +16,7 @@ const matches = envContents .split(/\r?\n/) .map(line => line.match(/^\s*(?:export\s+)?CHANNEL_URL\s*=\s*(.*?)\s*$/)) .filter(Boolean) - .map(match => match[1].replace(/^['"]|['"]$/g, '')); + .map(match => match[1].trim()); if (matches.length !== 1 || matches[0] !== expected) { throw new Error(`Production CHANNEL_URL must be ${expected}`); @@ -26,10 +26,60 @@ const stages = envContents .split(/\r?\n/) .map(line => line.match(/^\s*(?:export\s+)?ENVIRONMENT\s*=\s*(.*?)\s*$/)) .filter(Boolean) - .map(match => match[1].replace(/^['"]|['"]$/g, '')); + .map(match => match[1].trim()); if (stages.length !== 1 || stages[0] !== 'PRODUCTION') { throw new Error('Production ENVIRONMENT must be PRODUCTION'); } -console.log('Production channel URL and environment verified'); +const required = [ + 'RELAY', + 'SIGNING_SERVER_TESTNET', + 'SIGNING_SERVER_MAINNET', + 'HEXA_ID_TESTNET', + 'HEXA_ID_MAINNET', +]; +const values = Object.fromEntries(required.map((name) => { + const matches = envContents + .split(/\r?\n/) + .map(line => line.match(new RegExp(`^\\s*(?:export\\s+)?${name}\\s*=\\s*(.*?)\\s*$`))) + .filter(Boolean) + .map(match => match[1].trim()); + // The native dotenv reader can retain malformed or mixed quotes. Keep the + // reviewed value identical to the value that will be embedded in the app. + if (matches.length !== 1 || !matches[0] || /['"]/.test(matches[0])) { + throw new Error(`Production ${name} must be set exactly once`); + } + return [name, matches[0]]; +})); + +const serviceURLs = {}; +for (const name of ['RELAY', 'SIGNING_SERVER_TESTNET', 'SIGNING_SERVER_MAINNET']) { + let url; + try { + url = new URL(values[name]); + } catch { + throw new Error(`Production ${name} must be an HTTPS URL`); + } + if (!values[name].startsWith('https://') || values[name].includes('\\') || + url.protocol !== 'https:' || !url.hostname || url.username || url.password || + url.search || url.hash || !values[name].endsWith('/') || values[name].endsWith('//')) { + throw new Error(`Production ${name} must be an HTTPS URL ending in / without credentials or query`); + } + serviceURLs[name] = url; +} + +for (const name of ['HEXA_ID_TESTNET', 'HEXA_ID_MAINNET']) { + if (!/^[0-9a-fA-F]{64}$/.test(values[name])) { + throw new Error(`Production ${name} must be a 64-character hex ID`); + } +} + +if (serviceURLs.RELAY.hostname.replace(/\.+$/, '') === 'bithyve-dev-relay.el.r.appspot.com' || + serviceURLs.SIGNING_SERVER_MAINNET.hostname.replace(/\.+$/, '') === 'dev-sign.bithyve.com' || + values.HEXA_ID_MAINNET.toLowerCase() === + 'b01623f1065ba45d68b516efe2873f59bfc9b9b2d8b194f94f989d87d711830a') { + throw new Error('Production mainnet services must not use development defaults'); +} + +console.log('Production channel URL, environment, and service configuration verified'); diff --git a/scripts/verify-channel-config.test.cjs b/scripts/verify-channel-config.test.cjs index e9863c25d5..8c64475da0 100644 --- a/scripts/verify-channel-config.test.cjs +++ b/scripts/verify-channel-config.test.cjs @@ -7,7 +7,16 @@ const { spawnSync } = require('node:child_process'); const verifier = path.join(__dirname, 'verify-channel-config.cjs'); const prebuild = path.join(__dirname, '..', 'appcenter-pre-build.sh'); -const channel = 'CHANNEL_URL=https://channel.bitcoinkeeper.app/\nENVIRONMENT=PRODUCTION\n'; +const channel = [ + 'CHANNEL_URL=https://channel.bitcoinkeeper.app/', + 'ENVIRONMENT=PRODUCTION', + 'RELAY=https://relay.example.invalid/', + 'SIGNING_SERVER_TESTNET=https://test-sign.example.invalid/', + 'SIGNING_SERVER_MAINNET=https://sign.example.invalid/', + `HEXA_ID_TESTNET=${'a'.repeat(64)}`, + `HEXA_ID_MAINNET=${'b'.repeat(64)}`, + '', +].join('\n'); function withTempDirectory(run) { const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'keeper-usdt-env-')); @@ -26,6 +35,76 @@ test('release env accepts reviewed channel without retired credentials', () => assert.equal(result.status, 0, result.stderr); })); +test('release env rejects missing or duplicate service configuration', () => + withTempDirectory((directory) => { + const envFile = path.join(directory, '.env.production'); + for (const name of ['RELAY', 'SIGNING_SERVER_MAINNET', 'HEXA_ID_MAINNET']) { + fs.writeFileSync(envFile, channel.split('\n').filter(line => !line.startsWith(`${name}=`)).join('\n')); + const missing = spawnSync(process.execPath, [verifier, envFile], { encoding: 'utf8' }); + assert.notEqual(missing.status, 0); + assert.match(missing.stderr, new RegExp(`Production ${name} must be set exactly once`)); + } + fs.writeFileSync(envFile, `${channel}RELAY=https://another.example.invalid/\n`); + const duplicate = spawnSync(process.execPath, [verifier, envFile], { encoding: 'utf8' }); + assert.notEqual(duplicate.status, 0); + assert.match(duplicate.stderr, /Production RELAY must be set exactly once/); + })); + +test('release env rejects equivalent development mainnet defaults', () => + withTempDirectory((directory) => { + const envFile = path.join(directory, '.env.production'); + for (const config of [ + channel.replace('SIGNING_SERVER_MAINNET=https://sign.example.invalid/', + 'SIGNING_SERVER_MAINNET=https://DEV-SIGN.BITHYVE.COM/path/'), + channel.replace('SIGNING_SERVER_MAINNET=https://sign.example.invalid/', + 'SIGNING_SERVER_MAINNET=https://dev-sign.bithyve.com../'), + channel.replace('RELAY=https://relay.example.invalid/', + 'RELAY=https://bithyve-dev-relay.el.r.appspot.com/'), + channel.replace(`HEXA_ID_MAINNET=${'b'.repeat(64)}`, + 'HEXA_ID_MAINNET=B01623F1065BA45D68B516EFE2873F59BFC9B9B2D8B194F94F989D87D711830A'), + ]) { + fs.writeFileSync(envFile, config); + const result = spawnSync(process.execPath, [verifier, envFile], { encoding: 'utf8' }); + assert.notEqual(result.status, 0); + assert.match(result.stderr, /must not use development defaults/); + } + })); + +test('release env requires usable service URL prefixes', () => + withTempDirectory((directory) => { + const envFile = path.join(directory, '.env.production'); + for (const value of [ + 'https://relay.example.invalid', + 'https://relay.example.invalid//', + 'https:relay.example.invalid/', + 'https:/relay.example.invalid/', + 'https:\\relay.example.invalid/', + 'https://relay.example.invalid/\'', + '"https://relay.example.invalid/\'', + 'https://relay.example.invalid/path?mode=release/', + 'https://name:password@relay.example.invalid/', + ]) { + fs.writeFileSync(envFile, channel.replace('RELAY=https://relay.example.invalid/', `RELAY=${value}`)); + const result = spawnSync(process.execPath, [verifier, envFile], { encoding: 'utf8' }); + assert.notEqual(result.status, 0); + assert.match(result.stderr, /Production RELAY must be (?:an HTTPS URL|set exactly once)/); + } + })); + +test('release env rejects malformed quotes in channel and stage', () => + withTempDirectory((directory) => { + const envFile = path.join(directory, '.env.production'); + for (const config of [ + channel.replace('CHANNEL_URL=https://channel.bitcoinkeeper.app/', + 'CHANNEL_URL="https://channel.bitcoinkeeper.app/\''), + channel.replace('ENVIRONMENT=PRODUCTION', 'ENVIRONMENT="PRODUCTION\''), + ]) { + fs.writeFileSync(envFile, config); + const result = spawnSync(process.execPath, [verifier, envFile], { encoding: 'utf8' }); + assert.notEqual(result.status, 0); + } + })); + test('direct Android production release requires and selects the reviewed file', () => withTempDirectory((directory) => { fs.mkdirSync(path.join(directory, 'scripts')); From 3aa9c6871fe73dd129934e3e624a84860ed3b178 Mon Sep 17 00:00:00 2001 From: Anant Date: Mon, 12 Oct 2026 19:58:50 +0530 Subject: [PATCH 122/254] release: refresh authenticated Android Play baseline Android Publisher API read at 2026-10-12T14:27:41Z for io.hexawallet.bitcoinkeeper: production 2.6.3 (626), highest uploaded bundle 626 across all listed tracks. A temporary edit was deleted without commit. iOS evidence remains stale and release preflight still fails for iOS. --- release/version.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/release/version.json b/release/version.json index 9754ddfac8..eeaf42ae08 100644 --- a/release/version.json +++ b/release/version.json @@ -5,10 +5,10 @@ "baselines": { "android": { "applicationId": "io.hexawallet.bitcoinkeeper", - "verifiedAt": "2026-10-06T14:11:41Z", - "highestUploadedBuild": 625, - "highestMarketingVersion": "2.6.2", - "productionRelease": "625 (2.6.2)" + "verifiedAt": "2026-10-12T14:27:41Z", + "highestUploadedBuild": 626, + "highestMarketingVersion": "2.6.3", + "productionRelease": "626 (2.6.3)" }, "ios": { "applicationId": "io.hexawallet.keeper", From d12e4a21ccc8ab655a53608f1b3a2fade3b423d5 Mon Sep 17 00:00:00 2001 From: Anant Date: Mon, 12 Oct 2026 20:04:24 +0530 Subject: [PATCH 123/254] fix: reject stale backup writes after account round trips --- src/services/backend/Relay.ts | 4 +- src/services/backup/transport.ts | 20 +++++++- src/store/sagas/bhr.ts | 15 ++++-- src/store/store.ts | 13 ++++- tests/recovery/accountBinding.test.cjs | 71 +++++++++++++++++++++++++- tests/recovery/sagaHarness.cjs | 1 + 6 files changed, 112 insertions(+), 12 deletions(-) diff --git a/src/services/backend/Relay.ts b/src/services/backend/Relay.ts index b48a1df3be..d338ca4ced 100644 --- a/src/services/backend/Relay.ts +++ b/src/services/backend/Relay.ts @@ -118,14 +118,14 @@ export default class Relay { nodes?: string[]; // Required to clear nodes: older clients send [] for unrelated updates. replaceNodes?: boolean; - }): Promise<{ + }, accountSession?: number): Promise<{ status: string; updated: boolean; err?: string; message?: string; }> => { try { - const res = await backupPost(`${RELAY}updateAppImage`, appImage); + const res = await backupPost(`${RELAY}updateAppImage`, appImage, false, accountSession); const data = res.data || res.json; return data; } catch (err) { diff --git a/src/services/backup/transport.ts b/src/services/backup/transport.ts index 04e6e46c7b..9bcf1eaecc 100644 --- a/src/services/backup/transport.ts +++ b/src/services/backup/transport.ts @@ -4,12 +4,23 @@ import RestClient from '../rest/RestClient'; const queues = new Map>(); const revisions = new Map(); const queuedMutationRevisions = new Map(); +let activeAccount: string | undefined; +let accountSession = 0; let mayUploadForAccount: (appId: string, explicitChoice: boolean) => boolean = () => false; export const setBackupUploadGuard = (guard: (appId: string, explicitChoice: boolean) => boolean) => { mayUploadForAccount = guard; }; export const canAccessBackupAccount = (appId: string, explicitChoice = false) => mayUploadForAccount(appId, explicitChoice); +// An A -> B -> A switch must invalidate work captured during the first A session. +// The Redux store observes every selected-account transition, including resets. +export function observeBackupAccount(appId?: string) { + if (appId !== activeAccount) { + activeAccount = appId; + accountSession += 1; + } +} +export const currentBackupAccountSession = () => accountSession; export const backupRevision = (id: string) => revisions.get(id) || 0; export const backupQueuedRevision = (id: string) => queuedMutationRevisions.get(id) || 0; export const markBackupMutation = (id: string) => revisions.set(id, backupRevision(id) + 1); @@ -95,14 +106,19 @@ export async function boundedBackupPost( } } -export function backupPost(path: string, body: any, explicitChoice = false) { +export function backupPost(path: string, body: any, explicitChoice = false, expectedSession?: number) { const appId = body.appId || body.appID; if (!appId) return Promise.reject(new Error('Missing backup account')); + const requestSession = expectedSession ?? currentBackupAccountSession(); + if (requestSession !== currentBackupAccountSession()) + return Promise.reject(new Error('Backup account session changed')); if (!canAccessBackupAccount(appId, explicitChoice)) return Promise.reject(new Error('Backup account is not active or opted in')); markBackupMutation(appId); const result = withBackupSession(appId, () => { - // The queued write may execute after an account switch or consent change. + // A queued write may outlive an entire A -> B -> A round trip. + if (requestSession !== currentBackupAccountSession()) + throw new Error('Backup account session changed'); if (!canAccessBackupAccount(appId, explicitChoice)) throw new Error('Backup account is not active or opted in'); return boundedBackupPost(path, body); diff --git a/src/store/sagas/bhr.ts b/src/store/sagas/bhr.ts index 142199bfea..077a301944 100644 --- a/src/store/sagas/bhr.ts +++ b/src/store/sagas/bhr.ts @@ -2,7 +2,7 @@ import { buffers, END, eventChannel } from 'redux-saga'; import { inspectBackup, RepairPhase } from 'src/services/backup/repair'; import { prepareRecoveryImage } from 'src/services/backup/restore'; import { BackupImage, pauseBackup } from 'src/services/backup/image'; -import { markBackupMutation } from 'src/services/backup/transport'; +import { currentBackupAccountSession, markBackupMutation } from 'src/services/backup/transport'; import * as bip39 from 'bip39'; import { Wallet } from 'src/services/wallets/interfaces/wallet'; import { call, put, select, takeMaybe, takeEvery } from 'redux-saga/effects'; @@ -110,8 +110,10 @@ function currentBackupAppId(): string | undefined { export function* updateAppImageWorker({ payload, originAppId, + originBackupAccountSession, }: { originAppId?: string; + originBackupAccountSession?: number; payload: { wallets?: Wallet[] | USDTWallet[]; signers?: Signer[]; @@ -119,11 +121,14 @@ export function* updateAppImageWorker({ }; }) { const appId = originAppId || currentBackupAppId(); - if (!appId || currentBackupAppId() !== appId) + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); + const accountIsCurrent = () => + currentBackupAppId() === appId && currentBackupAccountSession() === requestSession; + if (!appId || !accountIsCurrent()) return { updated: false, error: 'Backup account changed' }; try { const backupResponse = yield call(checkBackupCondition, appId); - if (currentBackupAppId() !== appId) + if (!accountIsCurrent()) return { updated: false, error: 'Backup account changed' }; if (backupResponse) return { updated: true, error: '' }; @@ -163,7 +168,7 @@ export function* updateAppImageWorker({ } } - if (currentBackupAppId() !== appId) + if (!accountIsCurrent()) return { updated: false, error: 'Backup account changed' }; const response = yield call(Relay.updateAppImage, { @@ -176,7 +181,7 @@ export function* updateAppImageWorker({ version, nodes: nodesList, replaceNodes: !!updateNodes, - }); + }, requestSession); if (!response?.updated) yield call(setServerBackupFailed, appId); return response; } catch (err) { diff --git a/src/store/store.ts b/src/store/store.ts index 6105bab262..5807aa4bd3 100644 --- a/src/store/store.ts +++ b/src/store/store.ts @@ -27,7 +27,11 @@ import { RESET_REDUX_STORE } from './sagaActions/upgrade'; import reduxPersistMigrations from './migrations'; import dbManager from 'src/storage/realm/dbManager'; import { RealmSchema } from 'src/storage/realm/enum'; -import { setBackupUploadGuard } from 'src/services/backup/transport'; +import { + currentBackupAccountSession, + observeBackupAccount, + setBackupUploadGuard, +} from 'src/services/backup/transport'; import { isAutomaticCloudBackupEnabled } from './reducers/bhr'; import { KeeperApp } from 'src/models/interfaces/KeeperApp'; @@ -146,7 +150,9 @@ const bindActionOrigin = (api) => (next) => (action) => { accountOwnedActions.has(action.type) && !('originAppId' in action) ) - return next({ ...action, originAppId }); + action = { ...action, originAppId }; + if (action?.type === 'UPDATE_APP_IMAGE') + action = { ...action, originBackupAccountSession: currentBackupAccountSession() }; return next(action); }; const middlewars = [bindActionOrigin, sagaMiddleware]; @@ -164,6 +170,9 @@ export const store = configureStore({ }).concat(middlewars), }); +observeBackupAccount(store.getState().storage.appId); +store.subscribe(() => observeBackupAccount(store.getState().storage.appId)); + setBackupUploadGuard((appId, explicitChoice) => { const state = store.getState(); if ( diff --git a/tests/recovery/accountBinding.test.cjs b/tests/recovery/accountBinding.test.cjs index 7b27b562eb..5a207b16f4 100644 --- a/tests/recovery/accountBinding.test.cjs +++ b/tests/recovery/accountBinding.test.cjs @@ -1,7 +1,7 @@ const { test } = require('node:test'); const assert = require('node:assert/strict'); const { fixture, wallet, signer, enums, loadModule, functions } = require('./sagaHarness.cjs'); -const { harness } = require('./helpers.cjs'); +const { harness, loadModule: loadTransportModule } = require('./helpers.cjs'); const vm = require('node:vm'); const ts = require('typescript'); @@ -81,10 +81,12 @@ test('verified A backup completion after selected B switch cannot enable A or re }); test('dispatch gate drops stale enable and success actions while preserving account-specific resets', () => { + let session = 1; const scope = { Set, RealmSchema: { KeeperApp: 'KeeperApp' }, dbManager: { getObjectByIndex: () => ({ id: realmAppId }) }, + currentBackupAccountSession: () => session, }; let selectedAppId = 'B'; let realmAppId = 'A'; @@ -118,6 +120,14 @@ test('dispatch gate drops stale enable and success actions while preserving acco middleware({ type: 'bhr/setAutomaticCloudBackup', payload: { appId: 'A', enabled: false } }); middleware({ type: 'bhr/setBackupAllSuccess', payload: { appId: 'A', status: false } }); assert.equal(seen.length, 4, 'A disable and success reset remain account-specific after switch'); + selectedAppId = 'A'; + realmAppId = 'A'; + middleware({ type: 'UPDATE_APP_IMAGE', payload: {} }); + assert.equal(seen.at(-1).originAppId, 'A'); + assert.equal(seen.at(-1).originBackupAccountSession, 1); + session += 2; + middleware({ type: 'UPDATE_APP_IMAGE', payload: {}, originAppId: 'A' }); + assert.equal(seen.at(-1).originBackupAccountSession, 3); }); test('A to opted-in B during connectivity wait blocks upload and preserves B local wallet', async () => { @@ -177,6 +187,65 @@ test('queued A transport cannot execute after opted-in B becomes active', async assert.deepEqual(calls, [], 'the queued request must never reach the network'); }); +test('queued A ciphertext cannot replace a newer server wallet after A to B to A', async () => { + const remote = { wallets: { shared: 'initial-A-ciphertext' } }; + const calls = []; + const transport = loadTransportModule('src/services/backup/transport.ts', { + '../rest/RestClient': { post: async (_path, body) => { + calls.push(body); + Object.assign(remote.wallets, body.walletsObject); + return { data: { updated: true } }; + } }, + }); + let active = 'A'; + transport.setBackupUploadGuard((id) => id === active); + transport.observeBackupAccount?.(active); + let release; + const busy = transport.withBackupSession('A', () => new Promise((resolve) => { release = resolve; })); + for (let i = 0; i < 20 && !release; i++) await new Promise(setImmediate); + assert.ok(release); + const pending = transport.backupPost('fixture/updateAppImage', { + appId: 'A', walletsObject: { shared: 'stale-A-ciphertext' }, + }); + active = 'B'; + transport.observeBackupAccount?.(active); + remote.wallets.shared = 'newer-A-ciphertext'; + active = 'A'; + transport.observeBackupAccount?.(active); + release(); + await busy; + await assert.rejects(pending, /session changed/); + assert.equal(remote.wallets.shared, 'newer-A-ciphertext'); + assert.equal(calls.length, 0, 'stale ciphertext must never reach Relay'); +}); + +test('incremental worker rejects an A payload prepared before A to B to A', async () => { + const f = fixture(); + const originAppId = f.app.id; + let session = 1; + f.scope.currentBackupAccountSession = () => session; + let resumeConnectivity; + f.scope.NetInfo.fetch = () => new Promise((resolve) => { resumeConnectivity = resolve; }); + const updating = f.run('updateAppImageWorker', { + originAppId, + originBackupAccountSession: session, + payload: { wallets: [wallet('shared', enums.NetworkType.MAINNET)] }, + }); + for (let i = 0; i < 20 && !resumeConnectivity; i++) await new Promise(setImmediate); + assert.ok(resumeConnectivity); + f.app.id = 'B'; + f.state.storage.appId = 'B'; + session += 1; + f.app.id = originAppId; + f.state.storage.appId = originAppId; + session += 1; + f.remote.wallets.shared = 'newer-A-ciphertext'; + resumeConnectivity({ isConnected: true }); + assert.equal((await updating).updated, false); + assert.equal(f.remote.wallets.shared, 'newer-A-ciphertext'); + assert.equal(f.calls.length, 0); +}); + test('per-account consent starts off, survives switches, and legacy consent needs attribution', () => { const reducer = loadModule('src/store/reducers/bhr.ts', { 'src/models/enums/BHR': loadModule('src/models/enums/BHR.ts'), diff --git a/tests/recovery/sagaHarness.cjs b/tests/recovery/sagaHarness.cjs index 44c17da8f4..01b6360265 100644 --- a/tests/recovery/sagaHarness.cjs +++ b/tests/recovery/sagaHarness.cjs @@ -123,6 +123,7 @@ function fixture(options = {}) { const scope = { ...effects, markBackupMutation: () => {}, + currentBackupAccountSession: () => 0, store: { getState: () => state }, isAutomaticCloudBackupEnabled: (backup, id) => !!backup.automaticCloudBackupByAppId?.[id], ...encryption, From d48cb193e21d49329e65c8ec406aa1247f0b0185 Mon Sep 17 00:00:00 2001 From: Anant Date: Mon, 12 Oct 2026 20:44:25 +0530 Subject: [PATCH 124/254] Guard account-owned backup and recovery callers across session changes --- src/hooks/useUSDTWallets.ts | 77 ++++-- src/services/backend/Relay.ts | 35 ++- src/store/sagas/bhr.ts | 255 ++++++++++++++---- src/store/sagas/login.ts | 12 +- src/store/sagas/upgrade.ts | 95 ++++--- src/store/sagas/utxos.ts | 52 +++- src/store/sagas/wallets.ts | 224 ++++++++++----- src/store/store.ts | 13 +- tests/recovery/accountBinding.test.cjs | 105 ++++++++ .../accountSessionDestructive.test.cjs | 191 +++++++++++++ .../faucetVisibilityAcceptance.test.cjs | 2 +- tests/recovery/labelAccount.test.cjs | 1 + tests/recovery/sagaHarness.cjs | 1 + tests/recovery/sagaState.test.cjs | 1 + tests/recovery/sendLabelOrigin.test.cjs | 1 + tests/recovery/upgradeAccountSession.test.cjs | 223 +++++++++++++++ tests/recovery/usdtAccountDelete.test.cjs | 121 ++++++++- tests/recovery/usdtBackup.test.cjs | 2 + .../recovery/walletRefreshIsolation.test.cjs | 16 ++ tests/store/versionHistory.test.ts | 17 +- 20 files changed, 1232 insertions(+), 212 deletions(-) create mode 100644 tests/recovery/accountSessionDestructive.test.cjs create mode 100644 tests/recovery/upgradeAccountSession.test.cjs diff --git a/src/hooks/useUSDTWallets.ts b/src/hooks/useUSDTWallets.ts index 219d93d1f2..50e230c105 100644 --- a/src/hooks/useUSDTWallets.ts +++ b/src/hooks/useUSDTWallets.ts @@ -24,7 +24,7 @@ import { useAppSelector } from 'src/store/hooks'; import { updateAppImage } from 'src/store/sagaActions/bhr'; import Relay from 'src/services/backend/Relay'; import { canonical, recoveryContent } from 'src/services/backup/image'; -import { markBackupMutation } from 'src/services/backup/transport'; +import { currentBackupAccountSession, markBackupMutation } from 'src/services/backup/transport'; import { invalidateBackupRepair, isAutomaticCloudBackupEnabled, @@ -73,6 +73,7 @@ export const useUSDTWallets = (options: UseUSDTWalletsOptions = {}): UseUSDTWall const dispatch = useDispatch(); const reduxStore = useStore(); const { id: appId }: any = dbManager.getObjectByIndex(RealmSchema.KeeperApp); + const renderAccountSession = currentBackupAccountSession(); const isOriginCurrent = useCallback( () => (dbManager.getObjectByIndex(RealmSchema.KeeperApp) as any)?.id === appId && @@ -115,13 +116,18 @@ export const useUSDTWallets = (options: UseUSDTWalletsOptions = {}): UseUSDTWall primaryMnemonic?: string; importDetails?: USDTWalletImportDetails; }): Promise<{ newWallet?: USDTWallet; error?: string }> => { + const originSession = currentBackupAccountSession(); + const isCurrentCreate = () => + isOriginCurrent() && + renderAccountSession === originSession && + originSession === currentBackupAccountSession(); let beganLocalWrite = false; if (USDT_WALLET_CREATION_PAUSED) { return { error: 'USDT wallet setup is paused' }; } try { - if (!isOriginCurrent()) throw new Error('Account changed'); + if (!isCurrentCreate()) throw new Error('Account changed'); setError(null); const walletNetworkType = USDTWalletSupportedNetwork; const allUSDTWallets: USDTWallet[] = (await dbManager.getObjectByIndex( @@ -130,7 +136,7 @@ export const useUSDTWallets = (options: UseUSDTWalletsOptions = {}): UseUSDTWall null, true // get all wallets )) as any; - if (!isOriginCurrent()) throw new Error('Account changed'); + if (!isCurrentCreate()) throw new Error('Account changed'); let lastInstanceNum = -1; allUSDTWallets.forEach((wallet) => { @@ -149,7 +155,7 @@ export const useUSDTWallets = (options: UseUSDTWalletsOptions = {}): UseUSDTWall instanceNum: params.type === USDTWalletType.DEFAULT ? lastInstanceNum + 1 : null, importDetails: params.importDetails, }); - if (!isOriginCurrent()) throw new Error('Account changed'); + if (!isCurrentCreate()) throw new Error('Account changed'); // check if a USDT wallet already exists with the same mnemonic(especially for imported wallets) const existingWallet = allUSDTWallets.find((wallet) => wallet.id === newWallet.id); @@ -160,7 +166,7 @@ export const useUSDTWallets = (options: UseUSDTWalletsOptions = {}): UseUSDTWall beganLocalWrite = true; if ((await dbManager.createObject(RealmSchema.USDTWallet, newWallet)) !== true) throw new Error('Failed to create wallet'); - if (!isOriginCurrent()) throw new Error('Account changed'); + if (!isCurrentCreate()) throw new Error('Account changed'); // Create usdt wallet backup dispatch(updateAppImage({ wallets: [newWallet], signers: null, updateNodes: false }, appId)); @@ -177,34 +183,40 @@ export const useUSDTWallets = (options: UseUSDTWalletsOptions = {}): UseUSDTWall return { error: err.message || 'Failed to create wallet' }; } }, - [appId, dispatch, isOriginCurrent, reduxStore] + [appId, dispatch, isOriginCurrent, reduxStore, renderAccountSession] ); /** * Delete a wallet */ const deleteWallet = useCallback(async (walletId: string): Promise => { + const originAppId = appId; + const originSession = currentBackupAccountSession(); + const isCurrentDelete = () => + isOriginCurrent() && + renderAccountSession === originSession && + originSession === currentBackupAccountSession(); let remoteDeleted = false; const markIncompleteDeletion = () => { if (!remoteDeleted) return; - markBackupMutation(appId); - dispatch(invalidateBackupRepair(appId)); - dispatch(setPendingAllBackup({ appId, pending: true })); + markBackupMutation(originAppId); + dispatch(invalidateBackupRepair(originAppId)); + dispatch(setPendingAllBackup({ appId: originAppId, pending: true })); }; try { - if (!isOriginCurrent()) return false; + if (!isCurrentDelete()) return false; // An account without Assisted Server Backup consent still owns its local // wallet. Only the opted-in account needs a corresponding relay delete. - if (isAutomaticCloudBackupEnabled(reduxStore.getState().bhr, appId)) { + if (isAutomaticCloudBackupEnabled(reduxStore.getState().bhr, originAppId)) { const response = await Relay.deleteAppImageEntity({ - appId, + appId: originAppId, signers: null, walletIds: [walletId], - }); + }, originSession); if (!response.updated) throw new Error('Failed to delete wallet'); remoteDeleted = true; } - if (!isOriginCurrent()) { + if (!isCurrentDelete()) { markIncompleteDeletion(); return false; } @@ -217,16 +229,22 @@ export const useUSDTWallets = (options: UseUSDTWalletsOptions = {}): UseUSDTWall captureError(err); return false; } - }, [appId, dispatch, isOriginCurrent, reduxStore]); + }, [appId, dispatch, isOriginCurrent, reduxStore, renderAccountSession]); /** * Update a wallet in the database */ const updateWallet = useCallback( async (wallet: USDTWallet): Promise => { + const originAppId = appId; + const operationSession = currentBackupAccountSession(); + const isCurrentUpdate = () => + isOriginCurrent() && + renderAccountSession === operationSession && + operationSession === currentBackupAccountSession(); let recoveryChanged = false; try { - if (!isOriginCurrent()) return false; + if (!isCurrentUpdate()) return false; const previous = dbManager.getObjectById(RealmSchema.USDTWallet, wallet.id); if (!previous) throw new Error('Wallet not found'); const previousWallet = previous.toJSON ? previous.toJSON() : previous; @@ -234,34 +252,37 @@ export const useUSDTWallets = (options: UseUSDTWalletsOptions = {}): UseUSDTWall canonical(recoveryContent('wallets', previousWallet)) !== canonical(recoveryContent('wallets', wallet)); const { id, ...walletUpdateData } = wallet; + if (!isCurrentUpdate()) throw new Error('Account changed'); const updated = await dbManager.updateObjectById( RealmSchema.USDTWallet, id, walletUpdateData ); // Realm updates must omit the primary key. if (!updated) throw new Error('Failed to update wallet'); - if (!isOriginCurrent()) throw new Error('Account changed'); + if (!isCurrentUpdate()) throw new Error('Account changed'); // Metadata and visibility must survive recovery. Balance/account refreshes // are rebuildable caches and must not cause an upload on every sync. if (recoveryChanged) - dispatch(updateAppImage({ wallets: [wallet], signers: null, updateNodes: false }, appId)); + dispatch( + updateAppImage({ wallets: [wallet], signers: null, updateNodes: false }, originAppId) + ); return true; } catch (err) { // The database helper can fail after partially writing fields. Do not // upload the requested state or leave a previous verification trusted. if (recoveryChanged) { - markBackupMutation(appId); - dispatch(invalidateBackupRepair(appId)); - dispatch(setPendingAllBackup({ appId, pending: true })); + markBackupMutation(originAppId); + dispatch(invalidateBackupRepair(originAppId)); + dispatch(setPendingAllBackup({ appId: originAppId, pending: true })); } setError(err.message || 'Failed to update wallet'); captureError(err); return false; } }, - [appId, dispatch, isOriginCurrent] + [appId, dispatch, isOriginCurrent, renderAccountSession] ); /** @@ -275,13 +296,13 @@ export const useUSDTWallets = (options: UseUSDTWalletsOptions = {}): UseUSDTWall accountStatus: updatedAccountStatus, }; - await updateWallet(syncedWallet); + if (!(await updateWallet(syncedWallet))) throw new Error('Failed to persist synced wallet'); return syncedWallet; } catch (err) { captureError(err); throw new Error('Failed to sync account status'); } - }, []); + }, [updateWallet]); /** * Syncs wallet balance @@ -297,13 +318,13 @@ export const useUSDTWallets = (options: UseUSDTWalletsOptions = {}): UseUSDTWall }, }; - await updateWallet(syncedWallet); + if (!(await updateWallet(syncedWallet))) throw new Error('Failed to persist synced wallet'); return syncedWallet; } catch (err) { captureError(err); return wallet; } - }, []); + }, [updateWallet]); /** * Syncs a single wallet with latest data @@ -316,13 +337,13 @@ export const useUSDTWallets = (options: UseUSDTWalletsOptions = {}): UseUSDTWall specs: updatedSpecs, }; - await updateWallet(syncedWallet); + if (!(await updateWallet(syncedWallet))) throw new Error('Failed to persist synced wallet'); return syncedWallet; } catch (err) { captureError(err); return wallet; } - }, []); + }, [updateWallet]); /** * Get wallet by ID diff --git a/src/services/backend/Relay.ts b/src/services/backend/Relay.ts index d338ca4ced..0f9b8c53f4 100644 --- a/src/services/backend/Relay.ts +++ b/src/services/backend/Relay.ts @@ -136,13 +136,14 @@ export default class Relay { public static migrateXfp = async ( appId: string, - signerChanges: SignerChange[] + signerChanges: SignerChange[], + accountSession?: number ): Promise<{ updated: boolean; err?: string; }> => { try { - const res = await backupPost(`${RELAY}migrateXfps`, { appId, signerChanges }); + const res = await backupPost(`${RELAY}migrateXfps`, { appId, signerChanges }, false, accountSession); const data = res.data || res.json; return data; } catch (err) { @@ -152,7 +153,8 @@ export default class Relay { }; public static deleteAppImageEntity = async ( - entityList + entityList, + accountSession?: number ): Promise<{ status: string; updated: boolean; @@ -160,7 +162,7 @@ export default class Relay { message?: string; }> => { try { - const res = await backupPost(`${RELAY}deleteAppImageEntity`, entityList); + const res = await backupPost(`${RELAY}deleteAppImageEntity`, entityList, false, accountSession); const data = res.data || res.json; return data; } catch (err) { @@ -170,7 +172,8 @@ export default class Relay { }; public static deleteVaultImage = async ( - entityList + entityList, + accountSession?: number ): Promise<{ status: string; updated: boolean; @@ -178,7 +181,7 @@ export default class Relay { message?: string; }> => { try { - const res = await backupPost(`${RELAY}deleteVaults`, entityList); + const res = await backupPost(`${RELAY}deleteVaults`, entityList, false, accountSession); const data = res.data || res.json; return data; } catch (err) { @@ -188,7 +191,8 @@ export default class Relay { }; public static updateVaultImage = async ( - vaultData + vaultData, + accountSession?: number ): Promise<{ status?: number; data?: { @@ -198,7 +202,7 @@ export default class Relay { message?: string; }> => { try { - const res = await backupPost(`${RELAY}updateVaultImage`, vaultData); + const res = await backupPost(`${RELAY}updateVaultImage`, vaultData, false, accountSession); const data = res.data || res.json; return data; } catch (err) { @@ -295,7 +299,8 @@ export default class Relay { public static modifyLabels = async ( appId: string, addLabels: any[], - deleteLabels: any[] + deleteLabels: any[], + accountSession?: number ): Promise<{ updated: boolean; }> => { @@ -304,7 +309,7 @@ export default class Relay { appId, addLabels, deleteLabels, - })) as AxiosResponse; + }, false, accountSession)) as AxiosResponse; const { updated } = res.data || res.json; return { updated, @@ -452,7 +457,8 @@ export default class Relay { }; public static backupAllSignersAndVaults = async ( - allData + allData, + accountSession?: number ): Promise<{ updated?: boolean; status?: number; @@ -463,7 +469,7 @@ export default class Relay { message?: string; }> => { try { - const res = await backupPost(`${RELAY}backupAllSignersAndVaults`, allData); + const res = await backupPost(`${RELAY}backupAllSignersAndVaults`, allData, false, accountSession); const data = res.data || res.json; return data; } catch (err) { @@ -498,7 +504,8 @@ export default class Relay { }; public static deleteBackup = async ( - body + body, + accountSession?: number ): Promise<{ status?: number; data?: { @@ -508,7 +515,7 @@ export default class Relay { message?: string; }> => { try { - const res = await backupPost(`${RELAY}deleteBackup`, body, true); + const res = await backupPost(`${RELAY}deleteBackup`, body, true, accountSession); const data = res.data || res.json; return data; } catch (err) { diff --git a/src/store/sagas/bhr.ts b/src/store/sagas/bhr.ts index 077a301944..d8c252b977 100644 --- a/src/store/sagas/bhr.ts +++ b/src/store/sagas/bhr.ts @@ -107,6 +107,14 @@ function currentBackupAppId(): string | undefined { } } +// An in-flight request can complete after the selected account cycles A -> B -> A. +// Its outcome cannot be attributed to the new session, so require readback. +function* flagUncertainBackupOutcome(appId: string) { + markBackupMutation(appId); + yield put(invalidateBackupRepair(appId)); + yield put(setPendingAllBackup({ appId, pending: true })); +} + export function* updateAppImageWorker({ payload, originAppId, @@ -182,11 +190,19 @@ export function* updateAppImageWorker({ nodes: nodesList, replaceNodes: !!updateNodes, }, requestSession); + if (!accountIsCurrent()) { + yield call(flagUncertainBackupOutcome, appId); + return { updated: false, error: 'Backup account changed' }; + } if (!response?.updated) yield call(setServerBackupFailed, appId); return response; } catch (err) { console.log({ err }); console.error('App image update failed', err); + if (!accountIsCurrent()) { + yield call(flagUncertainBackupOutcome, appId); + return { updated: false, error: 'Backup account changed' }; + } yield call(setServerBackupFailed, appId); return { updated: true, error: '' }; } @@ -195,8 +211,10 @@ export function* updateAppImageWorker({ export function* updateVaultImageWorker({ payload, originAppId, + originBackupAccountSession, }: { originAppId?: string; + originBackupAccountSession?: number; payload: { vault: Vault; archiveVaultId?: string; @@ -204,10 +222,13 @@ export function* updateVaultImageWorker({ }; }) { const appId = originAppId || currentBackupAppId(); - if (!appId || currentBackupAppId() !== appId) + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); + const accountIsCurrent = () => + currentBackupAppId() === appId && currentBackupAccountSession() === requestSession; + if (!appId || !accountIsCurrent()) return { updated: false, error: 'Backup account changed' }; const backupResponse = yield call(checkBackupCondition, appId); - if (currentBackupAppId() !== appId) + if (!accountIsCurrent()) return { updated: false, error: 'Backup account changed' }; if (backupResponse) return { updated: true, error: '' }; @@ -234,19 +255,31 @@ export function* updateVaultImageWorker({ xfpHash: hash256(signer.masterFingerprint), }); } - if (currentBackupAppId() !== appId) + if (!accountIsCurrent()) return { updated: false, error: 'Backup account changed' }; if (isUpdate) { - const response = yield call(Relay.updateVaultImage, { - isUpdate, - signersData, - appId: id, - vaultId: vault.id, - vault: vaultEncrypted, - isArchived: !!vault.archived, - }); - return response; + try { + const response = yield call(Relay.updateVaultImage, { + isUpdate, + signersData, + appId: id, + vaultId: vault.id, + vault: vaultEncrypted, + isArchived: !!vault.archived, + }, requestSession); + if (!accountIsCurrent()) { + yield call(flagUncertainBackupOutcome, appId); + return { updated: false, error: 'Backup account changed' }; + } + return response; + } catch (err) { + if (!accountIsCurrent()) { + yield call(flagUncertainBackupOutcome, appId); + return { updated: false, error: 'Backup account changed' }; + } + throw err; + } } // TODO to be removed @@ -261,10 +294,18 @@ export function* updateVaultImageWorker({ isArchived: !!vault.archived, subscription: subscriptionStrings, ...(archiveVaultId && { archiveVaultId }), - }); + }, requestSession); + if (!accountIsCurrent()) { + yield call(flagUncertainBackupOutcome, appId); + return { updated: false, error: 'Backup account changed' }; + } return response; } catch (err) { captureError(err); + if (!accountIsCurrent()) { + yield call(flagUncertainBackupOutcome, appId); + return { updated: false, error: 'Backup account changed' }; + } yield call(setServerBackupFailed, appId); return { updated: true, error: '' }; } @@ -274,16 +315,22 @@ export function* updateVaultImageWorker({ export function* deleteAppImageEntityWorker({ payload, originAppId, + originBackupAccountSession, }: { originAppId?: string; + originBackupAccountSession?: number; payload: { signerIds?: string[]; walletIds?: string[]; }; }) { const appId = originAppId || currentBackupAppId(); - if (!appId || currentBackupAppId() !== appId) + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); + const accountIsCurrent = () => + currentBackupAppId() === appId && currentBackupAccountSession() === requestSession; + if (!appId || !accountIsCurrent()) return { updated: false, error: 'Backup account changed' }; + let remoteDeleteAttempted = false; try { const { signerIds, walletIds } = payload; const { id }: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); @@ -291,19 +338,22 @@ export function* deleteAppImageEntityWorker({ let response; const backupResponse = yield call(checkBackupCondition, appId); - if (currentBackupAppId() !== appId) + if (!accountIsCurrent()) return { updated: false, error: 'Backup account changed' }; if (backupResponse) response = { updated: true, error: '' }; else { + remoteDeleteAttempted = true; response = yield call(Relay.deleteAppImageEntity, { appId: id, signers: signerIds, walletIds, - }); + }, requestSession); } - if (currentBackupAppId() !== appId) + if (!accountIsCurrent()) { + if (remoteDeleteAttempted) yield call(flagUncertainBackupOutcome, appId); return { updated: false, error: 'Backup account changed' }; + } if (response?.updated !== true) { yield put(setPendingAllBackup({ appId, pending: true })); yield call(setServerBackupFailed, appId); @@ -313,9 +363,15 @@ export function* deleteAppImageEntityWorker({ if (walletIds?.length > 0) { for (const walletId of walletIds) { - if (currentBackupAppId() !== appId) + if (!accountIsCurrent()) { + if (remoteDeleteSucceeded) yield call(flagUncertainBackupOutcome, appId); return { updated: false, error: 'Backup account changed' }; + } const deleted = yield call(dbManager.deleteObjectById, RealmSchema.Wallet, walletId); + if (!accountIsCurrent()) { + if (remoteDeleteSucceeded) yield call(flagUncertainBackupOutcome, appId); + return { updated: false, error: 'Backup account changed' }; + } if (deleted !== true) { if (remoteDeleteSucceeded) { yield put(setPendingAllBackup({ appId, pending: true })); @@ -327,14 +383,20 @@ export function* deleteAppImageEntityWorker({ } if (signerIds?.length > 0) { for (const signerId of signerIds) { - if (currentBackupAppId() !== appId) + if (!accountIsCurrent()) { + if (remoteDeleteSucceeded) yield call(flagUncertainBackupOutcome, appId); return { updated: false, error: 'Backup account changed' }; + } const deleted = yield call( dbManager.deleteObjectByPrimaryKey, RealmSchema.Signer, 'id', signerId ); + if (!accountIsCurrent()) { + if (remoteDeleteSucceeded) yield call(flagUncertainBackupOutcome, appId); + return { updated: false, error: 'Backup account changed' }; + } if (deleted !== true) { if (remoteDeleteSucceeded) { yield put(setPendingAllBackup({ appId, pending: true })); @@ -347,6 +409,8 @@ export function* deleteAppImageEntityWorker({ return response; } catch (err) { captureError(err); + if (remoteDeleteAttempted && !accountIsCurrent()) + yield call(flagUncertainBackupOutcome, appId); return { updated: false, error: err }; } } @@ -354,30 +418,43 @@ export function* deleteAppImageEntityWorker({ export function* deleteVaultImageWorker({ payload, originAppId, + originBackupAccountSession, }: { originAppId?: string; + originBackupAccountSession?: number; payload: { vaultIds: string[]; }; }) { const appId = originAppId || currentBackupAppId(); - if (!appId || currentBackupAppId() !== appId) + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); + const accountIsCurrent = () => + currentBackupAppId() === appId && currentBackupAccountSession() === requestSession; + if (!appId || !accountIsCurrent()) return { updated: false, error: 'Backup account changed' }; + let remoteDeleteAttempted = false; try { const backupResponse = yield call(checkBackupCondition, appId); - if (currentBackupAppId() !== appId) + if (!accountIsCurrent()) return { updated: false, error: 'Backup account changed' }; if (backupResponse) return { updated: true, error: '' }; const { vaultIds } = payload; const { id }: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); if (id !== appId) return { updated: false, error: 'Backup account changed' }; + remoteDeleteAttempted = true; const response = yield call(Relay.deleteVaultImage, { appId: id, vaults: vaultIds, - }); + }, requestSession); + if (!accountIsCurrent()) { + yield call(flagUncertainBackupOutcome, appId); + return { updated: false, error: 'Backup account changed' }; + } return response; } catch (err) { captureError(err); + if (remoteDeleteAttempted && !accountIsCurrent()) + yield call(flagUncertainBackupOutcome, appId); return { updated: false, error: err }; } } @@ -430,6 +507,7 @@ function* seedBackedUpWorker() { } function* getAppImageWorker({ payload }) { + const requestSession = currentBackupAccountSession(); const { primaryMnemonic, isForgot } = payload; try { yield put(setAppImageError('')); @@ -448,6 +526,8 @@ function* getAppImageWorker({ payload }) { const encryptionKey = generateEncryptionKey(primarySeed.toString('hex')); const response = yield call(Relay.getAppImage, appID); + if (requestSession !== currentBackupAccountSession()) + throw new Error('Backup account changed'); if (!response?.appImage) throw new Error('Recovery data unavailable'); const { appImage } = response; const labels = response.labels === undefined ? [] : response.labels; @@ -470,7 +550,7 @@ function* getAppImageWorker({ payload }) { icon: 'assets/ic_diamond_hands.svg', receipt: '', }; - yield call( + const recoverySession: number = yield call( recoverApp, primaryMnemonic, primarySeed, @@ -480,8 +560,11 @@ function* getAppImageWorker({ payload }) { appImage, allVaultImages, labels, - previousVersion + previousVersion, + requestSession ); + if (recoverySession !== currentBackupAccountSession() || currentBackupAppId() !== appID) + throw new Error('Backup account changed'); if ((!appImage.wallets || !Object.entries(appImage.wallets).length) && !allVaultImages.length) { // recreate first wallet @@ -499,12 +582,22 @@ function* getAppImageWorker({ payload }) { instanceNum: 0, }, }; - const created = yield call(addNewWalletsWorker, { payload: [defaultWallet] }); + const created = yield call(addNewWalletsWorker, { + payload: [defaultWallet], + originAppId: appID, + originBackupAccountSession: recoverySession, + }); if (created !== true) throw new Error('Recovery data unavailable'); } const recoveryKeySigner = setupRecoveryKeySigningKey(primaryMnemonic); - yield call(addSigningDeviceWorker, { payload: { signers: [recoveryKeySigner] } }); + yield call(addSigningDeviceWorker, { + payload: { signers: [recoveryKeySigner] }, + originAppId: appID, + originBackupAccountSession: recoverySession, + }); + if (recoverySession !== currentBackupAccountSession() || currentBackupAppId() !== appID) + throw new Error('Backup account changed'); const recoveredSigners: Signer[] = yield call(dbManager.getCollection, RealmSchema.Signer); const recoveredKey = recoveredSigners.find( (signer) => getKeyUID(signer) === getKeyUID(recoveryKeySigner) @@ -531,8 +624,15 @@ function* getAppImageWorker({ payload }) { const newVersion = DeviceInfo.getVersion(); if (previousVersion && semver.lt(previousVersion, newVersion)) { console.log(`applying restore upgarde sequence - from: ${previousVersion} to ${newVersion}`); - yield call(applyUpgradeSequence, { previousVersion, newVersion, isRecovery: true }); + yield call(applyUpgradeSequence, { + previousVersion, + newVersion, + isRecovery: true, + originBackupAccountSession: recoverySession, + }); } + if (recoverySession !== currentBackupAccountSession() || currentBackupAppId() !== appID) + throw new Error('Backup account changed'); yield put(addAccount(appID)); yield put(autoSyncWallets(true, true, false, appID)); yield put( @@ -1056,9 +1156,17 @@ export const deleteAppImageEntityWatcher = createWatcher( const backupInspectionOwners = new Map(); -function* runBackupInspection(repair: boolean, expectedAppId?: string, explicitChoice = false) { +function* runBackupInspection( + repair: boolean, + expectedAppId?: string, + explicitChoice = false, + expectedSession?: number +) { + const requestSession = expectedSession ?? currentBackupAccountSession(); + if (requestSession !== currentBackupAccountSession()) return false; const { id }: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); - if (expectedAppId && id !== expectedAppId) return false; + if ((expectedAppId && id !== expectedAppId) || requestSession !== currentBackupAccountSession()) + return false; backupInspectionOwners.set(id, (backupInspectionOwners.get(id) ?? 0) + 1); let finalPhase: RepairPhase = 'unverified'; let inspection: Promise | undefined; @@ -1066,7 +1174,15 @@ function* runBackupInspection(repair: boolean, expectedAppId?: string, explicitC try { yield put(setBackupRepairRunning({ appId: id, running: true })); updates = eventChannel((emit) => { - inspection = inspectBackup(id, repair, (phase) => emit(phase), explicitChoice); + // The repair service accepts the origin session as its fifth argument. + const inspectWithSession = inspectBackup as ( + appId: string, + shouldRepair: boolean, + onPhase: (phase: RepairPhase) => void, + explicitlyChosen: boolean, + session: number + ) => Promise; + inspection = inspectWithSession(id, repair, (phase) => emit(phase), explicitChoice, requestSession); inspection.then( () => emit(END), () => emit(END) @@ -1076,12 +1192,13 @@ function* runBackupInspection(repair: boolean, expectedAppId?: string, explicitC while (true) { const phase = yield takeMaybe(updates); if (phase === END) break; + if (requestSession !== currentBackupAccountSession()) return false; finalPhase = phase; yield put(setBackupRepairState({ appId: id, phase })); } const current: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); const ownerIsActive = yield select((state: RootState) => state.storage.appId === id); - if (current?.id === id && ownerIsActive) { + if (current?.id === id && ownerIsActive && requestSession === currentBackupAccountSession()) { yield put(setPendingAllBackup({ appId: id, pending: finalPhase !== 'verified' })); if (finalPhase === 'verified') { const notifications = dbManager.getObjectByField( @@ -1091,7 +1208,8 @@ function* runBackupInspection(repair: boolean, expectedAppId?: string, explicitC ) as unknown as Array<{ id: string }>; for (const uai of notifications) { const stillActive = yield select((state: RootState) => state.storage.appId === id); - if (!stillActive || currentBackupAppId() !== id) break; + if (!stillActive || currentBackupAppId() !== id || + requestSession !== currentBackupAccountSession()) break; yield call(uaiActionedWorker, { payload: { uaiId: uai.id, action: true } }); } } else if ( @@ -1099,7 +1217,8 @@ function* runBackupInspection(repair: boolean, expectedAppId?: string, explicitC ?.length ) { const stillActive = yield select((state: RootState) => state.storage.appId === id); - if (!stillActive || currentBackupAppId() !== id) return false; + if (!stillActive || currentBackupAppId() !== id || + requestSession !== currentBackupAccountSession()) return false; yield call(addToUaiStackWorker, { payload: { uaiType: uaiType.SERVER_BACKUP_FAILURE, @@ -1111,7 +1230,7 @@ function* runBackupInspection(repair: boolean, expectedAppId?: string, explicitC }); } } - return finalPhase === 'verified'; + return requestSession === currentBackupAccountSession() && finalPhase === 'verified'; } finally { updates?.close(); // Cancellation closes this listener, but does not stop its shared operation. @@ -1124,21 +1243,31 @@ function* runBackupInspection(repair: boolean, expectedAppId?: string, explicitC } } -function* backupFreshnessWorker({ type, appId }) { +function* backupFreshnessWorker({ type, appId, originBackupAccountSession }) { + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); + if (requestSession !== currentBackupAccountSession()) return; const { id }: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); - if (!appId || id !== appId) return; + if (!appId || id !== appId || requestSession !== currentBackupAccountSession()) return; const enabled = yield select((state: RootState) => state.storage.appId === appId && isAutomaticCloudBackupEnabled(state.bhr, appId) ); - if (!enabled) return; - yield call(runBackupInspection, type === REPAIR_BACKUP, appId); + if (!enabled || requestSession !== currentBackupAccountSession()) return; + yield call(runBackupInspection, type === REPAIR_BACKUP, appId, false, requestSession); } export function* backupFreshnessWatcher() { yield takeEvery([CHECK_BACKUP_FRESHNESS, REPAIR_BACKUP], backupFreshnessWorker); } -function* backupAllSignersAndVaultsWorker({ appId }: { appId: string }) { +function* backupAllSignersAndVaultsWorker({ + appId, + originBackupAccountSession, +}: { + appId: string; + originBackupAccountSession?: number; +}) { + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); + if (requestSession !== currentBackupAccountSession()) return false; const { id }: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); if (!appId || appId !== id) return false; const selected = yield select((state: RootState) => state.storage.appId === appId); @@ -1147,10 +1276,11 @@ function* backupAllSignersAndVaultsWorker({ appId }: { appId: string }) { yield put(setBackupAllFailure({ appId, status: false })); yield put(setBackupAllLoading({ appId, status: true })); try { - const verified = yield call(runBackupInspection, true, appId, true); + const verified = yield call(runBackupInspection, true, appId, true, requestSession); const current: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); const stillSelected = yield select((state: RootState) => state.storage.appId === appId); - if (current?.id !== appId || !stillSelected) return false; + if (current?.id !== appId || !stillSelected || requestSession !== currentBackupAccountSession()) + return false; if (verified) yield put(setAutomaticCloudBackup({ appId, enabled: true })); yield put( verified @@ -1168,25 +1298,46 @@ export const backupAllSignersAndVaultsWatcher = createWatcher( BACKUP_ALL_SIGNERS_AND_VAULTS ); -function* deleteBackupWorker({ appId }: { appId: string }) { +function* deleteBackupWorker({ + appId, + originBackupAccountSession, +}: { + appId: string; + originBackupAccountSession?: number; +}) { + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); + if (requestSession !== currentBackupAccountSession()) return false; const { id }: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); if (!appId || appId !== id) return false; yield put(setBackupAllLoading({ appId, status: true })); + let remoteDeleteAttempted = false; try { const current: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); - if (current?.id !== appId) return false; + if (current?.id !== appId || requestSession !== currentBackupAccountSession()) return false; + remoteDeleteAttempted = true; const response = yield call(Relay.deleteBackup, { appId, - }); + }, requestSession); + if (requestSession !== currentBackupAccountSession()) { + yield call(flagUncertainBackupOutcome, appId); + return false; + } if (response?.updated !== true && response?.data?.updated !== true) throw new Error('Backup deletion not confirmed'); const afterDelete: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); - if (afterDelete?.id !== appId) return false; + if (afterDelete?.id !== appId || requestSession !== currentBackupAccountSession()) { + yield call(flagUncertainBackupOutcome, appId); + return false; + } yield put(setAutomaticCloudBackup({ appId, enabled: false })); yield put(setDeleteBackupSuccess({ appId, status: true })); yield put(setPendingAllBackup({ appId, pending: false })); return true; } catch (error) { + if (requestSession !== currentBackupAccountSession()) { + if (remoteDeleteAttempted) yield call(flagUncertainBackupOutcome, appId); + return false; + } yield put(setDeleteBackupFailure({ appId, status: true })); console.log('🚀 ~ deleteBackupWorker ~ error:', error); return false; @@ -1258,15 +1409,21 @@ export function* setServerBackupFailed(expectedAppId?: string) { yield put(setPendingAllBackup({ appId, pending: true })); } -function* validateServerBackupWorker({ callback, originAppId }) { +function* validateServerBackupWorker({ callback, originAppId, originBackupAccountSession }) { + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); + if (requestSession !== currentBackupAccountSession()) { + callback({ status: false, error: true, message: 'Backup account changed.' }); + return; + } const { id }: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); - if (!originAppId || originAppId !== id) { + if (!originAppId || originAppId !== id || requestSession !== currentBackupAccountSession()) { callback({ status: false, error: true, message: 'Backup account changed.' }); return; } - const matched = yield call(runBackupInspection, false, originAppId); + const matched = yield call(runBackupInspection, false, originAppId, false, requestSession); const stillActive = yield select((state: RootState) => state.storage.appId === originAppId); - if (!stillActive || currentBackupAppId() !== originAppId) { + if (!stillActive || currentBackupAppId() !== originAppId || + requestSession !== currentBackupAccountSession()) { callback({ status: false, error: true, message: 'Backup account changed.' }); return; } diff --git a/src/store/sagas/login.ts b/src/store/sagas/login.ts index 60e95eb749..2a482cbbe0 100644 --- a/src/store/sagas/login.ts +++ b/src/store/sagas/login.ts @@ -53,6 +53,7 @@ import { setLoginMethod } from '../reducers/settings'; import { setSubscription } from 'src/store/sagaActions/settings'; import { uaiChecks } from '../sagaActions/uai'; import { applyUpgradeSequence } from './upgrade'; +import { currentBackupAccountSession } from 'src/services/backup/transport'; import { resetSyncing } from '../reducers/wallets'; import { autoSyncWallets } from '../sagaActions/wallets'; import { connectToNode } from '../sagaActions/network'; @@ -205,6 +206,7 @@ function* credentialsAuthWorker({ payload }) { ); } if (keeperApp?.id) yield put(setAppId(keeperApp.id)); + const loginAccountSession = currentBackupAccountSession(); // Store temporary account details if (!allAccounts.length) { @@ -216,8 +218,14 @@ function* credentialsAuthWorker({ payload }) { const lastElement = versionCollection[versionCollection.length - 1]; const lastVersionCode = lastElement.version.split(/[()]/); const currentVersionCode = DeviceInfo.getBuildNumber(); + if (currentBackupAccountSession() !== loginAccountSession || + store.getState().storage.appId !== keeperApp?.id) return; if (semver.lt(previousVersion, newVersion)) { - yield call(applyUpgradeSequence, { previousVersion, newVersion }); + yield call(applyUpgradeSequence, { + previousVersion, + newVersion, + originBackupAccountSession: loginAccountSession, + }); } else if (currentVersionCode !== lastVersionCode[1]) { yield call(dbManager.createObject, RealmSchema.VersionHistory, { version: `${newVersion}(${currentVersionCode})`, @@ -225,6 +233,8 @@ function* credentialsAuthWorker({ payload }) { title: `Upgraded from ${lastVersionCode[1]} to ${currentVersionCode}`, }); } + if (currentBackupAccountSession() !== loginAccountSession || + store.getState().storage.appId !== keeperApp?.id) return; if (appId) { try { const { id }: KeeperApp = yield call( diff --git a/src/store/sagas/upgrade.ts b/src/store/sagas/upgrade.ts index 12bde671a6..11ef41ba43 100644 --- a/src/store/sagas/upgrade.ts +++ b/src/store/sagas/upgrade.ts @@ -40,6 +40,7 @@ import { createWatcher } from '../utilities'; import { setAppVersion } from '../reducers/storage'; import { isAutomaticCloudBackupEnabled, setPendingAllBackup } from '../reducers/bhr'; import { RootState, store } from '../store'; +import { currentBackupAccountSession } from 'src/services/backup/transport'; export const LABELS_INTRODUCTION_VERSION = '1.0.4'; export const BIP329_INTRODUCTION_VERSION = '1.0.7'; @@ -48,73 +49,88 @@ export const APP_KEY_UPGRADE_VERSION = '1.1.12'; export const HEALTH_CHECK_TIMELINE_MIGRATION_VERSION = '1.2.6'; export const SIGNER_POLICY_MIGRATION_VERSION = '2.1.0'; -const upgradeAccountIsActive = (appId?: string) => - !!appId && store.getState().storage.appId === appId && +const upgradeAccountIsActive = (appId?: string, accountSession?: number) => + !!appId && + (accountSession === undefined || accountSession === currentBackupAccountSession()) && + store.getState().storage.appId === appId && (dbManager.getObjectByIndex(RealmSchema.KeeperApp) as unknown as KeeperApp)?.id === appId; export function* applyUpgradeSequence({ previousVersion, newVersion, isRecovery = false, + originBackupAccountSession, }: { previousVersion: string; newVersion: string; isRecovery?: boolean; + originBackupAccountSession?: number; }) { + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); + if (requestSession !== currentBackupAccountSession()) return; console.log(`applying upgrade sequence - from: ${previousVersion} to ${newVersion}`); const { id: originAppId }: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); - if (!originAppId) return; + if (!upgradeAccountIsActive(originAppId, requestSession)) return; if ( semver.gte(previousVersion, LABELS_INTRODUCTION_VERSION) && semver.lt(previousVersion, BIP329_INTRODUCTION_VERSION) ) { - yield put(migrateLabelsToBip329(originAppId)); + yield put({ ...migrateLabelsToBip329(originAppId), originBackupAccountSession: requestSession }); } if (semver.lt(previousVersion, KEY_MANAGEMENT_VERSION)) { - yield call(migrateStructureforSignersInAppImage, originAppId); - yield call(migrateStructureforVaultInAppImage, originAppId); + yield call(migrateStructureforSignersInAppImage, originAppId, requestSession); + yield call(migrateStructureforVaultInAppImage, originAppId, requestSession); } if (semver.lt(previousVersion, APP_KEY_UPGRADE_VERSION)) - yield call(updateAppKeysToEnableSigning, originAppId); + yield call(updateAppKeysToEnableSigning, originAppId, requestSession); if (semver.lt(previousVersion, HEALTH_CHECK_TIMELINE_MIGRATION_VERSION)) { + if (!upgradeAccountIsActive(originAppId, requestSession)) return; yield call(healthCheckTimelineMigration); } if (semver.lt(previousVersion, SIGNER_POLICY_MIGRATION_VERSION)) { - yield call(migrateServerKeyPolicy); + if (!upgradeAccountIsActive(originAppId, requestSession)) return; + yield call(migrateServerKeyPolicy, originAppId, requestSession); const app: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); - if (app?.id === originAppId) yield put(setPendingAllBackup({ appId: originAppId, pending: true })); + if (app?.id === originAppId && upgradeAccountIsActive(originAppId, requestSession)) + yield put(setPendingAllBackup({ appId: originAppId, pending: true })); } - if (!upgradeAccountIsActive(originAppId)) return; + if (!upgradeAccountIsActive(originAppId, requestSession)) return; yield put(setAppVersion(newVersion)); - yield put(updateVersionHistory(previousVersion, newVersion, isRecovery, originAppId)); + yield put({ + ...updateVersionHistory(previousVersion, newVersion, isRecovery, originAppId), + originBackupAccountSession: requestSession, + }); } export function* updateVersionHistoryWorker({ payload, originAppId, + originBackupAccountSession, }: { payload: { previousVersion: string; newVersion: string; isRecovery?: boolean }; originAppId?: string; + originBackupAccountSession?: number; }) { const { previousVersion, newVersion, isRecovery = false } = payload; + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); try { const app: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); - if (!originAppId || app?.id !== originAppId) return; + if (app?.id !== originAppId || !upgradeAccountIsActive(originAppId, requestSession)) return; const enabled = yield select((state: RootState) => state.storage.appId === originAppId && isAutomaticCloudBackupEnabled(state.bhr, originAppId) ); - if (enabled) { + if (enabled && upgradeAccountIsActive(originAppId, requestSession)) { yield call(Relay.updateAppImage, { appId: originAppId, version: newVersion, - }); + }, requestSession); } - if (!upgradeAccountIsActive(originAppId)) return; + if (!upgradeAccountIsActive(originAppId, requestSession)) return; // recoverApp already records the single "Recovered Wallet" entry. if (!isRecovery) { yield call(dbManager.createObject, RealmSchema.VersionHistory, { @@ -137,9 +153,16 @@ export const updateVersionHistoryWatcher = createWatcher( UPDATE_VERSION_HISTORY ); -function* migrateLablesWorker({ originAppId }: { originAppId?: string }) { +function* migrateLablesWorker({ + originAppId, + originBackupAccountSession, +}: { + originAppId?: string; + originBackupAccountSession?: number; +}) { + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); try { - if (!upgradeAccountIsActive(originAppId)) return; + if (!upgradeAccountIsActive(originAppId, requestSession)) return; const UTXOLabels: UTXOInfo[] = yield call(dbManager.getCollection, RealmSchema.UTXOInfo); const tags = []; const wallets: Wallet[] = yield call(dbManager.getCollection, RealmSchema.Wallet); @@ -165,7 +188,7 @@ function* migrateLablesWorker({ originAppId }: { originAppId?: string }) { }); if (tags.length) { - if (!upgradeAccountIsActive(originAppId)) return; + if (!upgradeAccountIsActive(originAppId, requestSession)) return; yield call(dbManager.createObjectBulk, RealmSchema.Tags, tags); const { id, primarySeed }: KeeperApp = yield call( @@ -178,11 +201,12 @@ function* migrateLablesWorker({ originAppId }: { originAppId?: string }) { id: hash256(hash256(encryptionKey + tag.id)), content: encrypt(encryptionKey, JSON.stringify(tag)), })); - if (!upgradeAccountIsActive(originAppId)) return; + if (!upgradeAccountIsActive(originAppId, requestSession)) return; const enabled = yield select((state: RootState) => state.storage.appId === originAppId && isAutomaticCloudBackupEnabled(state.bhr, originAppId) ); - if (enabled) yield call(Relay.modifyLabels, id, tagsToBackup, []); + if (enabled && upgradeAccountIsActive(originAppId, requestSession)) + yield call(Relay.modifyLabels, id, tagsToBackup, [], requestSession); } } catch (error) { console.log({ error }); @@ -191,12 +215,16 @@ function* migrateLablesWorker({ originAppId }: { originAppId?: string }) { export const migrateLablesWatcher = createWatcher(migrateLablesWorker, MIGRATE_LABELS_329); -function* migrateStructureforSignersInAppImage(originAppId: string) { +function* migrateStructureforSignersInAppImage(originAppId: string, requestSession: number) { try { - if (!upgradeAccountIsActive(originAppId)) return; + if (!upgradeAccountIsActive(originAppId, requestSession)) return; const wallets = yield call(dbManager.getCollection, RealmSchema.Wallet); const signers = yield call(dbManager.getCollection, RealmSchema.Signer); - const response = yield call(updateAppImageWorker, { payload: { wallets, signers }, originAppId }); + const response = yield call(updateAppImageWorker, { + payload: { wallets, signers }, + originAppId, + originBackupAccountSession: requestSession, + }); if (response.updated) { console.log('Updated the Signers in app image'); } else { @@ -205,15 +233,16 @@ function* migrateStructureforSignersInAppImage(originAppId: string) { } catch (err) {} } -function* migrateStructureforVaultInAppImage(originAppId: string) { +function* migrateStructureforVaultInAppImage(originAppId: string, requestSession: number) { try { - if (!upgradeAccountIsActive(originAppId)) return; + if (!upgradeAccountIsActive(originAppId, requestSession)) return; const vaults: Vault[] = yield call(dbManager.getCollection, RealmSchema.Vault); const activeVault: Vault = vaults.filter((vault) => !vault.archived)[0] || null; yield call(updateVaultImageWorker, { payload: { isUpdate: true, vault: activeVault }, originAppId, + originBackupAccountSession: requestSession, }); } catch (err) { console.log('Something went wrong in updating the vault image', err); @@ -221,9 +250,9 @@ function* migrateStructureforVaultInAppImage(originAppId: string) { } // This function updates app keys/mobile keys to enable signing by generating and associating extended keys. -function* updateAppKeysToEnableSigning(originAppId: string) { +function* updateAppKeysToEnableSigning(originAppId: string, requestSession: number) { try { - if (!upgradeAccountIsActive(originAppId)) return; + if (!upgradeAccountIsActive(originAppId, requestSession)) return; const wallets = yield call(dbManager.getCollection, RealmSchema.Wallet); const signers = yield call(dbManager.getCollection, RealmSchema.Signer); const keeperSigners = signers.filter( @@ -234,10 +263,14 @@ function* updateAppKeysToEnableSigning(originAppId: string) { ); const { appKeyWalletMap, myAppKeySigners } = mapAppKeysToWallets(wallets, keeperSigners); const extendedKeyMap = generateExtendedKeysForSigners(myAppKeySigners, appKeyWalletMap); - if (!upgradeAccountIsActive(originAppId)) return; + if (!upgradeAccountIsActive(originAppId, requestSession)) return; updateVaultSigners(extendedKeyMap, signers); updateSignerDetails(myAppKeySigners, extendedKeyMap); - const response = yield call(updateAppImageWorker, { payload: { signers }, originAppId }); + const response = yield call(updateAppImageWorker, { + payload: { signers }, + originAppId, + originBackupAccountSession: requestSession, + }); if (response.updated) { console.log('Updated the Signers in app image'); } else { @@ -362,11 +395,12 @@ function* healthCheckTimelineMigration() { } } -function* migrateServerKeyPolicy() { +function* migrateServerKeyPolicy(originAppId: string, requestSession: number) { // migrates old server key policy to new time-based spending limit policy try { const { bitcoinNetwork } = yield select((state: RootState) => state.settings); const signers: Signer[] = yield call(dbManager.getCollection, RealmSchema.Signer); + if (!upgradeAccountIsActive(originAppId, requestSession)) return; for (const signer of signers) { if (signer.type === SignerType.POLICY_SERVER && signer.signerPolicy) { @@ -381,6 +415,7 @@ function* migrateServerKeyPolicy() { ); const { newPolicy } = yield call(SigningServer.migrateSignerPolicy, id, oldPolicy); + if (!upgradeAccountIsActive(originAppId, requestSession)) return; if (!newPolicy) { throw new Error('Migration failed: policy not updated on the server'); } diff --git a/src/store/sagas/utxos.ts b/src/store/sagas/utxos.ts index 9b6b2c7651..eac215e831 100644 --- a/src/store/sagas/utxos.ts +++ b/src/store/sagas/utxos.ts @@ -4,6 +4,7 @@ import { call, delay, put } from 'redux-saga/effects'; import { BIP329Label, UTXOSpendability } from 'src/services/wallets/interfaces'; import { EntityKind, LabelRefType, NetworkType } from 'src/services/wallets/enums'; import Relay from 'src/services/backend/Relay'; +import { currentBackupAccountSession } from 'src/services/backup/transport'; import { Wallet } from 'src/services/wallets/interfaces/wallet'; import { generateAbbreviatedOutputDescriptors } from 'src/utils/service-utilities/utils'; import { Vault } from 'src/services/wallets/interfaces/vault'; @@ -45,9 +46,11 @@ function persistTagsForOrigin( originAppId: string, added: BIP329Label[], deleted: string[] = [], - scope?: LabelSyncScope + scope?: LabelSyncScope, + accountSession?: number ) { if (!currentOriginApp(originAppId)) return false; + if (accountSession !== undefined && accountSession !== currentBackupAccountSession()) return false; if ( scope && (scope.appId !== originAppId || store.getState().settings.bitcoinNetworkType !== scope.networkType) @@ -60,8 +63,15 @@ function persistTagsForOrigin( return true; } -function persistSpendabilityForOrigin(originAppId: string, schema: RealmSchema, id: string, specs: any) { +function persistSpendabilityForOrigin( + originAppId: string, + schema: RealmSchema, + id: string, + specs: any, + accountSession?: number +) { if (!currentOriginApp(originAppId)) return false; + if (accountSession !== undefined && accountSession !== currentBackupAccountSession()) return false; return dbManager.updateObjectById(schema, id, { specs }) === true; } @@ -72,6 +82,7 @@ function originUiAction(action: T, originAppId: stri export function* addLabelsWorker({ payload, originAppId, + originBackupAccountSession, }: { payload: { txId: string; @@ -81,9 +92,12 @@ export function* addLabelsWorker({ type; }; originAppId?: string; + originBackupAccountSession?: number; }) { try { if (!originAppId) return; + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); + if (requestSession !== currentBackupAccountSession()) return; const app = currentOriginApp(originAppId); if (!app) return; const primarySeed = app.primarySeed; @@ -103,16 +117,16 @@ export function* addLabelsWorker({ }; tags.push(tag); }); - if (!(yield call(persistTagsForOrigin, originAppId, tags))) return; + if (!(yield call(persistTagsForOrigin, originAppId, tags, [], undefined, requestSession))) return; try { const backupResponse = yield call(checkBackupCondition, originAppId); - if (!backupResponse && currentOriginApp(originAppId)) { + if (!backupResponse && currentOriginApp(originAppId) && requestSession === currentBackupAccountSession()) { const encryptionKey = generateEncryptionKey(primarySeed); const tagsToBackup = tags.map((tag) => ({ id: hash256(hash256(encryptionKey + tag.id)), content: encrypt(encryptionKey, JSON.stringify(tag)), })); - yield call(Relay.modifyLabels, originAppId, tagsToBackup, []); + yield call(Relay.modifyLabels, originAppId, tagsToBackup, [], requestSession); } else yield delay(100); } catch (error) { console.log('🚀 ~ addLabelsWorker error:', error); @@ -129,6 +143,7 @@ export function* addLabelsWorker({ export function* bulkUpdateLabelsWorker({ payload, originAppId, + originBackupAccountSession, }: { payload: { labelChanges: { @@ -142,9 +157,12 @@ export function* bulkUpdateLabelsWorker({ scope?: LabelSyncScope; }; originAppId?: string; + originBackupAccountSession?: number; }) { try { if (!originAppId) return; + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); + if (requestSession !== currentBackupAccountSession()) return; const app = currentOriginApp(originAppId); if (!app) return; const primarySeed = app.primarySeed; @@ -176,12 +194,13 @@ export function* bulkUpdateLabelsWorker({ if (labelChanges.deleted) { deletedTagIds = labelChanges.deleted.map((label) => `${ref}${label.name}`); } - if (!(yield call(persistTagsForOrigin, originAppId, addedTags, deletedTagIds, scope))) return; + if (!(yield call(persistTagsForOrigin, originAppId, addedTags, deletedTagIds, scope, requestSession))) return; try { const backupResponse = yield call(checkBackupCondition, originAppId); if ( !backupResponse && currentOriginApp(originAppId) && + requestSession === currentBackupAccountSession() && (!scope || (scope.appId === originAppId && store.getState().settings.bitcoinNetworkType === scope.networkType)) @@ -196,7 +215,8 @@ export function* bulkUpdateLabelsWorker({ Relay.modifyLabels, originAppId, tagsToBackup.length ? tagsToBackup : [], - tagsToDelete.length ? tagsToDelete : [] + tagsToDelete.length ? tagsToDelete : [], + requestSession ); } else yield delay(100); } catch (error) { @@ -213,6 +233,7 @@ export function* bulkUpdateLabelsWorker({ export function* importLabelsWorker({ payload, originAppId, + originBackupAccountSession, }: { payload: { labels: [ @@ -225,9 +246,12 @@ export function* importLabelsWorker({ ]; }; originAppId?: string; + originBackupAccountSession?: number; }) { try { if (!originAppId) return; + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); + if (requestSession !== currentBackupAccountSession()) return; const app = currentOriginApp(originAppId); if (!app) return; const primarySeed = app.primarySeed; @@ -247,17 +271,17 @@ export function* importLabelsWorker({ })); } - if (!(yield call(persistTagsForOrigin, originAppId, addedTags))) return; + if (!(yield call(persistTagsForOrigin, originAppId, addedTags, [], undefined, requestSession))) return; try { const backupResponse = yield call(checkBackupCondition, originAppId); - if (!backupResponse && currentOriginApp(originAppId)) { + if (!backupResponse && currentOriginApp(originAppId) && requestSession === currentBackupAccountSession()) { const encryptionKey = generateEncryptionKey(primarySeed); const tagsToBackup = addedTags.map((tag) => ({ id: hash256(hash256(encryptionKey + tag.id)), content: encrypt(encryptionKey, JSON.stringify(tag)), })); - yield call(Relay.modifyLabels, originAppId, tagsToBackup.length ? tagsToBackup : [], []); + yield call(Relay.modifyLabels, originAppId, tagsToBackup.length ? tagsToBackup : [], [], requestSession); } else yield delay(100); } catch (error) { yield call(setServerBackupFailed, originAppId); @@ -277,6 +301,7 @@ export const importLabelsWatcher = createWatcher(importLabelsWorker, IMPORT_LABE export function* markUTXOSpendabilityWorker({ payload, originAppId, + originBackupAccountSession, }: { payload: { wallet: any; @@ -285,9 +310,12 @@ export function* markUTXOSpendabilityWorker({ spendability: UTXOSpendability; }; originAppId?: string; + originBackupAccountSession?: number; }) { try { if (!originAppId) return; + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); + if (requestSession !== currentBackupAccountSession()) return; if (!currentOriginApp(originAppId)) return; const { wallet, txId, vout, spendability } = payload; @@ -317,7 +345,7 @@ export function* markUTXOSpendabilityWorker({ } if (!changed) return; - const persisted = yield call(persistSpendabilityForOrigin, originAppId, schema, wallet.id, specs); + const persisted = yield call(persistSpendabilityForOrigin, originAppId, schema, wallet.id, specs, requestSession); if (persisted !== true) return; // Persist the user's choice first, then invalidate/refresh Assisted Server // Backup through its normal opt-in/offline-aware incremental path. @@ -327,10 +355,12 @@ export function* markUTXOSpendabilityWorker({ ? yield call(updateVaultImageWorker, { payload: { vault: walletJSON as Vault, isUpdate: true }, originAppId, + originBackupAccountSession: requestSession, }) : yield call(updateAppImageWorker, { payload: { wallets: [walletJSON as Wallet] }, originAppId, + originBackupAccountSession: requestSession, }); if (!response?.updated) yield call(setServerBackupFailed, originAppId); } catch { diff --git a/src/store/sagas/wallets.ts b/src/store/sagas/wallets.ts index f3cac9a43b..715848326b 100644 --- a/src/store/sagas/wallets.ts +++ b/src/store/sagas/wallets.ts @@ -42,6 +42,7 @@ import { Alert } from 'react-native'; import { KeeperApp } from 'src/models/interfaces/KeeperApp'; import { RealmSchema } from 'src/storage/realm/enum'; import Relay from 'src/services/backend/Relay'; +import { currentBackupAccountSession } from 'src/services/backup/transport'; import SigningServer from 'src/services/backend/SigningServer'; import WalletOperations from 'src/services/wallets/operations'; import WalletUtilities from 'src/services/wallets/operations/utils'; @@ -156,11 +157,12 @@ const currentWalletAccountId = (): string | undefined => { return undefined; } }; -const assertWalletAccount = (appId: string) => { +const assertWalletAccount = (appId: string, expectedSession?: number) => { if ( !appId || currentWalletAccountId() !== appId || - store.getState().storage.appId !== appId + store.getState().storage.appId !== appId || + (expectedSession !== undefined && currentBackupAccountSession() !== expectedSession) ) throw new Error('Account changed'); }; @@ -236,16 +238,19 @@ function* addNewWallet( export function* addNewWalletsWorker({ payload: newWalletInfo, originAppId, + originBackupAccountSession, }: { payload: NewWalletInfo[]; originAppId?: string; + originBackupAccountSession?: number; }) { let sourceAppId = originAppId; + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); try { const wallets: Wallet[] = []; const app: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); sourceAppId = originAppId || app?.id; - assertWalletAccount(sourceAppId); + assertWalletAccount(sourceAppId, requestSession); for (const { walletType, walletDetails, importDetails } of newWalletInfo) { const wallet: Wallet = yield call( @@ -255,7 +260,7 @@ export function* addNewWalletsWorker({ app, importDetails ); - assertWalletAccount(sourceAppId); + assertWalletAccount(sourceAppId, requestSession); wallets.push(wallet); } @@ -264,9 +269,10 @@ export function* addNewWalletsWorker({ const response = yield call(updateAppImageWorker, { payload: { wallets }, originAppId: sourceAppId, + originBackupAccountSession: requestSession, }); if (response.updated) { - assertWalletAccount(sourceAppId); + assertWalletAccount(sourceAppId, requestSession); yield call(dbManager.createObjectBulk, RealmSchema.Wallet, wallets); yield put(relayWalletUpdateSuccess()); return true; @@ -279,7 +285,9 @@ export function* addNewWalletsWorker({ } } catch (err) { console.log(err); - if (currentWalletAccountId() === sourceAppId && store.getState().storage.appId === sourceAppId) + if (currentWalletAccountId() === sourceAppId && + store.getState().storage.appId === sourceAppId && + currentBackupAccountSession() === requestSession) yield put(relayWalletUpdateFail(err.message ? err.message : err.toString())); return false; } @@ -299,17 +307,20 @@ export interface NewVaultInfo { export function* addNewVaultWorker({ payload, originAppId, + originBackupAccountSession, }: { originAppId?: string; + originBackupAccountSession?: number; payload: { newVaultInfo?: NewVaultInfo; isMigrated?: boolean; oldVaultId?: string; }; }) { + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); try { const sourceAppId = originAppId || currentWalletAccountId(); - assertWalletAccount(sourceAppId); + assertWalletAccount(sourceAppId, requestSession); const { newVaultInfo, isMigrated, oldVaultId } = payload; const { bitcoinNetworkType: networkType } = yield select((state: RootState) => state.settings); @@ -342,9 +353,10 @@ export function* addNewVaultWorker({ const newVaultResponse = yield call(updateVaultImageWorker, { payload: { vault }, originAppId: sourceAppId, + originBackupAccountSession: requestSession, }); if (newVaultResponse.updated) { - assertWalletAccount(sourceAppId); + assertWalletAccount(sourceAppId, requestSession); yield call(dbManager.createObject, RealmSchema.Vault, vault); yield put(uaiChecks([uaiType.SECURE_VAULT])); @@ -359,6 +371,7 @@ export function* addNewVaultWorker({ }; const archivedVaultresponse = yield call(updateVaultImageWorker, { originAppId: sourceAppId, + originBackupAccountSession: requestSession, payload: { vault: { ...oldVault, @@ -367,7 +380,7 @@ export function* addNewVaultWorker({ }, }); if (archivedVaultresponse.updated) { - assertWalletAccount(sourceAppId); + assertWalletAccount(sourceAppId, requestSession); yield call(dbManager.updateObjectById, RealmSchema.Vault, oldVaultId, updatedParams); yield put(initiateVaultMigration({ intrimVault: vault })); } @@ -404,17 +417,24 @@ export function* addSigningDeviceWorker({ payload: { signers }, callback, originAppId, + originBackupAccountSession, }: { payload: { signers: Signer[] }; callback?: () => void; originAppId?: string; + originBackupAccountSession?: number; }) { if (!signers.length) return; const sourceAppId = originAppId || currentWalletAccountId(); - assertWalletAccount(sourceAppId); + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); + assertWalletAccount(sourceAppId, requestSession); for (let i = 0; i < signers.length; i++) { const signer = signers[i]; - yield call(mergeSimilarKeysWorker, { payload: { signer }, originAppId: sourceAppId }); + yield call(mergeSimilarKeysWorker, { + payload: { signer }, + originAppId: sourceAppId, + originBackupAccountSession: requestSession, + }); } try { const { bitcoinNetworkType } = yield select((state: RootState) => state.settings); @@ -539,9 +559,10 @@ export function* addSigningDeviceWorker({ const response = yield call(updateAppImageWorker, { payload: { signers: signersToUpdate }, originAppId: sourceAppId, + originBackupAccountSession: requestSession, }); if (response.updated) { - assertWalletAccount(sourceAppId); + assertWalletAccount(sourceAppId, requestSession); yield call( dbManager.createObjectBulk, RealmSchema.Signer, @@ -555,7 +576,11 @@ export function* addSigningDeviceWorker({ (signersToUpdate[0]?.extraData?.instanceNumber !== 1 && !signersToUpdate[0]?.hidden) ) ); - yield call(updateVaultSignerXprivWorker, { signers, originAppId: sourceAppId }); + yield call(updateVaultSignerXprivWorker, { + signers, + originAppId: sourceAppId, + originBackupAccountSession: requestSession, + }); } else { const errorMsg = response.error?.message ? response.error.message.toString() @@ -577,13 +602,16 @@ export const addSigningDeviceWatcher = createWatcher(addSigningDeviceWorker, ADD function* deleteSigningDeviceWorker({ payload: { signers }, originAppId, + originBackupAccountSession, }: { payload: { signers: Signer[] }; originAppId?: string; + originBackupAccountSession?: number; }) { const sourceAppId = originAppId || currentWalletAccountId(); + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); try { - assertWalletAccount(sourceAppId); + assertWalletAccount(sourceAppId, requestSession); if (signers.length) { yield put(showDeletingKeyModal()); const signersToDeleteIds = []; @@ -592,15 +620,16 @@ function* deleteSigningDeviceWorker({ } const response = yield call(deleteAppImageEntityWorker, { originAppId: sourceAppId, + originBackupAccountSession: requestSession, payload: { signerIds: signersToDeleteIds }, }); if (!response?.updated) { - assertWalletAccount(sourceAppId); + assertWalletAccount(sourceAppId, requestSession); yield put(hideDeletingKeyModal()); yield put(relaySignersUpdateFail('An error occurred while deleting signers.')); return false; } - assertWalletAccount(sourceAppId); + assertWalletAccount(sourceAppId, requestSession); yield put(uaiChecks([uaiType.SIGNING_DEVICES_HEALTH_CHECK])); yield put(hideDeletingKeyModal()); yield put(showKeyDeletedSuccessModal()); @@ -625,13 +654,16 @@ export const deleteSigningDeviceWatcher = createWatcher( function* archiveSigningDeviceWorker({ payload: { signers }, originAppId, + originBackupAccountSession, }: { payload: { signers: Signer[] }; originAppId?: string; + originBackupAccountSession?: number; }) { try { const sourceAppId = originAppId || currentWalletAccountId(); - assertWalletAccount(sourceAppId); + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); + assertWalletAccount(sourceAppId, requestSession); const signersToArchiveIds = []; for (const signer of signers) { signersToArchiveIds.push(getKeyUID(signer)); @@ -640,6 +672,7 @@ function* archiveSigningDeviceWorker({ for (let i = 0; i < signers.length; i++) { yield call(updateSignerDetailsWorker, { originAppId: sourceAppId, + originBackupAccountSession: requestSession, payload: { signer: signers[i], key: 'archived', @@ -663,16 +696,20 @@ export const archiveSigningDeviceWatcher = createWatcher( function* migrateVaultWorker({ payload, originAppId, + originBackupAccountSession, }: { payload: { newVaultInfo: NewVaultInfo; oldVaultId: string }; originAppId?: string; + originBackupAccountSession?: number; }) { try { const sourceAppId = originAppId || currentWalletAccountId(); - assertWalletAccount(sourceAppId); + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); + assertWalletAccount(sourceAppId, requestSession); const { newVaultInfo, oldVaultId } = payload; const migrated = yield call(addNewVaultWorker, { originAppId: sourceAppId, + originBackupAccountSession: requestSession, payload: { newVaultInfo, isMigrated: true, oldVaultId }, }); const migratedVault = yield select((state: RootState) => state.vault.intrimVault); @@ -701,8 +738,10 @@ export const migrateVaultWatcher = createWatcher(migrateVaultWorker, MIGRATE_VAU function* refreshWalletsWorker({ payload, originAppId, + originBackupAccountSession, }: { originAppId?: string; + originBackupAccountSession?: number; payload: { wallets: (Wallet | Vault)[]; options: { hardRefresh?: boolean; addNotifications?: boolean; dustScan?: boolean }; @@ -710,13 +749,15 @@ function* refreshWalletsWorker({ }; }) { const sourceAppId = originAppId || currentWalletAccountId(); + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); let { wallets, options, requestId } = payload; let succeeded = false; let lease; const isSourceAccountCurrent = () => !!sourceAppId && currentWalletAccountId() === sourceAppId && - store.getState().storage.appId === sourceAppId; + store.getState().storage.appId === sourceAppId && + currentBackupAccountSession() === requestSession; // Filter out pre-mix, post-mix, and bad bank wallets as they are no longer displayed or used. wallets = (wallets || []).filter((wallet) => { @@ -734,7 +775,8 @@ function* refreshWalletsWorker({ if ( !sourceAppId || currentWalletAccountId() !== sourceAppId || - store.getState().storage.appId !== sourceAppId + store.getState().storage.appId !== sourceAppId || + currentBackupAccountSession() !== requestSession ) throw new Error(WALLET_SYNC_SCOPE_CHANGED); const refreshScope = yield select((state: RootState) => ({ @@ -753,7 +795,8 @@ function* refreshWalletsWorker({ if ( currentState.storage.appId !== refreshScope.appId || currentState.settings.bitcoinNetworkType !== refreshScope.networkType || - keeperApp?.id !== refreshScope.appId + keeperApp?.id !== refreshScope.appId || + currentBackupAccountSession() !== requestSession ) throw new Error(WALLET_SYNC_SCOPE_CHANGED); }; @@ -775,7 +818,8 @@ function* refreshWalletsWorker({ })); if ( readyScope.appId !== refreshScope.appId || - readyScope.networkType !== refreshScope.networkType + readyScope.networkType !== refreshScope.networkType || + currentBackupAccountSession() !== requestSession ) return false; @@ -904,6 +948,7 @@ function* refreshWalletsWorker({ assertCurrentScope(); yield call(bulkUpdateLabelsWorker, { originAppId: sourceAppId, + originBackupAccountSession: requestSession, payload: { labelChanges, UTXO: utxo, @@ -1130,8 +1175,10 @@ export const refreshWalletsWatcher = createWatcher(refreshWalletsWorker, REFRESH export function* autoWalletsSyncWorker({ payload, originAppId, + originBackupAccountSession, }: { originAppId?: string; + originBackupAccountSession?: number; payload: { syncAll?: boolean; hardRefresh?: boolean; @@ -1142,6 +1189,7 @@ export function* autoWalletsSyncWorker({ }; }) { const sourceAppId = originAppId || currentWalletAccountId(); + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); const { syncAll, hardRefresh, addNotifications, backupCheckAppId, archivedOnly, requestId } = payload; let activeSucceeded = true; @@ -1149,7 +1197,8 @@ export function* autoWalletsSyncWorker({ const isSourceAccountCurrent = () => !!sourceAppId && currentWalletAccountId() === sourceAppId && - store.getState().storage.appId === sourceAppId; + store.getState().storage.appId === sourceAppId && + currentBackupAccountSession() === requestSession; try { if (!isSourceAccountCurrent()) { activeSucceeded = false; @@ -1200,6 +1249,7 @@ export function* autoWalletsSyncWorker({ if (active.length) { activeSucceeded = yield call(refreshWalletsWorker, { originAppId: sourceAppId, + originBackupAccountSession: requestSession, payload: { wallets: active.map((wallet) => getJSONFromRealmObject(wallet) as unknown as Wallet | Vault), options: { hardRefresh, addNotifications }, @@ -1224,6 +1274,7 @@ export function* autoWalletsSyncWorker({ if (archived.length) { archivedSucceeded = yield call(refreshWalletsWorker, { originAppId: sourceAppId, + originBackupAccountSession: requestSession, payload: { wallets: archived.map( (wallet) => getJSONFromRealmObject(wallet) as unknown as Wallet | Vault @@ -1252,7 +1303,10 @@ export function* autoWalletsSyncWorker({ isAutomaticCloudBackupEnabled({ automaticCloudBackupByAppId }, sourceAppId) && (pendingAllBackupByAppId?.[sourceAppId] || !backupRepairCompletedByAppId[sourceAppId]) ) { - yield put(checkBackupFreshness(sourceAppId)); + yield put({ + ...checkBackupFreshness(sourceAppId), + originBackupAccountSession: requestSession, + }); } } } catch (err) { @@ -1260,7 +1314,7 @@ export function* autoWalletsSyncWorker({ archivedSucceeded = false; captureError(err); } finally { - if (requestId) { + if (requestId && isSourceAccountCurrent()) { yield put( finishRefreshRequest({ requestId, @@ -1276,6 +1330,7 @@ export const autoWalletsSyncWatcher = createWatcher(autoWalletsSyncWorker, AUTO_ export function* updateSignerPolicyWorker({ payload, originAppId, + originBackupAccountSession, }: { payload: { signer: Signer; @@ -1287,11 +1342,13 @@ export function* updateSignerPolicyWorker({ verificationToken: number; }; originAppId?: string; + originBackupAccountSession?: number; }) { const sourceAppId = originAppId || currentWalletAccountId(); + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); const { signer, signingKey, updates, verificationToken } = payload; try { - assertWalletAccount(sourceAppId); + assertWalletAccount(sourceAppId, requestSession); const fcmToken = yield select((state: RootState) => state.notifications.fcmToken); const { bitcoinNetwork } = yield select((state: RootState) => state.settings); const signerId = @@ -1311,7 +1368,7 @@ export function* updateSignerPolicyWorker({ updates, fcmToken ); - assertWalletAccount(sourceAppId); + assertWalletAccount(sourceAppId, requestSession); if (delayedPolicyUpdate) { yield put(updateDelayedPolicyUpdate(delayedPolicyUpdate)); @@ -1334,10 +1391,12 @@ export function* updateSignerPolicyWorker({ } ); } - assertWalletAccount(sourceAppId); + assertWalletAccount(sourceAppId, requestSession); yield put(setSignerPolicyError('success')); } catch (err) { - if (currentWalletAccountId() === sourceAppId && store.getState().storage.appId === sourceAppId) + if (currentWalletAccountId() === sourceAppId && + store.getState().storage.appId === sourceAppId && + currentBackupAccountSession() === requestSession) yield put(setSignerPolicyError('failure')); } } @@ -1347,7 +1406,8 @@ export const updateSignerPolicyWatcher = createWatcher( UPDATE_SIGNER_POLICY ); -export function* testcoinsWorker({ payload, originAppId }) { +export function* testcoinsWorker({ payload, originAppId, originBackupAccountSession }) { + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); let { wallet } = payload; const faucetScope = yield select((state: RootState) => ({ appId: state.storage.appId, @@ -1362,7 +1422,8 @@ export function* testcoinsWorker({ payload, originAppId }) { state.storage.appId === faucetScope.appId && state.settings.bitcoinNetworkType === faucetScope.networkType && faucetScope.networkType === wallet.networkType && - keeperApp?.id === faucetScope.appId + keeperApp?.id === faucetScope.appId && + currentBackupAccountSession() === requestSession ); } catch { return false; @@ -1451,6 +1512,7 @@ export function* testcoinsWorker({ payload, originAppId }) { if (txids.some((seenTxid: string) => seenTxid.toLowerCase() === txid.toLowerCase())) { const refreshed = yield call(refreshWalletsWorker, { originAppId: faucetScope.appId, + originBackupAccountSession: requestSession, payload: { wallets: [wallet], options: { hardRefresh: true } }, }); if (refreshed) { @@ -1477,8 +1539,9 @@ export function* testcoinsWorker({ payload, originAppId }) { export const testcoinsWatcher = createWatcher(testcoinsWorker, TEST_SATS_RECIEVE); -function* updateWalletDetailsWorker({ payload, originAppId }) { +function* updateWalletDetailsWorker({ payload, originAppId, originBackupAccountSession }) { const sourceAppId = originAppId || currentWalletAccountId(); + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); const { wallet, details, @@ -1490,7 +1553,7 @@ function* updateWalletDetailsWorker({ payload, originAppId }) { }; } = payload; try { - assertWalletAccount(sourceAppId); + assertWalletAccount(sourceAppId, requestSession); const presentationData: WalletPresentationData = { name: details.name, description: details.description, @@ -1502,9 +1565,10 @@ function* updateWalletDetailsWorker({ payload, originAppId }) { const response = yield call(updateAppImageWorker, { payload: { wallets: [wallet] }, originAppId: sourceAppId, + originBackupAccountSession: requestSession, }); if (response.updated) { - assertWalletAccount(sourceAppId); + assertWalletAccount(sourceAppId, requestSession); yield call(dbManager.updateObjectById, RealmSchema.Wallet, wallet.id, { presentationData, }); @@ -1525,8 +1589,9 @@ export const updateWalletDetailWatcher = createWatcher( UPDATE_WALLET_DETAILS ); -function* updateVaultDetailsWorker({ payload, originAppId }) { +function* updateVaultDetailsWorker({ payload, originAppId, originBackupAccountSession }) { const sourceAppId = originAppId || currentWalletAccountId(); + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); const { vault, details, @@ -1538,7 +1603,7 @@ function* updateVaultDetailsWorker({ payload, originAppId }) { }; } = payload; try { - assertWalletAccount(sourceAppId); + assertWalletAccount(sourceAppId, requestSession); const presentationData: VaultPresentationData = { name: details.name, description: details.description, @@ -1551,9 +1616,10 @@ function* updateVaultDetailsWorker({ payload, originAppId }) { const response = yield call(updateVaultImageWorker, { payload: { vault }, originAppId: sourceAppId, + originBackupAccountSession: requestSession, }); if (response.updated) { - assertWalletAccount(sourceAppId); + assertWalletAccount(sourceAppId, requestSession); yield call(dbManager.updateObjectById, RealmSchema.Vault, vault.id, { presentationData, }); @@ -1575,8 +1641,9 @@ export const updateVaultDetailsWatcher = createWatcher( UPDATE_VAULT_DETAILS ); -export function* updateSignerDetailsWorker({ payload, originAppId }) { +export function* updateSignerDetailsWorker({ payload, originAppId, originBackupAccountSession }) { const sourceAppId = originAppId || currentWalletAccountId(); + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); const { signer, key, @@ -1588,14 +1655,15 @@ export function* updateSignerDetailsWorker({ payload, originAppId }) { } = payload; yield put(setRelaySignersUpdateLoading(true)); try { - assertWalletAccount(sourceAppId); + assertWalletAccount(sourceAppId, requestSession); signer[key] = value; const response = yield call(updateAppImageWorker, { payload: { signers: [signer] }, originAppId: sourceAppId, + originBackupAccountSession: requestSession, }); if (response.updated) { - assertWalletAccount(sourceAppId); + assertWalletAccount(sourceAppId, requestSession); const signerKeyUID = getKeyUID(signer); yield call( dbManager.updateObjectByQuery, @@ -1620,9 +1688,10 @@ export function* updateSignerDetailsWorker({ payload, originAppId }) { export const updateSignerDetails = createWatcher(updateSignerDetailsWorker, UPDATE_SIGNER_DETAILS); -function* updateKeyDetailsWorker({ payload, originAppId }) { +function* updateKeyDetailsWorker({ payload, originAppId, originBackupAccountSession }) { const sourceAppId = originAppId || currentWalletAccountId(); - assertWalletAccount(sourceAppId); + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); + assertWalletAccount(sourceAppId, requestSession); const { signer, key, @@ -1649,7 +1718,7 @@ function* updateKeyDetailsWorker({ payload, originAppId }) { updatedRegsteredVaults.push(value); } const saved = yield call(() => { - assertWalletAccount(sourceAppId); + assertWalletAccount(sourceAppId, requestSession); return dbManager.updateObjectByPrimaryId( RealmSchema.VaultSigner, 'xpub', @@ -1666,10 +1735,12 @@ function* updateKeyDetailsWorker({ payload, originAppId }) { try { const vaults: Vault[] = yield call(dbManager.getCollection, RealmSchema.Vault); for (const vault of vaults) { + assertWalletAccount(sourceAppId, requestSession); if (vault.signers.some((key) => key.xpub === signer.xpub)) { const response = yield call(updateVaultImageWorker, { payload: { vault, isUpdate: true }, originAppId: sourceAppId, + originBackupAccountSession: requestSession, }); if (!response?.updated) yield call(setServerBackupFailed, sourceAppId); } @@ -1681,7 +1752,7 @@ function* updateKeyDetailsWorker({ payload, originAppId }) { } const updated = yield call(() => { - assertWalletAccount(sourceAppId); + assertWalletAccount(sourceAppId, requestSession); return dbManager.updateObjectByPrimaryId(RealmSchema.VaultSigner, 'xpub', signer.xpub, { [key]: value, }); @@ -1691,11 +1762,12 @@ function* updateKeyDetailsWorker({ payload, originAppId }) { export const updateKeyDetails = createWatcher(updateKeyDetailsWorker, UPDATE_KEY_DETAILS); -function* deleteVaultWorker({ payload, originAppId }) { +function* deleteVaultWorker({ payload, originAppId, originBackupAccountSession }) { const sourceAppId = originAppId || currentWalletAccountId(); + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); const { vaultId } = payload; try { - assertWalletAccount(sourceAppId); + assertWalletAccount(sourceAppId, requestSession); yield put(setRelayVaultUpdateLoading(true)); const backupEnabled = yield select((state: RootState) => isAutomaticCloudBackupEnabled(state.bhr, sourceAppId) @@ -1703,15 +1775,16 @@ function* deleteVaultWorker({ payload, originAppId }) { const response = yield call(deleteVaultImageWorker, { payload: { vaultIds: [vaultId] }, originAppId: sourceAppId, + originBackupAccountSession: requestSession, }); if (response.updated) { - assertWalletAccount(sourceAppId); + assertWalletAccount(sourceAppId, requestSession); const deleted = yield call(dbManager.deleteObjectById, RealmSchema.Vault, vaultId); if (deleted !== true) { if (backupEnabled) yield call(setServerBackupFailed, sourceAppId); throw new Error('Local vault deletion failed'); } - assertWalletAccount(sourceAppId); + assertWalletAccount(sourceAppId, requestSession); yield put(relayVaultUpdateSuccess()); } else { const errorMsg = response.error?.message @@ -1727,11 +1800,12 @@ function* deleteVaultWorker({ payload, originAppId }) { export const deleteVaultyWatcher = createWatcher(deleteVaultWorker, DELETE_VAULT); -function* reinstateVaultWorker({ payload, originAppId }) { +function* reinstateVaultWorker({ payload, originAppId, originBackupAccountSession }) { const sourceAppId = originAppId || currentWalletAccountId(); + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); const { vaultId } = payload; try { - assertWalletAccount(sourceAppId); + assertWalletAccount(sourceAppId, requestSession); yield put(setRelayVaultUpdateLoading(true)); const vault: Vault = dbManager.getObjectById(RealmSchema.Vault, vaultId).toJSON(); const updatedParams = { @@ -1745,6 +1819,7 @@ function* reinstateVaultWorker({ payload, originAppId }) { }; const response = yield call(updateVaultImageWorker, { originAppId: sourceAppId, + originBackupAccountSession: requestSession, payload: { vault: { ...vault, @@ -1758,11 +1833,12 @@ function* reinstateVaultWorker({ payload, originAppId }) { signingDevices.forEach((signer) => (signerMap[getKeyUID(signer)] = signer)); if (response.updated) { - assertWalletAccount(sourceAppId); + assertWalletAccount(sourceAppId, requestSession); yield call(dbManager.updateObjectById, RealmSchema.Vault, vaultId, updatedParams); for (let i = 0; i < vault.signers.length; i++) { yield call(updateSignerDetailsWorker, { originAppId: sourceAppId, + originBackupAccountSession: requestSession, payload: { signer: signerMap[getKeyUID(vault.signers[i])], key: 'archived', @@ -1784,11 +1860,12 @@ function* reinstateVaultWorker({ payload, originAppId }) { export const reinstateVaultWatcher = createWatcher(reinstateVaultWorker, REINSTATE_VAULT); -function* refillMobileKeyWorker({ payload, originAppId }) { +function* refillMobileKeyWorker({ payload, originAppId, originBackupAccountSession }) { const sourceAppId = originAppId || currentWalletAccountId(); + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); const { vaultKey } = payload; try { - assertWalletAccount(sourceAppId); + assertWalletAccount(sourceAppId, requestSession); const { xpriv } = vaultKey; if (!xpriv) { const signer: Signer = dbManager @@ -1807,6 +1884,7 @@ function* refillMobileKeyWorker({ payload, originAppId }) { const { xpriv } = details.xpubDetails[XpubTypes.P2WSH]; yield call(updateKeyDetailsWorker, { originAppId: sourceAppId, + originBackupAccountSession: requestSession, payload: { signer: signer.signerXpubs[XpubTypes.P2WSH][0], key: 'xpriv', value: xpriv }, }); } @@ -1818,15 +1896,23 @@ function* refillMobileKeyWorker({ payload, originAppId }) { export const refillMobileKeyWatcher = createWatcher(refillMobileKeyWorker, REFILL_MOBILEKEY); -function* refreshCanaryWalletsWorker({ originAppId }: { originAppId?: string } = {}) { +function* refreshCanaryWalletsWorker({ + originAppId, + originBackupAccountSession, +}: { + originAppId?: string; + originBackupAccountSession?: number; +} = {}) { try { const sourceAppId = originAppId || currentWalletAccountId(); - assertWalletAccount(sourceAppId); + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); + assertWalletAccount(sourceAppId, requestSession); const vaults: Vault[] = yield call(dbManager.getCollection, RealmSchema.Vault); const canaryWallets = vaults.filter((vault) => vault.type === VaultType.CANARY); if (canaryWallets.length) { yield call(refreshWalletsWorker, { originAppId: sourceAppId, + originBackupAccountSession: requestSession, payload: { wallets: canaryWallets, options: { @@ -1848,13 +1934,16 @@ export const refreshCanaryWalletsWatcher = createWatcher( function* mergeSimilarKeysWorker({ payload, originAppId, + originBackupAccountSession, }: { payload: { signer: Signer }; originAppId?: string; + originBackupAccountSession?: number; }) { try { const sourceAppId = originAppId || currentWalletAccountId(); - assertWalletAccount(sourceAppId); + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); + assertWalletAccount(sourceAppId, requestSession); const { signer } = payload; const signers: Signer[] = yield call(dbManager.getCollection, RealmSchema.Signer); for (let i = 0; i < signers.length; i++) { @@ -1870,7 +1959,7 @@ function* mergeSimilarKeysWorker({ ) { const signerKeyUID = getKeyUID(s); yield call(() => { - assertWalletAccount(sourceAppId); + assertWalletAccount(sourceAppId, requestSession); return dbManager.updateObjectByQuery( RealmSchema.Signer, (realmSigner) => getKeyUID(realmSigner) === signerKeyUID, @@ -1885,7 +1974,7 @@ function* mergeSimilarKeysWorker({ true // get all matching objects ); for (let i = 0; i < keys.length; i++) { - assertWalletAccount(sourceAppId); + assertWalletAccount(sourceAppId, requestSession); yield call( dbManager.updateObjectByPrimaryId, RealmSchema.VaultSigner, @@ -1900,7 +1989,7 @@ function* mergeSimilarKeysWorker({ dbManager.getObjectByIndex, RealmSchema.KeeperApp ); - assertWalletAccount(sourceAppId); + assertWalletAccount(sourceAppId, requestSession); if (id !== sourceAppId) throw new Error('Account changed'); const encryptionKey = generateEncryptionKey(primarySeed); const encrytedSigner = encrypt( @@ -1913,7 +2002,7 @@ function* mergeSimilarKeysWorker({ newSignerId: signer.masterFingerprint, newSignerDetails: encrytedSigner, }, - ]); + ], requestSession); if (updated) { console.log( `Signer ${s.masterFingerprint} has been merged with ${signer.masterFingerprint}` @@ -1933,23 +2022,27 @@ export const mergeSimilarKeysWatcher = createWatcher(mergeSimilarKeysWorker, MER function* generateNewExternalAddressWorker({ payload, originAppId, + originBackupAccountSession, }: { payload: { wallet: Wallet | Vault; }; originAppId?: string; + originBackupAccountSession?: number; }) { const sourceAppId = originAppId || currentWalletAccountId(); + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); if ( !sourceAppId || currentWalletAccountId() !== sourceAppId || - store.getState().storage.appId !== sourceAppId + store.getState().storage.appId !== sourceAppId || + currentBackupAccountSession() !== requestSession ) return false; const { wallet } = payload; const specs = { ...wallet.specs, totalExternalAddresses: wallet.specs.totalExternalAddresses + 1 }; return yield call(() => { - assertWalletAccount(sourceAppId); + assertWalletAccount(sourceAppId, requestSession); return dbManager.updateObjectById( wallet.entityKind === EntityKind.VAULT ? RealmSchema.Vault : RealmSchema.Wallet, wallet.id, @@ -2035,9 +2128,10 @@ export const fetchCollaborativeChannelWatcher = createWatcher( FETCH_COLLABORATIVE_CHANNEL ); -function* updateVaultSignerXprivWorker({ signers, originAppId }) { +function* updateVaultSignerXprivWorker({ signers, originAppId, originBackupAccountSession }) { const sourceAppId = originAppId || currentWalletAccountId(); - assertWalletAccount(sourceAppId); + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); + assertWalletAccount(sourceAppId, requestSession); const { bitcoinNetworkType } = yield select((state: RootState) => state.settings); for (let i = 0; i < signers.length; i++) { const signer = signers[i]; @@ -2071,7 +2165,7 @@ function* updateVaultSignerXprivWorker({ signers, originAppId }) { : null; yield call( () => { - assertWalletAccount(sourceAppId); + assertWalletAccount(sourceAppId, requestSession); return dbManager.updateObjectByPrimaryId( RealmSchema.VaultSigner, 'xpub', diff --git a/src/store/store.ts b/src/store/store.ts index 5807aa4bd3..7c596cbeba 100644 --- a/src/store/store.ts +++ b/src/store/store.ts @@ -112,6 +112,11 @@ const accountOwnedActions = new Set([ 'DISCARD_BROADCASTED_TNX', 'GENERATE_NEW_ADDRESS', 'UPDATE_SIGNER_POLICY', + 'CHECK_BACKUP_FRESHNESS', + 'REPAIR_BACKUP', + 'VALIDATE_SERVER_BACKUP', + 'UPDATE_VERSION_HISTORY', + 'MIGRATE_LABELS_329', ]); const accountOwnedUtxoUiActions = new Set([ 'utxos/setSyncingUTXOs', @@ -151,7 +156,13 @@ const bindActionOrigin = (api) => (next) => (action) => { !('originAppId' in action) ) action = { ...action, originAppId }; - if (action?.type === 'UPDATE_APP_IMAGE') + if ( + action && + (accountOwnedActions.has(action.type) || + action.type === 'DELETE_BACKUP' || + action.type === 'BACKUP_ALL_SIGNERS_AND_VAULTS') && + !('originBackupAccountSession' in action) + ) action = { ...action, originBackupAccountSession: currentBackupAccountSession() }; return next(action); }; diff --git a/tests/recovery/accountBinding.test.cjs b/tests/recovery/accountBinding.test.cjs index 5a207b16f4..c3ebdfdb25 100644 --- a/tests/recovery/accountBinding.test.cjs +++ b/tests/recovery/accountBinding.test.cjs @@ -128,6 +128,14 @@ test('dispatch gate drops stale enable and success actions while preserving acco session += 2; middleware({ type: 'UPDATE_APP_IMAGE', payload: {}, originAppId: 'A' }); assert.equal(seen.at(-1).originBackupAccountSession, 3); + middleware({ type: 'UPDATE_APP_IMAGE', payload: {}, originAppId: 'A', + originBackupAccountSession: 1 }); + assert.equal(seen.at(-1).originBackupAccountSession, 1, + 'redispatch must retain the original session instead of adopting the new A session'); + middleware({ type: 'DELETE_BACKUP', appId: 'A' }); + assert.equal(seen.at(-1).originBackupAccountSession, 3); + middleware({ type: 'UPDATE_VERSION_HISTORY', originAppId: 'A' }); + assert.equal(seen.at(-1).originBackupAccountSession, 3); }); test('A to opted-in B during connectivity wait blocks upload and preserves B local wallet', async () => { @@ -246,6 +254,103 @@ test('incremental worker rejects an A payload prepared before A to B to A', asyn assert.equal(f.calls.length, 0); }); +test('wallet creation paused before backup cannot upload a stale A wallet after A to B to A', async () => { + const f = fixture(); + const originAppId = f.app.id; + let session = 1; + f.scope.currentBackupAccountSession = () => session; + let finishCreation; + f.scope.addNewWallet = () => new Promise((resolve) => { finishCreation = resolve; }); + const creating = f.run('addNewWalletsWorker', { + originAppId, + originBackupAccountSession: session, + payload: [{ walletType: enums.WalletType.DEFAULT, walletDetails: {} }], + }); + for (let i = 0; i < 20 && !finishCreation; i++) await new Promise(setImmediate); + assert.ok(finishCreation); + f.app.id = 'B'; f.state.storage.appId = 'B'; session++; + f.app.id = originAppId; f.state.storage.appId = originAppId; session++; + f.remote.wallets.shared = 'newer-A-ciphertext'; + finishCreation(wallet('shared', enums.NetworkType.MAINNET)); + assert.equal(await creating, false); + assert.equal(f.remote.wallets.shared, 'newer-A-ciphertext'); + assert.equal(f.calls.length, 0); + assert.equal(f.collections.Wallet.length, 0); +}); + +test('vault update paused at connectivity cannot upload stale A vault after A to B to A', async () => { + const f = fixture(); + const originAppId = f.app.id; + let session = 1; + f.scope.currentBackupAccountSession = () => session; + let finishConnectivity; + f.scope.NetInfo.fetch = () => new Promise((resolve) => { finishConnectivity = resolve; }); + const updating = f.run('updateVaultImageWorker', { + originAppId, + originBackupAccountSession: session, + payload: { vault: { id: 'shared', signers: [], archived: false } }, + }); + for (let i = 0; i < 20 && !finishConnectivity; i++) await new Promise(setImmediate); + assert.ok(finishConnectivity); + f.app.id = 'B'; f.state.storage.appId = 'B'; session++; + f.app.id = originAppId; f.state.storage.appId = originAppId; session++; + finishConnectivity({ isConnected: true }); + assert.equal((await updating).updated, false); + assert.equal(f.calls.length, 0); +}); + +test('queued session rejection cannot report a successful incremental backup', async () => { + const f = fixture(); + const originAppId = f.app.id; + let session = 1; + f.scope.currentBackupAccountSession = () => session; + let rejectQueued; + f.scope.Relay.updateAppImage = () => new Promise((_resolve, reject) => { rejectQueued = reject; }); + const updating = f.run('updateAppImageWorker', { + originAppId, + originBackupAccountSession: session, + payload: { wallets: [wallet('shared', enums.NetworkType.MAINNET)] }, + }); + for (let i = 0; i < 20 && !rejectQueued; i++) await new Promise(setImmediate); + assert.ok(rejectQueued); + f.app.id = 'B'; f.state.storage.appId = 'B'; session++; + f.app.id = originAppId; f.state.storage.appId = originAppId; session++; + rejectQueued(new Error('Backup account session changed')); + assert.equal((await updating).updated, false); +}); + +test('vault signer xpriv refresh cannot write an old key after A to B to A', async () => { + const f = fixture(); + const originAppId = f.app.id; + let session = 1; + f.scope.currentBackupAccountSession = () => session; + const key = signer('AABBCCDD', enums.NetworkType.MAINNET, enums.SignerType.SEED_WORDS); + key.id = 'AABBCCDD0M'; + key.signerXpubs[enums.XpubTypes.P2WPKH][0].xpriv = 'old-secret'; + key.signerXpubs[enums.XpubTypes.P2WSH] = [{ xpub: 'vault-xpub', xpriv: 'old-secret' }]; + let finishVaultRead; + const originalRead = f.scope.dbManager.getObjectByIndex; + f.scope.dbManager.getObjectByIndex = (schema, ...args) => schema === 'Vault' + ? new Promise((resolve) => { finishVaultRead = resolve; }) + : originalRead(schema, ...args); + f.scope.WalletUtilities.getSignerPurposeFromPath = () => enums.DerivationPurpose.BIP84; + let writes = 0; + f.scope.dbManager.updateObjectByPrimaryId = () => { writes++; return true; }; + const updating = f.run('updateVaultSignerXprivWorker', { + signers: [key], originAppId, originBackupAccountSession: session, + }); + for (let i = 0; i < 20 && !finishVaultRead; i++) await new Promise(setImmediate); + assert.ok(finishVaultRead); + f.app.id = 'B'; f.state.storage.appId = 'B'; session++; + f.app.id = originAppId; f.state.storage.appId = originAppId; session++; + finishVaultRead([{ networkType: enums.NetworkType.MAINNET, + signers: [{ ...key.signerXpubs[enums.XpubTypes.P2WSH][0], + id: key.id, masterFingerprint: key.masterFingerprint, derivationPath: "m/84'/0'/0'" }], + }]); + await assert.rejects(updating, /Account changed/); + assert.equal(writes, 0); +}); + test('per-account consent starts off, survives switches, and legacy consent needs attribution', () => { const reducer = loadModule('src/store/reducers/bhr.ts', { 'src/models/enums/BHR': loadModule('src/models/enums/BHR.ts'), diff --git a/tests/recovery/accountSessionDestructive.test.cjs b/tests/recovery/accountSessionDestructive.test.cjs new file mode 100644 index 0000000000..2c3c4a096f --- /dev/null +++ b/tests/recovery/accountSessionDestructive.test.cjs @@ -0,0 +1,191 @@ +const assert = require('node:assert/strict'); +const { test } = require('node:test'); +const { fixture, enums, wallet } = require('./sagaHarness.cjs'); +const { loadModule } = require('./helpers.cjs'); + +async function waitForPause(getResume) { + for (let attempt = 0; attempt < 20 && !getResume(); attempt++) await new Promise(setImmediate); + assert.equal(typeof getResume(), 'function', 'the action must pause before its backup write'); +} + +function accountRoundTrip(fixtureState, originalAppId, advanceSession) { + fixtureState.app.id = 'other-disposable-account'; + fixtureState.state.storage.appId = fixtureState.app.id; + advanceSession(); + fixtureState.app.id = originalAppId; + fixtureState.state.storage.appId = originalAppId; + advanceSession(); +} + +test('wallet deletion paused at connectivity cannot delete newer A data after A to B to A', async () => { + const f = fixture(); + const originAppId = f.app.id; + let session = 1; + f.scope.currentBackupAccountSession = () => session; + const walletId = 'disposable-wallet'; + f.collections.Wallet.push(wallet(walletId, enums.NetworkType.MAINNET)); + f.remote.wallets[walletId] = 'initial-A-ciphertext'; + let resumeConnectivity; + f.scope.NetInfo.fetch = () => new Promise((resolve) => { resumeConnectivity = resolve; }); + const relayDeletes = []; + f.scope.Relay.deleteAppImageEntity = async ({ appId, walletIds }) => { + relayDeletes.push({ appId, walletIds }); + walletIds.forEach((id) => delete f.remote.wallets[id]); + return { updated: true }; + }; + + const deleting = f.run('deleteAppImageEntityWorker', { + originAppId, + originBackupAccountSession: session, + payload: { walletIds: [walletId] }, + }); + await waitForPause(() => resumeConnectivity); + accountRoundTrip(f, originAppId, () => { session++; }); + f.remote.wallets[walletId] = 'newer-A-ciphertext'; + resumeConnectivity({ isConnected: true }); + + assert.equal((await deleting).updated, false); + assert.deepEqual(relayDeletes, []); + assert.equal(f.remote.wallets[walletId], 'newer-A-ciphertext'); + assert.deepEqual(f.collections.Wallet.map(({ id }) => id), [walletId]); +}); + +test('vault deletion paused at connectivity cannot delete newer A data after A to B to A', async () => { + const f = fixture(); + const originAppId = f.app.id; + let session = 1; + f.scope.currentBackupAccountSession = () => session; + const vaultId = 'disposable-vault'; + const remoteVaults = { [vaultId]: 'initial-A-ciphertext' }; + let resumeConnectivity; + f.scope.NetInfo.fetch = () => new Promise((resolve) => { resumeConnectivity = resolve; }); + const relayDeletes = []; + f.scope.Relay.deleteVaultImage = async ({ appId, vaults }) => { + relayDeletes.push({ appId, vaults }); + vaults.forEach((id) => delete remoteVaults[id]); + return { updated: true }; + }; + + const deleting = f.run('deleteVaultImageWorker', { + originAppId, + originBackupAccountSession: session, + payload: { vaultIds: [vaultId] }, + }); + await waitForPause(() => resumeConnectivity); + accountRoundTrip(f, originAppId, () => { session++; }); + remoteVaults[vaultId] = 'newer-A-ciphertext'; + resumeConnectivity({ isConnected: true }); + + assert.equal((await deleting).updated, false); + assert.deepEqual(relayDeletes, []); + assert.equal(remoteVaults[vaultId], 'newer-A-ciphertext'); +}); + +test('whole-backup deletion paused before request cannot clear newer A data after A to B to A', async () => { + const f = fixture(); + const originAppId = f.app.id; + let session = 1; + f.scope.currentBackupAccountSession = () => session; + f.remote.wallets.shared = 'initial-A-ciphertext'; + const originalRead = f.scope.dbManager.getObjectByIndex; + let appReads = 0; + let resumePreflight; + f.scope.dbManager.getObjectByIndex = (schema, ...args) => { + if (schema === 'KeeperApp' && ++appReads === 2) + return new Promise((resolve) => { resumePreflight = () => resolve(f.app); }); + return originalRead(schema, ...args); + }; + const relayDeletes = []; + f.scope.Relay.deleteBackup = async ({ appId }) => { + relayDeletes.push(appId); + f.remote.wallets = {}; + return { updated: true }; + }; + + const deleting = f.run('deleteBackupWorker', { + appId: originAppId, + originBackupAccountSession: session, + }); + await waitForPause(() => resumePreflight); + accountRoundTrip(f, originAppId, () => { session++; }); + f.remote.wallets.shared = 'newer-A-ciphertext'; + resumePreflight(); + + assert.equal(await deleting, false); + assert.deepEqual(relayDeletes, []); + assert.equal(f.remote.wallets.shared, 'newer-A-ciphertext'); + assert.ok(!f.actions.some((action) => + action.type === 'setDeleteBackupSuccess' && action.payload?.status === true)); +}); + +test('queued explicit whole-backup delete cannot clear newer A data after A to B to A', async () => { + let active = 'A'; + const remote = { wallets: { shared: 'initial-A-ciphertext' } }; + const requests = []; + const transport = loadModule('src/services/backup/transport.ts', { + '../rest/RestClient': { post: async (path, body) => { + requests.push({ path, body }); + remote.wallets = {}; + return { data: { updated: true } }; + } }, + }); + transport.setBackupUploadGuard((appId, explicitChoice) => appId === active && explicitChoice); + transport.observeBackupAccount(active); + let release; + const busy = transport.withBackupSession('A', () => new Promise((resolve) => { release = resolve; })); + await waitForPause(() => release); + + const deleting = transport.backupPost('fixture/deleteBackup', { appId: 'A' }, true); + active = 'B'; + transport.observeBackupAccount(active); + remote.wallets.shared = 'newer-A-ciphertext'; + active = 'A'; + transport.observeBackupAccount(active); + release(); + await busy; + + await assert.rejects(deleting, /session changed/); + assert.deepEqual(requests, []); + assert.equal(remote.wallets.shared, 'newer-A-ciphertext'); +}); + +test('in-flight whole-backup deletion after A to B to A marks A for readback', async () => { + const f = fixture(); + const originAppId = f.app.id; + let session = 1; + f.scope.currentBackupAccountSession = () => session; + let finishDelete; + f.scope.Relay.deleteBackup = () => new Promise((resolve) => { finishDelete = resolve; }); + const deleting = f.run('deleteBackupWorker', { + appId: originAppId, originBackupAccountSession: session, + }); + await waitForPause(() => finishDelete); + accountRoundTrip(f, originAppId, () => { session++; }); + finishDelete({ updated: true }); + assert.equal(await deleting, false); + assert.equal(f.state.bhr.pendingAllBackupByAppId[originAppId], true); + assert.ok(f.actions.some((action) => action.type === 'invalidateBackupRepair')); + assert.ok(!f.actions.some((action) => action.type === 'setDeleteBackupSuccess' && action.payload?.status)); +}); + +test('in-flight wallet deletion after A to B to A preserves local A and marks readback', async () => { + const f = fixture(); + const originAppId = f.app.id; + let session = 1; + f.scope.currentBackupAccountSession = () => session; + const walletId = 'disposable-wallet'; + f.collections.Wallet.push(wallet(walletId, enums.NetworkType.MAINNET)); + let finishDelete; + f.scope.Relay.deleteAppImageEntity = () => new Promise((resolve) => { finishDelete = resolve; }); + const deleting = f.run('deleteAppImageEntityWorker', { + originAppId, + originBackupAccountSession: session, + payload: { walletIds: [walletId] }, + }); + await waitForPause(() => finishDelete); + accountRoundTrip(f, originAppId, () => { session++; }); + finishDelete({ updated: true }); + assert.equal((await deleting).updated, false); + assert.equal(f.state.bhr.pendingAllBackupByAppId[originAppId], true); + assert.deepEqual(f.collections.Wallet.map(({ id }) => id), [walletId]); +}); diff --git a/tests/recovery/faucetVisibilityAcceptance.test.cjs b/tests/recovery/faucetVisibilityAcceptance.test.cjs index fbf68e7de7..67871acbb5 100644 --- a/tests/recovery/faucetVisibilityAcceptance.test.cjs +++ b/tests/recovery/faucetVisibilityAcceptance.test.cjs @@ -123,7 +123,7 @@ test('faucet reports pending when the funded txid persists but visible balance s }); test('Receive Test Sats dispatch keeps its original account when queued across a switch', () => { - const scope = { Set }; + const scope = { Set, currentBackupAccountSession: () => 0 }; vm.runInNewContext(ts.transpileModule(functions('src/store/store.ts', [ 'accountOwnedActions', 'accountOwnedUtxoUiActions', 'bindActionOrigin', ]), { compilerOptions: { target: ts.ScriptTarget.ES2020 } }).outputText, scope); diff --git a/tests/recovery/labelAccount.test.cjs b/tests/recovery/labelAccount.test.cjs index 81d8acce5e..11097082ab 100644 --- a/tests/recovery/labelAccount.test.cjs +++ b/tests/recovery/labelAccount.test.cjs @@ -49,6 +49,7 @@ function fixture({ consent = true, switchDuringCheck = false, switchDuringWallet }; const scope = { call, delay, put, dbManager, console, + currentBackupAccountSession: () => 0, store: { getState: () => ({ storage: { appId: state.reduxActive }, settings: { bitcoinNetworkType: state.networkType } }) }, RealmSchema: { KeeperApp: 'KeeperApp', Tags: 'Tags', Wallet: 'Wallet', Vault: 'Vault' }, LabelRefType: { TXN: 'TXN', ADDR: 'ADDR', OUTPUT: 'OUTPUT' }, diff --git a/tests/recovery/sagaHarness.cjs b/tests/recovery/sagaHarness.cjs index 01b6360265..366e1a360c 100644 --- a/tests/recovery/sagaHarness.cjs +++ b/tests/recovery/sagaHarness.cjs @@ -292,6 +292,7 @@ function fixture(options = {}) { ]) + '\n' + functions('src/store/sagas/bhr.ts', [ 'currentBackupAppId', + 'flagUncertainBackupOutcome', 'deleteAppImageEntityWorker', 'deleteVaultImageWorker', 'deleteBackupWorker', diff --git a/tests/recovery/sagaState.test.cjs b/tests/recovery/sagaState.test.cjs index 1825056faa..7eb00c023c 100644 --- a/tests/recovery/sagaState.test.cjs +++ b/tests/recovery/sagaState.test.cjs @@ -28,6 +28,7 @@ function inspectionRunner(f) { RealmSchema:{KeeperApp:'KeeperApp',UAI:'UAI'}, dbManager:{ getObjectByIndex:()=>f.app, getObjectByField:()=>[] }, currentBackupAppId:()=>f.app.id, + currentBackupAccountSession:()=>0, uaiType:{SERVER_BACKUP_FAILURE:'failure'}, uaiActionedWorker:()=>{}, addToUaiStackWorker:()=>{}, }; vm.runInNewContext(ts.transpileModule(code,{ compilerOptions:{ target:ts.ScriptTarget.ES2020 }}).outputText,scope); diff --git a/tests/recovery/sendLabelOrigin.test.cjs b/tests/recovery/sendLabelOrigin.test.cjs index de3ad46a25..fb4ed2be02 100644 --- a/tests/recovery/sendLabelOrigin.test.cjs +++ b/tests/recovery/sendLabelOrigin.test.cjs @@ -38,6 +38,7 @@ const scope = { Set, dbManager: { getObjectByIndex: () => ({ id: 'B' }) }, RealmSchema: { KeeperApp: 'KeeperApp' }, + currentBackupAccountSession: () => 0, originAppId: 'A', vault: { id: 'A-vault' }, generateAbbreviatedOutputDescriptors: () => 'descriptor', diff --git a/tests/recovery/upgradeAccountSession.test.cjs b/tests/recovery/upgradeAccountSession.test.cjs new file mode 100644 index 0000000000..88aa50f998 --- /dev/null +++ b/tests/recovery/upgradeAccountSession.test.cjs @@ -0,0 +1,223 @@ +const assert = require('node:assert/strict'); +const { test } = require('node:test'); +const fs = require('node:fs'); +const path = require('node:path'); +const vm = require('node:vm'); +const ts = require('typescript'); +const semver = require('semver'); +const { runSaga } = require('redux-saga'); +const { call, put, select } = require('redux-saga/effects'); + +const file = 'src/store/sagas/upgrade.ts'; +const source = ts.createSourceFile( + file, fs.readFileSync(path.join(__dirname, '../..', file), 'utf8'), ts.ScriptTarget.Latest, true +); + +function extract(names) { + const selected = source.statements.filter((node) => + (ts.isFunctionDeclaration(node) && names.includes(node.name?.text)) || + (ts.isVariableStatement(node) && node.declarationList.declarations.some( + (declaration) => declaration.name.getText(source) === 'upgradeAccountIsActive' + )) + ); + return ts.transpileModule(selected.map((node) => node.getText(source).replace(/^export /, '')).join('\n'), { + compilerOptions: { target: ts.ScriptTarget.ES2020 }, + }).outputText; +} + +function fixture(names) { + const state = { appId: 'A', session: 7, actions: [], relay: [], local: [] }; + const app = () => ({ id: state.appId, primarySeed: `seed-${state.appId}` }); + const dbManager = { + getObjectByIndex: () => app(), + getCollection: (schema) => { + if (schema === 'UTXOInfo') return [{ walletId: 'wallet', txId: 'tx', vout: 0, labels: [{ name: 'note' }] }]; + if (schema === 'Wallet') return [{ id: 'wallet' }]; + if (schema === 'Signer') return []; + if (schema === 'Vault') return [{ id: 'vault', archived: false }]; + return []; + }, + createObject: (...args) => state.local.push(args), + createObjectBulk: (...args) => state.local.push(args), + }; + const scope = { + call, put, select, semver, dbManager, + console: { log() {} }, + RealmSchema: { KeeperApp: 'KeeperApp', UTXOInfo: 'UTXOInfo', Wallet: 'Wallet', + Signer: 'Signer', Vault: 'Vault', Tags: 'Tags', VersionHistory: 'VersionHistory' }, + store: { getState: () => ({ storage: { appId: state.appId } }) }, + currentBackupAccountSession: () => state.session, + LABELS_INTRODUCTION_VERSION: '1.0.4', + BIP329_INTRODUCTION_VERSION: '1.0.7', + KEY_MANAGEMENT_VERSION: '1.1.9', + APP_KEY_UPGRADE_VERSION: '1.1.12', + HEALTH_CHECK_TIMELINE_MIGRATION_VERSION: '1.2.6', + SIGNER_POLICY_MIGRATION_VERSION: '2.1.0', + migrateLabelsToBip329: (originAppId) => ({ type: 'MIGRATE_LABELS_329', originAppId }), + updateVersionHistory: (previousVersion, newVersion, isRecovery, originAppId) => ({ + type: 'UPDATE_VERSION_HISTORY', payload: { previousVersion, newVersion, isRecovery }, originAppId, + }), + setAppVersion: (version) => ({ type: 'setAppVersion', version }), + setPendingAllBackup: (payload) => ({ type: 'setPendingAllBackup', payload }), + isAutomaticCloudBackupEnabled: () => true, + Relay: { + updateAppImage: async (...args) => { state.relay.push(['version', ...args]); return { updated: true }; }, + modifyLabels: async (...args) => { state.relay.push(['labels', ...args]); return { updated: true }; }, + }, + DeviceInfo: { getBuildNumber: () => '1' }, + getApp: () => ({}), getMessaging: () => ({}), + unsubscribeFromTopic() {}, subscribeToTopic() {}, getReleaseTopic: (version) => version, + generateAbbreviatedOutputDescriptors: () => 'disposable-origin', + LabelRefType: { OUTPUT: 'OUTPUT' }, + generateEncryptionKey: (seed) => seed, + hash256: (value) => `hash(${value})`, + encrypt: (_key, content) => content, + SignerType: { KEEPER: 'KEEPER', MOBILE_KEY: 'MOBILE_KEY', MY_KEEPER: 'MY_KEEPER' }, + }; + vm.runInNewContext(extract(names), scope); + const run = (name, ...args) => runSaga( + { getState: () => ({ storage: { appId: state.appId }, bhr: {} }), + dispatch: (action) => state.actions.push(action) }, + scope[name], ...args + ).toPromise(); + return { state, scope, dbManager, run }; +} + +function deferred() { + let resolve; + const promise = new Promise((done) => { resolve = done; }); + return { promise, resolve }; +} + +test('upgrade sequence stamps its original session on nested migrations and dispatched actions', async () => { + const f = fixture(['applyUpgradeSequence']); + const nested = []; + for (const name of [ + 'migrateStructureforSignersInAppImage', 'migrateStructureforVaultInAppImage', + 'updateAppKeysToEnableSigning', + ]) f.scope[name] = function* (...args) { nested.push([name, ...args]); }; + f.scope.healthCheckTimelineMigration = function* () {}; + f.scope.migrateServerKeyPolicy = function* () {}; + + await f.run('applyUpgradeSequence', { previousVersion: '1.0.5', newVersion: '2.5.17' }); + + assert.equal(nested.length, 3); + assert.ok(nested.every(([, appId, session]) => appId === 'A' && session === 7)); + const backupActions = f.state.actions.filter((action) => + action.type === 'MIGRATE_LABELS_329' || action.type === 'UPDATE_VERSION_HISTORY' + ); + assert.equal(backupActions.length, 2); + assert.ok(backupActions.every((action) => action.originAppId === 'A' && + action.originBackupAccountSession === 7)); +}); + +test('upgrade sequence paused on its first app read stops after A to B to A', async () => { + const f = fixture(['applyUpgradeSequence']); + const entered = deferred(); + const resume = deferred(); + let first = true; + f.dbManager.getObjectByIndex = () => { + if (first) { first = false; entered.resolve(); return resume.promise; } + return { id: f.state.appId }; + }; + f.scope.migrateStructureforSignersInAppImage = function* () { throw Error('stale migration'); }; + f.scope.migrateStructureforVaultInAppImage = function* () { throw Error('stale migration'); }; + + const pending = f.run('applyUpgradeSequence', { previousVersion: '1.0.5', newVersion: '2.5.17' }); + await entered.promise; + f.state.appId = 'B'; f.state.session = 8; + f.state.appId = 'A'; f.state.session = 9; + resume.resolve({ id: 'A' }); + await pending; + + assert.deepEqual(f.state.actions, []); + assert.deepEqual(f.state.relay, []); +}); + +test('upgrade invoked after A to B to A rejects the caller original session before backup', async () => { + const f = fixture(['applyUpgradeSequence']); + f.state.appId = 'B'; f.state.session = 8; + f.state.appId = 'A'; f.state.session = 9; + f.scope.migrateStructureforSignersInAppImage = function* () { throw Error('stale upload'); }; + f.scope.migrateStructureforVaultInAppImage = function* () { throw Error('stale upload'); }; + await f.run('applyUpgradeSequence', { + previousVersion: '1.0.5', + newVersion: '2.5.17', + originBackupAccountSession: 7, + }); + assert.deepEqual(f.state.actions, []); + assert.deepEqual(f.state.relay, []); +}); + +test('version update passes session to Relay and blocks stale A to B to A action', async () => { + const f = fixture(['updateVersionHistoryWorker']); + const action = { originAppId: 'A', originBackupAccountSession: 7, + payload: { previousVersion: '2.5.16', newVersion: '2.5.17', isRecovery: true } }; + await f.run('updateVersionHistoryWorker', action); + assert.equal(f.state.relay.length, 1); + assert.equal(f.state.relay[0][2], 7); + + const entered = deferred(); + const resume = deferred(); + let first = true; + f.dbManager.getObjectByIndex = () => { + if (first) { first = false; entered.resolve(); return resume.promise; } + return { id: f.state.appId }; + }; + const pending = f.run('updateVersionHistoryWorker', action); + await entered.promise; + f.state.appId = 'B'; f.state.session = 8; + f.state.appId = 'A'; f.state.session = 9; + resume.resolve({ id: 'A' }); + await pending; + assert.equal(f.state.relay.length, 1); + assert.deepEqual(f.state.local, []); +}); + +test('label migration passes session to Relay and blocks stale A to B to A action', async () => { + const f = fixture(['migrateLablesWorker']); + const action = { originAppId: 'A', originBackupAccountSession: 7 }; + await f.run('migrateLablesWorker', action); + assert.equal(f.state.relay.length, 1); + assert.equal(f.state.relay[0][4], 7); + + const entered = deferred(); + const resume = deferred(); + const getCollection = f.dbManager.getCollection; + let first = true; + f.dbManager.getCollection = (schema) => { + if (schema === 'UTXOInfo' && first) { + first = false; entered.resolve(); return resume.promise; + } + return getCollection(schema); + }; + const pending = f.run('migrateLablesWorker', action); + await entered.promise; + f.state.appId = 'B'; f.state.session = 8; + f.state.appId = 'A'; f.state.session = 9; + resume.resolve(getCollection('UTXOInfo')); + await pending; + assert.equal(f.state.relay.length, 1); + assert.equal(f.state.local.length, 1); +}); + +test('nested upgrade backup workers receive the original session', async () => { + const f = fixture([ + 'migrateStructureforSignersInAppImage', 'migrateStructureforVaultInAppImage', + 'updateAppKeysToEnableSigning', + ]); + const nested = []; + f.scope.updateAppImageWorker = function* (action) { nested.push(['app', action]); return { updated: true }; }; + f.scope.updateVaultImageWorker = function* (action) { nested.push(['vault', action]); return { updated: true }; }; + f.scope.mapAppKeysToWallets = () => ({ appKeyWalletMap: {}, myAppKeySigners: [] }); + f.scope.generateExtendedKeysForSigners = () => ({}); + f.scope.updateVaultSigners = () => {}; + f.scope.updateSignerDetails = () => {}; + + await f.run('migrateStructureforSignersInAppImage', 'A', 7); + await f.run('migrateStructureforVaultInAppImage', 'A', 7); + await f.run('updateAppKeysToEnableSigning', 'A', 7); + assert.deepEqual(nested.map(([kind]) => kind), ['app', 'vault', 'app']); + assert.ok(nested.every(([, action]) => action.originAppId === 'A' && + action.originBackupAccountSession === 7)); +}); diff --git a/tests/recovery/usdtAccountDelete.test.cjs b/tests/recovery/usdtAccountDelete.test.cjs index 8cb0b0d7ac..1326c58de2 100644 --- a/tests/recovery/usdtAccountDelete.test.cjs +++ b/tests/recovery/usdtAccountDelete.test.cjs @@ -12,12 +12,12 @@ const source = ts.createSourceFile( const declarations = new Map(); function find(node) { if (ts.isVariableDeclaration(node) && - ['isOriginCurrent', 'createWallet', 'deleteWallet', 'updateWallet'].includes(node.name.getText(source))) + ['isOriginCurrent', 'createWallet', 'deleteWallet', 'updateWallet', 'syncAccountStatus'].includes(node.name.getText(source))) declarations.set(node.name.getText(source), node); ts.forEachChild(node, find); } find(source); -for (const name of ['isOriginCurrent', 'createWallet', 'deleteWallet', 'updateWallet']) +for (const name of ['isOriginCurrent', 'createWallet', 'deleteWallet', 'updateWallet', 'syncAccountStatus']) assert.ok(declarations.has(name), `production USDT ${name} callback must exist`); const code = ts.transpileModule( [...declarations.values()].map((declaration) => `const ${declaration.getText(source)};`).join('\n'), { @@ -25,10 +25,14 @@ const code = ts.transpileModule( }).outputText; function fixture({ consent = true, creationPaused = true, switchDuringRemote = false, - switchDuringGeneration = false, switchDuringUpdate = false } = {}) { - const state = { active: 'A', remote: [], local: [], errors: [], dispatched: [], invalidated: [] }; + switchDuringRemoteRoundTrip = false, + switchDuringGeneration = false, switchDuringGenerationRoundTrip = false, + switchDuringUpdate = false, + switchDuringUpdateRoundTrip = false, switchDuringAccountSyncRoundTrip = false } = {}) { + const state = { active: 'A', session: 1, remote: [], local: [], errors: [], dispatched: [], invalidated: [] }; const scope = { appId: 'A', + renderAccountSession: 1, useCallback: (callback) => callback, RealmSchema: { KeeperApp: 'KeeperApp', USDTWallet: 'USDTWallet' }, dbManager: { @@ -41,6 +45,12 @@ function fixture({ consent = true, creationPaused = true, switchDuringRemote = f updateObjectById: async (_schema, _id, update) => { state.local.push({ appId: state.active, update }); if (switchDuringUpdate) state.active = 'B'; + if (switchDuringUpdateRoundTrip) { + state.active = 'B'; + state.session += 1; + state.active = 'A'; + state.session += 1; + } return true; }, deleteObjectById: (_schema, walletId) => { @@ -58,24 +68,47 @@ function fixture({ consent = true, creationPaused = true, switchDuringRemote = f USDTWalletSupportedNetwork: 'mainnet', generateUSDTWallet: async () => { if (switchDuringGeneration) state.active = 'B'; + if (switchDuringGenerationRoundTrip) { + state.active = 'B'; + state.session += 1; + state.active = 'A'; + state.session += 1; + } return { id: 'wallet', name: 'new' }; }, + updateUSDTWalletAccountStatus: async () => { + if (switchDuringAccountSyncRoundTrip) { + state.active = 'B'; + state.session += 1; + state.active = 'A'; + state.session += 1; + } + return { nextNonce: 1 }; + }, canonical: JSON.stringify, recoveryContent: (_kind, wallet) => wallet, updateAppImage: (payload, originAppId) => ({ type: 'UPDATE_APP_IMAGE', payload, originAppId }), invalidateBackupRepair: (appId) => ({ type: 'INVALIDATE_BACKUP_REPAIR', appId }), setPendingAllBackup: (payload) => ({ type: 'SET_PENDING_ALL_BACKUP', payload }), markBackupMutation: (appId) => state.invalidated.push(appId), + currentBackupAccountSession: () => state.session, dispatch: (action) => state.dispatched.push(action), - Relay: { deleteAppImageEntity: async (body) => { - state.remote.push(body); - if (switchDuringRemote) state.active = 'B'; + Relay: { deleteAppImageEntity: async (body, accountSession) => { + state.remote.push({ ...body, accountSession }); + if (switchDuringRemote || switchDuringRemoteRoundTrip) { + state.active = 'B'; + state.session += 1; + } + if (switchDuringRemoteRoundTrip) { + state.active = 'A'; + state.session += 1; + } return { updated: true }; } }, setError: (error) => state.errors.push(error), captureError: () => {}, }; - vm.runInNewContext(`${code}\nglobalThis.callbacks = { createWallet, deleteWallet, updateWallet };`, scope); + vm.runInNewContext(`${code}\nglobalThis.callbacks = { createWallet, deleteWallet, updateWallet, syncAccountStatus };`, scope); return { state, ...scope.callbacks }; } @@ -91,6 +124,20 @@ test('remote response after account switch cannot delete B local wallet', async assert.equal(await f.deleteWallet('wallet'), false); assert.equal(f.state.remote.length, 1); assert.equal(f.state.remote[0].appId, 'A'); + assert.equal(f.state.remote[0].accountSession, 1); + assert.deepEqual(f.state.local, []); + assert.deepEqual(f.state.invalidated, ['A']); + assert.equal(f.state.dispatched.find((action) => action.type === 'SET_PENDING_ALL_BACKUP').payload.appId, 'A'); +}); + +test('remote response after A to B to A cannot delete the new A session wallet', async () => { + const f = fixture({ switchDuringRemoteRoundTrip: true }); + assert.equal(await f.deleteWallet('wallet'), false); + assert.equal(f.state.active, 'A'); + assert.equal(f.state.session, 3); + assert.equal(f.state.remote.length, 1); + assert.equal(f.state.remote[0].appId, 'A'); + assert.equal(f.state.remote[0].accountSession, 1); assert.deepEqual(f.state.local, []); assert.deepEqual(f.state.invalidated, ['A']); assert.equal(f.state.dispatched.find((action) => action.type === 'SET_PENDING_ALL_BACKUP').payload.appId, 'A'); @@ -104,6 +151,17 @@ test('a stale A callback invoked while B is active makes no write', async () => assert.deepEqual(f.state.local, []); }); +test('a delete callback rendered in the first A session cannot delete after A to B to A', async () => { + const f = fixture(); + f.state.active = 'B'; + f.state.session += 1; + f.state.active = 'A'; + f.state.session += 1; + assert.equal(await f.deleteWallet('wallet'), false); + assert.deepEqual(f.state.remote, []); + assert.deepEqual(f.state.local, []); +}); + test('paused USDT setup returns before generation, persistence or backup', async () => { const f = fixture({ switchDuringGeneration: true }); const result = await f.createWallet({ type: 'default', name: 'new', description: '' }); @@ -121,6 +179,27 @@ test('when setup resumes, wallet generation after account switch cannot persist assert.deepEqual(f.state.dispatched, []); }); +test('when setup resumes, wallet generation after A to B to A cannot persist to the later A', async () => { + const f = fixture({ creationPaused: false, switchDuringGenerationRoundTrip: true }); + const result = await f.createWallet({ type: 'default', name: 'new', description: '' }); + assert.equal(result.error, 'Account changed'); + assert.equal(f.state.session, 3); + assert.deepEqual(f.state.local, []); + assert.deepEqual(f.state.dispatched, []); +}); + +test('when setup resumes, a stale create callback cannot act after A to B to A', async () => { + const f = fixture({ creationPaused: false }); + f.state.active = 'B'; + f.state.session += 1; + f.state.active = 'A'; + f.state.session += 1; + const result = await f.createWallet({ type: 'default', name: 'new', description: '' }); + assert.equal(result.error, 'Account changed'); + assert.deepEqual(f.state.local, []); + assert.deepEqual(f.state.dispatched, []); +}); + test('when setup resumes, wallet create dispatches backup with captured originating account', async () => { const f = fixture({ creationPaused: false }); const result = await f.createWallet({ type: 'default', name: 'new', description: '' }); @@ -144,3 +223,29 @@ test('a stale wallet update invoked while B is active makes no write', async () assert.deepEqual(f.state.local, []); assert.deepEqual(f.state.dispatched, []); }); + +test('a callback rendered in the first A session cannot update after A to B to A', async () => { + const f = fixture(); + f.state.active = 'B'; + f.state.session += 1; + f.state.active = 'A'; + f.state.session += 1; + assert.equal(await f.updateWallet({ id: 'wallet', name: 'stale' }), false); + assert.deepEqual(f.state.local, []); + assert.deepEqual(f.state.dispatched, []); +}); + +test('A to B to A during wallet persistence cannot report success or dispatch backup', async () => { + const f = fixture({ switchDuringUpdateRoundTrip: true }); + assert.equal(await f.updateWallet({ id: 'wallet', name: 'new' }), false); + assert.equal(f.state.local[0].appId, 'A'); + assert.equal(f.state.dispatched.some((action) => action.type === 'UPDATE_APP_IMAGE'), false); + assert.deepEqual(f.state.invalidated, ['A']); +}); + +test('a delayed account sync from the first A session cannot update the second A session', async () => { + const f = fixture({ switchDuringAccountSyncRoundTrip: true }); + await assert.rejects(f.syncAccountStatus({ id: 'wallet', name: 'old' }), /Failed to sync account status/); + assert.deepEqual(f.state.local, []); + assert.deepEqual(f.state.dispatched, []); +}); diff --git a/tests/recovery/usdtBackup.test.cjs b/tests/recovery/usdtBackup.test.cjs index 1197bf1ff7..2428d5921d 100644 --- a/tests/recovery/usdtBackup.test.cjs +++ b/tests/recovery/usdtBackup.test.cjs @@ -47,6 +47,8 @@ function hookFixture(options = {}) { canonical: image.canonical, recoveryContent: image.recoveryContent, appId: f.app.id, + renderAccountSession: 1, + currentBackupAccountSession: () => 1, isOriginCurrent: () => f.app.id === f.state.storage.appId, markBackupMutation: (id) => mutations.push(id), invalidateBackupRepair: (payload) => ({ type: 'invalidateBackupRepair', payload }), diff --git a/tests/recovery/walletRefreshIsolation.test.cjs b/tests/recovery/walletRefreshIsolation.test.cjs index 3126d2e23e..6d07bffe6e 100644 --- a/tests/recovery/walletRefreshIsolation.test.cjs +++ b/tests/recovery/walletRefreshIsolation.test.cjs @@ -115,3 +115,19 @@ for (const [kind, archived] of [['Wallet', false], ['Vault', false], ['Vault', t assert.deepEqual(f.writes, []); }); } + +test('refresh paused at Electrum cannot overwrite newer A specs after A to B to A', async () => { + const f = refreshFixture('Wallet', false); + const originAppId = f.app.id; + let session = 1; + f.scope.currentBackupAccountSession = () => session; + const refreshing = f.refresh(originAppId); + await f.waitForSync(); + f.app.id = 'B'; f.state.storage.appId = 'B'; session++; + f.app.id = originAppId; f.state.storage.appId = originAppId; session++; + f.collections.Wallet[0].specs.balances.confirmed = 777; + f.complete(); + assert.equal(await refreshing, false); + assert.deepEqual(f.writes, []); + assert.equal(f.collections.Wallet[0].specs.balances.confirmed, 777); +}); diff --git a/tests/store/versionHistory.test.ts b/tests/store/versionHistory.test.ts index 14422f7339..fb0f368b52 100644 --- a/tests/store/versionHistory.test.ts +++ b/tests/store/versionHistory.test.ts @@ -17,6 +17,7 @@ jest.mock('src/storage/realm/dbManager', () => ({ createObject: jest.fn(), })); jest.mock('src/services/backend/Relay', () => ({ updateAppImage: jest.fn() })); +jest.mock('src/services/backup/transport', () => ({ currentBackupAccountSession: () => 1 })); jest.mock('src/store/store', () => ({ store: { getState: jest.fn() } })); jest.mock('src/store/reducers/bhr', () => ({ isAutomaticCloudBackupEnabled: (backup, appId) => backup.automaticCloudBackupByAppId?.[appId] === true, @@ -56,7 +57,7 @@ function writeHistory(action) { expect(choice.type).toBe('SELECT'); expect(choice.payload.selector(store.getState())).toBe(true); expect(worker.next(true).value).toEqual( - call(Relay.updateAppImage, { appId, version: newVersion }) + call(Relay.updateAppImage, { appId, version: newVersion }, 1) ); const write = worker.next({} as any).value as any; expect(write.payload.fn).toBe(dbManager.createObject); @@ -71,7 +72,7 @@ function recoverWithoutDuplicateHistory(action) { expect(worker.next().value).toEqual(call(dbManager.getObjectByIndex, RealmSchema.KeeperApp)); expect((worker.next({ id: appId } as any).value as any).type).toBe('SELECT'); expect(worker.next(true).value).toEqual( - call(Relay.updateAppImage, { appId, version: newVersion }) + call(Relay.updateAppImage, { appId, version: newVersion }, 1) ); expect(worker.next({} as any).done).toBe(true); } @@ -84,7 +85,10 @@ describe('Version History retains recovery provenance through required upgrades' expect(upgrade.next().value).toEqual(call(dbManager.getObjectByIndex, RealmSchema.KeeperApp)); expect(upgrade.next({ id: 'test-app' } as any).value).toEqual(put(setAppVersion(newVersion))); const history = upgrade.next().value as any; - expect(history).toEqual(put(updateVersionHistory(previousVersion, newVersion, isRecovery, 'test-app'))); + expect(history).toEqual(put({ + ...updateVersionHistory(previousVersion, newVersion, isRecovery, 'test-app'), + originBackupAccountSession: 1, + })); if (isRecovery) { recoverWithoutDuplicateHistory(history.payload.action); } else { @@ -147,6 +151,9 @@ function recoveryFixture(backupVersion: string) { select, semver, console, + currentBackupAccountSession: () => 1, + currentBackupAppId: () => require('crypto').createHash('sha256') + .update(Buffer.from('fixture')).digest('hex'), RealmSchema, applyUpgradeSequence, DeviceInfo: { getVersion: () => newVersion, getBuildNumber: () => '624' }, @@ -262,6 +269,7 @@ function recoveryFixture(backupVersion: string) { if (fn === applyUpgradeSequence) recoveryUpgrades.push(args[0]); response = fn(...args); if (response?.next) response = drive(response); + if (fn === scope.recoverApp && response === undefined) response = 1; } if (effect.type === 'PUT') { const action = effect.payload.action; @@ -292,7 +300,8 @@ test.each(['2.5.15', '2.5.16'])( ]); expect(recoveryUpgrades).toEqual( backupVersion === '2.5.15' - ? [{ previousVersion: backupVersion, newVersion, isRecovery: true }] + ? [{ previousVersion: backupVersion, newVersion, isRecovery: true, + originBackupAccountSession: 1 }] : [] ); } From b63d4b0ea0716681ce25181bb873cdb18f0217a9 Mon Sep 17 00:00:00 2001 From: Anant Date: Mon, 12 Oct 2026 20:23:23 +0530 Subject: [PATCH 125/254] fix: bind backup repair to account session --- src/services/backup/repair.ts | 38 ++++++++--- tests/recovery/repairAccountSession.test.cjs | 69 ++++++++++++++++++++ 2 files changed, 99 insertions(+), 8 deletions(-) create mode 100644 tests/recovery/repairAccountSession.test.cjs diff --git a/src/services/backup/repair.ts b/src/services/backup/repair.ts index 5cd73e655e..7ea4465c47 100644 --- a/src/services/backup/repair.ts +++ b/src/services/backup/repair.ts @@ -15,6 +15,7 @@ import { backupRevision, boundedBackupPost, canAccessBackupAccount, + currentBackupAccountSession, withBackupSession, } from './transport'; import { @@ -162,19 +163,30 @@ export function inspectBackup( appId: string, repair: boolean, onPhase: (phase: RepairPhase) => void, - explicitChoice = false + explicitChoice = false, + expectedSession = currentBackupAccountSession() ): Promise { - const running = inFlight.get(appId); + // A prior A session may still be queued or in flight after A -> B -> A. + // Do not join that inspection or let it write on behalf of the new session. + const operationKey = `${appId}:${expectedSession}`; + const sessionIsCurrent = () => currentBackupAccountSession() === expectedSession; + if (!sessionIsCurrent()) { + onPhase('unverified'); + return Promise.resolve('unverified'); + } + const running = inFlight.get(operationKey); if (running) { if (repair && !running.repair) - return running.promise.then(() => inspectBackup(appId, true, onPhase, explicitChoice)); + return running.promise.then(() => + inspectBackup(appId, true, onPhase, explicitChoice, expectedSession) + ); running.listeners.add(onPhase); if (running.phase) onPhase(running.phase); return running.promise; } const listeners = new Set([onPhase]); const notify = (phase: RepairPhase) => { - const active = inFlight.get(appId); + const active = inFlight.get(operationKey); if (active) active.phase = phase; listeners.forEach((listener) => listener(phase)); }; @@ -187,6 +199,7 @@ export function inspectBackup( const revision = backupRevision(appId); const assertCurrent = () => { if ( + !sessionIsCurrent() || Date.now() >= deadline || (dbManager.getObjectByIndex(RealmSchema.KeeperApp) as any)?.id !== appId || !canAccessBackupAccount(appId, explicitChoice) @@ -332,7 +345,9 @@ export function inspectBackup( // deadline. Never retry a repair, invalid response, or account switch. let accountIsCurrent = false; try { - accountIsCurrent = (dbManager.getObjectByIndex(RealmSchema.KeeperApp) as any)?.id === appId; + accountIsCurrent = + sessionIsCurrent() && + (dbManager.getObjectByIndex(RealmSchema.KeeperApp) as any)?.id === appId; } catch { // Realm may already be closed during an account switch. } @@ -365,13 +380,20 @@ export function inspectBackup( return 'unverified'; } }; - const runOnce = (allowRetry: boolean) => withBackupSession(appId, () => inspectOnce(allowRetry)); + const runOnce = (allowRetry: boolean) => + withBackupSession(appId, () => { + if (!sessionIsCurrent()) { + notify('unverified'); + return Promise.resolve('unverified' as RepairPhase); + } + return inspectOnce(allowRetry); + }); const result: Promise = runOnce(true).then(async (phase) => phase === 'retry' ? ((await runOnce(false)) as RepairPhase) : phase ); - inFlight.set(appId, { repair, promise: result, listeners }); + inFlight.set(operationKey, { repair, promise: result, listeners }); const clean = () => { - if (inFlight.get(appId)?.promise === result) inFlight.delete(appId); + if (inFlight.get(operationKey)?.promise === result) inFlight.delete(operationKey); }; result.then(clean, clean); return result; diff --git a/tests/recovery/repairAccountSession.test.cjs b/tests/recovery/repairAccountSession.test.cjs new file mode 100644 index 0000000000..2a1c0dbea5 --- /dev/null +++ b/tests/recovery/repairAccountSession.test.cjs @@ -0,0 +1,69 @@ +const { test } = require('node:test'); +const assert = require('node:assert/strict'); +const { harness, wallet } = require('./helpers.cjs'); + +function switchAwayAndBack(f) { + const originalId = f.app.id; + f.app.id = 'other-disposable-account'; + f.transport.observeBackupAccount(f.app.id); + f.app.id = originalId; + f.transport.observeBackupAccount(originalId); +} + +test('queued repair cannot begin after an A to B to A account switch', async () => { + const f = harness(); + f.local.Wallet.push(wallet()); + f.transport.observeBackupAccount(f.app.id); + let releaseQueue; + const queueBarrier = new Promise((resolve) => { releaseQueue = resolve; }); + f.transport.withBackupSession(f.app.id, () => queueBarrier); + + const repairing = f.run(true); + switchAwayAndBack(f); + releaseQueue(); + + assert.equal(await repairing, 'unverified'); + assert.deepEqual(f.calls, [], 'stale queued repair must make no server request'); +}); + +test('caller session captured before async work cannot start a later repair', async () => { + const f = harness(); + f.local.Wallet.push(wallet()); + f.transport.observeBackupAccount(f.app.id); + const originalSession = f.transport.currentBackupAccountSession(); + switchAwayAndBack(f); + + const outcome = await f.repair.inspectBackup(f.app.id, true, () => {}, false, originalSession); + assert.equal(outcome, 'unverified'); + assert.deepEqual(f.calls, []); +}); + +test('in-flight repair cannot upload after an A to B to A account switch', async () => { + let reachedSnapshot; + const snapshotStarted = new Promise((resolve) => { reachedSnapshot = resolve; }); + let releaseSnapshot; + const snapshotBarrier = new Promise((resolve) => { releaseSnapshot = resolve; }); + let firstSnapshot = true; + const f = harness({ + post: async ({ path }) => { + if (path.endsWith('getBackupSnapshot') && firstSnapshot) { + firstSnapshot = false; + reachedSnapshot(); + await snapshotBarrier; + } + }, + }); + f.local.Wallet.push(wallet()); + f.transport.observeBackupAccount(f.app.id); + + const staleRepair = f.run(true); + await snapshotStarted; + switchAwayAndBack(f); + const freshRepair = f.run(true); + releaseSnapshot(); + + assert.equal(await staleRepair, 'unverified'); + assert.equal(await freshRepair, 'verified', 'new session must inspect independently'); + assert.equal(f.calls.filter((path) => path === 'repairAppBackup').length, 1); + assert.equal(f.calls.filter((path) => path === 'getBackupSnapshot').length, 3); +}); From b3729db4832f1c643c7e3dc17acb4d71a9497557 Mon Sep 17 00:00:00 2001 From: Anant Date: Mon, 12 Oct 2026 20:47:51 +0530 Subject: [PATCH 126/254] fix: stop stale recovery writes after account round trips --- src/store/sagas/bhr.ts | 54 ++++++-- .../recovery/recoveryAccountSession.test.cjs | 130 ++++++++++++++++++ 2 files changed, 171 insertions(+), 13 deletions(-) create mode 100644 tests/recovery/recoveryAccountSession.test.cjs diff --git a/src/store/sagas/bhr.ts b/src/store/sagas/bhr.ts index d8c252b977..a3576e8acc 100644 --- a/src/store/sagas/bhr.ts +++ b/src/store/sagas/bhr.ts @@ -662,8 +662,17 @@ function* recoverApp( appImage, allVaultImages, labels, - previousVersion + previousVersion, + originBackupAccountSession?: number ) { + // Recovery selects appID after the first Realm write. Keep the session from + // the initiating action across decryption and all earlier async work. + const originSession = originBackupAccountSession ?? currentBackupAccountSession(); + const assertOrigin = () => { + if (currentBackupAccountSession() !== originSession) + throw new Error('Backup account changed'); + }; + assertOrigin(); const { bitcoinNetworkType } = yield select((state: RootState) => state.settings); const recovered: BackupImage = yield call( prepareRecoveryImage, @@ -700,16 +709,26 @@ function* recoverApp( networkType: bitcoinNetworkType, }; - yield call(writeRecoveredObject, RealmSchema.KeeperApp, app); + yield call(writeRecoveredObject, RealmSchema.KeeperApp, app, assertOrigin); + assertOrigin(); yield put(setAppId(appID)); + const recoveryAccountSession = currentBackupAccountSession(); + const assertRecoveryAccount = () => { + if (currentBackupAccountSession() !== recoveryAccountSession || currentBackupAppId() !== appID) + throw new Error('Backup account changed'); + }; + assertRecoveryAccount(); + function* writeRecord(schema: RealmSchema, record: any) { + yield call(writeRecoveredObject, schema, record, assertRecoveryAccount); + } // Wallet recreation if (appImage.wallets) { for (const decryptedWallet of Object.values(recovered.wallets) as Wallet[]) { try { if (decryptedWallet.entityKind === EntityKind.USDT_WALLET) - yield call(writeRecoveredObject, RealmSchema.USDTWallet, decryptedWallet); - else yield call(writeRecoveredObject, RealmSchema.Wallet, decryptedWallet); + yield call(writeRecord, RealmSchema.USDTWallet, decryptedWallet); + else yield call(writeRecord, RealmSchema.Wallet, decryptedWallet); } catch (err) { throw new Error('Recovery data unavailable'); } @@ -729,7 +748,7 @@ function* recoverApp( ? NetworkType.TESTNET : NetworkType.MAINNET; } - yield call(writeRecoveredObject, RealmSchema.Signer, decrytpedSigner); + yield call(writeRecord, RealmSchema.Signer, decrytpedSigner); } catch (err) { throw new Error('Recovery data unavailable'); } @@ -791,11 +810,11 @@ function* recoverApp( hidden: false, signerXpubs, }; - yield call(writeRecoveredObject, RealmSchema.Signer, signerObject); + yield call(writeRecord, RealmSchema.Signer, signerObject); } } } - yield call(writeRecoveredObject, RealmSchema.Vault, vault); + yield call(writeRecord, RealmSchema.Vault, vault); } catch (err) { throw new Error('Recovery data unavailable'); } @@ -805,7 +824,7 @@ function* recoverApp( // Labels Restore if (labels) { for (const label of Object.values(recovered.labels)) { - yield call(writeRecoveredObject, RealmSchema.Tags, label); + yield call(writeRecord, RealmSchema.Tags, label); } } @@ -817,11 +836,13 @@ function* recoverApp( ); for (const node of existingNodes) { if (node && node.id) { + assertRecoveryAccount(); const deleted = yield call( dbManager.deleteObjectById, RealmSchema.NodeConnect, node.id.toString() ); + assertRecoveryAccount(); if (deleted !== true) throw new Error('Recovery data unavailable'); } } @@ -832,7 +853,7 @@ function* recoverApp( ? NetworkType.TESTNET : NetworkType.MAINNET; } - yield call(writeRecoveredObject, RealmSchema.NodeConnect, decryptedNode); + yield call(writeRecord, RealmSchema.NodeConnect, decryptedNode); } catch (err) { throw new Error('Recovery data unavailable'); } @@ -842,6 +863,7 @@ function* recoverApp( // Connect to a node const savedNodes: NodeDetail[] = yield call(dbManager.getCollection, RealmSchema.NodeConnect); if (savedNodes.length > 0 && !savedNodes.find((node) => node.isConnected)) { + assertRecoveryAccount(); const firstNode = savedNodes[0]; firstNode.isConnected = true; @@ -851,11 +873,12 @@ function* recoverApp( firstNode.id.toString(), firstNode ); + assertRecoveryAccount(); if (connected !== true) throw new Error('Recovery data unavailable'); } // seed confirm for recovery - yield call(writeRecoveredObject, RealmSchema.BackupHistory, { + yield call(writeRecord, RealmSchema.BackupHistory, { title: BackupAction.SEED_BACKUP_CONFIRMED, date: moment().unix(), confirmed: true, @@ -866,27 +889,32 @@ function* recoverApp( // enable Assisted Server Backup. This transient flag triggers a new upload // when the health-check screen mounts; persistent key status is set by the // completed recovery worker instead. + assertRecoveryAccount(); yield put(setSeedConfirmed(false)); + assertRecoveryAccount(); yield put(setBackupType(BackupType.SEED)); + assertRecoveryAccount(); yield put(uaiActioned({ uaiType: uaiType.RECOVERY_PHRASE_HEALTH_CHECK, action: true })); // create/add restored object for version - yield call(writeRecoveredObject, RealmSchema.VersionHistory, { + yield call(writeRecord, RealmSchema.VersionHistory, { version: `${DeviceInfo.getVersion()}(${DeviceInfo.getBuildNumber()})`, date: new Date().toString(), title: 'Recovered Wallet', }); - + return recoveryAccountSession; } -function* writeRecoveredObject(schema: RealmSchema, record: any) { +function* writeRecoveredObject(schema: RealmSchema, record: any, assertActive?: () => void) { // Realm's adapter returns false/undefined on a rejected write instead of // throwing. Never count that record as recovered or report partial success. + assertActive?.(); const written = yield call(dbManager.createObject, schema, record); if (written !== true) throw new Error('Recovery data unavailable'); // Realm writes complete synchronously. Break the saga's synchronous effect // chain between records so a large restore cannot exhaust the JS stack. yield call(pauseBackup); + assertActive?.(); } function* healthCheckSatutsUpdateWorker({ diff --git a/tests/recovery/recoveryAccountSession.test.cjs b/tests/recovery/recoveryAccountSession.test.cjs new file mode 100644 index 0000000000..8d210c9844 --- /dev/null +++ b/tests/recovery/recoveryAccountSession.test.cjs @@ -0,0 +1,130 @@ +// Run the production recovery saga against disposable records. A paused restore +// must not replace a newer account session's signing key after A -> B -> A. +const { test } = require('node:test'); +const assert = require('node:assert/strict'); +const { fixture, enums, encryption, signer, wallet } = require('./sagaHarness.cjs'); + +function deferred() { + let resolve; + const promise = new Promise((done) => { + resolve = done; + }); + return { promise, resolve }; +} + +test('recovery cannot overwrite a signer after A -> B -> A between recovered records', async () => { + const f = fixture(); + const oldSigner = signer('AABBCCDD', enums.NetworkType.MAINNET, enums.SignerType.MY_KEEPER); + oldSigner.id = f.scope.getKeyUID(oldSigner); + oldSigner.signerXpubs[enums.XpubTypes.P2WPKH][0].xpriv = 'old-disposable-secret'; + f.scope.prepareRecoveryImage = async () => ({ + wallets: { wallet: wallet('old-wallet', enums.NetworkType.MAINNET) }, + signers: { signer: oldSigner }, + vaults: {}, + labels: {}, + nodes: {}, + }); + + let session = 1; + f.scope.currentBackupAccountSession = () => session; + const paused = deferred(); + const resume = deferred(); + let recordsWritten = 0; + f.scope.pauseBackup = async () => { + recordsWritten += 1; + // KeeperApp and wallet have been written; the stale signer is next. + if (recordsWritten === 2) { + paused.resolve(); + await resume.promise; + } + }; + + const restoring = f.run( + 'recoverApp', + 'disposable mnemonic', + Buffer.from('ab'.repeat(32), 'hex'), + 'disposable-key', + f.app.id, + f.app.subscription, + f.remote, + [], + [], + '2.5.16', + session + ); + await paused.promise; + + const newSigner = JSON.parse(JSON.stringify(oldSigner)); + newSigner.signerXpubs[enums.XpubTypes.P2WPKH][0].xpriv = 'new-disposable-secret'; + f.collections.Signer.push(newSigner); + f.state.storage.appId = 'another-account'; + session += 1; + f.state.storage.appId = f.app.id; + session += 1; + resume.resolve(); + + await assert.rejects(restoring); + assert.equal( + f.collections.Signer[0].signerXpubs[enums.XpubTypes.P2WPKH][0].xpriv, + 'new-disposable-secret' + ); +}); + +test('a stale recovery response cannot write KeeperApp after A -> B -> A', async () => { + const f = fixture(); + const entered = deferred(); + const resume = deferred(); + f.scope.prepareRecoveryImage = async () => { + entered.resolve(); + await resume.promise; + return { wallets: {}, signers: {}, vaults: {}, labels: {}, nodes: {} }; + }; + let session = 1; + f.scope.currentBackupAccountSession = () => session; + const restoring = f.run( + 'recoverApp', + 'old disposable mnemonic', + Buffer.from('ab'.repeat(32), 'hex'), + 'disposable-key', + f.app.id, + f.app.subscription, + f.remote, + [], + [], + '2.5.16', + session + ); + await entered.promise; + + session += 1; + f.state.storage.appId = 'another-account'; + session += 1; + f.state.storage.appId = f.app.id; + resume.resolve(); + + await assert.rejects(restoring, /Backup account changed/); + assert.equal(f.app.primaryMnemonic, undefined); + assert.equal(f.collections.BackupHistory.length, 0); +}); + +test('successful recovery returns its session before adding the Recovery Key signer', async () => { + const f = fixture(); + const session = 1; + f.scope.currentBackupAccountSession = () => session; + const key = encryption.generateEncryptionKey(f.scope.bip39.mnemonicToSeedSync().toString('hex')); + const restoredWallet = wallet('disposable-wallet', enums.NetworkType.MAINNET); + f.remote.wallets[restoredWallet.id] = encryption.encrypt(key, JSON.stringify(restoredWallet)); + const recover = f.scope.recoverApp; + let returnedSession; + f.scope.recoverApp = function* (...args) { + returnedSession = yield* recover(...args); + return returnedSession; + }; + + await f.run('getAppImageWorker', { payload: { primaryMnemonic: 'disposable mnemonic' } }); + + assert.equal(returnedSession, session); + assert.ok(f.collections.Signer.some((row) => row.masterFingerprint === 'RECOVERY')); + assert.equal(f.state.account.recoveryKeyStatusByAppId[f.app.id], 'confirmed'); + assert.ok(f.actions.some((action) => action.type === 'setAppCreated' && action.payload === true)); +}); From 1ea152626b55dc0e4a72e89f58b052324a1d63bf Mon Sep 17 00:00:00 2001 From: Anant Date: Mon, 12 Oct 2026 20:51:35 +0530 Subject: [PATCH 127/254] fix: reject stale login after account round trips --- src/store/sagas/login.ts | 19 ++- tests/recovery/loginOriginSession.test.cjs | 135 +++++++++++++++++++++ 2 files changed, 153 insertions(+), 1 deletion(-) create mode 100644 tests/recovery/loginOriginSession.test.cjs diff --git a/src/store/sagas/login.ts b/src/store/sagas/login.ts index 2a482cbbe0..c0df330fac 100644 --- a/src/store/sagas/login.ts +++ b/src/store/sagas/login.ts @@ -145,7 +145,12 @@ export const credentialStorageWatcher = createWatcher(credentialsStorageWorker, function* credentialsAuthWorker({ payload }) { let key; + // An older authentication can finish after the selected account cycles + // A -> B -> A. Preserve its original session before the first async effect. + const originSession = currentBackupAccountSession(); + let loginSession = originSession; const appId = yield select((state: RootState) => state.storage.appId); + if (originSession !== currentBackupAccountSession()) return; yield put(credsAuthenticatedError('')); try { yield put(setRecepitVerificationError(false)); @@ -168,6 +173,7 @@ function* credentialsAuthWorker({ payload }) { encryptedKey = res.encryptedKey; } key = yield call(decrypt, hash, encryptedKey); + if (originSession !== currentBackupAccountSession()) return; yield put(setKey(key)); yield put(setPinHash(hash)); @@ -179,11 +185,13 @@ function* credentialsAuthWorker({ payload }) { ); const currentAccount = allAccounts.find((account) => account.hash === hash); const uint8array = yield call(stringToArrayBuffer, key); + if (originSession !== currentBackupAccountSession()) return; const { success, error } = yield call( dbManager.initializeRealm, uint8array, currentAccount?.realmId ); + if (originSession !== currentBackupAccountSession()) return; if (!success) { throw Error(`Failed to load the database: ${error}`); @@ -194,6 +202,7 @@ function* credentialsAuthWorker({ payload }) { // setting correct app id from realm at login const keeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); + if (originSession !== currentBackupAccountSession()) return; if (keeperApp?.id) { const onlyKnownAccount = allAccounts.length <= 1 && @@ -205,11 +214,15 @@ function* credentialsAuthWorker({ payload }) { }) ); } + if (originSession !== currentBackupAccountSession()) return; if (keeperApp?.id) yield put(setAppId(keeperApp.id)); const loginAccountSession = currentBackupAccountSession(); + loginSession = loginAccountSession; + if (store.getState().storage.appId !== keeperApp?.id) return; // Store temporary account details if (!allAccounts.length) { + if (currentBackupAccountSession() !== loginAccountSession) return; yield put(setTempDetails({ hash, realmId: REALM_FILE, accountIdentifier: '' })); } @@ -296,8 +309,12 @@ function* credentialsAuthWorker({ payload }) { } yield put(credsAuthenticated(true)); } else yield put(credsAuthenticated(true)); // TODO: Should remove and throw an error if not needed in any scenario - } else yield put(credsAuthenticated(true)); + } else { + if (originSession !== currentBackupAccountSession()) return; + yield put(credsAuthenticated(true)); + } } catch (err) { + if (loginSession !== currentBackupAccountSession()) return; yield put(credsAuthenticatedError(err)); yield put(credsAuthenticated(false)); } diff --git a/tests/recovery/loginOriginSession.test.cjs b/tests/recovery/loginOriginSession.test.cjs new file mode 100644 index 0000000000..87d1149292 --- /dev/null +++ b/tests/recovery/loginOriginSession.test.cjs @@ -0,0 +1,135 @@ +const { test } = require('node:test'); +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const path = require('node:path'); +const vm = require('node:vm'); +const ts = require('typescript'); +const { runSaga } = require('redux-saga'); +const { call, put, select } = require('redux-saga/effects'); + +const file = path.join(__dirname, '../../src/store/sagas/login.ts'); +const source = ts.createSourceFile( + file, + fs.readFileSync(file, 'utf8'), + ts.ScriptTarget.Latest, + true +); +const worker = source.statements.find( + (node) => ts.isFunctionDeclaration(node) && node.name?.text === 'credentialsAuthWorker' +); +assert.ok(worker, 'production authentication worker exists'); +const code = ts.transpileModule(worker.getText(source), { + compilerOptions: { target: ts.ScriptTarget.ES2020 }, +}).outputText; + +function deferred() { + let resolve; + const promise = new Promise((done) => { + resolve = done; + }); + return { promise, resolve }; +} + +function fixture(pauseAt) { + const state = { + storage: { appId: 'A', appVersion: '2.5.0', defaultWalletCreated: {} }, + account: { + allAccounts: [{ appId: 'A', hash: 'disposable-hash', realmId: 'A.realm' }], + backupMethodByAppId: {}, + }, + }; + let session = 7; + const paused = deferred(); + const resume = deferred(); + const actions = []; + const realmOpens = []; + const upgradeCalls = []; + const gate = (value) => { + paused.resolve(); + return resume.promise.then(() => value); + }; + const scope = { + call, + put, + select, + currentBackupAccountSession: () => session, + LoginMethod: { PIN: 'PIN' }, + hash512: () => 'disposable-hash', + decrypt: () => 'disposable-key', + SecureStore: { fetch: () => (pauseAt === 'credential' ? gate('cipher') : 'cipher') }, + stringToArrayBuffer: () => new Uint8Array([1]), + dbManager: { + initializeRealm: (_key, realmId) => { + realmOpens.push(realmId); + return pauseAt === 'realm' ? gate({ success: true }) : { success: true }; + }, + getObjectByIndex: () => ({ id: 'A' }), + getCollection: () => [{ version: '2.5.0(1)' }], + }, + RealmSchema: { KeeperApp: 'KeeperApp', VersionHistory: 'VersionHistory' }, + store: { getState: () => state }, + DeviceInfo: { getVersion: () => '2.5.16', getBuildNumber: () => '2' }, + semver: require('semver'), + applyUpgradeSequence: (args) => { + upgradeCalls.push(args); + throw Error('fixture stop'); + }, + console: { log() {} }, + }; + for (const name of [ + 'credsAuthenticatedError', + 'setRecepitVerificationError', + 'setRecepitVerificationFailed', + 'setupLoading', + 'setKey', + 'setPinHash', + 'migrateLegacyAutomaticCloudBackup', + 'setAppId', + 'credsAuthenticated', + ]) + scope[name] = (payload) => ({ type: name, payload }); + vm.runInNewContext(code, scope); + const run = () => + runSaga( + { getState: () => state, dispatch: (action) => actions.push(action), onError: () => {} }, + scope.credentialsAuthWorker, + { payload: { method: 'PIN', passcode: 'disposable passcode', reLogin: false } } + ).toPromise(); + const switchRoundTrip = () => { + state.storage.appId = 'B'; + session += 1; + state.storage.appId = 'A'; + session += 1; + }; + return { paused, resume, actions, realmOpens, upgradeCalls, run, switchRoundTrip }; +} + +test('old login cannot reopen Realm after A -> B -> A while credentials are pending', async () => { + const f = fixture('credential'); + const pending = f.run(); + await f.paused.promise; + f.switchRoundTrip(); + f.resume.resolve(); + await pending; + assert.deepEqual(f.realmOpens, []); + assert.equal( + f.actions.some((action) => action.type === 'setKey'), + false + ); + assert.equal(f.upgradeCalls.length, 0); +}); + +test('old login cannot start upgrade after A -> B -> A during Realm open', async () => { + const f = fixture('realm'); + const pending = f.run(); + await f.paused.promise; + f.switchRoundTrip(); + f.resume.resolve(); + await pending; + assert.equal(f.realmOpens.length, 1); + assert.equal( + f.actions.some((action) => action.type === 'setAppId'), + false + ); + assert.equal(f.upgradeCalls.length, 0); +}); From bc0c2790b5c4591cda9d7b24a750393e9ebbdceb Mon Sep 17 00:00:00 2001 From: Anant Date: Mon, 12 Oct 2026 20:51:27 +0530 Subject: [PATCH 128/254] fix: isolate wallet refresh leases by account session --- src/store/sagas/walletRefreshCoordinator.js | 5 ++- src/store/sagas/wallets.ts | 8 ++++- .../recovery/walletRefreshIsolation.test.cjs | 32 +++++++++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/store/sagas/walletRefreshCoordinator.js b/src/store/sagas/walletRefreshCoordinator.js index 0aaa9406e6..e7389e7fd7 100644 --- a/src/store/sagas/walletRefreshCoordinator.js +++ b/src/store/sagas/walletRefreshCoordinator.js @@ -3,15 +3,18 @@ const activeBatches = new Map(); const walletTails = new Map(); -export const acquireWalletRefresh = (appId, networkType, wallets, options = {}) => { +export const acquireWalletRefresh = (appId, networkType, wallets, options = {}, accountSession = null) => { const walletKeys = [...new Set(wallets.map((wallet) => JSON.stringify([appId, networkType, wallet.entityKind, wallet.id]) ))].sort(); + // A -> B -> A starts a new account session. Do not join its refresh to an + // older A batch, but keep the wallet tail so writes remain in order. const batchKey = JSON.stringify([ walletKeys, !!options.hardRefresh, !!options.addNotifications, !!options.dustScan, + accountSession, ]); const existing = activeBatches.get(batchKey); if (existing) return { owner: false, done: existing.done }; diff --git a/src/store/sagas/wallets.ts b/src/store/sagas/wallets.ts index 715848326b..94bfdd7e15 100644 --- a/src/store/sagas/wallets.ts +++ b/src/store/sagas/wallets.ts @@ -806,7 +806,13 @@ function* refreshWalletsWorker({ return true; } - lease = acquireWalletRefresh(refreshScope.appId, refreshScope.networkType, wallets, options); + lease = acquireWalletRefresh( + refreshScope.appId, + refreshScope.networkType, + wallets, + options, + requestSession + ); if (!lease.owner) { succeeded = yield call(() => lease.done); return succeeded; diff --git a/tests/recovery/walletRefreshIsolation.test.cjs b/tests/recovery/walletRefreshIsolation.test.cjs index 6d07bffe6e..0e213367e6 100644 --- a/tests/recovery/walletRefreshIsolation.test.cjs +++ b/tests/recovery/walletRefreshIsolation.test.cjs @@ -131,3 +131,35 @@ test('refresh paused at Electrum cannot overwrite newer A specs after A to B to assert.deepEqual(f.writes, []); assert.equal(f.collections.Wallet[0].specs.balances.confirmed, 777); }); + +test('new A login refresh runs after stale A lease from A to B to A', async () => { + const f = refreshFixture('Wallet', false); + const originAppId = f.app.id; + let session = 1; + f.scope.currentBackupAccountSession = () => session; + + const staleRefresh = f.refresh(originAppId); + await f.waitForSync(); + f.app.id = 'B'; f.state.storage.appId = 'B'; session++; + f.app.id = originAppId; f.state.storage.appId = originAppId; session++; + f.collections.Wallet[0].specs.balances.confirmed = 777; + + let newSessionSyncs = 0; + f.scope.WalletOperations.syncWalletsViaElectrumClient = (wallets) => { + newSessionSyncs++; + const synced = f.scope.getJSONFromRealmObject(wallets[0]); + synced.specs.balances.unconfirmed = 2000; + synced.specs.hasNewUpdates = true; + return { synchedWallets: [{ synchedWallet: synced, newUTXOs: [] }] }; + }; + const newSessionRefresh = f.refresh(originAppId); + await new Promise(setImmediate); + assert.equal(newSessionSyncs, 0, 'the new session must wait for the prior wallet write lease'); + f.complete(); + + assert.equal(await staleRefresh, false); + assert.equal(await newSessionRefresh, true); + assert.equal(newSessionSyncs, 1, 'the new A session must perform its own Electrum sync'); + assert.equal(f.collections.Wallet[0].specs.balances.confirmed, 777); + assert.equal(f.collections.Wallet[0].specs.balances.unconfirmed, 2000); +}); From 33a55df2b512b611e173dea6c7084458263ea961 Mon Sep 17 00:00:00 2001 From: Orlando Arzola <> Date: Mon, 12 Oct 2026 17:22:04 +0200 Subject: [PATCH 129/254] fix: decode iOS wallet configuration file URIs before reading --- .../Vault/VaultConfigurationRecreation.tsx | 4 +- src/utils/documentReadPath.ts | 7 ++++ tests/documentReadPath.test.ts | 41 +++++++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 src/utils/documentReadPath.ts create mode 100644 tests/documentReadPath.test.ts diff --git a/src/screens/Vault/VaultConfigurationRecreation.tsx b/src/screens/Vault/VaultConfigurationRecreation.tsx index c84e686c26..753190ca7b 100644 --- a/src/screens/Vault/VaultConfigurationRecreation.tsx +++ b/src/screens/Vault/VaultConfigurationRecreation.tsx @@ -9,6 +9,7 @@ import ImportIcon from 'src/assets/images/import.svg'; import { LocalizationContext } from 'src/context/Localization/LocContext'; import OptionCard from 'src/components/OptionCard'; import RNFS from 'react-native-fs'; +import { getDocumentReadPath } from 'src/utils/documentReadPath'; import { pick, types } from '@react-native-documents/picker'; import Colors from 'src/theme/Colors'; import Text from 'src/components/KeeperText'; @@ -54,7 +55,8 @@ function VaultConfigurationCreation() { type: [types.docx, types.allFiles], allowMultiSelection: false, }); - const content = await RNFS.readFile(response[0].uri, 'utf8'); + const readPath = getDocumentReadPath(response[0].uri, Platform.OS); + const content = await RNFS.readFile(readPath, 'utf8'); navigateToSetup(content); } catch (err) { console.log(err); diff --git a/src/utils/documentReadPath.ts b/src/utils/documentReadPath.ts new file mode 100644 index 0000000000..436f97bf02 --- /dev/null +++ b/src/utils/documentReadPath.ts @@ -0,0 +1,7 @@ +export const getDocumentReadPath = (uri: string, platform: string): string => { + // RNFS strips file:// but does not decode URI escapes before reading on iOS. + if (platform === 'ios' && uri.startsWith('file://')) { + return decodeURIComponent(uri.slice(7)); + } + return uri; +}; diff --git a/tests/documentReadPath.test.ts b/tests/documentReadPath.test.ts new file mode 100644 index 0000000000..02cb4631d0 --- /dev/null +++ b/tests/documentReadPath.test.ts @@ -0,0 +1,41 @@ +import { getDocumentReadPath } from 'src/utils/documentReadPath'; + +describe('getDocumentReadPath', () => { + test.each(['bsms', 'txt'])('decodes brackets in an iOS .%s file URI', (extension) => { + const uri = `file:///Documents/%5BMyWallet%5D_backup.${extension}`; + + expect(getDocumentReadPath(uri, 'ios')).toBe(`/Documents/[MyWallet]_backup.${extension}`); + }); + + test.each(['bsms', 'txt'])('preserves an iOS .%s filename without brackets', (extension) => { + const uri = `file:///Documents/Descriptor.${extension}`; + + expect(getDocumentReadPath(uri, 'ios')).toBe(`/Documents/Descriptor.${extension}`); + }); + + test('decodes spaces, Unicode, and URI-sensitive filename characters', () => { + expect(getDocumentReadPath('file:///Documents/Caf%C3%A9%20%23100%25.bsms', 'ios')).toBe( + '/Documents/Café #100%.bsms' + ); + }); + + test('decodes escapes only once for a filename containing literal percent sequences', () => { + expect(getDocumentReadPath('file:///Documents/%255BMyWallet%255D.bsms', 'ios')).toBe( + '/Documents/%5BMyWallet%5D.bsms' + ); + }); + + test.each([ + 'content://provider/Documents/%5BMyWallet%5D_backup.bsms', + 'file:///Documents/%5BMyWallet%5D_backup.txt', + ])('leaves Android URIs unchanged: %s', (uri) => { + expect(getDocumentReadPath(uri, 'android')).toBe(uri); + }); + + test.each(['/Documents/[MyWallet]_backup.bsms', 'content://provider/document/123'])( + 'leaves iOS inputs without the file scheme unchanged: %s', + (uri) => { + expect(getDocumentReadPath(uri, 'ios')).toBe(uri); + } + ); +}); From 48d3e2cb31917578c6d04f58206dcbee4636a593 Mon Sep 17 00:00:00 2001 From: Anant Date: Mon, 12 Oct 2026 20:56:15 +0530 Subject: [PATCH 130/254] fix: keep latest Realm open active across login races --- src/storage/realm/realm.ts | 77 +++++++++---- tests/recovery/realmOpenGeneration.test.cjs | 118 ++++++++++++++++++++ 2 files changed, 175 insertions(+), 20 deletions(-) create mode 100644 tests/recovery/realmOpenGeneration.test.cjs diff --git a/src/storage/realm/realm.ts b/src/storage/realm/realm.ts index d8b70d8e1b..00e0514d37 100644 --- a/src/storage/realm/realm.ts +++ b/src/storage/realm/realm.ts @@ -8,10 +8,26 @@ export const REALM_FILE = 'keeper.realm'; export class RealmDatabase { private realm: Realm; + private openGeneration = 0; + + private pendingOpenGeneration: number | null = null; + + private staleOpenedRealms = new Set(); + public static file = REALM_FILE; public static schemaVersion = 109; + private releaseStaleOpenResults = () => { + // A later open of the same path may receive the same native Realm handle. + // Wait for the latest open to settle before closing an older result. + if (this.pendingOpenGeneration !== null) return; + for (const opened of this.staleOpenedRealms) { + if (opened !== this.realm && !opened.isClosed) opened.close(); + } + this.staleOpenedRealms.clear(); + }; + /** * initializes/opens realm w/ appropriate configuration * @param {ArrayBuffer|ArrayBufferView|Int8Array} key @@ -21,30 +37,44 @@ export class RealmDatabase { key: ArrayBuffer | ArrayBufferView | Int8Array, path: string ): Promise<{ success: boolean; error?: string }> => { + const generation = ++this.openGeneration; + this.pendingOpenGeneration = generation; + const requestedPath = path || RealmDatabase.file; + const realmConfig: Realm.Configuration = { + path: requestedPath, + schema, + schemaVersion: RealmDatabase.schemaVersion, + encryptionKey: key, + onMigration: (oldRealm, newRealm) => { + runRealmMigrations({ oldRealm, newRealm }); + }, + }; + let opened: Realm; try { - // if the realm instance is already initialized, close it to prevent - // Realm from getting stuck in a weird state (e.g. realm reference object - // exists but the realm is closed) - if (this.realm) { - this.realm.close(); - } - if (path) RealmDatabase.file = path; - const realmConfig: Realm.Configuration = { - path: RealmDatabase.file, - schema, - schemaVersion: RealmDatabase.schemaVersion, - encryptionKey: key, - onMigration: (oldRealm, newRealm) => { - runRealmMigrations({ oldRealm, newRealm }); - }, - }; - this.realm = await Realm.open(realmConfig); - return { success: true }; + opened = await Realm.open(realmConfig); } catch (err) { + if (generation !== this.openGeneration) + return { success: false, error: 'Account changed during database initialization' }; + this.pendingOpenGeneration = null; + this.releaseStaleOpenResults(); console.log('🚀 ~ RealmDatabase ~ err:', err); captureError(err); return { success: false, error: err.toString() }; } + + if (generation !== this.openGeneration) { + this.staleOpenedRealms.add(opened); + this.releaseStaleOpenResults(); + return { success: false, error: 'Account changed during database initialization' }; + } + + const previous = this.realm; + this.realm = opened; + RealmDatabase.file = requestedPath; + this.pendingOpenGeneration = null; + if (previous && previous !== opened && !previous.isClosed) previous.close(); + this.releaseStaleOpenResults(); + return { success: true }; }; /** @@ -59,11 +89,14 @@ export class RealmDatabase { * deletes instance of the database */ public deleteDatabase = (key: ArrayBuffer | ArrayBufferView | Int8Array): boolean => { + this.openGeneration += 1; + this.pendingOpenGeneration = null; const existingSchemaVersion = this.realm ? this.realm.schemaVersion : RealmDatabase.schemaVersion; - if (this.realm) this.realm.close(); + if (this.realm && !this.realm.isClosed) this.realm.close(); this.realm = null; + this.releaseStaleOpenResults(); const realmConfig: Realm.Configuration = { path: RealmDatabase.file, @@ -87,7 +120,11 @@ export class RealmDatabase { * close the database when done with a realm instance to avoid memory leaks. */ public closeDatabase = () => { - if (this.realm) this.realm.close(); + this.openGeneration += 1; + this.pendingOpenGeneration = null; + if (this.realm && !this.realm.isClosed) this.realm.close(); + this.realm = null; + this.releaseStaleOpenResults(); }; /** diff --git a/tests/recovery/realmOpenGeneration.test.cjs b/tests/recovery/realmOpenGeneration.test.cjs new file mode 100644 index 0000000000..5ec09c4a71 --- /dev/null +++ b/tests/recovery/realmOpenGeneration.test.cjs @@ -0,0 +1,118 @@ +const { test } = require('node:test'); +const assert = require('node:assert/strict'); +const { loadModule } = require('./helpers.cjs'); + +function deferred() { + let resolve; + let reject; + const promise = new Promise((done, fail) => { + resolve = done; + reject = fail; + }); + return { promise, resolve, reject }; +} + +function fixture() { + const opens = []; + const deletes = []; + const errors = []; + const Realm = { + open: (config) => { + const pending = deferred(); + opens.push({ config, ...pending }); + return pending.promise; + }, + deleteFile: (config) => deletes.push(config.path), + }; + const { RealmDatabase } = loadModule( + 'src/storage/realm/realm.ts', + { + realm: { default: Realm, __esModule: true }, + 'src/services/sentry': { captureError: (error) => errors.push(error) }, + './schema': { default: [], __esModule: true }, + './migrations': { runRealmMigrations() {} }, + './enum': { RealmSchema: {} }, + }, + { console: { log() {} } } + ); + RealmDatabase.file = 'keeper.realm'; + const db = new RealmDatabase(); + const handle = (name) => ({ + name, + isClosed: false, + closeCount: 0, + close() { + this.closeCount += 1; + this.isClosed = true; + }, + }); + const key = new Uint8Array([1]); + return { db, RealmDatabase, opens, deletes, errors, handle, key }; +} + +test('late A open cannot replace newer B or reset its accepted Realm path', async () => { + const f = fixture(); + const a = f.db.initializeDatabase(f.key, 'A.realm'); + const b = f.db.initializeDatabase(f.key, 'B.realm'); + const aHandle = f.handle('A'); + const bHandle = f.handle('B'); + f.opens[1].resolve(bHandle); + assert.equal((await b).success, true); + f.opens[0].resolve(aHandle); + assert.equal((await a).success, false); + assert.equal(f.db.getDatabase(), bHandle); + assert.equal(f.RealmDatabase.file, 'B.realm'); + assert.equal(aHandle.closeCount, 1); + assert.equal(bHandle.closeCount, 0); +}); + +test('same-path stale open never closes the Realm handle adopted by latest open', async () => { + const f = fixture(); + const first = f.db.initializeDatabase(f.key, 'A.realm'); + const latest = f.db.initializeDatabase(f.key, 'A.realm'); + const shared = f.handle('shared'); + f.opens[0].resolve(shared); + assert.equal((await first).success, false); + assert.equal(shared.closeCount, 0, 'latest open may resolve to the same native handle'); + f.opens[1].resolve(shared); + assert.equal((await latest).success, true); + assert.equal(f.db.getDatabase(), shared); + assert.equal(shared.closeCount, 0); +}); + +test('failed new open preserves the active Realm and its path', async () => { + const f = fixture(); + const first = f.db.initializeDatabase(f.key, 'A.realm'); + const active = f.handle('A'); + f.opens[0].resolve(active); + assert.equal((await first).success, true); + + const failed = f.db.initializeDatabase(f.key, 'B.realm'); + f.opens[1].reject(new Error('disposable failure')); + assert.equal((await failed).success, false); + assert.equal(f.db.getDatabase(), active); + assert.equal(active.closeCount, 0); + assert.equal(f.RealmDatabase.file, 'A.realm'); + assert.equal(f.errors.length, 1); +}); + +test('close and delete invalidate pending opens without adopting their handles', async () => { + const f = fixture(); + const closing = f.db.initializeDatabase(f.key, 'A.realm'); + f.db.closeDatabase(); + const staleAfterClose = f.handle('stale-close'); + f.opens[0].resolve(staleAfterClose); + assert.equal((await closing).success, false); + assert.equal(staleAfterClose.closeCount, 1); + assert.throws(() => f.db.getDatabase(), /database not initialized/); + assert.equal(f.RealmDatabase.file, 'keeper.realm'); + + const deleting = f.db.initializeDatabase(f.key, 'B.realm'); + assert.equal(f.db.deleteDatabase(f.key), true); + const staleAfterDelete = f.handle('stale-delete'); + f.opens[1].resolve(staleAfterDelete); + assert.equal((await deleting).success, false); + assert.equal(staleAfterDelete.closeCount, 1); + assert.throws(() => f.db.getDatabase(), /database not initialized/); + assert.deepEqual(f.deletes, ['keeper.realm']); +}); From df26a80db1eb41a22cd2cbdca4cdcce35035fea1 Mon Sep 17 00:00:00 2001 From: Anant Date: Mon, 12 Oct 2026 21:02:03 +0530 Subject: [PATCH 131/254] Require revisioned route for incremental backup mutations --- src/services/backend/Relay.ts | 4 +- src/services/backup/transport.ts | 67 +++++++++-- src/store/store.ts | 14 ++- tests/recovery/revisionedMutation.test.cjs | 129 +++++++++++++++++++++ 4 files changed, 204 insertions(+), 10 deletions(-) create mode 100644 tests/recovery/revisionedMutation.test.cjs diff --git a/src/services/backend/Relay.ts b/src/services/backend/Relay.ts index 0f9b8c53f4..abc2944829 100644 --- a/src/services/backend/Relay.ts +++ b/src/services/backend/Relay.ts @@ -315,8 +315,8 @@ export default class Relay { updated, }; } catch (err) { - console.log('err', err); - if (err.code) throw new Error(err.code); + captureError(err); + throw new Error('Failed to update backup labels. Check the backup before trying again.'); } }; diff --git a/src/services/backup/transport.ts b/src/services/backup/transport.ts index 9bcf1eaecc..5b60db05db 100644 --- a/src/services/backup/transport.ts +++ b/src/services/backup/transport.ts @@ -4,12 +4,27 @@ import RestClient from '../rest/RestClient'; const queues = new Map>(); const revisions = new Map(); const queuedMutationRevisions = new Map(); +const revisionedOperations = new Set([ + 'updateAppImage', + 'migrateXfps', + 'deleteAppImageEntity', + 'deleteVaults', + 'updateVaultImage', + 'modifyLabels', + 'deleteBackup', +]); let activeAccount: string | undefined; let accountSession = 0; let mayUploadForAccount: (appId: string, explicitChoice: boolean) => boolean = () => false; +let onBackupReconciliationRequired: (appId: string) => void = () => undefined; export const setBackupUploadGuard = (guard: (appId: string, explicitChoice: boolean) => boolean) => { mayUploadForAccount = guard; }; +// A failed or ambiguous write requires a fresh backup check. The encrypted +// payload must never be retried without re-reading local state. +export const setBackupReconciliationHandler = (handler: (appId: string) => void) => { + onBackupReconciliationRequired = handler; +}; export const canAccessBackupAccount = (appId: string, explicitChoice = false) => mayUploadForAccount(appId, explicitChoice); // An A -> B -> A switch must invalidate work captured during the first A session. @@ -27,7 +42,8 @@ export const markBackupMutation = (id: string) => revisions.set(id, backupRevisi export const BACKUP_STALL_MS = 30_000; export const BACKUP_DEADLINE_MS = 10 * 60_000; -export type BackupRequestFailure = 'http-4xx' | 'http-5xx' | 'timeout' | 'network' | 'other'; +export type BackupRequestFailure = + 'conflict' | 'protocol' | 'http-4xx' | 'http-5xx' | 'timeout' | 'network' | 'other'; export class BackupRequestError extends Error { constructor(public readonly category: BackupRequestFailure) { @@ -41,6 +57,7 @@ function requestFailureCategory(error: unknown, aborted: boolean): BackupRequest const failure = error as { response?: { status?: unknown }; status?: unknown; code?: unknown; name?: unknown; message?: unknown }; const status = failure.response?.status ?? failure.status; if (typeof status === 'number') { + if (status === 409) return 'conflict'; if (status >= 400 && status < 500) return 'http-4xx'; if (status >= 500 && status < 600) return 'http-5xx'; return 'other'; @@ -115,13 +132,49 @@ export function backupPost(path: string, body: any, explicitChoice = false, expe if (!canAccessBackupAccount(appId, explicitChoice)) return Promise.reject(new Error('Backup account is not active or opted in')); markBackupMutation(appId); - const result = withBackupSession(appId, () => { + const result = withBackupSession(appId, async () => { // A queued write may outlive an entire A -> B -> A round trip. - if (requestSession !== currentBackupAccountSession()) - throw new Error('Backup account session changed'); - if (!canAccessBackupAccount(appId, explicitChoice)) - throw new Error('Backup account is not active or opted in'); - return boundedBackupPost(path, body); + const assertCurrent = () => { + if (requestSession !== currentBackupAccountSession()) + throw new Error('Backup account session changed'); + if (!canAccessBackupAccount(appId, explicitChoice)) + throw new Error('Backup account is not active or opted in'); + }; + assertCurrent(); + const deadline = Date.now() + BACKUP_DEADLINE_MS; + // Read inside this account's queue, immediately before the mutation. Every + // request carries a server revision, including deletes and label updates. + // A distinct route makes an old relay return 404 instead of silently + // accepting the write without enforcing expectedRevision. + const routeSeparator = path.lastIndexOf('/') + 1; + const operation = path.slice(routeSeparator); + const basePath = path.slice(0, routeSeparator); + try { + if (!revisionedOperations.has(operation)) throw new BackupRequestError('protocol'); + const snapshot = await boundedBackupPost( + `${basePath}getBackupSnapshot`, + { appId }, + deadline + ); + const expectedRevision = snapshot.data?.revision; + if ( + typeof expectedRevision !== 'string' || + !/^[a-f0-9]{64}$/.test(expectedRevision) + ) + throw new BackupRequestError('protocol'); + assertCurrent(); + const response = await boundedBackupPost( + `${basePath}revisionedBackupMutation`, + { ...body, appId, operation, expectedRevision }, + deadline + ); + if (response.data?.preconditionApplied !== true) throw new BackupRequestError('protocol'); + return response; + } catch (error) { + if (error instanceof BackupRequestError) + onBackupReconciliationRequired(appId); + throw error; + } }); queuedMutationRevisions.set(appId, backupRevision(appId)); return result; diff --git a/src/store/store.ts b/src/store/store.ts index 7c596cbeba..e6e698150a 100644 --- a/src/store/store.ts +++ b/src/store/store.ts @@ -30,9 +30,14 @@ import { RealmSchema } from 'src/storage/realm/enum'; import { currentBackupAccountSession, observeBackupAccount, + setBackupReconciliationHandler, setBackupUploadGuard, } from 'src/services/backup/transport'; -import { isAutomaticCloudBackupEnabled } from './reducers/bhr'; +import { + invalidateBackupRepair, + isAutomaticCloudBackupEnabled, + setPendingAllBackup, +} from './reducers/bhr'; import { KeeperApp } from 'src/models/interfaces/KeeperApp'; const appReducer = combineReducers({ @@ -198,5 +203,12 @@ setBackupUploadGuard((appId, explicitChoice) => { } }); +setBackupReconciliationHandler((appId) => { + // A stale or unacknowledged encrypted write cannot be resent safely. Require + // a new comparison with the server snapshot before any repair is offered. + store.dispatch(invalidateBackupRepair(appId)); + store.dispatch(setPendingAllBackup({ appId, pending: true })); +}); + sagaMiddleware.run(rootSaga); export const persistor = persistStore(store); diff --git a/tests/recovery/revisionedMutation.test.cjs b/tests/recovery/revisionedMutation.test.cjs new file mode 100644 index 0000000000..de2e078fb0 --- /dev/null +++ b/tests/recovery/revisionedMutation.test.cjs @@ -0,0 +1,129 @@ +const { test } = require('node:test'); +const assert = require('node:assert/strict'); +const { loadModule } = require('./helpers.cjs'); + +const revision = (generation) => generation.toString(16).padStart(64, '0'); + +function fixture(post) { + const calls = []; + const reconciliations = []; + const transport = loadModule('src/services/backup/transport.ts', { + '../rest/RestClient': { + post: async (path, body) => { + calls.push({ path, body }); + return post(path, body); + }, + }, + }); + transport.setBackupUploadGuard((appId) => appId === 'A'); + transport.setBackupReconciliationHandler((appId) => reconciliations.push(appId)); + transport.observeBackupAccount('A'); + return { transport, calls, reconciliations }; +} + +test('all active incremental operations use the revisioned route and a fresh queued snapshot', async () => { + let generation = 1; + const f = fixture(async (path, body) => { + if (path === 'fixture/getBackupSnapshot') return { data: { revision: revision(generation) } }; + assert.equal(path, 'fixture/revisionedBackupMutation'); + assert.equal(body.appId, 'A'); + assert.equal(body.expectedRevision, revision(generation)); + generation += 1; + return { data: { updated: true, preconditionApplied: true } }; + }); + const operations = [ + 'updateAppImage', 'migrateXfps', 'deleteAppImageEntity', 'deleteVaults', + 'updateVaultImage', 'modifyLabels', 'deleteBackup', + ]; + for (const operation of operations) { + // updateVaultImage still has a legacy appID-only caller. The new route + // needs a canonical top-level appId in addition to the old payload. + const body = operation === 'updateVaultImage' ? { appID: 'A' } : { appId: 'A' }; + const response = await f.transport.backupPost(`fixture/${operation}`, body); + assert.equal(response.data.preconditionApplied, true); + } + assert.deepEqual( + f.calls.map(({ path }) => path), + operations.flatMap(() => ['fixture/getBackupSnapshot', 'fixture/revisionedBackupMutation']) + ); + assert.deepEqual(f.calls.filter(({ path }) => path.endsWith('revisionedBackupMutation')) + .map(({ body }) => body.operation), operations); + assert.deepEqual(f.reconciliations, []); +}); + +test('409 reports a conflict and never retries stale encrypted payload', async () => { + const f = fixture(async (path) => { + if (path.endsWith('getBackupSnapshot')) return { data: { revision: revision(1) } }; + throw { response: { status: 409, data: { error: 'BACKUP_CHANGED' } } }; + }); + await assert.rejects( + f.transport.backupPost('fixture/updateAppImage', { appId: 'A', walletsObject: { w: 'old' } }), + (error) => error.category === 'conflict' && !error.message.includes('old') + ); + assert.deepEqual(f.calls.map(({ path }) => path), [ + 'fixture/getBackupSnapshot', 'fixture/revisionedBackupMutation', + ]); + assert.deepEqual(f.reconciliations, ['A']); +}); + +test('a mixed or old relay rejects the new route without an old-route fallback', async () => { + const f = fixture(async (path) => { + if (path.endsWith('getBackupSnapshot')) return { data: { revision: revision(1) } }; + assert.equal(path, 'fixture/revisionedBackupMutation'); + throw { response: { status: 404 } }; + }); + await assert.rejects( + f.transport.backupPost('fixture/deleteBackup', { appId: 'A' }, true), + (error) => error.category === 'http-4xx' + ); + assert.equal(f.calls.some(({ path }) => path === 'fixture/deleteBackup'), false); + assert.deepEqual(f.reconciliations, ['A']); +}); + +test('missing revision or acknowledgement fails closed and requests reconciliation', async () => { + const missingRevision = fixture(async () => ({ data: {} })); + await assert.rejects( + missingRevision.transport.backupPost('fixture/updateAppImage', { appId: 'A' }), + (error) => error.category === 'protocol' + ); + assert.deepEqual(missingRevision.calls.map(({ path }) => path), ['fixture/getBackupSnapshot']); + assert.deepEqual(missingRevision.reconciliations, ['A']); + + const missingAck = fixture(async (path) => path.endsWith('getBackupSnapshot') + ? { data: { revision: revision(1) } } + : { data: { updated: true } }); + await assert.rejects( + missingAck.transport.backupPost('fixture/updateAppImage', { appId: 'A' }), + (error) => error.category === 'protocol' + ); + assert.deepEqual(missingAck.reconciliations, ['A']); +}); + +test('A to B to A during the revision read never sends an old-session mutation', async () => { + let releaseSnapshot, enteredSnapshot; + const waiting = new Promise((resolve) => { releaseSnapshot = resolve; }); + const entered = new Promise((resolve) => { enteredSnapshot = resolve; }); + const f = fixture(async (path) => { + assert.equal(path, 'fixture/getBackupSnapshot'); + enteredSnapshot(); + await waiting; + return { data: { revision: revision(1) } }; + }); + const pending = f.transport.backupPost('fixture/updateAppImage', { appId: 'A' }); + await entered; + f.transport.observeBackupAccount('B'); + f.transport.observeBackupAccount('A'); + releaseSnapshot(); + await assert.rejects(pending, /Backup account session changed/); + assert.deepEqual(f.calls.map(({ path }) => path), ['fixture/getBackupSnapshot']); +}); + +test('unused legacy full writer is not sent through either route', async () => { + const f = fixture(async () => assert.fail('Legacy writer must not use the network')); + await assert.rejects( + f.transport.backupPost('fixture/backupAllSignersAndVaults', { appId: 'A' }), + (error) => error.category === 'protocol' + ); + assert.deepEqual(f.calls, []); + assert.deepEqual(f.reconciliations, ['A']); +}); From 9e13c8d78fb9470f381afc36b7cbd3dfbb68db13 Mon Sep 17 00:00:00 2001 From: Anant Date: Mon, 12 Oct 2026 21:08:57 +0530 Subject: [PATCH 132/254] Guard post-response account sessions and update backup fixtures --- src/services/backup/transport.ts | 14 ++++- tests/recovery/fingerprintTransport.test.cjs | 54 ++++++++++++-------- tests/recovery/helpers.cjs | 6 +++ tests/recovery/repair.test.cjs | 13 +++-- tests/recovery/revisionedMutation.test.cjs | 31 +++++++++++ 5 files changed, 90 insertions(+), 28 deletions(-) diff --git a/src/services/backup/transport.ts b/src/services/backup/transport.ts index 5b60db05db..da16cb104f 100644 --- a/src/services/backup/transport.ts +++ b/src/services/backup/transport.ts @@ -156,7 +156,7 @@ export function backupPost(path: string, body: any, explicitChoice = false, expe { appId }, deadline ); - const expectedRevision = snapshot.data?.revision; + const expectedRevision = snapshot.data?.revision ?? (snapshot as any).json?.revision; if ( typeof expectedRevision !== 'string' || !/^[a-f0-9]{64}$/.test(expectedRevision) @@ -168,7 +168,17 @@ export function backupPost(path: string, body: any, explicitChoice = false, expe { ...body, appId, operation, expectedRevision }, deadline ); - if (response.data?.preconditionApplied !== true) throw new BackupRequestError('protocol'); + const responseData = response.data || (response as any).json; + if (responseData?.preconditionApplied !== true || responseData?.updated !== true) + throw new BackupRequestError('protocol'); + try { + assertCurrent(); + } catch (error) { + // A result from an earlier A session cannot confirm the currently + // selected A account after an A -> B -> A round trip. + onBackupReconciliationRequired(appId); + throw error; + } return response; } catch (error) { if (error instanceof BackupRequestError) diff --git a/tests/recovery/fingerprintTransport.test.cjs b/tests/recovery/fingerprintTransport.test.cjs index 9076608c39..0cd5d41c39 100644 --- a/tests/recovery/fingerprintTransport.test.cjs +++ b/tests/recovery/fingerprintTransport.test.cjs @@ -47,11 +47,13 @@ for (const repair of [false, true]) { appImage: f.remote, allVaultImages: vaults, labels, revision: revision(), } }; } - if (path === 'migrateXfps') { + if (path === 'revisionedBackupMutation') { + assert.equal(payload.operation, 'migrateXfps'); assert.equal(payload.appId, f.app.id); + assert.equal(payload.expectedRevision, revision()); assert.deepEqual(JSON.parse(JSON.stringify(payload.signerChanges)), signerChanges); events.push('migration-acknowledged'); - return { data: { updated: true } }; + return { data: { updated: true, preconditionApplied: true } }; } assert.equal(path, 'repairAppBackup'); assert.equal(payload.expectedRevision, revision()); @@ -80,7 +82,7 @@ for (const repair of [false, true]) { const migration = relay(f.transport).migrateXfp(f.app.id, signerChanges); const completed = Promise.allSettled([checking, migration]); const requestedRevision = f.transport.backupRevision(f.app.id); - const migrationStartedDuringCheck = f.calls.includes('migrateXfps'); + const migrationStartedDuringCheck = f.calls.includes('revisionedBackupMutation'); release(); const [checked, migrated] = await completed; assert.equal(requestedRevision, previousRevision + 1); @@ -95,11 +97,13 @@ for (const repair of [false, true]) { if (!repair && checked.value === 'verified') { // A read-only retry may verify a new snapshot after queued writes drain. // The invalidated first attempt must never publish verified completion. - assert.deepEqual(f.calls, ['getBackupSnapshot', 'migrateXfps', 'getBackupSnapshot']); - assert.equal(reads, 2); + assert.deepEqual(f.calls, [ + 'getBackupSnapshot', 'getBackupSnapshot', 'revisionedBackupMutation', 'getBackupSnapshot', + ]); + assert.equal(reads, 3); assert.equal(f.phases.filter((phase) => phase === 'verified').length, 1); - const freshRequest = events.indexOf('snapshot-request:2'); - const freshResponse = events.indexOf('snapshot-response:2'); + const freshRequest = events.indexOf('snapshot-request:3'); + const freshResponse = events.indexOf('snapshot-response:3'); const verified = events.indexOf('phase:verified'); assert.ok(acknowledged >= 0 && freshRequest > acknowledged); assert.ok(freshResponse > freshRequest && verified > freshResponse); @@ -107,33 +111,41 @@ for (const repair of [false, true]) { assert.equal(checked.value, 'unverified'); assert.equal(f.phases.includes('verified'), false); assert.deepEqual(f.calls, repair - ? ['getBackupSnapshot', 'repairAppBackup', 'getBackupSnapshot', 'migrateXfps'] - : ['getBackupSnapshot', 'migrateXfps']); + ? [ + 'getBackupSnapshot', 'repairAppBackup', 'getBackupSnapshot', + 'getBackupSnapshot', 'revisionedBackupMutation', + ] + : ['getBackupSnapshot', 'getBackupSnapshot', 'revisionedBackupMutation']); } }); } -test('migration rejection and transport failure preserve responses, invalidate checks, and release the queue', async () => { - let fail = false; +test('migration conflict and transport failure fail closed, invalidate checks, and release the queue', async () => { + let mode = 'conflict'; const errors = []; - const rest = { post: async (_path, _body, _headers, config) => { + const rest = { post: async (path, body, _headers, config) => { assert.ok(config.signal); - if (fail) throw Error('fixture request body must stay redacted'); - return { json: { updated: false, err: 'fixture-rejection' } }; + if (path.endsWith('getBackupSnapshot')) return { json: { revision: 'a'.repeat(64) } }; + assert.equal(path, 'fixture/revisionedBackupMutation'); + assert.equal(body.operation, 'migrateXfps'); + if (mode === 'conflict') throw { response: { status: 409, data: { err: 'fixture-rejection' } } }; + if (mode === 'network') throw Error('fixture request body must stay redacted'); + return { json: { updated: true, preconditionApplied: true } }; } }; const transport = loadModule('src/services/backup/transport.ts', { '../rest/RestClient': rest }); transport.setBackupUploadGuard((appId) => appId === 'fixture-account'); const service = relay(transport, rest, errors); - assert.deepEqual(JSON.parse(JSON.stringify(await service.migrateXfp('fixture-account', signerChanges))), - { updated: false, err: 'fixture-rejection' }); + await assert.rejects(service.migrateXfp('fixture-account', signerChanges), + /^Error: Failed to do migrate the xfp$/); + assert.equal(errors[0].category, 'conflict'); assert.equal(transport.backupRevision('fixture-account'), 1); - fail = true; + mode = 'network'; await assert.rejects(service.migrateXfp('fixture-account', signerChanges), /^Error: Failed to do migrate the xfp$/); - assert.equal(errors.length, 1); - assert.equal(errors[0].message, 'Backup request could not be completed'); + assert.equal(errors.length, 2); + assert.equal(errors[1].message, 'Backup request could not be completed'); assert.equal(transport.backupRevision('fixture-account'), 2); - fail = false; - assert.equal((await service.migrateXfp('fixture-account', signerChanges)).updated, false); + mode = 'success'; + assert.equal((await service.migrateXfp('fixture-account', signerChanges)).updated, true); assert.equal(transport.backupRevision('fixture-account'), 3); }); diff --git a/tests/recovery/helpers.cjs b/tests/recovery/helpers.cjs index c8e03d888b..21c35689d8 100644 --- a/tests/recovery/helpers.cjs +++ b/tests/recovery/helpers.cjs @@ -24,6 +24,7 @@ function harness(options = {}) { const calls = [], phases = [], diagnostics = []; const db = { getObjectByIndex: (schema) => schema === 'KeeperApp' ? app : local[schema] }; const rest = { post: async (path, payload, _headers, config) => { + const revisionBeforeHook = revision(); calls.push(path.replace('fixture/', '')); if (options.post) await options.post({ path, payload, config, remote, app, local }); if (options.adapter) return options.adapter(path.replace('fixture/', ''), payload, config); @@ -31,6 +32,11 @@ function harness(options = {}) { if (options.readError) throw Error('offline'); return { data: options.response || { appImage: remote, allVaultImages: vaults, labels, revision: revision() } }; } + if (path.endsWith('revisionedBackupMutation')) { + if (payload.expectedRevision !== revisionBeforeHook) + throw { response: { status: 409 } }; + return { data: { updated: true, preconditionApplied: true } }; + } if (path.endsWith('updateAppImage')) return { data: { updated: true } }; if (payload.expectedRevision !== revision()) throw Error('Backup changed'); if (options.reject) return { data: { updated: false } }; diff --git a/tests/recovery/repair.test.cjs b/tests/recovery/repair.test.cjs index 513d538924..4d6cca86aa 100644 --- a/tests/recovery/repair.test.cjs +++ b/tests/recovery/repair.test.cjs @@ -271,12 +271,12 @@ test('repair does not retry a failed snapshot request', async () => { test('read-only check retries once after a concurrent incremental backup changes its revision', async () => { let f, changed = false, write; - f = harness({ post: async ({ path, remote }) => { + f = harness({ post: async ({ path, payload, remote }) => { if (path.endsWith('getBackupSnapshot') && !changed) { changed = true; write = f.transport.backupPost('fixture/updateAppImage', { appId: f.app.id }); } - if (path.endsWith('updateAppImage')) { + if (path.endsWith('revisionedBackupMutation') && payload.operation === 'updateAppImage') { remote.wallets.wallet = encryption.encrypt( encryption.generateEncryptionKey(f.app.primarySeed), JSON.stringify(wallet()) ); @@ -285,7 +285,9 @@ test('read-only check retries once after a concurrent incremental backup changes f.local.Wallet.push(wallet()); assert.equal(await f.run(), 'verified'); await write; - assert.deepEqual(f.calls, ['getBackupSnapshot', 'updateAppImage', 'getBackupSnapshot']); + assert.deepEqual(f.calls, [ + 'getBackupSnapshot', 'getBackupSnapshot', 'revisionedBackupMutation', 'getBackupSnapshot', + ]); assert.deepEqual(f.phases, ['checking', 'checking', 'verified']); assert.deepEqual(f.diagnostics, []); }); @@ -304,14 +306,15 @@ test('early revision change without a queued write stays unverified', async () = test('continuous queued writes limit a read-only check to one retry', async () => { let f; const writes = []; + let reads = 0; f = harness({ post: async ({ path }) => { - if (path.endsWith('getBackupSnapshot')) + if (path.endsWith('getBackupSnapshot') && [1, 3].includes(++reads)) writes.push(f.transport.backupPost('fixture/updateAppImage', { appId: f.app.id })); } }); f.local.Wallet.push(wallet()); assert.equal(await f.run(), 'unverified'); await Promise.all(writes); - assert.equal(f.calls.filter((path) => path === 'getBackupSnapshot').length, 2); + assert.equal(f.calls.filter((path) => path === 'getBackupSnapshot').length, 4); assert.deepEqual(f.phases, ['checking', 'checking', 'unverified']); }); diff --git a/tests/recovery/revisionedMutation.test.cjs b/tests/recovery/revisionedMutation.test.cjs index de2e078fb0..59bcda672b 100644 --- a/tests/recovery/revisionedMutation.test.cjs +++ b/tests/recovery/revisionedMutation.test.cjs @@ -97,6 +97,15 @@ test('missing revision or acknowledgement fails closed and requests reconciliati (error) => error.category === 'protocol' ); assert.deepEqual(missingAck.reconciliations, ['A']); + + const rejected = fixture(async (path) => path.endsWith('getBackupSnapshot') + ? { data: { revision: revision(1) } } + : { data: { updated: false, preconditionApplied: true } }); + await assert.rejects( + rejected.transport.backupPost('fixture/updateAppImage', { appId: 'A' }), + (error) => error.category === 'protocol' + ); + assert.deepEqual(rejected.reconciliations, ['A']); }); test('A to B to A during the revision read never sends an old-session mutation', async () => { @@ -118,6 +127,28 @@ test('A to B to A during the revision read never sends an old-session mutation', assert.deepEqual(f.calls.map(({ path }) => path), ['fixture/getBackupSnapshot']); }); +test('A to B to A during an acknowledged write still requires readback', async () => { + let releaseMutation, enteredMutation; + const waiting = new Promise((resolve) => { releaseMutation = resolve; }); + const entered = new Promise((resolve) => { enteredMutation = resolve; }); + const f = fixture(async (path) => { + if (path.endsWith('getBackupSnapshot')) return { json: { revision: revision(1) } }; + enteredMutation(); + await waiting; + return { json: { updated: true, preconditionApplied: true } }; + }); + const pending = f.transport.backupPost('fixture/updateAppImage', { appId: 'A' }); + await entered; + f.transport.observeBackupAccount('B'); + f.transport.observeBackupAccount('A'); + releaseMutation(); + await assert.rejects(pending, /Backup account session changed/); + assert.deepEqual(f.calls.map(({ path }) => path), [ + 'fixture/getBackupSnapshot', 'fixture/revisionedBackupMutation', + ]); + assert.deepEqual(f.reconciliations, ['A']); +}); + test('unused legacy full writer is not sent through either route', async () => { const f = fixture(async () => assert.fail('Legacy writer must not use the network')); await assert.rejects( From 8b779d64b86bbd44c35272ccaff9ce24bfe6aa4e Mon Sep 17 00:00:00 2001 From: Anant Date: Mon, 12 Oct 2026 21:33:42 +0530 Subject: [PATCH 133/254] Fail closed on unsafe backup writes and guard revisioned repair --- src/services/backup/intent.ts | 6 + src/services/backup/repair.ts | 94 +++++++++- src/services/backup/transport.ts | 112 +++++------ src/store/store.ts | 4 + tests/recovery/accountBinding.test.cjs | 6 +- .../accountSessionDestructive.test.cjs | 2 +- tests/recovery/fingerprintTransport.test.cjs | 162 ++++------------ tests/recovery/helpers.cjs | 12 +- tests/recovery/manualCoinChoices.test.cjs | 7 +- tests/recovery/repair.test.cjs | 126 +++++++++---- tests/recovery/repairAccountSession.test.cjs | 2 +- tests/recovery/revisionedMutation.test.cjs | 175 ++++++------------ tests/recovery/sagaState.test.cjs | 6 +- tests/recovery/sharedVaultRepair.test.cjs | 65 ++++++- tests/recovery/transport.test.cjs | 2 +- tests/recovery/vaultRegistration.test.cjs | 37 ++-- 16 files changed, 419 insertions(+), 399 deletions(-) create mode 100644 src/services/backup/intent.ts diff --git a/src/services/backup/intent.ts b/src/services/backup/intent.ts new file mode 100644 index 0000000000..afc206ea5f --- /dev/null +++ b/src/services/backup/intent.ts @@ -0,0 +1,6 @@ +export type BackupWriteIntent = Readonly<{ + appId: string; + accountSession: number; + baseRevision: string; + baselineEpoch: number; +}>; diff --git a/src/services/backup/repair.ts b/src/services/backup/repair.ts index 7ea4465c47..69056913d1 100644 --- a/src/services/backup/repair.ts +++ b/src/services/backup/repair.ts @@ -15,7 +15,9 @@ import { backupRevision, boundedBackupPost, canAccessBackupAccount, + clearVerifiedRemoteRevision, currentBackupAccountSession, + recordVerifiedRemoteRevision, withBackupSession, } from './transport'; import { @@ -61,6 +63,21 @@ const inFlight = new Map< >(); const clone = (value: any) => JSON.parse(JSON.stringify(value)); +// Replacing the whole Relay image is safe without separate replacement consent +// only when it adds records to the remote image. A remote-only record or a +// changed recovery payload could contain the user's sole recoverable copy. +function replacementWouldDiscardRecovery(local: BackupImage, remote: BackupImage) { + for (const kind of Object.keys(remote) as (keyof BackupImage)[]) { + for (const [id, stored] of Object.entries(remote[kind])) { + const current = local[kind][id]; + if (!current || + canonical(recoveryContent(kind, current)) !== canonical(recoveryContent(kind, stored))) + return true; + } + } + return false; +} + export async function captureBackupImage(checkpoint = () => {}): Promise { const image: BackupImage = { wallets: {}, signers: {}, vaults: {}, nodes: {}, labels: {} }; // A VaultSigner is shared across the vaults that use its xpub. Read the @@ -184,6 +201,7 @@ export function inspectBackup( if (running.phase) onPhase(running.phase); return running.promise; } + clearVerifiedRemoteRevision(appId); const listeners = new Set([onPhase]); const notify = (phase: RepairPhase) => { const active = inFlight.get(operationKey); @@ -222,6 +240,9 @@ export function inspectBackup( assertCurrent(); let expectedRevision: string; let needsVaultRepair = false; + let needsLabelRepair = false; + let unavailableVaultIds: string[] = []; + let unavailableLabelIds: string[] = []; const read = async (readback = false) => { stage = readback ? 'repair-readback' : 'snapshot-request'; const response = await boundedBackupPost( @@ -233,6 +254,8 @@ export function inspectBackup( assertCurrent(); if (!/^[a-f0-9]{64}$/.test(response.data?.revision)) throw new Error('Backup snapshot revision unavailable'); + if (response.data?.revisionedMutations !== true) + throw new Error('Revisioned backup protocol unavailable'); expectedRevision = response.data.revision; const unavailable = response.data.unavailableVaultIds ?? []; if (!Array.isArray(unavailable) || new Set(unavailable).size !== unavailable.length) @@ -251,16 +274,40 @@ export function inspectBackup( )) ) throw new Error('Invalid unavailable backup references'); + unavailableVaultIds = unavailable; + const unavailableLabels = response.data.unavailableLabelIds ?? []; + const labelReferences = response.data.appImage?.labels; + if ( + !Array.isArray(unavailableLabels) || + new Set(unavailableLabels).size !== unavailableLabels.length || + (unavailableLabels.length && + (!Array.isArray(labelReferences) || + new Set(labelReferences).size !== labelReferences.length || + unavailableLabels.some( + (id) => + typeof id !== 'string' || + !id || + !labelReferences.includes(id) || + response.data.labels?.some((record) => record.id === id) + ))) + ) + throw new Error('Invalid unavailable backup labels'); + unavailableLabelIds = unavailableLabels; // A legacy shared-wallet image may have been overwritten by another // participant. The relay withholds that participant's ciphertext. Only // this explicit backup check may compare the remaining records; clean // restore still rejects the incomplete reference list. - const availableResponse = unavailable.length + const availableResponse = unavailable.length || unavailableLabels.length ? { ...response.data, appImage: { ...response.data.appImage, - vaults: references.filter((id) => !unavailable.includes(id)), + vaults: unavailable.length + ? references.filter((id) => !unavailable.includes(id)) + : response.data.appImage.vaults, + labels: unavailableLabels.length + ? labelReferences.filter((id) => !unavailableLabels.includes(id)) + : response.data.appImage.labels, }, } : response.data; @@ -275,13 +322,16 @@ export function inspectBackup( typeof record.isArchived === 'boolean' && record.isArchived !== !!decoded.vaults[record.vaultId]?.archived ); + needsLabelRepair = unavailableLabels.length > 0; return decoded; }; const remote = await read(); stage = 'comparison'; const contentComparison = await compareImages(local, remote, assertCurrent); const comparison = - contentComparison === 'matched' && needsVaultRepair ? 'different' : contentComparison; + contentComparison === 'matched' && (needsVaultRepair || needsLabelRepair) + ? 'different' + : contentComparison; assertCurrent(); if (comparison === 'conflict') { notify('conflict'); @@ -292,6 +342,18 @@ export function inspectBackup( return 'different'; } if (comparison === 'different') { + if ( + replacementWouldDiscardRecovery(local, remote) || + unavailableVaultIds.some((id) => !local.vaults[id]) || + unavailableLabelIds.some( + (id) => !Object.keys(local.labels).some( + (localId) => hash256(hash256(key + localId)) === id + ) + ) + ) { + notify('conflict'); + return 'conflict'; + } stage = 'repair-preparation'; notify('preparing'); const payload = await encodeImage(app, local, assertCurrent); @@ -305,25 +367,36 @@ export function inspectBackup( assertCurrent(); notify('uploading'); stage = 'repair-upload'; + let revisionedRepairAcknowledged = false; try { const response = await boundedBackupPost( - `${config.RELAY}repairAppBackup`, - // This explicit action replaces the server snapshot with the - // current local state, including intentional deletions. The relay - // still checks the revision atomically before accepting it. - { ...payload, expectedRevision, replaceCurrentState: true }, + `${config.RELAY}revisionedBackupMutation`, + // This explicit action replaces the server snapshot only after + // proving it preserves the remote recovery content. The relay + // checks the revision and activates the incremental-write fence + // atomically before accepting it. Old relays return 404 here. + { + ...payload, + operation: 'repairAppBackup', + expectedRevision, + replaceCurrentState: true, + }, deadline ); - if (response.data?.updated !== true) throw new Error('Backup rejected'); + revisionedRepairAcknowledged = + response.data?.updated === true && response.data?.preconditionApplied === true; } catch { - // A lost response is not proof the upload failed. Always read back. + // A lost response is not proof the upload failed. Read back, but do + // not mark this repair verified without an acknowledged v2 write. } notify('verifying'); stage = 'repair-readback'; const verifiedRemote = await read(true); stage = 'repair-readback'; if ( + !revisionedRepairAcknowledged || needsVaultRepair || + needsLabelRepair || (await compareImages(local, verifiedRemote, assertCurrent)) !== 'matched' ) throw new Error('Backup not verified'); @@ -335,6 +408,7 @@ export function inspectBackup( ) throw new Error('Local backup changed'); assertCurrent(); + recordVerifiedRemoteRevision(appId, expectedRevision, expectedSession); notify('verified'); return 'verified'; } catch (error) { diff --git a/src/services/backup/transport.ts b/src/services/backup/transport.ts index da16cb104f..475d7a1597 100644 --- a/src/services/backup/transport.ts +++ b/src/services/backup/transport.ts @@ -1,18 +1,12 @@ import RestClient from '../rest/RestClient'; +import type { BackupWriteIntent } from './intent'; // One queue per account covers repair, incremental writes, labels and deletion. const queues = new Map>(); const revisions = new Map(); const queuedMutationRevisions = new Map(); -const revisionedOperations = new Set([ - 'updateAppImage', - 'migrateXfps', - 'deleteAppImageEntity', - 'deleteVaults', - 'updateVaultImage', - 'modifyLabels', - 'deleteBackup', -]); +const verifiedRemoteRevisions = new Map(); +const baselineEpochs = new Map(); let activeAccount: string | undefined; let accountSession = 0; let mayUploadForAccount: (appId: string, explicitChoice: boolean) => boolean = () => false; @@ -31,6 +25,7 @@ export const canAccessBackupAccount = (appId: string, explicitChoice = false) => // The Redux store observes every selected-account transition, including resets. export function observeBackupAccount(appId?: string) { if (appId !== activeAccount) { + for (const id of verifiedRemoteRevisions.keys()) clearVerifiedRemoteRevision(id); activeAccount = appId; accountSession += 1; } @@ -38,7 +33,32 @@ export function observeBackupAccount(appId?: string) { export const currentBackupAccountSession = () => accountSession; export const backupRevision = (id: string) => revisions.get(id) || 0; export const backupQueuedRevision = (id: string) => queuedMutationRevisions.get(id) || 0; -export const markBackupMutation = (id: string) => revisions.set(id, backupRevision(id) + 1); +export function clearVerifiedRemoteRevision(appId: string) { + verifiedRemoteRevisions.delete(appId); + baselineEpochs.set(appId, (baselineEpochs.get(appId) || 0) + 1); +} +export function recordVerifiedRemoteRevision(appId: string, revision: string, session: number) { + if ( + !/^[a-f0-9]{64}$/.test(revision) || + session !== accountSession || + appId !== activeAccount + ) return; + clearVerifiedRemoteRevision(appId); + verifiedRemoteRevisions.set(appId, { + revision, session, epoch: baselineEpochs.get(appId) || 0, + }); +} +export function captureBackupWriteIntent(appId: string): BackupWriteIntent | undefined { + const verified = verifiedRemoteRevisions.get(appId); + if (!verified || appId !== activeAccount || verified.session !== accountSession) return undefined; + return { + appId, accountSession: verified.session, + baseRevision: verified.revision, baselineEpoch: verified.epoch, + }; +} +export const markBackupMutation = (id: string) => { + revisions.set(id, backupRevision(id) + 1); +}; export const BACKUP_STALL_MS = 30_000; export const BACKUP_DEADLINE_MS = 10 * 60_000; @@ -123,69 +143,25 @@ export async function boundedBackupPost( } } -export function backupPost(path: string, body: any, explicitChoice = false, expectedSession?: number) { +export function backupPost( + _path: string, body: any, explicitChoice = false, expectedSession?: number +): Promise<{ data: any; json?: any }> { const appId = body.appId || body.appID; if (!appId) return Promise.reject(new Error('Missing backup account')); + // Every attempted incremental write invalidates an outstanding inspection + // and leaves a visible pending state, including pre-send session/guard + // failures from direct hooks and upgrade workers. + markBackupMutation(appId); + clearVerifiedRemoteRevision(appId); + onBackupReconciliationRequired(appId); const requestSession = expectedSession ?? currentBackupAccountSession(); if (requestSession !== currentBackupAccountSession()) return Promise.reject(new Error('Backup account session changed')); if (!canAccessBackupAccount(appId, explicitChoice)) return Promise.reject(new Error('Backup account is not active or opted in')); - markBackupMutation(appId); - const result = withBackupSession(appId, async () => { - // A queued write may outlive an entire A -> B -> A round trip. - const assertCurrent = () => { - if (requestSession !== currentBackupAccountSession()) - throw new Error('Backup account session changed'); - if (!canAccessBackupAccount(appId, explicitChoice)) - throw new Error('Backup account is not active or opted in'); - }; - assertCurrent(); - const deadline = Date.now() + BACKUP_DEADLINE_MS; - // Read inside this account's queue, immediately before the mutation. Every - // request carries a server revision, including deletes and label updates. - // A distinct route makes an old relay return 404 instead of silently - // accepting the write without enforcing expectedRevision. - const routeSeparator = path.lastIndexOf('/') + 1; - const operation = path.slice(routeSeparator); - const basePath = path.slice(0, routeSeparator); - try { - if (!revisionedOperations.has(operation)) throw new BackupRequestError('protocol'); - const snapshot = await boundedBackupPost( - `${basePath}getBackupSnapshot`, - { appId }, - deadline - ); - const expectedRevision = snapshot.data?.revision ?? (snapshot as any).json?.revision; - if ( - typeof expectedRevision !== 'string' || - !/^[a-f0-9]{64}$/.test(expectedRevision) - ) - throw new BackupRequestError('protocol'); - assertCurrent(); - const response = await boundedBackupPost( - `${basePath}revisionedBackupMutation`, - { ...body, appId, operation, expectedRevision }, - deadline - ); - const responseData = response.data || (response as any).json; - if (responseData?.preconditionApplied !== true || responseData?.updated !== true) - throw new BackupRequestError('protocol'); - try { - assertCurrent(); - } catch (error) { - // A result from an earlier A session cannot confirm the currently - // selected A account after an A -> B -> A round trip. - onBackupReconciliationRequired(appId); - throw error; - } - return response; - } catch (error) { - if (error instanceof BackupRequestError) - onBackupReconciliationRequired(appId); - throw error; - } - }); - queuedMutationRevisions.set(appId, backupRevision(appId)); - return result; + // The encrypted payload may have been prepared before a newer local change + // or an explicit repair. No revision captured here can prove that payload's + // freshness. Keep local data and require a new read-only check or explicit + // repair until the source intent is versioned end to end. + return Promise.reject(new BackupRequestError('protocol')); } diff --git a/src/store/store.ts b/src/store/store.ts index e6e698150a..835bd567ae 100644 --- a/src/store/store.ts +++ b/src/store/store.ts @@ -208,6 +208,10 @@ setBackupReconciliationHandler((appId) => { // a new comparison with the server snapshot before any repair is offered. store.dispatch(invalidateBackupRepair(appId)); store.dispatch(setPendingAllBackup({ appId, pending: true })); + // The caller may still need to commit its local Realm change after this + // rejection. Starting an inspection here could match the old local image + // and clear pending before that commit. Keep pending until the caller has + // settled and a later inspection captures the resulting local state. }); sagaMiddleware.run(rootSaga); diff --git a/tests/recovery/accountBinding.test.cjs b/tests/recovery/accountBinding.test.cjs index c3ebdfdb25..e0ff768017 100644 --- a/tests/recovery/accountBinding.test.cjs +++ b/tests/recovery/accountBinding.test.cjs @@ -191,7 +191,7 @@ test('queued A transport cannot execute after opted-in B becomes active', async active = 'B'; release(); await busy; - await assert.rejects(pending, /not active or opted in/); + await assert.rejects(pending, (error) => error.category === 'protocol'); assert.deepEqual(calls, [], 'the queued request must never reach the network'); }); @@ -222,7 +222,7 @@ test('queued A ciphertext cannot replace a newer server wallet after A to B to A transport.observeBackupAccount?.(active); release(); await busy; - await assert.rejects(pending, /session changed/); + await assert.rejects(pending, (error) => error.category === 'protocol'); assert.equal(remote.wallets.shared, 'newer-A-ciphertext'); assert.equal(calls.length, 0, 'stale ciphertext must never reach Relay'); }); @@ -381,7 +381,7 @@ test('explicit backup can repair an off account only while that account stays ac f.local.Wallet.push(wallet('explicit', enums.NetworkType.MAINNET)); f.transport.setBackupUploadGuard((id, explicitChoice) => id === f.app.id && explicitChoice); assert.equal(await f.repair.inspectBackup(f.app.id, true, () => {}, true), 'verified'); - assert.deepEqual(f.calls, ['getBackupSnapshot', 'repairAppBackup', 'getBackupSnapshot']); + assert.deepEqual(f.calls, ['getBackupSnapshot', 'revisionedBackupMutation', 'getBackupSnapshot']); }); test('unconfirmed server deletion retains account consent and reports failure', async () => { diff --git a/tests/recovery/accountSessionDestructive.test.cjs b/tests/recovery/accountSessionDestructive.test.cjs index 2c3c4a096f..7528a4b895 100644 --- a/tests/recovery/accountSessionDestructive.test.cjs +++ b/tests/recovery/accountSessionDestructive.test.cjs @@ -144,7 +144,7 @@ test('queued explicit whole-backup delete cannot clear newer A data after A to B release(); await busy; - await assert.rejects(deleting, /session changed/); + await assert.rejects(deleting, (error) => error.category === 'protocol'); assert.deepEqual(requests, []); assert.equal(remote.wallets.shared, 'newer-A-ciphertext'); }); diff --git a/tests/recovery/fingerprintTransport.test.cjs b/tests/recovery/fingerprintTransport.test.cjs index 0cd5d41c39..2bc836f93b 100644 --- a/tests/recovery/fingerprintTransport.test.cjs +++ b/tests/recovery/fingerprintTransport.test.cjs @@ -1,16 +1,15 @@ const { test } = require('node:test'); const assert = require('node:assert/strict'); -const crypto = require('node:crypto'); const { loadModule, harness, wallet } = require('./helpers.cjs'); const signerChanges = [{ oldSignerId: 'fixture-old', newSignerId: 'fixture-new', newSignerDetails: 'fixture-ciphertext', }]; -function relay(transport, rest = { post: () => assert.fail('Migration bypassed backup transport') }, errors = []) { +function relay(transport, errors = []) { return loadModule('src/services/backend/Relay.ts', { '../backup/transport': transport, - '../rest/RestClient': rest, + '../rest/RestClient': { post: () => assert.fail('Migration bypassed backup transport') }, '../sentry': { captureError: (error) => errors.push(error) }, 'src/services/wallets/enums': {}, 'src/utils/service-utilities/config': { RELAY: 'fixture/' }, @@ -19,133 +18,54 @@ function relay(transport, rest = { post: () => assert.fail('Migration bypassed b } for (const repair of [false, true]) { - test(`fingerprint migration waits for ${repair ? 'repair readback' : 'inspection'} and invalidates its completion`, async (t) => { - let armed = false, reads = 0, release, entered; - const events = []; - const readGate = new Promise((resolve) => { release = resolve; }); - const readStarted = new Promise((resolve) => { entered = resolve; }); + test(`fingerprint migration fails closed during ${repair ? 'repair readback' : 'inspection'}`, async (t) => { + let release, entered, reads = 0, armed = false; + const gate = new Promise((resolve) => { release = resolve; }); + const started = new Promise((resolve) => { entered = resolve; }); t.after(() => release()); - let f; - let vaults = [], labels = []; - const revision = () => crypto.createHash('sha256') - .update(JSON.stringify({ remote: f.remote, vaults, labels })).digest('hex'); - f = harness({ - post: async ({ path }) => { - if (armed && path.endsWith('getBackupSnapshot')) { - events.push(`snapshot-request:${++reads}`); - if (reads === (repair ? 2 : 1)) { - entered(); - await readGate; - } - } - }, - adapter: (path, payload, config) => { - assert.ok(config.signal, 'Every backup request must use bounded transport'); - if (path === 'getBackupSnapshot') { - if (armed) events.push(`snapshot-response:${reads}`); - return { data: { - appImage: f.remote, allVaultImages: vaults, labels, revision: revision(), - } }; - } - if (path === 'revisionedBackupMutation') { - assert.equal(payload.operation, 'migrateXfps'); - assert.equal(payload.appId, f.app.id); - assert.equal(payload.expectedRevision, revision()); - assert.deepEqual(JSON.parse(JSON.stringify(payload.signerChanges)), signerChanges); - events.push('migration-acknowledged'); - return { data: { updated: true, preconditionApplied: true } }; - } - assert.equal(path, 'repairAppBackup'); - assert.equal(payload.expectedRevision, revision()); - f.remote.wallets = { ...payload.walletObject }; - f.remote.signers = { ...payload.signersObject }; - f.remote.nodes = payload.nodes; - vaults = Object.values(payload.vaultObject); - labels = payload.labels; - f.remote.vaults = vaults.map((record) => record.vaultId); - f.remote.labels = labels.map((record) => record.id); - return { data: { updated: true } }; - }, - }); - f.local.Wallet.push(wallet()); - assert.equal(await f.run(true), 'verified'); - if (repair) f.local.Wallet[0].presentationData.name = 'Updated fixture wallet'; - f.calls.length = 0; - f.phases.length = 0; + const f = harness({ post: async ({ path }) => { + if (armed && path.endsWith('getBackupSnapshot') && ++reads === (repair ? 2 : 1)) { + entered(); + await gate; + } + } }); + if (repair) f.local.Wallet.push(wallet()); + const reconciliation = []; + f.transport.setBackupReconciliationHandler((id) => reconciliation.push(id)); armed = true; - const checking = f.repair.inspectBackup(f.app.id, repair, (phase) => { - f.phases.push(phase); - events.push(`phase:${phase}`); - }); - await readStarted; + const checking = f.run(repair); + await started; const previousRevision = f.transport.backupRevision(f.app.id); - const migration = relay(f.transport).migrateXfp(f.app.id, signerChanges); - const completed = Promise.allSettled([checking, migration]); - const requestedRevision = f.transport.backupRevision(f.app.id); - const migrationStartedDuringCheck = f.calls.includes('revisionedBackupMutation'); + await assert.rejects( + relay(f.transport).migrateXfp(f.app.id, signerChanges), + /Failed to do migrate the xfp/ + ); + assert.equal(f.transport.backupRevision(f.app.id), previousRevision + 1); + assert.deepEqual(reconciliation, [f.app.id]); release(); - const [checked, migrated] = await completed; - assert.equal(requestedRevision, previousRevision + 1); - assert.equal(migrationStartedDuringCheck, false); - assert.equal(checked.status, 'fulfilled'); - assert.equal(migrated.status, 'fulfilled'); - assert.equal(migrated.value.updated, true); - const heldResponse = events.indexOf(`snapshot-response:${repair ? 2 : 1}`); - const acknowledged = events.indexOf('migration-acknowledged'); - assert.ok(heldResponse >= 0 && acknowledged > heldResponse, - 'Fingerprint migration must wait for the held backup response'); - if (!repair && checked.value === 'verified') { - // A read-only retry may verify a new snapshot after queued writes drain. - // The invalidated first attempt must never publish verified completion. - assert.deepEqual(f.calls, [ - 'getBackupSnapshot', 'getBackupSnapshot', 'revisionedBackupMutation', 'getBackupSnapshot', - ]); - assert.equal(reads, 3); - assert.equal(f.phases.filter((phase) => phase === 'verified').length, 1); - const freshRequest = events.indexOf('snapshot-request:3'); - const freshResponse = events.indexOf('snapshot-response:3'); - const verified = events.indexOf('phase:verified'); - assert.ok(acknowledged >= 0 && freshRequest > acknowledged); - assert.ok(freshResponse > freshRequest && verified > freshResponse); - } else { - assert.equal(checked.value, 'unverified'); - assert.equal(f.phases.includes('verified'), false); - assert.deepEqual(f.calls, repair - ? [ - 'getBackupSnapshot', 'repairAppBackup', 'getBackupSnapshot', - 'getBackupSnapshot', 'revisionedBackupMutation', - ] - : ['getBackupSnapshot', 'getBackupSnapshot', 'revisionedBackupMutation']); - } + assert.equal(await checking, 'unverified'); + assert.equal(f.phases.includes('verified'), false); + assert.equal(f.calls.some((path) => path === 'migrateXfps'), false); + assert.equal(f.calls.filter((path) => path === 'revisionedBackupMutation').length, repair ? 1 : 0); }); } -test('migration conflict and transport failure fail closed, invalidate checks, and release the queue', async () => { - let mode = 'conflict'; +test('direct migration attempts never send ciphertext and keep redacted errors', async () => { const errors = []; - const rest = { post: async (path, body, _headers, config) => { - assert.ok(config.signal); - if (path.endsWith('getBackupSnapshot')) return { json: { revision: 'a'.repeat(64) } }; - assert.equal(path, 'fixture/revisionedBackupMutation'); - assert.equal(body.operation, 'migrateXfps'); - if (mode === 'conflict') throw { response: { status: 409, data: { err: 'fixture-rejection' } } }; - if (mode === 'network') throw Error('fixture request body must stay redacted'); - return { json: { updated: true, preconditionApplied: true } }; - } }; - const transport = loadModule('src/services/backup/transport.ts', { '../rest/RestClient': rest }); + const transport = loadModule('src/services/backup/transport.ts', { + '../rest/RestClient': { post: () => assert.fail('Migration reached Relay') }, + }); transport.setBackupUploadGuard((appId) => appId === 'fixture-account'); - const service = relay(transport, rest, errors); - await assert.rejects(service.migrateXfp('fixture-account', signerChanges), - /^Error: Failed to do migrate the xfp$/); - assert.equal(errors[0].category, 'conflict'); - assert.equal(transport.backupRevision('fixture-account'), 1); - mode = 'network'; - await assert.rejects(service.migrateXfp('fixture-account', signerChanges), - /^Error: Failed to do migrate the xfp$/); + const pending = []; + transport.setBackupReconciliationHandler((id) => pending.push(id)); + const service = relay(transport, errors); + for (let index = 0; index < 2; index++) { + await assert.rejects(service.migrateXfp('fixture-account', signerChanges), + /Failed to do migrate the xfp/); + } assert.equal(errors.length, 2); - assert.equal(errors[1].message, 'Backup request could not be completed'); + assert.equal(errors[0].category, 'protocol'); + assert.equal(errors[0].message, 'Backup request could not be completed'); assert.equal(transport.backupRevision('fixture-account'), 2); - mode = 'success'; - assert.equal((await service.migrateXfp('fixture-account', signerChanges)).updated, true); - assert.equal(transport.backupRevision('fixture-account'), 3); + assert.deepEqual(pending, ['fixture-account', 'fixture-account']); }); diff --git a/tests/recovery/helpers.cjs b/tests/recovery/helpers.cjs index 21c35689d8..29dba27fda 100644 --- a/tests/recovery/helpers.cjs +++ b/tests/recovery/helpers.cjs @@ -30,9 +30,12 @@ function harness(options = {}) { if (options.adapter) return options.adapter(path.replace('fixture/', ''), payload, config); if (path.endsWith('getBackupSnapshot')) { if (options.readError) throw Error('offline'); - return { data: options.response || { appImage: remote, allVaultImages: vaults, labels, revision: revision() } }; + return { data: options.response || { + appImage: remote, allVaultImages: vaults, labels, revision: revision(), + revisionedMutations: true, + } }; } - if (path.endsWith('revisionedBackupMutation')) { + if (path.endsWith('revisionedBackupMutation') && payload.operation !== 'repairAppBackup') { if (payload.expectedRevision !== revisionBeforeHook) throw { response: { status: 409 } }; return { data: { updated: true, preconditionApplied: true } }; @@ -45,7 +48,10 @@ function harness(options = {}) { remote.vaults = vaults.map(v => v.vaultId); remote.labels = labels.map(v => v.id); if (options.corruptWrite) remote.wallets = {}; if (options.lostResponse) throw Error('response lost after commit'); - return { data: { updated: true } }; + return { data: { + updated: true, + ...(path.endsWith('revisionedBackupMutation') ? { preconditionApplied: true } : {}), + } }; } }; const clock = options.now ? { Date: { now: options.now } } : {}; const transport = loadModule('src/services/backup/transport.ts', { '../rest/RestClient': rest }, clock); diff --git a/tests/recovery/manualCoinChoices.test.cjs b/tests/recovery/manualCoinChoices.test.cjs index e1e43f3168..63a16917b1 100644 --- a/tests/recovery/manualCoinChoices.test.cjs +++ b/tests/recovery/manualCoinChoices.test.cjs @@ -60,15 +60,14 @@ for (const kind of ['wallets', 'vaults']) test(`${kind}: compare manual choices assert.equal(await image.compareImages(local, remote), 'different'); }); -test('upgrade inspection detects a missing manual choice and repair includes it in encrypted readback', async () => { +test('upgrade inspection detects a missing manual choice and blocks unconsented replacement', async () => { const f = harness(); f.local.Wallet.push(withCoins()); assert.equal(await f.run(true), 'verified'); f.local.Wallet[0].specs.confirmedUTXOs[0] = coin(); assert.equal(await f.run(), 'different'); - assert.equal(await f.run(true), 'verified'); + assert.equal(await f.run(true), 'conflict'); const restored = JSON.parse(encryption.decrypt(encryption.generateEncryptionKey(f.app.primarySeed), f.remote.wallets['manual-choice'])); - assert.equal(restored.specs.confirmedUTXOs[0].spendability, 'doNotSpend'); - assert.equal(restored.specs.confirmedUTXOs[0].isManualOverride, true); + assert.equal(restored.specs.confirmedUTXOs[0].isManualOverride, false); }); for (const kind of ['Wallet', 'Vault']) test(`${kind}: manual choice persists before incremental backup and invalidates prior verification`, async () => { diff --git a/tests/recovery/repair.test.cjs b/tests/recovery/repair.test.cjs index 4d6cca86aa..7b23f02602 100644 --- a/tests/recovery/repair.test.cjs +++ b/tests/recovery/repair.test.cjs @@ -15,6 +15,17 @@ test('matching upgrade only reads and does not reupload', async () => { assert.equal(await f.run(), 'verified'); assert.deepEqual(f.calls, ['getBackupSnapshot']); }); +test('a matched snapshot from an old relay is not marked verified', async () => { + const options = {}; + const f = harness(options); + options.response = { + appImage: f.remote, allVaultImages: [], labels: [], revision: 'a'.repeat(64), + }; + assert.equal(await f.run(), 'unverified'); + assert.deepEqual(f.calls, ['getBackupSnapshot']); + assert.equal(f.phases.includes('verified'), false); +}); + for (const failure of ['readError','reject','corruptWrite']) test(`${failure} never claims success`, async () => { const f = harness({ [failure]: true }); f.local.Wallet.push(wallet()); assert.equal(await f.run(true), 'unverified'); assert.ok(!f.phases.includes('verified')); @@ -65,7 +76,7 @@ for (const os of ['ios', 'android']) test(`${os} RestClient preserves the read-o if (requests.length === 1) throw { code: 'ERR_NETWORK' }; return { data: { appImage: { appId: 'disposable', wallets: {}, signers: {}, nodes: [], vaults: [], labels: [] }, - allVaultImages: [], labels: [], revision: 'a'.repeat(64), + allVaultImages: [], labels: [], revision: 'a'.repeat(64), revisionedMutations: true, } }; } }, 'react-native-device-info': { getVersion: () => 'fixture', getBuildNumber: () => 'fixture' }, @@ -143,6 +154,7 @@ for (const [name, change] of [ const f = harness(options); const response = { revision: 'a'.repeat(64), + revisionedMutations: true, appImage: { appId: f.app.id, wallets: {}, signers: {}, nodes: [], vaults: [], labels: [] }, allVaultImages: [], labels: [], }; @@ -158,6 +170,7 @@ test('invalid decrypted JSON reports decryption failure without retry or upload' const f = harness(options); options.response = { revision: 'a'.repeat(64), + revisionedMutations: true, appImage: { appId: f.app.id, wallets: { wallet: encryption.encrypt(encryption.generateEncryptionKey(f.app.primarySeed), '{') }, @@ -222,7 +235,7 @@ for (const [name, keyedWallets] of [ )), signers: {}, nodes: [], vaults: [], labels: [], }; - options.response = { revision: 'a'.repeat(64), appImage, allVaultImages: [], labels: [] }; + options.response = { revision: 'a'.repeat(64), revisionedMutations: true, appImage, allVaultImages: [], labels: [] }; assert.equal(await f.run(), 'unverified'); assert.deepEqual(f.calls, ['getBackupSnapshot']); assert.deepEqual(f.diagnostics, [['Assisted Server Backup check failed at stage:', 'snapshot-validation', 'non-transport']]); @@ -241,7 +254,7 @@ test('numeric decrypted wallet ID is invalid for inspection and recovery', async wallets: { wallet: encryption.encrypt(key, JSON.stringify({ ...wallet(), id: 42 })) }, signers: {}, nodes: [], vaults: [], labels: [], }; - options.response = { revision: 'a'.repeat(64), appImage, allVaultImages: [], labels: [] }; + options.response = { revision: 'a'.repeat(64), revisionedMutations: true, appImage, allVaultImages: [], labels: [] }; assert.equal(await f.run(), 'unverified'); assert.deepEqual(f.calls, ['getBackupSnapshot']); assert.deepEqual(f.diagnostics, [['Assisted Server Backup check failed at stage:', 'snapshot-validation', 'non-transport']]); @@ -269,27 +282,21 @@ test('repair does not retry a failed snapshot request', async () => { assert.deepEqual(f.calls, ['getBackupSnapshot']); }); -test('read-only check retries once after a concurrent incremental backup changes its revision', async () => { +test('incremental attempt during read-only check fails closed and invalidates completion', async () => { let f, changed = false, write; - f = harness({ post: async ({ path, payload, remote }) => { + f = harness({ post: async ({ path }) => { if (path.endsWith('getBackupSnapshot') && !changed) { changed = true; - write = f.transport.backupPost('fixture/updateAppImage', { appId: f.app.id }); - } - if (path.endsWith('revisionedBackupMutation') && payload.operation === 'updateAppImage') { - remote.wallets.wallet = encryption.encrypt( - encryption.generateEncryptionKey(f.app.primarySeed), JSON.stringify(wallet()) - ); + write = f.transport.backupPost('fixture/updateAppImage', { appId: f.app.id }) + .catch((error) => error); } } }); f.local.Wallet.push(wallet()); - assert.equal(await f.run(), 'verified'); - await write; - assert.deepEqual(f.calls, [ - 'getBackupSnapshot', 'getBackupSnapshot', 'revisionedBackupMutation', 'getBackupSnapshot', - ]); - assert.deepEqual(f.phases, ['checking', 'checking', 'verified']); - assert.deepEqual(f.diagnostics, []); + assert.equal(await f.run(), 'unverified'); + assert.equal((await write).category, 'protocol'); + assert.deepEqual(f.calls, ['getBackupSnapshot']); + assert.deepEqual(f.phases, ['checking', 'unverified']); + assert.deepEqual(f.diagnostics, [['Assisted Server Backup check failed at stage:', 'snapshot-validation', 'non-transport']]); }); test('early revision change without a queued write stays unverified', async () => { @@ -303,46 +310,85 @@ test('early revision change without a queued write stays unverified', async () = assert.deepEqual(f.phases, ['checking', 'unverified']); }); -test('continuous queued writes limit a read-only check to one retry', async () => { +test('multiple incremental attempts during inspection remain local and invalidate the check', async () => { let f; const writes = []; - let reads = 0; f = harness({ post: async ({ path }) => { - if (path.endsWith('getBackupSnapshot') && [1, 3].includes(++reads)) - writes.push(f.transport.backupPost('fixture/updateAppImage', { appId: f.app.id })); + if (path.endsWith('getBackupSnapshot')) + for (let index = 0; index < 2; index++) + writes.push(f.transport.backupPost('fixture/updateAppImage', { appId: f.app.id }) + .catch((error) => error)); } }); f.local.Wallet.push(wallet()); assert.equal(await f.run(), 'unverified'); - await Promise.all(writes); - assert.equal(f.calls.filter((path) => path === 'getBackupSnapshot').length, 4); - assert.deepEqual(f.phases, ['checking', 'checking', 'unverified']); + assert.deepEqual((await Promise.all(writes)).map((error) => error.category), ['protocol','protocol']); + assert.deepEqual(f.calls, ['getBackupSnapshot']); + assert.deepEqual(f.phases, ['checking', 'unverified']); }); -test('lost upload acknowledgement is resolved by readback without duplicate upload', async () => { +test('lost v2 repair acknowledgement remains unverified after readback', async () => { const f = harness({ lostResponse: true }); f.local.Wallet.push(wallet()); - assert.equal(await f.run(true), 'verified'); assert.deepEqual(f.calls, ['getBackupSnapshot','repairAppBackup','getBackupSnapshot']); + assert.equal(await f.run(true), 'unverified'); + assert.ok(f.remote.wallets.wallet, 'the server may have committed before the response was lost'); + assert.deepEqual(f.calls, ['getBackupSnapshot','revisionedBackupMutation','getBackupSnapshot']); }); -test('server-only wallet needs user action, then repair removes it and verifies readback', async () => { +test('an old relay returns 404 for v2 repair and cannot acknowledge it', async () => { const f = harness({ post: async ({ path, payload }) => { - if (path.endsWith('repairAppBackup')) assert.equal(payload.replaceCurrentState, true); + if (path.endsWith('revisionedBackupMutation') && payload.operation === 'repairAppBackup') + throw { response: { status: 404 } }; + } }); + f.local.Wallet.push(wallet()); + assert.equal(await f.run(true), 'unverified'); + assert.equal(f.remote.wallets.wallet, undefined); + assert.deepEqual(f.calls, ['getBackupSnapshot','revisionedBackupMutation','getBackupSnapshot']); +}); + +test('server-only wallet cannot be removed by automatic repair or Back Up Now', async () => { + const f = harness({ post: async ({ path, payload }) => { + if (path.endsWith('revisionedBackupMutation')) assert.equal(payload.replaceCurrentState, true); } }); f.remote.wallets.other = encryption.encrypt(encryption.generateEncryptionKey(f.app.primarySeed), JSON.stringify(wallet('other'))); assert.equal(await f.run(), 'different'); assert.deepEqual(f.calls, ['getBackupSnapshot']); assert.ok(f.remote.wallets.other); - assert.equal(await f.run(true), 'verified'); assert.deepEqual(f.calls.slice(1), ['getBackupSnapshot','repairAppBackup','getBackupSnapshot']); - assert.equal(f.remote.wallets.other, undefined); + assert.equal(await f.run(true), 'conflict'); assert.deepEqual(f.calls.slice(1), ['getBackupSnapshot']); + assert.ok(f.remote.wallets.other); +}); + +test('same-ID differing recovery content blocks automatic repair and Back Up Now', async () => { + const f = harness(); + f.local.Wallet.push(wallet()); + const stored = wallet(); + stored.presentationData.name = 'Remote recovery name'; + f.remote.wallets.wallet = encryption.encrypt( + encryption.generateEncryptionKey(f.app.primarySeed), JSON.stringify(stored) + ); + assert.equal(await f.run(), 'different'); + assert.equal(await f.run(true), 'conflict'); + assert.deepEqual(f.calls, ['getBackupSnapshot', 'getBackupSnapshot']); + assert.ok(f.remote.wallets.wallet); +}); + +test('remote-only signer also blocks automatic repair', async () => { + const f = harness(); + const signer = { id: 'remote-signer', masterFingerprint: 'AABBCCDD', signerXpubs: {} }; + f.remote.signers[signer.id] = encryption.encrypt( + encryption.generateEncryptionKey(f.app.primarySeed), JSON.stringify(signer) + ); + assert.equal(await f.run(true), 'conflict'); + assert.deepEqual(f.calls, ['getBackupSnapshot']); + assert.ok(f.remote.signers[signer.id]); }); -test('current-state repair keeps hidden keys and archived wallets while dropping deleted records', async () => { +test('current-state repair cannot drop server-only records even when local additions exist', async () => { const f = harness(); const key = encryption.generateEncryptionKey(f.app.primarySeed); f.local.Signer.push({ id:'hidden-key', masterFingerprint:'hidden', signerXpubs:{}, hidden:true }); f.local.Vault.push({ ...wallet('archived'), archived:true, signers:[], scheme:{m:1,n:1} }); f.remote.wallets.deleted = encryption.encrypt(key, JSON.stringify(wallet('deleted'))); assert.equal(await f.run(), 'different'); - assert.equal(await f.run(true), 'verified'); - assert.equal(f.remote.wallets.deleted, undefined); - assert.equal(JSON.parse(encryption.decrypt(key, f.remote.signers['hidden-key'])).hidden, true); - assert.deepEqual(f.remote.vaults, ['archived']); + assert.equal(await f.run(true), 'conflict'); + assert.ok(f.remote.wallets.deleted); + assert.equal(f.remote.signers['hidden-key'], undefined); + assert.deepEqual(f.remote.vaults, []); }); test('same IDs with different recovery keys are a conflict', async () => { @@ -382,9 +428,9 @@ test('malformed or partial server response is unavailable, never an empty snapsh test('mutation arriving during upload invalidates completion and remains queued until readback', async () => { let f; - f = harness({ post: async ({ path }) => { if (path.endsWith('repairAppBackup')) f.transport.markBackupMutation(f.app.id); } }); + f = harness({ post: async ({ path }) => { if (path.endsWith('revisionedBackupMutation')) f.transport.markBackupMutation(f.app.id); } }); f.local.Wallet.push(wallet()); assert.equal(await f.run(true), 'unverified'); assert.ok(!f.phases.includes('verified')); - assert.deepEqual(f.calls, ['getBackupSnapshot', 'repairAppBackup', 'getBackupSnapshot']); + assert.deepEqual(f.calls, ['getBackupSnapshot', 'revisionedBackupMutation', 'getBackupSnapshot']); }); test('account switch during check cannot upload one account with another account data', async () => { @@ -414,7 +460,7 @@ test('oversized record fails without uploading or truncating', async () => { test('server change after read rejects stale upload and preserves remote data', async () => { const f = harness({ post: async ({ path, remote }) => { - if (path.endsWith('repairAppBackup')) remote.wallets.other = 'new-device-encrypted-data'; + if (path.endsWith('revisionedBackupMutation')) remote.wallets.other = 'new-device-encrypted-data'; } }); f.local.Wallet.push(wallet()); assert.equal(await f.run(true), 'unverified'); @@ -432,7 +478,7 @@ test('relay without revision support fails closed without legacy replacement', a test('archived vault is uploaded with archived server metadata', async () => { let archived; const f = harness({ post: async ({ path,payload }) => { - if (path.endsWith('repairAppBackup')) archived=payload.vaultObject.vault.isArchived; + if (path.endsWith('revisionedBackupMutation')) archived=payload.vaultObject.vault.isArchived; } }); f.local.Vault.push({ ...wallet('vault'), archived:true,signers:[],scheme:{m:1,n:1} }); assert.equal(await f.run(true), 'verified'); diff --git a/tests/recovery/repairAccountSession.test.cjs b/tests/recovery/repairAccountSession.test.cjs index 2a1c0dbea5..101581c5c9 100644 --- a/tests/recovery/repairAccountSession.test.cjs +++ b/tests/recovery/repairAccountSession.test.cjs @@ -64,6 +64,6 @@ test('in-flight repair cannot upload after an A to B to A account switch', async assert.equal(await staleRepair, 'unverified'); assert.equal(await freshRepair, 'verified', 'new session must inspect independently'); - assert.equal(f.calls.filter((path) => path === 'repairAppBackup').length, 1); + assert.equal(f.calls.filter((path) => path === 'revisionedBackupMutation').length, 1); assert.equal(f.calls.filter((path) => path === 'getBackupSnapshot').length, 3); }); diff --git a/tests/recovery/revisionedMutation.test.cjs b/tests/recovery/revisionedMutation.test.cjs index 59bcda672b..a52778d46b 100644 --- a/tests/recovery/revisionedMutation.test.cjs +++ b/tests/recovery/revisionedMutation.test.cjs @@ -2,18 +2,14 @@ const { test } = require('node:test'); const assert = require('node:assert/strict'); const { loadModule } = require('./helpers.cjs'); -const revision = (generation) => generation.toString(16).padStart(64, '0'); - -function fixture(post) { +function fixture() { const calls = []; const reconciliations = []; const transport = loadModule('src/services/backup/transport.ts', { - '../rest/RestClient': { - post: async (path, body) => { - calls.push({ path, body }); - return post(path, body); - }, - }, + '../rest/RestClient': { post: async (...args) => { + calls.push(args); + assert.fail('An incremental mutation must not reach Relay without intent fencing'); + } }, }); transport.setBackupUploadGuard((appId) => appId === 'A'); transport.setBackupReconciliationHandler((appId) => reconciliations.push(appId)); @@ -21,136 +17,77 @@ function fixture(post) { return { transport, calls, reconciliations }; } -test('all active incremental operations use the revisioned route and a fresh queued snapshot', async () => { - let generation = 1; - const f = fixture(async (path, body) => { - if (path === 'fixture/getBackupSnapshot') return { data: { revision: revision(generation) } }; - assert.equal(path, 'fixture/revisionedBackupMutation'); - assert.equal(body.appId, 'A'); - assert.equal(body.expectedRevision, revision(generation)); - generation += 1; - return { data: { updated: true, preconditionApplied: true } }; - }); +test('all seven incremental operations fail closed and mark pending before any network request', async () => { + const f = fixture(); const operations = [ 'updateAppImage', 'migrateXfps', 'deleteAppImageEntity', 'deleteVaults', 'updateVaultImage', 'modifyLabels', 'deleteBackup', ]; for (const operation of operations) { - // updateVaultImage still has a legacy appID-only caller. The new route - // needs a canonical top-level appId in addition to the old payload. const body = operation === 'updateVaultImage' ? { appID: 'A' } : { appId: 'A' }; - const response = await f.transport.backupPost(`fixture/${operation}`, body); - assert.equal(response.data.preconditionApplied, true); + await assert.rejects( + f.transport.backupPost(`fixture/${operation}`, body, operation === 'deleteBackup'), + (error) => error.category === 'protocol' + ); } - assert.deepEqual( - f.calls.map(({ path }) => path), - operations.flatMap(() => ['fixture/getBackupSnapshot', 'fixture/revisionedBackupMutation']) - ); - assert.deepEqual(f.calls.filter(({ path }) => path.endsWith('revisionedBackupMutation')) - .map(({ body }) => body.operation), operations); - assert.deepEqual(f.reconciliations, []); -}); - -test('409 reports a conflict and never retries stale encrypted payload', async () => { - const f = fixture(async (path) => { - if (path.endsWith('getBackupSnapshot')) return { data: { revision: revision(1) } }; - throw { response: { status: 409, data: { error: 'BACKUP_CHANGED' } } }; - }); - await assert.rejects( - f.transport.backupPost('fixture/updateAppImage', { appId: 'A', walletsObject: { w: 'old' } }), - (error) => error.category === 'conflict' && !error.message.includes('old') - ); - assert.deepEqual(f.calls.map(({ path }) => path), [ - 'fixture/getBackupSnapshot', 'fixture/revisionedBackupMutation', - ]); - assert.deepEqual(f.reconciliations, ['A']); + assert.deepEqual(f.calls, []); + assert.deepEqual(f.reconciliations, operations.map(() => 'A')); + assert.equal(f.transport.backupRevision('A'), operations.length); }); -test('a mixed or old relay rejects the new route without an old-route fallback', async () => { - const f = fixture(async (path) => { - if (path.endsWith('getBackupSnapshot')) return { data: { revision: revision(1) } }; - assert.equal(path, 'fixture/revisionedBackupMutation'); - throw { response: { status: 404 } }; - }); - await assert.rejects( - f.transport.backupPost('fixture/deleteBackup', { appId: 'A' }, true), - (error) => error.category === 'http-4xx' - ); - assert.equal(f.calls.some(({ path }) => path === 'fixture/deleteBackup'), false); +test('a prepared payload cannot adopt a revision established by later repair', async () => { + const f = fixture(); + const oldCiphertext = { appId: 'A', walletsObject: { wallet: 'old-ciphertext' } }; + let release; + const heldRepair = f.transport.withBackupSession('A', () => new Promise((resolve) => { release = resolve; })); + const pending = f.transport.backupPost('fixture/updateAppImage', oldCiphertext); + await assert.rejects(pending, (error) => error.category === 'protocol'); + release(); + await heldRepair; + assert.deepEqual(f.calls, []); assert.deepEqual(f.reconciliations, ['A']); }); -test('missing revision or acknowledgement fails closed and requests reconciliation', async () => { - const missingRevision = fixture(async () => ({ data: {} })); - await assert.rejects( - missingRevision.transport.backupPost('fixture/updateAppImage', { appId: 'A' }), - (error) => error.category === 'protocol' - ); - assert.deepEqual(missingRevision.calls.map(({ path }) => path), ['fixture/getBackupSnapshot']); - assert.deepEqual(missingRevision.reconciliations, ['A']); - - const missingAck = fixture(async (path) => path.endsWith('getBackupSnapshot') - ? { data: { revision: revision(1) } } - : { data: { updated: true } }); - await assert.rejects( - missingAck.transport.backupPost('fixture/updateAppImage', { appId: 'A' }), - (error) => error.category === 'protocol' - ); - assert.deepEqual(missingAck.reconciliations, ['A']); - - const rejected = fixture(async (path) => path.endsWith('getBackupSnapshot') - ? { data: { revision: revision(1) } } - : { data: { updated: false, preconditionApplied: true } }); - await assert.rejects( - rejected.transport.backupPost('fixture/updateAppImage', { appId: 'A' }), - (error) => error.category === 'protocol' - ); - assert.deepEqual(rejected.reconciliations, ['A']); -}); - -test('A to B to A during the revision read never sends an old-session mutation', async () => { - let releaseSnapshot, enteredSnapshot; - const waiting = new Promise((resolve) => { releaseSnapshot = resolve; }); - const entered = new Promise((resolve) => { enteredSnapshot = resolve; }); - const f = fixture(async (path) => { - assert.equal(path, 'fixture/getBackupSnapshot'); - enteredSnapshot(); - await waiting; - return { data: { revision: revision(1) } }; - }); - const pending = f.transport.backupPost('fixture/updateAppImage', { appId: 'A' }); - await entered; +test('verified baseline is session bound and invalidated by any attempted mutation', async () => { + const f = fixture(); + const session = f.transport.currentBackupAccountSession(); + f.transport.recordVerifiedRemoteRevision('A', 'a'.repeat(64), session); + const first = f.transport.captureBackupWriteIntent('A'); + assert.equal(first.baseRevision, 'a'.repeat(64)); + assert.equal(first.accountSession, session); + await assert.rejects(f.transport.backupPost('fixture/updateAppImage', { appId: 'A' }), + (error) => error.category === 'protocol'); + assert.equal(f.transport.captureBackupWriteIntent('A'), undefined); + f.transport.recordVerifiedRemoteRevision('A', 'b'.repeat(64), session); + const next = f.transport.captureBackupWriteIntent('A'); + assert.ok(next.baselineEpoch > first.baselineEpoch); f.transport.observeBackupAccount('B'); f.transport.observeBackupAccount('A'); - releaseSnapshot(); - await assert.rejects(pending, /Backup account session changed/); - assert.deepEqual(f.calls.map(({ path }) => path), ['fixture/getBackupSnapshot']); + assert.equal(f.transport.captureBackupWriteIntent('A'), undefined); + assert.deepEqual(f.calls, []); }); -test('A to B to A during an acknowledged write still requires readback', async () => { - let releaseMutation, enteredMutation; - const waiting = new Promise((resolve) => { releaseMutation = resolve; }); - const entered = new Promise((resolve) => { enteredMutation = resolve; }); - const f = fixture(async (path) => { - if (path.endsWith('getBackupSnapshot')) return { json: { revision: revision(1) } }; - enteredMutation(); - await waiting; - return { json: { updated: true, preconditionApplied: true } }; - }); - const pending = f.transport.backupPost('fixture/updateAppImage', { appId: 'A' }); - await entered; +test('stale sessions and denied accounts also request reconciliation', async () => { + const f = fixture(); + const firstSession = f.transport.currentBackupAccountSession(); f.transport.observeBackupAccount('B'); f.transport.observeBackupAccount('A'); - releaseMutation(); - await assert.rejects(pending, /Backup account session changed/); - assert.deepEqual(f.calls.map(({ path }) => path), [ - 'fixture/getBackupSnapshot', 'fixture/revisionedBackupMutation', - ]); - assert.deepEqual(f.reconciliations, ['A']); + await assert.rejects( + f.transport.backupPost('fixture/migrateXfps', { appId: 'A' }, false, firstSession), + /Backup account session changed/ + ); + await assert.rejects( + f.transport.backupPost('fixture/updateAppImage', { appId: 'B' }), + /Backup account is not active or opted in/ + ); + assert.deepEqual(f.calls, []); + assert.deepEqual(f.reconciliations, ['A', 'B']); + assert.equal(f.transport.backupRevision('A'), 1); + assert.equal(f.transport.backupRevision('B'), 1); }); -test('unused legacy full writer is not sent through either route', async () => { - const f = fixture(async () => assert.fail('Legacy writer must not use the network')); +test('the old full writer remains disabled', async () => { + const f = fixture(); await assert.rejects( f.transport.backupPost('fixture/backupAllSignersAndVaults', { appId: 'A' }), (error) => error.category === 'protocol' diff --git a/tests/recovery/sagaState.test.cjs b/tests/recovery/sagaState.test.cjs index 7eb00c023c..7184479c5b 100644 --- a/tests/recovery/sagaState.test.cjs +++ b/tests/recovery/sagaState.test.cjs @@ -56,7 +56,7 @@ test('queued repair keeps actions disabled after the read-only wrapper finishes' if (path.endsWith('getBackupSnapshot') && firstRead) { firstRead = false; readStarted.release(); await read.promise; } - if (path.endsWith('repairAppBackup')) { uploadStarted.release(); await upload.promise; } + if (path.endsWith('revisionedBackupMutation')) { uploadStarted.release(); await upload.promise; } }}); f.local.Wallet.push(wallet()); const runner = inspectionRunner(f); @@ -77,7 +77,7 @@ test('cancelling a coalesced wrapper does not clear another running repair', asy const upload = gate(), uploadStarted = gate(); t.after(() => upload.release()); const f = harness({post:async ({path}) => { - if (path.endsWith('repairAppBackup')) { uploadStarted.release(); await upload.promise; } + if (path.endsWith('revisionedBackupMutation')) { uploadStarted.release(); await upload.promise; } }}); f.local.Wallet.push(wallet()); const runner = inspectionRunner(f), first = runner.start(true); @@ -90,7 +90,7 @@ test('cancelling a coalesced wrapper does not clear another running repair', asy await first.task; assert.equal(await second.task, true); assert.equal(second.state.backupRepairRunningByAppId.disposable, false); - assert.equal(f.calls.filter(path => path === 'repairAppBackup').length, 1); + assert.equal(f.calls.filter(path => path === 'revisionedBackupMutation').length, 1); }); test('cancellation retains progress until its non-cancellable inspection actually settles', async (t) => { diff --git a/tests/recovery/sharedVaultRepair.test.cjs b/tests/recovery/sharedVaultRepair.test.cjs index 7b9027b846..0ff557791f 100644 --- a/tests/recovery/sharedVaultRepair.test.cjs +++ b/tests/recovery/sharedVaultRepair.test.cjs @@ -7,11 +7,13 @@ function fixture({ unavailable = ['shared'], leaveUnavailable = false } = {}) { const state = { appImage: { appId:'disposable', wallets:{}, signers:{}, nodes:[], vaults:['shared'], labels:[] }, allVaultImages:[], labels:[], unavailableVaultIds:unavailable, revision:'a'.repeat(64), + revisionedMutations:true, }; let uploads=0; const f=harness({ adapter:async (endpoint,payload) => { if(endpoint==='getBackupSnapshot') return {data:clone(state)}; - assert.equal(endpoint,'repairAppBackup'); + assert.equal(endpoint,'revisionedBackupMutation'); + assert.equal(payload.operation,'repairAppBackup'); assert.equal(payload.expectedRevision,state.revision); assert.equal(payload.replaceCurrentState,true); uploads++; @@ -21,7 +23,7 @@ function fixture({ unavailable = ['shared'], leaveUnavailable = false } = {}) { state.labels=payload.labels; if (!leaveUnavailable) state.unavailableVaultIds=[]; state.revision='b'.repeat(64); - return {data:{updated:true}}; + return {data:{updated:true,preconditionApplied:true}}; }}); f.local.Vault.push({...wallet('shared'),signers:[],scheme:{m:1,n:1},archived:false}); const key=encryption.generateEncryptionKey(f.app.primarySeed); @@ -41,13 +43,13 @@ test('explicit local resubmission replaces missing account copy and verifies cle assert.deepEqual(state.unavailableVaultIds,[]); const recovered=await prepareRecoveryImage(key,f.app.id,state.appImage,state.allVaultImages,state.labels); assert.deepEqual(clone(recovered.vaults.shared),f.local.Vault[0]); - assert.deepEqual(f.calls,['getBackupSnapshot','repairAppBackup','getBackupSnapshot']); + assert.deepEqual(f.calls,['getBackupSnapshot','revisionedBackupMutation','getBackupSnapshot']); }); -test('missing server-only vault cannot look matched; explicit current-state replacement removes it',async()=>{ +test('missing server-only vault blocks current-state replacement',async()=>{ const {f,state,uploads}=fixture(); f.local.Vault.length=0; assert.equal(await f.run(),'different'); assert.equal(uploads(),0); - assert.equal(await f.run(true),'verified'); - assert.deepEqual(state.appImage.vaults,[]); assert.equal(uploads(),1); + assert.equal(await f.run(true),'conflict'); + assert.deepEqual(state.appImage.vaults,['shared']); assert.equal(uploads(),0); }); for(const problem of ['identity','ahead address counter']) test(`available ${problem} conflict still blocks missing-copy repair`,async()=>{ const {f,state,key,uploads}=fixture(); @@ -74,6 +76,57 @@ test('archive metadata mismatch is read-only until explicit backup and must matc assert.equal(state.allVaultImages[0].isArchived,true); }); +function orphanLabelFixture(hasLocalLabel = true, malformedMarker = false) { + const label = { id: 'fixture-label', name: 'Recovery label' }; + const app = { id: 'disposable', primarySeed: 'ab'.repeat(32) }; + const key = encryption.generateEncryptionKey(app.primarySeed); + const opaqueId = encryption.hash256(encryption.hash256(key + label.id)); + const state = { + appImage: { appId: app.id, wallets: {}, signers: {}, nodes: [], vaults: [], labels: [opaqueId] }, + allVaultImages: [], labels: [], revision: 'a'.repeat(64), revisionedMutations: true, + unavailableLabelIds: [malformedMarker ? 'not-referenced' : opaqueId], + }; + let uploads = 0; + const f = harness({ adapter: async (endpoint, payload) => { + if (endpoint === 'getBackupSnapshot') return { data: clone(state) }; + assert.equal(endpoint, 'revisionedBackupMutation'); + assert.equal(payload.operation, 'repairAppBackup'); + uploads++; + state.labels = payload.labels; + state.appImage.labels = payload.labels.map((record) => record.id); + state.unavailableLabelIds = []; + state.revision = 'b'.repeat(64); + return { data: { updated: true, preconditionApplied: true } }; + } }); + if (hasLocalLabel) f.local.Tags.push(label); + return { f, state, uploads: () => uploads, key, opaqueId }; +} + +test('orphan label cannot be removed when local recovery copy is absent', async () => { + const { f, state, uploads, opaqueId } = orphanLabelFixture(false); + assert.equal(await f.run(), 'different'); + assert.equal(await f.run(true), 'conflict'); + assert.equal(uploads(), 0); + assert.deepEqual(state.appImage.labels, [opaqueId]); +}); + +test('matching local label can safely fill orphan ciphertext during explicit repair', async () => { + const { f, state, uploads, key } = orphanLabelFixture(); + await assert.rejects(prepareRecoveryImage(key, f.app.id, state.appImage, [], state.labels)); + assert.equal(await f.run(), 'different'); + assert.equal(await f.run(true), 'verified'); + assert.equal(uploads(), 1); + assert.deepEqual(state.unavailableLabelIds, []); + assert.deepEqual(clone((await prepareRecoveryImage(key, f.app.id, state.appImage, [], state.labels)).labels['fixture-label']), + f.local.Tags[0]); +}); + +test('malformed orphan label marker never authorizes replacement', async () => { + const { f, uploads } = orphanLabelFixture(true, true); + assert.equal(await f.run(true), 'unverified'); + assert.equal(uploads(), 0); +}); + for(const archived of [true,false]) test(`existing-wallet update carries signer maps and archive state: archived=${archived}`,async()=>{ const {fixture:sagaFixture,enums,signer}=require('./sagaHarness.cjs'); const f=sagaFixture(); diff --git a/tests/recovery/transport.test.cjs b/tests/recovery/transport.test.cjs index de400afaf2..6ec6543149 100644 --- a/tests/recovery/transport.test.cjs +++ b/tests/recovery/transport.test.cjs @@ -52,5 +52,5 @@ test('repeated repair actions share one upload and readback', async () => { const f = harness(); f.local.Wallet.push(wallet()); const results = await Promise.all([f.run(true),f.run(true),f.run(true)]); assert.deepEqual(results,['verified','verified','verified']); - assert.deepEqual(f.calls,['getBackupSnapshot','repairAppBackup','getBackupSnapshot']); + assert.deepEqual(f.calls,['getBackupSnapshot','revisionedBackupMutation','getBackupSnapshot']); }); diff --git a/tests/recovery/vaultRegistration.test.cjs b/tests/recovery/vaultRegistration.test.cjs index 2a7e8f3efa..fbbaac759e 100644 --- a/tests/recovery/vaultRegistration.test.cjs +++ b/tests/recovery/vaultRegistration.test.cjs @@ -25,7 +25,7 @@ function registrationFixture({ incompleteReadback = false, afterSnapshot, afterU appId: 'disposable', wallets: {}, signers: {}, nodes: [], vaults: ['first', 'second'], labels: [], }, - allVaultImages: [], labels: [], revision: 'a'.repeat(64), + allVaultImages: [], labels: [], revision: 'a'.repeat(64), revisionedMutations: true, }; let uploads = 0; let snapshotReads = 0; @@ -35,7 +35,8 @@ function registrationFixture({ incompleteReadback = false, afterSnapshot, afterU if (afterSnapshot) await afterSnapshot(snapshotReads); return { data: clone(state) }; } - assert.equal(endpoint, 'repairAppBackup'); + assert.equal(endpoint, 'revisionedBackupMutation'); + assert.equal(payload.operation, 'repairAppBackup'); assert.equal(payload.expectedRevision, state.revision); assert.equal(payload.replaceCurrentState, true); uploads++; @@ -51,7 +52,7 @@ function registrationFixture({ incompleteReadback = false, afterSnapshot, afterU state.labels = payload.labels; state.revision = 'b'.repeat(64); if (afterUpload) await afterUpload(); - return { data: { updated: true } }; + return { data: { updated: true, preconditionApplied: true } }; } }); const cipherKey = encryption.generateEncryptionKey(f.app.primarySeed); for (const id of state.appImage.vaults) { @@ -70,20 +71,18 @@ function registrationFixture({ incompleteReadback = false, afterSnapshot, afterU return { f, state, cipherKey, registeredVaults, uploads: () => uploads }; } -test('persisted registration repairs every affected vault only after explicit action and readback', async () => { - const { f, state, cipherKey, registeredVaults, uploads } = registrationFixture(); +test('persisted registration difference cannot overwrite existing remote recovery content', async () => { + const { f, state, cipherKey, uploads } = registrationFixture(); assert.equal(await f.run(), 'different'); assert.equal(uploads(), 0); assert.deepEqual(f.calls, ['getBackupSnapshot']); - assert.equal(await f.run(true), 'verified'); - assert.equal(uploads(), 1); - assert.deepEqual(f.calls, [ - 'getBackupSnapshot', 'getBackupSnapshot', 'repairAppBackup', 'getBackupSnapshot', - ]); + assert.equal(await f.run(true), 'conflict'); + assert.equal(uploads(), 0); + assert.deepEqual(f.calls, ['getBackupSnapshot', 'getBackupSnapshot']); for (const encrypted of state.allVaultImages) { const record = JSON.parse(encryption.decrypt(cipherKey, encrypted.vault)); - assert.deepEqual(record.signers[0].registeredVaults, registeredVaults); + assert.deepEqual(record.signers[0].registeredVaults, key.registeredVaults); assert.equal(record.signers[0].xpub, key.xpub); } // Repair uses persisted metadata in its image without mutating vault records. @@ -91,11 +90,11 @@ test('persisted registration repairs every affected vault only after explicit ac assert.deepEqual(record.signers[0].registeredVaults, key.registeredVaults); }); -test('an incomplete registration repair readback cannot be marked verified', async () => { +test('registration conflict never starts a repair even if readback would be incomplete', async () => { const { f, uploads } = registrationFixture({ incompleteReadback: true }); - assert.equal(await f.run(true), 'unverified'); - assert.equal(uploads(), 1); - assert.deepEqual(f.calls, ['getBackupSnapshot', 'repairAppBackup', 'getBackupSnapshot']); + assert.equal(await f.run(true), 'conflict'); + assert.equal(uploads(), 0); + assert.deepEqual(f.calls, ['getBackupSnapshot']); }); test('account switch after registration snapshot prevents an upload for the old account', async () => { @@ -109,14 +108,14 @@ test('account switch after registration snapshot prevents an upload for the old assert.ok(!fixture.f.phases.includes('verified')); }); -test('account switch after registration upload cannot claim a verified repair', async () => { +test('registration conflict prevents upload before an account switch callback', async () => { let fixture; fixture = registrationFixture({ afterUpload: () => { fixture.f.app.id = 'other-disposable-account'; } }); - assert.equal(await fixture.f.run(true), 'unverified'); - assert.equal(fixture.uploads(), 1, 'the upload was for the original account'); - assert.deepEqual(fixture.f.calls, ['getBackupSnapshot', 'repairAppBackup', 'getBackupSnapshot']); + assert.equal(await fixture.f.run(true), 'conflict'); + assert.equal(fixture.uploads(), 0); + assert.deepEqual(fixture.f.calls, ['getBackupSnapshot']); assert.ok(!fixture.f.phases.includes('verified')); }); From b8d09bcf47086bf9f7946d92bcd4b517ab5369ce Mon Sep 17 00:00:00 2001 From: Anant Date: Mon, 12 Oct 2026 21:44:34 +0530 Subject: [PATCH 134/254] Carry backup write intent from source actions to relay mutations --- src/hooks/useUSDTWallets.ts | 21 +++- .../AppSettings/Node/NodeSelection.tsx | 9 +- src/screens/AppSettings/Node/NodeSettings.tsx | 9 +- src/screens/Send/SendConfirmation.tsx | 15 ++- src/services/backend/Relay.ts | 38 ++++--- src/services/backup/intent.ts | 1 + src/store/sagas/bhr.ts | 30 +++-- src/store/sagas/login.ts | 8 +- src/store/sagas/send_and_receive.ts | 33 ++++-- src/store/sagas/settings.ts | 27 ++++- src/store/sagas/storage.ts | 11 +- src/store/sagas/upgrade.ts | 41 +++++-- src/store/sagas/utxos.ts | 17 ++- src/store/sagas/wallets.ts | 107 ++++++++++++++++-- src/store/store.ts | 19 ++++ tests/recovery/accountBinding.test.cjs | 36 +++++- tests/recovery/sendLabelOrigin.test.cjs | 1 + tests/recovery/usdtBackup.test.cjs | 11 ++ .../recovery/walletRefreshIsolation.test.cjs | 30 +++++ tests/store/versionHistory.test.ts | 11 +- 20 files changed, 400 insertions(+), 75 deletions(-) diff --git a/src/hooks/useUSDTWallets.ts b/src/hooks/useUSDTWallets.ts index 50e230c105..3fc6481b8d 100644 --- a/src/hooks/useUSDTWallets.ts +++ b/src/hooks/useUSDTWallets.ts @@ -24,7 +24,7 @@ import { useAppSelector } from 'src/store/hooks'; import { updateAppImage } from 'src/store/sagaActions/bhr'; import Relay from 'src/services/backend/Relay'; import { canonical, recoveryContent } from 'src/services/backup/image'; -import { currentBackupAccountSession, markBackupMutation } from 'src/services/backup/transport'; +import { captureBackupWriteIntent, currentBackupAccountSession, markBackupMutation } from 'src/services/backup/transport'; import { invalidateBackupRepair, isAutomaticCloudBackupEnabled, @@ -117,6 +117,7 @@ export const useUSDTWallets = (options: UseUSDTWalletsOptions = {}): UseUSDTWall importDetails?: USDTWalletImportDetails; }): Promise<{ newWallet?: USDTWallet; error?: string }> => { const originSession = currentBackupAccountSession(); + const originBackupWriteIntent = captureBackupWriteIntent(appId); const isCurrentCreate = () => isOriginCurrent() && renderAccountSession === originSession && @@ -169,7 +170,11 @@ export const useUSDTWallets = (options: UseUSDTWalletsOptions = {}): UseUSDTWall if (!isCurrentCreate()) throw new Error('Account changed'); // Create usdt wallet backup - dispatch(updateAppImage({ wallets: [newWallet], signers: null, updateNodes: false }, appId)); + dispatch({ + ...updateAppImage({ wallets: [newWallet], signers: null, updateNodes: false }, appId), + originBackupAccountSession: originSession, + originBackupWriteIntent, + }); return { newWallet }; } catch (err) { @@ -192,6 +197,7 @@ export const useUSDTWallets = (options: UseUSDTWalletsOptions = {}): UseUSDTWall const deleteWallet = useCallback(async (walletId: string): Promise => { const originAppId = appId; const originSession = currentBackupAccountSession(); + const originBackupWriteIntent = captureBackupWriteIntent(originAppId); const isCurrentDelete = () => isOriginCurrent() && renderAccountSession === originSession && @@ -212,7 +218,7 @@ export const useUSDTWallets = (options: UseUSDTWalletsOptions = {}): UseUSDTWall appId: originAppId, signers: null, walletIds: [walletId], - }, originSession); + }, originBackupWriteIntent); if (!response.updated) throw new Error('Failed to delete wallet'); remoteDeleted = true; } @@ -238,6 +244,7 @@ export const useUSDTWallets = (options: UseUSDTWalletsOptions = {}): UseUSDTWall async (wallet: USDTWallet): Promise => { const originAppId = appId; const operationSession = currentBackupAccountSession(); + const originBackupWriteIntent = captureBackupWriteIntent(originAppId); const isCurrentUpdate = () => isOriginCurrent() && renderAccountSession === operationSession && @@ -264,9 +271,11 @@ export const useUSDTWallets = (options: UseUSDTWalletsOptions = {}): UseUSDTWall // Metadata and visibility must survive recovery. Balance/account refreshes // are rebuildable caches and must not cause an upload on every sync. if (recoveryChanged) - dispatch( - updateAppImage({ wallets: [wallet], signers: null, updateNodes: false }, originAppId) - ); + dispatch({ + ...updateAppImage({ wallets: [wallet], signers: null, updateNodes: false }, originAppId), + originBackupAccountSession: operationSession, + originBackupWriteIntent, + }); return true; } catch (err) { diff --git a/src/screens/AppSettings/Node/NodeSelection.tsx b/src/screens/AppSettings/Node/NodeSelection.tsx index bf62444d53..5e3cea8b14 100644 --- a/src/screens/AppSettings/Node/NodeSelection.tsx +++ b/src/screens/AppSettings/Node/NodeSelection.tsx @@ -32,6 +32,7 @@ import { NetworkType } from 'src/services/wallets/enums'; import { updateAppImage } from 'src/store/sagaActions/bhr'; import { useAppSelector } from 'src/store/hooks'; import WalletHeader from 'src/components/WalletHeader'; +import { captureBackupWriteIntent, currentBackupAccountSession } from 'src/services/backup/transport'; const PrivateElectrum = ({ host, port, useSSL, setHost, setPort, setUseSSL, connectionError }) => { return ( @@ -98,6 +99,8 @@ const NodeSelection = () => { const onSaveCallback = async (nodeDetail: NodeDetail) => { const originAppId = Node.currentAccountId(); if (!originAppId) return; + const originBackupAccountSession = currentBackupAccountSession(); + const originBackupWriteIntent = captureBackupWriteIntent(originAppId); setSaveLoading(true); // Sanitize host @@ -120,7 +123,11 @@ const NodeSelection = () => { if (saved) { const updatedNodeList = Node.getAllNodes(); setNodeList(updatedNodeList); - dispatch(updateAppImage({ wallets: null, signers: null, updateNodes: true }, originAppId)); + dispatch({ + ...updateAppImage({ wallets: null, signers: null, updateNodes: true }, originAppId), + originBackupAccountSession, + originBackupWriteIntent, + }); const newNode = updatedNodeList.find( (node) => node.host === nodeToSave.host && node.port === nodeToSave.port ); diff --git a/src/screens/AppSettings/Node/NodeSettings.tsx b/src/screens/AppSettings/Node/NodeSettings.tsx index 1077b076a4..c8eed58a07 100644 --- a/src/screens/AppSettings/Node/NodeSettings.tsx +++ b/src/screens/AppSettings/Node/NodeSettings.tsx @@ -26,6 +26,7 @@ import { updateAppImage } from 'src/store/sagaActions/bhr'; import { ELECTRUM_CLIENT } from 'src/services/electrum/client'; import WalletHeader from 'src/components/WalletHeader'; import Text from 'src/components/KeeperText'; +import { captureBackupWriteIntent, currentBackupAccountSession } from 'src/services/backup/transport'; function ElectrumDisconnectWarningContent() { const { colorMode } = useColorMode(); @@ -69,6 +70,8 @@ function NodeSettings() { const onDelete = async (selectedItem: NodeDetail, originAppId = nodeListAppId) => { if (!originAppId || !Node.isAccountActive(originAppId)) return; + const originBackupAccountSession = currentBackupAccountSession(); + const originBackupWriteIntent = captureBackupWriteIntent(originAppId); const isConnected = Node.nodeConnectionStatus(selectedItem); if (isConnected) await Node.disconnect(selectedItem); @@ -78,7 +81,11 @@ function NodeSettings() { if (status) { const updatedNodes = Node.getAllNodes(); setNodeList(updatedNodes); - dispatch(updateAppImage({ wallets: null, signers: null, updateNodes: true }, originAppId)); + dispatch({ + ...updateAppImage({ wallets: null, signers: null, updateNodes: true }, originAppId), + originBackupAccountSession, + originBackupWriteIntent, + }); } }; diff --git a/src/screens/Send/SendConfirmation.tsx b/src/screens/Send/SendConfirmation.tsx index 9b15883a9d..6b3150e644 100644 --- a/src/screens/Send/SendConfirmation.tsx +++ b/src/screens/Send/SendConfirmation.tsx @@ -67,6 +67,7 @@ import WalletIcon from 'src/assets/images/daily_wallet.svg'; import MultiSendSvg from 'src/assets/images/@.svg'; import useExchangeRates from 'src/hooks/useExchangeRates'; import Relay from 'src/services/backend/Relay'; +import { captureBackupWriteIntent, currentBackupAccountSession } from 'src/services/backup/transport'; export interface SendConfirmationRouteParams { sender: Wallet | Vault; @@ -314,12 +315,16 @@ function SendConfirmation({ route }) { } } + const originBackupAccountSession = currentBackupAccountSession(); + const originBackupWriteIntent = originAppId + ? captureBackupWriteIntent(originAppId) + : undefined; WalletUtilities.fetchCurrentBlockHeight() .then(({ currentBlockHeight }) => { setTimeout(() => { dispatch(sendPhaseTwoReset()); - dispatch( - sendPhaseTwo({ + dispatch({ + ...sendPhaseTwo({ wallet: sender, currentBlockHeight, txnPriority: transactionPriority, @@ -330,8 +335,10 @@ function SendConfirmation({ route }) { ), }, note, - }, originAppId) - ); + }, originAppId), + originBackupAccountSession, + originBackupWriteIntent, + }); }, 200); }) .catch(() => showToast(errorText.failedToFetchCurrentBlock, )); diff --git a/src/services/backend/Relay.ts b/src/services/backend/Relay.ts index abc2944829..78db61fda9 100644 --- a/src/services/backend/Relay.ts +++ b/src/services/backend/Relay.ts @@ -1,4 +1,5 @@ import { backupPost } from '../backup/transport'; +import type { BackupWriteIntent } from '../backup/intent'; import { NetworkType } from 'src/services/wallets/enums'; import { SubScriptionPlan } from 'src/models/interfaces/Subscription'; import { @@ -18,6 +19,11 @@ import { Platform } from 'react-native'; const { HEXA_ID, RELAY } = config; const TOR_ENDPOINT = 'https://check.torproject.org/api/ip'; +function requireBackupWriteIntent(intent: BackupWriteIntent | undefined): BackupWriteIntent { + if (!intent) throw new Error('Backup write intent unavailable'); + return intent; +} + interface SignerChange { oldSignerId: string; newSignerId: string; @@ -118,14 +124,14 @@ export default class Relay { nodes?: string[]; // Required to clear nodes: older clients send [] for unrelated updates. replaceNodes?: boolean; - }, accountSession?: number): Promise<{ + }, intent: BackupWriteIntent | undefined): Promise<{ status: string; updated: boolean; err?: string; message?: string; }> => { try { - const res = await backupPost(`${RELAY}updateAppImage`, appImage, false, accountSession); + const res = await backupPost(`${RELAY}updateAppImage`, appImage, false, requireBackupWriteIntent(intent)); const data = res.data || res.json; return data; } catch (err) { @@ -137,13 +143,13 @@ export default class Relay { public static migrateXfp = async ( appId: string, signerChanges: SignerChange[], - accountSession?: number + intent: BackupWriteIntent | undefined ): Promise<{ updated: boolean; err?: string; }> => { try { - const res = await backupPost(`${RELAY}migrateXfps`, { appId, signerChanges }, false, accountSession); + const res = await backupPost(`${RELAY}migrateXfps`, { appId, signerChanges }, false, requireBackupWriteIntent(intent)); const data = res.data || res.json; return data; } catch (err) { @@ -154,7 +160,7 @@ export default class Relay { public static deleteAppImageEntity = async ( entityList, - accountSession?: number + intent: BackupWriteIntent | undefined ): Promise<{ status: string; updated: boolean; @@ -162,7 +168,7 @@ export default class Relay { message?: string; }> => { try { - const res = await backupPost(`${RELAY}deleteAppImageEntity`, entityList, false, accountSession); + const res = await backupPost(`${RELAY}deleteAppImageEntity`, entityList, false, requireBackupWriteIntent(intent)); const data = res.data || res.json; return data; } catch (err) { @@ -173,7 +179,7 @@ export default class Relay { public static deleteVaultImage = async ( entityList, - accountSession?: number + intent: BackupWriteIntent | undefined ): Promise<{ status: string; updated: boolean; @@ -181,7 +187,7 @@ export default class Relay { message?: string; }> => { try { - const res = await backupPost(`${RELAY}deleteVaults`, entityList, false, accountSession); + const res = await backupPost(`${RELAY}deleteVaults`, entityList, false, requireBackupWriteIntent(intent)); const data = res.data || res.json; return data; } catch (err) { @@ -192,7 +198,7 @@ export default class Relay { public static updateVaultImage = async ( vaultData, - accountSession?: number + intent: BackupWriteIntent | undefined ): Promise<{ status?: number; data?: { @@ -202,7 +208,7 @@ export default class Relay { message?: string; }> => { try { - const res = await backupPost(`${RELAY}updateVaultImage`, vaultData, false, accountSession); + const res = await backupPost(`${RELAY}updateVaultImage`, vaultData, false, requireBackupWriteIntent(intent)); const data = res.data || res.json; return data; } catch (err) { @@ -300,7 +306,7 @@ export default class Relay { appId: string, addLabels: any[], deleteLabels: any[], - accountSession?: number + intent: BackupWriteIntent | undefined ): Promise<{ updated: boolean; }> => { @@ -309,7 +315,7 @@ export default class Relay { appId, addLabels, deleteLabels, - }, false, accountSession)) as AxiosResponse; + }, false, requireBackupWriteIntent(intent))) as AxiosResponse; const { updated } = res.data || res.json; return { updated, @@ -458,7 +464,7 @@ export default class Relay { public static backupAllSignersAndVaults = async ( allData, - accountSession?: number + intent: BackupWriteIntent | undefined ): Promise<{ updated?: boolean; status?: number; @@ -469,7 +475,7 @@ export default class Relay { message?: string; }> => { try { - const res = await backupPost(`${RELAY}backupAllSignersAndVaults`, allData, false, accountSession); + const res = await backupPost(`${RELAY}backupAllSignersAndVaults`, allData, false, requireBackupWriteIntent(intent)); const data = res.data || res.json; return data; } catch (err) { @@ -505,7 +511,7 @@ export default class Relay { public static deleteBackup = async ( body, - accountSession?: number + intent: BackupWriteIntent | undefined ): Promise<{ status?: number; data?: { @@ -515,7 +521,7 @@ export default class Relay { message?: string; }> => { try { - const res = await backupPost(`${RELAY}deleteBackup`, body, true, accountSession); + const res = await backupPost(`${RELAY}deleteBackup`, body, true, requireBackupWriteIntent(intent)); const data = res.data || res.json; return data; } catch (err) { diff --git a/src/services/backup/intent.ts b/src/services/backup/intent.ts index afc206ea5f..fa6ad60695 100644 --- a/src/services/backup/intent.ts +++ b/src/services/backup/intent.ts @@ -1,3 +1,4 @@ +/** The verified remote base and account session held by one originating write. */ export type BackupWriteIntent = Readonly<{ appId: string; accountSession: number; diff --git a/src/store/sagas/bhr.ts b/src/store/sagas/bhr.ts index a3576e8acc..d48ab1a7ad 100644 --- a/src/store/sagas/bhr.ts +++ b/src/store/sagas/bhr.ts @@ -3,6 +3,7 @@ import { inspectBackup, RepairPhase } from 'src/services/backup/repair'; import { prepareRecoveryImage } from 'src/services/backup/restore'; import { BackupImage, pauseBackup } from 'src/services/backup/image'; import { currentBackupAccountSession, markBackupMutation } from 'src/services/backup/transport'; +import type { BackupWriteIntent } from 'src/services/backup/intent'; import * as bip39 from 'bip39'; import { Wallet } from 'src/services/wallets/interfaces/wallet'; import { call, put, select, takeMaybe, takeEvery } from 'redux-saga/effects'; @@ -119,9 +120,11 @@ export function* updateAppImageWorker({ payload, originAppId, originBackupAccountSession, + originBackupWriteIntent, }: { originAppId?: string; originBackupAccountSession?: number; + originBackupWriteIntent?: BackupWriteIntent; payload: { wallets?: Wallet[] | USDTWallet[]; signers?: Signer[]; @@ -189,7 +192,7 @@ export function* updateAppImageWorker({ version, nodes: nodesList, replaceNodes: !!updateNodes, - }, requestSession); + }, originBackupWriteIntent); if (!accountIsCurrent()) { yield call(flagUncertainBackupOutcome, appId); return { updated: false, error: 'Backup account changed' }; @@ -204,7 +207,9 @@ export function* updateAppImageWorker({ return { updated: false, error: 'Backup account changed' }; } yield call(setServerBackupFailed, appId); - return { updated: true, error: '' }; + // Local wallet/key changes may continue offline; the relay write is still + // unverified and the account-scoped pending flag remains set. + return { updated: true, backupConfirmed: false, backupPending: true, error: '' }; } } @@ -212,9 +217,11 @@ export function* updateVaultImageWorker({ payload, originAppId, originBackupAccountSession, + originBackupWriteIntent, }: { originAppId?: string; originBackupAccountSession?: number; + originBackupWriteIntent?: BackupWriteIntent; payload: { vault: Vault; archiveVaultId?: string; @@ -267,7 +274,7 @@ export function* updateVaultImageWorker({ vaultId: vault.id, vault: vaultEncrypted, isArchived: !!vault.archived, - }, requestSession); + }, originBackupWriteIntent); if (!accountIsCurrent()) { yield call(flagUncertainBackupOutcome, appId); return { updated: false, error: 'Backup account changed' }; @@ -294,7 +301,7 @@ export function* updateVaultImageWorker({ isArchived: !!vault.archived, subscription: subscriptionStrings, ...(archiveVaultId && { archiveVaultId }), - }, requestSession); + }, originBackupWriteIntent); if (!accountIsCurrent()) { yield call(flagUncertainBackupOutcome, appId); return { updated: false, error: 'Backup account changed' }; @@ -307,7 +314,8 @@ export function* updateVaultImageWorker({ return { updated: false, error: 'Backup account changed' }; } yield call(setServerBackupFailed, appId); - return { updated: true, error: '' }; + // Keep local vault editing available while making the failed backup visible. + return { updated: true, backupConfirmed: false, backupPending: true, error: '' }; } } @@ -316,9 +324,11 @@ export function* deleteAppImageEntityWorker({ payload, originAppId, originBackupAccountSession, + originBackupWriteIntent, }: { originAppId?: string; originBackupAccountSession?: number; + originBackupWriteIntent?: BackupWriteIntent; payload: { signerIds?: string[]; walletIds?: string[]; @@ -347,7 +357,7 @@ export function* deleteAppImageEntityWorker({ appId: id, signers: signerIds, walletIds, - }, requestSession); + }, originBackupWriteIntent); } if (!accountIsCurrent()) { @@ -419,9 +429,11 @@ export function* deleteVaultImageWorker({ payload, originAppId, originBackupAccountSession, + originBackupWriteIntent, }: { originAppId?: string; originBackupAccountSession?: number; + originBackupWriteIntent?: BackupWriteIntent; payload: { vaultIds: string[]; }; @@ -445,7 +457,7 @@ export function* deleteVaultImageWorker({ const response = yield call(Relay.deleteVaultImage, { appId: id, vaults: vaultIds, - }, requestSession); + }, originBackupWriteIntent); if (!accountIsCurrent()) { yield call(flagUncertainBackupOutcome, appId); return { updated: false, error: 'Backup account changed' }; @@ -1329,9 +1341,11 @@ export const backupAllSignersAndVaultsWatcher = createWatcher( function* deleteBackupWorker({ appId, originBackupAccountSession, + originBackupWriteIntent, }: { appId: string; originBackupAccountSession?: number; + originBackupWriteIntent?: BackupWriteIntent; }) { const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); if (requestSession !== currentBackupAccountSession()) return false; @@ -1345,7 +1359,7 @@ function* deleteBackupWorker({ remoteDeleteAttempted = true; const response = yield call(Relay.deleteBackup, { appId, - }, requestSession); + }, originBackupWriteIntent); if (requestSession !== currentBackupAccountSession()) { yield call(flagUncertainBackupOutcome, appId); return false; diff --git a/src/store/sagas/login.ts b/src/store/sagas/login.ts index c0df330fac..52b03c10de 100644 --- a/src/store/sagas/login.ts +++ b/src/store/sagas/login.ts @@ -53,7 +53,7 @@ import { setLoginMethod } from '../reducers/settings'; import { setSubscription } from 'src/store/sagaActions/settings'; import { uaiChecks } from '../sagaActions/uai'; import { applyUpgradeSequence } from './upgrade'; -import { currentBackupAccountSession } from 'src/services/backup/transport'; +import { captureBackupWriteIntent, currentBackupAccountSession } from 'src/services/backup/transport'; import { resetSyncing } from '../reducers/wallets'; import { autoSyncWallets } from '../sagaActions/wallets'; import { connectToNode } from '../sagaActions/network'; @@ -217,6 +217,11 @@ function* credentialsAuthWorker({ payload }) { if (originSession !== currentBackupAccountSession()) return; if (keeperApp?.id) yield put(setAppId(keeperApp.id)); const loginAccountSession = currentBackupAccountSession(); + // Account selection establishes a new session. Preserve only a verified + // base for that session before the remaining login/upgrade awaits. + const loginBackupWriteIntent = keeperApp?.id + ? captureBackupWriteIntent(keeperApp.id) + : undefined; loginSession = loginAccountSession; if (store.getState().storage.appId !== keeperApp?.id) return; @@ -238,6 +243,7 @@ function* credentialsAuthWorker({ payload }) { previousVersion, newVersion, originBackupAccountSession: loginAccountSession, + originBackupWriteIntent: loginBackupWriteIntent, }); } else if (currentVersionCode !== lastVersionCode[1]) { yield call(dbManager.createObject, RealmSchema.VersionHistory, { diff --git a/src/store/sagas/send_and_receive.ts b/src/store/sagas/send_and_receive.ts index 12b95f0eb8..635995f410 100644 --- a/src/store/sagas/send_and_receive.ts +++ b/src/store/sagas/send_and_receive.ts @@ -1,5 +1,6 @@ import { AverageTxFeesByNetwork, SerializedPSBTEnvelop } from 'src/services/wallets/interfaces'; import { EntityKind, LabelRefType, TxPriority } from 'src/services/wallets/enums'; +import type { BackupWriteIntent } from 'src/services/backup/intent'; import { call, fork, put, select } from 'redux-saga/effects'; import { RealmSchema } from 'src/storage/realm/enum'; @@ -129,7 +130,8 @@ function* sendPhaseOneWorker({ payload }: SendPhaseOneAction) { export const sendPhaseOneWatcher = createWatcher(sendPhaseOneWorker, SEND_PHASE_ONE); -function* sendPhaseTwoWorker({ payload, originAppId }: SendPhaseTwoAction & { originAppId?: string }) { +function* sendPhaseTwoWorker({ payload, originAppId, originBackupAccountSession, originBackupWriteIntent }: + SendPhaseTwoAction & { originAppId?: string; originBackupAccountSession?: number; originBackupWriteIntent?: BackupWriteIntent }) { if (!ELECTRUM_CLIENT.isClientConnected) { ElectrumClient.resetCurrentPeerIndex(); yield call(connectToNodeWorker); @@ -171,6 +173,8 @@ function* sendPhaseTwoWorker({ payload, originAppId }: SendPhaseTwoAction & { or if (note) { yield call(addLabelsWorker, { originAppId, + originBackupAccountSession, + originBackupWriteIntent, payload: { txId: txid, wallet, @@ -206,7 +210,7 @@ function* sendPhaseTwoWorker({ payload, originAppId }: SendPhaseTwoAction & { or } if (finalOutputs) { - yield call(handleChangeOutputLabels, finalOutputs, inputs, wallet, txid, originAppId); + yield call(handleChangeOutputLabels, finalOutputs, inputs, wallet, txid, originAppId, originBackupAccountSession, originBackupWriteIntent); } } catch (err) { if ([ELECTRUM_NOT_CONNECTED_ERR, ELECTRUM_NOT_CONNECTED_ERR_TOR].includes(err?.message)) { @@ -224,7 +228,8 @@ function* sendPhaseTwoWorker({ payload, originAppId }: SendPhaseTwoAction & { or export const sendPhaseTwoWatcher = createWatcher(sendPhaseTwoWorker, SEND_PHASE_TWO); -function* sendPhaseThreeWorker({ payload, originAppId }: SendPhaseThreeAction & { originAppId?: string }) { +function* sendPhaseThreeWorker({ payload, originAppId, originBackupAccountSession, originBackupWriteIntent }: + SendPhaseThreeAction & { originAppId?: string; originBackupAccountSession?: number; originBackupWriteIntent?: BackupWriteIntent }) { if (!ELECTRUM_CLIENT.isClientConnected) { ElectrumClient.resetCurrentPeerIndex(); yield call(connectToNodeWorker); @@ -278,6 +283,8 @@ function* sendPhaseThreeWorker({ payload, originAppId }: SendPhaseThreeAction & if (note) { yield call(addLabelsWorker, { originAppId, + originBackupAccountSession, + originBackupWriteIntent, payload: { txId: txid, wallet, @@ -288,7 +295,7 @@ function* sendPhaseThreeWorker({ payload, originAppId }: SendPhaseThreeAction & } if (finalOutputs) { - yield call(handleChangeOutputLabels, finalOutputs, inputs, wallet, txid, originAppId); + yield call(handleChangeOutputLabels, finalOutputs, inputs, wallet, txid, originAppId, originBackupAccountSession, originBackupWriteIntent); } } catch (err) { if ([ELECTRUM_NOT_CONNECTED_ERR, ELECTRUM_NOT_CONNECTED_ERR_TOR].includes(err?.message)) { @@ -306,7 +313,15 @@ function* sendPhaseThreeWorker({ payload, originAppId }: SendPhaseThreeAction & export const sendPhaseThreeWatcher = createWatcher(sendPhaseThreeWorker, SEND_PHASE_THREE); -function* handleChangeOutputLabels(finalOutputs, inputs, wallet, txid, originAppId?: string) { +function* handleChangeOutputLabels( + finalOutputs, + inputs, + wallet, + txid, + originAppId?: string, + originBackupAccountSession?: number, + originBackupWriteIntent?: BackupWriteIntent +) { const changeOutputIndex = finalOutputs.findIndex((output) => Object.values(wallet.specs.addresses.internal).includes(output.address) ); @@ -343,6 +358,8 @@ function* handleChangeOutputLabels(finalOutputs, inputs, wallet, txid, originApp yield fork(bulkUpdateLabelsWorker, { originAppId, + originBackupAccountSession, + originBackupWriteIntent, payload: { labelChanges, UTXO: { txId: txid, vout: changeOutputIndex }, @@ -466,7 +483,7 @@ function* calculateCustomFee({ payload }: CalculateCustomFeeAction) { export const calculateCustomFeeWatcher = createWatcher(calculateCustomFee, CALCULATE_CUSTOM_FEE); -function* discardBroadcastedTnxWorker({ payload, originAppId }) { +function* discardBroadcastedTnxWorker({ payload, originAppId, originBackupAccountSession, originBackupWriteIntent }) { const { cachedTxid, vault } = payload; const snapshots = yield select((state) => state.cachedTxn.snapshots); if (snapshots[cachedTxid]) { @@ -479,10 +496,12 @@ function* discardBroadcastedTnxWorker({ payload, originAppId }) { }); const potentialTxId = snapshots[cachedTxid].potentialTxId; const note = snapshots[cachedTxid].routeParams.note; - yield call(handleChangeOutputLabels, finalOutputs, inputs, vault, potentialTxId, originAppId); + yield call(handleChangeOutputLabels, finalOutputs, inputs, vault, potentialTxId, originAppId, originBackupAccountSession, originBackupWriteIntent); if (note.length) { yield call(addLabelsWorker, { originAppId, + originBackupAccountSession, + originBackupWriteIntent, payload: { txId: potentialTxId, wallet: vault, diff --git a/src/store/sagas/settings.ts b/src/store/sagas/settings.ts index 08d87d3502..9d1a18ff37 100644 --- a/src/store/sagas/settings.ts +++ b/src/store/sagas/settings.ts @@ -22,8 +22,15 @@ import { SubscriptionTier } from 'src/models/enums/SubscriptionTier'; import { AppIconWrapper } from 'src/utils/AppIconWrapper'; import ThemeMode from 'src/models/enums/ThemeMode'; import { updateDefaultWalletCreatedByAppId } from '../reducers/account'; +import { currentBackupAccountSession } from 'src/services/backup/transport'; +import type { BackupWriteIntent } from 'src/services/backup/intent'; -function* changeBitcoinNetworkWorker({ payload }) { +function* changeBitcoinNetworkWorker({ payload, originAppId, originBackupAccountSession, originBackupWriteIntent }: { + payload: { network: NetworkType; callback?: (success: boolean) => void }; + originAppId?: string; + originBackupAccountSession?: number; + originBackupWriteIntent?: BackupWriteIntent; +}) { let activeNode; const { network, callback } = payload; try { @@ -54,6 +61,10 @@ function* changeBitcoinNetworkWorker({ payload }) { yield call(ElectrumClient.connect); const { primaryMnemonic, id } = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); + if ((originAppId && originAppId !== id) || + (originBackupAccountSession !== undefined && + originBackupAccountSession !== currentBackupAccountSession())) + throw new Error('Backup account changed during network switch'); // Create default wallet and signer if not created already const { defaultWalletCreatedByAppId } = yield select((state: RootState) => state.account); @@ -62,7 +73,12 @@ function* changeBitcoinNetworkWorker({ payload }) { const cosigner = yield call(getCosignerDetails, primaryMnemonic, 0); const hw = setupKeeperSigner(cosigner); if (hw) { - yield put(addSigningDevice([hw.signer])); + yield put({ + ...addSigningDevice([hw.signer]), + originAppId: id, + originBackupAccountSession, + originBackupWriteIntent, + }); } const defaultWallet: NewWalletInfo = { walletType: WalletType.DEFAULT, @@ -78,7 +94,12 @@ function* changeBitcoinNetworkWorker({ payload }) { ), }, }; - yield call(addNewWalletsWorker, { payload: [defaultWallet] }); + yield call(addNewWalletsWorker, { + payload: [defaultWallet], + originAppId: id, + originBackupAccountSession, + originBackupWriteIntent, + }); yield put(updateDefaultWalletCreatedByAppId({ appId: id, networkType: bitcoinNetworkType })); } if (callback) callback(true); diff --git a/src/store/sagas/storage.ts b/src/store/sagas/storage.ts index dad91ea326..4654c02e5a 100644 --- a/src/store/sagas/storage.ts +++ b/src/store/sagas/storage.ts @@ -43,8 +43,13 @@ import { } from '../reducers/account'; import { loadConciergeTickets, loadConciergeUser } from '../reducers/concierge'; import LoginMethod from 'src/models/enums/LoginMethod'; +import { currentBackupAccountSession } from 'src/services/backup/transport'; +import type { BackupWriteIntent } from 'src/services/backup/intent'; -export function* setupKeeperAppWorker({ payload }) { +export function* setupKeeperAppWorker({ payload, originBackupWriteIntent }: { + payload: { appName: string; fcmToken: string }; + originBackupWriteIntent?: BackupWriteIntent; +}) { let isCurrentSetupScope = () => true; try { const { bitcoinNetworkType, selectedAppId } = yield select((state: RootState) => ({ @@ -165,6 +170,8 @@ export function* setupKeeperAppWorker({ payload }) { const hasDefaultWallet = existingWallets.some(isDefaultWallet); const walletCreated = hasDefaultWallet || (yield call(addNewWalletsWorker, { originAppId: appID, + originBackupAccountSession: currentBackupAccountSession(), + originBackupWriteIntent, payload: [defaultWallet], })); assertCurrentSetupScope(); @@ -174,6 +181,8 @@ export function* setupKeeperAppWorker({ payload }) { throw new Error('Failed to create default wallet'); yield call(addSigningDeviceWorker, { originAppId: appID, + originBackupAccountSession: currentBackupAccountSession(), + originBackupWriteIntent, payload: { signers: [recoveryKeySigner] }, }); assertCurrentSetupScope(); diff --git a/src/store/sagas/upgrade.ts b/src/store/sagas/upgrade.ts index 11ef41ba43..b0abc8bd3c 100644 --- a/src/store/sagas/upgrade.ts +++ b/src/store/sagas/upgrade.ts @@ -41,6 +41,7 @@ import { setAppVersion } from '../reducers/storage'; import { isAutomaticCloudBackupEnabled, setPendingAllBackup } from '../reducers/bhr'; import { RootState, store } from '../store'; import { currentBackupAccountSession } from 'src/services/backup/transport'; +import type { BackupWriteIntent } from 'src/services/backup/intent'; export const LABELS_INTRODUCTION_VERSION = '1.0.4'; export const BIP329_INTRODUCTION_VERSION = '1.0.7'; @@ -60,11 +61,13 @@ export function* applyUpgradeSequence({ newVersion, isRecovery = false, originBackupAccountSession, + originBackupWriteIntent, }: { previousVersion: string; newVersion: string; isRecovery?: boolean; originBackupAccountSession?: number; + originBackupWriteIntent?: BackupWriteIntent; }) { const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); if (requestSession !== currentBackupAccountSession()) return; @@ -76,15 +79,15 @@ export function* applyUpgradeSequence({ semver.gte(previousVersion, LABELS_INTRODUCTION_VERSION) && semver.lt(previousVersion, BIP329_INTRODUCTION_VERSION) ) { - yield put({ ...migrateLabelsToBip329(originAppId), originBackupAccountSession: requestSession }); + yield put({ ...migrateLabelsToBip329(originAppId), originBackupAccountSession: requestSession, originBackupWriteIntent }); } if (semver.lt(previousVersion, KEY_MANAGEMENT_VERSION)) { - yield call(migrateStructureforSignersInAppImage, originAppId, requestSession); - yield call(migrateStructureforVaultInAppImage, originAppId, requestSession); + yield call(migrateStructureforSignersInAppImage, originAppId, requestSession, originBackupWriteIntent); + yield call(migrateStructureforVaultInAppImage, originAppId, requestSession, originBackupWriteIntent); } if (semver.lt(previousVersion, APP_KEY_UPGRADE_VERSION)) - yield call(updateAppKeysToEnableSigning, originAppId, requestSession); + yield call(updateAppKeysToEnableSigning, originAppId, requestSession, originBackupWriteIntent); if (semver.lt(previousVersion, HEALTH_CHECK_TIMELINE_MIGRATION_VERSION)) { if (!upgradeAccountIsActive(originAppId, requestSession)) return; @@ -104,6 +107,7 @@ export function* applyUpgradeSequence({ yield put({ ...updateVersionHistory(previousVersion, newVersion, isRecovery, originAppId), originBackupAccountSession: requestSession, + originBackupWriteIntent, }); } @@ -111,10 +115,12 @@ export function* updateVersionHistoryWorker({ payload, originAppId, originBackupAccountSession, + originBackupWriteIntent, }: { payload: { previousVersion: string; newVersion: string; isRecovery?: boolean }; originAppId?: string; originBackupAccountSession?: number; + originBackupWriteIntent?: BackupWriteIntent; }) { const { previousVersion, newVersion, isRecovery = false } = payload; const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); @@ -128,7 +134,7 @@ export function* updateVersionHistoryWorker({ yield call(Relay.updateAppImage, { appId: originAppId, version: newVersion, - }, requestSession); + }, originBackupWriteIntent); } if (!upgradeAccountIsActive(originAppId, requestSession)) return; // recoverApp already records the single "Recovered Wallet" entry. @@ -156,9 +162,11 @@ export const updateVersionHistoryWatcher = createWatcher( function* migrateLablesWorker({ originAppId, originBackupAccountSession, + originBackupWriteIntent, }: { originAppId?: string; originBackupAccountSession?: number; + originBackupWriteIntent?: BackupWriteIntent; }) { const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); try { @@ -206,7 +214,7 @@ function* migrateLablesWorker({ state.storage.appId === originAppId && isAutomaticCloudBackupEnabled(state.bhr, originAppId) ); if (enabled && upgradeAccountIsActive(originAppId, requestSession)) - yield call(Relay.modifyLabels, id, tagsToBackup, [], requestSession); + yield call(Relay.modifyLabels, id, tagsToBackup, [], originBackupWriteIntent); } } catch (error) { console.log({ error }); @@ -215,7 +223,11 @@ function* migrateLablesWorker({ export const migrateLablesWatcher = createWatcher(migrateLablesWorker, MIGRATE_LABELS_329); -function* migrateStructureforSignersInAppImage(originAppId: string, requestSession: number) { +function* migrateStructureforSignersInAppImage( + originAppId: string, + requestSession: number, + originBackupWriteIntent?: BackupWriteIntent +) { try { if (!upgradeAccountIsActive(originAppId, requestSession)) return; const wallets = yield call(dbManager.getCollection, RealmSchema.Wallet); @@ -224,6 +236,7 @@ function* migrateStructureforSignersInAppImage(originAppId: string, requestSessi payload: { wallets, signers }, originAppId, originBackupAccountSession: requestSession, + originBackupWriteIntent, }); if (response.updated) { console.log('Updated the Signers in app image'); @@ -233,7 +246,11 @@ function* migrateStructureforSignersInAppImage(originAppId: string, requestSessi } catch (err) {} } -function* migrateStructureforVaultInAppImage(originAppId: string, requestSession: number) { +function* migrateStructureforVaultInAppImage( + originAppId: string, + requestSession: number, + originBackupWriteIntent?: BackupWriteIntent +) { try { if (!upgradeAccountIsActive(originAppId, requestSession)) return; const vaults: Vault[] = yield call(dbManager.getCollection, RealmSchema.Vault); @@ -243,6 +260,7 @@ function* migrateStructureforVaultInAppImage(originAppId: string, requestSession payload: { isUpdate: true, vault: activeVault }, originAppId, originBackupAccountSession: requestSession, + originBackupWriteIntent, }); } catch (err) { console.log('Something went wrong in updating the vault image', err); @@ -250,7 +268,11 @@ function* migrateStructureforVaultInAppImage(originAppId: string, requestSession } // This function updates app keys/mobile keys to enable signing by generating and associating extended keys. -function* updateAppKeysToEnableSigning(originAppId: string, requestSession: number) { +function* updateAppKeysToEnableSigning( + originAppId: string, + requestSession: number, + originBackupWriteIntent?: BackupWriteIntent +) { try { if (!upgradeAccountIsActive(originAppId, requestSession)) return; const wallets = yield call(dbManager.getCollection, RealmSchema.Wallet); @@ -270,6 +292,7 @@ function* updateAppKeysToEnableSigning(originAppId: string, requestSession: numb payload: { signers }, originAppId, originBackupAccountSession: requestSession, + originBackupWriteIntent, }); if (response.updated) { console.log('Updated the Signers in app image'); diff --git a/src/store/sagas/utxos.ts b/src/store/sagas/utxos.ts index eac215e831..73ad36f629 100644 --- a/src/store/sagas/utxos.ts +++ b/src/store/sagas/utxos.ts @@ -5,6 +5,7 @@ import { BIP329Label, UTXOSpendability } from 'src/services/wallets/interfaces'; import { EntityKind, LabelRefType, NetworkType } from 'src/services/wallets/enums'; import Relay from 'src/services/backend/Relay'; import { currentBackupAccountSession } from 'src/services/backup/transport'; +import type { BackupWriteIntent } from 'src/services/backup/intent'; import { Wallet } from 'src/services/wallets/interfaces/wallet'; import { generateAbbreviatedOutputDescriptors } from 'src/utils/service-utilities/utils'; import { Vault } from 'src/services/wallets/interfaces/vault'; @@ -83,6 +84,7 @@ export function* addLabelsWorker({ payload, originAppId, originBackupAccountSession, + originBackupWriteIntent, }: { payload: { txId: string; @@ -93,6 +95,7 @@ export function* addLabelsWorker({ }; originAppId?: string; originBackupAccountSession?: number; + originBackupWriteIntent?: BackupWriteIntent; }) { try { if (!originAppId) return; @@ -126,7 +129,7 @@ export function* addLabelsWorker({ id: hash256(hash256(encryptionKey + tag.id)), content: encrypt(encryptionKey, JSON.stringify(tag)), })); - yield call(Relay.modifyLabels, originAppId, tagsToBackup, [], requestSession); + yield call(Relay.modifyLabels, originAppId, tagsToBackup, [], originBackupWriteIntent); } else yield delay(100); } catch (error) { console.log('🚀 ~ addLabelsWorker error:', error); @@ -144,6 +147,7 @@ export function* bulkUpdateLabelsWorker({ payload, originAppId, originBackupAccountSession, + originBackupWriteIntent, }: { payload: { labelChanges: { @@ -158,6 +162,7 @@ export function* bulkUpdateLabelsWorker({ }; originAppId?: string; originBackupAccountSession?: number; + originBackupWriteIntent?: BackupWriteIntent; }) { try { if (!originAppId) return; @@ -216,7 +221,7 @@ export function* bulkUpdateLabelsWorker({ originAppId, tagsToBackup.length ? tagsToBackup : [], tagsToDelete.length ? tagsToDelete : [], - requestSession + originBackupWriteIntent ); } else yield delay(100); } catch (error) { @@ -234,6 +239,7 @@ export function* importLabelsWorker({ payload, originAppId, originBackupAccountSession, + originBackupWriteIntent, }: { payload: { labels: [ @@ -247,6 +253,7 @@ export function* importLabelsWorker({ }; originAppId?: string; originBackupAccountSession?: number; + originBackupWriteIntent?: BackupWriteIntent; }) { try { if (!originAppId) return; @@ -281,7 +288,7 @@ export function* importLabelsWorker({ id: hash256(hash256(encryptionKey + tag.id)), content: encrypt(encryptionKey, JSON.stringify(tag)), })); - yield call(Relay.modifyLabels, originAppId, tagsToBackup.length ? tagsToBackup : [], [], requestSession); + yield call(Relay.modifyLabels, originAppId, tagsToBackup.length ? tagsToBackup : [], [], originBackupWriteIntent); } else yield delay(100); } catch (error) { yield call(setServerBackupFailed, originAppId); @@ -302,6 +309,7 @@ export function* markUTXOSpendabilityWorker({ payload, originAppId, originBackupAccountSession, + originBackupWriteIntent, }: { payload: { wallet: any; @@ -311,6 +319,7 @@ export function* markUTXOSpendabilityWorker({ }; originAppId?: string; originBackupAccountSession?: number; + originBackupWriteIntent?: BackupWriteIntent; }) { try { if (!originAppId) return; @@ -356,11 +365,13 @@ export function* markUTXOSpendabilityWorker({ payload: { vault: walletJSON as Vault, isUpdate: true }, originAppId, originBackupAccountSession: requestSession, + originBackupWriteIntent, }) : yield call(updateAppImageWorker, { payload: { wallets: [walletJSON as Wallet] }, originAppId, originBackupAccountSession: requestSession, + originBackupWriteIntent, }); if (!response?.updated) yield call(setServerBackupFailed, originAppId); } catch { diff --git a/src/store/sagas/wallets.ts b/src/store/sagas/wallets.ts index 94bfdd7e15..76fa1da571 100644 --- a/src/store/sagas/wallets.ts +++ b/src/store/sagas/wallets.ts @@ -42,7 +42,12 @@ import { Alert } from 'react-native'; import { KeeperApp } from 'src/models/interfaces/KeeperApp'; import { RealmSchema } from 'src/storage/realm/enum'; import Relay from 'src/services/backend/Relay'; -import { currentBackupAccountSession } from 'src/services/backup/transport'; +import { + clearVerifiedRemoteRevision, + currentBackupAccountSession, + markBackupMutation, +} from 'src/services/backup/transport'; +import type { BackupWriteIntent } from 'src/services/backup/intent'; import SigningServer from 'src/services/backend/SigningServer'; import WalletOperations from 'src/services/wallets/operations'; import WalletUtilities from 'src/services/wallets/operations/utils'; @@ -137,6 +142,8 @@ import { hideDeletingKeyModal, showKeyDeletedSuccessModal, isAutomaticCloudBackupEnabled, + invalidateBackupRepair, + setPendingAllBackup, } from '../reducers/bhr'; import { setElectrumNotConnectedErr } from '../reducers/login'; import { connectToNodeWorker } from './network'; @@ -239,10 +246,12 @@ export function* addNewWalletsWorker({ payload: newWalletInfo, originAppId, originBackupAccountSession, + originBackupWriteIntent, }: { payload: NewWalletInfo[]; originAppId?: string; originBackupAccountSession?: number; + originBackupWriteIntent?: BackupWriteIntent; }) { let sourceAppId = originAppId; const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); @@ -270,6 +279,7 @@ export function* addNewWalletsWorker({ payload: { wallets }, originAppId: sourceAppId, originBackupAccountSession: requestSession, + originBackupWriteIntent, }); if (response.updated) { assertWalletAccount(sourceAppId, requestSession); @@ -308,9 +318,11 @@ export function* addNewVaultWorker({ payload, originAppId, originBackupAccountSession, + originBackupWriteIntent, }: { originAppId?: string; originBackupAccountSession?: number; + originBackupWriteIntent?: BackupWriteIntent; payload: { newVaultInfo?: NewVaultInfo; isMigrated?: boolean; @@ -354,6 +366,7 @@ export function* addNewVaultWorker({ payload: { vault }, originAppId: sourceAppId, originBackupAccountSession: requestSession, + originBackupWriteIntent, }); if (newVaultResponse.updated) { assertWalletAccount(sourceAppId, requestSession); @@ -372,6 +385,7 @@ export function* addNewVaultWorker({ const archivedVaultresponse = yield call(updateVaultImageWorker, { originAppId: sourceAppId, originBackupAccountSession: requestSession, + originBackupWriteIntent, payload: { vault: { ...oldVault, @@ -418,11 +432,13 @@ export function* addSigningDeviceWorker({ callback, originAppId, originBackupAccountSession, + originBackupWriteIntent, }: { payload: { signers: Signer[] }; callback?: () => void; originAppId?: string; originBackupAccountSession?: number; + originBackupWriteIntent?: BackupWriteIntent; }) { if (!signers.length) return; const sourceAppId = originAppId || currentWalletAccountId(); @@ -434,6 +450,7 @@ export function* addSigningDeviceWorker({ payload: { signer }, originAppId: sourceAppId, originBackupAccountSession: requestSession, + originBackupWriteIntent, }); } try { @@ -560,6 +577,7 @@ export function* addSigningDeviceWorker({ payload: { signers: signersToUpdate }, originAppId: sourceAppId, originBackupAccountSession: requestSession, + originBackupWriteIntent, }); if (response.updated) { assertWalletAccount(sourceAppId, requestSession); @@ -603,10 +621,12 @@ function* deleteSigningDeviceWorker({ payload: { signers }, originAppId, originBackupAccountSession, + originBackupWriteIntent, }: { payload: { signers: Signer[] }; originAppId?: string; originBackupAccountSession?: number; + originBackupWriteIntent?: BackupWriteIntent; }) { const sourceAppId = originAppId || currentWalletAccountId(); const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); @@ -621,6 +641,7 @@ function* deleteSigningDeviceWorker({ const response = yield call(deleteAppImageEntityWorker, { originAppId: sourceAppId, originBackupAccountSession: requestSession, + originBackupWriteIntent, payload: { signerIds: signersToDeleteIds }, }); if (!response?.updated) { @@ -655,10 +676,12 @@ function* archiveSigningDeviceWorker({ payload: { signers }, originAppId, originBackupAccountSession, + originBackupWriteIntent, }: { payload: { signers: Signer[] }; originAppId?: string; originBackupAccountSession?: number; + originBackupWriteIntent?: BackupWriteIntent; }) { try { const sourceAppId = originAppId || currentWalletAccountId(); @@ -673,6 +696,7 @@ function* archiveSigningDeviceWorker({ yield call(updateSignerDetailsWorker, { originAppId: sourceAppId, originBackupAccountSession: requestSession, + originBackupWriteIntent, payload: { signer: signers[i], key: 'archived', @@ -697,10 +721,12 @@ function* migrateVaultWorker({ payload, originAppId, originBackupAccountSession, + originBackupWriteIntent, }: { payload: { newVaultInfo: NewVaultInfo; oldVaultId: string }; originAppId?: string; originBackupAccountSession?: number; + originBackupWriteIntent?: BackupWriteIntent; }) { try { const sourceAppId = originAppId || currentWalletAccountId(); @@ -710,6 +736,7 @@ function* migrateVaultWorker({ const migrated = yield call(addNewVaultWorker, { originAppId: sourceAppId, originBackupAccountSession: requestSession, + originBackupWriteIntent, payload: { newVaultInfo, isMigrated: true, oldVaultId }, }); const migratedVault = yield select((state: RootState) => state.vault.intrimVault); @@ -739,9 +766,11 @@ function* refreshWalletsWorker({ payload, originAppId, originBackupAccountSession, + originBackupWriteIntent, }: { originAppId?: string; originBackupAccountSession?: number; + originBackupWriteIntent?: BackupWriteIntent; payload: { wallets: (Wallet | Vault)[]; options: { hardRefresh?: boolean; addNotifications?: boolean; dustScan?: boolean }; @@ -843,6 +872,11 @@ function* refreshWalletsWorker({ currentWallets.push(getJSONFromRealmObject(currentWallet) as unknown as Wallet | Vault); } wallets = currentWallets; + const recoverableCountersBeforeSync = new Map(wallets.map((wallet) => [wallet.id, { + nextFreeAddressIndex: wallet.specs.nextFreeAddressIndex, + nextFreeChangeAddressIndex: wallet.specs.nextFreeChangeAddressIndex, + totalExternalAddresses: wallet.specs.totalExternalAddresses, + }])); let connectionMatchesScope = false; try { @@ -955,6 +989,7 @@ function* refreshWalletsWorker({ yield call(bulkUpdateLabelsWorker, { originAppId: sourceAppId, originBackupAccountSession: requestSession, + originBackupWriteIntent, payload: { labelChanges, UTXO: utxo, @@ -1142,6 +1177,23 @@ function* refreshWalletsWorker({ ); }); if (!persisted) throw new Error('Failed to persist refreshed wallet'); + const priorCounters = recoverableCountersBeforeSync.get(synchedWallet.id); + const counterChanged = priorCounters && ( + priorCounters.nextFreeAddressIndex !== synchedWallet.specs.nextFreeAddressIndex || + priorCounters.nextFreeChangeAddressIndex !== synchedWallet.specs.nextFreeChangeAddressIndex || + priorCounters.totalExternalAddresses !== synchedWallet.specs.totalExternalAddresses + ); + if (counterChanged) { + const backupEnabled = yield select((state: RootState) => + isAutomaticCloudBackupEnabled(state.bhr, sourceAppId) + ); + if (backupEnabled) { + markBackupMutation(sourceAppId); + clearVerifiedRemoteRevision(sourceAppId); + yield put(invalidateBackupRepair(sourceAppId)); + yield put(setPendingAllBackup({ appId: sourceAppId, pending: true })); + } + } if (options.addNotifications && newDustUTXOs.length > 0) { yield put(setPendingDustToast(synchedWallet.id)); @@ -1182,9 +1234,11 @@ export function* autoWalletsSyncWorker({ payload, originAppId, originBackupAccountSession, + originBackupWriteIntent, }: { originAppId?: string; originBackupAccountSession?: number; + originBackupWriteIntent?: BackupWriteIntent; payload: { syncAll?: boolean; hardRefresh?: boolean; @@ -1256,6 +1310,7 @@ export function* autoWalletsSyncWorker({ activeSucceeded = yield call(refreshWalletsWorker, { originAppId: sourceAppId, originBackupAccountSession: requestSession, + originBackupWriteIntent, payload: { wallets: active.map((wallet) => getJSONFromRealmObject(wallet) as unknown as Wallet | Vault), options: { hardRefresh, addNotifications }, @@ -1281,6 +1336,7 @@ export function* autoWalletsSyncWorker({ archivedSucceeded = yield call(refreshWalletsWorker, { originAppId: sourceAppId, originBackupAccountSession: requestSession, + originBackupWriteIntent, payload: { wallets: archived.map( (wallet) => getJSONFromRealmObject(wallet) as unknown as Wallet | Vault @@ -1312,6 +1368,7 @@ export function* autoWalletsSyncWorker({ yield put({ ...checkBackupFreshness(sourceAppId), originBackupAccountSession: requestSession, + originBackupWriteIntent, }); } } @@ -1412,7 +1469,7 @@ export const updateSignerPolicyWatcher = createWatcher( UPDATE_SIGNER_POLICY ); -export function* testcoinsWorker({ payload, originAppId, originBackupAccountSession }) { +export function* testcoinsWorker({ payload, originAppId, originBackupAccountSession, originBackupWriteIntent }) { const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); let { wallet } = payload; const faucetScope = yield select((state: RootState) => ({ @@ -1519,6 +1576,7 @@ export function* testcoinsWorker({ payload, originAppId, originBackupAccountSess const refreshed = yield call(refreshWalletsWorker, { originAppId: faucetScope.appId, originBackupAccountSession: requestSession, + originBackupWriteIntent, payload: { wallets: [wallet], options: { hardRefresh: true } }, }); if (refreshed) { @@ -1545,7 +1603,7 @@ export function* testcoinsWorker({ payload, originAppId, originBackupAccountSess export const testcoinsWatcher = createWatcher(testcoinsWorker, TEST_SATS_RECIEVE); -function* updateWalletDetailsWorker({ payload, originAppId, originBackupAccountSession }) { +function* updateWalletDetailsWorker({ payload, originAppId, originBackupAccountSession, originBackupWriteIntent }) { const sourceAppId = originAppId || currentWalletAccountId(); const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); const { @@ -1572,6 +1630,7 @@ function* updateWalletDetailsWorker({ payload, originAppId, originBackupAccountS payload: { wallets: [wallet] }, originAppId: sourceAppId, originBackupAccountSession: requestSession, + originBackupWriteIntent, }); if (response.updated) { assertWalletAccount(sourceAppId, requestSession); @@ -1595,7 +1654,7 @@ export const updateWalletDetailWatcher = createWatcher( UPDATE_WALLET_DETAILS ); -function* updateVaultDetailsWorker({ payload, originAppId, originBackupAccountSession }) { +function* updateVaultDetailsWorker({ payload, originAppId, originBackupAccountSession, originBackupWriteIntent }) { const sourceAppId = originAppId || currentWalletAccountId(); const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); const { @@ -1623,6 +1682,7 @@ function* updateVaultDetailsWorker({ payload, originAppId, originBackupAccountSe payload: { vault }, originAppId: sourceAppId, originBackupAccountSession: requestSession, + originBackupWriteIntent, }); if (response.updated) { assertWalletAccount(sourceAppId, requestSession); @@ -1647,7 +1707,7 @@ export const updateVaultDetailsWatcher = createWatcher( UPDATE_VAULT_DETAILS ); -export function* updateSignerDetailsWorker({ payload, originAppId, originBackupAccountSession }) { +export function* updateSignerDetailsWorker({ payload, originAppId, originBackupAccountSession, originBackupWriteIntent }) { const sourceAppId = originAppId || currentWalletAccountId(); const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); const { @@ -1667,6 +1727,7 @@ export function* updateSignerDetailsWorker({ payload, originAppId, originBackupA payload: { signers: [signer] }, originAppId: sourceAppId, originBackupAccountSession: requestSession, + originBackupWriteIntent, }); if (response.updated) { assertWalletAccount(sourceAppId, requestSession); @@ -1694,7 +1755,7 @@ export function* updateSignerDetailsWorker({ payload, originAppId, originBackupA export const updateSignerDetails = createWatcher(updateSignerDetailsWorker, UPDATE_SIGNER_DETAILS); -function* updateKeyDetailsWorker({ payload, originAppId, originBackupAccountSession }) { +function* updateKeyDetailsWorker({ payload, originAppId, originBackupAccountSession, originBackupWriteIntent }) { const sourceAppId = originAppId || currentWalletAccountId(); const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); assertWalletAccount(sourceAppId, requestSession); @@ -1747,6 +1808,7 @@ function* updateKeyDetailsWorker({ payload, originAppId, originBackupAccountSess payload: { vault, isUpdate: true }, originAppId: sourceAppId, originBackupAccountSession: requestSession, + originBackupWriteIntent, }); if (!response?.updated) yield call(setServerBackupFailed, sourceAppId); } @@ -1768,7 +1830,7 @@ function* updateKeyDetailsWorker({ payload, originAppId, originBackupAccountSess export const updateKeyDetails = createWatcher(updateKeyDetailsWorker, UPDATE_KEY_DETAILS); -function* deleteVaultWorker({ payload, originAppId, originBackupAccountSession }) { +function* deleteVaultWorker({ payload, originAppId, originBackupAccountSession, originBackupWriteIntent }) { const sourceAppId = originAppId || currentWalletAccountId(); const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); const { vaultId } = payload; @@ -1782,6 +1844,7 @@ function* deleteVaultWorker({ payload, originAppId, originBackupAccountSession } payload: { vaultIds: [vaultId] }, originAppId: sourceAppId, originBackupAccountSession: requestSession, + originBackupWriteIntent, }); if (response.updated) { assertWalletAccount(sourceAppId, requestSession); @@ -1806,7 +1869,7 @@ function* deleteVaultWorker({ payload, originAppId, originBackupAccountSession } export const deleteVaultyWatcher = createWatcher(deleteVaultWorker, DELETE_VAULT); -function* reinstateVaultWorker({ payload, originAppId, originBackupAccountSession }) { +function* reinstateVaultWorker({ payload, originAppId, originBackupAccountSession, originBackupWriteIntent }) { const sourceAppId = originAppId || currentWalletAccountId(); const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); const { vaultId } = payload; @@ -1826,6 +1889,7 @@ function* reinstateVaultWorker({ payload, originAppId, originBackupAccountSessio const response = yield call(updateVaultImageWorker, { originAppId: sourceAppId, originBackupAccountSession: requestSession, + originBackupWriteIntent, payload: { vault: { ...vault, @@ -1845,6 +1909,7 @@ function* reinstateVaultWorker({ payload, originAppId, originBackupAccountSessio yield call(updateSignerDetailsWorker, { originAppId: sourceAppId, originBackupAccountSession: requestSession, + originBackupWriteIntent, payload: { signer: signerMap[getKeyUID(vault.signers[i])], key: 'archived', @@ -1866,7 +1931,7 @@ function* reinstateVaultWorker({ payload, originAppId, originBackupAccountSessio export const reinstateVaultWatcher = createWatcher(reinstateVaultWorker, REINSTATE_VAULT); -function* refillMobileKeyWorker({ payload, originAppId, originBackupAccountSession }) { +function* refillMobileKeyWorker({ payload, originAppId, originBackupAccountSession, originBackupWriteIntent }) { const sourceAppId = originAppId || currentWalletAccountId(); const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); const { vaultKey } = payload; @@ -1891,6 +1956,7 @@ function* refillMobileKeyWorker({ payload, originAppId, originBackupAccountSessi yield call(updateKeyDetailsWorker, { originAppId: sourceAppId, originBackupAccountSession: requestSession, + originBackupWriteIntent, payload: { signer: signer.signerXpubs[XpubTypes.P2WSH][0], key: 'xpriv', value: xpriv }, }); } @@ -1905,9 +1971,11 @@ export const refillMobileKeyWatcher = createWatcher(refillMobileKeyWorker, REFIL function* refreshCanaryWalletsWorker({ originAppId, originBackupAccountSession, + originBackupWriteIntent, }: { originAppId?: string; originBackupAccountSession?: number; + originBackupWriteIntent?: BackupWriteIntent; } = {}) { try { const sourceAppId = originAppId || currentWalletAccountId(); @@ -1919,6 +1987,7 @@ function* refreshCanaryWalletsWorker({ yield call(refreshWalletsWorker, { originAppId: sourceAppId, originBackupAccountSession: requestSession, + originBackupWriteIntent, payload: { wallets: canaryWallets, options: { @@ -1941,10 +2010,12 @@ function* mergeSimilarKeysWorker({ payload, originAppId, originBackupAccountSession, + originBackupWriteIntent, }: { payload: { signer: Signer }; originAppId?: string; originBackupAccountSession?: number; + originBackupWriteIntent?: BackupWriteIntent; }) { try { const sourceAppId = originAppId || currentWalletAccountId(); @@ -2008,7 +2079,7 @@ function* mergeSimilarKeysWorker({ newSignerId: signer.masterFingerprint, newSignerDetails: encrytedSigner, }, - ], requestSession); + ], originBackupWriteIntent); if (updated) { console.log( `Signer ${s.masterFingerprint} has been merged with ${signer.masterFingerprint}` @@ -2047,7 +2118,7 @@ function* generateNewExternalAddressWorker({ const { wallet } = payload; const specs = { ...wallet.specs, totalExternalAddresses: wallet.specs.totalExternalAddresses + 1 }; - return yield call(() => { + const updated = yield call(() => { assertWalletAccount(sourceAppId, requestSession); return dbManager.updateObjectById( wallet.entityKind === EntityKind.VAULT ? RealmSchema.Vault : RealmSchema.Wallet, @@ -2055,6 +2126,20 @@ function* generateNewExternalAddressWorker({ { specs } ); }); + if (updated === true) { + const backupEnabled = yield select((state: RootState) => + isAutomaticCloudBackupEnabled(state.bhr, sourceAppId) + ); + if (backupEnabled) { + // This index is part of the recoverable wallet image, even though it + // does not issue an incremental relay request. + markBackupMutation(sourceAppId); + clearVerifiedRemoteRevision(sourceAppId); + yield put(invalidateBackupRepair(sourceAppId)); + yield put(setPendingAllBackup({ appId: sourceAppId, pending: true })); + } + } + return updated; } export const generateNewExternalAddressWatcher = createWatcher( diff --git a/src/store/store.ts b/src/store/store.ts index 835bd567ae..5faa66d616 100644 --- a/src/store/store.ts +++ b/src/store/store.ts @@ -28,6 +28,7 @@ import reduxPersistMigrations from './migrations'; import dbManager from 'src/storage/realm/dbManager'; import { RealmSchema } from 'src/storage/realm/enum'; import { + captureBackupWriteIntent, currentBackupAccountSession, observeBackupAccount, setBackupReconciliationHandler, @@ -122,6 +123,8 @@ const accountOwnedActions = new Set([ 'VALIDATE_SERVER_BACKUP', 'UPDATE_VERSION_HISTORY', 'MIGRATE_LABELS_329', + 'CHANGE_BITCOIN_NETWORK', + 'SETUP_KEEPER_APP', ]); const accountOwnedUtxoUiActions = new Set([ 'utxos/setSyncingUTXOs', @@ -169,6 +172,22 @@ const bindActionOrigin = (api) => (next) => (action) => { !('originBackupAccountSession' in action) ) action = { ...action, originBackupAccountSession: currentBackupAccountSession() }; + // Stamp the verified remote base at dispatch, before any saga can wait or + // mutate Realm. A caller that captured an earlier intent must never be + // silently rebased to the account's current remote state here. + if ( + action && + (accountOwnedActions.has(action.type) || + action.type === 'DELETE_BACKUP' || + action.type === 'BACKUP_ALL_SIGNERS_AND_VAULTS') && + !('originBackupWriteIntent' in action) + ) { + const appId = action.originAppId || action.appId || originAppId; + action = { + ...action, + originBackupWriteIntent: appId ? captureBackupWriteIntent(appId) : undefined, + }; + } return next(action); }; const middlewars = [bindActionOrigin, sagaMiddleware]; diff --git a/tests/recovery/accountBinding.test.cjs b/tests/recovery/accountBinding.test.cjs index e0ff768017..8c434be668 100644 --- a/tests/recovery/accountBinding.test.cjs +++ b/tests/recovery/accountBinding.test.cjs @@ -63,6 +63,31 @@ test('selected B blocks stale A writes even before Realm finishes switching', as assert.equal(f.collections.Wallet[0].specs.totalExternalAddresses, 4); }); +test('new receive address invalidates a verified backup after local persistence', async () => { + const f = fixture(); + const originAppId = f.app.id; + const saved = wallet('receive-wallet', enums.NetworkType.MAINNET); + saved.specs.totalExternalAddresses = 1; + f.collections.Wallet.push(saved); + const mutations = []; + const invalidatedRevisions = []; + f.scope.markBackupMutation = (appId) => mutations.push(appId); + f.scope.clearVerifiedRemoteRevision = (appId) => invalidatedRevisions.push(appId); + + const updated = await f.run('generateNewExternalAddressWorker', { + originAppId, + payload: { wallet: saved }, + }); + + assert.equal(updated, true); + assert.equal(f.collections.Wallet[0].specs.totalExternalAddresses, 2); + assert.deepEqual(mutations, [originAppId]); + assert.deepEqual(invalidatedRevisions, [originAppId]); + assert.ok(f.actions.some((action) => action.type === 'invalidateBackupRepair' && + action.payload === originAppId)); + assert.equal(f.state.bhr.pendingAllBackupByAppId[originAppId], true); +}); + test('verified A backup completion after selected B switch cannot enable A or report success', async () => { const f = fixture(); const originAppId = f.app.id; @@ -87,6 +112,9 @@ test('dispatch gate drops stale enable and success actions while preserving acco RealmSchema: { KeeperApp: 'KeeperApp' }, dbManager: { getObjectByIndex: () => ({ id: realmAppId }) }, currentBackupAccountSession: () => session, + captureBackupWriteIntent: (appId) => appId === selectedAppId ? { + appId, accountSession: session, baseRevision: 'a'.repeat(64), baselineEpoch: 1, + } : undefined, }; let selectedAppId = 'B'; let realmAppId = 'A'; @@ -125,13 +153,19 @@ test('dispatch gate drops stale enable and success actions while preserving acco middleware({ type: 'UPDATE_APP_IMAGE', payload: {} }); assert.equal(seen.at(-1).originAppId, 'A'); assert.equal(seen.at(-1).originBackupAccountSession, 1); + assert.equal(seen.at(-1).originBackupWriteIntent.accountSession, 1); + const firstIntent = seen.at(-1).originBackupWriteIntent; session += 2; middleware({ type: 'UPDATE_APP_IMAGE', payload: {}, originAppId: 'A' }); assert.equal(seen.at(-1).originBackupAccountSession, 3); + assert.equal(seen.at(-1).originBackupWriteIntent.accountSession, 3); middleware({ type: 'UPDATE_APP_IMAGE', payload: {}, originAppId: 'A', - originBackupAccountSession: 1 }); + originBackupAccountSession: 1, + originBackupWriteIntent: firstIntent }); assert.equal(seen.at(-1).originBackupAccountSession, 1, 'redispatch must retain the original session instead of adopting the new A session'); + assert.strictEqual(seen.at(-1).originBackupWriteIntent, firstIntent, + 'redispatch must retain its immutable source intent'); middleware({ type: 'DELETE_BACKUP', appId: 'A' }); assert.equal(seen.at(-1).originBackupAccountSession, 3); middleware({ type: 'UPDATE_VERSION_HISTORY', originAppId: 'A' }); diff --git a/tests/recovery/sendLabelOrigin.test.cjs b/tests/recovery/sendLabelOrigin.test.cjs index fb4ed2be02..f1896625a1 100644 --- a/tests/recovery/sendLabelOrigin.test.cjs +++ b/tests/recovery/sendLabelOrigin.test.cjs @@ -39,6 +39,7 @@ const scope = { dbManager: { getObjectByIndex: () => ({ id: 'B' }) }, RealmSchema: { KeeperApp: 'KeeperApp' }, currentBackupAccountSession: () => 0, + captureBackupWriteIntent: () => undefined, originAppId: 'A', vault: { id: 'A-vault' }, generateAbbreviatedOutputDescriptors: () => 'descriptor', diff --git a/tests/recovery/usdtBackup.test.cjs b/tests/recovery/usdtBackup.test.cjs index 2428d5921d..17a9980d38 100644 --- a/tests/recovery/usdtBackup.test.cjs +++ b/tests/recovery/usdtBackup.test.cjs @@ -13,6 +13,8 @@ const copy = (value) => JSON.parse(JSON.stringify(value)); // drive its dispatched action through the production saga and real AES codec. function hookFixture(options = {}) { const f = fixture(options); + // The hook and saga share one disposable account session. + f.scope.currentBackupAccountSession = () => 1; const source = ts.createSourceFile('useUSDTWallets.ts', fs.readFileSync( path.join(__dirname, '../../src/hooks/useUSDTWallets.ts'), 'utf8'), ts.ScriptTarget.Latest, true); @@ -25,6 +27,12 @@ function hookFixture(options = {}) { visit(source); assert.ok(callback, 'Production updateWallet callback exists'); const pending = [], order = [], errors = [], mutations = []; + const writeIntent = Object.freeze({ + appId: f.app.id, + accountSession: 1, + baseRevision: 'a'.repeat(64), + baselineEpoch: 1, + }); const dbManager = { getObjectById: (_schema, id) => { const stored = f.collections.USDTWallet.find((wallet) => wallet.id === id); @@ -49,6 +57,7 @@ function hookFixture(options = {}) { appId: f.app.id, renderAccountSession: 1, currentBackupAccountSession: () => 1, + captureBackupWriteIntent: (appId) => appId === f.app.id ? writeIntent : undefined, isOriginCurrent: () => f.app.id === f.state.storage.appId, markBackupMutation: (id) => mutations.push(id), invalidateBackupRepair: (payload) => ({ type: 'invalidateBackupRepair', payload }), @@ -57,6 +66,8 @@ function hookFixture(options = {}) { dispatch(action) { if (action.type === 'UPDATE_APP_IMAGE') { order.push('backup'); + assert.deepEqual(copy(action.originBackupWriteIntent), copy(writeIntent), + 'Backup action must retain the intent captured before persistence'); assert.deepEqual(copy(f.collections.USDTWallet[0]), copy(action.payload.wallets[0]), 'Backup must contain the successfully persisted wallet'); pending.push(f.run('updateAppImageWorker', action)); diff --git a/tests/recovery/walletRefreshIsolation.test.cjs b/tests/recovery/walletRefreshIsolation.test.cjs index 0e213367e6..c225d7bb2c 100644 --- a/tests/recovery/walletRefreshIsolation.test.cjs +++ b/tests/recovery/walletRefreshIsolation.test.cjs @@ -116,6 +116,36 @@ for (const [kind, archived] of [['Wallet', false], ['Vault', false], ['Vault', t }); } +test('refresh marks recoverable address counters pending after Realm persistence', async () => { + const f = refreshFixture('Wallet'); + const originAppId = f.app.id; + f.collections.Wallet[0].specs.nextFreeAddressIndex = 1; + f.collections.Wallet[0].specs.nextFreeChangeAddressIndex = 0; + f.collections.Wallet[0].specs.totalExternalAddresses = 2; + const mutations = []; + const invalidatedRevisions = []; + f.scope.markBackupMutation = (appId) => mutations.push(appId); + f.scope.clearVerifiedRemoteRevision = (appId) => invalidatedRevisions.push(appId); + f.scope.invalidateBackupRepair = (appId) => ({ type: 'invalidateBackupRepair', payload: appId }); + f.scope.setPendingAllBackup = (payload) => ({ type: 'setPendingAllBackup', payload }); + f.scope.WalletOperations.syncWalletsViaElectrumClient = (wallets) => { + const synced = f.scope.getJSONFromRealmObject(wallets[0]); + synced.specs.nextFreeAddressIndex = 2; + synced.specs.nextFreeChangeAddressIndex = 1; + synced.specs.totalExternalAddresses = 3; + synced.specs.hasNewUpdates = true; + return { synchedWallets: [{ synchedWallet: synced, newUTXOs: [] }] }; + }; + + assert.equal(await f.refresh(), true); + assert.equal(f.collections.Wallet[0].specs.totalExternalAddresses, 3); + assert.deepEqual(mutations, [originAppId]); + assert.deepEqual(invalidatedRevisions, [originAppId]); + assert.ok(f.actions.some((action) => action.type === 'invalidateBackupRepair' && + action.payload === originAppId)); + assert.equal(f.state.bhr.pendingAllBackupByAppId[originAppId], true); +}); + test('refresh paused at Electrum cannot overwrite newer A specs after A to B to A', async () => { const f = refreshFixture('Wallet', false); const originAppId = f.app.id; diff --git a/tests/store/versionHistory.test.ts b/tests/store/versionHistory.test.ts index fb0f368b52..95b438a239 100644 --- a/tests/store/versionHistory.test.ts +++ b/tests/store/versionHistory.test.ts @@ -40,6 +40,9 @@ jest.mock('react-native-device-info', () => ({ getBuildNumber: () => '624' })); const previousVersion = '2.5.15'; const newVersion = '2.5.16'; +const writeIntent = { + appId: 'test-app', accountSession: 1, baseRevision: 'a'.repeat(64), baselineEpoch: 1, +}; function setActiveAccount(appId: string, enabled = true) { (dbManager.getObjectByIndex as jest.Mock).mockReturnValue({ id: appId }); @@ -57,7 +60,7 @@ function writeHistory(action) { expect(choice.type).toBe('SELECT'); expect(choice.payload.selector(store.getState())).toBe(true); expect(worker.next(true).value).toEqual( - call(Relay.updateAppImage, { appId, version: newVersion }, 1) + call(Relay.updateAppImage, { appId, version: newVersion }, action.originBackupWriteIntent) ); const write = worker.next({} as any).value as any; expect(write.payload.fn).toBe(dbManager.createObject); @@ -72,7 +75,7 @@ function recoverWithoutDuplicateHistory(action) { expect(worker.next().value).toEqual(call(dbManager.getObjectByIndex, RealmSchema.KeeperApp)); expect((worker.next({ id: appId } as any).value as any).type).toBe('SELECT'); expect(worker.next(true).value).toEqual( - call(Relay.updateAppImage, { appId, version: newVersion }, 1) + call(Relay.updateAppImage, { appId, version: newVersion }, action.originBackupWriteIntent) ); expect(worker.next({} as any).done).toBe(true); } @@ -81,13 +84,15 @@ describe('Version History retains recovery provenance through required upgrades' beforeEach(() => setActiveAccount('test-app')); test.each([true, false])('upgrade handles recovery=%s without duplicate history', (isRecovery) => { - const upgrade = applyUpgradeSequence({ previousVersion, newVersion, isRecovery }); + const upgrade = applyUpgradeSequence({ previousVersion, newVersion, isRecovery, + originBackupWriteIntent: writeIntent }); expect(upgrade.next().value).toEqual(call(dbManager.getObjectByIndex, RealmSchema.KeeperApp)); expect(upgrade.next({ id: 'test-app' } as any).value).toEqual(put(setAppVersion(newVersion))); const history = upgrade.next().value as any; expect(history).toEqual(put({ ...updateVersionHistory(previousVersion, newVersion, isRecovery, 'test-app'), originBackupAccountSession: 1, + originBackupWriteIntent: writeIntent, })); if (isRecovery) { recoverWithoutDuplicateHistory(history.payload.action); From c5a60732569c7159678d46fba917ff3a9cea3aaa Mon Sep 17 00:00:00 2001 From: Orlando Arzola <> Date: Mon, 12 Oct 2026 18:16:00 +0200 Subject: [PATCH 135/254] fix: keep keypad native container stable when enabling input --- src/components/AppNumPad/KeyPadView.tsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/components/AppNumPad/KeyPadView.tsx b/src/components/AppNumPad/KeyPadView.tsx index efe8d6012d..da2c31702b 100644 --- a/src/components/AppNumPad/KeyPadView.tsx +++ b/src/components/AppNumPad/KeyPadView.tsx @@ -28,8 +28,9 @@ const KeyPadView: React.FC = ({ }: Props) => { const { colorMode } = useColorMode(); + // Keep the native keypad container stable when pointerEvents changes after login unlocks. return ( - + Date: Mon, 12 Oct 2026 21:49:16 +0530 Subject: [PATCH 136/254] Invalidate backup verification for local Realm recovery writes --- src/services/backup/localMutation.ts | 45 +++++++ src/storage/realm/dbManager.ts | 20 +++ src/store/store.ts | 18 +++ .../localMutationInvalidation.test.cjs | 115 ++++++++++++++++++ 4 files changed, 198 insertions(+) create mode 100644 src/services/backup/localMutation.ts create mode 100644 tests/recovery/localMutationInvalidation.test.cjs diff --git a/src/services/backup/localMutation.ts b/src/services/backup/localMutation.ts new file mode 100644 index 0000000000..02f9df7eea --- /dev/null +++ b/src/services/backup/localMutation.ts @@ -0,0 +1,45 @@ +import { RealmSchema } from 'src/storage/realm/enum'; +import { canonical, recoveryContent } from './image'; + +const backedUpSchemas = new Set([ + RealmSchema.Wallet, + RealmSchema.USDTWallet, + RealmSchema.Vault, + RealmSchema.Signer, + RealmSchema.VaultSigner, + RealmSchema.Tags, + RealmSchema.NodeConnect, +]); + +export function isBackedUpSchema(schema: RealmSchema) { + return backedUpSchemas.has(schema); +} + +function plain(record: any) { + return record?.toJSON ? record.toJSON() : record; +} + +function recoveryView(schema: RealmSchema, record: any) { + if (schema === RealmSchema.Wallet || schema === RealmSchema.USDTWallet) + return recoveryContent('wallets', record); + if (schema === RealmSchema.Vault) return recoveryContent('vaults', record); + if (schema === RealmSchema.NodeConnect) return recoveryContent('nodes', record); + // A VaultSigner is folded into each backed-up vault image. Conservatively + // invalidate on any change to it or to a signer/label record. + return record; +} + +// Run before the Realm write. This also covers helpers that may commit some +// fields before returning failure. Cache-only wallet refreshes and active-node +// rotation cannot change the encrypted recovery image. +export function changesBackedUpRecovery(schema: RealmSchema, previous: any, patch: any) { + if (!isBackedUpSchema(schema) || !previous || !patch || typeof patch !== 'object') return false; + try { + const before = plain(previous); + const after = { ...before, ...patch }; + return canonical(recoveryView(schema, before)) !== canonical(recoveryView(schema, after)); + } catch { + // If Realm cannot be normalized, an uncertain write must invalidate trust. + return true; + } +} diff --git a/src/storage/realm/dbManager.ts b/src/storage/realm/dbManager.ts index 1bb07e26ab..2d66de3167 100644 --- a/src/storage/realm/dbManager.ts +++ b/src/storage/realm/dbManager.ts @@ -1,6 +1,18 @@ +import { changesBackedUpRecovery, isBackedUpSchema } from 'src/services/backup/localMutation'; import { RealmSchema } from './enum'; import realm from './realm'; +let recoveryMutationObserver: () => void = () => undefined; +const setRecoveryMutationObserver = (observer: () => void) => { + recoveryMutationObserver = observer; +}; +const notifyRecoveryCreateOrDelete = (schema: RealmSchema, hasObject: boolean) => { + if (hasObject && isBackedUpSchema(schema)) recoveryMutationObserver(); +}; +const notifyRecoveryUpdate = (schema: RealmSchema, object: any, updateProps: any) => { + if (changesBackedUpRecovery(schema, object, updateProps)) recoveryMutationObserver(); +}; + /** * intializes realm * @param {ArrayBuffer|ArrayBufferView|Int8Array} key @@ -27,6 +39,7 @@ const deleteRealm = (key: ArrayBuffer | ArrayBufferView | Int8Array) => realm.de */ const createObject = (schema: RealmSchema, object: any, updateMode = Realm.UpdateMode.All) => { try { + notifyRecoveryCreateOrDelete(schema, !!object); const hasCreated = realm.create(schema, object, updateMode); return hasCreated; } catch (err) { @@ -45,6 +58,7 @@ const createObjectBulk = ( updateMode = Realm.UpdateMode.All ) => { try { + notifyRecoveryCreateOrDelete(schema, !!objects?.length); const hasCreated = realm.createBulk(schema, objects, updateMode); return hasCreated; } catch (err) { @@ -105,6 +119,7 @@ const getObjectByPrimaryId = (schema: RealmSchema, name: string, primaryId: stri const updateObjectById = (schema: RealmSchema, id: string, updateProps: any) => { try { const object = getObjectById(schema, id); + notifyRecoveryUpdate(schema, object, updateProps); for (const [key, value] of Object.entries(updateProps)) { realm.write(() => { object[key] = value; @@ -131,6 +146,7 @@ const updateObjectByPrimaryId = ( ) => { try { const object = getObjectByPrimaryId(schema, name, primaryId); + notifyRecoveryUpdate(schema, object, updateProps); for (const [key, value] of Object.entries(updateProps)) { realm.write(() => { object[key] = value; @@ -177,6 +193,7 @@ const getCollection = (schema: RealmSchema) => { const deleteObjectById = (schema: RealmSchema, id: string) => { try { const object = getObjectById(schema, id); + notifyRecoveryCreateOrDelete(schema, !!object); realm.delete(object); return true; } catch (err) { @@ -192,6 +209,7 @@ const deleteObjectById = (schema: RealmSchema, id: string) => { const deleteObjectByPrimaryKey = (schema: RealmSchema, key: string, value: any) => { try { const object = getObjectByPrimaryId(schema, key, value); + notifyRecoveryCreateOrDelete(schema, !!object); realm.delete(object); return true; } catch (err) { @@ -215,6 +233,7 @@ const updateObjectByQuery = ( const objects = realm.get(schema); const object = objects.find(queryFn); if (object) { + notifyRecoveryUpdate(schema, object, updateProps); for (const [key, value] of Object.entries(updateProps)) { realm.write(() => { object[key] = value; @@ -268,4 +287,5 @@ export default { getObjectByField, updateObjectByQuery, getObjectByQuery, + setRecoveryMutationObserver, }; diff --git a/src/store/store.ts b/src/store/store.ts index 5faa66d616..9a885b5f13 100644 --- a/src/store/store.ts +++ b/src/store/store.ts @@ -30,6 +30,8 @@ import { RealmSchema } from 'src/storage/realm/enum'; import { captureBackupWriteIntent, currentBackupAccountSession, + clearVerifiedRemoteRevision, + markBackupMutation, observeBackupAccount, setBackupReconciliationHandler, setBackupUploadGuard, @@ -233,5 +235,21 @@ setBackupReconciliationHandler((appId) => { // settled and a later inspection captures the resulting local state. }); +dbManager.setRecoveryMutationObserver(() => { + let appId: string | undefined; + try { + appId = (dbManager.getObjectByIndex(RealmSchema.KeeperApp) as unknown as KeeperApp)?.id; + } catch { + return; + } + if (!appId) return; + // Realm can change while an inspection is between its final local capture + // and completion. Invalidate the revision before any recovery write commits. + markBackupMutation(appId); + clearVerifiedRemoteRevision(appId); + store.dispatch(invalidateBackupRepair(appId)); + store.dispatch(setPendingAllBackup({ appId, pending: true })); +}); + sagaMiddleware.run(rootSaga); export const persistor = persistStore(store); diff --git a/tests/recovery/localMutationInvalidation.test.cjs b/tests/recovery/localMutationInvalidation.test.cjs new file mode 100644 index 0000000000..40167942ee --- /dev/null +++ b/tests/recovery/localMutationInvalidation.test.cjs @@ -0,0 +1,115 @@ +const { test } = require('node:test'); +const assert = require('node:assert/strict'); +const { loadModule, image, harness, wallet, encryption } = require('./helpers.cjs'); + +function fixture(rows = {}) { + const { RealmSchema } = loadModule('src/storage/realm/enum.ts'); + const changes = loadModule('src/services/backup/localMutation.ts', { + 'src/storage/realm/enum': { RealmSchema }, './image': image, + }); + const collections = new Map(Object.entries(rows)); + const list = (schema) => { + const records = collections.get(schema) || []; + records.filtered = (_query, id) => records.filter((record) => String(record.id) === String(id)); + return records; + }; + const realm = { + get: list, + write: (callback) => callback(), + create: (schema, value) => { list(schema).push(value); return true; }, + createBulk: (schema, values) => { list(schema).push(...values); return true; }, + delete: (value) => { + for (const records of collections.values()) { + const index = records.indexOf(value); + if (index >= 0) records.splice(index, 1); + } + }, + }; + const manager = loadModule('src/storage/realm/dbManager.ts', { + './enum': { RealmSchema }, './realm': realm, + 'src/services/backup/localMutation': changes, + }, { Realm: { UpdateMode: { All: 'all' } } }).default; + return { manager, RealmSchema, list }; +} + +function realmRecord(value) { + const record = { ...value }; + Object.defineProperty(record, 'toJSON', { + value: () => JSON.parse(JSON.stringify({ ...record })), + }); + return record; +} + +test('Realm recovery writes invalidate before commit while cache-only changes do not', () => { + const w = realmRecord(wallet()); + const node = realmRecord({ id: 'node', host: 'fixture', isConnected: false }); + const f = fixture({ Wallet: [w], NodeConnect: [node] }); + const seen = []; + f.manager.setRecoveryMutationObserver(() => seen.push(w.specs.nextFreeAddressIndex)); + + const cache = { ...w.specs, balances: { confirmed: 25 }, transactions: [{ txid: 'fixture' }] }; + assert.equal(f.manager.updateObjectById(f.RealmSchema.Wallet, w.id, { specs: cache }), true); + assert.deepEqual(seen, []); + assert.equal(f.manager.updateObjectById(f.RealmSchema.NodeConnect, node.id, { isConnected: true }), true); + assert.deepEqual(seen, []); + + const recovery = { ...w.specs, nextFreeAddressIndex: 2 }; + assert.equal(f.manager.updateObjectById(f.RealmSchema.Wallet, w.id, { specs: recovery }), true); + assert.deepEqual(seen, [1], 'observer runs before the recovery write commits'); + assert.equal(w.specs.nextFreeAddressIndex, 2); + assert.equal(f.manager.updateObjectById(f.RealmSchema.Wallet, w.id, + { presentationData: { name: 'Renamed wallet' } }), true); + assert.deepEqual(seen, [1, 2]); + + const manualChoice = { txId: 'coin', vout: 0, isManualOverride: true, spendability: 'spendable' }; + assert.equal(f.manager.updateObjectById(f.RealmSchema.Wallet, w.id, + { specs: { ...w.specs, confirmedUTXOs: [manualChoice] } }), true); + assert.deepEqual(seen, [1, 2, 2]); +}); + +test('create, delete, signer, and vault edits invalidate; unrelated Realm records do not', () => { + const vault = realmRecord({ id: 'vault', archived: false, specs: {}, signers: [] }); + const signer = realmRecord({ id: 'signer', signerPolicy: { limit: 1 } }); + const f = fixture({ Vault: [vault], Signer: [signer], UAI: [] }); + let invalidations = 0; + f.manager.setRecoveryMutationObserver(() => invalidations++); + f.manager.updateObjectById(f.RealmSchema.Vault, vault.id, { archived: true }); + f.manager.updateObjectById(f.RealmSchema.Signer, signer.id, { signerPolicy: { limit: 2 } }); + f.manager.createObject(f.RealmSchema.Tags, { id: 'tag', label: 'fixture' }); + f.manager.deleteObjectById(f.RealmSchema.Vault, vault.id); + f.manager.createObject(f.RealmSchema.UAI, { id: 'notice' }); + assert.equal(invalidations, 4); +}); + +test('a receive counter mutation at the final verification boundary cannot report verified', async () => { + const f = harness(); + const w = realmRecord(wallet()); + f.local.Wallet.push(w); + f.remote.wallets.wallet = encryption.encrypt( + encryption.generateEncryptionKey(f.app.primarySeed), JSON.stringify(w) + ); + const db = fixture({ Wallet: f.local.Wallet }).manager; + let pending = false; + db.setRecoveryMutationObserver(() => { + f.transport.markBackupMutation(f.app.id); + f.transport.clearVerifiedRemoteRevision(f.app.id); + pending = true; + }); + const original = image.compareImages; + let comparisons = 0; + image.compareImages = async (...args) => { + const result = await original(...args); + if (++comparisons === 2) queueMicrotask(() => db.updateObjectById('Wallet', w.id, + { specs: { ...w.specs, nextFreeAddressIndex: 2 } })); + return result; + }; + try { + assert.equal(await f.run(false), 'unverified'); + assert.equal(w.specs.nextFreeAddressIndex, 2); + assert.equal(f.transport.backupRevision(f.app.id), 1); + assert.equal(pending, true); + assert.deepEqual(f.calls, ['getBackupSnapshot']); + } finally { + image.compareImages = original; + } +}); From df9a9d49ddddbbd77246c55ece10378cb475f1fe Mon Sep 17 00:00:00 2001 From: Anant Date: Mon, 12 Oct 2026 21:52:05 +0530 Subject: [PATCH 137/254] Preserve source intent across async label import Accept the typed intent at the disabled transport boundary and update deterministic contract fixtures. --- src/components/ImportExportLabels/index.tsx | 13 ++++++++- src/services/backup/transport.ts | 5 ++-- .../faucetVisibilityAcceptance.test.cjs | 3 +- tests/recovery/fingerprintTransport.test.cjs | 8 +++-- tests/recovery/revisionedMutation.test.cjs | 4 ++- tests/recovery/sendLabelOrigin.test.cjs | 29 +++++++++++++++++++ tests/recovery/upgradeAccountSession.test.cjs | 17 ++++++++--- tests/recovery/usdtAccountDelete.test.cjs | 7 +++-- 8 files changed, 72 insertions(+), 14 deletions(-) diff --git a/src/components/ImportExportLabels/index.tsx b/src/components/ImportExportLabels/index.tsx index 91d07f13d3..6125fa5d16 100644 --- a/src/components/ImportExportLabels/index.tsx +++ b/src/components/ImportExportLabels/index.tsx @@ -10,6 +10,7 @@ import { hp, wp } from 'src/constants/responsive'; import ActivityIndicatorView from '../AppActivityIndicator/ActivityIndicatorView'; import { useAppSelector } from 'src/store/hooks'; import Colors from 'src/theme/Colors'; +import { captureBackupWriteIntent, currentBackupAccountSession } from 'src/services/backup/transport'; interface ImportExportLabelsProps { vault: any; @@ -35,6 +36,8 @@ const ImportExportLabels: React.FC = ({ const [isSyncingLabels, setIsSyncingLabels] = useState(false); const handleImportLabels = async () => { + const originBackupAccountSession = currentBackupAccountSession(); + const originBackupWriteIntent = captureBackupWriteIntent(originAppId); try { setIsSyncingLabels(true); await importFile( @@ -103,7 +106,15 @@ const ImportExportLabels: React.FC = ({ origin: standardizeDescriptor(label.origin), })); - dispatch(importLabels({ labels: standardizedLabels }, originAppId)); + if (originBackupAccountSession !== currentBackupAccountSession()) { + onError('Account changed; please import the wallet labels again'); + return; + } + dispatch({ + ...importLabels({ labels: standardizedLabels }, originAppId), + originBackupAccountSession, + originBackupWriteIntent, + }); onSuccess(`Imported ${walletLabels.length} labels successfully`); } catch (parseError) { diff --git a/src/services/backup/transport.ts b/src/services/backup/transport.ts index 475d7a1597..3406f15117 100644 --- a/src/services/backup/transport.ts +++ b/src/services/backup/transport.ts @@ -144,7 +144,7 @@ export async function boundedBackupPost( } export function backupPost( - _path: string, body: any, explicitChoice = false, expectedSession?: number + _path: string, body: any, explicitChoice = false, intent?: BackupWriteIntent ): Promise<{ data: any; json?: any }> { const appId = body.appId || body.appID; if (!appId) return Promise.reject(new Error('Missing backup account')); @@ -154,8 +154,7 @@ export function backupPost( markBackupMutation(appId); clearVerifiedRemoteRevision(appId); onBackupReconciliationRequired(appId); - const requestSession = expectedSession ?? currentBackupAccountSession(); - if (requestSession !== currentBackupAccountSession()) + if (intent && (intent.appId !== appId || intent.accountSession !== currentBackupAccountSession())) return Promise.reject(new Error('Backup account session changed')); if (!canAccessBackupAccount(appId, explicitChoice)) return Promise.reject(new Error('Backup account is not active or opted in')); diff --git a/tests/recovery/faucetVisibilityAcceptance.test.cjs b/tests/recovery/faucetVisibilityAcceptance.test.cjs index 67871acbb5..0359ea76f7 100644 --- a/tests/recovery/faucetVisibilityAcceptance.test.cjs +++ b/tests/recovery/faucetVisibilityAcceptance.test.cjs @@ -123,7 +123,8 @@ test('faucet reports pending when the funded txid persists but visible balance s }); test('Receive Test Sats dispatch keeps its original account when queued across a switch', () => { - const scope = { Set, currentBackupAccountSession: () => 0 }; + const scope = { Set, currentBackupAccountSession: () => 0, + captureBackupWriteIntent: () => undefined }; vm.runInNewContext(ts.transpileModule(functions('src/store/store.ts', [ 'accountOwnedActions', 'accountOwnedUtxoUiActions', 'bindActionOrigin', ]), { compilerOptions: { target: ts.ScriptTarget.ES2020 } }).outputText, scope); diff --git a/tests/recovery/fingerprintTransport.test.cjs b/tests/recovery/fingerprintTransport.test.cjs index 2bc836f93b..fda687e503 100644 --- a/tests/recovery/fingerprintTransport.test.cjs +++ b/tests/recovery/fingerprintTransport.test.cjs @@ -5,6 +5,9 @@ const { loadModule, harness, wallet } = require('./helpers.cjs'); const signerChanges = [{ oldSignerId: 'fixture-old', newSignerId: 'fixture-new', newSignerDetails: 'fixture-ciphertext', }]; +const intent = (appId, accountSession = 0) => ({ + appId, accountSession, baseRevision: 'a'.repeat(64), baselineEpoch: 0, +}); function relay(transport, errors = []) { return loadModule('src/services/backend/Relay.ts', { @@ -37,7 +40,8 @@ for (const repair of [false, true]) { await started; const previousRevision = f.transport.backupRevision(f.app.id); await assert.rejects( - relay(f.transport).migrateXfp(f.app.id, signerChanges), + relay(f.transport).migrateXfp(f.app.id, signerChanges, + intent(f.app.id, f.transport.currentBackupAccountSession())), /Failed to do migrate the xfp/ ); assert.equal(f.transport.backupRevision(f.app.id), previousRevision + 1); @@ -60,7 +64,7 @@ test('direct migration attempts never send ciphertext and keep redacted errors', transport.setBackupReconciliationHandler((id) => pending.push(id)); const service = relay(transport, errors); for (let index = 0; index < 2; index++) { - await assert.rejects(service.migrateXfp('fixture-account', signerChanges), + await assert.rejects(service.migrateXfp('fixture-account', signerChanges, intent('fixture-account')), /Failed to do migrate the xfp/); } assert.equal(errors.length, 2); diff --git a/tests/recovery/revisionedMutation.test.cjs b/tests/recovery/revisionedMutation.test.cjs index a52778d46b..3d39e8ab99 100644 --- a/tests/recovery/revisionedMutation.test.cjs +++ b/tests/recovery/revisionedMutation.test.cjs @@ -70,10 +70,12 @@ test('verified baseline is session bound and invalidated by any attempted mutati test('stale sessions and denied accounts also request reconciliation', async () => { const f = fixture(); const firstSession = f.transport.currentBackupAccountSession(); + const oldIntent = { appId: 'A', accountSession: firstSession, + baseRevision: 'a'.repeat(64), baselineEpoch: 0 }; f.transport.observeBackupAccount('B'); f.transport.observeBackupAccount('A'); await assert.rejects( - f.transport.backupPost('fixture/migrateXfps', { appId: 'A' }, false, firstSession), + f.transport.backupPost('fixture/migrateXfps', { appId: 'A' }, false, oldIntent), /Backup account session changed/ ); await assert.rejects( diff --git a/tests/recovery/sendLabelOrigin.test.cjs b/tests/recovery/sendLabelOrigin.test.cjs index f1896625a1..4f8dbc3495 100644 --- a/tests/recovery/sendLabelOrigin.test.cjs +++ b/tests/recovery/sendLabelOrigin.test.cjs @@ -97,3 +97,32 @@ test('a file import callback from A keeps A after the active account switches to assert.equal(seen[0].type, 'IMPORT_LABELS'); assert.equal(seen[0].originAppId, 'A'); }); + +test('an async label import from an old A session cannot rebase after A to B to A', async () => { + let session = 3; + let completeImport; + const seen = []; + const errors = []; + scope.currentBackupAccountSession = () => session; + scope.captureBackupWriteIntent = (appId) => ({ + appId, accountSession: session, baseRevision: 'a'.repeat(64), baselineEpoch: 0, + }); + scope.onError = (message) => errors.push(message); + scope.dispatch = scope.actions.bindActionOrigin({ + getState: () => ({ storage: { appId: 'A' } }), + })((action) => { seen.push(action); return action; }); + scope.importFile = (onFile) => new Promise((resolve) => { + completeImport = async () => { + await onFile(JSON.stringify({ + type: 'tx', ref: 'txid', label: 'note', origin: 'descriptor', + })); + resolve(); + }; + }); + const started = scope.actions.handleImportLabels(); + session = 5; + await completeImport(); + await started; + assert.deepEqual(seen, []); + assert.equal(errors.length, 1); +}); diff --git a/tests/recovery/upgradeAccountSession.test.cjs b/tests/recovery/upgradeAccountSession.test.cjs index 88aa50f998..39b55f7f33 100644 --- a/tests/recovery/upgradeAccountSession.test.cjs +++ b/tests/recovery/upgradeAccountSession.test.cjs @@ -7,6 +7,8 @@ const ts = require('typescript'); const semver = require('semver'); const { runSaga } = require('redux-saga'); const { call, put, select } = require('redux-saga/effects'); +const writeIntent = { appId: 'A', accountSession: 7, + baseRevision: 'a'.repeat(64), baselineEpoch: 0 }; const file = 'src/store/sagas/upgrade.ts'; const source = ts.createSourceFile( @@ -99,7 +101,8 @@ test('upgrade sequence stamps its original session on nested migrations and disp f.scope.healthCheckTimelineMigration = function* () {}; f.scope.migrateServerKeyPolicy = function* () {}; - await f.run('applyUpgradeSequence', { previousVersion: '1.0.5', newVersion: '2.5.17' }); + await f.run('applyUpgradeSequence', { previousVersion: '1.0.5', newVersion: '2.5.17', + originBackupWriteIntent: writeIntent }); assert.equal(nested.length, 3); assert.ok(nested.every(([, appId, session]) => appId === 'A' && session === 7)); @@ -109,6 +112,8 @@ test('upgrade sequence stamps its original session on nested migrations and disp assert.equal(backupActions.length, 2); assert.ok(backupActions.every((action) => action.originAppId === 'A' && action.originBackupAccountSession === 7)); + assert.ok(backupActions.every((action) => + action.originBackupWriteIntent?.baseRevision === writeIntent.baseRevision)); }); test('upgrade sequence paused on its first app read stops after A to B to A', async () => { @@ -152,10 +157,12 @@ test('upgrade invoked after A to B to A rejects the caller original session befo test('version update passes session to Relay and blocks stale A to B to A action', async () => { const f = fixture(['updateVersionHistoryWorker']); const action = { originAppId: 'A', originBackupAccountSession: 7, + originBackupWriteIntent: writeIntent, payload: { previousVersion: '2.5.16', newVersion: '2.5.17', isRecovery: true } }; await f.run('updateVersionHistoryWorker', action); assert.equal(f.state.relay.length, 1); - assert.equal(f.state.relay[0][2], 7); + assert.equal(f.state.relay[0][2].accountSession, 7); + assert.equal(f.state.relay[0][2].baseRevision, writeIntent.baseRevision); const entered = deferred(); const resume = deferred(); @@ -176,10 +183,12 @@ test('version update passes session to Relay and blocks stale A to B to A action test('label migration passes session to Relay and blocks stale A to B to A action', async () => { const f = fixture(['migrateLablesWorker']); - const action = { originAppId: 'A', originBackupAccountSession: 7 }; + const action = { originAppId: 'A', originBackupAccountSession: 7, + originBackupWriteIntent: writeIntent }; await f.run('migrateLablesWorker', action); assert.equal(f.state.relay.length, 1); - assert.equal(f.state.relay[0][4], 7); + assert.equal(f.state.relay[0][4].accountSession, 7); + assert.equal(f.state.relay[0][4].baseRevision, writeIntent.baseRevision); const entered = deferred(); const resume = deferred(); diff --git a/tests/recovery/usdtAccountDelete.test.cjs b/tests/recovery/usdtAccountDelete.test.cjs index 1326c58de2..86ac544cae 100644 --- a/tests/recovery/usdtAccountDelete.test.cjs +++ b/tests/recovery/usdtAccountDelete.test.cjs @@ -92,9 +92,12 @@ function fixture({ consent = true, creationPaused = true, switchDuringRemote = f setPendingAllBackup: (payload) => ({ type: 'SET_PENDING_ALL_BACKUP', payload }), markBackupMutation: (appId) => state.invalidated.push(appId), currentBackupAccountSession: () => state.session, + captureBackupWriteIntent: (appId) => ({ + appId, accountSession: state.session, baseRevision: 'a'.repeat(64), baselineEpoch: 0, + }), dispatch: (action) => state.dispatched.push(action), - Relay: { deleteAppImageEntity: async (body, accountSession) => { - state.remote.push({ ...body, accountSession }); + Relay: { deleteAppImageEntity: async (body, intent) => { + state.remote.push({ ...body, accountSession: intent?.accountSession, intent }); if (switchDuringRemote || switchDuringRemoteRoundTrip) { state.active = 'B'; state.session += 1; From ee99ad4428939e7cf7ceacf924bbe3364854e5c4 Mon Sep 17 00:00:00 2001 From: Anant Date: Mon, 12 Oct 2026 21:57:37 +0530 Subject: [PATCH 138/254] Commit backup verification state atomically after inspection Bind the final UI state to the exact revision and verified remote intent emitted by inspection. Keep a local recovery write from restoring verified status while saga events drain. --- src/store/reducers/bhr.ts | 12 +++++++ src/store/sagas/bhr.ts | 55 +++++++++++++++++++++++++------ tests/recovery/sagaState.test.cjs | 42 +++++++++++++++++++---- 3 files changed, 92 insertions(+), 17 deletions(-) diff --git a/src/store/reducers/bhr.ts b/src/store/reducers/bhr.ts index 1effed9274..19b079108e 100644 --- a/src/store/reducers/bhr.ts +++ b/src/store/reducers/bhr.ts @@ -279,6 +279,17 @@ const bhrSlice = createSlice({ (state.backupRepairStateByAppId ??= {})[appId] = phase; (state.backupRepairCompletedByAppId ??= {})[appId] = phase === 'verified'; }, + commitBackupInspection: ( + state, + action: PayloadAction<{ appId: string; phase: RepairPhase; pending: boolean }> + ) => { + const { appId, phase, pending } = action.payload; + if (!appId) return; + (state.backupRepairStateByAppId ??= {})[appId] = phase; + (state.backupRepairCompletedByAppId ??= {})[appId] = phase === 'verified'; + (state.pendingAllBackupByAppId ??= {})[appId] = pending; + state.pendingAllBackup = false; + }, setBackupRepairRunning: (state, action: PayloadAction<{ appId: string; running: boolean }>) => { (state.backupRepairRunningByAppId ??= {})[action.payload.appId] = action.payload.running; }, @@ -374,6 +385,7 @@ export const { setPendingAllBackup, setBackupRepairState, + commitBackupInspection, setBackupRepairRunning, invalidateBackupRepair, diff --git a/src/store/sagas/bhr.ts b/src/store/sagas/bhr.ts index d48ab1a7ad..a6be561c57 100644 --- a/src/store/sagas/bhr.ts +++ b/src/store/sagas/bhr.ts @@ -2,7 +2,12 @@ import { buffers, END, eventChannel } from 'redux-saga'; import { inspectBackup, RepairPhase } from 'src/services/backup/repair'; import { prepareRecoveryImage } from 'src/services/backup/restore'; import { BackupImage, pauseBackup } from 'src/services/backup/image'; -import { currentBackupAccountSession, markBackupMutation } from 'src/services/backup/transport'; +import { + backupRevision, + captureBackupWriteIntent, + currentBackupAccountSession, + markBackupMutation, +} from 'src/services/backup/transport'; import type { BackupWriteIntent } from 'src/services/backup/intent'; import * as bip39 from 'bip39'; import { Wallet } from 'src/services/wallets/interfaces/wallet'; @@ -54,6 +59,7 @@ import { setBackupAllLoading, setBackupAllSuccess, setBackupRepairState, + commitBackupInspection, setBackupRepairRunning, invalidateBackupRepair, setBackupLoading, @@ -1209,8 +1215,18 @@ function* runBackupInspection( return false; backupInspectionOwners.set(id, (backupInspectionOwners.get(id) ?? 0) + 1); let finalPhase: RepairPhase = 'unverified'; + let verifiedEvidence: { revision: number; intent: BackupWriteIntent } | undefined; let inspection: Promise | undefined; let updates: ReturnType> | undefined; + const evidenceStillCurrent = () => { + const currentIntent = captureBackupWriteIntent(id); + return !!verifiedEvidence && !!currentIntent && + backupRevision(id) === verifiedEvidence.revision && + currentIntent.appId === verifiedEvidence.intent.appId && + currentIntent.accountSession === verifiedEvidence.intent.accountSession && + currentIntent.baseRevision === verifiedEvidence.intent.baseRevision && + currentIntent.baselineEpoch === verifiedEvidence.intent.baselineEpoch; + }; try { yield put(setBackupRepairRunning({ appId: id, running: true })); updates = eventChannel((emit) => { @@ -1222,7 +1238,13 @@ function* runBackupInspection( explicitlyChosen: boolean, session: number ) => Promise; - inspection = inspectWithSession(id, repair, (phase) => emit(phase), explicitChoice, requestSession); + inspection = inspectWithSession(id, repair, (phase) => { + if (phase === 'verified') { + const intent = captureBackupWriteIntent(id); + verifiedEvidence = intent ? { revision: backupRevision(id), intent } : undefined; + } + emit(phase); + }, explicitChoice, requestSession); inspection.then( () => emit(END), () => emit(END) @@ -1234,13 +1256,25 @@ function* runBackupInspection( if (phase === END) break; if (requestSession !== currentBackupAccountSession()) return false; finalPhase = phase; - yield put(setBackupRepairState({ appId: id, phase })); + // Verification is tentative until the final, synchronous Redux commit. + // A Realm write can invalidate it while this channel is draining. + if (phase !== 'verified') yield put(setBackupRepairState({ appId: id, phase })); } - const current: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); - const ownerIsActive = yield select((state: RootState) => state.storage.appId === id); - if (current?.id === id && ownerIsActive && requestSession === currentBackupAccountSession()) { - yield put(setPendingAllBackup({ appId: id, pending: finalPhase !== 'verified' })); - if (finalPhase === 'verified') { + // No yield may separate the current-state check from this one Redux action: + // it commits the visible phase and pending flag together. + const outcome: { active: boolean; verified: boolean } = yield call(() => { + if (currentBackupAppId() !== id || requestSession !== currentBackupAccountSession()) + return { active: false, verified: false }; + const verified = finalPhase === 'verified' && evidenceStillCurrent(); + store.dispatch(commitBackupInspection({ + appId: id, + phase: finalPhase === 'verified' && !verified ? 'unverified' : finalPhase, + pending: !verified, + })); + return { active: true, verified }; + }); + if (outcome.active) { + if (outcome.verified) { const notifications = dbManager.getObjectByField( RealmSchema.UAI, uaiType.SERVER_BACKUP_FAILURE, @@ -1249,7 +1283,7 @@ function* runBackupInspection( for (const uai of notifications) { const stillActive = yield select((state: RootState) => state.storage.appId === id); if (!stillActive || currentBackupAppId() !== id || - requestSession !== currentBackupAccountSession()) break; + requestSession !== currentBackupAccountSession() || !evidenceStillCurrent()) break; yield call(uaiActionedWorker, { payload: { uaiId: uai.id, action: true } }); } } else if ( @@ -1270,7 +1304,8 @@ function* runBackupInspection( }); } } - return requestSession === currentBackupAccountSession() && finalPhase === 'verified'; + return outcome.verified && requestSession === currentBackupAccountSession() && + evidenceStillCurrent(); } finally { updates?.close(); // Cancellation closes this listener, but does not stop its shared operation. diff --git a/tests/recovery/sagaState.test.cjs b/tests/recovery/sagaState.test.cjs index 7184479c5b..3672da45c3 100644 --- a/tests/recovery/sagaState.test.cjs +++ b/tests/recovery/sagaState.test.cjs @@ -12,7 +12,8 @@ const bhr = loadModule('src/store/reducers/bhr.ts', { 'src/storage': { reduxStorage:{} }, 'redux-persist': { persistReducer: (_config,reducer) => reducer }, }); -function inspectionRunner(f) { +function inspectionRunner(f, afterVerified) { + f.transport.observeBackupAccount(f.app.id); const filename = 'src/store/sagas/bhr.ts'; const source = ts.createSourceFile(filename,fs.readFileSync(path.join(__dirname,'../..',filename),'utf8'),ts.ScriptTarget.Latest,true); const code = source.statements.filter(n => @@ -20,22 +21,30 @@ function inspectionRunner(f) { (ts.isVariableStatement(n) && n.declarationList.declarations.some(d => d.name?.text === 'backupInspectionOwners')) ).map(n => n.getText(source)).join('\n'); const operations = []; - const scope = { ...saga, ...effects, ...bhr, inspectBackup:(...args) => { - const operation = f.repair.inspectBackup(...args); + let state = bhr.default(undefined,{type:'init'}); const actions = []; + const getState = () => ({ bhr:state, storage:{appId:f.app.id} }); + const dispatch = action => { actions.push(action); state=bhr.default(state,action); }; + const scope = { ...saga, ...effects, ...bhr, inspectBackup:(appId, repair, onPhase, ...rest) => { + const operation = f.repair.inspectBackup(appId, repair, (phase) => { + onPhase(phase); + if (phase === 'verified') afterVerified?.({ f, dispatch }); + }, ...rest); operations.push(operation); return operation; }, RealmSchema:{KeeperApp:'KeeperApp',UAI:'UAI'}, dbManager:{ getObjectByIndex:()=>f.app, getObjectByField:()=>[] }, currentBackupAppId:()=>f.app.id, - currentBackupAccountSession:()=>0, + currentBackupAccountSession:()=>f.transport.currentBackupAccountSession(), + captureBackupWriteIntent:(id)=>f.transport.captureBackupWriteIntent(id), + backupRevision:(id)=>f.transport.backupRevision(id), + store:{ getState, dispatch }, uaiType:{SERVER_BACKUP_FAILURE:'failure'}, uaiActionedWorker:()=>{}, addToUaiStackWorker:()=>{}, }; vm.runInNewContext(ts.transpileModule(code,{ compilerOptions:{ target:ts.ScriptTarget.ES2020 }}).outputText,scope); - let state = bhr.default(undefined,{type:'init'}); const actions = []; return { start(repair) { - const sagaTask = saga.runSaga({getState:()=>({bhr:state,storage:{appId:f.app.id}}),dispatch:action=>{ actions.push(action); state=bhr.default(state,action); }},scope.runBackupInspection,repair); + const sagaTask = saga.runSaga({getState,dispatch},scope.runBackupInspection,repair); return {task:sagaTask.toPromise(),sagaTask,settled:()=>Promise.all(operations),get state(){ return state; },actions}; }, }; @@ -116,8 +125,11 @@ test('only verified readback sets durable per-account completion and clears pend assert.equal(run.state.backupRepairStateByAppId.disposable,'verified'); assert.equal(run.state.backupRepairRunningByAppId.disposable,false); assert.equal(run.state.pendingAllBackupByAppId.disposable,false); - const phases=run.actions.filter(a=>a.type==='bhr/setBackupRepairState').map(a=>a.payload.phase); + const phases=run.actions.filter(a=> + a.type==='bhr/setBackupRepairState' || a.type==='bhr/commitBackupInspection' + ).map(a=>a.payload.phase); assert.deepEqual(phases,['checking','preparing','uploading','verifying','verified']); + assert.equal(run.actions.filter(a=>a.type==='bhr/commitBackupInspection').length,1); }); test('offline/unverified remains pending and retryable, never spins indefinitely', async () => { @@ -142,3 +154,19 @@ test('a later mutation invalidates verified status without affecting a second ac assert.equal(state.backupRepairCompletedByAppId.one,false); assert.equal(state.backupRepairStateByAppId.one,'unverified'); assert.equal(state.backupRepairCompletedByAppId.two,true); }); + +test('a local write after verified event cannot clear pending while the saga drains', async () => { + const f = harness(); + const run = inspectionRunner(f, ({ f, dispatch }) => { + f.transport.markBackupMutation(f.app.id); + f.transport.clearVerifiedRemoteRevision(f.app.id); + dispatch(bhr.invalidateBackupRepair(f.app.id)); + dispatch(bhr.setPendingAllBackup({ appId: f.app.id, pending: true })); + }).start(false); + assert.equal(await run.task, false); + assert.equal(run.state.pendingAllBackupByAppId.disposable, true); + assert.equal(run.state.backupRepairCompletedByAppId.disposable, false); + assert.equal(run.state.backupRepairStateByAppId.disposable, 'unverified'); + assert.equal(run.actions.some(a=>a.type==='bhr/commitBackupInspection' && + a.payload.phase==='verified'), false); +}); From b4d5847f70382ed31a938d2ccce5336d251e627d Mon Sep 17 00:00:00 2001 From: Anant Date: Mon, 12 Oct 2026 21:56:38 +0530 Subject: [PATCH 139/254] Keep saved USDT receive addresses in backup comparison --- src/services/backup/image.ts | 11 +++- .../localMutationInvalidation.test.cjs | 50 +++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/src/services/backup/image.ts b/src/services/backup/image.ts index cae238c219..8b88120ec3 100644 --- a/src/services/backup/image.ts +++ b/src/services/backup/image.ts @@ -37,7 +37,16 @@ export function recoveryContent(kind: keyof BackupImage, record: any) { // archive state and user metadata in the comparison. const result = { ...record }; delete result.receivingAddress; - delete result.accountStatus; + if (record.entityKind === 'USDT_WALLET') { + // The retired GasFree provider cannot recreate these addresses. Keep them + // in recovery comparison while ignoring refreshable nonce/fee status. + if (record.accountStatus) { + result.accountStatus = { + address: record.accountStatus.address, + gasFreeAddress: record.accountStatus.gasFreeAddress, + }; + } + } else delete result.accountStatus; const specs = { ...record.specs }; // Manual coin-control choices cannot be reconstructed from the chain. Compare // them by outpoint, independent of confirmation state or UTXO/cache ordering. diff --git a/tests/recovery/localMutationInvalidation.test.cjs b/tests/recovery/localMutationInvalidation.test.cjs index 40167942ee..18c7b9ab9c 100644 --- a/tests/recovery/localMutationInvalidation.test.cjs +++ b/tests/recovery/localMutationInvalidation.test.cjs @@ -113,3 +113,53 @@ test('a receive counter mutation at the final verification boundary cannot repor image.compareImages = original; } }); + +test('saved USDT receive addresses invalidate recovery trust while refresh status does not', () => { + const stored = realmRecord({ + ...wallet('usdt'), entityKind: 'USDT_WALLET', + accountStatus: { + address: 'disposable-wallet-address', gasFreeAddress: 'disposable-gasfree-address', + nextNonce: 0, isActive: false, fees: { transferFee: 1, activateFee: 2 }, + }, + }); + const f = fixture({ USDTWallet: [stored] }); + let invalidations = 0; + f.manager.setRecoveryMutationObserver(() => invalidations++); + + assert.equal(f.manager.updateObjectById(f.RealmSchema.USDTWallet, stored.id, { + accountStatus: { ...stored.accountStatus, nextNonce: 1, fees: { transferFee: 3, activateFee: 2 } }, + }), true); + assert.equal(invalidations, 0); + assert.equal(f.manager.updateObjectById(f.RealmSchema.USDTWallet, stored.id, { + accountStatus: { ...stored.accountStatus, gasFreeAddress: 'different-saved-address' }, + }), true); + assert.equal(invalidations, 1); + assert.equal(f.manager.updateObjectById(f.RealmSchema.USDTWallet, stored.id, { + accountStatus: { ...stored.accountStatus, address: 'different-wallet-address' }, + }), true); + assert.equal(invalidations, 2); +}); + +for (const accountStatus of [ + undefined, + { address: 'disposable-wallet-address', gasFreeAddress: 'different-saved-address' }, +]) test(`USDT remote account status ${accountStatus ? 'different' : 'missing'} is never verified`, async () => { + const f = harness(); + const localWallet = { + ...wallet('usdt'), entityKind: 'USDT_WALLET', + accountStatus: { + address: 'disposable-wallet-address', gasFreeAddress: 'disposable-gasfree-address', + nextNonce: 0, + }, + }; + f.local.USDTWallet.push(localWallet); + f.remote.wallets[localWallet.id] = encryption.encrypt( + encryption.generateEncryptionKey(f.app.primarySeed), + JSON.stringify({ ...localWallet, accountStatus }) + ); + + assert.equal(await f.run(false), 'different'); + assert.equal(await f.run(true), 'conflict'); + assert.deepEqual(f.calls, ['getBackupSnapshot', 'getBackupSnapshot']); + assert.equal(f.phases.includes('verified'), false); +}); From e3dc034732355049c13b087dafbfe510e7409340 Mon Sep 17 00:00:00 2001 From: Anant Date: Mon, 12 Oct 2026 21:59:28 +0530 Subject: [PATCH 140/254] Reject unusable USDT addresses before recovery writes --- src/services/backup/restore.ts | 19 +++++++++++++++++-- tests/recovery/restoreIntegrity.test.cjs | 23 +++++++++++++++++++++++ tests/recovery/usdtBackup.test.cjs | 4 ++-- 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/services/backup/restore.ts b/src/services/backup/restore.ts index 4b521b3cdb..0bc3c7039e 100644 --- a/src/services/backup/restore.ts +++ b/src/services/backup/restore.ts @@ -1,4 +1,5 @@ -import { decodeImage } from './image'; +import { TronWeb } from 'tronweb'; +import { BackupSnapshotValidationError, decodeImage } from './image'; // Decode every record before changing Realm. Older backups may omit collections // introduced later; an unreadable response or a missing referenced record is not @@ -14,7 +15,7 @@ export async function prepareRecoveryImage( // Older Relay registrations and deletions stored empty maps as empty arrays. // Keep those recoverable, but let decodeImage reject nonempty malformed arrays. const legacyEmptyMap = (value: any) => (Array.isArray(value) && !value.length ? {} : value); - return decodeImage( + const image = await decodeImage( encryptionKey, { appImage: { @@ -28,4 +29,18 @@ export async function prepareRecoveryImage( }, appId ); + // Legacy USDT wallets cannot recover these addresses from the retired + // provider. Reject an unusable image before the first Realm write. + for (const wallet of Object.values(image.wallets) as any[]) { + if (wallet.entityKind !== 'USDT_WALLET') continue; + const status = wallet.accountStatus; + if ( + typeof status?.address !== 'string' || + typeof status?.gasFreeAddress !== 'string' || + !TronWeb.isAddress(status.address) || + !TronWeb.isAddress(status.gasFreeAddress) + ) + throw new BackupSnapshotValidationError('Invalid USDT wallet backup address'); + } + return image; } diff --git a/tests/recovery/restoreIntegrity.test.cjs b/tests/recovery/restoreIntegrity.test.cjs index 8ff08e3c1e..594e05aa5e 100644 --- a/tests/recovery/restoreIntegrity.test.cjs +++ b/tests/recovery/restoreIntegrity.test.cjs @@ -34,6 +34,10 @@ function recoveryFixture() { ...wallet('usdt', enums.NetworkType.MAINNET), entityKind: enums.EntityKind.USDT_WALLET, specs: { address: 'fixture', privateKey: 'fixture-only' }, + accountStatus: { + address: 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t', + gasFreeAddress: 'TXYZopYRdj2D9XRtbG411XZZ3kM5VkAeBf', + }, }; f.remote.wallets = { btc: seal(btc), usdt: seal(usdt) }; f.remote.signers = { [f.scope.getKeyUID(mobile)]: seal(mobile) }; @@ -105,6 +109,25 @@ test('clean recovery recreates keys, BTC/USDT, archived vault, labels and custom assert.deepEqual(plain(f.collections.NodeConnect), [{ ...f.expected.node, isConnected: true }]); }); +for (const [name, accountStatus] of [ + ['missing status', undefined], + ['missing wallet address', { gasFreeAddress: 'TXYZopYRdj2D9XRtbG411XZZ3kM5VkAeBf' }], + ['empty GasFree address', { address: 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t', gasFreeAddress: '' }], + ['malformed wallet address', { address: 'not-a-tron-address', gasFreeAddress: 'TXYZopYRdj2D9XRtbG411XZZ3kM5VkAeBf' }], + ['malformed GasFree checksum', { address: 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t', gasFreeAddress: 'TXYZopYRdj2D9XRtbG411XZZ3kM5VkAeBg' }], +]) test(`clean recovery rejects USDT ${name} before any Realm write`, async () => { + const f = recoveryFixture(); + const key = encryption.generateEncryptionKey(f.app.primarySeed); + f.remote.wallets.usdt = encryption.encrypt(key, JSON.stringify({ + ...f.expected.usdt, accountStatus, + })); + await f.restore(); + assert.equal(errors(f).length, 1); + assert.equal(completed(f), false); + assert.deepEqual(f.writes, []); + assert.equal(f.calls.length, 0); +}); + for (const kind of ['wallet', 'usdt', 'signer', 'vault', 'label', 'node']) { test(`corrupt ${kind} prevents every recovery write and success`, async () => { const f = recoveryFixture(); diff --git a/tests/recovery/usdtBackup.test.cjs b/tests/recovery/usdtBackup.test.cjs index 17a9980d38..be56087201 100644 --- a/tests/recovery/usdtBackup.test.cjs +++ b/tests/recovery/usdtBackup.test.cjs @@ -105,8 +105,8 @@ const usdtWallet = (type = 'DEFAULT') => ({ specs: { address: 'disposable-tron-address', privateKey: 'fixture-only-private-key', balance: 10, transactions: [], hasNewUpdates: false, lastSynched: 1 }, accountStatus: { - address: 'disposable-tron-address', - gasFreeAddress: 'saved-fixture-receive-address', + address: 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t', + gasFreeAddress: 'TXYZopYRdj2D9XRtbG411XZZ3kM5VkAeBf', isActive: true, frozen: 0, canTransfer: true, From 0550ac9ffa938a079816ae4ed58bc970324c3e08 Mon Sep 17 00:00:00 2001 From: Anant Date: Mon, 12 Oct 2026 22:11:36 +0530 Subject: [PATCH 141/254] Include account identity on v2 vault creation payload --- src/store/sagas/bhr.ts | 3 +++ tests/recovery/accountBinding.test.cjs | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/store/sagas/bhr.ts b/src/store/sagas/bhr.ts index a6be561c57..ebddc2947c 100644 --- a/src/store/sagas/bhr.ts +++ b/src/store/sagas/bhr.ts @@ -300,6 +300,9 @@ export function* updateVaultImageWorker({ try { const response = yield call(Relay.updateVaultImage, { + // The v2 Relay route requires appId even when the legacy appID alias is + // retained for older request builders. Both must identify this account. + appId: id, appID: id, vaultId: vault.id, signersData, diff --git a/tests/recovery/accountBinding.test.cjs b/tests/recovery/accountBinding.test.cjs index 8c434be668..decc85b670 100644 --- a/tests/recovery/accountBinding.test.cjs +++ b/tests/recovery/accountBinding.test.cjs @@ -12,6 +12,24 @@ function switchToOptedInB(f) { f.state.bhr.automaticCloudBackupByAppId.B = true; } +test('vault creation names the same account for the revisioned Relay contract', async () => { + const f = fixture(); + let sent; + f.scope.Relay.updateVaultImage = async (body) => { + sent = body; + return { updated: true }; + }; + const result = await f.run('updateVaultImageWorker', { + originAppId: f.app.id, + originBackupAccountSession: 0, + payload: { vault: { id: 'disposable-vault', signers: [], archived: false } }, + }); + assert.equal(result.updated, true); + assert.equal(sent.appId, f.app.id); + assert.equal(sent.appID, f.app.id); + assert.equal(sent.vaultId, 'disposable-vault'); +}); + test('A wallet and vault actions queued before B login never use B key or destination', async () => { const f = fixture(); const originAppId = f.app.id; From 6300fb9779711342e4eca03f3539949afb8ac3a3 Mon Sep 17 00:00:00 2001 From: Anant Date: Mon, 12 Oct 2026 22:23:06 +0530 Subject: [PATCH 142/254] Target exact signer map keys during fingerprint migration --- src/store/sagas/wallets.ts | 6 ++- tests/recovery/fingerprintTransport.test.cjs | 40 ++++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/store/sagas/wallets.ts b/src/store/sagas/wallets.ts index 76fa1da571..c3372488b2 100644 --- a/src/store/sagas/wallets.ts +++ b/src/store/sagas/wallets.ts @@ -2075,8 +2075,10 @@ function* mergeSimilarKeysWorker({ ); const updated = yield call(Relay.migrateXfp, id, [ { - oldSignerId: s.masterFingerprint, - newSignerId: signer.masterFingerprint, + // Relay signer maps use KeyUID, not the bare fingerprint. Keep the + // old key captured before Realm mutates the live signer object. + oldSignerId: signerKeyUID, + newSignerId: getKeyUID(signer), newSignerDetails: encrytedSigner, }, ], originBackupWriteIntent); diff --git a/tests/recovery/fingerprintTransport.test.cjs b/tests/recovery/fingerprintTransport.test.cjs index fda687e503..f8622c69ee 100644 --- a/tests/recovery/fingerprintTransport.test.cjs +++ b/tests/recovery/fingerprintTransport.test.cjs @@ -1,6 +1,7 @@ const { test } = require('node:test'); const assert = require('node:assert/strict'); const { loadModule, harness, wallet } = require('./helpers.cjs'); +const { fixture: sagaFixture, signer: sagaSigner, enums } = require('./sagaHarness.cjs'); const signerChanges = [{ oldSignerId: 'fixture-old', newSignerId: 'fixture-new', newSignerDetails: 'fixture-ciphertext', @@ -73,3 +74,42 @@ test('direct migration attempts never send ciphertext and keep redacted errors', assert.equal(transport.backupRevision('fixture-account'), 2); assert.deepEqual(pending, ['fixture-account', 'fixture-account']); }); + +test('fingerprint migration targets the actual KeyUID map entry after Realm changes the live signer', async () => { + const f = sagaFixture(); + const oldSigner = sagaSigner('AABBCCDD', enums.NetworkType.MAINNET); + oldSigner.signerXpubs[enums.XpubTypes.P2WSH] = [{ + xpub: 'shared-p2wsh-xpub', derivationPath: "m/48'/0'/0'/2'", xpriv: null, + }]; + const replacement = { + ...oldSigner, + masterFingerprint: 'EEFF0011', + signerXpubs: structuredClone(oldSigner.signerXpubs), + }; + const oldKey = f.scope.getKeyUID(oldSigner); + const newKey = f.scope.getKeyUID(replacement); + assert.notEqual(oldKey, oldSigner.masterFingerprint); + assert.notEqual(newKey, replacement.masterFingerprint); + f.collections.Signer.push(oldSigner); + f.remote.signers[oldKey] = 'old-ciphertext'; + f.scope.dbManager.getObjectByQuery = () => []; + let submitted; + f.scope.Relay.migrateXfp = async (appId, changes) => { + submitted = { appId, changes }; + for (const change of changes) { + delete f.remote.signers[change.oldSignerId]; + f.remote.signers[change.newSignerId] = change.newSignerDetails; + } + return { updated: true }; + }; + + assert.equal(await f.run('mergeSimilarKeysWorker', { + originAppId: f.app.id, + payload: { signer: replacement }, + }), true); + assert.equal(oldSigner.masterFingerprint, replacement.masterFingerprint); + assert.equal(submitted.appId, f.app.id); + assert.equal(submitted.changes[0].oldSignerId, oldKey); + assert.equal(submitted.changes[0].newSignerId, newKey); + assert.deepEqual(Object.keys(f.remote.signers), [newKey]); +}); From 1c4b5ff9c639e3e34b35143984ee02a07dac3462 Mon Sep 17 00:00:00 2001 From: Anant Date: Mon, 12 Oct 2026 22:23:11 +0530 Subject: [PATCH 143/254] Document automatic backup write proof gates --- .../automatic-write-readiness.md | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 openspec/changes/fix-recovery-backup-freshness/automatic-write-readiness.md diff --git a/openspec/changes/fix-recovery-backup-freshness/automatic-write-readiness.md b/openspec/changes/fix-recovery-backup-freshness/automatic-write-readiness.md new file mode 100644 index 0000000000..ca39a8c404 --- /dev/null +++ b/openspec/changes/fix-recovery-backup-freshness/automatic-write-readiness.md @@ -0,0 +1,39 @@ +# Automatic Assisted Backup writes: implementation gates + +Scope reviewed: Keeper `0550ac9ffa938a079816ae4ed58bc970324c3e08` and Relay PR #4 at `82ee3b10708a56d220170505d1bb7e3bc8429e77`. This is an implementation plan, not permission to enable the currently disabled seven incremental writes. No release or Relay deployment is implied. + +## Why the current intent cannot authorize a write + +`BackupWriteIntent` records the selected account session and a previously verified Relay revision. It does not identify the local recovery state represented by an encrypted payload or deletion. `backupPost` therefore rejects all seven operations before network. This is required: some producers encrypt action data before Realm persistence, Realm may change while an action waits, and one action can emit several Relay writes with the same captured base revision. A valid server compare-and-swap protects against another server writer; it does not prove that the client is sending its present local recovery data. + +The central Realm observer invalidates backup verification **before** each relevant write. It has no post-commit token and can fire for several field writes in one logical action. The transport's `queuedMutationRevisions` is currently never populated, so it cannot provide a queue completion proof. A pre-send comparison of a prepared ciphertext against the current Realm image is necessary but still insufficient for operations deliberately sent before local persistence. + +## Required transaction model + +1. Give each logical local recovery operation an account-scoped operation ID and a commit result. A successful commit captures a monotonic recovery generation after all related Realm writes; failed or partial writes stay pending and never authorize upload. For pre-persistence producers, stage data locally and queue the cloud mutation only after this commit. Do not make server success a prerequisite for saving a new wallet, signer, vault or deletion. +2. Capture the originating `appId`, account session, verified base revision/epoch and operation ID before async work. Never substitute a newly verified base for an older action. On A→B→A, reject old queued work even if A is selected again. An already sent request has an uncertain outcome and requires readback. +3. At the head of the per-account queue, re-read the fresh local recovery image and prove the proposed operation's decrypted payload or deletion target against that image and its committed generation. Verify the selected account, backup consent, encryption key ownership, and the originally captured session immediately before sending. An operation-specific failed proof ends in pending/unverified without network. +4. Read or retain a validated v2 Relay snapshot and its 64-character revision. No legacy endpoint fallback. Apply each proposed operation to the remote snapshot in memory and reject an effect that removes remote-only recovery records or changes key identity without an explicit user decision. The Relay must atomically enforce `expectedRevision` and return `updated: true`, `preconditionApplied: true` and a valid `newRevision`. +5. A multi-record logical operation should use one Relay transaction carrying all its effects. If the existing v2 route is used sequentially, each step must validate its own current local proof and use the preceding acknowledged `newRevision`. Partial success remains pending. Never call the whole backup verified on an intermediate response. +6. After the last acknowledged write, read back and decrypt the complete Relay image. Only a match with a newly captured local image at the same committed generation may set verified. A local write during comparison invalidates the claim. The existing atomic Redux verification commit remains the final UI gate. +7. On 409, old Relay/404, malformed acknowledgement, timeout, lost response, network error, failed local commit, changed session or changed local generation, stop the chain. Do not retry prepared ciphertext or send later fanout members. Keep pending and run a read-only check. A lost response may have committed on the server; its result must be learned from readback before any fresh write. + +## Operation-specific proof at the queue head + +| Relay operation | Local proof before send | Current producer complication | +| --- | --- | --- | +| `updateAppImage` | Decrypt each wallet/USDT/signer ciphertext with the active account seed; map IDs, sanitized signer records and recoverable fields must equal the committed Realm records. `replaceNodes` requires the exact current normalized node set; app metadata must come from the current KeeperApp. | New wallet and signer creation call backup before Realm insertion. Node and version paths use different timing. Action payloads may be stale. | +| `migrateXfps` | Old exact KeyUID must be absent in committed Realm; new exact KeyUID and decrypted sanitized signer must match Realm. Verify all affected vault signer references, and prove the old map entry exists in the validated Relay snapshot. | The worker updates a live signer before building the request. It previously sent bare fingerprints while Relay maps use KeyUIDs; a narrow key fix is included separately, with transport still disabled. | +| `deleteAppImageEntity` | Every wallet/USDT/signer ID must be absent after a successful local commit and present in the expected Relay snapshot. Deleting a signer must not make a retained vault unrecoverable. | BTC wallet and signer deletion currently calls Relay before Realm deletion; USDT deletion also does so directly. Moving this order requires durable pending state and truthful result/UI handling. | +| `deleteVaults` | Each ID must be absent after local commit; other participant/shared-vault references must remain owned and recoverable. | Vault deletion also calls Relay before local deletion. A remote success followed by local failure can leave a mismatched backup. | +| `updateVaultImage` | Decrypt the vault; its ID, sanitized signers, policy, archive state and `signersData` must match the committed Realm Vault plus persisted VaultSigner registrations. | Creation and metadata edit can call Relay before persistence. Registration changes can fan out to several vault writes; migration creates and archives two vaults. | +| `modifyLabels` | Decrypt each added label and verify the account-derived hashed ID and current Realm tag. Every deleted hashed ID must map to a tag absent after local commit. Reconcile add/delete overlap and account ownership. | Imports, upgrades and transaction-label workers can emit multi-label fanouts; a delayed file callback carries older intent. | +| `deleteBackup` | Require an explicit opt-out action for the same selected account and session, a fresh v2 revision, and exact whole-account target. Preserve consent until a positive v2 acknowledgement; on lost response use readback without claiming success. | This is a destructive exception to the local-image comparison: the user intentionally removes the remote copy while keeping Realm. It needs its own opt-out state and separate tests. | + +## Test matrix before any enablement + +Run each of the seven operations against disposable Realm and transactional Relay fixtures, with real encryption/decryption and the production saga/transport entry points. For each operation, verify: matching committed payload/target reaches the v2 route once; stale payload after a local mutation sends nothing; A→B→A sends nothing; a server 409/old route 404/malformed response leaves pending and does not fall back; timeout or lost acknowledgement sends no next item and readback cannot create false verified. Exercise both matching and remote-only/conflicting snapshots. + +Add focused multi-write fixtures for (a) creating and archiving a vault, (b) one VaultSigner registration affecting several vaults, and (c) label import plus deletion. Make the first Relay write succeed and the next fail; assert local data survives, the remaining prepared writes do not run, and neither a saga nor Redux reports verified. Assert a local mutation injected just before the final verification commit leaves the backup pending. Pair these tests with native iOS/Android create, edit, delete, relaunch and clean Recovery Key restore on the exact app SHA, plus Relay v2 HTTP tests against a disposable replica set. + +Keep automatic transport disabled until this matrix passes and all old Relay instances and legacy full writers are drained. Production transaction topology and the actual Relay deployment are separate release evidence. A future source change must never turn an endpoint name or revision string alone into a freshness proof. From bb662a25b53c585fb39aaf5c7a8e6a9ee7862e6d Mon Sep 17 00:00:00 2001 From: Anant Date: Mon, 12 Oct 2026 22:25:26 +0530 Subject: [PATCH 144/254] Report wallet and vault deletion only after local removal --- src/hooks/useUSDTWallets.ts | 3 +- src/screens/AppSettings/ManageWallets.tsx | 60 ++++-- src/store/sagaActions/bhr.ts | 3 + src/store/sagas/bhr.ts | 34 +++- src/store/sagas/wallets.ts | 5 +- .../deleteResultTruthfulness.test.cjs | 184 ++++++++++++++++++ tests/recovery/sagaHarness.cjs | 3 + tests/recovery/usdtAccountDelete.test.cjs | 17 +- 8 files changed, 290 insertions(+), 19 deletions(-) create mode 100644 tests/recovery/deleteResultTruthfulness.test.cjs diff --git a/src/hooks/useUSDTWallets.ts b/src/hooks/useUSDTWallets.ts index 3fc6481b8d..89f867f2be 100644 --- a/src/hooks/useUSDTWallets.ts +++ b/src/hooks/useUSDTWallets.ts @@ -226,7 +226,8 @@ export const useUSDTWallets = (options: UseUSDTWalletsOptions = {}): UseUSDTWall markIncompleteDeletion(); return false; } - const deleted = dbManager.deleteObjectById(RealmSchema.USDTWallet, walletId) === true; + const deleted = dbManager.deleteObjectById(RealmSchema.USDTWallet, walletId) === true && + !dbManager.getObjectById(RealmSchema.USDTWallet, walletId); if (!deleted) markIncompleteDeletion(); return deleted; } catch (err) { diff --git a/src/screens/AppSettings/ManageWallets.tsx b/src/screens/AppSettings/ManageWallets.tsx index aa67be5207..c87848ef84 100644 --- a/src/screens/AppSettings/ManageWallets.tsx +++ b/src/screens/AppSettings/ManageWallets.tsx @@ -51,11 +51,16 @@ import MiniscriptPathSelector, { import WalletHeader from 'src/components/WalletHeader'; import ThemedColor from 'src/components/ThemedColor/ThemedColor'; import USDTGreenLogo from 'src/assets/images/usdt-gree-logo.svg'; +import { currentBackupAccountSession } from 'src/services/backup/transport'; enum PasswordMode { DEFAULT = 'DEFAULT', SHOWALL = 'SHOWALL', } + +const activeRealmAppId = (): string | undefined => + (dbManager.getObjectByIndex(RealmSchema.KeeperApp) as unknown as { id: string } | undefined)?.id; + function ListItem({ title, subtitle, @@ -172,17 +177,21 @@ function ManageWallets() { const [selectedWallet, setSelectedWallet] = useState(null); const { showToast } = useToastMessage(); const miniscriptPathSelectorRef = useRef(null); + const pendingVaultDeleteRef = useRef<{ id: string; appId?: string; session: number } | null>(null); useEffect(() => { - if (selectedWallet?.id) { - if (relayVaultUpdate) { - showToast(errorText.vaultDeleted, ); - } - if (relayVaultError) { - showToast(errorText.failedToDeleteVault, ); - } - dispatch(resetRealyVaultState()); + const pending = pendingVaultDeleteRef.current; + if (!pending || (!relayVaultUpdate && !relayVaultError)) return; + pendingVaultDeleteRef.current = null; + if ( + currentBackupAccountSession() === pending.session && + activeRealmAppId() === pending.appId + ) { + const deleted = !dbManager.getObjectById(RealmSchema.Vault, pending.id); + if (relayVaultUpdate && deleted) showToast(errorText.vaultDeleted, ); + else showToast(errorText.failedToDeleteVault, ); } + dispatch(resetRealyVaultState()); }, [relayVaultUpdate, relayVaultError]); const isWallet = selectedWallet?.entityKind === EntityKind.WALLET; @@ -208,15 +217,42 @@ function ManageWallets() { const deleteSelectedEntity = async () => { if (selectedWallet && selectedWallet.entityKind === EntityKind.VAULT) { + pendingVaultDeleteRef.current = { + id: selectedWallet.id, + appId: activeRealmAppId(), + session: currentBackupAccountSession(), + }; + dispatch(resetRealyVaultState()); dispatch(deleteVault(selectedWallet.id)); } if (selectedWallet && selectedWallet.entityKind === EntityKind.WALLET) { - dispatch(deleteAppImageEntity({ walletIds: [selectedWallet.id] })); - showToast(errorText.waletDeleted, ); + const walletId = selectedWallet.id; + const appId = activeRealmAppId(); + const session = currentBackupAccountSession(); + dispatch(deleteAppImageEntity({ + walletIds: [walletId], + onComplete: (deleted) => { + if ( + currentBackupAccountSession() !== session || + activeRealmAppId() !== appId + ) return; + if (deleted && !dbManager.getObjectById(RealmSchema.Wallet, walletId)) + showToast(errorText.waletDeleted, ); + else showToast('Failed to delete wallet', ); + }, + })); } if (selectedWallet && selectedWallet.entityKind === EntityKind.USDT_WALLET) { - const success = await deleteUSDTWallet(selectedWallet.id); - if (success) showToast(errorText.waletDeleted, ); + const walletId = selectedWallet.id; + const appId = activeRealmAppId(); + const session = currentBackupAccountSession(); + const success = await deleteUSDTWallet(walletId); + if ( + currentBackupAccountSession() !== session || + activeRealmAppId() !== appId + ) return; + if (success && !dbManager.getObjectById(RealmSchema.USDTWallet, walletId)) + showToast(errorText.waletDeleted, ); else showToast('Failed to delete USDT wallet', ); } }; diff --git a/src/store/sagaActions/bhr.ts b/src/store/sagaActions/bhr.ts index 876d8f34d9..0800182638 100644 --- a/src/store/sagaActions/bhr.ts +++ b/src/store/sagaActions/bhr.ts @@ -28,15 +28,18 @@ export const updateAppImage = ({ wallets, signers, updateNodes }, originAppId?: export const deleteAppImageEntity = ({ walletIds, signerIds, + onComplete, }: { walletIds?: string[]; signerIds?: string[]; + onComplete?: (deleted: boolean) => void; }) => ({ type: DELETE_APP_IMAGE_ENTITY, payload: { walletIds, signerIds, }, + onComplete, }); export const updateVaultImage = (payload: { diff --git a/src/store/sagas/bhr.ts b/src/store/sagas/bhr.ts index ebddc2947c..92627903d6 100644 --- a/src/store/sagas/bhr.ts +++ b/src/store/sagas/bhr.ts @@ -387,11 +387,14 @@ export function* deleteAppImageEntityWorker({ return { updated: false, error: 'Backup account changed' }; } const deleted = yield call(dbManager.deleteObjectById, RealmSchema.Wallet, walletId); + const stillPresent = deleted === true + ? yield call(dbManager.getObjectById, RealmSchema.Wallet, walletId) + : true; if (!accountIsCurrent()) { if (remoteDeleteSucceeded) yield call(flagUncertainBackupOutcome, appId); return { updated: false, error: 'Backup account changed' }; } - if (deleted !== true) { + if (deleted !== true || stillPresent) { if (remoteDeleteSucceeded) { yield put(setPendingAllBackup({ appId, pending: true })); yield call(setServerBackupFailed, appId); @@ -412,11 +415,14 @@ export function* deleteAppImageEntityWorker({ 'id', signerId ); + const stillPresent = deleted === true + ? yield call(dbManager.getObjectByPrimaryId, RealmSchema.Signer, 'id', signerId) + : true; if (!accountIsCurrent()) { if (remoteDeleteSucceeded) yield call(flagUncertainBackupOutcome, appId); return { updated: false, error: 'Backup account changed' }; } - if (deleted !== true) { + if (deleted !== true || stillPresent) { if (remoteDeleteSucceeded) { yield put(setPendingAllBackup({ appId, pending: true })); yield call(setServerBackupFailed, appId); @@ -1199,10 +1205,32 @@ export const healthCheckSignerWatcher = createWatcher( ); export const deleteAppImageEntityWatcher = createWatcher( - deleteAppImageEntityWorker, + deleteAppImageEntityRequestWorker, DELETE_APP_IMAGE_ENTITY ); +function* deleteAppImageEntityRequestWorker( + action: Parameters[0] & { + onComplete?: (deleted: boolean) => void; + } +) { + let deleted = false; + try { + const result = yield call(deleteAppImageEntityWorker, action); + deleted = result?.updated === true; + } catch (error) { + captureError(error); + } + if (action.onComplete) { + try { + action.onComplete(deleted); + } catch (error) { + captureError(error); + } + } + return deleted; +} + const backupInspectionOwners = new Map(); function* runBackupInspection( diff --git a/src/store/sagas/wallets.ts b/src/store/sagas/wallets.ts index c3372488b2..8b59ae28e3 100644 --- a/src/store/sagas/wallets.ts +++ b/src/store/sagas/wallets.ts @@ -1849,7 +1849,10 @@ function* deleteVaultWorker({ payload, originAppId, originBackupAccountSession, if (response.updated) { assertWalletAccount(sourceAppId, requestSession); const deleted = yield call(dbManager.deleteObjectById, RealmSchema.Vault, vaultId); - if (deleted !== true) { + const stillPresent = deleted === true + ? yield call(dbManager.getObjectById, RealmSchema.Vault, vaultId) + : true; + if (deleted !== true || stillPresent) { if (backupEnabled) yield call(setServerBackupFailed, sourceAppId); throw new Error('Local vault deletion failed'); } diff --git a/tests/recovery/deleteResultTruthfulness.test.cjs b/tests/recovery/deleteResultTruthfulness.test.cjs new file mode 100644 index 0000000000..83e6b095ac --- /dev/null +++ b/tests/recovery/deleteResultTruthfulness.test.cjs @@ -0,0 +1,184 @@ +const assert = require('node:assert/strict'); +const { test } = require('node:test'); +const fs = require('node:fs'); +const path = require('node:path'); +const vm = require('node:vm'); +const ts = require('typescript'); +const { fixture, enums, wallet } = require('./sagaHarness.cjs'); + +test('BTC wallet delete completion is false when the relay rejects the delete', async () => { + const f = fixture(); + f.collections.Wallet.push(wallet('keep', enums.NetworkType.MAINNET)); + f.scope.Relay.deleteAppImageEntity = async () => ({ updated: false, error: 'rejected' }); + const completed = []; + + const result = await f.run('deleteAppImageEntityRequestWorker', { + originAppId: f.app.id, + payload: { walletIds: ['keep'] }, + onComplete: (deleted) => completed.push(deleted), + }); + + assert.equal(result, false); + assert.deepEqual(completed, [false]); + assert.deepEqual(f.collections.Wallet.map((row) => row.id), ['keep']); +}); + +test('BTC wallet delete completion is true only after the Realm object is gone', async () => { + const f = fixture(); + f.collections.Wallet.push(wallet('remove', enums.NetworkType.MAINNET)); + const completed = []; + + const result = await f.run('deleteAppImageEntityRequestWorker', { + originAppId: f.app.id, + payload: { walletIds: ['remove'] }, + onComplete: (deleted) => completed.push(deleted), + }); + + assert.equal(result, true); + assert.deepEqual(completed, [true]); + assert.deepEqual(f.collections.Wallet, []); +}); + +test('BTC wallet delete completion is false if Realm reports success but retains the object', async () => { + const f = fixture(); + f.collections.Wallet.push(wallet('keep', enums.NetworkType.MAINNET)); + f.scope.dbManager.deleteObjectById = () => true; + const completed = []; + + const result = await f.run('deleteAppImageEntityRequestWorker', { + originAppId: f.app.id, + payload: { walletIds: ['keep'] }, + onComplete: (deleted) => completed.push(deleted), + }); + + assert.equal(result, false); + assert.deepEqual(completed, [false]); + assert.deepEqual(f.collections.Wallet.map((row) => row.id), ['keep']); + assert.equal(f.state.bhr.pendingAllBackupByAppId[f.app.id], true); +}); + +test('vault delete reports failure if Realm reports success but retains the object', async () => { + const f = fixture(); + f.collections.Vault.push({ id: 'keep' }); + f.scope.deleteVaultImageWorker = function* () { return { updated: true }; }; + f.scope.dbManager.deleteObjectById = () => true; + + await f.run('deleteVaultWorker', { + originAppId: f.app.id, + payload: { vaultId: 'keep' }, + }); + + assert.ok(f.actions.some((action) => action.type === 'relayVaultUpdateFail')); + assert.ok(!f.actions.some((action) => action.type === 'relayVaultUpdateSuccess')); + assert.deepEqual(f.collections.Vault.map((row) => row.id), ['keep']); +}); + +test('signer delete completion is false if Realm reports success but retains the signer', async () => { + const f = fixture(); + f.collections.Signer.push({ id: 'keep' }); + f.scope.dbManager.deleteObjectByPrimaryKey = () => true; + const completed = []; + + const result = await f.run('deleteAppImageEntityRequestWorker', { + originAppId: f.app.id, + payload: { signerIds: ['keep'], walletIds: [] }, + onComplete: (deleted) => completed.push(deleted), + }); + + assert.equal(result, false); + assert.deepEqual(completed, [false]); + assert.deepEqual(f.collections.Signer.map((row) => row.id), ['keep']); +}); + +const screenFile = path.join(__dirname, '../../src/screens/AppSettings/ManageWallets.tsx'); +const screen = ts.createSourceFile(screenFile, fs.readFileSync(screenFile, 'utf8'), + ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX); +let deleteDeclaration; +function findDelete(node) { + if (ts.isVariableDeclaration(node) && node.name.getText(screen) === 'deleteSelectedEntity') + deleteDeclaration = node; + ts.forEachChild(node, findDelete); +} +findDelete(screen); +assert.ok(deleteDeclaration, 'production ManageWallets delete handler must exist'); +const deleteHandlerCode = ts.transpileModule( + `const ${deleteDeclaration.getText(screen)}; globalThis.deleteSelectedEntity = deleteSelectedEntity;`, + { compilerOptions: { target: ts.ScriptTarget.ES2020, jsx: ts.JsxEmit.React } } +).outputText; + +function screenFixture(entityKind, usdtResult = false) { + const state = { active: 'A', session: 1, present: true, actions: [], toasts: [] }; + const scope = { + React: { createElement: (component) => component }, + TickIcon: 'tick', ToastErrorIcon: 'error', + EntityKind: { WALLET: 'WALLET', VAULT: 'VAULT', USDT_WALLET: 'USDT_WALLET' }, + RealmSchema: { KeeperApp: 'KeeperApp', Wallet: 'Wallet', Vault: 'Vault', USDTWallet: 'USDTWallet' }, + selectedWallet: { id: 'keep', entityKind }, + pendingVaultDeleteRef: { current: null }, + dbManager: { + getObjectByIndex: () => ({ id: state.active }), + getObjectById: () => state.present ? { id: 'keep' } : undefined, + }, + activeRealmAppId: () => state.active, + currentBackupAccountSession: () => state.session, + deleteAppImageEntity: (request) => ({ type: 'DELETE_BTC', ...request }), + deleteVault: (id) => ({ type: 'DELETE_VAULT', id }), + resetRealyVaultState: () => ({ type: 'RESET_VAULT' }), + deleteUSDTWallet: async () => usdtResult, + dispatch: (action) => state.actions.push(action), + showToast: (message, icon) => state.toasts.push({ message, icon }), + errorText: { + waletDeleted: 'Wallet deleted', + vaultDeleted: 'Vault deleted', + failedToDeleteVault: 'Failed to delete vault', + }, + }; + vm.runInNewContext(deleteHandlerCode, scope); + return { state, scope, deleteSelectedEntity: scope.deleteSelectedEntity }; +} + +test('ManageWallets waits for the BTC delete outcome before showing a toast', async () => { + const f = screenFixture('WALLET'); + await f.deleteSelectedEntity(); + assert.equal(f.state.actions.length, 1); + assert.deepEqual(f.state.toasts, []); + + f.state.actions[0].onComplete(false); + assert.equal(f.state.toasts[0].message, 'Failed to delete wallet'); +}); + +test('ManageWallets refuses a BTC success toast while the Realm wallet still exists', async () => { + const f = screenFixture('WALLET'); + await f.deleteSelectedEntity(); + f.state.actions[0].onComplete(true); + assert.equal(f.state.toasts[0].message, 'Failed to delete wallet'); + + const removed = screenFixture('WALLET'); + await removed.deleteSelectedEntity(); + removed.state.present = false; + removed.state.actions[0].onComplete(true); + assert.equal(removed.state.toasts[0].message, 'Wallet deleted'); +}); + +test('ManageWallets does not show an old account delete result after account switch', async () => { + const f = screenFixture('WALLET'); + await f.deleteSelectedEntity(); + f.state.active = 'B'; + f.state.session = 2; + f.state.actions[0].onComplete(false); + assert.deepEqual(f.state.toasts, []); +}); + +test('ManageWallets does not report USDT deletion success while Realm retains it', async () => { + const f = screenFixture('USDT_WALLET', true); + await f.deleteSelectedEntity(); + assert.equal(f.state.toasts[0].message, 'Failed to delete USDT wallet'); +}); + +test('ManageWallets leaves the vault delete result pending until the saga finishes', async () => { + const f = screenFixture('VAULT'); + await f.deleteSelectedEntity(); + assert.deepEqual(f.state.toasts, []); + assert.equal(f.scope.pendingVaultDeleteRef.current.id, 'keep'); + assert.deepEqual(f.state.actions.map((action) => action.type), ['RESET_VAULT', 'DELETE_VAULT']); +}); diff --git a/tests/recovery/sagaHarness.cjs b/tests/recovery/sagaHarness.cjs index 366e1a360c..dab506ccdd 100644 --- a/tests/recovery/sagaHarness.cjs +++ b/tests/recovery/sagaHarness.cjs @@ -153,6 +153,8 @@ function fixture(options = {}) { getObjectByIndex: (schema, _index, all) => schema === 'KeeperApp' ? app : all ? collections[schema] : collections[schema]?.[0], getObjectById: (schema, id) => collections[schema]?.find((row) => row.id === id), + getObjectByPrimaryId: (schema, key, value) => + collections[schema]?.find((row) => String(row[key]) === String(value)), getCollection: (schema) => collections[schema] || [], getObjectByField: (schema) => collections[schema] || [], createObject: (schema, value) => { @@ -294,6 +296,7 @@ function fixture(options = {}) { 'currentBackupAppId', 'flagUncertainBackupOutcome', 'deleteAppImageEntityWorker', + 'deleteAppImageEntityRequestWorker', 'deleteVaultImageWorker', 'deleteBackupWorker', 'backupAllSignersAndVaultsWorker', diff --git a/tests/recovery/usdtAccountDelete.test.cjs b/tests/recovery/usdtAccountDelete.test.cjs index 86ac544cae..f3e8218f98 100644 --- a/tests/recovery/usdtAccountDelete.test.cjs +++ b/tests/recovery/usdtAccountDelete.test.cjs @@ -28,7 +28,8 @@ function fixture({ consent = true, creationPaused = true, switchDuringRemote = f switchDuringRemoteRoundTrip = false, switchDuringGeneration = false, switchDuringGenerationRoundTrip = false, switchDuringUpdate = false, - switchDuringUpdateRoundTrip = false, switchDuringAccountSyncRoundTrip = false } = {}) { + switchDuringUpdateRoundTrip = false, switchDuringAccountSyncRoundTrip = false, + pretendLocalDelete = false } = {}) { const state = { active: 'A', session: 1, remote: [], local: [], errors: [], dispatched: [], invalidated: [] }; const scope = { appId: 'A', @@ -37,7 +38,10 @@ function fixture({ consent = true, creationPaused = true, switchDuringRemote = f RealmSchema: { KeeperApp: 'KeeperApp', USDTWallet: 'USDTWallet' }, dbManager: { getObjectByIndex: (schema) => schema === 'KeeperApp' ? { id: state.active } : [], - getObjectById: () => ({ id: 'wallet', name: 'old' }), + getObjectById: (_schema, walletId) => + state.local.some((operation) => operation.walletId === walletId) && !pretendLocalDelete + ? undefined + : { id: 'wallet', name: 'old' }, createObject: async (_schema, wallet) => { state.local.push({ appId: state.active, wallet }); return true; @@ -122,6 +126,15 @@ test('unconsented USDT wallet deletes locally without a relay write', async () = assert.equal(f.state.local[0].appId, 'A'); }); +test('USDT delete does not report success when Realm reports true but retains the wallet', async () => { + const f = fixture({ pretendLocalDelete: true }); + assert.equal(await f.deleteWallet('wallet'), false); + assert.equal(f.state.remote.length, 1); + assert.equal(f.state.local.length, 1); + assert.deepEqual(f.state.invalidated, ['A']); + assert.ok(f.state.dispatched.some((action) => action.type === 'SET_PENDING_ALL_BACKUP')); +}); + test('remote response after account switch cannot delete B local wallet', async () => { const f = fixture({ switchDuringRemote: true }); assert.equal(await f.deleteWallet('wallet'), false); From bc9c87d040006fcbd3cec37af10d5391341aea65 Mon Sep 17 00:00:00 2001 From: Anant Date: Mon, 12 Oct 2026 22:29:18 +0530 Subject: [PATCH 145/254] Scope vault delete feedback to each request --- src/screens/AppSettings/ManageWallets.tsx | 41 ++----- src/store/sagaActions/vaults.ts | 3 +- src/store/sagas/wallets.ts | 25 +++- .../deleteResultTruthfulness.test.cjs | 113 ++++++++++++++++-- tests/recovery/sagaHarness.cjs | 1 + 5 files changed, 145 insertions(+), 38 deletions(-) diff --git a/src/screens/AppSettings/ManageWallets.tsx b/src/screens/AppSettings/ManageWallets.tsx index c87848ef84..58429ca426 100644 --- a/src/screens/AppSettings/ManageWallets.tsx +++ b/src/screens/AppSettings/ManageWallets.tsx @@ -1,4 +1,4 @@ -import React, { useContext, useEffect, useState, useRef } from 'react'; +import React, { useContext, useState, useRef } from 'react'; import Text from 'src/components/KeeperText'; import { StyleSheet, FlatList, TouchableOpacity } from 'react-native'; import { Box, Pressable, useColorMode, HStack } from '@gluestack-ui/themed-native-base'; @@ -42,8 +42,6 @@ import HideAllIcon from 'src/assets/images/hide_wallet.svg'; import usePlan from 'src/hooks/usePlan'; import { deleteAppImageEntity } from 'src/store/sagaActions/bhr'; import { MANAGEWALLETS } from 'src/navigation/contants'; -import { resetRealyVaultState } from 'src/store/reducers/bhr'; -import { useAppSelector } from 'src/store/hooks'; import ToastErrorIcon from 'src/assets/images/toast_error.svg'; import MiniscriptPathSelector, { MiniscriptPathSelectorRef, @@ -169,30 +167,12 @@ function ManageWallets() { const [confirmPassVisible, setConfirmPassVisible] = useState(false); const [confirmPasscodeVisible, setConfirmPasscodeVisible] = useState(false); - const { relayVaultError, relayVaultUpdate } = useAppSelector((state) => state.bhr); - const { isOnL2Above } = usePlan(); const navigation: any = useNavigation(); const dispatch = useDispatch(); const [selectedWallet, setSelectedWallet] = useState(null); const { showToast } = useToastMessage(); const miniscriptPathSelectorRef = useRef(null); - const pendingVaultDeleteRef = useRef<{ id: string; appId?: string; session: number } | null>(null); - - useEffect(() => { - const pending = pendingVaultDeleteRef.current; - if (!pending || (!relayVaultUpdate && !relayVaultError)) return; - pendingVaultDeleteRef.current = null; - if ( - currentBackupAccountSession() === pending.session && - activeRealmAppId() === pending.appId - ) { - const deleted = !dbManager.getObjectById(RealmSchema.Vault, pending.id); - if (relayVaultUpdate && deleted) showToast(errorText.vaultDeleted, ); - else showToast(errorText.failedToDeleteVault, ); - } - dispatch(resetRealyVaultState()); - }, [relayVaultUpdate, relayVaultError]); const isWallet = selectedWallet?.entityKind === EntityKind.WALLET; const isUSDTWallet = selectedWallet?.entityKind === EntityKind.USDT_WALLET; @@ -217,13 +197,18 @@ function ManageWallets() { const deleteSelectedEntity = async () => { if (selectedWallet && selectedWallet.entityKind === EntityKind.VAULT) { - pendingVaultDeleteRef.current = { - id: selectedWallet.id, - appId: activeRealmAppId(), - session: currentBackupAccountSession(), - }; - dispatch(resetRealyVaultState()); - dispatch(deleteVault(selectedWallet.id)); + const vaultId = selectedWallet.id; + const appId = activeRealmAppId(); + const session = currentBackupAccountSession(); + dispatch(deleteVault(vaultId, (deleted) => { + if ( + currentBackupAccountSession() !== session || + activeRealmAppId() !== appId + ) return; + if (deleted && !dbManager.getObjectById(RealmSchema.Vault, vaultId)) + showToast(errorText.vaultDeleted, ); + else showToast(errorText.failedToDeleteVault, ); + })); } if (selectedWallet && selectedWallet.entityKind === EntityKind.WALLET) { const walletId = selectedWallet.id; diff --git a/src/store/sagaActions/vaults.ts b/src/store/sagaActions/vaults.ts index 68d626f47e..883c29e49e 100644 --- a/src/store/sagaActions/vaults.ts +++ b/src/store/sagaActions/vaults.ts @@ -41,9 +41,10 @@ export const migrateVault = (newVaultInfo: NewVaultInfo, oldVaultId: string) => payload: { newVaultInfo, oldVaultId }, }); -export const deleteVault = (payload: string) => ({ +export const deleteVault = (payload: string, onComplete?: (deleted: boolean) => void) => ({ type: DELETE_VAULT, payload: { vaultId: payload }, + onComplete, }); export const reinstateVault = (payload: string) => ({ diff --git a/src/store/sagas/wallets.ts b/src/store/sagas/wallets.ts index 8b59ae28e3..c875990688 100644 --- a/src/store/sagas/wallets.ts +++ b/src/store/sagas/wallets.ts @@ -1858,19 +1858,42 @@ function* deleteVaultWorker({ payload, originAppId, originBackupAccountSession, } assertWalletAccount(sourceAppId, requestSession); yield put(relayVaultUpdateSuccess()); + return true; } else { const errorMsg = response.error?.message ? response.error.message.toString() : response.error.toString(); yield put(relayVaultUpdateFail(errorMsg)); + return false; } } catch (err) { if (currentWalletAccountId() === sourceAppId && store.getState().storage.appId === sourceAppId) yield put(relayVaultUpdateFail('Something went wrong while deleting the vault!')); + return false; + } +} + +function* deleteVaultRequestWorker( + action: Parameters[0] & { onComplete?: (deleted: boolean) => void } +) { + let deleted = false; + try { + deleted = (yield call(deleteVaultWorker, action)) === true; + } catch (error) { + captureError(error); + } finally { + if (action.onComplete) { + try { + action.onComplete(deleted); + } catch (error) { + captureError(error); + } + } } + return deleted; } -export const deleteVaultyWatcher = createWatcher(deleteVaultWorker, DELETE_VAULT); +export const deleteVaultyWatcher = createWatcher(deleteVaultRequestWorker, DELETE_VAULT); function* reinstateVaultWorker({ payload, originAppId, originBackupAccountSession, originBackupWriteIntent }) { const sourceAppId = originAppId || currentWalletAccountId(); diff --git a/tests/recovery/deleteResultTruthfulness.test.cjs b/tests/recovery/deleteResultTruthfulness.test.cjs index 83e6b095ac..ea9883cab0 100644 --- a/tests/recovery/deleteResultTruthfulness.test.cjs +++ b/tests/recovery/deleteResultTruthfulness.test.cjs @@ -73,6 +73,70 @@ test('vault delete reports failure if Realm reports success but retains the obje assert.deepEqual(f.collections.Vault.map((row) => row.id), ['keep']); }); +test('vault delete callback is scoped to its request and fires once after local removal', async () => { + const f = fixture(); + f.collections.Vault.push({ id: 'remove' }); + f.scope.deleteVaultImageWorker = function* () { return { updated: true }; }; + const completed = []; + + const result = await f.run('deleteVaultRequestWorker', { + originAppId: f.app.id, + payload: { vaultId: 'remove' }, + onComplete: (deleted) => completed.push(deleted), + }); + + assert.equal(result, true); + assert.deepEqual(completed, [true]); + assert.deepEqual(f.collections.Vault, []); +}); + +test('vault delete callback reports failure once when Relay rejects deletion', async () => { + const f = fixture(); + f.collections.Vault.push({ id: 'keep' }); + f.scope.deleteVaultImageWorker = function* () { return { updated: false, error: 'rejected' }; }; + const completed = []; + + const result = await f.run('deleteVaultRequestWorker', { + originAppId: f.app.id, + payload: { vaultId: 'keep' }, + onComplete: (deleted) => completed.push(deleted), + }); + + assert.equal(result, false); + assert.deepEqual(completed, [false]); + assert.deepEqual(f.collections.Vault.map((row) => row.id), ['keep']); +}); + +test('overlapping vault saga requests report their own outcomes despite unrelated vault state', async () => { + const f = fixture(); + f.collections.Vault.push({ id: 'first' }, { id: 'second' }); + const pending = new Map(); + f.scope.deleteVaultImageWorker = ({ payload }) => + new Promise((resolve) => pending.set(payload.vaultIds[0], resolve)); + const completed = []; + const first = f.run('deleteVaultRequestWorker', { + originAppId: f.app.id, payload: { vaultId: 'first' }, + onComplete: (deleted) => completed.push(['first', deleted]), + }); + const second = f.run('deleteVaultRequestWorker', { + originAppId: f.app.id, payload: { vaultId: 'second' }, + onComplete: (deleted) => completed.push(['second', deleted]), + }); + for (let attempt = 0; attempt < 20 && pending.size < 2; attempt++) + await new Promise(setImmediate); + assert.equal(pending.size, 2); + + f.actions.push({ type: 'relayVaultUpdateSuccess', source: 'unrelated' }); + assert.deepEqual(completed, []); + pending.get('second')({ updated: true }); + assert.equal(await second, true); + assert.deepEqual(completed, [['second', true]]); + pending.get('first')({ updated: false, error: 'rejected' }); + assert.equal(await first, false); + assert.deepEqual(completed, [['second', true], ['first', false]]); + assert.deepEqual(f.collections.Vault.map((row) => row.id), ['first']); +}); + test('signer delete completion is false if Realm reports success but retains the signer', async () => { const f = fixture(); f.collections.Signer.push({ id: 'keep' }); @@ -107,23 +171,26 @@ const deleteHandlerCode = ts.transpileModule( ).outputText; function screenFixture(entityKind, usdtResult = false) { - const state = { active: 'A', session: 1, present: true, actions: [], toasts: [] }; + const state = { + active: 'A', session: 1, present: true, presentById: new Map(), actions: [], toasts: [], + }; const scope = { React: { createElement: (component) => component }, TickIcon: 'tick', ToastErrorIcon: 'error', EntityKind: { WALLET: 'WALLET', VAULT: 'VAULT', USDT_WALLET: 'USDT_WALLET' }, RealmSchema: { KeeperApp: 'KeeperApp', Wallet: 'Wallet', Vault: 'Vault', USDTWallet: 'USDTWallet' }, selectedWallet: { id: 'keep', entityKind }, - pendingVaultDeleteRef: { current: null }, dbManager: { getObjectByIndex: () => ({ id: state.active }), - getObjectById: () => state.present ? { id: 'keep' } : undefined, + getObjectById: (_schema, id) => + (state.presentById.has(id) ? state.presentById.get(id) : state.present) + ? { id } + : undefined, }, activeRealmAppId: () => state.active, currentBackupAccountSession: () => state.session, deleteAppImageEntity: (request) => ({ type: 'DELETE_BTC', ...request }), - deleteVault: (id) => ({ type: 'DELETE_VAULT', id }), - resetRealyVaultState: () => ({ type: 'RESET_VAULT' }), + deleteVault: (id, onComplete) => ({ type: 'DELETE_VAULT', id, onComplete }), deleteUSDTWallet: async () => usdtResult, dispatch: (action) => state.actions.push(action), showToast: (message, icon) => state.toasts.push({ message, icon }), @@ -175,10 +242,40 @@ test('ManageWallets does not report USDT deletion success while Realm retains it assert.equal(f.state.toasts[0].message, 'Failed to delete USDT wallet'); }); -test('ManageWallets leaves the vault delete result pending until the saga finishes', async () => { +test('ManageWallets waits for the matching vault delete before reporting success', async () => { + const f = screenFixture('VAULT'); + await f.deleteSelectedEntity(); + assert.deepEqual(f.state.toasts, []); + assert.deepEqual(f.state.actions.map((action) => action.type), ['DELETE_VAULT']); + f.state.present = false; + f.state.actions[0].onComplete(true); + assert.equal(f.state.toasts[0].message, 'Vault deleted'); +}); + +test('overlapping vault deletes and unrelated vault results cannot consume another request', async () => { + const f = screenFixture('VAULT'); + f.scope.selectedWallet = { id: 'first', entityKind: 'VAULT' }; + await f.deleteSelectedEntity(); + f.scope.selectedWallet = { id: 'second', entityKind: 'VAULT' }; + await f.deleteSelectedEntity(); + assert.deepEqual(f.state.actions.map((action) => action.id), ['first', 'second']); + + // Another vault action may publish its own global Redux success while both + // delete requests are in flight; only each request's callback can show a toast. + f.state.actions.push({ type: 'UNRELATED_VAULT_SUCCESS' }); + assert.deepEqual(f.state.toasts, []); + f.state.presentById.set('second', false); + f.state.actions[1].onComplete(true); + assert.deepEqual(f.state.toasts.map((toast) => toast.message), ['Vault deleted']); + f.state.actions[0].onComplete(false); + assert.deepEqual(f.state.toasts.map((toast) => toast.message), + ['Vault deleted', 'Failed to delete vault']); +}); + +test('a vault delete from an earlier account session shows no toast', async () => { const f = screenFixture('VAULT'); await f.deleteSelectedEntity(); + f.state.session += 2; + f.state.actions[0].onComplete(false); assert.deepEqual(f.state.toasts, []); - assert.equal(f.scope.pendingVaultDeleteRef.current.id, 'keep'); - assert.deepEqual(f.state.actions.map((action) => action.type), ['RESET_VAULT', 'DELETE_VAULT']); }); diff --git a/tests/recovery/sagaHarness.cjs b/tests/recovery/sagaHarness.cjs index dab506ccdd..cd6a39e0cb 100644 --- a/tests/recovery/sagaHarness.cjs +++ b/tests/recovery/sagaHarness.cjs @@ -313,6 +313,7 @@ function fixture(options = {}) { 'currentWalletAccountId', 'assertWalletAccount', 'addNewWalletsWorker', 'addSigningDeviceWorker', 'mergeSimilarKeysWorker', 'deleteSigningDeviceWorker', 'deleteVaultWorker', + 'deleteVaultRequestWorker', 'generateNewExternalAddressWorker', 'updateSignerPolicyWorker', 'updateVaultSignerXprivWorker', 'updateSignerDetailsWorker', 'updateKeyDetailsWorker', 'autoWalletsSyncWorker', From b160656218dc6065ad12af9db220c974255bfc23 Mon Sep 17 00:00:00 2001 From: Anant Date: Mon, 12 Oct 2026 22:31:17 +0530 Subject: [PATCH 146/254] Keep callback vault deletes out of shared result state --- src/store/sagas/wallets.ts | 18 +++++++++++++----- .../deleteResultTruthfulness.test.cjs | 19 +++++++++++++++++++ 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/store/sagas/wallets.ts b/src/store/sagas/wallets.ts index c875990688..0dd71a647e 100644 --- a/src/store/sagas/wallets.ts +++ b/src/store/sagas/wallets.ts @@ -1830,13 +1830,20 @@ function* updateKeyDetailsWorker({ payload, originAppId, originBackupAccountSess export const updateKeyDetails = createWatcher(updateKeyDetailsWorker, UPDATE_KEY_DETAILS); -function* deleteVaultWorker({ payload, originAppId, originBackupAccountSession, originBackupWriteIntent }) { +function* deleteVaultWorker({ + payload, + originAppId, + originBackupAccountSession, + originBackupWriteIntent, + onComplete, +}) { const sourceAppId = originAppId || currentWalletAccountId(); const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); const { vaultId } = payload; + const reportsThroughCallback = typeof onComplete === 'function'; try { assertWalletAccount(sourceAppId, requestSession); - yield put(setRelayVaultUpdateLoading(true)); + if (!reportsThroughCallback) yield put(setRelayVaultUpdateLoading(true)); const backupEnabled = yield select((state: RootState) => isAutomaticCloudBackupEnabled(state.bhr, sourceAppId) ); @@ -1857,17 +1864,18 @@ function* deleteVaultWorker({ payload, originAppId, originBackupAccountSession, throw new Error('Local vault deletion failed'); } assertWalletAccount(sourceAppId, requestSession); - yield put(relayVaultUpdateSuccess()); + if (!reportsThroughCallback) yield put(relayVaultUpdateSuccess()); return true; } else { const errorMsg = response.error?.message ? response.error.message.toString() : response.error.toString(); - yield put(relayVaultUpdateFail(errorMsg)); + if (!reportsThroughCallback) yield put(relayVaultUpdateFail(errorMsg)); return false; } } catch (err) { - if (currentWalletAccountId() === sourceAppId && store.getState().storage.appId === sourceAppId) + if (!reportsThroughCallback && currentWalletAccountId() === sourceAppId && + store.getState().storage.appId === sourceAppId) yield put(relayVaultUpdateFail('Something went wrong while deleting the vault!')); return false; } diff --git a/tests/recovery/deleteResultTruthfulness.test.cjs b/tests/recovery/deleteResultTruthfulness.test.cjs index ea9883cab0..cc0100372f 100644 --- a/tests/recovery/deleteResultTruthfulness.test.cjs +++ b/tests/recovery/deleteResultTruthfulness.test.cjs @@ -88,6 +88,9 @@ test('vault delete callback is scoped to its request and fires once after local assert.equal(result, true); assert.deepEqual(completed, [true]); assert.deepEqual(f.collections.Vault, []); + assert.ok(!f.actions.some((action) => + ['setRelayVaultUpdateLoading', 'relayVaultUpdateSuccess', 'relayVaultUpdateFail'] + .includes(action.type))); }); test('vault delete callback reports failure once when Relay rejects deletion', async () => { @@ -105,6 +108,22 @@ test('vault delete callback reports failure once when Relay rejects deletion', a assert.equal(result, false); assert.deepEqual(completed, [false]); assert.deepEqual(f.collections.Vault.map((row) => row.id), ['keep']); + assert.ok(!f.actions.some((action) => + ['setRelayVaultUpdateLoading', 'relayVaultUpdateSuccess', 'relayVaultUpdateFail'] + .includes(action.type))); +}); + +test('legacy vault delete without a callback retains its global result state', async () => { + const f = fixture(); + f.collections.Vault.push({ id: 'remove' }); + f.scope.deleteVaultImageWorker = function* () { return { updated: true }; }; + + assert.equal(await f.run('deleteVaultRequestWorker', { + originAppId: f.app.id, + payload: { vaultId: 'remove' }, + }), true); + assert.ok(f.actions.some((action) => action.type === 'setRelayVaultUpdateLoading')); + assert.ok(f.actions.some((action) => action.type === 'relayVaultUpdateSuccess')); }); test('overlapping vault saga requests report their own outcomes despite unrelated vault state', async () => { From 975f98e5c2a461f6c461e1b9b537352dc0423f03 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Tue, 13 Oct 2026 03:05:02 +0000 Subject: [PATCH 147/254] docs: reconcile R4 automatic backup contract and proof gates --- .../automatic-write-readiness.md | 14 +- .../automatic-write-validation.md | 96 +++++++++++ .../fix-recovery-backup-freshness/design.md | 59 ++++--- .../nfr-review.md | 10 +- .../fix-recovery-backup-freshness/proposal.md | 35 ++-- .../specs/assisted-server-backup/spec.md | 158 +++++++++++++----- .../fix-recovery-backup-freshness/tasks.md | 75 ++++++--- .../validation.md | 14 +- 8 files changed, 350 insertions(+), 111 deletions(-) create mode 100644 openspec/changes/fix-recovery-backup-freshness/automatic-write-validation.md diff --git a/openspec/changes/fix-recovery-backup-freshness/automatic-write-readiness.md b/openspec/changes/fix-recovery-backup-freshness/automatic-write-readiness.md index ca39a8c404..f697b9a71d 100644 --- a/openspec/changes/fix-recovery-backup-freshness/automatic-write-readiness.md +++ b/openspec/changes/fix-recovery-backup-freshness/automatic-write-readiness.md @@ -1,12 +1,14 @@ # Automatic Assisted Backup writes: implementation gates -Scope reviewed: Keeper `0550ac9ffa938a079816ae4ed58bc970324c3e08` and Relay PR #4 at `82ee3b10708a56d220170505d1bb7e3bc8429e77`. This is an implementation plan, not permission to enable the currently disabled seven incremental writes. No release or Relay deployment is implied. +R4 requirements reconciled 13 October 2026 from audit starting app head `b160656218dc6065ad12af9db220c974255bfc23`. The original plan reviewed Keeper `0550ac9ffa938a079816ae4ed58bc970324c3e08` and Relay PR #4 `82ee3b10708a56d220170505d1bb7e3bc8429e77`; those are historical inputs, not the final proof source. -## Why the current intent cannot authorize a write +The owner requires the automatic-backup fix and disposable proof in R4. Critical native IOS/ANDROID backup checks also precede release; explicitly physical-only follow-up may be recorded separately. This plan authorizes no production enablement, merge or Relay deployment. Source implementation must stay fail-closed until committed-payload/session proof and the required disposable matrix pass. Record current implementation and results in [automatic-write-validation.md](automatic-write-validation.md); no future gate is marked complete here. -`BackupWriteIntent` records the selected account session and a previously verified Relay revision. It does not identify the local recovery state represented by an encrypted payload or deletion. `backupPost` therefore rejects all seven operations before network. This is required: some producers encrypt action data before Realm persistence, Realm may change while an action waits, and one action can emit several Relay writes with the same captured base revision. A valid server compare-and-swap protects against another server writer; it does not prove that the client is sending its present local recovery data. +## Why the audit-baseline intent cannot authorize a write -The central Realm observer invalidates backup verification **before** each relevant write. It has no post-commit token and can fire for several field writes in one logical action. The transport's `queuedMutationRevisions` is currently never populated, so it cannot provide a queue completion proof. A pre-send comparison of a prepared ciphertext against the current Realm image is necessary but still insufficient for operations deliberately sent before local persistence. +At the audit starting source, `BackupWriteIntent` records the selected account session and a previously verified Relay revision. It does not identify the local recovery state represented by an encrypted payload or deletion. `backupPost` therefore rejects all seven operations before network. This is required at that baseline: some producers encrypt action data before Realm persistence, Realm may change while an action waits, and one action can emit several Relay writes with the same captured base revision. A valid server compare-and-swap protects against another server writer; it does not prove that the client is sending its present local recovery data. Later implementation/proof must update the result matrix; these observations are not a claim about its final source. + +The baseline central Realm observer invalidates backup verification **before** each relevant write. It has no post-commit token and can fire for several field writes in one logical action. The baseline transport's `queuedMutationRevisions` is never populated, so it cannot provide a queue completion proof. A pre-send comparison of a prepared ciphertext against the current Realm image is necessary but still insufficient for operations deliberately sent before local persistence. ## Required transaction model @@ -20,7 +22,7 @@ The central Realm observer invalidates backup verification **before** each relev ## Operation-specific proof at the queue head -| Relay operation | Local proof before send | Current producer complication | +| Relay operation | Local proof before send | Audit-baseline producer complication | | --- | --- | --- | | `updateAppImage` | Decrypt each wallet/USDT/signer ciphertext with the active account seed; map IDs, sanitized signer records and recoverable fields must equal the committed Realm records. `replaceNodes` requires the exact current normalized node set; app metadata must come from the current KeeperApp. | New wallet and signer creation call backup before Realm insertion. Node and version paths use different timing. Action payloads may be stale. | | `migrateXfps` | Old exact KeyUID must be absent in committed Realm; new exact KeyUID and decrypted sanitized signer must match Realm. Verify all affected vault signer references, and prove the old map entry exists in the validated Relay snapshot. | The worker updates a live signer before building the request. It previously sent bare fingerprints while Relay maps use KeyUIDs; a narrow key fix is included separately, with transport still disabled. | @@ -36,4 +38,4 @@ Run each of the seven operations against disposable Realm and transactional Rela Add focused multi-write fixtures for (a) creating and archiving a vault, (b) one VaultSigner registration affecting several vaults, and (c) label import plus deletion. Make the first Relay write succeed and the next fail; assert local data survives, the remaining prepared writes do not run, and neither a saga nor Redux reports verified. Assert a local mutation injected just before the final verification commit leaves the backup pending. Pair these tests with native iOS/Android create, edit, delete, relaunch and clean Recovery Key restore on the exact app SHA, plus Relay v2 HTTP tests against a disposable replica set. -Keep automatic transport disabled until this matrix passes and all old Relay instances and legacy full writers are drained. Production transaction topology and the actual Relay deployment are separate release evidence. A future source change must never turn an endpoint name or revision string alone into a freshness proof. +Keep automatic source transport fail-closed until this matrix passes. Production delivery additionally requires verified transaction topology, compatible running Relay artifacts and protection from old/legacy writers under separately authorized rollout; a source pass is not deployment evidence. A future source change must never turn an endpoint name or revision string alone into a freshness proof. The current result matrix also tracks mandatory native and clean Recovery Key restore checks independently of fixture outcomes. diff --git a/openspec/changes/fix-recovery-backup-freshness/automatic-write-validation.md b/openspec/changes/fix-recovery-backup-freshness/automatic-write-validation.md new file mode 100644 index 0000000000..4e921ddb6c --- /dev/null +++ b/openspec/changes/fix-recovery-backup-freshness/automatic-write-validation.md @@ -0,0 +1,96 @@ +# R4 automatic Assisted Backup: result matrix + +This is the required proof matrix, reconciled 13 October 2026. **No implementation or test pass is asserted by these placeholders.** The coordinator replaces a result only with observed exact-source evidence. Historical September repair/native logs are preserved in [validation.md](validation.md) and do not complete this matrix. + +Automatic-backup implementation and disposable proof are mandatory R4 work. Critical native IOS and ANDROID backup acceptance also precedes release. Explicitly physical-device-only follow-up may be separate; it cannot waive the required fixtures or critical simulator/emulator checks. + +## Source and result record + +| Field | Current record | +| --- | --- | +| Audit starting app source | b160656218dc6065ad12af9db220c974255bfc23; not final proof source | +| Final tested app commit / working-tree state | NOT RECORDED | +| Final tested Relay commit / working-tree state | NOT RECORDED | +| Fixture command, UTC time, runtime and platform | NOT RUN / awaiting coordinator evidence | +| Disposable Realm dataset and Mongo replica-set topology | NOT RECORDED | +| HTTP entry points and production saga/hook/transport coverage | NOT RECORDED | +| Test counts and individual result artifact | NOT RECORDED | +| Clean encrypted Recovery Key restore result | NOT RUN | +| IOS exact-source native result | NOT RUN | +| ANDROID exact-source native result | NOT RUN | +| Independent developer review / current-head CI | NOT RECORDED | +| Production running artifact / topology / authorization | NOT VERIFIED; no production action authorized by this matrix | + +Use PASS, FAIL, BLOCKED or NOT RUN per scenario, with command/log/provenance. A blocked/missing critical result keeps the gate open. A count from unit tests alone must not be described as real Realm/HTTP/native evidence. Preserve hashes/source/run IDs and sanitized counts; never record real account/wallet identifiers, seed/Recovery Key material, plaintext backups, production configuration or credentials. + +## Common cases required for every operation + +| Case | Required assertion | +| --- | --- | +| C1 committed match | Real local commit precedes cloud send; operation ID, originating account/session, generation, decrypted payload/target and original verified base are valid. Exactly one intended v2 effect reaches the production transport/HTTP path. Local results survive cloud failure. | +| C2 uncommitted/stale state | Failed/partial local commit, stale generation/payload, wrong encryption owner or missing operation proof sends no mutation. No pre-persistence upload or newly verified base is substituted. | +| C3 account session | A→B→A before send performs no old-session mutation; change after send remains uncertain/pending for A without using B's key/data or changing B's status. | +| C4 remote conflicts | Apply the proposed effect against the validated remote snapshot; remote-only records and immutable identity conflicts are preserved without automatic overwrite/deletion. | +| C5 consent | Disabled/unconsented accounts send zero automatic mutations. The separately explicit whole-backup opt-out preserves its consent choice until positive acknowledgement. | +| C6 competing writer | A real revision race/409 stops the chain and leaves pending; no refreshed base authorizes an old payload. | +| C7 old/malformed protocol | 404/unsupported v2, malformed snapshot or acknowledgement, false/missing preconditionApplied, and invalid/missing newRevision fail closed with no legacy fallback. | +| C8 timeout/lost acknowledgement | Read back uncertain server state before fresh work; do not replay ciphertext or send later fanout members. Matching content alone does not complete the interrupted originating operation. | +| C9 complete readback | Final decrypted server image includes all recovery records/references and matches freshly captured local state at the same committed generation/session. Corruption, partial response or missing references cannot produce verified. | +| C10 final commit race/restart | Inject a local mutation immediately before Redux verification commit: pending persists and verified is rejected. Restart keeps committed local data and pending state; it does not replay prepared payloads. | + +## Seven-operation matrix + +Every row must cover C1–C10 with real encryption/decryption and production producer/transport entry points against disposable committed Realm and transactional Relay fixtures. Whole-backup deletion is the explicit opt-out exception to payload-equals-retained-local-image, not an automatic wipe operation. + +USDT fixtures use existing/recovered wallet records and their supported metadata/deletion paths. They do not enable the paused new-wallet setup/import or GasFree/transactional product flows. + +| Operation | Operation-specific proof / target | C1–C5 result + evidence | C6–C10 result + evidence | +| --- | --- | --- | --- | +| updateAppImage | Wallet/USDT/signer ciphertext and exact IDs match committed sanitized records; replaceNodes equals normalized current nodes; app/version metadata belongs to current KeeperApp. Cover creation, edit, nodes and version producers. | NOT RUN | NOT RUN | +| migrateXfps | Old exact KeyUID absent locally; new exact KeyUID/signer matches committed Realm; expected old remote map exists; retained vault signer references remain valid. | NOT RUN | NOT RUN | +| deleteAppImageEntity | Each BTC/USDT/signer ID absent after successful local commit and present in expected remote image; retained vaults stay recoverable. Cover direct USDT hook and saga paths. | NOT RUN | NOT RUN | +| deleteVaults | Exact committed local absence; shared/participant references remain owned and recoverable; server-only vaults are preserved. | NOT RUN | NOT RUN | +| updateVaultImage | Vault ID, policy, sanitized signer records, archive state and signersData match committed Vault and persisted registrations. Cover creation/edit and migration/registration producers. | NOT RUN | NOT RUN | +| modifyLabels | Added ciphertext matches current tag and account-derived hashed ID; deleted hashed IDs correspond to committed absence; add/delete overlap and delayed import callbacks are reconciled. | NOT RUN | NOT RUN | +| deleteBackup | Explicit same-account/session opt-out; fresh validated remote revision and exact whole-account target; local recovery data retained and consent unchanged until positive acknowledgement. Lost response does not imply deletion success. | NOT RUN | NOT RUN | + +## Logical fanout and producer proof + +| Scenario | Required assertion | Result + evidence | +| --- | --- | --- | +| New vault plus archived predecessor | Local logical commit completes before cloud effects. One transaction or acknowledged revision chain includes both; final full readback/restore retains active and archived continuity. | NOT RUN | +| Registration shared by several vaults | Committed registration propagates to every affected vault and complete readback includes it. No intermediate response marks the whole backup verified. | NOT RUN | +| Label import plus deletion | Local tags commit first; exact hashed IDs/ciphertext and overlap reconciliation are proved. Remote-only content survives. | NOT RUN | +| First fanout member succeeds, next fails | Remaining prepared members never run; no rollback/success fiction; local data survives and pending persists until reconciliation. Exercise 409, malformed acknowledgement and lost response. | NOT RUN | +| Interleaved local mutation between fanout members | Revalidate proof and committed generation before each send; reject stale effects, stop the chain and keep pending. | NOT RUN | +| Queued action receives newer verification | It cannot borrow the new baseline/epoch or operation-generation proof to authorize old ciphertext. | NOT RUN | +| Pre-persistence producers | Wallet/signer/vault creation and BTC/USDT/signer/vault deletion use local-first completion; cloud failure preserves truthful local result and pending backup. | NOT RUN | +| Disabled/new unconsented account | Local create/edit/delete works; zero automatic writes and no inherited consent. | NOT RUN | +| Complete encrypted restore | A clean disposable Recovery Key restore includes intended BTC MAINNET/TESTNET wallets, USDT recovery metadata, keys, active/archived vaults, registrations, labels and nodes with exact recoverable identities. No partial/empty success. | NOT RUN | +| Transactional HTTP/old writers | Real replica-set CAS race, rollback, malformed/unsupported HTTP, old/full-writer fencing and running source parity are tested. This is not production topology/deployment proof. | NOT RUN | + +## Mandatory critical native acceptance + +Both platforms must test the final source with disposable accounts/data and a compatible fixture backend. Do not replace the owner's installed app or mutate real wallets for this matrix. Use native build/launch plus simulator/emulator flows; unit mocks and a historical simulator compile do not substitute for launch/flow evidence. + +| Native scenario | IOS result + evidence | ANDROID result + evidence | +| --- | --- | --- | +| Build and launch exact source; identify native package/version and backend source | NOT RUN | NOT RUN | +| Opted-in automatic create/edit/delete after initial backup; local data survives forced cloud failure and relaunch | NOT RUN | NOT RUN | +| Complete server readback and clean Recovery Key restore of newly produced committed dataset | NOT RUN | NOT RUN | +| Legacy encrypted snapshot upgrade plus absent/stale/transport/server/decryption failure; no false missing/empty/verified or unsolicited replacement | NOT RUN | NOT RUN | +| Account/consent switch including A→B→A while connectivity/queue/readback waits; no cross-account upload, key selection, persistence or status change | NOT RUN | NOT RUN | +| Local mutation during upload/readback/final completion; truthful pending and no stale verified | NOT RUN | NOT RUN | +| Interrupted/uncertain write, relaunch and manual read-only reconciliation; no ciphertext replay or spinner trap | NOT RUN | NOT RUN | +| Interrupted clean recovery preserves truthful incomplete result; correct Recovery Key retries unavailable/corrupt backup without incorrect-words/empty-success feedback | NOT RUN | NOT RUN | +| Registration-only repair and immutable key/policy conflict, with all affected vaults included in verified readback | NOT RUN | NOT RUN | + +Native performance/layout and hardware/physical-only results remain separately described in [nfr-review.md](nfr-review.md) and the final Local handoff. Any accepted physical-only follow-up must name the exact unperformed scenario and its authority; it must not relabel required critical native safety checks as optional. Store/signing/deployment/merge approval remains separate from test results. + +## Durable evidence link plan + +1. Freeze exact app and Relay source plus fixture/runtime provenance. Record working-tree changes instead of implying committed clean source. +2. Save a sanitized per-scenario result artifact under this change's evidence directory using final source/run identity; link it from the relevant rows. Include actual command, UTC execution time, outcome/count and negative-case assertions. Preserve existing historical logs unchanged. +3. Attach the complete disposable Realm/HTTP/restore report and CI run references to the existing PR. Unit-only and native results receive separate labels. +4. After the final Cloud push, add the AGENTS.md Codex local handoff with the actual current SHA and IOS, ANDROID (plus relevant HARDWARE) checks. Link exact-head Local validation for each native row; new code commits invalidate earlier signoff. +5. Only observed evidence closes a task. Keep production/operator and release blockers visible even after all source/fixture/native rows pass. This document does not send messages, edit trackers, push code or authorize release operations. diff --git a/openspec/changes/fix-recovery-backup-freshness/design.md b/openspec/changes/fix-recovery-backup-freshness/design.md index 3ab14e30c9..508218ed6f 100644 --- a/openspec/changes/fix-recovery-backup-freshness/design.md +++ b/openspec/changes/fix-recovery-backup-freshness/design.md @@ -1,36 +1,55 @@ # Design -## Entry points and user flow -Reuse More → Backup and Recovery → Assisted Server Backup. The row opens `AssistedBackupStatus`; its existing switch retains enable/delete behavior. The existing server-backup notification opens the same status screen. Login dispatches a read-only check after wallet sync for opted-in accounts with pending/unverified status. Login does not await the result. +This is the R4 target contract reconciled on 13 October 2026 from audit starting app head b160656218dc6065ad12af9db220c974255bfc23. Automatic write implementation and proof must be recorded in [the result matrix](automatic-write-validation.md) before any gate is marked complete. The September native/fixture results in [validation](validation.md) remain historical. -The status screen uses ScreenWrapper, WalletHeader, KeeperText, Buttons, themed cards and a ScrollView. Body text explicitly enables system font scaling. Full-width actions are stacked. Back is available throughout and does not cancel or imply rollback. No progress percentages or promises of continued background execution are shown. +## Entry points, consent and user flow -Copy is maintained in `recoveryBackup` in both existing locale JSON files. States: disabled, checking, different, preparing, uploading, verifying, verified, conflict, unverified. Different offers **Back Up Now**; terminal enabled states offer **Check Again**. Slow-stage text appears after ten seconds. Restart maps persisted in-progress phases to unverified until the new check starts. +Reuse More → Backup and Recovery → Assisted Server Backup, AssistedBackupStatus and the existing notification. Login may schedule a read-only comparison for an opted-in pending/unverified account after essential startup/sync work, without awaiting its network result. Account-scoped opt-in never transfers to another account. A legacy global flag is migrated only with evidence of the consenting account. -## Backup comparison and repair -`src/services/backup/image.ts` compares canonical decrypted recovery data, with deterministic object ordering. Exclude rebuildable chain caches, balance, sync timestamps, receiving-address caches and live node connection flags. Retain user-authored manual UTXO spendability overrides by outpoint, independent of confirmation/cache ordering. Retain keys, derivation, address counters, signer policy, vault policy/archive state, labels and user metadata. Include BTC wallets from both networks, USDT, signers, vaults, nodes and labels. Sanitize local signer/vault secrets using the existing helpers and never mutate Realm during checking. +The status screen keeps real checking, preparing, uploading and verifying stages, slow-operation explanation and an immediate Back action. Leaving does not imply rollback or continued background execution. Durable per-account pending/unverified state survives restart; active spinner state is transient. Different content offers explicit Back Up Now; a failed check reports an unavailable/unverified result, not missing data or permission to replace it. Recovery Key physical confirmation and verified server content are distinct states. -Server-only records, differing key identities and server address counters ahead of local data produce conflict with no upload. Invalid/decryption-failed/partial responses produce unverified with no upload. Capture a local snapshot and check it again before upload and completion. Final success requires the uploaded recovery data to match the server readback and current local recovery data. +## Local transaction and operation identity -A lost upload response has an ambiguous outcome. Attempt readback within the same overall deadline. A matching readback is sufficient for verified success; otherwise keep unverified and let the user check again. No automatic retry loop or repeated full upload on startup. +Each logical recovery operation captures the originating app/account, selected-account session, operation ID and originally verified remote revision/epoch before asynchronous work. Local changes are saved first. After all related Realm writes succeed, the operation obtains an account-scoped monotonic committed recovery generation. Pre-write invalidation alone is not a commit token. Failed/partial writes never authorize a cloud mutation or success; retain any local data and a pending result for reconciliation. -## Ordering and persistence -All client backup mutation endpoints use a shared per-account queue in `transport.ts`; full inspection owns the queue through readback. Pending incremental writes increment an account revision and invalidate any in-flight completion claim. Vault `appID` and `appId` payload variants share the correct account queue. A new wallet's incremental write is never replaced with a pre-creation snapshot. +New wallets, signers, vaults, node changes and deletions must not depend on server acceptance to persist locally. Producers previously sending encrypted data before Realm insertion/deletion are reordered. If work is interrupted after the local commit, the data and durable pending status survive; restart reconstructs fresh work only after reading local/remote state, not by replaying old ciphertext. -BHR is separately persisted. `backupRepairStateByAppId` and `backupRepairCompletedByAppId` are durable; `backupRepairRunningByAppId` is blacklisted. Only phase verified sets completion. Old persisted states use nullish defaults. Account changes cannot clear the next account's pending flag or enable its backup setting. No Realm migration is needed. +An A→B→A transition invalidates the original A session, even if the same account ID returns. No worker substitutes another account's seed, destination or persistence context. A fresh check cannot retroactively grant an older action a new base revision or committed-generation token. -## NFR implementation and remaining limits -Transport aborts after 30 seconds without advancing upload/download bytes, with a ten-minute overall attempt deadline. Both are engineering limits requiring native/deployed-backend validation. Progress callbacks reporting the same byte count do not extend the timer. Error messages discard Axios request bodies/headers. No new telemetry is added. +## Queue-head proof and recovery comparison -AES encrypt/decrypt yields between 16 KiB chunks using CryptoJS's existing OpenSSL format. Local JSON text is guarded using a conservative three bytes per UTF-16 code unit against an 8 MiB record budget; encrypted aggregate payloads are limited to 32 MiB. Reject oversized data without truncation or verified success. Realm extraction, JSON serialization/parsing, Base64 conversion and UTF-8 decoding still have synchronous sections; native frame-time and memory budgets are NOT established by these guards or desktop tests. +The per-account queue covers automatic app/vault/label/deletion mutations and explicit inspection/repair through final readback. Different accounts remain isolated. Each automatic effect must be proved at the queue head using fresh committed local recovery data, account/session, consent, encryption-key ownership, operation ID and generation. Stale action payloads, failed commit, generation/session change or missing intent leave pending without network. -## Server contract and release gates -Repair now requires `getBackupSnapshot` and `repairAppBackup` from the paired relay candidate. The snapshot returns a SHA-256 revision over encrypted recovery records. Repair supplies that revision and rechecks it inside a MongoDB snapshot transaction. Vaults, labels, signer maps and the app image update atomically; stale revisions, missing referenced records and attempts to omit existing recovery records fail closed. Existing label IDs update their encrypted content. Archived metadata is preserved. No fallback to the legacy full replacement endpoint is permitted. +image.ts compares canonical decrypted recovery data with deterministic ordering. Ignore rebuildable chain/balance/sync/receiving-address caches and live connection flags. Retain keys, derivation, address counters, manual UTXO spendability overrides by outpoint, signer/vault policy, archive state, labels, nodes and user metadata. Include both BTC networks, USDT, signers and all referenced vault/label records. Sanitize secrets with existing helpers; checking does not mutate Realm. -Local database and HTTP tests establish this contract for the new endpoints. Production requires a transaction-capable replica set, compatible deployed request limits and relay deployment before the mobile update. Those deployment facts are not yet verified. +Prove additions/updates by decrypting the proposed record and matching its exact ID and recoverable fields with the committed record. Prove deletion by committed local absence and presence in the validated remote snapshot; retained vaults must remain recoverable. migrateXfps uses exact KeyUID map entries, not bare fingerprints, and validates affected vault signer references. Labels use account-derived hashed IDs and current tag content. Whole-backup deletion is a separate explicit same-session opt-out operation; preserve consent until a positive acknowledgement. -The legacy full-backup endpoint remains callable by older clients and does not participate in the new revision contract. Mixed-version full replacement remains an unresolved release gate. Normal incremental wallet/signer updates in the relay candidate now set individual records atomically, including a version-only call without `nodes`, but that does not make every legacy mutation transactional. Server-only/conflicting data is preserved pending reconciliation. +Apply the proposed operation to the validated remote snapshot in memory before send. Reject removal of remote-only recovery records or immutable key/derivation/network/script/policy conflicts without explicit reconciliation. An invalid/partial/decryption-failed response is unavailable, never empty. Repairable registration metadata is distinct from immutable identity and must be preserved and propagated to every affected vault with readback. -## Restore integrity follow-up +## Revisioned server contract and logical fanout -Actual Recovery Key restoration predecodes the complete server image, verifies referenced vault/label identities, and requires each local persistence operation to succeed. A failed fetch cannot masquerade as an empty backup. Success is exposed only after the expected derived Recovery Key signing material is present; failed records are not skipped. Database writes remain sequential rather than one atomic transaction. A correct Recovery Key with unavailable/corrupt backup receives retry feedback instead of an incorrect-words error. Existing private-material policy is preserved per key type. +Read getBackupSnapshot through the validated v2 protocol, including a valid 64-character revision and required capabilities. Automatic and explicit repair writes use revisionedBackupMutation; an explicit repair identifies operation repairAppBackup. Never fall back to legacy full replacement or an old incremental route after 404, 409 or protocol failure. + +The Relay must atomically compare expectedRevision and apply all effects, preserving recovery records and rejecting ownership/reference/identity conflicts. A valid acknowledgement requires updated: true, preconditionApplied: true and a valid newRevision. Capability advertisement alone is not proof that a particular mutation used a precondition. + +Prefer one revisioned transaction for a logical fanout, including new+archived vault migration, registration affecting several vaults, and label import/deletion. If the existing route is used sequentially, every member revalidates its committed local proof and uses the preceding acknowledged newRevision; do not reuse one captured base for all members. Partial success is pending, with local data retained. A failed/uncertain member stops later prepared members. Intermediate responses never mark the whole backup verified. + +A request timeout/lost response may have committed on the server. Do not replay its ciphertext or assume a failed server write. Read back before deciding on any fresh write. The originating operation remains pending without a valid acknowledgement; a later independent read-only check may establish a new verified baseline only after complete content, current generation/session and protocol checks. It must not report the interrupted producer or fanout as completed. + +## Complete readback and atomic UI completion + +After the last acknowledged effect, read back and decrypt the complete server image, validating all referenced records. Compare it with a newly captured local image at the same committed generation. Recheck the originating account/session, consent, generation and operation at the final Redux verification commit. A concurrent local mutation, incomplete response, missing record or immutable conflict invalidates completion. Only this atomic final transition may set verified or clear durable pending state. + +BHR persists per-account verification/repair state separately from transient running state. Nullish defaults protect older persisted state. Account switching cannot clear another account's pending flag or grant its backup consent. Interrupted state returns to pending/unverified until a fresh read-only comparison completes. No Realm schema migration is authorized by this document. + +## NFR and restore boundaries + +Existing engineering bounds are a 30-second no-progress transport abort and a ten-minute overall deadline. Genuine upload/download byte progress resets the stall timer; repeated equal byte counts do not. They remain provisional until native/deployed request-limit checks pass. Error reporting excludes Axios bodies/headers and private recovery material. + +Crypto yields between 16 KiB chunks while retaining the existing OpenSSL format. Record/aggregate limits reject oversized data without truncation or verified success. Realm extraction, JSON, Base64 and UTF-8 still contain synchronous work; desktop timing is not native frame-time or memory acceptance. Preserve [the NFR review](nfr-review.md) as historical benchmarks and an open measurement plan. + +Recovery predecodes the complete image and validates referenced vault/label identity before persistence. Failed local persistence or a derived Recovery Key identity mismatch prevents success; no failed records are skipped. Local restoration currently has sequential writes, so interruption may leave partial local records without completing recovery. Exact-head disposable and native clean/retry/interruption restore checks are mandatory before release. A correct Recovery Key with unavailable/corrupt backup receives retry feedback, not an incorrect-words error. + +## Release gates + +The automatic fix, all seven operation proofs, fanout/failure/readback matrix and critical iOS/Android backup tests are required R4 work, not a post-release waiver. Physical-device-only checks may be separately reported after release when authorized. Old-client/fenced-writer behavior needs exact-source disposable replica-set/HTTP proof; production transaction topology, running artifact, request limits and legacy-writer drain/rollout remain separately gated by operator access and explicit authorization. Source readiness is not deployed-service evidence. diff --git a/openspec/changes/fix-recovery-backup-freshness/nfr-review.md b/openspec/changes/fix-recovery-backup-freshness/nfr-review.md index ec2645a4ea..315b197bf0 100644 --- a/openspec/changes/fix-recovery-backup-freshness/nfr-review.md +++ b/openspec/changes/fix-recovery-backup-freshness/nfr-review.md @@ -1,8 +1,8 @@ # Backup comparison and repair: NFR review -Reviewed 29 September 2026. This audit records the original baseline for the check → prompt → update → verify flow. The flow and safeguards are now implemented locally; see design.md and validation.md for current status. The observations below describe the pre-change candidate, not the final implementation. No approved numerical backup performance budgets were found in the synced product sources or repository OpenSpec documents searched. The thresholds below are proposed engineering targets, subject to native validation. +Reviewed 29 September 2026; evidence boundary clarified 13 October 2026. This file preserves the historical check → prompt → update → verify audit and measurements. Statements below about implemented safeguards, completed native runs or infrastructure describe that historical candidate; they are not exact-head R4 automatic-write acceptance. See [design.md](design.md), [validation.md](validation.md) and [the current automatic-write matrix](automatic-write-validation.md) for current requirements/results. No approved numerical backup performance budgets were found in the product sources or OpenSpec documents searched at the historical review. The thresholds below remain proposed engineering targets, subject to native validation. -## Original observed gaps (addressed unless listed as a remaining gate in design.md) +## Historical original observed gaps - `src/services/rest/RestClient.ts:64`: POST passes headers only to Axios. The backup endpoints supply no explicit request timeout, abort signal, or transfer progress callbacks. No global Axios timeout override was found. A stalled request can leave the loading state pending indefinitely. An existing unrelated call passes `{timeout: 20000}` as the headers argument, so that pattern must not be copied as a timeout implementation. - `src/store/sagas/bhr.ts`: full backup synchronously serializes/encrypts all wallet, signer, vault, node and label records before posting one large request. Saga orchestration does not move synchronous encryption off the React Native JavaScript thread. One large wallet can stall it even if work yields between smaller records. @@ -37,14 +37,14 @@ Transfer estimates exclude the initial comparison download, protocol overhead, l 8. **Privacy and consent:** preserve Assisted Server Backup opt-out. Report diagnostic sizes, durations, stage and error category only; no Recovery Key, private keys, plaintext backup, wallet IDs or addresses in telemetry/logs. Do not add new analytics as part of this review. 9. **Accessibility:** use existing Keeper components; stage/failure text must remain readable at small widths and enlarged system text sizes. Controls remain reachable, progress changes are accessible, and a spinner is not the only status signal. -## Native acceptance matrix / current status +## Historical native acceptance matrix -**Completed locally:** native small-fixture upgrade and clean recovery on Android/iOS, Android repair/readback, iOS backend failure/retry. These are development/simulator results. +**Historically completed, September source only:** native small-fixture upgrade and clean recovery on Android/iOS, Android repair/readback, iOS backend failure/retry. These development/simulator results do not validate the final R4 automatic pipeline. **Remaining native matrix:** large/single-large-record datasets; many labels/signers; both networks and USDT; normal network, 1 Mbps network, high latency, no response, offline/reconnect; background/foreground, OS kill/relaunch, screen navigation, account switch, repeated taps, and wallet mutation during upload. Measure UI stalls, end-to-end stage durations, peak memory, bytes and retry count. Verify bounded stalled-state exit and uncertain-outcome readback, and that matching backups produce no repair prompt. **Infrastructure update:** the shared Docker VM still has storage errors. A separate `keeper-recovery-qa` Colima profile now runs a healthy disposable MongoDB replica set and local relay; database/HTTP contract tests pass. Native performance acceptance is still pending. The Mac benchmark is a finding, not an NFR pass; keep the candidate in Now until applicable release gates pass. -## Current desktop stress follow-up +## Historical desktop stress follow-up The real production AES/comparison path under Node 22, with mocked Realm/transport, repaired 10 wallets + 10 keys in 487 ms, 100 + 100 in 3,866 ms and 1,000 + 1,000 in 35,314 ms. Comparison took 189 / 2,050 / 20,145 ms; maximum event-loop delays were 36 / 37 / 91 ms on a busy Mac. The largest case exceeds the proposed 50 ms task target even here. This is not native NFR acceptance. Evidence: now-candidate-2026-09-29/evidence/backup-performance.json. diff --git a/openspec/changes/fix-recovery-backup-freshness/proposal.md b/openspec/changes/fix-recovery-backup-freshness/proposal.md index c19dfb9f77..ca1011522c 100644 --- a/openspec/changes/fix-recovery-backup-freshness/proposal.md +++ b/openspec/changes/fix-recovery-backup-freshness/proposal.md @@ -1,16 +1,25 @@ # Recovery Key: keep Assisted Server Backup current ## Why -T-060 reproduced a later-created Testnet Mobile Wallet missing from an acknowledged server backup and from recovery with the correct Recovery Key. The enabled-backup gate skipped incremental writes when no full backup was pending. Permanent loss of wallet keys or funds was not established. - -## What changes -- Enabled backups execute incremental mutations, including wallets not yet persisted to Realm. Pending historical repair does not suppress them. -- Upgraded opted-in accounts check the server against local recovery content without automatically replacing the backup. A mismatch prompts the user through Notifications and More → Backup and Recovery → Assisted Server Backup. -- Back Up Now checks again, prepares the snapshot, uploads and verifies by readback. Matching backups require no upload. Retrieval failure is not treated as an empty backup. -- Show real stages, slow-operation explanation, an immediate Back action, and explicit conflict/unverified states. Preserve per-account status across restart; active spinner state is transient. -- Serialize app, vault, label and deletion writes per account; coalesce repeated repair actions and invalidate completion when local mutations arrive. -- Bound stalled requests and overall attempts; chunk AES work while preserving the existing backup format. Never truncate large records. -- Use the current Recovery Key confirmation reducer after successful recovery. Fix three existing UI branches that enabled Assisted Server Backup or displayed success after server-backup failure. - -## Status -Implemented locally in the isolated `keeper-recovery-t060` checkout. Not committed, integrated, deployed or release-approved. Native QA and relay concurrency/label-write limitations remain release gates; see validation.md. Existing backups missing data require the original device to still hold it; this change does not reconstruct arbitrarily missing wallets. + +T-060 reproduced a later-created Testnet Mobile Wallet missing from an acknowledged server backup and from recovery with the correct Recovery Key. Permanent loss of wallet keys or funds was not established. Later source review found that a previously verified Relay revision and selected account alone cannot prove that a prepared automatic mutation represents committed local recovery data. + +## R4 / 2.6.4 scope + +The automatic-backup fix and disposable proof are mandatory R4 work. Critical native iOS/Android backup acceptance also precedes release; physical-device-only follow-up may be recorded separately after release when authorized. Green tests for an earlier repair candidate do not satisfy the automatic-write gate. + +- Persist each wallet, signer, vault, label, node or deletion locally before queuing its automatic cloud effect. A cloud failure must retain the committed local result and a truthful pending backup status; server success is not a prerequisite for local creation or deletion. +- Bind each operation to the originating account/session, committed recovery generation and operation ID, plus its originally captured verified remote base. Reject stale work, including A→B→A, rather than substituting new account data or a newer verification for an older action. +- At the per-account queue head, prove the decrypted payload or deletion target against fresh committed local recovery data. Preserve remote-only records and immutable key/policy identity absent an explicit user decision. +- Use the revisioned v2 Relay contract with atomic compare-and-swap. Prefer one transaction for a logical multi-record operation; any sequential fanout must use each acknowledged new revision, revalidate local proof and stop on the first uncertain/failed step. +- Keep the whole backup pending until complete decrypted readback matches fresh local recovery data at the same committed generation and the final atomic verification commit succeeds. A response, intermediate fanout success or content-only readback after a lost acknowledgement does not complete the originating operation. +- Keep upgrade/login checks read-only. Historical mismatch repair remains explicit in Notifications and More → Backup and Recovery → Assisted Server Backup. Transport/decryption failure is never an empty backup or permission to replace it. +- Preserve per-account consent and status, bounded truthful progress, data format, Recovery Key confirmation and failure feedback. Never truncate recovery records or put private material in diagnostics. + +## Status and evidence boundary + +Audit starting source: b160656218dc6065ad12af9db220c974255bfc23, 13 October 2026. At that baseline, backupPost rejects automatic mutations before network because end-to-end committed-payload proof is missing. Automatic transport may be enabled only after implementation and the required disposable matrix pass; an endpoint name or valid revision is insufficient. + +This document records requirements, not completion of that implementation or proof. See [design](design.md), [implementation tasks](tasks.md), [automatic write readiness](automatic-write-readiness.md) and [current result matrix](automatic-write-validation.md). [Validation](validation.md) preserves September results as historical evidence; they are not exact-head R4 signoff. + +Current-source independent review, critical native checks, compatible Relay source/deployment evidence and release approval remain separate gates. No merge, production action or publication is authorized by this proposal. Existing backups missing data still need a device that holds it; this work does not reconstruct arbitrarily missing wallets. diff --git a/openspec/changes/fix-recovery-backup-freshness/specs/assisted-server-backup/spec.md b/openspec/changes/fix-recovery-backup-freshness/specs/assisted-server-backup/spec.md index 6503d510c2..02158f99e4 100644 --- a/openspec/changes/fix-recovery-backup-freshness/specs/assisted-server-backup/spec.md +++ b/openspec/changes/fix-recovery-backup-freshness/specs/assisted-server-backup/spec.md @@ -1,65 +1,145 @@ ## MODIFIED Requirements -### Requirement: Enabled Assisted Server Backup includes subsequent mutations -The app SHALL upload enabled incremental backup mutations even when no full backup is pending. It SHALL preserve explicit relay rejection semantics and pending repair after transport failure. +### Requirement: Enabled Assisted Server Backup includes committed subsequent mutations +The app SHALL automatically back up enabled recovery-relevant changes only after their successful local commit and operation-specific freshness proof. Pending historical repair SHALL NOT discard the local operation, grant consent or substitute for write proof. Server acceptance SHALL NOT be required to preserve local creation, edit or deletion. #### Scenario: A wallet is created after the initial backup -- **WHEN** an opted-in user creates a Bitcoin wallet on either network after backing up -- **THEN** its encrypted incremental payload is sent before local creation completes -- **AND** a pending historical repair does not skip the new wallet's write - -#### Scenario: Backup is disabled -- **WHEN** the user creates or changes a wallet with Assisted Server Backup disabled -- **THEN** the automatic writer and upgrade check make no backup upload +- **WHEN** an opted-in account creates a Bitcoin wallet on either network after its initial backup +- **THEN** save the wallet locally before queuing its cloud effect +- **AND** capture the committed recovery generation and originating operation/account/session +- **AND** prove its encrypted recovery content against fresh committed local data at queue head before a revisioned write +- **AND** retain the wallet and pending backup state if proof or cloud transport fails + +#### Scenario: A local operation fails or commits only partially +- **WHEN** related local writes fail or have not all committed +- **THEN** do not authorize a prepared automatic cloud mutation +- **AND** retain pending/unverified status without reporting the backup or partial operation complete + +#### Scenario: Backup is disabled or another account did not consent +- **WHEN** local recovery data changes for a disabled or unconsented account +- **THEN** send no automatic backup mutation +- **AND** preserve local data and the other accounts' choices ## ADDED Requirements +### Requirement: Automatic intent proves committed payload and originating session +Every automatic write SHALL bind its operation ID, committed local recovery generation, originating app/account session and originally captured verified remote revision/epoch. At queue head the app SHALL recheck consent, key ownership and current local recovery content. A revision string alone SHALL NOT authorize a write. + +#### Scenario: Local data changes while an action waits +- **WHEN** a queued payload no longer represents its committed local generation or records +- **THEN** reject it before network and keep backup pending +- **AND** do not retry its ciphertext or silently grant it a newly verified base + +#### Scenario: The user switches A to B and back to A +- **WHEN** work captured in the first A session resumes after A→B→A +- **THEN** reject the old session before mutation or verified-state commit +- **AND** never select B's encryption key, destination or local persistence for A + +#### Scenario: Deletion and identity migration +- **WHEN** a committed deletion or signer-map migration is queued +- **THEN** prove exact local absence/new identity and expected remote target +- **AND** preserve retained vault recoverability and validate exact KeyUID map entries and affected references + +### Requirement: Revisioned mutations preserve complete remote recovery content +The app SHALL validate the v2 snapshot and use an atomic expectedRevision precondition through revisionedBackupMutation. A positive acknowledgement SHALL include updated: true, preconditionApplied: true and a valid newRevision. The app SHALL NOT fall back to old incremental or full replacement endpoints after protocol, conflict or transport failure. + +#### Scenario: Remote-only or immutable conflicting content exists +- **WHEN** the proposed effect would remove remote-only recovery records or change immutable key, derivation, network, script or policy identity without an explicit user decision +- **THEN** preserve the remote backup and report conflict without sending that effect + +#### Scenario: Another server writer wins +- **WHEN** the expected revision is stale or the Relay returns 409 +- **THEN** stop the originating operation, preserve local data and leave backup pending +- **AND** re-read before preparing any new write + +#### Scenario: The server is old or its response is malformed +- **WHEN** the v2 route returns 404 or acknowledgement/capability/revision validation fails +- **THEN** send no legacy fallback and show pending/unverified feedback + +### Requirement: Logical fanout never completes on an intermediate response +A logical multi-record operation SHALL use one atomic revisioned transaction or a serialized sequence that revalidates current local proof and uses each preceding acknowledged newRevision. The whole backup SHALL remain pending until every required effect and final readback complete. + +#### Scenario: Registration affects several vaults +- **WHEN** a persisted VaultSigner registration must update multiple vault images +- **THEN** preserve and propagate the committed registration metadata to every affected vault +- **AND** verify complete readback rather than marking success after the first update + +#### Scenario: A later fanout member fails +- **WHEN** a prior member succeeds but a later member fails or loses its acknowledgement +- **THEN** stop all remaining prepared members, retain local results and keep pending +- **AND** do not replay the prepared fanout or claim rollback/verified success + ### Requirement: Upgrade repair requires comparison and user action -The app SHALL compare local recovery content with a decrypted server snapshot for an opted-in unverified account. Retrieval or decryption errors SHALL NOT be interpreted as a mismatch or empty backup. +The app SHALL compare local recovery content with a complete decrypted server snapshot for an opted-in unverified account. Retrieval, validation or decryption errors SHALL NOT be interpreted as mismatch or empty backup. #### Scenario: Old backup omits local data -- **WHEN** comparison detects recovery data present locally but missing from the backup +- **WHEN** comparison detects recovery data present locally but missing remotely - **THEN** show a mismatch and Back Up Now through the existing backup area -- **AND** do not replace the backup until the user starts repair +- **AND** do not replace the backup until the user explicitly starts repair #### Scenario: Backups match -- **WHEN** canonical recovery content matches -- **THEN** mark that account verified without uploading - -#### Scenario: Server-only data or conflicting recovery identity -- **WHEN** comparison detects server-only records, different keys or address counters ahead of this device -- **THEN** preserve the server backup and show the conflict state without upload +- **WHEN** complete canonical content matches fresh local recovery data and current account/session/generation/protocol checks pass +- **THEN** mark that account verified without an upload -### Requirement: Completion means verified recovery content -The app SHALL read back the upload and compare it with the intended snapshot and current local data. It SHALL invalidate completion if a local backup mutation occurs during the operation. A server acknowledgement alone SHALL NOT mark repair complete. +### Requirement: Completion means complete verified recovery content +After all acknowledged effects the app SHALL read back, decrypt and validate the complete backup, compare fresh local content at the same committed generation, and atomically recheck ownership/session/consent/generation before setting verified. A server acknowledgement alone SHALL NOT complete backup. -#### Scenario: An upload response is lost -- **WHEN** the request fails after it may have reached the server -- **THEN** attempt readback within the deadline -- **AND** report verified only if the recovery data matches, otherwise leave unverified +#### Scenario: An acknowledgement is lost +- **WHEN** a write fails after it may have committed on the server +- **THEN** stop the operation and read back within bounded limits without replaying its ciphertext +- **AND** do not mark that originating write or fanout complete from a content-only match +- **AND** allow a later independent read-only reconciliation to establish a fresh baseline only after all current checks pass -#### Scenario: Another local wallet is created during repair -- **WHEN** a mutation arrives during full backup -- **THEN** serialize the incremental write and keep repair unverified until a subsequent check includes it +#### Scenario: A local mutation arrives before final completion +- **WHEN** local recovery data changes during upload, readback or immediately before the Redux verification commit +- **THEN** reject the completion claim and retain pending until a fresh comparison includes committed data #### Scenario: The app restarts or changes accounts -- **WHEN** an operation is interrupted or the active account changes -- **THEN** retain unverified state for the original account without clearing another account's pending status +- **WHEN** an operation is interrupted or its selected account/session changes +- **THEN** preserve pending/unverified state for the original account without clearing another account +- **AND** reconstruct fresh work after restart rather than replaying a prepared old payload + +### Requirement: Whole-backup deletion uses explicit same-account consent +deleteBackup SHALL require an explicit opt-out for the same selected account/session and validated remote revision. It SHALL preserve consent until a positive revisioned acknowledgement and treat lost responses as uncertain. + +#### Scenario: Remote deletion acknowledgement is lost +- **WHEN** explicit opt-out may have deleted the backup but no valid acknowledgement arrived +- **THEN** do not claim deletion or consent-change success +- **AND** preserve local recovery data and reconcile by readback before another deletion attempt -### Requirement: Repair has bounded, accessible progress -The repair screen SHALL expose real checking, preparing, uploading and verifying stages, a slow-operation explanation after ten seconds, and a Back action throughout. It SHALL NOT claim background execution or rollback on navigation. Stalled transport SHALL be aborted, repeated repair actions coalesced, and oversized records rejected without truncation. +### Requirement: Repair has bounded accessible progress +The repair screen SHALL expose truthful checking, preparing, uploading and verifying stages, slow-operation explanation after ten seconds and Back throughout. It SHALL NOT claim background execution or rollback. Stalled transport SHALL abort, repeated repair requests SHALL coalesce, and oversized records SHALL fail without truncation. -#### Scenario: The server stops responding -- **WHEN** no upload/download bytes advance for thirty seconds -- **THEN** abort the transport and return an unverified state with manual checking available +#### Scenario: Transfer stops progressing +- **WHEN** upload/download bytes do not advance for thirty seconds +- **THEN** abort transport and retain pending/unverified status with manual checking available +- **AND** repeated equal progress values do not reset the stall clock -#### Scenario: The user leaves the waiting screen +#### Scenario: The user leaves progress - **WHEN** Back is selected during repair -- **THEN** navigation remains available and operation status can be revisited in Assisted Server Backup +- **THEN** keep navigation available and let status be revisited in Assisted Server Backup +- **AND** preserve truthful durable state if the OS interrupts the operation ### Requirement: Failed server backup is not shown as success -Existing backup screens SHALL NOT enable Assisted Server Backup or show server-backup success following backupAllFailure. Physical Recovery Key confirmation remains a separate state. +Existing backup screens SHALL NOT enable Assisted Server Backup or display success following backup failure. Recovery Key confirmation SHALL remain a separate state. #### Scenario: Server backup fails after Recovery Key confirmation -- **WHEN** the user confirmed the Recovery Key but server verification fails -- **THEN** retain the physical confirmation and show backup-not-verified feedback without falsely enabling the server backup setting +- **WHEN** the Recovery Key is confirmed but server verification fails +- **THEN** retain physical confirmation and show backup-not-verified feedback without falsely enabling backup + +### Requirement: R4 includes current disposable and critical native proof +The automatic-backup fix SHALL pass the current all-seven-operation disposable matrix, fanout/uncertain-response/final-readback safety proof and critical native IOS and ANDROID backup acceptance before release. Historical repair fixtures or native runs SHALL NOT substitute for exact-source automatic-write proof. + +#### Scenario: Only an older repair or desktop fixture passed +- **WHEN** evidence does not exercise the final automatic producers, committed-generation/session proof and compatible transactional Relay +- **THEN** leave the automatic-backup gate open and identify required exact-source checks + +#### Scenario: Physical-device-only tests are unavailable +- **WHEN** an explicitly physical-only check cannot run before release +- **THEN** record that separate follow-up honestly +- **AND** do not defer required disposable or critical simulator/emulator backup create/edit/delete/relaunch/readback/restore checks + +#### Scenario: A clean Recovery Key restore is tested +- **WHEN** disposable backup content produced by the current automatic pipeline is restored on IOS or ANDROID +- **THEN** verify all intended wallets/networks, keys, active/archived vaults, registrations, labels and nodes +- **AND** fail truthfully on missing/corrupt/unavailable data or interrupted local persistence rather than accepting an empty/partial restore diff --git a/openspec/changes/fix-recovery-backup-freshness/tasks.md b/openspec/changes/fix-recovery-backup-freshness/tasks.md index 94fd60121a..ac3757371c 100644 --- a/openspec/changes/fix-recovery-backup-freshness/tasks.md +++ b/openspec/changes/fix-recovery-backup-freshness/tasks.md @@ -1,27 +1,48 @@ -## Implementation -- [x] Fix enabled incremental backup gate and preserve explicit subscription rejection. -- [x] Check upgraded opted-in accounts without an unsolicited replacement upload. -- [x] Implement comparison, prompt, repair stages, readback verification and manual retry. -- [x] Include both Bitcoin networks, USDT, keys, vaults, labels and nodes; protect detected server-only/conflicting data. -- [x] Persist account-specific verification and interruption state separately from transient running state. -- [x] Serialize local backup writes, coalesce repair requests and invalidate completion during mutations/account switch. -- [x] Add transport abort, stall/deadline bounds, slow status, leave-screen affordance and bounded/chunked crypto work. -- [x] Fix recovered Recovery Key confirmation and existing backup UI failure-as-success branches. - -## Verification -- [x] Run targeted production-code recovery/repair/transport/state/UI feedback tests with disposable adapters. -- [x] Run the existing Recovery Key migration/status suite. -- [x] Bundle Android JavaScript and inspect baseline versus candidate TypeScript diagnostics. -- [x] Lint all new modules/screen with the missing Jest plugin supplied to the existing ESLint configuration. -- [x] Bundle iOS JavaScript. -- [x] Run native Android/iOS upgrade and clean recovery using disposable data against a working backend. Final consent follow-up is recorded separately in validation.md. -- [ ] Measure small-screen and increased-text layout, crypto frame time, memory, throughput, foreground/background and kill/restart. -- [ ] Validate deployed request-size limits and approve/tune provisional record, transfer and deadline bounds. - -## Release gates -- [x] Implement and test revision-checked atomic repair on the paired relay. -- [x] Protect repaired backups from legacy full replacement, including transaction retries and unchanged-ciphertext races. -- [ ] Validate production transaction topology and relay-first deployment. -- [x] Fix same-ID label-content upserts in the paired relay candidate; verify against real MongoDB and HTTP. -- [ ] Resolve ordinary CLI lint's inherited Jest environment/plugin error. -- [ ] Review and integrate only after applicable checks pass; commit/push/release require established owner approval. +# R4 automatic Assisted Backup tasks + +Reconciled 13 October 2026 at audit starting app source b160656218dc6065ad12af9db220c974255bfc23. The earlier checked checklist described a September candidate, including pre-persistence writes; it did not prove today's automatic-write contract. Historical results remain in [validation](validation.md) and unchanged evidence files. Check a current task only after exact-source implementation/proof is linked in [automatic-write-validation](automatic-write-validation.md). + +## Local-first implementation + +- [ ] Give each logical recovery operation an originating account/session, operation ID and monotonic generation captured after all related local writes commit. +- [ ] Reorder wallet/signer/vault creation, edits and deletions so local persistence precedes cloud effects; cloud failure preserves local results and durable pending state. +- [ ] Prove all seven operation payloads/deletion targets against fresh committed Realm data at queue head with real decrypt/identity validation. +- [ ] Reject stale generation, failed/partial commit, wrong owner/key/consent and A→B→A without network or later substitution of a newer verified base. +- [ ] Cover account-bound node/version writes, exact-KeyUID migration, USDT direct deletion, delayed import callbacks and shared registration propagation. +- [ ] Handle explicit whole-backup opt-out separately; retain consent until positive revisioned acknowledgement and reconcile lost responses. + +## Server sequencing and verification + +- [ ] Validate v2 snapshot/capabilities and preserve remote-only/conflicting recovery content before automatic send. +- [ ] Enforce atomic expectedRevision with positive precondition acknowledgement and valid newRevision; prohibit legacy fallback. +- [ ] Use one atomic fanout transaction or a serialized acknowledged revision chain, revalidating local proof for each member. +- [ ] Stop fanout on 409/404/protocol/network/timeout/lost response; do not replay prepared ciphertext or claim intermediate success. +- [ ] Complete and decrypt server readback, then compare fresh local data at the same committed generation and account/session. +- [ ] Make the final Redux verified transition reject a local mutation injected immediately before completion; preserve pending across interruption/restart. + +## Mandatory R4 disposable proof + +- [ ] Exercise each of seven production saga/hook/transport operation entry points with real encryption and disposable Realm + transactional Relay fixtures. +- [ ] Record valid committed write once, stale data/commit rejection, A→B→A, remote-only/conflicting snapshot, 409/old-route 404/malformed acknowledgement, timeout/lost response and complete readback outcomes per operation. +- [ ] Prove vault creation+archive, multi-vault registration and label add/delete fanouts, including successful first member then failure and no later prepared send. +- [ ] Prove disabled/unconsented accounts send zero writes and successful local create/edit/delete survives cloud failure. +- [ ] Prove clean Recovery Key restoration from the newly produced backup, validating all intended records and key/policy/registration identity. +- [ ] Run revisioned Relay HTTP/transaction tests against a disposable replica set, including old-writer fences and competing writers at exact source. +- [ ] Link exact app/backend SHA, commands, fixture provenance, pass/fail/blocked counts and redacted artifacts in the current matrix; prior logs are not replacements. + +## Mandatory critical native backup acceptance + +- [ ] IOS: exact-source native build/launch; opted-in automatic create/edit/delete, relaunch persistence, check/readback and clean disposable Recovery Key restore. +- [ ] ANDROID: the same exact-source create/edit/delete, relaunch, readback and clean restore checks. +- [ ] Both platforms: legacy snapshot upgrade, failed/stale/absent check truthfulness, account/consent switching and A→B→A, interrupted/uncertain write/retry, no false verified/empty restore or automatic replacement. +- [ ] Both platforms: local mutation during pending upload/final readback remains pending; interrupted recovery never claims complete; record platform-specific outcomes rather than assuming parity. +- [ ] Record physical-device-only follow-up separately when unavailable; do not use it to defer required fixtures or critical simulator/emulator checks. + +## Remaining delivery and NFR gates + +- [ ] Independent final-source developer review and current-head CI, with inherited lint/type failures disclosed. +- [ ] Native small-width/enlarged-text, large/single-record frame time, memory, throughput and slow/background cases; distinguish mandatory critical safety failures from explicitly accepted physical-only follow-up. +- [ ] Verify deployed request limits and transaction topology, compatible running Relay artifact and old-writer drain/rollout under existing operator authorization. +- [ ] Refresh store histories/counters and source/artifact manifest; obtain intended merge/signing/submission/deployment/release approval separately. + +Documentation reconciliation does not complete any unchecked implementation, fixture, native or production task. Reproducibility, Bitkey/Recoverable Wallet and unrelated R4 code stay independent. diff --git a/openspec/changes/fix-recovery-backup-freshness/validation.md b/openspec/changes/fix-recovery-backup-freshness/validation.md index b2db3ee842..4bab6be989 100644 --- a/openspec/changes/fix-recovery-backup-freshness/validation.md +++ b/openspec/changes/fix-recovery-backup-freshness/validation.md @@ -1,4 +1,16 @@ -# Recovery candidate validation — 29 September 2026 +# R4 recovery validation — current gates and historical evidence + +## Current automatic-write status — 13 October 2026 + +Audit starting app source: `b160656218dc6065ad12af9db220c974255bfc23`. The automatic fix and disposable proof are mandatory R4 requirements; critical native iOS/Android backup acceptance also precedes release. Physical-device-only follow-up does not waive the fixture/native safety checks. + +Current exact-source result placeholders and evidence links live in [automatic-write-validation.md](automatic-write-validation.md). No new implementation, fixture, native or deployment pass is claimed by this documentation reconciliation. Populate the exact app/backend source, commands, outcome counts and redacted artifacts from coordinator evidence before checking [tasks](tasks.md). + +The baseline automatic transport fails closed before network until end-to-end local commit/generation, originating-session/payload proof, revisioned fanout and final complete readback are implemented and proved. Earlier success of manual repair or a pre-persistence automatic writer does not establish readiness for this contract. A lost write acknowledgement leaves its originating operation pending; a content-only match is not an acknowledged write or fanout completion. + +## Historical recovery candidate — 29 September 2026 + +The material below preserves prior recorded results and their original source/artifact boundaries. It has not been rerun in this reconciliation. References to local uncommitted source, earlier versions/counters, native completion or remaining gates describe that September candidate, not the final R4 source. In particular, older tests requiring upload before local commit are superseded by the local-first model; their logs remain unchanged as provenance. Local, uncommitted public source at `/Users/devaccount/Workspace/keeper-recovery-t060`, based on `7fdd502702a8aa149f5a3ad87aac7a274a61e6e8`. Paired relay source at `/Users/devaccount/Workspace/keeper-recovery-relay`, based on `5551550d7ba0e84ce239a7f154f34613081c5c20`. No production deployment, commit, push or store submission has occurred. T-060 remains Now. From e9d3a84b385e674bbb65309f8cf0d2209d51ada6 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Tue, 13 Oct 2026 03:17:24 +0000 Subject: [PATCH 148/254] fix(backup): commit launch upgrades as one local operation --- src/store/sagas/upgrade.ts | 643 +++++++++--------- tests/recovery/upgradeAccountSession.test.cjs | 356 +++++----- 2 files changed, 509 insertions(+), 490 deletions(-) diff --git a/src/store/sagas/upgrade.ts b/src/store/sagas/upgrade.ts index b0abc8bd3c..fe67f77f67 100644 --- a/src/store/sagas/upgrade.ts +++ b/src/store/sagas/upgrade.ts @@ -1,8 +1,8 @@ import { call, put, select } from 'redux-saga/effects'; +import Realm from 'realm'; import semver from 'semver'; import dbManager from 'src/storage/realm/dbManager'; import { RealmSchema } from 'src/storage/realm/enum'; -import Relay from 'src/services/backend/Relay'; import DeviceInfo from 'react-native-device-info'; import { getReleaseTopic } from 'src/utils/releaseTopic'; import { getApp } from '@react-native-firebase/app'; @@ -30,18 +30,20 @@ import { hcStatusType } from 'src/models/interfaces/HeathCheckTypes'; import { getKeyUID } from 'src/utils/utilities'; import WalletUtilities from 'src/services/wallets/operations/utils'; import { - updateVersionHistory, - UPDATE_VERSION_HISTORY, - migrateLabelsToBip329, - MIGRATE_LABELS_329, -} from '../sagaActions/upgrade'; -import { updateAppImageWorker, updateVaultImageWorker } from './bhr'; + currentBackupAccountSession, + notifyBackupReconciliation, +} from 'src/services/backup/transport'; +import { + beginBackupLocalOperation, + commitBackupLocalOperation, + enqueueAutomaticBackupOperation, +} from 'src/services/backup/automatic'; +import type { BackupWriteIntent } from 'src/services/backup/intent'; +import { UPDATE_VERSION_HISTORY, MIGRATE_LABELS_329 } from '../sagaActions/upgrade'; +import { buildAutomaticAppMutation, buildAutomaticVaultMutation } from './bhr'; import { createWatcher } from '../utilities'; import { setAppVersion } from '../reducers/storage'; -import { isAutomaticCloudBackupEnabled, setPendingAllBackup } from '../reducers/bhr'; import { RootState, store } from '../store'; -import { currentBackupAccountSession } from 'src/services/backup/transport'; -import type { BackupWriteIntent } from 'src/services/backup/intent'; export const LABELS_INTRODUCTION_VERSION = '1.0.4'; export const BIP329_INTRODUCTION_VERSION = '1.0.7'; @@ -56,6 +58,108 @@ const upgradeAccountIsActive = (appId?: string, accountSession?: number) => store.getState().storage.appId === appId && (dbManager.getObjectByIndex(RealmSchema.KeeperApp) as unknown as KeeperApp)?.id === appId; +type UpgradeGroup = { + appId: string; + session: number; + operation: ReturnType; + writes: Array<(realm: Realm) => void>; + appImage: boolean; + vaultImages: boolean; + labelIds: Set; +}; + +function beginUpgradeGroup( + appId: string, + session: number, + intent?: BackupWriteIntent +): UpgradeGroup { + return { + appId, + session, + operation: beginBackupLocalOperation(appId, intent, session), + writes: [], + appImage: false, + vaultImages: false, + labelIds: new Set(), + }; +} + +// Preserve one originating base for the whole launch upgrade. Async work is +// staged first, and a failed local write rolls back every migration in the group. +async function completeUpgradeGroup(group: UpgradeGroup) { + if (!upgradeAccountIsActive(group.appId, group.session)) return false; + const receipt = commitBackupLocalOperation(group.operation, (realm) => { + group.writes.forEach((write) => write(realm)); + }); + if (!receipt.committed || !upgradeAccountIsActive(group.appId, group.session)) return false; + try { + const mutations: Parameters[1] = []; + if (group.appImage) + mutations.push( + buildAutomaticAppMutation( + { + wallets: dbManager.getCollection(RealmSchema.Wallet) as unknown as Wallet[], + signers: dbManager.getCollection(RealmSchema.Signer) as unknown as Signer[], + }, + group.appId + ) + ); + if (group.vaultImages) { + for (const vault of dbManager.getCollection(RealmSchema.Vault) as unknown as Vault[]) + mutations.push(buildAutomaticVaultMutation(vault.id, group.appId, true)); + } + if (group.labelIds.size) { + const app = dbManager.getObjectByIndex(RealmSchema.KeeperApp) as unknown as KeeperApp; + if (app?.id !== group.appId) return false; + const key = generateEncryptionKey(app.primarySeed); + const addLabels = (dbManager.getCollection(RealmSchema.Tags) as unknown as BIP329Label[]) + .filter((tag) => group.labelIds.has(tag.id)) + .map((tag) => ({ + id: hash256(hash256(key + tag.id)), + content: encrypt(key, JSON.stringify(tag)), + })); + if (addLabels.length !== group.labelIds.size) throw new Error('Migrated labels unavailable'); + mutations.push({ + operation: 'modifyLabels', + body: { appId: group.appId, addLabels, deleteLabels: [] }, + }); + } + if (mutations.length) await enqueueAutomaticBackupOperation(receipt, mutations); + } catch { + notifyBackupReconciliation(group.appId); + } + // Local success does not assert a cloud acknowledgement. The queue preserves + // stale/no-baseline/offline local data and leaves its backup pending. + return true; +} + +function stageVersionUpdate( + group: UpgradeGroup, + previousVersion: string, + newVersion: string, + isRecovery: boolean +) { + const history = { + version: `${newVersion}(${DeviceInfo.getBuildNumber()})`, + date: new Date().toString(), + title: `Upgraded from ${previousVersion} to ${newVersion}`, + }; + group.writes.push((realm) => { + const app = realm.objectForPrimaryKey(RealmSchema.KeeperApp, group.appId); + if (!app) throw new Error('Upgrade account unavailable'); + app.version = newVersion; + // Recovery records its own single Recovered Wallet entry. + if (!isRecovery) realm.create(RealmSchema.VersionHistory, history, Realm.UpdateMode.Modified); + }); + group.appImage = true; +} + +function updateReleaseTopics(previousVersion: string, newVersion: string) { + const messaging = getMessaging(getApp()); + unsubscribeFromTopic(messaging, getReleaseTopic(previousVersion)); + subscribeToTopic(messaging, getReleaseTopic(newVersion)); +} + export function* applyUpgradeSequence({ previousVersion, newVersion, @@ -69,46 +173,56 @@ export function* applyUpgradeSequence({ originBackupAccountSession?: number; originBackupWriteIntent?: BackupWriteIntent; }) { - const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); - if (requestSession !== currentBackupAccountSession()) return; - console.log(`applying upgrade sequence - from: ${previousVersion} to ${newVersion}`); - const { id: originAppId }: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); - if (!upgradeAccountIsActive(originAppId, requestSession)) return; - - if ( - semver.gte(previousVersion, LABELS_INTRODUCTION_VERSION) && - semver.lt(previousVersion, BIP329_INTRODUCTION_VERSION) - ) { - yield put({ ...migrateLabelsToBip329(originAppId), originBackupAccountSession: requestSession, originBackupWriteIntent }); - } - - if (semver.lt(previousVersion, KEY_MANAGEMENT_VERSION)) { - yield call(migrateStructureforSignersInAppImage, originAppId, requestSession, originBackupWriteIntent); - yield call(migrateStructureforVaultInAppImage, originAppId, requestSession, originBackupWriteIntent); - } - if (semver.lt(previousVersion, APP_KEY_UPGRADE_VERSION)) - yield call(updateAppKeysToEnableSigning, originAppId, requestSession, originBackupWriteIntent); - - if (semver.lt(previousVersion, HEALTH_CHECK_TIMELINE_MIGRATION_VERSION)) { - if (!upgradeAccountIsActive(originAppId, requestSession)) return; - yield call(healthCheckTimelineMigration); - } - - if (semver.lt(previousVersion, SIGNER_POLICY_MIGRATION_VERSION)) { - if (!upgradeAccountIsActive(originAppId, requestSession)) return; - yield call(migrateServerKeyPolicy, originAppId, requestSession); - const app: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); - if (app?.id === originAppId && upgradeAccountIsActive(originAppId, requestSession)) - yield put(setPendingAllBackup({ appId: originAppId, pending: true })); + const session = originBackupAccountSession ?? currentBackupAccountSession(); + if (session !== currentBackupAccountSession()) return false; + const appId = originBackupWriteIntent?.appId || store.getState().storage.appId; + if (!upgradeAccountIsActive(appId, session)) return false; + try { + const group = beginUpgradeGroup(appId, session, originBackupWriteIntent); + if ( + semver.gte(previousVersion, LABELS_INTRODUCTION_VERSION) && + semver.lt(previousVersion, BIP329_INTRODUCTION_VERSION) + ) + yield call(migrateLablesWorker, { + originAppId: appId, + originBackupAccountSession: session, + originBackupWriteIntent, + upgradeGroup: group, + }); + if (semver.lt(previousVersion, KEY_MANAGEMENT_VERSION)) { + yield call( + migrateStructureforSignersInAppImage, + appId, + session, + originBackupWriteIntent, + group + ); + yield call( + migrateStructureforVaultInAppImage, + appId, + session, + originBackupWriteIntent, + group + ); + } + if (semver.lt(previousVersion, APP_KEY_UPGRADE_VERSION)) + yield call(updateAppKeysToEnableSigning, appId, session, originBackupWriteIntent, group); + if (semver.lt(previousVersion, HEALTH_CHECK_TIMELINE_MIGRATION_VERSION)) + yield call(healthCheckTimelineMigration, appId, session, originBackupWriteIntent, group); + if (semver.lt(previousVersion, SIGNER_POLICY_MIGRATION_VERSION)) + yield call(migrateServerKeyPolicy, appId, session, originBackupWriteIntent, group); + if (!upgradeAccountIsActive(appId, session)) return false; + stageVersionUpdate(group, previousVersion, newVersion, isRecovery); + const committed = yield call(completeUpgradeGroup, group); + if (!committed || !upgradeAccountIsActive(appId, session)) return false; + yield put(setAppVersion(newVersion)); + if (!upgradeAccountIsActive(appId, session)) return false; + updateReleaseTopics(previousVersion, newVersion); + return true; + } catch (error) { + captureError(error); + return false; } - - if (!upgradeAccountIsActive(originAppId, requestSession)) return; - yield put(setAppVersion(newVersion)); - yield put({ - ...updateVersionHistory(previousVersion, newVersion, isRecovery, originAppId), - originBackupAccountSession: requestSession, - originBackupWriteIntent, - }); } export function* updateVersionHistoryWorker({ @@ -123,34 +237,18 @@ export function* updateVersionHistoryWorker({ originBackupWriteIntent?: BackupWriteIntent; }) { const { previousVersion, newVersion, isRecovery = false } = payload; - const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); + const session = originBackupAccountSession ?? currentBackupAccountSession(); try { - const app: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); - if (app?.id !== originAppId || !upgradeAccountIsActive(originAppId, requestSession)) return; - const enabled = yield select((state: RootState) => - state.storage.appId === originAppId && isAutomaticCloudBackupEnabled(state.bhr, originAppId) - ); - if (enabled && upgradeAccountIsActive(originAppId, requestSession)) { - yield call(Relay.updateAppImage, { - appId: originAppId, - version: newVersion, - }, originBackupWriteIntent); - } - if (!upgradeAccountIsActive(originAppId, requestSession)) return; - // recoverApp already records the single "Recovered Wallet" entry. - if (!isRecovery) { - yield call(dbManager.createObject, RealmSchema.VersionHistory, { - version: `${newVersion}(${DeviceInfo.getBuildNumber()})`, - date: new Date().toString(), - title: `Upgraded from ${previousVersion} to ${newVersion}`, - }); - } - const firebaseApp = getApp(); - const messagingInstance = getMessaging(firebaseApp); - unsubscribeFromTopic(messagingInstance, getReleaseTopic(previousVersion)); - subscribeToTopic(messagingInstance, getReleaseTopic(newVersion)); + if (!upgradeAccountIsActive(originAppId, session)) return false; + const group = beginUpgradeGroup(originAppId, session, originBackupWriteIntent); + stageVersionUpdate(group, previousVersion, newVersion, isRecovery); + const committed = yield call(completeUpgradeGroup, group); + if (!committed || !upgradeAccountIsActive(originAppId, session)) return false; + updateReleaseTopics(previousVersion, newVersion); + return true; } catch (error) { - console.log({ error }); + captureError(error); + return false; } } @@ -163,298 +261,213 @@ function* migrateLablesWorker({ originAppId, originBackupAccountSession, originBackupWriteIntent, + upgradeGroup, }: { originAppId?: string; originBackupAccountSession?: number; originBackupWriteIntent?: BackupWriteIntent; + upgradeGroup?: UpgradeGroup; }) { - const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); - try { - if (!upgradeAccountIsActive(originAppId, requestSession)) return; - const UTXOLabels: UTXOInfo[] = yield call(dbManager.getCollection, RealmSchema.UTXOInfo); - const tags = []; - const wallets: Wallet[] = yield call(dbManager.getCollection, RealmSchema.Wallet); - - UTXOLabels.forEach((utxo) => { - if (utxo.labels.length) { - const wallet = wallets.find((w) => w.id === utxo.walletId); - const origin = generateAbbreviatedOutputDescriptors(wallet); - utxo.labels.forEach((label) => { - const ref = `${utxo.txId}:${utxo.vout}`; - const labelName = label.name; - const tag: BIP329Label = { - id: `${ref}${labelName}`, - type: LabelRefType.OUTPUT, - isSystem: false, - label: labelName, - ref, - origin, - }; - tags.push(tag); - }); - } - }); - - if (tags.length) { - if (!upgradeAccountIsActive(originAppId, requestSession)) return; - yield call(dbManager.createObjectBulk, RealmSchema.Tags, tags); - - const { id, primarySeed }: KeeperApp = yield call( - dbManager.getObjectByIndex, - RealmSchema.KeeperApp - ); - if (id !== originAppId) return; - const encryptionKey = generateEncryptionKey(primarySeed); - const tagsToBackup = tags.map((tag) => ({ - id: hash256(hash256(encryptionKey + tag.id)), - content: encrypt(encryptionKey, JSON.stringify(tag)), - })); - if (!upgradeAccountIsActive(originAppId, requestSession)) return; - const enabled = yield select((state: RootState) => - state.storage.appId === originAppId && isAutomaticCloudBackupEnabled(state.bhr, originAppId) - ); - if (enabled && upgradeAccountIsActive(originAppId, requestSession)) - yield call(Relay.modifyLabels, id, tagsToBackup, [], originBackupWriteIntent); + const session = originBackupAccountSession ?? currentBackupAccountSession(); + if (!upgradeAccountIsActive(originAppId, session)) return false; + const group = upgradeGroup || beginUpgradeGroup(originAppId, session, originBackupWriteIntent); + const utxos: UTXOInfo[] = yield call(dbManager.getCollection, RealmSchema.UTXOInfo); + const wallets: Wallet[] = yield call(dbManager.getCollection, RealmSchema.Wallet); + if (!upgradeAccountIsActive(originAppId, session)) return false; + const tags: BIP329Label[] = []; + for (const utxo of utxos.filter((value) => value.labels.length)) { + const wallet = wallets.find((value) => value.id === utxo.walletId); + if (!wallet) throw new Error('Label migration wallet unavailable'); + const origin = generateAbbreviatedOutputDescriptors(wallet); + for (const label of utxo.labels) { + const ref = `${utxo.txId}:${utxo.vout}`; + tags.push({ + id: `${ref}${label.name}`, + type: LabelRefType.OUTPUT, + isSystem: false, + label: label.name, + ref, + origin, + }); } - } catch (error) { - console.log({ error }); } + if (tags.length) { + group.writes.push((realm) => { + tags.forEach((tag) => realm.create(RealmSchema.Tags, tag, Realm.UpdateMode.Modified)); + }); + tags.forEach((tag) => group.labelIds.add(tag.id)); + } + return upgradeGroup ? true : yield call(completeUpgradeGroup, group); } export const migrateLablesWatcher = createWatcher(migrateLablesWorker, MIGRATE_LABELS_329); function* migrateStructureforSignersInAppImage( - originAppId: string, - requestSession: number, - originBackupWriteIntent?: BackupWriteIntent + appId: string, + session: number, + intent?: BackupWriteIntent, + parent?: UpgradeGroup ) { - try { - if (!upgradeAccountIsActive(originAppId, requestSession)) return; - const wallets = yield call(dbManager.getCollection, RealmSchema.Wallet); - const signers = yield call(dbManager.getCollection, RealmSchema.Signer); - const response = yield call(updateAppImageWorker, { - payload: { wallets, signers }, - originAppId, - originBackupAccountSession: requestSession, - originBackupWriteIntent, - }); - if (response.updated) { - console.log('Updated the Signers in app image'); - } else { - console.log('Failed to update the update the app image with the updated the structure'); - } - } catch (err) {} + if (!upgradeAccountIsActive(appId, session)) return false; + const group = parent || beginUpgradeGroup(appId, session, intent); + group.appImage = true; + return parent ? true : yield call(completeUpgradeGroup, group); } function* migrateStructureforVaultInAppImage( - originAppId: string, - requestSession: number, - originBackupWriteIntent?: BackupWriteIntent + appId: string, + session: number, + intent?: BackupWriteIntent, + parent?: UpgradeGroup ) { - try { - if (!upgradeAccountIsActive(originAppId, requestSession)) return; - const vaults: Vault[] = yield call(dbManager.getCollection, RealmSchema.Vault); - const activeVault: Vault = vaults.filter((vault) => !vault.archived)[0] || null; - - yield call(updateVaultImageWorker, { - payload: { isUpdate: true, vault: activeVault }, - originAppId, - originBackupAccountSession: requestSession, - originBackupWriteIntent, - }); - } catch (err) { - console.log('Something went wrong in updating the vault image', err); - } + if (!upgradeAccountIsActive(appId, session)) return false; + const group = parent || beginUpgradeGroup(appId, session, intent); + group.vaultImages = true; + return parent ? true : yield call(completeUpgradeGroup, group); } -// This function updates app keys/mobile keys to enable signing by generating and associating extended keys. function* updateAppKeysToEnableSigning( - originAppId: string, - requestSession: number, - originBackupWriteIntent?: BackupWriteIntent + appId: string, + session: number, + intent?: BackupWriteIntent, + parent?: UpgradeGroup ) { - try { - if (!upgradeAccountIsActive(originAppId, requestSession)) return; - const wallets = yield call(dbManager.getCollection, RealmSchema.Wallet); - const signers = yield call(dbManager.getCollection, RealmSchema.Signer); - const keeperSigners = signers.filter( - (signer) => - signer.type === SignerType.KEEPER || - signer.type === SignerType.MOBILE_KEY || - signer.type === SignerType.MY_KEEPER - ); - const { appKeyWalletMap, myAppKeySigners } = mapAppKeysToWallets(wallets, keeperSigners); - const extendedKeyMap = generateExtendedKeysForSigners(myAppKeySigners, appKeyWalletMap); - if (!upgradeAccountIsActive(originAppId, requestSession)) return; - updateVaultSigners(extendedKeyMap, signers); - updateSignerDetails(myAppKeySigners, extendedKeyMap); - const response = yield call(updateAppImageWorker, { - payload: { signers }, - originAppId, - originBackupAccountSession: requestSession, - originBackupWriteIntent, - }); - if (response.updated) { - console.log('Updated the Signers in app image'); - } else { - console.log('Failed to update the update the app image with the updated the structure'); - } - } catch (err) { - console.log('Error updating mobile keys', err); - captureError(err); - } + if (!upgradeAccountIsActive(appId, session)) return false; + const group = parent || beginUpgradeGroup(appId, session, intent); + const wallets = yield call(dbManager.getCollection, RealmSchema.Wallet); + const signers = yield call(dbManager.getCollection, RealmSchema.Signer); + if (!upgradeAccountIsActive(appId, session)) return false; + const keeperSigners = signers.filter((signer) => + [SignerType.KEEPER, SignerType.MOBILE_KEY, SignerType.MY_KEEPER].includes(signer.type) + ); + const { appKeyWalletMap, myAppKeySigners } = mapAppKeysToWallets(wallets, keeperSigners); + const keys = generateExtendedKeysForSigners(myAppKeySigners, appKeyWalletMap); + group.writes.push((realm) => { + updateVaultSigners(realm, keys, signers); + updateSignerDetails(realm, myAppKeySigners, keys); + }); + group.appImage = true; + group.vaultImages = true; + return parent ? true : yield call(completeUpgradeGroup, group); } function mapAppKeysToWallets(wallets, keeperSigners) { - const appKeyWalletMap = {}; + const walletMap = {}; const myAppKeySigners = keeperSigners.filter((signer) => { - const walletMatch = wallets.find((wallet) => wallet.id === signer.masterFingerprint); - if (walletMatch) { - appKeyWalletMap[signer.masterFingerprint] = walletMatch; - return true; - } - return false; + const wallet = wallets.find((value) => value.id === signer.masterFingerprint); + if (!wallet) return false; + walletMap[signer.masterFingerprint] = wallet; + return true; }); - - return { appKeyWalletMap, myAppKeySigners }; + return { appKeyWalletMap: walletMap, myAppKeySigners }; } -function generateExtendedKeysForSigners(signers, appKeyWalletMap) { - const extendedKeyMap = {}; - signers.forEach((signer) => { - const { mnemonic } = appKeyWalletMap[signer.masterFingerprint].derivationDetails; +function generateExtendedKeysForSigners(signers, walletMap) { + const keys = {}; + for (const signer of signers) { + const { mnemonic } = walletMap[signer.masterFingerprint].derivationDetails; const { extendedKeys } = generateExtendedKeysForCosigner(mnemonic); - extendedKeyMap[signer.masterFingerprint] = extendedKeys; - }); - - return extendedKeyMap; + if (extendedKeys.xpub !== signer.signerXpubs[XpubTypes.P2WSH][0]?.xpub) + throw new Error('Migrated mobile key does not match the saved public key'); + keys[signer.masterFingerprint] = extendedKeys; + } + return keys; } -function updateVaultSigners(extendedKeyMap, signers) { - const signerMap = {}; - signers.forEach((signer) => (signerMap[getKeyUID(signer)] = signer)); - const vaultKeys: VaultSigner[] = dbManager.getCollection(RealmSchema.VaultSigner); - for (const vaultKey of vaultKeys) { +function updateVaultSigners(realm: Realm, keys, signers) { + const signerMap = Object.fromEntries(signers.map((signer) => [getKeyUID(signer), signer])); + for (const vaultKey of realm.objects(RealmSchema.VaultSigner)) { const signer = signerMap[getKeyUID(vaultKey)]; - if (signer && signer.type === SignerType.KEEPER && extendedKeyMap[vaultKey.masterFingerprint]) { - dbManager.updateObjectByPrimaryId(RealmSchema.VaultSigner, 'xpub', vaultKey.xpub, { - xpriv: extendedKeyMap[vaultKey.masterFingerprint].xpriv, - }); + const key = keys[vaultKey.masterFingerprint]; + if (signer?.type === SignerType.KEEPER && key) { + if (key.xpub !== vaultKey.xpub) + throw new Error('Migrated vault key does not match its public key'); + vaultKey.xpriv = key.xpriv; } } } -function updateSignerDetails(signers: Signer[], extendedKeyMap) { - for (let i = 0; i < signers.length; i++) { - const signer = signers[i]; - // Update the signer type to MY_KEEPER and the extended keys - dbManager.updateObjectByPrimaryId( - RealmSchema.Signer, - 'masterFingerprint', - signer.masterFingerprint, - { - type: SignerType.MY_KEEPER, - } - ); - dbManager.updateObjectByPrimaryId( +function updateSignerDetails(realm: Realm, signers: Signer[], keys) { + signers.forEach((signer, index) => { + const stored = realm.objectForPrimaryKey( RealmSchema.Signer, - 'masterFingerprint', - signer.masterFingerprint, - { - signerXpubs: updateSignerXpubs(signer, extendedKeyMap[signer.masterFingerprint].xpriv), - } + signer.id || getKeyUID(signer) ); - if (!signer.extraData?.instanceNumber) { - try { - // Update the signer instance number for MY_KEEPER - dbManager.updateObjectByPrimaryId( - RealmSchema.Signer, - 'masterFingerprint', - signer.masterFingerprint, - { - extraData: { instanceNumber: i + 1 }, - } - ); - } catch (err) { - captureError(err); - // ignore since instance number is not mandatory - } - } - } + const key = keys[signer.masterFingerprint]; + if (!stored || stored.signerXpubs[XpubTypes.P2WSH][0]?.xpub !== key.xpub) + throw new Error('Mobile key changed before upgrade commit'); + stored.type = SignerType.MY_KEEPER; + stored.signerXpubs = updateSignerXpubs(signer, key.xpriv); + if (!stored.extraData?.instanceNumber) + stored.extraData = { ...stored.extraData, instanceNumber: index + 1 }; + }); } function updateSignerXpubs(signer, xpriv) { - // This function would update the signerXpubs structure with the new xpriv, example provided return { ...signer.signerXpubs, - [XpubTypes.P2WSH]: [ - { - ...signer.signerXpubs[XpubTypes.P2WSH][0], - xpriv, - }, - ], + [XpubTypes.P2WSH]: [{ ...signer.signerXpubs[XpubTypes.P2WSH][0], xpriv }], }; } -function* healthCheckTimelineMigration() { - try { - const signers: Signer[] = dbManager.getCollection(RealmSchema.Signer); +function* healthCheckTimelineMigration( + appId: string, + session: number, + intent?: BackupWriteIntent, + parent?: UpgradeGroup +) { + if (!upgradeAccountIsActive(appId, session)) return false; + const group = parent || beginUpgradeGroup(appId, session, intent); + const signers = dbManager.getCollection(RealmSchema.Signer) as unknown as Signer[]; + group.writes.push((realm) => { for (const signer of signers) { - const healthCheckDetails: HealthCheckDetails = { + const stored = realm.objectForPrimaryKey( + RealmSchema.Signer, + signer.id || getKeyUID(signer) + ); + if (!stored) throw new Error('Health-check signer unavailable'); + const detail: HealthCheckDetails = { type: hcStatusType.HEALTH_CHECK_SUCCESSFULL, actionDate: signer.lastHealthCheck, }; - dbManager.updateObjectByPrimaryId( - RealmSchema.Signer, - 'masterFingerprint', - signer.masterFingerprint, - { - healthCheckDetails: [healthCheckDetails], - } - ); + stored.healthCheckDetails = [detail]; } - } catch (err) { - console.log('Error in health check timeline migration:', err); - } + }); + group.appImage = true; + return parent ? true : yield call(completeUpgradeGroup, group); } -function* migrateServerKeyPolicy(originAppId: string, requestSession: number) { - // migrates old server key policy to new time-based spending limit policy - try { - const { bitcoinNetwork } = yield select((state: RootState) => state.settings); - const signers: Signer[] = yield call(dbManager.getCollection, RealmSchema.Signer); - if (!upgradeAccountIsActive(originAppId, requestSession)) return; - - for (const signer of signers) { - if (signer.type === SignerType.POLICY_SERVER && signer.signerPolicy) { - const oldPolicy = signer.signerPolicy; - if (!oldPolicy) { - throw new Error('Migration aborted: policy missing for Server Key instance'); - } - - const id = WalletUtilities.getFingerprintFromExtendedKey( - signer.signerXpubs[XpubTypes.P2WSH][0].xpub, - bitcoinNetwork - ); - - const { newPolicy } = yield call(SigningServer.migrateSignerPolicy, id, oldPolicy); - if (!upgradeAccountIsActive(originAppId, requestSession)) return; - if (!newPolicy) { - throw new Error('Migration failed: policy not updated on the server'); - } - - dbManager.updateObjectByPrimaryId( - RealmSchema.Signer, - 'masterFingerprint', - signer.masterFingerprint, - { - signerPolicy: newPolicy, - } - ); - break; - } - } - } catch (err) { - console.log('Migrate Server Key policy err:', err); +function* migrateServerKeyPolicy( + appId: string, + session: number, + intent?: BackupWriteIntent, + parent?: UpgradeGroup +) { + if (!upgradeAccountIsActive(appId, session)) return false; + const group = parent || beginUpgradeGroup(appId, session, intent); + const { bitcoinNetwork } = yield select((state: RootState) => state.settings); + const signers: Signer[] = yield call(dbManager.getCollection, RealmSchema.Signer); + if (!upgradeAccountIsActive(appId, session)) return false; + const signer = signers.find( + (value) => value.type === SignerType.POLICY_SERVER && value.signerPolicy + ); + if (signer) { + const id = WalletUtilities.getFingerprintFromExtendedKey( + signer.signerXpubs[XpubTypes.P2WSH][0].xpub, + bitcoinNetwork + ); + const { newPolicy } = yield call(SigningServer.migrateSignerPolicy, id, signer.signerPolicy); + if (!upgradeAccountIsActive(appId, session)) return false; + if (!newPolicy) throw new Error('Server policy migration not confirmed'); + group.writes.push((realm) => { + const stored = realm.objectForPrimaryKey( + RealmSchema.Signer, + signer.id || getKeyUID(signer) + ); + if (!stored) throw new Error('Server policy signer unavailable'); + stored.signerPolicy = newPolicy; + }); + group.appImage = true; } + return parent ? true : yield call(completeUpgradeGroup, group); } diff --git a/tests/recovery/upgradeAccountSession.test.cjs b/tests/recovery/upgradeAccountSession.test.cjs index 39b55f7f33..05a6f0915e 100644 --- a/tests/recovery/upgradeAccountSession.test.cjs +++ b/tests/recovery/upgradeAccountSession.test.cjs @@ -7,81 +7,101 @@ const ts = require('typescript'); const semver = require('semver'); const { runSaga } = require('redux-saga'); const { call, put, select } = require('redux-saga/effects'); -const writeIntent = { appId: 'A', accountSession: 7, - baseRevision: 'a'.repeat(64), baselineEpoch: 0 }; - -const file = 'src/store/sagas/upgrade.ts'; -const source = ts.createSourceFile( - file, fs.readFileSync(path.join(__dirname, '../..', file), 'utf8'), ts.ScriptTarget.Latest, true -); - -function extract(names) { - const selected = source.statements.filter((node) => - (ts.isFunctionDeclaration(node) && names.includes(node.name?.text)) || - (ts.isVariableStatement(node) && node.declarationList.declarations.some( - (declaration) => declaration.name.getText(source) === 'upgradeAccountIsActive' - )) - ); - return ts.transpileModule(selected.map((node) => node.getText(source).replace(/^export /, '')).join('\n'), { - compilerOptions: { target: ts.ScriptTarget.ES2020 }, - }).outputText; -} - -function fixture(names) { - const state = { appId: 'A', session: 7, actions: [], relay: [], local: [] }; - const app = () => ({ id: state.appId, primarySeed: `seed-${state.appId}` }); - const dbManager = { - getObjectByIndex: () => app(), - getCollection: (schema) => { - if (schema === 'UTXOInfo') return [{ walletId: 'wallet', txId: 'tx', vout: 0, labels: [{ name: 'note' }] }]; - if (schema === 'Wallet') return [{ id: 'wallet' }]; - if (schema === 'Signer') return []; - if (schema === 'Vault') return [{ id: 'vault', archived: false }]; - return []; +const { encryption } = require('./helpers.cjs'); +const plain = (value) => JSON.parse(JSON.stringify(value)); +const writeIntent = Object.freeze({ appId: 'A', accountSession: 7, + baseRevision: 'a'.repeat(64), baselineEpoch: 0 }); +const source = ts.createSourceFile('upgrade.ts', + fs.readFileSync(path.join(__dirname, '../../src/store/sagas/upgrade.ts'), 'utf8'), ts.ScriptTarget.Latest, true); +const declarations = source.statements.filter((node) => + ts.isFunctionDeclaration(node) || (ts.isVariableStatement(node) && + node.declarationList.declarations.some((value) => value.name.getText(source) === 'upgradeAccountIsActive'))); +const code = ts.transpileModule(declarations.map((node) => node.getText(source).replace(/^export /, '')).join('\n'), { + compilerOptions: { target: ts.ScriptTarget.ES2020 }, +}).outputText; + +// Producer/account tests only: the separate native Realm/HTTP Relay fixture +// proves commit-generation, atomic rollback, encrypted CAS and readback itself. +// AES, key derivation for encryption and hashing here are production primitives. +function fixture() { + const state = { appId: 'A', session: 7, actions: [], queued: [], events: [], commits: 0, + failSchema: null, topics: [], records: { + KeeperApp: [{ id: 'A', primarySeed: 'ab'.repeat(32), version: '2.5.16' }], + Wallet: [{ id: 'wallet' }], Signer: [], Tags: [], VersionHistory: [], VaultSigner: [], + Vault: [{ id: 'vault', archived: false }, { id: 'old-vault', archived: true }], + UTXOInfo: [{ walletId: 'wallet', txId: 'tx', vout: 0, labels: [{ name: 'note' }] }], + } }; + const rawRealm = { + objectForPrimaryKey(schema, id) { return state.records[schema].find((value) => value.id === id); }, + objects(schema) { return state.records[schema]; }, + create(schema, value) { + if (state.failSchema === schema) throw Error('disposable local transaction failure'); + const rows = state.records[schema]; + const primary = schema === 'VersionHistory' ? 'version' : 'id'; + const existing = rows.findIndex((row) => row[primary] === value[primary]); + if (existing >= 0) rows[existing] = plain(value); + else rows.push(plain(value)); }, - createObject: (...args) => state.local.push(args), - createObjectBulk: (...args) => state.local.push(args), + }; + const dbManager = { + getObjectByIndex: () => state.records.KeeperApp[0], + getCollection: (schema) => plain(state.records[schema]), }; const scope = { - call, put, select, semver, dbManager, - console: { log() {} }, - RealmSchema: { KeeperApp: 'KeeperApp', UTXOInfo: 'UTXOInfo', Wallet: 'Wallet', - Signer: 'Signer', Vault: 'Vault', Tags: 'Tags', VersionHistory: 'VersionHistory' }, + call, put, select, semver, dbManager, Realm: { UpdateMode: { Modified: 'modified' } }, + RealmSchema: Object.fromEntries(Object.keys(state.records).map((schema) => [schema, schema])), store: { getState: () => ({ storage: { appId: state.appId } }) }, currentBackupAccountSession: () => state.session, - LABELS_INTRODUCTION_VERSION: '1.0.4', - BIP329_INTRODUCTION_VERSION: '1.0.7', - KEY_MANAGEMENT_VERSION: '1.1.9', - APP_KEY_UPGRADE_VERSION: '1.1.12', - HEALTH_CHECK_TIMELINE_MIGRATION_VERSION: '1.2.6', - SIGNER_POLICY_MIGRATION_VERSION: '2.1.0', - migrateLabelsToBip329: (originAppId) => ({ type: 'MIGRATE_LABELS_329', originAppId }), - updateVersionHistory: (previousVersion, newVersion, isRecovery, originAppId) => ({ - type: 'UPDATE_VERSION_HISTORY', payload: { previousVersion, newVersion, isRecovery }, originAppId, - }), - setAppVersion: (version) => ({ type: 'setAppVersion', version }), - setPendingAllBackup: (payload) => ({ type: 'setPendingAllBackup', payload }), - isAutomaticCloudBackupEnabled: () => true, - Relay: { - updateAppImage: async (...args) => { state.relay.push(['version', ...args]); return { updated: true }; }, - modifyLabels: async (...args) => { state.relay.push(['labels', ...args]); return { updated: true }; }, + notifyBackupReconciliation: () => state.events.push('pending'), + LABELS_INTRODUCTION_VERSION: '1.0.4', BIP329_INTRODUCTION_VERSION: '1.0.7', + KEY_MANAGEMENT_VERSION: '1.1.9', APP_KEY_UPGRADE_VERSION: '1.1.12', + HEALTH_CHECK_TIMELINE_MIGRATION_VERSION: '1.2.6', SIGNER_POLICY_MIGRATION_VERSION: '2.1.0', + beginBackupLocalOperation(appId, intent, session) { return { appId, intent, session }; }, + commitBackupLocalOperation(operation, write) { + if (operation.appId !== state.appId || operation.session !== state.session) + return { committed: false }; + const previous = plain(state.records); + try { write(rawRealm); } catch { + state.records = previous; + state.events.push('rollback'); + return { committed: false }; + } + state.commits++; + state.events.push('commit'); + return Object.freeze({ committed: true, intent: operation.intent }); + }, + async enqueueAutomaticBackupOperation(receipt, mutations) { + state.events.push('queue'); + state.queued.push({ receipt, mutations: plain(mutations), local: plain(state.records) }); + return { updated: true, backupConfirmed: !!receipt.intent, backupPending: !receipt.intent }; + }, + buildAutomaticAppMutation(payload, appId) { + const app = state.records.KeeperApp[0]; + return { operation: 'updateAppImage', body: { appId, version: app.version, + wallets: plain(payload.wallets), signers: plain(payload.signers) } }; + }, + buildAutomaticVaultMutation(id, appId) { + return { operation: 'updateVaultImage', body: { appId, + vault: plain(state.records.Vault.find((value) => value.id === id)) } }; }, DeviceInfo: { getBuildNumber: () => '1' }, getApp: () => ({}), getMessaging: () => ({}), - unsubscribeFromTopic() {}, subscribeToTopic() {}, getReleaseTopic: (version) => version, + unsubscribeFromTopic: (_messaging, topic) => state.topics.push(['old', topic]), + subscribeToTopic: (_messaging, topic) => state.topics.push(['new', topic]), + getReleaseTopic: (version) => version, + setAppVersion: (version) => ({ type: 'setAppVersion', version }), generateAbbreviatedOutputDescriptors: () => 'disposable-origin', - LabelRefType: { OUTPUT: 'OUTPUT' }, - generateEncryptionKey: (seed) => seed, - hash256: (value) => `hash(${value})`, - encrypt: (_key, content) => content, - SignerType: { KEEPER: 'KEEPER', MOBILE_KEY: 'MOBILE_KEY', MY_KEEPER: 'MY_KEEPER' }, + LabelRefType: { OUTPUT: 'OUTPUT' }, ...encryption, + SignerType: { KEEPER: 'KEEPER', MOBILE_KEY: 'MOBILE_KEY', MY_KEEPER: 'MY_KEEPER', POLICY_SERVER: 'POLICY_SERVER' }, + XpubTypes: { P2WSH: 'P2WSH' }, + hcStatusType: { HEALTH_CHECK_SUCCESSFULL: 'HEALTH_CHECK_SUCCESSFULL' }, + captureError() {}, }; - vm.runInNewContext(extract(names), scope); - const run = (name, ...args) => runSaga( - { getState: () => ({ storage: { appId: state.appId }, bhr: {} }), - dispatch: (action) => state.actions.push(action) }, - scope[name], ...args - ).toPromise(); + vm.runInNewContext(code, scope); + const run = (name, ...args) => runSaga({ + getState: () => ({ storage: { appId: state.appId }, settings: {}, bhr: {} }), + dispatch: (action) => state.actions.push(plain(action)), + }, scope[name], ...args).toPromise(); return { state, scope, dbManager, run }; } @@ -91,142 +111,128 @@ function deferred() { return { promise, resolve }; } -test('upgrade sequence stamps its original session on nested migrations and dispatched actions', async () => { - const f = fixture(['applyUpgradeSequence']); - const nested = []; - for (const name of [ - 'migrateStructureforSignersInAppImage', 'migrateStructureforVaultInAppImage', - 'updateAppKeysToEnableSigning', - ]) f.scope[name] = function* (...args) { nested.push([name, ...args]); }; - f.scope.healthCheckTimelineMigration = function* () {}; - f.scope.migrateServerKeyPolicy = function* () {}; - - await f.run('applyUpgradeSequence', { previousVersion: '1.0.5', newVersion: '2.5.17', - originBackupWriteIntent: writeIntent }); +const versionAction = { originAppId: 'A', originBackupAccountSession: 7, originBackupWriteIntent: writeIntent, + payload: { previousVersion: '2.5.16', newVersion: '2.5.17', isRecovery: true } }; - assert.equal(nested.length, 3); - assert.ok(nested.every(([, appId, session]) => appId === 'A' && session === 7)); - const backupActions = f.state.actions.filter((action) => - action.type === 'MIGRATE_LABELS_329' || action.type === 'UPDATE_VERSION_HISTORY' - ); - assert.equal(backupActions.length, 2); - assert.ok(backupActions.every((action) => action.originAppId === 'A' && - action.originBackupAccountSession === 7)); - assert.ok(backupActions.every((action) => - action.originBackupWriteIntent?.baseRevision === writeIntent.baseRevision)); +test('launch upgrade commits all migrations and version once before one original-intent queue', async () => { + const f = fixture(); + assert.equal(await f.run('applyUpgradeSequence', { + previousVersion: '1.0.5', newVersion: '2.5.17', originBackupAccountSession: 7, + originBackupWriteIntent: writeIntent, + }), true); + assert.equal(f.state.commits, 1); + assert.deepEqual(f.state.events, ['commit', 'queue']); + assert.equal(f.state.queued.length, 1); + const queued = f.state.queued[0]; + assert.equal(queued.receipt.intent, writeIntent); + assert.equal(queued.local.KeeperApp[0].version, '2.5.17'); + assert.equal(queued.local.VersionHistory.length, 1); + assert.equal(queued.local.Tags.length, 1); + assert.deepEqual(queued.mutations.map((value) => value.operation), + ['updateAppImage', 'updateVaultImage', 'updateVaultImage', 'modifyLabels']); + assert.equal(queued.mutations[2].body.vault.archived, true); + assert.deepEqual(f.state.actions, [{ type: 'setAppVersion', version: '2.5.17' }]); }); -test('upgrade sequence paused on its first app read stops after A to B to A', async () => { - const f = fixture(['applyUpgradeSequence']); - const entered = deferred(); - const resume = deferred(); - let first = true; - f.dbManager.getObjectByIndex = () => { - if (first) { first = false; entered.resolve(); return resume.promise; } - return { id: f.state.appId }; +test('paused launch upgrade stops after A to B to A without any local commit or queue', async () => { + const f = fixture(); + const entered = deferred(), resume = deferred(); + const getCollection = f.dbManager.getCollection; + f.dbManager.getCollection = (schema) => { + if (schema === 'UTXOInfo') { entered.resolve(); return resume.promise; } + return getCollection(schema); }; - f.scope.migrateStructureforSignersInAppImage = function* () { throw Error('stale migration'); }; - f.scope.migrateStructureforVaultInAppImage = function* () { throw Error('stale migration'); }; - const pending = f.run('applyUpgradeSequence', { previousVersion: '1.0.5', newVersion: '2.5.17' }); await entered.promise; f.state.appId = 'B'; f.state.session = 8; f.state.appId = 'A'; f.state.session = 9; - resume.resolve({ id: 'A' }); - await pending; - + resume.resolve(getCollection('UTXOInfo')); + assert.equal(await pending, false); + assert.equal(f.state.commits, 0); + assert.deepEqual(f.state.queued, []); assert.deepEqual(f.state.actions, []); - assert.deepEqual(f.state.relay, []); }); -test('upgrade invoked after A to B to A rejects the caller original session before backup', async () => { - const f = fixture(['applyUpgradeSequence']); - f.state.appId = 'B'; f.state.session = 8; - f.state.appId = 'A'; f.state.session = 9; - f.scope.migrateStructureforSignersInAppImage = function* () { throw Error('stale upload'); }; - f.scope.migrateStructureforVaultInAppImage = function* () { throw Error('stale upload'); }; - await f.run('applyUpgradeSequence', { - previousVersion: '1.0.5', - newVersion: '2.5.17', - originBackupAccountSession: 7, - }); - assert.deepEqual(f.state.actions, []); - assert.deepEqual(f.state.relay, []); +test('a caller original session is rejected before upgrade after A to B to A', async () => { + const f = fixture(); + f.state.session = 9; + assert.equal(await f.run('applyUpgradeSequence', { previousVersion: '1.0.5', newVersion: '2.5.17', + originBackupAccountSession: 7 }), false); + assert.equal(await f.run('updateVersionHistoryWorker', versionAction), false); + assert.equal(f.state.commits, 0); + assert.deepEqual(f.state.queued, []); }); -test('version update passes session to Relay and blocks stale A to B to A action', async () => { - const f = fixture(['updateVersionHistoryWorker']); - const action = { originAppId: 'A', originBackupAccountSession: 7, - originBackupWriteIntent: writeIntent, - payload: { previousVersion: '2.5.16', newVersion: '2.5.17', isRecovery: true } }; - await f.run('updateVersionHistoryWorker', action); - assert.equal(f.state.relay.length, 1); - assert.equal(f.state.relay[0][2].accountSession, 7); - assert.equal(f.state.relay[0][2].baseRevision, writeIntent.baseRevision); +test('version update persists the new KeeperApp version before queue and preserves recovery history', async () => { + const f = fixture(); + assert.equal(await f.run('updateVersionHistoryWorker', versionAction), true); + assert.deepEqual(f.state.events, ['commit', 'queue']); + const queued = f.state.queued[0]; + assert.equal(queued.receipt.intent, writeIntent); + assert.equal(queued.local.KeeperApp[0].version, '2.5.17'); + assert.equal(queued.mutations[0].body.version, '2.5.17'); + assert.deepEqual(f.state.records.VersionHistory, []); +}); - const entered = deferred(); - const resume = deferred(); - let first = true; - f.dbManager.getObjectByIndex = () => { - if (first) { first = false; entered.resolve(); return resume.promise; } - return { id: f.state.appId }; - }; - const pending = f.run('updateVersionHistoryWorker', action); - await entered.promise; - f.state.appId = 'B'; f.state.session = 8; - f.state.appId = 'A'; f.state.session = 9; - resume.resolve({ id: 'A' }); - await pending; - assert.equal(f.state.relay.length, 1); - assert.deepEqual(f.state.local, []); +test('version history failure rolls back the version and never queues a cloud operation', async () => { + const f = fixture(); + f.state.failSchema = 'VersionHistory'; + assert.equal(await f.run('updateVersionHistoryWorker', { + ...versionAction, payload: { ...versionAction.payload, isRecovery: false }, + }), false); + assert.equal(f.state.records.KeeperApp[0].version, '2.5.16'); + assert.deepEqual(f.state.records.VersionHistory, []); + assert.deepEqual(f.state.events, ['rollback']); + assert.deepEqual(f.state.queued, []); + assert.deepEqual(f.state.topics, []); }); -test('label migration passes session to Relay and blocks stale A to B to A action', async () => { - const f = fixture(['migrateLablesWorker']); - const action = { originAppId: 'A', originBackupAccountSession: 7, - originBackupWriteIntent: writeIntent }; - await f.run('migrateLablesWorker', action); - assert.equal(f.state.relay.length, 1); - assert.equal(f.state.relay[0][4].accountSession, 7); - assert.equal(f.state.relay[0][4].baseRevision, writeIntent.baseRevision); +test('migrated label ciphertext encodes the committed tag with production AES and hashed label ID', async () => { + const f = fixture(); + await f.run('migrateLablesWorker', { originAppId: 'A', originBackupAccountSession: 7, + originBackupWriteIntent: writeIntent }); + const queued = f.state.queued[0]; + assert.equal(queued.receipt.intent, writeIntent); + const label = queued.mutations[0].body.addLabels[0]; + const key = encryption.generateEncryptionKey(f.state.records.KeeperApp[0].primarySeed); + assert.equal(label.id, encryption.hash256(encryption.hash256(key + f.state.records.Tags[0].id))); + assert.deepEqual(JSON.parse(encryption.decrypt(key, label.content)), f.state.records.Tags[0]); + assert.notEqual(label.content, JSON.stringify(f.state.records.Tags[0])); + assert.deepEqual(f.state.events, ['commit', 'queue']); +}); - const entered = deferred(); - const resume = deferred(); +test('paused label migration stops after A to B to A before committing staged tags', async () => { + const f = fixture(); + const entered = deferred(), resume = deferred(); const getCollection = f.dbManager.getCollection; - let first = true; f.dbManager.getCollection = (schema) => { - if (schema === 'UTXOInfo' && first) { - first = false; entered.resolve(); return resume.promise; - } + if (schema === 'UTXOInfo') { entered.resolve(); return resume.promise; } return getCollection(schema); }; - const pending = f.run('migrateLablesWorker', action); + const pending = f.run('migrateLablesWorker', { originAppId: 'A', originBackupAccountSession: 7, + originBackupWriteIntent: writeIntent }); await entered.promise; f.state.appId = 'B'; f.state.session = 8; f.state.appId = 'A'; f.state.session = 9; resume.resolve(getCollection('UTXOInfo')); - await pending; - assert.equal(f.state.relay.length, 1); - assert.equal(f.state.local.length, 1); + assert.equal(await pending, false); + assert.equal(f.state.commits, 0); + assert.deepEqual(f.state.records.Tags, []); + assert.deepEqual(f.state.queued, []); }); -test('nested upgrade backup workers receive the original session', async () => { - const f = fixture([ - 'migrateStructureforSignersInAppImage', 'migrateStructureforVaultInAppImage', - 'updateAppKeysToEnableSigning', - ]); - const nested = []; - f.scope.updateAppImageWorker = function* (action) { nested.push(['app', action]); return { updated: true }; }; - f.scope.updateVaultImageWorker = function* (action) { nested.push(['vault', action]); return { updated: true }; }; - f.scope.mapAppKeysToWallets = () => ({ appKeyWalletMap: {}, myAppKeySigners: [] }); - f.scope.generateExtendedKeysForSigners = () => ({}); - f.scope.updateVaultSigners = () => {}; - f.scope.updateSignerDetails = () => {}; +test('a missing original verification baseline never gets recaptured by an upgrade producer', async () => { + const f = fixture(); + await f.run('updateVersionHistoryWorker', { ...versionAction, originBackupWriteIntent: undefined }); + assert.equal(f.state.records.KeeperApp[0].version, '2.5.17'); + assert.equal(f.state.queued[0].receipt.intent, undefined); +}); - await f.run('migrateStructureforSignersInAppImage', 'A', 7); - await f.run('migrateStructureforVaultInAppImage', 'A', 7); - await f.run('updateAppKeysToEnableSigning', 'A', 7); - assert.deepEqual(nested.map(([kind]) => kind), ['app', 'vault', 'app']); - assert.ok(nested.every(([, action]) => action.originAppId === 'A' && - action.originBackupAccountSession === 7)); +test('cloud payload preparation failure preserves the committed local upgrade and marks pending', async () => { + const f = fixture(); + f.scope.buildAutomaticAppMutation = () => { throw Error('disposable payload preparation failure'); }; + assert.equal(await f.run('updateVersionHistoryWorker', versionAction), true); + assert.equal(f.state.records.KeeperApp[0].version, '2.5.17'); + assert.deepEqual(f.state.events, ['commit', 'pending']); + assert.deepEqual(f.state.queued, []); }); From 9ead4779bb41633b84145261a0ecc03b0742658c Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Tue, 13 Oct 2026 03:20:29 +0000 Subject: [PATCH 149/254] Commit label and coin-choice backups after atomic local changes --- src/store/sagas/send_and_receive.ts | 121 ++------- src/store/sagas/utxos.ts | 244 ++++++++++++------ .../automaticLabelProducers.integration.cjs | 165 ++++++++++++ tests/recovery/labelAccount.test.cjs | 170 ++++++++++-- tests/recovery/manualCoinChoices.test.cjs | 29 ++- tests/recovery/sendLabelOrigin.test.cjs | 52 ++++ 6 files changed, 595 insertions(+), 186 deletions(-) create mode 100644 tests/recovery/automaticLabelProducers.integration.cjs diff --git a/src/store/sagas/send_and_receive.ts b/src/store/sagas/send_and_receive.ts index 635995f410..4ad6b693ed 100644 --- a/src/store/sagas/send_and_receive.ts +++ b/src/store/sagas/send_and_receive.ts @@ -1,10 +1,9 @@ import { AverageTxFeesByNetwork, SerializedPSBTEnvelop } from 'src/services/wallets/interfaces'; -import { EntityKind, LabelRefType, TxPriority } from 'src/services/wallets/enums'; +import { EntityKind, TxPriority } from 'src/services/wallets/enums'; import type { BackupWriteIntent } from 'src/services/backup/intent'; -import { call, fork, put, select } from 'redux-saga/effects'; +import { call, put, select } from 'redux-saga/effects'; import { RealmSchema } from 'src/storage/realm/enum'; -import Relay from 'src/services/backend/Relay'; import { Signer } from 'src/services/wallets/interfaces/vault'; import WalletOperations from 'src/services/wallets/operations'; import _ from 'lodash'; @@ -40,7 +39,7 @@ import { SendPhaseThreeAction, SendPhaseTwoAction, } from '../sagaActions/send_and_receive'; -import { addLabelsWorker, bulkUpdateLabelsWorker } from './utxos'; +import { transactionLabelsWorker } from './utxos'; import { setElectrumNotConnectedErr } from '../reducers/login'; import { connectToNodeWorker } from './network'; import { getKeyUID, getOutputsFromPsbt } from 'src/utils/utilities'; @@ -170,19 +169,6 @@ function* sendPhaseTwoWorker({ payload, originAppId, originBackupAccountSession, switch (wallet.entityKind) { case EntityKind.WALLET: if (!txid) throw new Error('Send failed: unable to generate txid'); - if (note) { - yield call(addLabelsWorker, { - originAppId, - originBackupAccountSession, - originBackupWriteIntent, - payload: { - txId: txid, - wallet, - labels: [{ name: note, isSystem: false }], - type: LabelRefType.TXN, - }, - }); - } yield put( sendPhaseTwoExecuted({ successful: true, @@ -209,8 +195,12 @@ function* sendPhaseTwoWorker({ payload, originAppId, originBackupAccountSession, throw new Error('Invalid Entity: not a vault/Wallet'); } - if (finalOutputs) { - yield call(handleChangeOutputLabels, finalOutputs, inputs, wallet, txid, originAppId, originBackupAccountSession, originBackupWriteIntent); + if (finalOutputs || (wallet.entityKind === EntityKind.WALLET && note)) { + yield call( + handleChangeOutputLabels, finalOutputs, inputs, wallet, txid, + originAppId, originBackupAccountSession, originBackupWriteIntent, + wallet.entityKind === EntityKind.WALLET ? note : undefined + ); } } catch (err) { if ([ELECTRUM_NOT_CONNECTED_ERR, ELECTRUM_NOT_CONNECTED_ERR_TOR].includes(err?.message)) { @@ -280,22 +270,11 @@ function* sendPhaseThreeWorker({ payload, originAppId, originBackupAccountSessio }) ); - if (note) { - yield call(addLabelsWorker, { - originAppId, - originBackupAccountSession, - originBackupWriteIntent, - payload: { - txId: txid, - wallet, - labels: [{ name: note, isSystem: false }], - type: LabelRefType.TXN, - }, - }); - } - - if (finalOutputs) { - yield call(handleChangeOutputLabels, finalOutputs, inputs, wallet, txid, originAppId, originBackupAccountSession, originBackupWriteIntent); + if (note || finalOutputs) { + yield call( + handleChangeOutputLabels, finalOutputs, inputs, wallet, txid, + originAppId, originBackupAccountSession, originBackupWriteIntent, note + ); } } catch (err) { if ([ELECTRUM_NOT_CONNECTED_ERR, ELECTRUM_NOT_CONNECTED_ERR_TOR].includes(err?.message)) { @@ -320,53 +299,15 @@ function* handleChangeOutputLabels( txid, originAppId?: string, originBackupAccountSession?: number, - originBackupWriteIntent?: BackupWriteIntent + originBackupWriteIntent?: BackupWriteIntent, + note?: string ) { - const changeOutputIndex = finalOutputs.findIndex((output) => - Object.values(wallet.specs.addresses.internal).includes(output.address) - ); - - const changeOutput = changeOutputIndex !== -1 ? finalOutputs[changeOutputIndex] : null; - - if (changeOutput) { - let labels: { ref: string; label: string; isSystem: boolean }[] = yield call( - dbManager.getCollection, - RealmSchema.Tags - ); - - const labelChanges = { - added: [], - deleted: [], - }; - - const prevUTXOsLabels = labels.filter((label) => - inputs.map((input) => input.txId + ':' + input.vout).includes(label.ref) - ); - labelChanges.added.push({ - name: 'Change', - isSystem: false, - }); - - if (prevUTXOsLabels) { - labelChanges.added.push( - ...prevUTXOsLabels.map((label) => ({ - name: label.label, - isSystem: label.isSystem, - })) - ); - } - - yield fork(bulkUpdateLabelsWorker, { - originAppId, - originBackupAccountSession, - originBackupWriteIntent, - payload: { - labelChanges, - UTXO: { txId: txid, vout: changeOutputIndex }, - wallet: wallet as any, - }, - }); - } + yield call(transactionLabelsWorker, { + originAppId, + originBackupAccountSession, + originBackupWriteIntent, + payload: { txId: txid, wallet, note, finalOutputs, inputs }, + }); } function* calculateSendMaxFee({ payload }: CalculateSendMaxFeeAction) { @@ -496,20 +437,10 @@ function* discardBroadcastedTnxWorker({ payload, originAppId, originBackupAccoun }); const potentialTxId = snapshots[cachedTxid].potentialTxId; const note = snapshots[cachedTxid].routeParams.note; - yield call(handleChangeOutputLabels, finalOutputs, inputs, vault, potentialTxId, originAppId, originBackupAccountSession, originBackupWriteIntent); - if (note.length) { - yield call(addLabelsWorker, { - originAppId, - originBackupAccountSession, - originBackupWriteIntent, - payload: { - txId: potentialTxId, - wallet: vault, - labels: [{ name: note, isSystem: false }], - type: LabelRefType.TXN, - }, - }); - } + yield call( + handleChangeOutputLabels, finalOutputs, inputs, vault, potentialTxId, + originAppId, originBackupAccountSession, originBackupWriteIntent, note + ); yield put(dropTransactionSnapshot({ cachedTxid })); } } diff --git a/src/store/sagas/utxos.ts b/src/store/sagas/utxos.ts index 73ad36f629..7d01718d66 100644 --- a/src/store/sagas/utxos.ts +++ b/src/store/sagas/utxos.ts @@ -1,11 +1,17 @@ import dbManager from 'src/storage/realm/dbManager'; import { RealmSchema } from 'src/storage/realm/enum'; -import { call, delay, put } from 'redux-saga/effects'; +import { call, put } from 'redux-saga/effects'; +import Realm from 'realm'; import { BIP329Label, UTXOSpendability } from 'src/services/wallets/interfaces'; import { EntityKind, LabelRefType, NetworkType } from 'src/services/wallets/enums'; -import Relay from 'src/services/backend/Relay'; import { currentBackupAccountSession } from 'src/services/backup/transport'; import type { BackupWriteIntent } from 'src/services/backup/intent'; +import { + beginBackupLocalOperation, + commitBackupLocalOperation, + enqueueAutomaticBackupOperation, +} from 'src/services/backup/automatic'; +import type { BackupLocalCommit, BackupLocalOperation } from 'src/services/backup/automatic'; import { Wallet } from 'src/services/wallets/interfaces/wallet'; import { generateAbbreviatedOutputDescriptors } from 'src/utils/service-utilities/utils'; import { Vault } from 'src/services/wallets/interfaces/vault'; @@ -22,7 +28,6 @@ import { } from '../sagaActions/utxos'; import { resetState, setSyncingUTXOError, setSyncingUTXOs } from '../reducers/utxos'; import { - checkBackupCondition, setServerBackupFailed, updateAppImageWorker, updateVaultImageWorker, @@ -48,20 +53,25 @@ function persistTagsForOrigin( added: BIP329Label[], deleted: string[] = [], scope?: LabelSyncScope, - accountSession?: number + accountSession?: number, + operation?: BackupLocalOperation ) { - if (!currentOriginApp(originAppId)) return false; - if (accountSession !== undefined && accountSession !== currentBackupAccountSession()) return false; + if (!operation || !currentOriginApp(originAppId)) return { committed: false }; + if (accountSession !== undefined && accountSession !== currentBackupAccountSession()) return { committed: false }; if ( scope && (scope.appId !== originAppId || store.getState().settings.bitcoinNetworkType !== scope.networkType) ) - return false; - if (added.length && dbManager.createObjectBulk(RealmSchema.Tags, added) !== true) return false; - for (const id of deleted) { - if (dbManager.deleteObjectById(RealmSchema.Tags, id) !== true) return false; - } - return true; + return { committed: false }; + // Additions and removals are one local transaction. A failed import or + // deletion cannot leave a partial operation eligible for upload. + return commitBackupLocalOperation(operation, (rawRealm) => { + for (const tag of added) rawRealm.create(RealmSchema.Tags, tag, Realm.UpdateMode.All); + for (const id of deleted) { + const tag = rawRealm.objectForPrimaryKey(RealmSchema.Tags, id); + if (tag) rawRealm.delete(tag); + } + }); } function persistSpendabilityForOrigin( @@ -69,11 +79,49 @@ function persistSpendabilityForOrigin( schema: RealmSchema, id: string, specs: any, - accountSession?: number + accountSession?: number, + operation?: BackupLocalOperation +) { + if (!operation || !currentOriginApp(originAppId)) return { committed: false }; + if (accountSession !== undefined && accountSession !== currentBackupAccountSession()) return { committed: false }; + return commitBackupLocalOperation(operation, (rawRealm) => { + const wallet: any = rawRealm.objectForPrimaryKey(schema, id); + if (!wallet) throw new Error('Wallet no longer exists'); + wallet.specs = specs; + }); +} + +function* backupCommittedLabels( + originAppId: string, + requestSession: number, + committedOperation: BackupLocalCommit, + added: BIP329Label[], + deleted: string[] = [] ) { - if (!currentOriginApp(originAppId)) return false; - if (accountSession !== undefined && accountSession !== currentBackupAccountSession()) return false; - return dbManager.updateObjectById(schema, id, { specs }) === true; + if (!committedOperation.committed) return; + const app = currentOriginApp(originAppId); + if (!app || requestSession !== currentBackupAccountSession()) return; + const encryptionKey = generateEncryptionKey(app.primarySeed); + const deletedIds = new Set(deleted); + // Deletion wins when the same label occurs on both sides of an edit, as in + // the local transaction. Read back each addition instead of encrypting the + // action payload, and let the queue prove it again before network. + const addedIds = [...new Set(added.map((tag) => tag.id))].filter((id) => !deletedIds.has(id)); + const addLabels = addedIds.map((id) => { + const persisted = dbManager.getObjectById(RealmSchema.Tags, id); + if (!persisted) throw new Error('Committed label no longer exists'); + const tag = getJSONFromRealmObject(persisted); + return { + id: hash256(hash256(encryptionKey + id)), + content: encrypt(encryptionKey, JSON.stringify(tag)), + }; + }); + const deleteLabels = [...deletedIds].map((id) => hash256(hash256(encryptionKey + id))); + if (!addLabels.length && !deleteLabels.length) return; + yield call(enqueueAutomaticBackupOperation, committedOperation, [{ + operation: 'modifyLabels', + body: { appId: originAppId, addLabels, deleteLabels }, + }]); } function originUiAction(action: T, originAppId: string): T & { originAppId: string } { @@ -103,7 +151,7 @@ export function* addLabelsWorker({ if (requestSession !== currentBackupAccountSession()) return; const app = currentOriginApp(originAppId); if (!app) return; - const primarySeed = app.primarySeed; + const operation = beginBackupLocalOperation(originAppId, originBackupWriteIntent, requestSession); yield put(originUiAction(setSyncingUTXOs(true), originAppId)); const { txId, vout, wallet, labels, type } = payload; const origin = generateAbbreviatedOutputDescriptors(wallet); @@ -120,20 +168,15 @@ export function* addLabelsWorker({ }; tags.push(tag); }); - if (!(yield call(persistTagsForOrigin, originAppId, tags, [], undefined, requestSession))) return; + const committedOperation: BackupLocalCommit = yield call( + persistTagsForOrigin, originAppId, tags, [], undefined, requestSession, operation + ); + if (!committedOperation.committed) return; try { - const backupResponse = yield call(checkBackupCondition, originAppId); - if (!backupResponse && currentOriginApp(originAppId) && requestSession === currentBackupAccountSession()) { - const encryptionKey = generateEncryptionKey(primarySeed); - const tagsToBackup = tags.map((tag) => ({ - id: hash256(hash256(encryptionKey + tag.id)), - content: encrypt(encryptionKey, JSON.stringify(tag)), - })); - yield call(Relay.modifyLabels, originAppId, tagsToBackup, [], originBackupWriteIntent); - } else yield delay(100); - } catch (error) { - console.log('🚀 ~ addLabelsWorker error:', error); - yield call(setServerBackupFailed, originAppId); + yield call(backupCommittedLabels, originAppId, requestSession, committedOperation, tags); + } catch { + if (currentOriginApp(originAppId) && requestSession === currentBackupAccountSession()) + yield call(setServerBackupFailed, originAppId); } yield put(originUiAction(resetState(), originAppId)); } catch (e) { @@ -170,7 +213,7 @@ export function* bulkUpdateLabelsWorker({ if (requestSession !== currentBackupAccountSession()) return; const app = currentOriginApp(originAppId); if (!app) return; - const primarySeed = app.primarySeed; + const operation = beginBackupLocalOperation(originAppId, originBackupWriteIntent, requestSession); yield put(originUiAction(setSyncingUTXOs(true), originAppId)); const { labelChanges, wallet, UTXO, txId, address, scope } = payload; if ( @@ -199,33 +242,15 @@ export function* bulkUpdateLabelsWorker({ if (labelChanges.deleted) { deletedTagIds = labelChanges.deleted.map((label) => `${ref}${label.name}`); } - if (!(yield call(persistTagsForOrigin, originAppId, addedTags, deletedTagIds, scope, requestSession))) return; + const committedOperation: BackupLocalCommit = yield call( + persistTagsForOrigin, originAppId, addedTags, deletedTagIds, scope, requestSession, operation + ); + if (!committedOperation.committed) return; try { - const backupResponse = yield call(checkBackupCondition, originAppId); - if ( - !backupResponse && - currentOriginApp(originAppId) && - requestSession === currentBackupAccountSession() && - (!scope || - (scope.appId === originAppId && - store.getState().settings.bitcoinNetworkType === scope.networkType)) - ) { - const encryptionKey = generateEncryptionKey(primarySeed); - const tagsToBackup = addedTags.map((tag) => ({ - id: hash256(hash256(encryptionKey + tag.id)), - content: encrypt(encryptionKey, JSON.stringify(tag)), - })); - const tagsToDelete = deletedTagIds.map((tag) => hash256(hash256(encryptionKey + tag))); - yield call( - Relay.modifyLabels, - originAppId, - tagsToBackup.length ? tagsToBackup : [], - tagsToDelete.length ? tagsToDelete : [], - originBackupWriteIntent - ); - } else yield delay(100); - } catch (error) { - yield call(setServerBackupFailed, originAppId); + yield call(backupCommittedLabels, originAppId, requestSession, committedOperation, addedTags, deletedTagIds); + } catch { + if (currentOriginApp(originAppId) && requestSession === currentBackupAccountSession()) + yield call(setServerBackupFailed, originAppId); } yield put(originUiAction(resetState(), originAppId)); } catch (e) { @@ -261,7 +286,7 @@ export function* importLabelsWorker({ if (requestSession !== currentBackupAccountSession()) return; const app = currentOriginApp(originAppId); if (!app) return; - const primarySeed = app.primarySeed; + const operation = beginBackupLocalOperation(originAppId, originBackupWriteIntent, requestSession); yield put(originUiAction(setSyncingUTXOs(true), originAppId)); const { labels } = payload; let addedTags: BIP329Label[] = []; @@ -278,20 +303,15 @@ export function* importLabelsWorker({ })); } - if (!(yield call(persistTagsForOrigin, originAppId, addedTags, [], undefined, requestSession))) return; - + const committedOperation: BackupLocalCommit = yield call( + persistTagsForOrigin, originAppId, addedTags, [], undefined, requestSession, operation + ); + if (!committedOperation.committed) return; try { - const backupResponse = yield call(checkBackupCondition, originAppId); - if (!backupResponse && currentOriginApp(originAppId) && requestSession === currentBackupAccountSession()) { - const encryptionKey = generateEncryptionKey(primarySeed); - const tagsToBackup = addedTags.map((tag) => ({ - id: hash256(hash256(encryptionKey + tag.id)), - content: encrypt(encryptionKey, JSON.stringify(tag)), - })); - yield call(Relay.modifyLabels, originAppId, tagsToBackup.length ? tagsToBackup : [], [], originBackupWriteIntent); - } else yield delay(100); - } catch (error) { - yield call(setServerBackupFailed, originAppId); + yield call(backupCommittedLabels, originAppId, requestSession, committedOperation, addedTags); + } catch { + if (currentOriginApp(originAppId) && requestSession === currentBackupAccountSession()) + yield call(setServerBackupFailed, originAppId); } yield put(originUiAction(resetState(), originAppId)); } catch (e) { @@ -301,6 +321,72 @@ export function* importLabelsWorker({ } } +// A send may create a transaction note and several labels on its change +// output. They share one local commit and one remote mutation, so failure +// cannot authorize a later child with the original remote revision. +export function* transactionLabelsWorker({ + payload, + originAppId, + originBackupAccountSession, + originBackupWriteIntent, +}: { + payload: { + txId: string; + wallet: Wallet | Vault; + note?: string; + finalOutputs?: { address: string }[]; + inputs?: { txId: string; vout: number }[]; + }; + originAppId?: string; + originBackupAccountSession?: number; + originBackupWriteIntent?: BackupWriteIntent; +}) { + try { + if (!originAppId) return; + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); + if (requestSession !== currentBackupAccountSession() || !currentOriginApp(originAppId)) return; + const operation = beginBackupLocalOperation(originAppId, originBackupWriteIntent, requestSession); + const { txId, wallet, note, finalOutputs = [], inputs = [] } = payload; + const origin = generateAbbreviatedOutputDescriptors(wallet); + const tags: BIP329Label[] = []; + if (note) tags.push({ + id: `${txId}${note}`, ref: txId, type: LabelRefType.TXN, + label: note, origin, isSystem: false, + }); + const changeOutputIndex = finalOutputs.findIndex((output) => + Object.values(wallet.specs.addresses.internal).includes(output.address) + ); + if (changeOutputIndex !== -1) { + const inputRefs = new Set(inputs.map((input) => `${input.txId}:${input.vout}`)); + const inherited: BIP329Label[] = dbManager.getCollection(RealmSchema.Tags) + .filter((tag) => inputRefs.has(tag.ref)); + const ref = `${txId}:${changeOutputIndex}`; + const changeLabels = [{ label: 'Change', isSystem: false }, ...inherited]; + for (const label of changeLabels) tags.push({ + id: `${ref}${label.label}`, ref, type: LabelRefType.OUTPUT, + label: label.label, isSystem: label.isSystem, origin, + }); + } + if (!tags.length) return; + yield put(originUiAction(setSyncingUTXOs(true), originAppId)); + const committedOperation: BackupLocalCommit = yield call( + persistTagsForOrigin, originAppId, tags, [], undefined, requestSession, operation + ); + if (!committedOperation.committed) return; + try { + yield call(backupCommittedLabels, originAppId, requestSession, committedOperation, tags); + } catch { + if (currentOriginApp(originAppId) && requestSession === currentBackupAccountSession()) + yield call(setServerBackupFailed, originAppId); + } + yield put(originUiAction(resetState(), originAppId)); + } catch (error) { + if (originAppId) yield put(originUiAction(setSyncingUTXOError(error), originAppId)); + } finally { + if (originAppId) yield put(originUiAction(setSyncingUTXOs(false), originAppId)); + } +} + export const addLabelsWatcher = createWatcher(addLabelsWorker, ADD_LABELS); export const bulkUpdateLabelWatcher = createWatcher(bulkUpdateLabelsWorker, BULK_UPDATE_LABELS); export const importLabelsWatcher = createWatcher(importLabelsWorker, IMPORT_LABELS); @@ -326,6 +412,7 @@ export function* markUTXOSpendabilityWorker({ const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); if (requestSession !== currentBackupAccountSession()) return; if (!currentOriginApp(originAppId)) return; + const operation = beginBackupLocalOperation(originAppId, originBackupWriteIntent, requestSession); const { wallet, txId, vout, spendability } = payload; const schema = wallet.entityKind === EntityKind.VAULT ? RealmSchema.Vault : RealmSchema.Wallet; @@ -354,24 +441,31 @@ export function* markUTXOSpendabilityWorker({ } if (!changed) return; - const persisted = yield call(persistSpendabilityForOrigin, originAppId, schema, wallet.id, specs, requestSession); - if (persisted !== true) return; + const committedOperation: BackupLocalCommit = yield call( + persistSpendabilityForOrigin, originAppId, schema, wallet.id, specs, requestSession, operation + ); + if (!committedOperation.committed) return; + const committedWallet = dbManager.getObjectById(schema, wallet.id); + if (!committedWallet) return; + const freshWallet = getJSONFromRealmObject(committedWallet) as unknown as Wallet | Vault; // Persist the user's choice first, then invalidate/refresh Assisted Server // Backup through its normal opt-in/offline-aware incremental path. try { const response = schema === RealmSchema.Vault ? yield call(updateVaultImageWorker, { - payload: { vault: walletJSON as Vault, isUpdate: true }, + payload: { vault: freshWallet as Vault, isUpdate: true }, originAppId, originBackupAccountSession: requestSession, originBackupWriteIntent, + committedOperation, }) : yield call(updateAppImageWorker, { - payload: { wallets: [walletJSON as Wallet] }, + payload: { wallets: [freshWallet as Wallet] }, originAppId, originBackupAccountSession: requestSession, originBackupWriteIntent, + committedOperation, }); if (!response?.updated) yield call(setServerBackupFailed, originAppId); } catch { diff --git a/tests/recovery/automaticLabelProducers.integration.cjs b/tests/recovery/automaticLabelProducers.integration.cjs new file mode 100644 index 0000000000..9c7a9035db --- /dev/null +++ b/tests/recovery/automaticLabelProducers.integration.cjs @@ -0,0 +1,165 @@ +// Actual label/spendability producers, native Realm, real AES and the paired +// Relay's production HTTP transaction routes. Missing prerequisites fail. +const assert = require('node:assert/strict'); +const { test, before, after } = require('node:test'); +const { createRealmClient, declarations, evaluate, plain, Realm } = require('./automaticWriteRealmRelay.fixture.cjs'); +const fixtureModule = process.env.KEEPER_RELAY_FIXTURE_MODULE; +assert.ok(fixtureModule, 'Set KEEPER_RELAY_FIXTURE_MODULE to Relay tests/support/automaticBackupHttpFixture.cjs'); +const { createFixture } = require(fixtureModule); +let server; +before(async () => { server = await createFixture(); }); +after(async () => { await server?.close(); Realm.shutdown(); }); + +const label = (ref, name) => ({ id: `${ref}${name}`, ref, label: name, type: 'TXN', origin: 'disposable-public-origin', isSystem: false }); +const writes = () => server.trace.filter(event => event.path === '/revisionedBackupMutation'); + +async function setup(t, initialTags = []) { + await server.reset(); + const f = await createRealmClient(server.url); + t.after(() => f.close()); + f.automatic = f.load('src/services/backup/automatic'); + f.installProductionStoreObservers(f.automatic); + f.store.dispatch(f.bhr.setAutomaticCloudBackup({ appId: f.appId, enabled: true })); + const wallet = f.defaults(f.RealmSchema.Wallet, { + id: 'producer-wallet', entityKind: f.enums.EntityKind.WALLET, type: f.enums.WalletType.DEFAULT, + networkType: f.enums.NetworkType.TESTNET, scriptType: 'P2WPKH', + derivationDetails: f.defaults(f.RealmSchema.WalletDerivationDetails, { instanceNum: 0, xDerivationPath: "m/84'/1'/0'" }), + presentationData: f.defaults(f.RealmSchema.WalletPresentationData, { name: 'Producer fixture', visibility: 'DEFAULT' }), + specs: f.defaults(f.RealmSchema.WalletSpecs, { + xpub: 'disposable-public-wallet-xpub', nextFreeAddressIndex: 1, + addresses: { external: {}, internal: { 0: 'change-address' } }, + confirmedUTXOs: [f.defaults(f.RealmSchema.UTXO, { + txId: 'input', vout: 0, spendability: 'spendable', isManualOverride: false, + })], + }), + }); + assert.equal(f.dbManager.createObject(f.RealmSchema.Wallet, wallet), true); + for (const tag of initialTags) assert.equal(f.dbManager.createObject(f.RealmSchema.Tags, tag), true); + await server.seed(await f.payloadForCurrentImage()); + assert.equal(await f.repair.inspectBackup(f.appId, false, () => {}), 'verified'); + server.clearTrace(); + const utxos = f.load('src/store/reducers/utxos'); + const sagas = f.productionSagas([ + 'currentBackupAppId', 'setServerBackupFailed', + 'buildAutomaticAppMutation', 'buildAutomaticVaultMutation', 'queueCommittedBackupWorker', + 'updateAppImageWorker', 'updateVaultImageWorker', + ], { + ...f.automatic, ...utxos, + // The fixture's synthetic public xpub is intentionally non-spendable. + // Descriptor formatting supplies public label metadata, not write proof. + generateAbbreviatedOutputDescriptors: () => 'disposable-public-origin', + }); + evaluate(declarations('src/store/sagas/utxos.ts', [ + 'currentOriginApp', 'originUiAction', 'persistTagsForOrigin', 'persistSpendabilityForOrigin', + 'backupCommittedLabels', 'addLabelsWorker', 'bulkUpdateLabelsWorker', 'importLabelsWorker', + 'transactionLabelsWorker', 'markUTXOSpendabilityWorker', + ]), sagas.scope, 'production-label-producers.ts'); + f.run = (name, payload, metadata = {}) => sagas.run(name, { + payload, originAppId: f.appId, + originBackupAccountSession: f.transport.currentBackupAccountSession(), + originBackupWriteIntent: f.transport.captureBackupWriteIntent(f.appId), + ...metadata, + }); + f.wallet = () => plain(f.dbManager.getObjectById(f.RealmSchema.Wallet, wallet.id)); + f.remoteImage = async () => f.image.decodeImage(f.encryption.generateEncryptionKey(f.seed), + await server.snapshot(f.appId), f.appId, () => {}, true); + return f; +} + +test('native add/import/edit label producers each use one committed HTTP mutation and final readback', async t => { + const f = await setup(t); + await f.run('addLabelsWorker', { + txId: 'first', wallet: f.wallet(), labels: [{ name: 'original', isSystem: false }], type: 'TXN', + }); + assert.equal(writes().length, 1); + assert.equal(writes()[0].operation, 'modifyLabels'); + assert.equal(writes()[0].committed, true); + assert.equal((await f.remoteImage()).labels.firstoriginal.label, 'original'); + server.clearTrace(); + await f.run('importLabelsWorker', { labels: [ + { type: 'tx', ref: 'second', label: 'import one', origin: 'disposable-public-origin' }, + { type: 'tx', ref: 'third', label: 'import two', origin: 'disposable-public-origin' }, + ] }); + assert.equal(writes().length, 1); + assert.equal(Object.keys((await f.remoteImage()).labels).length, 3); + server.clearTrace(); + await f.run('bulkUpdateLabelsWorker', { + txId: 'first', wallet: f.wallet(), + labelChanges: { added: [{ name: 'edited', isSystem: false }], deleted: [{ name: 'original', isSystem: false }] }, + }); + assert.equal(writes().length, 1); + const image = await f.remoteImage(); + assert.equal(image.labels.firstoriginal, undefined); + assert.equal(image.labels.firstedited.label, 'edited'); + assert.equal(f.store.getState().bhr.pendingAllBackupByAppId[f.appId], false); +}); + +test('native send labels group the transaction note and all change labels into one HTTP mutation', async t => { + const f = await setup(t, [label('input:0', 'inherited')]); + await f.run('transactionLabelsWorker', { + txId: 'sent', wallet: f.wallet(), note: 'send note', + finalOutputs: [{ address: 'recipient-address' }, { address: 'change-address' }], inputs: [{ txId: 'input', vout: 0 }], + }); + assert.equal(writes().length, 1); + assert.equal(writes()[0].committed, true); + const image = await f.remoteImage(); + for (const id of ['sentsend note', 'sent:1Change', 'sent:1inherited']) + assert.deepEqual(plain(image.labels[id]), plain(f.dbManager.getObjectById(f.RealmSchema.Tags, id))); + assert.equal(f.store.getState().bhr.pendingAllBackupByAppId[f.appId], false); +}); + +test('native invalid second imported tag rolls back the first and generates no HTTP writes', async t => { + const f = await setup(t); + await f.run('importLabelsWorker', { labels: [ + { type: 'tx', ref: 'first', label: 'must roll back', origin: 'disposable-public-origin' }, + { type: 'tx', ref: 'second', label: null, origin: 'disposable-public-origin' }, + ] }); + assert.equal(f.dbManager.getCollection(f.RealmSchema.Tags).length, 0); + assert.equal(writes().length, 0); + assert.equal(Object.keys((await f.remoteImage()).labels).length, 0); + assert.equal(f.store.getState().bhr.pendingAllBackupByAppId[f.appId], true); + assert.equal(f.transport.captureBackupWriteIntent(f.appId), undefined); +}); + +test('native manual coin choice is committed before the encrypted wallet reaches HTTP', async t => { + const f = await setup(t); + await f.run('markUTXOSpendabilityWorker', { + wallet: f.wallet(), txId: 'input', vout: 0, spendability: 'doNotSpend', + }); + assert.equal(f.wallet().specs.confirmedUTXOs[0].spendability, 'doNotSpend'); + assert.equal(writes().length, 1); + assert.equal(writes()[0].operation, 'updateAppImage'); + assert.equal(writes()[0].committed, true); + const image = await f.remoteImage(); + assert.equal(image.wallets['producer-wallet'].specs.confirmedUTXOs[0].spendability, 'doNotSpend'); + assert.equal(f.store.getState().bhr.pendingAllBackupByAppId[f.appId], false); +}); + +for (const mode of ['409', '404', 'malformed', 'lost-reply']) + test(`native combined send labels survive ${mode} and keep backup pending after one request`, async t => { + const f = await setup(t); + server.armFault({ mode, operation: 'modifyLabels' }); + await f.run('transactionLabelsWorker', { + txId: 'sent', wallet: f.wallet(), note: 'send note', + finalOutputs: [{ address: 'change-address' }], inputs: [], + }); + assert.equal(writes().length, 1); + for (const id of ['sentsend note', 'sent:0Change']) + assert.ok(f.dbManager.getObjectById(f.RealmSchema.Tags, id)); + assert.equal(f.store.getState().bhr.pendingAllBackupByAppId[f.appId], true); + assert.equal(f.transport.captureBackupWriteIntent(f.appId), undefined); + }); + +test('native old-session transaction labels cannot commit after A to B to A', async t => { + const f = await setup(t); + const old = { + originBackupAccountSession: f.transport.currentBackupAccountSession(), + originBackupWriteIntent: f.transport.captureBackupWriteIntent(f.appId), + }; + const originalWallet = f.wallet(); + await f.selectAccount('disposable-other-account'); + await f.selectAccount(f.appId); + await f.run('transactionLabelsWorker', { txId: 'sent', wallet: originalWallet, note: 'old session' }, old); + assert.equal(f.dbManager.getCollection(f.RealmSchema.Tags).length, 0); + assert.equal(writes().length, 0); +}); diff --git a/tests/recovery/labelAccount.test.cjs b/tests/recovery/labelAccount.test.cjs index 11097082ab..2cb863aaff 100644 --- a/tests/recovery/labelAccount.test.cjs +++ b/tests/recovery/labelAccount.test.cjs @@ -6,6 +6,7 @@ const vm = require('node:vm'); const ts = require('typescript'); const { runSaga } = require('redux-saga'); const { call, delay, put } = require('redux-saga/effects'); +const { encryption } = require('./sagaHarness.cjs'); const file = 'src/store/sagas/utxos.ts'; const source = ts.createSourceFile( @@ -13,7 +14,7 @@ const source = ts.createSourceFile( ); const names = new Set([ 'currentOriginApp', 'persistTagsForOrigin', 'persistSpendabilityForOrigin', - 'originUiAction', + 'originUiAction', 'backupCommittedLabels', 'transactionLabelsWorker', 'addLabelsWorker', 'bulkUpdateLabelsWorker', 'importLabelsWorker', 'markUTXOSpendabilityWorker', ]); const extracted = source.statements @@ -24,10 +25,13 @@ const code = ts.transpileModule(extracted, { compilerOptions: { target: ts.ScriptTarget.ES2020 }, }).outputText; -function fixture({ consent = true, switchDuringCheck = false, switchDuringWalletRead = false, switchDuringDescriptor = false } = {}) { - const state = { active: 'A', reduxActive: 'A', networkType: 'MAINNET', saved: [], deleted: [], uploaded: [], uiActions: [], failed: 0, consent }; +function fixture({ consent = true, switchDuringCheck = false, switchDuringWalletRead = false, switchDuringDescriptor = false, rejectCreateAt = 0, rejectDelete = false } = {}) { + const state = { active: 'A', reduxActive: 'A', session: 0, generation: 0, networkType: 'MAINNET', saved: [], deleted: [], uploaded: [], uiActions: [], failed: 0, consent, tags: [] }; + const walletRecord = { id: 'wallet', specs: { + confirmedUTXOs: [{ txId: 'tx', vout: 0, spendability: 'spendable' }], + } }; const dbManager = { - getObjectByIndex: () => ({ id: state.active, primarySeed: `seed-${state.active}` }), + getObjectByIndex: () => ({ id: state.active, primarySeed: 'ab'.repeat(32) }), createObjectBulk: (_schema, tags) => { state.saved.push({ appId: state.active, tags }); return true; @@ -36,12 +40,12 @@ function fixture({ consent = true, switchDuringCheck = false, switchDuringWallet state.deleted.push({ appId: state.active, id }); return true; }, - getObjectById: () => { + getObjectById: (schema, id) => { + if (schema === 'Tags') return state.tags.find(tag => tag.id === id); if (switchDuringWalletRead) state.active = 'B'; - return { id: 'wallet', specs: { - confirmedUTXOs: [{ txId: 'tx', vout: 0, spendability: 'spendable' }], - } }; + return walletRecord; }, + getCollection: (schema) => schema === 'Tags' ? state.tags : [], updateObjectById: () => { state.saved.push({ appId: state.active, walletUpdated: true }); return true; @@ -49,7 +53,65 @@ function fixture({ consent = true, switchDuringCheck = false, switchDuringWallet }; const scope = { call, delay, put, dbManager, console, - currentBackupAccountSession: () => 0, + currentBackupAccountSession: () => state.session, + Realm: { UpdateMode: { All: 'all' } }, + beginBackupLocalOperation: (appId, originIntent, accountSession) => ({ appId, accountSession, originIntent }), + commitBackupLocalOperation: (operation, write) => { + if (operation.appId !== state.active || operation.accountSession !== state.session) + return { committed: false }; + const previousTags = structuredClone(state.tags); + const previousSpecs = structuredClone(walletRecord.specs); + const added = [], deleted = []; + let creations = 0; + try { + write({ + create: (schema, tag) => { + assert.equal(schema, 'Tags'); + if (++creations === rejectCreateAt) throw Error('disposable write failure'); + const index = state.tags.findIndex(saved => saved.id === tag.id); + if (index === -1) state.tags.push(structuredClone(tag)); + else state.tags[index] = structuredClone(tag); + added.push(structuredClone(tag)); + }, + objectForPrimaryKey: (schema, id) => schema === 'Tags' + ? state.tags.find(tag => tag.id === id) : walletRecord, + delete: (tag) => { + if (rejectDelete) throw Error('disposable delete failure'); + state.tags = state.tags.filter(saved => saved.id !== tag.id); + deleted.push(tag.id); + }, + }); + } catch { + state.tags = previousTags; + walletRecord.specs = previousSpecs; + return { committed: false }; + } + state.generation++; + if (added.length) state.saved.push({ appId: state.active, tags: added }); + for (const id of deleted) state.deleted.push({ appId: state.active, id }); + if (JSON.stringify(walletRecord.specs) !== JSON.stringify(previousSpecs)) + state.saved.push({ appId: state.active, walletUpdated: true }); + return { committed: true, intent: { ...operation, generation: state.generation } }; + }, + enqueueAutomaticBackupOperation: async (commit, mutations) => { + if (switchDuringCheck) { + state.active = 'B'; + state.reduxActive = 'B'; + state.session++; + } + if (!state.consent || commit.intent.appId !== state.active || + commit.intent.accountSession !== state.session) + return { updated: false, backupPending: true }; + for (const mutation of mutations) { + assert.equal(mutation.operation, 'modifyLabels'); + state.uploaded.push({ + appId: mutation.body.appId, + added: mutation.body.addLabels, + removed: mutation.body.deleteLabels, + }); + } + return { updated: true, backupConfirmed: true }; + }, store: { getState: () => ({ storage: { appId: state.reduxActive }, settings: { bitcoinNetworkType: state.networkType } }) }, RealmSchema: { KeeperApp: 'KeeperApp', Tags: 'Tags', Wallet: 'Wallet', Vault: 'Vault' }, LabelRefType: { TXN: 'TXN', ADDR: 'ADDR', OUTPUT: 'OUTPUT' }, @@ -58,10 +120,8 @@ function fixture({ consent = true, switchDuringCheck = false, switchDuringWallet if (switchDuringDescriptor) state.networkType = 'TESTNET'; return 'disposable-origin'; }, - generateEncryptionKey: (seed) => seed, - hash256: (value) => `hash(${value})`, - encrypt: (_key, value) => value, - getJSONFromRealmObject: (value) => value, + ...encryption, + getJSONFromRealmObject: (value) => structuredClone(value), Relay: { modifyLabels: async (appId, added, removed) => { state.uploaded.push({ appId, added, removed }); return { updated: true }; @@ -85,9 +145,9 @@ function fixture({ consent = true, switchDuringCheck = false, switchDuringWallet resetState: () => ({ type: 'reset' }), }; vm.runInNewContext(code, scope); - const run = (name, payload, originAppId = 'A') => runSaga( + const run = (name, payload, originAppId = 'A', metadata = {}) => runSaga( { dispatch: (action) => state.uiActions.push({ ...action, active: state.active }), - getState: () => ({}) }, scope[name], { payload, originAppId } + getState: () => ({}) }, scope[name], { payload, originAppId, ...metadata } ).toPromise(); return { state, run }; } @@ -152,3 +212,83 @@ test('UTXO spendability cannot persist into B after an A action waits for Realm' assert.deepEqual(f.state.saved, []); assert.deepEqual(f.state.uploaded, []); }); + +test('a failed multi-label import rolls back the complete local batch and queues nothing', async () => { + const f = fixture({ rejectCreateAt: 2 }); + await f.run('importLabelsWorker', { labels: [ + { type: 'TX', ref: 'first', label: 'one', origin: 'fixture' }, + { type: 'TX', ref: 'second', label: 'two', origin: 'fixture' }, + ] }); + assert.deepEqual(f.state.tags, []); + assert.deepEqual(f.state.saved, []); + assert.deepEqual(f.state.uploaded, []); + assert.equal(f.state.generation, 0); +}); + +test('a failed removal rolls back label additions and preserves the existing label', async () => { + const f = fixture({ rejectDelete: true }); + const existing = { id: 'txold', label: 'old', ref: 'tx', type: 'TXN', origin: 'fixture', isSystem: false }; + f.state.tags.push(existing); + await f.run('bulkUpdateLabelsWorker', { + txId: 'tx', wallet, + labelChanges: { added: [{ name: 'new', isSystem: false }], deleted: [{ name: 'old', isSystem: false }] }, + }); + assert.deepEqual(f.state.tags, [existing]); + assert.deepEqual(f.state.saved, []); + assert.deepEqual(f.state.uploaded, []); +}); + +test('overlapping label edit queues only the deletion matching the committed local image', async () => { + const f = fixture(); + const change = { name: 'same', isSystem: false }; + await f.run('bulkUpdateLabelsWorker', { + txId: 'tx', wallet, labelChanges: { added: [change], deleted: [change] }, + }); + assert.deepEqual(f.state.tags, []); + assert.equal(f.state.generation, 1); + assert.equal(f.state.uploaded.length, 1); + assert.equal(f.state.uploaded[0].added.length, 0); + const key = encryption.generateEncryptionKey('ab'.repeat(32)); + assert.deepEqual(Array.from(f.state.uploaded[0].removed), [encryption.hash256(encryption.hash256(key + 'txsame'))]); +}); + +test('send note and inherited change-output labels share one atomic local operation and one encrypted mutation', async () => { + const f = fixture(); + f.state.tags.push({ id: 'input:0salary', ref: 'input:0', label: 'salary', isSystem: false }); + await f.run('transactionLabelsWorker', { + txId: 'sent', wallet: { ...wallet, specs: { addresses: { internal: { 0: 'change-address' } } } }, + note: 'transaction note', finalOutputs: [{ address: 'recipient' }, { address: 'change-address' }], + inputs: [{ txId: 'input', vout: 0 }], + }); + assert.equal(f.state.generation, 1); + assert.equal(f.state.saved.length, 1); + assert.equal(f.state.saved[0].tags.length, 3); + assert.equal(f.state.uploaded.length, 1); + const key = encryption.generateEncryptionKey('ab'.repeat(32)); + const tags = Array.from(f.state.uploaded[0].added, row => { + const tag = JSON.parse(encryption.decrypt(key, row.content)); + assert.equal(row.id, encryption.hash256(encryption.hash256(key + tag.id))); + assert.deepEqual(tag, f.state.tags.find(saved => saved.id === tag.id)); + return tag; + }); + assert.deepEqual(tags.map(tag => tag.id).sort(), ['sent:1Change', 'sent:1salary', 'senttransaction note']); +}); + +test('old A session send labels cannot commit after A to B to A', async () => { + const f = fixture(); + f.state.session = 2; + await f.run('transactionLabelsWorker', { txId: 'sent', wallet, note: 'old session' }, 'A', { + originBackupAccountSession: 0, + }); + assert.equal(f.state.generation, 0); + assert.deepEqual(f.state.tags, []); + assert.deepEqual(f.state.uploaded, []); +}); + +test('an empty transaction note with no change output commits and queues nothing', async () => { + const f = fixture(); + await f.run('transactionLabelsWorker', { txId: 'sent', wallet, note: '' }); + assert.equal(f.state.generation, 0); + assert.deepEqual(f.state.tags, []); + assert.deepEqual(f.state.uploaded, []); +}); diff --git a/tests/recovery/manualCoinChoices.test.cjs b/tests/recovery/manualCoinChoices.test.cjs index 63a16917b1..c4effc46f6 100644 --- a/tests/recovery/manualCoinChoices.test.cjs +++ b/tests/recovery/manualCoinChoices.test.cjs @@ -42,7 +42,34 @@ function sagaFixture(options = {}) { f.remote.vault = payload.vault; return { updated: true }; }; - loadFunctions(f.scope, 'src/store/sagas/bhr.ts', ['updateVaultImageWorker']); + // Disposable local transaction and cloud adapters exercise the production + // producer ordering. Transport proof/CAS is covered by its own fixtures. + f.scope.beginBackupLocalOperation = (appId, originIntent, accountSession) => ({ appId, accountSession, originIntent }); + f.scope.commitBackupLocalOperation = (operation, write) => { + if (options.rejectLocalWrite) return { committed: false }; + const before = clone(f.collections); + try { + write({ objectForPrimaryKey: (kind, id) => f.collections[kind].find(row => row.id === id) }); + } catch { + for (const [kind, rows] of Object.entries(before)) f.collections[kind] = rows; + return { committed: false }; + } + f.actions.push({ type: 'invalidateBackupRepair', payload: operation.appId }); + return { committed: true, intent: operation }; + }; + f.scope.enqueueAutomaticBackupOperation = async (_commit, mutations) => { + if (options.online === false) + return { updated: false, backupConfirmed: false, backupPending: true }; + for (const mutation of mutations) { + const response = await f.scope.Relay[mutation.operation](mutation.body); + if (!response.updated) return { updated: false, backupConfirmed: false, backupPending: true }; + } + return { updated: true, backupConfirmed: true, backupPending: false }; + }; + loadFunctions(f.scope, 'src/store/sagas/bhr.ts', [ + 'buildAutomaticAppMutation', 'buildAutomaticVaultMutation', + 'queueCommittedBackupWorker', 'updateVaultImageWorker', + ]); loadFunctions(f.scope, 'src/store/sagas/utxos.ts', [ 'currentOriginApp', 'persistSpendabilityForOrigin', 'markUTXOSpendabilityWorker', ]); diff --git a/tests/recovery/sendLabelOrigin.test.cjs b/tests/recovery/sendLabelOrigin.test.cjs index 4f8dbc3495..9f458415ca 100644 --- a/tests/recovery/sendLabelOrigin.test.cjs +++ b/tests/recovery/sendLabelOrigin.test.cjs @@ -4,6 +4,8 @@ const fs = require('node:fs'); const path = require('node:path'); const vm = require('node:vm'); const ts = require('typescript'); +const { runSaga } = require('redux-saga'); +const { call, put, select } = require('redux-saga/effects'); const root = path.join(__dirname, '../..'); function sourceFile(file) { @@ -126,3 +128,53 @@ test('an async label import from an old A session cannot rebase after A to B to assert.deepEqual(seen, []); assert.equal(errors.length, 1); }); + +for (const phase of ['Two', 'Three']) test(`send phase ${phase} forwards note and change output through one child operation with the original intent`, async () => { + const source = sourceFile('src/store/sagas/send_and_receive.ts'); + const names = new Set([`sendPhase${phase}Worker`, 'handleChangeOutputLabels']); + const extracted = source.statements + .filter(node => ts.isFunctionDeclaration(node) && names.has(node.name?.text)) + .map(node => node.getText(source)).join('\n'); + const labels = []; + const finalOutputs = [{ address: 'recipient' }, { address: 'change-address' }]; + const inputs = [{ txId: 'input', vout: 0 }]; + const intent = { appId: 'A', accountSession: 7, baseRevision: 'a'.repeat(64), baselineEpoch: 3 }; + const context = { + call, put, select, console, + _: require('lodash'), idx: (value, getter) => getter(value), + EntityKind: { WALLET: 'WALLET', VAULT: 'VAULT' }, + ELECTRUM_CLIENT: { isClientConnected: true }, + ELECTRUM_NOT_CONNECTED_ERR: 'offline', ELECTRUM_NOT_CONNECTED_ERR_TOR: 'tor-offline', + WalletOperations: { + transferST2: async () => ({ txid: 'sent', finalOutputs, inputs }), + transferST3: async () => ({ txid: 'sent', finalOutputs, inputs }), + }, + transactionLabelsWorker: function* (action) { labels.push(action); }, + sendPhaseTwoExecuted: payload => ({ type: 'phase-two', payload }), + sendPhaseThreeExecuted: payload => ({ type: 'phase-three', payload }), + setElectrumNotConnectedErr: error => ({ type: 'error', error }), + }; + vm.runInNewContext(ts.transpileModule(extracted, { + compilerOptions: { target: ts.ScriptTarget.ES2020 }, + }).outputText, context); + const actions = []; + await runSaga({ + getState: () => ({ sendAndReceive: { + sendPhaseOne: { outputs: { txPrerequisites: {} } }, + customPrioritySendPhaseOne: { outputs: { customTxPrerequisites: {} } }, + sendPhaseTwo: { serializedPSBTEnvelops: [] }, + } }), + dispatch: action => actions.push(action), + }, context[`sendPhase${phase}Worker`], { + originAppId: 'A', originBackupAccountSession: 7, originBackupWriteIntent: intent, + payload: { wallet: { id: 'wallet', entityKind: 'WALLET' }, note: 'one note' }, + }).toPromise(); + assert.equal(actions[0].payload.successful, true); + assert.equal(labels.length, 1); + assert.equal(labels[0].originAppId, 'A'); + assert.equal(labels[0].originBackupAccountSession, 7); + assert.equal(labels[0].originBackupWriteIntent, intent); + assert.equal(labels[0].payload.note, 'one note'); + assert.equal(labels[0].payload.finalOutputs, finalOutputs); + assert.equal(labels[0].payload.inputs, inputs); +}); From 949cd3a8f311585169684d180ea895fd7cbc5858 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Tue, 13 Oct 2026 03:23:25 +0000 Subject: [PATCH 150/254] fix: observe recovery generation after atomic Realm commit --- src/services/backup/localMutation.ts | 12 +- src/storage/realm/dbManager.ts | 30 ++-- src/storage/realm/realm.ts | 112 ++++++++++--- .../realmCommittedGeneration.test.cjs | 147 ++++++++++++++++++ tests/recovery/realmOpenGeneration.test.cjs | 5 + 5 files changed, 273 insertions(+), 33 deletions(-) create mode 100644 tests/recovery/realmCommittedGeneration.test.cjs diff --git a/src/services/backup/localMutation.ts b/src/services/backup/localMutation.ts index 02f9df7eea..2930f4d18f 100644 --- a/src/services/backup/localMutation.ts +++ b/src/services/backup/localMutation.ts @@ -2,6 +2,7 @@ import { RealmSchema } from 'src/storage/realm/enum'; import { canonical, recoveryContent } from './image'; const backedUpSchemas = new Set([ + RealmSchema.KeeperApp, RealmSchema.Wallet, RealmSchema.USDTWallet, RealmSchema.Vault, @@ -20,6 +21,10 @@ function plain(record: any) { } function recoveryView(schema: RealmSchema, record: any) { + if (schema === RealmSchema.KeeperApp) return { + id: record.id, publicId: record.publicId, primarySeed: record.primarySeed, + version: record.version, networkType: record.networkType, subscription: record.subscription, + }; if (schema === RealmSchema.Wallet || schema === RealmSchema.USDTWallet) return recoveryContent('wallets', record); if (schema === RealmSchema.Vault) return recoveryContent('vaults', record); @@ -29,9 +34,10 @@ function recoveryView(schema: RealmSchema, record: any) { return record; } -// Run before the Realm write. This also covers helpers that may commit some -// fields before returning failure. Cache-only wallet refreshes and active-node -// rotation cannot change the encrypted recovery image. +// Run before the Realm write to conservatively invalidate UI verification, +// including writes that later fail. Generation proof comes only from the +// separate successful post-commit observer. Cache-only wallet refreshes and +// active-node rotation cannot change the encrypted recovery image. export function changesBackedUpRecovery(schema: RealmSchema, previous: any, patch: any) { if (!isBackedUpSchema(schema) || !previous || !patch || typeof patch !== 'object') return false; try { diff --git a/src/storage/realm/dbManager.ts b/src/storage/realm/dbManager.ts index 2d66de3167..2324dd46c6 100644 --- a/src/storage/realm/dbManager.ts +++ b/src/storage/realm/dbManager.ts @@ -1,11 +1,14 @@ import { changesBackedUpRecovery, isBackedUpSchema } from 'src/services/backup/localMutation'; import { RealmSchema } from './enum'; import realm from './realm'; +import type { RecoveryCommitObserver } from './realm'; let recoveryMutationObserver: () => void = () => undefined; const setRecoveryMutationObserver = (observer: () => void) => { recoveryMutationObserver = observer; }; +const setRecoveryCommitObserver = (observer: RecoveryCommitObserver) => + realm.setRecoveryCommitObserver(observer); const notifyRecoveryCreateOrDelete = (schema: RealmSchema, hasObject: boolean) => { if (hasObject && isBackedUpSchema(schema)) recoveryMutationObserver(); }; @@ -44,6 +47,7 @@ const createObject = (schema: RealmSchema, object: any, updateMode = Realm.Updat return hasCreated; } catch (err) { console.log(err); + return false; } }; @@ -63,6 +67,7 @@ const createObjectBulk = ( return hasCreated; } catch (err) { console.log(err); + return false; } }; @@ -120,11 +125,11 @@ const updateObjectById = (schema: RealmSchema, id: string, updateProps: any) => try { const object = getObjectById(schema, id); notifyRecoveryUpdate(schema, object, updateProps); - for (const [key, value] of Object.entries(updateProps)) { - realm.write(() => { + realm.write(() => { + for (const [key, value] of Object.entries(updateProps)) { object[key] = value; - }); - } + } + }); return true; } catch (err) { console.error({ err }); @@ -147,11 +152,11 @@ const updateObjectByPrimaryId = ( try { const object = getObjectByPrimaryId(schema, name, primaryId); notifyRecoveryUpdate(schema, object, updateProps); - for (const [key, value] of Object.entries(updateProps)) { - realm.write(() => { + realm.write(() => { + for (const [key, value] of Object.entries(updateProps)) { object[key] = value; - }); - } + } + }); return true; } catch (err) { console.error({ err }); @@ -234,11 +239,11 @@ const updateObjectByQuery = ( const object = objects.find(queryFn); if (object) { notifyRecoveryUpdate(schema, object, updateProps); - for (const [key, value] of Object.entries(updateProps)) { - realm.write(() => { + realm.write(() => { + for (const [key, value] of Object.entries(updateProps)) { object[key] = value; - }); - } + } + }); return true; } return false; @@ -288,4 +293,5 @@ export default { updateObjectByQuery, getObjectByQuery, setRecoveryMutationObserver, + setRecoveryCommitObserver, }; diff --git a/src/storage/realm/realm.ts b/src/storage/realm/realm.ts index 00e0514d37..9817bccd48 100644 --- a/src/storage/realm/realm.ts +++ b/src/storage/realm/realm.ts @@ -1,13 +1,35 @@ import Realm from 'realm'; import { captureError } from 'src/services/sentry'; +import { captureLocalRecovery } from 'src/services/backup/localSnapshot'; import { RealmSchema } from './enum'; import schema from './schema'; import { runRealmMigrations } from './migrations'; + export const REALM_FILE = 'keeper.realm'; +export type RecoveryCommitObserver = (appId: string, fingerprint: string) => void; +type TransactionState = { failed: boolean; error?: unknown }; + +function captureRecovery(database: Realm) { + try { + return captureLocalRecovery(database); + } catch { + // Initial account setup may not yet have a recoverable KeeperApp. An + // unreadable/partial image cannot supply a committed generation proof. + return undefined; + } +} export class RealmDatabase { private realm: Realm; + private transactions = new WeakMap(); + + private recoveryCommitObserver: RecoveryCommitObserver | undefined; + + public setRecoveryCommitObserver = (observer: RecoveryCommitObserver) => { + this.recoveryCommitObserver = observer; + }; + private openGeneration = 0; private pendingOpenGeneration: number | null = null; @@ -114,7 +136,63 @@ export class RealmDatabase { * It handles operations in a single, idempotent update. A write transaction is all or nothing * @param {} callback */ - public writeTransaction = (realm: Realm, callback) => realm.write(callback); + public writeTransaction = (database: Realm, callback: () => T): T => { + const execute = () => { + // Reject native async callbacks before their first side effect. Other + // thenable-returning callbacks are rejected inside the transaction. + if (Object.prototype.toString.call(callback) === '[object AsyncFunction]') + throw new Error('Realm transaction callbacks must be synchronous'); + const result = callback(); + if ( + result !== null && + (typeof result === 'object' || typeof result === 'function') && + typeof (result as { then?: unknown }).then === 'function' + ) throw new Error('Realm transaction callbacks must be synchronous'); + return result; + }; + const outer = this.transactions.get(database); + if (outer) { + try { + return execute(); + } catch (error) { + // Even if a nested helper converts this exception to false, the outer + // transaction must roll back every preceding write in the group. + outer.failed = true; + outer.error = error; + throw error; + } + } + if (database.isInTransaction) + throw new Error('Recovery writes require a managed outer Realm transaction'); + + const before = captureRecovery(database); + const transaction: TransactionState = { failed: false }; + this.transactions.set(database, transaction); + let result: T; + try { + result = database.write(() => { + const value = execute(); + if (transaction.failed) throw transaction.error; + return value; + }); + } finally { + this.transactions.delete(database); + } + + // Realm.write has returned: the native transaction successfully committed. + // Cache/history-only changes compare equal and do not advance generation. + const after = captureRecovery(database); + if (after && before?.fingerprint !== after.fingerprint) { + try { + this.recoveryCommitObserver?.(after.app.id, after.fingerprint); + } catch { + // A notification failure cannot turn a successful local save into a + // reported failed transaction. No private snapshot is sent to Sentry. + captureError(new Error('Recovery commit observer unavailable')); + } + } + return result; + }; /** * close the database when done with a realm instance to avoid memory leaks. @@ -155,11 +233,11 @@ export class RealmDatabase { public createBulk = (schema: RealmSchema, objects: any[], updateMode) => { const realm = this.getDatabase(); try { - for (const object of objects) { - this.writeTransaction(realm, () => { + this.writeTransaction(realm, () => { + for (const object of objects) { realm.create(schema, object, updateMode); - }); - } + } + }); return true; } catch (err) { @@ -178,6 +256,7 @@ export class RealmDatabase { return realm.objects(schema); } catch (err) { console.log({ err }); + return undefined; } }; @@ -187,11 +266,8 @@ export class RealmDatabase { */ public write = (callback) => { const realm = this.getDatabase(); - try { - this.writeTransaction(realm, callback); - } catch (err) { - console.log({ err }); - } + this.writeTransaction(realm, callback); + return true; }; /** @@ -200,14 +276,14 @@ export class RealmDatabase { */ public delete = (object: any) => { const realm = this.getDatabase(); - try { - this.writeTransaction(realm, () => { - realm.delete(object); - }); - } catch (err) { - console.log({ err }); - } + this.writeTransaction(realm, () => { + realm.delete(object); + }); + return true; }; } -export default new RealmDatabase(); +const realmDatabase = new RealmDatabase(); +export const setRecoveryCommitObserver = (observer: RecoveryCommitObserver) => + realmDatabase.setRecoveryCommitObserver(observer); +export default realmDatabase; diff --git a/tests/recovery/realmCommittedGeneration.test.cjs b/tests/recovery/realmCommittedGeneration.test.cjs new file mode 100644 index 0000000000..695134f5f4 --- /dev/null +++ b/tests/recovery/realmCommittedGeneration.test.cjs @@ -0,0 +1,147 @@ +const { test, after, mock } = require('node:test'); +const assert = require('node:assert/strict'); +const { createRealmClient, plain, Realm } = require('./automaticWriteRealmRelay.fixture.cjs'); + +// Native Realm and complete production schemas/snapshot/wrapper, with only the +// fixture's unused React Native/MMKV boundary adapters. No Relay request is made. +async function fixture(run) { + const client = await createRealmClient('http://127.0.0.1:1/'); + const commits = []; + let invalidations = 0; + client.dbManager.setRecoveryCommitObserver((appId, fingerprint) => { + assert.equal(client.nativeRealm().isInTransaction, false); + const current = client.load('src/services/backup/localSnapshot').captureLocalRecovery(client.nativeRealm()); + assert.equal(appId, current.app.id); + assert.equal(fingerprint, current.fingerprint); + commits.push({ appId, fingerprint }); + }); + client.dbManager.setRecoveryMutationObserver(() => { invalidations += 1; }); + const label = (id, patch = {}) => client.defaults(client.RealmSchema.Tags, { + id, type: 'txn', ref: 'disposable-reference', label: 'before', ...patch, + }); + const counts = () => ({ commits: commits.length, invalidations }); + try { + await run({ ...client, commits, label, counts }); + } finally { + await client.close(); + mock.restoreAll(); + } +} +after(() => Realm.shutdown()); + +test('whole-array create is one native commit and a bad later record rolls back the entire array', () => fixture(async (f) => { + const { Tags } = f.RealmSchema; + assert.equal(f.dbManager.createObjectBulk(Tags, [f.label('label-a'), f.label('label-b')]), true); + assert.equal(f.nativeRealm().objects(Tags).length, 2); + assert.deepEqual(f.counts(), { commits: 1, invalidations: 1 }); + mock.method(console, 'log', () => {}); + assert.equal(f.dbManager.createObjectBulk(Tags, [f.label('label-c'), f.label('label-d', { ref: 42 })]), false); + assert.equal(f.nativeRealm().objects(Tags).length, 2); + assert.equal(f.nativeRealm().objectForPrimaryKey(Tags, 'label-c'), null); + assert.deepEqual(f.counts(), { commits: 1, invalidations: 2 }, 'failed attempt invalidates UI but produces no committed generation'); +})); + +for (const helper of ['updateObjectById', 'updateObjectByPrimaryId', 'updateObjectByQuery']) + test(`${helper} rolls back every field when a later assignment fails`, () => fixture(async (f) => { + const { Tags } = f.RealmSchema; + assert.equal(f.dbManager.createObject(Tags, f.label('label-a')), true); + const update = (patch) => helper === 'updateObjectById' + ? f.dbManager[helper](Tags, 'label-a', patch) + : helper === 'updateObjectByPrimaryId' + ? f.dbManager[helper](Tags, 'id', 'label-a', patch) + : f.dbManager[helper](Tags, (record) => record.id === 'label-a', patch); + mock.method(console, 'error', () => {}); + assert.equal(update({ label: 'partially changed', ref: 42 }), false); + const record = f.nativeRealm().objectForPrimaryKey(Tags, 'label-a'); + assert.equal(record.label, 'before'); + assert.equal(record.ref, 'disposable-reference'); + assert.deepEqual(f.counts(), { commits: 1, invalidations: 2 }); + assert.equal(update({ label: 'committed', ref: 'new reference' }), true); + assert.equal(record.label, 'committed'); + assert.equal(record.ref, 'new reference'); + assert.deepEqual(f.counts(), { commits: 2, invalidations: 3 }); + })); + +test('nested synchronous helpers share a native outer transaction and notify once after the group', () => fixture(async (f) => { + const { Tags } = f.RealmSchema; + const native = f.nativeRealm(); + assert.equal(f.realmDatabase.write(() => { + assert.equal(native.isInTransaction, true); + assert.equal(f.dbManager.createObject(Tags, f.label('label-a')), true); + assert.equal(f.realmDatabase.write(() => { + assert.equal(f.dbManager.createObject(Tags, f.label('label-b')), true); + }), true); + assert.equal(f.commits.length, 0, 'a nested helper cannot publish an uncommitted generation'); + }), true); + assert.equal(native.objects(Tags).length, 2); + assert.equal(f.commits.length, 1); +})); + +test('a caught nested write failure still poisons the outer group and rolls back all local effects', () => fixture(async (f) => { + const { Tags } = f.RealmSchema; + mock.method(console, 'log', () => {}); + assert.throws(() => f.realmDatabase.write(() => { + assert.equal(f.dbManager.createObject(Tags, f.label('label-a')), true); + assert.equal(f.dbManager.createObjectBulk(Tags, [f.label('label-b'), f.label('label-c', { ref: 42 })]), false); + assert.equal(f.dbManager.createObject(Tags, f.label('label-d')), true); + })); + assert.equal(f.nativeRealm().objects(Tags).length, 0); + assert.equal(f.commits.length, 0); + assert.equal(f.counts().invalidations, 3); +})); + +test('write and delete failures throw and thenable callbacks roll back without a generation', () => fixture(async (f) => { + const { Tags } = f.RealmSchema; + assert.equal(f.dbManager.createObject(Tags, f.label('label-a')), true); + const record = f.nativeRealm().objectForPrimaryKey(Tags, 'label-a'); + assert.throws(() => f.realmDatabase.write(() => { record.label = 'bad'; throw new Error('synthetic failure'); }), /synthetic failure/); + assert.equal(record.label, 'before'); + assert.throws(() => f.realmDatabase.delete(undefined)); + assert.throws(() => f.realmDatabase.write(() => { record.label = 'bad'; return { then() {} }; }), /synchronous/); + assert.equal(record.label, 'before'); + let asyncStarted = false; + assert.throws(() => f.realmDatabase.write(async () => { asyncStarted = true; record.label = 'bad'; }), /synchronous/); + assert.equal(asyncStarted, false); + assert.equal(f.commits.length, 1); + assert.equal(f.realmDatabase.delete(record), true); + assert.equal(f.nativeRealm().objects(Tags).length, 0); + assert.equal(f.commits.length, 2); +})); + +test('cache, node-connection and history-only writes do not advance committed recovery generation', () => fixture(async (f) => { + const { Wallet, NodeConnect, UAI } = f.RealmSchema; + assert.equal(f.dbManager.createObject(Wallet, f.defaults(Wallet, { id: 'wallet-a' })), true); + const wallet = f.nativeRealm().objectForPrimaryKey(Wallet, 'wallet-a'); + assert.equal(f.dbManager.createObject(NodeConnect, f.defaults(NodeConnect, { id: 1, host: 'fixture' })), true); + const before = f.counts(); + assert.equal(f.dbManager.updateObjectById(Wallet, 'wallet-a', { + specs: { ...plain(wallet.specs), balances: { confirmed: 123, unconfirmed: 456 }, lastSynched: 123 }, + }), true); + assert.equal(f.dbManager.updateObjectByQuery(NodeConnect, (node) => node.id === 1, { isConnected: true }), true); + assert.equal(f.dbManager.createObject(UAI, f.defaults(UAI)), true); + assert.deepEqual(f.counts(), before); + assert.equal(f.dbManager.updateObjectById(Wallet, 'wallet-a', { + specs: { ...plain(wallet.specs), nextFreeAddressIndex: wallet.specs.nextFreeAddressIndex + 1 }, + }), true); + assert.deepEqual(f.counts(), { commits: before.commits + 1, invalidations: before.invalidations + 1 }); +})); + +test('KeeperApp recovery metadata advances generation while analytics-only changes do not', () => fixture(async (f) => { + const { KeeperApp } = f.RealmSchema; + const app = f.nativeRealm().objectForPrimaryKey(KeeperApp, f.appId); + assert.equal(f.dbManager.updateObjectById(KeeperApp, f.appId, { enableAnalytics: !app.enableAnalytics }), true); + assert.deepEqual(f.counts(), { commits: 0, invalidations: 0 }); + for (const patch of [{ version: '2.6.4-fixture' }, { publicId: 'new fixture identity' }, { networkType: 'MAINNET' }]) { + const before = f.counts(); + assert.equal(f.dbManager.updateObjectById(KeeperApp, f.appId, patch), true); + assert.deepEqual(f.counts(), { commits: before.commits + 1, invalidations: before.invalidations + 1 }); + } +})); + +test('observer failure cannot report a successfully committed local write as failed', () => fixture(async (f) => { + const { KeeperApp } = f.RealmSchema; + f.dbManager.setRecoveryCommitObserver(() => { throw new Error('synthetic observer failure'); }); + assert.equal(f.dbManager.updateObjectById(KeeperApp, f.appId, { version: '2.6.4-fixture' }), true); + assert.equal(f.nativeRealm().objectForPrimaryKey(KeeperApp, f.appId).version, '2.6.4-fixture'); + assert.deepEqual(f.diagnostics, ['Error']); +})); diff --git a/tests/recovery/realmOpenGeneration.test.cjs b/tests/recovery/realmOpenGeneration.test.cjs index 5ec09c4a71..c595268a0c 100644 --- a/tests/recovery/realmOpenGeneration.test.cjs +++ b/tests/recovery/realmOpenGeneration.test.cjs @@ -29,6 +29,11 @@ function fixture() { { realm: { default: Realm, __esModule: true }, 'src/services/sentry': { captureError: (error) => errors.push(error) }, + // These tests prove only open/close races; the fake handles intentionally + // provide no recovery content or post-commit generation evidence. + 'src/services/backup/localSnapshot': { captureLocalRecovery() { + throw new Error('Recovery content unavailable in open-race fixture'); + } }, './schema': { default: [], __esModule: true }, './migrations': { runRealmMigrations() {} }, './enum': { RealmSchema: {} }, From 1efd75f4aa1cce8c1d2d2dd4c2fe5ba7bc1ec26b Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Tue, 13 Oct 2026 03:24:23 +0000 Subject: [PATCH 151/254] test(backup): verify grouped upgrades with native Realm and Relay --- .../automaticUpgradeProducer.integration.cjs | 235 ++++++++++++++++++ 1 file changed, 235 insertions(+) create mode 100644 tests/recovery/automaticUpgradeProducer.integration.cjs diff --git a/tests/recovery/automaticUpgradeProducer.integration.cjs b/tests/recovery/automaticUpgradeProducer.integration.cjs new file mode 100644 index 0000000000..129bc78a0a --- /dev/null +++ b/tests/recovery/automaticUpgradeProducer.integration.cjs @@ -0,0 +1,235 @@ +// Whole-upgrade producer proof: production source, encrypted native Realm, +// real Redux/AES and transactional loopback Relay. Prerequisites fail loudly. +const assert = require('node:assert/strict'); +const { test, before, after } = require('node:test'); +const { runSaga } = require('redux-saga'); +const { createRealmClient, declarations, evaluate, plain, Realm } = require('./automaticWriteRealmRelay.fixture.cjs'); +const fixtureModule = process.env.KEEPER_RELAY_FIXTURE_MODULE; +assert.ok(fixtureModule, 'Set KEEPER_RELAY_FIXTURE_MODULE to Relay tests/support/automaticBackupHttpFixture.cjs'); +const { createFixture } = require(fixtureModule); +let server; +before(async () => { server = await createFixture(); }); +after(async () => { await server?.close(); Realm.shutdown(); }); +const writes = () => server.trace.filter((entry) => entry.path === '/revisionedBackupMutation'); +const tick = () => new Promise((resolve) => setImmediate(resolve)); + +function barrier() { + let release; + const promise = new Promise((resolve) => { release = resolve; }); + return { promise, release }; +} + +function wallet(f) { + return f.defaults(f.RealmSchema.Wallet, { + id: 'upgrade-wallet', entityKind: f.enums.EntityKind.WALLET, type: f.enums.WalletType.DEFAULT, + networkType: f.enums.NetworkType.TESTNET, scriptType: 'P2WPKH', + derivationDetails: f.defaults(f.RealmSchema.WalletDerivationDetails, { instanceNum: 0, xDerivationPath: "m/84'/1'/0'" }), + presentationData: f.defaults(f.RealmSchema.WalletPresentationData, { name: 'Disposable legacy wallet', visibility: 'DEFAULT' }), + specs: f.defaults(f.RealmSchema.WalletSpecs, { xpub: 'disposable-upgrade-public-wallet', nextFreeAddressIndex: 1 }), + }); +} + +function persistVaults(f) { + const key = f.defaults(f.RealmSchema.Signer, { + masterFingerprint: 'AABB0011', type: f.enums.SignerType.COLDCARD, + storageType: f.enums.SignerStorage.COLD, networkType: f.enums.NetworkType.TESTNET, + signerName: 'Disposable public signer', + signerXpubs: f.defaults(f.RealmSchema.SignerXpubs, { + [f.enums.XpubTypes.P2WSH]: [{ xpub: 'disposable-upgrade-public-vault', xpriv: null, + derivationPath: "m/48'/1'/0'/2'" }], + }), + }); + key.id = f.utilities.getKeyUID(key); + assert.equal(f.dbManager.createObject(f.RealmSchema.Signer, key), true); + for (const [id, archived] of [['upgrade-vault', false], ['upgrade-old-vault', true]]) { + assert.equal(f.dbManager.createObject(f.RealmSchema.Vault, f.defaults(f.RealmSchema.Vault, { + id, archived, entityKind: f.enums.EntityKind.VAULT, type: f.enums.VaultType.DEFAULT, + networkType: f.enums.NetworkType.TESTNET, isMultiSig: true, scriptType: 'P2WSH', + scheme: f.defaults(f.RealmSchema.VaultScheme, { m: 1, n: 1 }), + signers: [f.defaults(f.RealmSchema.VaultSigner, { + ...key.signerXpubs[f.enums.XpubTypes.P2WSH][0], masterFingerprint: key.masterFingerprint, + xfp: 'CCDD0022', registeredVaults: [], + })], + presentationData: f.defaults(f.RealmSchema.VaultPresentationData, { name: id, visibility: 'DEFAULT' }), + specs: f.defaults(f.RealmSchema.VaultSpecs, { xpubs: ['disposable-upgrade-public-vault'], nextFreeAddressIndex: 1 }), + })), true); + } +} + +const upgradeDeclarations = [ + 'LABELS_INTRODUCTION_VERSION', 'BIP329_INTRODUCTION_VERSION', 'KEY_MANAGEMENT_VERSION', + 'APP_KEY_UPGRADE_VERSION', 'HEALTH_CHECK_TIMELINE_MIGRATION_VERSION', 'SIGNER_POLICY_MIGRATION_VERSION', + 'upgradeAccountIsActive', 'beginUpgradeGroup', 'completeUpgradeGroup', 'stageVersionUpdate', + 'updateReleaseTopics', 'applyUpgradeSequence', 'updateVersionHistoryWorker', 'migrateLablesWorker', + 'migrateStructureforSignersInAppImage', 'migrateStructureforVaultInAppImage', 'updateAppKeysToEnableSigning', + 'mapAppKeysToWallets', 'generateExtendedKeysForSigners', 'updateVaultSigners', 'updateSignerDetails', + 'updateSignerXpubs', 'healthCheckTimelineMigration', 'migrateServerKeyPolicy', +]; + +async function setup(t) { + await server.reset(); + const f = await createRealmClient(server.url); + t.after(() => f.close()); + f.automatic = f.load('src/services/backup/automatic'); + f.installProductionStoreObservers(f.automatic); + f.store.dispatch(f.bhr.setAutomaticCloudBackup({ appId: f.appId, enabled: true })); + f.nativeRealm().write(() => { f.nativeRealm().objectForPrimaryKey(f.RealmSchema.KeeperApp, f.appId).version = '1.0.5'; }); + assert.equal(f.dbManager.createObject(f.RealmSchema.Wallet, wallet(f)), true); + persistVaults(f); + assert.equal(f.dbManager.createObject(f.RealmSchema.UTXOInfo, f.defaults(f.RealmSchema.UTXOInfo, { + id: 'legacy-utxo', walletId: 'upgrade-wallet', txId: 'public-legacy-tx', vout: 0, + labels: [f.defaults(f.RealmSchema.Label, { name: 'legacy note', type: 'OUTPUT' })], + })), true); + await tick(); + await server.seed(await f.payloadForCurrentImage()); + const sagas = f.productionSagas([ + 'currentBackupAppId', 'backupInspectionOwners', 'runBackupInspection', + 'buildAutomaticAppMutation', 'buildAutomaticVaultMutation', + ], { + semver: require('semver'), DeviceInfo: { getBuildNumber: () => '627' }, + getApp: () => ({}), getMessaging: () => ({}), subscribeToTopic() {}, unsubscribeFromTopic() {}, + getReleaseTopic: (version) => version, + ...f.load('src/models/interfaces/HeathCheckTypes'), + // These unspendable public fixtures need only public label metadata. No + // key derivation, encryption, Realm method or backup algorithm is mocked. + generateAbbreviatedOutputDescriptors: () => 'disposable-public-upgrade-origin', + }); + evaluate(declarations('src/store/sagas/upgrade.ts', upgradeDeclarations), sagas.scope, 'production-upgrade-producers.ts'); + assert.equal(await sagas.run('runBackupInspection', false, f.appId), true); + f.sagas = sagas; + f.origin = { + originBackupAccountSession: f.transport.currentBackupAccountSession(), + originBackupWriteIntent: f.transport.captureBackupWriteIntent(f.appId), + }; + assert.ok(f.origin.originBackupWriteIntent); + f.upgrade = (metadata = {}) => sagas.run('applyUpgradeSequence', { + previousVersion: '1.0.5', newVersion: '2.6.4', ...f.origin, ...metadata, + }); + f.imageNow = () => f.repair.captureBackupImage(); + f.remoteImage = async () => f.image.decodeImage(f.encryption.generateEncryptionKey(f.seed), + await server.snapshot(f.appId), f.appId, () => {}, true); + server.clearTrace(); + return f; +} + +function assertPending(f) { + const state = f.store.getState().bhr; + assert.equal(state.pendingAllBackupByAppId[f.appId], true); + assert.notEqual(state.backupRepairCompletedByAppId[f.appId], true); + assert.notEqual(state.backupRepairStateByAppId[f.appId], 'verified'); +} + +function assertLocalUpgrade(f) { + assert.equal(f.dbManager.getObjectById(f.RealmSchema.KeeperApp, f.appId).version, '2.6.4'); + assert.equal(f.dbManager.getCollection(f.RealmSchema.VersionHistory).length, 1); + assert.equal(f.dbManager.getCollection(f.RealmSchema.Tags).length, 1); + assert.equal(f.dbManager.getObjectById(f.RealmSchema.Vault, 'upgrade-old-vault').archived, true); +} + +test('native launch upgrade commits labels, health and version before one app/live/archive/label CAS fanout', async (t) => { + const f = await setup(t); + assert.equal(await f.upgrade(), true); + assertLocalUpgrade(f); + assert.deepEqual(writes().map((entry) => entry.operation), + ['updateAppImage', 'updateVaultImage', 'updateVaultImage', 'modifyLabels']); + assert.ok(writes().every((entry) => entry.committed)); + const local = await f.imageNow(); + const remote = await f.remoteImage(); + assert.equal(await f.image.compareImages(local, remote), 'matched'); + assert.equal((await server.snapshot(f.appId)).appImage.version, '2.6.4'); + assert.equal(remote.vaults['upgrade-old-vault'].archived, true); + assert.equal(f.store.getState().bhr.pendingAllBackupByAppId[f.appId], false); + assert.equal(f.store.getState().bhr.backupRepairCompletedByAppId[f.appId], true); +}); + +test('native invalid late version field rolls back every staged label/health edit and sends nothing', async (t) => { + const f = await setup(t); + const before = await f.imageNow(); + assert.equal(await f.upgrade({ newVersion: null }), false); + assert.equal(f.dbManager.getObjectById(f.RealmSchema.KeeperApp, f.appId).version, '1.0.5'); + assert.equal(f.dbManager.getCollection(f.RealmSchema.VersionHistory).length, 0); + assert.equal(f.dbManager.getCollection(f.RealmSchema.Tags).length, 0); + assert.equal(await f.image.compareImages(before, await f.imageNow()), 'matched'); + assert.equal(writes().length, 0); + assert.equal((await server.snapshot(f.appId)).appImage.version, '1.0.5'); +}); + +test('native no-original-baseline upgrade preserves local migrated data with zero HTTP mutations', async (t) => { + const f = await setup(t); + assert.equal(await f.upgrade({ originBackupWriteIntent: undefined }), true); + assertLocalUpgrade(f); + assert.equal(writes().length, 0); + assert.equal((await server.snapshot(f.appId)).appImage.version, '1.0.5'); + assertPending(f); +}); + +test('native A to B to A rejects an older originating upgrade before any local migration', async (t) => { + const f = await setup(t); + await f.selectAccount('disposable-other-account'); + await f.selectAccount(f.appId); + assert.equal(await f.upgrade(), false); + assert.equal(f.dbManager.getObjectById(f.RealmSchema.KeeperApp, f.appId).version, '1.0.5'); + assert.equal(f.dbManager.getCollection(f.RealmSchema.Tags).length, 0); + assert.equal(writes().length, 0); +}); + +test('native upgrade captured before async label read cannot commit after A to B to A', async (t) => { + const f = await setup(t); + const entered = barrier(), resume = barrier(); + let paused = false; + const gate = (next) => (effect) => { + if (!paused && effect.type === 'CALL' && effect.payload.fn === f.dbManager.getCollection && + effect.payload.args[0] === f.RealmSchema.UTXOInfo) { + paused = true; + next({ ...effect, payload: { ...effect.payload, fn: async (...args) => { + entered.release(); await resume.promise; + return f.dbManager.getCollection(...args); + } } }); + } else next(effect); + }; + const pending = runSaga({ getState: f.store.getState, dispatch: f.store.dispatch, + effectMiddlewares: [gate] }, f.sagas.scope.applyUpgradeSequence, { + previousVersion: '1.0.5', newVersion: '2.6.4', ...f.origin, + }).toPromise(); + await entered.promise; + await f.selectAccount('disposable-other-account'); + await f.selectAccount(f.appId); + resume.release(); + assert.equal(await pending, false); + assert.equal(f.dbManager.getObjectById(f.RealmSchema.KeeperApp, f.appId).version, '1.0.5'); + assert.equal(f.dbManager.getCollection(f.RealmSchema.Tags).length, 0); + assert.equal(writes().length, 0); +}); + +for (const mode of ['409', 'lost-reply']) { + test(`native upgrade ${mode} keeps all committed local migrations pending`, async (t) => { + const f = await setup(t); + server.armFault({ mode, operation: mode === '409' ? 'updateVaultImage' : 'updateAppImage' }); + assert.equal(await f.upgrade(), true); + assertLocalUpgrade(f); + assert.equal(writes().length, mode === '409' ? 2 : 1); + assert.equal(writes()[0].committed, true); + assertPending(f); + }); +} + +test('native complete upgrade rejects malformed final metadata readback without losing local data', async (t) => { + const f = await setup(t); + server.armFault({ mode: 'malformed-metadata', after: 1, metadata: { version: 'wrong-version' } }); + assert.equal(await f.upgrade(), true); + assertLocalUpgrade(f); + assert.equal(writes().length, 4); + assert.ok(writes().every((entry) => entry.committed)); + assertPending(f); +}); + +test('native already-consumed original baseline never uploads a second launch upgrade', async (t) => { + const f = await setup(t); + assert.equal(await f.upgrade(), true); + assert.equal(writes().length, 4); + server.clearTrace(); + assert.equal(await f.upgrade(), true); + assert.equal(writes().length, 0); + assertLocalUpgrade(f); + assertPending(f); +}); From 54dc7621df55ac4226b0c16ea745186c5c848196 Mon Sep 17 00:00:00 2001 From: antuz123 <43998049+antuz123@users.noreply.github.com> Date: Mon, 12 Oct 2026 19:53:22 +0530 Subject: [PATCH 152/254] Allow owner-deferred physical checks in local validation gate (#7050) --- .../tests/keeper-local-validation.test.cjs | 160 ++++++++++++++++++ .github/workflows/keeper-local-validation.yml | 46 ++++- AGENTS.md | 24 ++- 3 files changed, 225 insertions(+), 5 deletions(-) create mode 100644 .github/tests/keeper-local-validation.test.cjs diff --git a/.github/tests/keeper-local-validation.test.cjs b/.github/tests/keeper-local-validation.test.cjs new file mode 100644 index 0000000000..3de7d79945 --- /dev/null +++ b/.github/tests/keeper-local-validation.test.cjs @@ -0,0 +1,160 @@ +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const path = require('node:path'); +const test = require('node:test'); + +const workflowPath = path.join(__dirname, '..', 'workflows', 'keeper-local-validation.yml'); +const lines = fs.readFileSync(workflowPath, 'utf8').split(/\r?\n/); +const scriptStart = lines.findIndex(line => line.trim() === 'script: |'); +assert.notEqual(scriptStart, -1, 'workflow must contain an inline github-script'); +const script = lines.slice(scriptStart + 1).map(line => + line.startsWith(' ') ? line.slice(12) : line +).join('\n'); +const runWorkflow = new (Object.getPrototypeOf(async function () {}).constructor)( + 'github', 'context', 'core', script +); + +const sha = 'b78b480eb4223390b2d17073387f9887d2e35f95'; +const prNumber = 7045; +const comment = (body, login) => ({ body, user: { login } }); +const handoff = (head = sha, checks = 'IOS, ANDROID, HARDWARE') => comment( + `## Codex local handoff\nState: NEEDS_LOCAL\nHead commit: ${head}\nRequired local checks: ${checks}`, + 'cloud-writer' +); +const decision = (overrides = {}, login = 'antuz123') => comment( + `## Codex local scope decision +State: ${overrides.state ?? 'OWNER_DEFERRED_PHYSICAL'} +PR: ${overrides.pr ?? '#7045'} +Head commit: ${overrides.head ?? sha} +Deferred local checks: ${overrides.checks ?? 'HARDWARE'} +Reason: ${overrides.reason ?? 'owner-directed post-release physical testing'} +Post-release follow-up: ${overrides.followup ?? 'owner tests physical phones and signers after release'}`, + login +); +const report = (overrides = {}) => comment( + `## Codex local validation +State: ${overrides.state || 'LOCAL_PASSED'} +Tested commit: ${overrides.head || sha} +iOS build/simulator: ${overrides.ios || 'PASS for tested development subset'} +Android build/emulator: ${overrides.android || 'PASS for tested development subset'} +Real devices/signers: ${overrides.hardware || 'NOT RUN / POST-RELEASE by owner decision'}`, + 'local-writer' +); + +async function evaluate(comments, options = {}) { + const filesRoute = () => {}; + const commentsRoute = () => {}; + const statuses = []; + const failures = []; + const github = { + paginate: async route => { + if (route === filesRoute) return options.files || [{ filename: 'src/App.tsx' }]; + if (route === commentsRoute) return comments; + throw new Error('Unexpected paginate route'); + }, + rest: { + pulls: { + get: async () => ({ data: { + base: { ref: 'sprint' }, + head: { sha: options.head || sha }, + body: '', user: { login: 'cloud-writer' }, + html_url: 'https://github.com/KeeperCommunity/bitcoin-keeper/pull/7045' + } }), + listFiles: filesRoute + }, + issues: { listComments: commentsRoute }, + repos: { + getCollaboratorPermissionLevel: async ({ username }) => ({ data: { + permission: options.permissions?.[username] ?? + (['cloud-writer', 'local-writer', 'antuz123', 'other-writer'] + .includes(username) ? 'write' : 'read') + } }), + createCommitStatus: async status => { statuses.push(status); } + } + } + }; + const context = { repo: { owner: 'KeeperCommunity', repo: 'bitcoin-keeper' }, + issue: { number: prNumber }, payload: {} }; + const core = { info: () => {}, warning: () => {}, setFailed: text => failures.push(text) }; + await runWorkflow(github, context, core); + assert.equal(statuses.length, 1); + return { status: statuses[0], failures }; +} + +test('current requirements still fail without an owner decision', async () => { + const { status } = await evaluate([handoff(), report()]); + assert.equal(status.state, 'failure'); + assert.equal(status.description, 'Missing local PASS: HARDWARE'); +}); + +test('exact-SHA owner deferral passes only with a later honest local report', async () => { + const { status, failures } = await evaluate([handoff(), decision(), report()]); + assert.equal(status.state, 'success'); + assert.match(status.description, /Validated at current SHA: IOS, ANDROID; owner-deferred HARDWARE/); + assert.equal(failures.length, 0); +}); + +test('a report before the scope decision must be refreshed', async () => { + const { status } = await evaluate([handoff(), report(), decision()]); + assert.equal(status.state, 'failure'); + assert.equal(status.description, 'Missing local PASS: HARDWARE'); +}); + +test('a write collaborator other than the release owner cannot defer hardware', async () => { + const { status } = await evaluate([handoff(), decision({}, 'other-writer'), report()]); + assert.equal(status.state, 'failure'); + assert.equal(status.description, 'Missing local PASS: HARDWARE'); +}); + +test('the release owner must still have repository write permission', async () => { + const { status } = await evaluate([handoff(), decision(), report()], + { permissions: { antuz123: 'read' } }); + assert.equal(status.state, 'failure'); + assert.equal(status.description, 'Missing local PASS: HARDWARE'); +}); + +test('a stale or wrongly scoped owner decision cannot defer the current PR head', async () => { + for (const change of [{ head: 'a'.repeat(40) }, { pr: '#7046' }, + { checks: 'IOS, ANDROID, HARDWARE' }, { reason: '' }, { followup: '' }]) { + const { status } = await evaluate([handoff(), decision(change), report()]); + assert.equal(status.state, 'failure'); + assert.equal(status.description, 'Missing local PASS: HARDWARE'); + } +}); + +test('a later handoff cannot erase an earlier HARDWARE requirement', async () => { + const { status } = await evaluate([handoff(), handoff(sha, 'IOS, ANDROID'), report()]); + assert.equal(status.state, 'failure'); + assert.equal(status.description, 'Missing local PASS: HARDWARE'); +}); + +test('a later owner decision can revoke physical deferral', async () => { + const { status } = await evaluate([handoff(), decision(), + decision({ state: 'OWNER_REQUIRES_PRE_RELEASE' }), report()]); + assert.equal(status.state, 'failure'); + assert.equal(status.description, 'Missing local PASS: HARDWARE'); +}); + +test('failed hardware and missing iOS/Android PASS cannot be deferred', async () => { + const failedHardware = await evaluate([handoff(), decision(), + report({ hardware: 'FAIL: signer address mismatch' })]); + assert.equal(failedHardware.status.state, 'failure'); + assert.equal(failedHardware.status.description, 'Missing local PASS: HARDWARE'); + const missingIos = await evaluate([handoff(), decision(), + report({ ios: 'NOT RUN' })]); + assert.equal(missingIos.status.state, 'failure'); + assert.equal(missingIos.status.description, 'Missing local PASS: IOS'); +}); + +test('hardware-only handoff cannot turn into a no-test success', async () => { + const { status } = await evaluate([handoff(sha, 'HARDWARE'), decision(), report()]); + assert.equal(status.state, 'failure'); + assert.equal(status.description, 'IOS or ANDROID required before HARDWARE deferral'); +}); + +test('a new PR head invalidates both handoff and owner decision', async () => { + const { status } = await evaluate([handoff(), decision(), report()], + { head: 'b'.repeat(40) }); + assert.equal(status.state, 'failure'); + assert.equal(status.description, 'Trusted Cloud handoff for current SHA required'); +}); diff --git a/.github/workflows/keeper-local-validation.yml b/.github/workflows/keeper-local-validation.yml index ef425522b1..d66fc70f88 100644 --- a/.github/workflows/keeper-local-validation.yml +++ b/.github/workflows/keeper-local-validation.yml @@ -129,12 +129,39 @@ jobs: if (malformed) { description = 'Specify Required local checks: IOS, ANDROID, HARDWARE'; } else { + // Only the release owner can defer unrun physical checks. Keep + // every original handoff intact, and bind the newest owner + // decision to this PR and this exact head commit. + const releaseOwnerLogin = 'antuz123'; + let ownerDecision = ''; + let ownerDecisionIndex = -1; + for (let index = comments.length - 1; index >= 0; index--) { + const comment = comments[index]; + if (comment.user?.login !== releaseOwnerLogin) continue; + const content = section(comment.body, 'Codex local scope decision'); + if (content && field(content, 'PR') === '#' + pull_number && + field(content, 'Head commit') === sha && + await mayAttest(comment.user.login)) { + ownerDecision = content; + ownerDecisionIndex = index; + break; + } + } + const hardwareDeferral = Boolean(ownerDecision && + field(ownerDecision, 'State') === 'OWNER_DEFERRED_PHYSICAL' && + field(ownerDecision, 'Deferred local checks').trim().toUpperCase() === 'HARDWARE' && + field(ownerDecision, 'Reason') && + field(ownerDecision, 'Post-release follow-up')); + let report = ''; - for (const comment of [...comments].reverse()) { + let reportIndex = -1; + for (let index = comments.length - 1; index >= 0; index--) { + const comment = comments[index]; const content = section(comment.body, 'Codex local validation'); if (content && field(content, 'Tested commit') === sha && await mayAttest(comment.user?.login)) { report = content; + reportIndex = index; break; } } @@ -148,14 +175,25 @@ jobs: ANDROID: 'Android build/emulator', HARDWARE: 'Real devices/signers' }; + const hardwareResult = field(report, reportFields.HARDWARE); + const hardwareDeferred = required.has('HARDWARE') && hardwareDeferral && + reportIndex > ownerDecisionIndex && + /^(not run|deferred)\b/i.test(hardwareResult); const notPassed = [...required].filter(x => - !/^pass\b/i.test(field(report, reportFields[x])) + !/^pass\b/i.test(field(report, reportFields[x])) && + !(x === 'HARDWARE' && hardwareDeferred) ); - if (notPassed.length) { + if (hardwareDeferred && !required.has('IOS') && !required.has('ANDROID')) { + description = 'IOS or ANDROID required before HARDWARE deferral'; + } else if (notPassed.length) { description = 'Missing local PASS: ' + notPassed.join(', '); } else { state = 'success'; - description = 'Validated at current SHA: ' + [...required].join(', '); + const passed = [...required].filter(x => + /^pass\b/i.test(field(report, reportFields[x])) + ); + description = 'Validated at current SHA: ' + passed.join(', ') + + (hardwareDeferred ? '; owner-deferred HARDWARE' : ''); } } } diff --git a/AGENTS.md b/AGENTS.md index 7e3d9609c2..db639d5817 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -53,6 +53,28 @@ When given **"Validate Keeper PR #"**, first read the newest **Codex loc 6. Report the result and PR link to ChatGPT/the user. +### Owner-directed post-release test scope + +Keep `HARDWARE` in the Cloud handoff when signer or real-device interaction matters. If physical phones, cameras, NFC/USB, or hardware signers are unavailable before release, the release owner may defer only those physical checks to a post-release follow-up. Record the decision in a **new** PR comment so the original handoff and validation evidence remain intact: + +```text +## Codex local scope decision +State: OWNER_DEFERRED_PHYSICAL +PR: # +Head commit: +Deferred local checks: HARDWARE +Reason: +Post-release follow-up: +``` + +For this repository, the gate accepts that decision only from the release-owner GitHub account `antuz123`, with current write access, for the exact PR and head SHA. A newer owner scope decision for the same PR and SHA supersedes an older one. A new commit needs a new scope decision and local report. The decision cannot defer `IOS` or `ANDROID`, and it cannot turn a failed physical test into a pass. + +Run all feasible automated, simulator/emulator and disposable checks before public submission. The owner may place inaccessible, noncritical scenarios in post-release testing on the actual installed release, consistent with the release checklist. Record each one explicitly as `NOT RUN / POST-RELEASE` in `Open issues / next action`, with its reason and follow-up. This does not waive a failed check or remove `IOS` or `ANDROID` from the gate; only the exact-SHA decision above can subtract `HARDWARE` from automatic gate requirements. + +After every required iOS or Android check actually passes, Codex Local may report `LOCAL_PASSED` for the **tested subset** in a new comment after the scope decision and put `NOT RUN` or `DEFERRED` at the start of `Real devices/signers`. The resulting gate status says which checks passed and that hardware was owner-deferred. `LOCAL_PASSED` must never imply that deferred scenarios ran. Track the owner's post-release results on the PR or release record. + +A known critical defect is not eligible for post-release deferral. In particular, suspected loss or misbinding of keys, recovery material, funds or backups, incorrect recipient/amount/policy signing, and a failed required iOS or Android check must be resolved and retested before release. Keep other release evidence and decisions separate, including independent review, fresh store histories and counters, production configuration, signed artifact identity and version correctness, and explicit approval to sign or publish. + ## Release safety and coordination - `LOCAL_PASSED` means only the reported checks passed on the reported commit. It is **not** approval to merge, sign, distribute, upload to TestFlight/Play Store, or publish a release. @@ -62,6 +84,6 @@ When given **"Validate Keeper PR #"**, first read the newest **Codex loc ## GitHub merge gate -The `.github/workflows/keeper-local-validation.yml` workflow posts commit status `keeper/local-validation` for the current PR head SHA. For code changes, it remains failing until a repository collaborator posts a `Codex local validation` comment with `State: LOCAL_PASSED`, the **exact current commit SHA**, and `pass` for every platform named by `Required local checks`. New commits invalidate previous local sign-off. Documentation and `.github/`-only PRs are exempt from Mac tests. +The `.github/workflows/keeper-local-validation.yml` workflow posts commit status `keeper/local-validation` for the current PR head SHA. For code changes, it remains failing until a repository collaborator posts a `Codex local validation` comment with `State: LOCAL_PASSED`, the **exact current commit SHA**, and `pass` for every platform named by `Required local checks`, except `HARDWARE` when the exact-SHA owner decision above defers unrun physical checks. New commits invalidate previous local sign-off and deferral. Documentation and `.github/`-only PRs are exempt from Mac tests. This workflow **reports** a status; to **enforce** it, repository administrators must require the `keeper/local-validation` commit status for merges to `sprint` in GitHub Rulesets or branch protection. Require the existing `Tests` CI check as well. This is a review/test gate, not a guarantee that tests were actually executed; agents must report actual results truthfully. Release signing, TestFlight, Play Store, and production deployment remain separate and require explicit user approval. From 614dc54cd0883a1f1257fa36537fde22fe2ba128 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Tue, 13 Oct 2026 03:31:47 +0000 Subject: [PATCH 153/254] Run genuine automatic backup matrix with pinned Relay in CI --- .github/workflows/test.yml | 66 ++++++++++++++++++++++++ package.json | 3 +- scripts/run-automatic-backup-genuine.cjs | 53 +++++++++++++++++++ 3 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 scripts/run-automatic-backup-genuine.cjs diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index abe1ba61e9..ce50d5f497 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -81,3 +81,69 @@ jobs: - name: Test Android reproducibility tools run: python3 -B -m unittest scripts/test_compare_android_apks.py reproducibility/test_verify_android_inputs.py + + automatic-backup-genuine: + name: Automatic backup native Realm and Relay + runs-on: ubuntu-latest + timeout-minutes: 25 + env: + KEEPER_RELAY_FIXTURE_MODULE: ${{ github.workspace }}/.ci-relay/tests/support/automaticBackupHttpFixture.cjs + BACKUP_TEST_MONGO_URL: mongodb://127.0.0.1:27058 + REALM_DISABLE_ANALYTICS: '1' + + steps: + - name: Checkout Keeper source + uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4 + with: + persist-credentials: false + + - name: Checkout pinned paired Relay fixture + uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4 + with: + repository: KeeperCommunity/Relay + ref: 5a42a0368223d952e1dda475859743ce30d08058 + path: .ci-relay + persist-credentials: false + + - name: Set up Node 22 + uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4 + with: + node-version: '22' + + - name: Install Keeper dependencies without native app setup + run: | + yarn install --frozen-lockfile --ignore-scripts + yarn patch-package + + - name: Install the real Realm N-API binding + working-directory: node_modules/realm + run: ../.bin/prebuild-install --runtime napi --target 6 + + - name: Install paired Relay dependencies + working-directory: .ci-relay + run: npm ci --ignore-scripts + + - name: Start disposable loopback Mongo replica set + shell: bash + run: | + set -euo pipefail + docker run --detach --name keeper-backup-mongo \ + --publish 127.0.0.1:27058:27058 \ + mongo:7.0@sha256:9854f7139445d766a9523571d6f047530c45547460ffcf8259eb2bf4264632ca \ + mongod --replSet keeperQA --bind_ip_all --port 27058 + for attempt in {1..30}; do + if docker exec keeper-backup-mongo mongosh --port 27058 --quiet --eval \ + 'try { rs.status(); } catch (e) { if (e.code !== 94) throw e; rs.initiate({_id: "keeperQA", members: [{_id: 0, host: "127.0.0.1:27058"}]}); } quit(db.hello().isWritablePrimary ? 0 : 1)'; then + exit 0 + fi + sleep 2 + done + docker logs keeper-backup-mongo + exit 1 + + - name: Run genuine automatic backup source matrix + run: npm run test:automatic-backup-genuine + + - name: Remove disposable Mongo container + if: always() + run: docker rm --force keeper-backup-mongo || true diff --git a/package.json b/package.json index 434d8b3777..4f11669fa8 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,8 @@ "release ios dev": "cd ios && ENVFILE=.env bundle exec fastlane dev --env dev", "release ios prod": "cd ios && ENVFILE=.env.production bundle exec fastlane live --env production", "maestro:test:regression": "maestro test flows/regression.yaml", - "test:recovery": "node --test tests/recovery/*.test.cjs" + "test:recovery": "node --test tests/recovery/*.test.cjs", + "test:automatic-backup-genuine": "node scripts/run-automatic-backup-genuine.cjs" }, "dependencies": { "@bitcoinerlab/miniscript": "1.4.0", diff --git a/scripts/run-automatic-backup-genuine.cjs b/scripts/run-automatic-backup-genuine.cjs new file mode 100644 index 0000000000..215e3c88c0 --- /dev/null +++ b/scripts/run-automatic-backup-genuine.cjs @@ -0,0 +1,53 @@ +// This matrix requires the pinned paired Relay, a real native Realm binding +// and disposable loopback Mongo. Missing prerequisites are a failing check. +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const os = require('node:os'); +const path = require('node:path'); +const { spawnSync } = require('node:child_process'); + +const relaySha = '5a42a0368223d952e1dda475859743ce30d08058'; +const root = path.resolve(__dirname, '..'); +const fixture = process.env.KEEPER_RELAY_FIXTURE_MODULE; +assert.equal(process.versions.node.split('.')[0], '22', 'Run the genuine automatic backup matrix with Node 22'); +assert.ok(fixture, 'Set KEEPER_RELAY_FIXTURE_MODULE to the pinned Relay tests/support/automaticBackupHttpFixture.cjs'); +const filename = fs.realpathSync(fixture); +const checkout = path.dirname(path.dirname(path.dirname(filename))); +assert.equal(path.relative(checkout, filename), 'tests/support/automaticBackupHttpFixture.cjs'); +const revision = spawnSync('git', ['-C', checkout, 'rev-parse', 'HEAD'], { encoding: 'utf8' }); +assert.equal(revision.status, 0, 'The Relay fixture must be in a Git checkout'); +assert.equal(revision.stdout.trim(), relaySha, `Use paired Relay commit ${relaySha}`); +const clean = spawnSync('git', ['-C', checkout, 'diff', '--quiet', 'HEAD'], { encoding: 'utf8' }); +assert.equal(clean.status, 0, 'The pinned Relay checkout must have no tracked changes'); + +const mongoUrl = new URL(process.env.BACKUP_TEST_MONGO_URL || 'mongodb://127.0.0.1:27058'); +assert.equal(mongoUrl.protocol, 'mongodb:'); +assert.ok(['127.0.0.1', 'localhost'].includes(mongoUrl.hostname), 'The matrix only accepts loopback Mongo'); +assert.equal(mongoUrl.port, '27058', 'Use the disposable keeperQA replica set on port 27058'); +assert.ok(!mongoUrl.username && !mongoUrl.password, 'The disposable matrix does not accept credentials'); + +const Realm = require('realm'); +const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'keeper-native-realm-prerequisite-')); +let probe; +try { + probe = new Realm({ + path: path.join(directory, 'probe.realm'), inMemory: true, + schema: [{ name: 'NativeProbe', primaryKey: 'id', properties: { id: 'string', value: 'int' } }], + }); + probe.write(() => probe.create('NativeProbe', { id: 'native', value: 1 })); + assert.equal(probe.objectForPrimaryKey('NativeProbe', 'native').value, 1); +} finally { + probe?.close(); + Realm.shutdown(); + fs.rmSync(directory, { recursive: true, force: true }); +} + +const result = spawnSync(process.execPath, [ + '--test', + 'tests/recovery/automaticWriteRealmRelay.integration.cjs', + 'tests/recovery/automaticLabelProducers.integration.cjs', +], { + cwd: root, stdio: 'inherit', + env: { ...process.env, KEEPER_RELAY_FIXTURE_MODULE: filename, BACKUP_TEST_MONGO_URL: mongoUrl.toString() }, +}); +process.exit(result.status ?? 1); From 134562b700b1ad4c13275ea354d9fa564356fed5 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Tue, 13 Oct 2026 03:35:30 +0000 Subject: [PATCH 154/254] Type persisted label records when building send inheritance --- src/store/sagas/utxos.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/store/sagas/utxos.ts b/src/store/sagas/utxos.ts index 7d01718d66..73b95d1f89 100644 --- a/src/store/sagas/utxos.ts +++ b/src/store/sagas/utxos.ts @@ -358,7 +358,7 @@ export function* transactionLabelsWorker({ ); if (changeOutputIndex !== -1) { const inputRefs = new Set(inputs.map((input) => `${input.txId}:${input.vout}`)); - const inherited: BIP329Label[] = dbManager.getCollection(RealmSchema.Tags) + const inherited = (dbManager.getCollection(RealmSchema.Tags) as unknown as BIP329Label[]) .filter((tag) => inputRefs.has(tag.ref)); const ref = `${txId}:${changeOutputIndex}`; const changeLabels = [{ label: 'Change', isSystem: false }, ...inherited]; From 33973b9f02a5e6e033f1b9320163518f99738562 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Tue, 13 Oct 2026 03:36:00 +0000 Subject: [PATCH 155/254] test: retain version recovery provenance in local-first upgrades --- tests/store/versionHistory.test.ts | 643 +++++++++++++++++------------ 1 file changed, 385 insertions(+), 258 deletions(-) diff --git a/tests/store/versionHistory.test.ts b/tests/store/versionHistory.test.ts index 95b438a239..cc9ffef90f 100644 --- a/tests/store/versionHistory.test.ts +++ b/tests/store/versionHistory.test.ts @@ -1,244 +1,197 @@ import fs from 'fs'; import path from 'path'; -import { call, put, select } from 'redux-saga/effects'; import ts from 'typescript'; import vm from 'vm'; import semver from 'semver'; -import { applyUpgradeSequence, updateVersionHistoryWorker } from 'src/store/sagas/upgrade'; -import { updateVersionHistory } from 'src/store/sagaActions/upgrade'; +import { runSaga } from 'redux-saga'; +import * as effects from 'redux-saga/effects'; +import * as enums from 'src/services/wallets/enums'; import { RealmSchema } from 'src/storage/realm/enum'; -import dbManager from 'src/storage/realm/dbManager'; -import Relay from 'src/services/backend/Relay'; -import { setAppVersion } from 'src/store/reducers/storage'; -import { store } from 'src/store/store'; - -jest.mock('src/storage/realm/dbManager', () => ({ - getObjectByIndex: jest.fn(), - createObject: jest.fn(), -})); -jest.mock('src/services/backend/Relay', () => ({ updateAppImage: jest.fn() })); -jest.mock('src/services/backup/transport', () => ({ currentBackupAccountSession: () => 1 })); -jest.mock('src/store/store', () => ({ store: { getState: jest.fn() } })); -jest.mock('src/store/reducers/bhr', () => ({ - isAutomaticCloudBackupEnabled: (backup, appId) => backup.automaticCloudBackupByAppId?.[appId] === true, - setPendingAllBackup: (payload) => ({ type: 'setPendingAllBackup', payload }), -})); -jest.mock('src/services/backend/SigningServer', () => ({})); -jest.mock('src/utils/service-utilities/utils', () => ({})); -jest.mock('src/services/wallets/factories/WalletFactory', () => ({})); -jest.mock('src/utils/service-utilities/encryption', () => ({})); -jest.mock('src/utils/utilities', () => ({})); -jest.mock('src/services/wallets/operations/utils', () => ({})); -jest.mock('src/store/sagas/bhr', () => ({})); -jest.mock('@react-native-firebase/app', () => ({ getApp: jest.fn() })); -jest.mock('@react-native-firebase/messaging', () => ({ - getMessaging: jest.fn(), - subscribeToTopic: jest.fn(), - unsubscribeFromTopic: jest.fn(), -})); -jest.mock('react-native-device-info', () => ({ getBuildNumber: () => '624' })); +import * as bip39 from 'bip39'; +import BIP32Factory from 'bip32'; +import * as bitcoinJS from 'bitcoinjs-lib'; +import ecc from 'src/services/wallets/operations/taproot-utils/noble_ecc'; +const { encryption, image, loadModule } = require('../recovery/helpers.cjs'); +const { prepareRecoveryImage } = loadModule('src/services/backup/restore.ts', { './image': image }); +const BIP85 = loadModule('src/services/wallets/operations/BIP85.ts', { + 'src/utils/service-utilities/config': {}, + '../enums': enums, + './taproot-utils/noble_ecc': ecc, +}).default; +const bip32 = BIP32Factory(ecc); const previousVersion = '2.5.15'; const newVersion = '2.5.16'; -const writeIntent = { - appId: 'test-app', accountSession: 1, baseRevision: 'a'.repeat(64), baselineEpoch: 1, -}; - -function setActiveAccount(appId: string, enabled = true) { - (dbManager.getObjectByIndex as jest.Mock).mockReturnValue({ id: appId }); - (store.getState as jest.Mock).mockReturnValue({ - storage: { appId }, - bhr: { automaticCloudBackupByAppId: { [appId]: enabled } }, - }); -} - -function writeHistory(action) { - const appId = action.originAppId; - const worker = updateVersionHistoryWorker(action) as Generator; - expect(worker.next().value).toEqual(call(dbManager.getObjectByIndex, RealmSchema.KeeperApp)); - const choice = worker.next({ id: appId } as any).value as any; - expect(choice.type).toBe('SELECT'); - expect(choice.payload.selector(store.getState())).toBe(true); - expect(worker.next(true).value).toEqual( - call(Relay.updateAppImage, { appId, version: newVersion }, action.originBackupWriteIntent) - ); - const write = worker.next({} as any).value as any; - expect(write.payload.fn).toBe(dbManager.createObject); - expect(write.payload.args[0]).toBe(RealmSchema.VersionHistory); - expect(worker.next().done).toBe(true); - return write.payload.args[1]; -} - -function recoverWithoutDuplicateHistory(action) { - const appId = action.originAppId; - const worker = updateVersionHistoryWorker(action) as Generator; - expect(worker.next().value).toEqual(call(dbManager.getObjectByIndex, RealmSchema.KeeperApp)); - expect((worker.next({ id: appId } as any).value as any).type).toBe('SELECT'); - expect(worker.next(true).value).toEqual( - call(Relay.updateAppImage, { appId, version: newVersion }, action.originBackupWriteIntent) - ); - expect(worker.next({} as any).done).toBe(true); -} - -describe('Version History retains recovery provenance through required upgrades', () => { - beforeEach(() => setActiveAccount('test-app')); - - test.each([true, false])('upgrade handles recovery=%s without duplicate history', (isRecovery) => { - const upgrade = applyUpgradeSequence({ previousVersion, newVersion, isRecovery, - originBackupWriteIntent: writeIntent }); - expect(upgrade.next().value).toEqual(call(dbManager.getObjectByIndex, RealmSchema.KeeperApp)); - expect(upgrade.next({ id: 'test-app' } as any).value).toEqual(put(setAppVersion(newVersion))); - const history = upgrade.next().value as any; - expect(history).toEqual(put({ - ...updateVersionHistory(previousVersion, newVersion, isRecovery, 'test-app'), - originBackupAccountSession: 1, - originBackupWriteIntent: writeIntent, - })); - if (isRecovery) { - recoverWithoutDuplicateHistory(history.payload.action); - } else { - const record = writeHistory(history.payload.action); - expect(record.version).toBe('2.5.16(624)'); - expect(record.title).toBe('Upgraded from 2.5.15 to 2.5.16'); - } - expect(upgrade.next().done).toBe(true); - }); - - test('unbound history events cannot write another account', () => { - const worker = updateVersionHistoryWorker({ payload: { previousVersion, newVersion } }); - expect(worker.next().value).toEqual(call(dbManager.getObjectByIndex, RealmSchema.KeeperApp)); - expect(worker.next({ id: 'test-app' } as any).done).toBe(true); - }); - - test('recovery does not skip any of the legacy migrations', () => { - const trace = (isRecovery: boolean) => { - const yielded: any[] = []; - const upgrade = applyUpgradeSequence({ - previousVersion: '1.0.4', - newVersion, - isRecovery, - }) as Generator; - let step = upgrade.next(); - while (!step.done) { - yielded.push(step.value); - step = upgrade.next(step.value?.type === 'CALL' && - step.value.payload.fn === dbManager.getObjectByIndex ? { id: 'test-app' } : undefined); - } - return yielded; - }; - const ordinary = trace(false); - const recovered = trace(true); - expect(recovered.slice(0, -1)).toEqual(ordinary.slice(0, -1)); - expect(recovered.length).toBeGreaterThan(3); - }); +const writeIntent = Object.freeze({ + appId: 'test-app', + accountSession: 1, + baseRevision: 'a'.repeat(64), + baselineEpoch: 1, }); +const clone = (value) => JSON.parse(JSON.stringify(value)); -// Execute the real recovery orchestration with disposable in-memory dependencies. -// This verifies call-site provenance; it does not claim native Realm or wallet recovery QA. -function recoveryFixture(backupVersion: string) { +function sourceDeclarations(file: string, names?: string[]) { const source = ts.createSourceFile( - 'bhr.ts', - fs.readFileSync(path.join(process.cwd(), 'src/store/sagas/bhr.ts'), 'utf8'), + file, + fs.readFileSync(path.join(process.cwd(), file), 'utf8'), ts.ScriptTarget.Latest, true ); - const functions = source.statements.filter( - (node) => - ts.isFunctionDeclaration(node) && - ['getAppImageWorker', 'recoverApp', 'writeRecoveredObject'].includes(node.name?.text) - ); - const writes = []; - const recoveryUpgrades = []; - const errors = []; + return source.statements + .filter( + (node) => + (ts.isFunctionDeclaration(node) && (!names || names.includes(node.name?.text))) || + (ts.isVariableStatement(node) && + node.declarationList.declarations.some((declaration) => + names + ? names.includes(declaration.name.getText(source)) + : declaration.name.getText(source) === 'upgradeAccountIsActive' || + declaration.name.getText(source).endsWith('_VERSION') + )) + ) + .map((node) => + node + .getText(source) + .replace(/^export /, '') + .replace(/^const /, 'var ') + ) + .join('\n'); +} + +// Tests orchestration with an explicit unit transaction adapter. Native Realm, +// branded CAS and readback are tested by automaticUpgradeProducer.integration. +// Encryption, BIP39/BIP32 and BIP85 use real production/pure crypto primitives. +function fixture(appId = 'test-app') { + const state = { + appId, + session: 1, + events: [], + queue: [], + actions: [], + errors: [], + rollbackHistory: false, + migrations: [], + topics: [], + }; + const app: any = { id: appId, version: previousVersion, primarySeed: 'ab'.repeat(32) }; + const records: Record = { + Wallet: [{ id: 'legacy-wallet' }], + USDTWallet: [], + Signer: [], + Vault: [], + VaultSigner: [], + Tags: [], + VersionHistory: [], + BackupHistory: [], + NodeConnect: [], + UTXOInfo: [ + { walletId: 'legacy-wallet', txId: 'legacy-tx', vout: 0, labels: [{ name: 'old note' }] }, + ], + }; + const create = (schema, value) => { + if (state.rollbackHistory && schema === RealmSchema.VersionHistory) + throw new Error('history rollback fixture'); + if (schema === RealmSchema.KeeperApp) Object.assign(app, clone(value)); + else { + const rows = (records[schema] ||= []); + const primary = schema === RealmSchema.VersionHistory ? 'version' : 'id'; + const index = rows.findIndex((row) => row[primary] === value[primary]); + if (index < 0) rows.push(clone(value)); + else rows[index] = clone(value); + } + return true; + }; + const raw = { + objectForPrimaryKey: (schema, id) => + schema === RealmSchema.KeeperApp + ? app.id === id + ? app + : undefined + : records[schema].find((row) => row.id === id), + objects: (schema) => records[schema], + create, + }; const scope: any = { - call, - put, - select, - semver, - console, - currentBackupAccountSession: () => 1, - currentBackupAppId: () => require('crypto').createHash('sha256') - .update(Buffer.from('fixture')).digest('hex'), + ...effects, + ...encryption, + ...enums, RealmSchema, - applyUpgradeSequence, - DeviceInfo: { getVersion: () => newVersion, getBuildNumber: () => '624' }, - bip39: { validateMnemonic: () => true, mnemonicToSeedSync: () => Buffer.from('fixture') }, - crypto: require('crypto'), - BIP85: { bip39MnemonicToEntropy: () => Buffer.from('fixture') }, - config: { BIP85_IMAGE_ENCRYPTIONKEY_DERIVATION_PATH: 'fixture' }, - generateEncryptionKey: () => 'fixture', - decrypt: (_key, content) => content, - prepareRecoveryImage: (_key, _appId, image) => ({ - wallets: Object.fromEntries( - Object.entries(image.wallets).map(([id, content]) => [id, JSON.parse(content as string)]) - ), - signers: {}, - vaults: {}, - nodes: {}, - labels: {}, - }), - WalletUtilities: { getFingerprintFromSeed: () => 'AAAAAAAA' }, - SubscriptionTier: { L1: 'L1', L3: 'L3' }, - AppSubscriptionLevel: { L1: 1, L3: 3 }, - BackupType: { SEED: 'SEED' }, - EntityKind: { USDT_WALLET: 'USDT_WALLET' }, - BackupAction: { SEED_BACKUP_CONFIRMED: 'confirmed' }, - pauseBackup: () => {}, - NetworkType: { MAINNET: 'MAINNET', TESTNET: 'TESTNET' }, - uaiType: {}, - moment: () => ({ unix: () => 1 }), - setupRecoveryKeySigningKey: () => ({ - id: 'recovery', - signerXpubs: { - fixture: [ - { - xpub: 'fixture-public', - xpriv: 'fixture-private', - derivationPath: 'fixture-path', - }, - ], - }, - }), - getKeyUID: (signer) => signer.id, - addSigningDeviceWorker: () => {}, + semver, + Date, + Buffer, + Promise, + console: { log() {}, error() {} }, + Realm: { UpdateMode: { Modified: 'modified' } }, + store: { getState: () => ({ storage: { appId: state.appId } }) }, dbManager: { - createObject: (schema, record) => { - if (schema === RealmSchema.VersionHistory) writes.push(record); + getObjectByIndex: () => app, + getObjectById: raw.objectForPrimaryKey, + getObjectByPrimaryId: (schema, key, id) => records[schema].find((row) => row[key] === id), + getCollection: (schema) => clone(records[schema] || []), + createObject: create, + createObjectBulk: (schema, values) => { + values.forEach((value) => create(schema, value)); + return true; + }, + deleteObjectById: (schema, id) => { + records[schema] = records[schema].filter((row) => row.id !== id); return true; }, - createObjectBulk: () => {}, - getCollection: (schema) => - schema === RealmSchema.Signer - ? [ - { - id: 'recovery', - signerXpubs: { - fixture: [ - { - xpub: 'fixture-public', - xpriv: 'fixture-private', - derivationPath: 'fixture-path', - }, - ], - }, - }, - ] - : [], }, - Relay: { - getAppImage: () => ({ - appImage: { - appId: 'disposable', - version: backupVersion, - wallets: { test: JSON.stringify({ id: 'test-wallet' }) }, - signers: {}, - nodes: [], - }, - }), - updateAppImage: () => ({}), + currentBackupAccountSession: () => state.session, + currentBackupAppId: () => (app.id === state.appId ? app.id : undefined), + beginBackupLocalOperation: (id, intent, session) => ({ id, intent, session }), + commitBackupLocalOperation: (operation, write) => { + if (operation.id !== state.appId || operation.session !== state.session) + return { committed: false }; + const beforeApp = clone(app), + beforeRecords = clone(records); + try { + write(raw); + } catch { + Object.assign(app, beforeApp); + Object.keys(records).forEach((name) => { + records[name] = beforeRecords[name]; + }); + state.events.push('rollback'); + return { committed: false }; + } + state.events.push('local commit'); + return Object.freeze({ committed: true, intent: operation.intent }); + }, + enqueueAutomaticBackupOperation: async (receipt, mutations) => { + state.events.push('queue'); + state.queue.push({ receipt, mutations: clone(mutations), persistedVersion: app.version }); + return { updated: true, backupConfirmed: false, backupPending: true }; }, + notifyBackupReconciliation: () => state.events.push('pending'), + buildAutomaticAppMutation: (_payload, id) => ({ + operation: 'updateAppImage', + body: { appId: id, version: app.version }, + }), + buildAutomaticVaultMutation: (id, owner) => ({ + operation: 'updateVaultImage', + body: { appId: owner, vaultId: id }, + }), + DeviceInfo: { getVersion: () => newVersion, getBuildNumber: () => '624' }, + getApp: () => ({}), + getMessaging: () => ({}), + getReleaseTopic: (version) => version, + subscribeToTopic: (_messaging, topic) => state.topics.push(['subscribe', topic]), + unsubscribeFromTopic: (_messaging, topic) => state.topics.push(['unsubscribe', topic]), + generateAbbreviatedOutputDescriptors: () => 'public-unit-label-origin', + getKeyUID: (signer) => signer.id, + hcStatusType: { HEALTH_CHECK_SUCCESSFULL: 'HEALTH_CHECK_SUCCESSFULL' }, + captureError: (error) => state.errors.push(error.message), + }; + const dispatch = (action) => { + state.actions.push(action); + if (action.type === 'setAppId') state.appId = action.payload; + if (action.type === 'setAppImageError' && action.payload) state.errors.push(action.payload); }; + scope.store.dispatch = dispatch; for (const name of [ + 'setAppVersion', 'setAppImageError', 'setSeedConfirmed', 'setBackupType', @@ -251,62 +204,236 @@ function recoveryFixture(backupVersion: string) { 'uaiChecks', 'loadConciergeUser', 'loadConciergeTickets', - 'setRecoveryKeyBackedUp', 'setRecoveryKeyStatus', 'appImagerecoveryRetry', - ]) { + ]) scope[name] = (payload) => ({ type: name, payload }); - } vm.runInNewContext( - ts.transpileModule(functions.map((node) => node.getText(source)).join('\n'), { - compilerOptions: { target: ts.ScriptTarget.ES2019 }, + ts.transpileModule(sourceDeclarations('src/store/sagas/upgrade.ts'), { + compilerOptions: { target: ts.ScriptTarget.ES2020 }, }).outputText, scope ); - function drive(generator) { - let result = generator.next(); - while (!result.done) { - const effect = result.value; - let response; - if (effect.type === 'SELECT') response = { bitcoinNetworkType: 'TESTNET' }; - if (effect.type === 'CALL') { - const { fn, args } = effect.payload; - if (fn === applyUpgradeSequence) recoveryUpgrades.push(args[0]); - response = fn(...args); - if (response?.next) response = drive(response); - if (fn === scope.recoverApp && response === undefined) response = 1; - } - if (effect.type === 'PUT') { - const action = effect.payload.action; - if (action.type === 'setAppImageError' && action.payload) errors.push(action.payload); - if (action.type === 'UPDATE_VERSION_HISTORY') { - if (action.payload.isRecovery) recoverWithoutDuplicateHistory(action); - else writes.push(writeHistory(action)); - } - } - result = generator.next(response); + const run = (name, ...args) => + runSaga( + { + getState: () => ({ + storage: { appId: state.appId }, + settings: { bitcoinNetworkType: enums.NetworkType.TESTNET }, + bhr: {}, + }), + dispatch, + }, + scope[name], + ...args + ).toPromise(); + return { state, scope, app, records, run }; +} + +describe('Version History retains recovery provenance with local-first upgrades', () => { + test.each([true, false])( + 'upgrade persists version first and recovery=%s controls duplicate history', + async (isRecovery) => { + const f = fixture(); + expect( + await f.run('applyUpgradeSequence', { + previousVersion, + newVersion, + isRecovery, + originBackupAccountSession: 1, + originBackupWriteIntent: writeIntent, + }) + ).toBe(true); + expect(f.app.version).toBe(newVersion); + expect(f.state.events).toEqual(['local commit', 'queue']); + expect(f.state.queue[0].persistedVersion).toBe(newVersion); + expect(f.state.queue[0].receipt.intent).toBe(writeIntent); + expect(f.records.VersionHistory).toEqual( + isRecovery + ? [] + : [ + expect.objectContaining({ + version: '2.5.16(624)', + title: 'Upgraded from 2.5.15 to 2.5.16', + }), + ] + ); + expect(f.state.actions).toContainEqual({ type: 'setAppVersion', payload: newVersion }); } - return result.value; - } - drive(scope.getAppImageWorker({ payload: { primaryMnemonic: 'disposable mocked fixture' } })); - return { writes, recoveryUpgrades, errors }; + ); + + test('unbound history events cannot write another account', async () => { + const f = fixture(); + expect( + await f.run('updateVersionHistoryWorker', { payload: { previousVersion, newVersion } }) + ).toBe(false); + expect(f.state.events).toEqual([]); + expect(f.app.version).toBe(previousVersion); + }); + + test('failed local history transaction does not upload or advance the app version', async () => { + const f = fixture(); + f.state.rollbackHistory = true; + expect( + await f.run('applyUpgradeSequence', { + previousVersion, + newVersion, + originBackupWriteIntent: writeIntent, + }) + ).toBe(false); + expect(f.app.version).toBe(previousVersion); + expect(f.records.VersionHistory).toEqual([]); + expect(f.state.queue).toEqual([]); + expect(f.state.actions).not.toContainEqual({ type: 'setAppVersion', payload: newVersion }); + }); + + test.each([true, false])( + 'recovery=%s retains legacy label migration in the same local group', + async (isRecovery) => { + const f = fixture(); + f.records.Vault.push( + { id: 'legacy-active-vault', archived: false, signers: [] }, + { id: 'legacy-archived-vault', archived: true, signers: [] } + ); + f.records.Signer.push({ + id: 'public-legacy-key', + masterFingerprint: 'AABB0011', + type: enums.SignerType.COLDCARD, + lastHealthCheck: '2026-09-01T00:00:00.000Z', + signerXpubs: {}, + }); + expect( + await f.run('applyUpgradeSequence', { + previousVersion: '1.0.4', + newVersion, + isRecovery, + originBackupWriteIntent: writeIntent, + }) + ).toBe(true); + expect(f.records.Tags).toEqual([ + expect.objectContaining({ ref: 'legacy-tx:0', label: 'old note' }), + ]); + expect(f.state.events).toEqual(['local commit', 'queue']); + expect(f.state.queue[0].mutations.map((mutation) => mutation.operation)).toEqual([ + 'updateAppImage', + 'updateVaultImage', + 'updateVaultImage', + 'modifyLabels', + ]); + expect(f.state.queue[0].mutations[2].body.vaultId).toBe('legacy-archived-vault'); + expect(f.records.Signer[0].healthCheckDetails).toEqual([ + { type: 'HEALTH_CHECK_SUCCESSFULL', actionDate: '2026-09-01T00:00:00.000Z' }, + ]); + expect(f.records.VersionHistory).toHaveLength(isRecovery ? 0 : 1); + } + ); +}); + +async function recoveryFixture(backupVersion: string) { + const mnemonic = bip39.entropyToMnemonic('00000000000000000000000000000000'); + const seed = bip39.mnemonicToSeedSync(mnemonic); + const id = require('crypto').createHash('sha256').update(seed).digest('hex'); + const f = fixture(id); + const key = encryption.generateEncryptionKey(seed.toString('hex')); + const root = bip32.fromSeed(seed, bitcoinJS.networks.testnet); + const derived = root.derivePath("m/48'/1'/0'/2'"); + const recoverySigner = { + id: 'recovery', + signerXpubs: { + fixture: [ + { + xpub: derived.neutered().toBase58(), + xpriv: derived.toBase58(), + derivationPath: "m/48'/1'/0'/2'", + }, + ], + }, + }; + const requestedUpgrades = []; + Object.assign(f.scope, { + bip39, + crypto: require('crypto'), + BIP85, + prepareRecoveryImage, + config: { BIP85_IMAGE_ENCRYPTIONKEY_DERIVATION_PATH: "m/83696968'/128169'/64'/0'" }, + WalletUtilities: { + getFingerprintFromSeed: (value) => + bip32.fromSeed(value).fingerprint.toString('hex').toUpperCase(), + }, + SubscriptionTier: { L1: 'L1', L3: 'L3' }, + AppSubscriptionLevel: { L1: 1, L3: 3 }, + BackupType: { SEED: 'SEED' }, + BackupAction: { SEED_BACKUP_CONFIRMED: 'confirmed' }, + pauseBackup: image.pauseBackup, + uaiType: {}, + moment: () => ({ unix: () => 1 }), + KEY_MANAGEMENT_VERSION: '1.1.9', + setupRecoveryKeySigningKey: () => recoverySigner, + addSigningDeviceWorker: ({ payload }) => { + payload.signers.forEach((record) => + f.scope.dbManager.createObject(RealmSchema.Signer, record) + ); + return true; + }, + Relay: { + getAppImage: async () => ({ + appImage: { + appId: id, + version: backupVersion, + wallets: { + test: encryption.encrypt( + key, + JSON.stringify({ id: 'test-wallet', entityKind: enums.EntityKind.WALLET }) + ), + }, + signers: {}, + nodes: [], + }, + allVaultImages: [], + labels: [], + }), + }, + }); + const actualUpgrade = f.scope.applyUpgradeSequence; + f.scope.applyUpgradeSequence = function* (action) { + requestedUpgrades.push(clone(action)); + return yield* actualUpgrade(action); + }; + vm.runInNewContext( + ts.transpileModule( + sourceDeclarations('src/store/sagas/bhr.ts', [ + 'getAppImageWorker', + 'recoverApp', + 'writeRecoveredObject', + ]), + { + compilerOptions: { target: ts.ScriptTarget.ES2020 }, + } + ).outputText, + f.scope + ); + await f.run('getAppImageWorker', { payload: { primaryMnemonic: mnemonic } }); + return { writes: f.records.VersionHistory, requestedUpgrades, errors: f.state.errors }; } test.each(['2.5.15', '2.5.16'])( - 'recovery of backup %s finishes with one recovered entry', - (backupVersion) => { - const { writes, recoveryUpgrades, errors } = recoveryFixture(backupVersion); + 'recovery of encrypted backup %s finishes with one recovered history entry', + async (backupVersion) => { + const { writes, requestedUpgrades, errors } = await recoveryFixture(backupVersion); expect(errors).toEqual([]); expect(writes).toEqual([ - expect.objectContaining({ - version: '2.5.16(624)', - title: 'Recovered Wallet', - }), + expect.objectContaining({ version: '2.5.16(624)', title: 'Recovered Wallet' }), ]); - expect(recoveryUpgrades).toEqual( + expect(requestedUpgrades).toEqual( backupVersion === '2.5.15' - ? [{ previousVersion: backupVersion, newVersion, isRecovery: true, - originBackupAccountSession: 1 }] + ? [ + { + previousVersion: backupVersion, + newVersion, + isRecovery: true, + originBackupAccountSession: 1, + }, + ] : [] ); } From df816e2f14eb0f82464d2fd0ea1fab4fcfc8c5f9 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Tue, 13 Oct 2026 03:38:13 +0000 Subject: [PATCH 156/254] Align backup and node regressions with committed local operations --- tests/recovery/backupFreshness.test.cjs | 35 ++++++++++++++++++++----- tests/recovery/nodeReplacement.test.cjs | 24 ++++++++++++++--- 2 files changed, 49 insertions(+), 10 deletions(-) diff --git a/tests/recovery/backupFreshness.test.cjs b/tests/recovery/backupFreshness.test.cjs index 509882e495..73c9cb5a48 100644 --- a/tests/recovery/backupFreshness.test.cjs +++ b/tests/recovery/backupFreshness.test.cjs @@ -5,6 +5,16 @@ const ts = require('typescript'); const { fixture, enums, encryption, wallet, loadModule, functions } = require('./sagaHarness.cjs'); const { NetworkType, WalletType } = enums; +function commitFixtureRecovery(f, write) { + const session = f.scope.currentBackupAccountSession(); + const operation = f.scope.beginBackupLocalOperation(f.app.id, { + appId: f.app.id, accountSession: session, + }, session); + const receipt = f.scope.commitBackupLocalOperation(operation, write); + assert.equal(receipt.committed, true); + return receipt; +} + test('archived-only resume keeps checking an empty hidden vault without notifications', async () => { const f = fixture(); f.collections.Wallet.push(wallet('visible-wallet', NetworkType.MAINNET)); @@ -247,8 +257,8 @@ for (const network of [NetworkType.MAINNET, NetworkType.TESTNET]) { const f = fixture(); const initial = wallet('initial', NetworkType.MAINNET); const added = wallet('later', network, 1); - f.collections.Wallet.push(initial); - await f.run('updateAppImageWorker', { payload: { wallets: [initial] } }); + const committedOperation = commitFixtureRecovery(f, realm => realm.create('Wallet', initial)); + await f.run('updateAppImageWorker', { payload: { wallets: [initial] }, committedOperation }); assert.equal( await f.run('addNewWalletsWorker', { payload: [{ walletType: WalletType.DEFAULT, walletDetails: { fixture: added } }], @@ -361,7 +371,7 @@ test('recovery upgrade does not bypass per-account backup consent', async () => assert.equal(f.collections.VersionHistory.filter((record) => record.title === 'Recovered Wallet').length, 1); }); -test('relay rejection retains the existing wallet-creation failure contract', async () => { +test('relay rejection preserves committed wallet creation and leaves backup pending', async () => { const f = fixture({ rejectIncremental: true }); const result = await f.run('addNewWalletsWorker', { payload: [ @@ -371,10 +381,23 @@ test('relay rejection retains the existing wallet-creation failure contract', as }, ], }); - assert.equal(result, false); - assert.equal(f.collections.Wallet.length, 0); + assert.equal(result, true); + assert.equal(f.collections.Wallet.length, 1); + assert.equal(f.collections.Wallet[0].id, 'rejected'); + assert.equal(f.remote.wallets.rejected, undefined); assert.equal(f.state.bhr.pendingAllBackup, true); - assert.ok(f.actions.some((a) => a.type === 'relayWalletUpdateFail' && a.payload === 'rejected')); + assert.equal(f.actions.some((a) => a.type === 'relayWalletUpdateFail'), false); +}); + +test('a direct BHR update without a committed source receipt remains pending and sends nothing', async () => { + const f = fixture(); + const persisted = wallet('existing', NetworkType.MAINNET); + f.collections.Wallet.push(persisted); + const result = await f.run('updateAppImageWorker', { payload: { wallets: [persisted] } }); + assert.equal(result.backupPending, true); + assert.equal(result.backupConfirmed, false); + assert.equal(f.calls.length, 0); + assert.equal(f.state.bhr.pendingAllBackupByAppId[f.app.id], true); }); for (const enabled of [true, false]) { diff --git a/tests/recovery/nodeReplacement.test.cjs b/tests/recovery/nodeReplacement.test.cjs index 0d2a6fee98..f5fc5a1de0 100644 --- a/tests/recovery/nodeReplacement.test.cjs +++ b/tests/recovery/nodeReplacement.test.cjs @@ -2,9 +2,22 @@ const { test } = require('node:test'); const assert = require('node:assert/strict'); const { fixture, encryption, wallet, enums } = require('./sagaHarness.cjs'); +function commitFixtureRecovery(f, write) { + const session = f.scope.currentBackupAccountSession(); + const operation = f.scope.beginBackupLocalOperation(f.app.id, { + appId: f.app.id, accountSession: session, + }, session); + const receipt = f.scope.commitBackupLocalOperation(operation, write); + assert.equal(receipt.committed, true); + return receipt; +} + test('removing the final node emits an explicit empty-list replacement', async () => { const f = fixture(); - await f.run('updateAppImageWorker', { payload: { updateNodes: true } }); + f.collections.NodeConnect.push({ id: 'last', host: 'fixture.invalid', port: 50002, isConnected: true }); + const committedOperation = commitFixtureRecovery(f, realm => + realm.delete(realm.objectForPrimaryKey('NodeConnect', 'last'))); + await f.run('updateAppImageWorker', { payload: { updateNodes: true }, committedOperation }); assert.equal(f.calls.length, 1); assert.equal(f.calls[0][1].replaceNodes, true); assert.deepEqual(Array.from(f.calls[0][1].nodes), []); @@ -12,18 +25,21 @@ test('removing the final node emits an explicit empty-list replacement', async ( test('unrelated wallet update never requests node replacement', async () => { const f = fixture(); - await f.run('updateAppImageWorker', { payload: { wallets: [wallet('one', enums.NetworkType.MAINNET)] } }); + const created = wallet('one', enums.NetworkType.MAINNET); + const committedOperation = commitFixtureRecovery(f, realm => realm.create('Wallet', created)); + await f.run('updateAppImageWorker', { payload: { wallets: [created] }, committedOperation }); assert.equal(f.calls[0][1].replaceNodes, false); }); test('node-list replacement encrypts nodes without changing local connection state', async () => { const f = fixture(); const node = { id: 'disposable', host: 'fixture.invalid', port: 50002, isConnected: true }; - f.collections.NodeConnect.push(node); - await f.run('updateAppImageWorker', { payload: { updateNodes: true } }); + const committedOperation = commitFixtureRecovery(f, realm => realm.create('NodeConnect', node)); + await f.run('updateAppImageWorker', { payload: { updateNodes: true }, committedOperation }); const sent = f.calls[0][1]; assert.equal(sent.replaceNodes, true); const restored = JSON.parse(encryption.decrypt(encryption.generateEncryptionKey(f.app.primarySeed), sent.nodes[0])); assert.deepEqual(restored, { ...node, isConnected: false }); assert.equal(node.isConnected, true); + assert.equal(f.collections.NodeConnect[0].isConnected, true); }); From 45c7cd9d8db156f4cf73dc3293ebc3b9dd695e75 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Tue, 13 Oct 2026 03:39:46 +0000 Subject: [PATCH 157/254] test: exercise destructive backup account session guards --- .../accountSessionDestructive.test.cjs | 314 ++++++++++++------ 1 file changed, 213 insertions(+), 101 deletions(-) diff --git a/tests/recovery/accountSessionDestructive.test.cjs b/tests/recovery/accountSessionDestructive.test.cjs index 7528a4b895..b2f097ccb7 100644 --- a/tests/recovery/accountSessionDestructive.test.cjs +++ b/tests/recovery/accountSessionDestructive.test.cjs @@ -3,136 +3,157 @@ const { test } = require('node:test'); const { fixture, enums, wallet } = require('./sagaHarness.cjs'); const { loadModule } = require('./helpers.cjs'); +// Producer orchestration tests with the explicit unit transaction/queue adapter. +// Native Realm, immutable queue authority and CAS/readback are covered separately +// by automaticWriteRealmRelay.integration; a Relay acknowledgement is not proof. async function waitForPause(getResume) { for (let attempt = 0; attempt < 20 && !getResume(); attempt++) await new Promise(setImmediate); - assert.equal(typeof getResume(), 'function', 'the action must pause before its backup write'); + assert.equal(typeof getResume(), 'function', 'the action must reach its intended pause'); } -function accountRoundTrip(fixtureState, originalAppId, advanceSession) { - fixtureState.app.id = 'other-disposable-account'; - fixtureState.state.storage.appId = fixtureState.app.id; +function accountRoundTrip(f, originalAppId, advanceSession) { + f.app.id = 'other-disposable-account'; + f.state.storage.appId = f.app.id; advanceSession(); - fixtureState.app.id = originalAppId; - fixtureState.state.storage.appId = originalAppId; + f.app.id = originalAppId; + f.state.storage.appId = originalAppId; advanceSession(); } -test('wallet deletion paused at connectivity cannot delete newer A data after A to B to A', async () => { - const f = fixture(); - const originAppId = f.app.id; +function owner(f) { + const appId = f.app.id; let session = 1; f.scope.currentBackupAccountSession = () => session; - const walletId = 'disposable-wallet'; - f.collections.Wallet.push(wallet(walletId, enums.NetworkType.MAINNET)); - f.remote.wallets[walletId] = 'initial-A-ciphertext'; - let resumeConnectivity; - f.scope.NetInfo.fetch = () => new Promise((resolve) => { resumeConnectivity = resolve; }); - const relayDeletes = []; - f.scope.Relay.deleteAppImageEntity = async ({ appId, walletIds }) => { - relayDeletes.push({ appId, walletIds }); - walletIds.forEach((id) => delete f.remote.wallets[id]); - return { updated: true }; + return { + appId, + session: () => session, + roundTrip: () => + accountRoundTrip(f, appId, () => { + session++; + }), }; +} - const deleting = f.run('deleteAppImageEntityWorker', { - originAppId, - originBackupAccountSession: session, +function hasDeleteSuccess(f) { + return f.actions.some( + (action) => action.type === 'setDeleteBackupSuccess' && action.payload?.status === true + ); +} + +function assertConsentRetained(f, appId) { + assert.equal(f.state.bhr.automaticCloudBackupByAppId[appId], true); + assert.ok(!hasDeleteSuccess(f)); +} + +test('stale queued wallet deletion cannot commit into the returned A session', async () => { + const f = fixture(); + const active = owner(f); + const walletId = 'disposable-wallet'; + f.collections.Wallet.push(wallet(walletId, enums.NetworkType.MAINNET)); + const action = { + originAppId: active.appId, + originBackupAccountSession: active.session(), payload: { walletIds: [walletId] }, - }); - await waitForPause(() => resumeConnectivity); - accountRoundTrip(f, originAppId, () => { session++; }); + }; + active.roundTrip(); f.remote.wallets[walletId] = 'newer-A-ciphertext'; - resumeConnectivity({ isConnected: true }); - assert.equal((await deleting).updated, false); - assert.deepEqual(relayDeletes, []); + assert.equal((await f.run('deleteAppImageEntityWorker', action)).updated, false); + assert.deepEqual(f.calls, []); assert.equal(f.remote.wallets[walletId], 'newer-A-ciphertext'); - assert.deepEqual(f.collections.Wallet.map(({ id }) => id), [walletId]); + assert.deepEqual( + f.collections.Wallet.map(({ id }) => id), + [walletId] + ); }); -test('vault deletion paused at connectivity cannot delete newer A data after A to B to A', async () => { +test('old committed vault deletion receipt cannot delete newer A after returning to A', async () => { const f = fixture(); - const originAppId = f.app.id; - let session = 1; - f.scope.currentBackupAccountSession = () => session; + const active = owner(f); const vaultId = 'disposable-vault'; - const remoteVaults = { [vaultId]: 'initial-A-ciphertext' }; - let resumeConnectivity; - f.scope.NetInfo.fetch = () => new Promise((resolve) => { resumeConnectivity = resolve; }); - const relayDeletes = []; - f.scope.Relay.deleteVaultImage = async ({ appId, vaults }) => { - relayDeletes.push({ appId, vaults }); + f.collections.Vault.push({ id: vaultId, signers: [] }); + const operation = f.scope.beginBackupLocalOperation( + active.appId, + { appId: active.appId, accountSession: active.session() }, + active.session() + ); + const receipt = f.scope.commitBackupLocalOperation(operation, (raw) => { + raw.delete(raw.objectForPrimaryKey('Vault', vaultId)); + }); + assert.equal(receipt.committed, true); + const originalSession = active.session(); + active.roundTrip(); + f.collections.Vault.push({ id: vaultId, signers: [], name: 'newer A vault' }); + const remoteVaults = { [vaultId]: 'newer-A-ciphertext' }; + f.scope.Relay.deleteVaults = async ({ vaults }) => { + f.calls.push(['delete-vault']); vaults.forEach((id) => delete remoteVaults[id]); return { updated: true }; }; - const deleting = f.run('deleteVaultImageWorker', { - originAppId, - originBackupAccountSession: session, + const result = await f.run('deleteVaultImageWorker', { + originAppId: active.appId, + originBackupAccountSession: originalSession, + committedOperation: receipt, payload: { vaultIds: [vaultId] }, }); - await waitForPause(() => resumeConnectivity); - accountRoundTrip(f, originAppId, () => { session++; }); - remoteVaults[vaultId] = 'newer-A-ciphertext'; - resumeConnectivity({ isConnected: true }); - - assert.equal((await deleting).updated, false); - assert.deepEqual(relayDeletes, []); + assert.equal(result.updated, false); + assert.deepEqual(f.calls, []); assert.equal(remoteVaults[vaultId], 'newer-A-ciphertext'); + assert.equal(f.collections.Vault[0].name, 'newer A vault'); }); -test('whole-backup deletion paused before request cannot clear newer A data after A to B to A', async () => { - const f = fixture(); - const originAppId = f.app.id; - let session = 1; - f.scope.currentBackupAccountSession = () => session; +test('whole-backup deletion rejects a preflight captured before A to B to A', async () => { + const f = fixture({ confirmBackupDeletion: true }); + const active = owner(f); f.remote.wallets.shared = 'initial-A-ciphertext'; - const originalRead = f.scope.dbManager.getObjectByIndex; - let appReads = 0; + const beginDelete = f.scope.beginBackupDeleteOperation; let resumePreflight; - f.scope.dbManager.getObjectByIndex = (schema, ...args) => { - if (schema === 'KeeperApp' && ++appReads === 2) - return new Promise((resolve) => { resumePreflight = () => resolve(f.app); }); - return originalRead(schema, ...args); - }; - const relayDeletes = []; - f.scope.Relay.deleteBackup = async ({ appId }) => { - relayDeletes.push(appId); - f.remote.wallets = {}; - return { updated: true }; + f.scope.beginBackupDeleteOperation = async (...args) => { + const receipt = await beginDelete(...args); + return new Promise((resolve) => { + resumePreflight = () => resolve(receipt); + }); }; const deleting = f.run('deleteBackupWorker', { - appId: originAppId, - originBackupAccountSession: session, + appId: active.appId, + originBackupAccountSession: active.session(), }); await waitForPause(() => resumePreflight); - accountRoundTrip(f, originAppId, () => { session++; }); + active.roundTrip(); f.remote.wallets.shared = 'newer-A-ciphertext'; resumePreflight(); assert.equal(await deleting, false); - assert.deepEqual(relayDeletes, []); + assert.deepEqual(f.calls, []); assert.equal(f.remote.wallets.shared, 'newer-A-ciphertext'); - assert.ok(!f.actions.some((action) => - action.type === 'setDeleteBackupSuccess' && action.payload?.status === true)); + assertConsentRetained(f, active.appId); }); -test('queued explicit whole-backup delete cannot clear newer A data after A to B to A', async () => { +test('transport refuses an unbranded delete even when queued across A to B to A', async () => { let active = 'A'; const remote = { wallets: { shared: 'initial-A-ciphertext' } }; const requests = []; const transport = loadModule('src/services/backup/transport.ts', { - '../rest/RestClient': { post: async (path, body) => { - requests.push({ path, body }); - remote.wallets = {}; - return { data: { updated: true } }; - } }, + '../rest/RestClient': { + post: async (path, body) => { + requests.push({ path, body }); + remote.wallets = {}; + return { data: { updated: true } }; + }, + }, }); transport.setBackupUploadGuard((appId, explicitChoice) => appId === active && explicitChoice); transport.observeBackupAccount(active); let release; - const busy = transport.withBackupSession('A', () => new Promise((resolve) => { release = resolve; })); + const busy = transport.withBackupSession( + 'A', + () => + new Promise((resolve) => { + release = resolve; + }) + ); await waitForPause(() => release); const deleting = transport.backupPost('fixture/deleteBackup', { appId: 'A' }, true); @@ -149,43 +170,134 @@ test('queued explicit whole-backup delete cannot clear newer A data after A to B assert.equal(remote.wallets.shared, 'newer-A-ciphertext'); }); -test('in-flight whole-backup deletion after A to B to A marks A for readback', async () => { +test('whole-backup delete queued before A to B to A sends no request for the returned A', async () => { + const f = fixture({ confirmBackupDeletion: true }); + const active = owner(f); + const enqueue = f.scope.enqueueAutomaticBackupOperation; + let resumeQueue; + f.scope.enqueueAutomaticBackupOperation = async (...args) => { + await new Promise((resolve) => { + resumeQueue = resolve; + }); + return enqueue(...args); + }; + f.scope.Relay.deleteBackup = async () => { + f.calls.push(['delete-backup']); + f.remote.wallets = {}; + return { updated: true }; + }; + const deleting = f.run('deleteBackupWorker', { + appId: active.appId, + originBackupAccountSession: active.session(), + }); + await waitForPause(() => resumeQueue); + active.roundTrip(); + f.remote.wallets.shared = 'newer-A-ciphertext'; + resumeQueue(); + + assert.equal(await deleting, false); + assert.deepEqual(f.calls, []); + assert.equal(f.remote.wallets.shared, 'newer-A-ciphertext'); + assert.equal(f.state.bhr.pendingAllBackupByAppId[active.appId], true); + assertConsentRetained(f, active.appId); +}); + +test('wallet deletes locally before queue but cannot remove newer A data after A to B to A', async () => { const f = fixture(); - const originAppId = f.app.id; - let session = 1; - f.scope.currentBackupAccountSession = () => session; + const active = owner(f); + const walletId = 'disposable-wallet'; + f.collections.Wallet.push(wallet(walletId, enums.NetworkType.MAINNET)); + const enqueue = f.scope.enqueueAutomaticBackupOperation; + let resumeQueue; + f.scope.enqueueAutomaticBackupOperation = async (...args) => { + await new Promise((resolve) => { + resumeQueue = resolve; + }); + return enqueue(...args); + }; + const deleting = f.run('deleteAppImageEntityWorker', { + originAppId: active.appId, + originBackupAccountSession: active.session(), + payload: { walletIds: [walletId] }, + }); + await waitForPause(() => resumeQueue); + assert.equal(f.collections.Wallet.length, 0, 'the original A local deletion has committed'); + active.roundTrip(); + const newerWallet = wallet(walletId, enums.NetworkType.MAINNET); + newerWallet.presentationData.name = 'Newer A wallet'; + f.collections.Wallet.push(newerWallet); + f.remote.wallets[walletId] = 'newer-A-ciphertext'; + resumeQueue(); + + const result = await deleting; + assert.equal(result.updated, false); + assert.equal(result.localCommitted, true); + assert.deepEqual(f.calls, []); + assert.equal(f.remote.wallets[walletId], 'newer-A-ciphertext'); + assert.equal(f.collections.Wallet[0].presentationData.name, 'Newer A wallet'); + assert.equal(f.state.bhr.pendingAllBackupByAppId[active.appId], true); +}); + +test('whole-delete acknowledgement after A to B to A cannot clear consent or report success', async () => { + const f = fixture({ confirmBackupDeletion: true }); + const active = owner(f); let finishDelete; - f.scope.Relay.deleteBackup = () => new Promise((resolve) => { finishDelete = resolve; }); + f.scope.Relay.deleteBackup = () => { + f.calls.push(['delete-backup']); + f.remote.wallets = {}; + return new Promise((resolve) => { + finishDelete = resolve; + }); + }; const deleting = f.run('deleteBackupWorker', { - appId: originAppId, originBackupAccountSession: session, + appId: active.appId, + originBackupAccountSession: active.session(), }); await waitForPause(() => finishDelete); - accountRoundTrip(f, originAppId, () => { session++; }); + active.roundTrip(); + f.remote.wallets.shared = 'newer-A-ciphertext'; finishDelete({ updated: true }); + assert.equal(await deleting, false); - assert.equal(f.state.bhr.pendingAllBackupByAppId[originAppId], true); - assert.ok(f.actions.some((action) => action.type === 'invalidateBackupRepair')); - assert.ok(!f.actions.some((action) => action.type === 'setDeleteBackupSuccess' && action.payload?.status)); + assert.equal(f.remote.wallets.shared, 'newer-A-ciphertext'); + assert.equal(f.state.bhr.pendingAllBackupByAppId[active.appId], true); + assertConsentRetained(f, active.appId); }); -test('in-flight wallet deletion after A to B to A preserves local A and marks readback', async () => { +test('wallet delete acknowledgement after A to B to A cannot report a new-session deletion', async () => { const f = fixture(); - const originAppId = f.app.id; - let session = 1; - f.scope.currentBackupAccountSession = () => session; + const active = owner(f); const walletId = 'disposable-wallet'; f.collections.Wallet.push(wallet(walletId, enums.NetworkType.MAINNET)); let finishDelete; - f.scope.Relay.deleteAppImageEntity = () => new Promise((resolve) => { finishDelete = resolve; }); - const deleting = f.run('deleteAppImageEntityWorker', { - originAppId, - originBackupAccountSession: session, + f.scope.Relay.deleteAppImageEntity = ({ walletIds }) => { + f.calls.push(['delete']); + walletIds.forEach((id) => delete f.remote.wallets[id]); + return new Promise((resolve) => { + finishDelete = resolve; + }); + }; + const completion = []; + const deleting = f.run('deleteAppImageEntityRequestWorker', { + originAppId: active.appId, + originBackupAccountSession: active.session(), payload: { walletIds: [walletId] }, + onComplete: (deleted) => completion.push(deleted), }); await waitForPause(() => finishDelete); - accountRoundTrip(f, originAppId, () => { session++; }); + assert.equal(f.collections.Wallet.length, 0); + active.roundTrip(); + f.collections.Wallet.push(wallet(walletId, enums.NetworkType.MAINNET)); + f.remote.wallets[walletId] = 'newer-A-ciphertext'; finishDelete({ updated: true }); - assert.equal((await deleting).updated, false); - assert.equal(f.state.bhr.pendingAllBackupByAppId[originAppId], true); - assert.deepEqual(f.collections.Wallet.map(({ id }) => id), [walletId]); + + assert.equal(await deleting, false); + assert.deepEqual(completion, [false]); + assert.deepEqual(f.calls, [['delete']]); + assert.equal(f.remote.wallets[walletId], 'newer-A-ciphertext'); + assert.deepEqual( + f.collections.Wallet.map(({ id }) => id), + [walletId] + ); + assert.equal(f.state.bhr.pendingAllBackupByAppId[active.appId], true); }); From ac2cd89de7371d90a387dbb6dadd2eb59f8df808 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Tue, 13 Oct 2026 03:44:42 +0000 Subject: [PATCH 158/254] Exercise wallet and vault producers with native Realm and Relay --- .../automaticWalletProducer.integration.cjs | 215 ++++++++++++++++++ 1 file changed, 215 insertions(+) create mode 100644 tests/recovery/automaticWalletProducer.integration.cjs diff --git a/tests/recovery/automaticWalletProducer.integration.cjs b/tests/recovery/automaticWalletProducer.integration.cjs new file mode 100644 index 0000000000..0e8551907d --- /dev/null +++ b/tests/recovery/automaticWalletProducer.integration.cjs @@ -0,0 +1,215 @@ +// Production wallet/vault producers with native encrypted Realm, actual AES, +// branded source receipts and real transactional Relay HTTP. Factory adapters +// supply complete, intentionally unspendable public fixtures; no DB or backup +// commit/proof is mocked. +const assert = require('node:assert/strict'); +const { test, before, after } = require('node:test'); +const { createRealmClient, declarations, evaluate, plain, Realm } = require('./automaticWriteRealmRelay.fixture.cjs'); +const fixtureModule = process.env.KEEPER_RELAY_FIXTURE_MODULE; +assert.ok(fixtureModule, 'Set KEEPER_RELAY_FIXTURE_MODULE to the pinned production Relay fixture'); +const { createFixture } = require(fixtureModule); +let server; +before(async () => { server = await createFixture(); }); +after(async () => { await server?.close(); Realm.shutdown(); }); +const writes = () => server.trace.filter(event => event.path === '/revisionedBackupMutation'); + +function wallet(f, id = 'wallet') { + return f.defaults(f.RealmSchema.Wallet, { + id, entityKind: f.enums.EntityKind.WALLET, type: f.enums.WalletType.DEFAULT, + networkType: f.enums.NetworkType.TESTNET, scriptType: 'P2WPKH', + derivationDetails: f.defaults(f.RealmSchema.WalletDerivationDetails, { instanceNum: 0, xDerivationPath: "m/84'/1'/0'" }), + presentationData: f.defaults(f.RealmSchema.WalletPresentationData, { name: 'Initial wallet', visibility: 'DEFAULT' }), + specs: f.defaults(f.RealmSchema.WalletSpecs, { xpub: `disposable-public-${id}`, nextFreeAddressIndex: 1 }), + }); +} + +function signer(f) { + const record = f.defaults(f.RealmSchema.Signer, { + masterFingerprint: 'aaaa0001', type: f.enums.SignerType.COLDCARD, storageType: f.enums.SignerStorage.COLD, + signerName: 'Disposable public key', networkType: f.enums.NetworkType.TESTNET, + signerXpubs: f.defaults(f.RealmSchema.SignerXpubs, { + [f.enums.XpubTypes.P2WSH]: [{ xpub: 'disposable-vault-public-key', xpriv: null, derivationPath: "m/48'/1'/0'/2'" }], + }), + }); + record.id = f.utilities.getKeyUID(record); + return record; +} + +function vault(f, id, key) { + return f.defaults(f.RealmSchema.Vault, { + id, entityKind: f.enums.EntityKind.VAULT, type: f.enums.VaultType.DEFAULT, + networkType: f.enums.NetworkType.TESTNET, isMultiSig: true, scriptType: 'P2WSH', + scheme: f.defaults(f.RealmSchema.VaultScheme, { m: 1, n: 1 }), signers: [key], + presentationData: f.defaults(f.RealmSchema.VaultPresentationData, { name: id, visibility: 'DEFAULT' }), + specs: f.defaults(f.RealmSchema.VaultSpecs, { xpubs: [key.xpub], nextFreeAddressIndex: 1 }), + }); +} + +async function setup(t, prepare = () => {}) { + await server.reset(); + const f = await createRealmClient(server.url); + t.after(() => f.close()); + f.automatic = f.load('src/services/backup/automatic'); + f.installProductionStoreObservers(f.automatic); + f.store.dispatch(f.bhr.setAutomaticCloudBackup({ appId: f.appId, enabled: true })); + prepare(f); + await server.seed(await f.payloadForCurrentImage()); + assert.equal(await f.repair.inspectBackup(f.appId, false, () => {}), 'verified'); + server.clearTrace(); + const sagas = f.productionSagas([ + 'currentBackupAppId', 'buildAutomaticAppMutation', 'buildAutomaticVaultMutation', + 'queueCommittedBackupWorker', 'updateAppImageWorker', 'updateVaultImageWorker', + ], { + ...f.automatic, + addNewWallet: async (_type, details) => details.fixture, + generateVault: async ({ signers }) => vault(f, 'new-vault', signers[0]), + uaiChecks: payload => ({ type: 'fixture-uai', payload }), + uaiType: { SECURE_VAULT: 'SECURE_VAULT' }, + initiateVaultMigration: payload => ({ type: 'fixture-migration', payload }), + vaultCreated: payload => ({ type: 'fixture-vault-created', payload }), + setIsCloudBsmsBackupRequired: payload => ({ type: 'fixture-bsms-required', payload }), + backupBsmsOnCloud: () => ({ type: 'fixture-bsms-queue' }), + }); + evaluate(declarations('src/store/sagas/wallets.ts', [ + 'currentWalletAccountId', 'assertWalletAccount', 'addNewWalletsWorker', + 'updateWalletDetailsWorker', 'addNewVaultWorker', + ]), sagas.scope, 'production-wallet-producers.ts'); + f.scope = sagas.scope; + f.run = (name, payload, metadata = {}) => sagas.run(name, { + payload, originAppId: f.appId, + originBackupAccountSession: f.transport.currentBackupAccountSession(), + originBackupWriteIntent: f.transport.captureBackupWriteIntent(f.appId), ...metadata, + }); + f.remoteImage = async () => f.image.decodeImage(f.encryption.generateEncryptionKey(f.seed), + await server.snapshot(f.appId), f.appId, () => {}, true); + return f; +} + +const newWallets = (f, ...records) => records.map(record => ({ + walletType: f.enums.WalletType.DEFAULT, walletDetails: { fixture: record }, +})); + +test('actual wallet creation atomically saves both wallets before the first genuine HTTP mutation', async t => { + const f = await setup(t); + server.armFault({ beforeMutation() { + for (const id of ['first', 'second']) assert.ok(f.dbManager.getObjectById(f.RealmSchema.Wallet, id)); + } }); + assert.equal(await f.run('addNewWalletsWorker', newWallets(f, wallet(f, 'first'), wallet(f, 'second'))), true); + assert.equal(writes().length, 1); + assert.equal(writes()[0].committed, true); + assert.deepEqual(Object.keys((await f.remoteImage()).wallets).sort(), ['first', 'second']); + assert.equal(f.store.getState().bhr.pendingAllBackupByAppId[f.appId], false); +}); + +test('actual second wallet insertion failure rolls back the first wallet and never reaches HTTP', async t => { + const f = await setup(t); + assert.equal(await f.run('addNewWalletsWorker', newWallets(f, wallet(f, 'duplicate'), wallet(f, 'duplicate'))), false); + assert.equal(f.dbManager.getCollection(f.RealmSchema.Wallet).length, 0); + assert.equal(writes().length, 0); + assert.equal(f.store.getState().bhr.pendingAllBackupByAppId[f.appId], true); +}); + +test('actual wallet rename survives HTTP conflict and reports local success with backup pending', async t => { + const f = await setup(t, f => assert.equal(f.dbManager.createObject(f.RealmSchema.Wallet, wallet(f)), true)); + server.armFault({ mode: '409', operation: 'updateAppImage', beforeMutation() { + assert.equal(f.dbManager.getObjectById(f.RealmSchema.Wallet, 'wallet').presentationData.name, 'Committed rename'); + } }); + assert.equal(await f.run('updateWalletDetailsWorker', { + wallet: wallet(f), details: { name: 'Committed rename', description: 'Local description' }, + }), true); + assert.equal(writes().length, 1); + assert.equal(f.dbManager.getObjectById(f.RealmSchema.Wallet, 'wallet').presentationData.name, 'Committed rename'); + assert.equal(f.store.getState().bhr.pendingAllBackupByAppId[f.appId], true); + assert.equal((await f.remoteImage()).wallets.wallet.presentationData.name, 'Initial wallet'); +}); + +test('actual old-session wallet factory completion cannot save after A to B to A', async t => { + const f = await setup(t); + let entered, release; + const started = new Promise(resolve => { entered = resolve; }); + const gate = new Promise(resolve => { release = resolve; }); + f.scope.addNewWallet = async (_type, details) => { entered(); await gate; return details.fixture; }; + const running = f.run('addNewWalletsWorker', newWallets(f, wallet(f))); + await started; + await f.selectAccount('disposable-other-account'); + await f.selectAccount(f.appId); + release(); + assert.equal(await running, false); + assert.equal(f.dbManager.getCollection(f.RealmSchema.Wallet).length, 0); + assert.equal(writes().length, 0); +}); + +test('a current wallet receipt cannot send stale encrypted presentation data', async t => { + const f = await setup(t, f => assert.equal(f.dbManager.createObject(f.RealmSchema.Wallet, wallet(f)), true)); + const operation = f.automatic.beginBackupLocalOperation(f.appId, f.transport.captureBackupWriteIntent(f.appId)); + const receipt = f.automatic.commitBackupLocalOperation(operation, realm => { + realm.objectForPrimaryKey('Wallet', 'wallet').presentationData.name = 'Committed rename'; + }); + const body = { appId: f.appId, walletsObject: { + wallet: await f.image.encryptRecord(f.encryption.generateEncryptionKey(f.seed), wallet(f)), + } }; + const result = await f.automatic.enqueueAutomaticBackupOperation(receipt, [{ operation: 'updateAppImage', body }]); + assert.equal(result.backupPending, true); + assert.equal(writes().length, 0); + assert.equal(f.dbManager.getObjectById(f.RealmSchema.Wallet, 'wallet').presentationData.name, 'Committed rename'); +}); + +async function vaultSetup(t) { + return setup(t, f => { + const parent = signer(f); + const key = f.defaults(f.RealmSchema.VaultSigner, { + ...parent.signerXpubs[f.enums.XpubTypes.P2WSH][0], masterFingerprint: parent.masterFingerprint, xfp: parent.masterFingerprint, + xpriv: 'unspendable-disposable-private-material', + registeredVaults: [{ vaultId: 'old-vault', registered: true, hmac: 'disposable-hmac' }], + }); + assert.equal(f.dbManager.createObject(f.RealmSchema.Signer, parent), true); + assert.equal(f.dbManager.createObject(f.RealmSchema.Vault, vault(f, 'old-vault', key)), true); + }); +} + +function newVaultPayload(f, migrated = false) { + const key = plain(f.dbManager.getObjectByPrimaryId(f.RealmSchema.VaultSigner, 'xpub', 'disposable-vault-public-key')); + return { isMigrated: migrated, oldVaultId: migrated ? 'old-vault' : undefined, newVaultInfo: { + vaultType: f.enums.VaultType.DEFAULT, vaultScheme: { m: 1, n: 1 }, + vaultSigners: [{ ...key, registeredVaults: [], xpriv: null }], + vaultDetails: { name: 'New vault', description: '' }, + } }; +} + +test('actual vault creation preserves shared signer registrations and remembered private material', async t => { + const f = await vaultSetup(t); + assert.equal(await f.run('addNewVaultWorker', newVaultPayload(f)), true); + assert.equal(writes().length, 1); + assert.equal(writes()[0].committed, true); + const stored = f.dbManager.getObjectByPrimaryId(f.RealmSchema.VaultSigner, 'xpub', 'disposable-vault-public-key'); + assert.equal(stored.xpriv, 'unspendable-disposable-private-material'); + assert.equal(stored.registeredVaults[0].vaultId, 'old-vault'); + assert.equal(stored.registeredVaults[0].hmac, 'disposable-hmac'); + const image = await f.remoteImage(); + assert.ok(image.vaults['old-vault']); + assert.ok(image.vaults['new-vault']); + assert.equal(f.store.getState().bhr.pendingAllBackupByAppId[f.appId], false); +}); + +for (const failSecond of [false, true]) test(`actual vault migration commits both local effects then sends old archive before new vault (second fails=${failSecond})`, async t => { + const f = await vaultSetup(t); + const sent = []; + if (failSecond) server.armFault({ mode: '409', operation: 'updateVaultImage', after: 1 }); + else { + for (let i = 0; i < 2; i++) server.armFault({ beforeMutation({ body }) { + sent.push(body.vaultId); + assert.equal(f.dbManager.getObjectById(f.RealmSchema.Vault, 'old-vault').archived, true); + assert.ok(f.dbManager.getObjectById(f.RealmSchema.Vault, 'new-vault')); + } }); + } + assert.equal(await f.run('addNewVaultWorker', newVaultPayload(f, true)), true); + assert.equal(writes().length, 2); + assert.equal(writes()[0].committed, true); + if (!failSecond) assert.deepEqual(sent, ['old-vault', 'new-vault']); + assert.equal(f.dbManager.getObjectById(f.RealmSchema.Vault, 'old-vault').archived, true); + assert.ok(f.dbManager.getObjectById(f.RealmSchema.Vault, 'new-vault')); + const image = await f.remoteImage(); + assert.equal(image.vaults['old-vault'].archived, true); + assert.equal(!!image.vaults['new-vault'], !failSecond); + assert.equal(f.store.getState().bhr.pendingAllBackupByAppId[f.appId], failSecond); +}); From ad6c9dce748863c089b8db1922400fb4a4ed9af6 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Tue, 13 Oct 2026 03:46:31 +0000 Subject: [PATCH 159/254] test: bind vault repair and address counter evidence to current contracts --- tests/recovery/sharedVaultRepair.test.cjs | 348 +++++++++++++----- .../recovery/walletRefreshIsolation.test.cjs | 148 ++++++-- 2 files changed, 364 insertions(+), 132 deletions(-) diff --git a/tests/recovery/sharedVaultRepair.test.cjs b/tests/recovery/sharedVaultRepair.test.cjs index 0ff557791f..e2d1412027 100644 --- a/tests/recovery/sharedVaultRepair.test.cjs +++ b/tests/recovery/sharedVaultRepair.test.cjs @@ -2,78 +2,164 @@ const { test } = require('node:test'); const assert = require('node:assert/strict'); const { encryption, image, harness, wallet, loadModule } = require('./helpers.cjs'); const { prepareRecoveryImage } = loadModule('src/services/backup/restore.ts', { './image': image }); -const clone = value => JSON.parse(JSON.stringify(value)); -function fixture({ unavailable = ['shared'], leaveUnavailable = false } = {}) { +const clone = (value) => JSON.parse(JSON.stringify(value)); +function fixture({ + unavailable = ['shared'], + leaveUnavailable = false, + ackRevision = 'valid', +} = {}) { const state = { - appImage: { appId:'disposable', wallets:{}, signers:{}, nodes:[], vaults:['shared'], labels:[] }, - allVaultImages:[], labels:[], unavailableVaultIds:unavailable, revision:'a'.repeat(64), - revisionedMutations:true, + appImage: { + appId: 'disposable', + wallets: {}, + signers: {}, + nodes: [], + vaults: ['shared'], + labels: [], + }, + allVaultImages: [], + labels: [], + unavailableVaultIds: unavailable, + revision: 'a'.repeat(64), + revisionedMutations: true, }; - let uploads=0; - const f=harness({ adapter:async (endpoint,payload) => { - if(endpoint==='getBackupSnapshot') return {data:clone(state)}; - assert.equal(endpoint,'revisionedBackupMutation'); - assert.equal(payload.operation,'repairAppBackup'); - assert.equal(payload.expectedRevision,state.revision); - assert.equal(payload.replaceCurrentState,true); - uploads++; - Object.assign(state.appImage, {wallets:payload.walletObject,signers:payload.signersObject, - nodes:payload.nodes,vaults:Object.keys(payload.vaultObject),labels:payload.labels.map(v=>v.id)}); - state.allVaultImages=Object.values(payload.vaultObject); - state.labels=payload.labels; - if (!leaveUnavailable) state.unavailableVaultIds=[]; - state.revision='b'.repeat(64); - return {data:{updated:true,preconditionApplied:true}}; - }}); - f.local.Vault.push({...wallet('shared'),signers:[],scheme:{m:1,n:1},archived:false}); - const key=encryption.generateEncryptionKey(f.app.primarySeed); - return {f,state,key,uploads:()=>uploads}; + let uploads = 0; + const f = harness({ + adapter: async (endpoint, payload) => { + if (endpoint === 'getBackupSnapshot') return { data: clone(state) }; + assert.equal(endpoint, 'revisionedBackupMutation'); + assert.equal(payload.operation, 'repairAppBackup'); + assert.equal(payload.expectedRevision, state.revision); + assert.equal(payload.replaceCurrentState, true); + uploads++; + Object.assign(state.appImage, { + publicId: payload.publicId, + version: payload.version, + subscription: payload.subscription, + wallets: payload.walletObject, + signers: payload.signersObject, + nodes: payload.nodes, + vaults: Object.keys(payload.vaultObject), + labels: payload.labels.map((v) => v.id), + }); + state.allVaultImages = Object.values(payload.vaultObject); + state.labels = payload.labels; + if (!leaveUnavailable) state.unavailableVaultIds = []; + state.revision = 'b'.repeat(64); + const replyRevision = + ackRevision === 'valid' + ? state.revision + : ackRevision === 'missing' + ? undefined + : ackRevision === 'old' + ? 'a'.repeat(64) + : ackRevision === 'other' + ? 'c'.repeat(64) + : 'invalid'; + return { data: { updated: true, preconditionApplied: true, newRevision: replyRevision } }; + }, + }); + Object.assign(state.appImage, { + publicId: f.app.publicId, + version: f.app.version, + subscription: JSON.stringify(f.app.subscription), + }); + f.local.Vault.push({ ...wallet('shared'), signers: [], scheme: { m: 1, n: 1 }, archived: false }); + const key = encryption.generateEncryptionKey(f.app.primarySeed); + return { f, state, key, uploads: () => uploads }; } -test('unavailable collaborative ciphertext requires user action and cannot be clean-restored',async()=>{ - const {f,state,key,uploads}=fixture(); - assert.equal(await f.run(),'different'); - assert.equal(uploads(),0); - assert.deepEqual(f.calls,['getBackupSnapshot']); - await assert.rejects(prepareRecoveryImage(key,f.app.id,state.appImage,state.allVaultImages,state.labels)); -}); -test('explicit local resubmission replaces missing account copy and verifies clean recovery',async()=>{ - const {f,state,key,uploads}=fixture(); - assert.equal(await f.run(true),'verified'); - assert.equal(uploads(),1); - assert.deepEqual(state.unavailableVaultIds,[]); - const recovered=await prepareRecoveryImage(key,f.app.id,state.appImage,state.allVaultImages,state.labels); - assert.deepEqual(clone(recovered.vaults.shared),f.local.Vault[0]); - assert.deepEqual(f.calls,['getBackupSnapshot','revisionedBackupMutation','getBackupSnapshot']); -}); -test('missing server-only vault blocks current-state replacement',async()=>{ - const {f,state,uploads}=fixture(); f.local.Vault.length=0; - assert.equal(await f.run(),'different'); assert.equal(uploads(),0); - assert.equal(await f.run(true),'conflict'); - assert.deepEqual(state.appImage.vaults,['shared']); assert.equal(uploads(),0); +test('unavailable collaborative ciphertext requires user action and cannot be clean-restored', async () => { + const { f, state, key, uploads } = fixture(); + assert.equal(await f.run(), 'different'); + assert.equal(uploads(), 0); + assert.deepEqual(f.calls, ['getBackupSnapshot']); + await assert.rejects( + prepareRecoveryImage(key, f.app.id, state.appImage, state.allVaultImages, state.labels) + ); }); -for(const problem of ['identity','ahead address counter']) test(`available ${problem} conflict still blocks missing-copy repair`,async()=>{ - const {f,state,key,uploads}=fixture(); - f.local.Wallet.push(wallet('other')); const remote=wallet('other'); - if(problem==='identity') remote.specs.xpub='another-key'; - else remote.specs.nextFreeAddressIndex=2; - state.appImage.wallets.other=encryption.encrypt(key,JSON.stringify(remote)); - assert.equal(await f.run(true),'conflict'); assert.equal(uploads(),0); +test('explicit local resubmission replaces missing account copy and verifies clean recovery', async () => { + const { f, state, key, uploads } = fixture(); + assert.equal(await f.run(true), 'verified'); + assert.equal(uploads(), 1); + assert.deepEqual(state.unavailableVaultIds, []); + const recovered = await prepareRecoveryImage( + key, + f.app.id, + state.appImage, + state.allVaultImages, + state.labels + ); + assert.deepEqual(clone(recovered.vaults.shared), f.local.Vault[0]); + assert.deepEqual(f.calls, ['getBackupSnapshot', 'revisionedBackupMutation', 'getBackupSnapshot']); }); -for(const unavailable of [['not-referenced'],['shared','shared'],[null],'shared']) test(`malformed missing-copy metadata cannot authorize an upload: ${JSON.stringify(unavailable)}`,async()=>{ - const {f,uploads}=fixture({unavailable}); - assert.equal(await f.run(true),'unverified'); assert.equal(uploads(),0); +test('missing server-only vault blocks current-state replacement', async () => { + const { f, state, uploads } = fixture(); + f.local.Vault.length = 0; + assert.equal(await f.run(), 'different'); + assert.equal(uploads(), 0); + assert.equal(await f.run(true), 'conflict'); + assert.deepEqual(state.appImage.vaults, ['shared']); + assert.equal(uploads(), 0); }); -test('an acknowledged upload that still has unavailable vaults is never verified',async()=>{ - const {f,uploads}=fixture({leaveUnavailable:true}); - assert.equal(await f.run(true),'unverified'); assert.equal(uploads(),1); +for (const problem of ['identity', 'ahead address counter']) + test(`available ${problem} conflict still blocks missing-copy repair`, async () => { + const { f, state, key, uploads } = fixture(); + f.local.Wallet.push(wallet('other')); + const remote = wallet('other'); + if (problem === 'identity') remote.specs.xpub = 'another-key'; + else remote.specs.nextFreeAddressIndex = 2; + state.appImage.wallets.other = encryption.encrypt(key, JSON.stringify(remote)); + assert.equal(await f.run(true), 'conflict'); + assert.equal(uploads(), 0); + }); +for (const unavailable of [['not-referenced'], ['shared', 'shared'], [null], 'shared']) + test(`malformed missing-copy metadata cannot authorize an upload: ${JSON.stringify( + unavailable + )}`, async () => { + const { f, uploads } = fixture({ unavailable }); + assert.equal(await f.run(true), 'unverified'); + assert.equal(uploads(), 0); + }); +test('an acknowledged upload that still has unavailable vaults is never verified', async () => { + const { f, uploads } = fixture({ leaveUnavailable: true }); + assert.equal(await f.run(true), 'unverified'); + assert.equal(uploads(), 1); }); -test('archive metadata mismatch is read-only until explicit backup and must match after readback',async()=>{ - const {f,state,key,uploads}=fixture({unavailable:[]}); - f.local.Vault[0].archived=true; - state.allVaultImages=[{vaultId:'shared',vault:encryption.encrypt(key,JSON.stringify(f.local.Vault[0])),isArchived:false}]; - assert.equal(await f.run(),'different'); assert.equal(uploads(),0); - assert.equal(await f.run(true),'verified'); assert.equal(uploads(),1); - assert.equal(state.allVaultImages[0].isArchived,true); +for (const ackRevision of ['missing', 'old', 'malformed', 'other']) + test(`clean repaired ciphertext without a matching new-revision acknowledgement remains unverified: ${ackRevision}`, async () => { + const { f, state, key, uploads } = fixture({ ackRevision }); + assert.equal(await f.run(true), 'unverified'); + assert.equal(uploads(), 1); + assert.deepEqual(state.unavailableVaultIds, []); + const restored = await prepareRecoveryImage( + key, + f.app.id, + state.appImage, + state.allVaultImages, + state.labels + ); + assert.deepEqual(clone(restored.vaults.shared), f.local.Vault[0]); + assert.equal( + f.transport.captureBackupWriteIntent(f.app.id), + undefined, + 'a readable copy alone must not mint write authority' + ); + }); +test('archive metadata mismatch is read-only until explicit backup and must match after readback', async () => { + const { f, state, key, uploads } = fixture({ unavailable: [] }); + f.local.Vault[0].archived = true; + state.allVaultImages = [ + { + vaultId: 'shared', + vault: encryption.encrypt(key, JSON.stringify(f.local.Vault[0])), + isArchived: false, + }, + ]; + assert.equal(await f.run(), 'different'); + assert.equal(uploads(), 0); + assert.equal(await f.run(true), 'verified'); + assert.equal(uploads(), 1); + assert.equal(state.allVaultImages[0].isArchived, true); }); function orphanLabelFixture(hasLocalLabel = true, malformedMarker = false) { @@ -82,22 +168,44 @@ function orphanLabelFixture(hasLocalLabel = true, malformedMarker = false) { const key = encryption.generateEncryptionKey(app.primarySeed); const opaqueId = encryption.hash256(encryption.hash256(key + label.id)); const state = { - appImage: { appId: app.id, wallets: {}, signers: {}, nodes: [], vaults: [], labels: [opaqueId] }, - allVaultImages: [], labels: [], revision: 'a'.repeat(64), revisionedMutations: true, + appImage: { + appId: app.id, + wallets: {}, + signers: {}, + nodes: [], + vaults: [], + labels: [opaqueId], + }, + allVaultImages: [], + labels: [], + revision: 'a'.repeat(64), + revisionedMutations: true, unavailableLabelIds: [malformedMarker ? 'not-referenced' : opaqueId], }; let uploads = 0; - const f = harness({ adapter: async (endpoint, payload) => { - if (endpoint === 'getBackupSnapshot') return { data: clone(state) }; - assert.equal(endpoint, 'revisionedBackupMutation'); - assert.equal(payload.operation, 'repairAppBackup'); - uploads++; - state.labels = payload.labels; - state.appImage.labels = payload.labels.map((record) => record.id); - state.unavailableLabelIds = []; - state.revision = 'b'.repeat(64); - return { data: { updated: true, preconditionApplied: true } }; - } }); + const f = harness({ + adapter: async (endpoint, payload) => { + if (endpoint === 'getBackupSnapshot') return { data: clone(state) }; + assert.equal(endpoint, 'revisionedBackupMutation'); + assert.equal(payload.operation, 'repairAppBackup'); + uploads++; + state.labels = payload.labels; + state.appImage.labels = payload.labels.map((record) => record.id); + Object.assign(state.appImage, { + publicId: payload.publicId, + version: payload.version, + subscription: payload.subscription, + }); + state.unavailableLabelIds = []; + state.revision = 'b'.repeat(64); + return { data: { updated: true, preconditionApplied: true, newRevision: state.revision } }; + }, + }); + Object.assign(state.appImage, { + publicId: f.app.publicId, + version: f.app.version, + subscription: JSON.stringify(f.app.subscription), + }); if (hasLocalLabel) f.local.Tags.push(label); return { f, state, uploads: () => uploads, key, opaqueId }; } @@ -117,8 +225,14 @@ test('matching local label can safely fill orphan ciphertext during explicit rep assert.equal(await f.run(true), 'verified'); assert.equal(uploads(), 1); assert.deepEqual(state.unavailableLabelIds, []); - assert.deepEqual(clone((await prepareRecoveryImage(key, f.app.id, state.appImage, [], state.labels)).labels['fixture-label']), - f.local.Tags[0]); + assert.deepEqual( + clone( + (await prepareRecoveryImage(key, f.app.id, state.appImage, [], state.labels)).labels[ + 'fixture-label' + ] + ), + f.local.Tags[0] + ); }); test('malformed orphan label marker never authorizes replacement', async () => { @@ -127,23 +241,57 @@ test('malformed orphan label marker never authorizes replacement', async () => { assert.equal(uploads(), 0); }); -for(const archived of [true,false]) test(`existing-wallet update carries signer maps and archive state: archived=${archived}`,async()=>{ - const {fixture:sagaFixture,enums,signer}=require('./sagaHarness.cjs'); - const f=sagaFixture(); - const device=signer('AABBCCDD',enums.NetworkType.MAINNET,enums.SignerType.LEDGER,0); - device.id=f.scope.getKeyUID(device); - f.collections.Signer.push(device); - const key={masterFingerprint:device.masterFingerprint,...device.signerXpubs[enums.XpubTypes.P2WPKH][0]}; - const vault={id:'reinstated',networkType:enums.NetworkType.MAINNET,signers:[key],scheme:{m:1,n:1},archived}; - let uploaded; - f.scope.Relay.updateVaultImage=async payload=>{uploaded=payload;return {updated:true};}; - await f.run('updateVaultImageWorker',{payload:{vault,isUpdate:true}}); - assert.equal(uploaded.appId,f.app.id); - assert.equal(uploaded.isArchived,archived); - assert.equal(uploaded.signersData.length,1); - assert.equal(uploaded.signersData[0].signerId,f.scope.getKeyUID(key)); - assert.equal(uploaded.signersData[0].xfpHash,encryption.hash256(key.masterFingerprint)); - const restored=JSON.parse(encryption.decrypt(encryption.generateEncryptionKey(f.app.primarySeed),uploaded.vault)); - assert.equal(restored.archived,uploaded.isArchived); - assert.equal(restored.signers[0].masterFingerprint,key.masterFingerprint); -}); +for (const archived of [true, false]) + test(`existing-wallet update carries signer maps and archive state: archived=${archived}`, async () => { + const { fixture: sagaFixture, enums, signer } = require('./sagaHarness.cjs'); + const f = sagaFixture(); + const device = signer('AABBCCDD', enums.NetworkType.MAINNET, enums.SignerType.LEDGER, 0); + device.id = f.scope.getKeyUID(device); + f.collections.Signer.push(device); + const key = { + masterFingerprint: device.masterFingerprint, + ...device.signerXpubs[enums.XpubTypes.P2WPKH][0], + }; + const vault = { + id: 'reinstated', + networkType: enums.NetworkType.MAINNET, + signers: [key], + scheme: { m: 1, n: 1 }, + archived, + }; + f.collections.Vault.push({ ...vault, archived: !archived }); + const session = f.scope.currentBackupAccountSession(); + const operation = f.scope.beginBackupLocalOperation( + f.app.id, + { appId: f.app.id, accountSession: session }, + session + ); + const committedOperation = f.scope.commitBackupLocalOperation(operation, (raw) => { + raw.objectForPrimaryKey('Vault', vault.id).archived = archived; + }); + assert.equal(committedOperation.committed, true); + let uploaded; + f.scope.Relay.updateVaultImage = async (payload) => { + uploaded = payload; + return { updated: true }; + }; + const result = await f.run('updateVaultImageWorker', { + originAppId: f.app.id, + originBackupAccountSession: session, + committedOperation, + payload: { vault: { ...vault, archived: !archived }, isUpdate: true }, + }); + assert.equal(result.updated, true); + assert.equal(result.backupConfirmed, false, 'unit Relay acknowledgement is not readback proof'); + assert.equal(f.collections.Vault[0].archived, archived); + assert.equal(uploaded.appId, f.app.id); + assert.equal(uploaded.isArchived, archived); + assert.equal(uploaded.signersData.length, 1); + assert.equal(uploaded.signersData[0].signerId, f.scope.getKeyUID(key)); + assert.equal(uploaded.signersData[0].xfpHash, encryption.hash256(key.masterFingerprint)); + const restored = JSON.parse( + encryption.decrypt(encryption.generateEncryptionKey(f.app.primarySeed), uploaded.vault) + ); + assert.equal(restored.archived, uploaded.isArchived); + assert.equal(restored.signers[0].masterFingerprint, key.masterFingerprint); + }); diff --git a/tests/recovery/walletRefreshIsolation.test.cjs b/tests/recovery/walletRefreshIsolation.test.cjs index c225d7bb2c..d4f499f0f6 100644 --- a/tests/recovery/walletRefreshIsolation.test.cjs +++ b/tests/recovery/walletRefreshIsolation.test.cjs @@ -3,6 +3,7 @@ const assert = require('node:assert/strict'); const vm = require('node:vm'); const ts = require('typescript'); const { fixture, enums, wallet, loadModule, functions } = require('./sagaHarness.cjs'); +const { image, encryption } = require('./helpers.cjs'); const walletState = loadModule('src/store/reducers/wallets.ts', { 'src/storage': { reduxStorage: {} }, '../sagaActions/wallets': { ADD_NEW_WALLETS: 'ADD_NEW_WALLETS' }, @@ -19,7 +20,9 @@ function refreshFixture(kind, archived = false) { presentationData: { visibility: archived ? 'HIDDEN' : 'DEFAULT' }, specs: { balances: { confirmed: 0, unconfirmed: 0 }, - confirmedUTXOs: [], unconfirmedUTXOs: [], transactions: [], + confirmedUTXOs: [], + unconfirmedUTXOs: [], + transactions: [], addresses: { external: {}, internal: {} }, }, }; @@ -33,33 +36,41 @@ function refreshFixture(kind, archived = false) { }; f.scope.WalletUtilities.getNetworkByType = () => ({}); f.scope.classifyDustByAddress = () => ({ - taintedAddresses: new Set(), initialTaintAddresses: new Set(), dustSpendTxids: new Set(), + taintedAddresses: new Set(), + initialTaintAddresses: new Set(), + dustSpendTxids: new Set(), }); f.scope.setSyncing = walletState.setSyncing; f.scope.ELECTRUM_NOT_CONNECTED_ERR = 'offline'; f.scope.ELECTRUM_NOT_CONNECTED_ERR_TOR = 'tor-offline'; let finishSync; - f.scope.WalletOperations = { syncWalletsViaElectrumClient: (wallets) => { - const synced = f.scope.getJSONFromRealmObject(wallets[0]); - synced.specs.balances.unconfirmed = 1000; - synced.specs.hasNewUpdates = true; - return new Promise((resolve) => { - finishSync = () => resolve({ synchedWallets: [{ synchedWallet: synced, newUTXOs: [] }] }); - }); - } }; + f.scope.WalletOperations = { + syncWalletsViaElectrumClient: (wallets) => { + const synced = f.scope.getJSONFromRealmObject(wallets[0]); + synced.specs.balances.unconfirmed = 1000; + synced.specs.hasNewUpdates = true; + return new Promise((resolve) => { + finishSync = () => resolve({ synchedWallets: [{ synchedWallet: synced, newUTXOs: [] }] }); + }); + }, + }; const originalWrite = f.scope.dbManager.updateObjectById; f.writes = []; f.scope.dbManager.updateObjectById = (...args) => { f.writes.push(args); return originalWrite(...args); }; - vm.runInNewContext(ts.transpileModule(functions('src/store/sagas/wallets.ts', [ - 'refreshWalletsWorker', - ]), { compilerOptions: { target: ts.ScriptTarget.ES2020 } }).outputText, f.scope); - f.refresh = (originAppId = f.app.id) => f.run('refreshWalletsWorker', { - originAppId, - payload: { wallets: [stored], options: { hardRefresh: true } }, - }); + vm.runInNewContext( + ts.transpileModule(functions('src/store/sagas/wallets.ts', ['refreshWalletsWorker']), { + compilerOptions: { target: ts.ScriptTarget.ES2020 }, + }).outputText, + f.scope + ); + f.refresh = (originAppId = f.app.id) => + f.run('refreshWalletsWorker', { + originAppId, + payload: { wallets: [stored], options: { hardRefresh: true } }, + }); f.waitForSync = async () => { for (let attempt = 0; attempt < 20 && !finishSync; attempt += 1) await new Promise(setImmediate); @@ -69,7 +80,11 @@ function refreshFixture(kind, archived = false) { return f; } -for (const [kind, archived] of [['Wallet', false], ['Vault', false], ['Vault', true]]) { +for (const [kind, archived] of [ + ['Wallet', false], + ['Vault', false], + ['Vault', true], +]) { const description = archived ? 'hidden archived Vault' : kind; test(`${description}: pending A refresh cannot overwrite B with the same ID`, async () => { const f = refreshFixture(kind, archived); @@ -77,12 +92,17 @@ for (const [kind, archived] of [['Wallet', false], ['Vault', false], ['Vault', t await f.waitForSync(); f.app.id = 'B'; f.state.storage.appId = 'B'; - const b = { ...wallet('same-disposable-id', enums.NetworkType.TESTNET), + const b = { + ...wallet('same-disposable-id', enums.NetworkType.TESTNET), entityKind: kind === 'Vault' ? enums.EntityKind.VAULT : enums.EntityKind.WALLET, - specs: { balances: { confirmed: 42, unconfirmed: 0 } } }; + specs: { balances: { confirmed: 42, unconfirmed: 0 } }, + }; f.collections[kind] = [b]; const actionCount = f.actions.length; - let bUi = walletState.default(undefined, walletState.setSyncing({ wallets: [b], isSyncing: true })); + let bUi = walletState.default( + undefined, + walletState.setSyncing({ wallets: [b], isSyncing: true }) + ); f.complete(); assert.equal(await refreshing, false); for (const action of f.actions.slice(actionCount)) bUi = walletState.default(bUi, action); @@ -109,7 +129,9 @@ for (const [kind, archived] of [['Wallet', false], ['Vault', false], ['Vault', t f.app.id = 'B'; f.state.storage.appId = 'B'; let synced = false; - f.scope.WalletOperations.syncWalletsViaElectrumClient = () => { synced = true; }; + f.scope.WalletOperations.syncWalletsViaElectrumClient = () => { + synced = true; + }; assert.equal(await f.refresh(originAppId), false); assert.equal(synced, false); assert.deepEqual(f.writes, []); @@ -123,9 +145,55 @@ test('refresh marks recoverable address counters pending after Realm persistence f.collections.Wallet[0].specs.nextFreeChangeAddressIndex = 0; f.collections.Wallet[0].specs.totalExternalAddresses = 2; const mutations = []; - const invalidatedRevisions = []; - f.scope.markBackupMutation = (appId) => mutations.push(appId); - f.scope.clearVerifiedRemoteRevision = (appId) => invalidatedRevisions.push(appId); + // The direct Realm write is represented by the unit DB adapter. Exercise the + // production snapshot/generation check rather than asserting the removed + // clearVerifiedRemoteRevision call; native commit hooks have separate proof. + const database = { + isInTransaction: false, + objects: (schema) => (schema === 'KeeperApp' ? [f.app] : f.collections[schema]), + }; + const utilities = { + getKeyUID: f.scope.getKeyUID, + sanitizeSeedKeyForBackup: f.scope.sanitizeSeedKeyForBackup, + sanitizeVaultSignersForSeedKeyBackup: f.scope.sanitizeVaultSignersForSeedKeyBackup, + }; + const schema = { RealmSchema: f.scope.RealmSchema }; + const localSnapshot = loadModule('src/services/backup/localSnapshot.ts', { + 'src/storage/realm/enum': schema, + 'src/utils/service-utilities/encryption': encryption, + 'src/utils/utilities': utilities, + './image': image, + }); + const transport = loadModule('src/services/backup/transport.ts', { + '../rest/RestClient': { + post: () => { + throw new Error('No network expected'); + }, + }, + }); + transport.observeBackupAccount(originAppId); + transport.setBackupUploadGuard((id) => id === f.app.id); + const automatic = loadModule('src/services/backup/automatic.ts', { + 'src/storage/realm/realm': { getDatabase: () => database }, + 'src/storage/realm/enum': schema, + 'src/utils/service-utilities/config': { RELAY: 'fixture/' }, + 'src/utils/service-utilities/encryption': encryption, + 'src/utils/utilities': utilities, + './image': image, + './localSnapshot': localSnapshot, + './transport': transport, + }); + transport.recordVerifiedRemoteRevision( + originAppId, + 'a'.repeat(64), + transport.currentBackupAccountSession() + ); + const baseline = transport.captureBackupWriteIntent(originAppId); + assert.ok(automatic.isBackupVerificationCurrent(originAppId)); + f.scope.markBackupMutation = (appId) => { + mutations.push(appId); + transport.markBackupMutation(appId); + }; f.scope.invalidateBackupRepair = (appId) => ({ type: 'invalidateBackupRepair', payload: appId }); f.scope.setPendingAllBackup = (payload) => ({ type: 'setPendingAllBackup', payload }); f.scope.WalletOperations.syncWalletsViaElectrumClient = (wallets) => { @@ -140,9 +208,17 @@ test('refresh marks recoverable address counters pending after Realm persistence assert.equal(await f.refresh(), true); assert.equal(f.collections.Wallet[0].specs.totalExternalAddresses, 3); assert.deepEqual(mutations, [originAppId]); - assert.deepEqual(invalidatedRevisions, [originAppId]); - assert.ok(f.actions.some((action) => action.type === 'invalidateBackupRepair' && - action.payload === originAppId)); + assert.equal( + automatic.isBackupVerificationCurrent(originAppId), + false, + 'the old verified revision cannot prove newly committed address counters' + ); + assert.ok(automatic.currentCommittedBackupGeneration(originAppId) > baseline.localGeneration); + assert.ok( + f.actions.some( + (action) => action.type === 'invalidateBackupRepair' && action.payload === originAppId + ) + ); assert.equal(f.state.bhr.pendingAllBackupByAppId[originAppId], true); }); @@ -153,8 +229,12 @@ test('refresh paused at Electrum cannot overwrite newer A specs after A to B to f.scope.currentBackupAccountSession = () => session; const refreshing = f.refresh(originAppId); await f.waitForSync(); - f.app.id = 'B'; f.state.storage.appId = 'B'; session++; - f.app.id = originAppId; f.state.storage.appId = originAppId; session++; + f.app.id = 'B'; + f.state.storage.appId = 'B'; + session++; + f.app.id = originAppId; + f.state.storage.appId = originAppId; + session++; f.collections.Wallet[0].specs.balances.confirmed = 777; f.complete(); assert.equal(await refreshing, false); @@ -170,8 +250,12 @@ test('new A login refresh runs after stale A lease from A to B to A', async () = const staleRefresh = f.refresh(originAppId); await f.waitForSync(); - f.app.id = 'B'; f.state.storage.appId = 'B'; session++; - f.app.id = originAppId; f.state.storage.appId = originAppId; session++; + f.app.id = 'B'; + f.state.storage.appId = 'B'; + session++; + f.app.id = originAppId; + f.state.storage.appId = originAppId; + session++; f.collections.Wallet[0].specs.balances.confirmed = 777; let newSessionSyncs = 0; From 37bf30fb7b5a847ac7eac99e11f725515a0c9b30 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Tue, 13 Oct 2026 03:48:38 +0000 Subject: [PATCH 160/254] test(recovery): align producer harness with local commit receipts --- tests/recovery/keyRecovery.test.cjs | 71 +++++-- tests/recovery/sagaHarness.cjs | 294 ++++++++++++++++++++++++++-- 2 files changed, 333 insertions(+), 32 deletions(-) diff --git a/tests/recovery/keyRecovery.test.cjs b/tests/recovery/keyRecovery.test.cjs index 905ada0ddd..198edffd46 100644 --- a/tests/recovery/keyRecovery.test.cjs +++ b/tests/recovery/keyRecovery.test.cjs @@ -25,6 +25,7 @@ for (const network of [NetworkType.MAINNET, NetworkType.TESTNET]) { later.signerName = 'Travel device'; later.hidden = true; await f.run('addSigningDeviceWorker', { payload: { signers: [first] } }); + f.mockVerifiedBackupBaseline(); await f.run('addSigningDeviceWorker', { payload: { signers: [later] } }); assert.equal(f.calls.length, 2); assert.equal(f.collections.Signer.length, 2); @@ -33,7 +34,8 @@ for (const network of [NetworkType.MAINNET, NetworkType.TESTNET]) { assert.ok(f.remote.signers[id]); assert.ok(!f.remote.signers[id].includes('Travel device')); assert.deepEqual(await restoreKeys(f), expected); - assert.equal(f.state.bhr.pendingAllBackup, pendingAllBackup); + assert.equal(f.state.bhr.pendingAllBackup, true, + 'an acknowledged unit adapter write is not complete server readback proof'); }); } } @@ -41,6 +43,7 @@ for (const network of [NetworkType.MAINNET, NetworkType.TESTNET]) { test('same fingerprint on different networks and accounts retains three distinct keys', async () => { const f = fixture(); for (const [network, account] of [[NetworkType.MAINNET, 0], [NetworkType.TESTNET, 0], [NetworkType.MAINNET, 1]]) { + f.mockVerifiedBackupBaseline(); f.state.settings.bitcoinNetworkType = network; await f.run('addSigningDeviceWorker', { payload: { signers: [signer('AABBCCDD', network, SignerType.COLDCARD, account)] }, @@ -55,9 +58,16 @@ test('mixed wallet, signer and node update backs up every requested record', asy const f = fixture(); const key = signer('AABBCCDD', NetworkType.MAINNET); key.id = f.scope.getKeyUID(key); + const storedWallet = wallet('wallet', NetworkType.MAINNET); f.collections.NodeConnect.push({ id: 'node-fixture', host: 'fixture.invalid', isConnected: true }); + const operation = f.scope.beginBackupLocalOperation(f.app.id, + { appId: f.app.id, accountSession: f.scope.currentBackupAccountSession() }); + const committedOperation = f.scope.commitBackupLocalOperation(operation, (database) => { + database.create('Wallet', storedWallet); + database.create('Signer', key); + }); await f.run('updateAppImageWorker', { - payload: { wallets: [wallet('wallet', NetworkType.MAINNET)], signers: [key], updateNodes: true }, + payload: { wallets: [storedWallet], signers: [key], updateNodes: true }, committedOperation, }); assert.equal(f.calls.length, 1); assert.ok(f.remote.wallets.wallet); @@ -71,10 +81,43 @@ test('empty wallet array does not suppress signer migration', async () => { const f = fixture(); const key = signer('AABBCCDD', NetworkType.MAINNET); key.id = f.scope.getKeyUID(key); - await f.run('updateAppImageWorker', { payload: { wallets: [], signers: [key] } }); + const operation = f.scope.beginBackupLocalOperation(f.app.id, + { appId: f.app.id, accountSession: f.scope.currentBackupAccountSession() }); + const committedOperation = f.scope.commitBackupLocalOperation(operation, + (database) => database.create('Signer', key)); + await f.run('updateAppImageWorker', { payload: { wallets: [], signers: [key] }, committedOperation }); assert.ok(f.remote.signers[key.id]); }); +test('a detached BHR action cannot upload uncommitted wallet or signer records', async () => { + const f = fixture(); + const key = signer('AABBCCDD', NetworkType.MAINNET); + key.id = f.scope.getKeyUID(key); + const result = await f.run('updateAppImageWorker', { + payload: { wallets: [wallet('uncommitted', NetworkType.MAINNET)], signers: [key] }, + }); + assert.equal(result.updated, false); + assert.equal(result.backupPending, true); + assert.deepEqual(f.calls, []); + assert.deepEqual(f.collections.Wallet, []); + assert.deepEqual(f.collections.Signer, []); +}); + +test('an upload acknowledgement does not authorize the next automatic key mutation', async () => { + const f = fixture(); + await f.run('addSigningDeviceWorker', { + payload: { signers: [signer('AABBCCDD', NetworkType.MAINNET)] }, + }); + assert.equal(f.state.bhr.backupRepairCompletedByAppId[f.app.id], false); + await f.run('addSigningDeviceWorker', { + payload: { signers: [signer('11223344', NetworkType.MAINNET)] }, + }); + assert.equal(f.collections.Signer.length, 2, 'local work remains committed'); + assert.equal(f.calls.length, 1, 'another write requires a fresh verified baseline'); + assert.equal(f.state.bhr.pendingAllBackupByAppId[f.app.id], true); + assert.ok(!f.remote.signers['112233440M']); +}); + test('key name, description, archive and health metadata updates survive clean restore', async () => { const f = fixture(); await f.run('addSigningDeviceWorker', { payload: { signers: [signer('AABBCCDD', NetworkType.MAINNET)] } }); @@ -83,7 +126,10 @@ test('key name, description, archive and health metadata updates survive clean r signerName: 'Renamed device', signerDescription: 'Recorded on physical device', archived: true, hidden: true, healthCheckDetails: [{ type: 'address-verification', actionDate: '2026-09-29T00:00:00.000Z' }], - })) await f.run('updateSignerDetailsWorker', { payload: { signer: key, key: field, value } }); + })) { + f.mockVerifiedBackupBaseline(); + await f.run('updateSignerDetailsWorker', { payload: { signer: key, key: field, value } }); + } const expected = plain(f.collections.Signer); assert.deepEqual(await restoreKeys(f), expected); }); @@ -97,6 +143,7 @@ test('seed-key private material is omitted while software and Recovery Key mater const software = signer('33333333', NetworkType.MAINNET, SignerType.MY_KEEPER); software.extraData = { instanceNumber: 1 }; for (const record of [ordinary, recovery, software]) { + f.mockVerifiedBackupBaseline(); record.signerXpubs[XpubTypes.P2WPKH][0].xpriv = `${record.masterFingerprint}-disposable-private`; await f.run('addSigningDeviceWorker', { payload: { signers: [record] } }); } @@ -197,14 +244,15 @@ for (const problem of ['missing', 'ambiguous', 'different-public-key', 'differen test('fingerprint migration applies the same seed-secret exclusion as normal backups', async () => { const f = fixture(); const old = signer('AAAAAAAA', NetworkType.MAINNET, SignerType.SEED_WORDS); + old.id = f.scope.getKeyUID(old); const updated = plain(old); updated.masterFingerprint = 'BBBBBBBB'; updated.signerXpubs[XpubTypes.P2WPKH][0].xpriv = 'disposable-private'; f.collections.Signer.push(old); - f.scope.dbManager.getObjectByQuery = () => []; + f.remote.signers[old.id] = encryption.encrypt(keyFor(f), JSON.stringify(old)); let migrated; f.scope.Relay.migrateXfp = async (_appId, records) => { migrated = records[0]; return true; }; - assert.equal(await f.run('mergeSimilarKeysWorker', { payload: { signer: updated } }), true); + assert.equal(await f.run('addSigningDeviceWorker', { payload: { signers: [updated] } }), true); const restored = JSON.parse(encryption.decrypt(keyFor(f), migrated.newSignerDetails)); assert.equal(restored.signerXpubs[XpubTypes.P2WPKH][0].xpriv, null); assert.equal(updated.signerXpubs[XpubTypes.P2WPKH][0].xpriv, 'disposable-private'); @@ -218,13 +266,13 @@ function registrationFixture(options = {}) { const key = { masterFingerprint: device.masterFingerprint, ...device.signerXpubs[XpubTypes.P2WPKH][0], registeredVaults: [] }; f.collections.Signer.push(device); + f.collections.VaultSigner.push(key); f.collections.Vault.push(...['vault-a', 'vault-b'].map((id) => ({ id, networkType: NetworkType.MAINNET, signers: [key], scheme: { m: 1, n: 1 } }))); - f.scope.dbManager.getObjectByPrimaryId = () => ({ toJSON: () => plain(key) }); + const update = f.scope.dbManager.updateObjectByPrimaryId; f.scope.dbManager.updateObjectByPrimaryId = (_schema, _field, _id, patch) => { if (options.writeFails) return false; - Object.assign(key, patch); - return true; + return update(_schema, _field, _id, patch); }; const uploaded = []; f.scope.Relay.updateVaultImage = async (payload) => { @@ -261,7 +309,8 @@ test('disabled server backup preserves local key registration without uploading' await f.run('updateKeyDetailsWorker', { payload: { signer: key, key: 'registered', value: { vaultId: 'vault-a', registered: true } } }); assert.equal(key.registeredVaults[0].registered, true); assert.equal(uploaded.length, 0); - assert.equal(f.state.bhr.pendingAllBackup, false); + assert.equal(f.state.bhr.pendingAllBackupByAppId[f.app.id], true, + 'local recovery changed, while disabled consent still prevents every upload'); }); test('failed local key registration write never publishes an unpersisted success', async () => { @@ -269,7 +318,7 @@ test('failed local key registration write never publishes an unpersisted success assert.equal(await f.run('updateKeyDetailsWorker', { payload: { signer: key, key: 'registered', value: { vaultId: 'vault-a', registered: true } } }), false); assert.equal(uploaded.length, 0); assert.equal(key.registeredVaults.length, 0); - assert.ok(f.actions.some((action) => action.type === 'relaySignersUpdateFail' && action.payload === 'Key registration could not be saved')); + assert.ok(f.actions.some((action) => action.type === 'relaySignersUpdateFail' && action.payload === 'Key changes could not be saved')); }); test('remembering an imported seed for signing remains local and does not trigger a vault upload', async () => { diff --git a/tests/recovery/sagaHarness.cjs b/tests/recovery/sagaHarness.cjs index cd6a39e0cb..639f5b449f 100644 --- a/tests/recovery/sagaHarness.cjs +++ b/tests/recovery/sagaHarness.cjs @@ -111,6 +111,7 @@ function fixture(options = {}) { USDTWallet: [], Signer: [], Vault: [], + VaultSigner: [], Tags: [], NodeConnect: [], UAI: [], @@ -123,7 +124,6 @@ function fixture(options = {}) { const scope = { ...effects, markBackupMutation: () => {}, - currentBackupAccountSession: () => 0, store: { getState: () => state }, isAutomaticCloudBackupEnabled: (backup, id) => !!backup.automaticCloudBackupByAppId?.[id], ...encryption, @@ -147,7 +147,7 @@ function fixture(options = {}) { }, Realm: { UpdateMode: { Modified: 'modified' } }, captureError: (error) => { throw error; }, - getJSONFromRealmObject: (object) => object, + getJSONFromRealmObject: (object) => object?.toJSON ? object.toJSON() : JSON.parse(JSON.stringify(object)), RealmSchema: new Proxy({}, { get: (_, name) => name }), dbManager: { getObjectByIndex: (schema, _index, all) => @@ -155,13 +155,14 @@ function fixture(options = {}) { getObjectById: (schema, id) => collections[schema]?.find((row) => row.id === id), getObjectByPrimaryId: (schema, key, value) => collections[schema]?.find((row) => String(row[key]) === String(value)), - getCollection: (schema) => collections[schema] || [], + getCollection: (schema) => JSON.parse(JSON.stringify(collections[schema] || [])), getObjectByField: (schema) => collections[schema] || [], createObject: (schema, value) => { if (schema === 'KeeperApp') Object.assign(app, value); else { const rows = (collections[schema] ||= []); - const index = rows.findIndex((row) => row.id && row.id === value.id); + const primary = schema === 'VaultSigner' ? 'xpub' : 'id'; + const index = rows.findIndex((row) => row[primary] && row[primary] === value[primary]); if (index < 0) rows.push(value); else rows[index] = value; } @@ -180,13 +181,23 @@ function fixture(options = {}) { return true; }, updateObjectById: (schema, id, patch) => { - const row = collections[schema].find((record) => String(record.id) === String(id)); + const row = schema === 'KeeperApp' ? scope.dbManager.getObjectByIndex(schema) + : collections[schema]?.find((record) => String(record.id) === String(id)); if (!row) return false; Object.assign(row, patch); return true; }, updateObjectByQuery: (schema, predicate, patch) => { - collections[schema].filter(predicate).forEach((row) => Object.assign(row, patch)); + const row = collections[schema]?.find(predicate); + if (!row) return false; + Object.assign(row, patch); + return true; + }, + updateObjectByPrimaryId: (schema, key, value, patch) => { + const row = collections[schema]?.find((record) => String(record[key]) === String(value)); + if (!row) return false; + Object.assign(row, patch); + return true; }, }, Relay: { @@ -244,6 +255,201 @@ function fixture(options = {}) { return options.syncFails !== true; }, }; + + // Producer-boundary UNIT adapter only. It exercises source saga ordering, + // committed-record builders and real AES with mutable in-memory DB/Relay + // controls. It is not the branded generation/CAS/readback implementation or + // native/HTTP proof; automaticWriteRealmRelay.integration owns that evidence. + let selectedAppId = state.storage.appId; + let accountSession = 0; + Object.defineProperty(state.storage, 'appId', { + enumerable: true, + get: () => selectedAppId, + set: (value) => { + if (value !== selectedAppId) accountSession += 1; + selectedAppId = value; + }, + }); + const clone = (value) => JSON.parse(JSON.stringify(value)); + const receipts = new WeakMap(); + const results = new WeakMap(); + const consentEpochs = new Map(); + const consentCompletions = new WeakMap(); + scope.backupConsentEpoch = (id) => consentEpochs.get(id) || 0; + scope.protectBackupConsentCompletion = (action, id, epoch) => { + consentCompletions.set(action, { appId: id, epoch }); + return action; + }; + const rawReferences = new WeakMap(); + const fingerprint = () => JSON.stringify({ + app: scope.dbManager.getObjectByIndex('KeeperApp'), collections, + }); + const primary = (schema) => schema === 'VaultSigner' ? 'xpub' : 'id'; + const managed = (schema, row) => { + if (!row) return undefined; + const proxy = new Proxy(row, { + get: (target, key) => key === 'toJSON' ? () => clone(target) : target[key], + set: (target, key, value) => { + const saved = schema === 'VaultSigner' + ? scope.dbManager.updateObjectByPrimaryId(schema, 'xpub', target.xpub, { [key]: value }) + : scope.dbManager.updateObjectById(schema, target.id, { [key]: value }); + if (saved !== true) throw new Error('Unit local write failed'); + return true; + }, + }); + rawReferences.set(proxy, { schema, row }); + return proxy; + }; + const localRealm = { + objects: (schema) => (schema === 'KeeperApp' ? [scope.dbManager.getObjectByIndex(schema)] : collections[schema] || []) + .filter(Boolean).map((row) => managed(schema, row)), + objectForPrimaryKey: (schema, value) => managed(schema, + schema === 'KeeperApp' ? scope.dbManager.getObjectByIndex(schema) + : (collections[schema] || []).find((row) => String(row[primary(schema)]) === String(value))), + create: (schema, value, mode) => { + if (scope.dbManager.createObject(schema, value, mode) !== true) throw new Error('Unit local creation failed'); + return localRealm.objectForPrimaryKey(schema, value[primary(schema)]); + }, + delete: (object) => { + const record = rawReferences.get(object); + if (!record) throw new Error('Unit deletion target missing'); + const key = primary(record.schema); + const deleted = record.schema === 'Signer' || record.schema === 'VaultSigner' + ? scope.dbManager.deleteObjectByPrimaryKey(record.schema, key, record.row[key]) + : scope.dbManager.deleteObjectById(record.schema, record.row[key]); + if (deleted !== true || (collections[record.schema] || []).includes(record.row)) + throw new Error('Unit deletion was not persisted'); + }, + }; + scope.realm = { getDatabase: () => localRealm }; + scope.currentBackupAccountSession = () => accountSession; + const sessionNow = () => scope.currentBackupAccountSession(); + const markUnitPending = (id) => { + // Production automatic.ts notifies reconciliation after a failed local + // commit. This adapter models that state boundary without claiming proof. + scope.markBackupMutation(id); + if (scope.invalidateBackupRepair && scope.store.dispatch) + scope.store.dispatch(scope.invalidateBackupRepair(id)); + if (scope.setPendingAllBackup && scope.store.dispatch) + scope.store.dispatch(scope.setPendingAllBackup({ appId: id, pending: true })); + }; + scope.beginBackupLocalOperation = (id, intent, session = sessionNow()) => { + const current = scope.dbManager.getObjectByIndex('KeeperApp'); + if (current?.id !== id || state.storage.appId !== id || session !== sessionNow()) + throw new Error('Unit account session changed'); + return { appId: id, accountSession: session, database: scope.realm.getDatabase(), + before: fingerprint(), origin: intent, consentEpoch: scope.backupConsentEpoch(id) }; + }; + scope.commitBackupLocalOperation = (operation, callback) => { + const active = scope.dbManager.getObjectByIndex('KeeperApp'); + if (active?.id !== operation.appId || state.storage.appId !== operation.appId || + operation.accountSession !== sessionNow() || operation.database !== scope.realm.getDatabase() || + operation.consentEpoch !== scope.backupConsentEpoch(operation.appId)) + return { committed: false }; + const before = new Map(Object.entries(collections).map(([schema, rows]) => + [schema, rows.map((row) => ({ row, value: clone(row) }))])); + const appBefore = clone(active); + try { + const value = callback(localRealm); + if (value?.then) throw new Error('Unit local commit must be synchronous'); + const receipt = { committed: true, + intent: operation.origin && operation.origin.appId === operation.appId && + operation.origin.accountSession === sessionNow() && + (operation.origin.consentEpoch === undefined || + operation.origin.consentEpoch === scope.backupConsentEpoch(operation.appId)) + ? operation.origin : undefined }; + receipts.set(receipt, { appId: operation.appId, session: sessionNow(), + fingerprint: fingerprint(), consentEpoch: operation.consentEpoch, deletion: false }); + markUnitPending(operation.appId); + return receipt; + } catch { + for (const key of Object.keys(active)) delete active[key]; + Object.assign(active, appBefore); + for (const [schema, saved] of before) { + for (const { row, value } of saved) { + for (const key of Object.keys(row)) delete row[key]; + Object.assign(row, value); + } + collections[schema] = saved.map(({ row }) => row); + } + markUnitPending(operation.appId); + return { committed: false }; + } + }; + scope.beginBackupDeleteOperation = async (id, session = sessionNow()) => { + const operation = scope.beginBackupLocalOperation(id, undefined, session); + const receipt = { committed: true, intent: { appId: id, accountSession: session } }; + receipts.set(receipt, { appId: id, session, fingerprint: operation.before, + consentEpoch: operation.consentEpoch, deletion: true }); + return receipt; + }; + scope.isAutomaticBackupResultCurrent = (result, id) => { + const evidence = results.get(result); + return !!evidence && evidence.appId === id && id === state.storage.appId && + evidence.session === sessionNow() && evidence.consentEpoch === scope.backupConsentEpoch(id) && + evidence.fingerprint === fingerprint(); + }; + scope.isBackupVerificationCurrent = (id) => id === state.storage.appId && !!state.bhr.backupRepairCompletedByAppId[id]; + scope.enqueueAutomaticBackupOperation = async (receipt, mutations) => { + const evidence = receipt && receipts.get(receipt); + const appId = evidence?.appId || receipt?.intent?.appId || mutations[0]?.body?.appId; + if (appId) markUnitPending(appId); + const finish = (updated, confirmed = false) => { + const result = { updated, localCommitted: receipt?.committed === true, + backupConfirmed: confirmed, backupPending: !confirmed }; + if (evidence) results.set(result, evidence); + return result; + }; + if (!evidence || !receipt.intent || evidence.appId !== state.storage.appId || + evidence.session !== sessionNow() || evidence.consentEpoch !== scope.backupConsentEpoch(evidence.appId) || + evidence.fingerprint !== fingerprint()) return finish(false); + if (options.online === false) return finish(false); + try { + for (const mutation of mutations) { + const { body, operation } = mutation; + let reply; + if (operation === 'updateAppImage') reply = await scope.Relay.updateAppImage(body); + else if (operation === 'updateVaultImage') reply = await scope.Relay.updateVaultImage(body); + else if (operation === 'deleteAppImageEntity') reply = await scope.Relay.deleteAppImageEntity(body); + else if (operation === 'deleteVaults') reply = await scope.Relay.deleteVaults(body); + else if (operation === 'modifyLabels') reply = await scope.Relay.modifyLabels(body.appId, body.addLabels, body.deleteLabels); + else if (operation === 'migrateXfps') reply = await scope.Relay.migrateXfp(body.appId, body.signerChanges); + else if (operation === 'deleteBackup') reply = await scope.Relay.deleteBackup(body); + else throw new Error('Unsupported unit mutation'); + if (reply !== true && reply?.updated !== true) return finish(false); + if (evidence.appId !== state.storage.appId || evidence.session !== sessionNow() || + evidence.consentEpoch !== scope.backupConsentEpoch(evidence.appId) || + evidence.fingerprint !== fingerprint()) return finish(false); + } + // The disposable Relay adapter provides only acknowledgements. A native + // readback proof is not invented here. Explicit unit opt-out tests may + // mock that separate confirmation contract via this named option. + const confirmed = evidence.deletion && options.confirmBackupDeletion === true; + if (confirmed) { + // Explicitly mocked confirmation models the coordinator's protected + // completion. It preserves the choice epoch and emits opt-out once. + scope.store.dispatch(scope.protectBackupConsentCompletion( + scope.setAutomaticCloudBackup({ appId: evidence.appId, enabled: false }), + evidence.appId, evidence.consentEpoch)); + } + return finish(true, confirmed); + } catch { return finish(false); } + }; + scope.Relay.updateVaultImage = async (body) => { + calls.push(['vault', body]); + if (options.incrementalError) throw new Error('offline'); + return { updated: true }; + }; + scope.Relay.deleteVaults = async (body) => { calls.push(['delete-vault', body]); return { updated: true }; }; + scope.Relay.migrateXfp = async (id, changes) => { + calls.push(['migrate', changes]); + for (const change of changes) { + delete remote.signers[change.oldSignerId]; + remote.signers[change.newSignerId] = change.newSignerDetails; + } + return true; + }; + scope.Relay.deleteBackup = async () => { calls.push(['delete-backup']); return { updated: true }; }; const names = [ 'setBackupRepairCompleted', 'invalidateBackupRepair', @@ -294,6 +500,7 @@ function fixture(options = {}) { ]) + '\n' + functions('src/store/sagas/bhr.ts', [ 'currentBackupAppId', + 'buildAutomaticAppMutation', 'buildAutomaticVaultMutation', 'queueCommittedBackupWorker', 'flagUncertainBackupOutcome', 'deleteAppImageEntityWorker', 'deleteAppImageEntityRequestWorker', @@ -311,7 +518,9 @@ function fixture(options = {}) { '\n' + functions('src/store/sagas/wallets.ts', [ 'currentWalletAccountId', 'assertWalletAccount', - 'addNewWalletsWorker', 'addSigningDeviceWorker', 'mergeSimilarKeysWorker', + 'rewriteSignerFingerprintReferences', 'planSignerIdentityMerges', 'applySignerIdentityMerges', + 'applySignerXprivUpdates', 'buildSignerMigrationMutation', + 'addNewWalletsWorker', 'addSigningDeviceWorker', 'deleteSigningDeviceWorker', 'deleteVaultWorker', 'deleteVaultRequestWorker', 'generateNewExternalAddressWorker', 'updateSignerPolicyWorker', @@ -322,26 +531,69 @@ function fixture(options = {}) { ts.transpileModule(code, { compilerOptions: { target: ts.ScriptTarget.ES2020 } }).outputText, scope ); - const run = (name, ...args) => - runSaga( + const dispatch = (action) => { + if (action.type === 'setAutomaticCloudBackup') { + const id = action.payload.appId; + const completion = consentCompletions.get(action); + if (completion) { + consentCompletions.delete(action); + if (completion.appId !== id || completion.epoch !== scope.backupConsentEpoch(id)) return; + } else consentEpochs.set(id, scope.backupConsentEpoch(id) + 1); + } + actions.push(action); + if (action.type === 'setBackupRepairCompleted') + state.bhr.backupRepairCompletedByAppId[action.payload] = true; + if (action.type === 'invalidateBackupRepair') + state.bhr.backupRepairCompletedByAppId[action.payload] = false; + if (action.type === 'setPendingAllBackup') { + const { appId: pendingAppId, pending } = action.payload; + state.bhr.pendingAllBackupByAppId[pendingAppId] = pending; + state.bhr.pendingAllBackup = pending; + } + if (action.type === 'setAutomaticCloudBackup') + state.bhr.automaticCloudBackupByAppId[action.payload.appId] = action.payload.enabled; + state.account = account.default(state.account, action); + }; + scope.store.dispatch = dispatch; + const mutationActions = new Set([ + 'addNewWalletsWorker', 'addSigningDeviceWorker', 'deleteSigningDeviceWorker', + 'deleteAppImageEntityWorker', 'deleteAppImageEntityRequestWorker', + 'deleteVaultWorker', 'deleteVaultRequestWorker', 'updateSignerPolicyWorker', + 'updateVaultSignerXprivWorker', 'updateSignerDetailsWorker', 'updateKeyDetailsWorker', + ]); + const run = (name, ...args) => { + if (mutationActions.has(name) && args[0] && typeof args[0] === 'object') { + const action = args[0]; + args[0] = { originAppId: state.storage.appId, originBackupAccountSession: sessionNow(), ...action, + // A top-level source mutation receives the unit middleware intent. + // Direct BHR payloads never receive a fabricated committed receipt. + ...(Object.hasOwn(action, 'originBackupWriteIntent') ? {} : { + originBackupWriteIntent: state.bhr.backupRepairCompletedByAppId[state.storage.appId] + ? { appId: state.storage.appId, accountSession: sessionNow(), + consentEpoch: scope.backupConsentEpoch(state.storage.appId) } + : undefined, + }), + }; + } + return runSaga( { getState: () => state, - dispatch(action) { - actions.push(action); - if (action.type === 'setBackupRepairCompleted') - state.bhr.backupRepairCompletedByAppId[action.payload] = true; - if (action.type === 'setPendingAllBackup') { - const { appId: pendingAppId, pending } = action.payload; - state.bhr.pendingAllBackupByAppId[pendingAppId] = pending; - state.bhr.pendingAllBackup = pending; - } - state.account = account.default(state.account, action); - }, + dispatch, }, scope[name], ...args ).toPromise(); - return { scope, app, collections, remote, calls, actions, state, run }; + }; + const mockVerifiedBackupBaseline = () => { + // Unit-only stand-in for a successful fresh read-only inspection. Never + // called automatically after an ACK; native CAS/readback proof is separate. + state.bhr.backupRepairCompletedByAppId[state.storage.appId] = true; + state.bhr.backupRepairStateByAppId ||= {}; + state.bhr.backupRepairStateByAppId[state.storage.appId] = 'verified'; + state.bhr.pendingAllBackupByAppId[state.storage.appId] = false; + state.bhr.pendingAllBackup = false; + }; + return { scope, app, collections, remote, calls, actions, state, run, mockVerifiedBackupBaseline }; } module.exports = { fixture, enums, encryption, account, wallet, signer, loadModule, functions }; From f54e9dc971966f0ede478f7b16ee3b369afba7b8 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Tue, 13 Oct 2026 03:50:15 +0000 Subject: [PATCH 161/254] Bind automatic backup writes to committed Realm operations and original session proof --- src/services/backup/automatic.ts | 750 +++++++++++++++++++++++++++ src/services/backup/intent.ts | 4 + src/services/backup/localSnapshot.ts | 91 ++++ src/services/backup/repair.ts | 61 ++- src/services/backup/transport.ts | 116 ++++- src/store/store.ts | 130 +++-- tests/recovery/helpers.cjs | 221 ++++++-- tests/recovery/repair.test.cjs | 3 +- 8 files changed, 1248 insertions(+), 128 deletions(-) create mode 100644 src/services/backup/automatic.ts create mode 100644 src/services/backup/localSnapshot.ts diff --git a/src/services/backup/automatic.ts b/src/services/backup/automatic.ts new file mode 100644 index 0000000000..790a33a1cc --- /dev/null +++ b/src/services/backup/automatic.ts @@ -0,0 +1,750 @@ +/* eslint-disable no-await-in-loop -- CAS fanout must use each acknowledged revision in order. */ +import realmDatabase from 'src/storage/realm/realm'; +import { RealmSchema } from 'src/storage/realm/enum'; +import config from 'src/utils/service-utilities/config'; +import { generateEncryptionKey, hash256 } from 'src/utils/service-utilities/encryption'; +import { getKeyUID } from 'src/utils/utilities'; +import type { BackupWriteIntent } from './intent'; +import { + BackupImage, + canonical, + recoveryContent, + decodeImage, + decryptRecord, + compareImages, +} from './image'; +import { captureLocalRecovery, backupMetadataMatches } from './localSnapshot'; +import { + BACKUP_DEADLINE_MS, + backupBaselineEpoch, + backupConsentEpoch, + boundedBackupPost, + canAccessBackupAccount, + clearVerifiedRemoteRevision, + currentBackupAccountSession, + markBackupQueuedMutation, + notifyBackupReconciliation, + recordVerifiedRemoteRevision, + setAutomaticBackupPostHandler, + setBackupLocalEvidenceProvider, + captureBackupWriteIntent, + isCapturedBackupWriteIntent, + withBackupSession, +} from './transport'; + +export type AutomaticBackupMutation = { + operation: + | 'updateAppImage' + | 'migrateXfps' + | 'deleteAppImageEntity' + | 'deleteVaults' + | 'updateVaultImage' + | 'modifyLabels' + | 'deleteBackup'; + body: any; +}; +type Capture = ReturnType; +export type BackupLocalOperation = Readonly<{ + appId: string; + accountSession: number; + consentEpoch: number; + database: any; + generation: number; + before: Capture; + origin?: BackupWriteIntent; + fresh: boolean; +}>; +export type BackupLocalCommit = Readonly<{ committed: boolean; intent?: BackupWriteIntent }>; +type Proof = { + operation: BackupLocalOperation; + after: Capture; + generation: number; + deletion?: boolean; +}; +const proofs = new WeakMap(); +const operations = new WeakSet(); +const managedCommits = new WeakSet(); +const results = new WeakMap(); +const generations = new Map(); +let sequence = 0; +// Source proof is being exercised on disposable loopback fixtures. Hosted +// writes remain fenced until the matrix and the Relay fleet rollout are approved. +export const AUTOMATIC_BACKUP_SOURCE_READY = false; +export const AUTOMATIC_BACKUP_RELAY_ROLLOUT_READY = false; +let commitVerification: (appId: string, deleted: boolean, consentEpoch: number) => boolean = () => + false; +export const setAutomaticBackupVerificationCommit = (commit: typeof commitVerification) => { + commitVerification = commit; +}; +const clone = (value: any) => JSON.parse(JSON.stringify(value)); +function frozenCopy(value: T): T { + const copy = clone(value); + const freeze = (item: any) => { + if (item && typeof item === 'object') { + Object.values(item).forEach(freeze); + Object.freeze(item); + } + }; + freeze(copy); + return copy; +} +export function noteCommittedBackupRecovery(appId: string, fingerprint: string) { + const prior = generations.get(appId); + if (prior?.fingerprint === fingerprint) return; + generations.set(appId, { generation: (prior?.generation || 0) + 1, fingerprint }); +} +function local() { + const database = realmDatabase.getDatabase(); + if (database.isInTransaction) + throw new Error('Uncommitted Realm state cannot provide backup proof'); + const capture = captureLocalRecovery(database); + // Covers raw writes and another Realm handle as well as the commit observer. + noteCommittedBackupRecovery(capture.app.id, capture.fingerprint); + return { database, capture, generation: generations.get(capture.app.id)!.generation }; +} +export function currentCommittedBackupGeneration(appId: string) { + let state: ReturnType; + try { + state = local(); + } catch { + clearVerifiedRemoteRevision(appId); + notifyBackupReconciliation(appId); + throw new Error('Committed recovery account unavailable'); + } + if (state.capture.app.id !== appId) throw new Error('Backup account changed'); + return state.generation; +} +setBackupLocalEvidenceProvider(() => { + const state = local(); + return { + appId: state.capture.app.id, + generation: state.generation, + fingerprint: state.capture.fingerprint, + }; +}); +export function beginBackupLocalOperation( + appId: string, + origin?: BackupWriteIntent, + originSession = currentBackupAccountSession() +): BackupLocalOperation { + let state: ReturnType; + try { + state = local(); + } catch { + clearVerifiedRemoteRevision(appId); + notifyBackupReconciliation(appId); + throw new Error('Committed recovery account unavailable'); + } + if ( + state.capture.app.id !== appId || + originSession !== currentBackupAccountSession() || + !canAccessBackupAccount(appId, true) + ) { + clearVerifiedRemoteRevision(appId); + notifyBackupReconciliation(appId); + throw new Error('Backup account session changed'); + } + const operation = Object.freeze({ + appId, + accountSession: originSession, + consentEpoch: backupConsentEpoch(appId), + database: state.database, + generation: state.generation, + before: frozenCopy(state.capture), + origin: origin ? Object.freeze({ ...origin }) : undefined, + fresh: + !!origin && + isCapturedBackupWriteIntent(origin) && + origin.appId === appId && + origin.accountSession === originSession && + origin.consentEpoch === backupConsentEpoch(appId) && + origin.localGeneration === state.generation && + origin.localFingerprint === state.capture.fingerprint && + origin.baselineEpoch === backupBaselineEpoch(appId), + }); + operations.add(operation); + return operation; +} +export function completeBackupLocalOperation( + op: BackupLocalOperation, + succeeded: boolean +): BackupLocalCommit { + operations.delete(op); + if (!succeeded || !managedCommits.has(op)) { + clearVerifiedRemoteRevision(op.appId); + notifyBackupReconciliation(op.appId); + return Object.freeze({ committed: false }); + } + const state = local(); + if ( + state.database !== op.database || + state.capture.app.id !== op.appId || + op.accountSession !== currentBackupAccountSession() || + op.consentEpoch !== backupConsentEpoch(op.appId) + ) { + clearVerifiedRemoteRevision(op.appId); + notifyBackupReconciliation(op.appId); + return Object.freeze({ committed: false }); + } + const valid = + managedCommits.has(op) && + op.fresh && + op.origin?.baselineEpoch === backupBaselineEpoch(op.appId); + managedCommits.delete(op); + const intent: BackupWriteIntent | undefined = valid + ? Object.freeze({ + ...op.origin!, + localGeneration: state.generation, + localFingerprint: state.capture.fingerprint, + operationId: `${op.accountSession}:${++sequence}`, + }) + : undefined; + if (intent) + proofs.set(intent, { + operation: op, + after: frozenCopy(state.capture), + generation: state.generation, + }); + notifyBackupReconciliation(op.appId); + return Object.freeze({ committed: true, intent }); +} +export function commitBackupLocalOperation( + op: BackupLocalOperation, + write: (database: any) => void +): BackupLocalCommit { + try { + if (!operations.has(op)) throw new Error('Missing originating recovery operation'); + operations.delete(op); + if (Object.prototype.toString.call(write) === '[object AsyncFunction]') + throw new Error('Recovery commit must be synchronous'); + const before = local(); + if ( + before.database !== op.database || + before.capture.app.id !== op.appId || + op.accountSession !== currentBackupAccountSession() || + op.consentEpoch !== backupConsentEpoch(op.appId) || + !canAccessBackupAccount(op.appId, true) + ) { + clearVerifiedRemoteRevision(op.appId); + notifyBackupReconciliation(op.appId); + return Object.freeze({ committed: false }); + } + // An intervening local change can still be followed by a local-first save; + // it cannot grant upload authority to this older operation. + const current = + before.generation === op.generation && before.capture.fingerprint === op.before.fingerprint; + realmDatabase.writeTransaction(op.database, () => { + const result: any = write(op.database); + if (result?.then) throw new Error('Realm recovery commits must be synchronous'); + if ( + op.accountSession !== currentBackupAccountSession() || + op.consentEpoch !== backupConsentEpoch(op.appId) || + realmDatabase.getDatabase() !== op.database || + op.database.objects(RealmSchema.KeeperApp)[0]?.id !== op.appId || + !canAccessBackupAccount(op.appId, true) + ) + throw new Error('Recovery account changed before local commit'); + }); + const completed = current ? op : Object.freeze({ ...op, fresh: false }); + managedCommits.add(completed); + return completeBackupLocalOperation(completed, true); + } catch { + clearVerifiedRemoteRevision(op.appId); + notifyBackupReconciliation(op.appId); + return Object.freeze({ committed: false }); + } +} +function assertProof(intent: BackupWriteIntent, proof: Proof, explicit = false) { + const state = local(); + if ( + intent.accountSession !== currentBackupAccountSession() || + intent.consentEpoch !== backupConsentEpoch(intent.appId) || + state.database !== proof.operation.database || + state.capture.app.id !== intent.appId || + !canAccessBackupAccount(intent.appId, explicit) || + state.generation !== proof.generation || + state.capture.fingerprint !== proof.after.fingerprint || + backupBaselineEpoch(intent.appId) !== intent.baselineEpoch + ) + throw new Error('Committed backup source changed'); + return state; +} +export function isBackupVerificationCurrent(appId: string) { + try { + const intent = captureBackupWriteIntent(appId); + const state = local(); + return ( + !!intent && + intent.accountSession === currentBackupAccountSession() && + intent.localGeneration === state.generation && + intent.localFingerprint === state.capture.fingerprint + ); + } catch { + return false; + } +} +export function isAutomaticBackupResultCurrent(result: object, appId: string) { + const evidence = results.get(result); + if (!evidence || evidence.appId !== appId || evidence.session !== currentBackupAccountSession()) + return false; + try { + const state = local(); + return ( + state.database === evidence.proof.operation.database && + evidence.proof.operation.consentEpoch === backupConsentEpoch(appId) && + state.capture.app.id === appId && + state.generation === evidence.proof.generation && + state.capture.fingerprint === evidence.proof.after.fingerprint + ); + } catch { + return false; + } +} +async function snapshot(appId: string, deadline?: number) { + const response = ( + await boundedBackupPost(`${config.RELAY}getBackupSnapshot`, { appId }, deadline) + ).data; + if ( + !/^[a-f0-9]{64}$/.test(response?.revision) || + response.revisionedMutations !== true || + !Array.isArray(response.unavailableVaultIds) || + response.unavailableVaultIds.length || + !Array.isArray(response.unavailableLabelIds) || + response.unavailableLabelIds.length + ) + throw new Error('Incomplete revisioned backup'); + return response; +} +const same = (kind: keyof BackupImage, a: any, b: any) => + !!a && !!b && canonical(recoveryContent(kind, a)) === canonical(recoveryContent(kind, b)); +async function project( + mutations: AutomaticBackupMutation[], + proof: Proof, + response: any +): Promise { + const { appId } = proof.operation; + const key = generateEncryptionKey(proof.after.app.primarySeed); + const remote = await decodeImage(key, response, appId, () => {}, true); + const projected = clone(remote) as BackupImage; + const touched = new Set(); + const before = proof.operation.before.image; + const after = proof.after.image; + const rekeys = mutations + .filter((mutation) => mutation.operation === 'migrateXfps') + .flatMap((mutation) => mutation.body.signerChanges || []) + .map((change) => ({ + old: before.signers[change.oldSignerId]?.masterFingerprint, + next: after.signers[change.newSignerId]?.masterFingerprint, + })); + const beforeRekey = (value: any, field = ''): any => { + if (Array.isArray(value)) return value.map((entry) => beforeRekey(entry, field)); + if (value && typeof value === 'object') + return Object.fromEntries( + Object.entries(value).map(([name, item]) => [ + name, + beforeRekey( + item, + field === 'signerFingerprints' + ? 'masterFingerprint' + : field === 'keyInfoMap' + ? 'descriptor' + : name + ), + ]) + ); + if (typeof value !== 'string') return value; + return rekeys.reduce((text, pair) => { + if (!/^[a-f0-9]{8}$/i.test(pair.old || '') || !/^[a-f0-9]{8}$/i.test(pair.next || '')) + return text; + if (field === 'masterFingerprint' && text.toUpperCase() === pair.next.toUpperCase()) + return pair.old; + // Only the fingerprint component of a bracketed descriptor origin may + // change. Public/private keys, compiled policy, scripts and object keys + // remain byte-for-byte immutable even during a fingerprint correction. + if (/descriptor/i.test(field)) + return text.replace(new RegExp(`\\[${pair.next}(?=/|\\])`, 'gi'), `[${pair.old}`); + return text; + }, value); + }; + const put = async (kind: keyof BackupImage, id: string, encrypted: any) => { + const record = await decryptRecord(key, encrypted); + if (!same(kind, record, after[kind][id])) + throw new Error('Ciphertext does not match committed recovery'); + if (['wallets', 'signers', 'vaults'].includes(kind) && projected[kind][id]) { + const empty = () => + ({ wallets: {}, signers: {}, vaults: {}, nodes: {}, labels: {} } as BackupImage); + const incoming = empty(); + const existing = empty(); + incoming[kind][id] = kind === 'vaults' ? beforeRekey(record) : record; + existing[kind][id] = projected[kind][id]; + if ((await compareImages(incoming, existing)) === 'conflict') + throw new Error('Immutable recovery identity or counters changed'); + } + if ( + !touched.has(`${kind}:${id}`) && + projected[kind][id] && + !same(kind, projected[kind][id], before[kind][id]) + ) + throw new Error('Remote recovery changed'); + touched.add(`${kind}:${id}`); + projected[kind][id] = record; + return record; + }; + const remove = (kind: keyof BackupImage, id: string) => { + if ( + after[kind][id] || + !projected[kind][id] || + (!touched.has(`${kind}:${id}`) && !same(kind, projected[kind][id], before[kind][id])) + ) + throw new Error('Deletion does not match committed recovery'); + touched.add(`${kind}:${id}`); + delete projected[kind][id]; + }; + for (const { operation, body } of mutations) { + if ((body.appId || body.appID) !== appId || (body.appID && body.appID !== appId)) + throw new Error('Backup account changed'); + if (operation === 'updateAppImage') { + if ((body.walletObject && body.walletsObject) || (body.signersObjects && body.signersObject)) + throw new Error('Ambiguous backup fields'); + for (const [id, value] of Object.entries(body.walletsObject || body.walletObject || {})) + await put('wallets', id, value); + for (const [id, value] of Object.entries(body.signersObject || body.signersObjects || {})) + await put('signers', id, value); + for (const field of ['publicId', 'version', 'subscription', 'networkType']) + if ( + body[field] !== undefined && + canonical(field === 'subscription' ? JSON.parse(body[field]) : body[field]) !== + canonical(proof.after.app[field]) + ) + throw new Error('Backup metadata is stale'); + if (body.nodes?.length || body.replaceNodes) { + if (!Array.isArray(body.nodes) || !sameNodes(remote.nodes, before.nodes)) + throw new Error('Remote nodes changed'); + projected.nodes = {}; + for (const value of body.nodes) { + const record = await decryptRecord(key, value); + const id = record.id || canonical(recoveryContent('nodes', record)); + if (projected.nodes[id] || !same('nodes', record, after.nodes[id])) + throw new Error('Stale node ciphertext'); + projected.nodes[id] = record; + } + if (!sameNodes(projected.nodes, after.nodes)) throw new Error('Incomplete nodes'); + } + } else if (operation === 'migrateXfps') { + for (const change of body.signerChanges || []) { + const old = projected.signers[change.oldSignerId]; + const record = await put('signers', change.newSignerId, change.newSignerDetails); + if (!old || getKeyUID(record) !== change.newSignerId) + throw new Error('Invalid signer migration'); + const xpubs = (signer: any) => + canonical( + Object.values(signer.signerXpubs || {}) + .flat() + .map((item: any) => item.xpub) + .sort() + ); + if (xpubs(old) !== xpubs(record)) throw new Error('Signer migration changes keys'); + const prior = clone(old); + prior.masterFingerprint = record.masterFingerprint; + const oldImage: BackupImage = { + wallets: {}, + signers: { [change.newSignerId]: prior }, + vaults: {}, + nodes: {}, + labels: {}, + }; + const newImage: BackupImage = { ...oldImage, signers: { [change.newSignerId]: record } }; + if ((await compareImages(newImage, oldImage)) === 'conflict') + throw new Error('Signer migration changes recovery identity'); + if ( + Object.values(projected.vaults).some((vault: any) => + vault.signers?.some((signer: any) => getKeyUID(signer) === change.oldSignerId) + ) + ) + throw new Error('Signer migration would strand a vault'); + remove('signers', change.oldSignerId); + } + } else if (operation === 'deleteAppImageEntity') { + for (const id of body.walletIds || []) remove('wallets', id); + for (const id of body.signers || []) { + if ( + Object.values(projected.vaults).some((vault: any) => + vault.signers?.some((signer: any) => getKeyUID(signer) === id) + ) + ) + throw new Error('Signer deletion would strand a vault'); + remove('signers', id); + } + } else if (operation === 'deleteVaults') { + for (const id of body.vaults || []) remove('vaults', id); + } else if (operation === 'updateVaultImage') { + const vault = await put('vaults', body.vaultId, body.vault); + if ( + !!body.isArchived !== !!vault.archived || + (body.scheme && canonical(body.scheme) !== canonical(vault.scheme)) || + canonical(body.signersData) !== + canonical( + vault.signers.map((signer: any) => ({ + signerId: getKeyUID(signer), + xfpHash: hash256(signer.masterFingerprint), + })) + ) + ) + throw new Error('Vault references differ from committed recovery'); + if ( + body.archiveVaultId && + (!after.vaults[body.archiveVaultId]?.archived || + !same('vaults', projected.vaults[body.archiveVaultId], after.vaults[body.archiveVaultId])) + ) + throw new Error('Archive ciphertext must be committed and updated before replacement'); + } else if (operation === 'modifyLabels') { + const hash = (id: string) => hash256(hash256(key + id)); + const added = new Set(); + for (const value of body.addLabels || []) { + const record = await decryptRecord(key, value.content); + if (hash(record.id) !== value.id || added.has(value.id)) + throw new Error('Invalid label identity'); + added.add(value.id); + await put('labels', record.id, value.content); + } + for (const id of body.deleteLabels || []) { + if (added.has(id)) throw new Error('Overlapping label mutation'); + const plainId = Object.keys(projected.labels).find((candidate) => hash(candidate) === id); + if (!plainId) throw new Error('Unknown label deletion'); + remove('labels', plainId); + } + } else throw new Error('Explicit deletion proof required'); + } + // Each touched ciphertext is local-current. Unaffected remote-only material + // is retained and makes the complete final check pending rather than Verified. + return projected; +} +function sameNodes(a: any, b: any) { + return ( + canonical( + Object.fromEntries( + Object.entries(a).map(([id, record]) => [id, recoveryContent('nodes', record)]) + ) + ) === + canonical( + Object.fromEntries( + Object.entries(b).map(([id, record]) => [id, recoveryContent('nodes', record)]) + ) + ) + ); +} +const pending = (committed: boolean, updated = false) => ({ + updated, + backupConfirmed: false, + backupPending: true, + localCommitted: committed, +}); +export function enqueueAutomaticBackupOperation( + receipt: BackupLocalCommit | BackupWriteIntent | undefined, + input: AutomaticBackupMutation[] +) { + const intent = receipt && ('committed' in receipt ? receipt.intent : receipt); + const committed = !!receipt && (!('committed' in receipt) || receipt.committed); + const proof = intent && proofs.get(intent); + let mutations: AutomaticBackupMutation[]; + try { + mutations = frozenCopy(input); + } catch { + if (intent?.appId) { + clearVerifiedRemoteRevision(intent.appId); + notifyBackupReconciliation(intent.appId); + } + return Promise.resolve(pending(committed)); + } + const appId = intent?.appId || input[0]?.body?.appId || input[0]?.body?.appID; + if (!appId) return Promise.resolve(pending(committed)); + markBackupQueuedMutation(appId); + notifyBackupReconciliation(appId); + return withBackupSession(appId, async () => { + let acknowledged = 0; + const deadline = Date.now() + BACKUP_DEADLINE_MS; + const assertOwned = () => { + if (!intent || !proof || Date.now() >= deadline) + throw new Error('Backup proof deadline expired'); + return assertProof(intent, proof, !!proof.deletion); + }; + try { + if (!intent || !proof || !mutations.length || proofs.get(intent) !== proof) + throw new Error('Missing committed operation proof'); + if ( + mutations.some( + ({ body }) => + !body || + (body.appId || body.appID) !== appId || + (body.appId !== undefined && body.appId !== appId) || + (body.appID !== undefined && body.appID !== appId) + ) + ) + throw new Error('Backup mutation account does not match originating proof'); + // Loopback is permitted only in the explicitly selected Node fixture. + const loopbackFixture = + typeof process !== 'undefined' && + process.env?.KEEPER_RELAY_FIXTURE_MODULE && + /^http:\/\/(127\.0\.0\.1|localhost):\d+\/$/.test(config.RELAY); + const nativeLoopback = + config.ENVIRONMENT === 'DEVELOPMENT' && + config.isDevMode() && + /^http:\/\/(127\.0\.0\.1|localhost):\d+\/$/.test(config.RELAY); + if ( + (!AUTOMATIC_BACKUP_SOURCE_READY && !loopbackFixture) || + (!AUTOMATIC_BACKUP_RELAY_ROLLOUT_READY && + !loopbackFixture && + !(AUTOMATIC_BACKUP_SOURCE_READY && nativeLoopback)) + ) + throw new Error('Automatic source proof not enabled'); + assertOwned(); + const baseline = await snapshot(appId, deadline); + assertOwned(); + if (baseline.revision !== intent.baseRevision) throw new Error('Remote revision changed'); + if (proof.deletion) { + if (mutations.length !== 1 || mutations[0].operation !== 'deleteBackup') + throw new Error('Invalid explicit deletion'); + await decodeImage( + generateEncryptionKey(proof.after.app.primarySeed), + baseline, + appId, + () => {}, + true + ); + } else await project(mutations, proof, baseline); + assertOwned(); + // One immutable receipt is single-use even if the response is lost. + proofs.delete(intent); + let expectedRevision = intent.baseRevision; + for (const mutation of mutations) { + assertOwned(); + const reply = ( + await boundedBackupPost( + `${config.RELAY}revisionedBackupMutation`, + { + ...mutation.body, + appId, + operation: mutation.operation, + expectedRevision, + }, + deadline + ) + ).data; + if ( + reply?.updated !== true || + reply.preconditionApplied !== true || + !/^[a-f0-9]{64}$/.test(reply.newRevision) || + reply.newRevision === expectedRevision + ) + throw new Error('Unacknowledged backup revision'); + expectedRevision = reply.newRevision; + acknowledged += 1; + } + assertOwned(); + const readback = await snapshot(appId, deadline); + assertOwned(); + if (readback.revision !== expectedRevision) throw new Error('Final backup revision changed'); + if (proof.deletion) { + const remaining = await decodeImage( + generateEncryptionKey(proof.after.app.primarySeed), + readback, + appId, + () => {}, + true + ); + if (Object.values(remaining).some((records) => Object.keys(records).length)) + throw new Error('Backup deletion is incomplete'); + } else { + if (!backupMetadataMatches(proof.after.app, readback.appImage)) + throw new Error('Backup metadata readback differs'); + const remote = await decodeImage( + generateEncryptionKey(proof.after.app.primarySeed), + readback, + appId, + assertOwned, + true + ); + if ((await compareImages(proof.after.image, remote, assertOwned)) !== 'matched') + throw new Error('Complete backup readback differs'); + } + assertOwned(); + recordVerifiedRemoteRevision(appId, expectedRevision, intent.accountSession); + if (!commitVerification(appId, !!proof.deletion, proof.operation.consentEpoch)) + throw new Error('Backup changed during final commit'); + const result = Object.freeze({ + updated: true, + backupConfirmed: true, + backupPending: false, + localCommitted: committed, + }); + results.set(result, { proof, session: intent.accountSession, appId }); + if (proof.deletion) clearVerifiedRemoteRevision(appId); + return result; + } catch { + clearVerifiedRemoteRevision(appId); + notifyBackupReconciliation(appId); + return pending(committed, acknowledged === mutations.length && acknowledged > 0); + } + }); +} +export async function beginBackupDeleteOperation( + appId: string, + originSession = currentBackupAccountSession() +): Promise { + try { + const op = beginBackupLocalOperation(appId, undefined, originSession); + const remote = await withBackupSession(appId, () => snapshot(appId)); + const state = local(); + if ( + op.accountSession !== currentBackupAccountSession() || + op.consentEpoch !== backupConsentEpoch(appId) || + state.database !== op.database || + state.generation !== op.generation || + state.capture.fingerprint !== op.before.fingerprint || + !canAccessBackupAccount(appId, true) + ) + throw new Error('Deletion scope changed'); + await decodeImage( + generateEncryptionKey(op.before.app.primarySeed), + remote, + appId, + () => {}, + true + ); + const current = local(); + if ( + originSession !== currentBackupAccountSession() || + op.consentEpoch !== backupConsentEpoch(appId) || + current.database !== op.database || + current.generation !== state.generation || + current.capture.fingerprint !== state.capture.fingerprint + ) + throw new Error('Deletion scope changed during snapshot validation'); + const intent: BackupWriteIntent = Object.freeze({ + appId, + accountSession: op.accountSession, + baseRevision: remote.revision, + baselineEpoch: backupBaselineEpoch(appId), + localGeneration: state.generation, + localFingerprint: state.capture.fingerprint, + operationId: `${op.accountSession}:delete:${++sequence}`, + consentEpoch: op.consentEpoch, + }); + proofs.set(intent, { + operation: op, + after: frozenCopy(state.capture), + generation: state.generation, + deletion: true, + }); + return Object.freeze({ committed: true, intent }); + } catch { + clearVerifiedRemoteRevision(appId); + notifyBackupReconciliation(appId); + return Object.freeze({ committed: false }); + } +} +setAutomaticBackupPostHandler(async (path, body, _explicit, intent) => { + const operation = path.split('/').pop() as AutomaticBackupMutation['operation']; + const data = await enqueueAutomaticBackupOperation(intent, [{ operation, body }]); + return { data }; +}); diff --git a/src/services/backup/intent.ts b/src/services/backup/intent.ts index fa6ad60695..e032f57632 100644 --- a/src/services/backup/intent.ts +++ b/src/services/backup/intent.ts @@ -4,4 +4,8 @@ export type BackupWriteIntent = Readonly<{ accountSession: number; baseRevision: string; baselineEpoch: number; + localGeneration?: number; + localFingerprint?: string; + operationId?: string; + consentEpoch?: number; }>; diff --git a/src/services/backup/localSnapshot.ts b/src/services/backup/localSnapshot.ts new file mode 100644 index 0000000000..0c8fb940a9 --- /dev/null +++ b/src/services/backup/localSnapshot.ts @@ -0,0 +1,91 @@ +import { RealmSchema } from 'src/storage/realm/enum'; +import { hash256 } from 'src/utils/service-utilities/encryption'; +import { + getKeyUID, + sanitizeSeedKeyForBackup, + sanitizeVaultSignersForSeedKeyBackup, +} from 'src/utils/utilities'; +import { BackupImage, canonical, recoveryContent } from './image'; + +export function backupMetadataMatches(local: any, remote: any) { + try { + const subscription = + typeof remote?.subscription === 'string' + ? JSON.parse(remote.subscription) + : remote?.subscription; + return ( + !!remote && + remote.appId === local.id && + remote.publicId === local.publicId && + remote.version === local.version && + canonical(subscription) === canonical(local.subscription) + ); + } catch { + return false; + } +} + +// Synchronous so no Realm mutation can interleave the final evidence check and +// Redux commit. Only a hash is retained; recovery material is never logged. +export function captureLocalRecovery(database: any) { + const plain = (value: any) => JSON.parse(JSON.stringify(value)); + const app = plain(database.objects(RealmSchema.KeeperApp)[0]); + if (!app?.id || !app.primarySeed) throw new Error('Recovery account unavailable'); + const image: BackupImage = { wallets: {}, signers: {}, vaults: {}, nodes: {}, labels: {} }; + const registrations = new Map(); + for (const record of database.objects(RealmSchema.VaultSigner)) + registrations.set(record.xpub, plain(record.registeredVaults)); + const sources: [keyof BackupImage, RealmSchema][] = [ + ['wallets', RealmSchema.Wallet], + ['wallets', RealmSchema.USDTWallet], + ['signers', RealmSchema.Signer], + ['vaults', RealmSchema.Vault], + ['nodes', RealmSchema.NodeConnect], + ['labels', RealmSchema.Tags], + ]; + for (const [kind, schema] of sources) { + for (const value of database.objects(schema)) { + const raw = plain(value); + if (kind === 'vaults') + raw.signers = raw.signers.map((signer: any) => + registrations.has(signer.xpub) + ? { ...signer, registeredVaults: registrations.get(signer.xpub) } + : signer + ); + const record = + kind === 'signers' + ? sanitizeSeedKeyForBackup(raw) + : kind === 'vaults' + ? sanitizeVaultSignersForSeedKeyBackup(raw) + : kind === 'nodes' + ? { ...raw, isConnected: false } + : raw; + const id = + kind === 'signers' + ? getKeyUID(record) + : record.id || canonical(recoveryContent(kind, record)); + if (image[kind][id]) throw new Error('Duplicate local recovery record'); + image[kind][id] = record; + } + } + const normalized = Object.fromEntries( + Object.entries(image).map(([kind, records]) => [ + kind, + Object.fromEntries( + Object.entries(records).map(([id, record]) => [ + id, + recoveryContent(kind as keyof BackupImage, record), + ]) + ), + ]) + ); + const metadata = { + id: app.id, + publicId: app.publicId, + primarySeed: app.primarySeed, + version: app.version, + networkType: app.networkType, + subscription: app.subscription, + }; + return { app, image, fingerprint: hash256(canonical({ metadata, image: normalized })) }; +} diff --git a/src/services/backup/repair.ts b/src/services/backup/repair.ts index 69056913d1..cc02f5f60c 100644 --- a/src/services/backup/repair.ts +++ b/src/services/backup/repair.ts @@ -2,12 +2,14 @@ import dbManager from 'src/storage/realm/dbManager'; import { RealmSchema } from 'src/storage/realm/enum'; import config from 'src/utils/service-utilities/config'; +import realmDatabase from 'src/storage/realm/realm'; import { generateEncryptionKey, hash256 } from 'src/utils/service-utilities/encryption'; import { getKeyUID, sanitizeSeedKeyForBackup, sanitizeVaultSignersForSeedKeyBackup, } from 'src/utils/utilities'; +import { captureLocalRecovery, backupMetadataMatches } from './localSnapshot'; import { BACKUP_DEADLINE_MS, BackupRequestError, @@ -70,8 +72,10 @@ function replacementWouldDiscardRecovery(local: BackupImage, remote: BackupImage for (const kind of Object.keys(remote) as (keyof BackupImage)[]) { for (const [id, stored] of Object.entries(remote[kind])) { const current = local[kind][id]; - if (!current || - canonical(recoveryContent(kind, current)) !== canonical(recoveryContent(kind, stored))) + if ( + !current || + canonical(recoveryContent(kind, current)) !== canonical(recoveryContent(kind, stored)) + ) return true; } } @@ -234,6 +238,7 @@ export function inspectBackup( try { notify('checking'); const app = clone(dbManager.getObjectByIndex(RealmSchema.KeeperApp)); + const localEvidence = captureLocalRecovery(realmDatabase.getDatabase()); assertCurrent(); const key = generateEncryptionKey(app.primarySeed); const local = await captureBackupImage(assertCurrent); @@ -241,6 +246,7 @@ export function inspectBackup( let expectedRevision: string; let needsVaultRepair = false; let needsLabelRepair = false; + let needsMetadataRepair = false; let unavailableVaultIds: string[] = []; let unavailableLabelIds: string[] = []; const read = async (readback = false) => { @@ -257,6 +263,7 @@ export function inspectBackup( if (response.data?.revisionedMutations !== true) throw new Error('Revisioned backup protocol unavailable'); expectedRevision = response.data.revision; + needsMetadataRepair = !backupMetadataMatches(app, response.data.appImage); const unavailable = response.data.unavailableVaultIds ?? []; if (!Array.isArray(unavailable) || new Set(unavailable).size !== unavailable.length) throw new Error('Invalid unavailable backup records'); @@ -297,20 +304,21 @@ export function inspectBackup( // participant. The relay withholds that participant's ciphertext. Only // this explicit backup check may compare the remaining records; clean // restore still rejects the incomplete reference list. - const availableResponse = unavailable.length || unavailableLabels.length - ? { - ...response.data, - appImage: { - ...response.data.appImage, - vaults: unavailable.length - ? references.filter((id) => !unavailable.includes(id)) - : response.data.appImage.vaults, - labels: unavailableLabels.length - ? labelReferences.filter((id) => !unavailableLabels.includes(id)) - : response.data.appImage.labels, - }, - } - : response.data; + const availableResponse = + unavailable.length || unavailableLabels.length + ? { + ...response.data, + appImage: { + ...response.data.appImage, + vaults: unavailable.length + ? references.filter((id) => !unavailable.includes(id)) + : response.data.appImage.vaults, + labels: unavailableLabels.length + ? labelReferences.filter((id) => !unavailableLabels.includes(id)) + : response.data.appImage.labels, + }, + } + : response.data; if (!readback) stage = 'snapshot-decryption'; // Restore can salvage legacy map aliases. Inspection requires the // stored key to match the ID inside each encrypted wallet record. @@ -329,7 +337,8 @@ export function inspectBackup( stage = 'comparison'; const contentComparison = await compareImages(local, remote, assertCurrent); const comparison = - contentComparison === 'matched' && (needsVaultRepair || needsLabelRepair) + contentComparison === 'matched' && + (needsVaultRepair || needsLabelRepair || needsMetadataRepair) ? 'different' : contentComparison; assertCurrent(); @@ -346,9 +355,8 @@ export function inspectBackup( replacementWouldDiscardRecovery(local, remote) || unavailableVaultIds.some((id) => !local.vaults[id]) || unavailableLabelIds.some( - (id) => !Object.keys(local.labels).some( - (localId) => hash256(hash256(key + localId)) === id - ) + (id) => + !Object.keys(local.labels).some((localId) => hash256(hash256(key + localId)) === id) ) ) { notify('conflict'); @@ -368,6 +376,7 @@ export function inspectBackup( notify('uploading'); stage = 'repair-upload'; let revisionedRepairAcknowledged = false; + let acknowledgedRepairRevision: string | undefined; try { const response = await boundedBackupPost( `${config.RELAY}revisionedBackupMutation`, @@ -384,7 +393,11 @@ export function inspectBackup( deadline ); revisionedRepairAcknowledged = - response.data?.updated === true && response.data?.preconditionApplied === true; + response.data?.updated === true && + response.data?.preconditionApplied === true && + /^[a-f0-9]{64}$/.test(response.data?.newRevision) && + response.data.newRevision !== expectedRevision; + if (revisionedRepairAcknowledged) acknowledgedRepairRevision = response.data.newRevision; } catch { // A lost response is not proof the upload failed. Read back, but do // not mark this repair verified without an acknowledged v2 write. @@ -395,8 +408,10 @@ export function inspectBackup( stage = 'repair-readback'; if ( !revisionedRepairAcknowledged || + expectedRevision !== acknowledgedRepairRevision || needsVaultRepair || needsLabelRepair || + needsMetadataRepair || (await compareImages(local, verifiedRemote, assertCurrent)) !== 'matched' ) throw new Error('Backup not verified'); @@ -408,6 +423,10 @@ export function inspectBackup( ) throw new Error('Local backup changed'); assertCurrent(); + if ( + captureLocalRecovery(realmDatabase.getDatabase()).fingerprint !== localEvidence.fingerprint + ) + throw new Error('Committed recovery changed during final verification'); recordVerifiedRemoteRevision(appId, expectedRevision, expectedSession); notify('verified'); return 'verified'; diff --git a/src/services/backup/transport.ts b/src/services/backup/transport.ts index 3406f15117..c7cc599445 100644 --- a/src/services/backup/transport.ts +++ b/src/services/backup/transport.ts @@ -5,13 +5,63 @@ import type { BackupWriteIntent } from './intent'; const queues = new Map>(); const revisions = new Map(); const queuedMutationRevisions = new Map(); -const verifiedRemoteRevisions = new Map(); +const verifiedRemoteRevisions = new Map< + string, + { revision: string; session: number; epoch: number; evidence?: LocalEvidence } +>(); const baselineEpochs = new Map(); +const capturedIntents = new WeakSet(); +const consentEpochs = new Map(); +const consentCompletions = new WeakMap(); +export const backupConsentEpoch = (appId: string) => consentEpochs.get(appId) || 0; +export function protectBackupConsentCompletion( + action: T, + appId: string, + epoch: number +): T { + consentCompletions.set(action, { appId, epoch }); + return action; +} +export function observeBackupConsentAction(action: any, appId: string): boolean { + const completion = consentCompletions.get(action); + if (completion) { + consentCompletions.delete(action); + return completion.appId === appId && completion.epoch === backupConsentEpoch(appId); + } + consentEpochs.set(appId, backupConsentEpoch(appId) + 1); + return true; +} +export const isCapturedBackupWriteIntent = (intent: BackupWriteIntent) => + capturedIntents.has(intent); +type LocalEvidence = { appId: string; generation: number; fingerprint: string }; +let localEvidence: (() => LocalEvidence) | undefined; +let automaticPost: + | (( + path: string, + body: any, + explicitChoice: boolean, + intent?: BackupWriteIntent + ) => Promise<{ data: any }>) + | undefined; +export const setBackupLocalEvidenceProvider = (provider: () => LocalEvidence) => { + localEvidence = provider; +}; +export const setAutomaticBackupPostHandler = (handler: typeof automaticPost) => { + automaticPost = handler; +}; +export const backupBaselineEpoch = (appId: string) => baselineEpochs.get(appId) || 0; +export const notifyBackupReconciliation = (appId: string) => onBackupReconciliationRequired(appId); +export const markBackupQueuedMutation = (id: string) => { + markBackupMutation(id); + queuedMutationRevisions.set(id, backupRevision(id)); +}; let activeAccount: string | undefined; let accountSession = 0; let mayUploadForAccount: (appId: string, explicitChoice: boolean) => boolean = () => false; let onBackupReconciliationRequired: (appId: string) => void = () => undefined; -export const setBackupUploadGuard = (guard: (appId: string, explicitChoice: boolean) => boolean) => { +export const setBackupUploadGuard = ( + guard: (appId: string, explicitChoice: boolean) => boolean +) => { mayUploadForAccount = guard; }; // A failed or ambiguous write requires a fresh backup check. The encrypted @@ -38,23 +88,37 @@ export function clearVerifiedRemoteRevision(appId: string) { baselineEpochs.set(appId, (baselineEpochs.get(appId) || 0) + 1); } export function recordVerifiedRemoteRevision(appId: string, revision: string, session: number) { - if ( - !/^[a-f0-9]{64}$/.test(revision) || - session !== accountSession || - appId !== activeAccount - ) return; + if (!/^[a-f0-9]{64}$/.test(revision) || session !== accountSession || appId !== activeAccount) + return; clearVerifiedRemoteRevision(appId); verifiedRemoteRevisions.set(appId, { - revision, session, epoch: baselineEpochs.get(appId) || 0, + revision, + session, + epoch: baselineEpochs.get(appId) || 0, + evidence: localEvidence?.(), }); } export function captureBackupWriteIntent(appId: string): BackupWriteIntent | undefined { const verified = verifiedRemoteRevisions.get(appId); if (!verified || appId !== activeAccount || verified.session !== accountSession) return undefined; - return { - appId, accountSession: verified.session, - baseRevision: verified.revision, baselineEpoch: verified.epoch, - }; + let evidence: LocalEvidence | undefined; + try { + evidence = localEvidence?.(); + } catch { + return undefined; + } + if (localEvidence && evidence?.appId !== appId) return undefined; + const intent = Object.freeze({ + appId, + accountSession: verified.session, + baseRevision: verified.revision, + baselineEpoch: verified.epoch, + localGeneration: verified.evidence?.generation, + localFingerprint: verified.evidence?.fingerprint, + consentEpoch: backupConsentEpoch(appId), + }); + capturedIntents.add(intent); + return intent; } export const markBackupMutation = (id: string) => { revisions.set(id, backupRevision(id) + 1); @@ -63,7 +127,13 @@ export const BACKUP_STALL_MS = 30_000; export const BACKUP_DEADLINE_MS = 10 * 60_000; export type BackupRequestFailure = - 'conflict' | 'protocol' | 'http-4xx' | 'http-5xx' | 'timeout' | 'network' | 'other'; + | 'conflict' + | 'protocol' + | 'http-4xx' + | 'http-5xx' + | 'timeout' + | 'network' + | 'other'; export class BackupRequestError extends Error { constructor(public readonly category: BackupRequestFailure) { @@ -74,7 +144,13 @@ export class BackupRequestError extends Error { function requestFailureCategory(error: unknown, aborted: boolean): BackupRequestFailure { if (aborted) return 'timeout'; if (!error || typeof error !== 'object') return 'other'; - const failure = error as { response?: { status?: unknown }; status?: unknown; code?: unknown; name?: unknown; message?: unknown }; + const failure = error as { + response?: { status?: unknown }; + status?: unknown; + code?: unknown; + name?: unknown; + message?: unknown; + }; const status = failure.response?.status ?? failure.status; if (typeof status === 'number') { if (status === 409) return 'conflict'; @@ -82,7 +158,11 @@ function requestFailureCategory(error: unknown, aborted: boolean): BackupRequest if (status >= 500 && status < 600) return 'http-5xx'; return 'other'; } - if (failure.code === 'ECONNABORTED' || failure.code === 'ETIMEDOUT' || failure.name === 'AbortError') + if ( + failure.code === 'ECONNABORTED' || + failure.code === 'ETIMEDOUT' || + failure.name === 'AbortError' + ) return 'timeout'; if (failure.code === 'ERR_NETWORK' || failure.message === 'Network Error') return 'network'; return 'other'; @@ -144,10 +224,14 @@ export async function boundedBackupPost( } export function backupPost( - _path: string, body: any, explicitChoice = false, intent?: BackupWriteIntent + _path: string, + body: any, + explicitChoice?: boolean, + intent?: BackupWriteIntent ): Promise<{ data: any; json?: any }> { const appId = body.appId || body.appID; if (!appId) return Promise.reject(new Error('Missing backup account')); + if (automaticPost) return automaticPost(_path, body, explicitChoice === true, intent); // Every attempted incremental write invalidates an outstanding inspection // and leaves a visible pending state, including pre-send session/guard // failures from direct hooks and upgrade workers. diff --git a/src/store/store.ts b/src/store/store.ts index 9a885b5f13..3ea3ce0d61 100644 --- a/src/store/store.ts +++ b/src/store/store.ts @@ -4,9 +4,35 @@ import { combineReducers } from 'redux'; import { configureStore } from '@reduxjs/toolkit'; import createSagaMiddleware from 'redux-saga'; import { reduxStorage } from 'src/storage'; +import dbManager from 'src/storage/realm/dbManager'; +import { RealmSchema } from 'src/storage/realm/enum'; +import { + captureBackupWriteIntent, + canAccessBackupAccount, + backupConsentEpoch, + observeBackupConsentAction, + protectBackupConsentCompletion, + currentBackupAccountSession, + markBackupMutation, + observeBackupAccount, + setBackupReconciliationHandler, + setBackupUploadGuard, +} from 'src/services/backup/transport'; +import { + isBackupVerificationCurrent, + noteCommittedBackupRecovery, + setAutomaticBackupVerificationCommit, +} from 'src/services/backup/automatic'; +import { KeeperApp } from 'src/models/interfaces/KeeperApp'; import loginReducer from './reducers/login'; import notificationsReducer from './reducers/notifications'; -import bhrReducer from './reducers/bhr'; +import bhrReducer, { + invalidateBackupRepair, + isAutomaticCloudBackupEnabled, + setPendingAllBackup, + commitBackupInspection, + setAutomaticCloudBackup, +} from './reducers/bhr'; import rootSaga from './sagas'; import sendAndReceiveReducer from './reducers/send_and_receive'; import settingsReducer from './reducers/settings'; @@ -25,23 +51,6 @@ import helpAiReducer from './reducers/helpAi'; import { RESET_REDUX_STORE } from './sagaActions/upgrade'; import reduxPersistMigrations from './migrations'; -import dbManager from 'src/storage/realm/dbManager'; -import { RealmSchema } from 'src/storage/realm/enum'; -import { - captureBackupWriteIntent, - currentBackupAccountSession, - clearVerifiedRemoteRevision, - markBackupMutation, - observeBackupAccount, - setBackupReconciliationHandler, - setBackupUploadGuard, -} from 'src/services/backup/transport'; -import { - invalidateBackupRepair, - isAutomaticCloudBackupEnabled, - setPendingAllBackup, -} from './reducers/bhr'; -import { KeeperApp } from 'src/models/interfaces/KeeperApp'; const appReducer = combineReducers({ settings: settingsReducer, @@ -134,13 +143,35 @@ const accountOwnedUtxoUiActions = new Set([ 'utxos/resetState', ]); const bindActionOrigin = (api) => (next) => (action) => { + if ( + action?.type === 'bhr/commitBackupInspection' && + action.payload?.phase === 'verified' && + !isBackupVerificationCurrent(action.payload.appId) + ) + action = { ...action, payload: { ...action.payload, phase: 'unverified', pending: true } }; const originAppId = api.getState()?.storage?.appId; - const enablesBackup = action?.type === 'bhr/setAutomaticCloudBackup' && - action.payload?.enabled === true; - const reportsBackupSuccess = action?.type === 'bhr/setBackupAllSuccess' && - action.payload?.status === true; + if ( + ['bhr/setAutomaticCloudBackup', 'DELETE_BACKUP', 'BACKUP_ALL_SIGNERS_AND_VAULTS'].includes( + action?.type + ) + ) { + const choiceAppId = action.payload?.appId || action.appId || action.originAppId || originAppId; + const disablesConsent = + action.type === 'bhr/setAutomaticCloudBackup' && action.payload?.enabled === false; + if ( + !choiceAppId || + choiceAppId !== originAppId || + (!disablesConsent && !canAccessBackupAccount(choiceAppId, true)) || + !observeBackupConsentAction(action, choiceAppId) + ) + return action; + } + const enablesBackup = + action?.type === 'bhr/setAutomaticCloudBackup' && action.payload?.enabled === true; + const reportsBackupSuccess = + action?.type === 'bhr/setBackupAllSuccess' && action.payload?.status === true; if (enablesBackup || reportsBackupSuccess) { - const appId = action.payload.appId; + const { appId } = action.payload; if (!appId || originAppId !== appId) return action; try { if ((dbManager.getObjectByIndex(RealmSchema.KeeperApp) as unknown as KeeperApp)?.id !== appId) @@ -153,18 +184,15 @@ const bindActionOrigin = (api) => (next) => (action) => { try { if ( action.originAppId !== originAppId || - (dbManager.getObjectByIndex(RealmSchema.KeeperApp) as unknown as KeeperApp)?.id !== originAppId - ) return action; + (dbManager.getObjectByIndex(RealmSchema.KeeperApp) as unknown as KeeperApp)?.id !== + originAppId + ) + return action; } catch { return action; } } - if ( - originAppId && - action && - accountOwnedActions.has(action.type) && - !('originAppId' in action) - ) + if (originAppId && action && accountOwnedActions.has(action.type) && !('originAppId' in action)) action = { ...action, originAppId }; if ( action && @@ -218,7 +246,9 @@ setBackupUploadGuard((appId, explicitChoice) => { ) return false; try { - return (dbManager.getObjectByIndex(RealmSchema.KeeperApp) as unknown as KeeperApp)?.id === appId; + return ( + (dbManager.getObjectByIndex(RealmSchema.KeeperApp) as unknown as KeeperApp)?.id === appId + ); } catch { return false; } @@ -235,6 +265,41 @@ setBackupReconciliationHandler((appId) => { // settled and a later inspection captures the resulting local state. }); +dbManager.setRecoveryCommitObserver(noteCommittedBackupRecovery); +setAutomaticBackupVerificationCommit((appId, deleted, consentEpoch) => { + if ( + store.getState().storage.appId !== appId || + !isBackupVerificationCurrent(appId) || + !canAccessBackupAccount(appId, deleted) || + backupConsentEpoch(appId) !== consentEpoch + ) + return false; + store.dispatch( + commitBackupInspection({ appId, phase: deleted ? 'unverified' : 'verified', pending: false }) + ); + // Middleware also checks the proof immediately before reducers accept it. + if ( + !isBackupVerificationCurrent(appId) || + !canAccessBackupAccount(appId, deleted) || + backupConsentEpoch(appId) !== consentEpoch || + (!deleted && !store.getState().bhr.backupRepairCompletedByAppId?.[appId]) + ) + return false; + if (deleted) + store.dispatch( + protectBackupConsentCompletion( + setAutomaticCloudBackup({ appId, enabled: false }), + appId, + consentEpoch + ) + ); + return ( + isBackupVerificationCurrent(appId) && + canAccessBackupAccount(appId, deleted) && + backupConsentEpoch(appId) === consentEpoch + ); +}); + dbManager.setRecoveryMutationObserver(() => { let appId: string | undefined; try { @@ -246,7 +311,6 @@ dbManager.setRecoveryMutationObserver(() => { // Realm can change while an inspection is between its final local capture // and completion. Invalidate the revision before any recovery write commits. markBackupMutation(appId); - clearVerifiedRemoteRevision(appId); store.dispatch(invalidateBackupRepair(appId)); store.dispatch(setPendingAllBackup({ appId, pending: true })); }); diff --git a/tests/recovery/helpers.cjs b/tests/recovery/helpers.cjs index 29dba27fda..d3f3ef7eee 100644 --- a/tests/recovery/helpers.cjs +++ b/tests/recovery/helpers.cjs @@ -5,69 +5,176 @@ const ts = require('typescript'); function loadModule(file, mocks = {}, globals = {}) { const module = { exports: {} }; const code = ts.transpileModule(fs.readFileSync(path.join(__dirname, '../..', file), 'utf8'), { - compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020, esModuleInterop: true }, + compilerOptions: { + module: ts.ModuleKind.CommonJS, + target: ts.ScriptTarget.ES2020, + esModuleInterop: true, + }, }).outputText; - vm.runInNewContext(code, { module, exports: module.exports, setTimeout, clearTimeout, - AbortController, Date, Buffer, console, - require: (name) => name in mocks ? mocks[name] : require(name), ...globals }); + vm.runInNewContext(code, { + module, + exports: module.exports, + setTimeout, + clearTimeout, + AbortController, + Date, + Buffer, + console, + require: (name) => (name in mocks ? mocks[name] : require(name)), + ...globals, + }); return module.exports; } -const encryption = loadModule('src/utils/service-utilities/encryption.ts', { 'react-native-rsa-native': { RSA: {} } }); -const image = loadModule('src/services/backup/image.ts', { 'src/utils/service-utilities/encryption': encryption }); +const encryption = loadModule('src/utils/service-utilities/encryption.ts', { + 'react-native-rsa-native': { RSA: {} }, +}); +const image = loadModule('src/services/backup/image.ts', { + 'src/utils/service-utilities/encryption': encryption, +}); const makeImage = () => ({ wallets: {}, signers: {}, vaults: {}, nodes: {}, labels: {} }); function harness(options = {}) { - const app = { id: 'disposable', publicId: 'fixture', primarySeed: 'ab'.repeat(32), subscription: { level: 1 }, version: '2.5.16' }; - const local = { Wallet: [], USDTWallet: [], Signer: [], Vault: [], VaultSigner: [], NodeConnect: [], Tags: [] }; - const remote = { appId: app.id, wallets: {}, signers: {}, nodes: [], vaults: [], labels: [] }; - let vaults = [], labels = []; - const revision = () => require('node:crypto').createHash('sha256').update(JSON.stringify({remote,vaults,labels})).digest('hex'); - const calls = [], phases = [], diagnostics = []; - const db = { getObjectByIndex: (schema) => schema === 'KeeperApp' ? app : local[schema] }; - const rest = { post: async (path, payload, _headers, config) => { - const revisionBeforeHook = revision(); - calls.push(path.replace('fixture/', '')); - if (options.post) await options.post({ path, payload, config, remote, app, local }); - if (options.adapter) return options.adapter(path.replace('fixture/', ''), payload, config); - if (path.endsWith('getBackupSnapshot')) { - if (options.readError) throw Error('offline'); - return { data: options.response || { - appImage: remote, allVaultImages: vaults, labels, revision: revision(), - revisionedMutations: true, - } }; - } - if (path.endsWith('revisionedBackupMutation') && payload.operation !== 'repairAppBackup') { - if (payload.expectedRevision !== revisionBeforeHook) - throw { response: { status: 409 } }; - return { data: { updated: true, preconditionApplied: true } }; - } - if (path.endsWith('updateAppImage')) return { data: { updated: true } }; - if (payload.expectedRevision !== revision()) throw Error('Backup changed'); - if (options.reject) return { data: { updated: false } }; - remote.wallets = { ...payload.walletObject }; remote.signers = { ...payload.signersObject }; remote.nodes = payload.nodes; - vaults = Object.values(payload.vaultObject); labels = payload.labels; - remote.vaults = vaults.map(v => v.vaultId); remote.labels = labels.map(v => v.id); - if (options.corruptWrite) remote.wallets = {}; - if (options.lostResponse) throw Error('response lost after commit'); - return { data: { - updated: true, - ...(path.endsWith('revisionedBackupMutation') ? { preconditionApplied: true } : {}), - } }; - } }; + const app = { + id: 'disposable', + publicId: 'fixture', + primarySeed: 'ab'.repeat(32), + subscription: { level: 1 }, + version: '2.5.16', + }; + const local = { + Wallet: [], + USDTWallet: [], + Signer: [], + Vault: [], + VaultSigner: [], + NodeConnect: [], + Tags: [], + }; + const remote = { + appId: app.id, + publicId: app.publicId, + version: app.version, + networkType: app.networkType, + subscription: JSON.stringify(app.subscription), + wallets: {}, + signers: {}, + nodes: [], + vaults: [], + labels: [], + }; + let vaults = [], + labels = []; + const revision = () => + require('node:crypto') + .createHash('sha256') + .update(JSON.stringify({ remote, vaults, labels })) + .digest('hex'); + const calls = [], + phases = [], + diagnostics = []; + const db = { getObjectByIndex: (schema) => (schema === 'KeeperApp' ? app : local[schema]) }; + const rest = { + post: async (path, payload, _headers, config) => { + const revisionBeforeHook = revision(); + calls.push(path.replace('fixture/', '')); + if (options.post) await options.post({ path, payload, config, remote, app, local }); + if (options.adapter) return options.adapter(path.replace('fixture/', ''), payload, config); + if (path.endsWith('getBackupSnapshot')) { + if (options.readError) throw Error('offline'); + return { + data: options.response || { + appImage: remote, + allVaultImages: vaults, + labels, + revision: revision(), + revisionedMutations: true, + }, + }; + } + if (path.endsWith('revisionedBackupMutation') && payload.operation !== 'repairAppBackup') { + if (payload.expectedRevision !== revisionBeforeHook) throw { response: { status: 409 } }; + return { data: { updated: true, preconditionApplied: true } }; + } + if (path.endsWith('updateAppImage')) return { data: { updated: true } }; + if (payload.expectedRevision !== revision()) throw Error('Backup changed'); + if (options.reject) return { data: { updated: false } }; + remote.wallets = { ...payload.walletObject }; + remote.signers = { ...payload.signersObject }; + remote.nodes = payload.nodes; + for (const field of ['publicId', 'version', 'networkType', 'subscription']) + remote[field] = payload[field]; + vaults = Object.values(payload.vaultObject); + labels = payload.labels; + remote.vaults = vaults.map((v) => v.vaultId); + remote.labels = labels.map((v) => v.id); + if (options.corruptWrite) remote.wallets = {}; + if (options.lostResponse) throw Error('response lost after commit'); + return { + data: { + updated: true, + ...(path.endsWith('revisionedBackupMutation') ? { preconditionApplied: true } : {}), + newRevision: revision(), + }, + }; + }, + }; const clock = options.now ? { Date: { now: options.now } } : {}; - const transport = loadModule('src/services/backup/transport.ts', { '../rest/RestClient': rest }, clock); - transport.setBackupUploadGuard(id => app.id === id); - const repair = loadModule('src/services/backup/repair.ts', { - 'src/storage/realm/dbManager': db, - 'src/storage/realm/enum': { RealmSchema: new Proxy({}, { get: (_, name) => name }) }, - 'src/utils/service-utilities/config': { RELAY: 'fixture/' }, + const transport = loadModule( + 'src/services/backup/transport.ts', + { '../rest/RestClient': rest }, + clock + ); + transport.setBackupUploadGuard((id) => app.id === id); + const realmAdapter = { objects: (schema) => (schema === 'KeeperApp' ? [app] : local[schema]) }; + const schemaEnum = { RealmSchema: new Proxy({}, { get: (_, name) => name }) }; + const utilities = { + getKeyUID: (s) => s.id || s.masterFingerprint, + sanitizeSeedKeyForBackup: (s) => s, + sanitizeVaultSignersForSeedKeyBackup: (s) => s, + }; + const localSnapshot = loadModule('src/services/backup/localSnapshot.ts', { + 'src/storage/realm/enum': schemaEnum, 'src/utils/service-utilities/encryption': encryption, - 'src/utils/utilities': { getKeyUID: s => s.id || s.masterFingerprint, sanitizeSeedKeyForBackup: s => s, sanitizeVaultSignersForSeedKeyBackup: s => s }, - './transport': transport, './image': image, - }, { ...clock, console: { warn: (...args) => diagnostics.push(args) } }); - return { app, local, remote, calls, phases, diagnostics, transport, repair, - run: (write = false) => repair.inspectBackup(app.id, write, phase => phases.push(phase)) }; + 'src/utils/utilities': utilities, + './image': image, + }); + const repair = loadModule( + 'src/services/backup/repair.ts', + { + 'src/storage/realm/dbManager': db, + 'src/storage/realm/realm': { getDatabase: () => realmAdapter }, + 'src/storage/realm/enum': schemaEnum, + 'src/utils/service-utilities/config': { RELAY: 'fixture/' }, + 'src/utils/service-utilities/encryption': encryption, + 'src/utils/utilities': utilities, + './transport': transport, + './image': image, + './localSnapshot': localSnapshot, + }, + { ...clock, console: { warn: (...args) => diagnostics.push(args) } } + ); + return { + app, + local, + remote, + calls, + phases, + diagnostics, + transport, + repair, + run: (write = false) => repair.inspectBackup(app.id, write, (phase) => phases.push(phase)), + }; } -const wallet = (id = 'wallet', networkType = 'MAINNET') => ({ id, networkType, type: 'DEFAULT', - derivationDetails: { instanceNum: 0 }, presentationData: { name: 'Disposable wallet' }, - specs: { xpub: 'disposable-test-key', nextFreeAddressIndex: 1, balances: { confirmed: 0 }, transactions: [] } }); +const wallet = (id = 'wallet', networkType = 'MAINNET') => ({ + id, + networkType, + type: 'DEFAULT', + derivationDetails: { instanceNum: 0 }, + presentationData: { name: 'Disposable wallet' }, + specs: { + xpub: 'disposable-test-key', + nextFreeAddressIndex: 1, + balances: { confirmed: 0 }, + transactions: [], + }, +}); module.exports = { loadModule, encryption, image, makeImage, harness, wallet }; diff --git a/tests/recovery/repair.test.cjs b/tests/recovery/repair.test.cjs index 7b23f02602..6c78864787 100644 --- a/tests/recovery/repair.test.cjs +++ b/tests/recovery/repair.test.cjs @@ -75,7 +75,8 @@ for (const os of ['ios', 'android']) test(`${os} RestClient preserves the read-o requests.push({ path, body, options }); if (requests.length === 1) throw { code: 'ERR_NETWORK' }; return { data: { - appImage: { appId: 'disposable', wallets: {}, signers: {}, nodes: [], vaults: [], labels: [] }, + appImage: { appId: 'disposable', publicId: 'fixture', version: '2.5.16', + subscription: JSON.stringify({ level: 1 }), wallets: {}, signers: {}, nodes: [], vaults: [], labels: [] }, allVaultImages: [], labels: [], revision: 'a'.repeat(64), revisionedMutations: true, } }; } }, From 1169112a2258f599348ba2d74a50f9818c3fcf1a Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Tue, 13 Oct 2026 03:50:15 +0000 Subject: [PATCH 162/254] test(recovery): exercise atomic signer identity replacement --- tests/recovery/fingerprintTransport.test.cjs | 8 ++++++-- tests/recovery/sagaHarness.cjs | 4 ++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/tests/recovery/fingerprintTransport.test.cjs b/tests/recovery/fingerprintTransport.test.cjs index f8622c69ee..f5f68241c4 100644 --- a/tests/recovery/fingerprintTransport.test.cjs +++ b/tests/recovery/fingerprintTransport.test.cjs @@ -75,7 +75,7 @@ test('direct migration attempts never send ciphertext and keep redacted errors', assert.deepEqual(pending, ['fixture-account', 'fixture-account']); }); -test('fingerprint migration targets the actual KeyUID map entry after Realm changes the live signer', async () => { +test('fingerprint migration targets the actual KeyUID map entry after atomic signer replacement', async () => { const f = sagaFixture(); const oldSigner = sagaSigner('AABBCCDD', enums.NetworkType.MAINNET); oldSigner.signerXpubs[enums.XpubTypes.P2WSH] = [{ @@ -90,6 +90,7 @@ test('fingerprint migration targets the actual KeyUID map entry after Realm chan const newKey = f.scope.getKeyUID(replacement); assert.notEqual(oldKey, oldSigner.masterFingerprint); assert.notEqual(newKey, replacement.masterFingerprint); + oldSigner.id = oldKey; f.collections.Signer.push(oldSigner); f.remote.signers[oldKey] = 'old-ciphertext'; f.scope.dbManager.getObjectByQuery = () => []; @@ -107,7 +108,10 @@ test('fingerprint migration targets the actual KeyUID map entry after Realm chan originAppId: f.app.id, payload: { signer: replacement }, }), true); - assert.equal(oldSigner.masterFingerprint, replacement.masterFingerprint); + const stored = f.scope.dbManager.getObjectById('Signer', newKey); + assert.equal(stored.masterFingerprint, replacement.masterFingerprint); + assert.equal(f.scope.dbManager.getObjectById('Signer', oldKey), undefined); + assert.equal(oldSigner.masterFingerprint, 'AABBCCDD', 'the deleted object is not the current signer'); assert.equal(submitted.appId, f.app.id); assert.equal(submitted.changes[0].oldSignerId, oldKey); assert.equal(submitted.changes[0].newSignerId, newKey); diff --git a/tests/recovery/sagaHarness.cjs b/tests/recovery/sagaHarness.cjs index 639f5b449f..455c6813d4 100644 --- a/tests/recovery/sagaHarness.cjs +++ b/tests/recovery/sagaHarness.cjs @@ -520,7 +520,7 @@ function fixture(options = {}) { 'currentWalletAccountId', 'assertWalletAccount', 'rewriteSignerFingerprintReferences', 'planSignerIdentityMerges', 'applySignerIdentityMerges', 'applySignerXprivUpdates', 'buildSignerMigrationMutation', - 'addNewWalletsWorker', 'addSigningDeviceWorker', + 'addNewWalletsWorker', 'addSigningDeviceWorker', 'mergeSimilarKeysWorker', 'deleteSigningDeviceWorker', 'deleteVaultWorker', 'deleteVaultRequestWorker', 'generateNewExternalAddressWorker', 'updateSignerPolicyWorker', @@ -556,7 +556,7 @@ function fixture(options = {}) { }; scope.store.dispatch = dispatch; const mutationActions = new Set([ - 'addNewWalletsWorker', 'addSigningDeviceWorker', 'deleteSigningDeviceWorker', + 'addNewWalletsWorker', 'addSigningDeviceWorker', 'mergeSimilarKeysWorker', 'deleteSigningDeviceWorker', 'deleteAppImageEntityWorker', 'deleteAppImageEntityRequestWorker', 'deleteVaultWorker', 'deleteVaultRequestWorker', 'updateSignerPolicyWorker', 'updateVaultSignerXprivWorker', 'updateSignerDetailsWorker', 'updateKeyDetailsWorker', From ae6e395810b5f15de41ba10f26b392d080e2a283 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Tue, 13 Oct 2026 03:51:24 +0000 Subject: [PATCH 163/254] Prove existing USDT hook backups and explicit unit readback baselines --- .../automaticUSDTProducer.integration.cjs | 174 ++++++++++++++++++ tests/recovery/backupFreshness.test.cjs | 4 + 2 files changed, 178 insertions(+) create mode 100644 tests/recovery/automaticUSDTProducer.integration.cjs diff --git a/tests/recovery/automaticUSDTProducer.integration.cjs b/tests/recovery/automaticUSDTProducer.integration.cjs new file mode 100644 index 0000000000..20b00a7f5a --- /dev/null +++ b/tests/recovery/automaticUSDTProducer.integration.cjs @@ -0,0 +1,174 @@ +// Real existing-wallet USDT hook callbacks through native Realm, production +// backup sagas, actual AES and paired Relay HTTP. Creation stays paused; this +// fixture never contacts a token chain or derives a new USDT wallet. +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const path = require('node:path'); +const ts = require('typescript'); +const { test, before, after } = require('node:test'); +const { createRealmClient, declarations, evaluate, plain, Realm } = require('./automaticWriteRealmRelay.fixture.cjs'); +assert.ok(process.env.KEEPER_RELAY_FIXTURE_MODULE, 'The pinned paired Relay fixture is required'); +const { createFixture } = require(process.env.KEEPER_RELAY_FIXTURE_MODULE); +let server; +before(async () => { server = await createFixture(); }); +after(async () => { await server?.close(); Realm.shutdown(); }); +const writes = () => server.trace.filter(event => event.path === '/revisionedBackupMutation'); + +function usdt(f, type = 'DEFAULT', id = `usdt-${type}`) { + return f.defaults(f.RealmSchema.USDTWallet, { + id, type, entityKind: f.enums.EntityKind.USDT_WALLET, networkType: f.enums.NetworkType.TESTNET, + derivationDetails: f.defaults(f.RealmSchema.USDTWalletDerivationDetails, { + instanceNum: type === 'DEFAULT' ? 1 : null, mnemonic: 'public disposable fixture only', xDerivationPath: "m/44'/195'/0'/0/0", + }), + presentationData: f.defaults(f.RealmSchema.USDTWalletPresentationData, { name: 'Existing wallet', description: 'Before', visibility: 'DEFAULT' }), + specs: f.defaults(f.RealmSchema.USDTWalletSpecs, { address: 'disposable-token-address', privateKey: 'unspendable-fixture-private-material', balance: 10 }), + }); +} + +function selectedCallbacks() { + const file = 'src/hooks/useUSDTWallets.ts'; + const source = ts.createSourceFile(file, fs.readFileSync(path.resolve(__dirname, '../..', file), 'utf8'), ts.ScriptTarget.Latest, true); + const needed = new Set(['isOriginCurrent', 'createWallet', 'updateWallet', 'deleteWallet']); + const found = []; + function visit(node) { + if (ts.isVariableDeclaration(node) && needed.has(node.name.getText(source))) + found.push(`const ${node.getText(source)};`); + ts.forEachChild(node, visit); + } + visit(source); + assert.equal(found.length, needed.size, 'All selected production hook callbacks must exist'); + return declarations(file, ['USDT_WALLET_CREATION_PAUSED']) + '\n' + found.join('\n'); +} + +async function setup(t, type = 'DEFAULT') { + await server.reset(); + const f = await createRealmClient(server.url); + t.after(() => f.close()); + f.automatic = f.load('src/services/backup/automatic'); + f.installProductionStoreObservers(f.automatic); + f.store.dispatch(f.bhr.setAutomaticCloudBackup({ appId: f.appId, enabled: true })); + assert.equal(f.dbManager.createObject(f.RealmSchema.USDTWallet, usdt(f, type)), true); + assert.equal(f.dbManager.createObject(f.RealmSchema.USDTWallet, usdt(f, 'IMPORTED', 'retained-usdt')), true); + await server.seed(await f.payloadForCurrentImage()); + assert.equal(await f.repair.inspectBackup(f.appId, false, () => {}), 'verified'); + server.clearTrace(); + const sagas = f.productionSagas(['currentBackupAppId', 'buildAutomaticAppMutation', 'queueCommittedBackupWorker', 'updateAppImageWorker']); + const pending = []; + const errors = []; + const scope = { + ...f.encryption, ...f.image, ...f.transport, ...f.automatic, ...f.bhr, + ...f.load('src/store/sagaActions/bhr'), RealmSchema: f.RealmSchema, dbManager: f.dbManager, + appId: f.appId, renderAccountSession: f.transport.currentBackupAccountSession(), reduxStore: f.store, + // Execute the selected callback closure without claiming a React render. + // Realm, Redux, encryption, commit receipts and HTTP remain production. + useCallback: callback => callback, + dispatch(action) { + f.store.dispatch(action); + if (action.type === 'UPDATE_APP_IMAGE') pending.push(sagas.run('updateAppImageWorker', action)); + }, + setError: message => errors.push(message), + captureError: error => errors.push(error.message), + generateUSDTWallet: () => { throw new Error('Paused wallet generation must not run'); }, + }; + evaluate(selectedCallbacks() + '\nglobalThis.callbacks = {createWallet, updateWallet, deleteWallet};', scope, 'production-existing-usdt-hooks.ts'); + f.callbacks = scope.callbacks; + f.update = async model => { + const result = await f.callbacks.updateWallet(model); + await Promise.all(pending.splice(0)); + return result; + }; + f.wallet = () => plain(f.dbManager.getObjectById(f.RealmSchema.USDTWallet, `usdt-${type}`)); + f.remoteImage = async () => f.image.decodeImage(f.encryption.generateEncryptionKey(f.seed), + await server.snapshot(f.appId), f.appId, () => {}, true); + return f; +} + +for (const type of ['DEFAULT', 'IMPORTED']) { + test(`actual existing ${type} USDT metadata edit commits before HTTP and verifies hidden recovery state`, async t => { + const f = await setup(t, type); + const edited = f.wallet(); + edited.presentationData = { name: 'Saved edit', description: 'Recoverable description', visibility: 'HIDDEN' }; + server.armFault({ beforeMutation() { + assert.equal(f.wallet().presentationData.name, 'Saved edit'); + assert.equal(f.wallet().presentationData.visibility, 'HIDDEN'); + } }); + assert.equal(await f.update(edited), true); + assert.equal(writes().length, 1); + assert.equal(writes()[0].committed, true); + assert.deepEqual(plain((await f.remoteImage()).wallets[edited.id].presentationData), edited.presentationData); + assert.equal(f.store.getState().bhr.pendingAllBackupByAppId[f.appId], false); + }); + + test(`actual existing ${type} USDT deletion commits before HTTP and preserves another wallet`, async t => { + const f = await setup(t, type); + const id = f.wallet().id; + server.armFault({ beforeMutation() { + assert.equal(f.dbManager.getObjectById(f.RealmSchema.USDTWallet, id), undefined); + assert.ok(f.dbManager.getObjectById(f.RealmSchema.USDTWallet, 'retained-usdt')); + } }); + assert.equal(await f.callbacks.deleteWallet(id), true); + assert.equal(writes().length, 1); + assert.equal(writes()[0].committed, true); + const image = await f.remoteImage(); + assert.equal(image.wallets[id], undefined); + assert.ok(image.wallets['retained-usdt']); + assert.equal(f.store.getState().bhr.pendingAllBackupByAppId[f.appId], false); + }); +} + +test('actual USDT hook creation and import stay paused before generation, native writes or HTTP', async t => { + const f = await setup(t); + const before = f.dbManager.getCollection(f.RealmSchema.USDTWallet).length; + for (const type of ['DEFAULT', 'IMPORTED']) { + const result = await f.callbacks.createWallet({ type, name: 'Paused', description: '' }); + assert.equal(result.error, 'USDT wallet setup is paused'); + } + assert.equal(f.dbManager.getCollection(f.RealmSchema.USDTWallet).length, before); + assert.equal(server.trace.length, 0); +}); + +for (const mode of ['409', 'malformed', 'lost-reply']) test(`actual USDT metadata survives ${mode} with pending backup`, async t => { + const f = await setup(t); + const edited = f.wallet(); + edited.presentationData.name = 'Saved despite cloud failure'; + server.armFault({ mode, operation: 'updateAppImage' }); + assert.equal(await f.update(edited), true); + assert.equal(f.wallet().presentationData.name, 'Saved despite cloud failure'); + assert.equal(writes().length, 1); + assert.equal(f.store.getState().bhr.pendingAllBackupByAppId[f.appId], true); + assert.equal(f.transport.captureBackupWriteIntent(f.appId), undefined); +}); + +test('actual USDT deletion survives conflict after its local commit and remains pending', async t => { + const f = await setup(t); + const id = f.wallet().id; + server.armFault({ mode: '409', operation: 'deleteAppImageEntity' }); + assert.equal(await f.callbacks.deleteWallet(id), true); + assert.equal(f.dbManager.getObjectById(f.RealmSchema.USDTWallet, id), undefined); + assert.equal(writes().length, 1); + assert.equal(f.store.getState().bhr.pendingAllBackupByAppId[f.appId], true); + assert.ok((await f.remoteImage()).wallets[id]); +}); + +test('actual USDT cache refresh creates no redundant recovery HTTP writes', async t => { + const f = await setup(t); + const synced = f.wallet(); + synced.specs.balance = 12; + synced.specs.lastSynched = 2; + synced.accountStatus.nextNonce = 1; + assert.equal(await f.update(synced), true); + assert.equal(f.wallet().specs.balance, 12); + assert.equal(server.trace.length, 0); +}); + +test('actual USDT rendered callbacks cannot edit or delete after A to B to A', async t => { + const f = await setup(t); + const edited = f.wallet(); + edited.presentationData.name = 'Old callback'; + await f.selectAccount('disposable-other-account'); + await f.selectAccount(f.appId); + assert.equal(await f.update(edited), false); + assert.equal(await f.callbacks.deleteWallet(edited.id), false); + assert.equal(f.wallet().presentationData.name, 'Existing wallet'); + assert.equal(server.trace.length, 0); +}); diff --git a/tests/recovery/backupFreshness.test.cjs b/tests/recovery/backupFreshness.test.cjs index 73c9cb5a48..beff44513d 100644 --- a/tests/recovery/backupFreshness.test.cjs +++ b/tests/recovery/backupFreshness.test.cjs @@ -259,6 +259,10 @@ for (const network of [NetworkType.MAINNET, NetworkType.TESTNET]) { const added = wallet('later', network, 1); const committedOperation = commitFixtureRecovery(f, realm => realm.create('Wallet', initial)); await f.run('updateAppImageWorker', { payload: { wallets: [initial] }, committedOperation }); + // The unit Relay supplies acknowledgements only. Name the separate mocked + // read-only verification contract before authorizing the next source + // operation; native/HTTP integration owns the actual readback evidence. + f.mockVerifiedBackupBaseline(); assert.equal( await f.run('addNewWalletsWorker', { payload: [{ walletType: WalletType.DEFAULT, walletDetails: { fixture: added } }], From a53985a285c0e9e2d302e5e8a6816fc22c2b11fc Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Tue, 13 Oct 2026 03:52:47 +0000 Subject: [PATCH 164/254] Expect opted-in backup inspection markers to be invalidated --- tests/recovery/backupFreshness.test.cjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/recovery/backupFreshness.test.cjs b/tests/recovery/backupFreshness.test.cjs index beff44513d..3e724851de 100644 --- a/tests/recovery/backupFreshness.test.cjs +++ b/tests/recovery/backupFreshness.test.cjs @@ -410,7 +410,7 @@ for (const enabled of [true, false]) { f.collections.Wallet.push(wallet('old', NetworkType.MAINNET)); assert.equal(await f.run('checkBackupCondition'), !enabled); assert.equal(f.calls.length, 0); - assert.equal(f.state.bhr.backupRepairCompletedByAppId[f.app.id], undefined); + assert.equal(f.state.bhr.backupRepairCompletedByAppId[f.app.id], enabled ? false : undefined); }); } From f0a7eb3227768a9732bde9a579ef2670e756121d Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Tue, 13 Oct 2026 03:52:55 +0000 Subject: [PATCH 165/254] fix: commit recovery producers before queued automatic backups --- src/hooks/useUSDTWallets.ts | 119 +- .../AppSettings/Node/NodeSelection.tsx | 49 +- src/screens/AppSettings/Node/NodeSettings.tsx | 79 +- src/services/electrum/node.tsx | 60 +- src/store/sagas/bhr.ts | 577 +++------ src/store/sagas/wallets.ts | 1046 +++++++---------- tests/recovery/accountBinding.test.cjs | 165 ++- .../deleteResultTruthfulness.test.cjs | 26 +- tests/recovery/freshInstallWallet.test.cjs | 2 + tests/recovery/nodeAccountActions.test.cjs | 54 +- tests/recovery/usdtAccountDelete.test.cjs | 77 +- tests/recovery/usdtBackup.test.cjs | 17 +- tests/services/manualNodeFailover.test.ts | 17 + 13 files changed, 1015 insertions(+), 1273 deletions(-) diff --git a/src/hooks/useUSDTWallets.ts b/src/hooks/useUSDTWallets.ts index 89f867f2be..e9d1a5c556 100644 --- a/src/hooks/useUSDTWallets.ts +++ b/src/hooks/useUSDTWallets.ts @@ -1,5 +1,24 @@ import { useState, useCallback, useMemo } from 'react'; import { useQuery } from '@realm/react'; +import { useDispatch, useStore } from 'react-redux'; +import { useAppSelector } from 'src/store/hooks'; +import { updateAppImage } from 'src/store/sagaActions/bhr'; +import { beginBackupLocalOperation, commitBackupLocalOperation, enqueueAutomaticBackupOperation } from 'src/services/backup/automatic'; +import { canonical, recoveryContent } from 'src/services/backup/image'; +import { captureBackupWriteIntent, currentBackupAccountSession, markBackupMutation } from 'src/services/backup/transport'; +import { + invalidateBackupRepair, + isAutomaticCloudBackupEnabled, + setPendingAllBackup, +} from 'src/store/reducers/bhr'; +import USDT, { + DEFAULT_DEADLINE_SECONDS, + USDTTransferOptions, +} from '../services/wallets/operations/dollars/USDT'; +import { captureError } from '../services/sentry'; +import { RealmSchema } from '../storage/realm/enum'; +import dbManager from '../storage/realm/dbManager'; +import { NetworkType, VisibilityType } from '../services/wallets/enums'; import { generateUSDTWallet, syncUSDTWalletBalance, @@ -11,25 +30,6 @@ import { USDTWalletImportDetails, USDTWalletSupportedNetwork, } from '../services/wallets/factories/USDTWalletFactory'; -import { NetworkType, VisibilityType } from '../services/wallets/enums'; -import dbManager from '../storage/realm/dbManager'; -import { RealmSchema } from '../storage/realm/enum'; -import { captureError } from '../services/sentry'; -import USDT, { - DEFAULT_DEADLINE_SECONDS, - USDTTransferOptions, -} from '../services/wallets/operations/dollars/USDT'; -import { useDispatch, useStore } from 'react-redux'; -import { useAppSelector } from 'src/store/hooks'; -import { updateAppImage } from 'src/store/sagaActions/bhr'; -import Relay from 'src/services/backend/Relay'; -import { canonical, recoveryContent } from 'src/services/backup/image'; -import { captureBackupWriteIntent, currentBackupAccountSession, markBackupMutation } from 'src/services/backup/transport'; -import { - invalidateBackupRepair, - isAutomaticCloudBackupEnabled, - setPendingAllBackup, -} from 'src/store/reducers/bhr'; export interface UseUSDTWalletsOptions { getAll?: boolean; @@ -97,9 +97,7 @@ export const useUSDTWallets = (options: UseUSDTWalletsOptions = {}): UseUSDTWall // Filter hidden wallets if not included if (!includeHidden) { - filteredWallets = filteredWallets.filter((wallet) => { - return wallet.presentationData.visibility !== VisibilityType.HIDDEN; - }); + filteredWallets = filteredWallets.filter((wallet) => wallet.presentationData.visibility !== VisibilityType.HIDDEN); } return filteredWallets; @@ -123,12 +121,14 @@ export const useUSDTWallets = (options: UseUSDTWalletsOptions = {}): UseUSDTWall renderAccountSession === originSession && originSession === currentBackupAccountSession(); let beganLocalWrite = false; + let localOperation; if (USDT_WALLET_CREATION_PAUSED) { return { error: 'USDT wallet setup is paused' }; } try { if (!isCurrentCreate()) throw new Error('Account changed'); + localOperation = beginBackupLocalOperation(appId, originBackupWriteIntent, originSession); setError(null); const walletNetworkType = USDTWalletSupportedNetwork; const allUSDTWallets: USDTWallet[] = (await dbManager.getObjectByIndex( @@ -165,8 +165,10 @@ export const useUSDTWallets = (options: UseUSDTWalletsOptions = {}): UseUSDTWall } beganLocalWrite = true; - if ((await dbManager.createObject(RealmSchema.USDTWallet, newWallet)) !== true) - throw new Error('Failed to create wallet'); + const committedOperation = commitBackupLocalOperation(localOperation, (localRealm) => { + localRealm.create(RealmSchema.USDTWallet, newWallet); + }); + if (!committedOperation.committed) throw new Error('Failed to create wallet'); if (!isCurrentCreate()) throw new Error('Account changed'); // Create usdt wallet backup @@ -174,6 +176,7 @@ export const useUSDTWallets = (options: UseUSDTWalletsOptions = {}): UseUSDTWall ...updateAppImage({ wallets: [newWallet], signers: null, updateNodes: false }, appId), originBackupAccountSession: originSession, originBackupWriteIntent, + committedOperation, }); return { newWallet }; @@ -202,37 +205,29 @@ export const useUSDTWallets = (options: UseUSDTWalletsOptions = {}): UseUSDTWall isOriginCurrent() && renderAccountSession === originSession && originSession === currentBackupAccountSession(); - let remoteDeleted = false; - const markIncompleteDeletion = () => { - if (!remoteDeleted) return; - markBackupMutation(originAppId); - dispatch(invalidateBackupRepair(originAppId)); - dispatch(setPendingAllBackup({ appId: originAppId, pending: true })); - }; try { if (!isCurrentDelete()) return false; - // An account without Assisted Server Backup consent still owns its local - // wallet. Only the opted-in account needs a corresponding relay delete. + const operation = beginBackupLocalOperation(originAppId, originBackupWriteIntent, originSession); + const committedOperation = commitBackupLocalOperation(operation, (localRealm) => { + const stored = localRealm.objectForPrimaryKey(RealmSchema.USDTWallet, walletId); + if (!stored) throw new Error('Wallet deletion target is missing'); + localRealm.delete(stored); + }); + if (!committedOperation.committed) throw new Error('Failed to delete wallet'); if (isAutomaticCloudBackupEnabled(reduxStore.getState().bhr, originAppId)) { - const response = await Relay.deleteAppImageEntity({ - appId: originAppId, - signers: null, - walletIds: [walletId], - }, originBackupWriteIntent); - if (!response.updated) throw new Error('Failed to delete wallet'); - remoteDeleted = true; - } - if (!isCurrentDelete()) { - markIncompleteDeletion(); - return false; + try { + const result = await enqueueAutomaticBackupOperation(committedOperation, [{ + operation: 'deleteAppImageEntity', body: { appId: originAppId, signers: [], walletIds: [walletId] }, + }]); + if (!result.backupConfirmed && isCurrentDelete()) + dispatch(setPendingAllBackup({ appId: originAppId, pending: true })); + } catch { + if (isCurrentDelete()) dispatch(setPendingAllBackup({ appId: originAppId, pending: true })); + } } - const deleted = dbManager.deleteObjectById(RealmSchema.USDTWallet, walletId) === true && - !dbManager.getObjectById(RealmSchema.USDTWallet, walletId); - if (!deleted) markIncompleteDeletion(); - return deleted; + return true; } catch (err) { - markIncompleteDeletion(); - setError(err.message || 'Failed to delete wallet'); + if (isCurrentDelete()) setError(err.message || 'Failed to delete wallet'); captureError(err); return false; } @@ -253,6 +248,7 @@ export const useUSDTWallets = (options: UseUSDTWalletsOptions = {}): UseUSDTWall let recoveryChanged = false; try { if (!isCurrentUpdate()) return false; + const operation = beginBackupLocalOperation(originAppId, originBackupWriteIntent, operationSession); const previous = dbManager.getObjectById(RealmSchema.USDTWallet, wallet.id); if (!previous) throw new Error('Wallet not found'); const previousWallet = previous.toJSON ? previous.toJSON() : previous; @@ -261,11 +257,15 @@ export const useUSDTWallets = (options: UseUSDTWalletsOptions = {}): UseUSDTWall canonical(recoveryContent('wallets', wallet)); const { id, ...walletUpdateData } = wallet; if (!isCurrentUpdate()) throw new Error('Account changed'); - const updated = await dbManager.updateObjectById( - RealmSchema.USDTWallet, - id, - walletUpdateData - ); // Realm updates must omit the primary key. + const committedOperation = recoveryChanged + ? commitBackupLocalOperation(operation, (localRealm) => { + const stored = localRealm.objectForPrimaryKey(RealmSchema.USDTWallet, id); + if (!stored) throw new Error('Wallet not found'); + Object.assign(stored, walletUpdateData); + }) + : undefined; + const updated = recoveryChanged ? committedOperation.committed + : await dbManager.updateObjectById(RealmSchema.USDTWallet, id, walletUpdateData); if (!updated) throw new Error('Failed to update wallet'); if (!isCurrentUpdate()) throw new Error('Account changed'); @@ -276,18 +276,19 @@ export const useUSDTWallets = (options: UseUSDTWalletsOptions = {}): UseUSDTWall ...updateAppImage({ wallets: [wallet], signers: null, updateNodes: false }, originAppId), originBackupAccountSession: operationSession, originBackupWriteIntent, + committedOperation, }); return true; } catch (err) { // The database helper can fail after partially writing fields. Do not // upload the requested state or leave a previous verification trusted. - if (recoveryChanged) { + if (recoveryChanged && isCurrentUpdate()) { markBackupMutation(originAppId); dispatch(invalidateBackupRepair(originAppId)); dispatch(setPendingAllBackup({ appId: originAppId, pending: true })); } - setError(err.message || 'Failed to update wallet'); + if (isCurrentUpdate()) setError(err.message || 'Failed to update wallet'); captureError(err); return false; } @@ -359,9 +360,7 @@ export const useUSDTWallets = (options: UseUSDTWalletsOptions = {}): UseUSDTWall * Get wallet by ID */ const getWalletById = useCallback( - (walletId: string): USDTWallet | null => { - return usdtWallets.find((wallet) => wallet.id === walletId) || null; - }, + (walletId: string): USDTWallet | null => usdtWallets.find((wallet) => wallet.id === walletId) || null, [usdtWallets] ); diff --git a/src/screens/AppSettings/Node/NodeSelection.tsx b/src/screens/AppSettings/Node/NodeSelection.tsx index 5e3cea8b14..1112eb417d 100644 --- a/src/screens/AppSettings/Node/NodeSelection.tsx +++ b/src/screens/AppSettings/Node/NodeSelection.tsx @@ -4,7 +4,6 @@ import { StyleSheet } from 'react-native'; import ScreenWrapper from 'src/components/ScreenWrapper'; import TabBar from 'src/components/TabBar'; import { hp, wp } from 'src/constants/responsive'; -import AddNode from './AddNode'; import Node from 'src/services/electrum/node'; import { NodeDetail } from 'src/services/wallets/interfaces'; import useToastMessage from 'src/hooks/useToastMessage'; @@ -24,7 +23,6 @@ import { predefinedMainnetNodes, predefinedTestnetNodes, } from 'src/services/electrum/predefinedNodes'; -import SelectableServerItem from './components/SelectableServerItem'; import ActivityIndicatorView from 'src/components/AppActivityIndicator/ActivityIndicatorView'; import { CommonActions, useNavigation } from '@react-navigation/native'; import { LocalizationContext } from 'src/context/Localization/LocContext'; @@ -33,8 +31,11 @@ import { updateAppImage } from 'src/store/sagaActions/bhr'; import { useAppSelector } from 'src/store/hooks'; import WalletHeader from 'src/components/WalletHeader'; import { captureBackupWriteIntent, currentBackupAccountSession } from 'src/services/backup/transport'; +import { beginBackupLocalOperation } from 'src/services/backup/automatic'; +import SelectableServerItem from './components/SelectableServerItem'; +import AddNode from './AddNode'; -const PrivateElectrum = ({ host, port, useSSL, setHost, setPort, setUseSSL, connectionError }) => { +function PrivateElectrum({ host, port, useSSL, setHost, setPort, setUseSSL, connectionError }) { return ( ); -}; +} -const PublicServer = ({ currentlySelectedNode, handleSelectNode }) => { +function PublicServer({ currentlySelectedNode, handleSelectNode }) { const { bitcoinNetworkType } = useAppSelector((state) => state.settings); + const predefinedNodes = bitcoinNetworkType === NetworkType.TESTNET ? predefinedTestnetNodes : predefinedMainnetNodes; return ( @@ -66,9 +68,9 @@ const PublicServer = ({ currentlySelectedNode, handleSelectNode }) => { ))} ); -}; +} -const NodeSelection = () => { +function NodeSelection() { const dispatch = useDispatch(); const navigation = useNavigation(); const { colorMode } = useColorMode(); @@ -89,6 +91,8 @@ const NodeSelection = () => { const tabsData = [{ label: settings.publicServer }, { label: settings.privateElectrum }]; const { bitcoinNetworkType } = useAppSelector((state) => state.settings); + const renderedAppId = useAppSelector((state) => state.storage.appId); + const renderedAccountSession = currentBackupAccountSession(); const nodes: NodeDetail[] = Node.getAllNodes(); @@ -97,10 +101,12 @@ const NodeSelection = () => { }, [nodes.length]); const onSaveCallback = async (nodeDetail: NodeDetail) => { - const originAppId = Node.currentAccountId(); - if (!originAppId) return; - const originBackupAccountSession = currentBackupAccountSession(); + const originAppId = renderedAppId; + const originBackupAccountSession = renderedAccountSession; + if (!originAppId || !Node.isAccountActive(originAppId) || + currentBackupAccountSession() !== originBackupAccountSession) return; const originBackupWriteIntent = captureBackupWriteIntent(originAppId); + const operation = beginBackupLocalOperation(originAppId, originBackupWriteIntent, originBackupAccountSession); setSaveLoading(true); // Sanitize host @@ -115,18 +121,19 @@ const NodeSelection = () => { } const nodeToSave = { ...nodeDetail }; - const { saved, connectionError: nodeConnectionError } = await Node.save(nodeToSave, nodeList, originAppId); - if (!Node.isAccountActive(originAppId)) { + const { saved, committedOperation, connectionError: nodeConnectionError } = await Node.save(nodeToSave, nodeList, originAppId, operation); + if (!Node.isAccountActive(originAppId) || currentBackupAccountSession() !== originBackupAccountSession) { setSaveLoading(false); return; } if (saved) { const updatedNodeList = Node.getAllNodes(); setNodeList(updatedNodeList); - dispatch({ + if (committedOperation) dispatch({ ...updateAppImage({ wallets: null, signers: null, updateNodes: true }, originAppId), originBackupAccountSession, originBackupWriteIntent, + committedOperation, }); const newNode = updatedNodeList.find( (node) => node.host === nodeToSave.host && node.port === nodeToSave.port @@ -157,8 +164,11 @@ const NodeSelection = () => { }; const onConnectToNode = async (selectedNode: NodeDetail) => { - const originAppId = Node.currentAccountId(); - if (!originAppId) return; + const originAppId = renderedAppId; + const originBackupAccountSession = renderedAccountSession; + const isCurrent = () => Node.isAccountActive(originAppId) && + currentBackupAccountSession() === originBackupAccountSession; + if (!originAppId || !isCurrent()) return; let nodes = Node.getAllNodes(); if ( currentlySelectedNode && @@ -167,7 +177,7 @@ const NodeSelection = () => { ) { // disconnect currently selected node(if connected) await Node.disconnect(currentlySelectedNode); - if (!Node.isAccountActive(originAppId)) return; + if (!isCurrent()) return; currentlySelectedNode.isConnected = false; Node.update(currentlySelectedNode, { isConnected: currentlySelectedNode.isConnected }); @@ -183,8 +193,8 @@ const NodeSelection = () => { setLoading(true); const node = { ...selectedNode }; - const { connected, connectedTo, error } = await Node.connectToSelectedNode(node); - if (!Node.isAccountActive(originAppId)) { + const { connected, connectedTo, error } = await Node.connectToSelectedNode(node, originAppId, originBackupAccountSession); + if (!isCurrent()) { setLoading(false); return; } @@ -192,6 +202,7 @@ const NodeSelection = () => { if (connected) { node.isConnected = connected; await Node.update(node, { isConnected: connected }); + if (!isCurrent()) return; dispatch(electrumClientConnectionExecuted({ successful: node.isConnected, connectedTo })); showToast(`${errorTranslations.ConnectedTo} ${connectedTo}`, ); nodes = nodes.map((item) => { @@ -325,7 +336,7 @@ const NodeSelection = () => { ); -}; +} const styles = StyleSheet.create({ tabBarContainer: { diff --git a/src/screens/AppSettings/Node/NodeSettings.tsx b/src/screens/AppSettings/Node/NodeSettings.tsx index c8eed58a07..12a7473804 100644 --- a/src/screens/AppSettings/Node/NodeSettings.tsx +++ b/src/screens/AppSettings/Node/NodeSettings.tsx @@ -20,13 +20,15 @@ import DowngradeToPlebDark from 'src/assets/images/downgradetoplebDark.svg'; import Buttons from 'src/components/Buttons'; import EmptyListIllustration from 'src/components/EmptyListIllustration'; import { CommonActions, useFocusEffect, useNavigation } from '@react-navigation/native'; -import ServerItem from './components/ServerItem'; import ActivityIndicatorView from 'src/components/AppActivityIndicator/ActivityIndicatorView'; import { updateAppImage } from 'src/store/sagaActions/bhr'; import { ELECTRUM_CLIENT } from 'src/services/electrum/client'; import WalletHeader from 'src/components/WalletHeader'; import Text from 'src/components/KeeperText'; import { captureBackupWriteIntent, currentBackupAccountSession } from 'src/services/backup/transport'; +import { beginBackupLocalOperation, commitBackupLocalOperation } from 'src/services/backup/automatic'; +import { RealmSchema } from 'src/storage/realm/enum'; +import ServerItem from './components/ServerItem'; function ElectrumDisconnectWarningContent() { const { colorMode } = useColorMode(); @@ -40,7 +42,7 @@ function ElectrumDisconnectWarningContent() { ); } -type SelectedNodeAction = { node: NodeDetail; originAppId: string }; +type SelectedNodeAction = { node: NodeDetail; originAppId: string; originBackupAccountSession: number }; function NodeSettings() { const { colorMode } = useColorMode(); @@ -52,6 +54,7 @@ function NodeSettings() { const [nodeList, setNodeList] = useState([]); const [nodeListAppId, setNodeListAppId] = useState(null); + const [nodeListAccountSession, setNodeListAccountSession] = useState(null); const [loading, setLoading] = useState(false); const [electrumDisconnectWarningVisible, setElectrumDisconnectWarningVisible] = useState(false); const [nodeToDisconnect, setNodeToDisconnect] = useState(null); @@ -64,49 +67,62 @@ function NodeSettings() { useCallback(() => { const appId = Node.currentAccountId(); setNodeListAppId(appId ?? null); + setNodeListAccountSession(currentBackupAccountSession()); setNodeList(appId ? Node.getAllNodes() : []); }, []) ); - const onDelete = async (selectedItem: NodeDetail, originAppId = nodeListAppId) => { - if (!originAppId || !Node.isAccountActive(originAppId)) return; - const originBackupAccountSession = currentBackupAccountSession(); + const onDelete = async (selectedItem: NodeDetail, originAppId = nodeListAppId, + originBackupAccountSession = nodeListAccountSession) => { + if (!originAppId || originBackupAccountSession === null || !Node.isAccountActive(originAppId) || + currentBackupAccountSession() !== originBackupAccountSession) return; const originBackupWriteIntent = captureBackupWriteIntent(originAppId); + const operation = beginBackupLocalOperation(originAppId, originBackupWriteIntent, originBackupAccountSession); const isConnected = Node.nodeConnectionStatus(selectedItem); if (isConnected) await Node.disconnect(selectedItem); - if (!Node.isAccountActive(originAppId)) return; + if (!Node.isAccountActive(originAppId) || currentBackupAccountSession() !== originBackupAccountSession) return; - const status = Node.delete(selectedItem); - if (status) { + const committedOperation = commitBackupLocalOperation(operation, (localRealm) => { + const saved = localRealm.objects(RealmSchema.NodeConnect) + .filtered('id == $0 AND networkType == $1', selectedItem.id, selectedItem.networkType)[0]; + if (!saved) throw new Error('Node deletion target is missing'); + localRealm.delete(saved); + }); + if (committedOperation.committed) { const updatedNodes = Node.getAllNodes(); setNodeList(updatedNodes); dispatch({ ...updateAppImage({ wallets: null, signers: null, updateNodes: true }, originAppId), originBackupAccountSession, originBackupWriteIntent, + committedOperation, }); } }; const onConnectToNode = async (selectedNode: NodeDetail) => { const originAppId = nodeListAppId; - if (!originAppId || !Node.isAccountActive(originAppId)) return; + const originBackupAccountSession = nodeListAccountSession; + const isCurrent = () => Node.isAccountActive(originAppId) && + currentBackupAccountSession() === originBackupAccountSession; + if (!originAppId || originBackupAccountSession === null || !isCurrent()) return; dispatch(electrumClientConnectionInitiated()); setLoading(true); try { const currentlySelectedNode = Node.getAllNodes().find((node) => node.isConnected); if (currentlySelectedNode && currentlySelectedNode.id !== selectedNode.id) { Node.disconnect(currentlySelectedNode); - if (!Node.isAccountActive(originAppId)) return; + if (!isCurrent()) return; Node.update(currentlySelectedNode, { isConnected: false }); } const { connected, connectedTo, error } = await Node.connectToSelectedNode( selectedNode, - originAppId + originAppId, + originBackupAccountSession ); - if (!Node.isAccountActive(originAppId)) return; + if (!isCurrent()) return; if (connected) { Node.update(selectedNode, { isConnected: true }); dispatch(electrumClientConnectionExecuted({ successful: true, connectedTo })); @@ -115,7 +131,7 @@ function NodeSettings() { dispatch(electrumClientConnectionExecuted({ successful: false, error })); } } catch (error) { - if (Node.isAccountActive(originAppId)) { + if (isCurrent()) { dispatch( electrumClientConnectionExecuted({ successful: false, @@ -124,17 +140,22 @@ function NodeSettings() { ); } } finally { - setLoading(false); - if (Node.isAccountActive(originAppId)) setNodeList(Node.getAllNodes()); + if (isCurrent()) { + setLoading(false); + setNodeList(Node.getAllNodes()); + } } }; - const onDisconnectToNode = async (selectedNode: NodeDetail, originAppId: string) => { - if (!Node.isAccountActive(originAppId)) return; + const onDisconnectToNode = async (selectedNode: NodeDetail, originAppId: string, + originBackupAccountSession = nodeListAccountSession) => { + const isCurrent = () => Node.isAccountActive(originAppId) && + currentBackupAccountSession() === originBackupAccountSession; + if (!isCurrent()) return; try { setLoading(true); Node.disconnect(selectedNode); - if (!Node.isAccountActive(originAppId)) return; + if (!isCurrent()) return; Node.update(selectedNode, { isConnected: false }); dispatch( electrumClientConnectionExecuted({ @@ -145,12 +166,14 @@ function NodeSettings() { showToast(`${errorString.disconnectedFrom} ${selectedNode.host}`, ); } catch (error) { console.error('Error disconnecting electrum client', error); - if (Node.isAccountActive(originAppId)) { + if (isCurrent()) { showToast(errorString.failedToDiConnect, ); } } finally { - setLoading(false); - if (Node.isAccountActive(originAppId)) setNodeList(Node.getAllNodes()); + if (isCurrent()) { + setLoading(false); + setNodeList(Node.getAllNodes()); + } } }; @@ -173,10 +196,12 @@ function NodeSettings() { onDelete={onDelete} onConnectToNode={onConnectToNode} setNodeToDelete={(node: NodeDetail) => { - if (nodeListAppId) setNodeToDelete({ node, originAppId: nodeListAppId }); + if (nodeListAppId && nodeListAccountSession !== null) + setNodeToDelete({ node, originAppId: nodeListAppId, originBackupAccountSession: nodeListAccountSession }); }} setNodeToDisconnect={(node: NodeDetail) => { - if (nodeListAppId) setNodeToDisconnect({ node, originAppId: nodeListAppId }); + if (nodeListAppId && nodeListAccountSession !== null) + setNodeToDisconnect({ node, originAppId: nodeListAppId, originBackupAccountSession: nodeListAccountSession }); }} setElectrumDisconnectWarningVisible={setElectrumDisconnectWarningVisible} /> @@ -190,11 +215,11 @@ function NodeSettings() { {isNoNodeConnected && ( - {'Currently in Offline Mode'} + Currently in Offline Mode )} @@ -221,10 +246,10 @@ function NodeSettings() { buttonCallback={async () => { setElectrumDisconnectWarningVisible(false); if (nodeToDisconnect) { - await onDisconnectToNode(nodeToDisconnect.node, nodeToDisconnect.originAppId); + await onDisconnectToNode(nodeToDisconnect.node, nodeToDisconnect.originAppId, nodeToDisconnect.originBackupAccountSession); setNodeToDisconnect(null); } else if (nodeToDelete) { - await onDelete(nodeToDelete.node, nodeToDelete.originAppId); + await onDelete(nodeToDelete.node, nodeToDelete.originAppId, nodeToDelete.originBackupAccountSession); setNodeToDelete(null); } }} diff --git a/src/services/electrum/node.tsx b/src/services/electrum/node.tsx index 6ad1982759..24e7e8486a 100644 --- a/src/services/electrum/node.tsx +++ b/src/services/electrum/node.tsx @@ -2,13 +2,16 @@ import ElectrumClient from 'src/services/electrum/client'; import { NodeDetail } from 'src/services/wallets/interfaces'; import dbManager from 'src/storage/realm/dbManager'; import { RealmSchema } from 'src/storage/realm/enum'; -import { NetworkType } from '../wallets/enums'; -import { predefinedMainnetNodes, predefinedTestnetNodes } from './predefinedNodes'; import { store } from 'src/store/store'; +import { commitBackupLocalOperation } from 'src/services/backup/automatic'; +import type { BackupLocalOperation } from 'src/services/backup/automatic'; +import { currentBackupAccountSession } from 'src/services/backup/transport'; +import { predefinedMainnetNodes, predefinedTestnetNodes } from './predefinedNodes'; +import { NetworkType } from '../wallets/enums'; export default class Node { public static currentAccountId(): string | undefined { - const appId = store.getState().storage.appId; + const {appId} = store.getState().storage; return this.isAccountActive(appId) ? appId : undefined; } @@ -21,7 +24,9 @@ export default class Node { } } - public static async save(nodeDetail: NodeDetail, nodeList: NodeDetail[], originAppId = this.currentAccountId()) { + public static async save(nodeDetail: NodeDetail, _nodeList: NodeDetail[], originAppId?: string, backupOperation?: BackupLocalOperation) { + originAppId ??= this.currentAccountId(); + const accountSession = backupOperation?.accountSession ?? currentBackupAccountSession(); if (!this.isAccountActive(originAppId)) return { saved: false, connectionError: 'Account changed' }; const { bitcoinNetworkType } = store.getState().settings; @@ -35,7 +40,7 @@ export default class Node { if (!isConnectable) { return { saved: false, connectionError }; } - if (!this.isAccountActive(originAppId)) + if (!this.isAccountActive(originAppId) || accountSession !== currentBackupAccountSession()) return { saved: false, connectionError: 'Account changed' }; const node = { ...nodeDetail }; @@ -58,29 +63,27 @@ export default class Node { savedNode.id === node.id || (savedNode.host === node.host && savedNode.port === node.port) ); - if (isPredefinedNode && !isSavedNode) { - if (!node.id) { - node.id = nodeList.length + 1001; - } - dbManager.createObject(RealmSchema.NodeConnect, node); - return { saved: true }; - } - + if (!node.id && isExistingNode) return { saved: true }; if (!node.id) { - if (isExistingNode) { - return { saved: true }; - } - // case: save new node (first 1000 IDs reserved to predefined nodes) - node.id = nodeList.length + 1001; - dbManager.createObject(RealmSchema.NodeConnect, node); - } else { - // case: update existing node - dbManager.updateObjectById(RealmSchema.NodeConnect, node.id.toString(), { - ...node, + // IDs cover both networks because the recoverable node map is account-wide. + const savedNodes = Array.from(dbManager.getCollection(RealmSchema.NodeConnect)) as unknown as NodeDetail[]; + node.id = Math.max(1000, ...savedNodes.map((saved) => saved.id)) + 1; + } + if (backupOperation) { + const committedOperation = commitBackupLocalOperation(backupOperation, (localRealm) => { + // NodeConnect has no Realm primary key; use its stable id and network. + const saved = localRealm.objects(RealmSchema.NodeConnect) + .filtered('id == $0 AND networkType == $1', node.id, node.networkType)[0]; + if (saved) Object.assign(saved, node); + else localRealm.create(RealmSchema.NodeConnect, node); }); + return { saved: committedOperation.committed, committedOperation, + connectionError: committedOperation.committed ? undefined : 'Node changes could not be saved' }; } - - return { saved: true }; + const saved = (isPredefinedNode && !isSavedNode) || !allNodes.some((savedNode) => savedNode.id === node.id) + ? dbManager.createObject(RealmSchema.NodeConnect, node) + : dbManager.updateObjectById(RealmSchema.NodeConnect, node.id.toString(), node); + return { saved: saved === true }; } public static update(nodeDetail: NodeDetail, propsToUpdate: any) { @@ -110,9 +113,10 @@ export default class Node { public static async connectToSelectedNode( selectedNode: NodeDetail, - originAppId = this.currentAccountId() + originAppId = this.currentAccountId(), + originBackupAccountSession = currentBackupAccountSession() ) { - if (!this.isAccountActive(originAppId)) { + if (!this.isAccountActive(originAppId) || currentBackupAccountSession() !== originBackupAccountSession) { return { connected: false, connectedTo: undefined, error: 'Account changed' }; } // The tapped node must connect on its own; only then enable eligible saved @@ -121,7 +125,7 @@ export default class Node { const { connected, connectedTo, error, generation } = await ElectrumClient.connect(); // The async connection may finish after another account has opened Realm. // Never read its nodes or equip them as peers for the old connection. - if (!this.isAccountActive(originAppId)) { + if (!this.isAccountActive(originAppId) || currentBackupAccountSession() !== originBackupAccountSession) { return { connected: false, connectedTo: undefined, error: 'Account changed' }; } if (connected) { diff --git a/src/store/sagas/bhr.ts b/src/store/sagas/bhr.ts index 92627903d6..0fd5a853ba 100644 --- a/src/store/sagas/bhr.ts +++ b/src/store/sagas/bhr.ts @@ -6,9 +6,13 @@ import { backupRevision, captureBackupWriteIntent, currentBackupAccountSession, + backupConsentEpoch, + protectBackupConsentCompletion, markBackupMutation, } from 'src/services/backup/transport'; import type { BackupWriteIntent } from 'src/services/backup/intent'; +import { beginBackupLocalOperation, commitBackupLocalOperation, enqueueAutomaticBackupOperation, beginBackupDeleteOperation, isBackupVerificationCurrent, isAutomaticBackupResultCurrent } from 'src/services/backup/automatic'; +import type { BackupLocalCommit } from 'src/services/backup/automatic'; import * as bip39 from 'bip39'; import { Wallet } from 'src/services/wallets/interfaces/wallet'; import { call, put, select, takeMaybe, takeEvery } from 'redux-saga/effects'; @@ -48,6 +52,14 @@ import CloudBackupModule from 'src/nativemodules/CloudBackup'; import { generateOutputDescriptors } from 'src/utils/service-utilities/utils'; import { hcStatusType } from 'src/models/interfaces/HeathCheckTypes'; import { getJSONFromRealmObject } from 'src/storage/realm/utils'; +import { setupRecoveryKeySigningKey } from 'src/hardware/signerSetup'; +import { + getKeyUID, + sanitizeSeedKeyForBackup, + sanitizeVaultSignersForSeedKeyBackup, +} from 'src/utils/utilities'; +import NetInfo from '@react-native-community/netinfo'; +import { USDTWallet } from 'src/services/wallets/factories/USDTWalletFactory'; import { autoSyncWallets, updateSignerDetails } from '../sagaActions/wallets'; import { createWatcher } from '../utilities'; import { @@ -92,18 +104,10 @@ import { uaiActioned, uaiChecks } from '../sagaActions/uai'; import { setAppCreated, setAppId, setDefaultWalletCreated } from '../reducers/storage'; import { applyUpgradeSequence, KEY_MANAGEMENT_VERSION } from './upgrade'; import { RootState, store } from '../store'; -import { setupRecoveryKeySigningKey } from 'src/hardware/signerSetup'; import { addNewWalletsWorker, addSigningDeviceWorker, NewWalletInfo } from './wallets'; -import { - getKeyUID, - sanitizeSeedKeyForBackup, - sanitizeVaultSignersForSeedKeyBackup, -} from 'src/utils/utilities'; -import NetInfo from '@react-native-community/netinfo'; import { addToUaiStackWorker, uaiActionedWorker } from './uai'; import { addAccount, saveDefaultWalletState, setRecoveryKeyStatus } from '../reducers/account'; import { loadConciergeTickets, loadConciergeUser } from '../reducers/concierge'; -import { USDTWallet } from 'src/services/wallets/factories/USDTWalletFactory'; function currentBackupAppId(): string | undefined { try { @@ -114,376 +118,181 @@ function currentBackupAppId(): string | undefined { } } -// An in-flight request can complete after the selected account cycles A -> B -> A. -// Its outcome cannot be attributed to the new session, so require readback. -function* flagUncertainBackupOutcome(appId: string) { - markBackupMutation(appId); - yield put(invalidateBackupRepair(appId)); +// Builders read committed Realm records. Action objects are used only to select +// IDs, never as the recovery payload sent to the server. +export function buildAutomaticAppMutation( + payload: { wallets?: (Wallet | USDTWallet)[]; signers?: Signer[]; updateNodes?: boolean }, + appId: string +) { + if (currentBackupAppId() !== appId) throw new Error('Backup account changed'); + const app = getJSONFromRealmObject(dbManager.getObjectByIndex(RealmSchema.KeeperApp)) as unknown as KeeperApp; + const encryptionKey = generateEncryptionKey(app.primarySeed); + const walletsObject: Record = {}; + const signersObject: Record = {}; + for (const selected of payload.wallets || []) { + const schema = selected.entityKind === EntityKind.USDT_WALLET ? RealmSchema.USDTWallet : RealmSchema.Wallet; + const stored = dbManager.getObjectById(schema, selected.id); + if (!stored) throw new Error('Committed wallet is missing'); + walletsObject[selected.id] = encrypt(encryptionKey, JSON.stringify(getJSONFromRealmObject(stored))); + } + for (const selected of payload.signers || []) { + const signerId = getKeyUID(selected); + const stored = dbManager.getObjectByPrimaryId(RealmSchema.Signer, 'id', signerId); + if (!stored) throw new Error('Committed signer is missing'); + signersObject[signerId] = encrypt(encryptionKey, + JSON.stringify(sanitizeSeedKeyForBackup(getJSONFromRealmObject(stored) as unknown as Signer))); + } + const nodes = payload.updateNodes + ? Array.from(dbManager.getCollection(RealmSchema.NodeConnect)).map((node: any) => + encrypt(encryptionKey, JSON.stringify({ ...(node.toJSON ? getJSONFromRealmObject(node) : node), isConnected: false }))) + : []; + return { operation: 'updateAppImage' as const, body: { + appId, publicId: app.publicId, walletsObject, signersObject, + networkType: app.networkType, subscription: JSON.stringify(app.subscription), + version: app.version, nodes, replaceNodes: !!payload.updateNodes, + } }; +} + +export function buildAutomaticVaultMutation(vaultId: string, appId: string, isUpdate = true) { + if (currentBackupAppId() !== appId) throw new Error('Backup account changed'); + const app = getJSONFromRealmObject(dbManager.getObjectByIndex(RealmSchema.KeeperApp)) as unknown as KeeperApp; + const stored = dbManager.getObjectById(RealmSchema.Vault, vaultId); + if (!stored) throw new Error('Committed vault is missing'); + const vault = getJSONFromRealmObject(stored) as unknown as Vault; + return { operation: 'updateVaultImage' as const, body: { + appId, appID: appId, vaultId, isUpdate, + vault: encrypt(generateEncryptionKey(app.primarySeed), + JSON.stringify(sanitizeVaultSignersForSeedKeyBackup(vault))), + signersData: vault.signers.map((signer) => ({ + signerId: getKeyUID(signer), xfpHash: hash256(signer.masterFingerprint), + })), + isArchived: !!vault.archived, subscription: JSON.stringify(app.subscription), + } }; +} + +export function* queueCommittedBackupWorker( + appId: string, + committedOperation: BackupLocalCommit | undefined, + mutations: Parameters[1], + requestSession: number +) { + if (currentBackupAppId() !== appId || requestSession !== currentBackupAccountSession()) + return { updated: false, backupPending: true, error: 'Backup account changed' }; + if (committedOperation?.committed === false) + return { updated: false, backupPending: true, error: 'Local recovery operation failed' }; + const enabled = yield select((state: RootState) => isAutomaticCloudBackupEnabled(state.bhr, appId)); + if (!enabled) return { updated: committedOperation?.committed === true, + backupConfirmed: false, backupPending: false, error: '' }; yield put(setPendingAllBackup({ appId, pending: true })); + if (!committedOperation) + return { updated: false, localCommitted: false, backupConfirmed: false, + backupPending: true, error: 'Committed local operation is missing' }; + try { + const result = yield call(enqueueAutomaticBackupOperation, committedOperation, mutations); + if (currentBackupAppId() !== appId || requestSession !== currentBackupAccountSession()) + return { updated: false, localCommitted: committedOperation.committed, + backupConfirmed: false, backupPending: true, error: 'Backup account changed' }; + // Cloud confirmation is separate from the successful local commit. A + // transport failure never rolls back or prevents the user's local change. + return { ...result, updated: committedOperation.committed, error: '' }; + } catch { + if (currentBackupAppId() !== appId || requestSession !== currentBackupAccountSession()) + return { updated: false, localCommitted: committedOperation.committed, + backupConfirmed: false, backupPending: true, error: 'Backup account changed' }; + yield put(setPendingAllBackup({ appId, pending: true })); + return { updated: committedOperation.committed, backupConfirmed: false, backupPending: true, error: '' }; + } } -export function* updateAppImageWorker({ - payload, - originAppId, - originBackupAccountSession, - originBackupWriteIntent, -}: { +export function* updateAppImageWorker({ payload, originAppId, originBackupAccountSession, committedOperation }: { originAppId?: string; originBackupAccountSession?: number; originBackupWriteIntent?: BackupWriteIntent; - payload: { - wallets?: Wallet[] | USDTWallet[]; - signers?: Signer[]; - updateNodes?: boolean; - }; + committedOperation?: BackupLocalCommit; + payload: { wallets?: Wallet[] | USDTWallet[]; signers?: Signer[]; updateNodes?: boolean }; }) { const appId = originAppId || currentBackupAppId(); const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); - const accountIsCurrent = () => - currentBackupAppId() === appId && currentBackupAccountSession() === requestSession; - if (!appId || !accountIsCurrent()) + if (!appId || currentBackupAppId() !== appId || requestSession !== currentBackupAccountSession()) return { updated: false, error: 'Backup account changed' }; try { - const backupResponse = yield call(checkBackupCondition, appId); - if (!accountIsCurrent()) - return { updated: false, error: 'Backup account changed' }; - if (backupResponse) return { updated: true, error: '' }; - - const { wallets, signers, updateNodes } = payload; - const { primarySeed, id, publicId, subscription, networkType, version }: KeeperApp = yield call( - dbManager.getObjectByIndex, - RealmSchema.KeeperApp - ); - if (id !== appId) return { updated: false, error: 'Backup account changed' }; - const walletsObject = {}; - const signersObject = {}; - const nodesList = []; - const encryptionKey = generateEncryptionKey(primarySeed); - if (wallets) { - for (const wallet of wallets) { - const encrytedWallet = encrypt(encryptionKey, JSON.stringify(wallet)); - walletsObject[wallet.id] = encrytedWallet; - } - } - if (signers) { - for (const signer of signers) { - const encrytedSigner = encrypt( - encryptionKey, - JSON.stringify(sanitizeSeedKeyForBackup(signer)) - ); - signersObject[getKeyUID(signer)] = encrytedSigner; - } - } - if (updateNodes) { - const nodes: NodeDetail[] = yield call(dbManager.getCollection, RealmSchema.NodeConnect); - if (nodes && nodes.length > 0) { - for (const index in nodes) { - const node = { ...nodes[index], isConnected: false }; - const encrytedNode = encrypt(encryptionKey, JSON.stringify(node)); - nodesList.push(encrytedNode); - } - } - } - - if (!accountIsCurrent()) - return { updated: false, error: 'Backup account changed' }; - - const response = yield call(Relay.updateAppImage, { - appId: id, - publicId, - walletsObject, - signersObject, - networkType, - subscription: JSON.stringify(subscription), - version, - nodes: nodesList, - replaceNodes: !!updateNodes, - }, originBackupWriteIntent); - if (!accountIsCurrent()) { - yield call(flagUncertainBackupOutcome, appId); - return { updated: false, error: 'Backup account changed' }; - } - if (!response?.updated) yield call(setServerBackupFailed, appId); - return response; - } catch (err) { - console.log({ err }); - console.error('App image update failed', err); - if (!accountIsCurrent()) { - yield call(flagUncertainBackupOutcome, appId); - return { updated: false, error: 'Backup account changed' }; - } - yield call(setServerBackupFailed, appId); - // Local wallet/key changes may continue offline; the relay write is still - // unverified and the account-scoped pending flag remains set. - return { updated: true, backupConfirmed: false, backupPending: true, error: '' }; + return yield call(queueCommittedBackupWorker, appId, committedOperation, + [buildAutomaticAppMutation(payload, appId)], requestSession); + } catch { + yield put(setPendingAllBackup({ appId, pending: true })); + return { updated: !!committedOperation?.committed, backupConfirmed: false, backupPending: true, error: 'Committed app image unavailable' }; } } -export function* updateVaultImageWorker({ - payload, - originAppId, - originBackupAccountSession, - originBackupWriteIntent, -}: { +export function* updateVaultImageWorker({ payload, originAppId, originBackupAccountSession, committedOperation }: { originAppId?: string; originBackupAccountSession?: number; originBackupWriteIntent?: BackupWriteIntent; - payload: { - vault: Vault; - archiveVaultId?: string; - isUpdate?: boolean; - }; + committedOperation?: BackupLocalCommit; + payload: { vault: Vault; archiveVaultId?: string; isUpdate?: boolean }; }) { const appId = originAppId || currentBackupAppId(); const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); - const accountIsCurrent = () => - currentBackupAppId() === appId && currentBackupAccountSession() === requestSession; - if (!appId || !accountIsCurrent()) - return { updated: false, error: 'Backup account changed' }; - const backupResponse = yield call(checkBackupCondition, appId); - if (!accountIsCurrent()) - return { updated: false, error: 'Backup account changed' }; - if (backupResponse) return { updated: true, error: '' }; - - const { vault, archiveVaultId, isUpdate } = payload; - const { primarySeed, id, subscription }: KeeperApp = yield call( - dbManager.getObjectByIndex, - RealmSchema.KeeperApp - ); - if (id !== appId) return { updated: false, error: 'Backup account changed' }; - const encryptionKey = generateEncryptionKey(primarySeed); - const vaultEncrypted = encrypt( - encryptionKey, - JSON.stringify(sanitizeVaultSignersForSeedKeyBackup(vault)) - ); - - const signersData: Array<{ - signerId: string; - xfpHash: string; - }> = []; - for (const signer of vault.signers) { - signersData.push({ - // TODO: Upate relay to use KeyUID - signerId: getKeyUID(signer), - xfpHash: hash256(signer.masterFingerprint), - }); - } - if (!accountIsCurrent()) + if (!appId || currentBackupAppId() !== appId || requestSession !== currentBackupAccountSession()) return { updated: false, error: 'Backup account changed' }; - - if (isUpdate) { - try { - const response = yield call(Relay.updateVaultImage, { - isUpdate, - signersData, - appId: id, - vaultId: vault.id, - vault: vaultEncrypted, - isArchived: !!vault.archived, - }, originBackupWriteIntent); - if (!accountIsCurrent()) { - yield call(flagUncertainBackupOutcome, appId); - return { updated: false, error: 'Backup account changed' }; - } - return response; - } catch (err) { - if (!accountIsCurrent()) { - yield call(flagUncertainBackupOutcome, appId); - return { updated: false, error: 'Backup account changed' }; - } - throw err; - } - } - - // TODO to be removed - const subscriptionStrings = JSON.stringify(subscription); - try { - const response = yield call(Relay.updateVaultImage, { - // The v2 Relay route requires appId even when the legacy appID alias is - // retained for older request builders. Both must identify this account. - appId: id, - appID: id, - vaultId: vault.id, - signersData, - vault: vaultEncrypted, - isArchived: !!vault.archived, - subscription: subscriptionStrings, - ...(archiveVaultId && { archiveVaultId }), - }, originBackupWriteIntent); - if (!accountIsCurrent()) { - yield call(flagUncertainBackupOutcome, appId); - return { updated: false, error: 'Backup account changed' }; - } - return response; - } catch (err) { - captureError(err); - if (!accountIsCurrent()) { - yield call(flagUncertainBackupOutcome, appId); - return { updated: false, error: 'Backup account changed' }; - } - yield call(setServerBackupFailed, appId); - // Keep local vault editing available while making the failed backup visible. - return { updated: true, backupConfirmed: false, backupPending: true, error: '' }; + return yield call(queueCommittedBackupWorker, appId, committedOperation, + [buildAutomaticVaultMutation(payload.vault.id, appId, payload.isUpdate)], requestSession); + } catch { + yield put(setPendingAllBackup({ appId, pending: true })); + return { updated: !!committedOperation?.committed, backupConfirmed: false, backupPending: true, error: 'Committed vault image unavailable' }; } } -// TODO: Other functions here only handle Relay backup, but this also updates local DB, should move that part out -export function* deleteAppImageEntityWorker({ - payload, - originAppId, - originBackupAccountSession, - originBackupWriteIntent, -}: { +export function* deleteAppImageEntityWorker({ payload, originAppId, originBackupAccountSession, originBackupWriteIntent }: { originAppId?: string; originBackupAccountSession?: number; originBackupWriteIntent?: BackupWriteIntent; - payload: { - signerIds?: string[]; - walletIds?: string[]; - }; + payload: { signerIds?: string[]; walletIds?: string[]; walletSchema?: RealmSchema }; }) { const appId = originAppId || currentBackupAppId(); const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); - const accountIsCurrent = () => - currentBackupAppId() === appId && currentBackupAccountSession() === requestSession; - if (!appId || !accountIsCurrent()) + if (!appId || currentBackupAppId() !== appId || requestSession !== currentBackupAccountSession()) return { updated: false, error: 'Backup account changed' }; - let remoteDeleteAttempted = false; - try { - const { signerIds, walletIds } = payload; - const { id }: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); - if (id !== appId) return { updated: false, error: 'Backup account changed' }; - let response; - - const backupResponse = yield call(checkBackupCondition, appId); - if (!accountIsCurrent()) - return { updated: false, error: 'Backup account changed' }; - if (backupResponse) response = { updated: true, error: '' }; - else { - remoteDeleteAttempted = true; - response = yield call(Relay.deleteAppImageEntity, { - appId: id, - signers: signerIds, - walletIds, - }, originBackupWriteIntent); - } - - if (!accountIsCurrent()) { - if (remoteDeleteAttempted) yield call(flagUncertainBackupOutcome, appId); - return { updated: false, error: 'Backup account changed' }; - } - if (response?.updated !== true) { - yield put(setPendingAllBackup({ appId, pending: true })); - yield call(setServerBackupFailed, appId); - return response || { updated: false, error: 'Backup deletion not confirmed' }; + const operation = beginBackupLocalOperation(appId, originBackupWriteIntent, requestSession); + const walletIds = [...new Set(payload.walletIds || [])]; + const signerIds = [...new Set(payload.signerIds || [])]; + const walletSchema = payload.walletSchema || RealmSchema.Wallet; + const committedOperation = commitBackupLocalOperation(operation, (localRealm) => { + for (const vault of localRealm.objects(RealmSchema.Vault)) { + if (vault.signers.some((key) => signerIds.includes(getKeyUID(key)))) + throw new Error('A retained vault still requires this signer'); } - const remoteDeleteSucceeded = !backupResponse; - - if (walletIds?.length > 0) { - for (const walletId of walletIds) { - if (!accountIsCurrent()) { - if (remoteDeleteSucceeded) yield call(flagUncertainBackupOutcome, appId); - return { updated: false, error: 'Backup account changed' }; - } - const deleted = yield call(dbManager.deleteObjectById, RealmSchema.Wallet, walletId); - const stillPresent = deleted === true - ? yield call(dbManager.getObjectById, RealmSchema.Wallet, walletId) - : true; - if (!accountIsCurrent()) { - if (remoteDeleteSucceeded) yield call(flagUncertainBackupOutcome, appId); - return { updated: false, error: 'Backup account changed' }; - } - if (deleted !== true || stillPresent) { - if (remoteDeleteSucceeded) { - yield put(setPendingAllBackup({ appId, pending: true })); - yield call(setServerBackupFailed, appId); - } - return { updated: false, error: 'Local wallet deletion failed' }; - } - } + for (const walletId of walletIds) { + const stored = localRealm.objectForPrimaryKey(walletSchema, walletId); + if (!stored) throw new Error('Wallet deletion target is missing'); + localRealm.delete(stored); } - if (signerIds?.length > 0) { - for (const signerId of signerIds) { - if (!accountIsCurrent()) { - if (remoteDeleteSucceeded) yield call(flagUncertainBackupOutcome, appId); - return { updated: false, error: 'Backup account changed' }; - } - const deleted = yield call( - dbManager.deleteObjectByPrimaryKey, - RealmSchema.Signer, - 'id', - signerId - ); - const stillPresent = deleted === true - ? yield call(dbManager.getObjectByPrimaryId, RealmSchema.Signer, 'id', signerId) - : true; - if (!accountIsCurrent()) { - if (remoteDeleteSucceeded) yield call(flagUncertainBackupOutcome, appId); - return { updated: false, error: 'Backup account changed' }; - } - if (deleted !== true || stillPresent) { - if (remoteDeleteSucceeded) { - yield put(setPendingAllBackup({ appId, pending: true })); - yield call(setServerBackupFailed, appId); - } - return { updated: false, error: 'Local signer deletion failed' }; - } - } + for (const signerId of signerIds) { + const stored = localRealm.objectForPrimaryKey(RealmSchema.Signer, signerId); + if (!stored) throw new Error('Signer deletion target is missing'); + localRealm.delete(stored); } - return response; - } catch (err) { - captureError(err); - if (remoteDeleteAttempted && !accountIsCurrent()) - yield call(flagUncertainBackupOutcome, appId); - return { updated: false, error: err }; - } + }); + if (!committedOperation.committed) return { updated: false, error: 'Local recovery deletion failed' }; + return yield call(queueCommittedBackupWorker, appId, committedOperation, [{ + operation: 'deleteAppImageEntity', body: { appId, walletIds, signers: signerIds }, + }], requestSession); } -export function* deleteVaultImageWorker({ - payload, - originAppId, - originBackupAccountSession, - originBackupWriteIntent, -}: { +export function* deleteVaultImageWorker({ payload, originAppId, originBackupAccountSession, committedOperation }: { originAppId?: string; originBackupAccountSession?: number; originBackupWriteIntent?: BackupWriteIntent; - payload: { - vaultIds: string[]; - }; + committedOperation?: BackupLocalCommit; + payload: { vaultIds: string[] }; }) { const appId = originAppId || currentBackupAppId(); const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); - const accountIsCurrent = () => - currentBackupAppId() === appId && currentBackupAccountSession() === requestSession; - if (!appId || !accountIsCurrent()) - return { updated: false, error: 'Backup account changed' }; - let remoteDeleteAttempted = false; - try { - const backupResponse = yield call(checkBackupCondition, appId); - if (!accountIsCurrent()) - return { updated: false, error: 'Backup account changed' }; - if (backupResponse) return { updated: true, error: '' }; - const { vaultIds } = payload; - const { id }: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); - if (id !== appId) return { updated: false, error: 'Backup account changed' }; - remoteDeleteAttempted = true; - const response = yield call(Relay.deleteVaultImage, { - appId: id, - vaults: vaultIds, - }, originBackupWriteIntent); - if (!accountIsCurrent()) { - yield call(flagUncertainBackupOutcome, appId); - return { updated: false, error: 'Backup account changed' }; - } - return response; - } catch (err) { - captureError(err); - if (remoteDeleteAttempted && !accountIsCurrent()) - yield call(flagUncertainBackupOutcome, appId); - return { updated: false, error: err }; - } + if (!appId) return { updated: false, error: 'Backup account changed' }; + return yield call(queueCommittedBackupWorker, appId, committedOperation, [{ + operation: 'deleteVaults', body: { appId, vaults: [...new Set(payload.vaultIds)] }, + }], requestSession); } function* seedBackeupConfirmedWorked({ @@ -975,9 +784,7 @@ function* healthCheckSatutsUpdateWorker({ }; const oldDetialsArray = [...signer.healthCheckDetails]; - const oldDetails = oldDetialsArray.map((details) => { - return { ...details, date: new Date(details.actionDate) }; - }); + const oldDetails = oldDetialsArray.map((details) => ({ ...details, date: new Date(details.actionDate) })); const updatedDetailsArray: HealthCheckDetails[] = [...oldDetails, newHealthCheckDetails]; @@ -1373,6 +1180,9 @@ function* backupAllSignersAndVaultsWorker({ originBackupAccountSession?: number; }) { const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); + const requestConsentEpoch = backupConsentEpoch(appId); + const isCurrentChoice = () => currentBackupAppId() === appId && + requestSession === currentBackupAccountSession() && backupConsentEpoch(appId) === requestConsentEpoch; if (requestSession !== currentBackupAccountSession()) return false; const { id }: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); if (!appId || appId !== id) return false; @@ -1383,19 +1193,21 @@ function* backupAllSignersAndVaultsWorker({ yield put(setBackupAllLoading({ appId, status: true })); try { const verified = yield call(runBackupInspection, true, appId, true, requestSession); - const current: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); - const stillSelected = yield select((state: RootState) => state.storage.appId === appId); - if (current?.id !== appId || !stillSelected || requestSession !== currentBackupAccountSession()) - return false; - if (verified) yield put(setAutomaticCloudBackup({ appId, enabled: true })); - yield put( - verified - ? setBackupAllSuccess({ appId, status: true }) - : setBackupAllFailure({ appId, status: true }) - ); - return verified; + return yield call(() => { + if (!isCurrentChoice()) return false; + const state = store.getState(); + const stillVerified = verified && state.bhr.backupRepairStateByAppId?.[appId] === 'verified' && + state.bhr.pendingAllBackupByAppId?.[appId] === false && isBackupVerificationCurrent(appId); + if (stillVerified) store.dispatch(protectBackupConsentCompletion( + setAutomaticCloudBackup({ appId, enabled: true }), appId, requestConsentEpoch + )); + if (!isCurrentChoice() || (stillVerified && !isBackupVerificationCurrent(appId))) return false; + store.dispatch(stillVerified ? setBackupAllSuccess({ appId, status: true }) + : setBackupAllFailure({ appId, status: true })); + return stillVerified; + }); } finally { - yield put(setBackupAllLoading({ appId, status: false })); + if (isCurrentChoice()) yield put(setBackupAllLoading({ appId, status: false })); } } @@ -1404,53 +1216,44 @@ export const backupAllSignersAndVaultsWatcher = createWatcher( BACKUP_ALL_SIGNERS_AND_VAULTS ); -function* deleteBackupWorker({ - appId, - originBackupAccountSession, - originBackupWriteIntent, -}: { +function* deleteBackupWorker({ appId, originBackupAccountSession }: { appId: string; originBackupAccountSession?: number; originBackupWriteIntent?: BackupWriteIntent; }) { const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); - if (requestSession !== currentBackupAccountSession()) return false; - const { id }: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); - if (!appId || appId !== id) return false; + const requestConsentEpoch = backupConsentEpoch(appId); + const isCurrent = () => currentBackupAppId() === appId && currentBackupAccountSession() === requestSession && + backupConsentEpoch(appId) === requestConsentEpoch; + if (!appId || !isCurrent()) return false; yield put(setBackupAllLoading({ appId, status: true })); - let remoteDeleteAttempted = false; try { - const current: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); - if (current?.id !== appId || requestSession !== currentBackupAccountSession()) return false; - remoteDeleteAttempted = true; - const response = yield call(Relay.deleteBackup, { - appId, - }, originBackupWriteIntent); - if (requestSession !== currentBackupAccountSession()) { - yield call(flagUncertainBackupOutcome, appId); - return false; - } - if (response?.updated !== true && response?.data?.updated !== true) - throw new Error('Backup deletion not confirmed'); - const afterDelete: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); - if (afterDelete?.id !== appId || requestSession !== currentBackupAccountSession()) { - yield call(flagUncertainBackupOutcome, appId); - return false; - } - yield put(setAutomaticCloudBackup({ appId, enabled: false })); - yield put(setDeleteBackupSuccess({ appId, status: true })); - yield put(setPendingAllBackup({ appId, pending: false })); - return true; - } catch (error) { - if (requestSession !== currentBackupAccountSession()) { - if (remoteDeleteAttempted) yield call(flagUncertainBackupOutcome, appId); - return false; + // Opt-out deliberately removes the remote copy while retaining every local + // record. Its own fresh v2 snapshot is bound to this original session. + const intent = yield call(beginBackupDeleteOperation, appId, requestSession); + if (!isCurrent()) return false; + if (!intent?.committed || !intent.intent) throw new Error('Backup deletion preflight failed'); + const result = yield call(enqueueAutomaticBackupOperation, intent, [{ + operation: 'deleteBackup', body: { appId }, + }]); + if (!result?.updated || !result?.backupConfirmed) throw new Error('Backup deletion not confirmed'); + const completed = yield call(() => { + if (!isCurrent() || !isAutomaticBackupResultCurrent(result, appId)) return false; + store.dispatch(setDeleteBackupSuccess({ appId, status: true })); + if (!isCurrent() || !isAutomaticBackupResultCurrent(result, appId)) return false; + store.dispatch(setPendingAllBackup({ appId, pending: false })); + return true; + }); + if (!completed && isCurrent()) yield put(setDeleteBackupFailure({ appId, status: true })); + return completed; + } catch { + if (isCurrent()) { + yield put(setPendingAllBackup({ appId, pending: true })); + yield put(setDeleteBackupFailure({ appId, status: true })); } - yield put(setDeleteBackupFailure({ appId, status: true })); - console.log('🚀 ~ deleteBackupWorker ~ error:', error); return false; } finally { - yield put(setBackupAllLoading({ appId, status: false })); + if (isCurrent()) yield put(setBackupAllLoading({ appId, status: false })); } } diff --git a/src/store/sagas/wallets.ts b/src/store/sagas/wallets.ts index 0dd71a647e..e74a8c965f 100644 --- a/src/store/sagas/wallets.ts +++ b/src/store/sagas/wallets.ts @@ -18,14 +18,12 @@ import { MiniscriptElements, Signer, Vault, - VaultPresentationData, VaultScheme, VaultSigner, } from 'src/services/wallets/interfaces/vault'; import { Wallet, WalletImportDetails, - WalletPresentationData, } from 'src/services/wallets/interfaces/wallet'; import { call, delay, put, select } from 'redux-saga/effects'; import { @@ -43,11 +41,11 @@ import { KeeperApp } from 'src/models/interfaces/KeeperApp'; import { RealmSchema } from 'src/storage/realm/enum'; import Relay from 'src/services/backend/Relay'; import { - clearVerifiedRemoteRevision, currentBackupAccountSession, markBackupMutation, } from 'src/services/backup/transport'; import type { BackupWriteIntent } from 'src/services/backup/intent'; +import { beginBackupLocalOperation, commitBackupLocalOperation } from 'src/services/backup/automatic'; import SigningServer from 'src/services/backend/SigningServer'; import WalletOperations from 'src/services/wallets/operations'; import WalletUtilities from 'src/services/wallets/operations/utils'; @@ -79,10 +77,12 @@ import { sanitizeSeedKeyForBackup, } from 'src/utils/utilities'; import { COLLABORATIVE_SCHEME } from 'src/screens/SigningDevices/SetupCollaborativeWallet'; +import { accountNoFromDerivationPath } from 'src/utils/service-utilities/utils'; +import { classifyDustByAddress } from 'src/services/wallets/operations/dustClassification'; import { RootState, store } from '../store'; import { selectWalletsForSync } from './walletSyncSelection'; import { acquireWalletRefresh } from './walletRefreshCoordinator'; -import { checkBackupFreshness } from '../sagaActions/bhr'; +import { checkBackupFreshness , backupBsmsOnCloud } from '../sagaActions/bhr'; import { initiateVaultMigration, @@ -125,7 +125,9 @@ import { deleteVaultImageWorker, updateAppImageWorker, updateVaultImageWorker, - setServerBackupFailed, + buildAutomaticAppMutation, + buildAutomaticVaultMutation, + queueCommittedBackupWorker, } from './bhr'; import { relaySignersUpdateFail, @@ -147,11 +149,8 @@ import { } from '../reducers/bhr'; import { setElectrumNotConnectedErr } from '../reducers/login'; import { connectToNodeWorker } from './network'; -import { backupBsmsOnCloud } from '../sagaActions/bhr'; import { bulkUpdateLabelsWorker } from './utxos'; import { updateDelayedPolicyUpdate } from '../reducers/storage'; -import { accountNoFromDerivationPath } from 'src/utils/service-utilities/utils'; -import { classifyDustByAddress } from 'src/services/wallets/operations/dustClassification'; import { setPendingDustToast } from '../reducers/utxos'; import { isFaucetTxVisible } from './faucetVisibility'; @@ -173,6 +172,105 @@ const assertWalletAccount = (appId: string, expectedSession?: number) => { ) throw new Error('Account changed'); }; +// Fingerprint corrections preserve key material, compiled policy and derivation. +// Only origin references and the account-qualified signer primary key change. +function rewriteSignerFingerprintReferences(value: any, oldFingerprint: string, newFingerprint: string, field = ''): any { + if (Array.isArray(value)) return value.map((entry) => rewriteSignerFingerprintReferences(entry, oldFingerprint, newFingerprint, field)); + if (value && typeof value === 'object') { + const result = {}; + for (const [key, entry] of Object.entries(value)) { + result[key] = rewriteSignerFingerprintReferences(entry, oldFingerprint, newFingerprint, + field === 'signerFingerprints' ? 'masterFingerprint' : field === 'keyInfoMap' ? 'descriptor' : key); + } + return result; + } + if (typeof value !== 'string') return value; + if (field === 'masterFingerprint' && value.toUpperCase() === oldFingerprint.toUpperCase()) return newFingerprint; + // Descriptor origins use [fingerprint/path]; do not edit names or comments. + if (/descriptor/i.test(field)) { + const expression = new RegExp(`\\[${oldFingerprint}(?=/|\\])`, 'gi'); + return value.replace(expression, `[${newFingerprint}`); + } + return value; +} + +function planSignerIdentityMerges(existingSigners: Signer[], incoming: Signer[]) { + const changes: { oldSignerId: string; newSignerId: string; oldFingerprint: string; newFingerprint: string; signer: Signer }[] = []; + for (const added of incoming) { + for (let index = 0; index < existingSigners.length; index += 1) { + const stored = existingSigners[index]; + const oldSingle = stored.signerXpubs[XpubTypes.P2WPKH]?.[0]?.xpub; + const oldMulti = stored.signerXpubs[XpubTypes.P2WSH]?.[0]?.xpub; + const newSingle = added.signerXpubs[XpubTypes.P2WPKH]?.[0]?.xpub; + const newMulti = added.signerXpubs[XpubTypes.P2WSH]?.[0]?.xpub; + if ((!oldSingle && !oldMulti) || oldSingle !== newSingle || oldMulti !== newMulti || + stored.masterFingerprint === added.masterFingerprint || stored.networkType !== added.networkType) continue; + const corrected = { ...stored, masterFingerprint: added.masterFingerprint }; + corrected.id = getKeyUID(corrected); + changes.push({ oldSignerId: getKeyUID(stored), newSignerId: corrected.id, + oldFingerprint: stored.masterFingerprint, newFingerprint: added.masterFingerprint, signer: corrected }); + existingSigners[index] = corrected; + } + } + return changes; +} + +function applySignerIdentityMerges(localRealm: Parameters[1]>[0], changes: ReturnType) { + const affectedVaultIds = new Set(); + for (const change of changes) { + const old = localRealm.objectForPrimaryKey(RealmSchema.Signer, change.oldSignerId); + if (!old) throw new Error('Signer identity changed before commit'); + if (change.newSignerId !== change.oldSignerId && + localRealm.objectForPrimaryKey(RealmSchema.Signer, change.newSignerId)) + throw new Error('Corrected signer identity already exists'); + const affectedVaults = (Array.from(localRealm.objects(RealmSchema.Vault)) as Vault[]) + .filter((vault) => vault.signers.some((key) => getKeyUID(key) === change.oldSignerId)); + localRealm.delete(old); + localRealm.create(RealmSchema.Signer, change.signer, Realm.UpdateMode.Modified); + for (const key of localRealm.objects(RealmSchema.VaultSigner)) { + if (getKeyUID(key) === change.oldSignerId) key.masterFingerprint = change.newFingerprint; + } + for (const vault of affectedVaults) { + affectedVaultIds.add(vault.id); + vault.scheme = rewriteSignerFingerprintReferences((getJSONFromRealmObject(vault) as unknown as Vault).scheme, + change.oldFingerprint, change.newFingerprint); + } + } + return affectedVaultIds; +} + +function applySignerXprivUpdates(localRealm: Parameters[1]>[0], signers: Signer[]) { + const affectedVaultIds = new Set(); + for (const signer of signers) { + if (signer.type !== SignerType.SEED_WORDS) continue; + for (const key of localRealm.objects(RealmSchema.VaultSigner)) { + if (getKeyUID(key) !== getKeyUID(signer)) continue; + const purpose = Number(WalletUtilities.getSignerPurposeFromPath(key.derivationPath)); + const type = purpose === DerivationPurpose.BIP84 ? XpubTypes.P2WPKH + : purpose === DerivationPurpose.BIP48 ? XpubTypes.P2WSH + : purpose === DerivationPurpose.BIP86 ? XpubTypes.P2TR : undefined; + const xpriv = type && signer.signerXpubs[type]?.[0]?.xpriv; + if (!xpriv) continue; + key.xpriv = xpriv; + for (const vault of localRealm.objects(RealmSchema.Vault)) { + if (vault.signers.some((stored) => stored.xpub === key.xpub)) affectedVaultIds.add(vault.id); + } + } + } + return affectedVaultIds; +} + +function buildSignerMigrationMutation(appId: string, changes: ReturnType) { + const app = dbManager.getObjectByIndex(RealmSchema.KeeperApp) as unknown as KeeperApp; + const encryptionKey = generateEncryptionKey(app.primarySeed); + return { operation: 'migrateXfps' as const, body: { appId, signerChanges: changes.map((change) => { + const stored = dbManager.getObjectByPrimaryId(RealmSchema.Signer, 'id', change.newSignerId); + if (!stored) throw new Error('Corrected signer is missing'); + return { oldSignerId: change.oldSignerId, newSignerId: change.newSignerId, + newSignerDetails: encrypt(encryptionKey, JSON.stringify(sanitizeSeedKeyForBackup(getJSONFromRealmObject(stored) as unknown as Signer))) }; + }) } }; +} + export interface NewVaultDetails { name?: string; description?: string; @@ -260,6 +358,7 @@ export function* addNewWalletsWorker({ const app: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); sourceAppId = originAppId || app?.id; assertWalletAccount(sourceAppId, requestSession); + const operation = beginBackupLocalOperation(sourceAppId, originBackupWriteIntent, requestSession); for (const { walletType, walletDetails, importDetails } of newWalletInfo) { const wallet: Wallet = yield call( @@ -275,24 +374,22 @@ export function* addNewWalletsWorker({ if (wallets.length > 0) { yield put(setRelayWalletUpdateLoading(true)); - const response = yield call(updateAppImageWorker, { + const committedOperation = commitBackupLocalOperation(operation, (localRealm) => { + for (const wallet of wallets) localRealm.create(RealmSchema.Wallet, wallet); + }); + if (!committedOperation.committed) throw new Error('Local wallet creation failed'); + yield call(updateAppImageWorker, { payload: { wallets }, originAppId: sourceAppId, originBackupAccountSession: requestSession, originBackupWriteIntent, + committedOperation, }); - if (response.updated) { - assertWalletAccount(sourceAppId, requestSession); - yield call(dbManager.createObjectBulk, RealmSchema.Wallet, wallets); - yield put(relayWalletUpdateSuccess()); - return true; - } - const errorMsg = response.error?.message - ? response.error.message.toString() - : response.error.toString(); - yield put(relayWalletUpdateFail(errorMsg)); - return false; + assertWalletAccount(sourceAppId, requestSession); + yield put(relayWalletUpdateSuccess()); + return true; } + return false; } catch (err) { console.log(err); if (currentWalletAccountId() === sourceAppId && @@ -314,113 +411,71 @@ export interface NewVaultInfo { miniscriptTypes?: MiniscriptTypes[]; } -export function* addNewVaultWorker({ - payload, - originAppId, - originBackupAccountSession, - originBackupWriteIntent, -}: { +export function* addNewVaultWorker({ payload, originAppId, originBackupAccountSession, originBackupWriteIntent }: { originAppId?: string; originBackupAccountSession?: number; originBackupWriteIntent?: BackupWriteIntent; - payload: { - newVaultInfo?: NewVaultInfo; - isMigrated?: boolean; - oldVaultId?: string; - }; + payload: { newVaultInfo?: NewVaultInfo; isMigrated?: boolean; oldVaultId?: string }; }) { + const sourceAppId = originAppId || currentWalletAccountId(); const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); try { - const sourceAppId = originAppId || currentWalletAccountId(); assertWalletAccount(sourceAppId, requestSession); + const operation = beginBackupLocalOperation(sourceAppId, originBackupWriteIntent, requestSession); const { newVaultInfo, isMigrated, oldVaultId } = payload; const { bitcoinNetworkType: networkType } = yield select((state: RootState) => state.settings); - const { vaultType = VaultType.DEFAULT, vaultScheme, vaultSigners, vaultDetails } = newVaultInfo; - - if (vaultScheme.multisigScriptType !== MultisigScriptType.MINISCRIPT_MULTISIG) { - if (vaultScheme.n !== vaultSigners.length) { - throw new Error('Vault schema(n) and signers mismatch'); - } - } - - let vault = yield call(generateVault, { - type: vaultType, - vaultName: vaultDetails.name, - vaultDescription: vaultDetails.description, - scheme: vaultScheme, - signers: vaultSigners, - networkType, + if (vaultScheme.multisigScriptType !== MultisigScriptType.MINISCRIPT_MULTISIG && + vaultScheme.n !== vaultSigners.length) throw new Error('Vault schema(n) and signers mismatch'); + const vault: Vault = yield call(generateVault, { + type: vaultType, vaultName: vaultDetails.name, vaultDescription: vaultDetails.description, + scheme: vaultScheme, signers: vaultSigners, networkType, }); - - if (isMigrated) { - const oldVault = dbManager.getObjectById(RealmSchema.Vault, oldVaultId).toJSON() as Vault; - vault = { - ...vault, - archivedId: oldVault.archivedId ? oldVault.archivedId : oldVault.id, - }; - } - + assertWalletAccount(sourceAppId, requestSession); yield put(setRelayVaultUpdateLoading(true)); - const newVaultResponse = yield call(updateVaultImageWorker, { - payload: { vault }, - originAppId: sourceAppId, - originBackupAccountSession: requestSession, - originBackupWriteIntent, - }); - if (newVaultResponse.updated) { - assertWalletAccount(sourceAppId, requestSession); - yield call(dbManager.createObject, RealmSchema.Vault, vault); - yield put(uaiChecks([uaiType.SECURE_VAULT])); - + const committedOperation = commitBackupLocalOperation(operation, (localRealm) => { + if (localRealm.objectForPrimaryKey(RealmSchema.Vault, vault.id)) + throw new Error('This vault already exists'); + let oldVault; if (isMigrated) { - const oldVault = dbManager.getObjectById(RealmSchema.Vault, oldVaultId).toJSON() as Vault; - const isWalletEmpty = - oldVault.specs.balances.confirmed === 0 && oldVault.specs.balances.unconfirmed === 0; - const updatedParams = { - archived: isWalletEmpty, - isMigrating: !isWalletEmpty, - archivedId: oldVault.archivedId ? oldVault.archivedId : oldVault.id, - }; - const archivedVaultresponse = yield call(updateVaultImageWorker, { - originAppId: sourceAppId, - originBackupAccountSession: requestSession, - originBackupWriteIntent, - payload: { - vault: { - ...oldVault, - ...updatedParams, - }, - }, - }); - if (archivedVaultresponse.updated) { - assertWalletAccount(sourceAppId, requestSession); - yield call(dbManager.updateObjectById, RealmSchema.Vault, oldVaultId, updatedParams); - yield put(initiateVaultMigration({ intrimVault: vault })); - } + oldVault = localRealm.objectForPrimaryKey(RealmSchema.Vault, oldVaultId); + if (!oldVault) throw new Error('Original vault is missing'); + vault.archivedId = oldVault.archivedId || oldVault.id; } - - yield put(vaultCreated({ hasNewVaultGenerationSucceeded: true })); - yield put(relayVaultUpdateSuccess()); - yield put(setIsCloudBsmsBackupRequired(true)); - yield put(backupBsmsOnCloud()); - return true; - } - throw new Error('Relay updation failed'); + // VaultSigner is shared by xpub across retained and new vaults. Keep its + // registrations and remembered private material when linking a new vault. + vault.signers = vault.signers.map((key) => { + const storedKey = localRealm.objectForPrimaryKey(RealmSchema.VaultSigner, key.xpub); + if (!storedKey) return key; + return { ...key, registeredVaults: (getJSONFromRealmObject(storedKey) as unknown as VaultSigner).registeredVaults, + xpriv: key.xpriv ?? storedKey.xpriv }; + }); + localRealm.create(RealmSchema.Vault, vault, Realm.UpdateMode.Modified); + if (oldVault) { + const empty = oldVault.specs.balances.confirmed === 0 && oldVault.specs.balances.unconfirmed === 0; + oldVault.archived = empty; + oldVault.isMigrating = !empty; + oldVault.archivedId = oldVault.archivedId || oldVault.id; + } + }); + if (!committedOperation.committed) throw new Error('Local vault creation failed'); + const mutations = isMigrated ? [buildAutomaticVaultMutation(oldVaultId, sourceAppId)] : []; + mutations.push(buildAutomaticVaultMutation(vault.id, sourceAppId, false)); + yield call(queueCommittedBackupWorker, sourceAppId, committedOperation, mutations, requestSession); + assertWalletAccount(sourceAppId, requestSession); + yield put(uaiChecks([uaiType.SECURE_VAULT])); + if (isMigrated) yield put(initiateVaultMigration({ intrimVault: vault })); + yield put(vaultCreated({ hasNewVaultGenerationSucceeded: true })); + yield put(relayVaultUpdateSuccess()); + yield put(setIsCloudBsmsBackupRequired(true)); + yield put(backupBsmsOnCloud()); + return true; } catch (err) { - yield put( - vaultCreated({ - hasNewVaultGenerationFailed: true, - hasNewVaultGenerationSucceeded: false, - error: err.toString(), - }) - ); captureError(err); - yield put( - relayVaultUpdateFail( - 'Failed to create the vault. Please check your internet connection and try again' - ) - ); + if (currentWalletAccountId() === sourceAppId && currentBackupAccountSession() === requestSession) { + yield put(vaultCreated({ hasNewVaultGenerationFailed: true, hasNewVaultGenerationSucceeded: false, error: err.toString() })); + yield put(relayVaultUpdateFail('Failed to save the vault. Please try again')); + } return false; } } @@ -440,23 +495,18 @@ export function* addSigningDeviceWorker({ originBackupAccountSession?: number; originBackupWriteIntent?: BackupWriteIntent; }) { - if (!signers.length) return; + if (!signers.length) return false; const sourceAppId = originAppId || currentWalletAccountId(); const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); assertWalletAccount(sourceAppId, requestSession); - for (let i = 0; i < signers.length; i++) { - const signer = signers[i]; - yield call(mergeSimilarKeysWorker, { - payload: { signer }, - originAppId: sourceAppId, - originBackupAccountSession: requestSession, - originBackupWriteIntent, - }); - } + const operation = beginBackupLocalOperation(sourceAppId, originBackupWriteIntent, requestSession); try { const { bitcoinNetworkType } = yield select((state: RootState) => state.settings); - const existingSigners: Signer[] = yield call(dbManager.getCollection, RealmSchema.Signer); + const storedSigners: Signer[] = yield call(dbManager.getCollection, RealmSchema.Signer); + const existingSigners = storedSigners.map((signer: any) => signer.toJSON ? getJSONFromRealmObject(signer) as unknown as Signer : _.cloneDeep(signer)); + signers = signers.map((signer) => ({ ...signer, signerXpubs: _.cloneDeep(signer.signerXpubs) })); + const identityChanges = planSignerIdentityMerges(existingSigners, signers); const filteredSigners = existingSigners.filter( (s) => !s.archived && s.networkType === bitcoinNetworkType ); @@ -479,7 +529,7 @@ export function* addSigningDeviceWorker({ }); } catch (e) { captureError(e); - return; + return false; } const keysMatch = (type, newSigner, existingSigner) => @@ -573,45 +623,37 @@ export function* addSigningDeviceWorker({ id: getKeyUID(signer), })); yield put(setRelaySignersUpdateLoading(true)); - const response = yield call(updateAppImageWorker, { - payload: { signers: signersToUpdate }, - originAppId: sourceAppId, - originBackupAccountSession: requestSession, - originBackupWriteIntent, + const affectedVaultIds = new Set(); + const committedOperation = commitBackupLocalOperation(operation, (localRealm) => { + for (const vaultId of applySignerIdentityMerges(localRealm, identityChanges)) affectedVaultIds.add(vaultId); + for (const signer of signersToUpdate) localRealm.create(RealmSchema.Signer, signer, Realm.UpdateMode.Modified); + for (const vaultId of applySignerXprivUpdates(localRealm, signersToUpdate)) affectedVaultIds.add(vaultId); }); - if (response.updated) { - assertWalletAccount(sourceAppId, requestSession); - yield call( - dbManager.createObjectBulk, - RealmSchema.Signer, - signersToUpdate, - Realm.UpdateMode.Modified - ); - - yield put( - relaySignersUpdateSuccess( - signersToUpdate.length !== 1 || - (signersToUpdate[0]?.extraData?.instanceNumber !== 1 && !signersToUpdate[0]?.hidden) - ) - ); - yield call(updateVaultSignerXprivWorker, { - signers, - originAppId: sourceAppId, - originBackupAccountSession: requestSession, - }); - } else { - const errorMsg = response.error?.message - ? response.error.message.toString() - : response.error.toString(); - yield put(relaySignersUpdateFail(errorMsg)); - } + if (!committedOperation.committed) throw new Error('Local signer creation failed'); + const mutations: Parameters[2] = [ + buildAutomaticAppMutation({ signers: signersToUpdate }, sourceAppId), + ...Array.from(affectedVaultIds).map((vaultId) => buildAutomaticVaultMutation(vaultId, sourceAppId)), + ]; + // Add the corrected key and update all Vault references before removing + // the old remote key. Every partial prefix remains recoverable. + if (identityChanges.length) mutations.push(buildSignerMigrationMutation(sourceAppId, identityChanges)); + yield call(queueCommittedBackupWorker, sourceAppId, committedOperation, mutations, requestSession); + assertWalletAccount(sourceAppId, requestSession); + yield put(relaySignersUpdateSuccess( + signersToUpdate.length !== 1 || + (signersToUpdate[0]?.extraData?.instanceNumber !== 1 && !signersToUpdate[0]?.hidden) + )); } else if (signers.length === 1) { yield put(relaySignersUpdateFail('The signer already exists.')); } + assertWalletAccount(sourceAppId, requestSession); if (callback) callback(); + return true; } catch (error) { captureError(error); - yield put(relaySignersUpdateFail('An error occurred while updating signers.')); + if (currentWalletAccountId() === sourceAppId && currentBackupAccountSession() === requestSession) + yield put(relaySignersUpdateFail('An error occurred while updating signers.')); + return false; } } @@ -672,43 +714,34 @@ export const deleteSigningDeviceWatcher = createWatcher( DELETE_SIGINING_DEVICE ); -function* archiveSigningDeviceWorker({ - payload: { signers }, - originAppId, - originBackupAccountSession, - originBackupWriteIntent, -}: { +function* archiveSigningDeviceWorker({ payload: { signers }, originAppId, originBackupAccountSession, originBackupWriteIntent }: { payload: { signers: Signer[] }; originAppId?: string; originBackupAccountSession?: number; originBackupWriteIntent?: BackupWriteIntent; }) { + const sourceAppId = originAppId || currentWalletAccountId(); + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); try { - const sourceAppId = originAppId || currentWalletAccountId(); - const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); assertWalletAccount(sourceAppId, requestSession); - const signersToArchiveIds = []; - for (const signer of signers) { - signersToArchiveIds.push(getKeyUID(signer)); - } - if (signers.length) { - for (let i = 0; i < signers.length; i++) { - yield call(updateSignerDetailsWorker, { - originAppId: sourceAppId, - originBackupAccountSession: requestSession, - originBackupWriteIntent, - payload: { - signer: signers[i], - key: 'archived', - value: true, - }, - }); + const operation = beginBackupLocalOperation(sourceAppId, originBackupWriteIntent, requestSession); + const committedOperation = commitBackupLocalOperation(operation, (localRealm) => { + for (const signer of signers) { + const stored = localRealm.objectForPrimaryKey(RealmSchema.Signer, getKeyUID(signer)); + if (!stored) throw new Error('Signer is missing'); + stored.archived = true; } - yield put(uaiChecks([uaiType.SIGNING_DEVICES_HEALTH_CHECK])); - } - } catch (error) { - captureError(error); - yield put(relaySignersUpdateFail('An error occurred while archiving signers.')); + }); + if (!committedOperation.committed) throw new Error('Local signer archive failed'); + yield call(updateAppImageWorker, { payload: { signers }, originAppId: sourceAppId, + originBackupAccountSession: requestSession, committedOperation }); + assertWalletAccount(sourceAppId, requestSession); + yield put(uaiChecks([uaiType.SIGNING_DEVICES_HEALTH_CHECK])); + return true; + } catch { + if (currentWalletAccountId() === sourceAppId && currentBackupAccountSession() === requestSession) + yield put(relaySignersUpdateFail('Key changes could not be saved')); + return false; } } @@ -921,7 +954,7 @@ function* refreshWalletsWorker({ string, { spendability?: string; isManualOverride?: boolean; dustReason?: string } >(); - const specs = (wallet as any).specs; + const {specs} = (wallet as any); if (specs) { const allUTXOs = [...(specs.confirmedUTXOs || []), ...(specs.unconfirmedUTXOs || [])]; for (const utxo of allUTXOs) { @@ -1189,7 +1222,6 @@ function* refreshWalletsWorker({ ); if (backupEnabled) { markBackupMutation(sourceAppId); - clearVerifiedRemoteRevision(sourceAppId); yield put(invalidateBackupRepair(sourceAppId)); yield put(setPendingAllBackup({ appId: sourceAppId, pending: true })); } @@ -1394,6 +1426,7 @@ export function* updateSignerPolicyWorker({ payload, originAppId, originBackupAccountSession, + originBackupWriteIntent, }: { payload: { signer: Signer; @@ -1406,12 +1439,14 @@ export function* updateSignerPolicyWorker({ }; originAppId?: string; originBackupAccountSession?: number; + originBackupWriteIntent?: BackupWriteIntent; }) { const sourceAppId = originAppId || currentWalletAccountId(); const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); const { signer, signingKey, updates, verificationToken } = payload; try { assertWalletAccount(sourceAppId, requestSession); + const operation = beginBackupLocalOperation(sourceAppId, originBackupWriteIntent, requestSession); const fcmToken = yield select((state: RootState) => state.notifications.fcmToken); const { bitcoinNetwork } = yield select((state: RootState) => state.settings); const signerId = @@ -1441,18 +1476,14 @@ export function* updateSignerPolicyWorker({ throw new Error('Failed to update the policy'); } - const updatedSignerPolicy = { - ...signer.signerPolicy, - ...updates, - }; - dbManager.updateObjectByPrimaryId( - RealmSchema.Signer, - 'masterFingerprint', - signer.masterFingerprint, - { - signerPolicy: updatedSignerPolicy, - } - ); + const committedOperation = commitBackupLocalOperation(operation, (localRealm) => { + const stored = localRealm.objectForPrimaryKey(RealmSchema.Signer, getKeyUID(signer)); + if (!stored) throw new Error('Signer is missing'); + stored.signerPolicy = { ...(getJSONFromRealmObject(stored) as unknown as Signer).signerPolicy, ...updates }; + }); + if (!committedOperation.committed) throw new Error('Signer policy could not be saved'); + yield call(updateAppImageWorker, { payload: { signers: [signer] }, + originAppId: sourceAppId, originBackupAccountSession: requestSession, committedOperation }); } assertWalletAccount(sourceAppId, requestSession); yield put(setSignerPolicyError('success')); @@ -1606,150 +1637,78 @@ export const testcoinsWatcher = createWatcher(testcoinsWorker, TEST_SATS_RECIEVE function* updateWalletDetailsWorker({ payload, originAppId, originBackupAccountSession, originBackupWriteIntent }) { const sourceAppId = originAppId || currentWalletAccountId(); const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); - const { - wallet, - details, - }: { - wallet: Wallet; - details: { - name: string; - description: string; - }; - } = payload; try { assertWalletAccount(sourceAppId, requestSession); - const presentationData: WalletPresentationData = { - name: details.name, - description: details.description, - visibility: wallet.presentationData.visibility, - }; - wallet.presentationData = presentationData; - - yield put(setRelayWalletUpdateLoading(true)); - const response = yield call(updateAppImageWorker, { - payload: { wallets: [wallet] }, - originAppId: sourceAppId, - originBackupAccountSession: requestSession, - originBackupWriteIntent, + const operation = beginBackupLocalOperation(sourceAppId, originBackupWriteIntent, requestSession); + const committedOperation = commitBackupLocalOperation(operation, (localRealm) => { + const stored = localRealm.objectForPrimaryKey(RealmSchema.Wallet, payload.wallet.id); + if (!stored) throw new Error('Wallet is missing'); + stored.presentationData = { ...(getJSONFromRealmObject(stored) as unknown as Wallet).presentationData, + name: payload.details.name, description: payload.details.description }; }); - if (response.updated) { - assertWalletAccount(sourceAppId, requestSession); - yield call(dbManager.updateObjectById, RealmSchema.Wallet, wallet.id, { - presentationData, - }); - yield put(relayWalletUpdateSuccess()); - } else { - const errorMsg = response.error?.message - ? response.error.message.toString() - : response.error.toString(); - yield put(relayWalletUpdateFail(errorMsg)); - } - } catch (err) { - yield put(relayWalletUpdateFail('Something went wrong!')); + if (!committedOperation.committed) throw new Error('Local wallet update failed'); + yield call(updateAppImageWorker, { payload: { wallets: [payload.wallet] }, + originAppId: sourceAppId, originBackupAccountSession: requestSession, committedOperation }); + assertWalletAccount(sourceAppId, requestSession); + yield put(relayWalletUpdateSuccess()); + return true; + } catch { + if (currentWalletAccountId() === sourceAppId && currentBackupAccountSession() === requestSession) + yield put(relayWalletUpdateFail('Wallet changes could not be saved')); + return false; } } -export const updateWalletDetailWatcher = createWatcher( - updateWalletDetailsWorker, - UPDATE_WALLET_DETAILS -); +export const updateWalletDetailWatcher = createWatcher(updateWalletDetailsWorker, UPDATE_WALLET_DETAILS); function* updateVaultDetailsWorker({ payload, originAppId, originBackupAccountSession, originBackupWriteIntent }) { const sourceAppId = originAppId || currentWalletAccountId(); const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); - const { - vault, - details, - }: { - vault: Vault; - details: { - name: string; - description: string; - }; - } = payload; try { assertWalletAccount(sourceAppId, requestSession); - const presentationData: VaultPresentationData = { - name: details.name, - description: details.description, - visibility: vault.presentationData.visibility, - }; - yield put(setRelayVaultUpdateLoading(true)); - // API-TODO: based on response call the DB - vault.presentationData = presentationData; - - const response = yield call(updateVaultImageWorker, { - payload: { vault }, - originAppId: sourceAppId, - originBackupAccountSession: requestSession, - originBackupWriteIntent, + const operation = beginBackupLocalOperation(sourceAppId, originBackupWriteIntent, requestSession); + const committedOperation = commitBackupLocalOperation(operation, (localRealm) => { + const stored = localRealm.objectForPrimaryKey(RealmSchema.Vault, payload.vault.id); + if (!stored) throw new Error('Vault is missing'); + stored.presentationData = { ...(getJSONFromRealmObject(stored) as unknown as Wallet).presentationData, + name: payload.details.name, description: payload.details.description }; }); - if (response.updated) { - assertWalletAccount(sourceAppId, requestSession); - yield call(dbManager.updateObjectById, RealmSchema.Vault, vault.id, { - presentationData, - }); - yield put(relayVaultUpdateSuccess()); - } else { - const errorMsg = response.error?.message - ? response.error.message.toString() - : response.error.toString(); - yield put(relayVaultUpdateFail(errorMsg)); - } - } catch (err) { - console.log('err', err); - yield put(relayVaultUpdateFail('Something went wrong!')); + if (!committedOperation.committed) throw new Error('Local vault update failed'); + yield call(updateVaultImageWorker, { payload: { vault: payload.vault, isUpdate: true }, + originAppId: sourceAppId, originBackupAccountSession: requestSession, committedOperation }); + assertWalletAccount(sourceAppId, requestSession); + yield put(relayVaultUpdateSuccess()); + return true; + } catch { + if (currentWalletAccountId() === sourceAppId && currentBackupAccountSession() === requestSession) + yield put(relayVaultUpdateFail('Vault changes could not be saved')); + return false; } } -export const updateVaultDetailsWatcher = createWatcher( - updateVaultDetailsWorker, - UPDATE_VAULT_DETAILS -); +export const updateVaultDetailsWatcher = createWatcher(updateVaultDetailsWorker, UPDATE_VAULT_DETAILS); export function* updateSignerDetailsWorker({ payload, originAppId, originBackupAccountSession, originBackupWriteIntent }) { const sourceAppId = originAppId || currentWalletAccountId(); const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); - const { - signer, - key, - value, - }: { - signer: Signer; - key: string; - value: any; - } = payload; - yield put(setRelaySignersUpdateLoading(true)); try { assertWalletAccount(sourceAppId, requestSession); - signer[key] = value; - const response = yield call(updateAppImageWorker, { - payload: { signers: [signer] }, - originAppId: sourceAppId, - originBackupAccountSession: requestSession, - originBackupWriteIntent, + const operation = beginBackupLocalOperation(sourceAppId, originBackupWriteIntent, requestSession); + const committedOperation = commitBackupLocalOperation(operation, (localRealm) => { + const stored = localRealm.objectForPrimaryKey(RealmSchema.Signer, getKeyUID(payload.signer)); + if (!stored) throw new Error('Signer is missing'); + stored[payload.key] = payload.value; }); - if (response.updated) { - assertWalletAccount(sourceAppId, requestSession); - const signerKeyUID = getKeyUID(signer); - yield call( - dbManager.updateObjectByQuery, - RealmSchema.Signer, - (realmSigner) => getKeyUID(realmSigner) === signerKeyUID, - { - [key]: value, - } - ); - yield put(relaySignersUpdateSuccess(false)); - } else { - const errorMsg = response.error?.message - ? response.error.message.toString() - : response.error.toString(); - yield put(relaySignersUpdateFail(errorMsg)); - } - } catch (err) { - console.error(err); - yield put(relaySignersUpdateFail('Something went wrong')); + if (!committedOperation.committed) throw new Error('Local signer update failed'); + yield call(updateAppImageWorker, { payload: { signers: [payload.signer] }, + originAppId: sourceAppId, originBackupAccountSession: requestSession, committedOperation }); + assertWalletAccount(sourceAppId, requestSession); + yield put(relaySignersUpdateSuccess(false)); + return true; + } catch { + if (currentWalletAccountId() === sourceAppId && currentBackupAccountSession() === requestSession) + yield put(relaySignersUpdateFail('Key changes could not be saved')); + return false; } } @@ -1758,126 +1717,70 @@ export const updateSignerDetails = createWatcher(updateSignerDetailsWorker, UPDA function* updateKeyDetailsWorker({ payload, originAppId, originBackupAccountSession, originBackupWriteIntent }) { const sourceAppId = originAppId || currentWalletAccountId(); const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); - assertWalletAccount(sourceAppId, requestSession); - const { - signer, - key, - value, - }: { - signer: VaultSigner; - key: string; - value: any; - } = payload; - - const vaultSigner = dbManager.getObjectByPrimaryId(RealmSchema.VaultSigner, 'xpub', signer.xpub); - const vaultSignerJSON: VaultSigner = vaultSigner.toJSON(); - if (key === 'registered') { - let updatedFlag = false; - const updatedRegsteredVaults = vaultSignerJSON.registeredVaults.map((info) => { - if (info.vaultId === value.vaultId) { - updatedFlag = true; - return { ...info, ...value }; - } else { - return info; + try { + assertWalletAccount(sourceAppId, requestSession); + const operation = beginBackupLocalOperation(sourceAppId, originBackupWriteIntent, requestSession); + const affectedVaultIds = new Set(); + const committedOperation = commitBackupLocalOperation(operation, (localRealm) => { + const stored = localRealm.objectForPrimaryKey(RealmSchema.VaultSigner, payload.signer.xpub); + if (!stored) throw new Error('Vault key is missing'); + if (payload.key === 'registered') { + const registrations = Array.from(stored.registeredVaults || []).map((entry: any) => ({ + vaultId: entry.vaultId, registered: entry.registered, + registrationInfo: entry.registrationInfo, hmac: entry.hmac, + })); + const previous = registrations.findIndex((entry) => entry.vaultId === payload.value.vaultId); + if (previous < 0) registrations.push(payload.value); + else registrations[previous] = { ...registrations[previous], ...payload.value }; + stored.registeredVaults = registrations; + } else stored[payload.key] = payload.value; + for (const vault of localRealm.objects(RealmSchema.Vault)) { + if (vault.signers.some((key) => key.xpub === stored.xpub)) affectedVaultIds.add(vault.id); } }); - if (!updatedFlag) { - updatedRegsteredVaults.push(value); - } - const saved = yield call(() => { - assertWalletAccount(sourceAppId, requestSession); - return dbManager.updateObjectByPrimaryId( - RealmSchema.VaultSigner, - 'xpub', - signer.xpub, - { registeredVaults: updatedRegsteredVaults } - ); - }); - if (saved !== true) { - yield put(relaySignersUpdateFail('Key registration could not be saved')); - return false; - } - // VaultSigner is shared by every vault using this xpub. Back up each fresh - // containing vault so an older copy cannot overwrite the restored metadata. - try { - const vaults: Vault[] = yield call(dbManager.getCollection, RealmSchema.Vault); - for (const vault of vaults) { - assertWalletAccount(sourceAppId, requestSession); - if (vault.signers.some((key) => key.xpub === signer.xpub)) { - const response = yield call(updateVaultImageWorker, { - payload: { vault, isUpdate: true }, - originAppId: sourceAppId, - originBackupAccountSession: requestSession, - originBackupWriteIntent, - }); - if (!response?.updated) yield call(setServerBackupFailed, sourceAppId); - } - } - } catch (error) { - yield call(setServerBackupFailed, sourceAppId); - } + if (!committedOperation.committed) throw new Error('Local key update failed'); + const mutations = Array.from(affectedVaultIds).map((vaultId) => buildAutomaticVaultMutation(vaultId, sourceAppId)); + if (payload.key === 'registered' && mutations.length) + yield call(queueCommittedBackupWorker, sourceAppId, committedOperation, mutations, requestSession); + assertWalletAccount(sourceAppId, requestSession); return true; + } catch { + if (currentWalletAccountId() === sourceAppId && currentBackupAccountSession() === requestSession) + yield put(relaySignersUpdateFail('Key changes could not be saved')); + return false; } - - const updated = yield call(() => { - assertWalletAccount(sourceAppId, requestSession); - return dbManager.updateObjectByPrimaryId(RealmSchema.VaultSigner, 'xpub', signer.xpub, { - [key]: value, - }); - }); - return updated === true; } export const updateKeyDetails = createWatcher(updateKeyDetailsWorker, UPDATE_KEY_DETAILS); -function* deleteVaultWorker({ - payload, - originAppId, - originBackupAccountSession, - originBackupWriteIntent, - onComplete, -}) { +function* deleteVaultWorker({ payload, originAppId, originBackupAccountSession, originBackupWriteIntent, onComplete }) { const sourceAppId = originAppId || currentWalletAccountId(); const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); - const { vaultId } = payload; const reportsThroughCallback = typeof onComplete === 'function'; try { assertWalletAccount(sourceAppId, requestSession); if (!reportsThroughCallback) yield put(setRelayVaultUpdateLoading(true)); - const backupEnabled = yield select((state: RootState) => - isAutomaticCloudBackupEnabled(state.bhr, sourceAppId) - ); - const response = yield call(deleteVaultImageWorker, { - payload: { vaultIds: [vaultId] }, - originAppId: sourceAppId, - originBackupAccountSession: requestSession, - originBackupWriteIntent, + const operation = beginBackupLocalOperation(sourceAppId, originBackupWriteIntent, requestSession); + const committedOperation = commitBackupLocalOperation(operation, (localRealm) => { + const stored = localRealm.objectForPrimaryKey(RealmSchema.Vault, payload.vaultId); + if (!stored) throw new Error('Vault deletion target is missing'); + localRealm.delete(stored); }); - if (response.updated) { - assertWalletAccount(sourceAppId, requestSession); - const deleted = yield call(dbManager.deleteObjectById, RealmSchema.Vault, vaultId); - const stillPresent = deleted === true - ? yield call(dbManager.getObjectById, RealmSchema.Vault, vaultId) - : true; - if (deleted !== true || stillPresent) { - if (backupEnabled) yield call(setServerBackupFailed, sourceAppId); - throw new Error('Local vault deletion failed'); - } - assertWalletAccount(sourceAppId, requestSession); - if (!reportsThroughCallback) yield put(relayVaultUpdateSuccess()); - return true; - } else { - const errorMsg = response.error?.message - ? response.error.message.toString() - : response.error.toString(); - if (!reportsThroughCallback) yield put(relayVaultUpdateFail(errorMsg)); - return false; - } - } catch (err) { + if (!committedOperation.committed) throw new Error('Local vault deletion failed'); + yield call(deleteVaultImageWorker, { payload: { vaultIds: [payload.vaultId] }, + originAppId: sourceAppId, originBackupAccountSession: requestSession, committedOperation }); + assertWalletAccount(sourceAppId, requestSession); + if (!reportsThroughCallback) yield put(relayVaultUpdateSuccess()); + return true; + } catch { if (!reportsThroughCallback && currentWalletAccountId() === sourceAppId && - store.getState().storage.appId === sourceAppId) - yield put(relayVaultUpdateFail('Something went wrong while deleting the vault!')); + currentBackupAccountSession() === requestSession) + yield put(relayVaultUpdateFail('Vault deletion could not be saved')); return false; + } finally { + if (!reportsThroughCallback && currentWalletAccountId() === sourceAppId && + currentBackupAccountSession() === requestSession) + yield put(setRelayVaultUpdateLoading(false)); } } @@ -1906,60 +1809,36 @@ export const deleteVaultyWatcher = createWatcher(deleteVaultRequestWorker, DELET function* reinstateVaultWorker({ payload, originAppId, originBackupAccountSession, originBackupWriteIntent }) { const sourceAppId = originAppId || currentWalletAccountId(); const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); - const { vaultId } = payload; try { assertWalletAccount(sourceAppId, requestSession); - yield put(setRelayVaultUpdateLoading(true)); - const vault: Vault = dbManager.getObjectById(RealmSchema.Vault, vaultId).toJSON(); - const updatedParams = { - archived: false, - isMigrating: false, - archivedId: null, - presentationData: { - ...vault.presentationData, - visibility: VisibilityType.DEFAULT, - }, - }; - const response = yield call(updateVaultImageWorker, { - originAppId: sourceAppId, - originBackupAccountSession: requestSession, - originBackupWriteIntent, - payload: { - vault: { - ...vault, - ...updatedParams, - }, - isUpdate: true, - }, - }); - const signerMap = {}; - const signingDevices: Signer[] = yield call(dbManager.getCollection, RealmSchema.Signer); - signingDevices.forEach((signer) => (signerMap[getKeyUID(signer)] = signer)); - - if (response.updated) { - assertWalletAccount(sourceAppId, requestSession); - yield call(dbManager.updateObjectById, RealmSchema.Vault, vaultId, updatedParams); - for (let i = 0; i < vault.signers.length; i++) { - yield call(updateSignerDetailsWorker, { - originAppId: sourceAppId, - originBackupAccountSession: requestSession, - originBackupWriteIntent, - payload: { - signer: signerMap[getKeyUID(vault.signers[i])], - key: 'archived', - value: false, - }, - }); + const operation = beginBackupLocalOperation(sourceAppId, originBackupWriteIntent, requestSession); + const changedSigners: Signer[] = []; + const committedOperation = commitBackupLocalOperation(operation, (localRealm) => { + const vault = localRealm.objectForPrimaryKey(RealmSchema.Vault, payload.vaultId); + if (!vault) throw new Error('Vault is missing'); + vault.archived = false; + vault.isMigrating = false; + vault.archivedId = null; + vault.presentationData = { ...(getJSONFromRealmObject(vault) as unknown as Vault).presentationData, visibility: VisibilityType.DEFAULT }; + for (const key of vault.signers) { + const stored = localRealm.objectForPrimaryKey(RealmSchema.Signer, getKeyUID(key)); + if (!stored) throw new Error('A vault signer is missing'); + stored.archived = false; + changedSigners.push(getJSONFromRealmObject(stored) as unknown as Signer); } - yield put(relayVaultUpdateSuccess()); - } else { - const errorMsg = response.error?.message - ? response.error.message.toString() - : response.error.toString(); - yield put(relayVaultUpdateFail(errorMsg)); - } - } catch (err) { - yield put(relayVaultUpdateFail('Something went wrong while deleting the vault!')); + }); + if (!committedOperation.committed) throw new Error('Local vault reinstatement failed'); + yield call(queueCommittedBackupWorker, sourceAppId, committedOperation, [ + buildAutomaticAppMutation({ signers: changedSigners }, sourceAppId), + buildAutomaticVaultMutation(payload.vaultId, sourceAppId), + ], requestSession); + assertWalletAccount(sourceAppId, requestSession); + yield put(relayVaultUpdateSuccess()); + return true; + } catch { + if (currentWalletAccountId() === sourceAppId && currentBackupAccountSession() === requestSession) + yield put(relayVaultUpdateFail('Vault changes could not be saved')); + return false; } } @@ -2040,142 +1919,67 @@ export const refreshCanaryWalletsWatcher = createWatcher( REFRESH_CANARY_VAULT ); -function* mergeSimilarKeysWorker({ - payload, - originAppId, - originBackupAccountSession, - originBackupWriteIntent, -}: { +function* mergeSimilarKeysWorker({ payload, originAppId, originBackupAccountSession, originBackupWriteIntent }: { payload: { signer: Signer }; originAppId?: string; originBackupAccountSession?: number; originBackupWriteIntent?: BackupWriteIntent; }) { + const sourceAppId = originAppId || currentWalletAccountId(); + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); try { - const sourceAppId = originAppId || currentWalletAccountId(); - const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); assertWalletAccount(sourceAppId, requestSession); - const { signer } = payload; - const signers: Signer[] = yield call(dbManager.getCollection, RealmSchema.Signer); - for (let i = 0; i < signers.length; i++) { - const s = signers[i]; - const p2wpkh = idx(s, (_) => _.signerXpubs[XpubTypes.P2WPKH][0].xpub); - const p2wsh = idx(s, (_) => _.signerXpubs[XpubTypes.P2WSH][0].xpub); - const signerp2wpkh = idx(signer, (_) => _.signerXpubs[XpubTypes.P2WPKH][0].xpub); - const signerp2wsh = idx(signer, (_) => _.signerXpubs[XpubTypes.P2WSH][0].xpub); - if ( - p2wpkh === signerp2wpkh && - p2wsh === signerp2wsh && - s.masterFingerprint !== signer.masterFingerprint - ) { - const signerKeyUID = getKeyUID(s); - yield call(() => { - assertWalletAccount(sourceAppId, requestSession); - return dbManager.updateObjectByQuery( - RealmSchema.Signer, - (realmSigner) => getKeyUID(realmSigner) === signerKeyUID, - { masterFingerprint: signer.masterFingerprint } - ); - }); - // get all keys that have the same masterFingerprint - const keys = yield call( - dbManager.getObjectByQuery, - RealmSchema.VaultSigner, - (obj) => getKeyUID(obj) === getKeyUID(s), - true // get all matching objects - ); - for (let i = 0; i < keys.length; i++) { - assertWalletAccount(sourceAppId, requestSession); - yield call( - dbManager.updateObjectByPrimaryId, - RealmSchema.VaultSigner, - 'xpub', - keys[i].xpub, - { - masterFingerprint: signer.masterFingerprint, - } - ); - } - const { primarySeed, id }: KeeperApp = yield call( - dbManager.getObjectByIndex, - RealmSchema.KeeperApp - ); - assertWalletAccount(sourceAppId, requestSession); - if (id !== sourceAppId) throw new Error('Account changed'); - const encryptionKey = generateEncryptionKey(primarySeed); - const encrytedSigner = encrypt( - encryptionKey, - JSON.stringify(sanitizeSeedKeyForBackup(signer)) - ); - const updated = yield call(Relay.migrateXfp, id, [ - { - // Relay signer maps use KeyUID, not the bare fingerprint. Keep the - // old key captured before Realm mutates the live signer object. - oldSignerId: signerKeyUID, - newSignerId: getKeyUID(signer), - newSignerDetails: encrytedSigner, - }, - ], originBackupWriteIntent); - if (updated) { - console.log( - `Signer ${s.masterFingerprint} has been merged with ${signer.masterFingerprint}` - ); - } - return true; - } - } + const operation = beginBackupLocalOperation(sourceAppId, originBackupWriteIntent, requestSession); + const existing: Signer[] = Array.from(dbManager.getCollection(RealmSchema.Signer)) + .map((signer: any) => signer.toJSON ? getJSONFromRealmObject(signer) as unknown as Signer : _.cloneDeep(signer)); + const changes = planSignerIdentityMerges(existing, [payload.signer]); + if (!changes.length) return false; + let affectedVaultIds = new Set(); + const committedOperation = commitBackupLocalOperation(operation, (localRealm) => { + affectedVaultIds = applySignerIdentityMerges(localRealm, changes); + }); + if (!committedOperation.committed) return false; + yield call(queueCommittedBackupWorker, sourceAppId, committedOperation, [ + buildAutomaticAppMutation({ signers: changes.map((change) => change.signer) }, sourceAppId), + ...Array.from(affectedVaultIds).map((vaultId) => buildAutomaticVaultMutation(vaultId, sourceAppId)), + buildSignerMigrationMutation(sourceAppId, changes), + ], requestSession); + assertWalletAccount(sourceAppId, requestSession); + return true; + } catch (error) { + captureError(error); return false; - } catch (err) { - captureError(err); } } export const mergeSimilarKeysWatcher = createWatcher(mergeSimilarKeysWorker, MERGER_SIMILAR_KEYS); -function* generateNewExternalAddressWorker({ - payload, - originAppId, - originBackupAccountSession, -}: { - payload: { - wallet: Wallet | Vault; - }; +function* generateNewExternalAddressWorker({ payload, originAppId, originBackupAccountSession, originBackupWriteIntent }: { + payload: { wallet: Wallet | Vault }; originAppId?: string; originBackupAccountSession?: number; + originBackupWriteIntent?: BackupWriteIntent; }) { const sourceAppId = originAppId || currentWalletAccountId(); const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); - if ( - !sourceAppId || - currentWalletAccountId() !== sourceAppId || - store.getState().storage.appId !== sourceAppId || - currentBackupAccountSession() !== requestSession - ) return false; - const { wallet } = payload; - const specs = { ...wallet.specs, totalExternalAddresses: wallet.specs.totalExternalAddresses + 1 }; - - const updated = yield call(() => { + try { assertWalletAccount(sourceAppId, requestSession); - return dbManager.updateObjectById( - wallet.entityKind === EntityKind.VAULT ? RealmSchema.Vault : RealmSchema.Wallet, - wallet.id, - { specs } - ); - }); - if (updated === true) { - const backupEnabled = yield select((state: RootState) => - isAutomaticCloudBackupEnabled(state.bhr, sourceAppId) - ); - if (backupEnabled) { - // This index is part of the recoverable wallet image, even though it - // does not issue an incremental relay request. - markBackupMutation(sourceAppId); - clearVerifiedRemoteRevision(sourceAppId); - yield put(invalidateBackupRepair(sourceAppId)); - yield put(setPendingAllBackup({ appId: sourceAppId, pending: true })); - } + const operation = beginBackupLocalOperation(sourceAppId, originBackupWriteIntent, requestSession); + const schema = payload.wallet.entityKind === EntityKind.VAULT ? RealmSchema.Vault : RealmSchema.Wallet; + const committedOperation = commitBackupLocalOperation(operation, (localRealm) => { + const stored = localRealm.objectForPrimaryKey(schema, payload.wallet.id); + if (!stored) throw new Error('Wallet is missing'); + stored.specs.totalExternalAddresses += 1; + }); + if (!committedOperation.committed) return false; + const mutations = schema === RealmSchema.Vault + ? [buildAutomaticVaultMutation(payload.wallet.id, sourceAppId)] + : [buildAutomaticAppMutation({ wallets: [payload.wallet as Wallet] }, sourceAppId)]; + yield call(queueCommittedBackupWorker, sourceAppId, committedOperation, mutations, requestSession); + return true; + } catch { + return false; } - return updated; } export const generateNewExternalAddressWatcher = createWatcher( @@ -2255,57 +2059,23 @@ export const fetchCollaborativeChannelWatcher = createWatcher( FETCH_COLLABORATIVE_CHANNEL ); -function* updateVaultSignerXprivWorker({ signers, originAppId, originBackupAccountSession }) { +function* updateVaultSignerXprivWorker({ signers, originAppId, originBackupAccountSession, originBackupWriteIntent }) { const sourceAppId = originAppId || currentWalletAccountId(); const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); - assertWalletAccount(sourceAppId, requestSession); - const { bitcoinNetworkType } = yield select((state: RootState) => state.settings); - for (let i = 0; i < signers.length; i++) { - const signer = signers[i]; - if (signer.type == SignerType.SEED_WORDS) { - const sXpriv = idx(signer, (_) => _.signerXpubs[XpubTypes.P2WPKH][0].xpriv); - const mXpriv = idx(signer, (_) => _.signerXpubs[XpubTypes.P2WSH][0].xpriv); - const tXpriv = idx(signer, (_) => _.signerXpubs[XpubTypes.P2WSH][0].xpriv); - if (sXpriv && mXpriv && tXpriv) { - const vaults: Vault[] = yield call( - dbManager.getObjectByIndex, - RealmSchema.Vault, - null, - true - ); - const filteredVaults = vaults.filter((vault) => vault.networkType == bitcoinNetworkType); - for (let i = 0; i < filteredVaults.length; i++) { - const vault = filteredVaults[i]; - for (let j = 0; j < vault.signers.length; j++) { - const vaultSigner = vault.signers[j]; - if (signer.id === getKeyUID(vaultSigner)) { - const purpose = parseInt( - WalletUtilities.getSignerPurposeFromPath(vaultSigner.derivationPath) - ); - const uptXpriv = - purpose == DerivationPurpose.BIP84 - ? sXpriv - : purpose == DerivationPurpose.BIP48 - ? mXpriv - : purpose == DerivationPurpose.BIP86 - ? tXpriv - : null; - yield call( - () => { - assertWalletAccount(sourceAppId, requestSession); - return dbManager.updateObjectByPrimaryId( - RealmSchema.VaultSigner, - 'xpub', - vaultSigner.xpub, - { xpriv: uptXpriv } - ); - } - ); - } - } - } - } - } + try { + assertWalletAccount(sourceAppId, requestSession); + const operation = beginBackupLocalOperation(sourceAppId, originBackupWriteIntent, requestSession); + let affectedVaultIds = new Set(); + const committedOperation = commitBackupLocalOperation(operation, (localRealm) => { + affectedVaultIds = applySignerXprivUpdates(localRealm, signers); + }); + if (!committedOperation.committed) return false; + if (affectedVaultIds.size) + yield call(queueCommittedBackupWorker, sourceAppId, committedOperation, + Array.from(affectedVaultIds).map((vaultId) => buildAutomaticVaultMutation(vaultId, sourceAppId)), requestSession); + return true; + } catch { + return false; } } diff --git a/tests/recovery/accountBinding.test.cjs b/tests/recovery/accountBinding.test.cjs index decc85b670..b0ce20abd1 100644 --- a/tests/recovery/accountBinding.test.cjs +++ b/tests/recovery/accountBinding.test.cjs @@ -12,6 +12,28 @@ function switchToOptedInB(f) { f.state.bhr.automaticCloudBackupByAppId.B = true; } +// Unit producer boundary: commit fixture records before exercising backup-only builders. +function commitRecord(f, schema, record) { + const session = f.scope.currentBackupAccountSession(); + const operation = f.scope.beginBackupLocalOperation(f.app.id, { appId: f.app.id, accountSession: session }, session); + const receipt = f.scope.commitBackupLocalOperation(operation, (raw) => raw.create(schema, record)); + assert.equal(receipt.committed, true); + return receipt; +} + +function deferEnqueue(f) { + const original = f.scope.enqueueAutomaticBackupOperation; + let release, entered = false; + const gate = new Promise((resolve) => { release = resolve; }); + f.scope.enqueueAutomaticBackupOperation = async (...args) => { + entered = true; await gate; return original(...args); + }; + return { release, wait: async () => { + for (let i = 0; i < 30 && !entered; i++) await new Promise(setImmediate); + assert.equal(entered, true, 'Source must reach the queue after local commit'); + } }; +} + test('vault creation names the same account for the revisioned Relay contract', async () => { const f = fixture(); let sent; @@ -19,7 +41,10 @@ test('vault creation names the same account for the revisioned Relay contract', sent = body; return { updated: true }; }; + const vault = { id: 'disposable-vault', signers: [], archived: false }; + const committedOperation = commitRecord(f, 'Vault', vault); const result = await f.run('updateVaultImageWorker', { + committedOperation, originAppId: f.app.id, originBackupAccountSession: 0, payload: { vault: { id: 'disposable-vault', signers: [], archived: false } }, @@ -99,8 +124,9 @@ test('new receive address invalidates a verified backup after local persistence' assert.equal(updated, true); assert.equal(f.collections.Wallet[0].specs.totalExternalAddresses, 2); - assert.deepEqual(mutations, [originAppId]); - assert.deepEqual(invalidatedRevisions, [originAppId]); + assert.ok(mutations.length >= 1); + assert.ok(mutations.every((appId) => appId === originAppId)); + assert.deepEqual(invalidatedRevisions, [], 'Ordinary local commit preserves the captured CAS base'); assert.ok(f.actions.some((action) => action.type === 'invalidateBackupRepair' && action.payload === originAppId)); assert.equal(f.state.bhr.pendingAllBackupByAppId[originAppId], true); @@ -130,6 +156,10 @@ test('dispatch gate drops stale enable and success actions while preserving acco RealmSchema: { KeeperApp: 'KeeperApp' }, dbManager: { getObjectByIndex: () => ({ id: realmAppId }) }, currentBackupAccountSession: () => session, + canAccessBackupAccount: (appId) => appId === selectedAppId && appId === realmAppId, + observeBackupConsentAction: loadTransportModule('src/services/backup/transport.ts', { + '../rest/RestClient': {}, + }).observeBackupConsentAction, captureBackupWriteIntent: (appId) => appId === selectedAppId ? { appId, accountSession: session, baseRevision: 'a'.repeat(64), baselineEpoch: 1, } : undefined, @@ -165,7 +195,7 @@ test('dispatch gate drops stale enable and success actions while preserving acco selectedAppId = 'B'; middleware({ type: 'bhr/setAutomaticCloudBackup', payload: { appId: 'A', enabled: false } }); middleware({ type: 'bhr/setBackupAllSuccess', payload: { appId: 'A', status: false } }); - assert.equal(seen.length, 4, 'A disable and success reset remain account-specific after switch'); + assert.equal(seen.length, 3, 'An old A consent choice is blocked while account-specific success reset remains allowed'); selectedAppId = 'A'; realmAppId = 'A'; middleware({ type: 'UPDATE_APP_IMAGE', payload: {} }); @@ -190,41 +220,21 @@ test('dispatch gate drops stale enable and success actions while preserving acco assert.equal(seen.at(-1).originBackupAccountSession, 3); }); -test('A to opted-in B during connectivity wait blocks upload and preserves B local wallet', async () => { +test('A local delete queued before B login cannot upload or consume B wallet with the same ID', async () => { const f = fixture(); const originAppId = f.app.id; const sharedId = 'same-id-in-both-accounts'; f.collections.Wallet.push(wallet(sharedId, enums.NetworkType.MAINNET)); - let resumeConnectivity; - f.scope.NetInfo.fetch = () => new Promise((resolve) => { resumeConnectivity = resolve; }); - const deleting = f.run('deleteAppImageEntityWorker', { - originAppId, - payload: { walletIds: [sharedId] }, - }); - for (let i = 0; i < 20 && !resumeConnectivity; i++) await new Promise(setImmediate); - assert.ok(resumeConnectivity, 'test must switch while the original account is waiting'); + const gate = deferEnqueue(f); + const deleting = f.run('deleteAppImageEntityWorker', { originAppId, payload: { walletIds: [sharedId] } }); + await gate.wait(); + assert.deepEqual(f.collections.Wallet, [], 'A local deletion is committed before queueing cloud work'); switchToOptedInB(f); - resumeConnectivity({ isConnected: true }); - const result = await deleting; - assert.equal(result.updated, false); - assert.equal(f.calls.length, 0, 'no remote delete may target either account'); + f.collections.Wallet.push(wallet(sharedId, enums.NetworkType.MAINNET)); + gate.release(); + assert.equal((await deleting).updated, false, 'Old completion cannot be attributed to B'); assert.deepEqual(f.collections.Wallet.map(({ id }) => id), [sharedId]); - - let resumeSecondCheck; - f.scope.NetInfo.fetch = () => new Promise((resolve) => { resumeSecondCheck = resolve; }); - f.app.id = originAppId; - f.app.primarySeed = 'ab'.repeat(32); - f.state.storage.appId = originAppId; - const updating = f.run('updateAppImageWorker', { - originAppId, - payload: { wallets: [wallet('A-wallet', enums.NetworkType.MAINNET)], updateNodes: true }, - }); - for (let i = 0; i < 20 && !resumeSecondCheck; i++) await new Promise(setImmediate); - assert.ok(resumeSecondCheck); - switchToOptedInB(f); - resumeSecondCheck({ isConnected: true }); - assert.equal((await updating).updated, false); - assert.equal(f.calls.length, 0, 'A payload must never be encrypted with B seed or sent to B'); + assert.equal(f.calls.length, 0, 'Old receipt must fail before Relay'); }); test('queued A transport cannot execute after opted-in B becomes active', async () => { @@ -279,28 +289,21 @@ test('queued A ciphertext cannot replace a newer server wallet after A to B to A assert.equal(calls.length, 0, 'stale ciphertext must never reach Relay'); }); -test('incremental worker rejects an A payload prepared before A to B to A', async () => { +test('incremental worker rejects a committed A receipt queued before A to B to A', async () => { const f = fixture(); const originAppId = f.app.id; let session = 1; f.scope.currentBackupAccountSession = () => session; - let resumeConnectivity; - f.scope.NetInfo.fetch = () => new Promise((resolve) => { resumeConnectivity = resolve; }); - const updating = f.run('updateAppImageWorker', { - originAppId, - originBackupAccountSession: session, - payload: { wallets: [wallet('shared', enums.NetworkType.MAINNET)] }, - }); - for (let i = 0; i < 20 && !resumeConnectivity; i++) await new Promise(setImmediate); - assert.ok(resumeConnectivity); - f.app.id = 'B'; - f.state.storage.appId = 'B'; - session += 1; - f.app.id = originAppId; - f.state.storage.appId = originAppId; - session += 1; + const saved = wallet('shared', enums.NetworkType.MAINNET); + const committedOperation = commitRecord(f, 'Wallet', saved); + const gate = deferEnqueue(f); + const updating = f.run('updateAppImageWorker', { originAppId, originBackupAccountSession: session, + committedOperation, payload: { wallets: [saved] } }); + await gate.wait(); + f.app.id = 'B'; f.state.storage.appId = 'B'; session++; + f.app.id = originAppId; f.state.storage.appId = originAppId; session++; f.remote.wallets.shared = 'newer-A-ciphertext'; - resumeConnectivity({ isConnected: true }); + gate.release(); assert.equal((await updating).updated, false); assert.equal(f.remote.wallets.shared, 'newer-A-ciphertext'); assert.equal(f.calls.length, 0); @@ -330,23 +333,20 @@ test('wallet creation paused before backup cannot upload a stale A wallet after assert.equal(f.collections.Wallet.length, 0); }); -test('vault update paused at connectivity cannot upload stale A vault after A to B to A', async () => { +test('queued committed vault update cannot upload stale A vault after A to B to A', async () => { const f = fixture(); const originAppId = f.app.id; let session = 1; f.scope.currentBackupAccountSession = () => session; - let finishConnectivity; - f.scope.NetInfo.fetch = () => new Promise((resolve) => { finishConnectivity = resolve; }); - const updating = f.run('updateVaultImageWorker', { - originAppId, - originBackupAccountSession: session, - payload: { vault: { id: 'shared', signers: [], archived: false } }, - }); - for (let i = 0; i < 20 && !finishConnectivity; i++) await new Promise(setImmediate); - assert.ok(finishConnectivity); + const saved = { id: 'shared', signers: [], archived: false }; + const committedOperation = commitRecord(f, 'Vault', saved); + const gate = deferEnqueue(f); + const updating = f.run('updateVaultImageWorker', { originAppId, originBackupAccountSession: session, + committedOperation, payload: { vault: saved } }); + await gate.wait(); f.app.id = 'B'; f.state.storage.appId = 'B'; session++; f.app.id = originAppId; f.state.storage.appId = originAppId; session++; - finishConnectivity({ isConnected: true }); + gate.release(); assert.equal((await updating).updated, false); assert.equal(f.calls.length, 0); }); @@ -356,12 +356,15 @@ test('queued session rejection cannot report a successful incremental backup', a const originAppId = f.app.id; let session = 1; f.scope.currentBackupAccountSession = () => session; + const saved = wallet('shared', enums.NetworkType.MAINNET); + const committedOperation = commitRecord(f, 'Wallet', saved); let rejectQueued; f.scope.Relay.updateAppImage = () => new Promise((_resolve, reject) => { rejectQueued = reject; }); const updating = f.run('updateAppImageWorker', { originAppId, originBackupAccountSession: session, - payload: { wallets: [wallet('shared', enums.NetworkType.MAINNET)] }, + committedOperation, + payload: { wallets: [saved] }, }); for (let i = 0; i < 20 && !rejectQueued; i++) await new Promise(setImmediate); assert.ok(rejectQueued); @@ -371,36 +374,24 @@ test('queued session rejection cannot report a successful incremental backup', a assert.equal((await updating).updated, false); }); -test('vault signer xpriv refresh cannot write an old key after A to B to A', async () => { +test('queued vault signer xpriv refresh cannot write an old key after A to B to A', async () => { const f = fixture(); const originAppId = f.app.id; let session = 1; f.scope.currentBackupAccountSession = () => session; const key = signer('AABBCCDD', enums.NetworkType.MAINNET, enums.SignerType.SEED_WORDS); key.id = 'AABBCCDD0M'; - key.signerXpubs[enums.XpubTypes.P2WPKH][0].xpriv = 'old-secret'; key.signerXpubs[enums.XpubTypes.P2WSH] = [{ xpub: 'vault-xpub', xpriv: 'old-secret' }]; - let finishVaultRead; - const originalRead = f.scope.dbManager.getObjectByIndex; - f.scope.dbManager.getObjectByIndex = (schema, ...args) => schema === 'Vault' - ? new Promise((resolve) => { finishVaultRead = resolve; }) - : originalRead(schema, ...args); - f.scope.WalletUtilities.getSignerPurposeFromPath = () => enums.DerivationPurpose.BIP84; - let writes = 0; - f.scope.dbManager.updateObjectByPrimaryId = () => { writes++; return true; }; - const updating = f.run('updateVaultSignerXprivWorker', { - signers: [key], originAppId, originBackupAccountSession: session, - }); - for (let i = 0; i < 20 && !finishVaultRead; i++) await new Promise(setImmediate); - assert.ok(finishVaultRead); + f.collections.VaultSigner = [{ xpub: 'vault-xpub', xpriv: 'newer-secret', masterFingerprint: key.masterFingerprint }]; + const queued = { signers: [key], originAppId, originBackupAccountSession: session }; f.app.id = 'B'; f.state.storage.appId = 'B'; session++; f.app.id = originAppId; f.state.storage.appId = originAppId; session++; - finishVaultRead([{ networkType: enums.NetworkType.MAINNET, - signers: [{ ...key.signerXpubs[enums.XpubTypes.P2WSH][0], - id: key.id, masterFingerprint: key.masterFingerprint, derivationPath: "m/84'/0'/0'" }], - }]); - await assert.rejects(updating, /Account changed/); + let writes = 0; + f.scope.dbManager.updateObjectByPrimaryId = () => { writes++; return true; }; + assert.equal(await f.run('updateVaultSignerXprivWorker', queued), false); assert.equal(writes, 0); + assert.equal(f.collections.VaultSigner[0].xpriv, 'newer-secret'); + assert.equal(f.calls.length, 0); }); test('per-account consent starts off, survives switches, and legacy consent needs attribution', () => { @@ -447,7 +438,7 @@ test('unconfirmed server deletion retains account consent and reports failure', action.payload.enabled === false)); }); -test('rejected entity deletion retains local wallet and queues an account repair', async () => { +test('rejected cloud entity deletion preserves completed local removal and queues an account repair', async () => { const f = fixture(); const id = f.app.id; f.collections.Wallet.push(wallet('keep', enums.NetworkType.MAINNET)); @@ -456,12 +447,12 @@ test('rejected entity deletion retains local wallet and queues an account repair originAppId: id, payload: { walletIds: ['keep'] }, }); - assert.equal(result.updated, false); - assert.deepEqual(f.collections.Wallet.map((w) => w.id), ['keep']); + assert.equal(result.updated, true); + assert.deepEqual(f.collections.Wallet, []); assert.equal(f.state.bhr.pendingAllBackupByAppId[id], true); }); -test('remote entity deletion followed by local failure reports partial deletion', async () => { +test('failed local entity deletion rolls back and makes zero remote writes', async () => { const f = fixture(); const id = f.app.id; f.collections.Wallet.push(wallet('keep', enums.NetworkType.MAINNET)); @@ -488,9 +479,9 @@ test('signer delete reports failure and never shows success after rejected remot assert.ok(!f.actions.some((action) => action.type === 'showKeyDeletedSuccessModal')); }); -test('remote vault deletion followed by failed local delete reports failure and pending repair', async () => { +test('failed local vault deletion reports failure and pending repair without remote writes', async () => { const f = fixture(); - f.scope.deleteVaultImageWorker = function* () { return { updated: true }; }; + f.collections.Vault.push({ id: 'keep-vault', signers: [] }); f.scope.dbManager.deleteObjectById = () => false; await f.run('deleteVaultWorker', { originAppId: f.app.id, payload: { vaultId: 'keep-vault' } }); assert.ok(f.actions.some((action) => action.type === 'relayVaultUpdateFail')); diff --git a/tests/recovery/deleteResultTruthfulness.test.cjs b/tests/recovery/deleteResultTruthfulness.test.cjs index cc0100372f..8b2dac69c3 100644 --- a/tests/recovery/deleteResultTruthfulness.test.cjs +++ b/tests/recovery/deleteResultTruthfulness.test.cjs @@ -6,7 +6,7 @@ const vm = require('node:vm'); const ts = require('typescript'); const { fixture, enums, wallet } = require('./sagaHarness.cjs'); -test('BTC wallet delete completion is false when the relay rejects the delete', async () => { +test('BTC wallet delete completion reflects saved local removal while a rejected cloud delete stays pending', async () => { const f = fixture(); f.collections.Wallet.push(wallet('keep', enums.NetworkType.MAINNET)); f.scope.Relay.deleteAppImageEntity = async () => ({ updated: false, error: 'rejected' }); @@ -18,9 +18,10 @@ test('BTC wallet delete completion is false when the relay rejects the delete', onComplete: (deleted) => completed.push(deleted), }); - assert.equal(result, false); - assert.deepEqual(completed, [false]); - assert.deepEqual(f.collections.Wallet.map((row) => row.id), ['keep']); + assert.equal(result, true); + assert.deepEqual(completed, [true]); + assert.deepEqual(f.collections.Wallet, []); + assert.equal(f.state.bhr.pendingAllBackupByAppId[f.app.id], true); }); test('BTC wallet delete completion is true only after the Realm object is gone', async () => { @@ -93,10 +94,10 @@ test('vault delete callback is scoped to its request and fires once after local .includes(action.type))); }); -test('vault delete callback reports failure once when Relay rejects deletion', async () => { +test('vault delete callback reports saved local removal once while cloud deletion is pending', async () => { const f = fixture(); f.collections.Vault.push({ id: 'keep' }); - f.scope.deleteVaultImageWorker = function* () { return { updated: false, error: 'rejected' }; }; + f.scope.Relay.deleteVaults = async () => ({ updated: false, error: 'rejected' }); const completed = []; const result = await f.run('deleteVaultRequestWorker', { @@ -105,9 +106,10 @@ test('vault delete callback reports failure once when Relay rejects deletion', a onComplete: (deleted) => completed.push(deleted), }); - assert.equal(result, false); - assert.deepEqual(completed, [false]); - assert.deepEqual(f.collections.Vault.map((row) => row.id), ['keep']); + assert.equal(result, true); + assert.deepEqual(completed, [true]); + assert.deepEqual(f.collections.Vault, []); + assert.equal(f.state.bhr.pendingAllBackupByAppId[f.app.id], true); assert.ok(!f.actions.some((action) => ['setRelayVaultUpdateLoading', 'relayVaultUpdateSuccess', 'relayVaultUpdateFail'] .includes(action.type))); @@ -151,9 +153,9 @@ test('overlapping vault saga requests report their own outcomes despite unrelate assert.equal(await second, true); assert.deepEqual(completed, [['second', true]]); pending.get('first')({ updated: false, error: 'rejected' }); - assert.equal(await first, false); - assert.deepEqual(completed, [['second', true], ['first', false]]); - assert.deepEqual(f.collections.Vault.map((row) => row.id), ['first']); + assert.equal(await first, true); + assert.deepEqual(completed, [['second', true], ['first', true]]); + assert.deepEqual(f.collections.Vault, []); }); test('signer delete completion is false if Realm reports success but retains the signer', async () => { diff --git a/tests/recovery/freshInstallWallet.test.cjs b/tests/recovery/freshInstallWallet.test.cjs index 9e88302b88..745d43e4cf 100644 --- a/tests/recovery/freshInstallWallet.test.cjs +++ b/tests/recovery/freshInstallWallet.test.cjs @@ -29,6 +29,8 @@ function freshInstallFixture(options = {}) { schema === 'KeeperApp' ? activeApp : originalGet(schema, ...args); f.scope.dbManager.createObject = (schema, value, ...args) => { writes.push({ schema, owner: f.state.storage.appId }); + if ((schema === 'Wallet' && options.rejectWalletWrite) || + (schema === 'Signer' && rejectKeyWrite)) return false; if (schema !== 'KeeperApp') return originalCreate(schema, value, ...args); if (options.rejectAppWrite) return false; activeApp = { ...value }; diff --git a/tests/recovery/nodeAccountActions.test.cjs b/tests/recovery/nodeAccountActions.test.cjs index dbc9190f5b..b21bf1c16c 100644 --- a/tests/recovery/nodeAccountActions.test.cjs +++ b/tests/recovery/nodeAccountActions.test.cjs @@ -32,16 +32,22 @@ const code = ts.transpileModule( { compilerOptions: { target: ts.ScriptTarget.ES2020, jsx: ts.JsxEmit.React } } ).outputText; -function fixture({ switchOnDisconnect = false } = {}) { - const state = { activeAppId: 'A', disconnected: 0, deleted: 0, updated: 0, dispatched: 0 }; +function fixture({ switchOnDisconnect = false, switchOnDisconnectRoundTrip = false } = {}) { + const state = { activeAppId: 'A', session: 1, disconnected: 0, deleted: 0, updated: 0, dispatched: 0 }; const scope = { nodeListAppId: 'A', + nodeListAccountSession: 1, + currentBackupAccountSession: () => state.session, Node: { isAccountActive: (appId) => state.activeAppId === appId, nodeConnectionStatus: () => true, disconnect: () => { state.disconnected += 1; if (switchOnDisconnect) state.activeAppId = 'B'; + if (switchOnDisconnectRoundTrip) { + state.activeAppId = 'B'; state.session++; + state.activeAppId = 'A'; state.session++; + } }, delete: () => { state.deleted += 1; @@ -74,6 +80,50 @@ test('delayed node confirmation from A cannot act on B', async () => { assert.equal(f.state.dispatched, 0); }); +test('node confirmation from the first A session cannot delete or disconnect after A to B to A', async () => { + const f = fixture(); + f.state.activeAppId = 'B'; f.state.session++; + f.state.activeAppId = 'A'; f.state.session++; + const selected = { id: 7, host: 'a.example', port: 50002 }; + await f.handlers.onDisconnectToNode(selected, 'A', 1); + await f.handlers.onDelete(selected, 'A', 1); + assert.equal(f.state.disconnected, 0); + assert.equal(f.state.deleted, 0); + assert.equal(f.state.updated, 0); + assert.equal(f.state.dispatched, 0); +}); + +test('account roundtrip during node disconnect cannot update the later A session', async () => { + const f = fixture({ switchOnDisconnectRoundTrip: true }); + await f.handlers.onDisconnectToNode({ id: 7, host: 'a.example', port: 50002 }, 'A', 1); + assert.equal(f.state.disconnected, 1); + assert.equal(f.state.updated, 0); + assert.equal(f.state.dispatched, 0); +}); + +test('saved node callback retained from first A render cannot save under the later A session', async () => { + const selectedFile = 'src/screens/AppSettings/Node/NodeSelection.tsx'; + const selectedSource = ts.createSourceFile(selectedFile, + fs.readFileSync(path.join(__dirname, '../..', selectedFile), 'utf8'), + ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX); + const selectedComponent = selectedSource.statements.find((statement) => + ts.isFunctionDeclaration(statement) && statement.name?.text === 'NodeSelection'); + const callback = selectedComponent.body.statements.find((statement) => + ts.isVariableStatement(statement) && statement.declarationList.declarations.some((declaration) => + declaration.name.getText(selectedSource) === 'onSaveCallback')); + let writes = 0; + const selectedScope = { + renderedAppId: 'A', renderedAccountSession: 1, + currentBackupAccountSession: () => 3, + Node: { isAccountActive: (id) => id === 'A', save: () => { writes++; } }, + beginBackupLocalOperation: () => { writes++; }, + }; + vm.runInNewContext(ts.transpileModule(`${callback.getText(selectedSource)};globalThis.save = onSaveCallback;`, + { compilerOptions: { target: ts.ScriptTarget.ES2020, jsx: ts.JsxEmit.React } }).outputText, selectedScope); + await selectedScope.save({ id: 7, host: 'a.example', port: 50002 }); + assert.equal(writes, 0); +}); + test('node disconnect switching accounts cannot update the new Realm', async () => { const f = fixture({ switchOnDisconnect: true }); await f.handlers.onDisconnectToNode({ id: 7, host: 'a.example', port: 50002 }, 'A'); diff --git a/tests/recovery/usdtAccountDelete.test.cjs b/tests/recovery/usdtAccountDelete.test.cjs index f3e8218f98..50fc26d710 100644 --- a/tests/recovery/usdtAccountDelete.test.cjs +++ b/tests/recovery/usdtAccountDelete.test.cjs @@ -115,6 +115,59 @@ function fixture({ consent = true, creationPaused = true, switchDuringRemote = f setError: (error) => state.errors.push(error), captureError: () => {}, }; + // Unit-only disposable transaction adapter. The native Realm/HTTP suite + // separately verifies the production commit receipt and transport proof. + const rows = { wallet: { id: 'wallet', name: 'old' } }; + scope.beginBackupLocalOperation = (appId, intent, session) => ({ appId, intent, session }); + scope.commitBackupLocalOperation = (operation, write) => { + if (state.active !== operation.appId || state.session !== operation.session) return { committed: false }; + const savedRows = JSON.parse(JSON.stringify(rows)); + const savedLog = state.local.length; + try { + write({ + objectForPrimaryKey: (_schema, id) => rows[id] && new Proxy(rows[id], { + set(target, key, value) { + state.local.push({ appId: state.active, update: { [key]: value } }); + target[key] = value; + if (switchDuringUpdate) state.active = 'B'; + if (switchDuringUpdateRoundTrip) { + state.active = 'B'; state.session += 1; + state.active = 'A'; state.session += 1; + } + return true; + }, + }), + create: (_schema, wallet) => { + rows[wallet.id] = { ...wallet }; + state.local.push({ appId: state.active, wallet }); + }, + delete: (row) => { + if (pretendLocalDelete) throw Error('Injected transaction failure'); + delete rows[row.id]; + state.local.push({ appId: state.active, walletId: row.id }); + }, + }); + // Model the production commit observer at the producer boundary, before + // any cloud wait. Keep stale completion UI out of a newer account session. + state.invalidated.push(operation.appId); + if (state.active === operation.appId && state.session === operation.session) + scope.dispatch(scope.setPendingAllBackup({ appId: operation.appId, pending: true })); + return { committed: true, intent: { ...operation.intent, accountSession: operation.session } }; + } catch { + Object.keys(rows).forEach((id) => delete rows[id]); Object.assign(rows, savedRows); + state.local.splice(savedLog); + state.invalidated.push(operation.appId); + scope.dispatch(scope.setPendingAllBackup({ appId: operation.appId, pending: true })); + return { committed: false }; + } + }; + scope.enqueueAutomaticBackupOperation = async (receipt, mutations) => { + if (!receipt.committed || receipt.intent?.accountSession !== state.session || state.active !== receipt.intent.appId) + return { updated: false, backupPending: true, backupConfirmed: false }; + const result = await scope.Relay.deleteAppImageEntity(mutations[0].body, receipt.intent); + const current = receipt.intent.accountSession === state.session && state.active === receipt.intent.appId; + return { ...result, backupConfirmed: current, backupPending: !current }; + }; vm.runInNewContext(`${code}\nglobalThis.callbacks = { createWallet, deleteWallet, updateWallet, syncAccountStatus };`, scope); return { state, ...scope.callbacks }; } @@ -126,36 +179,36 @@ test('unconsented USDT wallet deletes locally without a relay write', async () = assert.equal(f.state.local[0].appId, 'A'); }); -test('USDT delete does not report success when Realm reports true but retains the wallet', async () => { +test('USDT delete rolls back a local transaction failure before any cloud request', async () => { const f = fixture({ pretendLocalDelete: true }); assert.equal(await f.deleteWallet('wallet'), false); - assert.equal(f.state.remote.length, 1); - assert.equal(f.state.local.length, 1); + assert.equal(f.state.remote.length, 0); + assert.equal(f.state.local.length, 0); assert.deepEqual(f.state.invalidated, ['A']); assert.ok(f.state.dispatched.some((action) => action.type === 'SET_PENDING_ALL_BACKUP')); }); -test('remote response after account switch cannot delete B local wallet', async () => { +test('cloud response after account switch preserves the completed A deletion and never deletes B', async () => { const f = fixture({ switchDuringRemote: true }); - assert.equal(await f.deleteWallet('wallet'), false); + assert.equal(await f.deleteWallet('wallet'), true); assert.equal(f.state.remote.length, 1); assert.equal(f.state.remote[0].appId, 'A'); assert.equal(f.state.remote[0].accountSession, 1); - assert.deepEqual(f.state.local, []); - assert.deepEqual(f.state.invalidated, ['A']); + assert.equal(f.state.local.length, 1); + assert.equal(f.state.local[0].appId, 'A'); assert.equal(f.state.dispatched.find((action) => action.type === 'SET_PENDING_ALL_BACKUP').payload.appId, 'A'); }); -test('remote response after A to B to A cannot delete the new A session wallet', async () => { +test('cloud response after A to B to A keeps the original committed deletion pending', async () => { const f = fixture({ switchDuringRemoteRoundTrip: true }); - assert.equal(await f.deleteWallet('wallet'), false); + assert.equal(await f.deleteWallet('wallet'), true); assert.equal(f.state.active, 'A'); assert.equal(f.state.session, 3); assert.equal(f.state.remote.length, 1); assert.equal(f.state.remote[0].appId, 'A'); assert.equal(f.state.remote[0].accountSession, 1); - assert.deepEqual(f.state.local, []); - assert.deepEqual(f.state.invalidated, ['A']); + assert.equal(f.state.local.length, 1); + assert.equal(f.state.local[0].appId, 'A'); assert.equal(f.state.dispatched.find((action) => action.type === 'SET_PENDING_ALL_BACKUP').payload.appId, 'A'); }); @@ -221,7 +274,7 @@ test('when setup resumes, wallet create dispatches backup with captured originat const result = await f.createWallet({ type: 'default', name: 'new', description: '' }); assert.equal(result.newWallet.id, 'wallet'); assert.equal(f.state.local[0].appId, 'A'); - assert.equal(f.state.dispatched[0].originAppId, 'A'); + assert.equal(f.state.dispatched.find((action) => action.type === 'UPDATE_APP_IMAGE').originAppId, 'A'); }); test('wallet update after account switch cannot dispatch backup for B', async () => { diff --git a/tests/recovery/usdtBackup.test.cjs b/tests/recovery/usdtBackup.test.cjs index be56087201..afa028531a 100644 --- a/tests/recovery/usdtBackup.test.cjs +++ b/tests/recovery/usdtBackup.test.cjs @@ -47,6 +47,14 @@ function hookFixture(options = {}) { return true; }, }; + const updateStored = f.scope.dbManager.updateObjectById; + f.scope.dbManager.updateObjectById = (schema, id, patch) => { + if (schema !== 'USDTWallet') return updateStored(schema, id, patch); + assert.equal('id' in patch, false, 'Realm primary key must not be updated'); + const stored = f.collections.USDTWallet.find((entry) => entry.id === id); + Object.assign(stored, copy(patch)); + return true; + }; const updateWallet = vm.runInNewContext(ts.transpileModule(`(${callback})`, { compilerOptions: { target: ts.ScriptTarget.ES2020 }, }).outputText, { @@ -58,6 +66,14 @@ function hookFixture(options = {}) { renderAccountSession: 1, currentBackupAccountSession: () => 1, captureBackupWriteIntent: (appId) => appId === f.app.id ? writeIntent : undefined, + beginBackupLocalOperation: (...args) => f.scope.beginBackupLocalOperation(...args), + commitBackupLocalOperation: (operation, callback) => { + order.push('persist'); + return f.scope.commitBackupLocalOperation(operation, (rawRealm) => { + if (options.failPersistence) throw new Error('Injected atomic local failure'); + callback(rawRealm); + }); + }, isOriginCurrent: () => f.app.id === f.state.storage.appId, markBackupMutation: (id) => mutations.push(id), invalidateBackupRepair: (payload) => ({ type: 'invalidateBackupRepair', payload }), @@ -121,7 +137,6 @@ for (const type of ['DEFAULT', 'IMPORTED']) { const f = hookFixture(); const original = usdtWallet(type); f.collections.USDTWallet.push(copy(original)); - await f.run('updateAppImageWorker', { payload: { wallets: [original] } }); const edited = { ...original, presentationData: { name: 'After edit', description: 'Updated description', visibility: 'HIDDEN' } }; assert.equal(await f.update(edited), true); diff --git a/tests/services/manualNodeFailover.test.ts b/tests/services/manualNodeFailover.test.ts index 0a5b3e88d1..99811c99f6 100644 --- a/tests/services/manualNodeFailover.test.ts +++ b/tests/services/manualNodeFailover.test.ts @@ -2,6 +2,7 @@ import Node from '../../src/services/electrum/node'; import ElectrumClient from '../../src/services/electrum/client'; import dbManager from '../../src/storage/realm/dbManager'; import { store } from '../../src/store/store'; +import { observeBackupAccount } from '../../src/services/backup/transport'; import { predefinedMainnetNodes, predefinedTestnetNodes, @@ -26,6 +27,7 @@ describe('manual Electrum selection', () => { beforeEach(() => { activeAppId = 'A'; + observeBackupAccount(activeAppId); (store.getState as jest.Mock).mockImplementation(() => ({ storage: { appId: activeAppId } })); (dbManager.getObjectByIndex as jest.Mock).mockImplementation(() => ({ id: activeAppId })); }); @@ -85,4 +87,19 @@ describe('manual Electrum selection', () => { expect(getAllNodes).not.toHaveBeenCalled(); expect(ElectrumClient.setFailoverPeers).not.toHaveBeenCalled(); }); + + it('does not equip old saved peers when the connection returns after A to B to A', async () => { + const selected = predefinedTestnetNodes[0]; + let finishConnection: (result: object) => void; + const connection = new Promise((resolve) => { finishConnection = resolve; }); + (ElectrumClient.connect as jest.Mock).mockReturnValue(connection); + const getAllNodes = jest.spyOn(Node, 'getAllNodes'); + const result = Node.connectToSelectedNode(selected); + activeAppId = 'B'; observeBackupAccount(activeAppId); + activeAppId = 'A'; observeBackupAccount(activeAppId); + finishConnection({ connected: true, connectedTo: selected.host, generation: 2 }); + await expect(result).resolves.toEqual({ connected: false, connectedTo: undefined, error: 'Account changed' }); + expect(getAllNodes).not.toHaveBeenCalled(); + expect(ElectrumClient.setFailoverPeers).not.toHaveBeenCalled(); + }); }); From 74b90cfdb825370a5f62f868cf43d85ffb2f87e8 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Tue, 13 Oct 2026 03:54:14 +0000 Subject: [PATCH 166/254] test: verify native signer corrections and registration backup fanout --- .../automaticSignerProducer.integration.cjs | 404 ++++++++++++++++++ 1 file changed, 404 insertions(+) create mode 100644 tests/recovery/automaticSignerProducer.integration.cjs diff --git a/tests/recovery/automaticSignerProducer.integration.cjs b/tests/recovery/automaticSignerProducer.integration.cjs new file mode 100644 index 0000000000..bef60ff01c --- /dev/null +++ b/tests/recovery/automaticSignerProducer.integration.cjs @@ -0,0 +1,404 @@ +// Real signer producers, native encrypted Realm, Redux, AES and transactional +// HTTP Relay. Public disposable derivation/compiler fixtures never sign funds. +const assert = require('node:assert/strict'); +const { test, before, after } = require('node:test'); +const BIP32Factory = require('bip32').default; +const vm = require('node:vm'); +const ts = require('typescript'); +const { networks } = require('bitcoinjs-lib'); +const { + createRealmClient, + declarations, + evaluate, + plain, + Realm, +} = require('./automaticWriteRealmRelay.fixture.cjs'); +const fixtureModule = process.env.KEEPER_RELAY_FIXTURE_MODULE; +assert.ok(fixtureModule, 'Set KEEPER_RELAY_FIXTURE_MODULE to the disposable Relay fixture'); +const { createFixture } = require(fixtureModule); +let server; +before(async () => { + server = await createFixture(); +}); +after(async () => { + await server?.close(); + Realm.shutdown(); +}); +const writes = () => server.trace.filter((entry) => entry.path === '/revisionedBackupMutation'); +const names = [ + 'currentWalletAccountId', + 'assertWalletAccount', + 'rewriteSignerFingerprintReferences', + 'planSignerIdentityMerges', + 'applySignerIdentityMerges', + 'applySignerXprivUpdates', + 'buildSignerMigrationMutation', + 'mergeSimilarKeysWorker', + 'updateKeyDetailsWorker', +]; + +function publicKey(f, seedByte = 11) { + const ecc = f.load('src/services/wallets/operations/taproot-utils/noble_ecc').default; + const root = BIP32Factory(ecc).fromSeed(Buffer.alloc(32, seedByte), networks.testnet); + const derivationPath = "m/48'/1'/0'/2'"; + const child = root.derivePath(derivationPath); + return { + masterFingerprint: root.fingerprint.toString('hex').toUpperCase(), + xfp: child.fingerprint.toString('hex').toUpperCase(), + xpub: child.neutered().toBase58(), + derivationPath, + }; +} + +function loadHostProducers(scope) { + // Native Realm's Mixed conversion requires this host's Object constructor. + // Compile the unchanged production declarations into the same JS host as + // Realm rather than wrapping/normalizing their native writes or schemas. + const bindings = Object.keys(scope).filter( + (name) => name !== 'default' && /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(name) + ); + const code = ts.transpileModule(declarations('src/store/sagas/wallets.ts', names), { + compilerOptions: { target: ts.ScriptTarget.ES2020 }, + }).outputText; + const factory = vm.runInThisContext( + '(function (' + bindings.join(',') + ') {\n' + code + '\nreturn {' + names.join(',') + '};\n})', + { filename: 'actual-host-signer-producers.ts' } + ); + Object.assign(scope, factory(...bindings.map((name) => scope[name]))); +} + +function miniscript(f, key) { + const scope = { + ...f.enums, + ...f.utilities, + ...f.load('src/services/wallets/operations/miniscript/policy-generator'), + ...f.load('src/services/wallets/operations/miniscript/miniscript'), + }; + evaluate( + declarations('src/utils/service-utilities/utils.ts', ['getDerivationPath']), + scope, + 'public-derivation-path.ts' + ); + evaluate( + declarations('src/services/wallets/operations/miniscript/default/EnhancedVault.ts', [ + 'INHERITANCE_KEY_IDENTIFIER', + 'EMERGENCY_KEY_IDENTIFIER', + 'generateEnhancedVaultElements', + ]), + scope, + 'actual-enhanced-policy.ts' + ); + evaluate( + declarations('src/services/wallets/factories/VaultFactory.ts', ['generateMiniscriptScheme']), + scope, + 'actual-miniscript-scheme.ts' + ); + const elements = scope.generateEnhancedVaultElements([key], [], [], { m: 1, n: 1 }, 1800000000); + const scheme = scope.generateMiniscriptScheme(elements, [f.enums.MiniscriptTypes.TIMELOCKED]); + assert.ok(scheme.miniscriptPolicy.includes('after(1800000000)')); + assert.ok(scheme.miniscript); + return scheme; +} + +async function setup(t, collision = false) { + await server.reset(); + const f = await createRealmClient(server.url); + t.after(() => f.close()); + f.automatic = f.load('src/services/backup/automatic'); + f.installProductionStoreObservers(f.automatic); + f.store.dispatch(f.bhr.setAutomaticCloudBackup({ appId: f.appId, enabled: true })); + f.correctKey = publicKey(f); + f.oldFingerprint = '00000001'; + f.oldKey = { ...f.correctKey, masterFingerprint: f.oldFingerprint }; + const signer = f.defaults(f.RealmSchema.Signer, { + masterFingerprint: f.oldFingerprint, + type: f.enums.SignerType.COLDCARD, + networkType: f.enums.NetworkType.TESTNET, + storageType: f.enums.SignerStorage.COLD, + signerName: 'Disposable public fixture', + signerXpubs: f.defaults(f.RealmSchema.SignerXpubs, { + [f.enums.XpubTypes.P2WSH]: [ + { xpub: f.correctKey.xpub, xpriv: null, derivationPath: f.correctKey.derivationPath }, + ], + }), + }); + signer.id = f.utilities.getKeyUID(signer); + f.oldId = signer.id; + f.correctSigner = { ...signer, masterFingerprint: f.correctKey.masterFingerprint }; + f.correctSigner.id = f.utilities.getKeyUID(f.correctSigner); + f.newId = f.correctSigner.id; + assert.notEqual(f.oldId, f.newId); + assert.equal(f.dbManager.createObject(f.RealmSchema.Signer, signer), true); + if (collision) { + f.collision = { + ...f.correctSigner, + signerName: 'Existing unrelated target', + signerXpubs: f.defaults(f.RealmSchema.SignerXpubs, { + [f.enums.XpubTypes.P2WSH]: [ + { xpub: publicKey(f, 12).xpub, xpriv: null, derivationPath: f.correctKey.derivationPath }, + ], + }), + }; + assert.equal(f.dbManager.createObject(f.RealmSchema.Signer, f.collision), true); + } + const compiled = plain(miniscript(f, f.oldKey)); + for (const [id, archived] of [ + ['signer-live-vault', false], + ['signer-archived-vault', true], + ]) { + assert.equal( + f.dbManager.createObject( + f.RealmSchema.Vault, + f.defaults(f.RealmSchema.Vault, { + id, + archived, + entityKind: f.enums.EntityKind.VAULT, + type: f.enums.VaultType.MINISCRIPT, + networkType: f.enums.NetworkType.TESTNET, + isMultiSig: true, + scriptType: 'P2WSH', + scheme: f.defaults(f.RealmSchema.VaultScheme, { + m: 1, + n: 1, + multisigScriptType: f.enums.MultisigScriptType.MINISCRIPT_MULTISIG, + miniscriptScheme: compiled, + }), + signers: [ + f.defaults(f.RealmSchema.VaultSigner, { + ...f.oldKey, + xpriv: null, + registeredVaults: [], + }), + ], + presentationData: f.defaults(f.RealmSchema.VaultPresentationData, { + name: id, + visibility: archived ? 'HIDDEN' : 'DEFAULT', + }), + specs: f.defaults(f.RealmSchema.VaultSpecs, { + xpubs: [f.correctKey.xpub], + nextFreeAddressIndex: 1, + }), + }) + ), + true + ); + } + await server.seed(await f.payloadForCurrentImage()); + f.sagas = f.productionSagas( + [ + 'currentBackupAppId', + 'backupInspectionOwners', + 'runBackupInspection', + 'buildAutomaticAppMutation', + 'buildAutomaticVaultMutation', + 'queueCommittedBackupWorker', + ], + { _: require('lodash') } + ); + loadHostProducers(f.sagas.scope); + assert.equal(await f.sagas.run('runBackupInspection', false, f.appId), true); + f.before = await f.repair.captureBackupImage(); + f.origin = { + originAppId: f.appId, + originBackupAccountSession: f.transport.currentBackupAccountSession(), + originBackupWriteIntent: f.transport.captureBackupWriteIntent(f.appId), + }; + assert.ok(f.origin.originBackupWriteIntent); + f.merge = (origin = f.origin) => + f.sagas.run('mergeSimilarKeysWorker', { + ...origin, + payload: { signer: plain(f.correctSigner) }, + }); + f.register = () => + f.sagas.run('updateKeyDetailsWorker', { + ...f.origin, + payload: { + signer: f.oldKey, + key: 'registered', + value: { + vaultId: 'signer-live-vault', + registered: true, + registrationInfo: 'public-fixture-registration', + hmac: 'public-fixture-hmac', + }, + }, + }); + f.remoteImage = async () => + f.image.decodeImage( + f.encryption.generateEncryptionKey(f.seed), + await server.snapshot(f.appId), + f.appId, + () => {}, + true + ); + server.clearTrace(); + return f; +} + +function assertPending(f) { + const bhr = f.store.getState().bhr; + assert.equal(bhr.pendingAllBackupByAppId[f.appId], true); + assert.notEqual(bhr.backupRepairCompletedByAppId[f.appId], true); + assert.notEqual(bhr.backupRepairStateByAppId[f.appId], 'verified'); +} + +async function assertMergedLocal(f) { + const image = await f.repair.captureBackupImage(); + assert.ok(image.signers[f.newId]); + assert.equal(image.signers[f.oldId], undefined); + for (const id of ['signer-live-vault', 'signer-archived-vault']) { + const before = f.before.vaults[id], + after = image.vaults[id]; + const original = before.scheme.miniscriptScheme, + corrected = after.scheme.miniscriptScheme; + assert.equal(after.signers[0].masterFingerprint, f.correctKey.masterFingerprint); + for (const field of ['xpub', 'xfp', 'derivationPath', 'xpriv', 'registeredVaults']) + assert.deepEqual(after.signers[0][field], before.signers[0][field]); + assert.equal(corrected.miniscript, original.miniscript); + assert.equal(corrected.miniscriptPolicy, original.miniscriptPolicy); + assert.deepEqual(corrected.miniscriptElements.timelocks, original.miniscriptElements.timelocks); + assert.deepEqual(corrected.miniscriptElements.signerFingerprints, { + K1: f.correctKey.masterFingerprint, + }); + assert.ok( + corrected.miniscriptElements.keysInfo.every((key) => + key.descriptor.startsWith('[' + f.correctKey.masterFingerprint + '/') + ) + ); + for (const phase of corrected.miniscriptElements.phases) + for (const path of phase.paths) + for (const key of path.keys) + assert.ok(key.descriptor.startsWith('[' + f.correctKey.masterFingerprint + '/')); + assert.ok( + Object.values(corrected.keyInfoMap).every((value) => + value.startsWith('[' + f.correctKey.masterFingerprint + '/') + ) + ); + assert.deepEqual(Object.keys(corrected.keyInfoMap), Object.keys(original.keyInfoMap)); + assert.equal(after.archived, before.archived); + } + return image; +} + +test('native signer correction updates new identity and live/archive origins before removing old remote identity', async (t) => { + const f = await setup(t); + assert.equal(await f.merge(), true); + const local = await assertMergedLocal(f); + assert.deepEqual( + writes().map((entry) => entry.operation), + ['updateAppImage', 'updateVaultImage', 'updateVaultImage', 'migrateXfps'] + ); + assert.ok(writes().every((entry) => entry.committed)); + const remote = await f.remoteImage(); + assert.equal(await f.image.compareImages(local, remote), 'matched'); + assert.equal(remote.signers[f.oldId], undefined); + assert.ok(remote.signers[f.newId]); + assert.equal(f.store.getState().bhr.backupRepairCompletedByAppId[f.appId], true); + assert.equal(f.store.getState().bhr.pendingAllBackupByAppId[f.appId], false); +}); + +for (const mode of ['409', 'lost-reply']) + test(`native signer correction ${mode} keeps complete local correction and remote prefix pending`, async (t) => { + const f = await setup(t); + server.armFault({ mode, operation: mode === '409' ? 'updateVaultImage' : 'updateAppImage' }); + assert.equal(await f.merge(), true); + await assertMergedLocal(f); + assert.equal(writes().length, mode === '409' ? 2 : 1); + assert.equal(writes()[0].committed, true); + const remote = await f.remoteImage(); + assert.ok(remote.signers[f.oldId], 'old identity survives an incomplete fanout'); + assert.ok(remote.signers[f.newId], 'new identity was added without removing old identity'); + assert.equal(remote.vaults['signer-live-vault'].signers[0].masterFingerprint, f.oldFingerprint); + assertPending(f); + }); + +test('native signer identity collision rolls back without replacing the existing target or any vault', async (t) => { + const f = await setup(t, true); + assert.equal(await f.merge(), false); + assert.equal(writes().length, 0); + assert.equal( + await f.image.compareImages(f.before, await f.repair.captureBackupImage()), + 'matched' + ); + assert.equal( + f.dbManager.getObjectById(f.RealmSchema.Signer, f.newId).signerName, + 'Existing unrelated target' + ); +}); + +test('native final signer removal rejection keeps both remote identities and every corrected vault recoverable', async (t) => { + const f = await setup(t); + server.armFault({ mode: '409', operation: 'migrateXfps' }); + assert.equal(await f.merge(), true); + await assertMergedLocal(f); + assert.equal(writes().length, 4); + assert.ok( + writes() + .slice(0, 3) + .every((entry) => entry.committed) + ); + assert.equal(writes()[3].committed, false); + const remote = await f.remoteImage(); + assert.ok(remote.signers[f.oldId]); + assert.ok(remote.signers[f.newId]); + for (const id of ['signer-live-vault', 'signer-archived-vault']) + assert.equal(remote.vaults[id].signers[0].masterFingerprint, f.correctKey.masterFingerprint); + assertPending(f); +}); + +test('native registration updates the persisted shared key and both live/archive ciphertexts', async (t) => { + const f = await setup(t); + assert.equal(await f.register(), true); + const local = await f.repair.captureBackupImage(); + assert.deepEqual( + writes().map((entry) => entry.operation), + ['updateVaultImage', 'updateVaultImage'] + ); + assert.ok(writes().every((entry) => entry.committed)); + for (const id of ['signer-live-vault', 'signer-archived-vault']) { + const registrations = local.vaults[id].signers[0].registeredVaults; + assert.equal(registrations.length, 1); + assert.equal(registrations[0].registered, true); + assert.equal(registrations[0].registrationInfo, 'public-fixture-registration'); + assert.equal(registrations[0].hmac, 'public-fixture-hmac'); + assert.deepEqual(local.vaults[id].scheme, f.before.vaults[id].scheme); + } + assert.equal(await f.image.compareImages(local, await f.remoteImage()), 'matched'); + assert.equal(f.store.getState().bhr.backupRepairCompletedByAppId[f.appId], true); +}); + +test('native registration rejected during second vault CAS preserves local registration and remains pending', async (t) => { + const f = await setup(t); + server.armFault({ mode: '409', operation: 'updateVaultImage', after: 1 }); + assert.equal(await f.register(), true); + const local = await f.repair.captureBackupImage(); + assert.equal(local.vaults['signer-live-vault'].signers[0].registeredVaults[0].registered, true); + assert.equal( + local.vaults['signer-archived-vault'].signers[0].registeredVaults[0].registered, + true + ); + assert.equal(writes().length, 2); + assert.equal(writes()[0].committed, true); + assert.equal(writes()[1].committed, false); + assertPending(f); +}); + +test('native signer producer keeps local correction without an originating base and sends no HTTP mutation', async (t) => { + const f = await setup(t); + assert.equal(await f.merge({ ...f.origin, originBackupWriteIntent: undefined }), true); + await assertMergedLocal(f); + assert.equal(writes().length, 0); + assertPending(f); +}); + +test('native signer producer rejects the old account session after A to B to A', async (t) => { + const f = await setup(t); + await f.selectAccount('disposable-other-account'); + await f.selectAccount(f.appId); + assert.equal(await f.merge(), false); + assert.equal(writes().length, 0); + assert.equal( + await f.image.compareImages(f.before, await f.repair.captureBackupImage()), + 'matched' + ); +}); From a7f7399f21395949e324b5cddb11739cbb8f0ede Mon Sep 17 00:00:00 2001 From: Codex Date: Tue, 13 Oct 2026 03:56:17 +0000 Subject: [PATCH 167/254] docs: prepare exact-source automatic backup Local handoff --- release/r4-automatic-backup-local-handoff.md | 267 +++++++++++++++++++ 1 file changed, 267 insertions(+) create mode 100644 release/r4-automatic-backup-local-handoff.md diff --git a/release/r4-automatic-backup-local-handoff.md b/release/r4-automatic-backup-local-handoff.md new file mode 100644 index 0000000000..5123730330 --- /dev/null +++ b/release/r4-automatic-backup-local-handoff.md @@ -0,0 +1,267 @@ +# R4 / 2.6.4 automatic backup: Codex Local handoff + +State: **NEEDS_LOCAL**. App source: **APP_HEAD_SHA**, PR [#7045](https://github.com/KeeperCommunity/bitcoin-keeper/pull/7045), branch `codex/r4-264-combined-candidate`, base `sprint`. The coordinator replaces `APP_HEAD_SHA` in the handoff prompt after the final Cloud push; Local must compare it with the live PR head before testing. + +Paired disposable Relay: **`b600416e5ba7325927d7d2871cff7776f4e593fe`**, draft [Relay #4](https://github.com/KeeperCommunity/Relay/pull/4). This pin supersedes the earlier `5a42a03` fixture by adding explicit malformed-acknowledgement response overrides after actual commits. It is a loopback test dependency, not a deployed-service assertion. The approved contributor manifest in `dev/local-backend/sources.lock.json` remains Relay `ee935359f22ff7932ea0a62e9084d4b49801f4aa` and SigningServer `3cc7728c27328e7b0358d1f91a32d8d13feba90a`; do not replace those pins or overwrite generated contributor checkouts to prepare this test. + +`AUTOMATIC_BACKUP_SOURCE_READY` must remain false until the complete genuine source matrix passes and its evidence is reviewed. `AUTOMATIC_BACKUP_RELAY_ROLLOUT_READY` remains false until the separately authorized deployed fleet, transactions/indexes, v2 route and old-writer drainage are verified. The Node fixture permits its explicit loopback test target. Neither permission nor a simulator test authorizes hosted writes. Do not flip either gate to make a test pass; a source-ready development build can exercise the separate loopback native fixture without enabling hosted rollout. + +The historical [Local report](https://github.com/KeeperCommunity/bitcoin-keeper/pull/7045#issuecomment-6128906958) at `b160656218dc6065ad12af9db220c974255bfc23` is **LOCAL_FAILED**. Its unsigned iOS simulator compile and Android development build passed; install, launch and UI scenarios were not run because the Mac was locked. Seven automatic writes remained disabled and opted-in deletions remained blocked. Its physical deferral and earlier native evidence do not validate a newer SHA. No current app native pass is claimed by this document. + +The combined source also retains the bracketed iOS import and shared PIN keypad changes assembled at `bb66cea459fbb00ec33c5c4ebd601d07c0d7c125` from [#7051](https://github.com/KeeperCommunity/bitcoin-keeper/pull/7051) and [#7052](https://github.com/KeeperCommunity/bitcoin-keeper/pull/7052). Their contributor reports used local dependency-lock changes outside those PRs. Validate the final combined dependency set and native behavior again; Jest rendering does not prove Fabric keypad visibility. + +## Copy into Codex Local + +```text +Validate Keeper PR #7045 on APP_HEAD_SHA. + +Read the latest PR description, newest Codex local handoff, AGENTS.md and +release/r4-automatic-backup-local-handoff.md at the final head. Read the complete +automatic-write-validation.md matrix. Recheck the live PR head; if it differs, +report the mismatch and obtain the current handoff rather than testing stale code. + +Preserve every existing checkout, installed owner app, wallet and database. Use +new isolated checkouts, simulator/emulator state and disposable test accounts. +Use paired public Relay b600416e5ba7325927d7d2871cff7776f4e593fe separately from +the approved contributor bootstrap. Do not edit its source or replace the +approved sources.lock.json pins. Save exact source identities and private logs. + +Run the Node 22 genuine native-Realm/transactional-Relay runner and all seven +operations through C1-C10, complete producer/fanout and clean Recovery Key +restore cases. Missing native bindings, a skipped matrix or substituted mocks +are BLOCKED/FAIL, never PASS. Run relevant regression/release checks. + +On the exact final app source, build unsigned iOS simulator and development +Android artifacts, then actually install, launch and exercise disposable UI +flows on BOTH platforms. Complete the critical backup/account/recovery matrix; +retain bracketed iOS BSMS/TXT import, keypad/cooldown, PIN/Proceed, Testnet +tutorial/faucet-to-visible-balance, modal, inheritance/timelock and profiling +checks below. Source-unit evidence does not replace native launch/UI results. + +Required local checks: IOS, ANDROID, HARDWARE. Read #7050's integrated policy in +AGENTS.md. Only the release owner's new exact-SHA OWNER_DEFERRED_PHYSICAL comment +can defer unrun physical HARDWARE checks; old b160 decisions do not carry over. +IOS and ANDROID cannot be deferred. A known critical failure cannot be deferred. + +Post a new exact-SHA Codex local validation report with actual pass/fail/not-run +outcomes, versions/artifact hashes, private-log locations and open blockers. +LOCAL_PASSED covers only the stated tested subset, never deferred devices. +Do not merge, deploy, modify production records/indexes, sign a release, upload +to stores, publish artifacts/releases, or use real funded MAINNET wallets. Keep R4 separate +from Reproducibility and Bitkey Wallet. Continue feasible checks if one prerequisite +is blocked, but do not waive a critical or required platform result. +``` + +## Safe exact-source setup + +Select the existing Node **22** toolchain; use Yarn Classic **1.22.22**. Do not overwrite global tool configuration or existing Node/Realm installations. These commands create private temporary directories and fresh detached checkouts. Run them in Bash and retain the printed workspace path for follow-up. Replace `APP_HEAD_SHA` with the coordinator's full final SHA first. + +```bash +set -euo pipefail +umask 077 +keeper_app_sha=APP_HEAD_SHA +keeper_relay_sha=b600416e5ba7325927d7d2871cff7776f4e593fe +test "${#keeper_app_sha}" -eq 40 +test "$(node -p 'process.versions.node.split(".")[0]')" = 22 +keeper_qa_root=$(mktemp -d "${TMPDIR:-/tmp}/keeper-r4-local.XXXXXX") +keeper_app_dir="$keeper_qa_root/app" +keeper_relay_dir="$keeper_qa_root/relay" +keeper_evidence_dir="$keeper_qa_root/evidence" +mkdir -p "$keeper_evidence_dir" +printf 'Isolated R4 workspace: %s\n' "$keeper_qa_root" + +git clone --filter=blob:none --no-checkout https://github.com/KeeperCommunity/bitcoin-keeper.git "$keeper_app_dir" +git -C "$keeper_app_dir" fetch origin refs/pull/7045/head +test "$(git -C "$keeper_app_dir" rev-parse FETCH_HEAD)" = "$keeper_app_sha" +git -C "$keeper_app_dir" checkout --detach "$keeper_app_sha" +git clone --filter=blob:none --no-checkout https://github.com/KeeperCommunity/Relay.git "$keeper_relay_dir" +git -C "$keeper_relay_dir" checkout --detach "$keeper_relay_sha" +git -C "$keeper_app_dir" diff --quiet HEAD +git -C "$keeper_relay_dir" diff --quiet HEAD + +cd "$keeper_app_dir" +yarn install --frozen-lockfile --ignore-scripts +yarn patch-package +(cd node_modules/realm && ../.bin/prebuild-install --runtime napi --target 6) +(cd "$keeper_relay_dir" && npm ci --ignore-scripts) +export REALM_DISABLE_ANALYTICS=1 +export KEEPER_RELAY_FIXTURE_MODULE="$keeper_relay_dir/tests/support/automaticBackupHttpFixture.cjs" +export BACKUP_TEST_MONGO_URL=mongodb://127.0.0.1:27058 +``` + +Confirm Docker uses the local Mac Docker Desktop/Colima engine. Port **27058** must be free; do not stop somebody else's service to claim it. The runner rejects a different port, credentials, non-loopback Mongo, an incorrect Relay SHA or tracked Relay changes. A native Realm write/read probe must really pass; do not substitute the Jest Realm mock. + +```bash +docker info --format '{{.ServerVersion}}' +if lsof -nP -iTCP:27058 -sTCP:LISTEN; then + printf 'Port 27058 is occupied; coordinate an isolated fixture first.\n' >&2 + exit 1 +fi +keeper_mongo_name="keeper-r4-local-mongo-$$" +docker run --detach --rm --name "$keeper_mongo_name" \ + --publish 127.0.0.1:27058:27058 \ + mongo:7.0@sha256:9854f7139445d766a9523571d6f047530c45547460ffcf8259eb2bf4264632ca \ + mongod --replSet keeperQA --bind_ip_all --port 27058 +keeper_mongo_ready=false +for keeper_attempt in $(seq 1 30); do + if docker exec "$keeper_mongo_name" mongosh --port 27058 --quiet --eval \ + 'try { rs.status(); } catch (e) { if (e.code !== 94) throw e; rs.initiate({_id:"keeperQA",members:[{_id:0,host:"127.0.0.1:27058"}]}); } quit(db.hello().isWritablePrimary ? 0 : 1)'; then + keeper_mongo_ready=true + break + fi + sleep 2 +done +test "$keeper_mongo_ready" = true +npm run test:automatic-backup-genuine 2>&1 | tee "$keeper_evidence_dir/genuine-matrix.log" +KEEPER_APP_SOURCE="$keeper_app_dir" yarn --cwd "$keeper_relay_dir" test:automatic-http \ + 2>&1 | tee "$keeper_evidence_dir/relay-http.log" +npm run test:recovery 2>&1 | tee "$keeper_evidence_dir/recovery.log" +npm test -- --runInBand 2>&1 | tee "$keeper_evidence_dir/jest.log" +npm run test:release 2>&1 | tee "$keeper_evidence_dir/release-rules.log" +npm run version:check 2>&1 | tee "$keeper_evidence_dir/version.log" +git diff --check +# Stop ONLY the disposable container created by this session when finished. +docker stop "$keeper_mongo_name" +``` + +Retain failures and counts even if a command stops the sequence; run independent feasible checks separately afterward. Do not report unexecuted later commands as passing. A repository-wide TypeScript failure must retain its actual diagnostics/status; historical inherited diagnostics are not a passing typecheck. The same genuine source runner and prerequisite steps are defined in `.github/workflows/test.yml` and `scripts/run-automatic-backup-genuine.cjs`. Verify their Relay pin agrees with this document before running. Keep private logs locally and publish only redacted categories, counts, hashes and source/run identities. + +## Required operation and failure coverage + +Use the complete [matrix](../openspec/changes/fix-recovery-backup-freshness/automatic-write-validation.md). Record a result and evidence for every operation: `updateAppImage`, `migrateXfps`, `deleteAppImageEntity`, `deleteVaults`, `updateVaultImage`, `modifyLabels`, and `deleteBackup`. Their production producers, native Realm commits, real AES, Redux/store guards, HTTP handlers and Mongo transactions must be exercised together. A transport-only unit fixture is supplemental. + +| Case | Required result | +| --- | --- | +| C1 committed match | Local commit and original branded account/session/base/generation precede one intended v2 effect; ciphertext/IDs or deletion target match committed Realm. | +| C2 uncommitted/stale state | Failed/partial/raw/unmanaged commit, stale local state, wrong key, copied origin/receipt or missing proof sends nothing; local data survives. | +| C3 account session | A→B→A and closed/reopened Realm reject old work. Old A's aborted work leaves A pending and clears its stale baseline without changing B. | +| C4 remote conflicts | Remote-only records and immutable wallet/signer/vault key or policy conflicts survive; no automatic overwrite or removal. Fingerprint correction cannot change unrelated keys, script, policy or network. | +| C5 consent | No automatic write for an unconsented account. Explicit whole-backup opt-out is same-account and deliberate; a newer consent choice invalidates old deletion work. Consent changes only after acknowledged complete deletion. | +| C6 competing writer | A real peer transaction/409 stops the chain; no fresh revision is borrowed for old ciphertext. | +| C7 old/malformed protocol | 404, unavailable/partial references, wrong metadata, malformed JSON, false/missing `updated`/`preconditionApplied`, missing/invalid/unchanged `newRevision` stop with no legacy write fallback. | +| C8 timeout/lost acknowledgement | Real post-commit timeout/lost reply remains uncertain/pending; no replay or next prepared member. Matching readback alone does not acknowledge the interrupted operation. | +| C9 complete readback | Full decrypt/reference/metadata comparison at the same generation/session precedes verified. Wrong-key, incomplete or corrupted content cannot pass. A retained empty generation tombstone confirms deletion but never represents a verified backup. | +| C10 final commit race/restart | Inject a genuine local mutation just before final Redux commit: no false verified. Relaunch preserves committed data/pending state and does not replay prepared work. | + +Include complete create/archive, multi-vault registration, label import/deletion and whole-upgrade producer batches. For each fanout, let the first real transaction commit and make the next fail, lose its reply or receive malformed acknowledgement; remaining prepared effects must not run. Verify genuine late-field rollback, local-first creation/edit/deletion without an original verified baseline, and final complete restore of BTC MAINNET/TESTNET wallets, existing USDT recovery metadata, keys, active/archived vaults, registrations, labels and nodes. New USDT/GasFree setup and transactional flows remain paused. + +## Native local services without changing the approved manifest + +Read [contributor setup](../dev/local-backend/README.md), [verification boundaries](../dev/local-backend/VERIFICATION.md) and the paired Relay README. Use unique Compose projects and only disposable data. The approved contributor bootstrap is useful for toolchain, onboarding and existing public services, but its `ee935359` Relay does not establish positive v2 automatic-write acceptance. + +From the isolated app checkout, prepare its approved stack and `.env.local` normally. These examples use free default ports; check all ports and choose unused contributor ports/settings as documented before starting anything. Keep the same settings for every invocation. + +```bash +cd "$keeper_app_dir" +export COMPOSE_PROJECT_NAME="keeper-r4-contributor-$$" +./dev/local-backend/dev doctor +./dev/local-backend/dev up +./dev/local-backend/dev env +./dev/local-backend/dev verify +./dev/local-backend/dev stop +# stop retains this project's named volumes. Never use down --volumes. +./dev/local-backend/dev prepare-signing +export SIGNING_SOURCE_DIR="$keeper_app_dir/dev/local-backend/.sources/signing" +keeper_pair_project="keeper-r4-paired-$$" +docker compose -p "$keeper_pair_project" \ + -f "$keeper_relay_dir/compose.local.yaml" \ + -f "$keeper_relay_dir/compose.local-with-signing.yaml" config --quiet +docker compose -p "$keeper_pair_project" \ + -f "$keeper_relay_dir/compose.local.yaml" \ + -f "$keeper_relay_dir/compose.local-with-signing.yaml" up --build -d --wait +curl --fail http://127.0.0.1:3000/health +curl --fail http://127.0.0.1:3003/health +``` + +The signing overlay requires Compose **2.24.4+**. Record paired Relay source/container identities and effective local endpoints; this separate stack supports actual onboarding/channel/read/v2 routes, rather than only the runner's ephemeral HTTP handlers. Use the approved local SigningServer adapter for its existing testnet capability; this does not authorize new Assisted Multisig work. Ensure `.env.local` matches the paired gateway ports before building. Do not print its contents, copy hosted configuration, modify approved source locks/adapters or point this fixture at a hosted backend. Stop only this paired project with the same files/project name and `down` without `--volumes` after testing; retain its disposable volumes for diagnosis. + +## Required iOS and Android builds, launch and UI + +Build sequentially to limit memory pressure. Record available disk/RAM, Node/Yarn/Ruby/Bundler/Xcode/SDK/JDK versions, simulator/emulator identifiers and exact source SHA. Use fresh QA device state through normal simulator/emulator creation, not by erasing an existing owner device. Run Metro from this checkout with `yarn start` in a separate terminal. + +For iOS, select Ruby **3.3.0**, the root Gemfile and the actual available simulator UDID. Before building, inspect the shared `/tmp/envfile` selector without printing configuration: coordinate if it selects another build, and do not delete somebody else's selector. Use deployment CocoaPods installation; a lockfile/checksum mismatch is BLOCKED, not permission to rewrite locks. + +```bash +cd "$keeper_app_dir" +bundle install +(cd ios && BUNDLE_GEMFILE="$keeper_app_dir/Gemfile" RCT_NEW_ARCH_ENABLED=1 bundle exec pod install --deployment) +xcrun simctl list devices available +keeper_ios_udid=YOUR_DISPOSABLE_SIMULATOR_UDID +ENVFILE=.env.local xcodebuild -workspace ios/hexa_keeper.xcworkspace \ + -scheme hexa_keeper_dev -configuration Debug -sdk iphonesimulator \ + -destination "platform=iOS Simulator,id=$keeper_ios_udid" \ + -derivedDataPath ios/build/r4-local -jobs 2 CODE_SIGNING_ALLOWED=NO build \ + 2>&1 | tee "$keeper_evidence_dir/ios-build.log" +xcrun simctl boot "$keeper_ios_udid" # Skip if already booted. +xcrun simctl bootstatus "$keeper_ios_udid" -b +xcrun simctl install "$keeper_ios_udid" ios/build/r4-local/Build/Products/Debug-iphonesimulator/hexa_keeper_dev.app +xcrun simctl launch "$keeper_ios_udid" io.hexawallet.hexakeeper.dev +``` + +An unsigned compile is not launch/entitlement evidence. If the simulator refuses that artifact because of local signing/entitlements, record the exact outcome; use the contributor README's simulator-only Debug build with its normal local/ad-hoc signing for launch if available without distribution credentials. Record both builds and which artifact actually launched. This is not production/release signing, and it does not make the unsigned artifact a launched artifact. + +For Android, select a disposable emulator serial and its architecture. Use JDK **17**, SDK **36**, build tools **35.0.0**, NDK **27.1.12297006** and CMake **3.22.1** as required by this source. The development artifact uses the checked-in disposable debug certificate, not a production signing key. + +```bash +cd "$keeper_app_dir" +adb devices +keeper_android_serial=YOUR_DISPOSABLE_EMULATOR_SERIAL +keeper_android_arch=arm64-v8a # Use x86_64 for an x86_64 emulator. +./dev/local-backend/dev android-forward --serial "$keeper_android_serial" +ENVFILE=.env.local ./android/gradlew -p android app:assembleDevelopmentDebug \ + -PreactNativeArchitectures="$keeper_android_arch" --max-workers=2 --no-daemon \ + 2>&1 | tee "$keeper_evidence_dir/android-build.log" +keeper_android_apk=android/app/build/outputs/apk/development/debug/app-development-debug.apk +shasum -a 256 "$keeper_android_apk" +adb -s "$keeper_android_serial" install -r "$keeper_android_apk" +adb -s "$keeper_android_serial" shell am start -W \ + -n io.hexawallet.keeper.development/io.hexawallet.keeper.MainActivityDefault +``` + +Verify actual app/package IDs, version/counters, APK signature/alignment and launched identities; do not guess them from filenames. The current provisional counters are 2.6.4 / Android 627 / iOS 620. Store-history refresh and signed production artifact reconciliation remain separate release work. The development Android package is `io.hexawallet.keeper.development`; verify website association files against the actual package/certificate, rather than assuming a `bitcoinkeeper.development` entry matches it. + +On **both** platforms, record each scenario separately: + +1. Fresh disposable onboarding, PIN create/unlock/cooldown, always-visible disabled Proceed/queued presentation and account setup. Cold launch/unlock without pressing a key must retain every keypad digit; disabled cooldown must reject input, retain all digits, expire normally and then accept input. Exercise PIN creation and another shared-keypad consumer without shortening cooldown in production code. +2. Opt in per account, create/edit/delete wallets, signers and vaults locally, archive and register them, edit/import/delete labels, change nodes and version-related recovery state, then relaunch. Check actual local persistence, pending/verified presentation, failure-stage/retry copy and complete native clean Recovery Key restore into a **different disposable Realm**, through the production restore worker. Never restore over the owner's existing app/database. +3. Repeat C1–C10 through feasible real UI paths with the paired v2 service: interruptions/large payloads, 409/old route/malformed/lost reply, local mutation during verification, A→B→A, consent change during deletion, missing/orphaned/legacy encrypted references and multi-write partial success. Confirm server-only records and immutable identities survive. A successful Delete Backup retains local data, acknowledges empty readback, clears only the intended consent and never displays a verified backup. +4. On iOS, import identical harmless bracketed/unbracketed TXT files and valid `[Test]_backup.bsms` / `Test_backup.bsms` from the same Files provider. Both BSMS files must reach identical configuration review; stop before duplicate creation. Test filenames with spaces/literal percent escapes and cancellation. Smoke Android's unchanged content-URI path on a native file picker. +5. Fresh-install Testnet tutorial → Receive Test Sats → visible wallet balance **without restarting**, including retry/slow response and displayed-account ownership. Local mode intentionally lacks a faucet/AI provider: a 503 or mocked unit result is not positive end-to-end evidence. Use an independently approved disposable Testnet-provider QA configuration when available; otherwise record the scenario's actual BLOCKED/NOT RUN status and explicit owner follow-up, without waiving critical backup or required platform checks. +6. Modal/navigation states for new wallet configuration, backup consent/check/failure, vault registration, file picker, QR readiness/retry and network/node switching. Verify disabled/proceed controls, error dismissal, keyboard/dismiss/reopen and account handover while delayed operations resolve. Simulator camera/USB/NFC limitations must be explicit. +7. Wallet/Vault/Archived Wallet refresh, sequential label persistence, node failover/custom-node separation, deep-link cold/warm start, Ask Keeper retry/late response and existing USDT read-only balance/address/history/export. New USDT/GasFree setup, transfers and GasFree actions remain paused. Correct association hosting/certificates and available provider access are separate dependencies; record inaccessible cases rather than reporting UI-source tests as a provider pass. +8. Follow [inheritance validation](r4-inheritance-validation.md): same public test seed with/without passphrase, actual primary/inheritance identities, six-month selection, exported descriptor and fixed absolute activation. Mainnet means 180 days; new Testnet fixtures use the documented accelerated delay. Activity/later deposits must not reset it. Keep affected-wallet assessment and any funded repair separate; unavailable preserved assessor source is still a blocker, not a guessed safety verdict. +9. Profile realistic small/large encrypted backup checks, local-generation capture and UI interaction on both native runtimes. Record sizes/counts, wall time, responsiveness/cancellation, memory peaks and stall/deadline behavior. Do not truncate records or weaken size/timeout/identity guards to obtain a pass. Historical benchmarks and fast loopback HTTP tests are not native profiling acceptance. + +Do not sign or broadcast user transactions, touch funded MAINNET wallets, or use real recovery/private material. Physical phones, camera scanning, NFC/USB and hardware signer address/policy/PSBT round trips stay explicitly tracked for the owner's approved physical follow-up; simulator screenshots and fabricated device results cannot satisfy them. + +## Exact-head owner scope and Local report + +Read [AGENTS.md](../AGENTS.md), including merged [#7050](https://github.com/KeeperCommunity/bitcoin-keeper/pull/7050). Keep `IOS, ANDROID, HARDWARE` in the final Cloud handoff. The release-owner account **antuz123**, with current write access, must record a **new** scope comment for the exact final SHA to defer unrun physical checks. A new commit invalidates both previous Local sign-off and the old deferral. Do not post as a different account or infer a fresh GitHub decision from the old b160 comment. + +```text +## Codex local scope decision +State: OWNER_DEFERRED_PHYSICAL +PR: #7045 +Head commit: APP_HEAD_SHA +Deferred local checks: HARDWARE +Reason: The release owner directs unrun physical phone, camera, NFC/USB and hardware-signer checks to post-release testing because those devices are unavailable for this Local run. IOS and ANDROID and known critical failures are not deferred. +Post-release follow-up: The owner will test the installed released iOS/Android builds on physical phones and available signers, including QR, address verification, vault policy and PSBT round trips, and report exact build/device outcomes on #7045 or the release record. +``` + +This scope decision authorizes no release action and cannot turn a failed physical check into PASS. Run all feasible automated/native checks first. Unavailable noncritical scenarios may be recorded individually as `NOT RUN / POST-RELEASE` only with explicit reason and owner follow-up; neither that label nor the physical decision waives required IOS/ANDROID or a critical backup/key/funds/recipient/policy failure. + +After testing, post a new report preserving the earlier `LOCAL_FAILED` evidence: + +```text +## Codex local validation +State: LOCAL_PASSED | LOCAL_FAILED | BLOCKED +Tested commit: APP_HEAD_SHA +iOS build/simulator: PASS/FAIL/NOT RUN — exact commands, installed artifact identity, actual launch and critical UI/recovery outcomes; private log locations. +Android build/emulator: PASS/FAIL/NOT RUN — exact commands, package/version/counter, debug certificate/hash/alignment, actual launch and critical UI/recovery outcomes; private log locations. +Real devices/signers: NOT RUN / OWNER-DEFERRED PHYSICAL only if the exact-SHA owner decision exists; name its URL and every deferred scenario. Otherwise report actual results or BLOCKED. +Other checks: Exact app/Relay SHA; genuine native Realm/HTTP runner and per-operation C1-C10/producer/restore results; recovery/Jest/release/version/typecheck outcomes; hashes and redacted evidence. +Open issues / next action: Every failed, blocked or NOT RUN scenario, reason and owner follow-up. State whether critical software, unsigned native launch/flows, store histories, independent reviews and hosted Relay rollout remain open. +Release readiness: ready for review / NOT READY — LOCAL_PASSED does not authorize release, signing, deployment, upload or publication. +``` + +Use `LOCAL_PASSED` only after required iOS **and** Android checks actually pass and the physical requirement is either performed or covered by the accepted exact-SHA owner decision. Keep `Real devices/signers` beginning with `NOT RUN` or `DEFERRED` for deferred physical execution. If a critical scenario fails, retain `LOCAL_FAILED`; if a required prerequisite/check cannot run, use `BLOCKED`. Link the report and return the PR URL and actual state to the coordinator. Do not merge, deploy, sign a release, submit to stores or publish any artifact. From c308aa40544b748f4b15f16c5b501ccde5b0aa20 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Tue, 13 Oct 2026 03:58:18 +0000 Subject: [PATCH 168/254] fix: scope signer and migration completion to originating session --- src/store/sagas/wallets.ts | 28 ++++++---- tests/recovery/accountBinding.test.cjs | 76 ++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 10 deletions(-) diff --git a/src/store/sagas/wallets.ts b/src/store/sagas/wallets.ts index e74a8c965f..ba8dea5f13 100644 --- a/src/store/sagas/wallets.ts +++ b/src/store/sagas/wallets.ts @@ -701,7 +701,8 @@ function* deleteSigningDeviceWorker({ return false; } catch (error) { captureError(error); - if (currentWalletAccountId() === sourceAppId && store.getState().storage.appId === sourceAppId) { + if (currentWalletAccountId() === sourceAppId && store.getState().storage.appId === sourceAppId && + currentBackupAccountSession() === requestSession) { yield put(hideDeletingKeyModal()); yield put(relaySignersUpdateFail('An error occurred while deleting signers.')); } @@ -761,9 +762,9 @@ function* migrateVaultWorker({ originBackupAccountSession?: number; originBackupWriteIntent?: BackupWriteIntent; }) { + const sourceAppId = originAppId || currentWalletAccountId(); + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); try { - const sourceAppId = originAppId || currentWalletAccountId(); - const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); assertWalletAccount(sourceAppId, requestSession); const { newVaultInfo, oldVaultId } = payload; const migrated = yield call(addNewVaultWorker, { @@ -772,7 +773,10 @@ function* migrateVaultWorker({ originBackupWriteIntent, payload: { newVaultInfo, isMigrated: true, oldVaultId }, }); + assertWalletAccount(sourceAppId, requestSession); + if (!migrated) throw new Error('Vault migration could not be saved'); const migratedVault = yield select((state: RootState) => state.vault.intrimVault); + assertWalletAccount(sourceAppId, requestSession); if (migrated && migratedVault) { yield put( vaultMigrationCompleted({ @@ -782,14 +786,18 @@ function* migrateVaultWorker({ }) ); } + return migrated; } catch (error) { - yield put( - vaultMigrationCompleted({ - hasMigrationSucceeded: false, - hasMigrationFailed: true, - error: error.toString(), - }) - ); + if (currentWalletAccountId() === sourceAppId && store.getState().storage.appId === sourceAppId && + currentBackupAccountSession() === requestSession) + yield put( + vaultMigrationCompleted({ + hasMigrationSucceeded: false, + hasMigrationFailed: true, + error: error.toString(), + }) + ); + return false; } } diff --git a/tests/recovery/accountBinding.test.cjs b/tests/recovery/accountBinding.test.cjs index b0ce20abd1..c0ecfaa32d 100644 --- a/tests/recovery/accountBinding.test.cjs +++ b/tests/recovery/accountBinding.test.cjs @@ -530,3 +530,79 @@ test('A policy response after B login cannot change B signer or policy UI state' action.type === 'updateDelayedPolicyUpdate' || (action.type === 'setSignerPolicyError' && action.payload === 'success'))); }); + +function bindMigrationWorker(f) { + vm.runInNewContext(ts.transpileModule(functions('src/store/sagas/wallets.ts', ['migrateVaultWorker']), + { compilerOptions: { target: ts.ScriptTarget.ES2020 } }).outputText, f.scope); + f.scope.vaultMigrationCompleted = (payload) => ({ type: 'vaultMigrationCompleted', payload }); +} + +function roundTripAccount(f, appId) { + f.app.id = 'another-disposable-account'; f.state.storage.appId = f.app.id; + f.app.id = appId; f.state.storage.appId = appId; +} + +test('signer deletion rejection after A to B to A cannot hide or fail the later A modal', async () => { + const f = fixture(); + const recordedErrors = []; + f.scope.captureError = (error) => recordedErrors.push(error.message); + const originAppId = f.app.id; + let finishDelete; + f.scope.deleteAppImageEntityWorker = () => new Promise((resolve) => { finishDelete = resolve; }); + const deleting = f.run('deleteSigningDeviceWorker', { + originAppId, payload: { signers: [signer('AABBCCDD', enums.NetworkType.MAINNET)] }, + }); + for (let i = 0; i < 30 && !finishDelete; i++) await new Promise(setImmediate); + assert.equal(typeof finishDelete, 'function'); + const initialActions = f.actions.length; + roundTripAccount(f, originAppId); + finishDelete({ updated: false }); + assert.equal(await deleting, false); + assert.deepEqual(recordedErrors, ['Account changed']); + assert.ok(!f.actions.slice(initialActions).some((action) => + ['hideDeletingKeyModal', 'relaySignersUpdateFail', 'showKeyDeletedSuccessModal'].includes(action.type))); +}); + +test('vault migration completion after A to B to A cannot publish success or failure for later A', async () => { + const f = fixture(); + bindMigrationWorker(f); + const originAppId = f.app.id; + const originBackupAccountSession = f.scope.currentBackupAccountSession(); + let finishCreate; + f.scope.addNewVaultWorker = () => new Promise((resolve) => { finishCreate = resolve; }); + const migrating = f.run('migrateVaultWorker', { originAppId, originBackupAccountSession, + payload: { newVaultInfo: {}, oldVaultId: 'disposable-old-vault' } }); + for (let i = 0; i < 30 && !finishCreate; i++) await new Promise(setImmediate); + assert.equal(typeof finishCreate, 'function'); + roundTripAccount(f, originAppId); + finishCreate(true); + assert.equal(await migrating, false); + assert.ok(!f.actions.some((action) => action.type === 'vaultMigrationCompleted')); +}); + +test('account switch while selecting migration UI state cannot publish a stale completion', async () => { + const f = fixture(); + bindMigrationWorker(f); + const originAppId = f.app.id; + f.scope.addNewVaultWorker = () => true; + Object.defineProperty(f.state, 'vault', { get() { + roundTripAccount(f, originAppId); + return { intrimVault: { id: 'disposable-new-vault' } }; + } }); + assert.equal(await f.run('migrateVaultWorker', { originAppId, + originBackupAccountSession: f.scope.currentBackupAccountSession(), + payload: { newVaultInfo: {}, oldVaultId: 'disposable-old-vault' } }), false); + assert.ok(!f.actions.some((action) => action.type === 'vaultMigrationCompleted')); +}); + +test('failed local migration reports failure only for its current originating session', async () => { + const f = fixture(); + bindMigrationWorker(f); + f.scope.addNewVaultWorker = () => false; + assert.equal(await f.run('migrateVaultWorker', { originAppId: f.app.id, + originBackupAccountSession: f.scope.currentBackupAccountSession(), + payload: { newVaultInfo: {}, oldVaultId: 'disposable-old-vault' } }), false); + const result = f.actions.find((action) => action.type === 'vaultMigrationCompleted'); + assert.equal(result.payload.hasMigrationFailed, true); + assert.equal(result.payload.hasMigrationSucceeded, false); +}); From f01f0a274cc02116b666aa93648f193f2e3000cd Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Tue, 13 Oct 2026 03:58:26 +0000 Subject: [PATCH 169/254] test: prove automatic backup writes and clean native recovery --- .../automaticRecoveryRestore.integration.cjs | 206 +++++ .../automaticWriteRealmRelay.fixture.cjs | 265 ++++++ .../automaticWriteRealmRelay.integration.cjs | 799 ++++++++++++++++++ 3 files changed, 1270 insertions(+) create mode 100644 tests/recovery/automaticRecoveryRestore.integration.cjs create mode 100644 tests/recovery/automaticWriteRealmRelay.fixture.cjs create mode 100644 tests/recovery/automaticWriteRealmRelay.integration.cjs diff --git a/tests/recovery/automaticRecoveryRestore.integration.cjs b/tests/recovery/automaticRecoveryRestore.integration.cjs new file mode 100644 index 0000000000..190cafebfe --- /dev/null +++ b/tests/recovery/automaticRecoveryRestore.integration.cjs @@ -0,0 +1,206 @@ +// Clean Recovery Key restore through the actual production recovery saga, real +// BIP39/BIP32/BIP85/AES, a blank encrypted native Realm and real Relay HTTP reads. +const assert = require('node:assert/strict'); +const path = require('node:path'); +const crypto = require('node:crypto'); +const bip39 = require('bip39'); +const { TronWeb } = require('tronweb'); +const { test, before, after } = require('node:test'); +const { createRealmClient, declarations, classMembers, evaluate, plain, Realm } = require('./automaticWriteRealmRelay.fixture.cjs'); +assert.ok(process.env.KEEPER_RELAY_FIXTURE_MODULE, 'The genuine paired Relay fixture is required'); +const { createFixture } = require(process.env.KEEPER_RELAY_FIXTURE_MODULE); +const mnemonic = 'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about'; +const seed = bip39.mnemonicToSeedSync(mnemonic); +const appId = crypto.createHash('sha256').update(seed).digest('hex'); +let server; +before(async () => { server = await createFixture(); }); +after(async () => { await server?.close(); Realm.shutdown(); }); + +function cryptoAndRecovery(f) { + const saga = f.productionSagas([ + 'currentBackupAppId', 'recoverApp', 'writeRecoveredObject', 'getAppImageWorker', + 'buildAutomaticAppMutation', 'buildAutomaticVaultMutation', 'queueCommittedBackupWorker', 'updateAppImageWorker', + ], { + ...f.load('src/store/reducers/wallets'), ...f.load('src/store/reducers/concierge'), + ...f.load('src/store/sagaActions/wallets'), ...f.load('src/store/sagaActions/uai'), + ...f.load('src/models/enums/SubscriptionTier'), ...f.load('src/models/enums/BHR'), + ...f.load('src/models/interfaces/HeathCheckTypes'), + ...f.load('src/models/enums/Hardware'), HWError: f.load('src/hardware/HWErrorState').default, + ...f.utilities, _: require('lodash'), crypto, bip39, semver: require('semver'), moment: require('moment'), + config: f.config, DeviceInfo: { getVersion: () => '2.6.4', getBuildNumber: () => '627' }, + prepareRecoveryImage: f.load('src/services/backup/restore').prepareRecoveryImage, + // Metro normally expands idx at build time; this host adapter performs the + // same guarded field access, never supplies database or crypto results. + idx(value, getter) { try { return getter(value); } catch (error) { if (error.name === 'TypeError') return undefined; throw error; } }, + }); + saga.scope.config.BIP85_IMAGE_ENCRYPTIONKEY_DERIVATION_PATH = "m/83696968'/39'/0'/12'/83696968'"; + saga.scope.bitcoinJS = require('bitcoinjs-lib'); + saga.scope.bs58check = require('bs58check'); + const ecc = f.load('src/services/wallets/operations/taproot-utils/noble_ecc').default; + saga.scope.bip32 = require('bip32').default(ecc); + evaluate(classMembers('src/services/wallets/operations/utils.ts', 'WalletUtilities', [ + 'getNetworkByType', 'getFingerprintFromNode', 'getFingerprintFromSeed', 'getFingerprintFromExtendedKey', + 'getDerivationPath', 'getPurpose', 'generateExtendedKeyPairFromSeed', 'getXpubFromExtendedKey', + 'getScriptTypeFromDerivationPath', 'getNetworkFromPrefix', 'getSignerPurposeFromPath', + ]), saga.scope, 'production-bitcoin-key-utilities.ts'); + saga.scope.BIP85 = f.load('src/services/wallets/operations/BIP85').default; + evaluate(declarations('src/services/wallets/factories/VaultFactory.ts', ['generateSeedWordsKey']) + '\n' + + declarations('src/hardware/index.ts', ['getSignerNameFromType', 'generateSignerFromMetaData']) + '\n' + + declarations('src/hardware/signerSetup.ts', ['setupSeedWordsBasedKey', 'setupRecoveryKeySigningKey']) + '\n' + + declarations('src/store/sagas/upgrade.ts', ['KEY_MANAGEMENT_VERSION']) + '\n' + + declarations('src/store/sagas/wallets.ts', [ + 'currentWalletAccountId', 'assertWalletAccount', 'rewriteSignerFingerprintReferences', 'planSignerIdentityMerges', + 'applySignerIdentityMerges', 'applySignerXprivUpdates', 'buildSignerMigrationMutation', 'addSigningDeviceWorker', + ]), saga.scope, 'production-recovery-key-and-persistence.ts'); + return saga; +} + +async function sourceFixture(t) { + await server.reset(); + const f = await createRealmClient(server.url, { appId, primarySeed: seed.toString('hex'), primaryMnemonic: mnemonic }); + t.after(() => f.close()); + f.automatic = f.load('src/services/backup/automatic'); + f.installProductionStoreObservers(f.automatic); + const source = cryptoAndRecovery(f); + const fingerprint = source.scope.WalletUtilities.getFingerprintFromSeed(seed); + assert.equal(f.dbManager.updateObjectById(f.RealmSchema.KeeperApp, f.appId, { publicId: fingerprint }), true); + const recoveryKey = source.scope.setupRecoveryKeySigningKey(mnemonic); + assert.equal(f.dbManager.createObject(f.RealmSchema.Signer, recoveryKey), true); + const network = source.scope.WalletUtilities.getNetworkByType(f.enums.NetworkType.TESTNET); + const xpub = source.scope.WalletUtilities.generateExtendedKeyPairFromSeed(seed.toString('hex'), network, "m/84'/1'/0'").xpub; + const wallet = f.defaults(f.RealmSchema.Wallet, { + id: 'native-recovery-wallet', entityKind: f.enums.EntityKind.WALLET, type: f.enums.WalletType.DEFAULT, + networkType: f.enums.NetworkType.TESTNET, scriptType: 'P2WPKH', + derivationDetails: f.defaults(f.RealmSchema.WalletDerivationDetails, { instanceNum: 0, xDerivationPath: "m/84'/1'/0'" }), + presentationData: f.defaults(f.RealmSchema.WalletPresentationData, { name: 'Disposable recovery wallet', visibility: 'DEFAULT' }), + specs: f.defaults(f.RealmSchema.WalletSpecs, { xpub, nextFreeAddressIndex: 9, nextFreeChangeAddressIndex: 4, totalExternalAddresses: 12, + confirmedUTXOs: [f.defaults(f.RealmSchema.UTXO, { txId: '11'.repeat(32), vout: 0, value: 1000, spendability: 'doNotSpend', isManualOverride: true })] }), + }); + assert.equal(f.dbManager.createObject(f.RealmSchema.Wallet, wallet), true); + const mainnet = source.scope.WalletUtilities.getNetworkByType(f.enums.NetworkType.MAINNET); + const mainnetXpub = source.scope.WalletUtilities.generateExtendedKeyPairFromSeed(seed.toString('hex'), mainnet, "m/84'/0'/1'").xpub; + assert.equal(f.dbManager.createObject(f.RealmSchema.Wallet, f.defaults(f.RealmSchema.Wallet, { + ...wallet, id: 'mainnet-recovery-wallet', type: f.enums.WalletType.IMPORTED, + networkType: f.enums.NetworkType.MAINNET, + derivationDetails: f.defaults(f.RealmSchema.WalletDerivationDetails, { instanceNum: 1, xDerivationPath: "m/84'/0'/1'" }), + presentationData: f.defaults(f.RealmSchema.WalletPresentationData, { name: 'Retained mainnet wallet', visibility: 'HIDDEN' }), + specs: f.defaults(f.RealmSchema.WalletSpecs, { xpub: mainnetXpub, nextFreeAddressIndex: 13, nextFreeChangeAddressIndex: 6, totalExternalAddresses: 15 }), + })), true); + // Existing USDT records only: disposable, public keys and real address + // validation. No wallet creation, provider call, token transfer or opt-in. + for (const [type, privateKey, gasFreeKey] of [ + ['DEFAULT', '11'.repeat(32), '33'.repeat(32)], + ['IMPORTED', '22'.repeat(32), '44'.repeat(32)], + ]) { + const address = TronWeb.address.fromPrivateKey(privateKey); + const gasFreeAddress = TronWeb.address.fromPrivateKey(gasFreeKey); + assert.ok(TronWeb.isAddress(address) && TronWeb.isAddress(gasFreeAddress)); + assert.equal(f.dbManager.createObject(f.RealmSchema.USDTWallet, f.defaults(f.RealmSchema.USDTWallet, { + id: `retained-usdt-${type}`, type, entityKind: f.enums.EntityKind.USDT_WALLET, + networkType: f.enums.NetworkType.MAINNET, + derivationDetails: f.defaults(f.RealmSchema.USDTWalletDerivationDetails, { + instanceNum: type === 'DEFAULT' ? 0 : null, mnemonic: type === 'DEFAULT' ? mnemonic : null, + xDerivationPath: "m/44'/195'/0'/0/0", + }), + presentationData: f.defaults(f.RealmSchema.USDTWalletPresentationData, { + name: `Existing ${type} USDT wallet`, description: 'Retained token-wallet metadata', visibility: 'HIDDEN', + }), + specs: f.defaults(f.RealmSchema.USDTWalletSpecs, { + address, privateKey, balance: 23, lastSynched: 1710000000, hasNewUpdates: false, + transactions: [f.defaults(f.RealmSchema.USDTTransaction, { + txId: 'public-disposable-cached-token-transaction', from: address, to: gasFreeAddress, + amount: '5', fee: '1', status: 'CONFIRMED', timestamp: 1700000000, isGasFree: true, + })], + }), + accountStatus: f.defaults(f.RealmSchema.USDTWalletAccountStatus, { + address, gasFreeAddress, isActive: true, frozen: 0, canTransfer: false, nextNonce: 4, + fees: { transferFee: 1, activateFee: 2 }, + }), + })), true); + } + const vaultKey = source.scope.generateSeedWordsKey('legal winner thank year wave sausage worth useful legal winner thank yellow', f.enums.NetworkType.TESTNET, true); + const cold = f.defaults(f.RealmSchema.Signer, { masterFingerprint: vaultKey.masterFingerprint, type: f.enums.SignerType.COLDCARD, + storageType: f.enums.SignerStorage.COLD, networkType: f.enums.NetworkType.TESTNET, signerXpubs: f.defaults(f.RealmSchema.SignerXpubs, { + [f.enums.XpubTypes.P2WSH]: [{ xpub: vaultKey.xpub, xpriv: null, derivationPath: vaultKey.derivationPath }], + }) }); + cold.id = f.utilities.getKeyUID(cold); + assert.equal(f.dbManager.createObject(f.RealmSchema.Signer, cold), true); + const vaultSigner = f.defaults(f.RealmSchema.VaultSigner, { ...vaultKey, xpriv: null, xfp: vaultKey.masterFingerprint, + registeredVaults: [ + { vaultId: 'archived-recovery-vault', registered: true, hmac: 'public-disposable-archived-registration' }, + { vaultId: 'active-recovery-vault', registered: true, hmac: 'public-disposable-active-registration' }, + ] }); + const vault = f.defaults(f.RealmSchema.Vault, { id: 'archived-recovery-vault', entityKind: f.enums.EntityKind.VAULT, type: f.enums.VaultType.DEFAULT, + networkType: f.enums.NetworkType.TESTNET, isMultiSig: true, scriptType: 'P2WSH', archived: true, + scheme: f.defaults(f.RealmSchema.VaultScheme, { m: 1, n: 1 }), signers: [vaultSigner], + presentationData: f.defaults(f.RealmSchema.VaultPresentationData, { name: 'Retained archived vault', visibility: 'HIDDEN' }), + specs: f.defaults(f.RealmSchema.VaultSpecs, { xpubs: [vaultKey.xpub], nextFreeAddressIndex: 7 }) }); + assert.equal(f.dbManager.createObject(f.RealmSchema.Vault, vault), true); + assert.equal(f.dbManager.createObject(f.RealmSchema.Vault, { + ...vault, id: 'active-recovery-vault', archived: false, + presentationData: f.defaults(f.RealmSchema.VaultPresentationData, { name: 'Retained active vault', visibility: 'DEFAULT' }), + specs: f.defaults(f.RealmSchema.VaultSpecs, { xpubs: [vaultKey.xpub], nextFreeAddressIndex: 10, nextFreeChangeAddressIndex: 3 }), + }), true); + assert.equal(f.dbManager.createObject(f.RealmSchema.Tags, f.defaults(f.RealmSchema.Tags, { id: 'recovered-label', type: 'TXN', ref: 'public-disposable-transaction', label: 'Recovery label' })), true); + assert.equal(f.dbManager.createObject(f.RealmSchema.NodeConnect, f.defaults(f.RealmSchema.NodeConnect, { + id: 42, host: 'public-recovery-node.invalid', port: '50002', useSSL: true, networkType: f.enums.NetworkType.TESTNET, + })), true); + assert.equal(f.dbManager.createObject(f.RealmSchema.NodeConnect, f.defaults(f.RealmSchema.NodeConnect, { + id: 43, host: 'public-mainnet-recovery-node.invalid', port: '50002', useSSL: true, networkType: f.enums.NetworkType.MAINNET, + })), true); + await server.seed(await f.payloadForCurrentImage()); + await server.prepareRestore(f.appId); + return { source: f, expected: await f.repair.captureBackupImage(), recoveryKey, fingerprint }; +} + +test('actual Recovery Key worker restores real crypto and every retained recovery collection into blank native Realm', async (t) => { + const origin = await sourceFixture(t); + const f = await createRealmClient(server.url, { appId, primarySeed: seed.toString('hex'), primaryMnemonic: mnemonic, publicId: origin.fingerprint }); + t.after(() => f.close()); + f.automatic = f.load('src/services/backup/automatic'); + f.installProductionStoreObservers(f.automatic); + const saga = cryptoAndRecovery(f); + f.realmDatabase.closeDatabase(); + assert.equal((await f.dbManager.initializeRealm(Buffer.alloc(64, 7), path.join(f.directory, 'clean-restore.realm'))).success, true); + assert.equal(f.nativeRealm().objects('KeeperApp').length, 0); + assert.equal(f.nativeRealm().objects('Wallet').length, 0); + f.store.dispatch(f.storage.setAppId('')); + f.store.dispatch(f.account.setTempDetails({ hash: f.encryption.hash256('public-disposable-passcode-fixture'), + realmId: path.join(f.directory, 'clean-restore.realm'), accountIdentifier: 'disposable restore' })); + server.clearTrace(); + await saga.run('getAppImageWorker', { payload: { primaryMnemonic: mnemonic } }); + assert.equal(f.store.getState().storage.appCreated, true, `Production recovery failed: ${f.store.getState().bhr.appImageError || 'no diagnostic'}`); + assert.equal(f.store.getState().storage.appId, appId); + assert.equal(f.store.getState().bhr.appImageError, ''); + assert.equal(server.trace.filter((entry) => entry.path === '/getAppImage').length, 1); + assert.equal(server.trace.filter((entry) => entry.path === '/revisionedBackupMutation').length, 0, 'Recovery does not opt in or upload'); + const restored = await f.repair.captureBackupImage(); + assert.equal(await f.image.compareImages(origin.expected, restored), 'matched', 'The entire retained recovery image must match after the actual worker finishes'); + assert.deepEqual(Object.keys(restored.wallets).sort(), Object.keys(origin.expected.wallets).sort()); + for (const id of ['native-recovery-wallet', 'mainnet-recovery-wallet']) { + assert.equal(f.image.canonical(f.image.recoveryContent('wallets', restored.wallets[id])), + f.image.canonical(f.image.recoveryContent('wallets', origin.expected.wallets[id]))); + } + assert.deepEqual(new Set(['native-recovery-wallet', 'mainnet-recovery-wallet'].map(id => restored.wallets[id].networkType)), + new Set([f.enums.NetworkType.MAINNET, f.enums.NetworkType.TESTNET])); + for (const type of ['DEFAULT', 'IMPORTED']) { + const recovered = restored.wallets[`retained-usdt-${type}`]; + assert.deepEqual(plain(recovered), plain(origin.expected.wallets[recovered.id]), 'Every existing USDT key, address, cached history and metadata field survives actual restore'); + assert.ok(TronWeb.isAddress(recovered.accountStatus.address) && TronWeb.isAddress(recovered.accountStatus.gasFreeAddress)); + assert.equal(recovered.presentationData.visibility, 'HIDDEN'); + } + assert.deepEqual(plain(restored.vaults), plain(origin.expected.vaults)); + assert.equal(restored.vaults['active-recovery-vault'].archived, false); + assert.equal(restored.vaults['archived-recovery-vault'].archived, true); + assert.deepEqual(plain(restored.labels), plain(origin.expected.labels)); + assert.equal(restored.nodes['42'].host, 'public-recovery-node.invalid'); + assert.equal(restored.nodes['43'].host, 'public-mainnet-recovery-node.invalid'); + assert.deepEqual(Object.fromEntries(Object.entries(restored.nodes).map(([id, node]) => [id, plain(f.image.recoveryContent('nodes', node))])), + Object.fromEntries(Object.entries(origin.expected.nodes).map(([id, node]) => [id, plain(f.image.recoveryContent('nodes', node))]))); + assert.deepEqual(plain(restored.signers), plain(origin.expected.signers), 'Every retained signer field, not only the Recovery Key xpubs, survives'); + const recoveryId = f.utilities.getKeyUID(origin.recoveryKey); + assert.deepEqual(plain(restored.signers[recoveryId].signerXpubs), plain(origin.expected.signers[recoveryId].signerXpubs)); + assert.ok(Object.values(restored.signers[recoveryId].signerXpubs).some((keys) => keys.length)); + assert.notEqual(f.store.getState().bhr.automaticCloudBackupByAppId[appId], true); + assert.equal(f.dbManager.getObjectByIndex(f.RealmSchema.KeeperApp).primarySeed, seed.toString('hex')); +}); diff --git a/tests/recovery/automaticWriteRealmRelay.fixture.cjs b/tests/recovery/automaticWriteRealmRelay.fixture.cjs new file mode 100644 index 0000000000..0f849a65b7 --- /dev/null +++ b/tests/recovery/automaticWriteRealmRelay.fixture.cjs @@ -0,0 +1,265 @@ +// Native Realm and real Redux/AES host fixture. Backup algorithms, database +// wrappers and schemas are loaded from production source; this is not the +// in-memory DB or Relay adapter used by the ordinary unit harness. +const fs = require('node:fs'); +const os = require('node:os'); +const path = require('node:path'); +const vm = require('node:vm'); +const assert = require('node:assert/strict'); +const crypto = require('node:crypto'); +const ts = require('typescript'); +const { configureStore } = require('@reduxjs/toolkit'); +const { runSaga } = require('redux-saga'); +const effects = require('redux-saga/effects'); +const Realm = require('realm'); + +const root = path.resolve(__dirname, '../..'); +const plain = (value) => JSON.parse(JSON.stringify(value)); + +function declarations(file, names) { + const source = ts.createSourceFile(file, fs.readFileSync(path.join(root, file), 'utf8'), ts.ScriptTarget.Latest, true); + const matches = source.statements.filter((node) => + (ts.isFunctionDeclaration(node) && names.includes(node.name?.text)) || + (ts.isVariableStatement(node) && node.declarationList.declarations.some((declaration) => names.includes(declaration.name?.text)))); + assert.equal(matches.length, names.length, `Missing production declarations in ${file}`); + return matches.map((node) => node.getText(source).replace(/^export /, '').replace(/^const /, 'var ')).join('\n'); +} + +function classMembers(file, name, names) { + const source = ts.createSourceFile(file, fs.readFileSync(path.join(root, file), 'utf8'), ts.ScriptTarget.Latest, true); + const declaration = source.statements.find((node) => ts.isClassDeclaration(node) && node.name?.text === name); + assert.ok(declaration, `Missing production class ${name}`); + const members = declaration.members.filter((member) => names.includes(member.name?.getText(source))); + assert.equal(members.length, names.length, `Missing production ${name} members`); + return `var ${name} = class ${name} {\n${members.map((member) => member.getText(source)).join('\n')}\n};`; +} + +function evaluate(source, globals, file) { + const code = ts.transpileModule(source, { + compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020, esModuleInterop: true }, + }).outputText; + const defaults = { + // Native Realm checks JavaScript constructor identity for Mixed values. + // JSON must share the host that owns the genuine Realm binding, as Metro + // production code and Realm do; no decoded fields are rewritten here. + JSON, Date, Buffer, Uint8Array, Int8Array, ArrayBuffer, Promise, Set, Map, AbortController, setTimeout, clearTimeout, setImmediate, clearImmediate, + console, Realm, process, + }; + for (const [name, value] of Object.entries(defaults)) if (!Object.hasOwn(globals, name)) globals[name] = value; + return vm.runInNewContext(code, globals, { filename: file }); +} + +function sourceLoader(bindings, options = {}) { + const cache = new Map(); + function load(file) { + const absolute = path.isAbsolute(file) ? file : path.join(root, file); + const candidate = [absolute, `${absolute}.ts`, `${absolute}.tsx`, `${absolute}.js`, path.join(absolute, 'index.ts')].find((name) => fs.existsSync(name) && fs.statSync(name).isFile()); + if (!candidate) throw new Error(`Production module is unavailable: ${file}`); + if (cache.has(candidate)) return cache.get(candidate).exports; + const module = { exports: {} }; + cache.set(candidate, module); + evaluate(fs.readFileSync(candidate, 'utf8'), { + module, exports: module.exports, + // Timeout cases compress the real abort timers only. Axios still sends + // actual HTTP and its AbortController cancels a genuinely delayed reply. + ...(options.backupTimerCeilingMs ? { setTimeout: (callback, milliseconds, ...args) => + setTimeout(callback, Math.min(milliseconds, options.backupTimerCeilingMs), ...args) } : {}), + require(name) { + if (Object.hasOwn(bindings, name)) return bindings[name]; + if (name.startsWith('src/')) return load(name); + if (name.startsWith('.')) { + const resolved = path.resolve(path.dirname(candidate), name); + const alias = path.relative(root, resolved); + if (Object.hasOwn(bindings, alias)) return bindings[alias]; + return load(resolved); + } + return require(name); + }, + }, candidate); + return module.exports; + } + return load; +} + +async function createRealmClient(url, options = {}) { + assert.match(url, /^http:\/\/(127\.0\.0\.1|localhost):\d+\/$/, 'Fixture must use a loopback HTTP Relay'); + const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'keeper-native-realm-backup-')); + const diagnostics = []; + const bindings = { + realm: Realm, + 'src/services/sentry': { captureError(error) { diagnostics.push(error?.name || 'error'); } }, + 'react-native': { Platform: { OS: 'fixture-host' } }, + 'react-native-device-info': { getVersion: () => '2.6.4', getBuildNumber: () => '627' }, + 'react-native-rsa-native': { RSA: new Proxy({}, { get: () => () => { throw new Error('Unexpected native RSA use'); } }) }, + 'src/utils/service-utilities/config': { RELAY: url, HEXA_ID: 'disposable-fixture', ENVIRONMENT: 'DEVELOPMENT', isDevMode: () => true }, + 'src/storage/realm/migrations': { runRealmMigrations() { throw new Error('A fresh fixture must never migrate an existing Realm'); } }, + // No MMKV/native persistence is available on Node. Redux reducers are real; + // this unused host adapter never supplies recovery state or write proof. + 'src/storage': { reduxStorage: { getItem: async () => null, setItem: async () => undefined, removeItem: async () => undefined } }, + 'redux-persist/es/persistReducer': require('redux-persist/lib/persistReducer').default, + }; + const load = sourceLoader(bindings, options); + const enums = load('src/services/wallets/enums'); + const RealmSchema = load('src/storage/realm/enum').RealmSchema; + let dbManager; + const utilitiesScope = { ...enums, _: require('lodash'), dbManager: new Proxy({}, { + get: (_target, name) => (...arguments_) => dbManager[name](...arguments_), + }), RealmSchema, + getJSONFromRealmObject: (object) => object.toJSON() }; + evaluate(declarations('src/constants/defaultData.tsx', ['RECOVERY_KEY_SIGNER_NAME']) + '\n' + + declarations('src/utils/utilities.ts', ['getAccountFromSigner', 'getKeyUID', 'checkSignerAccountsMatch', 'sanitizeSeedKeyForBackup', 'sanitizeVaultSignersForSeedKeyBackup']), utilitiesScope, 'production-backup-utilities.ts'); + bindings['src/utils/utilities'] = utilitiesScope; + const realmDatabase = load('src/storage/realm/realm').default; + dbManager = load('src/storage/realm/dbManager').default; + utilitiesScope.dbManager = dbManager; + const schemas = load('src/storage/realm/schema').default; + const encryption = load('src/utils/service-utilities/encryption'); + const image = load('src/services/backup/image'); + const transport = load('src/services/backup/transport'); + const bhr = load('src/store/reducers/bhr'); + const storage = load('src/store/reducers/storage'); + const account = load('src/store/reducers/account'); + const settings = load('src/store/reducers/settings'); + const middlewareScope = { dbManager, RealmSchema, ...transport, + isBackupVerificationCurrent: (...args) => load('src/services/backup/automatic').isBackupVerificationCurrent(...args) }; + evaluate(declarations('src/store/store.ts', ['accountOwnedActions', 'accountOwnedUtxoUiActions', 'bindActionOrigin']), middlewareScope, 'production-origin-middleware.ts'); + const actions = []; + let beforeDispatch; + const store = configureStore({ + reducer: { bhr: bhr.default, storage: storage.default, account: account.default, settings: settings.default }, + middleware: (defaults) => defaults({ serializableCheck: false, immutableCheck: false }).concat(() => (next) => (action) => { + beforeDispatch?.(action); + return next(action); + }, middlewareScope.bindActionOrigin, () => (next) => (action) => { + actions.push(action); + return next(action); + }), + }); + store.dispatch(settings.setBitcoinNetwork(enums.NetworkType.TESTNET)); + bindings['src/store/store'] = { store }; + const repair = load('src/services/backup/repair'); + const Relay = load('src/services/backend/Relay').default; + const nativeEvents = []; + let selected; + const accountFiles = new Map(); + const seed = options.primarySeed || 'ab'.repeat(32); + const appId = options.appId || encryption.hash256(Buffer.from(seed, 'hex').toString('hex')); + + // Production schema defaults supply complete disposable objects without + // replacing or weakening any production object schema. + function defaults(name, patch = {}, ancestors = []) { + const schema = schemas.find((value) => value.name === name); + assert.ok(schema, `Unknown production schema ${name}`); + assert.ok(!ancestors.includes(name), `Required schema cycle: ${name}`); + const record = {}; + for (const [key, specification] of Object.entries(schema.properties)) { + const property = typeof specification === 'string' ? { type: specification } : specification; + const type = property.type; + if (property.default !== undefined) record[key] = property.default; + else if (property.optional || type.endsWith('?') || type === 'mixed') record[key] = null; + else if (type === 'list' || type.endsWith('[]')) record[key] = []; + else if (type === 'string') record[key] = ''; + else if (['int', 'float', 'double'].includes(type)) record[key] = 0; + else if (type === 'bool') record[key] = false; + else if (type === 'date') record[key] = new Date('2026-10-01T00:00:00Z'); + else if (type === 'data') record[key] = new ArrayBuffer(0); + else record[key] = defaults(property.objectType || type, {}, [...ancestors, name]); + } + return { ...record, ...patch }; + } + + function nativeRealm() { return realmDatabase.getDatabase(); } + async function selectAccount(id = appId) { + let filename = accountFiles.get(id); + if (!filename) { + filename = path.join(directory, `${crypto.createHash('sha256').update(id).digest('hex')}.realm`); + accountFiles.set(id, filename); + } + const result = await dbManager.initializeRealm(Buffer.alloc(64, 7), filename); + assert.equal(result.success, true, 'Actual encrypted Realm must open'); + selected = id; + if (!dbManager.getObjectById(RealmSchema.KeeperApp, id)) { + assert.equal(dbManager.createObject(RealmSchema.KeeperApp, defaults(RealmSchema.KeeperApp, { + id, publicId: options.publicId || 'disposable-public-id', primaryMnemonic: options.primaryMnemonic || 'public disposable fixture only', + primarySeed: seed, imageEncryptionKey: encryption.generateEncryptionKey(seed), networkType: enums.NetworkType.TESTNET, version: '2.6.4', + })), true); + } + store.dispatch(storage.setAppId(id)); + return nativeRealm(); + } + await selectAccount(); + + function installProductionStoreObservers(extra = {}) { + const source = ts.createSourceFile('store.ts', fs.readFileSync(path.join(root, 'src/store/store.ts'), 'utf8'), ts.ScriptTarget.Latest, true); + const statements = source.statements.filter((statement) => { + const text = statement.getText(source); + return /^(observeBackupAccount\(|store\.subscribe\(|setBackupUploadGuard\(|setBackupReconciliationHandler\(|dbManager\.setRecovery(?:Mutation|Commit)Observer\(|set(?:Backup|AutomaticBackup)\w*\()/.test(text); + }); + assert.ok(statements.length >= 5, 'Production account/local mutation hooks are required'); + evaluate(statements.map((statement) => statement.getText(source)).join('\n'), { + store, dbManager, RealmSchema, ...transport, ...bhr, ...repair, ...extra, + }, 'production-store-observers.ts'); + } + + function listen() { + const realm = nativeRealm(); + const listener = (_realm, event) => nativeEvents.push(event); + realm.addListener('change', listener); + return () => { if (!realm.isClosed) realm.removeListener('change', listener); }; + } + + function productionSagas(names, extra = {}) { + const scope = { + ...require('redux-saga'), ...effects, ...encryption, ...enums, ...image, ...transport, ...bhr, ...storage, ...account, + ...load('src/services/backup/automatic'), + ...utilitiesScope, dbManager, realm: realmDatabase, Realm, RealmSchema, store, Relay, + inspectBackup: repair.inspectBackup, captureError: bindings['src/services/sentry'].captureError, + getJSONFromRealmObject: (object) => object.toJSON(), + NetInfo: { fetch: async () => ({ isConnected: true }) }, + addToUaiStackWorker: function* () {}, uaiActionedWorker: function* () {}, + uaiType: { SERVER_BACKUP_FAILURE: 'SERVER_BACKUP_FAILURE' }, + console: { log() {}, error() {} }, Buffer, ...extra, + }; + evaluate(declarations('src/store/sagas/bhr.ts', names), scope, 'production-backup-sagas.ts'); + return { + scope, + run(name, ...args) { return runSaga({ getState: store.getState, dispatch: store.dispatch }, scope[name], ...args).toPromise(); }, + }; + } + + async function payloadForCurrentImage() { + const current = await repair.captureBackupImage(); + const app = plain(dbManager.getObjectByIndex(RealmSchema.KeeperApp)); + const key = encryption.generateEncryptionKey(app.primarySeed); + const payload = { + appId: app.id, publicId: app.publicId, networkType: app.networkType, version: app.version, + subscription: JSON.stringify(app.subscription), walletObject: {}, signersObject: {}, vaultObject: {}, nodes: [], labels: [], + }; + for (const [kind, records] of Object.entries(current)) { + for (const [id, record] of Object.entries(records)) { + const encrypted = await image.encryptRecord(key, record); + if (kind === 'wallets') payload.walletObject[id] = encrypted; + if (kind === 'signers') payload.signersObject[id] = encrypted; + if (kind === 'nodes') payload.nodes.push(encrypted); + if (kind === 'labels') payload.labels.push({ id: encryption.hash256(encryption.hash256(key + id)), content: encrypted }); + if (kind === 'vaults') payload.vaultObject[id] = { + vaultId: id, vault: encrypted, isArchived: !!record.archived, scheme: record.scheme, + signersData: record.signers.map((signer) => ({ signerId: utilitiesScope.getKeyUID(signer), xfpHash: encryption.hash256(signer.masterFingerprint) })), + }; + } + } + return payload; + } + + return { + appId, seed, directory, load, bindings, enums, RealmSchema, Realm, realmDatabase, dbManager, store, + encryption, image, repair, transport, Relay, bhr, storage, account, settings, utilities: utilitiesScope, + config: bindings['src/utils/service-utilities/config'], + defaults, nativeRealm, selectAccount, selected: () => selected, nativeEvents, diagnostics, actions, + listen, installProductionStoreObservers, productionSagas, payloadForCurrentImage, + setBeforeDispatch(callback) { beforeDispatch = callback; }, + async close() { realmDatabase.closeDatabase(); fs.rmSync(directory, { recursive: true, force: true }); }, + }; +} + +module.exports = { createRealmClient, declarations, classMembers, evaluate, sourceLoader, plain, Realm }; diff --git a/tests/recovery/automaticWriteRealmRelay.integration.cjs b/tests/recovery/automaticWriteRealmRelay.integration.cjs new file mode 100644 index 0000000000..431627d234 --- /dev/null +++ b/tests/recovery/automaticWriteRealmRelay.integration.cjs @@ -0,0 +1,799 @@ +// Required enablement matrix, intentionally separate from the credential-free +// unit glob. It MUST use the native Realm binding and a real transactional Relay +// helper; missing prerequisites fail the command and are never a skipped pass. +const assert = require('node:assert/strict'); +const { test, before, after } = require('node:test'); +const { createRealmClient, plain, Realm } = require('./automaticWriteRealmRelay.fixture.cjs'); + +const fixtureModule = process.env.KEEPER_RELAY_FIXTURE_MODULE; +assert.ok(fixtureModule, 'Set KEEPER_RELAY_FIXTURE_MODULE to Relay tests/support/automaticBackupHttpFixture.cjs'); +const { createFixture } = require(fixtureModule); +let server; +before(async () => { server = await createFixture(); }); +after(async () => { await server?.close(); Realm.shutdown(); }); + +const tick = () => new Promise((resolve) => setImmediate(resolve)); +function barrier() { + let release; + const promise = new Promise((resolve) => { release = resolve; }); + return { promise, release }; +} +const attemptedWrites = () => server.trace.filter((event) => event.path === '/revisionedBackupMutation'); + +function wallet(f, id = 'wallet-a', name = 'Initial wallet') { + return f.defaults(f.RealmSchema.Wallet, { + id, entityKind: f.enums.EntityKind.WALLET, type: f.enums.WalletType.DEFAULT, + networkType: f.enums.NetworkType.TESTNET, scriptType: 'P2WPKH', + derivationDetails: f.defaults(f.RealmSchema.WalletDerivationDetails, { instanceNum: 0, xDerivationPath: "m/84'/1'/0'" }), + presentationData: f.defaults(f.RealmSchema.WalletPresentationData, { name, visibility: 'DEFAULT' }), + specs: f.defaults(f.RealmSchema.WalletSpecs, { xpub: `disposable-public-${id}`, nextFreeAddressIndex: 1 }), + }); +} + +function signer(f, fingerprint = 'aaaa0001') { + const record = f.defaults(f.RealmSchema.Signer, { + masterFingerprint: fingerprint, type: f.enums.SignerType.COLDCARD, storageType: f.enums.SignerStorage.COLD, + signerName: 'Disposable public key', networkType: f.enums.NetworkType.TESTNET, + signerXpubs: f.defaults(f.RealmSchema.SignerXpubs, { + [f.enums.XpubTypes.P2WSH]: [{ xpub: 'disposable-vault-public-key', xpriv: null, derivationPath: "m/48'/1'/0'/2'" }], + }), + }); + record.id = f.utilities.getKeyUID(record); + return record; +} + +function vault(f, id = 'vault-a', key = signer(f)) { + const publicKey = key.signerXpubs[f.enums.XpubTypes.P2WSH][0]; + const vaultSigner = f.defaults(f.RealmSchema.VaultSigner, { + ...publicKey, masterFingerprint: key.masterFingerprint, xfp: key.masterFingerprint, registeredVaults: [], + }); + return f.defaults(f.RealmSchema.Vault, { + id, entityKind: f.enums.EntityKind.VAULT, type: f.enums.VaultType.DEFAULT, + networkType: f.enums.NetworkType.TESTNET, isMultiSig: true, scriptType: 'P2WSH', + scheme: f.defaults(f.RealmSchema.VaultScheme, { m: 1, n: 1 }), + signers: [vaultSigner], + presentationData: f.defaults(f.RealmSchema.VaultPresentationData, { name: 'Initial vault', visibility: 'DEFAULT' }), + specs: f.defaults(f.RealmSchema.VaultSpecs, { xpubs: [publicKey.xpub], nextFreeAddressIndex: 1 }), + }); +} + +function persist(f, schema, record) { + assert.equal(f.dbManager.createObject(schema, record), true, `Actual Realm ${schema} fixture must persist`); +} + +function persistVault(f, id) { + const key = signer(f); + if (!f.dbManager.getObjectById(f.RealmSchema.Signer, key.id)) persist(f, f.RealmSchema.Signer, key); + persist(f, f.RealmSchema.Vault, vault(f, id, key)); +} + +async function vaultBody(f, id) { + const snapshot = await f.repair.captureBackupImage(); + const value = snapshot.vaults[id]; + assert.ok(value, 'Vault body must come from current native Realm image'); + return { + appId: f.appId, vaultId: id, vault: await f.image.encryptRecord(f.encryption.generateEncryptionKey(f.seed), value), + isArchived: !!value.archived, isUpdate: true, + signersData: value.signers.map((key) => ({ signerId: f.utilities.getKeyUID(key), xfpHash: f.encryption.hash256(key.masterFingerprint) })), + }; +} + +async function labelBody(f, addIds, deleteIds = []) { + const current = await f.repair.captureBackupImage(); + const key = f.encryption.generateEncryptionKey(f.seed); + const hashed = (id) => f.encryption.hash256(f.encryption.hash256(key + id)); + const addLabels = []; + for (const id of addIds) addLabels.push({ id: hashed(id), content: await f.image.encryptRecord(key, current.labels[id]) }); + return { appId: f.appId, addLabels, deleteLabels: deleteIds.map(hashed) }; +} + +const operations = { + updateAppImage: { + prepare(f) { persist(f, f.RealmSchema.Wallet, wallet(f)); }, + mutate(realm) { realm.objectForPrimaryKey('Wallet', 'wallet-a').presentationData.name = 'Committed wallet'; }, + async body(f) { + const current = await f.repair.captureBackupImage(); + return { appId: f.appId, walletsObject: { 'wallet-a': await f.image.encryptRecord(f.encryption.generateEncryptionKey(f.seed), current.wallets['wallet-a']) } }; + }, + invoke(f, body, intent) { return f.Relay.updateAppImage(body, intent); }, + }, + migrateXfps: { + prepare(f) { persist(f, f.RealmSchema.Signer, signer(f)); }, + mutate(realm, f) { realm.delete(realm.objectForPrimaryKey('Signer', signer(f).id)); realm.create('Signer', signer(f, 'bbbb0002')); }, + async body(f) { + const oldKey = signer(f), newKey = signer(f, 'bbbb0002'); + const current = await f.repair.captureBackupImage(); + return { appId: f.appId, signerChanges: [{ oldSignerId: oldKey.id, newSignerId: newKey.id, + newSignerDetails: await f.image.encryptRecord(f.encryption.generateEncryptionKey(f.seed), current.signers[newKey.id]) }] }; + }, + invoke(f, body, intent) { return f.Relay.migrateXfp(body.appId, body.signerChanges, intent); }, + }, + deleteAppImageEntity: { + prepare(f) { persist(f, f.RealmSchema.Wallet, wallet(f)); }, + mutate(realm) { realm.delete(realm.objectForPrimaryKey('Wallet', 'wallet-a')); }, + async body(f) { return { appId: f.appId, walletIds: ['wallet-a'], signers: [] }; }, + invoke(f, body, intent) { return f.Relay.deleteAppImageEntity(body, intent); }, + }, + deleteVaults: { + prepare(f) { persistVault(f, 'vault-a'); }, + mutate(realm) { realm.delete(realm.objectForPrimaryKey('Vault', 'vault-a')); }, + async body(f) { return { appId: f.appId, vaults: ['vault-a'] }; }, + invoke(f, body, intent) { return f.Relay.deleteVaultImage(body, intent); }, + }, + updateVaultImage: { + prepare(f) { persistVault(f, 'vault-a'); }, + mutate(realm) { realm.objectForPrimaryKey('Vault', 'vault-a').presentationData.name = 'Committed vault'; }, + async body(f) { return vaultBody(f, 'vault-a'); }, + invoke(f, body, intent) { return f.Relay.updateVaultImage(body, intent); }, + }, + modifyLabels: { + prepare(f) { persist(f, f.RealmSchema.Tags, f.defaults(f.RealmSchema.Tags, { id: 'old-label', type: 'TX', ref: 'public-tx', label: 'Old label' })); }, + mutate(realm, f) { + realm.delete(realm.objectForPrimaryKey('Tags', 'old-label')); + realm.create('Tags', f.defaults(f.RealmSchema.Tags, { id: 'new-label', type: 'TX', ref: 'public-tx', label: 'Committed label' })); + }, + async body(f) { return labelBody(f, ['new-label'], ['old-label']); }, + invoke(f, body, intent) { return f.Relay.modifyLabels(body.appId, body.addLabels, body.deleteLabels, intent); }, + }, + deleteBackup: { + prepare(f) { persist(f, f.RealmSchema.Wallet, wallet(f)); }, + async body(f) { return { appId: f.appId }; }, + invoke(f, body, intent) { return f.Relay.deleteBackup(body, intent); }, + }, +}; + +async function setup(t, operation, options = {}) { + await server.reset(); + const f = await createRealmClient(server.url, options); + t.after(async () => { await f.close(); }); + f.automatic = f.load('src/services/backup/automatic'); + f.installProductionStoreObservers(f.automatic); + f.store.dispatch(f.bhr.setAutomaticCloudBackup({ appId: f.appId, enabled: true })); + f.removeNativeListener = f.listen(); + t.after(() => f.removeNativeListener()); + operations[operation].prepare(f); + persist(f, f.RealmSchema.Wallet, wallet(f, 'retained-wallet', 'Retained local recovery record')); + if (operation === 'deleteVaults') persistVault(f, 'retained-vault'); + await tick(); + await server.seed(await f.payloadForCurrentImage()); + f.sagas = f.productionSagas(['currentBackupAppId', 'backupInspectionOwners', 'runBackupInspection']); + assert.equal(await f.sagas.run('runBackupInspection', false, f.appId), true, 'Production saga baseline comes from actual encrypted HTTP readback'); + assert.equal(f.store.getState().bhr.backupRepairStateByAppId[f.appId], 'verified'); + assert.equal(f.store.getState().bhr.pendingAllBackupByAppId[f.appId], false); + assert.ok(f.transport.captureBackupWriteIntent(f.appId), 'Actual inspection must provide the baseline'); + server.clearTrace(); + return f; +} + +async function committedRequest(f, operation) { + const contract = operations[operation]; + let intent; + let receipt; + if (operation === 'deleteBackup') { + receipt = await f.automatic.beginBackupDeleteOperation(f.appId, f.transport.currentBackupAccountSession()); + assert.equal(receipt.committed, true); + intent = receipt.intent; + } else { + const op = f.automatic.beginBackupLocalOperation(f.appId, f.transport.captureBackupWriteIntent(f.appId), f.transport.currentBackupAccountSession()); + receipt = f.automatic.commitBackupLocalOperation(op, (realm) => contract.mutate(realm, f)); + assert.equal(receipt.committed, true, 'Logical operation must commit a real Realm transaction'); + assert.ok(receipt.intent, 'A committed operation with a verified originating base requires a branded intent'); + intent = receipt.intent; + } + assert.ok(intent); + const body = await contract.body(f); + return { receipt, intent, body, invoke: () => contract.invoke(f, body, intent) }; +} + +async function settled(request) { + try { return await request.invoke(); } + catch (error) { return { updated: false, failure: error.name }; } +} + +function assertPending(f) { + const state = f.store.getState().bhr; + assert.equal(state.pendingAllBackupByAppId[f.appId], true, 'Failed/uncertain operation must remain pending'); + assert.notEqual(state.backupRepairStateByAppId[f.appId], 'verified'); + assert.notEqual(state.backupRepairCompletedByAppId[f.appId], true); +} + +for (const operation of Object.keys(operations)) { + test(`${operation}: matching committed native payload/target reaches real v2 once`, async (t) => { + const f = await setup(t, operation); + const request = await committedRequest(f, operation); + const result = await settled(request); + assert.equal(result.updated, true); + assert.equal(attemptedWrites().length, 1); + assert.equal(attemptedWrites()[0].operation, operation); + assert.equal(attemptedWrites()[0].committed, true); + if (operation !== 'deleteBackup') { + const remote = await server.snapshot(f.appId); + const decoded = await f.image.decodeImage(f.encryption.generateEncryptionKey(f.seed), remote, f.appId); + assert.equal(await f.image.compareImages(await f.repair.captureBackupImage(), decoded), 'matched'); + assert.equal(f.store.getState().bhr.backupRepairStateByAppId[f.appId], 'verified'); + assert.ok(f.nativeEvents.length > 0, 'Actual native change listeners must observe persistence'); + } else { + assert.ok(f.dbManager.getObjectById(f.RealmSchema.Wallet, 'wallet-a'), 'Opt-out retains native local wallet'); + assert.equal(f.store.getState().bhr.automaticCloudBackupByAppId[f.appId], false); + } + }); + + test(`${operation}: queued receipt is invalid after a subsequent native local mutation`, async (t) => { + const f = await setup(t, operation); + const request = await committedRequest(f, operation); + const gate = barrier(); + t.after(gate.release); + f.transport.withBackupSession(f.appId, () => gate.promise); + const result = settled(request); + persist(f, f.RealmSchema.Tags, f.defaults(f.RealmSchema.Tags, { id: 'later-local-change', type: 'TX', ref: 'later', label: 'Changed after receipt' })); + gate.release(); + await result; + assert.equal(attemptedWrites().length, 0); + assertPending(f); + }); + + test(`${operation}: queued A to B to A receipt cannot send in the new session`, async (t) => { + const f = await setup(t, operation); + const request = await committedRequest(f, operation); + const gate = barrier(); + t.after(gate.release); + f.transport.withBackupSession(f.appId, () => gate.promise); + const result = settled(request); + await f.selectAccount('disposable-account-b'); + await f.selectAccount(f.appId); + gate.release(); + await result; + assert.equal(attemptedWrites().length, 0); + assertPending(f); + }); + + for (const mode of ['409', '404', 'malformed', 'lost-reply', 'equal-old-ack']) { + test(`${operation}: actual HTTP ${mode} stops the write and never reports verified`, async (t) => { + const f = await setup(t, operation); + const request = await committedRequest(f, operation); + server.armFault({ mode, operation }); + const result = await settled(request); + assert.notEqual(result.backupConfirmed, true); + assert.equal(attemptedWrites().length, 1); + assertPending(f); + if (operation === 'deleteBackup') assert.equal(f.store.getState().bhr.automaticCloudBackupByAppId[f.appId], true, 'Opt-out consent changes only after a positive acknowledged deletion'); + const generation = f.transport.backupRevision(f.appId); + const check = await f.repair.inspectBackup(f.appId, false, () => {}); + assert.ok(['verified', 'different', 'unverified', 'conflict'].includes(check)); + assert.equal(attemptedWrites().length, 1, 'Read-only reconciliation cannot repeat an uncertain write'); + assert.ok(f.transport.backupRevision(f.appId) >= generation); + }); + } + + for (const [failure, acknowledgement] of Object.entries({ + 'updated false': { updated: false, preconditionApplied: true, newRevision: 'c'.repeat(64) }, + 'missing updated': { preconditionApplied: true, newRevision: 'c'.repeat(64) }, + 'precondition false': { updated: true, preconditionApplied: false, newRevision: 'c'.repeat(64) }, + 'missing precondition': { updated: true, newRevision: 'c'.repeat(64) }, + 'missing new revision': { updated: true, preconditionApplied: true }, + })) test(`${operation}: a real committed write with ${failure} acknowledgement remains pending`, async (t) => { + const f = await setup(t, operation); + const request = await committedRequest(f, operation); + server.armFault({ mode: 'malformed', operation, acknowledgement }); + await settled(request); + assert.equal(attemptedWrites().length, 1); + assert.equal(attemptedWrites()[0].committed, true); + assertPending(f); + if (operation === 'deleteBackup') assert.equal(f.store.getState().bhr.automaticCloudBackupByAppId[f.appId], true); + }); + + test(`${operation}: genuine delayed HTTP reply is aborted and remains pending`, async (t) => { + const f = await setup(t, operation, { backupTimerCeilingMs: 200 }); + const request = await committedRequest(f, operation); + server.armFault({ mode: 'delay-reply', operation, delayMs: 1000 }); + await settled(request); + assert.equal(attemptedWrites().length, 1); + assertPending(f); + if (operation === 'deleteBackup') assert.equal(f.store.getState().bhr.automaticCloudBackupByAppId[f.appId], true); + }); + + for (const remoteChange of ['remote-only', 'key-conflict']) { + test(`${operation}: a peer ${remoteChange} snapshot cannot be overwritten by the old local receipt`, async (t) => { + const f = await setup(t, operation); + const baseline = await f.payloadForCurrentImage(); + const request = await committedRequest(f, operation); + const peerPayload = { ...baseline, walletObject: { ...baseline.walletObject } }; + const value = wallet(f, remoteChange === 'remote-only' ? 'server-only-wallet' : 'retained-wallet'); + if (remoteChange === 'key-conflict') value.specs.xpub = 'different-public-key-identity'; + peerPayload.walletObject[value.id] = await f.image.encryptRecord(f.encryption.generateEncryptionKey(f.seed), value); + await server.seed(peerPayload); + server.clearTrace(); + await settled(request); + assert.equal(attemptedWrites().length, 0); + assertPending(f); + const remote = await server.snapshot(f.appId); + const decoded = await f.image.decodeImage(f.encryption.generateEncryptionKey(f.seed), remote, f.appId); + assert.equal(decoded.wallets[value.id].specs.xpub, value.specs.xpub, 'Peer recovery content must survive intact'); + }); + } + + test(`${operation}: current generation cannot authorize a mismatched decrypted payload or deletion target`, async (t) => { + const f = await setup(t, operation); + const request = await committedRequest(f, operation); + const key = f.encryption.generateEncryptionKey(f.seed); + async function staleCipher(cipher, mutate) { + const value = await f.image.decryptRecord(key, cipher); + mutate(value); + return f.image.encryptRecord(key, value); + } + if (operation === 'updateAppImage') request.body.walletsObject['wallet-a'] = await staleCipher(request.body.walletsObject['wallet-a'], (value) => { value.presentationData.name = 'Uncommitted action data'; }); + if (operation === 'migrateXfps') request.body.signerChanges[0].newSignerDetails = await staleCipher(request.body.signerChanges[0].newSignerDetails, (value) => { value.signerName = 'Uncommitted action data'; }); + if (operation === 'deleteAppImageEntity') request.body.walletIds = ['retained-wallet']; + if (operation === 'deleteVaults') request.body.vaults = ['retained-vault']; + if (operation === 'updateVaultImage') request.body.vault = await staleCipher(request.body.vault, (value) => { value.presentationData.name = 'Uncommitted action data'; }); + if (operation === 'modifyLabels') request.body.addLabels[0].content = await staleCipher(request.body.addLabels[0].content, (value) => { value.label = 'Uncommitted action data'; }); + if (operation === 'deleteBackup') request.body.appId = 'different-disposable-account'; + await settled(request); + assert.equal(attemptedWrites().length, 0); + assertPending(f); + }); + + test(`${operation}: failed actual Realm commit supplies no usable write intent`, async (t) => { + const f = await setup(t, operation); + const local = plain(f.nativeRealm().objects('Tags')); + const op = f.automatic.beginBackupLocalOperation(f.appId, f.transport.captureBackupWriteIntent(f.appId), f.transport.currentBackupAccountSession()); + const receipt = f.automatic.commitBackupLocalOperation(op, (realm) => { + realm.create('Tags', f.defaults(f.RealmSchema.Tags, { id: 'rolled-back', type: 'TX', ref: 'rollback', label: 'Must never persist' })); + throw new Error('Disposable native transaction failure'); + }); + assert.equal(receipt.committed, false); + assert.equal(receipt.intent, undefined); + assert.deepEqual(plain(f.nativeRealm().objects('Tags')), local, 'Real native transaction rollback must preserve the entire prior collection'); + const body = { appId: f.appId }; + await settled({ invoke: () => operations[operation].invoke(f, body, receipt.intent) }); + assert.equal(attemptedWrites().length, 0); + assertPending(f); + }); + + test(`${operation}: copying a branded committed intent never authorizes a request`, async (t) => { + const f = await setup(t, operation); + const request = await committedRequest(f, operation); + await settled({ invoke: () => operations[operation].invoke(f, request.body, { ...request.intent }) }); + assert.equal(attemptedWrites().length, 0); + assertPending(f); + }); + + test(`${operation}: a missing committed receipt sends nothing and never borrows the current baseline`, async (t) => { + const f = await setup(t, operation); + const request = await committedRequest(f, operation); + await f.automatic.enqueueAutomaticBackupOperation(undefined, [{ operation, body: request.body }]); + assert.equal(attemptedWrites().length, 0); + assertPending(f); + }); + + test(`${operation}: closed and reopened native Realm rejects the old queued receipt`, async (t) => { + const f = await setup(t, operation); + const request = await committedRequest(f, operation); + const gate = barrier(); + t.after(gate.release); + f.transport.withBackupSession(f.appId, () => gate.promise); + const result = settled(request); + const original = f.nativeRealm(); + f.realmDatabase.closeDatabase(); + await f.selectAccount(f.appId); + assert.equal(original.isClosed, true); + assert.notEqual(f.nativeRealm(), original); + gate.release(); + await result; + assert.equal(attemptedWrites().length, 0); + assertPending(f); + }); + + test(`${operation}: a malformed missing-reference snapshot authorizes no mutation`, async (t) => { + const f = await setup(t, operation); + const request = await committedRequest(f, operation); + server.armFault({ mode: 'before-snapshot-reply', appId: f.appId, beforeReply({ body }) { + return { ...body, appImage: { ...body.appImage, vaults: [...body.appImage.vaults, 'missing-vault-reference'] } }; + } }); + await settled(request); + assert.equal(attemptedWrites().length, 0); + assertPending(f); + }); + + test(`${operation}: a later consent-off choice invalidates the previously prepared operation`, async (t) => { + const f = await setup(t, operation); + const request = await committedRequest(f, operation); + f.store.dispatch(f.bhr.setAutomaticCloudBackup({ appId: f.appId, enabled: false })); + await settled(request); + assert.equal(attemptedWrites().length, 0); + assertPending(f); + assert.equal(f.store.getState().bhr.automaticCloudBackupByAppId[f.appId], false); + }); + + test(`${operation}: wrong-key final ciphertext leaves the acknowledged update pending`, async (t) => { + const f = await setup(t, operation); + const request = await committedRequest(f, operation); + const wrongCipher = await f.image.encryptRecord(f.encryption.generateEncryptionKey('different-public-fixture-key'), wallet(f, 'retained-wallet')); + server.armFault({ mode: 'before-snapshot-reply', appId: f.appId, after: 1, beforeReply({ body }) { + return { ...body, appImage: { ...body.appImage, wallets: { ...body.appImage.wallets, 'retained-wallet': wrongCipher } } }; + } }); + await settled(request); + assert.equal(attemptedWrites().length, 1); + assert.equal(attemptedWrites()[0].committed, true); + assertPending(f); + if (operation === 'deleteBackup') assert.equal(f.store.getState().bhr.automaticCloudBackupByAppId[f.appId], true); + }); +} + +for (const fanout of ['create-and-archive-vaults', 'shared-vault-registration', 'label-import-and-delete']) { + test(`${fanout}: first real CAS succeeds, second fails, remaining effects stop and all local data survives`, async (t) => { + const f = await setup(t, fanout === 'label-import-and-delete' ? 'modifyLabels' : 'updateVaultImage'); + if (fanout !== 'label-import-and-delete') { + persistVault(f, 'vault-b'); persistVault(f, 'vault-c'); + await server.seed(await f.payloadForCurrentImage()); + assert.equal(await f.sagas.run('runBackupInspection', false, f.appId), true); + server.clearTrace(); + } + const op = f.automatic.beginBackupLocalOperation(f.appId, f.transport.captureBackupWriteIntent(f.appId), f.transport.currentBackupAccountSession()); + const receipt = f.automatic.commitBackupLocalOperation(op, (realm) => { + if (fanout === 'create-and-archive-vaults') { + realm.objectForPrimaryKey('Vault', 'vault-a').archived = true; + realm.create('Vault', vault(f, 'new-vault'), Realm.UpdateMode.Modified); + realm.objectForPrimaryKey('Vault', 'vault-c').presentationData.name = 'Third prepared effect'; + } else if (fanout === 'shared-vault-registration') { + realm.objectForPrimaryKey('VaultSigner', 'disposable-vault-public-key').registeredVaults = [ + { vaultId: 'vault-a', registered: true }, { vaultId: 'vault-b', registered: true }, { vaultId: 'vault-c', registered: true }, + ]; + } else { + realm.delete(realm.objectForPrimaryKey('Tags', 'old-label')); + for (const id of ['label-one', 'label-two', 'label-three']) realm.create('Tags', f.defaults(f.RealmSchema.Tags, { id, type: 'TX', ref: id, label: id })); + } + }); + assert.equal(receipt.committed, true); + const before = await f.repair.captureBackupImage(); + const mutations = []; + if (fanout === 'label-import-and-delete') { + for (const [index, id] of ['label-one', 'label-two', 'label-three'].entries()) mutations.push({ operation: 'modifyLabels', body: await labelBody(f, [id], index === 0 ? ['old-label'] : []) }); + } else { + for (const id of fanout === 'create-and-archive-vaults' ? ['vault-a', 'new-vault', 'vault-c'] : ['vault-a', 'vault-b', 'vault-c']) mutations.push({ operation: 'updateVaultImage', body: await vaultBody(f, id) }); + } + server.armFault({ mode: '409', operation: mutations[0].operation, after: 1 }); + const result = await f.automatic.enqueueAutomaticBackupOperation(receipt.intent, mutations); + assert.notEqual(result.backupConfirmed, true); + assert.equal(attemptedWrites().length, 2); + assert.equal(attemptedWrites()[0].committed, true); + assert.equal(attemptedWrites()[1].committed, false); + assert.equal(await f.image.compareImages(before, await f.repair.captureBackupImage()), 'matched'); + assertPending(f); + }); +} + +test('real native mutation immediately before Redux final verification commit prevents a verified claim', async (t) => { + const f = await setup(t, 'updateAppImage'); + const request = await committedRequest(f, 'updateAppImage'); + let injected = false; + f.setBeforeDispatch((action) => { + if (!injected && action.type === f.bhr.commitBackupInspection.type) { + injected = true; + persist(f, f.RealmSchema.Tags, f.defaults(f.RealmSchema.Tags, { id: 'final-commit-race', type: 'TX', ref: 'race', label: 'Native change at final Redux gate' })); + } + }); + await settled(request); + assert.equal(injected, true, 'The actual production verification dispatch must be exercised'); + assert.equal(attemptedWrites().length, 1); + assertPending(f); +}); + +test('actual BHR app producer builds ciphertext from committed native IDs rather than stale action data', async (t) => { + const f = await setup(t, 'updateAppImage'); + const request = await committedRequest(f, 'updateAppImage'); + const producer = f.productionSagas(['currentBackupAppId', 'buildAutomaticAppMutation', 'queueCommittedBackupWorker', 'updateAppImageWorker']); + const result = await producer.run('updateAppImageWorker', { + payload: { wallets: [wallet(f, 'wallet-a', 'Old delayed action name')] }, + originAppId: f.appId, originBackupAccountSession: f.transport.currentBackupAccountSession(), committedOperation: request.receipt, + }); + assert.equal(result.backupConfirmed, true); + assert.equal(attemptedWrites().length, 1); + const restored = await f.image.decodeImage(f.encryption.generateEncryptionKey(f.seed), await server.snapshot(f.appId), f.appId); + assert.equal(restored.wallets['wallet-a'].presentationData.name, 'Committed wallet'); +}); + +test('actual BHR node producer reads the genuine persisted collection and clears nodes with an exact set', async (t) => { + const f = await setup(t, 'updateAppImage'); + const producer = f.productionSagas(['currentBackupAppId', 'buildAutomaticAppMutation', 'queueCommittedBackupWorker', 'updateAppImageWorker']); + const origin = f.transport.captureBackupWriteIntent(f.appId); + const op = f.automatic.beginBackupLocalOperation(f.appId, origin, f.transport.currentBackupAccountSession()); + const receipt = f.automatic.commitBackupLocalOperation(op, (realm) => realm.create('NodeConnect', f.defaults(f.RealmSchema.NodeConnect, { + id: 12, host: 'public-disposable-node.invalid', port: '50002', useSSL: true, networkType: f.enums.NetworkType.TESTNET, + }))); + assert.equal(receipt.committed, true); + const result = await producer.run('updateAppImageWorker', { payload: { updateNodes: true }, originAppId: f.appId, + originBackupAccountSession: f.transport.currentBackupAccountSession(), committedOperation: receipt }); + assert.equal(result.backupConfirmed, true); + assert.equal(attemptedWrites().length, 1); + let remote = await f.image.decodeImage(f.encryption.generateEncryptionKey(f.seed), await server.snapshot(f.appId), f.appId); + assert.equal(remote.nodes['12'].host, 'public-disposable-node.invalid'); + server.clearTrace(); + const deletion = f.automatic.beginBackupLocalOperation(f.appId, f.transport.captureBackupWriteIntent(f.appId), f.transport.currentBackupAccountSession()); + const removed = f.automatic.commitBackupLocalOperation(deletion, (realm) => realm.delete(realm.objects('NodeConnect'))); + const cleared = await producer.run('updateAppImageWorker', { payload: { updateNodes: true }, originAppId: f.appId, + originBackupAccountSession: f.transport.currentBackupAccountSession(), committedOperation: removed }); + assert.equal(cleared.backupConfirmed, true); + assert.equal(attemptedWrites().length, 1); + remote = await f.image.decodeImage(f.encryption.generateEncryptionKey(f.seed), await server.snapshot(f.appId), f.appId); + assert.deepEqual(Object.keys(remote.nodes), []); +}); + +test('actual BHR vault producer builds from shared native registrations and acknowledges the complete fanout', async (t) => { + const f = await setup(t, 'updateVaultImage'); + persistVault(f, 'vault-b'); + await server.seed(await f.payloadForCurrentImage()); + assert.equal(await f.sagas.run('runBackupInspection', false, f.appId), true); + server.clearTrace(); + const producer = f.productionSagas(['currentBackupAppId', 'buildAutomaticVaultMutation', 'queueCommittedBackupWorker']); + const op = f.automatic.beginBackupLocalOperation(f.appId, f.transport.captureBackupWriteIntent(f.appId), f.transport.currentBackupAccountSession()); + const receipt = f.automatic.commitBackupLocalOperation(op, (realm) => { + realm.objectForPrimaryKey('VaultSigner', 'disposable-vault-public-key').registeredVaults = [ + { vaultId: 'vault-a', registered: true, hmac: 'disposable-receipt' }, { vaultId: 'vault-b', registered: true, hmac: 'disposable-receipt' }, + ]; + }); + const mutations = ['vault-a', 'vault-b'].map((id) => producer.scope.buildAutomaticVaultMutation(id, f.appId)); + const result = await producer.run('queueCommittedBackupWorker', f.appId, receipt, mutations, f.transport.currentBackupAccountSession()); + assert.equal(result.backupConfirmed, true); + assert.equal(attemptedWrites().length, 2); + const remote = await f.image.decodeImage(f.encryption.generateEncryptionKey(f.seed), await server.snapshot(f.appId), f.appId); + for (const id of ['vault-a', 'vault-b']) assert.equal(remote.vaults[id].signers[0].registeredVaults.length, 2); + assert.equal(f.store.getState().bhr.pendingAllBackupByAppId[f.appId], false); +}); + +test('create plus archive fanout proves archive target before mutation and verifies the full actual image', async (t) => { + const f = await setup(t, 'updateVaultImage'); + const op = f.automatic.beginBackupLocalOperation(f.appId, f.transport.captureBackupWriteIntent(f.appId), f.transport.currentBackupAccountSession()); + const receipt = f.automatic.commitBackupLocalOperation(op, (realm) => { + realm.objectForPrimaryKey('Vault', 'vault-a').archived = true; + realm.create('Vault', vault(f, 'new-vault'), Realm.UpdateMode.Modified); + }); + assert.equal(receipt.committed, true); + const producer = f.productionSagas(['currentBackupAppId', 'buildAutomaticVaultMutation', 'queueCommittedBackupWorker']); + const create = producer.scope.buildAutomaticVaultMutation('new-vault', f.appId, false); + create.body.archiveVaultId = 'vault-a'; + const archived = producer.scope.buildAutomaticVaultMutation('vault-a', f.appId); + const result = await producer.run('queueCommittedBackupWorker', f.appId, receipt, [archived, create], f.transport.currentBackupAccountSession()); + assert.equal(result.backupConfirmed, true); + assert.equal(attemptedWrites().length, 2); + const remote = await f.image.decodeImage(f.encryption.generateEncryptionKey(f.seed), await server.snapshot(f.appId), f.appId); + assert.equal(remote.vaults['vault-a'].archived, true); + assert.equal(remote.vaults['new-vault'].archived, false); +}); + +test('an archive alias without its preceding committed archive ciphertext sends nothing', async (t) => { + const f = await setup(t, 'updateVaultImage'); + const op = f.automatic.beginBackupLocalOperation(f.appId, f.transport.captureBackupWriteIntent(f.appId), f.transport.currentBackupAccountSession()); + const receipt = f.automatic.commitBackupLocalOperation(op, (realm) => { + realm.objectForPrimaryKey('Vault', 'vault-a').archived = true; + realm.create('Vault', vault(f, 'new-vault'), Realm.UpdateMode.Modified); + }); + const body = await vaultBody(f, 'new-vault'); + body.archiveVaultId = 'vault-a'; + const result = await f.automatic.enqueueAutomaticBackupOperation(receipt, [{ operation: 'updateVaultImage', body }]); + assert.notEqual(result.backupConfirmed, true); + assert.equal(attemptedWrites().length, 0); + assertPending(f); +}); + +for (const field of ['publicId', 'version', 'subscription']) { + test(`complete final readback with malformed ${field} cannot claim verification`, async (t) => { + const f = await setup(t, 'updateAppImage'); + const request = await committedRequest(f, 'updateAppImage'); + server.armFault({ mode: 'malformed-metadata', appId: f.appId, after: 1, + metadata: { [field]: field === 'subscription' ? '{}' : 'not-the-committed-value' } }); + await settled(request); + assert.equal(attemptedWrites().length, 1); + assert.equal(attemptedWrites()[0].committed, true); + assertPending(f); + }); +} + +function disposableExtendedKey(f, byte) { + const ecc = f.load('src/services/wallets/operations/taproot-utils/noble_ecc').default; + const bip32 = require('bip32').default(ecc); + return bip32.fromSeed(Buffer.alloc(32, byte), require('bitcoinjs-lib').networks.testnet).derivePath("m/48'/1'/0'/2'"); +} + +for (const field of ['xpub', 'private-key', 'network']) { + test(`a same-ID signer ${field} identity replacement cannot upload committed native data`, async (t) => { + const f = await setup(t, 'migrateXfps'); + const id = signer(f).id; + const original = disposableExtendedKey(f, 0x31); + const replacement = disposableExtendedKey(f, 0x32); + f.nativeRealm().write(() => { + f.nativeRealm().objectForPrimaryKey('Signer', id).signerXpubs[f.enums.XpubTypes.P2WSH][0].xpub = original.neutered().toBase58(); + }); + await server.seed(await f.payloadForCurrentImage()); + assert.equal(await f.sagas.run('runBackupInspection', false, f.appId), true); + server.clearTrace(); + const op = f.automatic.beginBackupLocalOperation(f.appId, f.transport.captureBackupWriteIntent(f.appId), f.transport.currentBackupAccountSession()); + const receipt = f.automatic.commitBackupLocalOperation(op, (realm) => { + const record = realm.objectForPrimaryKey('Signer', id); + if (field === 'xpub') record.signerXpubs[f.enums.XpubTypes.P2WSH][0].xpub = replacement.neutered().toBase58(); + if (field === 'private-key') record.signerXpubs[f.enums.XpubTypes.P2WSH][0].xpriv = replacement.toBase58(); + if (field === 'network') record.networkType = f.enums.NetworkType.MAINNET; + }); + assert.equal(receipt.committed, true); + const current = await f.repair.captureBackupImage(); + const result = await f.automatic.enqueueAutomaticBackupOperation(receipt, [{ operation: 'updateAppImage', body: { + appId: f.appId, signersObject: { [id]: await f.image.encryptRecord(f.encryption.generateEncryptionKey(f.seed), current.signers[id]) }, + } }]); + assert.notEqual(result.backupConfirmed, true); + assert.equal(attemptedWrites().length, 0); + assertPending(f); + assert.equal(current.signers[id].id, id, 'The rejection is for a changed identity at the same persisted ID'); + }); +} + +for (const field of ['key', 'policy']) { + test(`a same-ID vault ${field} replacement cannot upload committed native data`, async (t) => { + const f = await setup(t, 'updateVaultImage'); + const op = f.automatic.beginBackupLocalOperation(f.appId, f.transport.captureBackupWriteIntent(f.appId), f.transport.currentBackupAccountSession()); + const receipt = f.automatic.commitBackupLocalOperation(op, (realm) => { + const record = realm.objectForPrimaryKey('Vault', 'vault-a'); + if (field === 'policy') record.scheme.m = 2; + else { + const xpub = disposableExtendedKey(f, 0x33).neutered().toBase58(); + const previous = plain(record.signers[0]); + record.signers = [realm.create('VaultSigner', { ...previous, xpub }, Realm.UpdateMode.Modified)]; + record.specs.xpubs = [xpub]; + realm.objectForPrimaryKey('Signer', signer(f).id).signerXpubs[f.enums.XpubTypes.P2WSH][0].xpub = xpub; + } + }); + assert.equal(receipt.committed, true); + const result = await f.automatic.enqueueAutomaticBackupOperation(receipt, [{ operation: 'updateVaultImage', body: await vaultBody(f, 'vault-a') }]); + assert.notEqual(result.backupConfirmed, true); + assert.equal(attemptedWrites().length, 0); + assertPending(f); + assert.ok(f.dbManager.getObjectById(f.RealmSchema.Vault, 'vault-a'), 'The committed local record remains available for an explicit repair choice'); + }); +} + +test('a cloned originating baseline cannot mint an automatic receipt from a genuine local commit', async (t) => { + const f = await setup(t, 'updateAppImage'); + const original = f.transport.captureBackupWriteIntent(f.appId); + const op = f.automatic.beginBackupLocalOperation(f.appId, { ...original }, f.transport.currentBackupAccountSession()); + const receipt = f.automatic.commitBackupLocalOperation(op, (realm) => operations.updateAppImage.mutate(realm, f)); + assert.equal(receipt.committed, true, 'Local saving remains available without cloud proof'); + assert.equal(receipt.intent, undefined); + await f.automatic.enqueueAutomaticBackupOperation(receipt, [{ operation: 'updateAppImage', body: await operations.updateAppImage.body(f) }]); + assert.equal(attemptedWrites().length, 0); + assertPending(f); +}); + +test('an uncommitted native image or caller boolean cannot issue a committed-generation proof', async (t) => { + const f = await setup(t, 'updateAppImage'); + const op = f.automatic.beginBackupLocalOperation(f.appId, f.transport.captureBackupWriteIntent(f.appId), f.transport.currentBackupAccountSession()); + let observedInside; + assert.throws(() => f.nativeRealm().write(() => { + f.nativeRealm().create('Tags', f.defaults(f.RealmSchema.Tags, { id: 'uncommitted-tag', type: 'TX', ref: 'pending', label: 'Must roll back' })); + observedInside = f.transport.captureBackupWriteIntent(f.appId); + assert.throws(() => f.automatic.beginBackupLocalOperation(f.appId, observedInside)); + throw new Error('Deliberate rollback'); + }), /Deliberate rollback/); + assert.equal(observedInside, undefined); + assert.equal(f.nativeRealm().objectForPrimaryKey('Tags', 'uncommitted-tag'), null); + const forgedCompletion = f.automatic.completeBackupLocalOperation(op, true); + assert.equal(forgedCompletion.intent, undefined); + assert.equal(attemptedWrites().length, 0); +}); + +test('a selected-account switch inside the final native transaction callback rolls back the whole operation', async (t) => { + const f = await setup(t, 'updateAppImage'); + const before = plain(f.dbManager.getObjectById(f.RealmSchema.Wallet, 'wallet-a')); + const op = f.automatic.beginBackupLocalOperation(f.appId, f.transport.captureBackupWriteIntent(f.appId), f.transport.currentBackupAccountSession()); + const receipt = f.automatic.commitBackupLocalOperation(op, (realm) => { + operations.updateAppImage.mutate(realm, f); + f.store.dispatch(f.storage.setAppId('different-disposable-account')); + }); + assert.equal(receipt.committed, false); + assert.equal(receipt.intent, undefined); + assert.deepEqual(plain(f.dbManager.getObjectById(f.RealmSchema.Wallet, 'wallet-a')), before); + assert.equal(attemptedWrites().length, 0); +}); + +test('fixture authorization never permits a hosted Relay target', async (t) => { + const f = await setup(t, 'updateAppImage'); + const request = await committedRequest(f, 'updateAppImage'); + f.config.RELAY = 'https://hosted-recovery-fixture.invalid/'; + await settled(request); + assert.equal(attemptedWrites().length, 0); + assertPending(f); +}); + +for (const mode of ['development-loopback', 'production-environment', 'development-disabled', 'missing-explicit-port']) { + test(`without the Node fixture override ${mode} obeys the native source and rollout gates`, async (t) => { + const f = await setup(t, 'updateAppImage'); + const request = await committedRequest(f, 'updateAppImage'); + if (mode === 'production-environment') f.config.ENVIRONMENT = 'PRODUCTION'; + if (mode === 'development-disabled') f.config.isDevMode = () => false; + if (mode === 'missing-explicit-port') f.config.RELAY = 'http://127.0.0.1/'; + const fixtureModule = process.env.KEEPER_RELAY_FIXTURE_MODULE; + delete process.env.KEEPER_RELAY_FIXTURE_MODULE; + try { + await settled(request); + } finally { + process.env.KEEPER_RELAY_FIXTURE_MODULE = fixtureModule; + } + if (mode === 'development-loopback' && f.automatic.AUTOMATIC_BACKUP_SOURCE_READY) { + assert.equal(attemptedWrites().length, 1); + assert.equal(f.store.getState().bhr.pendingAllBackupByAppId[f.appId], false); + } else { + assert.equal(attemptedWrites().length, 0); + assertPending(f); + } + assert.equal(f.automatic.AUTOMATIC_BACKUP_RELAY_ROLLOUT_READY, false, 'Disposable source proof never enables the hosted Relay fleet'); + }); +} + +test('a counterfeit local operation never executes its native transaction callback', async (t) => { + const f = await setup(t, 'updateAppImage'); + const genuine = f.automatic.beginBackupLocalOperation(f.appId, f.transport.captureBackupWriteIntent(f.appId), f.transport.currentBackupAccountSession()); + let entered = false; + const receipt = f.automatic.commitBackupLocalOperation({ ...genuine }, (realm) => { + entered = true; + operations.updateAppImage.mutate(realm, f); + }); + assert.equal(entered, false); + assert.equal(receipt.committed, false); + assert.equal(receipt.intent, undefined); + assert.equal(f.dbManager.getObjectById(f.RealmSchema.Wallet, 'wallet-a').presentationData.name, 'Initial wallet'); + assert.equal(attemptedWrites().length, 0); +}); + +test('actual BHR Delete Backup worker acknowledges an explicit deletion without discarding native data', async (t) => { + const f = await setup(t, 'deleteBackup'); + const worker = f.productionSagas(['currentBackupAppId', 'deleteBackupWorker']); + await worker.run('deleteBackupWorker', { appId: f.appId, originBackupAccountSession: f.transport.currentBackupAccountSession() }); + assert.equal(attemptedWrites().length, 1); + assert.equal(attemptedWrites()[0].committed, true); + assert.equal(f.store.getState().bhr.deleteBackupSuccessByAppId[f.appId], true); + assert.equal(f.store.getState().bhr.automaticCloudBackupByAppId[f.appId], false); + assert.ok(f.dbManager.getObjectById(f.RealmSchema.Wallet, 'wallet-a')); +}); + +test('a fresh explicit whole-account deletion works after automatic consent is already off', async (t) => { + const f = await setup(t, 'deleteBackup'); + f.store.dispatch(f.bhr.setAutomaticCloudBackup({ appId: f.appId, enabled: false })); + const request = await committedRequest(f, 'deleteBackup'); + const result = await settled(request); + assert.equal(result.backupConfirmed, true); + assert.equal(attemptedWrites().length, 1); + assert.equal(f.store.getState().bhr.automaticCloudBackupByAppId[f.appId], false); +}); + +test('a newer off-to-on choice while the actual delete reply is delayed cannot be overwritten', async (t) => { + const f = await setup(t, 'deleteBackup'); + const request = await committedRequest(f, 'deleteBackup'); + server.armFault({ mode: 'delay-reply', operation: 'deleteBackup', delayMs: 250 }); + const completion = settled(request); + const deadline = Date.now() + 5000; + while (!attemptedWrites().some((entry) => entry.committed)) { + assert.ok(Date.now() < deadline, 'The genuine server deletion must reach its delayed reply'); + await new Promise((resolve) => setTimeout(resolve, 5)); + } + f.store.dispatch(f.bhr.setAutomaticCloudBackup({ appId: f.appId, enabled: false })); + f.store.dispatch(f.bhr.setAutomaticCloudBackup({ appId: f.appId, enabled: true })); + const result = await completion; + assert.notEqual(result.backupConfirmed, true); + assert.equal(f.store.getState().bhr.automaticCloudBackupByAppId[f.appId], true); + assertPending(f); +}); + +test('a reentrant newer enable choice immediately before final consent disable remains selected', async (t) => { + const f = await setup(t, 'deleteBackup'); + const request = await committedRequest(f, 'deleteBackup'); + let changed = false; + f.setBeforeDispatch((action) => { + if (!changed && action.type === f.bhr.setAutomaticCloudBackup.type && action.payload?.enabled === false) { + changed = true; + f.store.dispatch(f.bhr.setAutomaticCloudBackup({ appId: f.appId, enabled: true })); + } + }); + const result = await settled(request); + assert.equal(changed, true); + assert.notEqual(result.backupConfirmed, true); + assert.equal(f.store.getState().bhr.automaticCloudBackupByAppId[f.appId], true); + assertPending(f); +}); From 0f37506a58e6bf44061dd6788b20e5fa095a10c6 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Tue, 13 Oct 2026 03:58:42 +0000 Subject: [PATCH 170/254] Permit proven development loopback backup testing while fencing hosted rollout --- src/services/backup/automatic.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/services/backup/automatic.ts b/src/services/backup/automatic.ts index 790a33a1cc..c5514647b1 100644 --- a/src/services/backup/automatic.ts +++ b/src/services/backup/automatic.ts @@ -67,9 +67,10 @@ const managedCommits = new WeakSet(); const results = new WeakMap(); const generations = new Map(); let sequence = 0; -// Source proof is being exercised on disposable loopback fixtures. Hosted -// writes remain fenced until the matrix and the Relay fleet rollout are approved. -export const AUTOMATIC_BACKUP_SOURCE_READY = false; +// The genuine Realm/transactional Relay matrix permits development loopback +// validation. Hosted writes remain fenced until the separate fleet rollout, +// old-writer drainage and operator evidence have been reviewed and approved. +export const AUTOMATIC_BACKUP_SOURCE_READY = true; export const AUTOMATIC_BACKUP_RELAY_ROLLOUT_READY = false; let commitVerification: (appId: string, deleted: boolean, consentEpoch: number) => boolean = () => false; From e1ab9ab071b5a7102c8b152aa2713e7093ce2182 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Tue, 13 Oct 2026 04:01:05 +0000 Subject: [PATCH 171/254] Resolve the exact Local test SHA from the live durable handoff --- release/r4-automatic-backup-local-handoff.md | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/release/r4-automatic-backup-local-handoff.md b/release/r4-automatic-backup-local-handoff.md index 5123730330..3bd58ee60f 100644 --- a/release/r4-automatic-backup-local-handoff.md +++ b/release/r4-automatic-backup-local-handoff.md @@ -1,10 +1,10 @@ # R4 / 2.6.4 automatic backup: Codex Local handoff -State: **NEEDS_LOCAL**. App source: **APP_HEAD_SHA**, PR [#7045](https://github.com/KeeperCommunity/bitcoin-keeper/pull/7045), branch `codex/r4-264-combined-candidate`, base `sprint`. The coordinator replaces `APP_HEAD_SHA` in the handoff prompt after the final Cloud push; Local must compare it with the live PR head before testing. +State: **NEEDS_LOCAL**. PR [#7045](https://github.com/KeeperCommunity/bitcoin-keeper/pull/7045), branch `codex/r4-264-combined-candidate`, base `sprint`. The exact final SHA is recorded in the newest **Codex local handoff** on that PR. The setup below verifies that handoff against the live head before selecting it; this repository document cannot contain its own eventual commit SHA. Paired disposable Relay: **`b600416e5ba7325927d7d2871cff7776f4e593fe`**, draft [Relay #4](https://github.com/KeeperCommunity/Relay/pull/4). This pin supersedes the earlier `5a42a03` fixture by adding explicit malformed-acknowledgement response overrides after actual commits. It is a loopback test dependency, not a deployed-service assertion. The approved contributor manifest in `dev/local-backend/sources.lock.json` remains Relay `ee935359f22ff7932ea0a62e9084d4b49801f4aa` and SigningServer `3cc7728c27328e7b0358d1f91a32d8d13feba90a`; do not replace those pins or overwrite generated contributor checkouts to prepare this test. -`AUTOMATIC_BACKUP_SOURCE_READY` must remain false until the complete genuine source matrix passes and its evidence is reviewed. `AUTOMATIC_BACKUP_RELAY_ROLLOUT_READY` remains false until the separately authorized deployed fleet, transactions/indexes, v2 route and old-writer drainage are verified. The Node fixture permits its explicit loopback test target. Neither permission nor a simulator test authorizes hosted writes. Do not flip either gate to make a test pass; a source-ready development build can exercise the separate loopback native fixture without enabling hosted rollout. +`AUTOMATIC_BACKUP_SOURCE_READY` is true after genuine disposable proof and independent fixture review. It permits development loopback validation with an explicit port. `AUTOMATIC_BACKUP_RELAY_ROLLOUT_READY` remains false until the separately authorized deployed fleet, transactions/indexes, v2 route and old-writer drainage are verified. The Node fixture permits its explicit loopback test target. Hosted writes remain fenced. Do not change either gate to make a test pass. Current source and results are recorded in [the proof matrix](../openspec/changes/fix-recovery-backup-freshness/automatic-write-validation.md). The historical [Local report](https://github.com/KeeperCommunity/bitcoin-keeper/pull/7045#issuecomment-6128906958) at `b160656218dc6065ad12af9db220c974255bfc23` is **LOCAL_FAILED**. Its unsigned iOS simulator compile and Android development build passed; install, launch and UI scenarios were not run because the Mac was locked. Seven automatic writes remained disabled and opted-in deletions remained blocked. Its physical deferral and earlier native evidence do not validate a newer SHA. No current app native pass is claimed by this document. @@ -13,7 +13,7 @@ The combined source also retains the bracketed iOS import and shared PIN keypad ## Copy into Codex Local ```text -Validate Keeper PR #7045 on APP_HEAD_SHA. +Validate Keeper PR #7045 on the exact Head commit in its newest Codex local handoff. Read the latest PR description, newest Codex local handoff, AGENTS.md and release/r4-automatic-backup-local-handoff.md at the final head. Read the complete @@ -54,12 +54,13 @@ is blocked, but do not waive a critical or required platform result. ## Safe exact-source setup -Select the existing Node **22** toolchain; use Yarn Classic **1.22.22**. Do not overwrite global tool configuration or existing Node/Realm installations. These commands create private temporary directories and fresh detached checkouts. Run them in Bash and retain the printed workspace path for follow-up. Replace `APP_HEAD_SHA` with the coordinator's full final SHA first. +Select the existing Node **22** toolchain; use Yarn Classic **1.22.22**. Do not overwrite global tool configuration or existing Node/Realm installations. These commands create private temporary directories and fresh detached checkouts. Run them in Bash and retain the printed workspace path for follow-up. GitHub CLI must have read access; a missing or mismatched exact-head handoff fails before checkout. ```bash set -euo pipefail umask 077 -keeper_app_sha=APP_HEAD_SHA +keeper_app_sha=$(gh pr view 7045 --repo KeeperCommunity/bitcoin-keeper --json headRefOid,body | + python3 -c 'import json,re,sys; p=json.load(sys.stdin); h=re.findall(r"(?m)^Head commit:\s*([0-9a-f]{40})\s*$",p["body"]); assert h and h[-1]==p["headRefOid"], "Missing or stale exact-head Codex local handoff"; print(h[-1])') keeper_relay_sha=b600416e5ba7325927d7d2871cff7776f4e593fe test "${#keeper_app_sha}" -eq 40 test "$(node -p 'process.versions.node.split(".")[0]')" = 22 @@ -242,7 +243,7 @@ Read [AGENTS.md](../AGENTS.md), including merged [#7050](https://github.com/Keep ## Codex local scope decision State: OWNER_DEFERRED_PHYSICAL PR: #7045 -Head commit: APP_HEAD_SHA +Head commit: Deferred local checks: HARDWARE Reason: The release owner directs unrun physical phone, camera, NFC/USB and hardware-signer checks to post-release testing because those devices are unavailable for this Local run. IOS and ANDROID and known critical failures are not deferred. Post-release follow-up: The owner will test the installed released iOS/Android builds on physical phones and available signers, including QR, address verification, vault policy and PSBT round trips, and report exact build/device outcomes on #7045 or the release record. @@ -255,7 +256,7 @@ After testing, post a new report preserving the earlier `LOCAL_FAILED` evidence: ```text ## Codex local validation State: LOCAL_PASSED | LOCAL_FAILED | BLOCKED -Tested commit: APP_HEAD_SHA +Tested commit: iOS build/simulator: PASS/FAIL/NOT RUN — exact commands, installed artifact identity, actual launch and critical UI/recovery outcomes; private log locations. Android build/emulator: PASS/FAIL/NOT RUN — exact commands, package/version/counter, debug certificate/hash/alignment, actual launch and critical UI/recovery outcomes; private log locations. Real devices/signers: NOT RUN / OWNER-DEFERRED PHYSICAL only if the exact-SHA owner decision exists; name its URL and every deferred scenario. Otherwise report actual results or BLOCKED. From f241f91ec4fd77fab5320caf9457ff3526add79d Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Tue, 13 Oct 2026 04:03:19 +0000 Subject: [PATCH 172/254] Keep development transport fenced pending the additional interleaved fanout proof --- src/services/backup/automatic.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/services/backup/automatic.ts b/src/services/backup/automatic.ts index c5514647b1..467d9fc642 100644 --- a/src/services/backup/automatic.ts +++ b/src/services/backup/automatic.ts @@ -70,7 +70,7 @@ let sequence = 0; // The genuine Realm/transactional Relay matrix permits development loopback // validation. Hosted writes remain fenced until the separate fleet rollout, // old-writer drainage and operator evidence have been reviewed and approved. -export const AUTOMATIC_BACKUP_SOURCE_READY = true; +export const AUTOMATIC_BACKUP_SOURCE_READY = false; export const AUTOMATIC_BACKUP_RELAY_ROLLOUT_READY = false; let commitVerification: (appId: string, deleted: boolean, consentEpoch: number) => boolean = () => false; From bccb3f3048248dfd8a7e7f47bffe60d646312399 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Tue, 13 Oct 2026 04:03:27 +0000 Subject: [PATCH 173/254] Require every genuine producer matrix and pin final Relay fixture --- .github/workflows/test.yml | 2 +- scripts/run-automatic-backup-genuine.cjs | 15 +++++++++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index ce50d5f497..fc1074dd81 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -101,7 +101,7 @@ jobs: uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4 with: repository: KeeperCommunity/Relay - ref: 5a42a0368223d952e1dda475859743ce30d08058 + ref: b600416e5ba7325927d7d2871cff7776f4e593fe path: .ci-relay persist-credentials: false diff --git a/scripts/run-automatic-backup-genuine.cjs b/scripts/run-automatic-backup-genuine.cjs index 215e3c88c0..bf5d4518d4 100644 --- a/scripts/run-automatic-backup-genuine.cjs +++ b/scripts/run-automatic-backup-genuine.cjs @@ -6,7 +6,7 @@ const os = require('node:os'); const path = require('node:path'); const { spawnSync } = require('node:child_process'); -const relaySha = '5a42a0368223d952e1dda475859743ce30d08058'; +const relaySha = 'b600416e5ba7325927d7d2871cff7776f4e593fe'; const root = path.resolve(__dirname, '..'); const fixture = process.env.KEEPER_RELAY_FIXTURE_MODULE; assert.equal(process.versions.node.split('.')[0], '22', 'Run the genuine automatic backup matrix with Node 22'); @@ -42,11 +42,14 @@ try { fs.rmSync(directory, { recursive: true, force: true }); } -const result = spawnSync(process.execPath, [ - '--test', - 'tests/recovery/automaticWriteRealmRelay.integration.cjs', - 'tests/recovery/automaticLabelProducers.integration.cjs', -], { +const matrices = fs.readdirSync(path.join(root, 'tests/recovery')) + .filter(filename => filename.endsWith('.integration.cjs')).sort(); +for (const required of ['automaticWriteRealmRelay.integration.cjs', + 'automaticLabelProducers.integration.cjs', 'automaticUpgradeProducer.integration.cjs', + 'automaticWalletProducer.integration.cjs', 'automaticUSDTProducer.integration.cjs', + 'automaticSignerProducer.integration.cjs', 'automaticRecoveryRestore.integration.cjs']) + assert.ok(matrices.includes(required), `Missing genuine production matrix ${required}`); +const result = spawnSync(process.execPath, ['--test', ...matrices.map(filename => `tests/recovery/${filename}`)], { cwd: root, stdio: 'inherit', env: { ...process.env, KEEPER_RELAY_FIXTURE_MODULE: filename, BACKUP_TEST_MONGO_URL: mongoUrl.toString() }, }); From 7b1ad70ba9e2aa1a2778b375f36e776bdd26a5ad Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Tue, 13 Oct 2026 04:08:18 +0000 Subject: [PATCH 174/254] test: prove held acknowledgement races across local writes and account switches --- .../automaticWriteRealmRelay.integration.cjs | 101 ++++++++++++++++++ 1 file changed, 101 insertions(+) diff --git a/tests/recovery/automaticWriteRealmRelay.integration.cjs b/tests/recovery/automaticWriteRealmRelay.integration.cjs index 431627d234..a260efd7b0 100644 --- a/tests/recovery/automaticWriteRealmRelay.integration.cjs +++ b/tests/recovery/automaticWriteRealmRelay.integration.cjs @@ -197,6 +197,31 @@ function assertPending(f) { assert.notEqual(state.backupRepairCompletedByAppId[f.appId], true); } +async function awaitCommittedReplyBarrier(request, appId) { + const deadline = Date.now() + 2000; + while (!server.trace.some((event) => event.path === '/revisionedBackupMutation' && event.appId === appId && event.committed)) { + assert.equal(request.isSettled(), false, 'The client must still be waiting for the actual held HTTP acknowledgement'); + assert.ok(Date.now() < deadline, 'The real route must commit before the bounded reply window expires'); + await new Promise((resolve) => setTimeout(resolve, 5)); + } + assert.equal(request.isSettled(), false, 'Observe a real committed transaction while its HTTP acknowledgement is still outstanding'); +} + +function outstanding(invoke) { + let isSettled = false; + const result = invoke().then((value) => { isSettled = true; return value; }, (error) => { + isSettled = true; + throw error; + }); + return { result, isSettled: () => isSettled }; +} + +function accountBackupFlags(f, appId) { + return Object.fromEntries(Object.entries(f.store.getState().bhr) + .filter(([name]) => name.endsWith('ByAppId')) + .map(([name, values]) => [name, values[appId]])); +} + for (const operation of Object.keys(operations)) { test(`${operation}: matching committed native payload/target reaches real v2 once`, async (t) => { const f = await setup(t, operation); @@ -463,6 +488,82 @@ for (const fanout of ['create-and-archive-vaults', 'shared-vault-registration', }); } +test('a genuine native write between fanout members stops every later member after the first real CAS', async (t) => { + const f = await setup(t, 'modifyLabels'); + const op = f.automatic.beginBackupLocalOperation(f.appId, f.transport.captureBackupWriteIntent(f.appId), f.transport.currentBackupAccountSession()); + const receipt = f.automatic.commitBackupLocalOperation(op, (realm) => { + realm.delete(realm.objectForPrimaryKey('Tags', 'old-label')); + for (const id of ['first-imported-label', 'second-imported-label', 'third-imported-label']) { + realm.create('Tags', f.defaults(f.RealmSchema.Tags, { id, type: 'TX', ref: id, label: id })); + } + }); + assert.equal(receipt.committed, true); + const before = await f.repair.captureBackupImage(); + const mutations = []; + for (const [index, id] of ['first-imported-label', 'second-imported-label', 'third-imported-label'].entries()) { + mutations.push({ operation: 'modifyLabels', body: await labelBody(f, [id], index === 0 ? ['old-label'] : []) }); + } + server.armFault({ mode: 'delay-reply', operation: 'modifyLabels', delayMs: 3000 }); + const request = outstanding(() => f.automatic.enqueueAutomaticBackupOperation(receipt, mutations)); + await awaitCommittedReplyBarrier(request, f.appId); + persist(f, f.RealmSchema.Tags, f.defaults(f.RealmSchema.Tags, { id: 'interleaved-local-label', type: 'TX', ref: 'interleaved', label: 'A later committed local write' })); + assert.equal(request.isSettled(), false, 'The local write must finish before the real timed acknowledgement arrives'); + const result = await request.result; + assert.notEqual(result.backupConfirmed, true); + assert.equal(attemptedWrites().length, 1, 'The second and third prepared members must send no HTTP request'); + assert.equal(attemptedWrites()[0].committed, true); + assertPending(f); + const after = await f.repair.captureBackupImage(); + for (const [id, label] of Object.entries(before.labels)) assert.deepEqual(plain(after.labels[id]), plain(label)); + assert.equal(after.labels['interleaved-local-label'].label, 'A later committed local write'); + const remote = await f.image.decodeImage(f.encryption.generateEncryptionKey(f.seed), await server.snapshot(f.appId), f.appId); + assert.ok(remote.labels['first-imported-label']); + assert.equal(remote.labels['second-imported-label'], undefined); + assert.equal(remote.labels['third-imported-label'], undefined); + assert.equal(remote.labels['old-label'], undefined); +}); + +for (const operation of Object.keys(operations)) { + test(`${operation}: after-send A to B to A rejects an outstanding real acknowledgement without changing the new account UI`, async (t) => { + const f = await setup(t, operation); + const prepared = await committedRequest(f, operation); + const expectedA = await f.repair.captureBackupImage(); + server.armFault({ mode: 'delay-reply', operation, delayMs: 3000 }); + const request = outstanding(() => settled(prepared)); + await awaitCommittedReplyBarrier(request, f.appId); + const seedB = 'cd'.repeat(32); + const appIdB = f.encryption.hash256(seedB); + await f.selectAccount(appIdB); + assert.equal(f.dbManager.updateObjectById(f.RealmSchema.KeeperApp, appIdB, { + primarySeed: seedB, publicId: appIdB.slice(0, 8), imageEncryptionKey: f.encryption.generateEncryptionKey(seedB), + }), true); + persist(f, f.RealmSchema.Wallet, wallet(f, 'distinct-account-b-wallet', 'A separate account B record')); + f.store.dispatch(f.bhr.setAutomaticCloudBackup({ appId: appIdB, enabled: true })); + const expectedB = await f.repair.captureBackupImage(); + assert.notEqual(f.dbManager.getObjectById(f.RealmSchema.KeeperApp, appIdB).primarySeed, f.seed); + await server.seed(await f.payloadForCurrentImage()); + assert.equal(await f.sagas.run('runBackupInspection', false, appIdB), true, 'Account B gets its own genuine baseline and UI state'); + await f.selectAccount(f.appId); + const newAFlags = accountBackupFlags(f, f.appId); + const unchangedBFlags = accountBackupFlags(f, appIdB); + assert.equal(request.isSettled(), false, 'All distinct-key B setup and the A return must finish while the real acknowledgement is outstanding'); + const actionBoundary = f.actions.length; + const result = await request.result; + assert.notEqual(result.backupConfirmed, true); + assert.equal(server.trace.filter((event) => event.path === '/revisionedBackupMutation' && event.appId === f.appId).length, 1); + assertPending(f); + assert.deepEqual(accountBackupFlags(f, f.appId), newAFlags, 'The old completion cannot change the newly selected A UI'); + assert.deepEqual(accountBackupFlags(f, appIdB), unchangedBFlags, 'The old A completion cannot change B backup state'); + assert.equal(f.store.getState().bhr.automaticCloudBackupByAppId[f.appId], true, 'Old delete acknowledgements cannot disable new-session consent'); + assert.equal(f.actions.slice(actionBoundary).some((action) => action.type === f.bhr.commitBackupInspection.type), false); + assert.equal(await f.image.compareImages(expectedA, await f.repair.captureBackupImage()), 'matched', 'Every committed native A record survives'); + const remoteB = await f.image.decodeImage(f.encryption.generateEncryptionKey(seedB), await server.snapshot(appIdB), appIdB); + assert.equal(await f.image.compareImages(expectedB, remoteB), 'matched', 'B records and its independently encrypted backup remain untouched'); + await f.selectAccount(appIdB); + assert.equal(await f.image.compareImages(expectedB, await f.repair.captureBackupImage()), 'matched', 'Native B records remain untouched after the old A completion'); + }); +} + test('real native mutation immediately before Redux final verification commit prevents a verified claim', async (t) => { const f = await setup(t, 'updateAppImage'); const request = await committedRequest(f, 'updateAppImage'); From efa6f89ebf7140f738f03ce8a7218a2a1e7f9bc2 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Tue, 13 Oct 2026 04:08:53 +0000 Subject: [PATCH 175/254] Enable development loopback after held-ack fanout and session proofs pass --- src/services/backup/automatic.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/services/backup/automatic.ts b/src/services/backup/automatic.ts index 467d9fc642..c5514647b1 100644 --- a/src/services/backup/automatic.ts +++ b/src/services/backup/automatic.ts @@ -70,7 +70,7 @@ let sequence = 0; // The genuine Realm/transactional Relay matrix permits development loopback // validation. Hosted writes remain fenced until the separate fleet rollout, // old-writer drainage and operator evidence have been reviewed and approved. -export const AUTOMATIC_BACKUP_SOURCE_READY = false; +export const AUTOMATIC_BACKUP_SOURCE_READY = true; export const AUTOMATIC_BACKUP_RELAY_ROLLOUT_READY = false; let commitVerification: (appId: string, deleted: boolean, consentEpoch: number) => boolean = () => false; From 8841aab9135355a7408fe4e2e518c475ccc2b12e Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Tue, 13 Oct 2026 04:16:01 +0000 Subject: [PATCH 176/254] docs: record current R4 automatic backup source proof --- .../automatic-write-readiness.md | 16 ++-- .../automatic-write-validation.md | 93 ++++++++++--------- .../fix-recovery-backup-freshness/design.md | 6 +- ...utomatic-backup-source-proof-2026-10-13.md | 53 +++++++++++ .../nfr-review.md | 4 + .../fix-recovery-backup-freshness/proposal.md | 4 +- .../specs/assisted-server-backup/spec.md | 7 +- .../fix-recovery-backup-freshness/tasks.md | 43 ++++----- .../validation.md | 2 +- 9 files changed, 147 insertions(+), 81 deletions(-) create mode 100644 openspec/changes/fix-recovery-backup-freshness/evidence/automatic-backup-source-proof-2026-10-13.md diff --git a/openspec/changes/fix-recovery-backup-freshness/automatic-write-readiness.md b/openspec/changes/fix-recovery-backup-freshness/automatic-write-readiness.md index f697b9a71d..f4d181ccf0 100644 --- a/openspec/changes/fix-recovery-backup-freshness/automatic-write-readiness.md +++ b/openspec/changes/fix-recovery-backup-freshness/automatic-write-readiness.md @@ -2,15 +2,15 @@ R4 requirements reconciled 13 October 2026 from audit starting app head `b160656218dc6065ad12af9db220c974255bfc23`. The original plan reviewed Keeper `0550ac9ffa938a079816ae4ed58bc970324c3e08` and Relay PR #4 `82ee3b10708a56d220170505d1bb7e3bc8429e77`; those are historical inputs, not the final proof source. -The owner requires the automatic-backup fix and disposable proof in R4. Critical native IOS/ANDROID backup checks also precede release; explicitly physical-only follow-up may be recorded separately. This plan authorizes no production enablement, merge or Relay deployment. Source implementation must stay fail-closed until committed-payload/session proof and the required disposable matrix pass. Record current implementation and results in [automatic-write-validation.md](automatic-write-validation.md); no future gate is marked complete here. +The owner requires the automatic-backup fix and disposable proof in R4. Critical native IOS/ANDROID backup checks also precede release; explicitly physical-only follow-up may be recorded separately. The implemented committed-payload/session proof has observed host Realm/transactional HTTP evidence in [automatic-write-validation.md](automatic-write-validation.md) and [October evidence](evidence/automatic-backup-source-proof-2026-10-13.md). `AUTOMATIC_BACKUP_SOURCE_READY=true` permits only development loopback testing while `AUTOMATIC_BACKUP_RELAY_ROLLOUT_READY=false` keeps hosted writes fenced. This record authorizes no production enablement, merge or Relay deployment. ## Why the audit-baseline intent cannot authorize a write -At the audit starting source, `BackupWriteIntent` records the selected account session and a previously verified Relay revision. It does not identify the local recovery state represented by an encrypted payload or deletion. `backupPost` therefore rejects all seven operations before network. This is required at that baseline: some producers encrypt action data before Realm persistence, Realm may change while an action waits, and one action can emit several Relay writes with the same captured base revision. A valid server compare-and-swap protects against another server writer; it does not prove that the client is sending its present local recovery data. Later implementation/proof must update the result matrix; these observations are not a claim about its final source. +At the audit starting source, `BackupWriteIntent` recorded the selected account session and a previously verified Relay revision. It did not identify the local recovery state represented by an encrypted payload or deletion. `backupPost` therefore rejected all seven operations before network. That was required at the baseline: some producers encrypted action data before Realm persistence, Realm could change while an action waited, and one action could emit several Relay writes with the same captured base revision. A valid server compare-and-swap protects against another server writer; it does not prove that the client is sending its present local recovery data. These historical observations are superseded for the final implementation by the source/proof result matrix. The baseline central Realm observer invalidates backup verification **before** each relevant write. It has no post-commit token and can fire for several field writes in one logical action. The baseline transport's `queuedMutationRevisions` is never populated, so it cannot provide a queue completion proof. A pre-send comparison of a prepared ciphertext against the current Realm image is necessary but still insufficient for operations deliberately sent before local persistence. -## Required transaction model +## Implemented transaction model 1. Give each logical local recovery operation an account-scoped operation ID and a commit result. A successful commit captures a monotonic recovery generation after all related Realm writes; failed or partial writes stay pending and never authorize upload. For pre-persistence producers, stage data locally and queue the cloud mutation only after this commit. Do not make server success a prerequisite for saving a new wallet, signer, vault or deletion. 2. Capture the originating `appId`, account session, verified base revision/epoch and operation ID before async work. Never substitute a newly verified base for an older action. On A→B→A, reject old queued work even if A is selected again. An already sent request has an uncertain outcome and requires readback. @@ -22,20 +22,20 @@ The baseline central Realm observer invalidates backup verification **before** e ## Operation-specific proof at the queue head -| Relay operation | Local proof before send | Audit-baseline producer complication | +| Relay operation | Local proof before send | Historical audit-baseline producer complication | | --- | --- | --- | | `updateAppImage` | Decrypt each wallet/USDT/signer ciphertext with the active account seed; map IDs, sanitized signer records and recoverable fields must equal the committed Realm records. `replaceNodes` requires the exact current normalized node set; app metadata must come from the current KeeperApp. | New wallet and signer creation call backup before Realm insertion. Node and version paths use different timing. Action payloads may be stale. | -| `migrateXfps` | Old exact KeyUID must be absent in committed Realm; new exact KeyUID and decrypted sanitized signer must match Realm. Verify all affected vault signer references, and prove the old map entry exists in the validated Relay snapshot. | The worker updates a live signer before building the request. It previously sent bare fingerprints while Relay maps use KeyUIDs; a narrow key fix is included separately, with transport still disabled. | -| `deleteAppImageEntity` | Every wallet/USDT/signer ID must be absent after a successful local commit and present in the expected Relay snapshot. Deleting a signer must not make a retained vault unrecoverable. | BTC wallet and signer deletion currently calls Relay before Realm deletion; USDT deletion also does so directly. Moving this order requires durable pending state and truthful result/UI handling. | +| `migrateXfps` | Old exact KeyUID must be absent in committed Realm; new exact KeyUID and decrypted sanitized signer must match Realm. Verify all affected vault signer references, and prove the old map entry exists in the validated Relay snapshot. | The baseline worker updated a live signer before building the request. It previously sent bare fingerprints while Relay maps use KeyUIDs; the baseline narrow fix still left automatic transport disabled. | +| `deleteAppImageEntity` | Every wallet/USDT/signer ID must be absent after a successful local commit and present in the expected Relay snapshot. Deleting a signer must not make a retained vault unrecoverable. | Baseline BTC wallet and signer deletion called Relay before Realm deletion; USDT deletion did so directly. Reordering required durable pending state and truthful result/UI handling. | | `deleteVaults` | Each ID must be absent after local commit; other participant/shared-vault references must remain owned and recoverable. | Vault deletion also calls Relay before local deletion. A remote success followed by local failure can leave a mismatched backup. | | `updateVaultImage` | Decrypt the vault; its ID, sanitized signers, policy, archive state and `signersData` must match the committed Realm Vault plus persisted VaultSigner registrations. | Creation and metadata edit can call Relay before persistence. Registration changes can fan out to several vault writes; migration creates and archives two vaults. | | `modifyLabels` | Decrypt each added label and verify the account-derived hashed ID and current Realm tag. Every deleted hashed ID must map to a tag absent after local commit. Reconcile add/delete overlap and account ownership. | Imports, upgrades and transaction-label workers can emit multi-label fanouts; a delayed file callback carries older intent. | | `deleteBackup` | Require an explicit opt-out action for the same selected account and session, a fresh v2 revision, and exact whole-account target. Preserve consent until a positive v2 acknowledgement; on lost response use readback without claiming success. | This is a destructive exception to the local-image comparison: the user intentionally removes the remote copy while keeping Realm. It needs its own opt-out state and separate tests. | -## Test matrix before any enablement +## Test matrix and remaining enablement gates Run each of the seven operations against disposable Realm and transactional Relay fixtures, with real encryption/decryption and the production saga/transport entry points. For each operation, verify: matching committed payload/target reaches the v2 route once; stale payload after a local mutation sends nothing; A→B→A sends nothing; a server 409/old route 404/malformed response leaves pending and does not fall back; timeout or lost acknowledgement sends no next item and readback cannot create false verified. Exercise both matching and remote-only/conflicting snapshots. Add focused multi-write fixtures for (a) creating and archiving a vault, (b) one VaultSigner registration affecting several vaults, and (c) label import plus deletion. Make the first Relay write succeed and the next fail; assert local data survives, the remaining prepared writes do not run, and neither a saga nor Redux reports verified. Assert a local mutation injected just before the final verification commit leaves the backup pending. Pair these tests with native iOS/Android create, edit, delete, relaunch and clean Recovery Key restore on the exact app SHA, plus Relay v2 HTTP tests against a disposable replica set. -Keep automatic source transport fail-closed until this matrix passes. Production delivery additionally requires verified transaction topology, compatible running Relay artifacts and protection from old/legacy writers under separately authorized rollout; a source pass is not deployment evidence. A future source change must never turn an endpoint name or revision string alone into a freshness proof. The current result matrix also tracks mandatory native and clean Recovery Key restore checks independently of fixture outcomes. +The host source matrix now supports development loopback testing. Preserve the hosted rollout fence until transaction topology, compatible running Relay artifacts and protection from old/legacy writers are reviewed under separately authorized rollout. A source pass is not deployment evidence. A future source change must never turn an endpoint name or revision string alone into a freshness proof. The current result matrix identifies which source/fixture cases actually ran and keeps native app relaunch, critical IOS/ANDROID and release checks open independently of host results. diff --git a/openspec/changes/fix-recovery-backup-freshness/automatic-write-validation.md b/openspec/changes/fix-recovery-backup-freshness/automatic-write-validation.md index 4e921ddb6c..5e416dabae 100644 --- a/openspec/changes/fix-recovery-backup-freshness/automatic-write-validation.md +++ b/openspec/changes/fix-recovery-backup-freshness/automatic-write-validation.md @@ -1,27 +1,28 @@ # R4 automatic Assisted Backup: result matrix -This is the required proof matrix, reconciled 13 October 2026. **No implementation or test pass is asserted by these placeholders.** The coordinator replaces a result only with observed exact-source evidence. Historical September repair/native logs are preserved in [validation.md](validation.md) and do not complete this matrix. +Updated 13 October 2026 from observed source/disposable evidence. Historical September logs remain unchanged in [validation.md](validation.md). The [sanitized October proof record](evidence/automatic-backup-source-proof-2026-10-13.md) records commands, source identities, counts and coverage boundaries. -Automatic-backup implementation and disposable proof are mandatory R4 work. Critical native IOS and ANDROID backup acceptance also precedes release. Explicitly physical-device-only follow-up may be separate; it cannot waive the required fixtures or critical simulator/emulator checks. +Automatic-backup implementation and disposable proof are mandatory R4 work. Critical IOS/ANDROID backup acceptance also precedes release. Host-native Realm means the real Node Realm SDK, not an iOS/Android app launch. Physical-device-only follow-up cannot waive critical simulator/emulator safety checks. ## Source and result record | Field | Current record | | --- | --- | -| Audit starting app source | b160656218dc6065ad12af9db220c974255bfc23; not final proof source | -| Final tested app commit / working-tree state | NOT RECORDED | -| Final tested Relay commit / working-tree state | NOT RECORDED | -| Fixture command, UTC time, runtime and platform | NOT RUN / awaiting coordinator evidence | -| Disposable Realm dataset and Mongo replica-set topology | NOT RECORDED | -| HTTP entry points and production saga/hook/transport coverage | NOT RECORDED | -| Test counts and individual result artifact | NOT RECORDED | -| Clean encrypted Recovery Key restore result | NOT RUN | -| IOS exact-source native result | NOT RUN | -| ANDROID exact-source native result | NOT RUN | -| Independent developer review / current-head CI | NOT RECORDED | -| Production running artifact / topology / authorization | NOT VERIFIED; no production action authorized by this matrix | - -Use PASS, FAIL, BLOCKED or NOT RUN per scenario, with command/log/provenance. A blocked/missing critical result keeps the gate open. A count from unit tests alone must not be described as real Realm/HTTP/native evidence. Preserve hashes/source/run IDs and sanitized counts; never record real account/wallet identifiers, seed/Recovery Key material, plaintext backups, production configuration or credentials. +| Audit starting app source | b160656218dc6065ad12af9db220c974255bfc23; historical | +| Final tested code commit before documentation | efa6f89ebf7140f738f03ce8a7218a2a1e7f9bc2 | +| Tested Relay fixture commit | b600416e5ba7325927d7d2871cff7776f4e593fe; runner requires clean tracked source | +| Runtime and topology | Node 22.23.3, actual Realm 12.15.0, Linux host, disposable loopback keeperQA Mongo replica set on port 27058; no hosted credentials | +| Combined genuine fixture command/result | `npm run test:automatic-backup-genuine`: PASS 253/253, 0 failures/skips, 162.1566 seconds, terminal recorded 2026-10-13 04:12 UTC; core 206 + producers 46 + restore 1 | +| Recovery unit command/result | `npm run test:recovery`: 430/430 PASS, 0 failures, 0 skips; source 0f37506a58e6bf44061dd6788b20e5fa095a10c6, including c308aa405 wallet guard follow-up | +| Clean encrypted Recovery Key restore | PASS: actual recovery saga, real key derivation/AES, blank encrypted Realm and real HTTP reads; complete image, all signer fields and normalized nodes checked | +| Source/hosted flags | AUTOMATIC_BACKUP_SOURCE_READY=true for DEVELOPMENT loopback only; AUTOMATIC_BACKUP_RELAY_ROLLOUT_READY=false; hosted rollout remains false | +| IOS / ANDROID exact-source result | NOT RUN / NOT RUN; NEEDS_LOCAL | +| Review and current-head CI | Independent agent fixture architecture review completed with restore assertion gap corrected. Final external developer review/current-head GitHub CI remain open; inherited backup Sonar failure and source PR #7042 with no checks remain visible | +| Type/lint boundary | Focused six core module lint/format PASS. Whole-project tsc FAIL with 945 inherited diagnostics versus 957 at baseline bb66cea459fbb00ec33c5c4ebd601d07c0d7c125; zero added normalized diagnostics, not a clean typecheck | +| Current release preflight | FAIL: 2026-10-13 04:11 UTC command found iOS store history older than 24 hours; refresh required before release | +| Production running artifact/topology/authorization | NOT VERIFIED; no deployment, merge or publication authorized | + +Use PASS, FAIL, BLOCKED or NOT RUN with provenance. The source SHA precedes this documentation commit; the final PR handoff must separately name the actual post-push head SHA. No real seed, private key, account identifier, label content, plaintext backup or credentials are included in the evidence. ## Common cases required for every operation @@ -38,36 +39,40 @@ Use PASS, FAIL, BLOCKED or NOT RUN per scenario, with command/log/provenance. A | C9 complete readback | Final decrypted server image includes all recovery records/references and matches freshly captured local state at the same committed generation/session. Corruption, partial response or missing references cannot produce verified. | | C10 final commit race/restart | Inject a local mutation immediately before Redux verification commit: pending persists and verified is rejected. Restart keeps committed local data and pending state; it does not replay prepared payloads. | -## Seven-operation matrix +## Seven-operation host matrix -Every row must cover C1–C10 with real encryption/decryption and production producer/transport entry points against disposable committed Realm and transactional Relay fixtures. Whole-backup deletion is the explicit opt-out exception to payload-equals-retained-local-image, not an automatic wipe operation. +The genuine matrix uses actual production snapshot/commit/automatic/transport code, real AES, production schemas and encrypted native Realm, extracted production Redux middleware/store observers, and real HTTP into the pinned transactional Relay. Common negatives include stale generation, queued A→B→A, remote-only/key conflict, failed commit, copied/missing proof, unsupported/malformed protocol, timeout/lost ACK, incomplete/wrong-key readback and closed/reopened Realm. The additional after-send A→B→A and inter-member native-write schedules passed 8/8 at fixture commit 7b1ad70ba9e2aa1a2778b375f36e776bdd26a5ad, before source commit efa6f89ebf7140f738f03ce8a7218a2a1e7f9bc2 enabled development loopback testing. The combined final runner result is recorded separately. -USDT fixtures use existing/recovered wallet records and their supported metadata/deletion paths. They do not enable the paused new-wallet setup/import or GasFree/transactional product flows. +Most operation mutation callbacks in the core matrix are constructed directly over the production commit API. Separate producer files exercise actual wallet/vault, signer/registration, label, version-upgrade and existing-USDT functions. This does not attest full React Native store boot, MMKV, UI navigation or native app relaunch. USDT new-wallet creation/import remains paused; tested USDT records are existing retained records only. -| Operation | Operation-specific proof / target | C1–C5 result + evidence | C6–C10 result + evidence | -| --- | --- | --- | --- | -| updateAppImage | Wallet/USDT/signer ciphertext and exact IDs match committed sanitized records; replaceNodes equals normalized current nodes; app/version metadata belongs to current KeeperApp. Cover creation, edit, nodes and version producers. | NOT RUN | NOT RUN | -| migrateXfps | Old exact KeyUID absent locally; new exact KeyUID/signer matches committed Realm; expected old remote map exists; retained vault signer references remain valid. | NOT RUN | NOT RUN | -| deleteAppImageEntity | Each BTC/USDT/signer ID absent after successful local commit and present in expected remote image; retained vaults stay recoverable. Cover direct USDT hook and saga paths. | NOT RUN | NOT RUN | -| deleteVaults | Exact committed local absence; shared/participant references remain owned and recoverable; server-only vaults are preserved. | NOT RUN | NOT RUN | -| updateVaultImage | Vault ID, policy, sanitized signer records, archive state and signersData match committed Vault and persisted registrations. Cover creation/edit and migration/registration producers. | NOT RUN | NOT RUN | -| modifyLabels | Added ciphertext matches current tag and account-derived hashed ID; deleted hashed IDs correspond to committed absence; add/delete overlap and delayed import callbacks are reconciled. | NOT RUN | NOT RUN | -| deleteBackup | Explicit same-account/session opt-out; fresh validated remote revision and exact whole-account target; local recovery data retained and consent unchanged until positive acknowledgement. Lost response does not imply deletion success. | NOT RUN | NOT RUN | +| Operation | Observed production proof | Host result / evidence | +| --- | --- | --- | +| updateAppImage | Committed wallet/signer records, node exact-set replacement and app/version metadata; stale action ciphertext rejected | PASS existing core/producer cases; added held-ACK schedules PASS; [October proof](evidence/automatic-backup-source-proof-2026-10-13.md) | +| migrateXfps | Exact old/new KeyUID and committed sanitized signer, live/archive vault references and registration/policy continuity | PASS existing core + signer producer 9; added held-ACK schedules PASS; [October proof](evidence/automatic-backup-source-proof-2026-10-13.md) | +| deleteAppImageEntity | Committed local BTC/USDT/signer absence with validated expected remote target and retained recovery content | PASS existing core + existing-USDT producer 11; added held-ACK schedules PASS; [October proof](evidence/automatic-backup-source-proof-2026-10-13.md) | +| deleteVaults | Exact committed local absence and preserved retained/server-only recovery records | PASS existing core; added held-ACK schedules PASS; [October proof](evidence/automatic-backup-source-proof-2026-10-13.md) | +| updateVaultImage | Committed vault/policy/archive/signersData and shared registrations; real create/archive and correction fanouts | PASS existing core + wallet 8 + signer 9; added held-ACK schedules PASS; [October proof](evidence/automatic-backup-source-proof-2026-10-13.md) | +| modifyLabels | Committed tags, exact account-derived hash/ciphertext, grouped send/change labels and import rollback | PASS existing core + label 9 + upgrade 9; added held-ACK schedules PASS; [October proof](evidence/automatic-backup-source-proof-2026-10-13.md) | +| deleteBackup | Fresh explicit same-session opt-out, positive ACK, complete empty readback, retained local records and protected consent completion | PASS existing core including newer-choice/reentrant completion guards; added held-ACK schedules PASS; [October proof](evidence/automatic-backup-source-proof-2026-10-13.md) | ## Logical fanout and producer proof -| Scenario | Required assertion | Result + evidence | -| --- | --- | --- | -| New vault plus archived predecessor | Local logical commit completes before cloud effects. One transaction or acknowledged revision chain includes both; final full readback/restore retains active and archived continuity. | NOT RUN | -| Registration shared by several vaults | Committed registration propagates to every affected vault and complete readback includes it. No intermediate response marks the whole backup verified. | NOT RUN | -| Label import plus deletion | Local tags commit first; exact hashed IDs/ciphertext and overlap reconciliation are proved. Remote-only content survives. | NOT RUN | -| First fanout member succeeds, next fails | Remaining prepared members never run; no rollback/success fiction; local data survives and pending persists until reconciliation. Exercise 409, malformed acknowledgement and lost response. | NOT RUN | -| Interleaved local mutation between fanout members | Revalidate proof and committed generation before each send; reject stale effects, stop the chain and keep pending. | NOT RUN | -| Queued action receives newer verification | It cannot borrow the new baseline/epoch or operation-generation proof to authorize old ciphertext. | NOT RUN | -| Pre-persistence producers | Wallet/signer/vault creation and BTC/USDT/signer/vault deletion use local-first completion; cloud failure preserves truthful local result and pending backup. | NOT RUN | -| Disabled/new unconsented account | Local create/edit/delete works; zero automatic writes and no inherited consent. | NOT RUN | -| Complete encrypted restore | A clean disposable Recovery Key restore includes intended BTC MAINNET/TESTNET wallets, USDT recovery metadata, keys, active/archived vaults, registrations, labels and nodes with exact recoverable identities. No partial/empty success. | NOT RUN | -| Transactional HTTP/old writers | Real replica-set CAS race, rollback, malformed/unsupported HTTP, old/full-writer fencing and running source parity are tested. This is not production topology/deployment proof. | NOT RUN | +| Scenario | Result / observed boundary | +| --- | --- | +| New vault plus archived predecessor | PASS real wallet producer migration; both local effects precede HTTP, archive sends before new vault, success readback and second-CAS rejection tested | +| Shared registration | PASS real persisted VaultSigner registration fans out to active/archive vaults; complete readback required; second CAS rejection leaves pending | +| Label import plus deletion / grouped send labels | PASS actual label producers, real tags/AES/HTTP; invalid late import rolls back atomically; 409/404/malformed/lost response preserves local data | +| First fanout member succeeds, next fails | PASS second-member 409 schedules for create/archive, shared registration and label fanouts. Signer/upgrade lost-first-reply schedules stop later effects. This does not claim every fault was injected at every position | +| Native local mutation between fanout members | PASS added actual native-write schedule after first real CAS while its ACK is held: the next prepared member sends nothing, local data survives and pending remains | +| New verification cannot authorize old/cloned intent | PASS immutable original-base/captured intent/operation tests, queued stale generation and no-original-baseline producer cases | +| Local-first creation/edit/deletion | PASS observed actual wallet/vault/signer/label/upgrade/existing-USDT producers; failures retain truthful committed local results and pending backup | +| Disabled/unconsented account | PASS no-original-baseline/consent-off host cases and unit coverage; no inherited consent or upload. Fresh explicit whole-backup opt-out while consent already off is separately tested | +| Complete encrypted restore | PASS actual Recovery Key worker into blank Realm; BTC MAINNET/TESTNET, existing DEFAULT/IMPORTED USDT, keys, active/archive vaults, registrations, labels and nodes | +| Final verification race | PASS native write immediately before actual Redux verification dispatch leaves pending; copied/uncommitted proof cannot bypass it | +| Account change after send | PASS added after-send A→B→A for all seven operations, with a distinct B seed/account/publicId and independently verified B backup; old A completion leaves B state, native data and remote backup unchanged | +| Realm close/reopen | PASS stale queued proof rejects the reopened handle; this is not native app/process restart or MMKV evidence | +| Persisted app interruption/relaunch and background behavior | NOT RUN; mandatory IOS/ANDROID Local checks | +| Transactional HTTP contract | PASS actual paired route/service/Mongoose transaction path and disposable source contract checks; hosted artifact parity, topology, limits and old-writer fleet rollout remain unverified | ## Mandatory critical native acceptance @@ -87,10 +92,6 @@ Both platforms must test the final source with disposable accounts/data and a co Native performance/layout and hardware/physical-only results remain separately described in [nfr-review.md](nfr-review.md) and the final Local handoff. Any accepted physical-only follow-up must name the exact unperformed scenario and its authority; it must not relabel required critical native safety checks as optional. Store/signing/deployment/merge approval remains separate from test results. -## Durable evidence link plan +## Durable evidence and handoff -1. Freeze exact app and Relay source plus fixture/runtime provenance. Record working-tree changes instead of implying committed clean source. -2. Save a sanitized per-scenario result artifact under this change's evidence directory using final source/run identity; link it from the relevant rows. Include actual command, UTC execution time, outcome/count and negative-case assertions. Preserve existing historical logs unchanged. -3. Attach the complete disposable Realm/HTTP/restore report and CI run references to the existing PR. Unit-only and native results receive separate labels. -4. After the final Cloud push, add the AGENTS.md Codex local handoff with the actual current SHA and IOS, ANDROID (plus relevant HARDWARE) checks. Link exact-head Local validation for each native row; new code commits invalidate earlier signoff. -5. Only observed evidence closes a task. Keep production/operator and release blockers visible even after all source/fixture/native rows pass. This document does not send messages, edit trackers, push code or authorize release operations. +The October proof record is sanitized durable evidence; September files remain unchanged. After the final Cloud push, the AGENTS.md Codex local handoff must name the actual current PR head and required IOS, ANDROID and relevant HARDWARE checks. Each native row requires exact-head Local results; new code commits invalidate prior signoff. Source/disposable passes do not authorize hosted rollout, merge, signing, store submission or publication. diff --git a/openspec/changes/fix-recovery-backup-freshness/design.md b/openspec/changes/fix-recovery-backup-freshness/design.md index 508218ed6f..12064230b0 100644 --- a/openspec/changes/fix-recovery-backup-freshness/design.md +++ b/openspec/changes/fix-recovery-backup-freshness/design.md @@ -1,6 +1,6 @@ # Design -This is the R4 target contract reconciled on 13 October 2026 from audit starting app head b160656218dc6065ad12af9db220c974255bfc23. Automatic write implementation and proof must be recorded in [the result matrix](automatic-write-validation.md) before any gate is marked complete. The September native/fixture results in [validation](validation.md) remain historical. +This is the R4 contract reconciled on 13 October 2026 from audit starting app head b160656218dc6065ad12af9db220c974255bfc23. The implemented source and observed host Realm/transactional HTTP proof are recorded in [the result matrix](automatic-write-validation.md) and [October evidence](evidence/automatic-backup-source-proof-2026-10-13.md). Source readiness permits development loopback testing; hosted Relay rollout remains disabled. The September native/fixture results in [validation](validation.md) remain historical. ## Entry points, consent and user flow @@ -22,7 +22,7 @@ The per-account queue covers automatic app/vault/label/deletion mutations and ex image.ts compares canonical decrypted recovery data with deterministic ordering. Ignore rebuildable chain/balance/sync/receiving-address caches and live connection flags. Retain keys, derivation, address counters, manual UTXO spendability overrides by outpoint, signer/vault policy, archive state, labels, nodes and user metadata. Include both BTC networks, USDT, signers and all referenced vault/label records. Sanitize secrets with existing helpers; checking does not mutate Realm. -Prove additions/updates by decrypting the proposed record and matching its exact ID and recoverable fields with the committed record. Prove deletion by committed local absence and presence in the validated remote snapshot; retained vaults must remain recoverable. migrateXfps uses exact KeyUID map entries, not bare fingerprints, and validates affected vault signer references. Labels use account-derived hashed IDs and current tag content. Whole-backup deletion is a separate explicit same-session opt-out operation; preserve consent until a positive acknowledgement. +Prove additions/updates by decrypting the proposed record and matching its exact ID and recoverable fields with the committed record. Prove deletion by committed local absence and presence in the validated remote snapshot; retained vaults must remain recoverable. migrateXfps uses exact KeyUID map entries, not bare fingerprints, and validates affected vault signer references. Labels use account-derived hashed IDs and current tag content. Whole-backup deletion is a separate explicit same-session opt-out operation; preserve the user's consent choice until a positive acknowledgement, complete empty readback and final current-session/current-consent-epoch commit. A newer off-to-on choice cannot be overwritten by the older deletion completion. Apply the proposed operation to the validated remote snapshot in memory before send. Reject removal of remote-only recovery records or immutable key/derivation/network/script/policy conflicts without explicit reconciliation. An invalid/partial/decryption-failed response is unavailable, never empty. Repairable registration metadata is distinct from immutable identity and must be preserved and propagated to every affected vault with readback. @@ -40,7 +40,7 @@ A request timeout/lost response may have committed on the server. Do not replay After the last acknowledged effect, read back and decrypt the complete server image, validating all referenced records. Compare it with a newly captured local image at the same committed generation. Recheck the originating account/session, consent, generation and operation at the final Redux verification commit. A concurrent local mutation, incomplete response, missing record or immutable conflict invalidates completion. Only this atomic final transition may set verified or clear durable pending state. -BHR persists per-account verification/repair state separately from transient running state. Nullish defaults protect older persisted state. Account switching cannot clear another account's pending flag or grant its backup consent. Interrupted state returns to pending/unverified until a fresh read-only comparison completes. No Realm schema migration is authorized by this document. +BHR defines persisted per-account verification/repair state separately from transient running state. Nullish defaults protect older persisted state. Account switching cannot clear another account's pending flag or grant its backup consent. Interrupted state returns to pending/unverified until a fresh read-only comparison completes. The host fixture uses real reducers and extracted production store hooks/origin middleware; it does not prove MMKV persistence or native app relaunch. Those checks remain in the required IOS/ANDROID matrix. No Realm schema migration is authorized by this document. ## NFR and restore boundaries diff --git a/openspec/changes/fix-recovery-backup-freshness/evidence/automatic-backup-source-proof-2026-10-13.md b/openspec/changes/fix-recovery-backup-freshness/evidence/automatic-backup-source-proof-2026-10-13.md new file mode 100644 index 0000000000..377bcaa626 --- /dev/null +++ b/openspec/changes/fix-recovery-backup-freshness/evidence/automatic-backup-source-proof-2026-10-13.md @@ -0,0 +1,53 @@ +# R4 automatic backup source/disposable proof — 13 October 2026 + +This is a sanitized result summary. It contains source identities, commands, counts and test boundaries only. It contains no backup content, real account/wallet identifiers, labels, seed material, private keys or credentials. Existing September logs are preserved unchanged. + +## Source identity and enablement boundary + +- Final tested app code commit before this documentation commit: `efa6f89ebf7140f738f03ce8a7218a2a1e7f9bc2`. +- Paired Relay: `b600416e5ba7325927d7d2871cff7776f4e593fe`. The app runner requires this exact clean tracked checkout and the expected fixture path. +- Final flags: `AUTOMATIC_BACKUP_SOURCE_READY=true` permits DEVELOPMENT loopback testing only; `AUTOMATIC_BACKUP_RELAY_ROLLOUT_READY=false` remains unchanged. Development source readiness does not enable hosted writes. +- The final PR Local handoff must separately record the exact post-push head SHA. This evidence file cannot name its own future commit or confer release approval. + +## Genuine host Realm and transactional HTTP matrix + +Command: `npm run test:automatic-backup-genuine`, with Node 22.23.3 on Linux, actual Realm 12.15.0, `KEEPER_RELAY_FIXTURE_MODULE` pointing to the pinned Relay's `tests/support/automaticBackupHttpFixture.cjs`, and `BACKUP_TEST_MONGO_URL=mongodb://127.0.0.1:27058`. The runner refuses a missing/different/dirty tracked Relay, missing required matrix, unavailable native binding, unsupported Node or non-loopback/credentialed Mongo. A missing prerequisite fails; it is not skipped or replaced by a unit adapter. + +Final combined command outcome/time: **PASS 253/253, 0 failures, 0 skips**, exit 0, 162.1566 seconds; terminal log recorded 2026-10-13 04:12 UTC on the exact app/Relay sources above. Earlier combined seven-file run passed 245/245 with zero skips. Eight added direct schedules passed 8/8 with zero skips, 30.469 seconds, observed 2026-10-13 04:08 UTC at fixture commit 7b1ad70ba9e2aa1a2778b375f36e776bdd26a5ad: one native local change after the first actual CAS while its ACK is held, and after-send A→B→A for each of the seven operations. Each B account uses a distinct seed/appId/publicId and real verified B baseline; old A completion preserves B UI/native/remote state. The final seven-file combined run includes all eight schedules at the enabled code SHA. + +| File / scope | Observed result | +| --- | --- | +| automaticWriteRealmRelay.integration.cjs — all seven operations and shared queue/proof gates | 206/206 PASS, including the eight added held-ACK schedules | +| automaticRecoveryRestore.integration.cjs — whole Recovery Key restore | 1/1 PASS; strengthened whole-image/all-signers/normalized-nodes comparison also reran 1/1 PASS | +| automaticLabelProducers.integration.cjs — actual label import/edit/send/manual-coin producers | 9/9 PASS | +| automaticUpgradeProducer.integration.cjs — actual launch upgrade/version/health/label fanout | 9/9 PASS | +| automaticWalletProducer.integration.cjs — actual wallet/vault creation, rename and migration | 8/8 PASS | +| automaticUSDTProducer.integration.cjs — existing DEFAULT/IMPORTED metadata/deletion, cache and paused creation | 11/11 PASS | +| automaticSignerProducer.integration.cjs — actual correction/registration/live/archive fanout | 9/9 PASS | + +The fixture loads actual production Realm wrappers and schemas, snapshot/sanitization/AES, backup automatic coordinator/transport, real Redux reducers, extracted production account-origin middleware and store observers. The paired server runs real HTTP through extracted production routes, production services and Mongoose transactions in a disposable keeperQA replica set. Protocol fault interception is explicit: malformed/old/missing/false ACK, 404, 409, lost or delayed reply and corrupt readback. Negative tests assert zero unauthorized effects or stopped fanout, retained local data, and pending/unverified status. They never manufacture an operation receipt, generation or baseline. + +Core mutation callbacks are often built directly over the production commit API; the five producer suites and selected BHR cases additionally execute actual source functions. Host platform/device-info, MMKV persistence adapter, unused RSA and notification helpers are disclosed host boundaries. No full React Native app boot, UI navigation, physical signing or native app persistence is attested. + +Clean restoration uses real BIP39/BIP32/BIP85/AES and actual getAppImageWorker/recoverApp/writeRecoveredObject into a blank encrypted Realm, reading from the actual HTTP Relay. The dataset includes BTC MAINNET and TESTNET, retained DEFAULT and IMPORTED USDT key/address/history metadata, Recovery Key and other signer data, active/archived vaults, registrations, labels and both-network nodes. Whole recovery-image comparison, complete signer fields and normalized nodes are asserted. Initial database opening is a real dbManager call, outside the native passcode/UI flow. No new USDT setup/import, provider transfer or consent opt-in is enabled by this test. + +## Other Cloud checks + +| Check | Actual outcome / boundary | +| --- | --- | +| Recovery suite: `npm run test:recovery` | 430/430 PASS, zero failures/skips, 47.9 seconds. Independently run on app source 0f37506a58e6bf44061dd6788b20e5fa095a10c6 after c308aa405 session-guard follow-up. Producer unit adapters are distinguished from genuine integration files; actual native Realm wrapper regressions are included | +| Realm commit/invalidation/open-session focused suite | 20/20 PASS, including ten actual-native atomic commit/rollback/observer cases | +| Root Jest | Coordinator reports 41 suites / 393 tests PASS | +| Release tooling | Coordinator reports Node 28 PASS, Python 7 PASS and Ruby 21 runs / 117 assertions PASS; these are tooling tests, not signing/store validation | +| Local validation gate tests | Coordinator reports 11/11 PASS; gate test code passing does not supply an actual Local validation comment | +| Six core backup modules lint/format | Coordinator reports PASS | +| Whole-project TypeScript | FAIL: coordinator reports 945 inherited diagnostics versus 957 at baseline bb66cea459fbb00ec33c5c4ebd601d07c0d7c125, zero added normalized diagnostics. No clean whole-project typecheck is claimed | +| Current release preflight | FAIL: observed 2026-10-13 04:11 UTC, iOS store history older than 24 hours; no release-readiness pass is inferred from tooling tests | +| Source CI gaps | Inherited backup Sonar failure and source PR #7042 without checks remain disclosed; current-head combined GitHub CI and external review are separate gates | +| Independent agent fixture review | PASS architecture/evidence review after strengthening missing full-signers/nodes restore assertions; no external final-source developer signoff implied | + +## Open gates + +IOS and ANDROID exact-source build/launch, automatic create/edit/delete/relaunch and complete Recovery Key restore safety matrix: **NOT RUN / NEEDS_LOCAL**. Host SDK close/reopen is not proof of process restart, persisted MMKV pending state, background execution, UI layout, enlarged text, memory or native 1/10/50-wallet profiling. Physical-only follow-up needs a separately recorded owner decision; it cannot waive critical native checks. + +Hosted running Relay source parity, transaction topology, request limits, old/legacy-writer fleet rollout and production authorization remain unverified. Final current-head CI/external developer review, store histories/counters, signed artifact identity, merge/signing/submission/deployment/publication approval remain open. This source/disposable proof does not authorize any such operation and stays independent of Reproducibility, Bitkey/Recoverable Wallet and unrelated R4 deliverables. diff --git a/openspec/changes/fix-recovery-backup-freshness/nfr-review.md b/openspec/changes/fix-recovery-backup-freshness/nfr-review.md index 315b197bf0..9a5a603f21 100644 --- a/openspec/changes/fix-recovery-backup-freshness/nfr-review.md +++ b/openspec/changes/fix-recovery-backup-freshness/nfr-review.md @@ -2,6 +2,10 @@ Reviewed 29 September 2026; evidence boundary clarified 13 October 2026. This file preserves the historical check → prompt → update → verify audit and measurements. Statements below about implemented safeguards, completed native runs or infrastructure describe that historical candidate; they are not exact-head R4 automatic-write acceptance. See [design.md](design.md), [validation.md](validation.md) and [the current automatic-write matrix](automatic-write-validation.md) for current requirements/results. No approved numerical backup performance budgets were found in the product sources or OpenSpec documents searched at the historical review. The thresholds below remain proposed engineering targets, subject to native validation. +## Current source evidence and unrun native measurements + +The October source/fixture results are linked in [the result matrix](automatic-write-validation.md) and [the sanitized proof record](evidence/automatic-backup-source-proof-2026-10-13.md). Host Realm/HTTP failure, timeout and readback tests exercise production cancellation and truthful pending state; they do not measure React Native frame time, memory, accessibility or background persistence. Required IOS/ANDROID safety flows and native 1/10/50-wallet profiles, including enlarged text and large/single-record workloads, remain NOT RUN. Development loopback readiness does not approve hosted limits, running artifacts, store submission or release. + ## Historical original observed gaps - `src/services/rest/RestClient.ts:64`: POST passes headers only to Axios. The backup endpoints supply no explicit request timeout, abort signal, or transfer progress callbacks. No global Axios timeout override was found. A stalled request can leave the loading state pending indefinitely. An existing unrelated call passes `{timeout: 20000}` as the headers argument, so that pattern must not be copied as a timeout implementation. diff --git a/openspec/changes/fix-recovery-backup-freshness/proposal.md b/openspec/changes/fix-recovery-backup-freshness/proposal.md index ca1011522c..9c55ef90b6 100644 --- a/openspec/changes/fix-recovery-backup-freshness/proposal.md +++ b/openspec/changes/fix-recovery-backup-freshness/proposal.md @@ -20,6 +20,8 @@ The automatic-backup fix and disposable proof are mandatory R4 work. Critical na Audit starting source: b160656218dc6065ad12af9db220c974255bfc23, 13 October 2026. At that baseline, backupPost rejects automatic mutations before network because end-to-end committed-payload proof is missing. Automatic transport may be enabled only after implementation and the required disposable matrix pass; an endpoint name or valid revision is insufficient. -This document records requirements, not completion of that implementation or proof. See [design](design.md), [implementation tasks](tasks.md), [automatic write readiness](automatic-write-readiness.md) and [current result matrix](automatic-write-validation.md). [Validation](validation.md) preserves September results as historical evidence; they are not exact-head R4 signoff. +The source now saves logical local operations atomically and proves their committed generation, account/session/consent and payload before revisioned writes and complete readback. The recorded host Realm/transactional HTTP tests support development loopback testing: `AUTOMATIC_BACKUP_SOURCE_READY=true`. `AUTOMATIC_BACKUP_RELAY_ROLLOUT_READY=false` keeps hosted writes fenced. This is source/disposable readiness, with the precise tested cases and remaining gaps recorded in [the current result matrix](automatic-write-validation.md) and [October evidence](evidence/automatic-backup-source-proof-2026-10-13.md). + +See [design](design.md), [implementation tasks](tasks.md) and [automatic write readiness](automatic-write-readiness.md). [Validation](validation.md) preserves September results unchanged as historical evidence; they are not exact-head R4 signoff. Current-source independent review, critical native checks, compatible Relay source/deployment evidence and release approval remain separate gates. No merge, production action or publication is authorized by this proposal. Existing backups missing data still need a device that holds it; this work does not reconstruct arbitrarily missing wallets. diff --git a/openspec/changes/fix-recovery-backup-freshness/specs/assisted-server-backup/spec.md b/openspec/changes/fix-recovery-backup-freshness/specs/assisted-server-backup/spec.md index 02158f99e4..8dc7050c3a 100644 --- a/openspec/changes/fix-recovery-backup-freshness/specs/assisted-server-backup/spec.md +++ b/openspec/changes/fix-recovery-backup-freshness/specs/assisted-server-backup/spec.md @@ -100,13 +100,18 @@ After all acknowledged effects the app SHALL read back, decrypt and validate the - **AND** reconstruct fresh work after restart rather than replaying a prepared old payload ### Requirement: Whole-backup deletion uses explicit same-account consent -deleteBackup SHALL require an explicit opt-out for the same selected account/session and validated remote revision. It SHALL preserve consent until a positive revisioned acknowledgement and treat lost responses as uncertain. +deleteBackup SHALL require an explicit opt-out for the same selected account/session and validated remote revision. It SHALL preserve the user's consent choice until a positive revisioned acknowledgement, complete empty readback and a current-session/current-consent-epoch final commit, and treat lost responses as uncertain. #### Scenario: Remote deletion acknowledgement is lost - **WHEN** explicit opt-out may have deleted the backup but no valid acknowledgement arrived - **THEN** do not claim deletion or consent-change success - **AND** preserve local recovery data and reconcile by readback before another deletion attempt +#### Scenario: A newer consent choice arrives before deletion completes +- **WHEN** the user makes a newer off-to-on choice while an older deletion is awaiting its reply or final completion +- **THEN** reject the older consent completion and preserve the newer selected choice +- **AND** retain pending/unverified state without claiming the older operation completed + ### Requirement: Repair has bounded accessible progress The repair screen SHALL expose truthful checking, preparing, uploading and verifying stages, slow-operation explanation after ten seconds and Back throughout. It SHALL NOT claim background execution or rollback. Stalled transport SHALL abort, repeated repair requests SHALL coalesce, and oversized records SHALL fail without truncation. diff --git a/openspec/changes/fix-recovery-backup-freshness/tasks.md b/openspec/changes/fix-recovery-backup-freshness/tasks.md index ac3757371c..cac20252f7 100644 --- a/openspec/changes/fix-recovery-backup-freshness/tasks.md +++ b/openspec/changes/fix-recovery-backup-freshness/tasks.md @@ -1,34 +1,35 @@ # R4 automatic Assisted Backup tasks -Reconciled 13 October 2026 at audit starting app source b160656218dc6065ad12af9db220c974255bfc23. The earlier checked checklist described a September candidate, including pre-persistence writes; it did not prove today's automatic-write contract. Historical results remain in [validation](validation.md) and unchanged evidence files. Check a current task only after exact-source implementation/proof is linked in [automatic-write-validation](automatic-write-validation.md). +Reconciled 13 October 2026 at audit starting app source b160656218dc6065ad12af9db220c974255bfc23, then updated from observed October source and disposable proof. The earlier checked checklist described a September candidate, including pre-persistence writes; it did not prove today's automatic-write contract. Historical results remain in [validation](validation.md) and unchanged evidence files. Current completion refers to [the result matrix](automatic-write-validation.md) and [sanitized October evidence](evidence/automatic-backup-source-proof-2026-10-13.md), not native or hosted release readiness. ## Local-first implementation -- [ ] Give each logical recovery operation an originating account/session, operation ID and monotonic generation captured after all related local writes commit. -- [ ] Reorder wallet/signer/vault creation, edits and deletions so local persistence precedes cloud effects; cloud failure preserves local results and durable pending state. -- [ ] Prove all seven operation payloads/deletion targets against fresh committed Realm data at queue head with real decrypt/identity validation. -- [ ] Reject stale generation, failed/partial commit, wrong owner/key/consent and A→B→A without network or later substitution of a newer verified base. -- [ ] Cover account-bound node/version writes, exact-KeyUID migration, USDT direct deletion, delayed import callbacks and shared registration propagation. -- [ ] Handle explicit whole-backup opt-out separately; retain consent until positive revisioned acknowledgement and reconcile lost responses. +- [x] Give each logical recovery operation an originating account/session, operation ID and monotonic generation captured after all related local writes commit. +- [x] Reorder wallet/signer/vault creation, edits and deletions so local persistence precedes cloud effects; cloud failure preserves local results and the reducer pending-state contract. Verify actual persisted restart behavior in the native matrix below. +- [x] Prove all seven operation payloads/deletion targets against fresh committed Realm data at queue head with real decrypt/identity validation. +- [x] Reject stale generation, failed/partial commit, wrong owner/key/consent and queued A→B→A without network or later substitution of a newer verified base. +- [x] Cover account-bound node/version writes, exact-KeyUID migration, USDT direct deletion, delayed import callbacks and shared registration propagation in the recorded source/producer cases. +- [x] Handle explicit whole-backup opt-out separately; retain the user's choice until acknowledged empty readback and current consent/session completion, and reconcile lost responses. ## Server sequencing and verification -- [ ] Validate v2 snapshot/capabilities and preserve remote-only/conflicting recovery content before automatic send. -- [ ] Enforce atomic expectedRevision with positive precondition acknowledgement and valid newRevision; prohibit legacy fallback. -- [ ] Use one atomic fanout transaction or a serialized acknowledged revision chain, revalidating local proof for each member. -- [ ] Stop fanout on 409/404/protocol/network/timeout/lost response; do not replay prepared ciphertext or claim intermediate success. -- [ ] Complete and decrypt server readback, then compare fresh local data at the same committed generation and account/session. -- [ ] Make the final Redux verified transition reject a local mutation injected immediately before completion; preserve pending across interruption/restart. +- [x] Validate v2 snapshot/capabilities and preserve remote-only/conflicting recovery content before automatic send. +- [x] Enforce atomic expectedRevision with positive precondition acknowledgement and valid newRevision; prohibit legacy fallback. +- [x] Use a serialized acknowledged revision chain, revalidating local proof for each member. +- [x] Stop on 409/404/protocol/network/timeout/lost response; do not replay prepared ciphertext or claim intermediate success. The matrix states which multi-member fault schedules actually ran. +- [x] Complete and decrypt server readback, then compare fresh local data at the same committed generation and account/session. +- [x] Make the final Redux verified transition reject a local mutation injected immediately before completion. +- [ ] Verify persisted pending state across actual native app interruption/restart; SDK Realm close/reopen and reducer tests do not substitute for this. ## Mandatory R4 disposable proof -- [ ] Exercise each of seven production saga/hook/transport operation entry points with real encryption and disposable Realm + transactional Relay fixtures. -- [ ] Record valid committed write once, stale data/commit rejection, A→B→A, remote-only/conflicting snapshot, 409/old-route 404/malformed acknowledgement, timeout/lost response and complete readback outcomes per operation. -- [ ] Prove vault creation+archive, multi-vault registration and label add/delete fanouts, including successful first member then failure and no later prepared send. -- [ ] Prove disabled/unconsented accounts send zero writes and successful local create/edit/delete survives cloud failure. -- [ ] Prove clean Recovery Key restoration from the newly produced backup, validating all intended records and key/policy/registration identity. -- [ ] Run revisioned Relay HTTP/transaction tests against a disposable replica set, including old-writer fences and competing writers at exact source. -- [ ] Link exact app/backend SHA, commands, fixture provenance, pass/fail/blocked counts and redacted artifacts in the current matrix; prior logs are not replacements. +- [x] Exercise each of seven production transport operations with real encryption and disposable Realm + transactional Relay fixtures, with separate actual saga/hook producer coverage. +- [x] Record valid committed write once, stale data/commit rejection, queued and after-send A→B→A, remote-only/conflicting snapshot, 409/old-route 404/malformed acknowledgement, timeout/lost response and complete readback outcomes per operation. +- [x] Prove vault creation+archive, multi-vault registration and label add/delete fanouts, including successful first member then failure and no later prepared send. Direct native mutation after first actual CAS/held ACK also stops the remaining effects. +- [x] Prove disabled/unconsented accounts send zero writes and successful local create/edit/delete survives cloud failure in the observed producer cases. +- [x] Prove clean Recovery Key restoration from the disposable encrypted backup, validating all intended records and key/policy/registration identity. +- [x] Run revisioned Relay HTTP/transaction source tests against a disposable replica set, including old-writer fences and competing writers. Deployed source/topology and fleet rollout remain open. +- [x] Link exact app/backend SHA, commands, fixture provenance, pass/fail/blocked counts and sanitized evidence in the current matrix; prior logs are not replacements. ## Mandatory critical native backup acceptance @@ -45,4 +46,4 @@ Reconciled 13 October 2026 at audit starting app source b160656218dc6065ad12af9d - [ ] Verify deployed request limits and transaction topology, compatible running Relay artifact and old-writer drain/rollout under existing operator authorization. - [ ] Refresh store histories/counters and source/artifact manifest; obtain intended merge/signing/submission/deployment/release approval separately. -Documentation reconciliation does not complete any unchecked implementation, fixture, native or production task. Reproducibility, Bitkey/Recoverable Wallet and unrelated R4 code stay independent. +Checked items describe observed source/disposable proof only. Every unchecked native, infrastructure, review/CI or release task remains open. Reproducibility, Bitkey/Recoverable Wallet and unrelated R4 code stay independent. diff --git a/openspec/changes/fix-recovery-backup-freshness/validation.md b/openspec/changes/fix-recovery-backup-freshness/validation.md index 4bab6be989..8ac0316eae 100644 --- a/openspec/changes/fix-recovery-backup-freshness/validation.md +++ b/openspec/changes/fix-recovery-backup-freshness/validation.md @@ -4,7 +4,7 @@ Audit starting app source: `b160656218dc6065ad12af9db220c974255bfc23`. The automatic fix and disposable proof are mandatory R4 requirements; critical native iOS/Android backup acceptance also precedes release. Physical-device-only follow-up does not waive the fixture/native safety checks. -Current exact-source result placeholders and evidence links live in [automatic-write-validation.md](automatic-write-validation.md). No new implementation, fixture, native or deployment pass is claimed by this documentation reconciliation. Populate the exact app/backend source, commands, outcome counts and redacted artifacts from coordinator evidence before checking [tasks](tasks.md). +Current exact-source results live in [automatic-write-validation.md](automatic-write-validation.md), with a sanitized [October source-proof record](evidence/automatic-backup-source-proof-2026-10-13.md). Source local-first generation/session/payload proof and disposable host Realm/HTTP outcomes are now recorded; required IOS/ANDROID native backup acceptance and hosted deployment remain open. The source flag permits development loopback testing only; hosted rollout remains fenced. See [tasks](tasks.md) for evidence-backed completion and remaining gates. The baseline automatic transport fails closed before network until end-to-end local commit/generation, originating-session/payload proof, revisioned fanout and final complete readback are implemented and proved. Earlier success of manual repair or a pre-persistence automatic writer does not establish readiness for this contract. A lost write acknowledgement leaves its originating operation pending; a content-only match is not an acknowledged write or fanout completion. From 8b66afd84eab0c84a7cd7a2fa92db449df92436c Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Tue, 13 Oct 2026 04:27:51 +0000 Subject: [PATCH 177/254] Use the paired Relay frozen Yarn lockfile in Local setup --- release/r4-automatic-backup-local-handoff.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/release/r4-automatic-backup-local-handoff.md b/release/r4-automatic-backup-local-handoff.md index 3bd58ee60f..d5b84f14ee 100644 --- a/release/r4-automatic-backup-local-handoff.md +++ b/release/r4-automatic-backup-local-handoff.md @@ -84,7 +84,7 @@ cd "$keeper_app_dir" yarn install --frozen-lockfile --ignore-scripts yarn patch-package (cd node_modules/realm && ../.bin/prebuild-install --runtime napi --target 6) -(cd "$keeper_relay_dir" && npm ci --ignore-scripts) +(cd "$keeper_relay_dir" && yarn install --frozen-lockfile --non-interactive --ignore-scripts) export REALM_DISABLE_ANALYTICS=1 export KEEPER_RELAY_FIXTURE_MODULE="$keeper_relay_dir/tests/support/automaticBackupHttpFixture.cjs" export BACKUP_TEST_MONGO_URL=mongodb://127.0.0.1:27058 From 874e123392b445db94375305f542d64f3e72b001 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Tue, 13 Oct 2026 04:29:16 +0000 Subject: [PATCH 178/254] Install paired Relay with its frozen Yarn lockfile in CI --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index fc1074dd81..f001fecfe3 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -121,7 +121,7 @@ jobs: - name: Install paired Relay dependencies working-directory: .ci-relay - run: npm ci --ignore-scripts + run: yarn install --frozen-lockfile --non-interactive --ignore-scripts - name: Start disposable loopback Mongo replica set shell: bash From 13d6d4821473a7af1ccb6e4671283306aed18543 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Tue, 13 Oct 2026 04:33:03 +0000 Subject: [PATCH 179/254] Validate emails with a bounded ASCII scanner --- src/utils/utilities.ts | 32 +++++++++++++-- tests/recovery/emailCheck.test.cjs | 63 ++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 3 deletions(-) create mode 100644 tests/recovery/emailCheck.test.cjs diff --git a/src/utils/utilities.ts b/src/utils/utilities.ts index de54e775f6..4e33e38617 100644 --- a/src/utils/utilities.ts +++ b/src/utils/utilities.ts @@ -158,9 +158,35 @@ export const crossInteractionHandler = (error): string => { export const getBackupDuration = () => config.ENVIRONMENT === APP_STAGE.PRODUCTION ? 1.555e7 : 1800; -export const emailCheck = (email) => { - const reg = /^\w+([.\-+]?\w+)*@\w+([\.-]?\w+)*(\.\w\w+)+$/; - return reg.test(email); +export const emailCheck = (email: unknown): boolean => { + if (typeof email !== 'string') return false; + const at = email.indexOf('@'); + if (at <= 0 || at !== email.lastIndexOf('@')) return false; + const lastDot = email.lastIndexOf('.'); + if (lastDot <= at + 1 || email.length - lastDot - 1 < 2) return false; + + const isWord = (code: number) => + (code >= 65 && code <= 90) || + (code >= 97 && code <= 122) || + (code >= 48 && code <= 57) || + code === 95; + const validPart = (start: number, end: number, separators: string) => { + let previousWasWord = false; + for (let index = start; index < end; index += 1) { + if (isWord(email.charCodeAt(index))) previousWasWord = true; + else if (previousWasWord && separators.includes(email[index])) previousWasWord = false; + else return false; + } + return previousWasWord; + }; + + // Preserve the existing ASCII word/separator rules without ambiguous regex + // repetition. The final dotted suffix contains at least two word characters. + if (!validPart(0, at, '.-+') || !validPart(at + 1, lastDot, '.-')) return false; + for (let index = lastDot + 1; index < email.length; index += 1) { + if (!isWord(email.charCodeAt(index))) return false; + } + return true; }; export function numberToOrdinal(cardinal) { diff --git a/tests/recovery/emailCheck.test.cjs b/tests/recovery/emailCheck.test.cjs new file mode 100644 index 0000000000..c23bf62ec9 --- /dev/null +++ b/tests/recovery/emailCheck.test.cjs @@ -0,0 +1,63 @@ +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const path = require('node:path'); +const vm = require('node:vm'); +const { spawnSync } = require('node:child_process'); +const { test } = require('node:test'); +const ts = require('typescript'); + +const filename = path.resolve(__dirname, '../../src/utils/utilities.ts'); +const source = ts.createSourceFile(filename, fs.readFileSync(filename, 'utf8'), ts.ScriptTarget.Latest, true); +const statement = source.statements.find(node => ts.isVariableStatement(node) && + node.declarationList.declarations.some(declaration => declaration.name.getText(source) === 'emailCheck')); +assert.ok(statement, 'The production email validator must exist'); +const code = ts.transpileModule(statement.getText(source).replace(/^export /, '') + '\nglobalThis.emailCheck = emailCheck;', { + compilerOptions: { target: ts.ScriptTarget.ES2020 }, +}).outputText; +const scope = {}; +vm.runInNewContext(code, scope); + +test('email validation preserves existing ASCII word, plus tag and domain separator support', () => { + for (const email of [ + 'alice@example.com', 'a@b.cd', '_name_@_domain_.__', + 'first.last+tag_2@my-host_1.co_uk', 'first-last+tag@example.co.uk', + 'a@a.b.cd', 'a@a.b.c.def', 'a@a-b.c-d.ef', 'NAME123@EXAMPLE99.COM', + ]) assert.equal(scope.emailCheck(email), true, email); +}); + +test('email validation rejects malformed separators, short final suffixes and whitespace', () => { + for (const email of [ + '', 'example.com', '@example.com', 'a@', 'a@@example.com', 'a@b.c', 'a@a.co.x', + '.a@b.cd', '-a@b.cd', '+a@b.cd', 'a.@b.cd', 'a+@b.cd', + 'a..b@c.de', 'a+-b@c.de', 'a--b@c.de', + 'a@.b.cd', 'a@-b.cd', 'a@b..cd', 'a@b-.cd', 'a@b.-cd', + 'a@b.c-d', 'a@b.c+d', 'a@b.cd.', 'a@b.cd-', + 'a b@c.de', 'a@b c.de', ' a@b.cd', 'a@b.cd ', 'a@b.cd\n', 'a@b.cd\r\n', + 'é@example.com', 'a@例子.com', + ]) assert.equal(scope.emailCheck(email), false, JSON.stringify(email)); +}); + +test('email validation rejects non-strings without coercing caller objects', () => { + const hostile = { toString() { throw new Error('Input must not be coerced'); } }; + for (const value of [null, undefined, 123, false, [], ['a@b.cd'], new String('a@b.cd'), hostile]) + assert.equal(scope.emailCheck(value), false); +}); + +test('hostile long email inputs complete within a subprocess deadline', () => { + // Keep a regressed exponential validator out of the test runner process. + // These late failures exercised the former nested optional repetitions. + const script = code + ` + const assert = require('node:assert/strict'); + const local = 'a'.repeat(200000); + assert.equal(emailCheck(local + '!'), false); + assert.equal(emailCheck(local + '..b@example.com'), false); + assert.equal(emailCheck('a@' + local + '!'), false); + assert.equal(emailCheck('a@' + 'a.'.repeat(100000) + 'co!'), false); + assert.equal(emailCheck(local + '@example.com'), true); + `; + const result = spawnSync(process.execPath, ['-e', script], { + timeout: 3000, encoding: 'utf8', maxBuffer: 1024 * 1024, + }); + assert.ifError(result.error); + assert.equal(result.status, 0, result.stderr); +}); From a243b53b5682a276eb88851c3d34379c0908fedd Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Tue, 13 Oct 2026 04:36:06 +0000 Subject: [PATCH 180/254] fix: validate derivation account segments without quote stripping --- src/utils/service-utilities/utils.ts | 21 +++-- tests/services/derivationParsing.test.ts | 107 +++++++++++++++++++++++ 2 files changed, 121 insertions(+), 7 deletions(-) create mode 100644 tests/services/derivationParsing.test.ts diff --git a/src/utils/service-utilities/utils.ts b/src/utils/service-utilities/utils.ts index 6eeffe8538..da239eb45a 100644 --- a/src/utils/service-utilities/utils.ts +++ b/src/utils/service-utilities/utils.ts @@ -281,16 +281,13 @@ const parseKeyExpression = (keyExpression) => { if (bracketMatch) { const insideBracket = bracketMatch[1]; masterFingerprint = insideBracket.substring(0, 8).toUpperCase(); - path = `m${insideBracket - .substring(8) - .replace(/(\d+)h/g, "$1'") - .replace(/'/g, "'")}`; + path = `m${insideBracket.substring(8).replace(/(\d+)h/g, "$1'")}`; xpub = bracketMatch[2].replace(/[^\w\s]+$/, '').split(/[^\w]+/)[0]; } else { // Case 2: If the keyExpression is not enclosed in square brackets const parts = keyExpression.split("'"); masterFingerprint = parts[0].substring(0, 8).toUpperCase(); - path = `m${keyExpression.substring(8, keyExpression.lastIndexOf("'") + 1).replace(/'/g, "'")}`; + path = `m${keyExpression.substring(8, keyExpression.lastIndexOf("'") + 1)}`; xpub = keyExpression .substring(keyExpression.lastIndexOf("'") + 1) .replace(/[^\w\s]+$/, '') @@ -779,6 +776,16 @@ export function findChangeFromReceiverAddresses( return receiverAddresses; } -export const accountNoFromDerivationPath = (derivationPath) => { - return derivationPath.split('/')[3].replace("'", ''); +export const accountNoFromDerivationPath = (derivationPath: string): string => { + if (typeof derivationPath !== 'string') throw new Error('Invalid derivation path'); + const segments = derivationPath.split('/'); + if (segments[0] !== 'm' || segments.length < 4) throw new Error('Invalid derivation path'); + let accountNumber = ''; + for (let index = 1; index < segments.length; index += 1) { + const child = /^([0-9]+)(?:'|h)?$/.exec(segments[index]); + if (!child || child[0] !== segments[index] || Number(child[1]) > 0x7fffffff) + throw new Error('Invalid derivation path'); + if (index === 3) [, accountNumber] = child; + } + return accountNumber; }; diff --git a/tests/services/derivationParsing.test.ts b/tests/services/derivationParsing.test.ts new file mode 100644 index 0000000000..cf2932bf12 --- /dev/null +++ b/tests/services/derivationParsing.test.ts @@ -0,0 +1,107 @@ +// Disposable public derivations only. The real metadata factory and KeyUID +// functions verify that parser hardening preserves normal signer identities. +jest.mock('src/store/store', () => ({ store: { getState: jest.fn(() => ({ + settings: { bitcoinNetworkType: 'TESTNET' }, +})), dispatch: jest.fn(), subscribe: jest.fn(), replaceReducer: jest.fn() } })); + +import BIP32Factory from 'bip32'; +import { networks } from 'bitcoinjs-lib'; +import { generateSignerFromMetaData } from 'src/hardware'; +import { NetworkType, SignerStorage, SignerType } from 'src/services/wallets/enums'; +import ecc from 'src/services/wallets/operations/taproot-utils/noble_ecc'; +import { store } from 'src/store/store'; +import { accountNoFromDerivationPath, parseTextforVaultConfig } from 'src/utils/service-utilities/utils'; +import { getAccountFromSigner, getKeyUID } from 'src/utils/utilities'; + +const bip32 = BIP32Factory(ecc); +const root = bip32.fromSeed(Buffer.alloc(32, 43), networks.testnet); +const fingerprint = root.fingerprint.toString('hex').toUpperCase(); +const xpub = root.derivePath("m/84'/1'/13'").neutered().toBase58(); + +describe('validated derivation account segments', () => { + test.each([ + ["m/84'/0'/0'", '0'], + ["m/84'/1'/13'/0/1", '13'], + ["m/48h/1h/13h/2h", '13'], + ["m/86'/0'/2147483647'", '2147483647'], + ["m/84'/0'/0013'", '0013'], + ['m/84/0/12', '12'], + ])('preserves the account text in %s', (path, account) => { + expect(accountNoFromDerivationPath(path)).toBe(account); + }); + + test.each([ + undefined, null, 13, '', 'm', "m/84'", "m/84'/0'", "m/84'/0'/", + "x/84'/0'/1'", "m//0'/1'", "m/84'/0'/1''", "m/84'/0'/1h'", + "m/84'/0'/-1'", "m/84'/0'/+1'", "m/84'/0'/1.5'", "m/84'/0'/1e3'", + "m/84'/0'/ 1'", "m/84'/0'/1H", "m/84'/0'/1'", "m/84'/0'/1'\n", + "m/84'/0'/2147483648'", "m/84'/0'/9007199254740993'", + "m/84'/0'/1'/2''", "m/84'/0'/1'/bad", "m/2147483648'/0'/1'", + "m/84'/0'/1'/", "m/84'/0'/1'junk", + ])('rejects the malformed or unsupported path %p', (path) => { + expect(() => accountNoFromDerivationPath(path as string)).toThrow('Invalid derivation path'); + }); +}); + +describe('vault descriptor origin parsing', () => { + test('apostrophe and h origins import the same public key and account path', () => { + const apostrophe = `wpkh([${fingerprint}/84'/1'/13']${xpub}/<0;1>/*)`; + const h = `wpkh([${fingerprint.toLowerCase()}/84h/1h/13h]${xpub}/<0;1>/*)`; + const expected = { isMultisig: false, scheme: { m: 1, n: 1 }, signersDetails: [{ + masterFingerprint: fingerprint, path: "m/84'/1'/13'", xpub, + }] }; + expect(parseTextforVaultConfig(apostrophe)).toEqual(expected); + expect(parseTextforVaultConfig(h)).toEqual(expected); + }); + + test('bare apostrophe origin expressions retain the account path', () => { + expect(parseTextforVaultConfig(`wpkh(${fingerprint}/84'/1'/13'${xpub})`).signersDetails) + .toEqual([{ masterFingerprint: fingerprint, path: "m/84'/1'/13'", xpub }]); + }); + + test('mixed multisig origins normalize without changing either public key', () => { + const second = bip32.fromSeed(Buffer.alloc(32, 44), networks.testnet); + const secondFingerprint = second.fingerprint.toString('hex').toUpperCase(); + const secondXpub = second.derivePath("m/48'/1'/7'/2'").neutered().toBase58(); + const multiXpub = root.derivePath("m/48'/1'/13'/2'").neutered().toBase58(); + const parsed = parseTextforVaultConfig(`wsh(sortedmulti(2,[${fingerprint}/48h/1'/13h/2']${multiXpub}/<0;1>/*,[${secondFingerprint}/48'/1h/7'/2h]${secondXpub}/<0;1>/*))`); + expect(parsed.scheme).toEqual({ m: 2, n: 2 }); + expect(parsed.signersDetails).toEqual([ + { masterFingerprint: fingerprint, path: "m/48'/1'/13'/2'", xpub: multiXpub }, + { masterFingerprint: secondFingerprint, path: "m/48'/1'/7'/2'", xpub: secondXpub }, + ]); + }); + + test('extra origin apostrophes remain invalid', () => { + expect(parseTextforVaultConfig(`wpkh([${fingerprint}/84'/1'/13'']${xpub})`).signersDetails) + .toEqual([null]); + }); +}); + +describe('real signer identity compatibility', () => { + test.each([ + [NetworkType.MAINNET, networks.bitcoin, '0', 'M'], + [NetworkType.TESTNET, networks.testnet, '1', 'T'], + ])('normal account imports keep existing KeyUIDs on %s', (networkType, network, coin, chain) => { + (store.getState as jest.Mock).mockReturnValue({ settings: { bitcoinNetworkType: networkType } }); + const master = bip32.fromSeed(Buffer.alloc(32, 45), network); + const masterFingerprint = master.fingerprint.toString('hex').toUpperCase(); + for (const account of [0, 13]) { + const canonicalPath = `m/48'/${coin}'/${account}'/2'`; + const publicKey = master.derivePath(canonicalPath).neutered().toBase58(); + for (const path of [canonicalPath, canonicalPath.replace(/'/g, 'h')]) { + const { signer, key } = generateSignerFromMetaData({ + xpub: publicKey, derivationPath: path, masterFingerprint, + signerType: SignerType.COLDCARD, storageType: SignerStorage.COLD, isMultisig: true, + }); + expect(key.derivationPath).toBe(canonicalPath); + expect(accountNoFromDerivationPath(path)).toBe(String(account)); + expect(getAccountFromSigner(signer)).toBe(account); + expect(getAccountFromSigner(key)).toBe(account); + expect(signer.id).toBe(`${masterFingerprint}${account}${chain}`); + expect(getKeyUID(signer)).toBe(signer.id); + expect(getKeyUID(key)).toBe(signer.id); + } + } + }); +}); From a25922fff349dec6f374b5cb2a4b8d9cf53f24dd Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Tue, 13 Oct 2026 04:44:55 +0000 Subject: [PATCH 181/254] docs: refresh exact-source recovery proof after CI fixes --- .../automatic-write-validation.md | 11 ++++++----- .../automatic-backup-source-proof-2026-10-13.md | 14 +++++++++----- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/openspec/changes/fix-recovery-backup-freshness/automatic-write-validation.md b/openspec/changes/fix-recovery-backup-freshness/automatic-write-validation.md index 5e416dabae..c3ac75a1b7 100644 --- a/openspec/changes/fix-recovery-backup-freshness/automatic-write-validation.md +++ b/openspec/changes/fix-recovery-backup-freshness/automatic-write-validation.md @@ -9,20 +9,21 @@ Automatic-backup implementation and disposable proof are mandatory R4 work. Crit | Field | Current record | | --- | --- | | Audit starting app source | b160656218dc6065ad12af9db220c974255bfc23; historical | -| Final tested code commit before documentation | efa6f89ebf7140f738f03ce8a7218a2a1e7f9bc2 | +| Final tested code commit before documentation | a243b53b5682a276eb88851c3d34379c0908fedd; includes the frozen-Yarn CI setup and email/derivation utility fixes | | Tested Relay fixture commit | b600416e5ba7325927d7d2871cff7776f4e593fe; runner requires clean tracked source | | Runtime and topology | Node 22.23.3, actual Realm 12.15.0, Linux host, disposable loopback keeperQA Mongo replica set on port 27058; no hosted credentials | -| Combined genuine fixture command/result | `npm run test:automatic-backup-genuine`: PASS 253/253, 0 failures/skips, 162.1566 seconds, terminal recorded 2026-10-13 04:12 UTC; core 206 + producers 46 + restore 1 | -| Recovery unit command/result | `npm run test:recovery`: 430/430 PASS, 0 failures, 0 skips; source 0f37506a58e6bf44061dd6788b20e5fa095a10c6, including c308aa405 wallet guard follow-up | +| Combined genuine fixture command/result | `npm run test:automatic-backup-genuine`: PASS 253/253, 0 failures/skips, 195.046 seconds, clean frozen-Yarn run terminal recorded 2026-10-13 04:40 UTC; core 206 + producers 46 + restore 1 | +| Recovery unit command/result | `npm run test:recovery`: 434/434 PASS, 0 failures/skips, 67.140 seconds at a243b53b5682a276eb88851c3d34379c0908fedd | | Clean encrypted Recovery Key restore | PASS: actual recovery saga, real key derivation/AES, blank encrypted Realm and real HTTP reads; complete image, all signer fields and normalized nodes checked | | Source/hosted flags | AUTOMATIC_BACKUP_SOURCE_READY=true for DEVELOPMENT loopback only; AUTOMATIC_BACKUP_RELAY_ROLLOUT_READY=false; hosted rollout remains false | | IOS / ANDROID exact-source result | NOT RUN / NOT RUN; NEEDS_LOCAL | -| Review and current-head CI | Independent agent fixture architecture review completed with restore assertion gap corrected. Final external developer review/current-head GitHub CI remain open; inherited backup Sonar failure and source PR #7042 with no checks remain visible | +| Review and current-head CI | Independent agent fixture architecture review completed with restore assertion gap corrected. Final external developer review/current-head combined GitHub CI remain open. Earlier backup Sonar failure stays disclosed. Source PR #7042 trigger corrected by workflow-only 74df55446d8645ebfac05405fce81fa43446acd9; runtime unchanged, CodeQL workflow succeeded, hosted Tests run 38347397038 pending. Separate CodeQL alert-check success is not inferred | +| Current Jest | 42 suites / 432 tests PASS, 72.836 seconds at a243b53b5682a276eb88851c3d34379c0908fedd | | Type/lint boundary | Focused six core module lint/format PASS. Whole-project tsc FAIL with 945 inherited diagnostics versus 957 at baseline bb66cea459fbb00ec33c5c4ebd601d07c0d7c125; zero added normalized diagnostics, not a clean typecheck | | Current release preflight | FAIL: 2026-10-13 04:11 UTC command found iOS store history older than 24 hours; refresh required before release | | Production running artifact/topology/authorization | NOT VERIFIED; no deployment, merge or publication authorized | -Use PASS, FAIL, BLOCKED or NOT RUN with provenance. The source SHA precedes this documentation commit; the final PR handoff must separately name the actual post-push head SHA. No real seed, private key, account identifier, label content, plaintext backup or credentials are included in the evidence. +Use PASS, FAIL, BLOCKED or NOT RUN with provenance. The source SHA precedes this documentation commit; the final PR handoff must separately name the actual post-push head SHA. Earlier October results at efa6 (253 genuine) and 0f375 (430 recovery), and the earlier 41-suite/393-test Jest report, remain as the prior checkpoint in the linked evidence. No real seed, private key, account identifier, label content, plaintext backup or credentials are included in the evidence. ## Common cases required for every operation diff --git a/openspec/changes/fix-recovery-backup-freshness/evidence/automatic-backup-source-proof-2026-10-13.md b/openspec/changes/fix-recovery-backup-freshness/evidence/automatic-backup-source-proof-2026-10-13.md index 377bcaa626..1ea86c9d86 100644 --- a/openspec/changes/fix-recovery-backup-freshness/evidence/automatic-backup-source-proof-2026-10-13.md +++ b/openspec/changes/fix-recovery-backup-freshness/evidence/automatic-backup-source-proof-2026-10-13.md @@ -4,7 +4,7 @@ This is a sanitized result summary. It contains source identities, commands, cou ## Source identity and enablement boundary -- Final tested app code commit before this documentation commit: `efa6f89ebf7140f738f03ce8a7218a2a1e7f9bc2`. +- Final tested app code commit before this documentation commit: `a243b53b5682a276eb88851c3d34379c0908fedd`. This includes frozen-Yarn Relay CI setup, bounded ASCII email validation and strict derivation-account parsing fixes. - Paired Relay: `b600416e5ba7325927d7d2871cff7776f4e593fe`. The app runner requires this exact clean tracked checkout and the expected fixture path. - Final flags: `AUTOMATIC_BACKUP_SOURCE_READY=true` permits DEVELOPMENT loopback testing only; `AUTOMATIC_BACKUP_RELAY_ROLLOUT_READY=false` remains unchanged. Development source readiness does not enable hosted writes. - The final PR Local handoff must separately record the exact post-push head SHA. This evidence file cannot name its own future commit or confer release approval. @@ -13,7 +13,7 @@ This is a sanitized result summary. It contains source identities, commands, cou Command: `npm run test:automatic-backup-genuine`, with Node 22.23.3 on Linux, actual Realm 12.15.0, `KEEPER_RELAY_FIXTURE_MODULE` pointing to the pinned Relay's `tests/support/automaticBackupHttpFixture.cjs`, and `BACKUP_TEST_MONGO_URL=mongodb://127.0.0.1:27058`. The runner refuses a missing/different/dirty tracked Relay, missing required matrix, unavailable native binding, unsupported Node or non-loopback/credentialed Mongo. A missing prerequisite fails; it is not skipped or replaced by a unit adapter. -Final combined command outcome/time: **PASS 253/253, 0 failures, 0 skips**, exit 0, 162.1566 seconds; terminal log recorded 2026-10-13 04:12 UTC on the exact app/Relay sources above. Earlier combined seven-file run passed 245/245 with zero skips. Eight added direct schedules passed 8/8 with zero skips, 30.469 seconds, observed 2026-10-13 04:08 UTC at fixture commit 7b1ad70ba9e2aa1a2778b375f36e776bdd26a5ad: one native local change after the first actual CAS while its ACK is held, and after-send A→B→A for each of the seven operations. Each B account uses a distinct seed/appId/publicId and real verified B baseline; old A completion preserves B UI/native/remote state. The final seven-file combined run includes all eight schedules at the enabled code SHA. +Final combined command outcome/time: **PASS 253/253, 0 failures, 0 skips**, exit 0, 195.046 seconds; clean frozen-Yarn run terminal log recorded 2026-10-13 04:40 UTC on the exact app/Relay sources above. Earlier combined seven-file run passed 245/245 with zero skips. Eight added direct schedules passed 8/8 with zero skips, 30.469 seconds, observed 2026-10-13 04:08 UTC at fixture commit 7b1ad70ba9e2aa1a2778b375f36e776bdd26a5ad: one native local change after the first actual CAS while its ACK is held, and after-send A→B→A for each of the seven operations. Each B account uses a distinct seed/appId/publicId and real verified B baseline; old A completion preserves B UI/native/remote state. The final seven-file combined run includes all eight schedules at the enabled code SHA. | File / scope | Observed result | | --- | --- | @@ -35,17 +35,21 @@ Clean restoration uses real BIP39/BIP32/BIP85/AES and actual getAppImageWorker/r | Check | Actual outcome / boundary | | --- | --- | -| Recovery suite: `npm run test:recovery` | 430/430 PASS, zero failures/skips, 47.9 seconds. Independently run on app source 0f37506a58e6bf44061dd6788b20e5fa095a10c6 after c308aa405 session-guard follow-up. Producer unit adapters are distinguished from genuine integration files; actual native Realm wrapper regressions are included | +| Recovery suite: `npm run test:recovery` | 434/434 PASS, zero failures/skips, 67.140 seconds, exit 0 on app source a243b53b5682a276eb88851c3d34379c0908fedd. Producer unit adapters are distinguished from genuine integration files; actual native Realm wrapper regressions are included | | Realm commit/invalidation/open-session focused suite | 20/20 PASS, including ten actual-native atomic commit/rollback/observer cases | -| Root Jest | Coordinator reports 41 suites / 393 tests PASS | +| Root Jest | 42 suites / 432 tests PASS, exit 0, 72.836 seconds on a243b53b5682a276eb88851c3d34379c0908fedd | | Release tooling | Coordinator reports Node 28 PASS, Python 7 PASS and Ruby 21 runs / 117 assertions PASS; these are tooling tests, not signing/store validation | | Local validation gate tests | Coordinator reports 11/11 PASS; gate test code passing does not supply an actual Local validation comment | | Six core backup modules lint/format | Coordinator reports PASS | | Whole-project TypeScript | FAIL: coordinator reports 945 inherited diagnostics versus 957 at baseline bb66cea459fbb00ec33c5c4ebd601d07c0d7c125, zero added normalized diagnostics. No clean whole-project typecheck is claimed | | Current release preflight | FAIL: observed 2026-10-13 04:11 UTC, iOS store history older than 24 hours; no release-readiness pass is inferred from tooling tests | -| Source CI gaps | Inherited backup Sonar failure and source PR #7042 without checks remain disclosed; current-head combined GitHub CI and external review are separate gates | +| Source CI gaps | Earlier backup Sonar failure remains disclosed. Source PR #7042 pull_request trigger was corrected by workflow-only commit 74df55446d8645ebfac05405fce81fa43446acd9; runtime unchanged. CodeQL workflow succeeded; hosted Tests run 38347397038 remains pending. Workflow success is not a separate CodeQL alert-check pass or current-head combined CI/external signoff | | Independent agent fixture review | PASS architecture/evidence review after strengthening missing full-signers/nodes restore assertions; no external final-source developer signoff implied | +## Preserved earlier October checkpoint + +The earlier source checkpoint remains evidence for its own tested source: genuine 253/253, zero failures/skips, 162.1566 seconds at app efa6f89ebf7140f738f03ce8a7218a2a1e7f9bc2 and the same pinned Relay, terminal recorded 2026-10-13 04:12 UTC. Earlier recovery 430/430, zero failures/skips, 47.9 seconds was run on 0f37506a58e6bf44061dd6788b20e5fa095a10c6 after the c308aa405 session guards. The coordinator's earlier Jest report was 41 suites / 393 tests PASS. These are historical October results, superseded for the current source by the refreshed counts above; no result is retroactively transferred to a new SHA. September evidence files are unchanged. + ## Open gates IOS and ANDROID exact-source build/launch, automatic create/edit/delete/relaunch and complete Recovery Key restore safety matrix: **NOT RUN / NEEDS_LOCAL**. Host SDK close/reopen is not proof of process restart, persisted MMKV pending state, background execution, UI layout, enlarged text, memory or native 1/10/50-wallet profiling. Physical-only follow-up needs a separately recorded owner decision; it cannot waive critical native checks. From 9414a1f958455c1766a41a04faf632364876d056 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Tue, 13 Oct 2026 04:45:38 +0000 Subject: [PATCH 182/254] docs: record observed source PR CodeQL check success --- .../fix-recovery-backup-freshness/automatic-write-validation.md | 2 +- .../evidence/automatic-backup-source-proof-2026-10-13.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/openspec/changes/fix-recovery-backup-freshness/automatic-write-validation.md b/openspec/changes/fix-recovery-backup-freshness/automatic-write-validation.md index c3ac75a1b7..16d04a7d6f 100644 --- a/openspec/changes/fix-recovery-backup-freshness/automatic-write-validation.md +++ b/openspec/changes/fix-recovery-backup-freshness/automatic-write-validation.md @@ -17,7 +17,7 @@ Automatic-backup implementation and disposable proof are mandatory R4 work. Crit | Clean encrypted Recovery Key restore | PASS: actual recovery saga, real key derivation/AES, blank encrypted Realm and real HTTP reads; complete image, all signer fields and normalized nodes checked | | Source/hosted flags | AUTOMATIC_BACKUP_SOURCE_READY=true for DEVELOPMENT loopback only; AUTOMATIC_BACKUP_RELAY_ROLLOUT_READY=false; hosted rollout remains false | | IOS / ANDROID exact-source result | NOT RUN / NOT RUN; NEEDS_LOCAL | -| Review and current-head CI | Independent agent fixture architecture review completed with restore assertion gap corrected. Final external developer review/current-head combined GitHub CI remain open. Earlier backup Sonar failure stays disclosed. Source PR #7042 trigger corrected by workflow-only 74df55446d8645ebfac05405fce81fa43446acd9; runtime unchanged, CodeQL workflow succeeded, hosted Tests run 38347397038 pending. Separate CodeQL alert-check success is not inferred | +| Review and current-head CI | Independent agent fixture architecture review completed with restore assertion gap corrected. Final external developer review/current-head combined GitHub CI remain open. Earlier backup Sonar failure stays disclosed. Source PR #7042 trigger corrected by workflow-only 74df55446d8645ebfac05405fce81fa43446acd9; runtime unchanged. Separate CodeQL check 115132119008 completed SUCCESS (No new alerts in code changed, zero annotations), Analyze 115131867787 SUCCESS; hosted Tests run 38347397038 pending. These source-PR checks do not supply combined-head CI | | Current Jest | 42 suites / 432 tests PASS, 72.836 seconds at a243b53b5682a276eb88851c3d34379c0908fedd | | Type/lint boundary | Focused six core module lint/format PASS. Whole-project tsc FAIL with 945 inherited diagnostics versus 957 at baseline bb66cea459fbb00ec33c5c4ebd601d07c0d7c125; zero added normalized diagnostics, not a clean typecheck | | Current release preflight | FAIL: 2026-10-13 04:11 UTC command found iOS store history older than 24 hours; refresh required before release | diff --git a/openspec/changes/fix-recovery-backup-freshness/evidence/automatic-backup-source-proof-2026-10-13.md b/openspec/changes/fix-recovery-backup-freshness/evidence/automatic-backup-source-proof-2026-10-13.md index 1ea86c9d86..712897cd35 100644 --- a/openspec/changes/fix-recovery-backup-freshness/evidence/automatic-backup-source-proof-2026-10-13.md +++ b/openspec/changes/fix-recovery-backup-freshness/evidence/automatic-backup-source-proof-2026-10-13.md @@ -43,7 +43,7 @@ Clean restoration uses real BIP39/BIP32/BIP85/AES and actual getAppImageWorker/r | Six core backup modules lint/format | Coordinator reports PASS | | Whole-project TypeScript | FAIL: coordinator reports 945 inherited diagnostics versus 957 at baseline bb66cea459fbb00ec33c5c4ebd601d07c0d7c125, zero added normalized diagnostics. No clean whole-project typecheck is claimed | | Current release preflight | FAIL: observed 2026-10-13 04:11 UTC, iOS store history older than 24 hours; no release-readiness pass is inferred from tooling tests | -| Source CI gaps | Earlier backup Sonar failure remains disclosed. Source PR #7042 pull_request trigger was corrected by workflow-only commit 74df55446d8645ebfac05405fce81fa43446acd9; runtime unchanged. CodeQL workflow succeeded; hosted Tests run 38347397038 remains pending. Workflow success is not a separate CodeQL alert-check pass or current-head combined CI/external signoff | +| Source CI gaps | Earlier backup Sonar failure remains disclosed. Source PR #7042 pull_request trigger was corrected by workflow-only commit 74df55446d8645ebfac05405fce81fa43446acd9; runtime unchanged. Separate CodeQL check 115132119008 completed SUCCESS with No new alerts in code changed and zero annotations; Analyze 115131867787 completed SUCCESS. Hosted Tests run 38347397038 remains pending. These source-PR results do not supply current-head combined CI or external signoff | | Independent agent fixture review | PASS architecture/evidence review after strengthening missing full-signers/nodes restore assertions; no external final-source developer signoff implied | ## Preserved earlier October checkpoint From 2071acbdcc572de9988d41f894b7356618bc14be Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Tue, 13 Oct 2026 04:47:04 +0000 Subject: [PATCH 183/254] docs: disclose observed source PR live-network CI failures --- .../fix-recovery-backup-freshness/automatic-write-validation.md | 2 +- .../evidence/automatic-backup-source-proof-2026-10-13.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/openspec/changes/fix-recovery-backup-freshness/automatic-write-validation.md b/openspec/changes/fix-recovery-backup-freshness/automatic-write-validation.md index 16d04a7d6f..14e1d76750 100644 --- a/openspec/changes/fix-recovery-backup-freshness/automatic-write-validation.md +++ b/openspec/changes/fix-recovery-backup-freshness/automatic-write-validation.md @@ -17,7 +17,7 @@ Automatic-backup implementation and disposable proof are mandatory R4 work. Crit | Clean encrypted Recovery Key restore | PASS: actual recovery saga, real key derivation/AES, blank encrypted Realm and real HTTP reads; complete image, all signer fields and normalized nodes checked | | Source/hosted flags | AUTOMATIC_BACKUP_SOURCE_READY=true for DEVELOPMENT loopback only; AUTOMATIC_BACKUP_RELAY_ROLLOUT_READY=false; hosted rollout remains false | | IOS / ANDROID exact-source result | NOT RUN / NOT RUN; NEEDS_LOCAL | -| Review and current-head CI | Independent agent fixture architecture review completed with restore assertion gap corrected. Final external developer review/current-head combined GitHub CI remain open. Earlier backup Sonar failure stays disclosed. Source PR #7042 trigger corrected by workflow-only 74df55446d8645ebfac05405fce81fa43446acd9; runtime unchanged. Separate CodeQL check 115132119008 completed SUCCESS (No new alerts in code changed, zero annotations), Analyze 115131867787 SUCCESS; hosted Tests run 38347397038 pending. These source-PR checks do not supply combined-head CI | +| Review and current-head CI | Independent agent fixture architecture review completed with restore assertion gap corrected. Final external developer review/current-head combined GitHub CI remain open. Earlier backup Sonar failure stays disclosed. Source PR #7042 trigger corrected by workflow-only 74df55446d8645ebfac05405fce81fa43446acd9; runtime unchanged. Separate CodeQL check 115132119008 completed SUCCESS (No new alerts in code changed, zero annotations), Analyze 115131867787 SUCCESS; hosted Tests run 38347397038 at 74df FAILED: 34/35 Jest suites, 333/340 tests, seven unchanged live-network Electrum failures; recovery/version/release checks SKIPPED. A test-only deterministic Electrum fixture correction is in progress with no runtime/dependency change; no future pass or combined-head CI is inferred | | Current Jest | 42 suites / 432 tests PASS, 72.836 seconds at a243b53b5682a276eb88851c3d34379c0908fedd | | Type/lint boundary | Focused six core module lint/format PASS. Whole-project tsc FAIL with 945 inherited diagnostics versus 957 at baseline bb66cea459fbb00ec33c5c4ebd601d07c0d7c125; zero added normalized diagnostics, not a clean typecheck | | Current release preflight | FAIL: 2026-10-13 04:11 UTC command found iOS store history older than 24 hours; refresh required before release | diff --git a/openspec/changes/fix-recovery-backup-freshness/evidence/automatic-backup-source-proof-2026-10-13.md b/openspec/changes/fix-recovery-backup-freshness/evidence/automatic-backup-source-proof-2026-10-13.md index 712897cd35..9409f2168c 100644 --- a/openspec/changes/fix-recovery-backup-freshness/evidence/automatic-backup-source-proof-2026-10-13.md +++ b/openspec/changes/fix-recovery-backup-freshness/evidence/automatic-backup-source-proof-2026-10-13.md @@ -43,7 +43,7 @@ Clean restoration uses real BIP39/BIP32/BIP85/AES and actual getAppImageWorker/r | Six core backup modules lint/format | Coordinator reports PASS | | Whole-project TypeScript | FAIL: coordinator reports 945 inherited diagnostics versus 957 at baseline bb66cea459fbb00ec33c5c4ebd601d07c0d7c125, zero added normalized diagnostics. No clean whole-project typecheck is claimed | | Current release preflight | FAIL: observed 2026-10-13 04:11 UTC, iOS store history older than 24 hours; no release-readiness pass is inferred from tooling tests | -| Source CI gaps | Earlier backup Sonar failure remains disclosed. Source PR #7042 pull_request trigger was corrected by workflow-only commit 74df55446d8645ebfac05405fce81fa43446acd9; runtime unchanged. Separate CodeQL check 115132119008 completed SUCCESS with No new alerts in code changed and zero annotations; Analyze 115131867787 completed SUCCESS. Hosted Tests run 38347397038 remains pending. These source-PR results do not supply current-head combined CI or external signoff | +| Source CI gaps | Earlier backup Sonar failure remains disclosed. Source PR #7042 pull_request trigger was corrected by workflow-only commit 74df55446d8645ebfac05405fce81fa43446acd9; runtime unchanged. Separate CodeQL check 115132119008 completed SUCCESS with No new alerts in code changed and zero annotations; Analyze 115131867787 completed SUCCESS. Hosted Tests run 38347397038 at 74df FAILED: 34/35 Jest suites, 333/340 tests, seven unchanged live-network Electrum failures; recovery/version/release checks SKIPPED. A test-only deterministic Electrum fixture correction is in progress with no runtime/dependency change; no future pass, current-head combined CI or external signoff is inferred | | Independent agent fixture review | PASS architecture/evidence review after strengthening missing full-signers/nodes restore assertions; no external final-source developer signoff implied | ## Preserved earlier October checkpoint From dbc6b566ce00835e72225431a1f59858d6b2f679 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Tue, 13 Oct 2026 04:58:50 +0000 Subject: [PATCH 184/254] Require cold-launch verification rehydration acceptance in Local handoff --- release/r4-automatic-backup-local-handoff.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/release/r4-automatic-backup-local-handoff.md b/release/r4-automatic-backup-local-handoff.md index d5b84f14ee..604ce37b37 100644 --- a/release/r4-automatic-backup-local-handoff.md +++ b/release/r4-automatic-backup-local-handoff.md @@ -224,7 +224,7 @@ Verify actual app/package IDs, version/counters, APK signature/alignment and lau On **both** platforms, record each scenario separately: 1. Fresh disposable onboarding, PIN create/unlock/cooldown, always-visible disabled Proceed/queued presentation and account setup. Cold launch/unlock without pressing a key must retain every keypad digit; disabled cooldown must reject input, retain all digits, expire normally and then accept input. Exercise PIN creation and another shared-keypad consumer without shortening cooldown in production code. -2. Opt in per account, create/edit/delete wallets, signers and vaults locally, archive and register them, edit/import/delete labels, change nodes and version-related recovery state, then relaunch. Check actual local persistence, pending/verified presentation, failure-stage/retry copy and complete native clean Recovery Key restore into a **different disposable Realm**, through the production restore worker. Never restore over the owner's existing app/database. +2. Opt in per account, create/edit/delete wallets, signers and vaults locally, archive and register them, edit/import/delete labels, change nodes and version-related recovery state, then relaunch. Check actual local persistence, pending/verified presentation, failure-stage/retry copy and complete native clean Recovery Key restore into a **different disposable Realm**, through the production restore worker. Never restore over the owner's existing app/database. After a verified backup, terminate and cold-launch the disposable app with the next snapshot delayed or unavailable. Open backup status immediately: saved verification and interrupted phases must be unverified/pending before a fresh complete read establishes current proof. Consent and committed local records must survive; no prepared ciphertext may replay. Repeat after a failed partial fanout and across accounts. Record native MMKV persistence and actual process restart separately from host Redux-persist rehydration tests. 3. Repeat C1–C10 through feasible real UI paths with the paired v2 service: interruptions/large payloads, 409/old route/malformed/lost reply, local mutation during verification, A→B→A, consent change during deletion, missing/orphaned/legacy encrypted references and multi-write partial success. Confirm server-only records and immutable identities survive. A successful Delete Backup retains local data, acknowledges empty readback, clears only the intended consent and never displays a verified backup. 4. On iOS, import identical harmless bracketed/unbracketed TXT files and valid `[Test]_backup.bsms` / `Test_backup.bsms` from the same Files provider. Both BSMS files must reach identical configuration review; stop before duplicate creation. Test filenames with spaces/literal percent escapes and cancellation. Smoke Android's unchanged content-URI path on a native file picker. 5. Fresh-install Testnet tutorial → Receive Test Sats → visible wallet balance **without restarting**, including retry/slow response and displayed-account ownership. Local mode intentionally lacks a faucet/AI provider: a 503 or mocked unit result is not positive end-to-end evidence. Use an independently approved disposable Testnet-provider QA configuration when available; otherwise record the scenario's actual BLOCKED/NOT RUN status and explicit owner follow-up, without waiving critical backup or required platform checks. From a2c49c2658cd137a6e824bdf412040f8edfacb45 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Tue, 13 Oct 2026 05:21:18 +0000 Subject: [PATCH 185/254] fix: expire persisted backup proof before state rehydration --- src/store/reducers/bhr.ts | 45 +++++ src/store/store.ts | 182 +++++++++++------- tests/recovery/backupRehydration.test.cjs | 216 ++++++++++++++++++++++ 3 files changed, 372 insertions(+), 71 deletions(-) create mode 100644 tests/recovery/backupRehydration.test.cjs diff --git a/src/store/reducers/bhr.ts b/src/store/reducers/bhr.ts index 19b079108e..10456c7826 100644 --- a/src/store/reducers/bhr.ts +++ b/src/store/reducers/bhr.ts @@ -120,6 +120,51 @@ export const isAutomaticCloudBackupEnabled = ( appId?: string ): boolean => !!appId && backup.automaticCloudBackupByAppId?.[appId] === true; +// Redux persistence contains historical UI state, never the current session's +// Realm/revision proof. Invalidate saved completion and interrupted operations +// before Redux Persist merges them; consent and recovery records stay intact. +export const invalidateRehydratedBackupState = (saved: Partial) => { + if (!saved || typeof saved !== 'object') return saved; + const phases = { ...saved.backupRepairStateByAppId }; + const completed = { ...saved.backupRepairCompletedByAppId }; + const pending = { ...saved.pendingAllBackupByAppId }; + const running = { ...saved.backupRepairRunningByAppId }; + const expiredPhases = new Set(['verified', 'checking', 'preparing', 'uploading', 'verifying']); + const appIds = new Set([...Object.keys(phases), ...Object.keys(completed), ...Object.keys(running)]); + for (const appId of appIds) { + if (expiredPhases.has(phases[appId]) || completed[appId] === true || running[appId] === true) { + phases[appId] = 'unverified'; + completed[appId] = false; + pending[appId] = true; + } + if (appId in running) running[appId] = false; + } + const transientUi: Partial = {}; + for (const field of [ + 'backupAllLoading', 'backupAllSuccess', 'backupAllFailure', + 'deleteBackupSuccess', 'deleteBackupFailure', + ] as const) { + if (field in saved) transientUi[field] = false; + } + for (const field of [ + 'backupAllLoadingByAppId', 'backupAllSuccessByAppId', 'backupAllFailureByAppId', + 'deleteBackupSuccessByAppId', 'deleteBackupFailureByAppId', + ] as const) { + if (field in saved) + transientUi[field] = Object.fromEntries( + Object.keys(saved[field] || {}).map((appId) => [appId, false]) + ); + } + return { + ...saved, + ...transientUi, + backupRepairStateByAppId: phases, + backupRepairCompletedByAppId: completed, + pendingAllBackupByAppId: pending, + backupRepairRunningByAppId: running, + }; +}; + const bhrSlice = createSlice({ name: 'bhr', initialState, diff --git a/src/store/store.ts b/src/store/store.ts index 3ea3ce0d61..28bb1a0c94 100644 --- a/src/store/store.ts +++ b/src/store/store.ts @@ -32,6 +32,7 @@ import bhrReducer, { setPendingAllBackup, commitBackupInspection, setAutomaticCloudBackup, + invalidateRehydratedBackupState, } from './reducers/bhr'; import rootSaga from './sagas'; import sendAndReceiveReducer from './reducers/send_and_receive'; @@ -142,83 +143,122 @@ const accountOwnedUtxoUiActions = new Set([ 'utxos/setSyncingUTXOError', 'utxos/resetState', ]); -const bindActionOrigin = (api) => (next) => (action) => { - if ( - action?.type === 'bhr/commitBackupInspection' && - action.payload?.phase === 'verified' && - !isBackupVerificationCurrent(action.payload.appId) - ) - action = { ...action, payload: { ...action.payload, phase: 'unverified', pending: true } }; - const originAppId = api.getState()?.storage?.appId; - if ( - ['bhr/setAutomaticCloudBackup', 'DELETE_BACKUP', 'BACKUP_ALL_SIGNERS_AND_VAULTS'].includes( - action?.type - ) - ) { - const choiceAppId = action.payload?.appId || action.appId || action.originAppId || originAppId; - const disablesConsent = - action.type === 'bhr/setAutomaticCloudBackup' && action.payload?.enabled === false; +const bindActionOrigin = (api) => { + let canonicalBhrHydration: 'unresolved' | 'present' | 'absent' | 'failed' = 'unresolved'; + let deferredLegacyBhr: Parameters[0]; + return (next) => (action) => { + if (action?.type === 'persist/REHYDRATE') { + if (action.key === 'bhr') { + const present = + !!action.payload && typeof action.payload === 'object' && !Array.isArray(action.payload); + canonicalBhrHydration = + action.err || (!present && action.payload != null) + ? 'failed' + : present + ? 'present' + : 'absent'; + const restored = + canonicalBhrHydration === 'present' + ? action.payload + : canonicalBhrHydration === 'absent' + ? deferredLegacyBhr + : undefined; + deferredLegacyBhr = undefined; + action = { ...action, payload: invalidateRehydratedBackupState(restored) }; + } else if (action.key === 'root' && action.payload?.bhr) { + // Older root snapshots can still contain bhr despite its current blacklist. + const payload = { ...action.payload }; + delete payload.bhr; + if (!action.err && canonicalBhrHydration === 'unresolved') { + deferredLegacyBhr = action.payload.bhr; + } else if (!action.err && canonicalBhrHydration === 'absent') { + const currentBhr = api.getState()?.bhr; + payload.bhr = { + ...currentBhr, + ...invalidateRehydratedBackupState(action.payload.bhr), + _persist: currentBhr?._persist, + }; + } + action = { ...action, payload }; + } + } if ( - !choiceAppId || - choiceAppId !== originAppId || - (!disablesConsent && !canAccessBackupAccount(choiceAppId, true)) || - !observeBackupConsentAction(action, choiceAppId) + action?.type === 'bhr/commitBackupInspection' && + action.payload?.phase === 'verified' && + !isBackupVerificationCurrent(action.payload.appId) ) - return action; - } - const enablesBackup = - action?.type === 'bhr/setAutomaticCloudBackup' && action.payload?.enabled === true; - const reportsBackupSuccess = - action?.type === 'bhr/setBackupAllSuccess' && action.payload?.status === true; - if (enablesBackup || reportsBackupSuccess) { - const { appId } = action.payload; - if (!appId || originAppId !== appId) return action; - try { - if ((dbManager.getObjectByIndex(RealmSchema.KeeperApp) as unknown as KeeperApp)?.id !== appId) - return action; - } catch { - return action; - } - } - if (action?.originAppId && accountOwnedUtxoUiActions.has(action.type)) { - try { + action = { ...action, payload: { ...action.payload, phase: 'unverified', pending: true } }; + const originAppId = api.getState()?.storage?.appId; + if ( + ['bhr/setAutomaticCloudBackup', 'DELETE_BACKUP', 'BACKUP_ALL_SIGNERS_AND_VAULTS'].includes( + action?.type + ) + ) { + const choiceAppId = action.payload?.appId || action.appId || action.originAppId || originAppId; + const disablesConsent = + action.type === 'bhr/setAutomaticCloudBackup' && action.payload?.enabled === false; if ( - action.originAppId !== originAppId || - (dbManager.getObjectByIndex(RealmSchema.KeeperApp) as unknown as KeeperApp)?.id !== - originAppId + !choiceAppId || + choiceAppId !== originAppId || + (!disablesConsent && !canAccessBackupAccount(choiceAppId, true)) || + !observeBackupConsentAction(action, choiceAppId) ) return action; - } catch { - return action; } - } - if (originAppId && action && accountOwnedActions.has(action.type) && !('originAppId' in action)) - action = { ...action, originAppId }; - if ( - action && - (accountOwnedActions.has(action.type) || - action.type === 'DELETE_BACKUP' || - action.type === 'BACKUP_ALL_SIGNERS_AND_VAULTS') && - !('originBackupAccountSession' in action) - ) - action = { ...action, originBackupAccountSession: currentBackupAccountSession() }; - // Stamp the verified remote base at dispatch, before any saga can wait or - // mutate Realm. A caller that captured an earlier intent must never be - // silently rebased to the account's current remote state here. - if ( - action && - (accountOwnedActions.has(action.type) || - action.type === 'DELETE_BACKUP' || - action.type === 'BACKUP_ALL_SIGNERS_AND_VAULTS') && - !('originBackupWriteIntent' in action) - ) { - const appId = action.originAppId || action.appId || originAppId; - action = { - ...action, - originBackupWriteIntent: appId ? captureBackupWriteIntent(appId) : undefined, - }; - } - return next(action); + const enablesBackup = + action?.type === 'bhr/setAutomaticCloudBackup' && action.payload?.enabled === true; + const reportsBackupSuccess = + action?.type === 'bhr/setBackupAllSuccess' && action.payload?.status === true; + if (enablesBackup || reportsBackupSuccess) { + const { appId } = action.payload; + if (!appId || originAppId !== appId) return action; + try { + if ((dbManager.getObjectByIndex(RealmSchema.KeeperApp) as unknown as KeeperApp)?.id !== appId) + return action; + } catch { + return action; + } + } + if (action?.originAppId && accountOwnedUtxoUiActions.has(action.type)) { + try { + if ( + action.originAppId !== originAppId || + (dbManager.getObjectByIndex(RealmSchema.KeeperApp) as unknown as KeeperApp)?.id !== + originAppId + ) + return action; + } catch { + return action; + } + } + if (originAppId && action && accountOwnedActions.has(action.type) && !('originAppId' in action)) + action = { ...action, originAppId }; + if ( + action && + (accountOwnedActions.has(action.type) || + action.type === 'DELETE_BACKUP' || + action.type === 'BACKUP_ALL_SIGNERS_AND_VAULTS') && + !('originBackupAccountSession' in action) + ) + action = { ...action, originBackupAccountSession: currentBackupAccountSession() }; + // Stamp the verified remote base at dispatch, before any saga can wait or + // mutate Realm. A caller that captured an earlier intent must never be + // silently rebased to the account's current remote state here. + if ( + action && + (accountOwnedActions.has(action.type) || + action.type === 'DELETE_BACKUP' || + action.type === 'BACKUP_ALL_SIGNERS_AND_VAULTS') && + !('originBackupWriteIntent' in action) + ) { + const appId = action.originAppId || action.appId || originAppId; + action = { + ...action, + originBackupWriteIntent: appId ? captureBackupWriteIntent(appId) : undefined, + }; + } + return next(action); + }; }; const middlewars = [bindActionOrigin, sagaMiddleware]; diff --git a/tests/recovery/backupRehydration.test.cjs b/tests/recovery/backupRehydration.test.cjs new file mode 100644 index 0000000000..e2c26f7e17 --- /dev/null +++ b/tests/recovery/backupRehydration.test.cjs @@ -0,0 +1,216 @@ +// Real Redux Persist serialization/reconciliation, production BHR reducer and +// origin middleware. MMKV I/O uses a disposable Promise storage adapter here. +// The fresh-proof predicate is an explicitly controlled UI-unit boundary; +// automaticWriteRealmRelay.integration proves actual branded Realm/HTTP proof. +// Native MMKV and an iOS/Android process relaunch are NOT tested by this file. +const { test } = require('node:test'); +const assert = require('node:assert/strict'); +const vm = require('node:vm'); +const ts = require('typescript'); +const { combineReducers, createStore, applyMiddleware } = require('redux'); +const { persistReducer, persistStore, createMigrate } = require('redux-persist'); +const { loadModule, functions, enums } = require('./sagaHarness.cjs'); +const copy = (value) => JSON.parse(JSON.stringify(value)); +const tick = () => new Promise(setImmediate); +const serialize = (value) => JSON.stringify(Object.fromEntries(Object.entries(value).map( + ([key, field]) => [key, JSON.stringify(field)] +))); + +async function setup(t, { nested, legacy, guarded = true } = {}) { + const values = new Map([ + ['persist:bhr', nested === undefined ? null : serialize({ ...nested, _persist: { version: -1, rehydrated: true } })], + ['persist:root', serialize({ storage: { appId: 'A', pinHash: 'disposable-pin-marker' }, + account: { allAccounts: [{ appId: 'A' }, { appId: 'B' }], recoveryKeyStatusByAppId: { A: 'confirmed' } }, + ...(legacy ? { bhr: legacy } : {}), _persist: { version: 4, rehydrated: true } })], + ]); + const reads = new Map(); + const hostStorage = { + getItem: (key) => new Promise((resolve, reject) => reads.set(key, { resolve, reject })), + setItem: async (key, value) => values.set(key, value), + removeItem: async (key) => values.delete(key), + }; + const bhr = loadModule('src/store/reducers/bhr.ts', { + 'src/models/enums/BHR': loadModule('src/models/enums/BHR.ts'), + 'src/storage': { reduxStorage: hostStorage }, + }); + const storage = loadModule('src/store/reducers/storage.ts', { 'src/services/wallets/enums': enums }); + const proofPredicate = new Set(); + const actions = [], snapshots = []; + let store; + const scope = { + ...bhr, persistReducer, createMigrate, reduxStorage: hostStorage, storage: hostStorage, + reduxPersistMigrations: loadModule('src/store/migrations.ts').default, + RESET_REDUX_STORE: 'RESET_REDUX_STORE', + appReducer: combineReducers({ bhr: bhr.default, storage: storage.default, + // Unrelated account payload is opaque: no account migration is run at v4. + account: (state = {}) => state }), + RealmSchema: { KeeperApp: 'KeeperApp' }, + dbManager: { getObjectByIndex: () => ({ id: store.getState().storage.appId }) }, + currentBackupAccountSession: () => 0, + captureBackupWriteIntent: () => undefined, + canAccessBackupAccount: () => false, + observeBackupConsentAction: (_action, appId) => store.getState().storage.appId === appId, + isBackupVerificationCurrent: (appId) => proofPredicate.has(appId) && + store.getState().storage.appId === appId, + }; + vm.runInNewContext(ts.transpileModule(functions('src/store/store.ts', [ + 'rootReducer', 'persistConfig', 'persistedReducer', + 'accountOwnedActions', 'accountOwnedUtxoUiActions', 'bindActionOrigin', + ]), { compilerOptions: { target: ts.ScriptTarget.ES2020 } }).outputText, scope); + store = createStore(scope.persistedReducer, applyMiddleware( + ...(guarded ? [scope.bindActionOrigin] : []), + () => (next) => (action) => { actions.push(action); return next(action); } + )); + store.subscribe(() => snapshots.push(copy(store.getState()))); + let booted; + const boot = new Promise((resolve) => { booted = resolve; }); + const persistor = persistStore(store, { manualPersist: true }, booted); + t.after(() => persistor.pause()); + persistor.persist(); + for (let i = 0; i < 20 && reads.size < 2; i++) await tick(); + assert.equal(reads.has('persist:root'), true); + assert.equal(reads.has('persist:bhr'), true); + const release = async (key, error) => { + const previous = actions.filter((action) => action.type === 'persist/REHYDRATE').length; + const read = reads.get(`persist:${key}`); + if (error) read.reject(error); else read.resolve(values.get(`persist:${key}`)); + for (let i = 0; i < 50 && actions.filter((action) => action.type === 'persist/REHYDRATE').length === previous; i++) + await tick(); + assert.equal(actions.some((action) => action.type === 'persist/REHYDRATE' && action.key === key), true); + }; + return { store, bhr, storage, snapshots, actions, proofPredicate, release, boot, persistor, + stored: (key) => { + const record = JSON.parse(values.get(`persist:${key}`)); + return Object.fromEntries(Object.entries(record).map(([field, value]) => [field, JSON.parse(value)])); + }, + async finish(order = ['bhr', 'root']) { for (const key of order) await release(key); await boot; }, + }; +} + +const savedVerified = { automaticCloudBackupByAppId: { A: true, B: false }, + backupRepairStateByAppId: { A: 'verified' }, backupRepairCompletedByAppId: { A: true }, + pendingAllBackupByAppId: { A: false, B: true }, backupMethod: 'SEED' }; + +function assertNeverVerified(f) { + assert.ok(f.snapshots.every((snapshot) => snapshot.bhr.backupRepairStateByAppId.A !== 'verified')); + assert.ok(f.snapshots.every((snapshot) => snapshot.bhr.backupRepairCompletedByAppId.A !== true)); +} + +test('control: default persistence resurrects saved Verified despite an empty proof predicate', async (t) => { + const f = await setup(t, { nested: savedVerified, guarded: false }); + await f.finish(); + assert.equal(f.proofPredicate.size, 0); + assert.equal(f.store.getState().bhr.backupRepairStateByAppId.A, 'verified'); + assert.equal(f.store.getState().bhr.backupRepairCompletedByAppId.A, true); +}); + +test('nested rehydration invalidates saved proof and interrupted UI before subscribers see it', async (t) => { + const nested = { ...savedVerified, + backupRepairStateByAppId: { A: 'verified', B: 'checking', C: 'preparing', D: 'uploading', E: 'verifying', F: 'different' }, + backupRepairCompletedByAppId: { A: true, G: true, F: false }, + backupRepairRunningByAppId: { H: true, A: false }, + pendingAllBackupByAppId: { A: false, F: true, J: true }, + backupAllSuccess: true, backupAllLoading: true, deleteBackupSuccess: true, + backupAllSuccessByAppId: { A: true }, backupAllLoadingByAppId: { B: true }, deleteBackupSuccessByAppId: { A: true }, + signingDevices: [{ xpub: 'disposable-public-key' }], + }; + const before = copy(nested); + const f = await setup(t, { nested }); + await f.finish(); + assertNeverVerified(f); + const state = f.store.getState(); + for (const id of ['A', 'B', 'C', 'D', 'E', 'G', 'H']) { + assert.equal(state.bhr.backupRepairStateByAppId[id], 'unverified'); + assert.equal(state.bhr.backupRepairCompletedByAppId[id], false); + assert.equal(state.bhr.pendingAllBackupByAppId[id], true); + } + assert.equal(state.bhr.backupRepairRunningByAppId.H, false); + assert.equal(state.bhr.backupRepairStateByAppId.F, 'different'); + assert.equal(state.bhr.pendingAllBackupByAppId.F, true); + assert.equal(state.bhr.pendingAllBackupByAppId.J, true); + assert.deepEqual(copy(state.bhr.automaticCloudBackupByAppId), before.automaticCloudBackupByAppId); + assert.deepEqual(copy(state.bhr.signingDevices), before.signingDevices); + assert.equal(state.bhr.backupMethod, before.backupMethod); + assert.equal(state.storage.pinHash, 'disposable-pin-marker'); + assert.equal(state.account.recoveryKeyStatusByAppId.A, 'confirmed'); + assert.equal(state.bhr.backupAllSuccess, false); + assert.equal(state.bhr.backupAllLoading, false); + assert.equal(state.bhr.deleteBackupSuccess, false); + assert.equal(state.bhr.backupAllSuccessByAppId.A, false); + assert.equal(state.bhr.backupAllLoadingByAppId.B, false); + assert.equal(state.bhr.deleteBackupSuccessByAppId.A, false); + assert.deepEqual(nested, before, 'Sanitization never mutates the supplied snapshot'); + assert.ok(f.actions.every((action) => action.type.startsWith('persist/'))); +}); + +for (const order of [['root', 'bhr'], ['bhr', 'root']]) { + test(`canonical opt-out beats legacy opt-in in every subscriber state: ${order.join(' then ')}`, async (t) => { + const f = await setup(t, { nested: { ...savedVerified, automaticCloudBackupByAppId: { A: false, B: true } }, + legacy: savedVerified }); + await f.release(order[0]); + assert.equal(f.store.getState().bhr.automaticCloudBackupByAppId.A, order[0] === 'root' ? undefined : false); + await f.release(order[1]); await f.boot; + assert.equal(f.store.getState().bhr.automaticCloudBackupByAppId.A, false); + assert.equal(f.store.getState().bhr.automaticCloudBackupByAppId.B, true); + assert.ok(f.snapshots.every((snapshot) => snapshot.bhr.automaticCloudBackupByAppId.A !== true)); + assertNeverVerified(f); + }); +} + +for (const order of [['root', 'bhr'], ['bhr', 'root']]) { + test(`legacy fallback waits for confirmed canonical absence: ${order.join(' then ')}`, async (t) => { + const f = await setup(t, { legacy: savedVerified }); + await f.release(order[0]); + if (order[0] === 'root') assert.equal(f.store.getState().bhr.automaticCloudBackupByAppId.A, undefined); + await f.release(order[1]); await f.boot; + assert.equal(f.store.getState().bhr.automaticCloudBackupByAppId.A, true); + assert.equal(f.store.getState().bhr.backupRepairStateByAppId.A, 'unverified'); + assert.equal(f.store.getState().bhr.pendingAllBackupByAppId.A, true); + assertNeverVerified(f); + }); +} + +test('unreadable canonical storage suppresses deferred legacy consent rather than treating failure as absence', async (t) => { + const f = await setup(t, { legacy: savedVerified }); + await f.release('root'); + await f.release('bhr', new Error('Injected host storage read failure')); await f.boot; + assert.equal(f.store.getState().bhr.automaticCloudBackupByAppId.A, undefined); + assertNeverVerified(f); + assert.ok(f.snapshots.every((snapshot) => snapshot.bhr.automaticCloudBackupByAppId.A !== true)); +}); + +test('absence-first legacy fallback keeps initialized fields and continues nested persistence after flush', async (t) => { + const f = await setup(t, { legacy: savedVerified }); + await f.finish(['bhr', 'root']); + assert.equal(f.store.getState().bhr._persist.rehydrated, true); + assert.equal(f.store.getState().bhr.relayWalletUpdateLoading, false); + f.store.dispatch(f.bhr.setAutomaticCloudBackup({ appId: 'A', enabled: false })); + f.store.dispatch(f.bhr.setPendingAllBackup({ appId: 'B', pending: true })); + f.store.dispatch(f.bhr.setSeedConfirmed(true)); + await f.persistor.flush(); + const stored = f.stored('bhr'); + assert.equal(stored.pendingAllBackupByAppId.B, true); + assert.equal(stored.automaticCloudBackupByAppId.A, false); + assert.equal(stored.backupRepairStateByAppId.A, 'unverified'); + assert.equal(stored._persist.rehydrated, true); + assert.equal(stored.seedConfirmed, undefined, 'The actual nested write blacklist remains effective'); +}); + +test('proof-gated fresh completion remains possible and cannot clear another account pending state', async (t) => { + const f = await setup(t, { nested: savedVerified }); + await f.finish(); + f.store.dispatch(f.bhr.commitBackupInspection({ appId: 'A', phase: 'verified', pending: false })); + assert.equal(f.store.getState().bhr.backupRepairStateByAppId.A, 'unverified'); + assert.equal(f.store.getState().bhr.pendingAllBackupByAppId.A, true); + // Explicit UI-unit predicate mock only; the genuine native suite obtains + // this decision from the unchanged production Realm/HTTP proof verifier. + f.proofPredicate.add('A'); + f.store.dispatch(f.bhr.commitBackupInspection({ appId: 'A', phase: 'verified', pending: false })); + assert.equal(f.store.getState().bhr.backupRepairStateByAppId.A, 'verified'); + assert.equal(f.store.getState().bhr.pendingAllBackupByAppId.A, false); + assert.equal(f.store.getState().bhr.pendingAllBackupByAppId.B, true); + f.store.dispatch(f.storage.setAppId('B')); + f.store.dispatch(f.bhr.commitBackupInspection({ appId: 'A', phase: 'verified', pending: false })); + assert.equal(f.store.getState().bhr.backupRepairStateByAppId.A, 'unverified'); + assert.equal(f.store.getState().bhr.pendingAllBackupByAppId.B, true); +}); From 49349a5a82e68d92adef5328ddcaf034a611d20c Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Tue, 13 Oct 2026 05:22:18 +0000 Subject: [PATCH 186/254] Require current session proof for displayed backup verification --- src/screens/AppSettings/AssistedBackupStatus.tsx | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/screens/AppSettings/AssistedBackupStatus.tsx b/src/screens/AppSettings/AssistedBackupStatus.tsx index 5c8d7811ac..f91cb70c04 100644 --- a/src/screens/AppSettings/AssistedBackupStatus.tsx +++ b/src/screens/AppSettings/AssistedBackupStatus.tsx @@ -7,6 +7,7 @@ import { RealmSchema } from 'src/storage/realm/enum'; import { useAppDispatch, useAppSelector } from 'src/store/hooks'; import { checkBackupFreshness, repairBackup } from 'src/store/sagaActions/bhr'; import { isAutomaticCloudBackupEnabled } from 'src/store/reducers/bhr'; +import { isBackupVerificationCurrent } from 'src/services/backup/automatic'; import { LocalizationContext } from 'src/context/Localization/LocContext'; import ScreenWrapper from 'src/components/ScreenWrapper'; import WalletHeader from 'src/components/WalletHeader'; @@ -27,7 +28,9 @@ export default function AssistedBackupStatus() { backupRepairRunningByAppId = {}, } = useAppSelector((state) => state.bhr); const automaticCloudBackup = isAutomaticCloudBackupEnabled({ automaticCloudBackupByAppId }, id); - const phase = backupRepairStateByAppId[id]; + const recordedPhase = backupRepairStateByAppId[id]; + const phase = + recordedPhase === 'verified' && !isBackupVerificationCurrent(id) ? 'unverified' : recordedPhase; const running = !!backupRepairRunningByAppId[id]; const [slow, setSlow] = useState(false); useEffect(() => { From 3cc9368ca2f3b2149db5743a5f9d230aa516d6a0 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Tue, 13 Oct 2026 05:22:54 +0000 Subject: [PATCH 187/254] test: verify native backup rehydration and session status --- .../automaticWriteRealmRelay.fixture.cjs | 99 +++++- .../automaticWriteRealmRelay.integration.cjs | 294 +++++++++++++++++- 2 files changed, 384 insertions(+), 9 deletions(-) diff --git a/tests/recovery/automaticWriteRealmRelay.fixture.cjs b/tests/recovery/automaticWriteRealmRelay.fixture.cjs index 0f849a65b7..e78ab871e1 100644 --- a/tests/recovery/automaticWriteRealmRelay.fixture.cjs +++ b/tests/recovery/automaticWriteRealmRelay.fixture.cjs @@ -9,6 +9,8 @@ const assert = require('node:assert/strict'); const crypto = require('node:crypto'); const ts = require('typescript'); const { configureStore } = require('@reduxjs/toolkit'); +const { combineReducers } = require('redux'); +const { persistReducer, persistStore, createMigrate } = require('redux-persist'); const { runSaga } = require('redux-saga'); const effects = require('redux-saga/effects'); const Realm = require('realm'); @@ -25,6 +27,16 @@ function declarations(file, names) { return matches.map((node) => node.getText(source).replace(/^export /, '').replace(/^const /, 'var ')).join('\n'); } +function componentDeclarations(file, component, names) { + const source = ts.createSourceFile(file, fs.readFileSync(path.join(root, file), 'utf8'), ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX); + const declaration = source.statements.find((node) => ts.isFunctionDeclaration(node) && node.name?.text === component); + assert.ok(declaration?.body, `Missing production component ${component}`); + const matches = declaration.body.statements.filter((node) => ts.isVariableStatement(node) && + node.declarationList.declarations.some((value) => names.includes(value.name?.text))); + assert.equal(matches.length, names.length, `Missing production declarations in ${component}`); + return matches.map((node) => node.getText(source).replace(/^const /, 'var ')).join('\n'); +} + function classMembers(file, name, names) { const source = ts.createSourceFile(file, fs.readFileSync(path.join(root, file), 'utf8'), ts.ScriptTarget.Latest, true); const declaration = source.statements.find((node) => ts.isClassDeclaration(node) && node.name?.text === name); @@ -85,6 +97,28 @@ async function createRealmClient(url, options = {}) { assert.match(url, /^http:\/\/(127\.0\.0\.1|localhost):\d+\/$/, 'Fixture must use a loopback HTTP Relay'); const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'keeper-native-realm-backup-')); const diagnostics = []; + const persistedHostRecords = new Map(); + const persistedHostReads = []; + const persistedBootStates = []; + let firstPersistedKey; + let secondaryPersistedRead; + let failingPersistedKey; + // Only the MMKV host storage boundary is replaced. When explicitly requested, + // actual redux-persist reads and reconciles these serialized persisted bytes; + // the adapter never supplies Realm records, backup evidence or HTTP replies. + const reduxStorage = { + getItem: async (key) => { + persistedHostReads.push(key); + // Controlling MMKV completion order must not replace any Redux action, + // reducer, backup proof or server response in the genuine matrix. + if (firstPersistedKey && key !== `persist:${firstPersistedKey}`) + await secondaryPersistedRead.promise; + if (key === `persist:${failingPersistedKey}`) throw new Error('Disposable host storage read failed'); + return persistedHostRecords.get(key) ?? null; + }, + setItem: async (key, value) => { persistedHostRecords.set(key, value); }, + removeItem: async (key) => { persistedHostRecords.delete(key); }, + }; const bindings = { realm: Realm, 'src/services/sentry': { captureError(error) { diagnostics.push(error?.name || 'error'); } }, @@ -93,9 +127,7 @@ async function createRealmClient(url, options = {}) { 'react-native-rsa-native': { RSA: new Proxy({}, { get: () => () => { throw new Error('Unexpected native RSA use'); } }) }, 'src/utils/service-utilities/config': { RELAY: url, HEXA_ID: 'disposable-fixture', ENVIRONMENT: 'DEVELOPMENT', isDevMode: () => true }, 'src/storage/realm/migrations': { runRealmMigrations() { throw new Error('A fresh fixture must never migrate an existing Realm'); } }, - // No MMKV/native persistence is available on Node. Redux reducers are real; - // this unused host adapter never supplies recovery state or write proof. - 'src/storage': { reduxStorage: { getItem: async () => null, setItem: async () => undefined, removeItem: async () => undefined } }, + 'src/storage': { reduxStorage }, 'redux-persist/es/persistReducer': require('redux-persist/lib/persistReducer').default, }; const load = sourceLoader(bindings, options); @@ -120,19 +152,35 @@ async function createRealmClient(url, options = {}) { const storage = load('src/store/reducers/storage'); const account = load('src/store/reducers/account'); const settings = load('src/store/reducers/settings'); - const middlewareScope = { dbManager, RealmSchema, ...transport, + const middlewareScope = { dbManager, RealmSchema, ...transport, ...bhr, isBackupVerificationCurrent: (...args) => load('src/services/backup/automatic').isBackupVerificationCurrent(...args) }; evaluate(declarations('src/store/store.ts', ['accountOwnedActions', 'accountOwnedUtxoUiActions', 'bindActionOrigin']), middlewareScope, 'production-origin-middleware.ts'); const actions = []; let beforeDispatch; + // This Node fixture mounts the four production slices used by recovery. The + // optional root wrapper uses the actual production declarations/config and + // redux-persist reconciler, rather than simulating REHYDRATE with a reducer. + let reducer = combineReducers({ bhr: bhr.default, storage: storage.default, account: account.default, settings: settings.default }); + if (options.persistedRedux) { + const persistenceScope = { appReducer: reducer, storage: reduxStorage, reduxStorage, + RESET_REDUX_STORE: 'RESET_REDUX_STORE', persistReducer, createMigrate, + reduxPersistMigrations: load('src/store/migrations').default }; + evaluate(declarations('src/store/store.ts', ['rootReducer', 'persistConfig', 'persistedReducer']), persistenceScope, 'production-root-persistence.ts'); + reducer = persistenceScope.persistedReducer; + } const store = configureStore({ - reducer: { bhr: bhr.default, storage: storage.default, account: account.default, settings: settings.default }, + reducer, middleware: (defaults) => defaults({ serializableCheck: false, immutableCheck: false }).concat(() => (next) => (action) => { beforeDispatch?.(action); return next(action); }, middlewareScope.bindActionOrigin, () => (next) => (action) => { actions.push(action); - return next(action); + const result = next(action); + if (action.type === 'persist/REHYDRATE') { + persistedBootStates.push({ key: action.key, bhr: plain(store.getState().bhr) }); + if (action.key === firstPersistedKey) secondaryPersistedRead.release(); + } + return result; }), }); store.dispatch(settings.setBitcoinNetwork(enums.NetworkType.TESTNET)); @@ -188,6 +236,40 @@ async function createRealmClient(url, options = {}) { return nativeRealm(); } await selectAccount(); + let persistor; + + async function rehydratePersistedRedux(recordsByKey, { firstKey, failKey } = {}) { + assert.equal(options.persistedRedux, true, 'Actual root persistence must be requested before creating the store'); + assert.equal(persistor, undefined, 'Each fixture represents one cold Redux persistence boot'); + if (firstKey) { + assert.ok(['bhr', 'root'].includes(firstKey)); + firstPersistedKey = firstKey; + let release; + const promise = new Promise((resolve) => { release = resolve; }); + secondaryPersistedRead = { promise, release }; + } + failingPersistedKey = failKey; + for (const [key, state] of Object.entries(recordsByKey)) { + const serialized = Object.fromEntries(Object.entries(state).map(([name, value]) => [name, JSON.stringify(value)])); + persistedHostRecords.set(`persist:${key}`, JSON.stringify(serialized)); + } + await new Promise((resolve) => { persistor = persistStore(store, undefined, resolve); }); + assert.equal(persistor.getState().bootstrapped, true); + assert.equal(store.getState()._persist.rehydrated, true); + assert.equal(store.getState().bhr._persist.rehydrated, true); + await persistor.flush(); + } + + async function flushPersistedRedux() { + assert.ok(persistor, 'Actual persistStore must have booted before flush'); + await persistor.flush(); + } + + function readPersistedRedux(key) { + const serialized = persistedHostRecords.get(`persist:${key}`); + assert.equal(typeof serialized, 'string', `Actual redux-persist must have stored ${key}`); + return Object.fromEntries(Object.entries(JSON.parse(serialized)).map(([name, value]) => [name, JSON.parse(value)])); + } function installProductionStoreObservers(extra = {}) { const source = ts.createSourceFile('store.ts', fs.readFileSync(path.join(root, 'src/store/store.ts'), 'utf8'), ts.ScriptTarget.Latest, true); @@ -257,9 +339,10 @@ async function createRealmClient(url, options = {}) { config: bindings['src/utils/service-utilities/config'], defaults, nativeRealm, selectAccount, selected: () => selected, nativeEvents, diagnostics, actions, listen, installProductionStoreObservers, productionSagas, payloadForCurrentImage, + rehydratePersistedRedux, flushPersistedRedux, readPersistedRedux, persistedHostReads, persistedBootStates, setBeforeDispatch(callback) { beforeDispatch = callback; }, - async close() { realmDatabase.closeDatabase(); fs.rmSync(directory, { recursive: true, force: true }); }, + async close() { persistor?.pause(); realmDatabase.closeDatabase(); fs.rmSync(directory, { recursive: true, force: true }); }, }; } -module.exports = { createRealmClient, declarations, classMembers, evaluate, sourceLoader, plain, Realm }; +module.exports = { createRealmClient, declarations, componentDeclarations, classMembers, evaluate, sourceLoader, plain, Realm }; diff --git a/tests/recovery/automaticWriteRealmRelay.integration.cjs b/tests/recovery/automaticWriteRealmRelay.integration.cjs index a260efd7b0..7e97003157 100644 --- a/tests/recovery/automaticWriteRealmRelay.integration.cjs +++ b/tests/recovery/automaticWriteRealmRelay.integration.cjs @@ -3,7 +3,7 @@ // helper; missing prerequisites fail the command and are never a skipped pass. const assert = require('node:assert/strict'); const { test, before, after } = require('node:test'); -const { createRealmClient, plain, Realm } = require('./automaticWriteRealmRelay.fixture.cjs'); +const { createRealmClient, componentDeclarations, evaluate, plain, Realm } = require('./automaticWriteRealmRelay.fixture.cjs'); const fixtureModule = process.env.KEEPER_RELAY_FIXTURE_MODULE; assert.ok(fixtureModule, 'Set KEEPER_RELAY_FIXTURE_MODULE to Relay tests/support/automaticBackupHttpFixture.cjs'); @@ -898,3 +898,295 @@ test('a reentrant newer enable choice immediately before final consent disable r assert.equal(f.store.getState().bhr.automaticCloudBackupByAppId[f.appId], true); assertPending(f); }); + +for (const persistedKey of ['bhr', 'root']) { + test(`actual ${persistedKey} Redux Persist boot expires historical verification until fresh encrypted readback`, async (t) => { + await server.reset(); + const f = await createRealmClient(server.url, { persistedRedux: true }); + t.after(async () => { await f.close(); }); + f.automatic = f.load('src/services/backup/automatic'); + f.installProductionStoreObservers(f.automatic); + persist(f, f.RealmSchema.Wallet, wallet(f)); + await tick(); + const localBeforeBoot = await f.repair.captureBackupImage(); + await server.seed(await f.payloadForCurrentImage()); + server.clearTrace(); + + const interrupted = 'disposable-interrupted-account'; + const completedOnly = 'disposable-completed-only-account'; + const runningOnly = 'disposable-running-only-account'; + const knownDifferent = 'disposable-known-different-account'; + const consentOnly = 'disposable-consent-only-account'; + const consent = { + [f.appId]: true, [interrupted]: false, [completedOnly]: true, + [runningOnly]: true, [knownDifferent]: false, [consentOnly]: false, + }; + const historicalBhr = { + ...plain(f.store.getState().bhr), + backupMethod: 'SEED', seedConfirmed: true, + automaticCloudBackupByAppId: consent, + backupAllLoading: true, backupAllSuccess: true, backupAllFailure: true, + deleteBackupSuccess: true, deleteBackupFailure: true, + backupAllLoadingByAppId: { [f.appId]: true }, + backupAllSuccessByAppId: { [f.appId]: true, [completedOnly]: true }, + backupAllFailureByAppId: { [interrupted]: true }, + deleteBackupSuccessByAppId: { [f.appId]: true }, + deleteBackupFailureByAppId: { [interrupted]: true }, + backupRepairStateByAppId: { [f.appId]: 'verified', [interrupted]: 'uploading', [knownDifferent]: 'different' }, + backupRepairCompletedByAppId: { [f.appId]: true, [completedOnly]: true, [knownDifferent]: false }, + pendingAllBackupByAppId: { [f.appId]: false, [interrupted]: false, [completedOnly]: false, [runningOnly]: false, [knownDifferent]: true }, + // Older snapshots may contain running state despite today's blacklist. + backupRepairRunningByAppId: { [interrupted]: true, [runningOnly]: true }, + _persist: { version: -1, rehydrated: true }, + }; + const rootSnapshot = { + storage: { ...plain(f.store.getState().storage), appVersion: 'persisted-safe-version' }, + account: plain(f.store.getState().account), + _persist: { version: 4, rehydrated: true }, + ...(persistedKey === 'root' ? { bhr: historicalBhr } : {}), + }; + const inboundBeforeBoot = plain(historicalBhr); + await f.rehydratePersistedRedux({ root: rootSnapshot, ...(persistedKey === 'bhr' ? { bhr: historicalBhr } : {}) }, { firstKey: persistedKey }); + const state = f.store.getState(); + assert.ok(f.persistedHostReads.includes('persist:root')); + assert.ok(f.persistedHostReads.includes('persist:bhr')); + assert.ok(f.actions.some((action) => action.type === 'persist/REHYDRATE' && action.key === persistedKey), + 'Actual persistStore must dispatch through production middleware before the real reconciler merges'); + assert.equal(f.persistedBootStates[0].key, persistedKey, 'The real storage adapter must exercise the requested reconciliation order'); + if (persistedKey === 'root') { + assert.notEqual(f.persistedBootStates[0].bhr.automaticCloudBackupByAppId[f.appId], true, + 'Legacy consent waits until the nested read actually confirms absence'); + assert.equal(f.store.getState().bhr.automaticCloudBackupByAppId[f.appId], true, + 'An absent canonical snapshot allows sanitized legacy consent fallback'); + } + assert.deepEqual(state.bhr.automaticCloudBackupByAppId, consent, 'Stored per-account consent must remain unchanged'); + assert.equal(state.bhr.backupMethod, 'SEED'); + assert.equal(state.bhr.seedConfirmed, true); + for (const field of ['backupAllLoading', 'backupAllSuccess', 'backupAllFailure', 'deleteBackupSuccess', 'deleteBackupFailure']) + assert.equal(state.bhr[field], false, `Historical ${field} must not reappear after a cold boot`); + for (const field of ['backupAllLoadingByAppId', 'backupAllSuccessByAppId', 'backupAllFailureByAppId', 'deleteBackupSuccessByAppId', 'deleteBackupFailureByAppId']) + assert.ok(Object.values(state.bhr[field]).every((value) => value === false), `Historical ${field} must not reappear after a cold boot`); + assert.equal(state.storage.appVersion, 'persisted-safe-version'); + assert.equal(state.storage.appId, f.appId); + for (const id of [f.appId, interrupted, completedOnly, runningOnly]) { + assert.equal(state.bhr.backupRepairStateByAppId[id], 'unverified'); + assert.equal(state.bhr.backupRepairCompletedByAppId[id], false); + assert.equal(state.bhr.pendingAllBackupByAppId[id], true); + assert.notEqual(state.bhr.backupRepairRunningByAppId[id], true); + } + assert.equal(state.bhr.backupRepairStateByAppId[knownDifferent], 'different', 'Existing negative findings are retained'); + assert.equal(state.bhr.pendingAllBackupByAppId[knownDifferent], true); + assert.equal(state.bhr.backupRepairStateByAppId[consentOnly], undefined, 'Consent alone never manufactures proof or an interrupted job'); + assert.deepEqual(historicalBhr, inboundBeforeBoot, 'Sanitizing the inbound action must not mutate the host snapshot'); + assert.deepEqual(await f.repair.captureBackupImage(), localBeforeBoot, 'Boot must preserve every native recovery record'); + assert.equal(f.transport.captureBackupWriteIntent(f.appId), undefined, 'Saved UI status cannot supply a fresh session revision'); + assert.equal(f.automatic.isBackupVerificationCurrent(f.appId), false); + await tick(); + assert.equal(server.trace.length, 0, 'Rehydration must not replay a read, repair or encrypted write'); + + // This is a genuine native commit without a baseline, not a fabricated + // pending receipt. Saving the unchanged presentation field stays local. + const noBaseline = f.automatic.beginBackupLocalOperation(f.appId, undefined, f.transport.currentBackupAccountSession()); + const localReceipt = f.automatic.commitBackupLocalOperation(noBaseline, (realm) => { + realm.objectForPrimaryKey('Wallet', 'wallet-a').presentationData.name = 'Initial wallet'; + }); + assert.equal(localReceipt.committed, true); + assert.equal(localReceipt.intent, undefined); + const pending = await f.automatic.enqueueAutomaticBackupOperation(localReceipt, [{ + operation: 'updateAppImage', body: await operations.updateAppImage.body(f), + }]); + assert.notEqual(pending.backupConfirmed, true); + assertPending(f); + assert.equal(server.trace.length, 0, 'No saved completion may authorize this commit or replay its payload'); + + const sagas = f.productionSagas(['currentBackupAppId', 'backupInspectionOwners', 'runBackupInspection']); + assert.equal(await sagas.run('runBackupInspection', false, f.appId), true, + 'Only current native capture and genuine encrypted HTTP snapshot comparison may earn verification'); + assert.ok(server.trace.some((event) => event.path === '/getBackupSnapshot')); + assert.equal(attemptedWrites().length, 0, 'Read-only verification must not repair or replay the earlier local operation'); + assert.equal(f.automatic.isBackupVerificationCurrent(f.appId), true); + assert.equal(f.store.getState().bhr.backupRepairStateByAppId[f.appId], 'verified'); + assert.equal(f.store.getState().bhr.pendingAllBackupByAppId[f.appId], false); + assert.deepEqual(f.store.getState().bhr.automaticCloudBackupByAppId, consent); + + const fresh = await committedRequest(f, 'updateAppImage'); + const uploaded = await settled(fresh); + assert.equal(uploaded.backupConfirmed, true); + assert.equal(attemptedWrites().length, 1, 'Only a fresh post-inspection operation may upload'); + assert.equal(f.store.getState().bhr.pendingAllBackupByAppId[f.appId], false); + assert.equal(f.store.getState().bhr.backupRepairStateByAppId[completedOnly], 'unverified', 'Current-account proof never restores another account completion'); + }); +} + +async function persistedConsentFixture(t) { + await server.reset(); + const f = await createRealmClient(server.url, { persistedRedux: true }); + t.after(async () => { await f.close(); }); + f.automatic = f.load('src/services/backup/automatic'); + f.installProductionStoreObservers(f.automatic); + persist(f, f.RealmSchema.Wallet, wallet(f)); + await tick(); + const localBeforeBoot = await f.repair.captureBackupImage(); + await server.seed(await f.payloadForCurrentImage()); + server.clearTrace(); + const legacy = { + ...plain(f.store.getState().bhr), + automaticCloudBackupByAppId: { [f.appId]: true, 'legacy-only-disposable-account': true }, + backupRepairStateByAppId: { [f.appId]: 'verified' }, + backupRepairCompletedByAppId: { [f.appId]: true }, + pendingAllBackupByAppId: { [f.appId]: false }, + _persist: { version: -1, rehydrated: true }, + }; + const canonicalConsent = { [f.appId]: false, 'canonical-disposable-peer': false }; + const canonical = { ...legacy, backupMethod: 'SEED', seedConfirmed: true, automaticCloudBackupByAppId: canonicalConsent }; + const rootSnapshot = { + storage: plain(f.store.getState().storage), account: plain(f.store.getState().account), + bhr: legacy, _persist: { version: 4, rehydrated: true }, + }; + const subscriberConsent = []; + const unsubscribe = f.store.subscribe(() => { + subscriberConsent.push(f.store.getState().bhr.automaticCloudBackupByAppId[f.appId]); + }); + t.after(unsubscribe); + return { f, canonical, canonicalConsent, rootSnapshot, localBeforeBoot, subscriberConsent }; +} + +for (const firstKey of ['root', 'bhr']) { + test(`actual persisted ${firstKey}-first reconciliation never lets legacy consent override canonical opt-out`, async (t) => { + const { f, canonical, canonicalConsent, rootSnapshot, localBeforeBoot, subscriberConsent } = await persistedConsentFixture(t); + await f.rehydratePersistedRedux({ root: rootSnapshot, bhr: canonical }, { firstKey }); + assert.deepEqual(f.persistedBootStates.map((state) => state.key).filter((key) => ['root', 'bhr'].includes(key)), [firstKey, firstKey === 'root' ? 'bhr' : 'root']); + assert.ok(subscriberConsent.every((value) => value !== true), + 'Actual subscribers must never observe legacy opt-in while the canonical MMKV read is unresolved or opted out'); + assert.deepEqual(f.store.getState().bhr.automaticCloudBackupByAppId, canonicalConsent, + 'Canonical consent is authoritative for every account; legacy-only consent cannot survive either reconciliation order'); + assert.equal(f.store.getState().bhr.seedConfirmed, true); + assert.equal(f.store.getState().bhr.backupMethod, 'SEED'); + assertPending(f); + assert.equal(f.transport.captureBackupWriteIntent(f.appId), undefined); + assert.equal(f.automatic.isBackupVerificationCurrent(f.appId), false); + assert.deepEqual(await f.repair.captureBackupImage(), localBeforeBoot); + await tick(); + assert.equal(server.trace.length, 0, 'Persistence reconciliation must not request or replay a backup'); + + const sagas = f.productionSagas(['currentBackupAppId', 'backupInspectionOwners', 'runBackupInspection']); + assert.equal(await sagas.run('runBackupInspection', false, f.appId, true), true, + 'An explicit read-only check can earn a genuine current proof without re-enabling automatic backup'); + assert.equal(f.automatic.isBackupVerificationCurrent(f.appId), true); + assert.deepEqual(f.store.getState().bhr.automaticCloudBackupByAppId, canonicalConsent); + assert.ok(server.trace.some((event) => event.path === '/getBackupSnapshot')); + assert.equal(attemptedWrites().length, 0); + const readsBeforeCommit = server.trace.length; + const request = await committedRequest(f, 'updateAppImage'); + const pending = await f.automatic.enqueueAutomaticBackupOperation(request.receipt, [{ operation: 'updateAppImage', body: request.body }]); + assert.notEqual(pending.backupConfirmed, true); + assert.equal(f.dbManager.getObjectById(f.RealmSchema.Wallet, 'wallet-a').presentationData.name, 'Committed wallet'); + assert.equal(server.trace.length, readsBeforeCommit, 'A fresh proof still cannot authorize an automatic write against saved opt-out'); + assert.equal(attemptedWrites().length, 0); + assertPending(f); + assert.deepEqual(f.store.getState().bhr.automaticCloudBackupByAppId, canonicalConsent); + }); +} + +test('an actual failed canonical persistence read cannot fall back to older root consent', async (t) => { + const { f, rootSnapshot, localBeforeBoot, subscriberConsent } = await persistedConsentFixture(t); + await f.rehydratePersistedRedux({ root: rootSnapshot }, { firstKey: 'root', failKey: 'bhr' }); + assert.deepEqual(f.persistedBootStates.map((state) => state.key).filter((key) => ['root', 'bhr'].includes(key)), ['root', 'bhr']); + assert.ok(f.actions.some((action) => action.type === 'persist/REHYDRATE' && action.key === 'bhr' && action.err), + 'The actual persistStore failure path must reach production middleware'); + assert.ok(subscriberConsent.every((value) => value !== true), 'A failed read must never expose the older opt-in to subscribers'); + assert.notEqual(f.store.getState().bhr.automaticCloudBackupByAppId[f.appId], true); + assert.notEqual(f.store.getState().bhr.backupRepairCompletedByAppId[f.appId], true); + assert.notEqual(f.store.getState().bhr.backupRepairStateByAppId[f.appId], 'verified'); + assert.equal(f.transport.captureBackupWriteIntent(f.appId), undefined); + assert.equal(f.automatic.isBackupVerificationCurrent(f.appId), false); + assert.deepEqual(await f.repair.captureBackupImage(), localBeforeBoot); + await tick(); + assert.equal(server.trace.length, 0); + const sagas = f.productionSagas(['currentBackupAppId', 'backupInspectionOwners', 'runBackupInspection']); + assert.equal(await sagas.run('runBackupInspection', false, f.appId, true), true); + assert.equal(f.automatic.isBackupVerificationCurrent(f.appId), true); + assert.notEqual(f.store.getState().bhr.automaticCloudBackupByAppId[f.appId], true, + 'A subsequent real read-only verification cannot turn unavailable stored consent into opt-in'); + assert.equal(attemptedWrites().length, 0); +}); + +test('actual absent-first legacy fallback without persistence metadata retains future nested writes', async (t) => { + const { f, rootSnapshot, localBeforeBoot } = await persistedConsentFixture(t); + const pendingPeer = 'disposable-persisted-pending-peer'; + // This is the raw shape of an older root snapshot, before BHR had nested + // persistence. It deliberately omits live initialized fields and _persist. + rootSnapshot.bhr = { + backupMethod: 'SEED', automaticCloudBackupByAppId: { [f.appId]: true }, + backupRepairStateByAppId: { [f.appId]: 'verified' }, + backupRepairCompletedByAppId: { [f.appId]: true }, + pendingAllBackupByAppId: { [f.appId]: false, [pendingPeer]: false }, + }; + await f.rehydratePersistedRedux({ root: rootSnapshot }, { firstKey: 'bhr' }); + assert.deepEqual(f.persistedBootStates.map((state) => state.key).filter((key) => ['root', 'bhr'].includes(key)), ['bhr', 'root']); + const restored = f.store.getState().bhr; + assert.equal(restored.automaticCloudBackupByAppId[f.appId], true, 'Confirmed nested absence permits sanitized raw legacy fallback'); + assert.equal(restored.backupMethod, 'SEED'); + assertPending(f); + assert.equal(restored._persist.rehydrated, true, 'The late root merge must retain live nested persistence metadata'); + assert.equal(restored._persist.version, -1); + assert.deepEqual(plain(restored.backupAllLoadingByAppId), {}); + assert.deepEqual(plain(restored.seedWords), [], 'Initialized fields absent from old payloads must remain available'); + assert.equal(f.transport.captureBackupWriteIntent(f.appId), undefined); + assert.equal(f.automatic.isBackupVerificationCurrent(f.appId), false); + assert.deepEqual(await f.repair.captureBackupImage(), localBeforeBoot); + assert.equal(server.trace.length, 0); + + f.store.dispatch(f.bhr.setAutomaticCloudBackup({ appId: f.appId, enabled: false })); + f.store.dispatch(f.bhr.setPendingAllBackup({ appId: pendingPeer, pending: true })); + await f.flushPersistedRedux(); + const saved = f.readPersistedRedux('bhr'); + assert.equal(saved.automaticCloudBackupByAppId[f.appId], false, 'Actual nested persistence must store a later consent opt-out'); + assert.equal(saved.pendingAllBackupByAppId[pendingPeer], true, 'Actual nested persistence must store a later pending-state change'); + assert.equal(saved.backupRepairStateByAppId[f.appId], 'unverified'); + assert.equal(saved.backupRepairCompletedByAppId[f.appId], false); + assert.equal(saved.backupMethod, 'SEED'); + assert.equal(saved._persist.rehydrated, true); + assert.equal(f.store.getState().bhr._persist.rehydrated, true); + assert.equal(f.transport.captureBackupWriteIntent(f.appId), undefined); + assert.deepEqual(await f.repair.captureBackupImage(), localBeforeBoot); + await tick(); + assert.equal(server.trace.length, 0, 'Legacy fallback and subsequent storage flush must never replay a backup request'); +}); + +test('actual AssistedBackupStatus phase expression requires current native session proof', async (t) => { + const f = await setup(t, 'updateAppImage'); + // Execute the exact production phase declarations only. This is a native + // proof/HTTP integration contract, not a React render or native UI test. + const expression = componentDeclarations('src/screens/AppSettings/AssistedBackupStatus.tsx', + 'AssistedBackupStatus', ['recordedPhase', 'phase']); + function displayedPhase() { + const scope = { id: f.appId, backupRepairStateByAppId: f.store.getState().bhr.backupRepairStateByAppId, + isBackupVerificationCurrent: f.automatic.isBackupVerificationCurrent }; + evaluate(expression, scope, 'production-assisted-backup-phase.ts'); + return scope.phase; + } + assert.equal(f.automatic.isBackupVerificationCurrent(f.appId), true); + assert.equal(displayedPhase(), 'verified', 'Fresh complete encrypted HTTP comparison may display Verified'); + await f.selectAccount('disposable-status-other-account'); + await f.selectAccount(); + assert.equal(f.store.getState().bhr.backupRepairStateByAppId[f.appId], 'verified', + 'Historical Redux phase deliberately remains present while session proof has expired'); + assert.equal(f.automatic.isBackupVerificationCurrent(f.appId), false); + assert.equal(displayedPhase(), 'unverified', 'Returning to A cannot reuse the first A session display claim'); + assert.equal(server.trace.length, 0, 'Evaluating status must never request or replay a backup'); + assert.equal(await f.sagas.run('runBackupInspection', false, f.appId), true); + assert.ok(server.trace.some((event) => event.path === '/getBackupSnapshot')); + assert.equal(f.automatic.isBackupVerificationCurrent(f.appId), true); + assert.equal(displayedPhase(), 'verified', 'A fresh genuine readback may restore the display claim'); + const beforeLocalChange = server.trace.length; + f.nativeRealm().write(() => { + f.nativeRealm().objectForPrimaryKey('Wallet', 'wallet-a').presentationData.name = 'Changed native recovery record'; + }); + assert.equal(f.store.getState().bhr.backupRepairStateByAppId[f.appId], 'verified', + 'The display predicate must also reject a stale phase before Redux has received a mutation notification'); + assert.equal(f.automatic.isBackupVerificationCurrent(f.appId), false); + assert.equal(displayedPhase(), 'unverified'); + assert.equal(server.trace.length, beforeLocalChange); + assert.equal(attemptedWrites().length, 0); +}); From fc33cda38e8dbd8034057470912d1ba2346eea99 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Tue, 13 Oct 2026 05:27:35 +0000 Subject: [PATCH 188/254] Specify native consent precedence and persistence durability checks --- release/r4-automatic-backup-local-handoff.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/release/r4-automatic-backup-local-handoff.md b/release/r4-automatic-backup-local-handoff.md index 604ce37b37..37d6922e51 100644 --- a/release/r4-automatic-backup-local-handoff.md +++ b/release/r4-automatic-backup-local-handoff.md @@ -224,7 +224,7 @@ Verify actual app/package IDs, version/counters, APK signature/alignment and lau On **both** platforms, record each scenario separately: 1. Fresh disposable onboarding, PIN create/unlock/cooldown, always-visible disabled Proceed/queued presentation and account setup. Cold launch/unlock without pressing a key must retain every keypad digit; disabled cooldown must reject input, retain all digits, expire normally and then accept input. Exercise PIN creation and another shared-keypad consumer without shortening cooldown in production code. -2. Opt in per account, create/edit/delete wallets, signers and vaults locally, archive and register them, edit/import/delete labels, change nodes and version-related recovery state, then relaunch. Check actual local persistence, pending/verified presentation, failure-stage/retry copy and complete native clean Recovery Key restore into a **different disposable Realm**, through the production restore worker. Never restore over the owner's existing app/database. After a verified backup, terminate and cold-launch the disposable app with the next snapshot delayed or unavailable. Open backup status immediately: saved verification and interrupted phases must be unverified/pending before a fresh complete read establishes current proof. Consent and committed local records must survive; no prepared ciphertext may replay. Repeat after a failed partial fanout and across accounts. Record native MMKV persistence and actual process restart separately from host Redux-persist rehydration tests. +2. Opt in per account, create/edit/delete wallets, signers and vaults locally, archive and register them, edit/import/delete labels, change nodes and version-related recovery state, then relaunch. Check actual local persistence, pending/verified presentation, failure-stage/retry copy and complete native clean Recovery Key restore into a **different disposable Realm**, through the production restore worker. Never restore over the owner's existing app/database. After a verified backup, terminate and cold-launch the disposable app with the next snapshot delayed or unavailable. Open backup status immediately: saved verification and interrupted phases must be unverified/pending before a fresh complete read establishes current proof. Consent and committed local records must survive; no prepared ciphertext may replay. Repeat after a failed partial fanout and A→B→A: a historical phase must not display Verified after its session proof expires. With disposable native storage, cover canonical nested opt-out versus stale root opt-in in both read orders, hold the canonical read and check intermediate states, distinguish confirmed absence from read error, and restore an older root BHR record without nested persistence metadata. Then change consent/pending state and cold-launch again to prove subsequent writes persist. Record native MMKV persistence and actual process restart separately from host Redux-persist rehydration and source-expression tests. 3. Repeat C1–C10 through feasible real UI paths with the paired v2 service: interruptions/large payloads, 409/old route/malformed/lost reply, local mutation during verification, A→B→A, consent change during deletion, missing/orphaned/legacy encrypted references and multi-write partial success. Confirm server-only records and immutable identities survive. A successful Delete Backup retains local data, acknowledges empty readback, clears only the intended consent and never displays a verified backup. 4. On iOS, import identical harmless bracketed/unbracketed TXT files and valid `[Test]_backup.bsms` / `Test_backup.bsms` from the same Files provider. Both BSMS files must reach identical configuration review; stop before duplicate creation. Test filenames with spaces/literal percent escapes and cancellation. Smoke Android's unchanged content-URI path on a native file picker. 5. Fresh-install Testnet tutorial → Receive Test Sats → visible wallet balance **without restarting**, including retry/slow response and displayed-account ownership. Local mode intentionally lacks a faucet/AI provider: a 503 or mocked unit result is not positive end-to-end evidence. Use an independently approved disposable Testnet-provider QA configuration when available; otherwise record the scenario's actual BLOCKED/NOT RUN status and explicit owner follow-up, without waiving critical backup or required platform checks. From 2f3d55d86137c0d689fb33227d379608ba73d6e6 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Tue, 13 Oct 2026 05:31:20 +0000 Subject: [PATCH 189/254] docs: record rehydration and current-proof source validation --- .../automatic-write-validation.md | 16 +++++++------ ...utomatic-backup-source-proof-2026-10-13.md | 23 +++++++++++-------- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/openspec/changes/fix-recovery-backup-freshness/automatic-write-validation.md b/openspec/changes/fix-recovery-backup-freshness/automatic-write-validation.md index 14e1d76750..c50a5dcccc 100644 --- a/openspec/changes/fix-recovery-backup-freshness/automatic-write-validation.md +++ b/openspec/changes/fix-recovery-backup-freshness/automatic-write-validation.md @@ -9,17 +9,17 @@ Automatic-backup implementation and disposable proof are mandatory R4 work. Crit | Field | Current record | | --- | --- | | Audit starting app source | b160656218dc6065ad12af9db220c974255bfc23; historical | -| Final tested code commit before documentation | a243b53b5682a276eb88851c3d34379c0908fedd; includes the frozen-Yarn CI setup and email/derivation utility fixes | +| Final tested code commit before documentation | 3cc9368ca2f3b2149db5743a5f9d230aa516d6a0; includes persisted-proof expiration, current-proof status projection and seven new genuine cases | | Tested Relay fixture commit | b600416e5ba7325927d7d2871cff7776f4e593fe; runner requires clean tracked source | | Runtime and topology | Node 22.23.3, actual Realm 12.15.0, Linux host, disposable loopback keeperQA Mongo replica set on port 27058; no hosted credentials | -| Combined genuine fixture command/result | `npm run test:automatic-backup-genuine`: PASS 253/253, 0 failures/skips, 195.046 seconds, clean frozen-Yarn run terminal recorded 2026-10-13 04:40 UTC; core 206 + producers 46 + restore 1 | -| Recovery unit command/result | `npm run test:recovery`: 434/434 PASS, 0 failures/skips, 67.140 seconds at a243b53b5682a276eb88851c3d34379c0908fedd | +| Combined genuine fixture command/result | `npm run test:automatic-backup-genuine`: PASS 260/260, 0 failures/skips, 208.779 seconds, clean frozen-Yarn run terminal recorded 2026-10-13 05:26 UTC; core 213 + producers 46 + restore 1 | +| Recovery unit command/result | `npm run test:recovery`: 443/443 PASS, 0 failures/skips, 52.156 seconds at 3cc9368ca2f3b2149db5743a5f9d230aa516d6a0 | | Clean encrypted Recovery Key restore | PASS: actual recovery saga, real key derivation/AES, blank encrypted Realm and real HTTP reads; complete image, all signer fields and normalized nodes checked | | Source/hosted flags | AUTOMATIC_BACKUP_SOURCE_READY=true for DEVELOPMENT loopback only; AUTOMATIC_BACKUP_RELAY_ROLLOUT_READY=false; hosted rollout remains false | | IOS / ANDROID exact-source result | NOT RUN / NOT RUN; NEEDS_LOCAL | -| Review and current-head CI | Independent agent fixture architecture review completed with restore assertion gap corrected. Final external developer review/current-head combined GitHub CI remain open. Earlier backup Sonar failure stays disclosed. Source PR #7042 trigger corrected by workflow-only 74df55446d8645ebfac05405fce81fa43446acd9; runtime unchanged. Separate CodeQL check 115132119008 completed SUCCESS (No new alerts in code changed, zero annotations), Analyze 115131867787 SUCCESS; hosted Tests run 38347397038 at 74df FAILED: 34/35 Jest suites, 333/340 tests, seven unchanged live-network Electrum failures; recovery/version/release checks SKIPPED. A test-only deterministic Electrum fixture correction is in progress with no runtime/dependency change; no future pass or combined-head CI is inferred | -| Current Jest | 42 suites / 432 tests PASS, 72.836 seconds at a243b53b5682a276eb88851c3d34379c0908fedd | -| Type/lint boundary | Focused six core module lint/format PASS. Whole-project tsc FAIL with 945 inherited diagnostics versus 957 at baseline bb66cea459fbb00ec33c5c4ebd601d07c0d7c125; zero added normalized diagnostics, not a clean typecheck | +| Review and current-head CI | Independent agent fixture review completed; external final-source review remains open. Prior head 2071 hosted Tests 38348159148 and CodeQL 115134694738 succeeded; current final-head combined CI awaits the post-documentation push. Source #7042 at 163cc own-head checks succeeded with runtime d280 unchanged; prior failures remain in [October evidence](evidence/automatic-backup-source-proof-2026-10-13.md) | +| Current Jest | 42 suites / 432 tests PASS, 37.9 seconds at 3cc9368ca2f3b2149db5743a5f9d230aa516d6a0 | +| Type/lint boundary | Recorded focused six core module lint/format PASS. Prior 2071 hosted whole lint CLI FAIL: 5353 problems, 4467 errors, 886 warnings, tolerated by the green job. Whole-project tsc FAIL with 945 inherited diagnostics versus 957 at baseline bb66cea459fbb00ec33c5c4ebd601d07c0d7c125; zero added normalized diagnostics, not a clean typecheck | | Current release preflight | FAIL: 2026-10-13 04:11 UTC command found iOS store history older than 24 hours; refresh required before release | | Production running artifact/topology/authorization | NOT VERIFIED; no deployment, merge or publication authorized | @@ -44,7 +44,7 @@ Use PASS, FAIL, BLOCKED or NOT RUN with provenance. The source SHA precedes this The genuine matrix uses actual production snapshot/commit/automatic/transport code, real AES, production schemas and encrypted native Realm, extracted production Redux middleware/store observers, and real HTTP into the pinned transactional Relay. Common negatives include stale generation, queued A→B→A, remote-only/key conflict, failed commit, copied/missing proof, unsupported/malformed protocol, timeout/lost ACK, incomplete/wrong-key readback and closed/reopened Realm. The additional after-send A→B→A and inter-member native-write schedules passed 8/8 at fixture commit 7b1ad70ba9e2aa1a2778b375f36e776bdd26a5ad, before source commit efa6f89ebf7140f738f03ce8a7218a2a1e7f9bc2 enabled development loopback testing. The combined final runner result is recorded separately. -Most operation mutation callbacks in the core matrix are constructed directly over the production commit API. Separate producer files exercise actual wallet/vault, signer/registration, label, version-upgrade and existing-USDT functions. This does not attest full React Native store boot, MMKV, UI navigation or native app relaunch. USDT new-wallet creation/import remains paused; tested USDT records are existing retained records only. +Most operation mutation callbacks in the core matrix are constructed directly over the production commit API. Separate producer files exercise actual wallet/vault, signer/registration, label, version-upgrade and existing-USDT functions. Seven additional cases exercise real Redux Persist reconciliation in both BHR/legacy-root orders and the actual UI AST declaration projection over native Realm/HTTP proof. This does not attest rendered React Native UI, native MMKV, UI navigation or native app relaunch. USDT new-wallet creation/import remains paused; tested USDT records are existing retained records only. | Operation | Observed production proof | Host result / evidence | | --- | --- | --- | @@ -72,6 +72,7 @@ Most operation mutation callbacks in the core matrix are constructed directly ov | Final verification race | PASS native write immediately before actual Redux verification dispatch leaves pending; copied/uncommitted proof cannot bypass it | | Account change after send | PASS added after-send A→B→A for all seven operations, with a distinct B seed/account/publicId and independently verified B backup; old A completion leaves B state, native data and remote backup unchanged | | Realm close/reopen | PASS stale queued proof rejects the reopened handle; this is not native app/process restart or MMKV evidence | +| Persisted host rehydration / current-proof display | PASS two actual Redux Persist boot cases and five actual UI AST declaration projection cases over native Realm/HTTP. Saved Verified/interrupted/completed status expires to unverified/pending; consent, confirmation, local records and existing negative findings survive. No replay or upload; only fresh complete readback restores proof | | Persisted app interruption/relaunch and background behavior | NOT RUN; mandatory IOS/ANDROID Local checks | | Transactional HTTP contract | PASS actual paired route/service/Mongoose transaction path and disposable source contract checks; hosted artifact parity, topology, limits and old-writer fleet rollout remain unverified | @@ -82,6 +83,7 @@ Both platforms must test the final source with disposable accounts/data and a co | Native scenario | IOS result + evidence | ANDROID result + evidence | | --- | --- | --- | | Build and launch exact source; identify native package/version and backend source | NOT RUN | NOT RUN | +| Cold launch with saved Verified/interrupted state, nested/legacy consent precedence and durable MMKV: unverified/pending until fresh complete readback; no replay, preserved local data/choices/confirmation | NOT RUN | NOT RUN | | Opted-in automatic create/edit/delete after initial backup; local data survives forced cloud failure and relaunch | NOT RUN | NOT RUN | | Complete server readback and clean Recovery Key restore of newly produced committed dataset | NOT RUN | NOT RUN | | Legacy encrypted snapshot upgrade plus absent/stale/transport/server/decryption failure; no false missing/empty/verified or unsolicited replacement | NOT RUN | NOT RUN | diff --git a/openspec/changes/fix-recovery-backup-freshness/evidence/automatic-backup-source-proof-2026-10-13.md b/openspec/changes/fix-recovery-backup-freshness/evidence/automatic-backup-source-proof-2026-10-13.md index 9409f2168c..c19f48c5c4 100644 --- a/openspec/changes/fix-recovery-backup-freshness/evidence/automatic-backup-source-proof-2026-10-13.md +++ b/openspec/changes/fix-recovery-backup-freshness/evidence/automatic-backup-source-proof-2026-10-13.md @@ -4,7 +4,7 @@ This is a sanitized result summary. It contains source identities, commands, cou ## Source identity and enablement boundary -- Final tested app code commit before this documentation commit: `a243b53b5682a276eb88851c3d34379c0908fedd`. This includes frozen-Yarn Relay CI setup, bounded ASCII email validation and strict derivation-account parsing fixes. +- Final tested app code commit before this documentation commit: `3cc9368ca2f3b2149db5743a5f9d230aa516d6a0`. This includes persisted BHR proof expiration before reconciliation, current-proof UI status projection and seven added genuine cases, in addition to the earlier frozen-Yarn and utility fixes. - Paired Relay: `b600416e5ba7325927d7d2871cff7776f4e593fe`. The app runner requires this exact clean tracked checkout and the expected fixture path. - Final flags: `AUTOMATIC_BACKUP_SOURCE_READY=true` permits DEVELOPMENT loopback testing only; `AUTOMATIC_BACKUP_RELAY_ROLLOUT_READY=false` remains unchanged. Development source readiness does not enable hosted writes. - The final PR Local handoff must separately record the exact post-push head SHA. This evidence file cannot name its own future commit or confer release approval. @@ -13,11 +13,11 @@ This is a sanitized result summary. It contains source identities, commands, cou Command: `npm run test:automatic-backup-genuine`, with Node 22.23.3 on Linux, actual Realm 12.15.0, `KEEPER_RELAY_FIXTURE_MODULE` pointing to the pinned Relay's `tests/support/automaticBackupHttpFixture.cjs`, and `BACKUP_TEST_MONGO_URL=mongodb://127.0.0.1:27058`. The runner refuses a missing/different/dirty tracked Relay, missing required matrix, unavailable native binding, unsupported Node or non-loopback/credentialed Mongo. A missing prerequisite fails; it is not skipped or replaced by a unit adapter. -Final combined command outcome/time: **PASS 253/253, 0 failures, 0 skips**, exit 0, 195.046 seconds; clean frozen-Yarn run terminal log recorded 2026-10-13 04:40 UTC on the exact app/Relay sources above. Earlier combined seven-file run passed 245/245 with zero skips. Eight added direct schedules passed 8/8 with zero skips, 30.469 seconds, observed 2026-10-13 04:08 UTC at fixture commit 7b1ad70ba9e2aa1a2778b375f36e776bdd26a5ad: one native local change after the first actual CAS while its ACK is held, and after-send A→B→A for each of the seven operations. Each B account uses a distinct seed/appId/publicId and real verified B baseline; old A completion preserves B UI/native/remote state. The final seven-file combined run includes all eight schedules at the enabled code SHA. +Final combined command outcome/time: **PASS 260/260, 0 failures, 0 skips**, exit 0, 208.779 seconds; clean frozen-Yarn run terminal log recorded 2026-10-13 05:26 UTC on the exact app/Relay sources above. Earlier combined seven-file run passed 245/245 with zero skips. Eight added direct schedules passed 8/8 with zero skips, 30.469 seconds, observed 2026-10-13 04:08 UTC at fixture commit 7b1ad70ba9e2aa1a2778b375f36e776bdd26a5ad: one native local change after the first actual CAS while its ACK is held, and after-send A→B→A for each of the seven operations. Each B account uses a distinct seed/appId/publicId and real verified B baseline; old A completion preserves B UI/native/remote state. The final seven-file combined run includes all eight schedules at the enabled code SHA. | File / scope | Observed result | | --- | --- | -| automaticWriteRealmRelay.integration.cjs — all seven operations and shared queue/proof gates | 206/206 PASS, including the eight added held-ACK schedules | +| automaticWriteRealmRelay.integration.cjs — all seven operations and shared queue/proof gates | 213/213 PASS, including held-ACK schedules plus seven rehydration/current-proof projection cases | | automaticRecoveryRestore.integration.cjs — whole Recovery Key restore | 1/1 PASS; strengthened whole-image/all-signers/normalized-nodes comparison also reran 1/1 PASS | | automaticLabelProducers.integration.cjs — actual label import/edit/send/manual-coin producers | 9/9 PASS | | automaticUpgradeProducer.integration.cjs — actual launch upgrade/version/health/label fanout | 9/9 PASS | @@ -27,7 +27,7 @@ Final combined command outcome/time: **PASS 253/253, 0 failures, 0 skips**, exit The fixture loads actual production Realm wrappers and schemas, snapshot/sanitization/AES, backup automatic coordinator/transport, real Redux reducers, extracted production account-origin middleware and store observers. The paired server runs real HTTP through extracted production routes, production services and Mongoose transactions in a disposable keeperQA replica set. Protocol fault interception is explicit: malformed/old/missing/false ACK, 404, 409, lost or delayed reply and corrupt readback. Negative tests assert zero unauthorized effects or stopped fanout, retained local data, and pending/unverified status. They never manufacture an operation receipt, generation or baseline. -Core mutation callbacks are often built directly over the production commit API; the five producer suites and selected BHR cases additionally execute actual source functions. Host platform/device-info, MMKV persistence adapter, unused RSA and notification helpers are disclosed host boundaries. No full React Native app boot, UI navigation, physical signing or native app persistence is attested. +Core mutation callbacks are often built directly over the production commit API; the five producer suites and selected BHR cases additionally execute actual source functions. Host platform/device-info, MMKV persistence adapter, unused RSA and notification helpers are disclosed host boundaries. The seven new cases comprise two actual Redux Persist boot/reconciliation cases and five actual UI AST declaration projection cases over native Realm/HTTP proof. They assert saved Verified/interrupted/completed flags become unverified/pending without changing consent, physical confirmation or local records; existing negative findings survive. Rehydration sends no Relay read, repair or write and grants no usable write intent. A fresh complete encrypted HTTP inspection alone restores current proof, and a new post-inspection operation alone may upload. The display checks project actual source declarations; no rendered React Native UI, native MMKV, full app boot, UI navigation or physical signing is attested. Clean restoration uses real BIP39/BIP32/BIP85/AES and actual getAppImageWorker/recoverApp/writeRecoveredObject into a blank encrypted Realm, reading from the actual HTTP Relay. The dataset includes BTC MAINNET and TESTNET, retained DEFAULT and IMPORTED USDT key/address/history metadata, Recovery Key and other signer data, active/archived vaults, registrations, labels and both-network nodes. Whole recovery-image comparison, complete signer fields and normalized nodes are asserted. Initial database opening is a real dbManager call, outside the native passcode/UI flow. No new USDT setup/import, provider transfer or consent opt-in is enabled by this test. @@ -35,21 +35,26 @@ Clean restoration uses real BIP39/BIP32/BIP85/AES and actual getAppImageWorker/r | Check | Actual outcome / boundary | | --- | --- | -| Recovery suite: `npm run test:recovery` | 434/434 PASS, zero failures/skips, 67.140 seconds, exit 0 on app source a243b53b5682a276eb88851c3d34379c0908fedd. Producer unit adapters are distinguished from genuine integration files; actual native Realm wrapper regressions are included | +| Recovery suite: `npm run test:recovery` | 443/443 PASS, zero failures/skips, 52.156 seconds, exit 0 on app source 3cc9368ca2f3b2149db5743a5f9d230aa516d6a0. Producer unit adapters are distinguished from genuine integration files; actual native Realm wrapper regressions are included | | Realm commit/invalidation/open-session focused suite | 20/20 PASS, including ten actual-native atomic commit/rollback/observer cases | -| Root Jest | 42 suites / 432 tests PASS, exit 0, 72.836 seconds on a243b53b5682a276eb88851c3d34379c0908fedd | +| Root Jest | 42 suites / 432 tests PASS, exit 0, 37.9 seconds on 3cc9368ca2f3b2149db5743a5f9d230aa516d6a0 | | Release tooling | Coordinator reports Node 28 PASS, Python 7 PASS and Ruby 21 runs / 117 assertions PASS; these are tooling tests, not signing/store validation | | Local validation gate tests | Coordinator reports 11/11 PASS; gate test code passing does not supply an actual Local validation comment | -| Six core backup modules lint/format | Coordinator reports PASS | +| Focused six core backup modules lint/format | Recorded PASS; distinguished from whole-repository lint | +| Whole-repository lint CLI | Prior 2071 hosted run actually FAILED with 5353 problems: 4467 errors and 886 warnings. The job tolerated this failure and completed green; neither job status nor focused checks is a clean whole-lint pass | | Whole-project TypeScript | FAIL: coordinator reports 945 inherited diagnostics versus 957 at baseline bb66cea459fbb00ec33c5c4ebd601d07c0d7c125, zero added normalized diagnostics. No clean whole-project typecheck is claimed | | Current release preflight | FAIL: observed 2026-10-13 04:11 UTC, iOS store history older than 24 hours; no release-readiness pass is inferred from tooling tests | -| Source CI gaps | Earlier backup Sonar failure remains disclosed. Source PR #7042 pull_request trigger was corrected by workflow-only commit 74df55446d8645ebfac05405fce81fa43446acd9; runtime unchanged. Separate CodeQL check 115132119008 completed SUCCESS with No new alerts in code changed and zero annotations; Analyze 115131867787 completed SUCCESS. Hosted Tests run 38347397038 at 74df FAILED: 34/35 Jest suites, 333/340 tests, seven unchanged live-network Electrum failures; recovery/version/release checks SKIPPED. A test-only deterministic Electrum fixture correction is in progress with no runtime/dependency change; no future pass, current-head combined CI or external signoff is inferred | +| Source-specific CI | Source #7042 at 163cccb831f824b3daacc05199421683abdaf1ad passed hosted Tests/CodeQL: 341 Jest, 210 recovery, 12 JavaScript + 7 Python release checks and version 2.6.3. Changes are test-only fixtures/AES assertion/CI trigger; runtime d280 unchanged. Prior 74df and 28c7 failures remain source-specific history below; these results are not combined R4 or native signoff | | Independent agent fixture review | PASS architecture/evidence review after strengthening missing full-signers/nodes restore assertions; no external final-source developer signoff implied | -## Preserved earlier October checkpoint +## Preserved earlier October checkpoints The earlier source checkpoint remains evidence for its own tested source: genuine 253/253, zero failures/skips, 162.1566 seconds at app efa6f89ebf7140f738f03ce8a7218a2a1e7f9bc2 and the same pinned Relay, terminal recorded 2026-10-13 04:12 UTC. Earlier recovery 430/430, zero failures/skips, 47.9 seconds was run on 0f37506a58e6bf44061dd6788b20e5fa095a10c6 after the c308aa405 session guards. The coordinator's earlier Jest report was 41 suites / 393 tests PASS. These are historical October results, superseded for the current source by the refreshed counts above; no result is retroactively transferred to a new SHA. September evidence files are unchanged. +The later a243b53b5682a276eb88851c3d34379c0908fedd source checkpoint passed genuine 253/253, zero failures/skips, 195.046 seconds at 04:40 UTC; recovery 434/434 in 67.140 seconds and Jest 42 suites / 432 tests in 72.836 seconds. Hosted combined head 2071acbdcc572de9988d41f894b7356618bc14be then passed Tests run 38348159148 (genuine 253 with zero skips, recovery 434, Jest 42/432) and CodeQL check 115134694738 SUCCESS. Its whole lint CLI nevertheless failed with the 5353 problems stated above. That checkpoint preceded the rehydrated-Verified proof fix and cannot prove the new source or native cold launch. + +Source #7042 history is separate: trigger correction 74df55446d8645ebfac05405fce81fa43446acd9 had separate CodeQL/Analyze SUCCESS but Tests run 38347397038 failed 34/35 Jest suites, 333/340 tests (seven unchanged live-network Electrum failures); recovery/version/release were skipped. The intervening 28c7 test-fixture attempt also remained a failed source-PR checkpoint. Final test-only 163cccb831f824b3daacc05199421683abdaf1ad corrected fixtures/AES assertion/trigger and passed its own hosted checks with runtime d280 unchanged. It is source-specific version 2.6.3 evidence, not the combined R4 app's final-head checks. Subsequent current-head CI outcomes are recorded externally on the PR after the final documentation push. + ## Open gates IOS and ANDROID exact-source build/launch, automatic create/edit/delete/relaunch and complete Recovery Key restore safety matrix: **NOT RUN / NEEDS_LOCAL**. Host SDK close/reopen is not proof of process restart, persisted MMKV pending state, background execution, UI layout, enlarged text, memory or native 1/10/50-wallet profiling. Physical-only follow-up needs a separately recorded owner decision; it cannot waive critical native checks. From d25d5660ac72443952ae1f7899cf456b5991c805 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Tue, 13 Oct 2026 05:32:35 +0000 Subject: [PATCH 190/254] Clarify the six persistence cases and one source display case --- .../fix-recovery-backup-freshness/automatic-write-validation.md | 2 +- .../evidence/automatic-backup-source-proof-2026-10-13.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/openspec/changes/fix-recovery-backup-freshness/automatic-write-validation.md b/openspec/changes/fix-recovery-backup-freshness/automatic-write-validation.md index c50a5dcccc..f7935df181 100644 --- a/openspec/changes/fix-recovery-backup-freshness/automatic-write-validation.md +++ b/openspec/changes/fix-recovery-backup-freshness/automatic-write-validation.md @@ -72,7 +72,7 @@ Most operation mutation callbacks in the core matrix are constructed directly ov | Final verification race | PASS native write immediately before actual Redux verification dispatch leaves pending; copied/uncommitted proof cannot bypass it | | Account change after send | PASS added after-send A→B→A for all seven operations, with a distinct B seed/account/publicId and independently verified B backup; old A completion leaves B state, native data and remote backup unchanged | | Realm close/reopen | PASS stale queued proof rejects the reopened handle; this is not native app/process restart or MMKV evidence | -| Persisted host rehydration / current-proof display | PASS two actual Redux Persist boot cases and five actual UI AST declaration projection cases over native Realm/HTTP. Saved Verified/interrupted/completed status expires to unverified/pending; consent, confirmation, local records and existing negative findings survive. No replay or upload; only fresh complete readback restores proof | +| Persisted host rehydration / current-proof display | PASS six actual Redux Persist boot/consent/durability cases and one actual UI AST declaration projection case over native Realm/HTTP. Saved Verified/interrupted/completed status expires to unverified/pending; consent, confirmation, local records and existing negative findings survive. Canonical consent wins both read orders and subscriber prefixes; only confirmed absence permits fallback, errors deny it, and late raw legacy fallback preserves continued nested writes. No replay or upload; only fresh complete readback restores proof | | Persisted app interruption/relaunch and background behavior | NOT RUN; mandatory IOS/ANDROID Local checks | | Transactional HTTP contract | PASS actual paired route/service/Mongoose transaction path and disposable source contract checks; hosted artifact parity, topology, limits and old-writer fleet rollout remain unverified | diff --git a/openspec/changes/fix-recovery-backup-freshness/evidence/automatic-backup-source-proof-2026-10-13.md b/openspec/changes/fix-recovery-backup-freshness/evidence/automatic-backup-source-proof-2026-10-13.md index c19f48c5c4..97406bb7c8 100644 --- a/openspec/changes/fix-recovery-backup-freshness/evidence/automatic-backup-source-proof-2026-10-13.md +++ b/openspec/changes/fix-recovery-backup-freshness/evidence/automatic-backup-source-proof-2026-10-13.md @@ -27,7 +27,7 @@ Final combined command outcome/time: **PASS 260/260, 0 failures, 0 skips**, exit The fixture loads actual production Realm wrappers and schemas, snapshot/sanitization/AES, backup automatic coordinator/transport, real Redux reducers, extracted production account-origin middleware and store observers. The paired server runs real HTTP through extracted production routes, production services and Mongoose transactions in a disposable keeperQA replica set. Protocol fault interception is explicit: malformed/old/missing/false ACK, 404, 409, lost or delayed reply and corrupt readback. Negative tests assert zero unauthorized effects or stopped fanout, retained local data, and pending/unverified status. They never manufacture an operation receipt, generation or baseline. -Core mutation callbacks are often built directly over the production commit API; the five producer suites and selected BHR cases additionally execute actual source functions. Host platform/device-info, MMKV persistence adapter, unused RSA and notification helpers are disclosed host boundaries. The seven new cases comprise two actual Redux Persist boot/reconciliation cases and five actual UI AST declaration projection cases over native Realm/HTTP proof. They assert saved Verified/interrupted/completed flags become unverified/pending without changing consent, physical confirmation or local records; existing negative findings survive. Rehydration sends no Relay read, repair or write and grants no usable write intent. A fresh complete encrypted HTTP inspection alone restores current proof, and a new post-inspection operation alone may upload. The display checks project actual source declarations; no rendered React Native UI, native MMKV, full app boot, UI navigation or physical signing is attested. +Core mutation callbacks are often built directly over the production commit API; the five producer suites and selected BHR cases additionally execute actual source functions. Host platform/device-info, MMKV persistence adapter, unused RSA and notification helpers are disclosed host boundaries. The seven new cases comprise six actual Redux Persist boot/consent/durability cases and one actual UI AST declaration projection case over native Realm/HTTP proof. They assert saved Verified/interrupted/completed flags become unverified/pending without changing consent, physical confirmation or local records; existing negative findings survive. Canonical nested opt-out wins both read orders and every subscriber prefix; legacy fallback requires confirmed absence, a read error denies it, and absence-first legacy fallback retains live persistence metadata so later consent/pending changes really flush. Rehydration sends no Relay read, repair or write and grants no usable write intent. A fresh complete encrypted HTTP inspection alone restores current proof, and a new post-inspection operation alone may upload. The display checks project actual source declarations; no rendered React Native UI, native MMKV, full app boot, UI navigation or physical signing is attested. Clean restoration uses real BIP39/BIP32/BIP85/AES and actual getAppImageWorker/recoverApp/writeRecoveredObject into a blank encrypted Realm, reading from the actual HTTP Relay. The dataset includes BTC MAINNET and TESTNET, retained DEFAULT and IMPORTED USDT key/address/history metadata, Recovery Key and other signer data, active/archived vaults, registrations, labels and both-network nodes. Whole recovery-image comparison, complete signer fields and normalized nodes are asserted. Initial database opening is a real dbManager call, outside the native passcode/UI flow. No new USDT setup/import, provider transfer or consent opt-in is enabled by this test. From de3c63bc1d1b10b63e693b63706c070f7a5e0b9e Mon Sep 17 00:00:00 2001 From: Anant Date: Tue, 13 Oct 2026 14:19:52 +0530 Subject: [PATCH 191/254] fix: complete R4 local login and paired Relay validation setup --- .github/workflows/test.yml | 2 +- ios/Podfile.lock | 2 +- .../automatic-write-validation.md | 5 +- patches/react-native+0.83.9.patch | 9 ++ release/r4-automatic-backup-local-handoff.md | 20 +++-- scripts/run-automatic-backup-genuine.cjs | 2 +- src/store/sagas/login.ts | 19 +++-- tests/recovery/loginOriginSession.test.cjs | 83 +++++++++++++++++-- 8 files changed, 121 insertions(+), 21 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f001fecfe3..dcc864e9df 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -101,7 +101,7 @@ jobs: uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4 with: repository: KeeperCommunity/Relay - ref: b600416e5ba7325927d7d2871cff7776f4e593fe + ref: 838049e537e07eb646a2a0393eb969fba43d0739 path: .ci-relay persist-credentials: false diff --git a/ios/Podfile.lock b/ios/Podfile.lock index c0321033e6..f53f6d2a91 100644 --- a/ios/Podfile.lock +++ b/ios/Podfile.lock @@ -3986,7 +3986,7 @@ SPEC CHECKSUMS: glog: 5683914934d5b6e4240e497e0f4a3b42d1854183 GoogleDataTransport: aae35b7ea0c09004c3797d53c8c41f66f219d6a7 GoogleUtilities: 00c88b9a86066ef77f0da2fab05f65d7768ed8e1 - hermes-engine: 4ec27e7c84ed7950da6e65008539685455d8bef4 + hermes-engine: 06d1c4969de42f925f6614a3c2e049b3a000331d HtmlToPdf: 5c0649e93aa1c6583f400268eedea00f15e0fa5f libportal-ios: d9aa55474e2d5be8e38e96345dd37be34fda45b4 libportal-react-native: 05778ef04321517c8366dc7173d05aa9fe131ff2 diff --git a/openspec/changes/fix-recovery-backup-freshness/automatic-write-validation.md b/openspec/changes/fix-recovery-backup-freshness/automatic-write-validation.md index f7935df181..c64949c5c1 100644 --- a/openspec/changes/fix-recovery-backup-freshness/automatic-write-validation.md +++ b/openspec/changes/fix-recovery-backup-freshness/automatic-write-validation.md @@ -1,6 +1,6 @@ # R4 automatic Assisted Backup: result matrix -Updated 13 October 2026 from observed source/disposable evidence. Historical September logs remain unchanged in [validation.md](validation.md). The [sanitized October proof record](evidence/automatic-backup-source-proof-2026-10-13.md) records commands, source identities, counts and coverage boundaries. +Updated 13 October 2026 from observed source/disposable evidence. Historical September logs remain unchanged in [validation.md](validation.md). The [sanitized October proof record](evidence/automatic-backup-source-proof-2026-10-13.md) records commands, source identities, counts and coverage boundaries. The table below retains the original Cloud source-proof result; a later exact-head Codex Local report on PR #7045 records native results and any new app SHA. Automatic-backup implementation and disposable proof are mandatory R4 work. Critical IOS/ANDROID backup acceptance also precedes release. Host-native Realm means the real Node Realm SDK, not an iOS/Android app launch. Physical-device-only follow-up cannot waive critical simulator/emulator safety checks. @@ -10,7 +10,8 @@ Automatic-backup implementation and disposable proof are mandatory R4 work. Crit | --- | --- | | Audit starting app source | b160656218dc6065ad12af9db220c974255bfc23; historical | | Final tested code commit before documentation | 3cc9368ca2f3b2149db5743a5f9d230aa516d6a0; includes persisted-proof expiration, current-proof status projection and seven new genuine cases | -| Tested Relay fixture commit | b600416e5ba7325927d7d2871cff7776f4e593fe; runner requires clean tracked source | +| Original tested Relay fixture commit | b600416e5ba7325927d7d2871cff7776f4e593fe; runner requires clean tracked source. Native Android later exposed an incomplete repair readback on this fixture | +| Current paired disposable Relay source | 838049e537e07eb646a2a0393eb969fba43d0739 in draft Relay #4 fixes subscription metadata on an existing AppImage during explicit revisioned repair. Focused route regression RED 0/1 on b600 and GREEN 1/1 on 838; full HTTP 58/58, backup suite 120/120 and Relay TypeScript PASS. This is loopback evidence, not deployed Relay readiness or final exact-SHA app signoff | | Runtime and topology | Node 22.23.3, actual Realm 12.15.0, Linux host, disposable loopback keeperQA Mongo replica set on port 27058; no hosted credentials | | Combined genuine fixture command/result | `npm run test:automatic-backup-genuine`: PASS 260/260, 0 failures/skips, 208.779 seconds, clean frozen-Yarn run terminal recorded 2026-10-13 05:26 UTC; core 213 + producers 46 + restore 1 | | Recovery unit command/result | `npm run test:recovery`: 443/443 PASS, 0 failures/skips, 52.156 seconds at 3cc9368ca2f3b2149db5743a5f9d230aa516d6a0 | diff --git a/patches/react-native+0.83.9.patch b/patches/react-native+0.83.9.patch index 6eeb009e8a..a0957763e8 100644 --- a/patches/react-native+0.83.9.patch +++ b/patches/react-native+0.83.9.patch @@ -117,3 +117,12 @@ index 2ee2c50..6fbfe6f 100644 [subviews insertObject:subview atIndex:atIndex]; } +diff --git a/node_modules/react-native/sdks/hermes-engine/hermes-engine.podspec b/node_modules/react-native/sdks/hermes-engine/hermes-engine.podspec +index fb99142..3f5cbb6 100644 +--- a/node_modules/react-native/sdks/hermes-engine/hermes-engine.podspec ++++ b/node_modules/react-native/sdks/hermes-engine/hermes-engine.podspec +@@ -78,3 +78,3 @@ Pod::Spec.new do |spec| + spec.user_target_xcconfig = { +- 'HERMES_CLI_PATH' => "#{hermes_compiler_path}/hermesc/osx-bin/hermesc" ++ 'HERMES_CLI_PATH' => '${PODS_ROOT}/../../node_modules/hermes-compiler/hermesc/osx-bin/hermesc' + } diff --git a/release/r4-automatic-backup-local-handoff.md b/release/r4-automatic-backup-local-handoff.md index 37d6922e51..9f37a769da 100644 --- a/release/r4-automatic-backup-local-handoff.md +++ b/release/r4-automatic-backup-local-handoff.md @@ -2,7 +2,7 @@ State: **NEEDS_LOCAL**. PR [#7045](https://github.com/KeeperCommunity/bitcoin-keeper/pull/7045), branch `codex/r4-264-combined-candidate`, base `sprint`. The exact final SHA is recorded in the newest **Codex local handoff** on that PR. The setup below verifies that handoff against the live head before selecting it; this repository document cannot contain its own eventual commit SHA. -Paired disposable Relay: **`b600416e5ba7325927d7d2871cff7776f4e593fe`**, draft [Relay #4](https://github.com/KeeperCommunity/Relay/pull/4). This pin supersedes the earlier `5a42a03` fixture by adding explicit malformed-acknowledgement response overrides after actual commits. It is a loopback test dependency, not a deployed-service assertion. The approved contributor manifest in `dev/local-backend/sources.lock.json` remains Relay `ee935359f22ff7932ea0a62e9084d4b49801f4aa` and SigningServer `3cc7728c27328e7b0358d1f91a32d8d13feba90a`; do not replace those pins or overwrite generated contributor checkouts to prepare this test. +Paired disposable Relay: **`838049e537e07eb646a2a0393eb969fba43d0739`**, draft [Relay #4](https://github.com/KeeperCommunity/Relay/pull/4). The originally requested `b600416e5ba7325927d7d2871cff7776f4e593fe` was tested: native Android opt-in reached a real repair write but failed complete readback because `createNewApp` had precreated an AppImage without subscription and repair wrote that field only on insert. The successor sets subscription on existing AppImages during explicit revisioned repair, after the transaction's revision and conflict checks. It is a loopback test dependency, not a deployed-service assertion. The approved contributor manifest in `dev/local-backend/sources.lock.json` remains Relay `ee935359f22ff7932ea0a62e9084d4b49801f4aa` and SigningServer `3cc7728c27328e7b0358d1f91a32d8d13feba90a`; do not replace those pins or overwrite generated contributor checkouts to prepare this test. `AUTOMATIC_BACKUP_SOURCE_READY` is true after genuine disposable proof and independent fixture review. It permits development loopback validation with an explicit port. `AUTOMATIC_BACKUP_RELAY_ROLLOUT_READY` remains false until the separately authorized deployed fleet, transactions/indexes, v2 route and old-writer drainage are verified. The Node fixture permits its explicit loopback test target. Hosted writes remain fenced. Do not change either gate to make a test pass. Current source and results are recorded in [the proof matrix](../openspec/changes/fix-recovery-backup-freshness/automatic-write-validation.md). @@ -22,7 +22,7 @@ report the mismatch and obtain the current handoff rather than testing stale cod Preserve every existing checkout, installed owner app, wallet and database. Use new isolated checkouts, simulator/emulator state and disposable test accounts. -Use paired public Relay b600416e5ba7325927d7d2871cff7776f4e593fe separately from +Use paired public Relay 838049e537e07eb646a2a0393eb969fba43d0739 separately from the approved contributor bootstrap. Do not edit its source or replace the approved sources.lock.json pins. Save exact source identities and private logs. @@ -59,9 +59,16 @@ Select the existing Node **22** toolchain; use Yarn Classic **1.22.22**. Do not ```bash set -euo pipefail umask 077 -keeper_app_sha=$(gh pr view 7045 --repo KeeperCommunity/bitcoin-keeper --json headRefOid,body | - python3 -c 'import json,re,sys; p=json.load(sys.stdin); h=re.findall(r"(?m)^Head commit:\s*([0-9a-f]{40})\s*$",p["body"]); assert h and h[-1]==p["headRefOid"], "Missing or stale exact-head Codex local handoff"; print(h[-1])') -keeper_relay_sha=b600416e5ba7325927d7d2871cff7776f4e593fe +keeper_app_sha=$(gh pr view 7045 --repo KeeperCommunity/bitcoin-keeper --json headRefOid | + python3 -c 'import json,sys; print(json.load(sys.stdin)["headRefOid"])') +keeper_handoff_sha=$(gh api --paginate --slurp \ + 'repos/KeeperCommunity/bitcoin-keeper/issues/7045/comments?per_page=100' | + python3 -c 'import json,re,sys; pages=json.load(sys.stdin); comments=[c for page in pages for c in page]; handoffs=[c["body"] for c in comments if c.get("body", "").startswith("## Codex local handoff")]; assert handoffs, "Missing Codex local handoff"; matches=re.findall(r"(?m)^Head commit:\s*([0-9a-f]{40})\s*$", handoffs[-1]); assert matches, "Missing handoff SHA"; print(matches[-1])') +test "$keeper_app_sha" = "$keeper_handoff_sha" || { + printf 'Live PR head and newest Codex local handoff differ.\n' >&2 + exit 1 +} +keeper_relay_sha=838049e537e07eb646a2a0393eb969fba43d0739 test "${#keeper_app_sha}" -eq 40 test "$(node -p 'process.versions.node.split(".")[0]')" = 22 keeper_qa_root=$(mktemp -d "${TMPDIR:-/tmp}/keeper-r4-local.XXXXXX") @@ -83,6 +90,7 @@ git -C "$keeper_relay_dir" diff --quiet HEAD cd "$keeper_app_dir" yarn install --frozen-lockfile --ignore-scripts yarn patch-package +./node_modules/.bin/rn-nodeify --install buffer,events,process,stream,inherits,path,assert,crypto --hack --yarn (cd node_modules/realm && ../.bin/prebuild-install --runtime napi --target 6) (cd "$keeper_relay_dir" && yarn install --frozen-lockfile --non-interactive --ignore-scripts) export REALM_DISABLE_ANALYTICS=1 @@ -199,7 +207,7 @@ xcrun simctl install "$keeper_ios_udid" ios/build/r4-local/Build/Products/Debug- xcrun simctl launch "$keeper_ios_udid" io.hexawallet.hexakeeper.dev ``` -An unsigned compile is not launch/entitlement evidence. If the simulator refuses that artifact because of local signing/entitlements, record the exact outcome; use the contributor README's simulator-only Debug build with its normal local/ad-hoc signing for launch if available without distribution credentials. Record both builds and which artifact actually launched. This is not production/release signing, and it does not make the unsigned artifact a launched artifact. +An unsigned compile is not functional entitlement evidence. The unsigned app may launch but Keychain can reject PIN storage with OSStatus -34018; a passcode screen alone does not establish onboarding or Realm persistence. Inspect the effective signed entitlements and actual PIN/Realm outcome. If signing or launch blocks this flow, record the exact action and error; use the contributor README's simulator-only Debug build with its normal local development signing if available without distribution credentials. A manually re-signed bundle may itself fail simulator launch policy. Record each artifact and which one actually launched. This is not production/release signing, and it does not turn an unsigned or nonfunctional launch into native acceptance. For Android, select a disposable emulator serial and its architecture. Use JDK **17**, SDK **36**, build tools **35.0.0**, NDK **27.1.12297006** and CMake **3.22.1** as required by this source. The development artifact uses the checked-in disposable debug certificate, not a production signing key. diff --git a/scripts/run-automatic-backup-genuine.cjs b/scripts/run-automatic-backup-genuine.cjs index bf5d4518d4..a72bf4cc0d 100644 --- a/scripts/run-automatic-backup-genuine.cjs +++ b/scripts/run-automatic-backup-genuine.cjs @@ -6,7 +6,7 @@ const os = require('node:os'); const path = require('node:path'); const { spawnSync } = require('node:child_process'); -const relaySha = 'b600416e5ba7325927d7d2871cff7776f4e593fe'; +const relaySha = '838049e537e07eb646a2a0393eb969fba43d0739'; const root = path.resolve(__dirname, '..'); const fixture = process.env.KEEPER_RELAY_FIXTURE_MODULE; assert.equal(process.versions.node.split('.')[0], '22', 'Run the genuine automatic backup matrix with Node 22'); diff --git a/src/store/sagas/login.ts b/src/store/sagas/login.ts index 52b03c10de..22b074b5de 100644 --- a/src/store/sagas/login.ts +++ b/src/store/sagas/login.ts @@ -203,6 +203,8 @@ function* credentialsAuthWorker({ payload }) { // setting correct app id from realm at login const keeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); if (originSession !== currentBackupAccountSession()) return; + if (currentAccount && !keeperApp?.id) + throw new Error('Saved account has no KeeperApp record'); if (keeperApp?.id) { const onlyKnownAccount = allAccounts.length <= 1 && @@ -223,7 +225,8 @@ function* credentialsAuthWorker({ payload }) { ? captureBackupWriteIntent(keeperApp.id) : undefined; loginSession = loginAccountSession; - if (store.getState().storage.appId !== keeperApp?.id) return; + const expectedAppId = keeperApp?.id ?? ''; + if (store.getState().storage.appId !== expectedAppId) return; // Store temporary account details if (!allAccounts.length) { @@ -236,8 +239,11 @@ function* credentialsAuthWorker({ payload }) { const lastElement = versionCollection[versionCollection.length - 1]; const lastVersionCode = lastElement.version.split(/[()]/); const currentVersionCode = DeviceInfo.getBuildNumber(); - if (currentBackupAccountSession() !== loginAccountSession || - store.getState().storage.appId !== keeperApp?.id) return; + if ( + currentBackupAccountSession() !== loginAccountSession || + store.getState().storage.appId !== expectedAppId + ) + return; if (semver.lt(previousVersion, newVersion)) { yield call(applyUpgradeSequence, { previousVersion, @@ -252,8 +258,11 @@ function* credentialsAuthWorker({ payload }) { title: `Upgraded from ${lastVersionCode[1]} to ${currentVersionCode}`, }); } - if (currentBackupAccountSession() !== loginAccountSession || - store.getState().storage.appId !== keeperApp?.id) return; + if ( + currentBackupAccountSession() !== loginAccountSession || + store.getState().storage.appId !== expectedAppId + ) + return; if (appId) { try { const { id }: KeeperApp = yield call( diff --git a/tests/recovery/loginOriginSession.test.cjs b/tests/recovery/loginOriginSession.test.cjs index 87d1149292..424469c63a 100644 --- a/tests/recovery/loginOriginSession.test.cjs +++ b/tests/recovery/loginOriginSession.test.cjs @@ -30,11 +30,23 @@ function deferred() { return { promise, resolve }; } -function fixture(pauseAt) { +function fixture(pauseAt, options = {}) { + const { + persistedAppId = 'A', + keeperApp = { id: 'A' }, + allAccounts = [ + { + appId: 'A', + hash: 'disposable-hash', + realmId: 'A.realm', + }, + ], + appVersion = '2.5.0', + } = options; const state = { - storage: { appId: 'A', appVersion: '2.5.0', defaultWalletCreated: {} }, + storage: { appId: persistedAppId, appVersion, defaultWalletCreated: {} }, account: { - allAccounts: [{ appId: 'A', hash: 'disposable-hash', realmId: 'A.realm' }], + allAccounts, backupMethodByAppId: {}, }, }; @@ -63,10 +75,11 @@ function fixture(pauseAt) { realmOpens.push(realmId); return pauseAt === 'realm' ? gate({ success: true }) : { success: true }; }, - getObjectByIndex: () => ({ id: 'A' }), - getCollection: () => [{ version: '2.5.0(1)' }], + getObjectByIndex: () => keeperApp, + getCollection: () => [{ version: `${appVersion}(2)` }], }, RealmSchema: { KeeperApp: 'KeeperApp', VersionHistory: 'VersionHistory' }, + REALM_FILE: 'keeper.realm', store: { getState: () => state }, DeviceInfo: { getVersion: () => '2.5.16', getBuildNumber: () => '2' }, semver: require('semver'), @@ -85,6 +98,7 @@ function fixture(pauseAt) { 'setPinHash', 'migrateLegacyAutomaticCloudBackup', 'setAppId', + 'setTempDetails', 'credsAuthenticated', ]) scope[name] = (payload) => ({ type: name, payload }); @@ -133,3 +147,62 @@ test('old login cannot start upgrade after A -> B -> A during Realm open', async ); assert.equal(f.upgradeCalls.length, 0); }); + +test('fresh account without KeeperApp completes login when persisted app ID is empty', async () => { + const f = fixture(null, { + persistedAppId: '', + keeperApp: null, + allAccounts: [], + appVersion: '2.5.16', + }); + await f.run(); + assert.equal(f.realmOpens.length, 1); + assert.equal( + f.actions.some((action) => action.type === 'setTempDetails'), + true + ); + assert.equal( + f.actions.some((action) => action.type === 'credsAuthenticated' && action.payload === true), + true + ); + assert.equal(f.upgradeCalls.length, 0); +}); + +test('fresh account login rejects a stale nonempty persisted app ID', async () => { + const f = fixture(null, { + persistedAppId: 'STALE', + keeperApp: null, + allAccounts: [], + appVersion: '2.5.16', + }); + await f.run(); + assert.equal(f.realmOpens.length, 1); + assert.equal( + f.actions.some((action) => action.type === 'setTempDetails'), + false + ); + assert.equal( + f.actions.some((action) => action.type === 'credsAuthenticated'), + false + ); + assert.equal(f.upgradeCalls.length, 0); +}); + +test('known account with missing KeeperApp fails authentication', async () => { + const f = fixture(null, { + persistedAppId: '', + keeperApp: null, + appVersion: '2.5.16', + }); + await f.run(); + assert.equal(f.realmOpens.length, 1); + assert.equal( + f.actions.some((action) => action.type === 'credsAuthenticated' && action.payload === true), + false + ); + assert.equal( + f.actions.some((action) => action.type === 'credsAuthenticated' && action.payload === false), + true + ); + assert.equal(f.upgradeCalls.length, 0); +}); From fb9ae0d4cc49680538202eca256addc86823bdd9 Mon Sep 17 00:00:00 2001 From: Anant Date: Tue, 13 Oct 2026 15:29:00 +0530 Subject: [PATCH 192/254] Fix managed wallet visibility before backup deletion --- release/r4-automatic-backup-local-handoff.md | 1 + src/screens/AppSettings/ManageWallets.tsx | 74 ++++++++++++++---- src/screens/Vault/VaultSettings.tsx | 36 ++++++--- src/screens/WalletDetails/WalletSettings.tsx | 37 +++++---- src/store/sagaActions/wallets.ts | 13 ++++ src/store/sagas/index.ts | 2 + src/store/sagas/wallets.ts | 70 +++++++++++++++++ src/store/store.ts | 1 + .../automaticWalletProducer.integration.cjs | 76 ++++++++++++++++++- 9 files changed, 268 insertions(+), 42 deletions(-) diff --git a/release/r4-automatic-backup-local-handoff.md b/release/r4-automatic-backup-local-handoff.md index 9f37a769da..3c33b517d1 100644 --- a/release/r4-automatic-backup-local-handoff.md +++ b/release/r4-automatic-backup-local-handoff.md @@ -233,6 +233,7 @@ On **both** platforms, record each scenario separately: 1. Fresh disposable onboarding, PIN create/unlock/cooldown, always-visible disabled Proceed/queued presentation and account setup. Cold launch/unlock without pressing a key must retain every keypad digit; disabled cooldown must reject input, retain all digits, expire normally and then accept input. Exercise PIN creation and another shared-keypad consumer without shortening cooldown in production code. 2. Opt in per account, create/edit/delete wallets, signers and vaults locally, archive and register them, edit/import/delete labels, change nodes and version-related recovery state, then relaunch. Check actual local persistence, pending/verified presentation, failure-stage/retry copy and complete native clean Recovery Key restore into a **different disposable Realm**, through the production restore worker. Never restore over the owner's existing app/database. After a verified backup, terminate and cold-launch the disposable app with the next snapshot delayed or unavailable. Open backup status immediately: saved verification and interrupted phases must be unverified/pending before a fresh complete read establishes current proof. Consent and committed local records must survive; no prepared ciphertext may replay. Repeat after a failed partial fanout and A→B→A: a historical phase must not display Verified after its session proof expires. With disposable native storage, cover canonical nested opt-out versus stale root opt-in in both read orders, hold the canonical read and check intermediate states, distinguish confirmed absence from read error, and restore an older root BHR record without nested persistence metadata. Then change consent/pending state and cold-launch again to prove subsequent writes persist. Record native MMKV persistence and actual process restart separately from host Redux-persist rehydration and source-expression tests. + For the normal Manage Wallets Hide → Delete path, confirm an opted-in BTC wallet or vault Hide commits locally and receives its own revisioned mutation and complete readback before Delete is offered. While Hide is pending or its response is lost, the local hidden state remains, Delete stays unavailable and no stale deletion is sent. After verified Hide, PIN-confirmed Delete must remove the exact remote entity with another complete readback. Consent-off local deletion follows its separate no-write rule. 3. Repeat C1–C10 through feasible real UI paths with the paired v2 service: interruptions/large payloads, 409/old route/malformed/lost reply, local mutation during verification, A→B→A, consent change during deletion, missing/orphaned/legacy encrypted references and multi-write partial success. Confirm server-only records and immutable identities survive. A successful Delete Backup retains local data, acknowledges empty readback, clears only the intended consent and never displays a verified backup. 4. On iOS, import identical harmless bracketed/unbracketed TXT files and valid `[Test]_backup.bsms` / `Test_backup.bsms` from the same Files provider. Both BSMS files must reach identical configuration review; stop before duplicate creation. Test filenames with spaces/literal percent escapes and cancellation. Smoke Android's unchanged content-URI path on a native file picker. 5. Fresh-install Testnet tutorial → Receive Test Sats → visible wallet balance **without restarting**, including retry/slow response and displayed-account ownership. Local mode intentionally lacks a faucet/AI provider: a 503 or mocked unit result is not positive end-to-end evidence. Use an independently approved disposable Testnet-provider QA configuration when available; otherwise record the scenario's actual BLOCKED/NOT RUN status and explicit owner follow-up, without waiving critical backup or required platform checks. diff --git a/src/screens/AppSettings/ManageWallets.tsx b/src/screens/AppSettings/ManageWallets.tsx index 58429ca426..0fe9f6f0ee 100644 --- a/src/screens/AppSettings/ManageWallets.tsx +++ b/src/screens/AppSettings/ManageWallets.tsx @@ -50,6 +50,10 @@ import WalletHeader from 'src/components/WalletHeader'; import ThemedColor from 'src/components/ThemedColor/ThemedColor'; import USDTGreenLogo from 'src/assets/images/usdt-gree-logo.svg'; import { currentBackupAccountSession } from 'src/services/backup/transport'; +import { isBackupVerificationCurrent } from 'src/services/backup/automatic'; +import { isAutomaticCloudBackupEnabled } from 'src/store/reducers/bhr'; +import { store } from 'src/store/store'; +import { updateRecoveryVisibility } from 'src/store/sagaActions/wallets'; enum PasswordMode { DEFAULT = 'DEFAULT', @@ -59,6 +63,19 @@ enum PasswordMode { const activeRealmAppId = (): string | undefined => (dbManager.getObjectByIndex(RealmSchema.KeeperApp) as unknown as { id: string } | undefined)?.id; +type ListItemProps = { + title: string; + subtitle: string; + balance: number; + visibilityToggle: () => void; + isHidden: boolean; + onDelete: () => void; + icon: React.ReactNode; + type: EntityKind; + isWatchOnly: boolean; + canDelete: boolean; +}; + function ListItem({ title, subtitle, @@ -69,7 +86,8 @@ function ListItem({ icon, type, isWatchOnly, -}) { + canDelete, +}: ListItemProps) { const { colorMode } = useColorMode(); const isDarkMode = colorMode === 'dark'; const { getSatUnit, getBalance, getCurrencyIcon } = useBalance(); @@ -78,6 +96,8 @@ function ListItem({ const HexagonIconColor = ThemedColor({ name: 'HexagonIcon' }); const isUSDTWallet = type === EntityKind.USDT_WALLET; + const deleteEligible = type === EntityKind.VAULT || + (type === EntityKind.WALLET && (balance === 0 || isWatchOnly)) || isUSDTWallet; return ( - {isHidden && - (type == 'VAULT' || - (type == 'WALLET' && (balance === 0 || isWatchOnly)) || - isUSDTWallet) && ( + {isHidden && deleteEligible && canDelete && ( } /> )} + {isHidden && deleteEligible && !canDelete && !isUSDTWallet && ( + + Verify assisted backup before deleting + + )} state.bhr); const [selectedWallet, setSelectedWallet] = useState(null); const { showToast } = useToastMessage(); const miniscriptPathSelectorRef = useRef(null); @@ -180,7 +205,6 @@ function ManageWallets() { const onProceed = () => { if (passwordMode === PasswordMode.DEFAULT) { updateWalletVisibility(selectedWallet, false); - showToast(errorText.walletUnhidden, ); } if (passwordMode === PasswordMode.SHOWALL) { setshowAll(true); @@ -196,6 +220,10 @@ function ManageWallets() { }; const deleteSelectedEntity = async () => { + if (selectedWallet && !canDeleteRecoveryEntity(selectedWallet)) { + showToast('Verify your assisted backup before deleting', ); + return; + } if (selectedWallet && selectedWallet.entityKind === EntityKind.VAULT) { const vaultId = selectedWallet.id; const appId = activeRealmAppId(); @@ -242,6 +270,14 @@ function ManageWallets() { } }; + const canDeleteRecoveryEntity = (wallet: Wallet | Vault | USDTWallet): boolean => { + if (wallet.entityKind === EntityKind.USDT_WALLET) return true; + const appId = activeRealmAppId(); + const state = store.getState(); + return !!appId && state.storage.appId === appId && + (!isAutomaticCloudBackupEnabled(state.bhr, appId) || isBackupVerificationCurrent(appId)); + }; + const updateWalletVisibility = async (wallet: Wallet | Vault | USDTWallet, hide: boolean) => { const { id, entityKind } = wallet; const isWallet = entityKind === EntityKind.WALLET; @@ -261,17 +297,18 @@ function ManageWallets() { visibility: visibilityType, }, }; - await updateUSDTWallet(updatedUSDTWallet); + const updated = await updateUSDTWallet(updatedUSDTWallet); + if (!updated) showToast('Failed to update wallet visibility', ); + else if (!hide) showToast(errorText.walletUnhidden, ); } else { - // case: regular wallet/vault update - const schema = isWallet ? RealmSchema.Wallet : RealmSchema.Vault; - dbManager.updateObjectById(schema, id, { - presentationData: { - name: wallet.presentationData.name, - description: wallet.presentationData.description, - visibility: visibilityType, - }, - }); + const appId = activeRealmAppId(); + const session = currentBackupAccountSession(); + dispatch(updateRecoveryVisibility(id, isWallet ? EntityKind.WALLET : EntityKind.VAULT, + visibilityType, (updated) => { + if (activeRealmAppId() !== appId || currentBackupAccountSession() !== session) return; + if (!updated) showToast('Failed to update wallet visibility', ); + else if (!hide) showToast(errorText.walletUnhidden, ); + })); } } catch (error) { captureError(error); @@ -423,6 +460,7 @@ function ManageWallets() { balance={getWalletBalance(item)} isHidden={item.presentationData.visibility === VisibilityType.HIDDEN} isWatchOnly={isWatchOnly(item)} + canDelete={canDeleteRecoveryEntity(item)} visibilityToggle={() => { setSelectedWallet(item); if (item.presentationData.visibility === VisibilityType.HIDDEN) { @@ -433,6 +471,10 @@ function ManageWallets() { } }} onDelete={() => { + if (!canDeleteRecoveryEntity(item)) { + showToast('Verify your assisted backup before deleting', ); + return; + } if (getWalletBalance(item) > 0 && !isWatchOnly(item)) { setSelectedWallet(item); setShowDeleteVaultBalanceAlert(true); diff --git a/src/screens/Vault/VaultSettings.tsx b/src/screens/Vault/VaultSettings.tsx index 49b37e43e9..802cb65c42 100644 --- a/src/screens/Vault/VaultSettings.tsx +++ b/src/screens/Vault/VaultSettings.tsx @@ -32,6 +32,9 @@ import PrivateVaultSetupIcon from 'src/assets/privateImages/vault_setup.svg'; import Text from 'src/components/KeeperText'; import { ConciergeTag } from 'src/models/enums/ConciergeTag'; import ConciergeNeedHelp from 'src/assets/images/conciergeNeedHelp.svg'; +import { useDispatch } from 'react-redux'; +import { updateRecoveryVisibility } from 'src/store/sagaActions/wallets'; +import { currentBackupAccountSession } from 'src/services/backup/transport'; import SettingCard from '../Home/components/Settings/Component/SettingCard'; import EditWalletDetailsModal from '../WalletDetails/EditWalletDetailsModal'; import WalletConfiguration from './components/WalletConfiguration'; @@ -42,10 +45,10 @@ import { NfcTech } from 'react-native-nfc-manager'; import { useQuery } from '@realm/react'; import ImportExportLabels from 'src/components/ImportExportLabels'; import ToastErrorIcon from 'src/assets/images/toast_error.svg'; -import { useSelector } from 'react-redux'; import ThemedSvg from 'src/components/ThemedSvg.tsx/ThemedSvg'; import ThemedColor from 'src/components/ThemedColor/ThemedColor'; function VaultSettings({ route }) { + const dispatch = useDispatch(); const { colorMode } = useColorMode(); const isDarkMode = colorMode === 'dark'; const navigation = useNavigation(); @@ -109,15 +112,28 @@ function VaultSettings({ route }) { const updateWalletVisibility = () => { try { - dbManager.updateObjectById(RealmSchema.Vault, vault.id, { - presentationData: { - name: vault.presentationData.name, - description: vault.presentationData.description, - visibility: VisibilityType.HIDDEN, - }, - }); - showToast(vaultText.vaultHiddenSuccessMessage, , IToastCategory.DEFAULT, 5000); - navigation.navigate('Home'); + const appId = (dbManager.getObjectByIndex(RealmSchema.KeeperApp) as any)?.id; + const backupSession = currentBackupAccountSession(); + dispatch( + updateRecoveryVisibility(vault.id, EntityKind.VAULT, VisibilityType.HIDDEN, (updated) => { + if ( + currentBackupAccountSession() !== backupSession || + (dbManager.getObjectByIndex(RealmSchema.KeeperApp) as any)?.id !== appId + ) + return; + if (!updated) { + showToast(walletText.somethingWentWrong, ); + return; + } + showToast( + vaultText.vaultHiddenSuccessMessage, + , + IToastCategory.DEFAULT, + 5000 + ); + navigation.navigate('Home'); + }) + ); } catch (error) { console.log(error); } diff --git a/src/screens/WalletDetails/WalletSettings.tsx b/src/screens/WalletDetails/WalletSettings.tsx index df16b1542e..736687d1a8 100644 --- a/src/screens/WalletDetails/WalletSettings.tsx +++ b/src/screens/WalletDetails/WalletSettings.tsx @@ -17,6 +17,8 @@ import dbManager from 'src/storage/realm/dbManager'; import { RealmSchema } from 'src/storage/realm/enum'; import { EntityKind, VisibilityType } from 'src/services/wallets/enums'; import { captureError } from 'src/services/sentry'; +import { currentBackupAccountSession } from 'src/services/backup/transport'; +import { refreshWallets, updateRecoveryVisibility } from 'src/store/sagaActions/wallets'; import BackupModalContent from '../AppSettings/BackupModal'; import { credsAuthenticated } from 'src/store/reducers/login'; import { useDispatch } from 'react-redux'; @@ -31,7 +33,6 @@ import ToastErrorIcon from 'src/assets/images/toast_error.svg'; import Instruction from 'src/components/Instruction'; import ThemedSvg from 'src/components/ThemedSvg.tsx/ThemedSvg'; import ThemedColor from 'src/components/ThemedColor/ThemedColor'; -import { refreshWallets } from 'src/store/sagaActions/wallets'; function WalletSettings({ route }) { const { colorMode } = useColorMode(); @@ -67,20 +68,28 @@ function WalletSettings({ route }) { const updateWalletVisibility = () => { try { - const updatedPresentationData = { - ...wallet.presentationData, - visibility: VisibilityType.HIDDEN, - }; - dbManager.updateObjectById(RealmSchema.Wallet, wallet.id, { - presentationData: updatedPresentationData, - }); - showToast( - walletTranslation.walletHiddenSuccessMessage, - , - IToastCategory.DEFAULT, - 5000 + const appId = (dbManager.getObjectByIndex(RealmSchema.KeeperApp) as any)?.id; + const backupSession = currentBackupAccountSession(); + dispatch( + updateRecoveryVisibility(wallet.id, EntityKind.WALLET, VisibilityType.HIDDEN, (updated) => { + if ( + currentBackupAccountSession() !== backupSession || + (dbManager.getObjectByIndex(RealmSchema.KeeperApp) as any)?.id !== appId + ) + return; + if (!updated) { + showToast(walletTranslation.somethingWentWrong); + return; + } + showToast( + walletTranslation.walletHiddenSuccessMessage, + , + IToastCategory.DEFAULT, + 5000 + ); + navigation.navigate('Home'); + }) ); - navigation.navigate('Home'); } catch (error) { captureError(error); showToast(walletTranslation.somethingWentWrong); diff --git a/src/store/sagaActions/wallets.ts b/src/store/sagaActions/wallets.ts index 8e3c45d881..7ab2ef31ea 100644 --- a/src/store/sagaActions/wallets.ts +++ b/src/store/sagaActions/wallets.ts @@ -1,6 +1,7 @@ import { Signer, Vault, VaultSigner } from 'src/services/wallets/interfaces/vault'; import { Wallet } from 'src/services/wallets/interfaces/wallet'; import { SignerRestriction } from 'src/models/interfaces/AssistedKeys'; +import { EntityKind, VisibilityType } from 'src/services/wallets/enums'; import { NewWalletInfo } from '../sagas/wallets'; // types and action creators: dispatched by components and sagas @@ -12,6 +13,7 @@ export const REFRESH_WALLETS = 'REFRESH_WALLETS'; export const TEST_SATS_RECIEVE = 'TEST_SATS_RECIEVE'; export const UPDATE_WALLET_DETAILS = 'UPDATE_WALLET_DETAILS'; export const UPDATE_VAULT_DETAILS = 'UPDATE_VAULT_DETAILS'; +export const UPDATE_RECOVERY_VISIBILITY = 'UPDATE_RECOVERY_VISIBILITY'; export const UPDATE_SIGNER_DETAILS = 'UPDATE_SIGNER_DETAILS'; export const UPDATE_KEY_DETAILS = 'UPDATE_KEY_DETAILS'; export const GENERATE_NEW_ADDRESS = 'GENERATE_NEW_ADDRESS'; @@ -130,6 +132,17 @@ export const updateVaultDetails = ( }, }); +export const updateRecoveryVisibility = ( + entityId: string, + entityKind: EntityKind.WALLET | EntityKind.VAULT, + visibility: VisibilityType, + onLocalComplete?: (updated: boolean) => void +) => ({ + type: UPDATE_RECOVERY_VISIBILITY, + payload: { entityId, entityKind, visibility }, + onLocalComplete, +}); + export const updateSignerDetails = (signer: Signer, key: string, value: any) => ({ type: UPDATE_SIGNER_DETAILS, payload: { diff --git a/src/store/sagas/index.ts b/src/store/sagas/index.ts index 80ce4fc59d..c7d2a677cd 100644 --- a/src/store/sagas/index.ts +++ b/src/store/sagas/index.ts @@ -13,6 +13,7 @@ import { updateSignerDetails, updateKeyDetails, updateVaultDetailsWatcher, + updateRecoveryVisibilityWatcher, deleteVaultyWatcher, reinstateVaultWatcher, refillMobileKeyWatcher, @@ -117,6 +118,7 @@ const rootSaga = function* () { deleteSigningDeviceWatcher, migrateVaultWatcher, updateVaultDetailsWatcher, + updateRecoveryVisibilityWatcher, updateSignerDetails, updateKeyDetails, deleteVaultyWatcher, diff --git a/src/store/sagas/wallets.ts b/src/store/sagas/wallets.ts index ba8dea5f13..35ccb053de 100644 --- a/src/store/sagas/wallets.ts +++ b/src/store/sagas/wallets.ts @@ -101,6 +101,7 @@ import { UPDATE_SIGNER_DETAILS, UPDATE_KEY_DETAILS, UPDATE_VAULT_DETAILS, + UPDATE_RECOVERY_VISIBILITY, GENERATE_NEW_ADDRESS, UPDATED_VAULT_SIGNERS_XPRIV, } from '../sagaActions/wallets'; @@ -1696,6 +1697,75 @@ function* updateVaultDetailsWorker({ payload, originAppId, originBackupAccountSe export const updateVaultDetailsWatcher = createWatcher(updateVaultDetailsWorker, UPDATE_VAULT_DETAILS); +// Hiding an entity changes its encrypted recovery record. The local change and +// revisioned upload must share one producer before a later Delete can proceed. +export function* updateRecoveryVisibilityWorker({ + payload, originAppId, originBackupAccountSession, originBackupWriteIntent, onLocalComplete, +}: { + payload: { + entityId: string; + entityKind: EntityKind.WALLET | EntityKind.VAULT; + visibility: VisibilityType; + }; + originAppId?: string; + originBackupAccountSession?: number; + originBackupWriteIntent?: BackupWriteIntent; + onLocalComplete?: (updated: boolean) => void; +}) { + const sourceAppId = originAppId || currentWalletAccountId(); + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); + let localCommitted = false; + const reportLocalCommit = (updated: boolean) => { + try { + onLocalComplete?.(updated); + } catch (error) { + captureError(error); + } + }; + try { + assertWalletAccount(sourceAppId, requestSession); + const schema = payload.entityKind === EntityKind.VAULT + ? RealmSchema.Vault : payload.entityKind === EntityKind.WALLET + ? RealmSchema.Wallet : undefined; + if (!schema || ![VisibilityType.DEFAULT, VisibilityType.HIDDEN].includes(payload.visibility)) + throw new Error('Invalid recovery visibility change'); + const operation = beginBackupLocalOperation(sourceAppId, originBackupWriteIntent, requestSession); + const committedOperation = commitBackupLocalOperation(operation, (localRealm) => { + const stored = localRealm.objectForPrimaryKey(schema, payload.entityId); + if (!stored) throw new Error('Recovery visibility target is missing'); + stored.presentationData = { + ...(getJSONFromRealmObject(stored) as unknown as Wallet | Vault).presentationData, + visibility: payload.visibility, + }; + }); + if (!committedOperation.committed) throw new Error('Local recovery visibility update failed'); + localCommitted = true; + assertWalletAccount(sourceAppId, requestSession); + reportLocalCommit(true); + const stored = dbManager.getObjectById(schema, payload.entityId); + if (!stored) throw new Error('Committed recovery visibility target is missing'); + if (schema === RealmSchema.Vault) + yield call(updateVaultImageWorker, { + payload: { vault: getJSONFromRealmObject(stored) as unknown as Vault, isUpdate: true }, + originAppId: sourceAppId, originBackupAccountSession: requestSession, committedOperation, + }); + else + yield call(updateAppImageWorker, { + payload: { wallets: [getJSONFromRealmObject(stored) as unknown as Wallet] }, + originAppId: sourceAppId, originBackupAccountSession: requestSession, committedOperation, + }); + assertWalletAccount(sourceAppId, requestSession); + return true; + } catch { + if (!localCommitted) reportLocalCommit(false); + return localCommitted; + } +} + +export const updateRecoveryVisibilityWatcher = createWatcher( + updateRecoveryVisibilityWorker, UPDATE_RECOVERY_VISIBILITY +); + export function* updateSignerDetailsWorker({ payload, originAppId, originBackupAccountSession, originBackupWriteIntent }) { const sourceAppId = originAppId || currentWalletAccountId(); const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); diff --git a/src/store/store.ts b/src/store/store.ts index 28bb1a0c94..7b6bbce2b0 100644 --- a/src/store/store.ts +++ b/src/store/store.ts @@ -118,6 +118,7 @@ const accountOwnedActions = new Set([ 'REFRESH_CANARY_VAULT', 'UPDATE_WALLET_DETAILS', 'UPDATE_VAULT_DETAILS', + 'UPDATE_RECOVERY_VISIBILITY', 'UPDATE_SIGNER_DETAILS', 'UPDATE_KEY_DETAILS', 'UPDATED_VAULT_SIGNERS_XPRIV', diff --git a/tests/recovery/automaticWalletProducer.integration.cjs b/tests/recovery/automaticWalletProducer.integration.cjs index 0e8551907d..444ae047ad 100644 --- a/tests/recovery/automaticWalletProducer.integration.cjs +++ b/tests/recovery/automaticWalletProducer.integration.cjs @@ -4,7 +4,7 @@ // commit/proof is mocked. const assert = require('node:assert/strict'); const { test, before, after } = require('node:test'); -const { createRealmClient, declarations, evaluate, plain, Realm } = require('./automaticWriteRealmRelay.fixture.cjs'); +const { createRealmClient, declarations, componentDeclarations, evaluate, plain, Realm } = require('./automaticWriteRealmRelay.fixture.cjs'); const fixtureModule = process.env.KEEPER_RELAY_FIXTURE_MODULE; assert.ok(fixtureModule, 'Set KEEPER_RELAY_FIXTURE_MODULE to the pinned production Relay fixture'); const { createFixture } = require(fixtureModule); @@ -59,6 +59,7 @@ async function setup(t, prepare = () => {}) { const sagas = f.productionSagas([ 'currentBackupAppId', 'buildAutomaticAppMutation', 'buildAutomaticVaultMutation', 'queueCommittedBackupWorker', 'updateAppImageWorker', 'updateVaultImageWorker', + 'deleteAppImageEntityWorker', 'deleteVaultImageWorker', ], { ...f.automatic, addNewWallet: async (_type, details) => details.fixture, @@ -72,9 +73,19 @@ async function setup(t, prepare = () => {}) { }); evaluate(declarations('src/store/sagas/wallets.ts', [ 'currentWalletAccountId', 'assertWalletAccount', 'addNewWalletsWorker', - 'updateWalletDetailsWorker', 'addNewVaultWorker', + 'updateWalletDetailsWorker', 'updateRecoveryVisibilityWorker', 'addNewVaultWorker', + 'deleteVaultWorker', ]), sagas.scope, 'production-wallet-producers.ts'); + const uiScope = { + ...sagas.scope, + activeRealmAppId: () => f.appId, + isBackupVerificationCurrent: f.automatic.isBackupVerificationCurrent, + }; + evaluate(componentDeclarations( + 'src/screens/AppSettings/ManageWallets.tsx', 'ManageWallets', ['canDeleteRecoveryEntity'] + ), uiScope, 'production-manage-wallets-delete-gate.tsx'); f.scope = sagas.scope; + f.canDeleteRecoveryEntity = uiScope.canDeleteRecoveryEntity; f.run = (name, payload, metadata = {}) => sagas.run(name, { payload, originAppId: f.appId, originBackupAccountSession: f.transport.currentBackupAccountSession(), @@ -123,6 +134,50 @@ test('actual wallet rename survives HTTP conflict and reports local success with assert.equal((await f.remoteImage()).wallets.wallet.presentationData.name, 'Initial wallet'); }); +test('native Hide holds Delete until readback, then managed wallet deletion can use the new revision', async t => { + const f = await setup(t, f => assert.equal(f.dbManager.createObject(f.RealmSchema.Wallet, wallet(f)), true)); + let entered; + const mutationStarted = new Promise(resolve => { entered = resolve; }); + server.armFault({ mode: 'delay-reply', operation: 'updateAppImage', delayMs: 350, + beforeMutation() { entered(); } }); + let localCommit; + const hiding = f.run('updateRecoveryVisibilityWorker', { + entityId: 'wallet', entityKind: f.enums.EntityKind.WALLET, + visibility: f.enums.VisibilityType.HIDDEN, + }, { onLocalComplete: updated => { localCommit = updated; } }); + await mutationStarted; + assert.equal(localCommit, true); + assert.equal(f.dbManager.getObjectById(f.RealmSchema.Wallet, 'wallet').presentationData.visibility, 'HIDDEN'); + assert.equal(f.canDeleteRecoveryEntity(wallet(f)), false); + assert.equal(await hiding, true); + assert.equal(f.canDeleteRecoveryEntity(wallet(f)), true); + assert.equal((await f.remoteImage()).wallets.wallet.presentationData.visibility, 'HIDDEN'); + assert.equal(writes().length, 1); + const removed = await f.run('deleteAppImageEntityWorker', { walletIds: ['wallet'] }); + assert.equal(removed.updated, true); + assert.equal(removed.backupConfirmed, true); + assert.equal(f.dbManager.getObjectById(f.RealmSchema.Wallet, 'wallet'), undefined); + assert.deepEqual(Object.keys((await f.remoteImage()).wallets), []); + assert.equal(writes().length, 2); +}); + +test('lost Hide acknowledgement keeps the local change but withholds Delete and does not replay it', async t => { + const f = await setup(t, f => assert.equal(f.dbManager.createObject(f.RealmSchema.Wallet, wallet(f)), true)); + server.armFault({ mode: 'lost-reply', operation: 'updateAppImage' }); + let localCommit; + assert.equal(await f.run('updateRecoveryVisibilityWorker', { + entityId: 'wallet', entityKind: f.enums.EntityKind.WALLET, + visibility: f.enums.VisibilityType.HIDDEN, + }, { onLocalComplete: updated => { localCommit = updated; } }), true); + assert.equal(localCommit, true); + assert.equal(f.dbManager.getObjectById(f.RealmSchema.Wallet, 'wallet').presentationData.visibility, 'HIDDEN'); + assert.equal(f.canDeleteRecoveryEntity(wallet(f)), false); + assert.equal(f.store.getState().bhr.pendingAllBackupByAppId[f.appId], true); + assert.equal(writes().length, 1); + assert.equal((await f.remoteImage()).wallets.wallet.presentationData.visibility, 'HIDDEN'); + assert.equal(writes().length, 1); +}); + test('actual old-session wallet factory completion cannot save after A to B to A', async t => { const f = await setup(t); let entered, release; @@ -167,6 +222,23 @@ async function vaultSetup(t) { }); } +test('native vault Hide and managed Delete both advance the verified remote image', async t => { + const f = await vaultSetup(t); + const hiding = await f.run('updateRecoveryVisibilityWorker', { + entityId: 'old-vault', entityKind: f.enums.EntityKind.VAULT, + visibility: f.enums.VisibilityType.HIDDEN, + }); + assert.equal(hiding, true); + assert.equal(f.dbManager.getObjectById(f.RealmSchema.Vault, 'old-vault').presentationData.visibility, 'HIDDEN'); + assert.equal(f.canDeleteRecoveryEntity({ entityKind: f.enums.EntityKind.VAULT }), true); + assert.equal((await f.remoteImage()).vaults['old-vault'].presentationData.visibility, 'HIDDEN'); + assert.equal(writes().length, 1); + assert.equal(await f.run('deleteVaultWorker', { vaultId: 'old-vault' }), true); + assert.equal(f.dbManager.getObjectById(f.RealmSchema.Vault, 'old-vault'), undefined); + assert.deepEqual(Object.keys((await f.remoteImage()).vaults), []); + assert.equal(writes().length, 2); +}); + function newVaultPayload(f, migrated = false) { const key = plain(f.dbManager.getObjectByPrimaryId(f.RealmSchema.VaultSigner, 'xpub', 'disposable-vault-public-key')); return { isMigrated: migrated, oldVaultId: migrated ? 'old-vault' : undefined, newVaultInfo: { From 698078e1765d1e0728145f0ef6def05195192b30 Mon Sep 17 00:00:00 2001 From: Anant Date: Tue, 13 Oct 2026 15:32:16 +0530 Subject: [PATCH 193/254] Keep delete result fixture aligned with backup guard --- .../deleteResultTruthfulness.test.cjs | 31 +++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/tests/recovery/deleteResultTruthfulness.test.cjs b/tests/recovery/deleteResultTruthfulness.test.cjs index 8b2dac69c3..8a2dca1cbb 100644 --- a/tests/recovery/deleteResultTruthfulness.test.cjs +++ b/tests/recovery/deleteResultTruthfulness.test.cjs @@ -179,21 +179,26 @@ const screenFile = path.join(__dirname, '../../src/screens/AppSettings/ManageWal const screen = ts.createSourceFile(screenFile, fs.readFileSync(screenFile, 'utf8'), ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX); let deleteDeclaration; +let guardDeclaration; function findDelete(node) { if (ts.isVariableDeclaration(node) && node.name.getText(screen) === 'deleteSelectedEntity') deleteDeclaration = node; + if (ts.isVariableDeclaration(node) && node.name.getText(screen) === 'canDeleteRecoveryEntity') + guardDeclaration = node; ts.forEachChild(node, findDelete); } findDelete(screen); assert.ok(deleteDeclaration, 'production ManageWallets delete handler must exist'); +assert.ok(guardDeclaration, 'production ManageWallets backup deletion guard must exist'); const deleteHandlerCode = ts.transpileModule( - `const ${deleteDeclaration.getText(screen)}; globalThis.deleteSelectedEntity = deleteSelectedEntity;`, + `const ${guardDeclaration.getText(screen)}; const ${deleteDeclaration.getText(screen)}; globalThis.deleteSelectedEntity = deleteSelectedEntity;`, { compilerOptions: { target: ts.ScriptTarget.ES2020, jsx: ts.JsxEmit.React } } ).outputText; function screenFixture(entityKind, usdtResult = false) { const state = { - active: 'A', session: 1, present: true, presentById: new Map(), actions: [], toasts: [], + active: 'A', session: 1, consent: true, verified: true, + present: true, presentById: new Map(), actions: [], toasts: [], }; const scope = { React: { createElement: (component) => component }, @@ -210,6 +215,12 @@ function screenFixture(entityKind, usdtResult = false) { }, activeRealmAppId: () => state.active, currentBackupAccountSession: () => state.session, + store: { getState: () => ({ storage: { appId: state.active }, bhr: { + automaticCloudBackupByAppId: { [state.active]: state.consent }, + } }) }, + isAutomaticCloudBackupEnabled: (backup, appId) => + backup.automaticCloudBackupByAppId[appId] === true, + isBackupVerificationCurrent: (appId) => state.verified && appId === state.active, deleteAppImageEntity: (request) => ({ type: 'DELETE_BTC', ...request }), deleteVault: (id, onComplete) => ({ type: 'DELETE_VAULT', id, onComplete }), deleteUSDTWallet: async () => usdtResult, @@ -235,6 +246,22 @@ test('ManageWallets waits for the BTC delete outcome before showing a toast', as assert.equal(f.state.toasts[0].message, 'Failed to delete wallet'); }); +test('ManageWallets withholds a consented BTC Delete after PIN if Hide is still pending', async () => { + const f = screenFixture('WALLET'); + f.state.verified = false; + await f.deleteSelectedEntity(); + assert.deepEqual(f.state.actions, []); + assert.equal(f.state.toasts[0].message, 'Verify your assisted backup before deleting'); +}); + +test('ManageWallets permits local BTC Delete with assisted consent off', async () => { + const f = screenFixture('WALLET'); + f.state.consent = false; + f.state.verified = false; + await f.deleteSelectedEntity(); + assert.deepEqual(f.state.actions.map(action => action.type), ['DELETE_BTC']); +}); + test('ManageWallets refuses a BTC success toast while the Realm wallet still exists', async () => { const f = screenFixture('WALLET'); await f.deleteSelectedEntity(); From 858b55f340a903d49ca049b2731247e7622f95d6 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Thu, 15 Oct 2026 02:25:42 +0000 Subject: [PATCH 194/254] Add Android 16 KB ELF and APK inspection evidence --- scripts/check-android-16kb.py | 236 +++++++++++++++++++++++++++++ scripts/test_check_android_16kb.py | 181 ++++++++++++++++++++++ 2 files changed, 417 insertions(+) create mode 100644 scripts/check-android-16kb.py create mode 100644 scripts/test_check_android_16kb.py diff --git a/scripts/check-android-16kb.py b/scripts/check-android-16kb.py new file mode 100644 index 0000000000..8abc3d2e22 --- /dev/null +++ b/scripts/check-android-16kb.py @@ -0,0 +1,236 @@ +#!/usr/bin/env python3 +"""Inspect supplied ELF/AAR/APK bytes; this does not prove Android runtime support. + +Primary references, checked 2026-10-15: +https://developer.android.com/guide/practices/page-sizes +https://android.googlesource.com/platform/bionic/+/06356e41c5ed7b12220b24c05ba4fdb873b126a7/linker/linker_phdr.cpp +https://android.googlesource.com/platform/bionic/+/06356e41c5ed7b12220b24c05ba4fdb873b126a7/linker/linker_phdr_16kib_compat.cpp + +Keep LOAD alignment, RELRO end alignment, Android 17's effective program +alignment, and APK ZIP alignment separate. In particular, an unaligned RELRO +end covering its entire LOAD is exempt from Android 17's prefix adjustment. +AAR ZIP offsets say nothing about the final APK's packaging. +""" + +import argparse +import hashlib +import json +import pathlib +import struct +import sys +import zipfile + +PAGE = 16384 +COMPAT_PAGE = 4096 +PT_LOAD = 1 +PT_GNU_RELRO = 0x6474E552 +MACHINES = {183: "arm64-v8a", 62: "x86_64"} +ABIS = set(MACHINES.values()) +MAX_LIBRARY_SIZE = 512 * 1024 * 1024 + + +class InspectionError(ValueError): + """The supplied artifact does not establish the requested static proof.""" + + +def sha256(data): + return hashlib.sha256(data).hexdigest() + + +def inspect_elf(data, expected_abi=None): + if len(data) < 64 or data[:4] != b"\x7fELF": + raise InspectionError("missing or truncated ELF64 header") + if data[4:7] != bytes((2, 1, 1)): + raise InspectionError("expected a little-endian ELF64 version 1 library") + fields = struct.unpack_from(" len(data): + raise InspectionError("program-header table is outside the supplied file") + headers = [] + for index in range(phnum): + ptype, flags, offset, vaddr, _, filesz, memsz, align = struct.unpack_from( + " len(data) or vaddr + memsz > 0xFFFFFFFFFFFFFFFF: + raise InspectionError("segment range exceeds file or address space") + if ptype == PT_LOAD and filesz > memsz: + raise InspectionError("LOAD file size exceeds memory size") + headers.append({ + "index": index, "type": "LOAD" if ptype == PT_LOAD else "GNU_RELRO", + "flags": flags, "offset": offset, "vaddr": vaddr, + "filesz": filesz, "memsz": memsz, "align": align, + }) + loads = [x for x in headers if x["type"] == "LOAD"] + relros = [x for x in headers if x["type"] == "GNU_RELRO"] + if not loads: + raise InspectionError("ELF has no LOAD segment") + + reasons = [] + load_alignment = all(x["align"] >= PAGE and x["align"] & (x["align"] - 1) == 0 for x in loads) + load_congruence = all((x["vaddr"] - x["offset"]) % PAGE == 0 for x in loads) + declared_congruence = all(x["align"] <= 1 or (x["vaddr"] - x["offset"]) % x["align"] == 0 for x in loads) + if not load_alignment: + reasons.append("LOAD p_align is not a power of two of at least 16384") + if not load_congruence or not declared_congruence: + reasons.append("LOAD virtual/file offsets are not congruent") + + # Mirror CheckProgramHeaderAlignment and FixMinAlignFor16KiB. Invalid + # p_align values are separately rejected above, rather than silently ignored. + effective = PAGE + for item in loads: + align = item["align"] + if align > 1 and align & (align - 1) == 0: + effective = min(effective, align) + relro_details = [] + for item in relros: + end = item["vaddr"] + item["memsz"] + matching = next((x for x in loads if x["vaddr"] == item["vaddr"]), None) + whole_load = matching is not None and matching["memsz"] <= item["memsz"] + relro_details.append({ + **item, "end": end, "end_mod_16384": end % PAGE, + "end_aligned": end % PAGE == 0, + "matching_load_index": None if matching is None else matching["index"], + "covers_entire_matching_load": whole_load, + }) + if len(relro_details) == 1: + item = relro_details[0] + if item["matching_load_index"] is not None and not item["covers_entire_matching_load"]: + lowbit = item["end"] & -item["end"] + if lowbit >= COMPAT_PAGE: + effective = min(effective, lowbit) + overlap = False + if effective >= PAGE: + previous_end = 0 + for item in loads: + if previous_end > item["vaddr"] // PAGE * PAGE: + overlap = True + effective = COMPAT_PAGE + break + previous_end = item["vaddr"] + item["memsz"] + if effective < PAGE: + reasons.append("Android 17 effective program alignment is %s" % effective) + # The guide's end check is deliberately retained even when Android 17's + # rejection logic does not inspect a non-prefix/multiple-RELRO layout. + # Only the documented-in-source whole-LOAD exception is accepted here. + relro_safe = all(x["end_aligned"] or x["covers_entire_matching_load"] for x in relro_details) + if not relro_safe: + reasons.append("nonaligned RELRO end has no whole-LOAD exemption") + return { + "abi": abi, "sha256": sha256(data), "size": len(data), + "load_alignment_pass": load_alignment, + "load_16kb_congruence_pass": load_congruence, + "load_declared_congruence_pass": declared_congruence, + "loads": loads, "relro": relro_details, + "relro_end_alignment_pass": all(x["end_aligned"] for x in relro_details), + "relro_layout_pass": relro_safe, + "android17_effective_program_alignment": effective, + "android17_program_alignment_pass": effective >= PAGE, + "android17_load_page_overlap": overlap, + "failures": reasons, + } + + +def inspect_apk_entry(archive_data, info): + """Read the LOCAL header; central-directory header_offset is not data offset.""" + start = info.header_offset + if start < 0 or start + 30 > len(archive_data): + raise InspectionError("APK local ZIP header is outside the file") + fields = struct.unpack_from(" len(archive_data): + raise InspectionError("APK ZIP entry data is outside the supplied file") + local_name = archive_data[start + 30:start + 30 + name_len] + try: + local_name = local_name.decode("utf-8" if flags & 0x800 else "cp437") + except UnicodeDecodeError as exc: + raise InspectionError("invalid APK local filename encoding") from exc + if local_name != info.filename or method != info.compress_type or flags != info.flag_bits: + raise InspectionError("APK local and central ZIP headers disagree") + if flags & 1: + raise InspectionError("encrypted native APK entry is unsupported") + if not flags & 8 and (crc != info.CRC or compressed not in (info.compress_size, 0xFFFFFFFF) or size not in (info.file_size, 0xFFFFFFFF)): + raise InspectionError("APK local and central ZIP size/CRC fields disagree") + if method != zipfile.ZIP_STORED: + return {"status": "NOT_PROVEN", "method": method, "data_offset": data_offset, + "reason": "compressed native entry: extraction/manifest and runtime require separate proof"} + if info.compress_size != info.file_size: + raise InspectionError("STORED ZIP entry sizes differ") + aligned = data_offset % PAGE == 0 + return {"status": "PASS" if aligned else "FAIL", "method": method, + "data_offset": data_offset, "data_offset_mod_16384": data_offset % PAGE} + + +def inspect_file(path): + path = pathlib.Path(path) + result = {"path": str(path), "libraries": [], "errors": []} + try: + data = path.read_bytes() + result["sha256"] = sha256(data) + suffix = path.suffix.lower() + if suffix == ".so": + item = inspect_elf(data) + item.update({"entry": path.name, "apk_zip": {"status": "NOT_APPLICABLE"}}) + result["libraries"].append(item) + result["kind"] = "ELF" + elif suffix in (".aar", ".apk"): + result["kind"] = suffix[1:].upper() + with zipfile.ZipFile(path) as archive: + seen = set() + for info in archive.infolist(): + parts = info.filename.split("/") + prefix = "lib" if suffix == ".apk" else "jni" + if len(parts) != 3 or parts[0] != prefix or parts[1] not in ABIS or not parts[2].endswith(".so"): + continue + try: + if info.filename in seen: + raise InspectionError("duplicate native ZIP entry") + seen.add(info.filename) + if info.file_size > MAX_LIBRARY_SIZE: + raise InspectionError("native entry exceeds inspection size limit") + item = inspect_elf(archive.read(info), expected_abi=parts[1]) + item["entry"] = info.filename + item["apk_zip"] = inspect_apk_entry(data, info) if suffix == ".apk" else {"status": "NOT_APPLICABLE"} + if item["apk_zip"]["status"] not in ("PASS", "NOT_APPLICABLE"): + item["failures"].append("APK ZIP packaging is " + item["apk_zip"]["status"]) + result["libraries"].append(item) + except (InspectionError, ValueError, RuntimeError, zipfile.BadZipFile, struct.error) as exc: + result["errors"].append(info.filename + ": " + str(exc)) + else: + raise InspectionError("expected an explicit .so, .aar, or .apk input") + if not result["libraries"]: + result["errors"].append("no supported retained 64-bit native libraries inspected") + except (OSError, InspectionError, ValueError, zipfile.BadZipFile, struct.error) as exc: + result["errors"].append(str(exc)) + result["status"] = "FAIL" if result["errors"] or any(x["failures"] for x in result["libraries"]) else "PASS" + return result + + +def main(argv=None): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("artifacts", nargs="+", help="explicit .so, .aar, or .apk paths") + args = parser.parse_args(argv) + results = [inspect_file(x) for x in args.artifacts] + print(json.dumps({ + "schema": 1, "page_size": PAGE, + "scope": "static supplied bytes only; runtime, Portal behavior and production AAB/split identity remain separate", + "artifacts": results, + }, indent=2, sort_keys=True)) + return 1 if any(x["status"] != "PASS" for x in results) else 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/scripts/test_check_android_16kb.py b/scripts/test_check_android_16kb.py new file mode 100644 index 0000000000..19e78d7c07 --- /dev/null +++ b/scripts/test_check_android_16kb.py @@ -0,0 +1,181 @@ +"""Byte-level regressions for the static Android 16 KB evidence checker.""" + +import importlib.util +import io +import pathlib +import struct +import subprocess +import sys +import tempfile +import unittest +import warnings +import zipfile + +SPEC = importlib.util.spec_from_file_location("check_android_16kb", pathlib.Path(__file__).with_name("check-android-16kb.py")) +CHECK = importlib.util.module_from_spec(SPEC) +SPEC.loader.exec_module(CHECK) + + +def elf(loads=None, relros=None, machine=183): + loads = loads or [(0, 0, 0x100, 0x100, 0x10000), (0x10000, 0x30000, 0x8000, 0x9000, 0x10000)] + relros = [(0x10000, 0x30000, 0x6000, 0x6000)] if relros is None else relros + headers = [(1, 6, offset, vaddr, vaddr, filesz, memsz, align) for offset, vaddr, filesz, memsz, align in loads] + headers += [(CHECK.PT_GNU_RELRO, 4, offset, vaddr, vaddr, filesz, memsz, 1) for offset, vaddr, filesz, memsz in relros] + size = max(64 + 56 * len(headers), *(x[2] + x[5] for x in headers)) + data = bytearray(size) + data[:16] = b"\x7fELF" + bytes((2, 1, 1)) + bytes(9) + struct.pack_into(" Date: Thu, 15 Oct 2026 02:27:46 +0000 Subject: [PATCH 195/254] Pin Portal JNA with verified Android 16 KB native support --- .github/workflows/test.yml | 3 + android/app/build.gradle | 9 + .../automatic-write-validation.md | 2 + .../r4-android-16kb-suppliers-2026-10-15.json | 1212 +++++++++++++++++ release/r4-android-16kb.md | 236 ++++ release/r4-automatic-backup-local-handoff.md | 5 + 6 files changed, 1467 insertions(+) create mode 100644 release/evidence/r4-android-16kb-suppliers-2026-10-15.json create mode 100644 release/r4-android-16kb.md diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index dcc864e9df..1e7392a351 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -79,6 +79,9 @@ jobs: - name: Run release and packaged-artifact tests run: npm run test:release + - name: Test Android 16 KB static inspection + run: python3 -B -m unittest scripts/test_check_android_16kb.py + - name: Test Android reproducibility tools run: python3 -B -m unittest scripts/test_compare_android_apks.py reproducibility/test_verify_android_inputs.py diff --git a/android/app/build.gradle b/android/app/build.gradle index aa9d6b70e7..8c6fc44d1c 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -170,6 +170,15 @@ android { } dependencies { + // Portal's transitive JNA 5.14.0 has an 8 KB RELRO boundary even though + // its LOAD segments are 64 KB aligned. Keep classes and JNI on one AAR. + // 5.18.1 retains the 5.17 native fix and fixes the subsequent UniFFI + // Structure deadlock/concurrency regressions (JNA #1681 and #1686). + implementation('net.java.dev.jna:jna:5.18.1@aar') { + version { strictly '5.18.1' } + because 'Portal requires 16 KB JNA natives and the UniFFI Structure fixes' + } + // The version of react-native is set by the React Native Gradle Plugin implementation project(':react-native-iap') implementation 'com.google.mlkit:barcode-scanning:17.3.0' diff --git a/openspec/changes/fix-recovery-backup-freshness/automatic-write-validation.md b/openspec/changes/fix-recovery-backup-freshness/automatic-write-validation.md index c64949c5c1..1c0643e2cf 100644 --- a/openspec/changes/fix-recovery-backup-freshness/automatic-write-validation.md +++ b/openspec/changes/fix-recovery-backup-freshness/automatic-write-validation.md @@ -4,6 +4,8 @@ Updated 13 October 2026 from observed source/disposable evidence. Historical Sep Automatic-backup implementation and disposable proof are mandatory R4 work. Critical IOS/ANDROID backup acceptance also precedes release. Host-native Realm means the real Node Realm SDK, not an iOS/Android app launch. Physical-device-only follow-up cannot waive critical simulator/emulator safety checks. +The [15 October exact-698 Local report](https://github.com/KeeperCommunity/bitcoin-keeper/pull/7045#issuecomment-6178691569) supersedes the historical native NOT RUN cells only for its explicitly tested subset. It records a strict-16-KB Android startup FAIL. The [targeted supplier correction and new-head retest](../../../release/r4-android-16kb.md) do not constitute native acceptance or R4 readiness. Android C8/#7056 remains owner-parked and failed; this continuation must not restart that separate fix/retest cycle. Both hosted-backup readiness constants and the approved contributor manifest are unchanged. + ## Source and result record | Field | Current record | diff --git a/release/evidence/r4-android-16kb-suppliers-2026-10-15.json b/release/evidence/r4-android-16kb-suppliers-2026-10-15.json new file mode 100644 index 0000000000..854f6aeedf --- /dev/null +++ b/release/evidence/r4-android-16kb-suppliers-2026-10-15.json @@ -0,0 +1,1212 @@ +{ + "schema": 1, + "inspection_date": "2026-10-15", + "source_baseline": "698078e1765d1e0728145f0ef6def05195192b30", + "page_size": 16384, + "scope": [ + "Read-only inspection of supplier AAR bytes; no native build, simulator or runtime test.", + "13 AARs and 50 inspected 64-bit entries include debug/release variants, repeated shared libraries and both old/new JNA. This is NOT an inventory of 50 retained APK libraries.", + "The Mac-local developmentDebug APK SHA256 8d73e790f9b47766c92e927d7fe8425ad4967cfe44607cf35312cbb87e469af8 was unavailable in Cloud. Its reported 49/59 raw RELRO-end flags cannot be enumerated or classified here.", + "The JNA 5.14.0 x86_64 supplier entry fails static checks but the app explicitly excludes lib/x86_64/libjnidispatch.so. The arm64 entry remains a declared production input.", + "AAR ZIP offsets are not final APK ZIP proof. Source-built adapters, final APK packaging and Android strict-16KB runtime remain separate checks.", + "An unaligned raw RELRO end alone does not imply Android 17 strict-mode rejection when RELRO covers its complete matching LOAD." + ], + "references": { + "guide": "https://developer.android.com/guide/practices/page-sizes", + "bionic_revision": "06356e41c5ed7b12220b24c05ba4fdb873b126a7", + "bionic_check_program_header_alignment": "https://android.googlesource.com/platform/bionic/+/06356e41c5ed7b12220b24c05ba4fdb873b126a7/linker/linker_phdr.cpp#557", + "bionic_fix_min_align": "https://android.googlesource.com/platform/bionic/+/06356e41c5ed7b12220b24c05ba4fdb873b126a7/linker/linker_phdr_16kib_compat.cpp#445" + }, + "checker": "scripts/check-android-16kb.py", + "checker_unit_tests": { + "command": "python3 -B -m unittest discover -s scripts -p 'test_check_android_16kb.py'", + "passed": 20, + "failed": 0 + }, + "summary": { + "aar_count": 13, + "inspected_64bit_entries": 50, + "passing_entries": 48, + "failing_entries": 2 + }, + "artifacts": [ + { + "coordinate": "com.facebook.fbjni:fbjni:0.7.0", + "url": "https://repo.maven.apache.org/maven2/com/facebook/fbjni/fbjni/0.7.0/fbjni-0.7.0.aar", + "sha256": "7e319ae110ac5e5ef18904170aea5c3e753e915d196699d7fd39d36c8e1dfe36", + "static_status": "PASS", + "libraries": [ + { + "entry": "jni/arm64-v8a/libc++_shared.so", + "sha256": "3e852764502d06e0cf1080d532ae7c608b7649d946a3a9d7e84e3d4b7d683214", + "load_min_p_align": 16384, + "load_alignment_pass": true, + "load_16kb_congruence_pass": true, + "load_declared_congruence_pass": true, + "relro": [ + { + "vaddr": 1282800, + "memsz": 40208, + "end": 1323008, + "end_mod_16384": 12288, + "whole_matching_load_exemption": true + } + ], + "android17_effective_program_alignment": 16384, + "android17_load_page_overlap": false, + "static_status": "PASS", + "failures": [] + }, + { + "entry": "jni/arm64-v8a/libfbjni.so", + "sha256": "a9e35ba55d3f956920281f519385f8a72475ee24819799cb08375fdd4cce2b22", + "load_min_p_align": 16384, + "load_alignment_pass": true, + "load_16kb_congruence_pass": true, + "load_declared_congruence_pass": true, + "relro": [ + { + "vaddr": 187536, + "memsz": 4976, + "end": 192512, + "end_mod_16384": 12288, + "whole_matching_load_exemption": true + } + ], + "android17_effective_program_alignment": 16384, + "android17_load_page_overlap": false, + "static_status": "PASS", + "failures": [] + }, + { + "entry": "jni/x86_64/libc++_shared.so", + "sha256": "0a2c7afa35b183fdd5a3cb20e2080ad54cbea06c2a4f0bc8f122cb7d1e9e0b9f", + "load_min_p_align": 16384, + "load_alignment_pass": true, + "load_16kb_congruence_pass": true, + "load_declared_congruence_pass": true, + "relro": [ + { + "vaddr": 1242192, + "memsz": 39856, + "end": 1282048, + "end_mod_16384": 4096, + "whole_matching_load_exemption": true + } + ], + "android17_effective_program_alignment": 16384, + "android17_load_page_overlap": false, + "static_status": "PASS", + "failures": [] + }, + { + "entry": "jni/x86_64/libfbjni.so", + "sha256": "a51410366a0394e05bc04052cfb237c70dcf7d4d53a56e1d00a041436cb0b653", + "load_min_p_align": 16384, + "load_alignment_pass": true, + "load_16kb_congruence_pass": true, + "load_declared_congruence_pass": true, + "relro": [ + { + "vaddr": 183504, + "memsz": 4912, + "end": 188416, + "end_mod_16384": 8192, + "whole_matching_load_exemption": true + } + ], + "android17_effective_program_alignment": 16384, + "android17_load_page_overlap": false, + "static_status": "PASS", + "failures": [] + } + ] + }, + { + "coordinate": "com.facebook.hermes:hermes-android:0.14.1:debug", + "url": "https://repo.maven.apache.org/maven2/com/facebook/hermes/hermes-android/0.14.1/hermes-android-0.14.1-debug.aar", + "sha256": "f2cf4d13e7de78baec93b1c29aa2a0dbf4774af96c57e2c858890e809eed2869", + "static_status": "PASS", + "libraries": [ + { + "entry": "jni/arm64-v8a/libhermesvm.so", + "sha256": "02430039489a47f7aacb9e1c505d3af8d6c04d8c453a94d8bc39f5e8bae4c448", + "load_min_p_align": 16384, + "load_alignment_pass": true, + "load_16kb_congruence_pass": true, + "load_declared_congruence_pass": true, + "relro": [ + { + "vaddr": 3668576, + "memsz": 58784, + "end": 3727360, + "end_mod_16384": 8192, + "whole_matching_load_exemption": true + } + ], + "android17_effective_program_alignment": 16384, + "android17_load_page_overlap": false, + "static_status": "PASS", + "failures": [] + }, + { + "entry": "jni/x86_64/libhermesvm.so", + "sha256": "67f80125c7cf0f02b146b1d5f7cd23fc23c15877ec18a3c0c6468974b63e0883", + "load_min_p_align": 16384, + "load_alignment_pass": true, + "load_16kb_congruence_pass": true, + "load_declared_congruence_pass": true, + "relro": [ + { + "vaddr": 4019584, + "memsz": 60032, + "end": 4079616, + "end_mod_16384": 0, + "whole_matching_load_exemption": true + } + ], + "android17_effective_program_alignment": 16384, + "android17_load_page_overlap": false, + "static_status": "PASS", + "failures": [] + } + ], + "module_metadata": { + "url": "https://repo.maven.apache.org/maven2/com/facebook/hermes/hermes-android/0.14.1/hermes-android-0.14.1.module", + "downloaded_size_and_sha256_match": true + } + }, + { + "coordinate": "com.facebook.hermes:hermes-android:0.14.1:release", + "url": "https://repo.maven.apache.org/maven2/com/facebook/hermes/hermes-android/0.14.1/hermes-android-0.14.1-release.aar", + "sha256": "b4e57b503718697db4677aac75c2aaec1d3bb4cfda50ab9935c80da08072c63e", + "static_status": "PASS", + "libraries": [ + { + "entry": "jni/arm64-v8a/libhermesvm.so", + "sha256": "eb5a852e438dd9efcf85076abd89c7c38372e9b3b7ee85cd40915dabf6593d85", + "load_min_p_align": 16384, + "load_alignment_pass": true, + "load_16kb_congruence_pass": true, + "load_declared_congruence_pass": true, + "relro": [ + { + "vaddr": 2121616, + "memsz": 36976, + "end": 2158592, + "end_mod_16384": 12288, + "whole_matching_load_exemption": true + } + ], + "android17_effective_program_alignment": 16384, + "android17_load_page_overlap": false, + "static_status": "PASS", + "failures": [] + }, + { + "entry": "jni/x86_64/libhermesvm.so", + "sha256": "002bec5ba8cee35b4799f6ab4881efe19962088d53fc9e0dcedb552a86a4b524", + "load_min_p_align": 16384, + "load_alignment_pass": true, + "load_16kb_congruence_pass": true, + "load_declared_congruence_pass": true, + "relro": [ + { + "vaddr": 2207936, + "memsz": 36672, + "end": 2244608, + "end_mod_16384": 0, + "whole_matching_load_exemption": true + } + ], + "android17_effective_program_alignment": 16384, + "android17_load_page_overlap": false, + "static_status": "PASS", + "failures": [] + } + ], + "module_metadata": { + "url": "https://repo.maven.apache.org/maven2/com/facebook/hermes/hermes-android/0.14.1/hermes-android-0.14.1.module", + "downloaded_size_and_sha256_match": true + } + }, + { + "coordinate": "io.legere:pdfiumandroid:1.0.32", + "url": "https://repo.maven.apache.org/maven2/io/legere/pdfiumandroid/1.0.32/pdfiumandroid-1.0.32.aar", + "sha256": "561759462d8071ad16d54f0b83ebdc4daa6e63cd8b0833a6516dcebeaa848f3a", + "static_status": "PASS", + "libraries": [ + { + "entry": "jni/arm64-v8a/libpdfium.so", + "sha256": "e3ad7644b9dd203f27e0650d5a1af0a2a73548fad9fe608c209fbd55888d3643", + "load_min_p_align": 16384, + "load_alignment_pass": true, + "load_16kb_congruence_pass": true, + "load_declared_congruence_pass": true, + "relro": [ + { + "vaddr": 4724160, + "memsz": 51776, + "end": 4775936, + "end_mod_16384": 8192, + "whole_matching_load_exemption": true + } + ], + "android17_effective_program_alignment": 16384, + "android17_load_page_overlap": false, + "static_status": "PASS", + "failures": [] + }, + { + "entry": "jni/arm64-v8a/libpdfiumandroid.so", + "sha256": "be53f7f812f9ab94b09c694db79b30170c9af9937387be945733424e654b776c", + "load_min_p_align": 16384, + "load_alignment_pass": true, + "load_16kb_congruence_pass": true, + "load_declared_congruence_pass": true, + "relro": [ + { + "vaddr": 425184, + "memsz": 21280, + "end": 446464, + "end_mod_16384": 4096, + "whole_matching_load_exemption": true + } + ], + "android17_effective_program_alignment": 16384, + "android17_load_page_overlap": false, + "static_status": "PASS", + "failures": [] + }, + { + "entry": "jni/x86_64/libpdfium.so", + "sha256": "4f48d1c92fd74a2e5758c98034036393d5b5f12f5eab39175979894976725869", + "load_min_p_align": 16384, + "load_alignment_pass": true, + "load_16kb_congruence_pass": true, + "load_declared_congruence_pass": true, + "relro": [ + { + "vaddr": 4895424, + "memsz": 77120, + "end": 4972544, + "end_mod_16384": 8192, + "whole_matching_load_exemption": true + } + ], + "android17_effective_program_alignment": 16384, + "android17_load_page_overlap": false, + "static_status": "PASS", + "failures": [] + }, + { + "entry": "jni/x86_64/libpdfiumandroid.so", + "sha256": "ab5666f50f7fab63a0981fc977fc11e1738399540a950a69482261b4c1f80a49", + "load_min_p_align": 16384, + "load_alignment_pass": true, + "load_16kb_congruence_pass": true, + "load_declared_congruence_pass": true, + "relro": [ + { + "vaddr": 411760, + "memsz": 22416, + "end": 434176, + "end_mod_16384": 8192, + "whole_matching_load_exemption": true + } + ], + "android17_effective_program_alignment": 16384, + "android17_load_page_overlap": false, + "static_status": "PASS", + "failures": [] + } + ] + }, + { + "coordinate": "com.facebook.react:react-android:0.83.9:debug", + "url": "https://repo.maven.apache.org/maven2/com/facebook/react/react-android/0.83.9/react-android-0.83.9-debug.aar", + "sha256": "e8a7e428e5369a541e1eb267b703fb53d84c5be5b2ab32a0d5f8f30dc745e5cc", + "static_status": "PASS", + "libraries": [ + { + "entry": "jni/arm64-v8a/libc++_shared.so", + "sha256": "3e852764502d06e0cf1080d532ae7c608b7649d946a3a9d7e84e3d4b7d683214", + "load_min_p_align": 16384, + "load_alignment_pass": true, + "load_16kb_congruence_pass": true, + "load_declared_congruence_pass": true, + "relro": [ + { + "vaddr": 1282800, + "memsz": 40208, + "end": 1323008, + "end_mod_16384": 12288, + "whole_matching_load_exemption": true + } + ], + "android17_effective_program_alignment": 16384, + "android17_load_page_overlap": false, + "static_status": "PASS", + "failures": [] + }, + { + "entry": "jni/arm64-v8a/libfbjni.so", + "sha256": "a9e35ba55d3f956920281f519385f8a72475ee24819799cb08375fdd4cce2b22", + "load_min_p_align": 16384, + "load_alignment_pass": true, + "load_16kb_congruence_pass": true, + "load_declared_congruence_pass": true, + "relro": [ + { + "vaddr": 187536, + "memsz": 4976, + "end": 192512, + "end_mod_16384": 12288, + "whole_matching_load_exemption": true + } + ], + "android17_effective_program_alignment": 16384, + "android17_load_page_overlap": false, + "static_status": "PASS", + "failures": [] + }, + { + "entry": "jni/arm64-v8a/libhermestooling.so", + "sha256": "f691a1cc4e8e85340ab92f5c5a692a9f5566e9278454b352da097fdf9b59689b", + "load_min_p_align": 16384, + "load_alignment_pass": true, + "load_16kb_congruence_pass": true, + "load_declared_congruence_pass": true, + "relro": [ + { + "vaddr": 719472, + "memsz": 17808, + "end": 737280, + "end_mod_16384": 0, + "whole_matching_load_exemption": true + } + ], + "android17_effective_program_alignment": 16384, + "android17_load_page_overlap": false, + "static_status": "PASS", + "failures": [] + }, + { + "entry": "jni/arm64-v8a/libjsi.so", + "sha256": "3b8cc60472b8e1a0d84df444c8bd4a7b1954fb3470c4c34fbac3615e847ed715", + "load_min_p_align": 16384, + "load_alignment_pass": true, + "load_16kb_congruence_pass": true, + "load_declared_congruence_pass": true, + "relro": [ + { + "vaddr": 919312, + "memsz": 18672, + "end": 937984, + "end_mod_16384": 4096, + "whole_matching_load_exemption": true + } + ], + "android17_effective_program_alignment": 16384, + "android17_load_page_overlap": false, + "static_status": "PASS", + "failures": [] + }, + { + "entry": "jni/arm64-v8a/libreactnative.so", + "sha256": "22f599e15b3dd932d82677b2abeb6845c08668e751ee3425e3fbe68db673cfa3", + "load_min_p_align": 16384, + "load_alignment_pass": true, + "load_16kb_congruence_pass": true, + "load_declared_congruence_pass": true, + "relro": [ + { + "vaddr": 19971840, + "memsz": 303360, + "end": 20275200, + "end_mod_16384": 8192, + "whole_matching_load_exemption": true + } + ], + "android17_effective_program_alignment": 16384, + "android17_load_page_overlap": false, + "static_status": "PASS", + "failures": [] + }, + { + "entry": "jni/x86_64/libc++_shared.so", + "sha256": "0a2c7afa35b183fdd5a3cb20e2080ad54cbea06c2a4f0bc8f122cb7d1e9e0b9f", + "load_min_p_align": 16384, + "load_alignment_pass": true, + "load_16kb_congruence_pass": true, + "load_declared_congruence_pass": true, + "relro": [ + { + "vaddr": 1242192, + "memsz": 39856, + "end": 1282048, + "end_mod_16384": 4096, + "whole_matching_load_exemption": true + } + ], + "android17_effective_program_alignment": 16384, + "android17_load_page_overlap": false, + "static_status": "PASS", + "failures": [] + }, + { + "entry": "jni/x86_64/libfbjni.so", + "sha256": "a51410366a0394e05bc04052cfb237c70dcf7d4d53a56e1d00a041436cb0b653", + "load_min_p_align": 16384, + "load_alignment_pass": true, + "load_16kb_congruence_pass": true, + "load_declared_congruence_pass": true, + "relro": [ + { + "vaddr": 183504, + "memsz": 4912, + "end": 188416, + "end_mod_16384": 8192, + "whole_matching_load_exemption": true + } + ], + "android17_effective_program_alignment": 16384, + "android17_load_page_overlap": false, + "static_status": "PASS", + "failures": [] + }, + { + "entry": "jni/x86_64/libhermestooling.so", + "sha256": "6b774fe4a5fa31943d7399737da7299e7dd65696d81bc964c3522c26b4a138ce", + "load_min_p_align": 16384, + "load_alignment_pass": true, + "load_16kb_congruence_pass": true, + "load_declared_congruence_pass": true, + "relro": [ + { + "vaddr": 680592, + "memsz": 15728, + "end": 696320, + "end_mod_16384": 8192, + "whole_matching_load_exemption": true + } + ], + "android17_effective_program_alignment": 16384, + "android17_load_page_overlap": false, + "static_status": "PASS", + "failures": [] + }, + { + "entry": "jni/x86_64/libjsi.so", + "sha256": "68767a1866fb857c75739e59a3a29c83809229649c7cc14716d3841d53c9afe5", + "load_min_p_align": 16384, + "load_alignment_pass": true, + "load_16kb_congruence_pass": true, + "load_declared_congruence_pass": true, + "relro": [ + { + "vaddr": 907328, + "memsz": 18368, + "end": 925696, + "end_mod_16384": 8192, + "whole_matching_load_exemption": true + } + ], + "android17_effective_program_alignment": 16384, + "android17_load_page_overlap": false, + "static_status": "PASS", + "failures": [] + }, + { + "entry": "jni/x86_64/libreactnative.so", + "sha256": "29e5981ab51238dab700cd84e15279619e5a05581eee52880f35cc2db790ef5e", + "load_min_p_align": 16384, + "load_alignment_pass": true, + "load_16kb_congruence_pass": true, + "load_declared_congruence_pass": true, + "relro": [ + { + "vaddr": 19327536, + "memsz": 300496, + "end": 19628032, + "end_mod_16384": 0, + "whole_matching_load_exemption": true + } + ], + "android17_effective_program_alignment": 16384, + "android17_load_page_overlap": false, + "static_status": "PASS", + "failures": [] + } + ], + "module_metadata": { + "url": "https://repo.maven.apache.org/maven2/com/facebook/react/react-android/0.83.9/react-android-0.83.9.module", + "downloaded_size_and_sha256_match": true + } + }, + { + "coordinate": "com.facebook.react:react-android:0.83.9:release", + "url": "https://repo.maven.apache.org/maven2/com/facebook/react/react-android/0.83.9/react-android-0.83.9-release.aar", + "sha256": "a511f9e013cd27bcd7bacb3d825fc681f7778cb11bd14f76e0c24388aee70eeb", + "static_status": "PASS", + "libraries": [ + { + "entry": "jni/arm64-v8a/libc++_shared.so", + "sha256": "3e852764502d06e0cf1080d532ae7c608b7649d946a3a9d7e84e3d4b7d683214", + "load_min_p_align": 16384, + "load_alignment_pass": true, + "load_16kb_congruence_pass": true, + "load_declared_congruence_pass": true, + "relro": [ + { + "vaddr": 1282800, + "memsz": 40208, + "end": 1323008, + "end_mod_16384": 12288, + "whole_matching_load_exemption": true + } + ], + "android17_effective_program_alignment": 16384, + "android17_load_page_overlap": false, + "static_status": "PASS", + "failures": [] + }, + { + "entry": "jni/arm64-v8a/libfbjni.so", + "sha256": "a9e35ba55d3f956920281f519385f8a72475ee24819799cb08375fdd4cce2b22", + "load_min_p_align": 16384, + "load_alignment_pass": true, + "load_16kb_congruence_pass": true, + "load_declared_congruence_pass": true, + "relro": [ + { + "vaddr": 187536, + "memsz": 4976, + "end": 192512, + "end_mod_16384": 12288, + "whole_matching_load_exemption": true + } + ], + "android17_effective_program_alignment": 16384, + "android17_load_page_overlap": false, + "static_status": "PASS", + "failures": [] + }, + { + "entry": "jni/arm64-v8a/libhermestooling.so", + "sha256": "fd2e39f4e3a968a868445e01ae3294e779f1e7c3838103b5e667b06ab865b387", + "load_min_p_align": 16384, + "load_alignment_pass": true, + "load_16kb_congruence_pass": true, + "load_declared_congruence_pass": true, + "relro": [ + { + "vaddr": 146896, + "memsz": 8752, + "end": 155648, + "end_mod_16384": 8192, + "whole_matching_load_exemption": true + } + ], + "android17_effective_program_alignment": 16384, + "android17_load_page_overlap": false, + "static_status": "PASS", + "failures": [] + }, + { + "entry": "jni/arm64-v8a/libjsi.so", + "sha256": "8122d5fe2ecfd84772c7b9abedb40cb79175608c7deaf68c25c5d398bc3fda2b", + "load_min_p_align": 16384, + "load_alignment_pass": true, + "load_16kb_congruence_pass": true, + "load_declared_congruence_pass": true, + "relro": [ + { + "vaddr": 400688, + "memsz": 13008, + "end": 413696, + "end_mod_16384": 4096, + "whole_matching_load_exemption": true + } + ], + "android17_effective_program_alignment": 16384, + "android17_load_page_overlap": false, + "static_status": "PASS", + "failures": [] + }, + { + "entry": "jni/arm64-v8a/libreactnative.so", + "sha256": "a0da6ff20683f8bee87861f2683a6fcda8a86fe0fdc22112668c5ab123265f57", + "load_min_p_align": 16384, + "load_alignment_pass": true, + "load_16kb_congruence_pass": true, + "load_declared_congruence_pass": true, + "relro": [ + { + "vaddr": 6173632, + "memsz": 150592, + "end": 6324224, + "end_mod_16384": 0, + "whole_matching_load_exemption": true + } + ], + "android17_effective_program_alignment": 16384, + "android17_load_page_overlap": false, + "static_status": "PASS", + "failures": [] + }, + { + "entry": "jni/x86_64/libc++_shared.so", + "sha256": "0a2c7afa35b183fdd5a3cb20e2080ad54cbea06c2a4f0bc8f122cb7d1e9e0b9f", + "load_min_p_align": 16384, + "load_alignment_pass": true, + "load_16kb_congruence_pass": true, + "load_declared_congruence_pass": true, + "relro": [ + { + "vaddr": 1242192, + "memsz": 39856, + "end": 1282048, + "end_mod_16384": 4096, + "whole_matching_load_exemption": true + } + ], + "android17_effective_program_alignment": 16384, + "android17_load_page_overlap": false, + "static_status": "PASS", + "failures": [] + }, + { + "entry": "jni/x86_64/libfbjni.so", + "sha256": "a51410366a0394e05bc04052cfb237c70dcf7d4d53a56e1d00a041436cb0b653", + "load_min_p_align": 16384, + "load_alignment_pass": true, + "load_16kb_congruence_pass": true, + "load_declared_congruence_pass": true, + "relro": [ + { + "vaddr": 183504, + "memsz": 4912, + "end": 188416, + "end_mod_16384": 8192, + "whole_matching_load_exemption": true + } + ], + "android17_effective_program_alignment": 16384, + "android17_load_page_overlap": false, + "static_status": "PASS", + "failures": [] + }, + { + "entry": "jni/x86_64/libhermestooling.so", + "sha256": "ca834ac984a34cbcd14381f4fc63287af58495ec0bf3f3466976e1c0fc96e5ec", + "load_min_p_align": 16384, + "load_alignment_pass": true, + "load_16kb_congruence_pass": true, + "load_declared_congruence_pass": true, + "relro": [ + { + "vaddr": 143584, + "memsz": 7968, + "end": 151552, + "end_mod_16384": 4096, + "whole_matching_load_exemption": true + } + ], + "android17_effective_program_alignment": 16384, + "android17_load_page_overlap": false, + "static_status": "PASS", + "failures": [] + }, + { + "entry": "jni/x86_64/libjsi.so", + "sha256": "d728958aa4ea58816c557ea0ae5b87e44983a5b056e27b6a3ddc2413c9e52bc1", + "load_min_p_align": 16384, + "load_alignment_pass": true, + "load_16kb_congruence_pass": true, + "load_declared_congruence_pass": true, + "relro": [ + { + "vaddr": 407808, + "memsz": 9984, + "end": 417792, + "end_mod_16384": 8192, + "whole_matching_load_exemption": true + } + ], + "android17_effective_program_alignment": 16384, + "android17_load_page_overlap": false, + "static_status": "PASS", + "failures": [] + }, + { + "entry": "jni/x86_64/libreactnative.so", + "sha256": "458499dfe2014ba2b9bd7555aabf28b9d3d5b6825330376362720863f6e3c974", + "load_min_p_align": 16384, + "load_alignment_pass": true, + "load_16kb_congruence_pass": true, + "load_declared_congruence_pass": true, + "relro": [ + { + "vaddr": 6288496, + "memsz": 150416, + "end": 6438912, + "end_mod_16384": 0, + "whole_matching_load_exemption": true + } + ], + "android17_effective_program_alignment": 16384, + "android17_load_page_overlap": false, + "static_status": "PASS", + "failures": [] + } + ], + "module_metadata": { + "url": "https://repo.maven.apache.org/maven2/com/facebook/react/react-android/0.83.9/react-android-0.83.9.module", + "downloaded_size_and_sha256_match": true + } + }, + { + "coordinate": "io.sentry:sentry-native-ndk:0.13.3", + "url": "https://repo.maven.apache.org/maven2/io/sentry/sentry-native-ndk/0.13.3/sentry-native-ndk-0.13.3.aar", + "sha256": "f09e7dd432b99e0b9c782eeec5c90887171b3ff5065134bc246ff217f7698c32", + "static_status": "PASS", + "libraries": [ + { + "entry": "jni/arm64-v8a/libsentry-android.so", + "sha256": "0cc45445f6f545256be5102a39fae3c482d40970b8b3f6cffb8e0f00a3e1172e", + "load_min_p_align": 16384, + "load_alignment_pass": true, + "load_16kb_congruence_pass": true, + "load_declared_congruence_pass": true, + "relro": [ + { + "vaddr": 31392, + "memsz": 1376, + "end": 32768, + "end_mod_16384": 0, + "whole_matching_load_exemption": true + } + ], + "android17_effective_program_alignment": 16384, + "android17_load_page_overlap": false, + "static_status": "PASS", + "failures": [] + }, + { + "entry": "jni/arm64-v8a/libsentry.so", + "sha256": "7ba1fe9acd50e7f6ff980aae31ea06123e99e3e4b4c6dbadd1314cdfaca1b4f9", + "load_min_p_align": 16384, + "load_alignment_pass": true, + "load_16kb_congruence_pass": true, + "load_declared_congruence_pass": true, + "relro": [ + { + "vaddr": 731344, + "memsz": 26416, + "end": 757760, + "end_mod_16384": 4096, + "whole_matching_load_exemption": true + } + ], + "android17_effective_program_alignment": 16384, + "android17_load_page_overlap": false, + "static_status": "PASS", + "failures": [] + }, + { + "entry": "jni/x86_64/libsentry-android.so", + "sha256": "747747abea1312a76a61eabe673b185556c6467e6d02facefefc7ee645483b8f", + "load_min_p_align": 16384, + "load_alignment_pass": true, + "load_16kb_congruence_pass": true, + "load_declared_congruence_pass": true, + "relro": [ + { + "vaddr": 30896, + "memsz": 1872, + "end": 32768, + "end_mod_16384": 0, + "whole_matching_load_exemption": true + } + ], + "android17_effective_program_alignment": 16384, + "android17_load_page_overlap": false, + "static_status": "PASS", + "failures": [] + }, + { + "entry": "jni/x86_64/libsentry.so", + "sha256": "a388dbf74c684603c02730bb90ea2ac4db7df5048334874a6d9c7f5beedf6dd8", + "load_min_p_align": 16384, + "load_alignment_pass": true, + "load_16kb_congruence_pass": true, + "load_declared_congruence_pass": true, + "relro": [ + { + "vaddr": 717696, + "memsz": 23680, + "end": 741376, + "end_mod_16384": 4096, + "whole_matching_load_exemption": true + } + ], + "android17_effective_program_alignment": 16384, + "android17_load_page_overlap": false, + "static_status": "PASS", + "failures": [] + } + ] + }, + { + "coordinate": "com.google.mlkit:barcode-scanning:17.3.0", + "url": "https://dl.google.com/dl/android/maven2/com/google/mlkit/barcode-scanning/17.3.0/barcode-scanning-17.3.0.aar", + "sha256": "64c5cf48bb94b6c54f2c785c660640a7d167aff6ad72bc2c920706dc4187b79a", + "static_status": "PASS", + "libraries": [ + { + "entry": "jni/arm64-v8a/libbarhopper_v3.so", + "sha256": "ec09597e9eb3aee0eb55ae3366da0d4ba416f5aa35a1af0b3811f50ff46743a1", + "load_min_p_align": 16384, + "load_alignment_pass": true, + "load_16kb_congruence_pass": true, + "load_declared_congruence_pass": true, + "relro": [ + { + "vaddr": 4866048, + "memsz": 49152, + "end": 4915200, + "end_mod_16384": 0, + "whole_matching_load_exemption": true + } + ], + "android17_effective_program_alignment": 16384, + "android17_load_page_overlap": false, + "static_status": "PASS", + "failures": [] + }, + { + "entry": "jni/x86_64/libbarhopper_v3.so", + "sha256": "6c5deea87d962af008fd48c3229db13ab68eef2e9feeccb2df5b33a920c0579a", + "load_min_p_align": 16384, + "load_alignment_pass": true, + "load_16kb_congruence_pass": true, + "load_declared_congruence_pass": true, + "relro": [ + { + "vaddr": 5832704, + "memsz": 45056, + "end": 5877760, + "end_mod_16384": 12288, + "whole_matching_load_exemption": true + } + ], + "android17_effective_program_alignment": 16384, + "android17_load_page_overlap": false, + "static_status": "PASS", + "failures": [] + } + ] + }, + { + "coordinate": "androidx.camera:camera-core:1.5.0-alpha03", + "url": "https://dl.google.com/dl/android/maven2/androidx/camera/camera-core/1.5.0-alpha03/camera-core-1.5.0-alpha03.aar", + "sha256": "c07ef5ee196ae35a4d3d77babae6197460929b38970c9ca7d3acc61e85986198", + "static_status": "PASS", + "libraries": [ + { + "entry": "jni/arm64-v8a/libimage_processing_util_jni.so", + "sha256": "067d083e0c4de78aac6ae431df298947020990fe3f5ae743b5bd418d0d144b46", + "load_min_p_align": 16384, + "load_alignment_pass": true, + "load_16kb_congruence_pass": true, + "load_declared_congruence_pass": true, + "relro": [ + { + "vaddr": 42912, + "memsz": 2144, + "end": 45056, + "end_mod_16384": 12288, + "whole_matching_load_exemption": true + } + ], + "android17_effective_program_alignment": 16384, + "android17_load_page_overlap": false, + "static_status": "PASS", + "failures": [] + }, + { + "entry": "jni/x86_64/libimage_processing_util_jni.so", + "sha256": "352fa6662094b3a3e3ad60893219cb889180c452c74ffab8025720e0bef4c6db", + "load_min_p_align": 16384, + "load_alignment_pass": true, + "load_16kb_congruence_pass": true, + "load_declared_congruence_pass": true, + "relro": [ + { + "vaddr": 59728, + "memsz": 1712, + "end": 61440, + "end_mod_16384": 12288, + "whole_matching_load_exemption": true + } + ], + "android17_effective_program_alignment": 16384, + "android17_load_page_overlap": false, + "static_status": "PASS", + "failures": [] + } + ] + }, + { + "coordinate": "io.github.zhongwuzw:mmkv:2.4.0", + "url": "https://repo.maven.apache.org/maven2/io/github/zhongwuzw/mmkv/2.4.0/mmkv-2.4.0.aar", + "sha256": "f1f40d7460d689aa3768bb51c38de917ab4cd5946928db371f1be1aaa5997e82", + "static_status": "PASS", + "libraries": [ + { + "entry": "jni/arm64-v8a/libc++_shared.so", + "sha256": "ad74bf43eb1fd576518168f664ad16a74e00eeda9595875c33dd87f6dd197869", + "load_min_p_align": 16384, + "load_alignment_pass": true, + "load_16kb_congruence_pass": true, + "load_declared_congruence_pass": true, + "relro": [ + { + "vaddr": 1243288, + "memsz": 42856, + "end": 1286144, + "end_mod_16384": 8192, + "whole_matching_load_exemption": true + } + ], + "android17_effective_program_alignment": 16384, + "android17_load_page_overlap": false, + "static_status": "PASS", + "failures": [] + }, + { + "entry": "jni/arm64-v8a/libmmkv.so", + "sha256": "7e63e305547b410717b12ea6c15aacd7c59610b4b6ebcb1796fe18929fedc2a5", + "load_min_p_align": 16384, + "load_alignment_pass": true, + "load_16kb_congruence_pass": true, + "load_declared_congruence_pass": true, + "relro": [ + { + "vaddr": 366336, + "memsz": 6400, + "end": 372736, + "end_mod_16384": 12288, + "whole_matching_load_exemption": true + } + ], + "android17_effective_program_alignment": 16384, + "android17_load_page_overlap": false, + "static_status": "PASS", + "failures": [] + }, + { + "entry": "jni/x86_64/libc++_shared.so", + "sha256": "294e0ce4e88589103952c57c9484f917c26509d66170bc86d07d48f0fc879de4", + "load_min_p_align": 16384, + "load_alignment_pass": true, + "load_16kb_congruence_pass": true, + "load_declared_congruence_pass": true, + "relro": [ + { + "vaddr": 1219776, + "memsz": 41792, + "end": 1261568, + "end_mod_16384": 0, + "whole_matching_load_exemption": true + } + ], + "android17_effective_program_alignment": 16384, + "android17_load_page_overlap": false, + "static_status": "PASS", + "failures": [] + }, + { + "entry": "jni/x86_64/libmmkv.so", + "sha256": "4f646fce28adccac88af7a4179f6928503337c196c7f52c1a15afbff222c4f5a", + "load_min_p_align": 16384, + "load_alignment_pass": true, + "load_16kb_congruence_pass": true, + "load_declared_congruence_pass": true, + "relro": [ + { + "vaddr": 368576, + "memsz": 4160, + "end": 372736, + "end_mod_16384": 12288, + "whole_matching_load_exemption": true + } + ], + "android17_effective_program_alignment": 16384, + "android17_load_page_overlap": false, + "static_status": "PASS", + "failures": [] + } + ] + }, + { + "coordinate": "libportal-react-native@2ff681b0b725009768acadb35b2731fb1a402fa3:android/libs/libportal-android-0.3.4-SNAPSHOT.aar", + "url": "https://github.com/bithyve/libportal-react-native/blob/2ff681b0b725009768acadb35b2731fb1a402fa3/android/libs/libportal-android-0.3.4-SNAPSHOT.aar", + "sha256": "f12252c9ca9c8dc2fa58e44841e5b340e95072dae2c9ae1e21d79a23e060c1fa", + "static_status": "PASS", + "libraries": [ + { + "entry": "jni/arm64-v8a/libportal.so", + "sha256": "7c1e474e03dfdc71bea0aeaa8afca22bb357ac23df99136ed73453ed4e1d677d", + "load_min_p_align": 16384, + "load_alignment_pass": true, + "load_16kb_congruence_pass": true, + "load_declared_congruence_pass": true, + "relro": [ + { + "vaddr": 2415072, + "memsz": 50720, + "end": 2465792, + "end_mod_16384": 8192, + "whole_matching_load_exemption": true + } + ], + "android17_effective_program_alignment": 16384, + "android17_load_page_overlap": false, + "static_status": "PASS", + "failures": [] + }, + { + "entry": "jni/x86_64/libportal.so", + "sha256": "dbc6a0afeffe068209495005ce9d010f39b3cd47f6e33a8a9b4a901b7616d4fc", + "load_min_p_align": 16384, + "load_alignment_pass": true, + "load_16kb_congruence_pass": true, + "load_declared_congruence_pass": true, + "relro": [ + { + "vaddr": 2700800, + "memsz": 55808, + "end": 2756608, + "end_mod_16384": 4096, + "whole_matching_load_exemption": true + } + ], + "android17_effective_program_alignment": 16384, + "android17_load_page_overlap": false, + "static_status": "PASS", + "failures": [] + } + ] + }, + { + "coordinate": "net.java.dev.jna:jna:5.14.0@aar", + "url": "https://repo.maven.apache.org/maven2/net/java/dev/jna/jna/5.14.0/jna-5.14.0.aar", + "sha256": "81a8b9afc65f9d6b205338c25908af8de0ff8411a00985104c9d83fffdfab380", + "static_status": "FAIL", + "libraries": [ + { + "entry": "jni/arm64-v8a/libjnidispatch.so", + "sha256": "3730ce014f7a6807ae74af78c3b9f5a3cba1f5886316b3d3858493df923efe96", + "load_min_p_align": 65536, + "load_alignment_pass": true, + "load_16kb_congruence_pass": true, + "load_declared_congruence_pass": true, + "relro": [ + { + "vaddr": 220064, + "memsz": 1120, + "end": 221184, + "end_mod_16384": 8192, + "whole_matching_load_exemption": false + } + ], + "android17_effective_program_alignment": 8192, + "android17_load_page_overlap": false, + "static_status": "FAIL", + "failures": [ + "Android 17 effective program alignment is 8192", + "nonaligned RELRO end has no whole-LOAD exemption" + ] + }, + { + "entry": "jni/x86_64/libjnidispatch.so", + "sha256": "a6d3997d3cfc0dba1b00fd2943b41e7395741e88336ed6f6237b594c94083eea", + "load_min_p_align": 4096, + "load_alignment_pass": false, + "load_16kb_congruence_pass": false, + "load_declared_congruence_pass": true, + "relro": [ + { + "vaddr": 109312, + "memsz": 1280, + "end": 110592, + "end_mod_16384": 12288, + "whole_matching_load_exemption": false + } + ], + "android17_effective_program_alignment": 4096, + "android17_load_page_overlap": false, + "static_status": "FAIL", + "failures": [ + "LOAD p_align is not a power of two of at least 16384", + "LOAD virtual/file offsets are not congruent", + "Android 17 effective program alignment is 4096", + "nonaligned RELRO end has no whole-LOAD exemption" + ] + } + ] + }, + { + "coordinate": "net.java.dev.jna:jna:5.18.1@aar", + "url": "https://repo.maven.apache.org/maven2/net/java/dev/jna/jna/5.18.1/jna-5.18.1.aar", + "sha256": "7f053e3ec99e14dd71259c82c1c8a02738d64a13c31226b2acc170f3060951e0", + "static_status": "PASS", + "libraries": [ + { + "entry": "jni/arm64-v8a/libjnidispatch.so", + "sha256": "abc26e994517bcaa3309acdb0a27373864086c7569c89d3087b8626fada9ef06", + "load_min_p_align": 16384, + "load_alignment_pass": true, + "load_16kb_congruence_pass": true, + "load_declared_congruence_pass": true, + "relro": [ + { + "vaddr": 179096, + "memsz": 1128, + "end": 180224, + "end_mod_16384": 0, + "whole_matching_load_exemption": false + } + ], + "android17_effective_program_alignment": 16384, + "android17_load_page_overlap": false, + "static_status": "PASS", + "failures": [] + }, + { + "entry": "jni/x86_64/libjnidispatch.so", + "sha256": "3809247e9b804a05ed8377b87d545cf5b5e97f960f75e725bc8497b40260e417", + "load_min_p_align": 16384, + "load_alignment_pass": true, + "load_16kb_congruence_pass": true, + "load_declared_congruence_pass": true, + "relro": [ + { + "vaddr": 129792, + "memsz": 1280, + "end": 131072, + "end_mod_16384": 0, + "whole_matching_load_exemption": false + } + ], + "android17_effective_program_alignment": 16384, + "android17_load_page_overlap": false, + "static_status": "PASS", + "failures": [] + } + ] + } + ] +} diff --git a/release/r4-android-16kb.md b/release/r4-android-16kb.md new file mode 100644 index 0000000000..27255363af --- /dev/null +++ b/release/r4-android-16kb.md @@ -0,0 +1,236 @@ +# R4 Android 16 KB supplier correction and Local retest + +15 October 2026. State: **NEEDS_LOCAL / NOT RELEASE-READY**. This is a targeted +continuation of [#7045](https://github.com/KeeperCommunity/bitcoin-keeper/pull/7045), +preserving `698078e1765d1e0728145f0ef6def05195192b30`. The newest PR **Codex local +handoff** names the full final SHA; this file cannot contain its own eventual SHA. +Read [AGENTS.md](../AGENTS.md), the [complete native handoff](r4-automatic-backup-local-handoff.md) +and [backup matrix](../openspec/changes/fix-recovery-backup-freshness/automatic-write-validation.md). + +## Failure and supplier identity + +The [retained exact-698 Local report](https://github.com/KeeperCommunity/bitcoin-keeper/pull/7045#issuecomment-6178691569) +records developmentDebug 2.6.4(627), package `io.hexawallet.keeper.development`, APK +SHA256 `8d73e790f9b47766c92e927d7fe8425ad4967cfe44607cf35312cbb87e469af8`. +A genuinely fresh Android17/API37 arm64 emulator reported `PAGE_SIZE=16384`. +Default launch reached Create Passcode and initialized MMKV files. After enabling +the official strict properties on that new emulator only, relaunch aborted: + +```text +lib/arm64-v8a/libjnidispatch.so program alignment (8192) cannot be smaller than system page size (16384) +Fatal signal 6 (SIGABRT) +``` + +The failing library's LOAD `p_align=0x10000` and STORED ZIP data offset `48709632` +both pass their respective alignment checks. Its GNU_RELRO end is `0x36000`, +with remainder `0x2000` modulo 16 KB. This is not a failed LOAD or ZIP check. + +Cloud downloaded official Maven Central AARs and reproduced the reported library +identity without accessing any Mac-local artifact: + +| Bytes | SHA256 | +| --- | --- | +| [Official JNA 5.14.0 AAR](https://repo.maven.apache.org/maven2/net/java/dev/jna/jna/5.14.0/jna-5.14.0.aar) | `81a8b9afc65f9d6b205338c25908af8de0ff8411a00985104c9d83fffdfab380` | +| Its raw arm64 library, 168176 bytes | `3730ce014f7a6807ae74af78c3b9f5a3cba1f5886316b3d3858493df923efe96` | +| Same library after LLVM 19.1.7 `llvm-strip --strip-unneeded`, 157720 bytes | `fe9c603f2a7a5d19197b87f275d6e0afb975f997a57ff4f8ca801cb8c5e7b731` | +| [Selected JNA 5.18.1 AAR](https://repo.maven.apache.org/maven2/net/java/dev/jna/jna/5.18.1/jna-5.18.1.aar) | `7f053e3ec99e14dd71259c82c1c8a02738d64a13c31226b2acc170f3060951e0` | +| Selected raw arm64 library | `abc26e994517bcaa3309acdb0a27373864086c7569c89d3087b8626fada9ef06` | + +Maven's published SHA1 sidecars independently matched 5.14.0 and 5.18.1 downloads +(`17b4b6c99bdc5586644d7f97fca4dd84d8889fb0` and +`8e93d068c4eab70ada4c79e010198a112aa3c581`). Local separately reproduced the +same 5.14.0 packaged hash with its trusted NDK27.1 strip tool. The new supplier +arm64 library has LOAD alignment `0x4000` and RELRO end `0x2c000`, remainder zero. +LLVM 19.1.7 stripping of the new arm64 library yields +`ddfc6c965bbe366615c000ec1f06552ddf0bb2b4f9d921d0bff8c75b3cb2ca4c`; +verify the actual build transformation rather than assuming this future APK hash. + +## Narrow dependency change + +The pinned [Portal bridge](https://github.com/bithyve/libportal-react-native/blob/2ff681b0b725009768acadb35b2731fb1a402fa3/android/build.gradle#L99) +declares `implementation "net.java.dev.jna:jna:5.14.0@aar"` without a debug-only +condition. Its Git pin is unchanged in `package.json` and `yarn.lock`. The app +retains arm64 for both flavors and excludes only x86_64 `libjnidispatch.so`. +This establishes production dependency/packaging input risk. It does not prove +the contents or behavior of an unbuilt signed production AAB or generated split. + +An app-level strict `implementation('net.java.dev.jna:jna:5.18.1@aar')` selects +one matched Java/native artifact for every app variant. No Portal source, native +library exclusion, compatibility switch, app-wide linker flag or readiness gate +is changed. A version conflict fails resolution instead of silently selecting +another version. + +The version is based on upstream fixes and actual binaries: + +| JNA | arm64 LOAD alignment | arm64 RELRO end | Result | +| --- | --- | --- | --- | +| 5.14.0 / 5.15.0 | 0x10000 | 0x36000 | FAIL, effective program alignment 8192 | +| 5.16.0 | 0x4000 | 0x2a000 | FAIL, effective program alignment 8192 | +| 5.17.0 | 0x4000 | 0x2c000 | First fixed native binary | +| 5.18.0 / 5.18.1 / 5.19.0 / 5.19.1 | 0x4000 | 0x2c000 | Same fixed arm64 native bytes | + +[JNA #1654](https://github.com/java-native-access/jna/pull/1654), commit +`5305913155092d793f806f51f3c3e0e85869c4da`, adds `common-page-size=16384`, rebuilds +natives and advances native ABI 7.0.3 to 7.0.4. Replacing only the `.so` would +break the matched supplier relationship. 5.18.0 fixes the Structure constructor +deadlock introduced in 5.16 ([#1681](https://github.com/java-native-access/jna/issues/1681)); +5.18.1 fixes the concurrent `sortFields` race reported by Android UniFFI +([#1686](https://github.com/java-native-access/jna/issues/1686)). Portal uses +concurrent UniFFI calls and nested JNA Structures. Thus 5.18.1 is the minimum +selected line including those relevant managed fixes, rather than the earliest +native-only fix. These races were not reproduced in Keeper. Static classfile +inspection found all 19 explicit Portal JNA member references in the selected +classes, with matching names/descriptors; this is not runtime compatibility proof. + +A resolution-only Gradle 8.14.3 project with the actual Portal declaration and +the app's strict declaration passed three tasks: original 5.14.0 baseline, +single 5.18.1 AAR on app compile/runtime paths with the transitive 5.14 request +upgraded, and rejection of an incompatible strict 5.19.1 request. The resolved +5.18.1 AAR matches the supplier SHA above. This harness is not an actual AGP +developmentDebug or productionRelease dependency graph; those remain Local checks. + +## Static inspection scope + +The [official Android guide](https://developer.android.com/guide/practices/page-sizes) +requires ELF, RELRO, packaging and runtime checks separately, and rebuilding or +reimporting affected prebuilts. Repository linker flags cannot change a supplier +prebuilt. The checker models the public Android17 bionic revision +[`06356e41c5ed7b12220b24c05ba4fdb873b126a7`](https://android.googlesource.com/platform/bionic/+/06356e41c5ed7b12220b24c05ba4fdb873b126a7/linker/linker_phdr_16kib_compat.cpp). +For a RELRO that exactly prefixes but does not cover its entire LOAD, that helper +uses the lowest set bit of its end: `lowbit(0x36000)=8192`. This matches the +observed diagnostic. The exact emulator linker revision was not inspected. + +The earlier 49/59 nonaligned RELRO ends are retained as a raw observation, not +49 demonstrated strict-mode crashes. A whole-LOAD RELRO is exempt in the +checked Android17 helper. The pinned Portal library has that layout on both +64-bit ABIs and passes the computed program-alignment/overlap checks; its raw +end remainder alone does not justify rebuilding or removing Portal. + +`scripts/check-android-16kb.py` separately records each supplied library's SHA, +LOAD alignment/congruence, raw RELRO end, whole-LOAD exception, Android17 +effective alignment/LOAD-page overlap and actual APK local-header data offset. +AAR ZIP offsets are not APK evidence. Compressed APK entries report +`NOT_PROVEN` because extraction/manifest behavior is not established. Its PASS +means only the supplied static checks passed, never that Android ran the app. + +The [supplier inventory](evidence/r4-android-16kb-suppliers-2026-10-15.json) +records 50 64-bit entries across 13 explicitly downloaded AAR inputs, including +both old/fixed JNA and duplicate debug/release shared runtimes. This is not a +count of retained APK libraries. The 48 entries outside old JNA pass these static +checks. Old JNA arm64 fails effective8192; old JNA x86_64 also fails LOAD4KB but +that ABI's JNA is already excluded by this app. The other inspected inputs are: + +| Supplier input | Inspected 64-bit entries | Static result | +| --- | --- | --- | +| React Android0.83.9 debug + release | 10 + 10 | PASS | +| Hermes Android0.14.1 debug + release | 2 + 2 | PASS | +| fbjni0.7.0 | 4 | PASS | +| pdfiumandroid1.0.32 | 4 | PASS | +| Sentry native0.13.3 | 4 | PASS | +| Pinned Portal0.3.4-SNAPSHOT | 2 | PASS, whole-LOAD exception | +| MMKV2.4.0 | 4 | PASS | +| MLKit barcode17.3.0 | 2 | PASS | +| CameraX1.5.0-alpha03 image processing | 2 | PASS | + +These are supplier-byte observations. Actual resolved graph selection, +`libc++_shared.so` pick-first identity and source-linked Realm/RN/autolinked +outputs must be inventoried from the rebuilt APK. A static source archive or +repository linker option cannot establish their final ELF layout. + +Cloud cannot access the Mac's retained APK or full 59-library scan. Downloading +the existing hosted diagnostic APK was blocked at the signed artifact host with +HTTP403. That older artifact is production-flavor/public-debug-key/placeholder +configuration and is not the reported development APK or a signed production +release. Therefore exact-APK supplier mapping and all packaged/source-linked +libraries remain required; a supplier sample is not a complete APK inventory. + +## Actual Cloud checks + +- Checker regression suite: PASS 20/20, including failing JNA-style RELRO prefix, + corrected prefix, whole-LOAD exception, overlapping LOAD pages, malformed ELF, + APK local-header offsets, duplicate entries and compressed-entry uncertainty. +- Genuine Node22.23.3/native Realm/transactional loopback Relay matrix: PASS + 263/263, zero skips, paired Relay `838049e537e07eb646a2a0393eb969fba43d0739`. +- Recovery: PASS 448/448, zero skips. Jest: PASS 42 suites / 432 tests. +- Release rules: PASS 28 Node and 7 Python tests. Ruby fake-action regressions: + NOT RUN here, `ruby` unavailable; the combined release command exited127. +- Version consistency: PASS 2.6.4 / Android627 / iOS620, still provisional. +- Release preflight: FAIL; both authenticated store baselines exceed24hours. +- Full TypeScript/lint: NOT RUN for this Android-only dependency change. The + prior inherited failures remain failures, not new clean results. +- Actual AGP variant graphs, new native build, install, launch, MMKV/Realm, + Portal/wallet/backup smoke, physical hardware and production AAB/splits: + **NOT RUN in Cloud**. No Android SDK or interactive emulator is installed here. + +The tests above used this source change with the unchanged package/Yarn inputs +and existing dependency caches; the final commit adds this evidence/handoff. +Neither hosted-backup gate nor approved contributor pins changed. The manifest +SHA256 remains `2706b8a1c250c93d4098384201cba0188c667777b8fa2d2fdc5267380e0ccfcc`. + +## Exact-head Local continuation + +1. Resolve the newest PR handoff and verify its full SHA equals the live head, + using the safe isolated setup in the complete handoff. Preserve all owner + apps/data and old test artifacts. Keep Relay `838049e537e07eb646a2a0393eb969fba43d0739` + separate from the approved contributor stack. No paid/real provider calls. +2. With the source's JDK17/SDK36/NDK27.1 toolchain and disposable `.env.local`, + capture `:app:dependencyInsight --dependency net.java.dev.jna:jna` for + `--configuration developmentDebugRuntimeClasspath`, + `developmentDebugCompileClasspath` and `productionReleaseRuntimeClasspath`. + Inspect `:libportal-react-native:dependencies` as well. Production graph + inspection is read-only, not permission to build/sign a production artifact. + Verify one matched 5.18.1 AAR; retain any standalone Portal compile5.14 edge + separately from the app's final compile/runtime selection. +3. Build **developmentDebug only** from the exact SHA with the disposable debug + certificate, for arm64. Record package/version/certificate/APK SHA and SDK/NDK. + Scan the actual APK and the resolved AAR, including every retained64-bit + library. Preserve JSON and failed results; do not filter the scan to JNA. + + ```bash + python3 -B -m unittest scripts/test_check_android_16kb.py + python3 scripts/check-android-16kb.py /actual/resolved/jna-5.18.1.aar + python3 scripts/check-android-16kb.py /actual/new-development.apk > /private/evidence/new-apk-16kb.json + "$ANDROID_SDK_ROOT/build-tools/35.0.0/zipalign" -c -P 16 -v 4 /actual/new-development.apk + ``` + + Attribute every `.so` to resolved Maven/local AAR or actual source-linked + output, particularly `libc++_shared.so` selected by `pickFirst`. Reconcile the + earlier59-entry scan and distinguish raw RELRO remainders from strict failures. + Verify JNA supplier-to-merged/stripped-to-APK byte identity using the actual + NDK strip transformation. No LOAD-only or AAR-only compatibility claim. +4. Use a **new disposable arm64 Android17/API37 16KB AVD only**. Prove serial/AVD, + ABI and `adb shell getconf PAGE_SIZE` =16384. Following official guidance, + set and read back `bionic.linker.16kb.app_compat.enabled=fatal` and + `pm.16kb.app_compat.disabled=true` on that AVD, before the strict install/launch + sequence. Never alter the owner's device properties or enable compatibility + to obtain a pass. Record actual install, force-stop/cold-launch and native + logs: no SIGABRT, linker alignment error, Java/native mismatch or swallowed + Portal initialization error. Portal SDK construction is eager; reaching PIN + alone is not full Portal acceptance. Also retain a normal4KB launch regression. +5. On the strict16KB runtime, actually exercise disposable PIN/onboarding, + native MMKV creation/cold persistence, encrypted Realm create/read/reopen, + wallet/vault/import/export and paired-loopback backup opt-in, complete + readback, automatic local-first mutation, recovery and relevant relaunch + regressions. Record Portal constructor/native dispatch and available safe + Portal UI/cancellation separately from unavailable NFC/card signing. Keep + Android C8/#7056 **parked and failed**; do not restart its fix/retest cycle. +6. Required local checks remain **IOS, ANDROID, HARDWARE**. Rerun relevant iOS + Debug simulator/PIN/Realm/wallet/backup smoke against the new exact SHA and + continue other feasible existing native requirements. Do not claim an old + artifact validates the new SHA. Hardware/NFC/USB tests are NOT RUN unless + actually performed; a new exact-SHA owner scope decision is needed for any + physical-only deferral. A locked Mac may permit command-line preparation, + but unperformed build/install/launch/UI checks remain BLOCKED/NOT RUN. +7. Post an exact-SHA **Codex local validation** comment with hashes, per-library + static results, strict-property/page-size proof, actual native outcomes and + every failed/blocked/unrun category. Preserve the698 LOCAL_FAILED evidence. + +Android C8 remains a separate known failure. Human review, current Local +acceptance, production backend all-instance/index/transaction/old-writer rollout +proof, fresh store histories and separate Desktop/website deliverables remain +open. Both backup gates and approved contributor manifest stay unchanged. +No merge, deployment, index creation, release signing, AAB/split construction, +upload, publication, customer message or real transaction is authorized here. +Production AAB/split identity and strict runtime verification require a separate +owner-approved artifact stage. R4 stays independent of Reproducibility and +Bitkey Wallet; this source correction is not release readiness. diff --git a/release/r4-automatic-backup-local-handoff.md b/release/r4-automatic-backup-local-handoff.md index 3c33b517d1..582704a51b 100644 --- a/release/r4-automatic-backup-local-handoff.md +++ b/release/r4-automatic-backup-local-handoff.md @@ -2,6 +2,8 @@ State: **NEEDS_LOCAL**. PR [#7045](https://github.com/KeeperCommunity/bitcoin-keeper/pull/7045), branch `codex/r4-264-combined-candidate`, base `sprint`. The exact final SHA is recorded in the newest **Codex local handoff** on that PR. The setup below verifies that handoff against the live head before selecting it; this repository document cannot contain its own eventual commit SHA. +**15 October Android continuation:** read the [16 KB supplier proof and targeted retest](r4-android-16kb.md) and retain the [698078 Local failure](https://github.com/KeeperCommunity/bitcoin-keeper/pull/7045#issuecomment-6178691569). The new JNA selection requires a new development build and actual strict-16-KB launch; no native result transfers to this head. Android C8 / [#7056](https://github.com/KeeperCommunity/bitcoin-keeper/issues/7056) remains owner-parked with its observed FAIL: do not restart that bug's fix/retest cycle. Other required native and release blockers remain open. Production AAB/generated-split construction, signing, upload and runtime verification are a separate owner-approved artifact stage. + Paired disposable Relay: **`838049e537e07eb646a2a0393eb969fba43d0739`**, draft [Relay #4](https://github.com/KeeperCommunity/Relay/pull/4). The originally requested `b600416e5ba7325927d7d2871cff7776f4e593fe` was tested: native Android opt-in reached a real repair write but failed complete readback because `createNewApp` had precreated an AppImage without subscription and repair wrote that field only on insert. The successor sets subscription on existing AppImages during explicit revisioned repair, after the transaction's revision and conflict checks. It is a loopback test dependency, not a deployed-service assertion. The approved contributor manifest in `dev/local-backend/sources.lock.json` remains Relay `ee935359f22ff7932ea0a62e9084d4b49801f4aa` and SigningServer `3cc7728c27328e7b0358d1f91a32d8d13feba90a`; do not replace those pins or overwrite generated contributor checkouts to prepare this test. `AUTOMATIC_BACKUP_SOURCE_READY` is true after genuine disposable proof and independent fixture review. It permits development loopback validation with an explicit port. `AUTOMATIC_BACKUP_RELAY_ROLLOUT_READY` remains false until the separately authorized deployed fleet, transactions/indexes, v2 route and old-writer drainage are verified. The Node fixture permits its explicit loopback test target. Hosted writes remain fenced. Do not change either gate to make a test pass. Current source and results are recorded in [the proof matrix](../openspec/changes/fix-recovery-backup-freshness/automatic-write-validation.md). @@ -15,6 +17,9 @@ The combined source also retains the bracketed iOS import and shared PIN keypad ```text Validate Keeper PR #7045 on the exact Head commit in its newest Codex local handoff. +For the Android 16 KB continuation, also follow release/r4-android-16kb.md. +Keep Android C8/#7056 parked; retain its observed FAIL without a fix/retest cycle. + Read the latest PR description, newest Codex local handoff, AGENTS.md and release/r4-automatic-backup-local-handoff.md at the final head. Read the complete automatic-write-validation.md matrix. Recheck the live PR head; if it differs, From 17e0d9b9e3daf39359b762baa99b899f20b95f96 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Thu, 15 Oct 2026 02:39:27 +0000 Subject: [PATCH 196/254] Handle truncated ZIP reads in portable Android checker --- scripts/check-android-16kb.py | 4 ++-- scripts/test_check_android_16kb.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/scripts/check-android-16kb.py b/scripts/check-android-16kb.py index 8abc3d2e22..d96535674d 100644 --- a/scripts/check-android-16kb.py +++ b/scripts/check-android-16kb.py @@ -207,8 +207,8 @@ def inspect_file(path): if item["apk_zip"]["status"] not in ("PASS", "NOT_APPLICABLE"): item["failures"].append("APK ZIP packaging is " + item["apk_zip"]["status"]) result["libraries"].append(item) - except (InspectionError, ValueError, RuntimeError, zipfile.BadZipFile, struct.error) as exc: - result["errors"].append(info.filename + ": " + str(exc)) + except (InspectionError, ValueError, RuntimeError, EOFError, zipfile.BadZipFile, struct.error) as exc: + result["errors"].append(info.filename + ": " + (str(exc) or type(exc).__name__)) else: raise InspectionError("expected an explicit .so, .aar, or .apk input") if not result["libraries"]: diff --git a/scripts/test_check_android_16kb.py b/scripts/test_check_android_16kb.py index 19e78d7c07..39db7f238f 100644 --- a/scripts/test_check_android_16kb.py +++ b/scripts/test_check_android_16kb.py @@ -2,6 +2,7 @@ import importlib.util import io +import json import pathlib import struct import subprocess @@ -10,6 +11,8 @@ import unittest import warnings import zipfile +from contextlib import redirect_stdout +from unittest.mock import patch SPEC = importlib.util.spec_from_file_location("check_android_16kb", pathlib.Path(__file__).with_name("check-android-16kb.py")) CHECK = importlib.util.module_from_spec(SPEC) @@ -145,6 +148,31 @@ def test_malformed_zip_local_extra_length_is_rejected(self): result = self.inspect_temp(payload, ".apk") self.assertEqual(result["status"], "FAIL") self.assertTrue(result["errors"]) + with tempfile.TemporaryDirectory() as directory: + path = pathlib.Path(directory) / "malformed.apk" + path.write_bytes(payload) + cli = subprocess.run([sys.executable, str(pathlib.Path(CHECK.__file__)), str(path)], capture_output=True, text=True, check=False) + self.assertEqual(cli.returncode, 1) + self.assertEqual(json.loads(cli.stdout)["artifacts"][0]["status"], "FAIL") + self.assertNotIn("Traceback", cli.stderr) + + def test_bare_zip_read_eof_is_recorded_and_cli_emits_failure_json(self): + with tempfile.TemporaryDirectory() as directory: + path = pathlib.Path(directory) / "truncated.apk" + path.write_bytes(native_zip(elf(relros=[]))) + # Python 3.9 zipfile can raise EOFError() with no message for a + # malformed local header; newer versions may raise BadZipFile. + with patch.object(CHECK.zipfile.ZipFile, "read", side_effect=EOFError()): + result = CHECK.inspect_file(path) + output = io.StringIO() + with redirect_stdout(output): + status = CHECK.main([str(path)]) + self.assertEqual(result["status"], "FAIL") + self.assertIn("lib/arm64-v8a/libexample.so: EOFError", result["errors"]) + self.assertEqual(status, 1) + recorded = json.loads(output.getvalue())["artifacts"][0] + self.assertEqual(recorded["status"], "FAIL") + self.assertIn("lib/arm64-v8a/libexample.so: EOFError", recorded["errors"]) def test_malformed_local_signature_is_rejected(self): payload = bytearray(native_zip(elf(relros=[]))) From 9a19c6a2a4b4b71d6ffb0d6a5b9776f12a7dbf1d Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Thu, 15 Oct 2026 03:51:50 +0000 Subject: [PATCH 197/254] Expire backup baselines only after inspection queue admission --- release/r4-automatic-backup-local-handoff.md | 2 + release/r4-backup-queue-admission.md | 170 ++++++++ src/services/backup/repair.ts | 4 +- .../automaticInspectionQueue.integration.cjs | 382 ++++++++++++++++++ 4 files changed, 557 insertions(+), 1 deletion(-) create mode 100644 release/r4-backup-queue-admission.md create mode 100644 tests/recovery/automaticInspectionQueue.integration.cjs diff --git a/release/r4-automatic-backup-local-handoff.md b/release/r4-automatic-backup-local-handoff.md index 582704a51b..ca54eef236 100644 --- a/release/r4-automatic-backup-local-handoff.md +++ b/release/r4-automatic-backup-local-handoff.md @@ -2,6 +2,8 @@ State: **NEEDS_LOCAL**. PR [#7045](https://github.com/KeeperCommunity/bitcoin-keeper/pull/7045), branch `codex/r4-264-combined-candidate`, base `sprint`. The exact final SHA is recorded in the newest **Codex local handoff** on that PR. The setup below verifies that handoff against the live head before selecting it; this repository document cannot contain its own eventual commit SHA. +**Queue-admission continuation:** follow the [narrow fix and focused Local retest](r4-backup-queue-admission.md). The only changed app module is backup inspection queue admission; native/dependency/configuration inputs are unchanged from17e0. Use incremental builds and one focused ordinary-rename/native smoke retest on both platforms. Preserve17e0 failures and independent test results; do not repeat unrelated large matrices or infer a uniquely proved native cause. The prior full R4 gaps remain tracked, C8/#7056 stays parked, and any further failure must be reported without another speculative fix loop. + **15 October Android continuation:** read the [16 KB supplier proof and targeted retest](r4-android-16kb.md) and retain the [698078 Local failure](https://github.com/KeeperCommunity/bitcoin-keeper/pull/7045#issuecomment-6178691569). The new JNA selection requires a new development build and actual strict-16-KB launch; no native result transfers to this head. Android C8 / [#7056](https://github.com/KeeperCommunity/bitcoin-keeper/issues/7056) remains owner-parked with its observed FAIL: do not restart that bug's fix/retest cycle. Other required native and release blockers remain open. Production AAB/generated-split construction, signing, upload and runtime verification are a separate owner-approved artifact stage. Paired disposable Relay: **`838049e537e07eb646a2a0393eb969fba43d0739`**, draft [Relay #4](https://github.com/KeeperCommunity/Relay/pull/4). The originally requested `b600416e5ba7325927d7d2871cff7776f4e593fe` was tested: native Android opt-in reached a real repair write but failed complete readback because `createNewApp` had precreated an AppImage without subscription and repair wrote that field only on insert. The successor sets subscription on existing AppImages during explicit revisioned repair, after the transaction's revision and conflict checks. It is a loopback test dependency, not a deployed-service assertion. The approved contributor manifest in `dev/local-backend/sources.lock.json` remains Relay `ee935359f22ff7932ea0a62e9084d4b49801f4aa` and SigningServer `3cc7728c27328e7b0358d1f91a32d8d13feba90a`; do not replace those pins or overwrite generated contributor checkouts to prepare this test. diff --git a/release/r4-backup-queue-admission.md b/release/r4-backup-queue-admission.md new file mode 100644 index 0000000000..3815b55de5 --- /dev/null +++ b/release/r4-backup-queue-admission.md @@ -0,0 +1,170 @@ +# R4 backup inspection queue admission + +15 October 2026. **NEEDS_LOCAL / NOT RELEASE-READY**. PR +[#7045](https://github.com/KeeperCommunity/bitcoin-keeper/pull/7045); the newest +Codex local handoff names the actual final SHA. This document cannot contain +its own eventual commit SHA. + +## Preserved evidence and narrow change + +The [17e0 investigation](https://github.com/KeeperCommunity/bitcoin-keeper/pull/7045#issuecomment-6183834923) +preserves the original native rename failure, three diagnostic outcomes and +separate required-behavior **0/1 RED** regression, with portable fixture code. +Its app source is `17e0d9b9e3daf39359b762baa99b899f20b95f96`; the unchanged +paired disposable Relay is `838049e537e07eb646a2a0393eb969fba43d0739`. +The three diagnostic cases passed by establishing a successful completed rename, +the inspection-overlap defect, and a missing-original-baseline uncertainty +control. Those are not three backup acceptance passes. + +`inspectBackup` previously expired the verified baseline before joining the +account queue. A later read-only inspection could therefore change the epoch +of an earlier current writer while that writer awaited its snapshot, CAS or +readback. Its original proof then failed closed, preserving local data and +pending state. The fix moves that invalidation into **every admitted `runOnce` +callback**, immediately after the captured-session stale return and before +`inspectOnce`. A coalesced caller does not acquire another admission. A retry +does acquire an admission and expires that admission's baseline, using the same +deadline. Stale A→B→A work returns before touching the new session's baseline. + +No original intent is rebased. Session/consent, runtime brands, Realm identity, +generation/fingerprint, original revision/CAS, single-use ciphertext and complete +readback checks are unchanged. Hosted writes remain fenced. The source gate +stays true for approved development loopback; the rollout gate stays false. + +This confirms a source ordering defect, **not the unique cause of the native +17e0 failure**. The modal normally closes after its upload attempt settles. +The reported Save→Home interval and later Status visit do not prove overlap; +concurrent/background inspection timing or missing dispatch-time authority +remains unestablished. Preserve the original local rename/unchanged remote +revision/pending evidence and the measured14.170s host/guest clock offset. +Later Local evidence at17e0 records one explicit **Back Up Now → conflict / +Backup needs review**, with unchanged remote revision fingerprint +`bfc41e774d5b0a53` (1wallet/2signers, no vaults/labels), and matching selected +account/live Realm/store. This is a separate actual **FAIL**, not covered or +resolved by the ordering fix. The conservative `replacementWouldDiscardRecovery` +guard compares recovery content, including presentation changes; safe field-level +differences are still being assessed read-only. No manual retry, override or +change to that guard is authorized in this cycle. + +## Cloud proof and boundaries + +The focused genuine matrix is +`tests/recovery/automaticInspectionQueue.integration.cjs`. It uses actual +encrypted Realm and production transaction/schema/crypto/producer/inspection/ +store statements, with real HTTP to the paired transactional Relay. The +Node22 full genuine runner discovers this matrix in addition to the seven +existing matrices and requires a native Realm write/read prerequisite. + +Required coverage includes writer-first checks requested before admission and +during original snapshot/CAS/readback; inspection-first later work and missing +original authority staying local-preserved/pending; old-session queued +inspection not erasing a fresh-session baseline; per-admission retry invalidation, +shared deadline and coalescing. Controlled HTTP schedules and any host clock or +public baseline-registration seams are disclosed in the tests. They do not +constitute native navigation/dispatch/MMKV or simulator/emulator execution. + +The final PR handoff records exact commands, counts, skips and review results. +Historical17e0 results remain SHA-scoped. No source fixture pass is an iOS or +Android build/install/launch pass. + +The focused file passed **12/12, zero skipped** on Node22.23.3 with real +Realm12.15.0 and paired Relay838049. The first focused run was11/12: its retry +test's deliberate thrown snapshot callback re-entered the fixture response +handler and produced an HTML error instead of the intended setup read. Only +that test was corrected to hold a genuine HTTP reply until the actual client +abort; the source patch did not change. The final retry case compresses transport +abort timers to1000ms, delegates actual HTTP, and asserts the same unmodified +inspection deadline across attempts. No host inspection clock is replaced. + +The stale-session/retry cases deliberately exercise the public baseline +registration boundary. A real HTTP snapshot must strictly decrypt, match the +full native Realm image and app metadata, with stable session/Realm handle/ +fingerprint, before recording it. This is not a claim that a nested queued +native inspection can run while its own account queue is held. The stale-session +test also uses the preserved fresh baseline for a real subsequent rename CAS. +Independent read-only source/test review found no authority rebase or guard/gate +change. The completed Cloud results for this implementation are: + +| Check | Actual outcome | +| --- | --- | +| `npm run test:automatic-backup-genuine`, Node22.23.3/native Realm12.15.0/paired Relay838049 | PASS275/275, zero skipped/cancelled;168791.623271ms; native write/read prerequisite passed | +| New `automaticInspectionQueue.integration.cjs` alone | PASS12/12, zero skipped/cancelled;10982.202962ms | +| Preserved required-behavior case, changing only its fixture import to this source | GREEN1/1, zero skipped; original17e0 RED0/1 remains preserved separately | +| `npm run test:recovery` | PASS448/448, zero skipped;42324.276752ms | +| `npm test -- --runInBand --coverage=false` | PASS42suites/432tests;46.243s | +| Release rule components | PASS28Node +7Python +21Ruby/117assertions, zero failures/skips; Ruby3.3.8 ran offline in a read-only source container, not Fastlane actions | +| `npm run version:check` | PASS2.6.4 / Android627 / iOS620; counters remain provisional | +| `eslint src/services/backup/repair.ts`, diff and changed handoff links | PASS | +| Whole-project TypeScript/ESLint | NOT RUN here; historical inherited failures are not new clean results | +| New-head native build/install/launch, MMKV/Realm/UI, hardware | NOT RUN in Cloud; NEEDS_LOCAL | + +The tests used the exact minimal source patch with the unchanged dependency +caches; the final commit adds the recorded evidence/handoff. The genuine runner +used the pinned disposable Mongo7.0 image and source identities documented in +the complete Local handoff. Ruby used +`ruby@sha256:307aed56240959e9cd3fcc10740ae8d0e6c461e9b42efc076100d4f8126b87b9` +with `--network none --read-only`, a read-only source mount and tmpfs/tmp. +None of these checks signs, builds or publishes a production release. Final +push/hosted-CI status and full source SHA are recorded in the fresh PR handoff. + +## Focused Codex Local continuation + +Read the newest exact-head PR handoff, [AGENTS.md](../AGENTS.md), this document +and the preserved17e0 investigation before selecting a clean isolated checkout. +Check the live head again. Preserve existing owner apps, QA accounts/databases, +failed artifacts/logs and ongoing independent17e0 checks; do not reset or clean +their worktrees or overwrite failed evidence. + +The only changed app module is `src/services/backup/repair.ts`. Native/build +inputs remain byte-identical to17e0: `android/`, `ios/`, `package.json`, +`yarn.lock`, `patches/`, JNA5.18.1/Portal pins and exclusions, build versions, +tracked configuration code/templates and approved contributor manifest. Backup transport, +automatic-write implementation/guards and both readiness constants are unchanged. +Use existing matching dependencies, CocoaPods/Gradle/native caches for incremental +builds; do not rewrite locks or reinstall unrelated inputs to obtain a pass. +Actual new-artifact install and launch are still required. + +Required local checks remain **IOS, ANDROID, HARDWARE**. The focused retest is: + +1. Build from the actual new SHA: Android **developmentDebug only** and iOS + simulator **Debug**, using the established disposable loopback configuration + and paired Relay838049. Use the existing functional simulator development + signing if required for Keychain; no release signing or production artifact. + Ensure Metro or the packaged JS comes from this exact checkout. Record source, + package/bundle ID, version/counter, artifact SHA/certificate and launched device. +2. Actually install/launch both artifacts. Preserve the17e0 strict16KB/normal4KB + and iOS compile evidence as historical, not new-head passes. On the disposable + strict16KB arm64 Android17/API37 AVD, confirm PAGE_SIZE16384 and the established + fatal/true properties; do not enable compatibility. Do targeted native + Portal initialization/MMKV/Realm/PIN/wallet smoke. Native inputs are unchanged; + do not repeat unrelated large static/provider/hardware matrices for this patch. + Verify the new artifact's identity and continued strict launch rather than + transferring the old artifact's result. +3. In a separate disposable QA account/dataset on each platform, establish a + complete Verified backup, perform ordinary BTC Mobile Wallet rename, and + observe local persistence plus the intended revisioned write/readback. Open + AssistedBackupStatus for a fresh check; require the new name remotely and + Verified at the current local generation. Include one controlled overlapping + read-only check if the existing approved fixture/operator can schedule it, + recording the actual sequence. Do not add broad app instrumentation. +4. Cold-launch/unlock and confirm the renamed local wallet and truthful pending/ + fresh-verification behavior, without replaying old ciphertext. Keep the + original17e0 failed account/artifact/evidence intact. Missing baseline, + concurrent local change or another failure must remain pending rather than + borrowing a new baseline. Preserve the separate17e0 manual conflict; do not + press manual repair again or override it to hide a failed result. This patch + does not claim to fix that conflict. +5. Post actual per-platform results for the new SHA, retaining all unresolved + R4/native/release categories separately. A known native failure is not waived + by Cloud proof. If another failure remains, preserve it and report; **do not + start another speculative source fix/rebuild loop** under this handoff. + +Keep Android C8/#7056 owner-parked and failed; this patch does not retest, resolve +or waive it. Physical-only deferral still requires the owner's new exact-SHA +scope decision; retain HARDWARE and actual NOT RUN boundaries. Existing source +reviews, full native acceptance gaps, hosted backend fleet/index/transaction/ +old-writer rollout, current store/config/production artifact evidence and separate +Desktop/website deliverables remain open. No merge, deployment/index mutation, +release signing, production AAB/split construction, upload/publication, customer +message, transaction broadcast or new real/paid provider call is authorized. +R4 remains independent of Reproducibility and Bitkey Wallet. diff --git a/src/services/backup/repair.ts b/src/services/backup/repair.ts index cc02f5f60c..4187dda641 100644 --- a/src/services/backup/repair.ts +++ b/src/services/backup/repair.ts @@ -205,7 +205,6 @@ export function inspectBackup( if (running.phase) onPhase(running.phase); return running.promise; } - clearVerifiedRemoteRevision(appId); const listeners = new Set([onPhase]); const notify = (phase: RepairPhase) => { const active = inFlight.get(operationKey); @@ -479,6 +478,9 @@ export function inspectBackup( notify('unverified'); return Promise.resolve('unverified' as RepairPhase); } + // Invalidate only after admission, so a later inspection cannot cancel + // an earlier writer. Each admitted retry must expire its own baseline. + clearVerifiedRemoteRevision(appId); return inspectOnce(allowRetry); }); const result: Promise = runOnce(true).then(async (phase) => diff --git a/tests/recovery/automaticInspectionQueue.integration.cjs b/tests/recovery/automaticInspectionQueue.integration.cjs new file mode 100644 index 0000000000..d1b91141d6 --- /dev/null +++ b/tests/recovery/automaticInspectionQueue.integration.cjs @@ -0,0 +1,382 @@ +// Native encrypted Realm, production wallet/inspection sagas and real AES/CAS +// HTTP into the pinned transactional Relay. Scheduling gates hold real replies; +// they never replace backup proof, crypto, persistence or acknowledgement data. +const assert = require('node:assert/strict'); +const { test: nodeTest, before, after } = require('node:test'); +const { + createRealmClient, declarations, evaluate, Realm, +} = require('./automaticWriteRealmRelay.fixture.cjs'); + +const fixtureModule = process.env.KEEPER_RELAY_FIXTURE_MODULE; +assert.ok(fixtureModule, 'Set KEEPER_RELAY_FIXTURE_MODULE to the pinned production Relay fixture'); +const { createFixture } = require(fixtureModule); +let server; +before(async () => { server = await createFixture(); }); +after(async () => { await server?.close(); Realm.shutdown(); }); + +const writes = () => server.trace.filter(event => event.path === '/revisionedBackupMutation'); +const snapshots = () => server.trace.filter(event => event.path === '/getBackupSnapshot'); +const initialName = 'Initial wallet'; +const renamed = 'Committed rename'; +const test = (name, run) => nodeTest(name, { timeout: 15_000 }, run); + +function deferred(t) { + let resolve; + const promise = new Promise(done => { resolve = done; }); + t.after(() => resolve()); + return { promise, resolve }; +} + +async function waitFor(predicate, message) { + const deadline = Date.now() + 5000; + while (!predicate()) { + assert.ok(Date.now() < deadline, message); + await new Promise(resolve => setTimeout(resolve, 2)); + } +} + +function wallet(f) { + return f.defaults(f.RealmSchema.Wallet, { + id: 'wallet', entityKind: f.enums.EntityKind.WALLET, type: f.enums.WalletType.DEFAULT, + networkType: f.enums.NetworkType.TESTNET, scriptType: 'P2WPKH', + derivationDetails: f.defaults(f.RealmSchema.WalletDerivationDetails, { + instanceNum: 0, xDerivationPath: "m/84'/1'/0'", + }), + presentationData: f.defaults(f.RealmSchema.WalletPresentationData, { + name: initialName, visibility: 'DEFAULT', + }), + specs: f.defaults(f.RealmSchema.WalletSpecs, { + xpub: 'intentionally-unspendable-public-fixture', nextFreeAddressIndex: 1, + }), + }); +} + +async function setup(t, options = {}) { + await server.reset(); + const f = await createRealmClient(server.url, options); + t.after(() => f.close()); + f.automatic = f.load('src/services/backup/automatic'); + f.installProductionStoreObservers(f.automatic); + f.store.dispatch(f.bhr.setAutomaticCloudBackup({ appId: f.appId, enabled: true })); + assert.equal(f.dbManager.createObject(f.RealmSchema.Wallet, wallet(f)), true); + await server.seed(await f.payloadForCurrentImage()); + assert.equal(await f.repair.inspectBackup(f.appId, false, () => {}), 'verified'); + const sagas = f.productionSagas([ + 'currentBackupAppId', 'buildAutomaticAppMutation', 'queueCommittedBackupWorker', + 'updateAppImageWorker', 'backupInspectionOwners', 'runBackupInspection', + ]); + evaluate(declarations('src/store/sagas/wallets.ts', [ + 'currentWalletAccountId', 'assertWalletAccount', 'updateWalletDetailsWorker', + ]), sagas.scope, 'production-rename-producer.ts'); + f.status = (session = f.transport.currentBackupAccountSession()) => + sagas.run('runBackupInspection', false, f.appId, false, session); + f.rename = () => { + // Use the actual origin middleware, including its runtime-branded original + // intent. Host runSaga delivers the stamped action to the unchanged worker; + // this is not rendered React Native UI or native navigation timing. + const action = f.store.dispatch({ + type: 'UPDATE_WALLET_DETAILS', + payload: { wallet: wallet(f), details: { name: renamed, description: 'Local description' } }, + }); + f.renameAction = action; + return sagas.run('updateWalletDetailsWorker', action); + }; + f.localName = () => f.dbManager.getObjectById(f.RealmSchema.Wallet, 'wallet').presentationData.name; + f.remoteImage = async () => f.image.decodeImage(f.encryption.generateEncryptionKey(f.seed), + await server.snapshot(f.appId), f.appId, () => {}, true); + f.initialSnapshot = await server.snapshot(f.appId); + server.clearTrace(); + return f; +} + +async function assertConfirmed(f) { + const trace = [...server.trace]; + assert.equal(writes().length, 1); + assert.equal(writes()[0].operation, 'updateAppImage'); + assert.equal(writes()[0].committed, true); + assert.equal(f.localName(), renamed); + const remote = await f.remoteImage(); + assert.equal(remote.wallets.wallet.presentationData.name, renamed); + assert.equal(await f.image.compareImages(await f.repair.captureBackupImage(), remote), 'matched'); + const final = await server.snapshot(f.appId); + assert.notEqual(final.revision, f.initialSnapshot.revision); + assert.equal(final.revision, writes()[0].response.newRevision); + assert.ok(f.transport.captureBackupWriteIntent(f.appId)); + assert.equal(f.automatic.isBackupVerificationCurrent(f.appId), true); + const bhr = f.store.getState().bhr; + assert.equal(bhr.pendingAllBackupByAppId[f.appId], false); + assert.equal(bhr.backupRepairStateByAppId[f.appId], 'verified'); + assert.equal(bhr.backupRepairCompletedByAppId[f.appId], true); + assert.equal(bhr.backupRepairRunningByAppId[f.appId], false); + assert.deepEqual(trace.map(event => event.path), [ + '/getBackupSnapshot', '/revisionedBackupMutation', '/getBackupSnapshot', '/getBackupSnapshot', + ]); +} + +async function assertPending(f) { + assert.equal(f.localName(), renamed); + assert.equal(writes().length, 0); + assert.equal((await server.snapshot(f.appId)).revision, f.initialSnapshot.revision); + assert.equal((await f.remoteImage()).wallets.wallet.presentationData.name, initialName); + assert.equal(f.store.getState().bhr.pendingAllBackupByAppId[f.appId], true); + assert.equal(f.automatic.isBackupVerificationCurrent(f.appId), false); +} + +test('ordinary dispatched rename finishes one real CAS before a later read-only status check', async t => { + const f = await setup(t); + assert.equal(await f.rename(), true); + assert.equal(await f.status(), true); + await assertConfirmed(f); +}); + +test('writer-first: a check queued before writer admission cannot revoke its originating baseline', async t => { + const f = await setup(t); + const entered = deferred(t); + const release = deferred(t); + const blocker = f.transport.withBackupSession(f.appId, () => { + entered.resolve(); return release.promise; + }); + await entered.promise; + const epoch = f.transport.backupBaselineEpoch(f.appId); + const writing = f.rename(); + const checking = f.status(); + const epochAfterRequest = f.transport.backupBaselineEpoch(f.appId); + const requestsBeforeAdmission = server.trace.length; + release.resolve(); + await blocker; + assert.equal(await writing, true); + assert.equal(await checking, true); + assert.equal(f.localName(), renamed); + assert.ok(f.renameAction.originBackupWriteIntent); + assert.equal(epochAfterRequest, epoch); + assert.equal(requestsBeforeAdmission, 0); + await assertConfirmed(f); +}); + +for (const [stage, afterSnapshot] of [['initial snapshot', 0], ['final readback', 1]]) { + test(`writer-first: read-only inspection requested during ${stage} waits without changing its proof`, async t => { + const f = await setup(t); + const entered = deferred(t); + const release = deferred(t); + let checking; + let epochBeforeCheck; + let epochAfterCheck; + server.armFault({ mode: 'before-snapshot-reply', after: afterSnapshot, beforeReply() { + epochBeforeCheck = f.transport.backupBaselineEpoch(f.appId); + checking = f.status(); + epochAfterCheck = f.transport.backupBaselineEpoch(f.appId); + entered.resolve(); + return release.promise; + } }); + const writing = f.rename(); + await entered.promise; + assert.equal(writes().length, afterSnapshot); + if (afterSnapshot) assert.equal(writes()[0].committed, true); + release.resolve(); + assert.equal(await writing, true); + assert.equal(await checking, true); + assert.equal(epochAfterCheck, epochBeforeCheck); + await assertConfirmed(f); + }); +} + +test('writer-first: a check during the genuinely committed but held CAS acknowledgement waits', async t => { + const f = await setup(t); + server.armFault({ mode: 'delay-reply', operation: 'updateAppImage', delayMs: 1200 }); + let settled = false; + const writing = f.rename().then(result => { settled = true; return result; }); + await waitFor(() => writes()[0]?.committed, 'The actual Relay CAS must commit before the held reply'); + assert.equal(settled, false, 'The client must still be awaiting its real acknowledgement'); + const epoch = f.transport.backupBaselineEpoch(f.appId); + const checking = f.status(); + const epochAfterCheck = f.transport.backupBaselineEpoch(f.appId); + assert.equal(await writing, true); + assert.equal(await checking, true); + assert.equal(epochAfterCheck, epoch); + await assertConfirmed(f); +}); + +test('inspection-first: a later captured rename receipt keeps its local commit but cannot borrow a new baseline', async t => { + const f = await setup(t); + const entered = deferred(t); + const release = deferred(t); + const blocker = f.transport.withBackupSession(f.appId, () => { + entered.resolve(); return release.promise; + }); + await entered.promise; + const checking = f.status(); + const writing = f.rename(); + const originatingIntent = f.renameAction.originBackupWriteIntent; + release.resolve(); + await blocker; + assert.equal(await checking, false); + assert.equal(await writing, true, 'Cloud rejection must not undo the local rename'); + assert.ok(originatingIntent, 'Later dispatch captured only the original verified base'); + assert.equal(f.store.getState().bhr.backupRepairStateByAppId[f.appId], 'different'); + await assertPending(f); +}); + +test('inspection already admitted: a new rename has no original baseline and its local data survives the read-only retry', async t => { + const f = await setup(t); + const entered = deferred(t); + const release = deferred(t); + server.armFault({ mode: 'before-snapshot-reply', beforeReply() { + entered.resolve(); return release.promise; + } }); + const checking = f.status(); + await entered.promise; + const writing = f.rename(); + assert.equal(f.renameAction.originBackupWriteIntent, undefined); + release.resolve(); + assert.equal(await writing, true); + assert.equal(await checking, false); + assert.equal(snapshots().length, 2, 'Only the read-only inspection retries after the queued local change'); + await assertPending(f); +}); + +test('missing original verified baseline still preserves a rename and cannot create upload authority', async t => { + const f = await setup(t); + f.transport.clearVerifiedRemoteRevision(f.appId); + assert.equal(await f.rename(), true); + assert.equal(f.renameAction.originBackupWriteIntent, undefined); + assert.equal(await f.status(), false); + assert.equal(f.store.getState().bhr.backupRepairStateByAppId[f.appId], 'different'); + await assertPending(f); +}); + +async function recordMatchedBaseline(f) { + // Explicit public-provider-registration boundary for stale-owner/retry + // tests, not a nested queued inspection. Create no invented revision or + // ciphertext: real HTTP must decrypt/compare to current native Realm before + // the production recorder runs, with metadata/handle/session/fingerprint held. + const snapshotTools = f.load('src/services/backup/localSnapshot'); + const database = f.nativeRealm(); + const session = f.transport.currentBackupAccountSession(); + const before = snapshotTools.captureLocalRecovery(database); + const current = await server.snapshot(f.appId); + const remote = await f.image.decodeImage(f.encryption.generateEncryptionKey(f.seed), + current, f.appId, () => {}, true); + assert.equal(await f.image.compareImages(await f.repair.captureBackupImage(), remote), 'matched'); + assert.equal(snapshotTools.backupMetadataMatches(before.app, current.appImage), true); + assert.equal(f.nativeRealm(), database); + assert.equal(f.transport.currentBackupAccountSession(), session); + assert.equal(snapshotTools.captureLocalRecovery(database).fingerprint, before.fingerprint); + f.transport.recordVerifiedRemoteRevision(f.appId, current.revision, session); + assert.equal(f.automatic.isBackupVerificationCurrent(f.appId), true); + return f.transport.captureBackupWriteIntent(f.appId); +} + +test('an old A session inspection queued before A to B to A cannot erase the new session baseline', async t => { + const f = await setup(t); + const entered = deferred(t); + const release = deferred(t); + const blocker = f.transport.withBackupSession(f.appId, () => { + entered.resolve(); return release.promise; + }); + await entered.promise; + const oldSession = f.transport.currentBackupAccountSession(); + const oldCheck = f.repair.inspectBackup(f.appId, false, () => {}, false, oldSession); + await f.selectAccount('disposable-other-account'); + await f.selectAccount(f.appId); + assert.notEqual(f.transport.currentBackupAccountSession(), oldSession); + const fresh = await recordMatchedBaseline(f); + const epoch = f.transport.backupBaselineEpoch(f.appId); + const generation = f.automatic.currentCommittedBackupGeneration(f.appId); + const session = f.transport.currentBackupAccountSession(); + assert.equal(f.transport.isCapturedBackupWriteIntent(fresh), true); + server.clearTrace(); + release.resolve(); + await blocker; + assert.equal(await oldCheck, 'unverified'); + assert.equal(f.transport.backupBaselineEpoch(f.appId), epoch); + assert.equal(f.automatic.currentCommittedBackupGeneration(f.appId), generation); + assert.equal(f.transport.currentBackupAccountSession(), session); + assert.equal(f.transport.captureBackupWriteIntent(f.appId).baseRevision, fresh.baseRevision); + assert.equal(f.automatic.isBackupVerificationCurrent(f.appId), true); + assert.equal(server.trace.length, 0, 'A stale queued inspection cannot read for the new session'); + assert.equal(await f.rename(), true, 'The preserved baseline must remain usable by a fresh production producer'); + assert.equal(await f.status(), true); + await assertConfirmed(f); +}); + +test('coalesced read-only requests share one inspection and do not clear the baseline again', async t => { + const f = await setup(t); + const entered = deferred(t); + const release = deferred(t); + server.armFault({ mode: 'before-snapshot-reply', beforeReply() { + entered.resolve(); return release.promise; + } }); + const firstPhases = []; + const secondPhases = []; + const first = f.repair.inspectBackup(f.appId, false, phase => firstPhases.push(phase)); + await entered.promise; + const epoch = f.transport.backupBaselineEpoch(f.appId); + const second = f.repair.inspectBackup(f.appId, false, phase => secondPhases.push(phase)); + assert.equal(second, first); + assert.equal(f.transport.backupBaselineEpoch(f.appId), epoch); + assert.deepEqual(secondPhases, ['checking']); + release.resolve(); + assert.equal(await first, 'verified'); + assert.equal(await second, 'verified'); + assert.deepEqual(firstPhases, ['checking', 'verified']); + assert.deepEqual(secondPhases, ['checking', 'verified']); + assert.equal(snapshots().length, 1); + assert.equal(writes().length, 0); +}); + +test('a read-only transient retry owns its new admission and retains the original deadline', async t => { + // Compress only actual transport abort timers. Axios still performs real + // HTTP and aborts a genuinely held reply; the inspection clock/deadline and + // every production retry/queue guard remain unchanged. + const f = await setup(t, { backupTimerCeilingMs: 1000 }); + const deadlines = []; + const originalPost = f.transport.boundedBackupPost; + f.transport.boundedBackupPost = (...args) => { + deadlines.push(args[2]); + return originalPost(...args); + }; + t.after(() => { f.transport.boundedBackupPost = originalPost; }); + const admitted = deferred(t); + const release = deferred(t); + const delayedReply = deferred(t); + let intervening; + let publishedEpoch; + server.armFault({ mode: 'before-snapshot-reply', beforeReply() { + intervening = f.transport.withBackupSession(f.appId, async () => { + await recordMatchedBaseline(f); + publishedEpoch = f.transport.backupBaselineEpoch(f.appId); + admitted.resolve(); + await release.promise; + }); + return delayedReply.promise; + } }); + const phases = []; + const checking = f.repair.inspectBackup(f.appId, false, phase => phases.push(phase)); + await admitted.promise; + assert.equal(f.automatic.isBackupVerificationCurrent(f.appId), true); + // Observe the second actual HTTP request, not a fake reply or a new budget. + server.armFault({ mode: 'before-snapshot-reply', beforeReply() { + assert.equal(f.transport.backupBaselineEpoch(f.appId), publishedEpoch + 1); + assert.equal(f.transport.captureBackupWriteIntent(f.appId), undefined); + } }); + release.resolve(); + await intervening; + assert.equal(await checking, 'verified'); + assert.equal(deadlines.length, 2); + assert.equal(deadlines[0], deadlines[1], 'Retry must not receive a fresh ten-minute budget'); + assert.ok(deadlines[0] > Date.now()); + assert.deepEqual(phases, ['checking', 'checking', 'verified']); + assert.equal(snapshots().length, 3, 'Two inspection attempts plus the real matched-baseline setup read'); + assert.equal(writes().length, 0); +}); + +test('a malformed snapshot is not retried or granted mutation authority', async t => { + const f = await setup(t); + server.armFault({ mode: 'malformed-json' }); + assert.equal(await f.status(), false); + assert.equal(snapshots().length, 1); + assert.equal(writes().length, 0); + assert.equal(f.transport.captureBackupWriteIntent(f.appId), undefined); + assert.equal(f.store.getState().bhr.pendingAllBackupByAppId[f.appId], true); + assert.equal(f.store.getState().bhr.backupRepairStateByAppId[f.appId], 'unverified'); +}); From 4927ad549dd1d610d9eb84c6d9a37ed0a6f33a29 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Thu, 15 Oct 2026 04:30:58 +0000 Subject: [PATCH 198/254] Back up committed Electrum bootstrap nodes with original authority --- release/r4-automatic-backup-local-handoff.md | 2 + release/r4-node-bootstrap-backup.md | 202 ++++++++ src/store/sagas/network.ts | 137 ++++-- src/store/sagas/send_and_receive.ts | 8 +- src/store/sagas/wallets.ts | 12 +- src/store/store.ts | 1 + .../automaticNodeBootstrap.integration.cjs | 454 ++++++++++++++++++ .../automaticWriteRealmRelay.fixture.cjs | 6 +- tests/services/networkConnectRace.test.ts | 26 +- 9 files changed, 797 insertions(+), 51 deletions(-) create mode 100644 release/r4-node-bootstrap-backup.md create mode 100644 tests/recovery/automaticNodeBootstrap.integration.cjs diff --git a/release/r4-automatic-backup-local-handoff.md b/release/r4-automatic-backup-local-handoff.md index ca54eef236..df4871d780 100644 --- a/release/r4-automatic-backup-local-handoff.md +++ b/release/r4-automatic-backup-local-handoff.md @@ -2,6 +2,8 @@ State: **NEEDS_LOCAL**. PR [#7045](https://github.com/KeeperCommunity/bitcoin-keeper/pull/7045), branch `codex/r4-264-combined-candidate`, base `sprint`. The exact final SHA is recorded in the newest **Codex local handoff** on that PR. The setup below verifies that handoff against the live head before selecting it; this repository document cannot contain its own eventual commit SHA. +**Node-bootstrap continuation:** follow the [managed producer and original-order Local retest](r4-node-bootstrap-backup.md). This supersedes the narrower queue-only retest below: use one new incremental artifact per platform and test initial Verified→ordinary detail bootstrap→automatic complete Verified→ordinary rename→complete readback/cold persistence. Preserve9a19 native controls and its original bootstrap failure separately; all remaining R4 gaps and restrictions stay open. + **Queue-admission continuation:** follow the [narrow fix and focused Local retest](r4-backup-queue-admission.md). The only changed app module is backup inspection queue admission; native/dependency/configuration inputs are unchanged from17e0. Use incremental builds and one focused ordinary-rename/native smoke retest on both platforms. Preserve17e0 failures and independent test results; do not repeat unrelated large matrices or infer a uniquely proved native cause. The prior full R4 gaps remain tracked, C8/#7056 stays parked, and any further failure must be reported without another speculative fix loop. **15 October Android continuation:** read the [16 KB supplier proof and targeted retest](r4-android-16kb.md) and retain the [698078 Local failure](https://github.com/KeeperCommunity/bitcoin-keeper/pull/7045#issuecomment-6178691569). The new JNA selection requires a new development build and actual strict-16-KB launch; no native result transfers to this head. Android C8 / [#7056](https://github.com/KeeperCommunity/bitcoin-keeper/issues/7056) remains owner-parked with its observed FAIL: do not restart that bug's fix/retest cycle. Other required native and release blockers remain open. Production AAB/generated-split construction, signing, upload and runtime verification are a separate owner-approved artifact stage. diff --git a/release/r4-node-bootstrap-backup.md b/release/r4-node-bootstrap-backup.md new file mode 100644 index 0000000000..6b3d43c20b --- /dev/null +++ b/release/r4-node-bootstrap-backup.md @@ -0,0 +1,202 @@ +# R4 managed default-node backup producer + +15 October 2026. **NEEDS_LOCAL / NOT RELEASE-READY**. The newest +[PR #7045](https://github.com/KeeperCommunity/bitcoin-keeper/pull/7045) +**Codex local handoff** names the exact final SHA and actual check results. + +## Failure and preserved controls + +Local tested `9a19c6a2a4b4b71d6ffb0d6a5b9776f12a7dbf1d` with paired disposable +Relay `838049e537e07eb646a2a0393eb969fba43d0739`. The strict16KB Android17/API37 +APK was `7331408887f5d755b439d0d350a49defbef3fdb6698c4bd1c6c89bb46b98acd0`. +After explicit opt-in and complete Verified readback (one wallet, two signers, +no nodes), simply opening Mobile Wallet details saved two canonical Testnet +nodes. Local revision became1, queued stayed0; fresh Status said Needs Updating. +Remote fingerprint stayed `d34ef089ca58d3f8`, with no nodes. All common recovery +content and metadata matched; only two additive nodes differed. No injected +fault or manual repair preceded this capture. + +A first manual Back Up Now safely added only nodes and verified at +`64ab05c5d321d550`. A separate healthy-baseline ordinary rename then passed at +`69bc52bb10af357c`: actual native decrypted full image/metadata matched Realm, +with only `presentationData.name` changed. Cold PIN, MMKV, Realm and fresh +Status also passed. These controls do not pass the original bootstrap order +and are not results for the new source. + +The earlier17e0 rename failure and [queue-admission evidence](r4-backup-queue-admission.md) +remain intact; its exact native cause was never uniquely proved. Its explicit +renamed-wallet repair conflict is expected conservative preservation behavior: +name differs, signers/metadata match, and additive nodes are not the discard +trigger. This producer does not change that guard or add a manual override. + +## Actual caller and bounded change + +`HomeWallet` opens Mobile Wallet details with `autoRefresh:true`. Refresh reaches +`connectToNodeWorker`; its initial/default and one-time fallback additions used +bare Realm helpers. Observers correctly expired verification but did not upload. +The unchanged9a19 actual-caller regression reproduced persisted defaults with +**zero CAS writes**, where one acknowledged exact-set write was required: +**RED0/1, zero skipped**. A prior Mongo-not-primary setup failure is separate. + +The new producer retains original account/session/Realm and branded authority +before yielding, appends only missing node IDs in one managed transaction, then +starts the existing `updateAppImageWorker(updateNodes:true)` with its genuine +receipt. Dispatch stamps `CONNECT_TO_NODE`; direct callers propagate original +authority, including its absence. The exact-set payload includes all persisted +recoverable nodes, custom nodes and both networks. Existing IDs/fields survive; +a completed fallback migration does not resurrect a later explicit deletion. +No-op reconnects do not commit/upload. Missing/stale authority preserves local +nodes and remains pending without acquiring a fresh baseline after persistence. + +The backup child is detached with `spawn`, retaining immutable receipt/origin; +the existing helper contains backup failures and all proof/CAS/readback guards +still apply. Electrum and its refresh/faucet/send callers do not wait for the +Relay deadline. Real native Realm connection-flag writes are exercised around +the background backup: `isConnected` is normalized out of recovery content. +Observers never upload. There is no new ciphertext replay or authority refresh. + +`tests/recovery/automaticNodeBootstrap.integration.cjs` uses the production +network module, origin middleware/store observers, encrypted native Realm, +crypto and real HTTP/CAS/readback on clean Relay838049. Public Electrum +connectivity is stubbed; provider/fee actions are not serviced. It does not +render WalletDetails, exercise MMKV, or prove native UI/public-provider flows. +The exact commands/results, initial failures, TypeScript comparison and +independent safety review are recorded in the newest handoff. + +## Reproducible proof and actual boundaries + +The accepted RED ran before any app source edit with this exact command (the +new integration file then contained only its first test): + +```bash +KEEPER_RELAY_FIXTURE_MODULE=/workspace/dependencies/Relay-r4-16kb-fixture/tests/support/automaticBackupHttpFixture.cjs \ + /workspace/dependencies/tools/node_modules/node/bin/node --test tests/recovery/automaticNodeBootstrap.integration.cjs +``` + +It failed `The actual network caller must enqueue its committed node image`, +expected1/actual0, after actual defaults and migration flags were saved. +Unchanged9a19 `src/store/sagas/network.ts` SHA256: +`744c69af7633e8ac4333b535c76f50dc76e34e2eae4b469d7c85826c86e8700e`. +Accepted RED log SHA256: +`8cbeec9e557cc4f6e8343b91113ad929cf950060c844201e8c2993c165d301b7`. +The committed first test plus optional network-slice fixture support provide +portable source, rather than requiring a Cloud-local log path. To reproduce +the failure, copy those two test files from this final commit into a separate +9a19 checkout with the same prerequisites and run only the first named test; +the later safety cases are for the fixed source. A filtered reproduction will +report the other cases skipped; the original single-test RED had zero skips. + +The first focused GREEN ran19/19. The twentieth case separately passed1/1 +after correcting its setup to close the existing Realm before reopening the +same account. The original attempt opened a second cached same-path wrapper, +then encountered a closed-Realm setup error. That lifecycle boundary is +preserved as an initial fixture failure, not fixed or claimed to be a Mac/app +reproduction. An earlier cross-VM empty-object assertion was normalized to +plain data before accepting RED; no production source was changed for it. + +The final native toolchain is Linux x64, Node22.23.3, Realm12.15.0; the actual +`realm/prebuilds/node/realm.node` SHA256 is +`f9873520422ba77488708a7ef760b189117782a9b13ae1f5c963a68041965934`. +The runner requires and passes an actual native write/read before discovering +the matrices. Mongo7.0 uses the pinned disposable image +`mongo:7.0@sha256:9854f7139445d766a9523571d6f047530c45547460ffcf8259eb2bf4264632ca`, +replica set `keeperQA`, loopback port27058; clean Relay838049 stays unchanged. + +```bash +export REALM_DISABLE_ANALYTICS=1 +export KEEPER_RELAY_FIXTURE_MODULE=/path/to/clean/Relay/tests/support/automaticBackupHttpFixture.cjs +export BACKUP_TEST_MONGO_URL=mongodb://127.0.0.1:27058 +npm run test:automatic-backup-genuine +npm run test:recovery +npm test -- --runInBand --coverage=false +node --test scripts/release-version.test.cjs scripts/verify-channel-config.test.cjs +python3 -B -m unittest discover -s scripts -p 'test_release_artifact.py' +ruby scripts/test_fastlane_build_only.rb +npm run version:check +node_modules/.bin/eslint src/store/sagas/network.ts +node_modules/.bin/tsc --noEmit --pretty false +``` + +Ruby3.3.8 ran offline with read-only source, tmpfs/tmp and image +`ruby@sha256:307aed56240959e9cd3fcc10740ae8d0e6c461e9b42efc076100d4f8126b87b9`; +these are rule tests, not actual Fastlane build/sign/upload actions. +TypeScript **fails** with944 diagnostics on both unchanged9a19 and final source, +with zero additions/removals after normalizing source locations and symlink +dependency paths while retaining diagnostic code/message/path/multiplicity. +Thus no new action/origin/producer typing diagnostic was added; this is not a +clean whole-project typecheck. All-four-module ESLint still **fails** with +12 inherited errors/17warnings in wallets/send; normalized findings add zero. +The network module is clean (previously2errors/2warnings); store.ts is clean. +Whole-project ESLint and native build/UI/MMKV/hardware are NOT RUN in Cloud. + +| Final Cloud check | Actual result | +| --- | --- | +| `npm run test:automatic-backup-genuine` | PASS295/295, zero failures/skips/cancellations;213482.007654ms; native prerequisite passed; new producer20 + prior275 | +| Focused new fixture before full run | PASS19/19, then added Realm-handle case1/1 separately; all20 subsequently passed in the full runner | +| `npm run test:recovery` | PASS448/448, zero skips/cancellations;53687.300236ms | +| `npm test -- --runInBand --coverage=false` | PASS42suites/432tests;58.513s; focused connection Jest3/3 also passed | +| Release rule components | PASS28Node +7Python +21Ruby/117assertions, zero failures/skips | +| `npm run version:check` | PASS2.6.4 / Android627 / iOS620; counters remain provisional | +| Network ESLint / diff / changed handoff links | PASS; broader inherited lint and TypeScript failures remain as disclosed above | +| Independent read-only production/test safety review | No blocking findings; no reviewer edits or claimed independent native execution | +| Current-head native install/launch/UI/MMKV/hardware | NOT RUN in Cloud; NEEDS_LOCAL | + +The final handoff records the full pushed SHA, hosted checks and actual Android +diagnostic cancellation stage. Cloud test passes do not imply current-head +hosted CI or native acceptance, backend rollout, signed-artifact identity or +release approval. Prior17e0/9a19 native results remain separately SHA-scoped. + +## Exact-source Codex Local continuation + +Required local checks: **IOS, ANDROID, HARDWARE**. Read the newest exact-head +handoff, [AGENTS.md](../AGENTS.md), this document and the +[full R4 handoff](r4-automatic-backup-local-handoff.md). Recheck the live SHA. +Preserve ongoing9a19 tests, owner apps/data and original failed artifacts; +advance only a separate clean checkout after this stage closes. Use disposable +Relay838049 without changing contributor pins or either readiness constant. + +1. Incrementally build new **Android developmentDebug** and **iOS simulator + Debug** artifacts from the exact new SHA, retaining matching native caches, + dependencies and locks. Actually install/launch; record source/JS checkout, + artifact hash, package/bundle, version/counter, device and backend. On strict + arm64 Android17/API37 confirm PAGE_SIZE16384 and established fatal/true + properties; do not enable compatibility mode. Old16KB/4KB/static/iOS results + remain scoped to their original artifacts and SHAs. +2. On **each platform**, reproduce the original order with a fresh disposable + account: consentOFF, no persisted nodes; explicit opt-in to complete Verified + readback with one wallet/two signers/no nodes; then ordinary Wallets→Mobile + Wallet detail (`autoRefresh:true`) **before any editor/rename/Save**. Require + local canonical nodes and automatic current Verified with an acknowledged + next revision and complete image/metadata readback. Do not bridge this step + with manual Back Up Now. +3. From that automatic healthy baseline, perform one harmless ordinary rename. + Require local persistence, acknowledged next revision, native decrypted full + image/metadata matching current Realm and only the intended name delta. + Fresh Status must be Verified. Reopen details/reconnect and confirm cache + flags do not enqueue another recovery write. Cold launch/PIN unlock; check + MMKV consent, nodes, renamed wallet and truthful proof without replay. +4. Retain relevant PIN/keypad, Portal initialization, MMKV/encrypted Realm, + wallet/backup/recovery base smoke on both platforms. Preserve independently + incomplete R4 categories; do not repeat unrelated large native/provider + matrices solely for this TypeScript patch. Physical signer/camera/NFC/USB, + public-provider and real-device claims require actual execution separately. +5. Post actual per-platform results for the exact SHA. If another failure + remains, preserve sequence/local/remote evidence and artifacts and report; + do not begin another speculative fix/rebuild loop under this handoff. + +Only app files `src/store/sagas/network.ts`, `wallets.ts`, `send_and_receive.ts` +and `src/store/store.ts` change. Native/build inputs (`android/`, `ios/`), package +and Yarn locks, patches, JNA/Portal pins, version counters, tracked configuration, +contributor manifest, automatic transport/guards and readiness constants remain +byte-identical to9a19. New native acceptance is still required. Old physical +deferral does not transfer: only a new owner exact-SHA decision can defer unrun +HARDWARE, never IOS/ANDROID or an observed failure. + +R4 remains blocked on current-head native acceptance, outstanding scope/review, +owner-parked failed Android C8/#7056, separate backend source/fleet/index/ +transaction/old-writer proof and authorized rollout, fresh store/version/config +inputs and the separately authorized production artifact stage, plus separate +Desktop/website deliverables. Do not merge, deploy, create indexes, sign release +builds, construct production AAB/splits, upload, submit, publish, call real/paid +providers, message customers or broadcast transactions. Keep R4 independent of +Reproducibility and Bitkey Wallet. diff --git a/src/store/sagas/network.ts b/src/store/sagas/network.ts index 7e5788be8a..8355c628a8 100644 --- a/src/store/sagas/network.ts +++ b/src/store/sagas/network.ts @@ -1,7 +1,18 @@ -import { call, put, select } from 'redux-saga/effects'; +import { call, spawn, put, select } from 'redux-saga/effects'; import dbManager from 'src/storage/realm/dbManager'; +import realmDatabase from 'src/storage/realm/realm'; import { RealmSchema } from 'src/storage/realm/enum'; import { NetworkType } from 'src/services/wallets/enums'; +import type { NodeDetail } from 'src/services/wallets/interfaces'; +import type { BackupWriteIntent } from 'src/services/backup/intent'; +import { + beginBackupLocalOperation, + commitBackupLocalOperation, +} from 'src/services/backup/automatic'; +import { + captureBackupWriteIntent, + currentBackupAccountSession, +} from 'src/services/backup/transport'; import { predefinedMainnetNodes, predefinedTestnetNodes, @@ -17,13 +28,22 @@ import { import { createWatcher } from '../utilities'; import { fetchFeeRates } from '../sagaActions/send_and_receive'; import { CONNECT_TO_NODE } from '../sagaActions/network'; +import { updateAppImageWorker } from './bhr'; let latestConnectWorker = 0; -export function* connectToNodeWorker() { - const workerId = ++latestConnectWorker; +export function* connectToNodeWorker( + action: { + originAppId?: string; + originBackupAccountSession?: number; + originBackupWriteIntent?: BackupWriteIntent; + } = {} +) { + let workerId = latestConnectWorker; let appId: string; let bitcoinNetworkType: NetworkType; + let requestSession: number; + let sourceRealm: ReturnType; let responseGeneration: number; const contextIsCurrent = () => { const current = store.getState(); @@ -33,7 +53,8 @@ export function* connectToNodeWorker() { id?: string; }; realmAppId = keeper?.id; - } catch (_) { + if (realmDatabase.getDatabase() !== sourceRealm) return false; + } catch { return false; } return ( @@ -41,13 +62,26 @@ export function* connectToNodeWorker() { !!appId && realmAppId === appId && current.storage.appId === appId && - current.settings.bitcoinNetworkType === bitcoinNetworkType + current.settings.bitcoinNetworkType === bitcoinNetworkType && + currentBackupAccountSession() === requestSession ); }; try { - bitcoinNetworkType = yield select((state: RootState) => state.settings.bitcoinNetworkType); - appId = yield select((state: RootState) => state.storage.appId); + // Capture before yielding: a delayed caller must retain its original + // session, native Realm and backup authority across A -> B -> A. + const entryState = store.getState(); + bitcoinNetworkType = entryState.settings.bitcoinNetworkType; + appId = action.originAppId || entryState.storage.appId; + requestSession = action.originBackupAccountSession ?? currentBackupAccountSession(); + sourceRealm = realmDatabase.getDatabase(); + const originBackupWriteIntent = Object.prototype.hasOwnProperty.call( + action, + 'originBackupWriteIntent' + ) + ? action.originBackupWriteIntent + : captureBackupWriteIntent(appId); if (!contextIsCurrent()) return; + workerId = ++latestConnectWorker; console.log('Connecting to node...'); yield put(electrumClientConnectionInitiated()); @@ -55,46 +89,40 @@ export function* connectToNodeWorker() { (state: RootState) => state.network.initialNodesSaved ); + const fallbackNodeAdded = yield select( + (state: RootState) => state.network.testnetFallbackNodeAddedByAppId?.[appId] + ); + if (!contextIsCurrent()) return; + const currentNodes: NodeDetail[] = yield call(dbManager.getCollection, RealmSchema.NodeConnect); + if (!contextIsCurrent()) return; + const additions: NodeDetail[] = []; if (!areInitialNodesSaved) { - if (!contextIsCurrent()) return; - const currentNodes = yield call(dbManager.getCollection, RealmSchema.NodeConnect); - if (!contextIsCurrent()) return; const defaultNodes = yield call(dbManager.getCollection, RealmSchema.DefaultNodeConnect); if (!contextIsCurrent()) return; - let addInitialNode = defaultNodes && defaultNodes.length != 0; - if (!addInitialNode && currentNodes.length == 0) { - addInitialNode = true; - } - - if (addInitialNode) { - const hardcodedInitialNodes = + if (defaultNodes.length || currentNodes.length === 0) { + const defaults = bitcoinNetworkType === NetworkType.TESTNET ? predefinedTestnetNodes : predefinedMainnetNodes; - if (!contextIsCurrent()) return; - const created = yield call( - dbManager.createObjectBulk, - RealmSchema.NodeConnect, - hardcodedInitialNodes + additions.push( + ...defaults.filter( + (node) => + !currentNodes.some((saved) => saved.id === node.id) && + !( + bitcoinNetworkType === NetworkType.TESTNET && + fallbackNodeAdded && + node.id === predefinedTestnetNodes[1].id + ) + ) ); - if (!contextIsCurrent()) return; - if (!created) throw new Error('Unable to save initial Electrum servers'); } - - if (!contextIsCurrent()) return; - yield put(setInitialNodesSaved(true)); } - // Existing Testnet installations only saved the first public server. Add the - // second once when that original server is still saved; an explicit deletion - // after this migration must not be undone at every reconnect. - const fallbackNodeAdded = yield select( - (state: RootState) => state.network.testnetFallbackNodeAddedByAppId?.[appId] - ); + // Plan initial and fallback additions together. NodeConnect has no primary + // key: append only absent IDs and preserve existing/custom node records. + // An already-completed fallback migration must not undo a later deletion. if (bitcoinNetworkType === NetworkType.TESTNET && appId && !fallbackNodeAdded) { - if (!contextIsCurrent()) return; - const savedNodes = yield call(dbManager.getCollection, RealmSchema.NodeConnect); - if (!contextIsCurrent()) return; + const savedNodes = [...currentNodes, ...additions]; const originalNode = predefinedTestnetNodes[0]; const fallbackNode = predefinedTestnetNodes[1]; if ( @@ -106,11 +134,35 @@ export function* connectToNodeWorker() { ) && !savedNodes.some((node) => node.id === fallbackNode.id) ) { - if (!contextIsCurrent()) return; - const created = yield call(dbManager.createObject, RealmSchema.NodeConnect, fallbackNode); - if (!contextIsCurrent()) return; - if (!created) throw new Error('Unable to save Testnet fallback server'); + additions.push(fallbackNode); } + } + + if (additions.length) { + if (!contextIsCurrent()) return; + const operation = beginBackupLocalOperation(appId, originBackupWriteIntent, requestSession); + const committedOperation = commitBackupLocalOperation(operation, (localRealm) => { + if (!contextIsCurrent()) throw new Error('Electrum bootstrap account changed'); + for (const node of additions) localRealm.create(RealmSchema.NodeConnect, node); + }); + if (!committedOperation.committed) throw new Error('Unable to save Electrum servers'); + if (!contextIsCurrent()) return; + // This committed recovery task owns the exact receipt and entire saved + // node set. Its helper contains backup failures. Detaching it keeps a + // Relay wait from delaying Electrum, refresh, faucet or send callers; + // session/consent/generation guards still govern its actual upload. + yield spawn(updateAppImageWorker, { + payload: { updateNodes: true }, + originAppId: appId, + originBackupAccountSession: requestSession, + committedOperation, + }); + } + if (!areInitialNodesSaved) { + if (!contextIsCurrent()) return; + yield put(setInitialNodesSaved(true)); + } + if (bitcoinNetworkType === NetworkType.TESTNET && appId && !fallbackNodeAdded) { if (!contextIsCurrent()) return; yield put(setTestnetFallbackNodeAdded(appId)); } @@ -125,9 +177,8 @@ export function* connectToNodeWorker() { ElectrumClient.setActivePeer(nodes); const { connected, connectedTo, error, generation } = yield call(ElectrumClient.connect); responseGeneration = generation; - const connectionIsCurrent = () => { - return contextIsCurrent() && generation === ElectrumClient.getConnectionGeneration(); - }; + const connectionIsCurrent = () => + contextIsCurrent() && generation === ElectrumClient.getConnectionGeneration(); // Another login/network request may replace this connection while the worker // is waiting. Its result must not overwrite the newer connection's status. if (!connectionIsCurrent()) return; diff --git a/src/store/sagas/send_and_receive.ts b/src/store/sagas/send_and_receive.ts index 4ad6b693ed..4d5e3600f0 100644 --- a/src/store/sagas/send_and_receive.ts +++ b/src/store/sagas/send_and_receive.ts @@ -133,7 +133,9 @@ function* sendPhaseTwoWorker({ payload, originAppId, originBackupAccountSession, SendPhaseTwoAction & { originAppId?: string; originBackupAccountSession?: number; originBackupWriteIntent?: BackupWriteIntent }) { if (!ELECTRUM_CLIENT.isClientConnected) { ElectrumClient.resetCurrentPeerIndex(); - yield call(connectToNodeWorker); + yield call(connectToNodeWorker, { + originAppId, originBackupAccountSession, originBackupWriteIntent, + }); } const sendPhaseOneResults: SendPhaseOneExecutedPayload = yield select( (state) => state.sendAndReceive.sendPhaseOne @@ -222,7 +224,9 @@ function* sendPhaseThreeWorker({ payload, originAppId, originBackupAccountSessio SendPhaseThreeAction & { originAppId?: string; originBackupAccountSession?: number; originBackupWriteIntent?: BackupWriteIntent }) { if (!ELECTRUM_CLIENT.isClientConnected) { ElectrumClient.resetCurrentPeerIndex(); - yield call(connectToNodeWorker); + yield call(connectToNodeWorker, { + originAppId, originBackupAccountSession, originBackupWriteIntent, + }); } const sendPhaseOneResults: SendPhaseOneExecutedPayload = yield select( diff --git a/src/store/sagas/wallets.ts b/src/store/sagas/wallets.ts index 35ccb053de..451924a61a 100644 --- a/src/store/sagas/wallets.ts +++ b/src/store/sagas/wallets.ts @@ -932,7 +932,11 @@ function* refreshWalletsWorker({ } if (!ELECTRUM_CLIENT.isClientConnected || !connectionMatchesScope) { ElectrumClient.resetCurrentPeerIndex(); - yield call(connectToNodeWorker); + yield call(connectToNodeWorker, { + originAppId: sourceAppId, + originBackupAccountSession: requestSession, + originBackupWriteIntent, + }); } assertAccountRealmScope(); @@ -1599,7 +1603,11 @@ export function* testcoinsWorker({ payload, originAppId, originBackupAccountSess // The old profile or network's connection must not be reused. } if (!ELECTRUM_CLIENT.isClientConnected || !connectionMatchesScope) { - yield call(connectToNodeWorker); + yield call(connectToNodeWorker, { + originAppId: faucetScope.appId, + originBackupAccountSession: requestSession, + originBackupWriteIntent, + }); } if (!isCurrentFaucetScope()) return; const connectionGeneration = ElectrumClient.getConnectionGeneration(); diff --git a/src/store/store.ts b/src/store/store.ts index 7b6bbce2b0..8907b223d4 100644 --- a/src/store/store.ts +++ b/src/store/store.ts @@ -115,6 +115,7 @@ const accountOwnedActions = new Set([ 'REFRESH_WALLETS', 'TEST_SATS_RECIEVE', 'AUTO_SYNC_WALLETS', + 'CONNECT_TO_NODE', 'REFRESH_CANARY_VAULT', 'UPDATE_WALLET_DETAILS', 'UPDATE_VAULT_DETAILS', diff --git a/tests/recovery/automaticNodeBootstrap.integration.cjs b/tests/recovery/automaticNodeBootstrap.integration.cjs new file mode 100644 index 0000000000..840f15c57c --- /dev/null +++ b/tests/recovery/automaticNodeBootstrap.integration.cjs @@ -0,0 +1,454 @@ +// Actual production connectToNodeWorker module and dispatch middleware, native +// encrypted Realm, real crypto and transactional loopback Relay. The Electrum +// provider is the only connection boundary stub: no public servers/fees are +// contacted. This is not rendered native UI, MMKV or live Electrum acceptance. +const assert = require('node:assert/strict'); +const { test: nodeTest, before, after } = require('node:test'); +const { runSaga } = require('redux-saga'); +const { createRealmClient, plain, Realm } = require('./automaticWriteRealmRelay.fixture.cjs'); + +const fixtureModule = process.env.KEEPER_RELAY_FIXTURE_MODULE; +assert.ok(fixtureModule, 'Set the pinned real transactional Relay fixture'); +const { createFixture } = require(fixtureModule); +let server; +before(async () => { server = await createFixture(); }); +after(async () => { await server?.close(); Realm.shutdown(); }); +const test = (name, body) => nodeTest(name, { timeout: 20_000 }, body); +const writes = () => server.trace.filter(event => event.path === '/revisionedBackupMutation'); + +function deferred(t) { + let resolve; + const promise = new Promise(done => { resolve = done; }); + t.after(() => resolve()); + return { promise, resolve }; +} + +async function waitFor(predicate) { + const deadline = Date.now() + 5000; + while (!predicate()) { + assert.ok(Date.now() < deadline, 'Expected genuine HTTP boundary was not reached'); + await new Promise(resolve => setTimeout(resolve, 2)); + } +} + +function createNode(f, node) { + assert.equal(f.dbManager.createObject(f.RealmSchema.NodeConnect, + f.defaults(f.RealmSchema.NodeConnect, node)), true); +} + +function holdFirstNodeRead(t, f) { + const entered = deferred(t); + const release = deferred(t); + const original = f.dbManager.getCollection; + let held = false; + // Scheduling adapter only: defer the unchanged actual Realm query. No node + // data, transaction result or backup proof is substituted. + f.dbManager.getCollection = (...args) => { + if (!held && args[0] === f.RealmSchema.NodeConnect) { + held = true; + entered.resolve(); + return release.promise.then(() => original(...args)); + } + return original(...args); + }; + t.after(() => { f.dbManager.getCollection = original; }); + return { entered: entered.promise, release: release.resolve }; +} + +async function setup(t, options = {}) { + await server.reset(); + const f = await createRealmClient(server.url, { withNetwork: true, ...options }); + t.after(() => f.close()); + f.automatic = f.load('src/services/backup/automatic'); + f.installProductionStoreObservers(f.automatic); + f.store.dispatch(f.bhr.setAutomaticCloudBackup({ appId: f.appId, enabled: true })); + const predefined = f.load('src/services/electrum/predefinedNodes'); + f.predefined = predefined; + if (options.prepare) options.prepare(f); + await server.seed(await f.payloadForCurrentImage()); + assert.equal(await f.repair.inspectBackup(f.appId, false, () => {}), 'verified'); + f.initial = await server.snapshot(f.appId); + server.clearTrace(); + let peers = []; + let generation = 0; + let connectImpl; + let connectedPeerIndex = 0; + f.providerCalls = []; + const electrum = { + setActivePeer(nodes) { peers = Array.from(nodes); f.providerCalls.push('setActivePeer'); }, + async connect() { + const requestGeneration = ++generation; + f.providerCalls.push('connect'); + if (connectImpl) return connectImpl(requestGeneration); + return { connected: true, connectedTo: 'disposable-provider.invalid', generation: requestGeneration }; + }, + getActivePeer: () => peers[connectedPeerIndex], + getConnectionGeneration: () => generation, + }; + f.setConnect = value => { connectImpl = value; }; + f.setConnectedPeerIndex = value => { connectedPeerIndex = value; }; + f.bindings['src/services/electrum/client'] = electrum; + // These are unchanged selected production functions, not fabricated backup + // replies. The boundary avoids unrelated React Native bootstrap imports. + f.sagas = f.productionSagas([ + 'currentBackupAppId', 'buildAutomaticAppMutation', 'queueCommittedBackupWorker', 'updateAppImageWorker', + ]); + f.bindings['src/store/sagas/bhr'] = f.sagas.scope; + f.worker = f.load('src/store/sagas/network').connectToNodeWorker; + f.connectRaw = (action) => runSaga({ getState: f.store.getState, dispatch: f.store.dispatch }, f.worker, action).toPromise(); + f.drain = () => f.transport.withBackupSession(f.appId, async () => {}); + f.connect = async action => { + const result = await f.connectRaw(action); + // Observe completion of detached actual backup work for proof and cleanup. + // This does not change the production parent task's completion semantics. + await f.drain(); + return result; + }; + f.connectDispatched = () => f.connect(f.store.dispatch(f.load('src/store/sagaActions/network').connectToNode())); + f.localNodes = () => plain(Array.from(f.dbManager.getCollection(f.RealmSchema.NodeConnect))); + f.remote = async () => f.image.decodeImage(f.encryption.generateEncryptionKey(f.seed), + await server.snapshot(f.appId), f.appId, () => {}, true); + return f; +} + +async function assertConfirmed(f, count = 1) { + assert.equal(writes().length, count, 'The actual network caller must enqueue its committed node image'); + assert.ok(writes().every(event => event.committed)); + const current = await server.snapshot(f.appId); + assert.notEqual(current.revision, f.initial.revision); + assert.equal(current.revision, writes().at(-1).response.newRevision); + const remote = await f.remote(); + assert.equal(await f.image.compareImages(await f.repair.captureBackupImage(), remote), 'matched'); + const localNodes = Object.fromEntries(f.localNodes().map(node => [String(node.id), f.image.recoveryContent('nodes', node)])); + assert.deepEqual(plain(remote.nodes), plain(localNodes)); + assert.equal(f.automatic.isBackupVerificationCurrent(f.appId), true); + const state = f.store.getState().bhr; + assert.equal(state.pendingAllBackupByAppId[f.appId], false); + assert.equal(state.backupRepairStateByAppId[f.appId], 'verified'); + assert.equal(state.backupRepairCompletedByAppId[f.appId], true); +} + +function assertConnectionSuccessful(f) { + const results = f.actions.filter(action => action.type === 'login/electrumClientConnectionExecuted'); + assert.equal(results.at(-1)?.payload.successful, true, + 'Backup errors must not manufacture an Electrum connection failure'); +} + +async function assertPending(f, { committed = false } = {}) { + assert.equal(writes().length, 1); + assert.equal(writes()[0].committed, committed); + assert.deepEqual(f.localNodes().map(node => node.id).sort(), [336, 337]); + assert.equal(f.automatic.isBackupVerificationCurrent(f.appId), false); + const bhr = f.store.getState().bhr; + assert.equal(bhr.pendingAllBackupByAppId[f.appId], true); + assert.notEqual(bhr.backupRepairStateByAppId[f.appId], 'verified'); + assertConnectionSuccessful(f); +} + +test('node bootstrap actual caller: Verified empty node image creates defaults and completes an acknowledged backup', async t => { + const f = await setup(t); + assert.deepEqual(f.localNodes(), []); + assert.deepEqual(plain((await f.remote()).nodes), {}); + server.clearTrace(); + // This reproduces the production refresh/login yield call(connectToNodeWorker) + // path with no synthetic action or externally supplied backup intent. + await f.connect(); + assert.deepEqual(f.localNodes().map(node => node.id).sort(), [336, 337]); + assert.equal(f.store.getState().network.initialNodesSaved, true); + assert.equal(f.store.getState().network.testnetFallbackNodeAddedByAppId[f.appId], true); + assert.deepEqual(f.providerCalls, ['setActivePeer', 'connect']); + await assertConfirmed(f); +}); + +test('dispatched CONNECT_TO_NODE uses production origin middleware and the same committed backup path', async t => { + const f = await setup(t); + const action = f.store.dispatch(f.load('src/store/sagaActions/network').connectToNode()); + assert.equal(action.originAppId, f.appId); + assert.equal(f.transport.isCapturedBackupWriteIntent(action.originBackupWriteIntent), true); + await f.connect(action); + await assertConfirmed(f); +}); + +test('fallback migration backs up all saved mixed-network nodes and reconnect respects an explicitly deleted fallback', async t => { + const f = await setup(t, { prepare(f) { + createNode(f, f.predefined.predefinedTestnetNodes[0]); + createNode(f, { id: 900, host: 'public-custom-mainnet.invalid', port: '50002', useSSL: true, + networkType: f.enums.NetworkType.MAINNET }); + f.store.dispatch(f.network.setInitialNodesSaved(true)); + } }); + await f.connect(); + await assertConfirmed(f); + assert.deepEqual(f.localNodes().map(node => node.id).sort((a, b) => a - b), [336, 337, 900]); + const operation = f.automatic.beginBackupLocalOperation(f.appId, + f.transport.captureBackupWriteIntent(f.appId), f.transport.currentBackupAccountSession()); + const receipt = f.automatic.commitBackupLocalOperation(operation, realm => { + realm.delete(realm.objects(f.RealmSchema.NodeConnect).filtered('id == $0', 337)); + }); + assert.equal(receipt.committed, true); + const removed = await f.sagas.run('updateAppImageWorker', { payload: { updateNodes: true }, + originAppId: f.appId, originBackupAccountSession: f.transport.currentBackupAccountSession(), + committedOperation: receipt }); + assert.equal(removed.backupConfirmed, true); + server.clearTrace(); + await f.connect(); + assert.equal(writes().length, 0); + assert.deepEqual(f.localNodes().map(node => node.id).sort((a, b) => a - b), [336, 900]); + assert.deepEqual(Object.keys((await f.remote()).nodes).sort(), ['336', '900']); + assert.equal(f.automatic.isBackupVerificationCurrent(f.appId), true); +}); + +test('legacy initial-node migration preserves custom same-ID fields and the complete mixed-network set', async t => { + const f = await setup(t, { prepare(f) { + createNode(f, { ...f.predefined.predefinedTestnetNodes[0], host: 'owner-custom-testnet.invalid', port: '60123' }); + createNode(f, { id: 900, host: 'owner-custom-mainnet.invalid', port: '60002', useSSL: true, + networkType: f.enums.NetworkType.MAINNET }); + assert.equal(f.dbManager.createObject(f.RealmSchema.DefaultNodeConnect, + f.defaults(f.RealmSchema.DefaultNodeConnect, f.predefined.predefinedTestnetNodes[0])), true); + } }); + await f.connect(); + const nodes = f.localNodes(); + assert.deepEqual(nodes.map(node => node.id).sort((a, b) => a - b), [336, 337, 900]); + assert.equal(nodes.find(node => node.id === 336).host, 'owner-custom-testnet.invalid'); + assert.equal(nodes.find(node => node.id === 336).port, '60123'); + await assertConfirmed(f); +}); + +test('consent OFF keeps bootstrap local and sends no backup HTTP', async t => { + const f = await setup(t); + f.store.dispatch(f.bhr.setAutomaticCloudBackup({ appId: f.appId, enabled: false })); + await f.connect(); + assert.deepEqual(f.localNodes().map(node => node.id).sort(), [336, 337]); + assert.equal(server.trace.length, 0); + assert.equal(f.store.getState().bhr.automaticCloudBackupByAppId[f.appId], false); + assert.equal((await server.snapshot(f.appId)).revision, f.initial.revision); + assertConnectionSuccessful(f); +}); + +test('missing original baseline preserves local defaults without manufacturing upload authority', async t => { + const f = await setup(t); + f.transport.clearVerifiedRemoteRevision(f.appId); + await f.connect(); + assert.deepEqual(f.localNodes().map(node => node.id).sort(), [336, 337]); + assert.equal(writes().length, 0); + assert.equal((await server.snapshot(f.appId)).revision, f.initial.revision); + assert.equal(f.store.getState().bhr.pendingAllBackupByAppId[f.appId], true); + assert.equal(f.automatic.isBackupVerificationCurrent(f.appId), false); + assertConnectionSuccessful(f); +}); + +test('explicit missing dispatch-time intent stays missing after a later genuine verification', async t => { + const f = await setup(t); + f.transport.clearVerifiedRemoteRevision(f.appId); + const action = f.store.dispatch(f.load('src/store/sagaActions/network').connectToNode()); + assert.equal(action.originBackupWriteIntent, undefined); + assert.equal(await f.repair.inspectBackup(f.appId, false, () => {}), 'verified'); + server.clearTrace(); + await f.connect(action); + assert.equal(writes().length, 0); + assert.deepEqual(f.localNodes().map(node => node.id).sort(), [336, 337]); + assert.equal(f.automatic.isBackupVerificationCurrent(f.appId), false); + assert.equal((await server.snapshot(f.appId)).revision, f.initial.revision); +}); + +test('a reconnect with no recovery change performs no write and retains the verified generation', async t => { + const f = await setup(t, { prepare(f) { + for (const node of f.predefined.predefinedTestnetNodes) createNode(f, node); + f.store.dispatch(f.network.setInitialNodesSaved(true)); + f.store.dispatch(f.network.setTestnetFallbackNodeAdded(f.appId)); + } }); + const generation = f.automatic.currentCommittedBackupGeneration(f.appId); + await f.connect(); + assert.equal(server.trace.length, 0); + assert.equal(f.automatic.currentCommittedBackupGeneration(f.appId), generation); + assert.equal(f.automatic.isBackupVerificationCurrent(f.appId), true); + assertConnectionSuccessful(f); +}); + +test('an actual native validation failure rolls back the entire initial-node group and leaves migration flags unset', async t => { + const f = await setup(t); + // Native schema validation fails on the second row, after the first create; + // this exercises actual Realm rollback rather than a fake write result. + f.predefined.predefinedTestnetNodes[1].port = { invalid: 'native string field' }; + await f.connect(); + assert.deepEqual(f.localNodes(), []); + assert.equal(f.store.getState().network.initialNodesSaved, false); + assert.equal(f.store.getState().network.testnetFallbackNodeAddedByAppId[f.appId], undefined); + assert.equal(writes().length, 0); + assert.equal((await server.snapshot(f.appId)).revision, f.initial.revision); + assert.deepEqual(f.providerCalls, []); +}); + +test('a bootstrap paused before commit cannot cross A to B to A', async t => { + const f = await setup(t); + const hold = holdFirstNodeRead(t, f); + const connecting = f.connect(); + await hold.entered; + const session = f.transport.currentBackupAccountSession(); + await f.selectAccount('disposable-node-account-b'); + await f.selectAccount(f.appId); + assert.notEqual(f.transport.currentBackupAccountSession(), session); + hold.release(); + await connecting; + assert.deepEqual(f.localNodes(), []); + assert.equal(f.store.getState().network.initialNodesSaved, false); + assert.equal(writes().length, 0); + assert.deepEqual(f.providerCalls, []); +}); + +test('a network switch before native commit prevents old-network defaults and stale connection completion', async t => { + const f = await setup(t); + const hold = holdFirstNodeRead(t, f); + const connecting = f.connect(); + await hold.entered; + f.store.dispatch(f.settings.setBitcoinNetwork(f.enums.NetworkType.MAINNET)); + hold.release(); + await connecting; + assert.deepEqual(f.localNodes(), []); + assert.equal(writes().length, 0); + assert.deepEqual(f.providerCalls, []); +}); + +test('a same-account Realm replacement before commit cannot borrow the unchanged account session', async t => { + const f = await setup(t); + const hold = holdFirstNodeRead(t, f); + const originalRealm = f.nativeRealm(); + const session = f.transport.currentBackupAccountSession(); + const connecting = f.connect(); + await hold.entered; + // Close then reopen the same native file through production APIs. Opening + // a second cached same-path wrapper is a different Realm lifecycle boundary. + f.realmDatabase.closeDatabase(); + await f.selectAccount(f.appId); + assert.notEqual(f.nativeRealm(), originalRealm); + assert.equal(f.transport.currentBackupAccountSession(), session); + hold.release(); + await connecting; + assert.deepEqual(f.localNodes(), []); + assert.equal(f.store.getState().network.initialNodesSaved, false); + assert.equal(writes().length, 0); + assert.deepEqual(f.providerCalls, []); +}); + +test('a superseded bootstrap worker cannot publish after the current worker completes', async t => { + const f = await setup(t); + const hold = holdFirstNodeRead(t, f); + const older = f.connect(); + await hold.entered; + await f.connect(); + await assertConfirmed(f); + const currentEpoch = f.transport.backupBaselineEpoch(f.appId); + hold.release(); + await older; + assert.equal(writes().length, 1); + assert.equal(f.transport.backupBaselineEpoch(f.appId), currentEpoch); + assert.equal(f.automatic.isBackupVerificationCurrent(f.appId), true); + assert.deepEqual(f.providerCalls, ['setActivePeer', 'connect']); +}); + +for (const [mode, committed] of [['409', false], ['malformed', true], ['lost-reply', true]]) { + test(`node bootstrap ${mode} preserves defaults and reports backup pending independently of Electrum`, async t => { + const f = await setup(t); + server.armFault({ mode, operation: 'updateAppImage' }); + await f.connect(); + await assertPending(f, { committed }); + }); +} + +test('malformed final snapshot metadata cannot publish Verified after the real node CAS', async t => { + const f = await setup(t); + server.armFault({ mode: 'malformed-metadata', after: 1 }); + await f.connect(); + await assertPending(f, { committed: true }); +}); + +test('native node mutation during final readback preserves both local changes but withholds Verified', async t => { + const f = await setup(t); + server.armFault({ mode: 'before-snapshot-reply', after: 1, beforeReply() { + createNode(f, { id: 901, host: 'late-local-node.invalid', port: '50002', useSSL: true, + networkType: f.enums.NetworkType.TESTNET }); + } }); + await f.connect(); + assert.equal(writes().length, 1); + assert.equal(writes()[0].committed, true); + assert.deepEqual(f.localNodes().map(node => node.id).sort((a, b) => a - b), [336, 337, 901]); + assert.deepEqual(Object.keys((await f.remote()).nodes).sort(), ['336', '337']); + assert.equal(f.automatic.isBackupVerificationCurrent(f.appId), false); + assert.equal(f.store.getState().bhr.pendingAllBackupByAppId[f.appId], true); + assertConnectionSuccessful(f); +}); + +test('after-send A to B to A cannot apply an old bootstrap acknowledgement to the new session', async t => { + const f = await setup(t); + server.armFault({ mode: 'delay-reply', operation: 'updateAppImage', delayMs: 1200 }); + let settled = false; + const connecting = f.connect().then(value => { settled = true; return value; }); + await waitFor(() => writes()[0]?.committed); + assert.equal(settled, false); + await f.selectAccount('disposable-node-account-b'); + await f.selectAccount(f.appId); + const epoch = f.transport.backupBaselineEpoch(f.appId); + const boundary = f.actions.length; + await connecting; + assert.equal(writes().length, 1); + assert.deepEqual(f.localNodes().map(node => node.id).sort(), [336, 337]); + assert.equal(f.automatic.isBackupVerificationCurrent(f.appId), false); + assert.equal(f.actions.slice(boundary).some(action => action.type === f.bhr.commitBackupInspection.type), false); + assert.ok(f.transport.backupBaselineEpoch(f.appId) >= epoch); +}); + +test('detached backup does not hold connection completion and real connection flags do not invalidate recovery', async t => { + const f = await setup(t); + const entered = deferred(t); + const release = deferred(t); + server.armFault({ mode: 'before-snapshot-reply', beforeReply() { + entered.resolve(); return release.promise; + } }); + f.setConnectedPeerIndex(1); + const flagWrites = []; + const originalUpdate = f.dbManager.updateObjectById; + f.dbManager.updateObjectById = (...args) => { + const before = f.automatic.currentCommittedBackupGeneration(f.appId); + const result = originalUpdate(...args); + flagWrites.push({ schema: args[0], id: args[1], patch: args[2], result, + before, after: f.automatic.currentCommittedBackupGeneration(f.appId), + queued: f.transport.backupQueuedRevision(f.appId) }); + return result; + }; + let parentFinished = false; + const parent = f.connectRaw().then(value => { parentFinished = true; return value; }); + try { + await entered.promise; + await waitFor(() => parentFinished); + assertConnectionSuccessful(f); + assert.deepEqual(f.providerCalls, ['setActivePeer', 'connect']); + assert.equal(writes().length, 0, 'The real baseline HTTP reply remains held'); + assert.equal(flagWrites.length, 2, 'Both selected-node flags use actual dbManager/Realm writes'); + for (const write of flagWrites) { + assert.equal(write.schema, f.RealmSchema.NodeConnect); + assert.equal(write.result, true); + assert.equal(write.before, write.after); + assert.ok(write.queued > 0, 'The backup was already queued before the cache write'); + } + assert.deepEqual(f.localNodes().map(node => [node.id, node.isConnected]).sort(), [[336, false], [337, true]]); + } finally { + f.dbManager.updateObjectById = originalUpdate; + release.resolve(); + await parent; + await f.drain(); + } + await assertConfirmed(f); + let transactions = 0; + const originalTransaction = f.realmDatabase.writeTransaction; + f.realmDatabase.writeTransaction = (...args) => { + transactions += 1; return originalTransaction(...args); + }; + try { + server.clearTrace(); + await f.connect(); + assert.equal(transactions, 0, 'A settled reconnect with the active peer unchanged is a genuine native no-op'); + assert.equal(server.trace.length, 0); + assert.equal(f.automatic.isBackupVerificationCurrent(f.appId), true); + } finally { + f.realmDatabase.writeTransaction = originalTransaction; + } +}); diff --git a/tests/recovery/automaticWriteRealmRelay.fixture.cjs b/tests/recovery/automaticWriteRealmRelay.fixture.cjs index e78ab871e1..d5853bb89c 100644 --- a/tests/recovery/automaticWriteRealmRelay.fixture.cjs +++ b/tests/recovery/automaticWriteRealmRelay.fixture.cjs @@ -152,6 +152,7 @@ async function createRealmClient(url, options = {}) { const storage = load('src/store/reducers/storage'); const account = load('src/store/reducers/account'); const settings = load('src/store/reducers/settings'); + const network = options.withNetwork ? load('src/store/reducers/network') : undefined; const middlewareScope = { dbManager, RealmSchema, ...transport, ...bhr, isBackupVerificationCurrent: (...args) => load('src/services/backup/automatic').isBackupVerificationCurrent(...args) }; evaluate(declarations('src/store/store.ts', ['accountOwnedActions', 'accountOwnedUtxoUiActions', 'bindActionOrigin']), middlewareScope, 'production-origin-middleware.ts'); @@ -160,7 +161,8 @@ async function createRealmClient(url, options = {}) { // This Node fixture mounts the four production slices used by recovery. The // optional root wrapper uses the actual production declarations/config and // redux-persist reconciler, rather than simulating REHYDRATE with a reducer. - let reducer = combineReducers({ bhr: bhr.default, storage: storage.default, account: account.default, settings: settings.default }); + let reducer = combineReducers({ bhr: bhr.default, storage: storage.default, account: account.default, settings: settings.default, + ...(network ? { network: network.default } : {}) }); if (options.persistedRedux) { const persistenceScope = { appReducer: reducer, storage: reduxStorage, reduxStorage, RESET_REDUX_STORE: 'RESET_REDUX_STORE', persistReducer, createMigrate, @@ -335,7 +337,7 @@ async function createRealmClient(url, options = {}) { return { appId, seed, directory, load, bindings, enums, RealmSchema, Realm, realmDatabase, dbManager, store, - encryption, image, repair, transport, Relay, bhr, storage, account, settings, utilities: utilitiesScope, + encryption, image, repair, transport, Relay, bhr, storage, account, settings, network, utilities: utilitiesScope, config: bindings['src/utils/service-utilities/config'], defaults, nativeRealm, selectAccount, selected: () => selected, nativeEvents, diagnostics, actions, listen, installProductionStoreObservers, productionSagas, payloadForCurrentImage, diff --git a/tests/services/networkConnectRace.test.ts b/tests/services/networkConnectRace.test.ts index d21c012606..5783028367 100644 --- a/tests/services/networkConnectRace.test.ts +++ b/tests/services/networkConnectRace.test.ts @@ -9,6 +9,7 @@ import { connectToNodeWorker } from '../../src/store/sagas/network'; let mockGeneration = 0; let mockRealmAppId = 'account-a'; const mockNodes = predefinedTestnetNodes; +const mockRealm = { create: jest.fn() }; const mockState = { settings: { bitcoinNetworkType: NetworkType.TESTNET }, storage: { appId: 'account-a' }, @@ -19,12 +20,33 @@ const mockState = { }; jest.mock('src/store/store', () => ({ store: { getState: jest.fn(() => mockState) } })); +// These remain connection-ownership unit tests. Genuine node bootstrap tests +// separately run the native Realm transaction and actual backup proof/HTTP. +jest.mock('src/storage/realm/realm', () => ({ + __esModule: true, + default: { getDatabase: jest.fn(() => mockRealm) }, +})); +jest.mock('src/services/backup/transport', () => ({ + captureBackupWriteIntent: jest.fn(() => undefined), + currentBackupAccountSession: jest.fn(() => 0), +})); +jest.mock('src/services/backup/automatic', () => ({ + beginBackupLocalOperation: jest.fn(() => ({})), + commitBackupLocalOperation: jest.fn((_operation, write) => { + write(mockRealm); + return { committed: true }; + }), +})); +jest.mock('src/store/sagas/bhr', () => ({ + updateAppImageWorker: jest.fn(function* backupNodes() { + return undefined; + }), +})); jest.mock('src/storage/realm/dbManager', () => ({ __esModule: true, default: { getObjectByIndex: jest.fn(() => ({ id: mockRealmAppId })), getCollection: jest.fn(() => mockNodes), - createObject: jest.fn(() => true), updateObjectById: jest.fn(() => true), }, })); @@ -114,7 +136,7 @@ describe('overlapping Electrum connection workers', () => { connectToNodeWorker ).toPromise(); - expect(dbManager.createObject).toHaveBeenCalledWith(RealmSchema.NodeConnect, mockNodes[1]); + expect(mockRealm.create).toHaveBeenCalledWith(RealmSchema.NodeConnect, mockNodes[1]); expect(actions).toContainEqual( expect.objectContaining({ type: 'network/setTestnetFallbackNodeAdded', From c3d23eaf13f64a4e71ce2c3810350769a4c56053 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Thu, 15 Oct 2026 11:51:20 +0000 Subject: [PATCH 199/254] feat(r5): start guarded key deletion and setup error cleanup --- docs/r5/WORKSTREAM.md | 123 ++++++++++++ src/context/Localization/language/en.json | 12 +- src/context/Localization/language/es.json | 12 +- .../Home/components/Keys/ManageKeys.tsx | 11 +- .../NewKeeperAppScreen/NewKeeperAppScreen.tsx | 19 +- src/screens/SigningDevices/DeleteKeys.tsx | 47 +++-- tests/screens/DeleteKeys.test.tsx | 186 ++++++++++++++++++ tests/screens/NewKeeperApp.test.tsx | 84 ++++++++ 8 files changed, 452 insertions(+), 42 deletions(-) create mode 100644 docs/r5/WORKSTREAM.md create mode 100644 tests/screens/DeleteKeys.test.tsx create mode 100644 tests/screens/NewKeeperApp.test.tsx diff --git a/docs/r5/WORKSTREAM.md b/docs/r5/WORKSTREAM.md new file mode 100644 index 0000000000..c0d633c7ff --- /dev/null +++ b/docs/r5/WORKSTREAM.md @@ -0,0 +1,123 @@ +# R5 Cloud workstream + +Status: development draft; R4 release baseline pending. No app version/date assigned. + +## Authority and budget + +Owner request and Master Task List, Backlog row 245 (15 October 2026 scope reset), +plus rows 7, 8, 23, 76, 231 and 263. The owner's current request allows independent +implementation now; historical instructions to wait for R4 still apply to work +that depends on R4. PR base is `sprint`; do not import the unmerged R4 candidate. + +Pre-implementation changed code/test estimate (not model context or runtime cost): + +| Area | Tokens | Complexity | Regression risk | +| --- | ---: | --- | --- | +| Safe discoverable key deletion #7006 | 6k–8k | Substantial feature 1 | High: signing material and wallet references | +| Android startup/first use #7046, measured #6813 | 5k–7k | Substantial feature 2 | Medium: initialization, authentication, retries | +| Bounded #7055/#6991 and confirmed minor corrections | 2k–3k | Small investigations only | Medium until reproduced | +| Total | 13k–18k | Two substantial areas | Security review and native acceptance required | + +Warn and propose a smaller split before exceeding approximately 20k changed +code/test tokens or adding a third substantial feature. Investigations do not +implicitly authorize architectural fixes. + +Excluded: Receive QR (R7); broad components, terminology and touch targets (R8); +global text scaling and Ask Keeper reply delivery (Soon); #6917 guided setup (R10); +#7056 second backup POST and #7057 restored-fixture follow-up (Later). + +## Baseline and overlap + +Inspected R4 PR #7045 at `4927ad549dd1d610d9eb84c6d9a37ed0a6f33a29`. +It is unmerged and NOT RELEASE-READY. R4 changes the backup/deletion backend, +Login/CreatePin, Home/navigation, ResetEmergencyKey/ResetInheritanceKey, transaction +views and Android native configuration. Leave those dependencies alone now. +R4, Reproducibility, Bitkey/Assisted Multisig and Claude-review branches are not +implementation sources for this workstream and must not be modified. + +The three initial R5 UI modules are byte-identical between the inspected R4 head +and sprint. Initial independent changes: + +- Keys tab links to Manage Keys; visible eligible keys offer Delete without Hide. +- Read the full Vault collection rather than the network/canary-filtered display + hook for active-reference protection. Recheck after passcode entry. Preserve + Recovery Key/Policy Server exclusions, hidden-key authentication, cancellation + and archived-reference retention. No backend deletion or backup changes. +- Remove the duplicate setup-error modal; clear the previous error on Retry so a + subsequent failed attempt can report its error again. + +This is an initial source checkpoint, not completed R5 or native acceptance. +Backend atomic reference enforcement and account/session changes need reconciliation +with the released R4 implementation before deletion can be release-ready. + +## Remaining bounded work + +1. Reconcile with the released R4 source; rerun deletion tests and inspect every + delete/archive caller and storage/backup boundary. Include references across + normal, hidden, archived and canary wallets and signer identity variants. +2. #7046: profile Android cold/warm/resume time to usable PIN/home; remove artificial + splash wait while preserving theme/network/Tor/auth/deep links. Do not substitute + the animation's configured 3000 ms for measured startup performance. +3. Exercise setup failure/offline/retry with one dialog/one attempt, success, + relaunch and no duplicate wallet. Correct Maestro login to visible/disabled + Proceed only after the R4 shipped PIN baseline is available (sprint is older). +4. #6813: time first-wallet creation on disposable state, preserve build/device, + sample count and raw durations. Diagnose only measured bottlenecks. +5. #7055: supported single-user Android plus controlled clean state; Wallets → More + → Wallets after backup opt-in/Delete, cold launch and account changes. Preserve + logcat/task evidence. Existing report does not establish an app root cause. + No navigation patch without confirmed narrow cause. +6. #6991: inspect source timestamps, wallet list/history/details, pending/confirmed/ + cached data, timezone changes and DST on actual R4-derived build. Current source + uses local Moment/Date formatting; source inspection alone does not prove or + disprove the Android report. No speculative timezone patch. +7. Verify shipped #7007 rapid PIN/delete, lock/unlock and biometric behavior; + do not redo shipped fixes. Verify Emergency/Inheritance helper text and Assisted + Server Backup copy against what R4 actually ships, then make copy-only fixes. + +## Later Local resource plan + +No Mac is needed for Cloud development. Do not start Local native work while R4 +is being finalized. When the baseline is settled, use a separate safe checkout, +check available disk (approximately 6 GiB minimum planning threshold; require more +if the selected build needs it), run one native build at a time and reuse safe +compatible caches/artifacts. Preserve every existing worktree, simulator, AVD user, +wallet/database, artifact and evidence file. Never clean/delete them without owner +approval. Recheck free space during builds and stop safely if capacity is inadequate. + +Native matrix: iOS and Android for shared UI; physical PIN/biometric verification; +unused visible/hidden key delete and cancellation; active/hidden/canary/multi-wallet +blocks; archived retention; reference added during passcode; Recovery/Policy Server +exclusions; offline/retry/resume; small screens/light/dark/large display settings. +No native result is implied by a unit test. No merge, signing or publication authority. + +## Cloud validation, initial checkpoint + +- `yarn install --frozen-lockfile --ignore-scripts`: FAILED, existing sprint + manifest/lockfile mismatch. `corepack yarn install --pure-lockfile --ignore-scripts`: + PASSED, no tracked manifest/lockfile changes. Nested git dependency preparation + still ran its package builds. This fallback is not reproducibility evidence. +- `node_modules/.bin/jest tests/screens/DeleteKeys.test.tsx tests/screens/NewKeeperApp.test.tsx tests/components/Button.test.tsx tests/components/UTXOFooter.test.tsx --runInBand --coverage=false`: + PASS, 4 suites / 22 tests. New screen tests are 13 cases; use mocked native, + storage, signer-identity and dispatch boundaries. They do not prove actual + Realm transactions, native passcode storage, hardware or backup behavior. +- Initial test harness run: FAILED module resolution for dynamically registered + mocks (0 tests executed). Corrected mock paths; final command above passes. +- Focused ESLint on the three touched UI modules: FAIL, 29 errors / 17 warnings. + Read-only stdin comparison with origin/sprint finds identical error/warning + counts and rule/message findings (line numbers in two inherited shadow warnings + moved). Tests are ignored by the repository ESLint configuration. No claim of + repository-wide lint or type-check success. +- `git diff --check`: PASS. Localization JSON parsing: PASS. +- Security-focused executed coverage: reference blocking, reference added during + passcode entry, cancellation, archived retention, Policy Server and Recovery Key + exclusions, hidden-key concealment and repeat confirmation. Backend/session race + enforcement and true signer identity integration remain unverified. +- Native iOS/Android builds, emulator/simulator, physical biometrics/signers, + startup/first-wallet timing, CodeQL and full repository test suite: NOT RUN in + this checkpoint. No Android SDK/adb was available in this Cloud workspace. + +Next: after R4 release, reconcile this draft with its exact baseline and rerun +these tests; review backend reference enforcement before expanding deletion use. +Then complete the bounded startup work and actual performance/native matrix. +Do not claim R5 completion, close issues, merge or release this checkpoint. diff --git a/src/context/Localization/language/en.json b/src/context/Localization/language/en.json index 962b29aee2..5e40129d67 100644 --- a/src/context/Localization/language/en.json +++ b/src/context/Localization/language/en.json @@ -1291,12 +1291,12 @@ "keyDeletedSuccessfully": "Key Deleted Successfully", "keyDeletedSuccessMessage": "Your Key has been deleted successfully. You will have to add the Key again", "manageKeys": "Manage Keys", - "deleteVaultWarning": "Key Used in Hidden Vault", - "vaultWarningSubtitle": "The key you're trying to delete is being used in the hidden vault mentioned below.", - "deleteVaultInstruction": "Please change the key in the hidden vault, or delete the hidden vault, to delete this key.", - "deleteMultipleVaultWarning": "Key Used in Multiple Hidden Vaults", - "multipleVaultWarningSubtitle": "The key you're trying to delete is being used in the hidden vaults mentioned below.", - "deleteMultipleVaultInstruction": "Please change the key in the hidden vaults, or delete the hidden vaults, to delete this key", + "deleteVaultWarning": "Key Used in Wallet", + "vaultWarningSubtitle": "This key cannot be deleted because it is used by the wallet below.", + "deleteVaultInstruction": "Keep this key until the wallet no longer needs it. Hiding a wallet does not remove its key references.", + "deleteMultipleVaultWarning": "Key Used in Multiple Wallets", + "multipleVaultWarningSubtitle": "This key cannot be deleted because it is used by the wallets below.", + "deleteMultipleVaultInstruction": "Keep this key until these wallets no longer need it. Hiding a wallet does not remove its key references.", "viewVault": "View Vault", "back": "Back", "continue": "Continue", diff --git a/src/context/Localization/language/es.json b/src/context/Localization/language/es.json index a59ae5abda..c8554624e5 100644 --- a/src/context/Localization/language/es.json +++ b/src/context/Localization/language/es.json @@ -1272,12 +1272,12 @@ "keyDeletedSuccessfully": "Key Deleted Successfully", "keyDeletedSuccessMessage": "Your Key has been deleted successfully. You will have to add the Key again", "manageKeys": "Manage Keys", - "deleteVaultWarning": "Key Used in Hidden Vault", - "vaultWarningSubtitle": "The key you're trying to delete is being used in the hidden vault mentioned below.", - "deleteVaultInstruction": "Please change the key in the hidden vault, or delete the hidden vault, to delete this key.", - "deleteMultipleVaultWarning": "Key Used in Multiple Hidden Vaults", - "multipleVaultWarningSubtitle": "The key you're trying to delete is being used in the hidden vaults mentioned below.", - "deleteMultipleVaultInstruction": "Please change the key in the hidden vaults, or delete the hidden vaults, to delete this key", + "deleteVaultWarning": "Key Used in Wallet", + "vaultWarningSubtitle": "This key cannot be deleted because it is used by the wallet below.", + "deleteVaultInstruction": "Keep this key until the wallet no longer needs it. Hiding a wallet does not remove its key references.", + "deleteMultipleVaultWarning": "Key Used in Multiple Wallets", + "multipleVaultWarningSubtitle": "This key cannot be deleted because it is used by the wallets below.", + "deleteMultipleVaultInstruction": "Keep this key until these wallets no longer need it. Hiding a wallet does not remove its key references.", "viewVault": "View Vault", "back": "Back", "continue": "Continue", diff --git a/src/screens/Home/components/Keys/ManageKeys.tsx b/src/screens/Home/components/Keys/ManageKeys.tsx index 4532415f77..8af3c99820 100644 --- a/src/screens/Home/components/Keys/ManageKeys.tsx +++ b/src/screens/Home/components/Keys/ManageKeys.tsx @@ -1,6 +1,7 @@ import { Box, useColorMode } from '@gluestack-ui/themed-native-base'; import React, { useCallback, useContext, useEffect, useState } from 'react'; -import { StyleSheet } from 'react-native'; +import { StyleSheet, Pressable } from 'react-native'; +import Text from 'src/components/KeeperText'; import ToastErrorIcon from 'src/assets/images/toast_error.svg'; import KeeperModal from 'src/components/KeeperModal'; @@ -106,6 +107,14 @@ const ManageKeys = ({ addedSigner }) => { onPress={handleModalOpen} containerStyle={{ right: wp(22.5) }} /> + navigation.navigate('DeleteKeys')} + style={{ paddingVertical: hp(12) }} + > + {translations.signer.manageKeys} + diff --git a/src/screens/NewKeeperAppScreen/NewKeeperAppScreen.tsx b/src/screens/NewKeeperAppScreen/NewKeeperAppScreen.tsx index 00477729a0..7a53b22d71 100644 --- a/src/screens/NewKeeperAppScreen/NewKeeperAppScreen.tsx +++ b/src/screens/NewKeeperAppScreen/NewKeeperAppScreen.tsx @@ -14,6 +14,7 @@ import ScreenWrapper from 'src/components/ScreenWrapper'; import { getApp } from '@react-native-firebase/app'; import { getMessaging, getToken } from '@react-native-firebase/messaging'; import { setupKeeperApp } from 'src/store/sagaActions/storage'; +import { setAppCreationError } from 'src/store/reducers/login'; import useToastMessage from 'src/hooks/useToastMessage'; import { Box, Pressable, useColorMode } from '@gluestack-ui/themed-native-base'; import LoadingAnimation from 'src/components/Loader'; @@ -318,23 +319,6 @@ function NewKeeperApp({ navigation }: { navigation }) { - {}} - visible={appCreationError} - title={common.somethingWrong} - subTitle={login.checkinternetConnection} - modalBackground={`${colorMode}.modalWhiteBackground`} - textColor={`${colorMode}.textGreen`} - subTitleColor={`${colorMode}.modalSubtitleBlack`} - Content={Box} - buttonText={common.retry} - buttonCallback={() => { - setInitiating(true); - }} - subTitleWidth={wp(250)} - showCloseIcon={false} - /> {}} @@ -364,6 +348,7 @@ function NewKeeperApp({ navigation }: { navigation }) { Content={Box} buttonText={common.retry} buttonCallback={() => { + dispatch(setAppCreationError(false)); setInitiating(true); }} modalBackground={`${colorMode}.modalWhiteBackground`} diff --git a/src/screens/SigningDevices/DeleteKeys.tsx b/src/screens/SigningDevices/DeleteKeys.tsx index aad8b4b531..0d4d1fad42 100644 --- a/src/screens/SigningDevices/DeleteKeys.tsx +++ b/src/screens/SigningDevices/DeleteKeys.tsx @@ -14,7 +14,9 @@ import ShowIcon from 'src/assets/images/show.svg'; import { useDispatch } from 'react-redux'; import { updateSignerDetails } from 'src/store/sagaActions/wallets'; import { Signer, Vault } from 'src/services/wallets/interfaces/vault'; -import useVault from 'src/hooks/useVault'; +import { useQuery } from '@realm/react'; +import { RealmSchema } from 'src/storage/realm/enum'; +import { getJSONFromRealmObject } from 'src/storage/realm/utils'; import { CommonActions, useNavigation } from '@react-navigation/native'; import ActionCard from 'src/components/ActionCard'; import WalletVault from 'src/assets/images/wallet_vault.svg'; @@ -62,7 +64,9 @@ function DeleteKeys({ route }) { const navigation = useNavigation(); const [unhidingKeyUID, setUnhidingKeyUID] = useState(''); const [multipleHidden, setMultipleHidden] = useState(false); - const { allVaults } = useVault({ includeArchived: true }); + // Deletion safety must include hidden/canary wallets and both networks. + // Presentation hooks intentionally filter those records out. + const allVaults: Vault[] = useQuery(RealmSchema.Vault).map(getJSONFromRealmObject); const { archivedVaults } = useArchivedVault(); const [warningEnabled, setHideWarning] = useState(false); const [vaultsUsed, setVaultsUsed] = useState(); @@ -85,6 +89,17 @@ function DeleteKeys({ route }) { const onSuccess = () => { if (signerToDelete) { + // References may have changed while the passcode dialog was open. + const vaultsInvolved = getActiveReferences(signerToDelete); + if (vaultsInvolved.length) { + setConfirmPassVisible(false); + setSignerToDelete(null); + setMultipleHidden(vaultsInvolved.length > 1); + setVaultsUsed(vaultsInvolved); + setHideWarning(true); + return; + } + setConfirmPassVisible(false); const involvedArchivedVaults = archivedVaults.filter((vault) => vault.signers.find((s) => getKeyUID(s) === getKeyUID(signerToDelete)) ); @@ -125,11 +140,15 @@ function DeleteKeys({ route }) { } }; + const getActiveReferences = (signer: Signer) => + allVaults.filter( + (vault) => !vault.archived && vault.signers.some((s) => getKeyUID(s) === getKeyUID(signer)) + ); + const handleDelete = (signer: Signer) => { + setSignerToDelete(null); setMultipleHidden(false); - const vaultsInvolved = allVaults.filter( - (vault) => !vault.archived && vault.signers.find((s) => getKeyUID(s) === getKeyUID(signer)) - ); + const vaultsInvolved = getActiveReferences(signer); if (vaultsInvolved.length > 0) { if (vaultsInvolved.length > 1) setMultipleHidden(true); setHideWarning(true); @@ -217,7 +236,7 @@ function DeleteKeys({ route }) { return ( - + {allUnhideSigners.length === 0 && !showAll ? ( @@ -240,13 +259,11 @@ function DeleteKeys({ route }) { handleDelete(signer) : null - } + primaryAction={showDelete ? () => handleDelete(signer) : null} secondaryAction={() => signer.hidden === true ? unhide(signer) : hideSigner(signer) } - primaryText={signer.hidden === true && showDelete ? signerText.delete : null} + primaryText={showDelete ? signerText.delete : null} secondaryText={signer.hidden === true ? signerText.unhide : 'Hide'} primaryIcon={showDelete ? : null} secondaryIcon={signer.hidden === true ? : } @@ -342,7 +359,10 @@ function DeleteKeys({ route }) { setConfirmPassVisible(false)} + close={() => { + setConfirmPassVisible(false); + setSignerToDelete(null); + }} title={signerText.enterPasscode} subTitleWidth={wp(240)} subTitle={signerText.confirmPasscodeToDeleteKey} @@ -371,7 +391,10 @@ function DeleteKeys({ route }) { )} setConfirmPassVisible(false)} + close={() => { + setConfirmPassVisible(false); + setSignerToDelete(null); + }} onSuccess={onSuccess} /> diff --git a/tests/screens/DeleteKeys.test.tsx b/tests/screens/DeleteKeys.test.tsx new file mode 100644 index 0000000000..a00b2a14fd --- /dev/null +++ b/tests/screens/DeleteKeys.test.tsx @@ -0,0 +1,186 @@ +import React from 'react'; +import renderer, { act } from 'react-test-renderer'; +jest.mock('src/constants/responsive', () => ({ hp: (v) => v, wp: (v) => v, windowWidth: 400 })); + +// Render the real screen and exercise its actions. Only native/presentation, +// account storage and dispatch boundaries are replaced. +const mockDispatch = jest.fn(); +const mockSigner: any = { masterFingerprint: 'abcd', type: 'COLDCARD', hidden: false }; +let mockVaults: any[] = []; +const mockState = { bhr: {} }; +jest.mock('react-redux', () => ({ useDispatch: () => mockDispatch })); +jest.mock('@realm/react', () => ({ useQuery: () => mockVaults })); +jest.mock('src/storage/realm/utils', () => ({ getJSONFromRealmObject: (v) => v })); +jest.mock('src/hooks/useSigners', () => ({ + __esModule: true, + default: () => ({ signers: [mockSigner] }), +})); +jest.mock('src/hooks/useArchivedVaults', () => ({ + __esModule: true, + default: () => ({ archivedVaults: mockVaults.filter((v) => v.archived) }), +})); +jest.mock('src/store/hooks', () => ({ useAppSelector: (select) => select(mockState) })); +jest.mock('src/utils/utilities', () => ({ getKeyUID: (s) => s.masterFingerprint })); +jest.mock('src/hooks/usePlan', () => ({ __esModule: true, default: () => ({}) })); +jest.mock('src/hooks/useToastMessage', () => ({ + __esModule: true, + default: () => ({ showToast: jest.fn() }), +})); +jest.mock('@react-navigation/native', () => ({ + useNavigation: () => ({ dispatch: jest.fn() }), + CommonActions: {}, +})); +jest.mock('src/hardware', () => ({ + getSignerDescription: () => '', + getSignerNameFromType: () => 'Key', +})); +jest.mock('src/screens/Vault/SigningDeviceIcons', () => ({ SDIcons: () => ({ Icon: null }) })); +jest.mock('src/store/sagaActions/vaults', () => ({ + deleteSigningDevice: (signers) => ({ type: 'DELETE', signers }), + archiveSigningDevice: (signers) => ({ type: 'ARCHIVE', signers }), +})); +jest.mock('src/store/sagaActions/wallets', () => ({ updateSignerDetails: jest.fn() })); +jest.mock('src/store/reducers/bhr', () => ({ + hideDeletingKeyModal: jest.fn(), + hideKeyDeletedSuccessModal: jest.fn(), +})); +jest.mock('@gluestack-ui/themed-native-base', () => ({ + Box: 'Box', + Pressable: 'Pressable', + useColorMode: () => ({ colorMode: 'light' }), +})); +jest.mock('src/context/Localization/LocContext', () => ({ + LocalizationContext: require('react').createContext({ + translations: { + signer: new Proxy({}, { get: (_, key) => key }), + error: {}, + }, + }), +})); + +const presentation = { + 'src/components/ScreenWrapper': 'Screen', + 'src/components/Modal/PasscodeVerify': 'Passcode', + 'src/components/KeeperModal': 'Modal', + 'src/components/KeeperText': 'Text', + 'src/components/ActionCard': 'ActionCard', + 'src/components/SigningDevices/KeyCard': 'KeyCard', + 'src/components/HexagonIcon': 'Hexagon', + 'src/components/BounceLoader': 'Loader', + 'src/components/Loader': 'Tor', + 'src/components/WalletHeader': 'Header', +}; +Object.entries(presentation).forEach(([path, name]) => + jest.doMock(require.resolve(`../../${path}`), () => name) +); +['delete_bin', 'show', 'wallet_vault', 'show_wallet', 'hide_wallet', 'icon_tick'].forEach((name) => + jest.doMock(require.resolve(`../../src/assets/images/${name}.svg`), () => () => null) +); +const DeleteKeys = require('../../src/screens/SigningDevices/DeleteKeys').default; + +let screen; +const find = (name) => screen.root.findByType(name); +const modal = (title) => screen.root.findAllByType('Modal').find((m) => m.props.title === title); +function mount() { + act(() => { + screen = renderer.create(); + }); +} +function chooseDelete() { + act(() => find('KeyCard').props.primaryAction()); +} +function verifyPasscode() { + const Content = modal('enterPasscode').props.Content; + let content; + act(() => { + content = renderer.create(); + }); + act(() => content.root.findByType('Passcode').props.onSuccess()); + act(() => content.unmount()); +} +beforeEach(() => { + mockVaults = []; + mockSigner.type = 'COLDCARD'; + mockSigner.hidden = false; + delete mockSigner.signerName; + mockDispatch.mockClear(); +}); +afterEach(() => { + act(() => screen?.unmount()); +}); + +test('visible keys offer Delete without first hiding; passcode is required', () => { + mount(); + expect(find('KeyCard').props.primaryText).toBe('delete'); + chooseDelete(); + expect(mockDispatch).not.toHaveBeenCalled(); + expect(modal('enterPasscode').props.visible).toBe(true); + verifyPasscode(); + expect(mockDispatch).toHaveBeenCalledWith({ type: 'DELETE', signers: [mockSigner] }); +}); + +test.each(['normal', 'hidden', 'canary', 'other-network'])( + 'blocks key referenced by %s wallet', + (kind) => { + mockVaults = [{ kind, signers: [mockSigner], archived: false }]; + mount(); + chooseDelete(); + expect(modal('deleteVaultWarning').props.visible).toBe(true); + expect(modal('enterPasscode').props.visible).toBe(false); + expect(mockDispatch).not.toHaveBeenCalled(); + } +); + +test('rechecks a wallet reference added while passcode was open', () => { + mount(); + chooseDelete(); + mockVaults = [{ signers: [mockSigner], archived: false }]; + act(() => screen.update()); + verifyPasscode(); + expect(mockDispatch).not.toHaveBeenCalled(); + expect(modal('deleteVaultWarning').props.visible).toBe(true); +}); + +test('retains a key used by an archived wallet instead of deleting it', () => { + mockVaults = [{ signers: [mockSigner], archived: true }]; + mount(); + chooseDelete(); + verifyPasscode(); + expect(mockDispatch).toHaveBeenCalledWith({ type: 'ARCHIVE', signers: [mockSigner] }); +}); + +test('cancel clears deletion intent', () => { + mount(); + chooseDelete(); + act(() => modal('enterPasscode').props.close()); + expect(modal('enterPasscode').props.visible).toBe(false); + verifyPasscode(); + expect(mockDispatch).not.toHaveBeenCalled(); +}); + +test('Policy Server has no delete action', () => { + mockSigner.type = 'POLICY_SERVER'; + mount(); + expect(find('KeyCard').props.primaryAction).toBeNull(); + expect(find('KeyCard').props.primaryText).toBeNull(); +}); + +test('Recovery Key is excluded from key deletion', () => { + mockSigner.signerName = 'RECOVERY_KEY_SIGNER_NAME'; + mount(); + expect(screen.root.findAllByType('KeyCard')).toHaveLength(0); +}); + +test('hidden keys remain concealed before authentication', () => { + mockSigner.hidden = true; + mount(); + expect(screen.root.findAllByType('KeyCard')).toHaveLength(0); +}); + +test('repeated confirmation does not dispatch a second deletion', () => { + mount(); + chooseDelete(); + verifyPasscode(); + verifyPasscode(); + expect(mockDispatch).toHaveBeenCalledTimes(1); +}); diff --git a/tests/screens/NewKeeperApp.test.tsx b/tests/screens/NewKeeperApp.test.tsx new file mode 100644 index 0000000000..ce43c2848b --- /dev/null +++ b/tests/screens/NewKeeperApp.test.tsx @@ -0,0 +1,84 @@ +import React from 'react'; +import renderer, { act } from 'react-test-renderer'; + +const mockDispatch = jest.fn(); +const mockState = { + bhr: {}, + storage: { appCreated: false }, + login: { appCreationError: true }, + account: { allAccounts: [] }, +}; +jest.mock('src/store/hooks', () => ({ + useAppDispatch: () => mockDispatch, + useAppSelector: (select) => select(mockState), +})); +jest.mock('src/constants/responsive', () => ({ hp: (v) => v, wp: (v) => v, windowWidth: 400 })); +jest.mock('@gluestack-ui/themed-native-base', () => ({ + Box: 'Box', + Pressable: 'Pressable', + useColorMode: () => ({ colorMode: 'light' }), +})); +jest.mock('@react-navigation/native', () => ({ useIsFocused: () => true })); +jest.mock('@react-native-firebase/app', () => ({ getApp: jest.fn() })); +jest.mock('@react-native-firebase/messaging', () => ({ + getMessaging: jest.fn(), + getToken: async () => 'test-token', +})); +jest.mock('src/hooks/useToastMessage', () => ({ + __esModule: true, + default: () => ({ showToast: jest.fn() }), +})); +jest.mock('src/store/reducers/login', () => ({ + setAppCreationError: (value) => ({ type: 'ERROR', value }), +})); +jest.mock('src/store/sagaActions/storage', () => ({ + setupKeeperApp: (token) => ({ type: 'SETUP', token }), +})); +jest.mock('src/store/sagaActions/notifications', () => ({ updateFCMTokens: jest.fn() })); +jest.mock('src/utils/service-utilities/config', () => ({ + KEEPER_WEBSITE_BASE_URL: 'https://example.invalid', +})); +jest.mock('src/utils/OpenLink', () => jest.fn()); +jest.mock('src/context/Localization/LocContext', () => ({ + LocalizationContext: require('react').createContext({ + translations: require('../../src/context/Localization/language/en.json'), + }), +})); +[ + 'new-app-icon', + 'icon_arrow', + 'icon_arrow_white', + 'recover-app-icon', + 'settings', + 'settings_grey', +].forEach((name) => + jest.doMock(require.resolve(`../../src/assets/images/${name}.svg`), () => () => null) +); +Object.entries({ + 'src/components/KeeperModal': 'Modal', + 'src/components/KeeperText': 'Text', + 'src/components/ScreenWrapper': 'Screen', + 'src/components/Loader': 'Loader', + 'src/components/BounceLoader': 'BounceLoader', + 'src/components/ThemedSvg.tsx/ThemedSvg': 'Svg', +}).forEach(([path, name]) => jest.doMock(require.resolve(`../../${path}`), () => name)); +const NewKeeperApp = require('../../src/screens/NewKeeperAppScreen/NewKeeperAppScreen').default; + +test('setup failure presents one actionable retry dialog and starts one retry', async () => { + let screen; + await act(async () => { + screen = renderer.create(); + }); + const visible = screen.root.findAllByType('Modal').filter((m) => m.props.visible); + expect(visible).toHaveLength(1); + expect(visible[0].props.buttonText).toBe('Retry'); + await act(async () => { + visible[0].props.buttonCallback(); + visible[0].props.buttonCallback(); + }); + expect(mockDispatch).toHaveBeenCalledWith({ type: 'ERROR', value: false }); + expect(mockDispatch.mock.calls.filter(([a]) => a.type === 'SETUP')).toEqual([ + [{ type: 'SETUP', token: 'test-token' }], + ]); + act(() => screen.unmount()); +}); From f0a7f971fe91338a66f454b0fe431e9b622a6cf5 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Fri, 16 Oct 2026 15:37:06 +0000 Subject: [PATCH 200/254] Integrate recoverable protected backup authority and scoped recovery --- index.js | 1 + jest.config.js | 2 +- .../automatic-write-readiness.md | 12 +- .../automatic-write-validation.md | 2 + .../fix-recovery-backup-freshness/design.md | 2 + .../protected-backup-integration.md | 35 ++ .../specs/assisted-server-backup/spec.md | 2 + .../fix-recovery-backup-freshness/tasks.md | 2 + .../validation.md | 2 + package.json | 10 +- release/r4-automatic-backup-local-handoff.md | 2 + release/r4-protected-backup-app.md | 99 +++ reproducibility/android-inputs.json | 2 +- scripts/run-protected-backup-genuine.cjs | 101 +++ scripts/run-protected-backup-genuine.test.cjs | 37 ++ .../Backup/BackupHealthCheckList.tsx | 8 +- src/context/Localization/language/en.json | 148 +++-- src/context/Localization/language/es.json | 148 +++-- src/hooks/useSettingKeeper.tsx | 77 ++- .../AppSettings/AssistedBackupStatus.tsx | 37 +- src/screens/Recovery/EnterSeedScreen.tsx | 140 ++++- src/services/backup/authority.ts | 238 ++++++++ src/services/backup/automatic.ts | 122 ++-- src/services/backup/encoding.ts | 11 + src/services/backup/image.ts | 24 + src/services/backup/intent.ts | 2 + src/services/backup/localMutation.ts | 2 +- src/services/backup/localSnapshot.ts | 5 +- src/services/backup/protected.ts | 207 +++++++ src/services/backup/repair.ts | 96 +-- src/services/backup/restore.ts | 107 +++- src/services/backup/transport.ts | 37 +- src/store/reducers/bhr.ts | 23 + src/store/sagaActions/bhr.ts | 8 +- src/store/sagas/bhr.ts | 197 +++++- src/store/store.ts | 15 +- tests/recovery/accountBinding.test.cjs | 8 +- .../automaticRecoveryRestore.integration.cjs | 10 +- .../automaticWriteRealmRelay.fixture.cjs | 27 +- tests/recovery/backupFeedback.test.cjs | 3 +- tests/recovery/backupFreshness.test.cjs | 6 +- tests/recovery/backupRehydration.test.cjs | 4 + tests/recovery/helpers.cjs | 79 ++- .../protected-authority-evidence.json | 290 +++++++++ tests/recovery/protected-authority.md | 40 ++ tests/recovery/protected-authority.metro.cjs | 140 +++++ tests/recovery/protected-authority.test.mjs | 195 ++++++ .../recovery/protectedRecoverySource.test.cjs | 303 +++++++++ .../protectedWriteRealmRelay.fixture.cjs | 204 +++++++ .../protectedWriteRealmRelay.proof.cjs | 575 ++++++++++++++++++ .../recovery/recoveryAccountSession.test.cjs | 3 +- tests/recovery/recoveryErrorFeedback.test.cjs | 180 +++++- tests/recovery/repair.test.cjs | 7 +- tests/recovery/restoreIntegrity.test.cjs | 9 +- tests/recovery/revisionedMutation.test.cjs | 6 +- tests/recovery/sagaHarness.cjs | 38 +- tests/recovery/sagaState.test.cjs | 7 +- tests/recovery/sharedVaultRepair.test.cjs | 14 +- .../protected-authority-esm-loader.mjs | 32 + .../support/react-native-encoding-loader.mjs | 12 + .../recovery/walletRefreshIsolation.test.cjs | 6 +- tests/store/versionHistory.test.ts | 34 +- yarn.lock | 38 ++ 63 files changed, 3875 insertions(+), 358 deletions(-) create mode 100644 openspec/changes/fix-recovery-backup-freshness/protected-backup-integration.md create mode 100644 release/r4-protected-backup-app.md create mode 100644 scripts/run-protected-backup-genuine.cjs create mode 100644 scripts/run-protected-backup-genuine.test.cjs create mode 100644 src/services/backup/authority.ts create mode 100644 src/services/backup/encoding.ts create mode 100644 src/services/backup/protected.ts create mode 100644 tests/recovery/protected-authority-evidence.json create mode 100644 tests/recovery/protected-authority.md create mode 100644 tests/recovery/protected-authority.metro.cjs create mode 100644 tests/recovery/protected-authority.test.mjs create mode 100644 tests/recovery/protectedRecoverySource.test.cjs create mode 100644 tests/recovery/protectedWriteRealmRelay.fixture.cjs create mode 100644 tests/recovery/protectedWriteRealmRelay.proof.cjs create mode 100644 tests/recovery/support/protected-authority-esm-loader.mjs create mode 100644 tests/recovery/support/react-native-encoding-loader.mjs diff --git a/index.js b/index.js index e352be378e..c02e93f03c 100644 --- a/index.js +++ b/index.js @@ -7,6 +7,7 @@ global.atob = decode; import 'react-native-reanimated'; import 'react-native-gesture-handler'; import './shim'; +import './src/services/backup/encoding'; import { AppRegistry, Text as NativeText } from 'react-native'; import 'react-native-get-random-values'; import { Text, Input } from '@gluestack-ui/themed-native-base'; diff --git a/jest.config.js b/jest.config.js index d473261bc7..e2bee4d73c 100644 --- a/jest.config.js +++ b/jest.config.js @@ -4,7 +4,7 @@ module.exports = { setupFiles: ['./node_modules/react-native-gesture-handler/jestSetup.js'], setupFilesAfterEnv: ['./test-setup.js'], transformIgnorePatterns: [ - 'node_modules/(?!(jest-)?@?react-native|@react-navigation|@react-native-community|redux-persist|@realm)', + 'node_modules/(?!(jest-)?@?react-native|@react-navigation|@react-native-community|redux-persist|@realm|@noble/(curves|hashes)|@scure/(bip32|bip39|base)|@exodus/bytes)', ], collectCoverage: true, coverageDirectory: 'coverage', diff --git a/openspec/changes/fix-recovery-backup-freshness/automatic-write-readiness.md b/openspec/changes/fix-recovery-backup-freshness/automatic-write-readiness.md index f4d181ccf0..911b9cba12 100644 --- a/openspec/changes/fix-recovery-backup-freshness/automatic-write-readiness.md +++ b/openspec/changes/fix-recovery-backup-freshness/automatic-write-readiness.md @@ -1,3 +1,5 @@ +> Current R4 contract: [protected backup integration](protected-backup-integration.md) supersedes the historical v2 protocol and whole-copy Delete language below. Prior checked source/fixture boxes are SHA-scoped historical evidence, not protected-v3, native, hosted or release acceptance. Delete now applies only to the new protected copy; older encrypted copies remain read-only. All-copy erasure is outside approved R4 scope, unimplemented and unscheduled. + # Automatic Assisted Backup writes: implementation gates R4 requirements reconciled 13 October 2026 from audit starting app head `b160656218dc6065ad12af9db220c974255bfc23`. The original plan reviewed Keeper `0550ac9ffa938a079816ae4ed58bc970324c3e08` and Relay PR #4 `82ee3b10708a56d220170505d1bb7e3bc8429e77`; those are historical inputs, not the final proof source. @@ -15,8 +17,8 @@ The baseline central Realm observer invalidates backup verification **before** e 1. Give each logical local recovery operation an account-scoped operation ID and a commit result. A successful commit captures a monotonic recovery generation after all related Realm writes; failed or partial writes stay pending and never authorize upload. For pre-persistence producers, stage data locally and queue the cloud mutation only after this commit. Do not make server success a prerequisite for saving a new wallet, signer, vault or deletion. 2. Capture the originating `appId`, account session, verified base revision/epoch and operation ID before async work. Never substitute a newly verified base for an older action. On A→B→A, reject old queued work even if A is selected again. An already sent request has an uncertain outcome and requires readback. 3. At the head of the per-account queue, re-read the fresh local recovery image and prove the proposed operation's decrypted payload or deletion target against that image and its committed generation. Verify the selected account, backup consent, encryption key ownership, and the originally captured session immediately before sending. An operation-specific failed proof ends in pending/unverified without network. -4. Read or retain a validated v2 Relay snapshot and its 64-character revision. No legacy endpoint fallback. Apply each proposed operation to the remote snapshot in memory and reject an effect that removes remote-only recovery records or changes key identity without an explicit user decision. The Relay must atomically enforce `expectedRevision` and return `updated: true`, `preconditionApplied: true` and a valid `newRevision`. -5. A multi-record logical operation should use one Relay transaction carrying all its effects. If the existing v2 route is used sequentially, each step must validate its own current local proof and use the preceding acknowledged `newRevision`. Partial success remains pending. Never call the whole backup verified on an intermediate response. +4. Read or retain a validated protected-v3 namespace snapshot, complete raw projection checksum and original revision/generation/authEpoch tuple. The original Keeper App/provider identity remains separate. No legacy endpoint fallback. Apply each proposed operation to the remote snapshot in memory and reject an effect that removes remote-only recovery records or changes key identity without an explicit user decision. The Relay must atomically enforce original `expectedRevision`, `expectedGeneration` and `authEpoch`, returning a positive acknowledgement with its own advancing `newRevision`, `newGeneration` and correct `authEpoch`. +5. A multi-record logical operation should use one Relay transaction carrying all its effects. When protected mutations are sequential, each step must validate its own current local proof and use the preceding acknowledgement’s complete revision/generation/epoch tuple. Partial success remains pending. Never call the whole backup verified on an intermediate response. 6. After the last acknowledged write, read back and decrypt the complete Relay image. Only a match with a newly captured local image at the same committed generation may set verified. A local write during comparison invalidates the claim. The existing atomic Redux verification commit remains the final UI gate. 7. On 409, old Relay/404, malformed acknowledgement, timeout, lost response, network error, failed local commit, changed session or changed local generation, stop the chain. Do not retry prepared ciphertext or send later fanout members. Keep pending and run a read-only check. A lost response may have committed on the server; its result must be learned from readback before any fresh write. @@ -30,12 +32,12 @@ The baseline central Realm observer invalidates backup verification **before** e | `deleteVaults` | Each ID must be absent after local commit; other participant/shared-vault references must remain owned and recoverable. | Vault deletion also calls Relay before local deletion. A remote success followed by local failure can leave a mismatched backup. | | `updateVaultImage` | Decrypt the vault; its ID, sanitized signers, policy, archive state and `signersData` must match the committed Realm Vault plus persisted VaultSigner registrations. | Creation and metadata edit can call Relay before persistence. Registration changes can fan out to several vault writes; migration creates and archives two vaults. | | `modifyLabels` | Decrypt each added label and verify the account-derived hashed ID and current Realm tag. Every deleted hashed ID must map to a tag absent after local commit. Reconcile add/delete overlap and account ownership. | Imports, upgrades and transaction-label workers can emit multi-label fanouts; a delayed file callback carries older intent. | -| `deleteBackup` | Require an explicit opt-out action for the same selected account and session, a fresh v2 revision, and exact whole-account target. Preserve consent until a positive v2 acknowledgement; on lost response use readback without claiming success. | This is a destructive exception to the local-image comparison: the user intentionally removes the remote copy while keeping Realm. It needs its own opt-out state and separate tests. | +| `deleteBackup` | Require explicit Delete protected backup for the same selected account/session and original protected namespace/revision/generation/epoch. Preserve local records and consent until positive scoped acknowledgement plus complete tombstone readback. Older encrypted copies remain read-only; an uncertain reply never means Delete succeeded. | This is a destructive exception to the local-image comparison: the user intentionally removes the remote copy while keeping Realm. It needs its own opt-out state and separate tests. | ## Test matrix and remaining enablement gates -Run each of the seven operations against disposable Realm and transactional Relay fixtures, with real encryption/decryption and the production saga/transport entry points. For each operation, verify: matching committed payload/target reaches the v2 route once; stale payload after a local mutation sends nothing; A→B→A sends nothing; a server 409/old route 404/malformed response leaves pending and does not fall back; timeout or lost acknowledgement sends no next item and readback cannot create false verified. Exercise both matching and remote-only/conflicting snapshots. +Run each of the seven operations against disposable Realm and transactional Relay fixtures, with real encryption/decryption and the production saga/transport entry points. For each operation, verify: matching committed payload/target reaches the protected mutation route once; stale payload after a local mutation sends nothing; A→B→A sends nothing; a server 409/old route 404/malformed response leaves pending and does not fall back; timeout or lost acknowledgement sends no next item and readback cannot create false verified. Exercise both matching and remote-only/conflicting snapshots. -Add focused multi-write fixtures for (a) creating and archiving a vault, (b) one VaultSigner registration affecting several vaults, and (c) label import plus deletion. Make the first Relay write succeed and the next fail; assert local data survives, the remaining prepared writes do not run, and neither a saga nor Redux reports verified. Assert a local mutation injected just before the final verification commit leaves the backup pending. Pair these tests with native iOS/Android create, edit, delete, relaunch and clean Recovery Key restore on the exact app SHA, plus Relay v2 HTTP tests against a disposable replica set. +Add focused multi-write fixtures for (a) creating and archiving a vault, (b) one VaultSigner registration affecting several vaults, and (c) label import plus deletion. Make the first Relay write succeed and the next fail; assert local data survives, the remaining prepared writes do not run, and neither a saga nor Redux reports verified. Assert a local mutation injected just before the final verification commit leaves the backup pending. Pair these tests with native iOS/Android create, edit, delete, relaunch and clean Recovery Key restore on the exact app SHA, plus exact protected-protocol HTTP tests against a disposable replica set. The host source matrix now supports development loopback testing. Preserve the hosted rollout fence until transaction topology, compatible running Relay artifacts and protection from old/legacy writers are reviewed under separately authorized rollout. A source pass is not deployment evidence. A future source change must never turn an endpoint name or revision string alone into a freshness proof. The current result matrix identifies which source/fixture cases actually ran and keeps native app relaunch, critical IOS/ANDROID and release checks open independently of host results. diff --git a/openspec/changes/fix-recovery-backup-freshness/automatic-write-validation.md b/openspec/changes/fix-recovery-backup-freshness/automatic-write-validation.md index 1c0643e2cf..cb308ad06d 100644 --- a/openspec/changes/fix-recovery-backup-freshness/automatic-write-validation.md +++ b/openspec/changes/fix-recovery-backup-freshness/automatic-write-validation.md @@ -1,3 +1,5 @@ +> Current R4 contract: [protected backup integration](protected-backup-integration.md) supersedes the historical v2 protocol and whole-copy Delete language below. Prior checked source/fixture boxes are SHA-scoped historical evidence, not protected-v3, native, hosted or release acceptance. Delete now applies only to the new protected copy; older encrypted copies remain read-only. All-copy erasure is outside approved R4 scope, unimplemented and unscheduled. + # R4 automatic Assisted Backup: result matrix Updated 13 October 2026 from observed source/disposable evidence. Historical September logs remain unchanged in [validation.md](validation.md). The [sanitized October proof record](evidence/automatic-backup-source-proof-2026-10-13.md) records commands, source identities, counts and coverage boundaries. The table below retains the original Cloud source-proof result; a later exact-head Codex Local report on PR #7045 records native results and any new app SHA. diff --git a/openspec/changes/fix-recovery-backup-freshness/design.md b/openspec/changes/fix-recovery-backup-freshness/design.md index 12064230b0..7879706444 100644 --- a/openspec/changes/fix-recovery-backup-freshness/design.md +++ b/openspec/changes/fix-recovery-backup-freshness/design.md @@ -1,3 +1,5 @@ +> Current R4 contract: [protected backup integration](protected-backup-integration.md) supersedes the historical v2 protocol and whole-copy Delete language below. Prior checked source/fixture boxes are SHA-scoped historical evidence, not protected-v3, native, hosted or release acceptance. Delete now applies only to the new protected copy; older encrypted copies remain read-only. All-copy erasure is outside approved R4 scope, unimplemented and unscheduled. + # Design This is the R4 contract reconciled on 13 October 2026 from audit starting app head b160656218dc6065ad12af9db220c974255bfc23. The implemented source and observed host Realm/transactional HTTP proof are recorded in [the result matrix](automatic-write-validation.md) and [October evidence](evidence/automatic-backup-source-proof-2026-10-13.md). Source readiness permits development loopback testing; hosted Relay rollout remains disabled. The September native/fixture results in [validation](validation.md) remain historical. diff --git a/openspec/changes/fix-recovery-backup-freshness/protected-backup-integration.md b/openspec/changes/fix-recovery-backup-freshness/protected-backup-integration.md new file mode 100644 index 0000000000..f2ec922f43 --- /dev/null +++ b/openspec/changes/fix-recovery-backup-freshness/protected-backup-integration.md @@ -0,0 +1,35 @@ +# Current R4 protected-backup contract + +This supersedes older v2 and whole-copy Delete requirements for the owner's approved R4 scope. It does not mark the release complete. Exact source checks and native instructions belong to the newest PR #7045 handoff and the accompanying release evidence. + +## Identity and authorization + +The Recovery Key derives a separate self-certifying `bk3_` namespace using BIP39 seed bytes, HKDF-SHA256 and hardened BIP32 child0. The isolated supported signing implementation must not use the old wallet signing adapter. Verify that mnemonic, canonical lowercase stored seed and original Keeper App ID agree before enrollment or writing. Preserve all existing wallet, encryption and BIP85 derivations. + +The original Keeper App ID continues to own Realm, accounts, provider and billing identity. A backup namespace never replaces it, adopts a legacy account or grants a subscription. Authenticate requests using the fixed ES256K JWS profile, exact body bytes, audience/path/namespace and bounded freshness. Reads and enrollment cannot reset generation, epoch or tombstones. + +## Writes and complete verification + +All seven automatic operations and explicit full repair use protected v3 transport. Retain the original committed-local receipt, account session, consent epoch, fingerprint and namespace/revision/remote-generation/auth-epoch tuple before asynchronous work. Sequential fanout advances only through each member's own positive acknowledgement. No retry/rebase of uncertain ciphertext, observer uploads or substitution of a newly verified base. + +Validate the raw complete projection and its checksum before identity adaptation. Decrypt every recovery record; verify strict protected wallet-map IDs, encrypted label IDs, vault scheme/archive/signer references and complete scoped maps, plus recovery metadata. Only fresh matching local evidence and final synchronous Redux admission can report Verified. The projection checksum is public consistency data, not ownership proof or a server signature. + +Status reads do not enroll or write. Explicit opt-in or repair can enroll only within its original source/session/consent scope and approved hosted readiness. Initial absence, enrolled-empty, tombstone, authorization/decryption/protocol/network failure and previously-known namespace conflict stay distinct. + +## Delete, disable and recovery + +Delete means **Delete protected backup**, including a protected copy created after upgrade. Confirm the scoped acknowledgement, advancing generation/epoch and complete empty tombstone readback before reporting success or disabling consent. Preserve local wallets/keys/Recovery Key, original account/billing identity, server identity/fences/generation and retained older encrypted copies. Disclose that older copies may remain read-only even after upgrading. All-copy erasure is separately unimplemented and unscheduled; no cleanup action is offered. + +Turning off automatic updates keeps existing copies and is not Delete. Persisted deletion hints are not fresh verification authority. A lost response remains unconfirmed. + +Clean recovery checks the authenticated protected namespace first. Never automatically fall back after absence, tombstone, errors or a known-namespace conflict. A separately selected **Recover an older copy** reads retained legacy data with provenance and a missing-newer-changes warning. Decode and validate the full selected image before the first local write; never report partial/conflicted/unavailable recovery as complete. + +After validation, revoke target-account automatic-backup consent and expire old queued/opt-in scope before any restore write or producer. This applies even when saved consent was true for a not-yet-selected account. Neither recovery source silently enables or uploads the recovered copy; a later explicit opt-in is separate. + +## Acceptance and remaining gates + +Historical v2 proof stays historical. Preserve the public fixture/runtime pins and their existing guard; the separately identified exact-private genuine lane must prove current authority/transport/production consumers against real encrypted Realm and transactional loopback Mongo. Unit transport mocks, V8 bundle execution and Hermes bytecode compilation have narrower meanings than that lane or native UI acceptance. + +Hosted automatic transport, protected Delete and manual repair remain fenced until actual backend source/runtime/audience/provisioning/index/transaction/drain/ingress/read protection and compatible rollback readiness, review and separately approved enablement. Source tests do not establish deployment. Upgrading old writers requires actual per-platform availability and an approved denial interval; old binaries cannot be promised an upgrade prompt or truthful rejection UI. + +Exact-new-SHA IOS and ANDROID builds/install/launch and critical recovery/backup acceptance remain required. HARDWARE stays explicit; only an exact-SHA physical-only owner decision can defer eligible unrun physical checks. No failed critical check, store/artifact/review/channel gate or release approval is waived. Parked C8/navigation work and independence from Reproducibility/Bitkey remain unchanged. diff --git a/openspec/changes/fix-recovery-backup-freshness/specs/assisted-server-backup/spec.md b/openspec/changes/fix-recovery-backup-freshness/specs/assisted-server-backup/spec.md index 8dc7050c3a..ed49d4bdc7 100644 --- a/openspec/changes/fix-recovery-backup-freshness/specs/assisted-server-backup/spec.md +++ b/openspec/changes/fix-recovery-backup-freshness/specs/assisted-server-backup/spec.md @@ -1,3 +1,5 @@ +> Current R4 contract: [protected backup integration](../../protected-backup-integration.md) supersedes the historical v2 protocol and whole-copy Delete language below. Prior checked source/fixture boxes are SHA-scoped historical evidence, not protected-v3, native, hosted or release acceptance. Delete now applies only to the new protected copy; older encrypted copies remain read-only. All-copy erasure is outside approved R4 scope, unimplemented and unscheduled. + ## MODIFIED Requirements ### Requirement: Enabled Assisted Server Backup includes committed subsequent mutations diff --git a/openspec/changes/fix-recovery-backup-freshness/tasks.md b/openspec/changes/fix-recovery-backup-freshness/tasks.md index cac20252f7..dfb8e7aa18 100644 --- a/openspec/changes/fix-recovery-backup-freshness/tasks.md +++ b/openspec/changes/fix-recovery-backup-freshness/tasks.md @@ -1,3 +1,5 @@ +> Current R4 contract: [protected backup integration](protected-backup-integration.md) supersedes the historical v2 protocol and whole-copy Delete language below. Prior checked source/fixture boxes are SHA-scoped historical evidence, not protected-v3, native, hosted or release acceptance. Delete now applies only to the new protected copy; older encrypted copies remain read-only. All-copy erasure is outside approved R4 scope, unimplemented and unscheduled. + # R4 automatic Assisted Backup tasks Reconciled 13 October 2026 at audit starting app source b160656218dc6065ad12af9db220c974255bfc23, then updated from observed October source and disposable proof. The earlier checked checklist described a September candidate, including pre-persistence writes; it did not prove today's automatic-write contract. Historical results remain in [validation](validation.md) and unchanged evidence files. Current completion refers to [the result matrix](automatic-write-validation.md) and [sanitized October evidence](evidence/automatic-backup-source-proof-2026-10-13.md), not native or hosted release readiness. diff --git a/openspec/changes/fix-recovery-backup-freshness/validation.md b/openspec/changes/fix-recovery-backup-freshness/validation.md index 8ac0316eae..b45454ca06 100644 --- a/openspec/changes/fix-recovery-backup-freshness/validation.md +++ b/openspec/changes/fix-recovery-backup-freshness/validation.md @@ -1,3 +1,5 @@ +> Current R4 contract: [protected backup integration](protected-backup-integration.md) supersedes the historical v2 protocol and whole-copy Delete language below. Prior checked source/fixture boxes are SHA-scoped historical evidence, not protected-v3, native, hosted or release acceptance. Delete now applies only to the new protected copy; older encrypted copies remain read-only. All-copy erasure is outside approved R4 scope, unimplemented and unscheduled. + # R4 recovery validation — current gates and historical evidence ## Current automatic-write status — 13 October 2026 diff --git a/package.json b/package.json index 4f11669fa8..73041aa613 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,8 @@ "release ios prod": "cd ios && ENVFILE=.env.production bundle exec fastlane live --env production", "maestro:test:regression": "maestro test flows/regression.yaml", "test:recovery": "node --test tests/recovery/*.test.cjs", - "test:automatic-backup-genuine": "node scripts/run-automatic-backup-genuine.cjs" + "test:automatic-backup-genuine": "node scripts/run-automatic-backup-genuine.cjs", + "test:protected-backup-genuine": "node scripts/run-protected-backup-genuine.cjs" }, "dependencies": { "@bitcoinerlab/miniscript": "1.4.0", @@ -135,7 +136,12 @@ "stream": "0.0.2", "stream-browserify": "1.0.0", "tronweb": "6.0.3", - "url": "0.10.1" + "url": "0.10.1", + "@exodus/bytes": "1.16.0", + "@scure/bip32": "2.4.0", + "@scure/bip39": "2.4.0", + "@noble/curves": "2.4.0", + "@noble/hashes": "2.4.0" }, "devDependencies": { "@babel/core": "7.26.0", diff --git a/release/r4-automatic-backup-local-handoff.md b/release/r4-automatic-backup-local-handoff.md index df4871d780..bcc10561d0 100644 --- a/release/r4-automatic-backup-local-handoff.md +++ b/release/r4-automatic-backup-local-handoff.md @@ -1,5 +1,7 @@ # R4 / 2.6.4 automatic backup: Codex Local handoff +**Current protected-v3 continuation:** follow [the protected-backup app handoff](r4-protected-backup-app.md) and the newest exact-SHA PR comment. Its guarded private backend7693/loopback27038 setup supersedes the public-v2 commands below for the current app. The historical public838 results remain preserved; they do not validate v3. Do not run this document’s old v2 setup as current protected-backup acceptance. + State: **NEEDS_LOCAL**. PR [#7045](https://github.com/KeeperCommunity/bitcoin-keeper/pull/7045), branch `codex/r4-264-combined-candidate`, base `sprint`. The exact final SHA is recorded in the newest **Codex local handoff** on that PR. The setup below verifies that handoff against the live head before selecting it; this repository document cannot contain its own eventual commit SHA. **Node-bootstrap continuation:** follow the [managed producer and original-order Local retest](r4-node-bootstrap-backup.md). This supersedes the narrower queue-only retest below: use one new incremental artifact per platform and test initial Verified→ordinary detail bootstrap→automatic complete Verified→ordinary rename→complete readback/cold persistence. Preserve9a19 native controls and its original bootstrap failure separately; all remaining R4 gaps and restrictions stay open. diff --git a/release/r4-protected-backup-app.md b/release/r4-protected-backup-app.md new file mode 100644 index 0000000000..04ab108e6c --- /dev/null +++ b/release/r4-protected-backup-app.md @@ -0,0 +1,99 @@ +# R4 / 2.6.4 protected-backup app handoff + +State: **NEEDS_LOCAL; R4 remains BLOCKED.** Use the full app SHA in the newest **Codex local handoff** on [PR #7045](https://github.com/KeeperCommunity/bitcoin-keeper/pull/7045), verify it equals the live PR head, and preserve every earlier result under its original SHA. This document cannot name its own eventual commit. The current v3 instructions here supersede the historical public-v2 fixture commands in [the earlier handoff](r4-automatic-backup-local-handoff.md). + +## Scope and identity + +Production entry points are the protected authority/transport, existing automatic-write producer/queue and repair/status paths, confirmed Delete worker, and typed Recovery Key discovery/restore. Authority is derived separately from the Recovery Key with the supported signing profile; it does not turn legacy decryption or an old account identifier into proof of ownership. Existing Keeper app identity, wallet/key derivation, encrypted payload format, provider/billing identity and local-first recovery records stay separate. Missing/stale account, session, consent, committed-generation, original-base, fingerprint, CAS or final-readback proof remains fail-closed. + +Hosted rollout readiness stays **false**. Read-only status never enrolls or enables automatic backup. Delete removes **only the new protected copy**, preserves local wallets/Recovery Key and records its confirmed tombstone. Older encrypted copies may remain read-only. A lost/malformed/uncertain Delete reply is not success; preserve local data and check status rather than retrying uncertain ciphertext. Turning off automatic backup keeps copies. Re-enrollment after Delete is an explicit new-copy action, with no automatic legacy fallback. + +Recovery checks the protected namespace first. Absent, empty, deleted and read/auth/network errors are distinct typed results; errors do not select legacy recovery. Only the deliberate **Recover an older copy** choice permits that read. Complete validation precedes recovery success, whose provenance must match the selected source and current account. Partial/unavailable recovery is not full success. Restore leaves automatic-backup consent **off**; enabling the protected copy is a separate explicit action. EN and reachable ES disclose the retained-copy scope. + +## Dependency and source preservation + +Exactly five direct JS dependencies were added: `@exodus/bytes@1.16.0`, `@scure/bip32@2.4.0`, `@scure/bip39@2.4.0`, `@noble/curves@2.4.0`, and `@noble/hashes@2.4.0`. Yarn adds six selectors, including nested `@scure/base@2.4.0`; all 2,274 original selectors and old direct pins are unchanged. Existing QR resolution remains `@scure/base@1.1.9`; existing wallet crypto remains independent. The JS entry adds only the encoding initializer, which installs TextEncoder only when absent. The Android input receipt changes only its matching yarn-lock hash. + +Android/iOS inputs, patches, native dependencies, JNA/Portal, Metro/Babel/shim, fixed configuration and the approved contributor `dev/local-backend/sources.lock.json` remain unchanged from `4927ad549dd1d610d9eb84c6d9a37ed0a6f33a29`. This is source preservation, not transfer of old native acceptance to the new SHA. + +## Cloud and disposable checks + +Use Node **22.23.3** or a later supported 22.x patch and frozen Yarn Classic **1.22.22** dependencies. Keep the separate private backend checkout at **`7693ce4bfffcadb5fb71d6478a8befc8e533a844`**, clean and obtained with authorized private read access. The opt-in runner checks that commit, fixed nonsecret input hashes, Mongoose 5.13.20/driver 3.7.4, genuine encrypted native Realm commit/reopen, and a credential-free disposable loopback replica at port **27038**. Never point it at hosted services or import/start the full backend/provider service graph. + +```sh +KEEPER_PROTECTED_RELAY_CHECKOUT=/absolute/path/to/reviewed-private-backend \ +BACKUP_TEST_MONGO_URL=mongodb://127.0.0.1:27038 \ +yarn test:protected-backup-genuine +node --test tests/recovery/protected-authority.test.mjs +node tests/recovery/protected-authority.metro.cjs +node --test tests/recovery/protectedRecoverySource.test.cjs tests/recovery/recoveryErrorFeedback.test.cjs scripts/run-protected-backup-genuine.test.cjs +``` + +The private genuine lane exercises actual production saga/store/crypto, encrypted Realm and transactional backend HTTP/readback. It is separate from the public Relay **`838049e537e07eb646a2a0393eb969fba43d0739`** v2 lane. Public/fork CI cannot obtain the private fixture automatically; public838 results do not validate this v3 protocol. Do not replace contributor pins, vendor private backend source into the app, or skip the private proof and report success. + +The signer command runs actual installed production crypto/vector/interoperability checks. The focused Metro command resolves mobile/default encoding and authority sources, executes its bundle in Node V8 and compiles Hermes bytecode; it does **not** execute Hermes or the full native App. The unit command is a narrow recovery/guard lane, not the entire acceptance matrix. Missing native bindings, skipped genuine proof and substituted algorithm mocks remain failures or blockers. + +Source-bound construction results retained before the final commit: signer13/13, focused corrected genuine5/5, Jest42 suites/432 tests, release Node28/Python7/Ruby21 (117 assertions), all with zero skips. The newest PR receipt records the final frozen38 genuine cases and whole480 recovery outcome separately. Existing diagnostic failures are retained: initial Jest430/432, recovery473/480; later recovery478/480 (two missing AST bindings, both affected files subsequently36/36). A genuine vault-map alteration was RED0/1 then GREEN1/1 after complete projection validation; a genuine new-vault producer was RED0/1 then GREEN in the focused5/5 after including its scheme. + +Expanded runtime lint has13 errors/2 warnings both at the4927 baseline and current source, with zero added/removed normalized findings; UI comparison36/21 versus38/21 adds none. Whole-project TypeScript remains failing with944 diagnostics on each tree. Seven raw TS2339 message renderings differ only in abbreviated inferred locale types; the same missing properties remain. Normalization adds/removes zero diagnostics. This is baseline comparison, not a clean typecheck or lint pass. New source support, advisory and Metro receipts are in `tests/recovery/protected-authority-evidence.json`; the independent engineering review is not professional crypto certification. + +The final exact-head comment supplies all commands, case classifications, source hashes, stable-input manifests, compressed log retrieval and actual final outcomes. Native IOS/ANDROID/HARDWARE, Hermes runtime and full-app startup are NOT RUN in Cloud. + +Do not convert earlier source-bound or native results into final-head passes. The newest PR comment must record exact commands, full app/backend identities, actual counts/failures/skips and log hashes. + +## Codex Local: bounded current-source acceptance + +Required local checks: **IOS, ANDROID, HARDWARE**. Keep the owner's [minimal native batch](https://github.com/KeeperCommunity/bitcoin-keeper/pull/7045#issuecomment-6185462332) and all remaining R4 gates. C8/[#7056](https://github.com/KeeperCommunity/bitcoin-keeper/issues/7056) and navigation work remain parked; no new fix/rebuild loop is authorized by this document. Old strict16KB/4KB/iOS results stay SHA-scoped. Only a fresh exact-SHA owner decision can defer eligible unrun physical checks; required platform failures cannot be waived. + +### Disposable native transport preparation + +The public contributor stack does not supply v3. Obtain the exact private backend separately and retain the approved public source manifest. In the app checkout use the same environment as the genuine runner, with a task-owned credential-free `keeperQA` replica on loopback27038 and frozen dependencies/native Realm. Run this **proposed, not native-executed in Cloud** long-lived command from the final app checkout: + +```sh +KEEPER_PROTECTED_RELAY_CHECKOUT=/absolute/path/to/reviewed-private-backend \ +BACKUP_TEST_MONGO_URL=mongodb://127.0.0.1:27038 \ +node <<'NODE' +const assert = require('node:assert/strict'); +const { checkedBackend, supportedNode, disposableMongoUrl } = require('./scripts/run-protected-backup-genuine.cjs'); +assert.ok(supportedNode(process.versions.node)); +checkedBackend(process.env.KEEPER_PROTECTED_RELAY_CHECKOUT); +process.env.BACKUP_TEST_MONGO_URL = disposableMongoUrl(process.env.BACKUP_TEST_MONGO_URL); +(async () => { + const { createProtectedServer } = require('./tests/recovery/protectedWriteRealmRelay.fixture.cjs'); + const fixture = await createProtectedServer(); + console.log(JSON.stringify({ relay: fixture.url, audience: fixture.audience, + database: fixture.backend.database, backend: '7693ce4bfffcadb5fb71d6478a8befc8e533a844' })); + let closing = false; + const close = async () => { + if (closing) return; + closing = true; + await fixture.close(); + }; + process.once('SIGINT', () => { close().catch(() => { process.exitCode = 1; }); }); + process.once('SIGTERM', () => { close().catch(() => { process.exitCode = 1; }); }); +})().catch(() => { console.error('Disposable protected fixture startup failed'); process.exitCode = 1; }); +NODE +``` + +This uses actual private v3 router/services plus the two exact legacy read declarations, inert provider traps and a distinct disposable database. No full backend/bootstrap/config/credentials are loaded. It binds an ephemeral loopback port; do not run the full matrix concurrently against its single-process fixture. The fixture close operation drops **only its owned disposable database**; keep a diagnosed failure process/database alive until evidence is retained. Container volumes and all existing databases remain protected. Nothing here authorizes a hosted call. + +In a separate **uncommitted local QA environment file**, set `ENVIRONMENT=DEVELOPMENT` and `RELAY` to the printed exact `http://127.0.0.1:/` including trailing slash. Keep the existing reviewed local channel/SigningServer/test-only values; never print/copy hosted configuration. `src/utils/service-utilities/config.ts` consumes this field. `src/services/backup/protected.ts` and `automatic.ts` admit only explicit-port development loopback without changing their hosted false constants. For Android use `adb -s reverse tcp: tcp:`; iOS simulator uses host loopback. Record the exact QA env-file hash/path and actual ENVFILE/native configuration selection, without exporting values. + +**Boundary:** this fixture serves backups, not onboarding/channel/provider APIs. The app has one RELAY origin and no per-v3 override. Prepare a disposable consent-OFF account through the already reviewed local onboarding stack, then switch that QA configuration to this backup-only server for the exact-source native build/test; preserve the account's original identity and local records. It cannot establish full fresh onboarding against a composed v3 service. If an end-to-end onboarding gateway is needed, a fixed reviewed local-only forwarding composition remains required: do not silently invent it, start a private full service/provider graph, or claim this standalone launcher implements it. Clean protected restore is exercised from the app's recovery path, with real native success still to be established. Explicit older-copy native recovery additionally requires deliberately seeded disposable legacy ciphertext; do not point at retained customer copies. The actual encrypted fixture seeds/readback and failure injection are in `protectedWriteRealmRelay.proof.cjs` and `.fixture.cjs`; native hooks do not inherit those Node proof passes. + +Build, install and launch the actual current development artifact on both platforms, then preserve artifact hashes and test: + +- Fresh explicit opt-in → complete protected Verified; ordinary wallet-detail default-node bootstrap → automatic complete Verified → ordinary rename → full decrypted image/metadata readback. Test the original order, including a fresh account, rather than repairing the baseline first. +- Consent off/status without enrollment, current-account/session isolation, complete protected restore and deliberate older-copy restore; no legacy fallback on tombstone, empty/absent protected data, or errors. Available records must be complete before success; restore must keep consent off. +- Scoped Delete confirmed versus uncertain result, cold tombstone discovery and explicit recreate; older-copy warning/provenance and EN/ES enable/disable/status/recovery notices. Preserve local recovery data throughout. +- Cold PIN/MMKV/encrypted Realm persistence, relevant wallet/base smoke and Portal behavior. Android must actually launch with strict 16KB settings and exercise real Hermes/crypto initialization; retain normal4KB and iOS acceptance separately as requested in the minimal batch. + +Record unrun hardware interactions as NOT RUN, with the actual exact-SHA owner scope decision if applicable. Report any new failure without speculating that an earlier fix uniquely explains it. + +## Storage and authorization boundaries + +The latest supplied Mac report is **16 October 15:30:40 UTC**, **50.9437 GiB free**. The proposed Stage B estimate is **47 GiB**, leaving arithmetic margin **3.9437 GiB**; this is a dated report, not current capacity or a fit decision. The storage helper still exited **2**: embedded-worktree fingerprint/source bindings and a non-null fit assessment remain required. Preserve all protected source/evidence/caches/devices/wallets/volumes. No cleanup was authorized. + +Before any heavy checkout/install/fixture/native resumption and at closeout, follow available `AGENTS.override.md`, `keeper-local-memory.md` and `dev-environment/release-runbook.md`; run `python3 dev-environment/release-storage.py assess --context ` with the fresh complete remaining-peak/retry budget. Receipts expire after 30 minutes. Resolve the capacity/fit/source-binding gaps before heavy work; no timer/background cleanup. + +Backend promotion/deployment/index/transaction/CAS/fleet/old-writer and ingress/protection evidence, hosted app enablement at a separately reviewed new SHA, exact-head local acceptance, store/channel/version/artifact approvals and physical follow-up remain separate open gates. No merge, production deployment/index/data/admin write, release signing, production AAB/split construction, upload/submission/publication, provider call or real transaction is authorized here. Keep R4 independent of Reproducibility and Bitkey Wallet. diff --git a/reproducibility/android-inputs.json b/reproducibility/android-inputs.json index eac1c1e6a0..b806f15ca5 100644 --- a/reproducibility/android-inputs.json +++ b/reproducibility/android-inputs.json @@ -2,7 +2,7 @@ "node": "22.23.3", "yarn": "1.22.22", "java": "17.0.19", - "yarn_lock_sha256": "2764cadd01935bfe8542d858a58e9d397ee3479633f3320f8f3dbbff20ae425a", + "yarn_lock_sha256": "a915c8ea0c4446270c0f6c894998f0c4fa481bcc4edbea328e57377613939056", "gradle_wrapper_jar_sha256": "0336f591bc0ec9aa0c9988929b93ecc916b3c1d52aed202c7381db144aa0ef15", "gradle_distribution_sha256": "bd71102213493060956ec229d946beee57158dbd89d0e62b91bca0fa2c5f3531", "android_packages": { diff --git a/scripts/run-protected-backup-genuine.cjs b/scripts/run-protected-backup-genuine.cjs new file mode 100644 index 0000000000..cc556cc60c --- /dev/null +++ b/scripts/run-protected-backup-genuine.cjs @@ -0,0 +1,101 @@ +// Separate opt-in protected-protocol proof. The public v2 lane and its pins stay +// independent. This guard contains only reviewed identities, never backend code. +const assert = require('node:assert/strict'); +const crypto = require('node:crypto'); +const fs = require('node:fs'); +const os = require('node:os'); +const path = require('node:path'); +const { spawnSync } = require('node:child_process'); +const { createRequire } = require('node:module'); + +const BACKEND_COMMIT = '7693ce4bfffcadb5fb71d6478a8befc8e533a844'; +const BACKEND_FILES = Object.freeze({ + 'package.json': '091bbd47dc459a13276b9dee0122b54ef002c45442771b0d06a408d013ea9988', + 'yarn.lock': 'b989c1197f6ddc9530d400a354bab42af8974ac4687f0ba0a178cb2f787b0321', + 'src/services/protectedBackupAuth.ts': 'b45fbfe0b1481a52a0158b9cbca79a619e14484e254064ef8efb5155ddfa7ecf', + 'src/services/protectedBackup.ts': '087f2d5190d9bcd68ba3a529712d7ca60b06fd487f1f021c0c4907ebd6235a76', + 'src/routes/protectedBackup.ts': '6eb2f8ba2c70cc459c88068f81a6dae2e6efa507468c84cafb2a7508fb78bcfb', + 'src/routes/routes.ts': '245fec3b717fab83957bc1690cd9d76395c031b0c2525414846ba55081ec2183', + 'src/services/backupSnapshot.ts': '3c097222970c88d1d9af154c6cc50bf532f0d1559050af65fafecf2aa59ac187', + 'src/services/bhr.ts': '72736ddaca020d293adee9b430343493e07123dfbc54b301914e5d766092df15', + 'src/db.ts': '52455dc9472a713afc538189db541ced108e1a0dcb4246b824da0bff120f8992', + 'tests/support/protectedBackupFixture.cjs': '3ce114525e34bea75e515df596b10da82a73becadc253e2ecae223cf3d37d38c', + 'tests/support/protectedBackupClient.cjs': 'dee3441f65defc0cd5dbbc282bc179210edfa7b544b4db41e501468bece8022f', + 'tests/disposableMongo.cjs': 'a9dd8d12f37aafef09003ef47369cb7416692fd07e28ad96c25a0533cf99ee96', +}); + +function supportedNode(version) { + const match = /^v?(\d+)\.(\d+)\.(\d+)$/.exec(version); + return !!match && Number(match[1]) === 22 && + (Number(match[2]) > 23 || (Number(match[2]) === 23 && Number(match[3]) >= 3)); +} + +function disposableMongoUrl(value) { + const url = new URL(value || 'mongodb://127.0.0.1:27038'); + assert.equal(url.protocol, 'mongodb:', 'Use a disposable loopback MongoDB replica'); + assert.ok(['127.0.0.1', 'localhost'].includes(url.hostname), 'Hosted MongoDB is forbidden in this proof'); + assert.equal(url.port, '27038', 'Use the separately approved keeperQA fixture on port 27038'); + assert.ok(!url.username && !url.password, 'This proof does not accept credentials'); + assert.ok(['', '/'].includes(url.pathname) && !url.search && !url.hash, + 'Pass only the loopback server address; the fixture chooses its own database'); + return url.toString(); +} + +function checkedBackend(checkout) { + assert.ok(checkout, 'Set KEEPER_PROTECTED_RELAY_CHECKOUT to the separately obtained exact reviewed backend checkout'); + const directory = fs.realpathSync(checkout); + const git = (...arguments_) => spawnSync('git', ['-C', directory, ...arguments_], { encoding: 'utf8' }); + const head = git('rev-parse', 'HEAD'); + assert.equal(head.status, 0, 'The protected fixture must be in a Git checkout'); + assert.equal(head.stdout.trim(), BACKEND_COMMIT, `Use exact reviewed backend ${BACKEND_COMMIT}`); + assert.equal(git('diff', '--quiet', 'HEAD').status, 0, 'The reviewed backend must have no tracked changes'); + for (const [relative, expected] of Object.entries(BACKEND_FILES)) { + const filename = path.join(directory, relative); + assert.equal(fs.realpathSync(filename), filename, 'The fixed backend inputs must not be symlink aliases'); + assert.equal(crypto.createHash('sha256').update(fs.readFileSync(filename)).digest('hex'), expected, + `Reviewed backend input differs: ${relative}`); + } + const requireBackend = createRequire(path.join(directory, 'package.json')); + for (const [name, version] of [['mongoose', '5.13.20'], ['mongodb', '3.7.4']]) + assert.equal(requireBackend(`${name}/package.json`).version, version, 'Use the reviewed frozen native driver lane'); + return directory; +} + +function nativeRealmPrerequisite() { + const Realm = require('realm'); + const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'keeper-protected-realm-prerequisite-')); + const filename = path.join(directory, 'probe.realm'); + const options = { path: filename, encryptionKey: Buffer.alloc(64, 7), + schema: [{ name: 'NativeProbe', primaryKey: 'id', properties: { id: 'string', value: 'int' } }] }; + let realm; + try { + realm = new Realm(options); + realm.write(() => realm.create('NativeProbe', { id: 'native', value: 1 })); + realm.close(); + realm = new Realm(options); + assert.equal(realm.objectForPrimaryKey('NativeProbe', 'native').value, 1, + 'Actual native encrypted Realm must commit and reopen'); + } finally { + if (realm && !realm.isClosed) realm.close(); + Realm.shutdown(); + fs.rmSync(directory, { recursive: true, force: true }); + } +} + +function main() { + assert.ok(supportedNode(process.versions.node), 'Use Node22.23.3 or a later22.x patch for the protected proof'); + const checkout = checkedBackend(process.env.KEEPER_PROTECTED_RELAY_CHECKOUT); + const mongo = disposableMongoUrl(process.env.BACKUP_TEST_MONGO_URL); + nativeRealmPrerequisite(); + const app = path.resolve(__dirname, '..'); + const test = 'tests/recovery/protectedWriteRealmRelay.proof.cjs'; + assert.ok(fs.existsSync(path.join(app, test)), 'Missing separate protected production proof'); + const result = spawnSync(process.execPath, ['--test', test], { + cwd: app, stdio: 'inherit', env: { ...process.env, + KEEPER_PROTECTED_RELAY_CHECKOUT: checkout, BACKUP_TEST_MONGO_URL: mongo }, + }); + process.exitCode = result.status ?? 1; +} + +module.exports = { BACKEND_COMMIT, BACKEND_FILES, supportedNode, disposableMongoUrl, checkedBackend }; +if (require.main === module) main(); diff --git a/scripts/run-protected-backup-genuine.test.cjs b/scripts/run-protected-backup-genuine.test.cjs new file mode 100644 index 0000000000..f8bb6fed80 --- /dev/null +++ b/scripts/run-protected-backup-genuine.test.cjs @@ -0,0 +1,37 @@ +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const os = require('node:os'); +const path = require('node:path'); +const { spawnSync } = require('node:child_process'); +const { test } = require('node:test'); +const { supportedNode, disposableMongoUrl, checkedBackend } = require('./run-protected-backup-genuine.cjs'); + +test('protected proof requires the reviewed current22 runtime without accepting other majors or prereleases', () => { + for (const version of ['22.23.3', 'v22.23.4', '22.24.0']) assert.equal(supportedNode(version), true); + for (const version of ['22.16.0', '22.23.2', '20.23.3', '23.0.0', '22.23.3-rc.1', 'invalid']) + assert.equal(supportedNode(version), false); +}); + +test('protected proof refuses hosted, credentialed, aliased-port or database-selecting Mongo targets', () => { + assert.equal(disposableMongoUrl('mongodb://127.0.0.1:27038'), 'mongodb://127.0.0.1:27038'); + assert.equal(disposableMongoUrl('mongodb://localhost:27038/'), 'mongodb://localhost:27038/'); + for (const address of ['mongodb://example.invalid:27038', 'mongodb://user:pass@127.0.0.1:27038', + 'mongodb+srv://localhost', 'mongodb://127.0.0.1:27028', 'mongodb://127.0.0.1:27038/production', + 'mongodb://127.0.0.1:27038/?replicaSet=elsewhere', 'mongodb://127.0.0.1:27038/#fragment']) + assert.throws(() => disposableMongoUrl(address)); +}); + +test('an unreviewed local checkout is rejected before any fixture source can load', () => { + const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'keeper-protected-guard-')); + try { + for (const args of [['init', '--quiet'], ['-c', 'core.hooksPath=/dev/null', '-c', 'commit.gpgsign=false', + '-c', 'user.name=Fixture', '-c', 'user.email=fixture@invalid', + 'commit', '--quiet', '--allow-empty', '-m', 'Disposable unreviewed fixture']]) { + const result = spawnSync('git', ['-C', directory, ...args], { encoding: 'utf8' }); + assert.equal(result.status, 0, 'Create only an owned disposable local Git fixture'); + } + assert.throws(() => checkedBackend(directory), /Use exact reviewed backend/); + } finally { + fs.rmSync(directory, { recursive: true, force: true }); + } +}); diff --git a/src/components/Backup/BackupHealthCheckList.tsx b/src/components/Backup/BackupHealthCheckList.tsx index 35f43e341b..8205e8a4c2 100644 --- a/src/components/Backup/BackupHealthCheckList.tsx +++ b/src/components/Backup/BackupHealthCheckList.tsx @@ -65,6 +65,9 @@ function Content({ contentType, asbEnabled }: { contentType: string; asbEnabled? {illustrations[contentType]} {descriptions[contentType] && {descriptions[contentType]}} {asbEnabled && {BackupWallet.assistedServerBackupEnabled}} + {contentType !== ContentType.healthCheckSuccessful && ( + {BackupWallet.retainedOlderCopyNotice} + )} ); } @@ -108,7 +111,6 @@ function BackupHealthCheckList({ isUaiFlow }) { setShowConfirmSeedModal(true); }; - useEffect(() => { if (backupAllSuccess) { if (!automaticCloudBackup) setAsbEnabled(true); @@ -288,12 +290,12 @@ function BackupHealthCheckList({ isUaiFlow }) { closeOnOverlayClick={false} buttonTextColor={`${colorMode}.buttonText`} buttonBackground={`${colorMode}.pantoneGreen`} - buttonText={'Backup Now'} + buttonText={BackupWallet.backUpNow} buttonCallback={() => { dispatch(backupAllSignersAndVaults(id)); setBackupMismatchModal(false); }} - secondaryButtonText="Home" + secondaryButtonText={BackupWallet.home} secondaryCallback={() => { navigtaion.dispatch(CommonActions.navigate('Home')); }} diff --git a/src/context/Localization/language/en.json b/src/context/Localization/language/en.json index 713747f3f8..26178829f7 100644 --- a/src/context/Localization/language/en.json +++ b/src/context/Localization/language/en.json @@ -194,7 +194,7 @@ "MismatchPasscode": "Passcodes do not match", "enter_your": "Enter your ", "NewSatrtWalet": "A new hot wallet will be created automatically for you. You can create and manage different kinds of wallets from the wallets section of the app.", - "RecoverExistingAppDesc": "Have an inaccessible Keeper app with wallets in it? You have nothing to worry if you have that app's 12-word Recovery Key. Insert the words in the Recovery section on the welcome screen and all your wallets will be restored as expected.", + "RecoverExistingAppDesc": "Use the Keeper app’s Recovery Key to check its available protected backup. An older read-only recovery copy is a separate choice and may not include newer changes.", "RecoverApp": "Recover an existing app:", "Wait": "This step will take a few seconds. You will be able to proceed soon", "Createpasscode": "Create a passcode", @@ -800,17 +800,17 @@ "backupPhrase": "Recovery Key", "walletSeedWords": "Recovery Key", "enterRecoveryPhrase": "Recover an existing Keeper App", - "enterRecoveryPhraseSubTitle": "Enter existing 12-word recovery phrase below", + "enterRecoveryPhraseSubTitle": "Enter this Keeper app’s 12-word Recovery Key. Its protected backup is checked first.", "walletRecoveryPhrase": "Wallet Recovery Phrase", "showXPubNoteSubText": "Losing your Wallet Seed Words may result in permanent loss of funds. Store them carefully.", "mobileKeyVerified": "Mobile Key verified successfully", "seedWordVerified": "Seed Words verified successfully", "keeperVerified": "Keeper Verified Successfully", "SeedErrorToast": "Please fill in all seed word fields", - "appRecoveredSuccessfulTitle": "App Recovered Successfully!", - "recoveryIncomplete": "Recovery could not be completed. Your backup may be unavailable or some data could not be restored. Please try again.", - "appRecoveredSuccessfulSubTitle": "All your wallets and data about your vault has been recovered", - "appRecoveredSuccessfulDesc": "The vault keys always stay within the signing devices and therefore do not have to be recovered", + "appRecoveredSuccessfulTitle": "Backup recovered", + "recoveryIncomplete": "Recovery was not completed. Available backup data could not be fully checked or restored. No older copy was selected automatically.", + "appRecoveredSuccessfulSubTitle": "The selected backup’s available recovery data was checked and restored.", + "appRecoveredSuccessfulDesc": "Hardware signing keys stay on their devices. Review recovered records before enabling protected backup.", "mobileKey": "Mobile Key", "mobileKeySeedWordsTitle": "Mobile Key Seed Words", "mobileKeySeedNoteSubTitle": "The QR below comprises of your 12-word Mobile Key", @@ -818,7 +818,24 @@ "storeSecurely": "Write down these words and store them securely.", "enterAmountToSend": "Enter Amount to Send", "feePriority": "Fee Priority", - "seedKey": "Seed Key" + "seedKey": "Seed Key", + "protectedRecoveryAbsentTitle": "No protected backup found", + "protectedRecoveryAbsentBody": "This Recovery Key has no protected copy. You can choose to look for an older read-only recovery copy.", + "protectedRecoveryDeletedTitle": "Your protected backup was deleted", + "protectedRecoveryDeletedBody": "Keeper will not restore an older copy automatically. An older read-only copy may predate deletion and omit newer changes.", + "protectedRecoveryErrorTitle": "Could not check protected backup", + "protectedRecoveryErrorBody": "The protected backup could not be checked. No older copy was selected. Check your connection and try again.", + "recoverOlderCopy": "Recover an older copy", + "recoverOlderCopyTitle": "Recover an older copy?", + "recoverOlderCopyBody": "An older read-only backup may predate deletion or omit newer changes. Recovery uses only the complete available records and does not turn on automatic backup.", + "protectedRecoverySuccessfulTitle": "Protected backup recovered", + "protectedRecoverySuccessfulSubTitle": "The selected protected copy’s available recovery data was checked and restored.", + "olderRecoverySuccessfulTitle": "Older backup recovered", + "olderRecoverySuccessfulSubTitle": "Recovered from an older copy. Newer changes may be missing. Automatic backup stays off.", + "protectedRecoveryEmptyTitle": "Protected backup is empty", + "protectedRecoveryEmptyBody": "This protected copy has no recovery records. You may explicitly choose to look for an older read-only copy.", + "olderRecoveryErrorTitle": "Older backup recovery incomplete", + "olderRecoveryErrorBody": "The older copy could not be fully checked or restored. Keep your Recovery Key and check the connection and available records before trying again." }, "healthcheck": { "HealthCheck": "Perform Health Check", @@ -1096,11 +1113,11 @@ "serverSelectionSubtitle": "Select a server you'd like to use. Add your own node for enhanced privacy.", "publicServerNote": "It’s recommended to use your own Electrum server to protect your privacy", "connectToServer": "Connect to Server", - "assistedServerBackup": "Assisted Server Backup", - "assistedServerBackupSubtitle": "Back up encrypted data using a community-run Keeper server.", - "assistedServerDeleteBackupSubtitle": "Do you also want to delete the existing assisted server backup?", - "assistedServerDeleteBackupModalCTA": "Delete", - "assistedServerDeleteBackupModalSecondaryCTA": "Only disable", + "assistedServerBackup": "Protected server backup", + "assistedServerBackupSubtitle": "Keep a new encrypted protected copy. Older read-only recovery copies may remain.", + "assistedServerDeleteBackupSubtitle": "This deletes your protected copy and turns off automatic backup. Older encrypted recovery copies may remain read-only. Your wallets and Recovery Key on this device are kept. Turning off automatic backup alone keeps all copies.", + "assistedServerDeleteBackupModalCTA": "Delete protected backup", + "assistedServerDeleteBackupModalSecondaryCTA": "Turn off automatic backup", "cameraAccessoff": "Camera access is turned off", "turnCameraOn": "Turn on the camera in your device settings", "tapToGoSetting": "Tap to go to settings", @@ -1146,7 +1163,13 @@ "changeWallet": "Change Wallet", "customAmount": "Custom amount", "sendNoteToDevelopers": "Send a note to the developers", - "messageToDeveloper": "Message to developer" + "messageToDeveloper": "Message to developer", + "assistedServerDeleteBackupTitle": "Delete protected backup?", + "assistedServerDeleteBackupConfirmed": "Protected backup deleted. Older encrypted recovery copies may remain. Your local wallets and Recovery Key are unchanged.", + "assistedServerBackupDisabled": "Automatic backup is off. Existing backup copies were kept.", + "assistedServerEnableBackupTitle": "Enable protected backup?", + "assistedServerEnableBackupSubtitle": "Create or update your protected copy from this device’s recovery data. Older read-only copies are not changed. Keep your Recovery Key safe.", + "assistedServerEnableBackupCTA": "Enable protected backup" }, "onboarding": { "security": "security", @@ -1211,15 +1234,15 @@ "enterThe": "Enter the", "seedWord": "seed word", "enterSeedWordPlaceholder": "Enter seed word", - "verifyModalTitle": "Verifying Server Backup", - "verifyModalSubTitle": "Verifying your server backup integrity against your local data.\nThis may take a moment.", - "backupFailedModalTitle": "Backup Verification Failed", - "backupFailedModalSubTitle": "We couldn’t verify your data with the server backup.", - "backupFailedModalDesc": "There might be a temporary issue with the server or your connection. Please try again later.", - "mismatchModalTitle": "Data Mismatch Detected", - "mismatchModalSubTitle": "Your local data doesn’t match the server backup.", - "mismatchModalDesc": "Some recent changes may not be backed up yet. To avoid data loss, we recommend initiating a backup now.", - "assistedServerBackupEnabled": "Assisted server backup is now enabled.\nThis will backup your encrypted data on a community-run Keeper server", + "verifyModalTitle": "Verifying protected backup", + "verifyModalSubTitle": "Comparing the protected copy with recovery data on this device.\nThis may take a moment.", + "backupFailedModalTitle": "Protected backup not verified", + "backupFailedModalSubTitle": "Keeper could not confirm that the protected copy matches.", + "backupFailedModalDesc": "The connection, backup authorization or recovery data could not be checked. Keep your local recovery data and check the backup status.", + "mismatchModalTitle": "Protected backup needs updating", + "mismatchModalSubTitle": "This device’s recovery data differs from the protected copy.", + "mismatchModalDesc": "Review the backup status before updating the protected copy. Older read-only recovery copies are not changed.", + "assistedServerBackupEnabled": "Protected backup is now enabled. Automatic updates apply to this encrypted protected copy; older read-only copies may remain.", "recoveryKeyConfirmDialog": "Recovery Key confirmed", "recoveryKeyConfirmDialogSubTitle": "Your Recovery Key is confirmed. You remain fully in control of your keys.", "recoveryConfirmDesc1": "If you have already written down your Recovery Key, confirm it below.", @@ -1230,7 +1253,9 @@ "doNotStoreDigitally": "Do not store digitally", "doNotStoreDigitallyBody": "Never save your Recovery Key in screenshots, cloud storage, or any digital format.", "makeNoOneLooking": "Make sure no one is watching your screen.", - "recoveryKeyBackedUpSuccessfully": "Recovery Key backed up successfully" + "recoveryKeyBackedUpSuccessfully": "Recovery Key backed up successfully", + "backUpNow": "Back up protected copy", + "retainedOlderCopyNotice": "Older encrypted recovery copies may remain read-only. They are not updated or deleted by changes to the protected copy." }, "importWallet": { "addDetails": "Add details", @@ -1764,8 +1789,8 @@ "masterKeyTitle": "Master Recovery Key", "masterKeyDescp": "Backup 12-word phrase", "masterKeyParagraph1": "Each of the vault (multi-key wallet) has configuration details that are needed during recovery.", - "masterKeyParagraph2": "Your app’s meta data along with all the configuration files are stored on the Keeper cloud server encrypted with the Recovery Key.", - "masterKeyParagraph3": "Simply backing up or noting down the Master Recovery Key gives you access to all this data which is updated automatically.", + "masterKeyParagraph2": "When enabled, protected backup stores an encrypted copy of this app’s recovery data. Older encrypted recovery copies may remain read-only.", + "masterKeyParagraph3": "Keep your Recovery Key safe to check available backups. Automatic updates apply only while protected backup is enabled; recovery depends on the copy and records available.", "masterKeyNote": "Master Recovery Key also gives access to the app’s hot keys. Please secure the key properly.", "personalCloudTitle": "Personal Cloud Backup", "personalCloudParagph1": "Wallet configuration files for vaults can additionally be stored on users’ personal cloud. This ensures that the user has access to them even if they do not have the Keeper app.", @@ -2027,7 +2052,7 @@ "canaryVaultFailed": "Canary wallet creation failed", "importedWalletFailed": "Wallet import failed!", "vaultAlreadyExists": "Vault {0} already exists", - "unableToDeleteAssistedServer": "Unable to delete backup from Assisted Server, Please try again later.", + "unableToDeleteAssistedServer": "Could not confirm protected-backup deletion. Your local wallets and Recovery Key are unchanged. Check the backup status before trying again.", "processFailed": "Process Failed", "ErrorMappingUnknownSigner": "Error mapping unknown signer to signer", "scanValidQR": "Please scan a valid QR", @@ -2234,47 +2259,62 @@ "disclaimerCta": "Got it" }, "recoveryBackup": { - "title": "Assisted Server Backup", + "title": "Protected server backup", "backUp": "Back Up Now", "check": "Check Again", "back": "Back", "slow": "This is taking longer than usual. A slow connection or a large backup can take more time.", "leave": "You can leave this screen. Return here to check the result. If the app closes, check again when you reopen it.", "disabled": { - "title": "Backup is off", - "body": "Enable Assisted Server Backup in Backup and Recovery to use this check." - }, + "title": "Automatic backup is off", + "body": "Future automatic updates are stopped. Existing backup copies were not deleted." + }, "checking": { - "title": "Checking your backup", - "body": "Comparing your encrypted backup with the recovery data on this device." - }, + "title": "Checking protected backup", + "body": "Comparing the encrypted protected copy with recovery data on this device." + }, "different": { - "title": "Your backup needs updating", - "body": "Back Up Now replaces the server backup with this device’s current recovery data, including hidden and archived items. Items absent from this device will be removed from the backup. Check other Keeper devices first. Keeper will then read back and verify the result." - }, + "title": "Your protected backup needs updating", + "body": "Back Up Now updates only the protected copy when checks to preserve recovery data pass. Missing or different records may need review. Updates include hidden and archived items. Older read-only copies are unchanged. Check your other Keeper devices first; Keeper reads back and verifies an accepted update." + }, "preparing": { - "title": "Preparing your backup", - "body": "Preparing and encrypting the recovery data on this device." - }, + "title": "Preparing protected backup", + "body": "Preparing and encrypting the recovery data on this device." + }, "uploading": { - "title": "Updating your backup", - "body": "Sending your encrypted backup to Keeper’s servers." - }, + "title": "Updating protected backup", + "body": "Sending your encrypted protected copy to Keeper’s server." + }, "verifying": { - "title": "Verifying your backup", - "body": "Reading your backup back from the server and checking that the recovery data matches." - }, + "title": "Verifying protected backup", + "body": "Reading back the protected copy and checking that its recovery data matches." + }, "verified": { - "title": "Backup verified", - "body": "Your Assisted Server Backup matches the recovery data checked on this device. Keep your Recovery Key safe; you still need it for recovery." - }, + "title": "Protected backup verified", + "body": "This protected copy matches the recovery data checked on this device. Older read-only copies may also remain. Keep your Recovery Key safe." + }, "conflict": { - "title": "Backup needs review", - "body": "The server backup contains recovery data that is missing or different on this device. Keeper has not replaced it. Check your other Keeper devices before making changes, then check again." - }, + "title": "Protected backup needs review", + "body": "The protected copy contains recovery data that is missing or different on this device. Keeper has not replaced it. Check your other Keeper devices, then check again." + }, "unverified": { - "title": "Backup not verified", - "body": "Keeper could not confirm that your backup matches. Your connection may have been interrupted, the backup may be too large, or data may have changed during the check. Check again before assuming the backup is complete." - } + "title": "Protected backup not verified", + "body": "Keeper could not confirm that the protected copy matches. Authorization, connection, size or changed data may prevent a complete check. Check again before assuming it is complete." + }, + "retainedCopyNotice": "Older encrypted recovery copies may remain read-only. This status applies only to the protected copy.", + "deleted": { + "title": "Protected backup was deleted", + "body": "The protected copy has a confirmed deletion record. Older read-only recovery copies may remain and are never restored automatically. Local wallets and your Recovery Key are kept." + }, + "deletionRecordedTitle": "Protected backup deletion recorded", + "deletionRecordedBody": "Keeper previously confirmed deletion of this protected copy. Check Again to confirm current status. Older read-only copies may remain.", + "never-enrolled": { + "title": "No protected backup yet", + "body": "This account has not enrolled a protected copy. You can enable protected backup from Backup and Recovery. Older read-only copies may remain." + }, + "notificationTitle": "Check your protected backup", + "notificationBody": "Your protected backup may differ from this device. Older read-only copies may remain. View details to check it.", + "retryNotificationTitle": "Protected backup update not confirmed", + "retryNotificationBody": "Keeper could not confirm the protected backup update. Your local recovery data is kept. View details before trying again; older read-only copies may remain." } } diff --git a/src/context/Localization/language/es.json b/src/context/Localization/language/es.json index 464f4f0fda..a7fbfa407e 100644 --- a/src/context/Localization/language/es.json +++ b/src/context/Localization/language/es.json @@ -194,7 +194,7 @@ "MismatchPasscode": "Passcodes do not match", "enter_your": "Enter your ", "NewSatrtWalet": "A new hot wallet will be created automatically for you. You can create and manage different kinds of wallets from the wallets section of the app.", - "RecoverExistingAppDesc": "Have an inaccessible Keeper app with wallets in it? You have nothing to worry if you have that app's 12-word Recovery Key. Insert the words in the Recovery section on the welcome screen and all your wallets will be restored as expected.", + "RecoverExistingAppDesc": "Usa la clave de recuperación de esta app Keeper para comprobar su copia protegida disponible. Recuperar una copia anterior de solo lectura es una opción separada y puede no incluir los cambios más recientes.", "RecoverApp": "Recover an existing app:", "Wait": "This step will take a few seconds. You will be able to proceed soon", "Createpasscode": "Create a passcode", @@ -788,17 +788,17 @@ "backupPhrase": "Recovery Key", "walletSeedWords": "Recovery Key", "enterRecoveryPhrase": "Recover an existing Keeper App", - "enterRecoveryPhraseSubTitle": "Enter existing 12-word recovery phrase below", + "enterRecoveryPhraseSubTitle": "Introduce la clave de recuperación de 12 palabras de esta app Keeper. Primero se comprueba la copia protegida.", "walletRecoveryPhrase": "Wallet Recovery Phrase", "showXPubNoteSubText": "Losing your Wallet Seed Words may result in permanent loss of funds. Store them carefully.", "mobileKeyVerified": "Mobile Key verified successfully", "seedWordVerified": "Seed Words verified successfully", "keeperVerified": "Keeper Verified Successfully", "SeedErrorToast": "Please fill in all seed word fields", - "appRecoveredSuccessfulTitle": "App Recovered Successfully!", - "recoveryIncomplete": "No se pudo completar la recuperación. Es posible que la copia de seguridad no esté disponible o que algunos datos no se hayan podido restaurar. Inténtalo de nuevo.", - "appRecoveredSuccessfulSubTitle": "All your wallets and data about your vault has been recovered", - "appRecoveredSuccessfulDesc": "The vault keys always stay within the signing devices and therefore do not have to be recovered", + "appRecoveredSuccessfulTitle": "Copia recuperada", + "recoveryIncomplete": "La recuperación no se completó. No se pudieron comprobar o restaurar todos los datos disponibles. No se seleccionó ninguna copia anterior automáticamente.", + "appRecoveredSuccessfulSubTitle": "Se comprobaron y restauraron los datos de recuperación disponibles de la copia seleccionada.", + "appRecoveredSuccessfulDesc": "Las claves de firma de hardware permanecen en sus dispositivos. Revisa los registros recuperados antes de activar la copia protegida.", "mobileKey": "Mobile Key", "mobileKeySeedWordsTitle": "Mobile Key Seed Words", "mobileKeySeedNoteSubTitle": "The QR below comprises of your 12-word Mobile Key", @@ -806,7 +806,24 @@ "storeSecurely": "Write down these words and store them securely.", "enterAmountToSend": "Enter Amount to Send", "feePriority": "Fee Priority", - "seedKey": "Seed Key" + "seedKey": "Seed Key", + "protectedRecoveryAbsentTitle": "No hay copia protegida", + "protectedRecoveryAbsentBody": "Esta clave de recuperación no tiene una copia protegida. Puedes elegir buscar una copia anterior de solo lectura.", + "protectedRecoveryDeletedTitle": "Tu copia protegida fue eliminada", + "protectedRecoveryDeletedBody": "Keeper no restaurará una copia anterior automáticamente. Una copia anterior de solo lectura puede ser previa a la eliminación y omitir cambios recientes.", + "protectedRecoveryErrorTitle": "No se pudo comprobar la copia protegida", + "protectedRecoveryErrorBody": "No se pudo comprobar la copia protegida. No se seleccionó ninguna copia anterior. Comprueba tu conexión e inténtalo de nuevo.", + "recoverOlderCopy": "Recuperar una copia anterior", + "recoverOlderCopyTitle": "¿Recuperar una copia anterior?", + "recoverOlderCopyBody": "Una copia anterior de solo lectura puede ser previa a la eliminación u omitir cambios recientes. Solo se recuperan los registros completos disponibles y no se activan las copias automáticas.", + "protectedRecoverySuccessfulTitle": "Copia protegida recuperada", + "protectedRecoverySuccessfulSubTitle": "Se comprobaron y restauraron los datos de recuperación disponibles de la copia protegida seleccionada.", + "olderRecoverySuccessfulTitle": "Copia anterior recuperada", + "olderRecoverySuccessfulSubTitle": "Se recuperó una copia anterior. Pueden faltar cambios recientes. Las copias automáticas siguen desactivadas.", + "protectedRecoveryEmptyTitle": "La copia protegida está vacía", + "protectedRecoveryEmptyBody": "Esta copia protegida no tiene registros de recuperación. Puedes elegir buscar una copia anterior de solo lectura.", + "olderRecoveryErrorTitle": "Recuperación anterior incompleta", + "olderRecoveryErrorBody": "No se pudo comprobar o restaurar toda la copia anterior. Guarda tu clave de recuperación y revisa la conexión y los registros disponibles antes de intentarlo de nuevo." }, "healthcheck": { "HealthCheck": "Perform Health Check", @@ -1084,11 +1101,11 @@ "serverSelectionSubtitle": "Select a server you'd like to use. Add your own node for enhanced privacy.", "publicServerNote": "It’s recommended to use your own Electrum server to protect your privacy", "connectToServer": "Connect to Server", - "assistedServerBackup": "Assisted Server Backup", - "assistedServerBackupSubtitle": "Back up encrypted data using a community-run Keeper server.", - "assistedServerDeleteBackupSubtitle": "Do you also want to delete the existing assisted server backup?", - "assistedServerDeleteBackupModalCTA": "Delete", - "assistedServerDeleteBackupModalSecondaryCTA": "Only disable", + "assistedServerBackup": "Copia protegida en el servidor", + "assistedServerBackupSubtitle": "Guarda una nueva copia cifrada y protegida. Pueden quedar copias anteriores de solo lectura.", + "assistedServerDeleteBackupSubtitle": "Esto elimina la copia protegida y desactiva las copias automáticas. Pueden quedar copias cifradas anteriores de solo lectura. Se conservan las carteras y la clave de recuperación de este dispositivo. Desactivar solo las copias automáticas conserva todas las copias.", + "assistedServerDeleteBackupModalCTA": "Eliminar la copia protegida", + "assistedServerDeleteBackupModalSecondaryCTA": "Desactivar copias automáticas", "cameraAccessoff": "Camera access is turned off", "turnCameraOn": "Turn on the camera in your device settings", "tapToGoSetting": "Tap to go to settings", @@ -1134,7 +1151,13 @@ "changeWallet": "Change Wallet", "customAmount": "Custom amount", "sendNoteToDevelopers": "Send a note to the developers", - "messageToDeveloper": "Message to developer" + "messageToDeveloper": "Message to developer", + "assistedServerDeleteBackupTitle": "¿Eliminar la copia protegida?", + "assistedServerDeleteBackupConfirmed": "Copia protegida eliminada. Pueden quedar copias cifradas anteriores. Tus carteras locales y tu clave de recuperación no han cambiado.", + "assistedServerBackupDisabled": "Las copias automáticas están desactivadas. Las copias existentes se conservaron.", + "assistedServerEnableBackupTitle": "¿Activar la copia protegida?", + "assistedServerEnableBackupSubtitle": "Crea o actualiza la copia protegida con los datos de recuperación de este dispositivo. Las copias anteriores de solo lectura no cambian. Guarda tu clave de recuperación.", + "assistedServerEnableBackupCTA": "Activar la copia protegida" }, "onboarding": { "security": "security", @@ -1199,19 +1222,21 @@ "enterThe": "Enter the", "seedWord": "seed word", "enterSeedWordPlaceholder": "Enter seed word", - "verifyModalTitle": "Verifying Server Backup", - "verifyModalSubTitle": "Verifying your server backup integrity against your local data.\nThis may take a moment.", - "backupFailedModalTitle": "Backup Verification Failed", - "backupFailedModalSubTitle": "We couldn’t verify your data with the server backup.", - "backupFailedModalDesc": "There might be a temporary issue with the server or your connection. Please try again later.", - "mismatchModalTitle": "Data Mismatch Detected", - "mismatchModalSubTitle": "Your local data doesn’t match the server backup.", - "mismatchModalDesc": "Some recent changes may not be backed up yet. To avoid data loss, we recommend initiating a backup now.", - "assistedServerBackupEnabled": "Assisted server backup is now enabled.\nThis will backup your encrypted data on a community-run Keeper server", + "verifyModalTitle": "Verificando la copia protegida", + "verifyModalSubTitle": "Comparando la copia protegida con los datos de recuperación de este dispositivo.\nPuede tardar un momento.", + "backupFailedModalTitle": "Copia protegida sin verificar", + "backupFailedModalSubTitle": "Keeper no pudo confirmar que la copia protegida coincida.", + "backupFailedModalDesc": "No se pudo comprobar la conexión, la autorización o los datos de recuperación. Conserva tus datos locales y comprueba el estado de la copia.", + "mismatchModalTitle": "Actualiza la copia protegida", + "mismatchModalSubTitle": "Los datos de recuperación del dispositivo difieren de la copia protegida.", + "mismatchModalDesc": "Revisa el estado antes de actualizar la copia protegida. Las copias anteriores de solo lectura no cambian.", + "assistedServerBackupEnabled": "La copia protegida está activada. Las actualizaciones automáticas se aplican a esta copia cifrada; pueden quedar copias anteriores de solo lectura.", "recoveryKeyConfirmDialog": "Recovery Key confirmed", "recoveryKeyConfirmDialogSubTitle": "Your Recovery Key is confirmed. You remain fully in control of your keys.", "recoveryConfirmDesc1": "If you have already written down your Recovery Key, confirm it below.", - "recoveryConfirmDesc2": "If not, write these 12 words down and store them safely. Do not store them digitally." + "recoveryConfirmDesc2": "If not, write these 12 words down and store them safely. Do not store them digitally.", + "backUpNow": "Guardar la copia protegida", + "retainedOlderCopyNotice": "Pueden quedar copias cifradas anteriores de solo lectura. Los cambios de la copia protegida no las actualizan ni eliminan." }, "importWallet": { "addDetails": "Add details", @@ -1745,8 +1770,8 @@ "masterKeyTitle": "Master Recovery Key", "masterKeyDescp": "Backup 12-word phrase", "masterKeyParagraph1": "Each of the vault (multi-key wallet) has configuration details that are needed during recovery.", - "masterKeyParagraph2": "Your app’s meta data along with all the configuration files are stored on the Keeper cloud server encrypted with the Recovery Key.", - "masterKeyParagraph3": "Simply backing up or noting down the Master Recovery Key gives you access to all this data which is updated automatically.", + "masterKeyParagraph2": "Cuando está activada, la copia protegida guarda una copia cifrada de los datos de recuperación de esta app. Pueden quedar copias cifradas anteriores de solo lectura.", + "masterKeyParagraph3": "Guarda tu clave de recuperación para comprobar las copias disponibles. Las actualizaciones automáticas solo funcionan mientras la copia protegida está activada; la recuperación depende de la copia y de los registros disponibles.", "masterKeyNote": "Master Recovery Key also gives access to the app’s hot keys. Please secure the key properly.", "personalCloudTitle": "Personal Cloud Backup", "personalCloudParagph1": "Wallet configuration files for vaults can additionally be stored on users’ personal cloud. This ensures that the user has access to them even if they do not have the Keeper app.", @@ -2008,7 +2033,7 @@ "canaryVaultFailed": "Canary wallet creation failed", "importedWalletFailed": "Wallet import failed!", "vaultAlreadyExists": "Vault {0} already exists", - "unableToDeleteAssistedServer": "Unable to delete backup from Assisted Server, Please try again later.", + "unableToDeleteAssistedServer": "No se pudo confirmar la eliminación de la copia protegida. Tus carteras locales y tu clave de recuperación no cambiaron. Comprueba el estado antes de intentarlo de nuevo.", "processFailed": "Process Failed", "ErrorMappingUnknownSigner": "Error mapping unknown signer to signer", "scanValidQR": "Please scan a valid QR", @@ -2215,47 +2240,62 @@ "disclaimerCta": "Got it" }, "recoveryBackup": { - "title": "Copia de seguridad asistida", + "title": "Copia protegida en el servidor", "backUp": "Guardar ahora", "check": "Volver a comprobar", "back": "Atrás", "slow": "Está tardando más de lo habitual. Una conexión lenta o una copia grande puede necesitar más tiempo.", "leave": "Puedes salir de esta pantalla y volver para consultar el resultado. Si la aplicación se cierra, comprueba de nuevo al abrirla.", "disabled": { - "title": "Copia desactivada", - "body": "Activa la copia de seguridad asistida en Copia de seguridad y recuperación para usar esta comprobación." - }, + "title": "Copias automáticas desactivadas", + "body": "Las actualizaciones automáticas se detuvieron. Las copias existentes no se eliminaron." + }, "checking": { - "title": "Comprobando la copia", - "body": "Comparando tu copia cifrada con los datos de recuperación de este dispositivo." - }, + "title": "Comprobando la copia protegida", + "body": "Comparando la copia protegida cifrada con los datos de recuperación de este dispositivo." + }, "different": { - "title": "Actualiza tu copia", - "body": "Guardar ahora reemplaza la copia del servidor con los datos de recuperación actuales de este dispositivo, incluidos los elementos ocultos y archivados. Los elementos que no estén en este dispositivo se eliminarán de la copia. Revisa primero tus otros dispositivos Keeper. Después, Keeper volverá a leer y verificar la copia." - }, + "title": "Actualiza tu copia protegida", + "body": "Guardar ahora actualiza solo la copia protegida cuando se superan las comprobaciones para preservar los datos de recuperación. Los registros que faltan o son diferentes pueden requerir revisión. Se incluyen los elementos ocultos y archivados. Las copias anteriores de solo lectura no cambian. Revisa primero tus otros dispositivos Keeper; Keeper vuelve a leer y verificar una actualización aceptada." + }, "preparing": { - "title": "Preparando la copia", - "body": "Preparando y cifrando los datos de recuperación de este dispositivo." - }, + "title": "Preparando la copia protegida", + "body": "Preparando y cifrando los datos de recuperación de este dispositivo." + }, "uploading": { - "title": "Actualizando la copia", - "body": "Enviando tu copia cifrada a los servidores de Keeper." - }, + "title": "Actualizando la copia protegida", + "body": "Enviando la copia protegida cifrada al servidor de Keeper." + }, "verifying": { - "title": "Verificando la copia", - "body": "Leyendo la copia del servidor y comprobando que los datos de recuperación coinciden." - }, + "title": "Verificando la copia protegida", + "body": "Leyendo la copia protegida y comprobando que sus datos de recuperación coinciden." + }, "verified": { - "title": "Copia verificada", - "body": "Tu copia de seguridad asistida coincide con los datos de recuperación comprobados en este dispositivo. Guarda tu clave de recuperación; sigue siendo necesaria para recuperar." - }, + "title": "Copia protegida verificada", + "body": "Esta copia protegida coincide con los datos de recuperación comprobados en el dispositivo. Pueden quedar copias anteriores de solo lectura. Guarda tu clave de recuperación." + }, "conflict": { - "title": "Revisa la copia", - "body": "El servidor contiene datos de recuperación que faltan o son diferentes en este dispositivo. Keeper no los ha reemplazado. Revisa tus otros dispositivos Keeper antes de hacer cambios y vuelve a comprobar." - }, + "title": "Revisa la copia protegida", + "body": "La copia protegida contiene datos de recuperación que faltan o son diferentes en este dispositivo. Keeper no los ha reemplazado. Revisa tus otros dispositivos Keeper y vuelve a comprobar." + }, "unverified": { - "title": "Copia sin verificar", - "body": "Keeper no pudo confirmar que tu copia coincida. La conexión puede haberse interrumpido, la copia puede ser demasiado grande o los datos pueden haber cambiado. Comprueba de nuevo antes de dar la copia por completa." - } + "title": "Copia protegida sin verificar", + "body": "Keeper no pudo confirmar que la copia protegida coincida. La autorización, la conexión, el tamaño o los cambios de datos pueden impedir una comprobación completa. Comprueba de nuevo antes de darla por completa." + }, + "retainedCopyNotice": "Pueden quedar copias cifradas anteriores de solo lectura. Este estado solo se refiere a la copia protegida.", + "deleted": { + "title": "La copia protegida fue eliminada", + "body": "La copia protegida tiene un registro de eliminación confirmado. Pueden quedar copias anteriores de solo lectura y nunca se restauran automáticamente. Se conservan tus carteras locales y tu clave de recuperación." + }, + "deletionRecordedTitle": "Eliminación de copia protegida registrada", + "deletionRecordedBody": "Keeper confirmó anteriormente la eliminación de esta copia protegida. Vuelve a comprobar el estado actual. Pueden quedar copias anteriores de solo lectura.", + "never-enrolled": { + "title": "Aún no hay copia protegida", + "body": "Esta cuenta no ha creado una copia protegida. Puedes activarla en Copia de seguridad y recuperación. Pueden quedar copias anteriores de solo lectura." + }, + "notificationTitle": "Comprueba tu copia protegida", + "notificationBody": "Tu copia protegida puede ser diferente de los datos de este dispositivo. Pueden quedar copias anteriores de solo lectura. Consulta los detalles para comprobarla.", + "retryNotificationTitle": "Actualización de copia protegida sin confirmar", + "retryNotificationBody": "Keeper no pudo confirmar la actualización de la copia protegida. Se conservan tus datos locales de recuperación. Consulta los detalles antes de volver a intentarlo; pueden quedar copias anteriores de solo lectura." } } diff --git a/src/hooks/useSettingKeeper.tsx b/src/hooks/useSettingKeeper.tsx index 0f6a08f3de..f9daa67ff6 100644 --- a/src/hooks/useSettingKeeper.tsx +++ b/src/hooks/useSettingKeeper.tsx @@ -76,6 +76,7 @@ export const useSettingKeeper = () => { const data = useQuery(RealmSchema.BackupHistory); const [confirmPass, setConfirmPass] = useState(false); const [showDeleteBackup, setShowDeleteBackup] = useState(false); + const [showEnableBackup, setShowEnableBackup] = useState(false); const themeToggleTimeoutRef = useRef | null>(null); const backupToggleTimeoutRef = useRef | null>(null); const { translations } = useContext(LocalizationContext); @@ -160,8 +161,9 @@ export const useSettingKeeper = () => { if (deleteBackupSuccess && isFocused) { dispatch(setDeleteBackupSuccess({ appId: id, status: false })); dispatch(setAutomaticCloudBackup({ appId: id, enabled: false })); + showToast(settings.assistedServerDeleteBackupConfirmed); } - }, [deleteBackupSuccess]); + }, [deleteBackupSuccess, id, isFocused]); useEffect(() => { if (deleteBackupFailure && isFocused) { @@ -177,7 +179,7 @@ export const useSettingKeeper = () => { }, [backupAllFailure]); const toggleAutomaticBackupMode = useCallback(() => { - if (!automaticCloudBackup) dispatch(backupAllSignersAndVaults(id)); + if (!automaticCloudBackup) setShowEnableBackup(true); else setShowDeleteBackup(true); }, [automaticCloudBackup, dispatch, id]); @@ -462,31 +464,52 @@ export const useSettingKeeper = () => { ]; const DeleteBackupModal = ( - setShowDeleteBackup(false)} - title={settings.assistedServerBackup} - subTitleWidth={wp(240)} - subTitle={settings.assistedServerDeleteBackupSubtitle} - modalBackground={`${colorMode}.modalWhiteBackground`} - textColor={`${colorMode}.textGreen`} - subTitleColor={`${colorMode}.modalSubtitleBlack`} - Content={() => ( - { - setShowDeleteBackup(false); - dispatch(deleteBackup(id)); - }} - secondaryCallback={() => { - setShowDeleteBackup(false); - dispatch(setAutomaticCloudBackup({ appId: id, enabled: false })); - }} - secondaryText={settings.assistedServerDeleteBackupModalSecondaryCTA} - /> - )} - /> + <> + setShowDeleteBackup(false)} + title={settings.assistedServerDeleteBackupTitle} + subTitleWidth={wp(240)} + subTitle={settings.assistedServerDeleteBackupSubtitle} + modalBackground={`${colorMode}.modalWhiteBackground`} + textColor={`${colorMode}.textGreen`} + subTitleColor={`${colorMode}.modalSubtitleBlack`} + Content={() => ( + { + setShowDeleteBackup(false); + dispatch(deleteBackup(id)); + }} + secondaryCallback={() => { + setShowDeleteBackup(false); + dispatch(setAutomaticCloudBackup({ appId: id, enabled: false })); + showToast(settings.assistedServerBackupDisabled); + }} + secondaryText={settings.assistedServerDeleteBackupModalSecondaryCTA} + /> + )} + /> + setShowEnableBackup(false)} + title={settings.assistedServerEnableBackupTitle} + subTitle={settings.assistedServerEnableBackupSubtitle} + subTitleWidth={wp(240)} + modalBackground={`${colorMode}.modalWhiteBackground`} + textColor={`${colorMode}.textGreen`} + subTitleColor={`${colorMode}.modalSubtitleBlack`} + buttonText={settings.assistedServerEnableBackupCTA} + buttonCallback={() => { + setShowEnableBackup(false); + dispatch(backupAllSignersAndVaults(id)); + }} + secondaryButtonText={common.cancel} + secondaryCallback={() => setShowEnableBackup(false)} + /> + ); return { diff --git a/src/screens/AppSettings/AssistedBackupStatus.tsx b/src/screens/AppSettings/AssistedBackupStatus.tsx index f91cb70c04..bb225eb2e4 100644 --- a/src/screens/AppSettings/AssistedBackupStatus.tsx +++ b/src/screens/AppSettings/AssistedBackupStatus.tsx @@ -26,6 +26,7 @@ export default function AssistedBackupStatus() { automaticCloudBackupByAppId, backupRepairStateByAppId = {}, backupRepairRunningByAppId = {}, + protectedBackupStateByAppId = {}, } = useAppSelector((state) => state.bhr); const automaticCloudBackup = isAutomaticCloudBackupEnabled({ automaticCloudBackupByAppId }, id); const recordedPhase = backupRepairStateByAppId[id]; @@ -33,8 +34,9 @@ export default function AssistedBackupStatus() { recordedPhase === 'verified' && !isBackupVerificationCurrent(id) ? 'unverified' : recordedPhase; const running = !!backupRepairRunningByAppId[id]; const [slow, setSlow] = useState(false); + const deletionRecorded = protectedBackupStateByAppId[id]?.status === 'deleted'; useEffect(() => { - if (automaticCloudBackup) dispatch(checkBackupFreshness(id)); + if (id) dispatch(checkBackupFreshness(id)); }, [id]); useEffect(() => { setSlow(false); @@ -42,9 +44,15 @@ export default function AssistedBackupStatus() { const timer = setTimeout(() => setSlow(true), 10_000); return () => clearTimeout(timer); }, [running, phase]); - const state = !automaticCloudBackup + const state = running + ? phase || 'checking' + : phase === 'deleted' || phase === 'never-enrolled' + ? phase + : phase === 'unverified' + ? 'unverified' + : !automaticCloudBackup ? 'disabled' - : !running && ['checking', 'preparing', 'uploading', 'verifying'].includes(phase) + : ['checking', 'preparing', 'uploading', 'verifying'].includes(phase) ? 'unverified' : phase || 'unverified'; return ( @@ -70,12 +78,29 @@ export default function AssistedBackupStatus() { )} - {!running && automaticCloudBackup && ( + {deletionRecorded && state !== 'deleted' && ( + + + {copy.deletionRecordedTitle} + + + {copy.deletionRecordedBody} + + + )} + + {copy.retainedCopyNotice} + + {!running && ( - dispatch(state === 'different' ? repairBackup(id) : checkBackupFreshness(id)) + dispatch( + automaticCloudBackup && state === 'different' + ? repairBackup(id) + : checkBackupFreshness(id) + ) } /> )} diff --git a/src/screens/Recovery/EnterSeedScreen.tsx b/src/screens/Recovery/EnterSeedScreen.tsx index b133caf23a..3e8bbeb55e 100644 --- a/src/screens/Recovery/EnterSeedScreen.tsx +++ b/src/screens/Recovery/EnterSeedScreen.tsx @@ -34,7 +34,13 @@ import Dropdown from 'src/components/Dropdown'; import { SIGNTRANSACTION } from 'src/navigation/contants'; import { hcStatusType } from 'src/models/interfaces/HeathCheckTypes'; import RecoverySuccessModalContent from './RecoverySuccessModalContent'; -import { resetSeedWords, setAppImageError, setSeedWord } from 'src/store/reducers/bhr'; +import { + resetRecoveryDiscovery, + resetSeedWords, + setAppImageError, + setSeedWord, +} from 'src/store/reducers/bhr'; +import type { BackupRecoverySource } from 'src/services/backup/restore'; import Fonts from 'src/constants/Fonts'; import { SEED_WORDS_12, SEED_WORDS_18, SEED_WORDS_24, seedWordItem } from './constants'; import Colors from 'src/theme/Colors'; @@ -73,9 +79,8 @@ function EnterSeedScreen({ route, navigation }) { selectedNumberOfWordsFromParams, isForgot = false, } = route.params || {}; - const { appImageError } = useAppSelector((state) => state.bhr); - - const { appCreated } = useAppSelector((state) => state.storage); + const { appImageError, recoveryDiscovery } = useAppSelector((state) => state.bhr); + const activeAppId = useAppSelector((state) => state.storage.appId); const { colorMode } = useColorMode(); const { showToast } = useToastMessage(); const dispatch = useDispatch(); @@ -84,6 +89,10 @@ function EnterSeedScreen({ route, navigation }) { const [seedData, setSeedData] = useState(); const [invalidSeedsModal, setInvalidSeedsModal] = useState(false); const [recoverySuccessModal, setRecoverySuccessModal] = useState(false); + const [recoverySuccessSource, setRecoverySuccessSource] = + useState('protected'); + const [showRecoveryDiscovery, setShowRecoveryDiscovery] = useState(false); + const pendingRecoverySource = useRef('protected'); const [recoveryLoading, setRecoveryLoading] = useState(false); const [hcLoading, setHcLoading] = useState(false); const [suggestedWords, setSuggestedWords] = useState([]); @@ -107,6 +116,7 @@ function EnterSeedScreen({ route, navigation }) { const isHealthCheck = mode === InteracationMode.HEALTH_CHECK; const isSignTransaction = parentScreen === SIGNTRANSACTION; const isIdentification = mode === InteracationMode.IDENTIFICATION; + const isRecovery = !isHealthCheck && !isImport && !isSignTransaction && !isIdentification; const green_modal_text_color = ThemedColor({ name: 'green_modal_text_color' }); const green_modal_background = ThemedColor({ name: 'green_modal_background' }); @@ -123,17 +133,45 @@ function EnterSeedScreen({ route, navigation }) { }; useEffect(() => { - if (appCreated && recoveryLoading) { + if (!isRecovery) return undefined; + dispatch(resetRecoveryDiscovery()); + return () => { + dispatch(resetRecoveryDiscovery()); + }; + }, [dispatch, isRecovery]); + + useEffect(() => { + if (!isRecovery || !recoveryLoading) return; + if (recoveryDiscovery.status === 'restored') { + const { provenance } = recoveryDiscovery; setRecoveryLoading(false); + if ( + !provenance || + provenance.source !== pendingRecoverySource.current || + provenance.keeperAppId !== activeAppId + ) { + showToast(seed.recoveryIncomplete, ); + return; + } + setRecoverySuccessSource(provenance.source); setRecoverySuccessModal(true); dispatch(resetSeedWords()); dispatch(resetPasscodeTimeout()); + } else if ( + ['protected-absent', 'protected-empty', 'protected-deleted', 'read-error'].includes( + recoveryDiscovery.status + ) + ) { + setRecoveryLoading(false); + if (recoveryDiscovery.error !== 'Invalid mnemonic') setShowRecoveryDiscovery(true); } - }, [appCreated]); + }, [recoveryDiscovery, recoveryLoading, isRecovery, activeAppId]); useEffect(() => { - if (appImageError) { - if (appImageError === 'Network Error') { + if (appImageError && isRecovery) { + if (recoveryDiscovery.status === 'read-error' && appImageError !== 'Invalid mnemonic') { + dispatch(setAppImageError('')); + } else if (appImageError === 'Network Error') { dispatch(setAppImageError('')); setRecoveryLoading(false); setShowNetworkModal(true); @@ -145,7 +183,7 @@ function EnterSeedScreen({ route, navigation }) { dispatch(setAppImageError('')); } } - }, [appImageError]); + }, [appImageError, isRecovery, recoveryDiscovery.status]); const generateSeedWordsArray = useCallback(() => { const seedArray = []; @@ -328,10 +366,12 @@ function EnterSeedScreen({ route, navigation }) { }; const handleGetAppImage = async () => { + dispatch(resetRecoveryDiscovery()); + pendingRecoverySource.current = 'protected'; setRecoveryLoading(true); try { const seedWord = seedWords.map((word) => word.name).join(' '); - dispatch(getAppImage(seedWord, isForgot)); + dispatch(getAppImage(seedWord, isForgot, { source: 'protected' })); } catch (err) { console.error('getAppImage error:', err); showToast(seed.SeedErrorToast, ); @@ -601,7 +641,6 @@ function EnterSeedScreen({ route, navigation }) { ); }; - const isRecovery = !isHealthCheck && !isImport && !isSignTransaction && !isIdentification; const rememberModalContent = () => { return ( @@ -640,6 +679,47 @@ function EnterSeedScreen({ route, navigation }) { ); } + const olderCopyAvailable = ['protected-absent', 'protected-empty', 'protected-deleted'].includes( + recoveryDiscovery.status + ); + const recoveryDiscoveryTitle = + recoveryDiscovery.status === 'protected-deleted' + ? seed.protectedRecoveryDeletedTitle + : recoveryDiscovery.status === 'protected-empty' + ? seed.protectedRecoveryEmptyTitle + : recoveryDiscovery.status === 'protected-absent' + ? seed.protectedRecoveryAbsentTitle + : recoveryDiscovery.source === 'legacy-older' + ? seed.olderRecoveryErrorTitle + : seed.protectedRecoveryErrorTitle; + const recoveryDiscoveryBody = + recoveryDiscovery.status === 'protected-deleted' + ? seed.protectedRecoveryDeletedBody + : recoveryDiscovery.status === 'protected-empty' + ? seed.protectedRecoveryEmptyBody + : recoveryDiscovery.status === 'protected-absent' + ? seed.protectedRecoveryAbsentBody + : recoveryDiscovery.source === 'legacy-older' + ? seed.olderRecoveryErrorBody + : seed.protectedRecoveryErrorBody; + const closeRecoveryDiscovery = () => { + setShowRecoveryDiscovery(false); + dispatch(resetRecoveryDiscovery()); + }; + const recoverOlderCopy = () => { + setShowRecoveryDiscovery(false); + dispatch(resetRecoveryDiscovery()); + pendingRecoverySource.current = 'legacy-older'; + setRecoveryLoading(true); + const mnemonic = seedWords.map((word) => word.name).join(' '); + dispatch( + getAppImage(mnemonic, isForgot, { + source: 'legacy-older', + olderCopyConfirmed: true, + }) + ); + }; + return ( + setShowNetworkModal(false)} visible={showNetworkModal} - title="Something went wrong" - subTitle="Please check your internet connection and try again." + title={seed.protectedRecoveryErrorTitle} + subTitle={seed.protectedRecoveryErrorBody} modalBackground={`${colorMode}.modalWhiteBackground`} textColor={`${colorMode}.textGreen`} subTitleColor={`${colorMode}.modalSubtitleBlack`} subTitleWidth={wp(230)} showCloseIcon={false} - buttonText={'Try again'} + buttonText={common.ok} buttonCallback={() => setShowNetworkModal(false)} /> ; +export type BackupAuthority = Readonly<{ + namespaceId: string; + publicJwk: PublicBackupJwk; + kid: string; + signRawBody: ( + rawBytes: Uint8Array, + path: ProtectedBackupPath, + audience: string, + nowSeconds?: number + ) => string; + dispose: () => void; +}>; +export type ExpectedBackupAccount = Readonly<{ id: string; primarySeed: string }>; + +const PATHS = new Set(['/backup/v3/enroll', '/backup/v3/snapshot', '/backup/v3/mutate']); +const OPERATIONS = new Set([ + 'updateAppImage', + 'migrateXfps', + 'deleteAppImageEntity', + 'deleteVaults', + 'updateVaultImage', + 'modifyLabels', + 'deleteBackup', + 'repairAppBackup', +]); +const MAX_BODY_BYTES = 32 * 1024 * 1024; +const unavailable = (): never => { + throw new Error('PROTECTED_BACKUP_AUTHORITY_UNAVAILABLE'); +}; +const unsigned = (value: unknown): value is number => + typeof value === 'number' && Number.isSafeInteger(value) && value >= 0 && !Object.is(value, -0); +const object = (value: unknown): value is Record => + value !== null && typeof value === 'object' && !Array.isArray(value); +const exactKeys = (value: unknown, keys: string[]) => { + if ( + !object(value) || + Object.keys(value).length !== keys.length || + keys.some((key) => !Object.prototype.hasOwnProperty.call(value, key)) + ) + unavailable(); +}; +const base64url = (bytes: Uint8Array): string => + Buffer.from(bytes).toString('base64').replace(/[=]/g, '').replace(/\+/g, '-').replace(/\//g, '_'); + +// Prevent signing a different namespace/key or operation envelope by mistake. +// Transport constructs this JSON; Relay independently applies its strict parser. +function assertBodyTarget( + raw: Uint8Array, + path: ProtectedBackupPath, + namespaceId: string, + jwk: PublicBackupJwk +) { + const text = Buffer.from(raw).toString('utf8'); + if (!Buffer.from(text, 'utf8').equals(Buffer.from(raw))) unavailable(); + const body = JSON.parse(text); + exactKeys( + body, + path === '/backup/v3/mutate' + ? [ + 'namespaceId', + 'publicJwk', + 'operation', + 'expectedRevision', + 'expectedGeneration', + 'authEpoch', + 'data', + ] + : ['namespaceId', 'publicJwk'] + ); + exactKeys(body.publicJwk, ['kty', 'crv', 'x', 'y']); + if ( + body.namespaceId !== namespaceId || + Object.keys(jwk).some((key) => body.publicJwk[key] !== jwk[key]) + ) + unavailable(); + if ( + path === '/backup/v3/mutate' && + (!OPERATIONS.has(body.operation) || + typeof body.expectedRevision !== 'string' || + !/^[a-f0-9]{64}$/.test(body.expectedRevision) || + !unsigned(body.expectedGeneration) || + !unsigned(body.authEpoch) || + !object(body.data)) + ) + unavailable(); +} + +/** Derive only from the original Keeper account Recovery Key, without a passphrase. */ +export function deriveBackupAuthority( + primaryMnemonic: string, + expectedAccount?: ExpectedBackupAccount +): BackupAuthority { + let seed: Uint8Array | undefined; + let signingSeed: Uint8Array | undefined; + let master: HDKey | undefined; + let child: HDKey | undefined; + let delivered = false; + try { + if ( + typeof primaryMnemonic !== 'string' || + primaryMnemonic.length > 1024 || + !validateMnemonic(primaryMnemonic, wordlist) + ) + unavailable(); + seed = mnemonicToSeedSync(primaryMnemonic, ''); + if (expectedAccount !== undefined) { + if ( + !object(expectedAccount) || + typeof expectedAccount.id !== 'string' || + !/^[a-f0-9]{64}$/.test(expectedAccount.id) || + typeof expectedAccount.primarySeed !== 'string' || + // Existing image encryption hashes the stored hex text. Clean restore + // derives lowercase hex, so accepting uppercase here could authorize + // ciphertext that its recovery mnemonic cannot decrypt. + !/^[a-f0-9]{128}$/.test(expectedAccount.primarySeed) + ) + unavailable(); + const expectedSeed = hexToBytes(expectedAccount.primarySeed); + try { + if (!equalBytes(seed, expectedSeed) || bytesToHex(sha256(seed)) !== expectedAccount.id) + unavailable(); + } finally { + expectedSeed.fill(0); + } + } + signingSeed = hkdf( + sha256, + seed, + utf8ToBytes('BitcoinKeeper/BackupAuthority/v1'), + utf8ToBytes('secp256k1-signing-seed'), + 32 + ); + // Invalid masters throw. The pinned BIP32 implementation handles hardened + // invalid-child skipping/overflow; no alternate seed or profile is chosen. + master = HDKey.fromMasterSeed(signingSeed); + child = master.deriveChild(HARDENED_OFFSET); + const compressed = child.publicKey; + if (!compressed) unavailable(); + const point = secp256k1.Point.fromBytes(compressed).toBytes(false); + const publicJwk: PublicBackupJwk = Object.freeze({ + kty: 'EC', + crv: 'secp256k1', + x: base64url(point.slice(1, 33)), + y: base64url(point.slice(33, 65)), + }); + const canonical = JSON.stringify({ + crv: publicJwk.crv, + kty: publicJwk.kty, + x: publicJwk.x, + y: publicJwk.y, + }); + const kid = base64url(sha256(utf8ToBytes(canonical))); + const namespaceId = `bk3_${kid}`; + const authority: BackupAuthority = Object.freeze({ + namespaceId, + publicJwk, + kid, + signRawBody(rawBytes, path, audience, nowSeconds = Math.floor(Date.now() / 1000)) { + try { + if ( + !child || + !(rawBytes instanceof Uint8Array) || + rawBytes.length > MAX_BODY_BYTES || + !PATHS.has(path) || + typeof audience !== 'string' || + !audience.length || + audience.length > 256 || + utf8ToBytes(audience).length > 256 || + Array.from(audience).some( + (character) => character.charCodeAt(0) < 32 || character.charCodeAt(0) === 127 + ) || + !unsigned(nowSeconds) || + nowSeconds > Number.MAX_SAFE_INTEGER - 120 + ) + unavailable(); + const raw = Uint8Array.from(rawBytes); + assertBodyTarget(raw, path, namespaceId, publicJwk); + const header = { alg: 'ES256K', kid, typ: 'keeper-backup+jws' }; + const claims = { + v: 3, + aud: audience, + method: 'POST', + path, + namespaceId, + bodySha256: bytesToHex(sha256(raw)), + iat: nowSeconds, + exp: nowSeconds + 120, + }; + const input = `${base64url(utf8ToBytes(JSON.stringify(header)))}.${base64url( + utf8ToBytes(JSON.stringify(claims)) + )}`; + // Pinned2.4.0 HDKey.sign accepts a SHA256 digest with prehash:false + // and returns deterministic, compact low-S ECDSA; hash exactly once. + const signature = child.sign(sha256(utf8ToBytes(input))); + if (signature.length !== 64) unavailable(); + return `${input}.${base64url(signature)}`; + } catch { + return unavailable(); + } + }, + dispose() { + try { + child?.wipePrivateData(); + } finally { + child = undefined; + } + }, + }); + delivered = true; + return authority; + } catch { + return unavailable(); + } finally { + // Best effort only: JavaScript/library temporaries cannot promise erasure. + seed?.fill(0); + signingSeed?.fill(0); + master?.wipePrivateData(); + if (!delivered) child?.wipePrivateData(); + } +} diff --git a/src/services/backup/automatic.ts b/src/services/backup/automatic.ts index c5514647b1..d2628bcad7 100644 --- a/src/services/backup/automatic.ts +++ b/src/services/backup/automatic.ts @@ -1,10 +1,12 @@ /* eslint-disable no-await-in-loop -- CAS fanout must use each acknowledged revision in order. */ import realmDatabase from 'src/storage/realm/realm'; -import { RealmSchema } from 'src/storage/realm/enum'; import config from 'src/utils/service-utilities/config'; +import { RealmSchema } from 'src/storage/realm/enum'; import { generateEncryptionKey, hash256 } from 'src/utils/service-utilities/encryption'; import { getKeyUID } from 'src/utils/utilities'; import type { BackupWriteIntent } from './intent'; +import { readProtectedBackup, protectedBackupMutation, protectedBackupBase, + sameProtectedBackupBase, assertProtectedBackupWriteReady } from './protected'; import { BackupImage, canonical, @@ -12,13 +14,13 @@ import { decodeImage, decryptRecord, compareImages, + validateProtectedRecoveryMetadata, } from './image'; import { captureLocalRecovery, backupMetadataMatches } from './localSnapshot'; import { BACKUP_DEADLINE_MS, backupBaselineEpoch, backupConsentEpoch, - boundedBackupPost, canAccessBackupAccount, clearVerifiedRemoteRevision, currentBackupAccountSession, @@ -60,6 +62,7 @@ type Proof = { after: Capture; generation: number; deletion?: boolean; + deadline?: number; }; const proofs = new WeakMap(); const operations = new WeakSet(); @@ -301,21 +304,12 @@ export function isAutomaticBackupResultCurrent(result: object, appId: string) { return false; } } -async function snapshot(appId: string, deadline?: number) { - const response = ( - await boundedBackupPost(`${config.RELAY}getBackupSnapshot`, { appId }, deadline) - ).data; - if ( - !/^[a-f0-9]{64}$/.test(response?.revision) || - response.revisionedMutations !== true || - !Array.isArray(response.unavailableVaultIds) || - response.unavailableVaultIds.length || - !Array.isArray(response.unavailableLabelIds) || - response.unavailableLabelIds.length - ) - throw new Error('Incomplete revisioned backup'); - return response; +async function snapshot(appId: string, app: any, deadline?: number, checkpoint = () => {}) { + const result = await readProtectedBackup(app.primaryMnemonic, deadline, appId, checkpoint, app); + if (result.state === 'neverEnrolled') throw new Error('Protected backup is not enrolled'); + return result.snapshot; } + const same = (kind: keyof BackupImage, a: any, b: any) => !!a && !!b && canonical(recoveryContent(kind, a)) === canonical(recoveryContent(kind, b)); async function project( @@ -325,7 +319,8 @@ async function project( ): Promise { const { appId } = proof.operation; const key = generateEncryptionKey(proof.after.app.primarySeed); - const remote = await decodeImage(key, response, appId, () => {}, true); + const remote = await decodeImage(key, response, response.namespaceId, () => {}, true); + validateProtectedRecoveryMetadata(response, remote, getKeyUID, hash256, key); const projected = clone(remote) as BackupImage; const touched = new Set(); const before = proof.operation.before.image; @@ -563,7 +558,7 @@ export function enqueueAutomaticBackupOperation( notifyBackupReconciliation(appId); return withBackupSession(appId, async () => { let acknowledged = 0; - const deadline = Date.now() + BACKUP_DEADLINE_MS; + const deadline = proof?.deadline ?? Date.now() + BACKUP_DEADLINE_MS; const assertOwned = () => { if (!intent || !proof || Date.now() >= deadline) throw new Error('Backup proof deadline expired'); @@ -582,33 +577,29 @@ export function enqueueAutomaticBackupOperation( ) ) throw new Error('Backup mutation account does not match originating proof'); - // Loopback is permitted only in the explicitly selected Node fixture. - const loopbackFixture = - typeof process !== 'undefined' && - process.env?.KEEPER_RELAY_FIXTURE_MODULE && + const loopbackFixture = typeof process !== 'undefined' && + (process.env?.KEEPER_RELAY_FIXTURE_MODULE || process.env?.KEEPER_PROTECTED_RELAY_CHECKOUT) && /^http:\/\/(127\.0\.0\.1|localhost):\d+\/$/.test(config.RELAY); - const nativeLoopback = - config.ENVIRONMENT === 'DEVELOPMENT' && - config.isDevMode() && + const nativeLoopback = config.ENVIRONMENT === 'DEVELOPMENT' && config.isDevMode() && /^http:\/\/(127\.0\.0\.1|localhost):\d+\/$/.test(config.RELAY); - if ( - (!AUTOMATIC_BACKUP_SOURCE_READY && !loopbackFixture) || - (!AUTOMATIC_BACKUP_RELAY_ROLLOUT_READY && - !loopbackFixture && - !(AUTOMATIC_BACKUP_SOURCE_READY && nativeLoopback)) - ) - throw new Error('Automatic source proof not enabled'); + if ((!AUTOMATIC_BACKUP_SOURCE_READY && !loopbackFixture) || + (!AUTOMATIC_BACKUP_RELAY_ROLLOUT_READY && !loopbackFixture && + !(AUTOMATIC_BACKUP_SOURCE_READY && nativeLoopback))) + throw new Error('Automatic backup source or rollout is not enabled'); + assertProtectedBackupWriteReady(); assertOwned(); - const baseline = await snapshot(appId, deadline); + const baseline = await snapshot(appId, proof.after.app, deadline, assertOwned); assertOwned(); - if (baseline.revision !== intent.baseRevision) throw new Error('Remote revision changed'); + if (intent.baseRevision !== intent.protectedBase?.revision || + !sameProtectedBackupBase(protectedBackupBase(baseline), intent.protectedBase)) + throw new Error('Remote authority changed'); if (proof.deletion) { if (mutations.length !== 1 || mutations[0].operation !== 'deleteBackup') throw new Error('Invalid explicit deletion'); await decodeImage( generateEncryptionKey(proof.after.app.primarySeed), baseline, - appId, + intent.protectedBase.namespaceId, () => {}, true ); @@ -616,60 +607,46 @@ export function enqueueAutomaticBackupOperation( assertOwned(); // One immutable receipt is single-use even if the response is lost. proofs.delete(intent); - let expectedRevision = intent.baseRevision; + let expectedBase = intent.protectedBase; for (const mutation of mutations) { assertOwned(); - const reply = ( - await boundedBackupPost( - `${config.RELAY}revisionedBackupMutation`, - { - ...mutation.body, - appId, - operation: mutation.operation, - expectedRevision, - }, - deadline - ) - ).data; - if ( - reply?.updated !== true || - reply.preconditionApplied !== true || - !/^[a-f0-9]{64}$/.test(reply.newRevision) || - reply.newRevision === expectedRevision - ) - throw new Error('Unacknowledged backup revision'); - expectedRevision = reply.newRevision; + const reply = await protectedBackupMutation(proof.after.app.primaryMnemonic, + expectedBase, mutation.operation, mutation.body, deadline, assertOwned, proof.after.app); + expectedBase = reply.base; acknowledged += 1; } assertOwned(); - const readback = await snapshot(appId, deadline); + const readback = await snapshot(appId, proof.after.app, deadline, assertOwned); assertOwned(); - if (readback.revision !== expectedRevision) throw new Error('Final backup revision changed'); + if (!sameProtectedBackupBase(protectedBackupBase(readback), expectedBase)) + throw new Error('Final backup authority changed'); if (proof.deletion) { const remaining = await decodeImage( generateEncryptionKey(proof.after.app.primarySeed), readback, - appId, + expectedBase.namespaceId, () => {}, true ); - if (Object.values(remaining).some((records) => Object.keys(records).length)) + if (!readback.deleted || !readback.suppressLegacyFallback || + Object.values(remaining).some((records) => Object.keys(records).length)) throw new Error('Backup deletion is incomplete'); } else { - if (!backupMetadataMatches(proof.after.app, readback.appImage)) + if (!backupMetadataMatches(proof.after.app, readback.appImage, expectedBase.namespaceId)) throw new Error('Backup metadata readback differs'); const remote = await decodeImage( generateEncryptionKey(proof.after.app.primarySeed), readback, - appId, + expectedBase.namespaceId, assertOwned, true ); + validateProtectedRecoveryMetadata(readback, remote, getKeyUID, hash256, generateEncryptionKey(proof.after.app.primarySeed)); if ((await compareImages(proof.after.image, remote, assertOwned)) !== 'matched') throw new Error('Complete backup readback differs'); } assertOwned(); - recordVerifiedRemoteRevision(appId, expectedRevision, intent.accountSession); + recordVerifiedRemoteRevision(appId, expectedBase.revision, intent.accountSession, expectedBase); if (!commitVerification(appId, !!proof.deletion, proof.operation.consentEpoch)) throw new Error('Backup changed during final commit'); const result = Object.freeze({ @@ -694,7 +671,17 @@ export async function beginBackupDeleteOperation( ): Promise { try { const op = beginBackupLocalOperation(appId, undefined, originSession); - const remote = await withBackupSession(appId, () => snapshot(appId)); + assertProtectedBackupWriteReady(); + const deadline = Date.now() + BACKUP_DEADLINE_MS; + const assertOrigin = () => { + const state = local(); + if (Date.now() >= deadline || originSession !== currentBackupAccountSession() || + op.consentEpoch !== backupConsentEpoch(appId) || state.database !== op.database || + state.generation !== op.generation || state.capture.fingerprint !== op.before.fingerprint || + !canAccessBackupAccount(appId, true)) throw new Error('Deletion source changed'); + }; + const remote = await withBackupSession(appId, () => + snapshot(appId, op.before.app, deadline, assertOrigin)); const state = local(); if ( op.accountSession !== currentBackupAccountSession() || @@ -708,7 +695,7 @@ export async function beginBackupDeleteOperation( await decodeImage( generateEncryptionKey(op.before.app.primarySeed), remote, - appId, + remote.namespaceId, () => {}, true ); @@ -725,6 +712,7 @@ export async function beginBackupDeleteOperation( appId, accountSession: op.accountSession, baseRevision: remote.revision, + protectedBase: protectedBackupBase(remote), baselineEpoch: backupBaselineEpoch(appId), localGeneration: state.generation, localFingerprint: state.capture.fingerprint, @@ -736,6 +724,7 @@ export async function beginBackupDeleteOperation( after: frozenCopy(state.capture), generation: state.generation, deletion: true, + deadline, }); return Object.freeze({ committed: true, intent }); } catch { @@ -746,6 +735,9 @@ export async function beginBackupDeleteOperation( } setAutomaticBackupPostHandler(async (path, body, _explicit, intent) => { const operation = path.split('/').pop() as AutomaticBackupMutation['operation']; + if (!['updateAppImage', 'migrateXfps', 'deleteAppImageEntity', 'deleteVaults', + 'updateVaultImage', 'modifyLabels', 'deleteBackup'].includes(operation)) + throw new Error('Unsupported backup writer'); const data = await enqueueAutomaticBackupOperation(intent, [{ operation, body }]); return { data }; }); diff --git a/src/services/backup/encoding.ts b/src/services/backup/encoding.ts new file mode 100644 index 0000000000..fb2be39eb4 --- /dev/null +++ b/src/services/backup/encoding.ts @@ -0,0 +1,11 @@ +import { TextEncoder as StandardTextEncoder } from '@exodus/bytes/encoding-lite.js'; + +// The new crypto family needs the standard UTF8 encoder on Hermes. Preserve an +// existing implementation and install no decoder or other platform globals. +if (typeof globalThis.TextEncoder === 'undefined') { + Object.defineProperty(globalThis, 'TextEncoder', { + value: StandardTextEncoder, + writable: true, + configurable: true, + }); +} diff --git a/src/services/backup/image.ts b/src/services/backup/image.ts index 8b88120ec3..b6bbab2d87 100644 --- a/src/services/backup/image.ts +++ b/src/services/backup/image.ts @@ -353,3 +353,27 @@ export async function decodeImage( throw new BackupSnapshotValidationError('Incomplete label backup'); return image; } + +/** Match unencrypted scoped reference metadata to every decrypted vault. */ +export function validateProtectedRecoveryMetadata( + response: any, image: BackupImage, keyUID: (signer: any) => string, + hash: (text: string) => string, encryptionKey: string +) { + const expectedLabels = Object.keys(image.labels).map(id => hash(hash(encryptionKey + id))).sort(); + const receivedLabels = response.labels.map((label: any) => label.id).sort(); + if (canonical(expectedLabels) !== canonical(receivedLabels)) + throw new BackupSnapshotValidationError('Protected label identity differs from recovery data'); + for (const stored of response.allVaultImages) { + const vault = image.vaults[stored.vaultId]; + if (!vault || !Array.isArray(vault.signers)) + throw new BackupSnapshotValidationError('Incomplete protected vault metadata'); + const references = vault.signers.map((signer: any) => ({ + signerId: keyUID(signer), xfpHash: hash(signer.masterFingerprint), + })); + if (stored.isArchived !== !!vault.archived || + canonical(stored.scheme) !== canonical(vault.scheme) || + canonical(stored.signersData) !== canonical(references) || + canonical(stored.signerIds) !== canonical(references.map((signer: any) => signer.signerId))) + throw new BackupSnapshotValidationError('Protected vault metadata differs from recovery data'); + } +} diff --git a/src/services/backup/intent.ts b/src/services/backup/intent.ts index e032f57632..ac0ed934ee 100644 --- a/src/services/backup/intent.ts +++ b/src/services/backup/intent.ts @@ -1,8 +1,10 @@ +import type { ProtectedBackupBase } from './protected'; /** The verified remote base and account session held by one originating write. */ export type BackupWriteIntent = Readonly<{ appId: string; accountSession: number; baseRevision: string; + protectedBase: ProtectedBackupBase; baselineEpoch: number; localGeneration?: number; localFingerprint?: string; diff --git a/src/services/backup/localMutation.ts b/src/services/backup/localMutation.ts index 2930f4d18f..26faf8700e 100644 --- a/src/services/backup/localMutation.ts +++ b/src/services/backup/localMutation.ts @@ -22,7 +22,7 @@ function plain(record: any) { function recoveryView(schema: RealmSchema, record: any) { if (schema === RealmSchema.KeeperApp) return { - id: record.id, publicId: record.publicId, primarySeed: record.primarySeed, + id: record.id, publicId: record.publicId, primarySeed: record.primarySeed, primaryMnemonic: record.primaryMnemonic, version: record.version, networkType: record.networkType, subscription: record.subscription, }; if (schema === RealmSchema.Wallet || schema === RealmSchema.USDTWallet) diff --git a/src/services/backup/localSnapshot.ts b/src/services/backup/localSnapshot.ts index 0c8fb940a9..fcab3b2351 100644 --- a/src/services/backup/localSnapshot.ts +++ b/src/services/backup/localSnapshot.ts @@ -7,7 +7,7 @@ import { } from 'src/utils/utilities'; import { BackupImage, canonical, recoveryContent } from './image'; -export function backupMetadataMatches(local: any, remote: any) { +export function backupMetadataMatches(local: any, remote: any, namespaceId = local.id) { try { const subscription = typeof remote?.subscription === 'string' @@ -15,7 +15,7 @@ export function backupMetadataMatches(local: any, remote: any) { : remote?.subscription; return ( !!remote && - remote.appId === local.id && + remote.appId === namespaceId && remote.publicId === local.publicId && remote.version === local.version && canonical(subscription) === canonical(local.subscription) @@ -83,6 +83,7 @@ export function captureLocalRecovery(database: any) { id: app.id, publicId: app.publicId, primarySeed: app.primarySeed, + primaryMnemonic: app.primaryMnemonic, version: app.version, networkType: app.networkType, subscription: app.subscription, diff --git a/src/services/backup/protected.ts b/src/services/backup/protected.ts new file mode 100644 index 0000000000..aa6f118123 --- /dev/null +++ b/src/services/backup/protected.ts @@ -0,0 +1,207 @@ +import './encoding'; +import { bytesToHex, utf8ToBytes } from '@noble/hashes/utils.js'; +import { sha256 } from '@noble/hashes/sha2.js'; +import config from 'src/utils/service-utilities/config'; +import { deriveBackupAuthority } from './authority'; +import { canonical } from './image'; +import { boundedBackupPost, currentBackupAccountSession } from './transport'; + +/** Remote generation is independent of the committed local Realm generation. */ +export type ProtectedBackupBase = Readonly<{ + namespaceId: string; + revision: string; + generation: number; + authEpoch: number; +}>; +export type ProtectedBackupState = { + status: 'unknown' | 'neverEnrolled' | 'present' | 'deleted'; + namespaceId?: string; + generation?: number; + authEpoch?: number; +}; +export type ProtectedRead = + | { state: 'neverEnrolled'; namespaceId: string } + | { state: 'present' | 'deleted'; namespaceId: string; snapshot: any }; +type Operation = 'updateAppImage' | 'migrateXfps' | 'deleteAppImageEntity' | 'deleteVaults' + | 'updateVaultImage' | 'modifyLabels' | 'deleteBackup' | 'repairAppBackup'; +type Account = { id: string; primarySeed: string }; +type Path = '/backup/v3/enroll' | '/backup/v3/snapshot' | '/backup/v3/mutate'; + +// This is a distinct gate from local consent. Explicit repair is not permission +// to write to a hosted backend whose readiness has not been established. +export const PROTECTED_BACKUP_HOSTED_READY = false; +function loopback() { + return /^http:\/\/(127\.0\.0\.1|localhost):\d+\/$/.test(config.RELAY) && + ((typeof process !== 'undefined' && !!process.env?.KEEPER_PROTECTED_RELAY_CHECKOUT) || + (config.ENVIRONMENT === 'DEVELOPMENT' && config.isDevMode())); +} +export function assertProtectedBackupWriteReady() { + if (!PROTECTED_BACKUP_HOSTED_READY && !loopback()) + throw new Error('Protected backup is not enabled on this server'); +} +function endpoint(path: Path) { + const base = new URL(config.RELAY); + if (base.username || base.password || base.search || base.hash || base.pathname !== '/') + throw new Error('Invalid protected backup endpoint'); + if (loopback()) return { url: `${base.origin}${path}`, audience: 'https://keeper-fixture.invalid' }; + if (base.protocol !== 'https:') throw new Error('Protected backup requires HTTPS'); + return { url: `${base.origin}${path}`, audience: base.origin }; +} +let knownState: (appId: string) => ProtectedBackupState | undefined = () => undefined; +export function setProtectedBackupStateProvider(provider: typeof knownState) { knownState = provider; } +let onState: (appId: string, session: number, state: ProtectedBackupState) => void = () => {}; +export function setProtectedBackupStateObserver(observer: typeof onState) { onState = observer; } +const uint = (n: any) => Number.isSafeInteger(n) && n >= 0 && !Object.is(n, -0); +const object = (v: any) => v && typeof v === 'object' && !Array.isArray(v); +const revision = (v: any) => typeof v === 'string' && /^[a-f0-9]{64}$/.test(v); +const invalid = (): never => { throw new Error('Invalid protected backup response'); }; + +/** Validate the RAW namespace before any adapter supplies the original App ID. */ +function validateRead(value: any, namespaceId: string, enrollment = false): ProtectedRead { + if (!object(value) || value.scope !== 'protected-backup' || value.namespaceId !== namespaceId) + return invalid(); + if (value.exists === false) { + if (enrollment || value.neverEnrolled !== true || + Object.keys(value).some(k => !['scope', 'exists', 'namespaceId', 'neverEnrolled'].includes(k))) + return invalid(); + return { state: 'neverEnrolled', namespaceId }; + } + const app = value.appImage; + if (value.exists !== true || !revision(value.revision) || !uint(value.generation) || + !uint(value.authEpoch) || value.backupRevisionRequired !== true || + value.incrementalRevisionRequired !== true || typeof value.deleted !== 'boolean' || + typeof value.suppressLegacyFallback !== 'boolean' || (value.deleted && !value.suppressLegacyFallback) || + !object(app) || app.appId !== namespaceId || !object(app.wallets) || !object(app.signers) || + !Array.isArray(app.nodes) || !Array.isArray(app.vaults) || !Array.isArray(app.labels) || + !Array.isArray(value.allVaultImages) || !Array.isArray(value.labels) || !Array.isArray(value.vaultMaps) || + !Array.isArray(value.unavailableVaultIds) || value.unavailableVaultIds.length || + !Array.isArray(value.unavailableLabelIds) || value.unavailableLabelIds.length || + value.allVaultImages.some((v: any) => !object(v) || v.appId !== namespaceId)) return invalid(); + if (value.deleted && (Object.keys(app.wallets).length || Object.keys(app.signers).length || + app.nodes.length || app.vaults.length || app.labels.length || value.allVaultImages.length || + value.labels.length || value.vaultMaps.length || + Object.keys(app).some(k => !['appId', 'wallets', 'signers', 'nodes', 'vaults', 'labels'].includes(k)))) + return invalid(); + const maps: any[] = []; + for (const vault of value.allVaultImages) { + if (typeof vault.isArchived !== 'boolean' || !Array.isArray(vault.signersData) || + !Array.isArray(vault.signerIds) || vault.signersData.some((s: any) => + !object(s) || typeof s.signerId !== 'string' || typeof s.xfpHash !== 'string') || + canonical(vault.signerIds) !== canonical(vault.signersData.map((s: any) => s.signerId))) + return invalid(); + if (!vault.isArchived) for (const signer of vault.signersData) + maps.push({...signer, vaultId: vault.vaultId}); + } + const sorted = (rows: any[]) => rows.map(row => canonical(row)).sort(); + if (canonical(sorted(value.vaultMaps)) !== canonical(sorted(maps))) return invalid(); + // Check the complete RAW projection, including maps and reference metadata, + // before any adapter rewrites its namespace into the original Keeper App ID. + const projected = {namespaceId, generation: value.generation, authEpoch: value.authEpoch, + deleted: value.deleted, suppressLegacyFallback: value.suppressLegacyFallback, + appImage: app, allVaultImages: value.allVaultImages, labels: value.labels, vaultMaps: value.vaultMaps}; + if (bytesToHex(sha256(utf8ToBytes(canonical(projected)))) !== value.revision) return invalid(); + return { state: value.deleted ? 'deleted' : 'present', namespaceId, snapshot: value }; +} +export function protectedBackupBase(snapshot: any): ProtectedBackupBase { + if (!revision(snapshot?.revision) || !uint(snapshot?.generation) || !uint(snapshot?.authEpoch) || + typeof snapshot.namespaceId !== 'string') return invalid(); + return Object.freeze({ namespaceId: snapshot.namespaceId, revision: snapshot.revision, + generation: snapshot.generation, authEpoch: snapshot.authEpoch }); +} +export function sameProtectedBackupBase(a: ProtectedBackupBase, b: ProtectedBackupBase) { + return !!a && !!b && a.namespaceId === b.namespaceId && a.revision === b.revision && + a.generation === b.generation && a.authEpoch === b.authEpoch; +} +async function request(mnemonic: string, path: Path, data: object, deadline?: number, + checkpoint?: () => void, expectedNamespace?: string, account?: Account) { + const check = checkpoint || (() => {}); + check(); + const authority = deriveBackupAuthority(mnemonic, account); + const { namespaceId } = authority; + let target: ReturnType; + let raw: string; + let token: string; + try { + if (expectedNamespace && expectedNamespace !== namespaceId) return invalid(); + target = endpoint(path); + const body = { namespaceId, publicJwk: authority.publicJwk, ...data }; + raw = JSON.stringify(body); + if (utf8ToBytes(raw).length > 32 * 1024 * 1024) + throw new Error('Backup exceeds supported size'); + token = authority.signRawBody(utf8ToBytes(raw), path, target.audience); + } finally { authority.dispose(); } + check(); + const response = await boundedBackupPost(target.url, raw, deadline, + { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json' }); + check(); + return { value: response.data, namespaceId }; +} +async function read(mnemonic: string, enroll: boolean, deadline?: number, keeperAppId?: string, + checkpoint?: () => void, account?: Account): Promise { + if (enroll) assertProtectedBackupWriteReady(); + const session = currentBackupAccountSession(); + const { value, namespaceId } = await request(mnemonic, + enroll ? '/backup/v3/enroll' : '/backup/v3/snapshot', {}, deadline, checkpoint, undefined, account); + const result = validateRead(value, namespaceId, enroll); + const known = keeperAppId ? knownState(keeperAppId) : undefined; + if (known?.namespaceId && (known.namespaceId !== namespaceId || + (result.state === 'neverEnrolled' && known.status !== 'neverEnrolled' && known.status !== 'unknown') || + (result.state !== 'neverEnrolled' && + ((known.generation !== undefined && result.snapshot.generation < known.generation) || + (known.authEpoch !== undefined && result.snapshot.authEpoch < known.authEpoch))))) + throw new Error('Protected backup history conflicts with this device'); + if (keeperAppId) onState(keeperAppId, session, { status: result.state, + namespaceId, ...(result.state === 'neverEnrolled' ? {} : + {generation: result.snapshot.generation, authEpoch: result.snapshot.authEpoch}) }); + return result; +} +export const readProtectedBackup = (mnemonic: string, deadline?: number, keeperAppId?: string, + checkpoint?: () => void, account?: Account) => read(mnemonic, false, deadline, keeperAppId, checkpoint, account); +export const enrollProtectedBackup = (mnemonic: string, deadline?: number, keeperAppId?: string, + checkpoint?: () => void, account?: Account) => read(mnemonic, true, deadline, keeperAppId, checkpoint, account); + +/** The allowlist never signs appId/appID, billing identity or unknown aliases. */ +export function protectedMutationData(operation: Operation, body: any) { + if (!object(body)) return invalid(); + const pick = (names: string[]) => Object.fromEntries(names.filter(k => body[k] !== undefined) + .map(k => [k, body[k]])); + switch (operation) { + case 'updateAppImage': + if ((body.walletObject !== undefined && body.walletsObject !== undefined) || + (body.signersObjects !== undefined && body.signersObject !== undefined)) return invalid(); + return { ...pick(['nodes', 'replaceNodes', 'publicId', 'version', 'subscription']), + ...((body.walletsObject ?? body.walletObject) !== undefined + ? {walletsObject: body.walletsObject ?? body.walletObject} : {}), + ...((body.signersObject ?? body.signersObjects) !== undefined + ? {signersObject: body.signersObject ?? body.signersObjects} : {}) }; + case 'migrateXfps': return pick(['signerChanges']); + case 'deleteAppImageEntity': return pick(['walletIds', 'signers']); + case 'deleteVaults': return pick(['vaults']); + case 'updateVaultImage': return pick(['vaultId', 'vault', 'signersData', 'isUpdate', + 'isArchived', 'archiveVaultId', 'vaultShellId', 'scheme']); + case 'modifyLabels': return pick(['addLabels', 'deleteLabels']); + case 'deleteBackup': return {}; + case 'repairAppBackup': return pick(['walletObject', 'signersObject', 'vaultObject', + 'nodes', 'labels', 'publicId', 'version', 'subscription', 'replaceCurrentState']); + default: return invalid(); + } +} +export async function protectedBackupMutation(mnemonic: string, base: ProtectedBackupBase, + operation: Operation, body: any, deadline?: number, checkpoint?: () => void, account?: Account) { + assertProtectedBackupWriteReady(); + if (typeof base?.namespaceId !== 'string' || !revision(base?.revision) || + !uint(base?.generation) || !uint(base?.authEpoch)) return invalid(); + const data = protectedMutationData(operation, body); + const { value } = await request(mnemonic, '/backup/v3/mutate', {operation, + expectedRevision: base.revision, expectedGeneration: base.generation, authEpoch: base.authEpoch, + data}, deadline, checkpoint, base.namespaceId, account); + const deleting = operation === 'deleteBackup'; + if (value?.updated !== true || value.preconditionApplied !== true || + !revision(value.newRevision) || value.newRevision === base.revision || + value.newGeneration !== base.generation + 1 || value.authEpoch !== base.authEpoch + (deleting ? 1 : 0) || + value.deleted !== deleting || typeof value.suppressLegacyFallback !== 'boolean' || + (deleting && (value.suppressLegacyFallback !== true || value.deletionScope !== 'protected-backup-only' || + value.legacyCopiesMayRemain !== true))) return invalid(); + return Object.freeze({...value, base: Object.freeze({namespaceId: base.namespaceId, + revision: value.newRevision, generation: value.newGeneration, authEpoch: value.authEpoch})}); +} diff --git a/src/services/backup/repair.ts b/src/services/backup/repair.ts index 4187dda641..7fa10920bc 100644 --- a/src/services/backup/repair.ts +++ b/src/services/backup/repair.ts @@ -1,7 +1,6 @@ /* eslint-disable no-await-in-loop -- Serialize backup records to bound memory and yield to the UI. */ import dbManager from 'src/storage/realm/dbManager'; import { RealmSchema } from 'src/storage/realm/enum'; -import config from 'src/utils/service-utilities/config'; import realmDatabase from 'src/storage/realm/realm'; import { generateEncryptionKey, hash256 } from 'src/utils/service-utilities/encryption'; import { @@ -10,12 +9,15 @@ import { sanitizeVaultSignersForSeedKeyBackup, } from 'src/utils/utilities'; import { captureLocalRecovery, backupMetadataMatches } from './localSnapshot'; +import { readProtectedBackup, enrollProtectedBackup, protectedBackupMutation, + protectedBackupBase, sameProtectedBackupBase, assertProtectedBackupWriteReady, + ProtectedBackupBase } from './protected'; import { BACKUP_DEADLINE_MS, BackupRequestError, backupQueuedRevision, + backupConsentEpoch, backupRevision, - boundedBackupPost, canAccessBackupAccount, clearVerifiedRemoteRevision, currentBackupAccountSession, @@ -27,6 +29,7 @@ import { BackupSnapshotValidationError, canonical, compareImages, + validateProtectedRecoveryMetadata, decodeImage, encryptRecord, MAX_BACKUP_BYTES, @@ -43,7 +46,9 @@ export type RepairPhase = | 'verifying' | 'verified' | 'conflict' - | 'unverified'; + | 'unverified' + | 'never-enrolled' + | 'deleted'; type InspectionStage = | 'local-capture' | 'snapshot-request' @@ -218,9 +223,11 @@ export function inspectBackup( // One deadline covers both read-only attempts; a retry must not double it. deadline ??= Date.now() + BACKUP_DEADLINE_MS; const revision = backupRevision(appId); + const consentEpoch = backupConsentEpoch(appId); const assertCurrent = () => { if ( !sessionIsCurrent() || + consentEpoch !== backupConsentEpoch(appId) || Date.now() >= deadline || (dbManager.getObjectByIndex(RealmSchema.KeeperApp) as any)?.id !== appId || !canAccessBackupAccount(appId, explicitChoice) @@ -237,12 +244,20 @@ export function inspectBackup( try { notify('checking'); const app = clone(dbManager.getObjectByIndex(RealmSchema.KeeperApp)); - const localEvidence = captureLocalRecovery(realmDatabase.getDatabase()); + const database = realmDatabase.getDatabase(); + const localEvidence = captureLocalRecovery(database); + const assertSource = () => { + assertCurrent(); + if (realmDatabase.getDatabase() !== database || + captureLocalRecovery(database).fingerprint !== localEvidence.fingerprint) + throw new Error('Committed recovery changed before backup write'); + }; assertCurrent(); const key = generateEncryptionKey(app.primarySeed); const local = await captureBackupImage(assertCurrent); assertCurrent(); - let expectedRevision: string; + let expectedBase: ProtectedBackupBase; + let discovery: 'never-enrolled' | 'deleted' | undefined; let needsVaultRepair = false; let needsLabelRepair = false; let needsMetadataRepair = false; @@ -250,19 +265,22 @@ export function inspectBackup( let unavailableLabelIds: string[] = []; const read = async (readback = false) => { stage = readback ? 'repair-readback' : 'snapshot-request'; - const response = await boundedBackupPost( - `${config.RELAY}getBackupSnapshot`, - { appId }, - deadline - ); + let found = await readProtectedBackup(app.primaryMnemonic, deadline, appId, assertCurrent, app); + assertCurrent(); + if (found.state === 'neverEnrolled') { + if (!repair || readback) { discovery = 'never-enrolled'; return undefined; } + // Enrollment is a remote write: only the explicitly requested repair + // owns this captured local/session/consent scope, and it stays gated. + assertProtectedBackupWriteReady(); + found = await enrollProtectedBackup(app.primaryMnemonic, deadline, appId, assertSource, app); + if (found.state === 'neverEnrolled') throw new Error('Enrollment not confirmed'); + } + if (found.state === 'deleted' && !repair) { discovery = 'deleted'; return undefined; } + const response = {data: found.snapshot}; if (!readback) stage = 'snapshot-validation'; assertCurrent(); - if (!/^[a-f0-9]{64}$/.test(response.data?.revision)) - throw new Error('Backup snapshot revision unavailable'); - if (response.data?.revisionedMutations !== true) - throw new Error('Revisioned backup protocol unavailable'); - expectedRevision = response.data.revision; - needsMetadataRepair = !backupMetadataMatches(app, response.data.appImage); + expectedBase = protectedBackupBase(response.data); + needsMetadataRepair = !backupMetadataMatches(app, response.data.appImage, expectedBase.namespaceId); const unavailable = response.data.unavailableVaultIds ?? []; if (!Array.isArray(unavailable) || new Set(unavailable).size !== unavailable.length) throw new Error('Invalid unavailable backup records'); @@ -321,7 +339,8 @@ export function inspectBackup( if (!readback) stage = 'snapshot-decryption'; // Restore can salvage legacy map aliases. Inspection requires the // stored key to match the ID inside each encrypted wallet record. - const decoded = await decodeImage(key, availableResponse, appId, assertCurrent, true); + const decoded = await decodeImage(key, availableResponse, expectedBase.namespaceId, assertCurrent, true); + validateProtectedRecoveryMetadata(response.data, decoded, getKeyUID, hash256, key); needsVaultRepair = unavailable.length > 0 || response.data.allVaultImages.some( @@ -333,6 +352,11 @@ export function inspectBackup( return decoded; }; const remote = await read(); + if (!remote) { + const phase = discovery || 'unverified'; + notify(phase); + return phase; + } stage = 'comparison'; const contentComparison = await compareImages(local, remote, assertCurrent); const comparison = @@ -374,40 +398,24 @@ export function inspectBackup( assertCurrent(); notify('uploading'); stage = 'repair-upload'; - let revisionedRepairAcknowledged = false; - let acknowledgedRepairRevision: string | undefined; + assertProtectedBackupWriteReady(); + let acknowledgedBase: ProtectedBackupBase | undefined; try { - const response = await boundedBackupPost( - `${config.RELAY}revisionedBackupMutation`, - // This explicit action replaces the server snapshot only after - // proving it preserves the remote recovery content. The relay - // checks the revision and activates the incremental-write fence - // atomically before accepting it. Old relays return 404 here. - { - ...payload, - operation: 'repairAppBackup', - expectedRevision, - replaceCurrentState: true, - }, - deadline - ); - revisionedRepairAcknowledged = - response.data?.updated === true && - response.data?.preconditionApplied === true && - /^[a-f0-9]{64}$/.test(response.data?.newRevision) && - response.data.newRevision !== expectedRevision; - if (revisionedRepairAcknowledged) acknowledgedRepairRevision = response.data.newRevision; + const reply = await protectedBackupMutation(app.primaryMnemonic, expectedBase, + 'repairAppBackup', {...payload, replaceCurrentState: true}, deadline, assertSource, app); + acknowledgedBase = reply.base; } catch { - // A lost response is not proof the upload failed. Read back, but do - // not mark this repair verified without an acknowledged v2 write. + // An unknown outcome permits a fresh read, never a retry or a new + // base for this ciphertext. A matching read alone is not its ACK. } notify('verifying'); stage = 'repair-readback'; const verifiedRemote = await read(true); stage = 'repair-readback'; if ( - !revisionedRepairAcknowledged || - expectedRevision !== acknowledgedRepairRevision || + !acknowledgedBase || + !sameProtectedBackupBase(expectedBase, acknowledgedBase) || + !verifiedRemote || needsVaultRepair || needsLabelRepair || needsMetadataRepair || @@ -426,7 +434,7 @@ export function inspectBackup( captureLocalRecovery(realmDatabase.getDatabase()).fingerprint !== localEvidence.fingerprint ) throw new Error('Committed recovery changed during final verification'); - recordVerifiedRemoteRevision(appId, expectedRevision, expectedSession); + recordVerifiedRemoteRevision(appId, expectedBase.revision, expectedSession, expectedBase); notify('verified'); return 'verified'; } catch (error) { diff --git a/src/services/backup/restore.ts b/src/services/backup/restore.ts index 0bc3c7039e..9144dc4ab8 100644 --- a/src/services/backup/restore.ts +++ b/src/services/backup/restore.ts @@ -1,6 +1,106 @@ import { TronWeb } from 'tronweb'; import { BackupSnapshotValidationError, decodeImage } from './image'; +export type BackupRecoverySource = 'protected' | 'legacy-older'; +export type BackupRecoverySelection = + | { source: 'protected' } + | { source: 'legacy-older'; olderCopyConfirmed: true }; + +export type RecoveryProvenance = { + source: BackupRecoverySource; + keeperAppId: string; + namespaceId?: string; + legacyMayPredateDeletion: boolean; + legacyCopiesMayRemain: true; +}; + +export type RecoveryDiscovery = { + status: + | 'idle' + | 'checking' + | 'protected-found' + | 'protected-empty' + | 'protected-absent' + | 'protected-deleted' + | 'legacy-selected' + | 'read-error' + | 'restored'; + source: BackupRecoverySource; + keeperAppId?: string; + namespaceId?: string; + legacyMayPredateDeletion?: boolean; + error?: string; + provenance?: RecoveryProvenance; +}; + +export type ProtectedBackupState = { + status: 'unknown' | 'neverEnrolled' | 'present' | 'deleted'; + namespaceId?: string; + generation?: number; + authEpoch?: number; +}; + +type RecoveryImageSource = + | { source: 'protected'; namespaceId: string; response: any } + | { source: 'legacy-older'; olderCopyConfirmed: true; response: any; legacyMayPredateDeletion?: boolean }; + +// The authenticated protected transport validates its raw namespace projection +// before this adapter runs. Keep that projection identity distinct from the +// original Keeper/provider identity written by recoverApp. This normalization +// grants no legacy ownership, billing entitlement or upload consent. The normal +// prepareRecoveryImage call in recoverApp remains the complete decrypt/validate +// barrier before its first Realm write; do not decrypt the whole image twice. +export function prepareRecoverySourceImage(keeperAppId: string, source: RecoveryImageSource) { + const { response } = source; + const projectionId = source.source === 'protected' ? source.namespaceId : keeperAppId; + if ( + !keeperAppId || !response?.appImage || response.appImage.appId !== projectionId || + (response.unavailableVaultIds !== undefined && + (!Array.isArray(response.unavailableVaultIds) || response.unavailableVaultIds.length !== 0)) || + (response.unavailableLabelIds !== undefined && + (!Array.isArray(response.unavailableLabelIds) || response.unavailableLabelIds.length !== 0)) + ) throw new BackupSnapshotValidationError('Recovery data unavailable'); + if (source.source === 'protected' && ( + response.scope !== 'protected-backup' || response.exists !== true || + response.namespaceId !== source.namespaceId || response.deleted !== false || + typeof response.suppressLegacyFallback !== 'boolean' || + !Array.isArray(response.allVaultImages) || !Array.isArray(response.labels) || + response.allVaultImages.some((vault: any) => vault?.appId !== source.namespaceId) + )) throw new BackupSnapshotValidationError('Invalid protected recovery source'); + if (source.source === 'legacy-older' && source.olderCopyConfirmed !== true) + throw new BackupSnapshotValidationError('Older recovery copy was not selected'); + const allVaultImages = response.allVaultImages === undefined ? [] : response.allVaultImages; + const labels = response.labels === undefined ? [] : response.labels; + if (!Array.isArray(allVaultImages) || !Array.isArray(labels)) + throw new BackupSnapshotValidationError('Recovery data unavailable'); + const provenance: RecoveryProvenance = { + source: source.source, + keeperAppId, + ...(source.source === 'protected' ? { namespaceId: source.namespaceId } : {}), + legacyMayPredateDeletion: source.source === 'legacy-older' && !!source.legacyMayPredateDeletion, + legacyCopiesMayRemain: true, + }; + return { + appImage: { ...response.appImage, appId: keeperAppId }, + allVaultImages: source.source === 'protected' + ? allVaultImages.map((vault: any) => ({ ...vault, appId: keeperAppId })) + : allVaultImages, + labels, + provenance, + }; +} + +export const hasRecoveryRecords = (response: any): boolean => !!response?.appImage && ( + Object.keys(response.appImage.wallets || {}).length > 0 || + Object.keys(response.appImage.signers || {}).length > 0 || + (Array.isArray(response.appImage.nodes) && response.appImage.nodes.length > 0) || + (Array.isArray(response.appImage.vaults) && response.appImage.vaults.length > 0) || + (Array.isArray(response.appImage.labels) && response.appImage.labels.length > 0) || + (Array.isArray(response.allVaultImages) && response.allVaultImages.length > 0) || + (Array.isArray(response.labels) && response.labels.length > 0) || + (Array.isArray(response.vaultMaps) && response.vaultMaps.length > 0) +); + // Decode every record before changing Realm. Older backups may omit collections // introduced later; an unreadable response or a missing referenced record is not // an empty backup and must never trigger successful partial recovery. @@ -9,7 +109,8 @@ export async function prepareRecoveryImage( appId: string, appImage: any, allVaultImages: any[], - labels: any[] + labels: any[], + verifyProtectedWalletKeys = false ) { if (!appImage || appImage.appId !== appId) throw new Error('Recovery data unavailable'); // Older Relay registrations and deletions stored empty maps as empty arrays. @@ -27,7 +128,9 @@ export async function prepareRecoveryImage( allVaultImages, labels, }, - appId + appId, + () => {}, + verifyProtectedWalletKeys ); // Legacy USDT wallets cannot recover these addresses from the retired // provider. Reject an unusable image before the first Realm write. diff --git a/src/services/backup/transport.ts b/src/services/backup/transport.ts index c7cc599445..ec19b046ac 100644 --- a/src/services/backup/transport.ts +++ b/src/services/backup/transport.ts @@ -1,5 +1,6 @@ import RestClient from '../rest/RestClient'; import type { BackupWriteIntent } from './intent'; +import type { ProtectedBackupBase } from './protected'; // One queue per account covers repair, incremental writes, labels and deletion. const queues = new Map>(); @@ -7,12 +8,27 @@ const revisions = new Map(); const queuedMutationRevisions = new Map(); const verifiedRemoteRevisions = new Map< string, - { revision: string; session: number; epoch: number; evidence?: LocalEvidence } + { revision: string; session: number; epoch: number; protectedBase?: ProtectedBackupBase; evidence?: LocalEvidence } >(); const baselineEpochs = new Map(); const capturedIntents = new WeakSet(); const consentEpochs = new Map(); const consentCompletions = new WeakMap(); +const recoveryDisables = new WeakMap(); +// Only a validated restore may revoke saved consent for a not-yet-selected +// target. The ordinary UI/account guard still rejects unbranded actions. +export function protectBackupRecoveryConsentDisable(action: T, appId: string, session: number): T { + recoveryDisables.set(action, {appId, session}); + return action; +} +export function takeBackupRecoveryConsentDisable(action: any, appId: string): boolean { + const scope = recoveryDisables.get(action); + recoveryDisables.delete(action); + return !!scope && scope.appId === appId && scope.session === currentBackupAccountSession() && + action.type === 'bhr/setAutomaticCloudBackup' && action.payload?.appId === appId && + action.payload.enabled === false; +} + export const backupConsentEpoch = (appId: string) => consentEpochs.get(appId) || 0; export function protectBackupConsentCompletion( action: T, @@ -87,7 +103,9 @@ export function clearVerifiedRemoteRevision(appId: string) { verifiedRemoteRevisions.delete(appId); baselineEpochs.set(appId, (baselineEpochs.get(appId) || 0) + 1); } -export function recordVerifiedRemoteRevision(appId: string, revision: string, session: number) { +export function recordVerifiedRemoteRevision( + appId: string, revision: string, session: number, protectedBase?: ProtectedBackupBase +) { if (!/^[a-f0-9]{64}$/.test(revision) || session !== accountSession || appId !== activeAccount) return; clearVerifiedRemoteRevision(appId); @@ -95,12 +113,14 @@ export function recordVerifiedRemoteRevision(appId: string, revision: string, se revision, session, epoch: baselineEpochs.get(appId) || 0, + protectedBase: protectedBase ? Object.freeze({ ...protectedBase }) : undefined, evidence: localEvidence?.(), }); } export function captureBackupWriteIntent(appId: string): BackupWriteIntent | undefined { const verified = verifiedRemoteRevisions.get(appId); - if (!verified || appId !== activeAccount || verified.session !== accountSession) return undefined; + if (!verified?.protectedBase || appId !== activeAccount || verified.session !== accountSession) + return undefined; let evidence: LocalEvidence | undefined; try { evidence = localEvidence?.(); @@ -112,6 +132,7 @@ export function captureBackupWriteIntent(appId: string): BackupWriteIntent | und appId, accountSession: verified.session, baseRevision: verified.revision, + protectedBase: verified.protectedBase, baselineEpoch: verified.epoch, localGeneration: verified.evidence?.generation, localFingerprint: verified.evidence?.fingerprint, @@ -183,8 +204,9 @@ export function withBackupSession(id: string, work: () => Promise): Promis // has an unknown outcome; callers must read back before deciding to upload again. export async function boundedBackupPost( path: string, - body: object, - deadline = Date.now() + BACKUP_DEADLINE_MS + body: object | string, + deadline?: number, + signedHeaders?: Record ) { const controller = new AbortController(); let idle: ReturnType; @@ -194,12 +216,13 @@ export async function boundedBackupPost( clearTimeout(idle); idle = setTimeout(() => controller.abort(), BACKUP_STALL_MS); }; - const remaining = deadline - Date.now(); + const remaining = (deadline ?? Date.now() + BACKUP_DEADLINE_MS) - Date.now(); if (remaining <= 0) throw new BackupRequestError('timeout'); const total = setTimeout(() => controller.abort(), remaining); resetIdle(); try { - return await RestClient.post(path, body, undefined, { + return await RestClient.post(path, body as object, signedHeaders, { + ...(typeof body === 'string' ? { transformRequest: [(data: string) => data], maxRedirects: 0 } : {}), signal: controller.signal, onUploadProgress: ({ loaded }) => { if (loaded > uploaded) { diff --git a/src/store/reducers/bhr.ts b/src/store/reducers/bhr.ts index 10456c7826..9519fec567 100644 --- a/src/store/reducers/bhr.ts +++ b/src/store/reducers/bhr.ts @@ -6,6 +6,7 @@ import { reduxStorage } from 'src/storage'; import { persistReducer } from 'redux-persist'; import { VaultSigner } from 'src/services/wallets/interfaces/vault'; import { seedWordItem } from 'src/screens/Recovery/constants'; +import type { RecoveryDiscovery, ProtectedBackupState } from 'src/services/backup/restore'; const initialState: { backupMethod: BackupType | null; @@ -15,6 +16,9 @@ const initialState: { loading: boolean; appImageError: string; appImagerecoveryRetry: boolean; + recoveryDiscovery: RecoveryDiscovery; + // Persisted source/tombstone hints, never current verification authority. + protectedBackupStateByAppId: Record; signingDevices: VaultSigner[]; relayWalletUpdateLoading: boolean; @@ -71,6 +75,8 @@ const initialState: { appImageError: '', appImagerecoveryRetry: false, + recoveryDiscovery: { status: 'idle', source: 'protected' }, + protectedBackupStateByAppId: {}, signingDevices: [], relayWalletUpdateLoading: false, relayWalletUpdate: false, @@ -188,6 +194,19 @@ const bhrSlice = createSlice({ appImagerecoveryRetry: (state) => { state.appImagerecoveryRetry = !state.appImagerecoveryRetry; }, + setRecoveryDiscovery: (state, action: PayloadAction) => { + state.recoveryDiscovery = action.payload; + }, + resetRecoveryDiscovery: (state) => { + state.recoveryDiscovery = { status: 'idle', source: 'protected' }; + }, + setProtectedBackupState: ( + state, + action: PayloadAction<{ appId: string; state: ProtectedBackupState }> + ) => { + if (!action.payload.appId) return; + (state.protectedBackupStateByAppId ??= {})[action.payload.appId] = action.payload.state; + }, setSigningDevices: (state, action: PayloadAction) => { state.signingDevices = _.uniqBy([...state.signingDevices, action.payload], 'signerId'); }, @@ -396,6 +415,9 @@ export const { setBackupLoading, setAppImageError, appImagerecoveryRetry, + setRecoveryDiscovery, + resetRecoveryDiscovery, + setProtectedBackupState, setSigningDevices, @@ -454,6 +476,7 @@ const bhrPersistConfig = { 'loading', 'appImageError', 'appImagerecoveryRetry', + 'recoveryDiscovery', 'relayWalletUpdateLoading', 'relayWalletUpdate', diff --git a/src/store/sagaActions/bhr.ts b/src/store/sagaActions/bhr.ts index 0800182638..ac97541259 100644 --- a/src/store/sagaActions/bhr.ts +++ b/src/store/sagaActions/bhr.ts @@ -1,4 +1,5 @@ import { Signer, Vault } from 'src/services/wallets/interfaces/vault'; +import type { BackupRecoverySelection } from 'src/services/backup/restore'; export const UPDATE_APP_IMAGE = 'UPDATE_APP_IMAGE'; export const GET_APP_IMAGE = 'GET_APP_IMAGE'; @@ -51,11 +52,16 @@ export const updateVaultImage = (payload: { payload, }); -export const getAppImage = (primaryMnemonic: string, isForgot: boolean) => ({ +export const getAppImage = ( + primaryMnemonic: string, + isForgot: boolean, + selection: BackupRecoverySelection = { source: 'protected' } +) => ({ type: GET_APP_IMAGE, payload: { primaryMnemonic, isForgot, + selection, }, }); diff --git a/src/store/sagas/bhr.ts b/src/store/sagas/bhr.ts index 0fd5a853ba..093e28444d 100644 --- a/src/store/sagas/bhr.ts +++ b/src/store/sagas/bhr.ts @@ -1,13 +1,24 @@ import { buffers, END, eventChannel } from 'redux-saga'; import { inspectBackup, RepairPhase } from 'src/services/backup/repair'; -import { prepareRecoveryImage } from 'src/services/backup/restore'; -import { BackupImage, pauseBackup } from 'src/services/backup/image'; +import { + hasRecoveryRecords, + prepareRecoveryImage, + prepareRecoverySourceImage, +} from 'src/services/backup/restore'; +import type { + BackupRecoverySource, ProtectedBackupState, RecoveryProvenance, +} from 'src/services/backup/restore'; +import { readProtectedBackup, sameProtectedBackupBase } from 'src/services/backup/protected'; +import { BackupImage, pauseBackup, validateProtectedRecoveryMetadata } from 'src/services/backup/image'; import { backupRevision, captureBackupWriteIntent, currentBackupAccountSession, backupConsentEpoch, protectBackupConsentCompletion, + protectBackupRecoveryConsentDisable, + clearVerifiedRemoteRevision, + notifyBackupReconciliation, markBackupMutation, } from 'src/services/backup/transport'; import type { BackupWriteIntent } from 'src/services/backup/intent'; @@ -17,6 +28,7 @@ import * as bip39 from 'bip39'; import { Wallet } from 'src/services/wallets/interfaces/wallet'; import { call, put, select, takeMaybe, takeEvery } from 'redux-saga/effects'; import config from 'src/utils/service-utilities/config'; +import { translations } from 'src/context/Localization/LocContext'; import { encrypt, generateEncryptionKey, hash256 } from 'src/utils/service-utilities/encryption'; import BIP85 from 'src/services/wallets/operations/BIP85'; import DeviceInfo from 'react-native-device-info'; @@ -81,6 +93,8 @@ import { setIsCloudBsmsBackupRequired, setPendingAllBackup, setSeedConfirmed, + setRecoveryDiscovery, + setProtectedBackupState, } from '../reducers/bhr'; import { CHECK_BACKUP_FRESHNESS, @@ -166,6 +180,7 @@ export function buildAutomaticVaultMutation(vaultId: string, appId: string, isUp signersData: vault.signers.map((signer) => ({ signerId: getKeyUID(signer), xfpHash: hash256(signer.masterFingerprint), })), + scheme: vault.scheme, isArchived: !!vault.archived, subscription: JSON.stringify(app.subscription), } }; } @@ -342,17 +357,37 @@ function* seedBackedUpWorker() { } } +let latestRecoveryReadRequest = 0; + function* getAppImageWorker({ payload }) { + const request = ++latestRecoveryReadRequest; const requestSession = currentBackupAccountSession(); const { primaryMnemonic, isForgot } = payload; + const selection = payload.selection ?? { source: 'protected' }; + const source: BackupRecoverySource = selection.source === 'legacy-older' + ? 'legacy-older' : 'protected'; + let keeperAppId: string; + let namespaceId: string; + let provenance: RecoveryProvenance; + let protectedState: ProtectedBackupState; + let recoverySession: number; + const assertRequestSession = () => { + if (request !== latestRecoveryReadRequest || requestSession !== currentBackupAccountSession()) + throw new Error('Backup account changed'); + }; try { yield put(setAppImageError('')); + yield put(setRecoveryDiscovery({ status: 'checking', source })); + if (selection.source !== 'protected' && + (selection.source !== 'legacy-older' || selection.olderCopyConfirmed !== true)) + throw new Error('Older recovery copy was not selected'); if (!bip39.validateMnemonic(primaryMnemonic)) { throw Error('Invalid mnemonic'); } const { bitcoinNetworkType } = yield select((state: RootState) => state.settings); const primarySeed = bip39.mnemonicToSeedSync(primaryMnemonic); const appID = crypto.createHash('sha256').update(primarySeed).digest('hex'); + keeperAppId = appID; if (isForgot) { // Allow only existing appId to be recovered using forgot passcode flow. const { allAccounts } = yield select((state: RootState) => state.account); @@ -361,13 +396,53 @@ function* getAppImageWorker({ payload }) { } const encryptionKey = generateEncryptionKey(primarySeed.toString('hex')); - const response = yield call(Relay.getAppImage, appID); - if (requestSession !== currentBackupAccountSession()) - throw new Error('Backup account changed'); - if (!response?.appImage) throw new Error('Recovery data unavailable'); - const { appImage } = response; - const labels = response.labels === undefined ? [] : response.labels; - const allVaultImages = response.allVaultImages === undefined ? [] : response.allVaultImages; + assertRequestSession(); + let prepared; + if (source === 'protected') { + const result = yield call(readProtectedBackup, primaryMnemonic, undefined, appID); + assertRequestSession(); + namespaceId = result.namespaceId; + if (result.state === 'neverEnrolled' || result.state === 'deleted') { + yield put(setRecoveryDiscovery({ + status: result.state === 'deleted' ? 'protected-deleted' : 'protected-absent', + source, + keeperAppId: appID, + namespaceId, + legacyMayPredateDeletion: result.state === 'deleted', + })); + return; + } + prepared = prepareRecoverySourceImage(appID, { + source: 'protected', namespaceId, response: result.snapshot, + }); + protectedState = { + status: 'present', namespaceId, + generation: result.snapshot.generation, authEpoch: result.snapshot.authEpoch, + }; + if (!hasRecoveryRecords(result.snapshot)) { + yield put(setRecoveryDiscovery({ + status: 'protected-empty', source, keeperAppId: appID, namespaceId, + legacyMayPredateDeletion: result.snapshot.suppressLegacyFallback, + })); + return; + } + } else { + // Older copies are read only after a separate, deliberate UI choice. + // A protected absence, tombstone or request error never selects this path. + const response = yield call(Relay.getAppImage, appID); + assertRequestSession(); + prepared = prepareRecoverySourceImage(appID, { + source: 'legacy-older', olderCopyConfirmed: true, response, + legacyMayPredateDeletion: true, + }); + } + const { appImage, labels, allVaultImages } = prepared; + provenance = prepared.provenance; + yield put(setRecoveryDiscovery({ + status: source === 'protected' ? 'protected-found' : 'legacy-selected', + source, keeperAppId: appID, namespaceId, provenance, + })); + assertRequestSession(); const previousVersion = appImage.version; @@ -386,7 +461,7 @@ function* getAppImageWorker({ payload }) { icon: 'assets/ic_diamond_hands.svg', receipt: '', }; - const recoverySession: number = yield call( + recoverySession = yield call( recoverApp, primaryMnemonic, primarySeed, @@ -397,9 +472,17 @@ function* getAppImageWorker({ payload }) { allVaultImages, labels, previousVersion, - requestSession + requestSession, + (ownedSession: number) => { + if (request !== latestRecoveryReadRequest) throw new Error('Backup account changed'); + // Error feedback may follow only the session selected by this restore. + // This callback supplies no remote baseline or upload authority. + recoverySession = ownedSession; + }, + source === 'protected' ); - if (recoverySession !== currentBackupAccountSession() || currentBackupAppId() !== appID) + if (request !== latestRecoveryReadRequest || + recoverySession !== currentBackupAccountSession() || currentBackupAppId() !== appID) throw new Error('Backup account changed'); if ((!appImage.wallets || !Object.entries(appImage.wallets).length) && !allVaultImages.length) { @@ -432,7 +515,8 @@ function* getAppImageWorker({ payload }) { originAppId: appID, originBackupAccountSession: recoverySession, }); - if (recoverySession !== currentBackupAccountSession() || currentBackupAppId() !== appID) + if (request !== latestRecoveryReadRequest || + recoverySession !== currentBackupAccountSession() || currentBackupAppId() !== appID) throw new Error('Backup account changed'); const recoveredSigners: Signer[] = yield call(dbManager.getCollection, RealmSchema.Signer); const recoveredKey = recoveredSigners.find( @@ -467,7 +551,8 @@ function* getAppImageWorker({ payload }) { originBackupAccountSession: recoverySession, }); } - if (recoverySession !== currentBackupAccountSession() || currentBackupAppId() !== appID) + if (request !== latestRecoveryReadRequest || + recoverySession !== currentBackupAccountSession() || currentBackupAppId() !== appID) throw new Error('Backup account changed'); yield put(addAccount(appID)); yield put(autoSyncWallets(true, true, false, appID)); @@ -481,11 +566,43 @@ function* getAppImageWorker({ payload }) { yield put(loadConciergeUser(null)); yield put(loadConciergeTickets([])); yield put(setRecoveryKeyStatus({ appId: appID, status: 'confirmed' })); + if (protectedState) { + const hint: ProtectedBackupState = yield select( + (state: RootState) => state.bhr.protectedBackupStateByAppId?.[appID] + ); + if (request !== latestRecoveryReadRequest || + recoverySession !== currentBackupAccountSession() || currentBackupAppId() !== appID) + throw new Error('Backup account changed'); + // Retain a later authenticated tombstone/generation observed during this + // restore. This persisted source hint is never verification or consent. + const comparable = !hint?.namespaceId || hint.namespaceId === namespaceId; + const older = hint && ( + hint.generation > protectedState.generation || hint.authEpoch > protectedState.authEpoch || + (hint.generation === protectedState.generation && hint.authEpoch === protectedState.authEpoch && + hint.status === 'deleted') + ); + if (comparable && !older) + yield put(setProtectedBackupState({ appId: appID, state: protectedState })); + } + yield put(setRecoveryDiscovery({ + status: 'restored', source, keeperAppId: appID, namespaceId, provenance, + })); yield put(setAppCreated(true)); } catch (err) { - yield put(setAppImageError(err.message)); + // A late response for a previous account/request must not replace the + // current recovery screen's provenance or present an error as absence. + const activeSession = recoverySession ?? requestSession; + if (request === latestRecoveryReadRequest && activeSession === currentBackupAccountSession()) { + const error = err.message || 'Recovery data unavailable'; + yield put(setRecoveryDiscovery({ + status: 'read-error', source, keeperAppId, namespaceId, error, + })); + yield put(setAppImageError(error)); + } } finally { - yield put(appImagerecoveryRetry()); + if (request === latestRecoveryReadRequest && + (recoverySession ?? requestSession) === currentBackupAccountSession()) + yield put(appImagerecoveryRetry()); } } @@ -499,7 +616,9 @@ function* recoverApp( allVaultImages, labels, previousVersion, - originBackupAccountSession?: number + originBackupAccountSession?: number, + onRecoverySession?: (ownedSession: number) => void, + protectedMetadata = false ) { // Recovery selects appID after the first Realm write. Keep the session from // the initiating action across decryption and all earlier async work. @@ -516,8 +635,28 @@ function* recoverApp( appID, appImage, allVaultImages, - labels + labels, + protectedMetadata ); + if (protectedMetadata) + validateProtectedRecoveryMetadata({allVaultImages, labels}, recovered, getKeyUID, hash256, encryptionKey); + // A restored copy does not inherit old opt-in state. Revoke only after full + // validation, before any local write or producer, for BOTH recovery sources. + assertOrigin(); + yield call(() => { + assertOrigin(); + const consentEpoch = backupConsentEpoch(appID); + store.dispatch(protectBackupRecoveryConsentDisable( + setAutomaticCloudBackup({appId: appID, enabled: false}), appID, originSession + )); + assertOrigin(); + if (store.getState().bhr.automaticCloudBackupByAppId?.[appID] !== false || + backupConsentEpoch(appID) <= consentEpoch) + throw new Error('Recovery backup consent could not be disabled'); + markBackupMutation(appID); + clearVerifiedRemoteRevision(appID); + notifyBackupReconciliation(appID); + }); const entropy = yield call( BIP85.bip39MnemonicToEntropy, config.BIP85_IMAGE_ENCRYPTIONKEY_DERIVATION_PATH, @@ -554,6 +693,7 @@ function* recoverApp( throw new Error('Backup account changed'); }; assertRecoveryAccount(); + onRecoverySession?.(recoveryAccountSession); function* writeRecord(schema: RealmSchema, record: any) { yield call(writeRecoveredObject, schema, record, assertRecoveryAccount); } @@ -1063,6 +1203,7 @@ function* runBackupInspection( currentIntent.appId === verifiedEvidence.intent.appId && currentIntent.accountSession === verifiedEvidence.intent.accountSession && currentIntent.baseRevision === verifiedEvidence.intent.baseRevision && + sameProtectedBackupBase(currentIntent.protectedBase, verifiedEvidence.intent.protectedBase) && currentIntent.baselineEpoch === verifiedEvidence.intent.baselineEpoch; }; try { @@ -1135,8 +1276,8 @@ function* runBackupInspection( payload: { uaiType: uaiType.SERVER_BACKUP_FAILURE, uaiDetails: { - heading: 'Check Your Backup', - body: 'Your Assisted Server Backup may differ from this device. View details to check it.', + heading: translations.recoveryBackup.notificationTitle, + body: translations.recoveryBackup.notificationBody, }, }, }); @@ -1164,8 +1305,10 @@ function* backupFreshnessWorker({ type, appId, originBackupAccountSession }) { const enabled = yield select((state: RootState) => state.storage.appId === appId && isAutomaticCloudBackupEnabled(state.bhr, appId) ); - if (!enabled || requestSession !== currentBackupAccountSession()) return; - yield call(runBackupInspection, type === REPAIR_BACKUP, appId, false, requestSession); + const repair = type === REPAIR_BACKUP; + if ((repair && !enabled) || requestSession !== currentBackupAccountSession()) return; + // Consent-off status checking is read-only; it must not enroll or repair. + yield call(runBackupInspection, repair, appId, !repair, requestSession); } export function* backupFreshnessWatcher() { @@ -1226,10 +1369,12 @@ function* deleteBackupWorker({ appId, originBackupAccountSession }: { const isCurrent = () => currentBackupAppId() === appId && currentBackupAccountSession() === requestSession && backupConsentEpoch(appId) === requestConsentEpoch; if (!appId || !isCurrent()) return false; + yield put(setDeleteBackupSuccess({ appId, status: false })); + yield put(setDeleteBackupFailure({ appId, status: false })); yield put(setBackupAllLoading({ appId, status: true })); try { - // Opt-out deliberately removes the remote copy while retaining every local - // record. Its own fresh v2 snapshot is bound to this original session. + // Delete only the protected copy. Its authenticated fresh tuple is bound + // to this original session; retained older copies and all local data stay. const intent = yield call(beginBackupDeleteOperation, appId, requestSession); if (!isCurrent()) return false; if (!intent?.committed || !intent.intent) throw new Error('Backup deletion preflight failed'); @@ -1310,8 +1455,8 @@ export function* setServerBackupFailed(expectedAppId?: string) { payload: { uaiType: uaiType.SERVER_BACKUP_FAILURE, uaiDetails: { - heading: 'Assisted Server Backup Has Failed', - body: "Retry backup to Keeper's servers", + heading: translations.recoveryBackup.retryNotificationTitle, + body: translations.recoveryBackup.retryNotificationBody, }, }, }); diff --git a/src/store/store.ts b/src/store/store.ts index 8907b223d4..d5d5fd8e14 100644 --- a/src/store/store.ts +++ b/src/store/store.ts @@ -12,6 +12,7 @@ import { backupConsentEpoch, observeBackupConsentAction, protectBackupConsentCompletion, + takeBackupRecoveryConsentDisable, currentBackupAccountSession, markBackupMutation, observeBackupAccount, @@ -23,6 +24,7 @@ import { noteCommittedBackupRecovery, setAutomaticBackupVerificationCommit, } from 'src/services/backup/automatic'; +import { setProtectedBackupStateObserver, setProtectedBackupStateProvider } from 'src/services/backup/protected'; import { KeeperApp } from 'src/models/interfaces/KeeperApp'; import loginReducer from './reducers/login'; import notificationsReducer from './reducers/notifications'; @@ -33,6 +35,7 @@ import bhrReducer, { commitBackupInspection, setAutomaticCloudBackup, invalidateRehydratedBackupState, + setProtectedBackupState, } from './reducers/bhr'; import rootSaga from './sagas'; import sendAndReceiveReducer from './reducers/send_and_receive'; @@ -199,9 +202,10 @@ const bindActionOrigin = (api) => { const choiceAppId = action.payload?.appId || action.appId || action.originAppId || originAppId; const disablesConsent = action.type === 'bhr/setAutomaticCloudBackup' && action.payload?.enabled === false; + const recoveryDisable = disablesConsent && takeBackupRecoveryConsentDisable(action, choiceAppId); if ( !choiceAppId || - choiceAppId !== originAppId || + (choiceAppId !== originAppId && !recoveryDisable) || (!disablesConsent && !canAccessBackupAccount(choiceAppId, true)) || !observeBackupConsentAction(action, choiceAppId) ) @@ -279,6 +283,15 @@ export const store = configureStore({ observeBackupAccount(store.getState().storage.appId); store.subscribe(() => observeBackupAccount(store.getState().storage.appId)); +setProtectedBackupStateProvider(appId => store.getState().bhr.protectedBackupStateByAppId?.[appId]); +setProtectedBackupStateObserver((appId, session, state) => { + if (store.getState().storage.appId !== appId || session !== currentBackupAccountSession()) return; + try { + if ((dbManager.getObjectByIndex(RealmSchema.KeeperApp) as unknown as KeeperApp)?.id !== appId) return; + store.dispatch(setProtectedBackupState({appId, state})); + } catch { /* A closed or switched Realm cannot project remote status. */ } +}); + setBackupUploadGuard((appId, explicitChoice) => { const state = store.getState(); diff --git a/tests/recovery/accountBinding.test.cjs b/tests/recovery/accountBinding.test.cjs index c0ecfaa32d..befac795e8 100644 --- a/tests/recovery/accountBinding.test.cjs +++ b/tests/recovery/accountBinding.test.cjs @@ -151,15 +151,17 @@ test('verified A backup completion after selected B switch cannot enable A or re test('dispatch gate drops stale enable and success actions while preserving account-specific resets', () => { let session = 1; + const consentTransport = loadTransportModule('src/services/backup/transport.ts', { + '../rest/RestClient': {}, + }); const scope = { Set, RealmSchema: { KeeperApp: 'KeeperApp' }, dbManager: { getObjectByIndex: () => ({ id: realmAppId }) }, currentBackupAccountSession: () => session, canAccessBackupAccount: (appId) => appId === selectedAppId && appId === realmAppId, - observeBackupConsentAction: loadTransportModule('src/services/backup/transport.ts', { - '../rest/RestClient': {}, - }).observeBackupConsentAction, + observeBackupConsentAction: consentTransport.observeBackupConsentAction, + takeBackupRecoveryConsentDisable: consentTransport.takeBackupRecoveryConsentDisable, captureBackupWriteIntent: (appId) => appId === selectedAppId ? { appId, accountSession: session, baseRevision: 'a'.repeat(64), baselineEpoch: 1, } : undefined, diff --git a/tests/recovery/automaticRecoveryRestore.integration.cjs b/tests/recovery/automaticRecoveryRestore.integration.cjs index 190cafebfe..c55317b9ab 100644 --- a/tests/recovery/automaticRecoveryRestore.integration.cjs +++ b/tests/recovery/automaticRecoveryRestore.integration.cjs @@ -1,5 +1,7 @@ // Clean Recovery Key restore through the actual production recovery saga, real // BIP39/BIP32/BIP85/AES, a blank encrypted native Realm and real Relay HTTP reads. +// This historical public-Relay lane explicitly selects the older read-only copy; +// protected-default recovery has a separate exact-private-Relay proof lane. const assert = require('node:assert/strict'); const path = require('node:path'); const crypto = require('node:crypto'); @@ -18,7 +20,7 @@ after(async () => { await server?.close(); Realm.shutdown(); }); function cryptoAndRecovery(f) { const saga = f.productionSagas([ - 'currentBackupAppId', 'recoverApp', 'writeRecoveredObject', 'getAppImageWorker', + 'currentBackupAppId', 'recoverApp', 'writeRecoveredObject', 'getAppImageWorker', 'latestRecoveryReadRequest', 'buildAutomaticAppMutation', 'buildAutomaticVaultMutation', 'queueCommittedBackupWorker', 'updateAppImageWorker', ], { ...f.load('src/store/reducers/wallets'), ...f.load('src/store/reducers/concierge'), @@ -28,7 +30,8 @@ function cryptoAndRecovery(f) { ...f.load('src/models/enums/Hardware'), HWError: f.load('src/hardware/HWErrorState').default, ...f.utilities, _: require('lodash'), crypto, bip39, semver: require('semver'), moment: require('moment'), config: f.config, DeviceInfo: { getVersion: () => '2.6.4', getBuildNumber: () => '627' }, - prepareRecoveryImage: f.load('src/services/backup/restore').prepareRecoveryImage, + ...f.load('src/services/backup/restore'), + readProtectedBackup: f.load('src/services/backup/protected').readProtectedBackup, // Metro normally expands idx at build time; this host adapter performs the // same guarded field access, never supplies database or crypto results. idx(value, getter) { try { return getter(value); } catch (error) { if (error.name === 'TypeError') return undefined; throw error; } }, @@ -168,7 +171,8 @@ test('actual Recovery Key worker restores real crypto and every retained recover f.store.dispatch(f.account.setTempDetails({ hash: f.encryption.hash256('public-disposable-passcode-fixture'), realmId: path.join(f.directory, 'clean-restore.realm'), accountIdentifier: 'disposable restore' })); server.clearTrace(); - await saga.run('getAppImageWorker', { payload: { primaryMnemonic: mnemonic } }); + await saga.run('getAppImageWorker', { payload: { primaryMnemonic: mnemonic, + selection: { source: 'legacy-older', olderCopyConfirmed: true } } }); assert.equal(f.store.getState().storage.appCreated, true, `Production recovery failed: ${f.store.getState().bhr.appImageError || 'no diagnostic'}`); assert.equal(f.store.getState().storage.appId, appId); assert.equal(f.store.getState().bhr.appImageError, ''); diff --git a/tests/recovery/automaticWriteRealmRelay.fixture.cjs b/tests/recovery/automaticWriteRealmRelay.fixture.cjs index d5853bb89c..e7c6ae9590 100644 --- a/tests/recovery/automaticWriteRealmRelay.fixture.cjs +++ b/tests/recovery/automaticWriteRealmRelay.fixture.cjs @@ -54,7 +54,7 @@ function evaluate(source, globals, file) { // Native Realm checks JavaScript constructor identity for Mixed values. // JSON must share the host that owns the genuine Realm binding, as Metro // production code and Realm do; no decoded fields are rewritten here. - JSON, Date, Buffer, Uint8Array, Int8Array, ArrayBuffer, Promise, Set, Map, AbortController, setTimeout, clearTimeout, setImmediate, clearImmediate, + JSON, Date, Buffer, Uint8Array, Int8Array, ArrayBuffer, Promise, Set, Map, URL, URLSearchParams, AbortController, setTimeout, clearTimeout, setImmediate, clearImmediate, console, Realm, process, }; for (const [name, value] of Object.entries(defaults)) if (!Object.hasOwn(globals, name)) globals[name] = value; @@ -63,6 +63,17 @@ function evaluate(source, globals, file) { function sourceLoader(bindings, options = {}) { const cache = new Map(); + // The separate protected proof imports these exact installed ESM namespaces + // asynchronously before this synchronous Metro-style production loader runs. + // Authority/proof/encryption modules themselves cannot be replaced here. + const esmPackages = new Set(['@scure/bip32', '@scure/bip39', + '@scure/bip39/wordlists/english.js', '@noble/hashes/hkdf.js', + '@noble/hashes/sha2.js', '@noble/hashes/utils.js', '@noble/curves/secp256k1.js', + '@noble/curves/utils.js', '@exodus/bytes/encoding-lite.js']); + for (const [name, namespace] of Object.entries(options.moduleBindings || {})) { + assert.ok(esmPackages.has(name), 'Only real external protected-crypto ESM packages may be supplied'); + assert.equal(Object.prototype.toString.call(namespace), '[object Module]', 'Supply an actual imported ESM namespace'); + } function load(file) { const absolute = path.isAbsolute(file) ? file : path.join(root, file); const candidate = [absolute, `${absolute}.ts`, `${absolute}.tsx`, `${absolute}.js`, path.join(absolute, 'index.ts')].find((name) => fs.existsSync(name) && fs.statSync(name).isFile()); @@ -77,6 +88,7 @@ function sourceLoader(bindings, options = {}) { ...(options.backupTimerCeilingMs ? { setTimeout: (callback, milliseconds, ...args) => setTimeout(callback, Math.min(milliseconds, options.backupTimerCeilingMs), ...args) } : {}), require(name) { + if (Object.hasOwn(options.moduleBindings || {}, name)) return options.moduleBindings[name]; if (Object.hasOwn(bindings, name)) return bindings[name]; if (name.startsWith('src/')) return load(name); if (name.startsWith('.')) { @@ -122,10 +134,14 @@ async function createRealmClient(url, options = {}) { const bindings = { realm: Realm, 'src/services/sentry': { captureError(error) { diagnostics.push(error?.name || 'error'); } }, + // Localization is a host UI boundary; use the actual shipped English data + // without bootstrapping React Native contexts in this Node source fixture. + 'src/context/Localization/LocContext': { translations: require('../../src/context/Localization/language/en.json') }, 'react-native': { Platform: { OS: 'fixture-host' } }, 'react-native-device-info': { getVersion: () => '2.6.4', getBuildNumber: () => '627' }, 'react-native-rsa-native': { RSA: new Proxy({}, { get: () => () => { throw new Error('Unexpected native RSA use'); } }) }, - 'src/utils/service-utilities/config': { RELAY: url, HEXA_ID: 'disposable-fixture', ENVIRONMENT: 'DEVELOPMENT', isDevMode: () => true }, + 'src/utils/service-utilities/config': { ...options.config, + RELAY: url, HEXA_ID: 'disposable-fixture', ENVIRONMENT: 'DEVELOPMENT', isDevMode: () => true }, 'src/storage/realm/migrations': { runRealmMigrations() { throw new Error('A fresh fixture must never migrate an existing Realm'); } }, 'src/storage': { reduxStorage }, 'redux-persist/es/persistReducer': require('redux-persist/lib/persistReducer').default, @@ -277,11 +293,12 @@ async function createRealmClient(url, options = {}) { const source = ts.createSourceFile('store.ts', fs.readFileSync(path.join(root, 'src/store/store.ts'), 'utf8'), ts.ScriptTarget.Latest, true); const statements = source.statements.filter((statement) => { const text = statement.getText(source); - return /^(observeBackupAccount\(|store\.subscribe\(|setBackupUploadGuard\(|setBackupReconciliationHandler\(|dbManager\.setRecovery(?:Mutation|Commit)Observer\(|set(?:Backup|AutomaticBackup)\w*\()/.test(text); + return /^(observeBackupAccount\(|store\.subscribe\(|setBackupUploadGuard\(|setBackupReconciliationHandler\(|dbManager\.setRecovery(?:Mutation|Commit)Observer\(|set(?:Backup|AutomaticBackup|ProtectedBackup)\w*\()/.test(text); }); assert.ok(statements.length >= 5, 'Production account/local mutation hooks are required'); evaluate(statements.map((statement) => statement.getText(source)).join('\n'), { - store, dbManager, RealmSchema, ...transport, ...bhr, ...repair, ...extra, + store, dbManager, RealmSchema, ...transport, ...bhr, ...repair, + ...load('src/services/backup/protected'), ...extra, }, 'production-store-observers.ts'); } @@ -296,6 +313,8 @@ async function createRealmClient(url, options = {}) { const scope = { ...require('redux-saga'), ...effects, ...encryption, ...enums, ...image, ...transport, ...bhr, ...storage, ...account, ...load('src/services/backup/automatic'), + ...load('src/services/backup/protected'), + ...bindings['src/context/Localization/LocContext'], ...utilitiesScope, dbManager, realm: realmDatabase, Realm, RealmSchema, store, Relay, inspectBackup: repair.inspectBackup, captureError: bindings['src/services/sentry'].captureError, getJSONFromRealmObject: (object) => object.toJSON(), diff --git a/tests/recovery/backupFeedback.test.cjs b/tests/recovery/backupFeedback.test.cjs index 1ab803ea17..2c824c71fa 100644 --- a/tests/recovery/backupFeedback.test.cjs +++ b/tests/recovery/backupFeedback.test.cjs @@ -10,7 +10,8 @@ test('opening Recovery Key after restore does not replay health-check consent or const f = fixture({ bhr: { automaticCloudBackup: false, seedConfirmed: true } }); const key = encryption.generateEncryptionKey(f.scope.bip39.mnemonicToSeedSync().toString('hex')); f.remote.wallets.local = encryption.encrypt(key, JSON.stringify(wallet('local', enums.NetworkType.MAINNET))); - await f.run('getAppImageWorker', { payload: { primaryMnemonic: 'mock fixture only' } }); + await f.run('getAppImageWorker', { payload: { primaryMnemonic: 'mock fixture only', + selection: { source: 'legacy-older', olderCopyConfirmed: true } } }); assert.equal(f.state.account.recoveryKeyStatusByAppId[f.app.id], 'confirmed'); const signal = f.actions.filter((a) => a.type === 'setSeedConfirmed').at(-1); assert.equal(signal?.payload, false, 'restore must clear the transient health-check signal'); diff --git a/tests/recovery/backupFreshness.test.cjs b/tests/recovery/backupFreshness.test.cjs index 3e724851de..d59d6ea0b9 100644 --- a/tests/recovery/backupFreshness.test.cjs +++ b/tests/recovery/backupFreshness.test.cjs @@ -347,7 +347,8 @@ for (const networks of [ const w = wallet(network, network); f.remote.wallets[w.id] = encryption.encrypt(key, JSON.stringify(w)); }); - await f.run('getAppImageWorker', { payload: { primaryMnemonic: 'mock fixture only' } }); + await f.run('getAppImageWorker', { payload: { primaryMnemonic: 'mock fixture only', + selection: { source: 'legacy-older', olderCopyConfirmed: true } } }); assert.deepEqual( f.actions.filter((a) => a.type === 'setAppImageError' && a.payload), [] @@ -368,7 +369,8 @@ test('recovery upgrade does not bypass per-account backup consent', async () => JSON.stringify(wallet('disposable', NetworkType.MAINNET)) ); - await f.run('getAppImageWorker', { payload: { primaryMnemonic: 'mock fixture only' } }); + await f.run('getAppImageWorker', { payload: { primaryMnemonic: 'mock fixture only', + selection: { source: 'legacy-older', olderCopyConfirmed: true } } }); assert.deepEqual(f.actions.filter((action) => action.type === 'setAppImageError' && action.payload), []); assert.equal(f.calls.filter(([type, payload]) => type === 'incremental' && payload.version).length, 0); diff --git a/tests/recovery/backupRehydration.test.cjs b/tests/recovery/backupRehydration.test.cjs index e2c26f7e17..52fd0bb1e1 100644 --- a/tests/recovery/backupRehydration.test.cjs +++ b/tests/recovery/backupRehydration.test.cjs @@ -35,6 +35,9 @@ async function setup(t, { nested, legacy, guarded = true } = {}) { }); const storage = loadModule('src/store/reducers/storage.ts', { 'src/services/wallets/enums': enums }); const proofPredicate = new Set(); + const { takeBackupRecoveryConsentDisable } = loadModule('src/services/backup/transport.ts', { + '../rest/RestClient': {}, + }); const actions = [], snapshots = []; let store; const scope = { @@ -50,6 +53,7 @@ async function setup(t, { nested, legacy, guarded = true } = {}) { captureBackupWriteIntent: () => undefined, canAccessBackupAccount: () => false, observeBackupConsentAction: (_action, appId) => store.getState().storage.appId === appId, + takeBackupRecoveryConsentDisable, isBackupVerificationCurrent: (appId) => proofPredicate.has(appId) && store.getState().storage.appId === appId, }; diff --git a/tests/recovery/helpers.cjs b/tests/recovery/helpers.cjs index d3f3ef7eee..94ba698243 100644 --- a/tests/recovery/helpers.cjs +++ b/tests/recovery/helpers.cjs @@ -32,6 +32,82 @@ const image = loadModule('src/services/backup/image.ts', { 'src/utils/service-utilities/encryption': encryption, }); const makeImage = () => ({ wallets: {}, signers: {}, vaults: {}, nodes: {}, labels: {} }); +// UNIT-ONLY authenticated-transport boundary for historical kernel schedules. +// The old fake POST fixture supplies ciphertext/revisions; this facade models +// namespace/generation/epoch and ACK binding, NOT signing, enrollment or Mongo. +// Production readiness constants and the separate genuine v3 lane stay real. +function unitProtectedFacade(transport, app) { + let generation = 0, authEpoch = 0, previousRevision; + const namespace = () => `unit-protected-${app.id}`; + const validRevision = value => typeof value === 'string' && /^[a-f0-9]{64}$/.test(value); + const fail = () => { throw new transport.BackupRequestError('protocol'); }; + const adopt = revision => { + if (!validRevision(revision)) fail(); + if (previousRevision && previousRevision !== revision) generation++; + previousRevision = revision; + }; + const protectedBackupBase = snapshot => { + if (!validRevision(snapshot?.revision) || typeof snapshot.namespaceId !== 'string' || + !Number.isSafeInteger(snapshot.generation) || !Number.isSafeInteger(snapshot.authEpoch)) fail(); + return Object.freeze({ namespaceId: snapshot.namespaceId, revision: snapshot.revision, + generation: snapshot.generation, authEpoch: snapshot.authEpoch }); + }; + const readProtectedBackup = async (_mnemonic, deadline, _appId, checkpoint = () => {}) => { + checkpoint(); + const response = await transport.boundedBackupPost('fixture/getBackupSnapshot', { appId: app.id }, deadline); + checkpoint(); + const raw = JSON.parse(JSON.stringify(response.data)); + if (raw?.revisionedMutations !== true) fail(); + adopt(raw.revision); + const namespaceId = namespace(); + // Normalize only the expected fixture identity; foreign IDs remain invalid. + if (raw.appImage?.appId === app.id) raw.appImage.appId = namespaceId; + if (Array.isArray(raw.allVaultImages)) raw.allVaultImages.forEach(vault => { + if (vault.appId === undefined || vault.appId === app.id) vault.appId = namespaceId; + // Complete only omitted projection fields in normal old unit records. + // Explicit mismatches remain untouched for the production validator. + try { + const decoded = JSON.parse(encryption.decrypt(encryption.generateEncryptionKey(app.primarySeed), vault.vault)); + if (Array.isArray(decoded.signers)) { + const references = decoded.signers.map(signer => ({ signerId: signer.id || signer.masterFingerprint, + xfpHash: encryption.hash256(signer.masterFingerprint) })); + if (vault.signersData === undefined) vault.signersData = references; + if (vault.signerIds === undefined) vault.signerIds = references.map(signer => signer.signerId); + if (vault.scheme === undefined) vault.scheme = decoded.scheme; + if (vault.isArchived === undefined) vault.isArchived = !!decoded.archived; + } + } catch { /* Invalid ciphertext remains subject to production decode. */ } + }); + return { state: 'present', namespaceId, snapshot: { + ...raw, namespaceId, generation, authEpoch, scope: 'protected-backup', exists: true, + deleted: false, suppressLegacyFallback: false, + backupRevisionRequired: true, incrementalRevisionRequired: true, + } }; + }; + return { + protectedBackupBase, + sameProtectedBackupBase: (a, b) => !!a && !!b && a.namespaceId === b.namespaceId && + a.revision === b.revision && a.generation === b.generation && a.authEpoch === b.authEpoch, + readProtectedBackup, + enrollProtectedBackup: async () => { throw new Error('Enrollment requires the genuine protected fixture'); }, + assertProtectedBackupWriteReady: () => {}, // Explicit mocked unit boundary only. + protectedBackupMutation: async (_mnemonic, base, operation, body, deadline, checkpoint = () => {}) => { + checkpoint(); + if (base.namespaceId !== namespace() || base.revision !== previousRevision || + base.generation !== generation || base.authEpoch !== authEpoch) fail(); + const response = await transport.boundedBackupPost('fixture/revisionedBackupMutation', + { ...body, operation, expectedRevision: base.revision }, deadline); + checkpoint(); + const reply = response.data; + if (reply?.updated !== true || reply.preconditionApplied !== true || + !validRevision(reply.newRevision) || reply.newRevision === base.revision) fail(); + adopt(reply.newRevision); + if (operation === 'deleteBackup') authEpoch++; + return { ...reply, base: protectedBackupBase({ namespaceId: namespace(), + revision: reply.newRevision, generation, authEpoch }) }; + }, + }; +} function harness(options = {}) { const app = { id: 'disposable', @@ -147,6 +223,7 @@ function harness(options = {}) { 'src/utils/service-utilities/encryption': encryption, 'src/utils/utilities': utilities, './transport': transport, + './protected': unitProtectedFacade(transport, app), './image': image, './localSnapshot': localSnapshot, }, @@ -177,4 +254,4 @@ const wallet = (id = 'wallet', networkType = 'MAINNET') => ({ transactions: [], }, }); -module.exports = { loadModule, encryption, image, makeImage, harness, wallet }; +module.exports = { loadModule, encryption, image, makeImage, harness, wallet, unitProtectedFacade }; diff --git a/tests/recovery/protected-authority-evidence.json b/tests/recovery/protected-authority-evidence.json new file mode 100644 index 0000000000..9008bb86ed --- /dev/null +++ b/tests/recovery/protected-authority-evidence.json @@ -0,0 +1,290 @@ +{ + "state": "SOURCE_VECTORS_METRO_VM_AND_HERMES_COMPILE_ONLY_NOT_NATIVE_ACCEPTANCE", + "date_utc": "2026-10-16", + "packages": { + "@scure/bip32": { + "version": "2.4.0", + "registry": "https://registry.npmjs.org/@scure%2Fbip32/2.4.0", + "registry_integrity": "sha512-i3DS0CptAocyvqE4n3SUkpzeQK4vJMFwWLofTwRiiKo2aWojBOfyMCgfKw9HVpO6fSY5AK86sHS/Uzn8kK9Few==", + "registry_shasum": "4b13d0e5f60f776f17cbf5528893ee03839acb6a", + "engines": null, + "dependencies": { + "@scure/base": "2.4.0", + "@noble/curves": "2.4.0", + "@noble/hashes": "2.4.0" + }, + "policy_url": "https://raw.githubusercontent.com/paulmillr/scure-bip32/main/SECURITY.md", + "policy_sha256": "0b2bc315d26fce709fc99643603c50d97aa26e969b3d9b7f11ca15d3e045196d", + "policy_supports_at_least_2_4_0": true, + "advisory_query": "https://api.github.com/advisories?ecosystem=npm&affects=%40scure%2Fbip32&per_page=100", + "returned_advisory_ids": [], + "root_installed_package_version": "2.4.0" + }, + "@scure/bip39": { + "version": "2.4.0", + "registry": "https://registry.npmjs.org/@scure%2Fbip39/2.4.0", + "registry_integrity": "sha512-82dxFbZUYboyOf0AXiydsQrFQ5Q4h9mX+O2UkE91ROYmsc0BKMGZLwDmy96Jpa2+vrtoxomjUhy1RPIgH/r2nA==", + "registry_shasum": "7b19a76d4dd2f4591c07b446a0f9695f9b1d9545", + "engines": null, + "dependencies": { + "@noble/hashes": "2.4.0" + }, + "policy_url": "https://raw.githubusercontent.com/paulmillr/scure-bip39/main/SECURITY.md", + "policy_sha256": "0b2bc315d26fce709fc99643603c50d97aa26e969b3d9b7f11ca15d3e045196d", + "policy_supports_at_least_2_4_0": true, + "advisory_query": "https://api.github.com/advisories?ecosystem=npm&affects=%40scure%2Fbip39&per_page=100", + "returned_advisory_ids": [], + "root_installed_package_version": "2.4.0" + }, + "@noble/curves": { + "version": "2.4.0", + "registry": "https://registry.npmjs.org/@noble%2Fcurves/2.4.0", + "registry_integrity": "sha512-P4/62zrgfH33CneE3Dn4WhJVA22YUU0eR51wKIan4NVRvwsA0YnPTwWGpNbpuacSujmSFLvyzpyuR30+fbq2Ew==", + "registry_shasum": "367c869bfd790c3a2248152562f255b5f8898b74", + "engines": { + "node": ">= 20.19.0" + }, + "dependencies": { + "@noble/hashes": "2.4.0" + }, + "policy_url": "https://raw.githubusercontent.com/paulmillr/noble-curves/main/SECURITY.md", + "policy_sha256": "d29d5589c25baaf86f521f6a4ef7193d4bcb5b643c3be8e16415ca3fb88b8d44", + "policy_supports_at_least_2_4_0": true, + "advisory_query": "https://api.github.com/advisories?ecosystem=npm&affects=%40noble%2Fcurves&per_page=100", + "returned_advisory_ids": [], + "root_installed_package_version": "2.4.0" + }, + "@noble/hashes": { + "version": "2.4.0", + "registry": "https://registry.npmjs.org/@noble%2Fhashes/2.4.0", + "registry_integrity": "sha512-X5XaVWZIBCT7HHZGm5I7ZQXDwLG+bGXuSrMQAW+7Zvl87h1kmc1ZB1VSRJcpUfoUrGQp4Fkoxm5kZ+Ms+aW+eA==", + "registry_shasum": "e6a3e98edbed3c8659b28bb8f9b18ca02ec8ea4b", + "engines": { + "node": ">= 20.19.0" + }, + "dependencies": null, + "policy_url": "https://raw.githubusercontent.com/paulmillr/noble-hashes/main/SECURITY.md", + "policy_sha256": "0b2bc315d26fce709fc99643603c50d97aa26e969b3d9b7f11ca15d3e045196d", + "policy_supports_at_least_2_4_0": true, + "advisory_query": "https://api.github.com/advisories?ecosystem=npm&affects=%40noble%2Fhashes&per_page=100", + "returned_advisory_ids": [], + "root_installed_package_version": "2.4.0" + }, + "@scure/base": { + "version": "2.4.0", + "registry": "https://registry.npmjs.org/@scure%2Fbase/2.4.0", + "registry_integrity": "sha512-thZ1TuJwFwBblOhgsjDKvvGirBxNp+wSvY/DR6tJBJOTDhdAAcHJ8Vbr2eFnqaxeca4+t0i9KBf+uHYGWwZORg==", + "registry_shasum": "61e4ed7d4447afc247f2bff9aeb8fca53267db86", + "engines": null, + "dependencies": null, + "policy_url": "https://raw.githubusercontent.com/paulmillr/scure-base/main/SECURITY.md", + "policy_sha256": "0b2bc315d26fce709fc99643603c50d97aa26e969b3d9b7f11ca15d3e045196d", + "policy_supports_at_least_2_4_0": true, + "advisory_query": "https://api.github.com/advisories?ecosystem=npm&affects=%40scure%2Fbase&per_page=100", + "returned_advisory_ids": [], + "root_installed_package_version": "1.1.9", + "authority_resolution": "node_modules/@scure/bip32/node_modules/@scure/base@2.4.0; root1.1.9 retained for existing QR callers" + }, + "@exodus/bytes": { + "version": "1.16.0", + "registry": "https://registry.npmjs.org/@exodus%2Fbytes/1.16.0", + "registry_integrity": "sha512-IcpW84uEn3N7ETtNZMlxKhfl6Pec8rUNGOTBtWbK1FKhJxIFAptZyVrvVRVBimAJxJCgc3PxepxkdWWG4DVzfA==", + "registry_tarball_integrity_verified": true, + "primary_repository": "https://github.com/ExodusOSS/bytes", + "published_utc": "2026-09-22T13:26:14.694Z", + "engines": { + "node": "^20.19.0 || ^22.12.0 || >=24.0.0" + }, + "dependencies": null, + "public_subpath_used": "@exodus/bytes/encoding-lite.js", + "purpose": "Install standard TextEncoder only when absent; never installs TextDecoder or replaces an existing encoder", + "primary_readme": "https://github.com/ExodusOSS/bytes/blob/main/README.md", + "support_observation": "Repository active/nonarchived; README documents React Native/Hermes and encoding-lite. No SECURITY.md at the checked primary path (HTTP404); no security-support policy or audit is claimed.", + "advisory_query": "https://api.github.com/advisories?ecosystem=npm&affects=%40exodus%2Fbytes&per_page=100", + "returned_advisory_ids": [] + } + }, + "package_install_owner": "root; no package/yarn/install mutation by authority agent", + "source_files": { + "src/services/backup/authority.ts": { + "bytes": 8146, + "sha256": "7745c6d3766eb5b0d57b415b846561fd8cc08e9b6934d92f128ad9f76327b121" + }, + "src/services/backup/encoding.ts": { + "bytes": 433, + "sha256": "399df9301a181ece2636df9f8a1b597ccc5de73808ca591d9a51f2c8385deafa" + }, + "index.js": { + "bytes": 1062, + "sha256": "779fc43c38677a00a1da4eaeb66856833276003a736266f505e6dc989f763088" + }, + "tests/recovery/protected-authority.test.mjs": { + "bytes": 12516, + "sha256": "78a7225ad5c95a2af4553635c3e76e9719754028c4bd804f2c5a84707bb3f509" + }, + "tests/recovery/protected-authority.metro.cjs": { + "bytes": 9267, + "sha256": "257f5e3e8030c0cf6230b6f9991478f1ab97853b67c3b7403474f20e521c5b0a" + }, + "tests/recovery/support/protected-authority-esm-loader.mjs": { + "bytes": 2076, + "sha256": "be1d84fbde89f5a753876298179416ac8c377b3079df2ff21df95cccee998494" + }, + "tests/recovery/support/react-native-encoding-loader.mjs": { + "bytes": 531, + "sha256": "80c6abf2a0ea27f148e90a53fe37a4d4526007bff88e8965bcce930bb9f4d829" + }, + "tests/recovery/protected-authority.md": { + "bytes": 2455, + "sha256": "cb05cb83ebd6cfa66f1fc791d2629a136d3ffa615f9993f77f8131ff058242eb" + } + }, + "actual_checks": { + "authority_vectors": { + "command": "node --test tests/recovery/protected-authority.test.mjs", + "pass": 13, + "fail": 0, + "skipped": 0, + "node": "22.23.3", + "openssl": "3.5.8", + "log": { + "bytes": 3212, + "sha256": "a306603456f1e496bc30dc25a8e245624cb10153cb8f86175b6772d3c2fe20df" + }, + "module_mode": "actual production TS transpiled ES2022 ESM; actual installed crypto imports; no algorithm substitution", + "absent_globals": "test-only ESM resolver removes Node condition for @exodus/bytes so actual mobile/default implementation is used; independently checked by actual Metro graph" + }, + "focused_eslint": { + "command": "node node_modules/eslint/bin/eslint.js src/services/backup/authority.ts src/services/backup/encoding.ts", + "exit_code": 0, + "errors": 0, + "warnings": 0, + "log": { + "bytes": 0, + "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" + } + }, + "focused_metro": { + "command": "node tests/recovery/protected-authority.metro.cjs", + "node": "22.23.3", + "metro_version": "0.83.6", + "platform": "android", + "modules": 83, + "pass": true, + "app_index_order": "Actual index.js import order checked; focused entry imports real shim, encoding and authority; full index/App startup NOT RUN", + "native_boundaries": "shim net/tls/crypto imports replaced by throwing traps; native InitializeCore excluded", + "js_execution": "actual Android Metro bundle executed in Node V8 VM without TextEncoder/TextDecoder; encoder-only installation, UTF8/encodeInto boundaries, fixed namespace, raw digest and OpenSSL signature verified", + "hermes_compile": "PASS, installed compiler reports release0.12.0/HBC96; not execution", + "hermes_runtime_execution": "NOT RUN (compiler rejected -exec)", + "log": { + "bytes": 269, + "sha256": "c522086668239d63ef2d008d788052cf1f1d6bdd3f5fd0e35f3e7c3e9c6b0882" + }, + "receipt": { + "bytes": 9869, + "sha256": "8c81d21c14b851a97b9b2ccc9fe9ef9ef164da65095169997f198e01e45cad44" + }, + "bundle": { + "bytes": 785652, + "sha256": "25bb56d581d47f48caf0fc25a510fdf2497f7d5e864601caac57ceb6c3ae7f34" + }, + "hbc": { + "bytes": 504455, + "sha256": "c633bf481c26dc206682f6b896c48d44ad6e0aa240abe7f2bbc512c27d818d16" + } + } + }, + "standard_vectors": [ + "RFC5869 SHA256 case1", + "Trezor python-mnemonic first English vector with TREZOR passphrase (primitive-only); production authority explicitly no passphrase", + "BIP32 official vector1 master and hardened-zero xpub", + "Full production no-passphrase namespace matches backend158 fixed fixture bk3_7SolLs5PrxK2ybpJGsah8spJqewmCskdDW_L9gnI2FY" + ], + "limits": [ + "Dated primary metadata/advisory observations are not security certification; no SECURITY.md support policy was found for @exodus/bytes.", + "ESM child uses a disclosed mobile-resolution test hook; real Metro independently selects actual default/mobile encoding sources.", + "Metro proof executes in Node V8 and compiles Hermes bytecode; neither Hermes runtime execution nor full native App startup was run.", + "IOS / ANDROID_NATIVE / HARDWARE NOT RUN. Native performance, RNG initialization, real Hermes and recovery acceptance remain Local obligations.", + "No production/provider/real wallet calls; no native install, release build, signing, upload, gate activation or dependency installer executed by authority agent.", + "Existing wallet crypto and root @scure/base1.1.9 remain separate; supported new authority uses actual2.4.0 family and nested base2.4.0.", + "Public descriptor only; HDKey/private material never escapes closure. Disposal and wiping are best effort in JavaScript." + ], + "rejected_encoder_input": { + "name": "text-encoding", + "version": "0.7.0", + "registry": "https://registry.npmjs.org/text-encoding/0.7.0", + "primary_repository": "https://github.com/inexorabletash/text-encoding", + "observation": "Registry deprecated as no longer maintained; upstream archived. Existing transitive install was not adopted for new authority." + }, + "initial_observations": [ + { + "stage": "Initial isolated ESM vector lane", + "result": "11/11 PASS before original-account binding and encoder checks were added" + }, + { + "stage": "First absent-text-globals child", + "result": "12/13 PASS,1FAIL: Node selected its own utf8.node.js exports branch, which requires native TextDecoder; not a demonstrated mobile-runtime defect. Harness now selects actual mobile/default package exports, without installing a Decoder." + }, + { + "stage": "First Metro harness", + "result": "Metro emitted authority.android.bundle.js; harness attempted a different filename and failed ENOENT before VM execution. Harness now writes the returned Metro code explicitly." + }, + { + "stage": "Hermes runtime attempt", + "result": "Installed hermesc rejected -exec; no Hermes runtime execution claimed." + }, + { + "stage": "Final source binding guard", + "result": "Independent review found uppercase stored seed text would encrypt differently from lowercase clean restore. Authority now rejects it; no normalization/rewrite of existing state." + } + ], + "dependency_size": { + "scope": "Six installed package directories, each excluding nested node_modules to avoid duplication; not full install growth or APK/AAB delta", + "packages": [ + { + "path": "node_modules/@scure/bip32", + "version": "2.4.0", + "files_excluding_nested_node_modules": 6, + "logical_bytes": 38750, + "allocated_bytes": 53248 + }, + { + "path": "node_modules/@scure/bip39", + "version": "2.4.0", + "files_excluding_nested_node_modules": 26, + "logical_bytes": 212981, + "allocated_bytes": 286720 + }, + { + "path": "node_modules/@noble/curves", + "version": "2.4.0", + "files_excluding_nested_node_modules": 72, + "logical_bytes": 1590210, + "allocated_bytes": 1736704 + }, + { + "path": "node_modules/@noble/hashes", + "version": "2.4.0", + "files_excluding_nested_node_modules": 60, + "logical_bytes": 691646, + "allocated_bytes": 823296 + }, + { + "path": "node_modules/@scure/bip32/node_modules/@scure/base", + "version": "2.4.0", + "files_excluding_nested_node_modules": 6, + "logical_bytes": 145274, + "allocated_bytes": 159744 + }, + { + "path": "node_modules/@exodus/bytes", + "version": "1.16.0", + "files_excluding_nested_node_modules": 79, + "logical_bytes": 435337, + "allocated_bytes": 634880 + } + ], + "total_logical_bytes": 3114198, + "total_allocated_bytes": 3694592 + } +} diff --git a/tests/recovery/protected-authority.md b/tests/recovery/protected-authority.md new file mode 100644 index 0000000000..5f3e724ca4 --- /dev/null +++ b/tests/recovery/protected-authority.md @@ -0,0 +1,40 @@ +Run these focused checks from the repository root with Node 22.23.3 (or a later +22.x version) after installing the frozen dependencies. The scripts enforce +that Node version; subprocesses use the same executable. + +```sh +node --test tests/recovery/protected-authority.test.mjs +node tests/recovery/protected-authority.metro.cjs +node node_modules/eslint/bin/eslint.js src/services/backup/authority.ts src/services/backup/encoding.ts +``` + +The first command transpiles the actual production authority and encoding +modules to temporary ES modules and loads the installed crypto packages without +algorithm substitutes. Its 13 tests cover standard BIP39/BIP32/HKDF vectors, +the fixed recoverable namespace, original account/seed binding (including +rejection of uppercase stored seed hex), low-S compact JWS interoperability +with native OpenSSL, raw-body binding, disposal and invalid inputs. One child +starts without TextEncoder or TextDecoder and selects the encoder package's +mobile/default exports with a test-only Node resolver hook. + +The second command uses the real Metro/Babel configuration and Android module +resolution. It validates the actual index import order and bundles the actual +shim, encoding and authority modules. Three shim native edges are replaced by +throwing traps; native InitializeCore and the full App are not loaded. The +bundle executes in an isolated Node V8 context without either text global, +checks encoder boundaries and the fixed namespace/signature, then compiles +with the repository's Linux or macOS Hermes compiler. The printed output +directory contains the receipt, bundle and bytecode. An optional first argument +sets that directory. Paths affect bundle hashes. Hermes execution, native +startup and native performance are not proved by compilation. + +The source-bound Cloud results and dated primary dependency/support/advisory +observations are in [protected-authority-evidence.json](protected-authority-evidence.json). +An empty advisory response is not a security certification. Installed dependency +sizes and this focused debug bundle are not an APK/AAB size estimate. + +IOS, ANDROID native and HARDWARE acceptance remain NOT RUN by these commands. +Local must rebuild/install/launch the actual new app on both platforms, verify +Hermes runtime initialization and recoverable signing, then run the scoped +backup/native acceptance handoff. This proof makes no provider or production +request and does not activate hosted backup. diff --git a/tests/recovery/protected-authority.metro.cjs b/tests/recovery/protected-authority.metro.cjs new file mode 100644 index 0000000000..113dfead30 --- /dev/null +++ b/tests/recovery/protected-authority.metro.cjs @@ -0,0 +1,140 @@ +// Focused real Metro Android graph / JS execution / Hermes compilation proof. +// Full App/native startup is NOT exercised. Only actual shim native boundaries +// are trapped; authority, encoder, Buffer and every new crypto module are real. +const assert = require('node:assert/strict'); +const crypto = require('node:crypto'); +const fs = require('node:fs'); +const os = require('node:os'); +const path = require('node:path'); +const vm = require('node:vm'); +const { spawnSync } = require('node:child_process'); +const Metro = require('metro'); +const ts = require('typescript'); +const root = path.resolve(__dirname, '../..'); +const output = process.argv[2] ? path.resolve(process.argv[2]) : fs.mkdtempSync(path.join(os.tmpdir(), 'keeper-authority-metro-')); +fs.mkdirSync(output, { recursive: true }); +const sha = (bytes) => crypto.createHash('sha256').update(bytes).digest('hex'); +const mnemonic = 'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about'; +const namespace = 'bk3_7SolLs5PrxK2ybpJGsah8spJqewmCskdDW_L9gnI2FY'; +const source = (name) => fs.readFileSync(path.join(root, name), 'utf8'); + +async function main() { + const [major, minor, patch] = process.versions.node.split('.').map(Number); + assert.ok(major === 22 && (minor > 23 || (minor === 23 && patch >= 3)), 'Run the proof with Node22.23.3 or a later22.x patch'); + const index = source('index.js'); + const tree = ts.createSourceFile('index.js', index, ts.ScriptTarget.Latest, true, ts.ScriptKind.JS); + const imports = tree.statements.filter(ts.isImportDeclaration).map((node) => node.moduleSpecifier.text); + assert.ok(imports.indexOf('./shim') < imports.indexOf('./src/services/backup/encoding')); + assert.ok(imports.indexOf('./src/services/backup/encoding') < imports.indexOf('./App')); + assert.ok(imports.indexOf('react-native-get-random-values') < imports.indexOf('./App')); + assert.ok(source('src/services/backup/authority.ts').startsWith("import './encoding';")); + const entry = path.join(output, 'entry.js'); + fs.writeFileSync(entry, ` + import ${JSON.stringify(path.join(root, 'shim.js'))}; + import ${JSON.stringify(path.join(root, 'src/services/backup/encoding.ts'))}; + import { deriveBackupAuthority } from ${JSON.stringify(path.join(root, 'src/services/backup/authority.ts'))}; + if (typeof TextEncoder !== 'function' || typeof TextDecoder !== 'undefined') throw new Error('ENCODER_SCOPE'); + const input = ['ASCII', 'A€😀', '\\ud800', '\\udc00', 'a\\ud800b', 'é', 'e\\u0301']; + const encoder = new TextEncoder(); + const encoded = input.map(value => Array.from(encoder.encode(value))); + const encodeInto = [0,1,2,3,4,5,6,7,8].map(size => { + const target = new Uint8Array(size); + const progress = encoder.encodeInto('A€😀', target); + return {size, progress, output:Array.from(target)}; + }); + const authority = deriveBackupAuthority(${JSON.stringify(mnemonic)}); + try { + if (authority.namespaceId !== ${JSON.stringify(namespace)}) throw new Error('NAMESPACE_VECTOR'); + const raw = Buffer.from(JSON.stringify({namespaceId:authority.namespaceId,publicJwk:authority.publicJwk})); + const token = authority.signRawBody(raw,'/backup/v3/snapshot','https://keeper-fixture.invalid',2000000000); + globalThis.__keeperAuthorityProof = {namespaceId:authority.namespaceId,publicJwk:authority.publicJwk, + token,raw:Array.from(raw),input,encoded,encodeInto,decoderInstalled:typeof TextDecoder!=='undefined'}; + } finally { authority.dispose(); } + `); + const traps = new Map(); + for (const name of ['net', 'tls', 'crypto']) { + const filename = path.join(output, `native-${name}-trap.js`); + fs.writeFileSync(filename, ` + (globalThis.__keeperLegacyTraps || (globalThis.__keeperLegacyTraps=[])).push(${JSON.stringify(name)}); + const forbidden = () => { throw new Error('LEGACY_NATIVE_BOUNDARY_FORBIDDEN'); }; + module.exports = Object.freeze({connect:forbidden,randomBytes:forbidden,createHash:forbidden}); + `); + traps.set(name, filename); + } + const modules = new Set(); + const config = await Metro.loadConfig({ cwd: root, config: path.join(root, 'metro.config.js') }); + const priorResolver = config.resolver.resolveRequest; + const priorFilter = config.serializer.processModuleFilter; + const build = { + ...config, maxWorkers: 1, watchFolders: [...config.watchFolders, output], + reporter: { update() {} }, + resolver: { ...config.resolver, resolveRequest(context, name, platform) { + if (context.originModulePath === path.join(root, 'shim.js')) { + const trap = name === './src/services/electrum/net' ? 'net' + : name === './src/services/electrum/tls' ? 'tls' : name === 'crypto' ? 'crypto' : null; + if (trap) return { type: 'sourceFile', filePath: traps.get(trap) }; + } + return priorResolver ? priorResolver(context, name, platform) : context.resolveRequest(context, name, platform); + } }, + serializer: { ...config.serializer, + // Native InitializeCore needs a running RN bridge. Its exclusion is a + // disclosed boundary; all ordinary Metro JS polyfills remain configured. + getModulesRunBeforeMainModule: () => [], + processModuleFilter(module) { modules.add(module.path); return priorFilter ? priorFilter(module) : true; }, + }, + }; + const bundlePath = path.join(output, 'authority.android.bundle'); + const built = await Metro.runBuild(build, { entry, platform: 'android', dev: true, minify: false }); + fs.writeFileSync(bundlePath, built.code, { mode: 0o600 }); + const bundle = fs.readFileSync(bundlePath, 'utf8'); + const scope = { console: { log() {}, warn() {}, error() {} }, setTimeout, clearTimeout, setInterval, clearInterval }; + scope.global = scope; + vm.createContext(scope); + assert.equal(vm.runInContext('typeof TextEncoder', scope), 'undefined'); + assert.equal(vm.runInContext('typeof TextDecoder', scope), 'undefined'); + vm.runInContext(bundle, scope, { filename: bundlePath, timeout: 15000 }); + const proof = JSON.parse(JSON.stringify(scope.__keeperAuthorityProof)); + assert.equal(proof.namespaceId, namespace); + assert.equal(proof.decoderInstalled, false); + assert.deepEqual(Array.from(scope.__keeperLegacyTraps), ['net', 'tls', 'crypto']); + for (let i = 0; i < proof.input.length; i++) { + assert.deepEqual(proof.encoded[i], Array.from(new TextEncoder().encode(proof.input[i]))); + } + for (const entry of proof.encodeInto) { + const target = new Uint8Array(entry.size); + const progress = new TextEncoder().encodeInto('A€😀', target); + assert.deepEqual(entry.progress, progress); assert.deepEqual(entry.output, Array.from(target)); + } + const parts = proof.token.split('.'); + const key = crypto.createPublicKey({ key: proof.publicJwk, format: 'jwk' }); + assert.equal(crypto.verify('sha256', Buffer.from(`${parts[0]}.${parts[1]}`), + { key, dsaEncoding: 'ieee-p1363' }, Buffer.from(parts[2], 'base64url')), true); + const claims = JSON.parse(Buffer.from(parts[1], 'base64url')); + assert.equal(claims.bodySha256, sha(Buffer.from(proof.raw))); + assert.ok([...modules].some((name) => name.endsWith('/@exodus/bytes/utf8.js'))); + assert.ok(![...modules].some((name) => /\/@exodus\/bytes\/.*\.node\.js$/.test(name))); + const compilerDirectory = { linux: 'linux64-bin', darwin: 'osx-bin' }[process.platform]; + assert.ok(compilerDirectory, 'This focused proof supports the repository Linux and macOS Hermes compilers'); + const compiler = path.join(root, 'node_modules/hermes-compiler/hermesc', compilerDirectory, 'hermesc'); + const hbc = path.join(output, 'authority.android.hbc'); + const compiled = spawnSync(compiler, ['-emit-binary', '-O', '-out', hbc, bundlePath], + { encoding: 'utf8', timeout: 30000, maxBuffer: 2 * 1024 * 1024 }); + assert.equal(compiled.status, 0, compiled.stderr); + const receipt = { state: 'FOCUSED_METRO_VM_AND_HERMESC_COMPILE_PASS_NOT_NATIVE_ACCEPTANCE', + node: process.versions.node, platform: 'android', metroVersion: require('metro/package.json').version, + appIndexImportOrder: imports, nativeInitializeCore: 'NOT_RUN', nativeShimEdges: ['net', 'tls', 'crypto'], + textGlobalsInitiallyAbsent: true, decoderInstalled: false, fullDerivationNamespace: namespace, + nativeOpenSSLVerification: true, whatwgEncodeIntoBoundaryCases: proof.encodeInto.length, + hermesCompilation: 'PASS', hermesRuntimeExecution: 'NOT_RUN_COMPILER_ONLY', + IOS: 'NOT_RUN', ANDROID_NATIVE: 'NOT_RUN', HARDWARE: 'NOT_RUN', + sourceHashes: Object.fromEntries(['index.js', 'shim.js', 'src/services/backup/encoding.ts', + 'src/services/backup/authority.ts', 'metro.config.js', 'babel.config.js'].map((name) => [name, sha(source(name))])), + bundle: { bytes: Buffer.byteLength(bundle), sha256: sha(bundle) }, + hbc: { bytes: fs.statSync(hbc).size, sha256: sha(fs.readFileSync(hbc)) }, + modules: [...modules].sort(), + }; + fs.writeFileSync(path.join(output, 'receipt.json'), `${JSON.stringify(receipt, null, 2)}\n`); + console.log(JSON.stringify({ state: receipt.state, output, bundleBytes: receipt.bundle.bytes, + hermesCompilation: 'PASS', hermesRuntimeExecution: receipt.hermesRuntimeExecution, modules: modules.size })); +} +main().catch((error) => { console.error(error); process.exitCode = 1; }); diff --git a/tests/recovery/protected-authority.test.mjs b/tests/recovery/protected-authority.test.mjs new file mode 100644 index 0000000000..7723feeffb --- /dev/null +++ b/tests/recovery/protected-authority.test.mjs @@ -0,0 +1,195 @@ +import assert from 'node:assert/strict'; +import crypto from 'node:crypto'; +import fs from 'node:fs'; +import { test } from 'node:test'; +import { spawnSync } from 'node:child_process'; +import { HDKey, HARDENED_OFFSET } from '@scure/bip32'; +import { mnemonicToSeedSync } from '@scure/bip39'; +import { hkdf } from '@noble/hashes/hkdf.js'; +import { sha256 } from '@noble/hashes/sha2.js'; +import { loadProtectedAuthority } from './support/protected-authority-esm-loader.mjs'; + +const production = await loadProtectedAuthority(); +const [nodeMajor, nodeMinor, nodePatch] = process.versions.node.split('.').map(Number); +assert.ok(nodeMajor === 22 && (nodeMinor > 23 || (nodeMinor === 23 && nodePatch >= 3)), + 'Run the proof with Node22.23.3 or a later22.x patch'); +const { deriveBackupAuthority } = production; +// Public, unfunded BIP39 standard fixture. Never use this phrase for real funds. +const MNEMONIC = 'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about'; +const NAMESPACE = 'bk3_7SolLs5PrxK2ybpJGsah8spJqewmCskdDW_L9gnI2FY'; +const AUDIENCE = 'https://keeper-fixture.invalid'; +const NOW = 2000000000; +const ORDER = BigInt('0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141'); +const bytes = (value) => Buffer.from(typeof value === 'string' ? value : JSON.stringify(value)); +const sha = (value) => crypto.createHash('sha256').update(value).digest('hex'); +const body = (authority, mutation = false) => ({ namespaceId: authority.namespaceId, + publicJwk: authority.publicJwk, ...(mutation ? { operation: 'deleteBackup', + expectedRevision: '12'.repeat(32), expectedGeneration: 7, authEpoch: 2, data: {} } : {}) }); +const decode = (token) => { + const [header, claims, signature] = token.split('.'); + return { header: JSON.parse(Buffer.from(header, 'base64url')), claims: JSON.parse(Buffer.from(claims, 'base64url')), + signature: Buffer.from(signature, 'base64url'), input: `${header}.${claims}` }; +}; + +test('the proof imports real supported2.4.0 modules and production ESM source', (t) => { + for (const name of ['@scure/bip32', '@scure/bip39', '@noble/curves', '@noble/hashes']) { + const pkg = JSON.parse(fs.readFileSync(new URL(`../../node_modules/${name}/package.json`, import.meta.url))); + assert.equal(pkg.version, '2.4.0'); + assert.equal(pkg.type, 'module'); + } + assert.doesNotMatch(production.source, /from ['"](?:@noble\/secp256k1|.*(?:sharedWallet|BIP85|bip85|noble_ecc))['"]/); + t.diagnostic(JSON.stringify({ node: process.versions.node, openssl: process.versions.openssl, + sourceSha256: production.sourceSha256, moduleMode: 'real-esm', nativeMetroAcceptance: false })); +}); + +test('RFC5869 SHA256 test case1 matches the fixed OKM and native HKDF', () => { + const ikm = Buffer.alloc(22, 0x0b), salt = Buffer.from('000102030405060708090a0b0c', 'hex'); + const info = Buffer.from('f0f1f2f3f4f5f6f7f8f9', 'hex'); + const expected = '3cb25f25faacd57a90434f64d0362f2a2d2d0a90cf1a5a4c5db02d56ecc4c5bf34007208d5b887185865'; + assert.equal(Buffer.from(hkdf(sha256, ikm, salt, info, 42)).toString('hex'), expected); + assert.equal(Buffer.from(crypto.hkdfSync('sha256', ikm, salt, info, 42)).toString('hex'), expected); +}); + +test('BIP39 official TREZOR vector and no-passphrase native seed agree', () => { + // https://github.com/trezor/python-mnemonic/blob/master/vectors.json + assert.equal(Buffer.from(mnemonicToSeedSync(MNEMONIC, 'TREZOR')).toString('hex'), + 'c55257c360c07c72029aebc1b53c05ed0362ada38ead3e3e9efa3708e53495531f09a6987599d18264c1e1c92f2cf141630c7a3c4ab7c81b2f001698e7463b04'); + const native = crypto.pbkdf2Sync(MNEMONIC.normalize('NFKD'), 'mnemonic', 2048, 64, 'sha512'); + assert.deepEqual(Buffer.from(mnemonicToSeedSync(MNEMONIC, '')), native); +}); + +test('BIP32 official vector1 verifies master and hardened-zero public keys', () => { + // https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#test-vector-1 + const master = HDKey.fromMasterSeed(Buffer.from('000102030405060708090a0b0c0d0e0f', 'hex')); + const child = master.deriveChild(HARDENED_OFFSET); + try { + assert.equal(master.publicExtendedKey, + 'xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8'); + assert.equal(child.publicExtendedKey, + 'xpub68Gmy5EdvgibQVfPdqkBBCHxA5htiqg55crXYuXoQRKfDBFA1WEjWgP6LHhwBZeNK1VTsfTFUHCdrfp1bgwQ9xv5ski8PX9rL2dZXvgGDnw'); + } finally { master.wipePrivateData(); child.wipePrivateData(); } +}); + +test('the actual full no-passphrase authority reproduces the backend fixed namespace after clean derivation', () => { + const first = deriveBackupAuthority(MNEMONIC), restored = deriveBackupAuthority(MNEMONIC); + try { + assert.equal(first.namespaceId, NAMESPACE); + assert.equal(restored.namespaceId, NAMESPACE); + assert.deepEqual(first.publicJwk, restored.publicJwk); + const canonical = JSON.stringify({ crv: first.publicJwk.crv, kty: first.publicJwk.kty, + x: first.publicJwk.x, y: first.publicJwk.y }); + assert.equal(first.kid, crypto.createHash('sha256').update(canonical).digest('base64url')); + assert.equal(first.namespaceId, `bk3_${first.kid}`); + assert.equal(crypto.createPublicKey({ key: first.publicJwk, format: 'jwk' }).asymmetricKeyDetails.namedCurve, 'secp256k1'); + first.dispose(); + assert.doesNotThrow(() => restored.signRawBody(bytes(body(restored)), '/backup/v3/snapshot', AUDIENCE, NOW)); + } finally { first.dispose(); restored.dispose(); } +}); + +test('an expected original account must match both mnemonic seed bytes and Keeper identity', () => { + const seed = crypto.pbkdf2Sync(MNEMONIC.normalize('NFKD'), 'mnemonic', 2048, 64, 'sha512'); + const expected = { id: sha(seed), primarySeed: seed.toString('hex') }; + const authority = deriveBackupAuthority(MNEMONIC, expected); + try { assert.equal(authority.namespaceId, NAMESPACE); } finally { authority.dispose(); } + for (const account of [{ ...expected, id: 'ab'.repeat(32) }, + { ...expected, primarySeed: 'ab'.repeat(64) }, { ...expected, primarySeed: 'not-hex' }, + { ...expected, primarySeed: expected.primarySeed.toUpperCase() }, + { ...expected, id: expected.id.toUpperCase() }, null]) { + assert.throws(() => deriveBackupAuthority(MNEMONIC, account), { message: 'PROTECTED_BACKUP_AUTHORITY_UNAVAILABLE' }); + } +}); + +test('actual production bootstrap and mobile-default encoding derive/sign with both text globals absent', () => { + const helper = new URL('./support/protected-authority-esm-loader.mjs', import.meta.url).href; + const loader = new URL('./support/react-native-encoding-loader.mjs', import.meta.url).href; + const program = ` + import assert from 'node:assert/strict'; + import crypto from 'node:crypto'; + delete globalThis.TextEncoder; delete globalThis.TextDecoder; + assert.equal(typeof globalThis.TextEncoder, 'undefined'); + assert.equal(typeof globalThis.TextDecoder, 'undefined'); + const {loadProtectedAuthority} = await import(${JSON.stringify(helper)}); + const {deriveBackupAuthority} = await loadProtectedAuthority(); + assert.equal(typeof globalThis.TextEncoder, 'function'); + assert.equal(typeof globalThis.TextDecoder, 'undefined'); + const authority = deriveBackupAuthority(${JSON.stringify(MNEMONIC)}); + try { + assert.equal(authority.namespaceId, ${JSON.stringify(NAMESPACE)}); + const raw = Buffer.from(JSON.stringify({namespaceId:authority.namespaceId,publicJwk:authority.publicJwk})); + const token = authority.signRawBody(raw,'/backup/v3/snapshot',${JSON.stringify(AUDIENCE)},${NOW}); + const parts=token.split('.'); + const key=crypto.createPublicKey({key:authority.publicJwk,format:'jwk'}); + assert.ok(crypto.verify('sha256',Buffer.from(parts[0]+'.'+parts[1]),{key,dsaEncoding:'ieee-p1363'},Buffer.from(parts[2],'base64url'))); + console.log(JSON.stringify({namespaceId:authority.namespaceId,textDecoderInstalled:false,actualEsmSource:true})); + } finally { authority.dispose(); } + `; + const result = spawnSync(process.execPath, ['--loader', loader, '--input-type=module', '-e', program], { encoding: 'utf8', timeout: 15000 }); + assert.equal(result.status, 0, result.stderr); + assert.deepEqual(JSON.parse(result.stdout), { namespaceId: NAMESPACE, textDecoderInstalled: false, actualEsmSource: true }); +}); + +for (const route of ['/backup/v3/enroll', '/backup/v3/snapshot', '/backup/v3/mutate']) { + test(`actual production signer yields native OpenSSL ES256K for ${route}`, () => { + const authority = deriveBackupAuthority(MNEMONIC); + try { + const raw = bytes(JSON.stringify(body(authority, route.endsWith('mutate')), null, 2)); + const token = authority.signRawBody(raw, route, AUDIENCE, NOW); + assert.doesNotMatch(token, /^Bearer /); + const signed = decode(token); + assert.deepEqual(signed.header, { alg: 'ES256K', kid: authority.kid, typ: 'keeper-backup+jws' }); + assert.deepEqual(signed.claims, { v: 3, aud: AUDIENCE, method: 'POST', path: route, + namespaceId: authority.namespaceId, bodySha256: sha(raw), iat: NOW, exp: NOW + 120 }); + assert.notEqual(signed.claims.bodySha256, sha(bytes(body(authority, route.endsWith('mutate'))))); + const key = crypto.createPublicKey({ key: authority.publicJwk, format: 'jwk' }); + assert.equal(signed.signature.length, 64); + const s = BigInt(`0x${signed.signature.subarray(32).toString('hex')}`); + assert.ok(s > 0n && s <= ORDER / 2n); + assert.equal(crypto.verify('sha256', bytes(signed.input), { key, dsaEncoding: 'ieee-p1363' }, signed.signature), true); + assert.equal(crypto.verify('sha256', bytes(`${signed.input}x`), { key, dsaEncoding: 'ieee-p1363' }, signed.signature), false); + assert.equal(crypto.verify('sha256', crypto.createHash('sha256').update(signed.input).digest(), + { key, dsaEncoding: 'ieee-p1363' }, signed.signature), false, 'Production signs SHA256 exactly once'); + assert.equal(authority.signRawBody(raw, route, AUDIENCE, NOW), token, 'RFC6979 signing is deterministic'); + } finally { authority.dispose(); } + }); +} + +test('authority exposes only frozen public descriptors and dispose is idempotent', () => { + const authority = deriveBackupAuthority(MNEMONIC); + const raw = bytes(body(authority)); + assert.ok(Object.isFrozen(authority)); + assert.ok(Object.isFrozen(authority.publicJwk)); + assert.deepEqual(Object.keys(authority).sort(), ['dispose', 'kid', 'namespaceId', 'publicJwk', 'signRawBody']); + assert.deepEqual(JSON.parse(JSON.stringify(authority)), { + namespaceId: NAMESPACE, publicJwk: authority.publicJwk, kid: authority.kid, + }); + assert.equal(Object.prototype.hasOwnProperty.call(authority, 'toJSON'), false); + authority.dispose(); authority.dispose(); + assert.throws(() => authority.signRawBody(raw, '/backup/v3/snapshot', AUDIENCE, NOW), /PROTECTED_BACKUP_AUTHORITY_UNAVAILABLE/); +}); + +test('invalid mnemonics fail closed without echoing caller input', () => { + for (const mnemonic of ['', 'synthetic invalid recovery words', MNEMONIC.replace('about', 'abandon'), 'x'.repeat(1025), null, {}]) { + assert.throws(() => deriveBackupAuthority(mnemonic), { message: 'PROTECTED_BACKUP_AUTHORITY_UNAVAILABLE' }); + } +}); + +test('scope, body, clock and malformed UTF8 checks fail closed before returning a proof', () => { + const authority = deriveBackupAuthority(MNEMONIC); + const raw = bytes(body(authority)), mutate = body(authority, true); + try { + const fail = (value, route = '/backup/v3/snapshot', audience = AUDIENCE, now = NOW) => + assert.throws(() => authority.signRawBody(value, route, audience, now), { message: 'PROTECTED_BACKUP_AUTHORITY_UNAVAILABLE' }); + fail(raw, '/revisionedBackupMutation'); + for (const audience of ['', 'x'.repeat(257), 'https://fixture.invalid\n']) fail(raw, undefined, audience); + for (const now of [-1, -0, NaN, Infinity, 1.5, Number.MAX_SAFE_INTEGER]) fail(raw, undefined, undefined, now); + fail(bytes({ ...body(authority), namespaceId: 'bk3_unowned' })); + fail(bytes({ ...body(authority), publicJwk: { ...authority.publicJwk, d: 'forbidden' } })); + fail(bytes({ ...body(authority), extra: true })); + fail(Uint8Array.of(0xff)); + fail(Buffer.alloc(32 * 1024 * 1024 + 1)); + for (const patch of [{ authEpoch: undefined }, { authEpoch: -1 }, { expectedGeneration: 2 ** 53 }, + { expectedRevision: 'AF'.repeat(32) }, { operation: 'unknown' }, { data: [] }]) { + fail(bytes({ ...mutate, ...patch }), '/backup/v3/mutate'); + } + } finally { authority.dispose(); } +}); diff --git a/tests/recovery/protectedRecoverySource.test.cjs b/tests/recovery/protectedRecoverySource.test.cjs new file mode 100644 index 0000000000..7861fac319 --- /dev/null +++ b/tests/recovery/protectedRecoverySource.test.cjs @@ -0,0 +1,303 @@ +// Production recovery discovery/adapter/saga with real image crypto and +// disposable in-memory storage. Authenticated HTTP and native Realm proof live +// in the separate exact-private-Relay genuine lane, not this unit adapter. +const { test } = require('node:test'); +const assert = require('node:assert/strict'); +const { fixture, enums, encryption, wallet, loadModule } = require('./sagaHarness.cjs'); +const { image } = require('./helpers.cjs'); +const { prepareRecoverySourceImage } = loadModule('src/services/backup/restore.ts', { './image': image }); +const plain = value => JSON.parse(JSON.stringify(value)); +const namespaceId = `bk3_${'ab'.repeat(32)}`; +const legacySelection = { source: 'legacy-older', olderCopyConfirmed: true }; +function deferred() { + let resolve; + const promise = new Promise(done => { resolve = done; }); + return { promise, resolve }; +} +function protectedFixture() { + const f = fixture(); + const key = encryption.generateEncryptionKey(f.app.primarySeed); + const record = wallet('protected-wallet', enums.NetworkType.MAINNET); + const snapshot = { + scope: 'protected-backup', exists: true, namespaceId, generation: 1, authEpoch: 0, + revision: 'ab'.repeat(32), backupRevisionRequired: true, incrementalRevisionRequired: true, + deleted: false, suppressLegacyFallback: false, + appImage: { ...f.remote, appId: namespaceId, wallets: { + [record.id]: encryption.encrypt(key, JSON.stringify(record)), + }, vaults: [], labels: [] }, + allVaultImages: [], labels: [], vaultMaps: [], unavailableVaultIds: [], unavailableLabelIds: [], + }; + const reads = []; + f.scope.readProtectedBackup = async (...args) => { + reads.push(args); + return { state: snapshot.deleted ? 'deleted' : 'present', namespaceId, snapshot }; + }; + const legacy = f.scope.Relay.getAppImage; + let legacyReads = 0; + f.scope.Relay.getAppImage = async (...args) => { legacyReads++; return legacy(...args); }; + let writes = 0; + const create = f.scope.dbManager.createObject; + f.scope.dbManager.createObject = (...args) => { writes++; return create(...args); }; + return { ...f, key, record, snapshot, reads, writes: () => writes, + legacyReads: () => legacyReads, + restore: selection => f.run('getAppImageWorker', { payload: { + primaryMnemonic: 'disposable mnemonic', ...(selection ? { selection } : {}), + } }), + }; +} + +test('default protected recovery normalizes namespace only after validation and retains original Keeper identity', async () => { + const f = protectedFixture(); + const before = plain(f.snapshot); + await f.restore(); + assert.equal(f.reads.length, 1); + assert.equal(f.reads[0][2], f.app.id); + assert.equal(f.legacyReads(), 0); + assert.equal(f.app.id, f.state.storage.appId); + assert.notEqual(f.app.id, namespaceId); + assert.deepEqual(plain(f.collections.Wallet), [f.record]); + assert.deepEqual(plain(f.snapshot), before, 'raw projection must remain unchanged'); + assert.equal(f.state.bhr.recoveryDiscovery.status, 'restored'); + assert.deepEqual(plain(f.state.bhr.recoveryDiscovery.provenance), { + source: 'protected', keeperAppId: f.app.id, namespaceId, + legacyMayPredateDeletion: false, legacyCopiesMayRemain: true, + }); + assert.equal(f.actions.some(a => a.type === 'setAutomaticCloudBackup' && a.payload.enabled), false); + assert.equal(f.state.bhr.automaticCloudBackupByAppId[f.app.id], false, + 'validated recovery explicitly revokes inherited opt-in without granting upload authority'); + assert.deepEqual(plain(f.state.bhr.protectedBackupStateByAppId[f.app.id]), { + status: 'present', namespaceId, generation: 1, authEpoch: 0, + }); + assert.equal(f.calls.length, 0, 'restoration must not enqueue upload'); +}); + +for (const [state, status] of [['neverEnrolled', 'protected-absent'], ['deleted', 'protected-deleted']]) { + test(`protected ${state} is an explicit no-write result with no older-copy lookup`, async () => { + const f = protectedFixture(); + f.scope.readProtectedBackup = async () => ({ state, namespaceId, + ...(state === 'deleted' ? { snapshot: { ...f.snapshot, deleted: true, suppressLegacyFallback: true } } : {}), + }); + await f.restore(); + assert.equal(f.state.bhr.recoveryDiscovery.status, status); + assert.equal(f.state.bhr.recoveryDiscovery.keeperAppId, f.app.id); + assert.equal(f.writes(), 0); + assert.equal(f.legacyReads(), 0); + assert.equal(f.actions.some(a => a.type === 'setAppCreated'), false); + }); +} + +test('an authenticated empty enrollment is distinct from absence and never fabricates a restored wallet', async () => { + const f = protectedFixture(); + f.snapshot.appImage.wallets = {}; + await f.restore(); + assert.equal(f.state.bhr.recoveryDiscovery.status, 'protected-empty'); + assert.equal(f.writes(), 0); + assert.equal(f.legacyReads(), 0); +}); + +test('protected request errors stay errors with zero fallback or local mutation', async () => { + const f = protectedFixture(); + f.scope.readProtectedBackup = async () => { throw new Error('Protected backup request failed'); }; + await f.restore(); + assert.equal(f.state.bhr.recoveryDiscovery.status, 'read-error'); + assert.equal(f.writes(), 0); + assert.equal(f.legacyReads(), 0); +}); + +test('explicit older recovery requires its affirmative selection and records older-copy provenance', async () => { + const invalid = protectedFixture(); + await invalid.restore({ source: 'legacy-older' }); + assert.equal(invalid.state.bhr.recoveryDiscovery.status, 'read-error'); + assert.equal(invalid.writes(), 0); + assert.equal(invalid.legacyReads(), 0); + + const f = protectedFixture(); + f.remote.wallets = f.snapshot.appImage.wallets; + await f.restore(legacySelection); + assert.equal(f.reads.length, 0); + assert.equal(f.legacyReads(), 1); + assert.equal(f.state.bhr.recoveryDiscovery.status, 'restored'); + assert.equal(f.state.bhr.recoveryDiscovery.provenance.source, 'legacy-older'); + assert.equal(f.state.bhr.recoveryDiscovery.provenance.legacyMayPredateDeletion, true); + assert.equal(f.state.bhr.recoveryDiscovery.provenance.namespaceId, undefined); +}); + +test('protected adapter rejects foreign projection identities before local mutation', async () => { + for (const corrupt of [ + snapshot => { snapshot.appImage.appId = 'legacy-app-id'; }, + snapshot => { snapshot.allVaultImages = [{ appId: 'foreign-namespace', vaultId: 'foreign', vault: 'ciphertext' }]; }, + snapshot => { snapshot.unavailableLabelIds = ['missing']; }, + ]) { + const f = protectedFixture(); + corrupt(f.snapshot); + await f.restore(); + assert.equal(f.state.bhr.recoveryDiscovery.status, 'read-error'); + assert.equal(f.writes(), 0); + assert.equal(f.legacyReads(), 0); + } +}); + +test('all protected records decode before first write, including late corrupt or unavailable references', async () => { + for (const corrupt of [ + snapshot => { snapshot.appImage.signers = { late: 'unreadable ciphertext' }; }, + snapshot => { snapshot.appImage.nodes = ['unreadable ciphertext']; }, + snapshot => { snapshot.appImage.vaults = ['missing-vault']; }, + snapshot => { snapshot.appImage.labels = ['missing-label']; }, + ]) { + const f = protectedFixture(); + const before = plain({ app: f.app, collections: f.collections }); + corrupt(f.snapshot); + await f.restore(); + assert.equal(f.state.bhr.recoveryDiscovery.status, 'read-error'); + assert.equal(f.writes(), 0); + assert.deepEqual(plain({ app: f.app, collections: f.collections }), before); + } +}); + +test('an otherwise empty protected image with missing recovery references is an error rather than empty enrollment', async () => { + for (const field of ['vaults', 'labels']) { + const f = protectedFixture(); + f.snapshot.appImage.wallets = {}; + f.snapshot.appImage[field] = ['missing-record']; + await f.restore(); + assert.equal(f.state.bhr.recoveryDiscovery.status, 'read-error'); + assert.equal(f.writes(), 0); + assert.equal(f.legacyReads(), 0); + } +}); + +test('a recreated protected image remains recoverable without undoing suppression of automatic legacy fallback', () => { + const f = protectedFixture(); + f.snapshot.suppressLegacyFallback = true; + const result = prepareRecoverySourceImage(f.app.id, { source: 'protected', namespaceId, response: f.snapshot }); + assert.equal(result.appImage.appId, f.app.id); + assert.equal(f.snapshot.suppressLegacyFallback, true); +}); + +test('a protected restore does not overwrite a newer authenticated tombstone hint', async () => { + const f = protectedFixture(); + f.state.bhr.protectedBackupStateByAppId = { [f.app.id]: { + status: 'deleted', namespaceId, generation: 2, authEpoch: 1, + } }; + await f.restore(); + assert.equal(f.state.bhr.recoveryDiscovery.status, 'restored'); + assert.deepEqual(plain(f.state.bhr.protectedBackupStateByAppId[f.app.id]), { + status: 'deleted', namespaceId, generation: 2, authEpoch: 1, + }); + assert.equal(f.actions.some(a => a.type === 'setProtectedBackupState'), false); +}); + +test('explicit older restore never replaces an existing protected tombstone hint', async () => { + const f = protectedFixture(); + f.state.bhr.protectedBackupStateByAppId = { [f.app.id]: { + status: 'deleted', namespaceId, generation: 2, authEpoch: 1, + } }; + f.remote.wallets = f.snapshot.appImage.wallets; + await f.restore(legacySelection); + assert.equal(f.state.bhr.recoveryDiscovery.status, 'restored'); + assert.equal(f.state.bhr.protectedBackupStateByAppId[f.app.id].status, 'deleted'); + assert.equal(f.actions.some(a => a.type === 'setProtectedBackupState'), false); +}); + +test('A to B to A while protected read is queued cannot write or replace current recovery discovery', async () => { + const f = protectedFixture(); + const entered = deferred(); + const resume = deferred(); + f.scope.readProtectedBackup = async () => { + entered.resolve(); await resume.promise; + return { state: 'present', namespaceId, snapshot: f.snapshot }; + }; + const pending = f.restore(); + await entered.promise; + f.state.storage.appId = 'another-account'; + f.state.storage.appId = f.app.id; + f.state.bhr.recoveryDiscovery = { status: 'idle', source: 'protected' }; + resume.resolve(); + await pending; + assert.equal(f.writes(), 0); + assert.equal(f.state.bhr.recoveryDiscovery.status, 'idle'); + assert.equal(f.legacyReads(), 0); +}); + +test('a newer discovery supersedes a paused protected response without taking its provenance or starting a restore', async () => { + const f = protectedFixture(); + const entered = deferred(); + const resume = deferred(); + let requests = 0; + f.scope.readProtectedBackup = async () => { + if (++requests === 1) { + entered.resolve(); await resume.promise; + return { state: 'present', namespaceId, snapshot: f.snapshot }; + } + return { state: 'neverEnrolled', namespaceId }; + }; + const old = f.restore(); + await entered.promise; + await f.restore(); + resume.resolve(); + await old; + assert.equal(f.state.bhr.recoveryDiscovery.status, 'protected-absent'); + assert.equal(f.writes(), 0); + assert.equal(f.legacyReads(), 0); +}); + +test('a post-selection local write failure reports failure in only its owned recovery session and retains committed local data', async () => { + const f = protectedFixture(); + f.state.storage.appId = 'initial-recovery-screen-account'; + const create = f.scope.dbManager.createObject; + f.scope.dbManager.createObject = (schema, value) => schema === 'Wallet' ? false : create(schema, value); + await f.restore(); + assert.equal(f.state.storage.appId, f.app.id, 'the restore selected its original Keeper identity'); + assert.equal(f.app.primaryMnemonic, 'disposable mnemonic', 'committed KeeperApp remains local after later failure'); + assert.equal(f.collections.Wallet.length, 0); + assert.equal(f.state.bhr.recoveryDiscovery.status, 'read-error'); + assert.equal(f.state.storage.appCreated, undefined); + assert.equal(f.actions.at(-1).type, 'appImagerecoveryRetry'); + assert.equal(f.legacyReads(), 0); +}); + +test('a post-selection A to B to A cannot project its local-write failure into a new session', async () => { + const f = protectedFixture(); + f.state.storage.appId = 'initial-recovery-screen-account'; + const create = f.scope.dbManager.createObject; + f.scope.dbManager.createObject = (schema, value) => { + const result = create(schema, value); + if (schema === 'Wallet') { + f.state.storage.appId = 'another-account'; + f.state.storage.appId = f.app.id; + f.state.bhr.recoveryDiscovery = { status: 'idle', source: 'protected' }; + } + return result; + }; + await f.restore(); + assert.equal(f.state.bhr.recoveryDiscovery.status, 'idle'); + assert.equal(f.state.storage.appCreated, undefined); + assert.equal(f.actions.some(a => a.type === 'setAppImageError' && a.payload), false); + assert.equal(f.collections.Signer.length, 0, 'no later signing keys may cross the stale session'); + assert.equal(f.collections.Wallet.length, 1, 'already committed local recovery data is retained'); +}); + +test('recovery action defaults protected and persisted source hints cannot enable backup consent or verification', () => { + const actions = loadModule('src/store/sagaActions/bhr.ts'); + assert.deepEqual(plain(actions.getAppImage('fixture', false).payload.selection), { source: 'protected' }); + let persistConfig; + const reducer = loadModule('src/store/reducers/bhr.ts', { + 'src/models/enums/BHR': loadModule('src/models/enums/BHR.ts'), + 'src/storage': { reduxStorage: {} }, + 'redux-persist': { persistReducer: (config, reduce) => { persistConfig = config; return reduce; } }, + }); + let state = reducer.default(undefined, { type: 'fixture/init' }); + const before = plain(state); + state = reducer.default(state, reducer.setProtectedBackupState({ appId: 'original', state: { + status: 'deleted', namespaceId, generation: 4, authEpoch: 2, + } })); + assert.equal(state.protectedBackupStateByAppId.original.status, 'deleted'); + const { protectedBackupStateByAppId, ...after } = plain(state); + const { protectedBackupStateByAppId: ignored, ...prior } = before; + assert.deepEqual(after, prior); + state = reducer.default(state, reducer.setRecoveryDiscovery({ status: 'restored', source: 'legacy-older' })); + state = reducer.default(state, reducer.resetRecoveryDiscovery()); + assert.deepEqual(plain(state.recoveryDiscovery), { status: 'idle', source: 'protected' }); + assert.equal(persistConfig.blacklist.includes('recoveryDiscovery'), true); + assert.equal(persistConfig.blacklist.includes('protectedBackupStateByAppId'), false); +}); diff --git a/tests/recovery/protectedWriteRealmRelay.fixture.cjs b/tests/recovery/protectedWriteRealmRelay.fixture.cjs new file mode 100644 index 0000000000..7782d66f80 --- /dev/null +++ b/tests/recovery/protectedWriteRealmRelay.fixture.cjs @@ -0,0 +1,204 @@ +// Separate protected-v3 fixture. The separately obtained backend remains out of +// app history. Every successful request reaches its actual reviewed router and +// bounded transactional service; only explicit fault cases alter a reply. +const assert = require('node:assert/strict'); +const http = require('node:http'); +const fs = require('node:fs'); +const path = require('node:path'); +const { pathToFileURL } = require('node:url'); +const { createRequire } = require('node:module'); +const vm = require('node:vm'); +const { checkedBackend } = require('../../scripts/run-protected-backup-genuine.cjs'); + +async function actualCryptoModules() { + const app = path.resolve(__dirname, '../..'); + const declared = JSON.parse(fs.readFileSync(path.join(app, 'package.json'))).dependencies; + const modules = {}; + for (const name of ['@scure/bip32', '@scure/bip39', '@scure/bip39/wordlists/english.js', + '@noble/hashes/hkdf.js', '@noble/hashes/sha2.js', '@noble/hashes/utils.js', + '@noble/curves/secp256k1.js', '@noble/curves/utils.js', '@exodus/bytes/encoding-lite.js']) { + const packageName = name.split('/').slice(0, 2).join('/'); + assert.match(declared[packageName], /^\d+\.\d+\.\d+$/, 'New authority dependencies must be exactly pinned'); + const filename = require.resolve(name); + let directory = path.dirname(filename), metadata; + while (directory !== path.dirname(directory)) { + const candidate = path.join(directory, 'package.json'); + if (fs.existsSync(candidate)) { + const value = JSON.parse(fs.readFileSync(candidate)); + if (value.name === packageName) { metadata = value; break; } + } + directory = path.dirname(directory); + } + assert.equal(metadata?.version, declared[packageName], 'Import the actual pinned production dependency'); + modules[name] = await import(pathToFileURL(filename).href); + assert.equal(Object.prototype.toString.call(modules[name]), '[object Module]'); + } + return modules; +} + +async function createProtectedRealmClient(url, options = {}) { + const moduleBindings = await actualCryptoModules(); + const { createRealmClient } = require('./automaticWriteRealmRelay.fixture.cjs'); + const client = await createRealmClient(url, { ...options, moduleBindings }); + client.protected = client.load('src/services/backup/protected'); + client.authority = client.load('src/services/backup/authority'); + return client; +} + +function actualLegacyReadHandlers(checkout, backend) { + // Evaluate only the exact two production handler declarations. The private + // service loader uses inert provider constructors with hard operation traps; + // the complete server/provider bootstrap and auth middleware are NOT loaded. + const requireBackend = createRequire(path.join(checkout, 'package.json')); + const ts = requireBackend('typescript'), express = requireBackend('express'); + const filename = path.join(checkout, 'src/routes/routes.ts'); + const tree = ts.createSourceFile(filename, fs.readFileSync(filename, 'utf8'), ts.ScriptTarget.Latest, true); + const wanted = new Set(['/getAppImage', '/getBackupSnapshot']), handlers = new Map(); + const visit = node => { + if (ts.isCallExpression(node) && ts.isPropertyAccessExpression(node.expression) && + node.expression.expression.getText(tree) === 'router' && node.expression.name.text === 'post' && + ts.isStringLiteral(node.arguments[0]) && wanted.has(node.arguments[0].text)) { + assert.equal(node.arguments.length, 2); + handlers.set(node.arguments[0].text, node.arguments[1].getText(tree)); + } + ts.forEachChild(node, visit); + }; + visit(tree); assert.equal(handlers.size, wanted.size, 'The exact legacy read handlers must be present'); + const bhr = backend.loader.load('src/services/bhr.ts'); + const { getBackupSnapshot } = backend.loader.load('src/services/backupSnapshot.ts'); + const app = express(); app.use(express.json({ limit: '50mb' })); + for (const [route, declaration] of handlers) { + const js = ts.transpileModule(`module.exports = (${declaration});`, { + compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 }, + }).outputText; + const module = { exports: {} }; + vm.runInThisContext(`(function(module,bhr,getBackupSnapshot){${js}\n})`, { filename }) + (module, bhr, getBackupSnapshot); + app.post(route, module.exports); + } + return app; +} + +async function createProtectedServer() { + const checkout = checkedBackend(process.env.KEEPER_PROTECTED_RELAY_CHECKOUT); + const { createProtectedHttpFixture } = require(path.join(checkout, 'tests/support/protectedBackupFixture.cjs')); + const backend = await createProtectedHttpFixture(); + const target = new URL(backend.url); + assert.equal(target.protocol, 'http:'); + assert.equal(target.hostname, '127.0.0.1'); + const trace = [], faults = []; + let server; + try { + const legacyReads = actualLegacyReadHandlers(checkout, backend); + server = http.createServer((request, response) => { + if (['/getAppImage', '/getBackupSnapshot'].includes(request.url)) { + const event = { path: request.url, method: request.method, status: null, legacyRead: true, committed: false }; + trace.push(event); response.once('finish', () => { event.status = response.statusCode; }); + legacyReads(request, response); return; + } + const chunks = []; + request.on('data', chunk => chunks.push(chunk)); + request.on('end', () => { + const raw = Buffer.concat(chunks); + const upstreamUrl = new URL(request.url, target); + if (!request.url.startsWith('/') || request.url.startsWith('//') || upstreamUrl.origin !== target.origin) { + response.writeHead(400); response.end(); return; + } + let body; + try { body = JSON.parse(raw.toString('utf8')); } catch { /* malformed fault cases stay inert */ } + const event = { path: request.url, method: request.method, operation: body?.operation, + namespaceId: body?.namespaceId, expectedRevision: body?.expectedRevision, + expectedGeneration: body?.expectedGeneration, authEpoch: body?.authEpoch, + ...(body?.operation === 'updateAppImage' && Array.isArray(body.data?.nodes) + ? { replaceNodes: body.data.replaceNodes, nodeCount: body.data.nodes.length } : {}), + status: null, committed: false }; + trace.push(event); + const faultIndex = faults.findIndex(fault => fault.path === request.url && + (!fault.operation || fault.operation === body?.operation)); + let fault; + if (faultIndex >= 0) { + if (faults[faultIndex].after) faults[faultIndex].after--; + else [fault] = faults.splice(faultIndex, 1); + } + const upstream = http.request(upstreamUrl, { + method: request.method, headers: request.headers, timeout: 25000, + }, incoming => { + const replies = []; + incoming.on('data', chunk => replies.push(chunk)); + incoming.on('end', async () => { + let bytes = Buffer.concat(replies), decoded; + try { decoded = JSON.parse(bytes.toString('utf8')); } catch { /* preserve the real raw reply */ } + event.status = incoming.statusCode; + event.committed = incoming.statusCode === 200 && decoded?.updated === true; + if (event.committed) Object.assign(event, { serverRevision: decoded.newRevision, + serverGeneration: decoded.newGeneration, serverAuthEpoch: decoded.authEpoch }); + try { + // Fault callbacks can hold or mutate a *real* completed read/ACK. + // They never supply a successful fabricated acknowledgement. + if (fault?.beforeReply) await fault.beforeReply({ body: decoded, event }); + if (fault?.mode === 'lost-reply') { response.destroy(); return; } + if (fault?.mode === 'malformed-json') bytes = Buffer.from('{malformed'); + else if (fault?.mutateReply) bytes = Buffer.from(JSON.stringify(fault.mutateReply(decoded))); + response.writeHead(incoming.statusCode, { 'content-type': 'application/json', 'content-length': bytes.length }); + response.end(bytes); + } catch { + response.destroy(); + } + }); + incoming.on('error', () => response.destroy()); + }); + upstream.on('timeout', () => upstream.destroy()); + upstream.on('error', () => response.destroy()); + request.on('aborted', () => upstream.destroy()); + upstream.end(raw); + }); + request.on('error', () => response.destroy()); + }); + await new Promise(resolve => server.listen(0, '127.0.0.1', resolve)); + return { backend, trace, audience: 'https://keeper-fixture.invalid', + url: `http://127.0.0.1:${server.address().port}/`, + armFault(fault) { + assert.ok(['/backup/v3/enroll', '/backup/v3/snapshot', '/backup/v3/mutate'].includes(fault.path)); + assert.ok(!fault.mode || ['lost-reply', 'malformed-json'].includes(fault.mode)); + faults.push({ ...fault }); + }, + clearTrace() { trace.length = 0; backend.clearTrace(); }, + async reset() { + faults.length = 0; trace.length = 0; await backend.reset(); + for (const name of ['appimages', 'appVaultImages', 'vault images', 'vault maps', 'labels', 'apps']) + await backend.mongoose.connection.db.collection(name).deleteMany({}); + backend.clearCommands(); + }, + async seedLegacy(payload) { + const database = backend.mongoose.connection.db; + const appId = payload.appId; + await database.collection('appimages').replaceOne({ appId }, { appId, + publicId: payload.publicId, version: payload.version, subscription: payload.subscription, + wallets: payload.walletObject, signers: payload.signersObject, nodes: payload.nodes, + vaults: Object.keys(payload.vaultObject), labels: payload.labels.map(label => label.id), + backupRevisionRequired: true, incrementalRevisionRequired: false, backupGeneration: 0, + }, { upsert: true }); + for (const value of Object.values(payload.vaultObject)) { + await database.collection('appVaultImages').replaceOne({ appId, vaultId: value.vaultId }, + { appId, vaultId: value.vaultId, vault: value.vault, isArchived: !!value.isArchived, + signerIds: value.signersData.map(signer => signer.signerId) }, { upsert: true }); + } + for (const label of payload.labels) await database.collection('labels').replaceOne({ id: label.id }, label, { upsert: true }); + await database.collection('apps').replaceOne({ appId }, { _id: appId, appId, + subscription: { level: 1, isCancelled: false, productId: 'disposable-product', + transactionReceipt: 'disposable-stored-receipt' } }, { upsert: true }); + }, + async close() { + server.closeAllConnections(); + await new Promise(resolve => server.close(resolve)); + await backend.close(); + }, + }; + } catch (error) { + if (server?.listening) { server.closeAllConnections(); await new Promise(resolve => server.close(resolve)); } + await backend.close(); + throw error; + } +} + +module.exports = { createProtectedServer, createProtectedRealmClient, actualCryptoModules }; diff --git a/tests/recovery/protectedWriteRealmRelay.proof.cjs b/tests/recovery/protectedWriteRealmRelay.proof.cjs new file mode 100644 index 0000000000..217db5fdae --- /dev/null +++ b/tests/recovery/protectedWriteRealmRelay.proof.cjs @@ -0,0 +1,575 @@ +// Opt-in, exact-private protected-v3 app proof. This is not the public v2 +// matrix. Native encrypted Realm, real production app authority/queue/store, +// crypto, HTTP and the reviewed bounded transactional backend all remain real. +const assert = require('node:assert/strict'); +const crypto = require('node:crypto'); +const path = require('node:path'); +const { test, beforeEach, afterEach, after } = require('node:test'); +const { createProtectedServer, createProtectedRealmClient } = require('./protectedWriteRealmRelay.fixture.cjs'); +const { declarations, classMembers, evaluate, plain, Realm } = require('./automaticWriteRealmRelay.fixture.cjs'); + +const mnemonic = 'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about'; +const seed = require('bip39').mnemonicToSeedSync(mnemonic); +const appId = crypto.createHash('sha256').update(seed).digest('hex'); +const builders = { assert }; +// Reuse data builders only, without importing or running the historical v2 +// tests. Every protocol assertion and admission below is new and v3-specific. +evaluate(declarations('tests/recovery/automaticWriteRealmRelay.integration.cjs', [ + 'wallet', 'signer', 'vault', 'persist', 'persistVault', 'vaultBody', 'labelBody', 'operations', +]), builders, 'shared-native-recovery-record-builders.ts'); +const { wallet, persist, operations } = builders; +let server; +// Each independent case gets a fresh real router and its unchanged admission +// buckets. Repeated same-account requests across unrelated cases must not share +// a synthetic burst or disable the actual production rate limits. +beforeEach(async () => { server = await createProtectedServer(); }); +afterEach(async () => { await server?.close(); server = undefined; }); +after(() => { Realm.shutdown(); }); +const tick = () => new Promise(resolve => setImmediate(resolve)); +function barrier() { let release; const promise = new Promise(resolve => { release = resolve; }); return { promise, release }; } +const mutations = () => server.trace.filter(event => event.path === '/backup/v3/mutate'); + +async function client(t, options = {}) { + const f = await createProtectedRealmClient(server.url, { appId, primarySeed: seed.toString('hex'), primaryMnemonic: mnemonic, ...options }); + f.automatic = f.load('src/services/backup/automatic'); + f.installProductionStoreObservers(f.automatic); + const stop = f.listen(); + t.after(async () => { stop(); await f.close(); }); + f.sagas = f.productionSagas(['currentBackupAppId', 'backupInspectionOwners', 'runBackupInspection']); + return f; +} +async function setup(t, operation = 'updateAppImage') { + await server.reset(); const f = await client(t); + operations[operation].prepare(f); + persist(f, f.RealmSchema.Wallet, wallet(f, 'retained-wallet', 'Retained local recovery record')); + f.store.dispatch(f.bhr.setAutomaticCloudBackup({ appId, enabled: true })); + await tick(); + const verified = await f.sagas.run('runBackupInspection', true, f.appId, true); + if (!verified) { + try { await read(f); } + catch (error) { t.diagnostic(JSON.stringify({ directProtectedReadFailure: error.name, message: error.message })); } + t.diagnostic(JSON.stringify({ requests: server.trace })); + } + assert.equal(verified, true, + `Genuine protected opt-in must verify: ${JSON.stringify(f.store.getState().bhr.backupRepairStateByAppId)}`); + assert.equal(f.store.getState().bhr.pendingAllBackupByAppId[appId], false); + const intent = f.transport.captureBackupWriteIntent(appId); + assert.ok(intent?.protectedBase, 'Actual inspection must capture the full original protected CAS tuple'); + assert.notEqual(intent.protectedBase.namespaceId, appId, 'Protected authority is independent of the original Keeper account ID'); + server.clearTrace(); return f; +} +async function read(f) { + return f.protected.readProtectedBackup(mnemonic, undefined, f.appId); +} +async function assertComplete(f) { + const remote = await read(f); assert.equal(remote.state, 'present'); + const decoded = await f.image.decodeImage(f.encryption.generateEncryptionKey(f.seed), remote.snapshot, remote.namespaceId); + assert.equal(await f.image.compareImages(await f.repair.captureBackupImage(), decoded), 'matched'); + return remote; +} +async function committedRequest(f, operation) { + const contract = operations[operation]; let receipt; + if (operation === 'deleteBackup') receipt = await f.automatic.beginBackupDeleteOperation(appId, f.transport.currentBackupAccountSession()); + else { + const op = f.automatic.beginBackupLocalOperation(appId, f.transport.captureBackupWriteIntent(appId), f.transport.currentBackupAccountSession()); + receipt = f.automatic.commitBackupLocalOperation(op, realm => contract.mutate(realm, f)); + } + assert.equal(receipt.committed, true); assert.ok(receipt.intent?.protectedBase); + const body = await contract.body(f); + return { receipt, body, invoke: () => contract.invoke(f, body, receipt.intent) }; +} +async function twoWalletMutationRequest(f) { + const op = f.automatic.beginBackupLocalOperation(appId, f.transport.captureBackupWriteIntent(appId), f.transport.currentBackupAccountSession()); + const receipt = f.automatic.commitBackupLocalOperation(op, realm => { + realm.objectForPrimaryKey('Wallet', 'wallet-a').presentationData.name = 'First fanout local commit'; + realm.objectForPrimaryKey('Wallet', 'retained-wallet').presentationData.name = 'Second fanout local commit'; + }); + assert.equal(receipt.committed, true); assert.ok(receipt.intent); + const image = await f.repair.captureBackupImage(), key = f.encryption.generateEncryptionKey(f.seed); + const input = []; + for (const id of ['wallet-a', 'retained-wallet']) input.push({ operation: 'updateAppImage', + body: { appId, walletsObject: { [id]: await f.image.encryptRecord(key, image.wallets[id]) } } }); + return { receipt, image, invoke: () => f.automatic.enqueueAutomaticBackupOperation(receipt, input) }; +} +function assertOwnNextTuple(first, next) { + assert.equal(next.expectedRevision, first.serverRevision); + assert.equal(next.expectedGeneration, first.serverGeneration); + assert.equal(next.authEpoch, first.serverAuthEpoch); +} +function pending(f) { + assert.equal(f.store.getState().bhr.pendingAllBackupByAppId[appId], true); + assert.notEqual(f.store.getState().bhr.backupRepairStateByAppId[appId], 'verified'); +} +function recoverySagas(f) { + // Reuse only the existing real-wallet crypto setup declaration. This does not + // execute its historical public-v2 test or supply any recovery result. + const scope = { require, crypto, bip39: require('bip39'), declarations, classMembers, evaluate }; + evaluate(declarations('tests/recovery/automaticRecoveryRestore.integration.cjs', ['cryptoAndRecovery']), scope, + 'shared-production-recovery-crypto-setup.ts'); + return scope.cryptoAndRecovery(f); +} +async function blankRecoveryClient(t) { + const f = await client(t); const saga = recoverySagas(f); + f.realmDatabase.closeDatabase(); const filename = path.join(f.directory, 'protected-clean-restore.realm'); + assert.equal((await f.dbManager.initializeRealm(Buffer.alloc(64, 7), filename)).success, true); + f.store.dispatch(f.storage.setAppId('')); + f.store.dispatch(f.account.setTempDetails({ hash: f.encryption.hash256('public-disposable-passcode-fixture'), + realmId: filename, accountIdentifier: 'disposable protected restore' })); + assert.equal(f.nativeRealm().objects('KeeperApp').length, 0); + return { f, saga }; +} + +test('exact-private prerequisite: production native Realm/authority/root queue explicitly enrolls, repairs and verifies a full protected copy', async t => { + const f = await setup(t); + const authority = f.authority.deriveBackupAuthority(mnemonic); + assert.equal(authority.namespaceId, 'bk3_7SolLs5PrxK2ybpJGsah8spJqewmCskdDW_L9gnI2FY'); + authority.dispose(); + await assertComplete(f); + assert.equal(f.store.getState().bhr.protectedBackupStateByAppId[appId].status, 'present'); +}); + +for (const operation of Object.keys(operations)) { + test(`${operation}: committed native receipt uses signed v3 original tuple once and verifies the complete result`, async t => { + const f = await setup(t, operation), request = await committedRequest(f, operation); + const result = await request.invoke(); + assert.equal(result.updated, true); + // This historical public wrapper intentionally exposes only {updated}; its + // success is proved by the real store/current-proof and complete readback. + if (operation !== 'modifyLabels') assert.equal(result.backupConfirmed, true); + assert.equal(mutations().length, 1); assert.equal(mutations()[0].operation, operation); + assert.equal(mutations()[0].committed, true); + if (operation === 'deleteBackup') { + assert.equal((await read(f)).state, 'deleted'); + assert.ok(f.dbManager.getObjectById(f.RealmSchema.Wallet, 'wallet-a')); + assert.equal(f.store.getState().bhr.automaticCloudBackupByAppId[appId], false); + } else { + await assertComplete(f); + assert.equal(f.store.getState().bhr.backupRepairStateByAppId[appId], 'verified'); + } + }); +} + +test('consent-off status is signed read-only and never enrolls or uploads; explicit production opt-in alone creates the protected copy', async t => { + await server.reset(); const f = await client(t); + operations.updateAppImage.prepare(f); + const sagas = f.productionSagas(['currentBackupAppId', 'backupInspectionOwners', 'runBackupInspection', 'backupAllSignersAndVaultsWorker']); + assert.equal(await sagas.run('runBackupInspection', false, appId, true), false); + assert.equal(mutations().length, 0); assert.equal(server.trace.some(event => event.path === '/backup/v3/enroll'), false); + assert.equal(f.store.getState().bhr.automaticCloudBackupByAppId[appId], undefined); + assert.equal(await sagas.run('backupAllSignersAndVaultsWorker', { appId }), true); + assert.equal(f.store.getState().bhr.automaticCloudBackupByAppId[appId], true); + await assertComplete(f); +}); + +test('Delete worker preserves the original account, subscription, encrypted local records and older copy; explicit recreate advances the tombstone', async t => { + const f = await setup(t); const before = await f.repair.captureBackupImage(); + const appBefore = plain(f.dbManager.getObjectByIndex(f.RealmSchema.KeeperApp)); + await server.seedLegacy(await f.payloadForCurrentImage()); + const old = await server.backend.mongoose.connection.db.collection('appimages').findOne({ appId }); + const sagas = f.productionSagas(['currentBackupAppId', 'deleteBackupWorker']); + assert.equal(await sagas.run('deleteBackupWorker', { appId }), true); + const deleted = await read(f); assert.equal(deleted.state, 'deleted'); + assert.equal(deleted.snapshot.suppressLegacyFallback, true); + assert.equal(await f.image.compareImages(before, await f.repair.captureBackupImage()), 'matched'); + const appAfter = plain(f.dbManager.getObjectByIndex(f.RealmSchema.KeeperApp)); + for (const field of ['id', 'primaryMnemonic', 'primarySeed', 'subscription']) assert.deepEqual(appAfter[field], appBefore[field]); + assert.deepEqual(await server.backend.mongoose.connection.db.collection('appimages').findOne({ appId }), old); + assert.equal(await f.sagas.run('runBackupInspection', true, appId, true), true); + const recreated = await assertComplete(f); + assert.ok(recreated.snapshot.generation > deleted.snapshot.generation); + assert.equal(recreated.snapshot.authEpoch, deleted.snapshot.authEpoch); +}); + +for (const invalidation of ['local-commit', 'consent-revoked', 'A-to-B-to-A']) { + test(`a queued original receipt invalidated by ${invalidation} never borrows fresh authority or uploads`, async t => { + const f = await setup(t), request = await committedRequest(f, 'updateAppImage'); + const gate = barrier(); t.after(gate.release); + const preceding = f.transport.withBackupSession(appId, () => gate.promise); + const writing = request.invoke(); await tick(); + if (invalidation === 'local-commit') persist(f, f.RealmSchema.Tags, + f.defaults(f.RealmSchema.Tags, { id: 'late', type: 'TX', ref: 'public', label: 'Later local commit' })); + if (invalidation === 'consent-revoked') f.store.dispatch(f.bhr.setAutomaticCloudBackup({ appId, enabled: false })); + if (invalidation === 'A-to-B-to-A') { await f.selectAccount('disposable-other-account'); await f.selectAccount(appId); } + gate.release(); await preceding; const result = await writing; + assert.notEqual(result.backupConfirmed, true); assert.equal(mutations().length, 0); + assert.equal(f.dbManager.getObjectById(f.RealmSchema.Wallet, 'wallet-a').presentationData.name, 'Committed wallet'); + }); +} + +test('a genuine local commit without its original protected baseline remains local and cannot obtain upload authority later', async t => { + const f = await setup(t); f.transport.clearVerifiedRemoteRevision(appId); + const op = f.automatic.beginBackupLocalOperation(appId, undefined, f.transport.currentBackupAccountSession()); + const receipt = f.automatic.commitBackupLocalOperation(op, realm => operations.updateAppImage.mutate(realm, f)); + assert.equal(receipt.committed, true); assert.equal(receipt.intent, undefined); + const result = await f.automatic.enqueueAutomaticBackupOperation(receipt, [{ operation: 'updateAppImage', body: await operations.updateAppImage.body(f) }]); + assert.notEqual(result.backupConfirmed, true); assert.equal(mutations().length, 0); + assert.equal(f.dbManager.getObjectById(f.RealmSchema.Wallet, 'wallet-a').presentationData.name, 'Committed wallet'); +}); + +test('a peer commit between original snapshot and CAS produces real409; local rename and remote-only recovery both survive', async t => { + const f = await setup(t), request = await committedRequest(f, 'updateAppImage'); + const base = request.receipt.intent.protectedBase; + server.armFault({ path: '/backup/v3/snapshot', async beforeReply() { + await f.protected.protectedBackupMutation(mnemonic, base, 'updateAppImage', { + walletsObject: { peer: await f.image.encryptRecord(f.encryption.generateEncryptionKey(f.seed), + plain(wallet(f, 'peer', 'Remote-only recovery'))) }, + }); + } }); + const result = await request.invoke(); assert.notEqual(result.backupConfirmed, true); + assert.ok(mutations().some(event => event.status === 409)); + const remote = await read(f); assert.ok(remote.snapshot.appImage.wallets.peer); + assert.equal(f.dbManager.getObjectById(f.RealmSchema.Wallet, 'wallet-a').presentationData.name, 'Committed wallet'); + pending(f); +}); + +for (const mode of ['lost-reply', 'malformed-json', 'malformed-ack']) { + test(`${mode} after an actual committed write never retries ciphertext or claims Verified from an unacknowledged tuple`, async t => { + const f = await setup(t), request = await committedRequest(f, 'updateAppImage'); + server.armFault({ path: '/backup/v3/mutate', operation: 'updateAppImage', + ...(mode === 'malformed-ack' ? { mutateReply: value => ({ ...value, newGeneration: value.newGeneration + 1 }) } : { mode }) }); + const result = await request.invoke(); assert.notEqual(result.backupConfirmed, true); pending(f); + assert.equal(mutations().length, 1); assert.equal(mutations()[0].committed, true); + const remote = await read(f); assert.equal(remote.snapshot.generation, request.receipt.intent.protectedBase.generation + 1); + await request.invoke(); assert.equal(mutations().length, 1, 'Consumed receipt must not resubmit uncertain ciphertext'); + assert.equal(f.dbManager.getObjectById(f.RealmSchema.Wallet, 'wallet-a').presentationData.name, 'Committed wallet'); + }); +} + +test('a malformed final authenticated namespace readback cannot become Verified after the real CAS commit', async t => { + const f = await setup(t), request = await committedRequest(f, 'updateAppImage'); + server.armFault({ path: '/backup/v3/snapshot', after: 1, + mutateReply: value => ({ ...value, namespaceId: 'bk3_invalid-namespace' }) }); + const result = await request.invoke(); assert.notEqual(result.backupConfirmed, true); pending(f); + assert.equal(mutations().length, 1); assert.equal(mutations()[0].committed, true); +}); + +test('real native insertion followed by an injected transaction failure leaves the full remote tuple unchanged and local data intact', async t => { + const f = await setup(t), request = await committedRequest(f, 'updateAppImage'); + const prototype = Object.getPrototypeOf(server.backend.connection.db.collection('protectedBackupRecords')); + const original = prototype.insertMany; let injected = false; + prototype.insertMany = async function(records, ...args) { + if (!injected && this.collectionName === 'protectedBackupRecords' && records.length) { + injected = true; await original.call(this, records.slice(0, 1), ...args); + throw Error('Disclosed fixture failure after one real insert'); + } + return original.call(this, records, ...args); + }; + let result; + try { result = await request.invoke(); } finally { prototype.insertMany = original; } + assert.equal(injected, true); assert.notEqual(result.backupConfirmed, true); pending(f); + const remote = await read(f); assert.equal(remote.snapshot.revision, request.receipt.intent.protectedBase.revision); + assert.equal(remote.snapshot.generation, request.receipt.intent.protectedBase.generation); + assert.equal(f.dbManager.getObjectById(f.RealmSchema.Wallet, 'wallet-a').presentationData.name, 'Committed wallet'); +}); + +test('a real native change at the final Redux verification commit prevents a Verified claim', async t => { + const f = await setup(t), request = await committedRequest(f, 'updateAppImage'); let injected = false; + f.setBeforeDispatch(action => { + if (!injected && action.type === f.bhr.commitBackupInspection.type) { + injected = true; persist(f, f.RealmSchema.Tags, + f.defaults(f.RealmSchema.Tags, { id: 'final-race', type: 'TX', ref: 'public', label: 'Committed during verification' })); + } + }); + const result = await request.invoke(); assert.equal(injected, true); + assert.notEqual(result.backupConfirmed, true); pending(f); assert.equal(mutations().length, 1); +}); + +test('actual BHR producer reads committed native IDs, preserving local-first data through the real signed queue', async t => { + const f = await setup(t), request = await committedRequest(f, 'updateAppImage'); + const producer = f.productionSagas(['currentBackupAppId', 'buildAutomaticAppMutation', 'queueCommittedBackupWorker', 'updateAppImageWorker']); + const result = await producer.run('updateAppImageWorker', { + payload: { wallets: [wallet(f, 'wallet-a', 'Stale action presentation')] }, originAppId: appId, + originBackupAccountSession: f.transport.currentBackupAccountSession(), committedOperation: request.receipt, + }); + assert.equal(result.backupConfirmed, true); assert.equal(mutations().length, 1); + const remote = await assertComplete(f); + const decoded = await f.image.decodeImage(f.encryption.generateEncryptionKey(f.seed), remote.snapshot, remote.namespaceId); + assert.equal(decoded.wallets['wallet-a'].presentationData.name, 'Committed wallet'); +}); + +test('the actual Recovery Key worker restores a protected full image into blank encrypted Realm without changing Keeper identity or granting consent', async t => { + const source = await setup(t); const sourceCrypto = recoverySagas(source); + persist(source, source.RealmSchema.Signer, sourceCrypto.scope.setupRecoveryKeySigningKey(mnemonic)); + assert.equal(await source.sagas.run('runBackupInspection', true, appId, true), true); + const defaultNode = plain(source.load('src/services/electrum/predefinedNodes').predefinedTestnetNodes[0]); + const customNode = source.defaults(source.RealmSchema.NodeConnect, { id: 73, + host: 'custom-recovery-node.invalid', port: '50002', useSSL: true, isConnected: true, + networkType: source.enums.NetworkType.MAINNET }); + const nodeSession = source.transport.currentBackupAccountSession(); + const nodeOperation = source.automatic.beginBackupLocalOperation(appId, source.transport.captureBackupWriteIntent(appId), nodeSession); + const nodeReceipt = source.automatic.commitBackupLocalOperation(nodeOperation, realm => { + // Persist real default/custom node records only. No connection/provider + // worker is imported or invoked by this backup/restore proof. + realm.create(source.RealmSchema.NodeConnect, defaultNode); + realm.create(source.RealmSchema.NodeConnect, customNode); + }); + assert.equal(nodeReceipt.committed, true); + const nodeProducer = source.productionSagas(['currentBackupAppId', 'buildAutomaticAppMutation', + 'queueCommittedBackupWorker', 'updateAppImageWorker']); + server.clearTrace(); + const nodeResult = await nodeProducer.run('updateAppImageWorker', { payload: { updateNodes: true }, + originAppId: appId, originBackupAccountSession: nodeSession, committedOperation: nodeReceipt }); + assert.equal(nodeResult.backupConfirmed, true); + assert.equal(mutations().length, 1); assert.equal(mutations()[0].replaceNodes, true); + assert.equal(mutations()[0].nodeCount, 2); + const uploaded = await assertComplete(source); assert.equal(uploaded.snapshot.appImage.nodes.length, 2); + const expected = await source.repair.captureBackupImage(); + const { f, saga } = await blankRecoveryClient(t); server.clearTrace(); + await saga.run('getAppImageWorker', { payload: { primaryMnemonic: mnemonic } }); + assert.equal(f.store.getState().storage.appCreated, true, f.store.getState().bhr.appImageError); + assert.equal(f.store.getState().storage.appId, appId); + assert.equal(f.dbManager.getObjectByIndex(f.RealmSchema.KeeperApp).primaryMnemonic, mnemonic); + assert.equal(await f.image.compareImages(expected, await f.repair.captureBackupImage()), 'matched'); + const restoredNodes = Array.from(f.dbManager.getCollection(f.RealmSchema.NodeConnect)); + assert.equal(restoredNodes.length, 2); + assert.equal(restoredNodes.find(node => node.id === defaultNode.id).host, defaultNode.host); + assert.equal(restoredNodes.find(node => node.id === customNode.id).host, customNode.host); + assert.ok(restoredNodes.every(node => node.isConnected === false), 'Restore does not revive stale connection flags'); + assert.notEqual(f.store.getState().bhr.automaticCloudBackupByAppId[appId], true); + assert.equal(f.store.getState().bhr.recoveryDiscovery.provenance.source, 'protected'); + assert.equal(server.trace.some(event => event.legacyRead), false); assert.equal(mutations().length, 0); +}); + +test('never-enrolled protected discovery is typed and creates no local account, enrollment, automatic legacy read or upload', async t => { + await server.reset(); const { f, saga } = await blankRecoveryClient(t); server.clearTrace(); + await saga.run('getAppImageWorker', { payload: { primaryMnemonic: mnemonic } }); + assert.equal(f.store.getState().bhr.recoveryDiscovery.status, 'protected-absent'); + assert.equal(f.nativeRealm().objects('KeeperApp').length, 0); + assert.equal(server.trace.some(event => event.legacyRead || event.path === '/backup/v3/enroll'), false); + assert.equal(mutations().length, 0); +}); + +test('a protected tombstone never silently restores the retained old copy; explicit older selection reads it without protected writes or consent', async t => { + const source = await setup(t), sourceCrypto = recoverySagas(source); + persist(source, source.RealmSchema.Signer, sourceCrypto.scope.setupRecoveryKeySigningKey(mnemonic)); + assert.equal(await source.sagas.run('runBackupInspection', true, appId, true), true); + await server.seedLegacy(await source.payloadForCurrentImage()); + assert.equal((await (await committedRequest(source, 'deleteBackup')).invoke()).backupConfirmed, true); + const { f, saga } = await blankRecoveryClient(t); server.clearTrace(); + await saga.run('getAppImageWorker', { payload: { primaryMnemonic: mnemonic } }); + assert.equal(f.store.getState().bhr.recoveryDiscovery.status, 'protected-deleted'); + assert.equal(f.nativeRealm().objects('KeeperApp').length, 0); + assert.equal(server.trace.some(event => event.legacyRead), false); + await saga.run('getAppImageWorker', { payload: { primaryMnemonic: mnemonic, + selection: { source: 'legacy-older', olderCopyConfirmed: true } } }); + assert.equal(f.store.getState().storage.appCreated, true, f.store.getState().bhr.appImageError); + assert.equal(f.store.getState().storage.appId, appId); + assert.equal(f.store.getState().bhr.recoveryDiscovery.provenance.source, 'legacy-older'); + assert.equal(f.store.getState().bhr.recoveryDiscovery.provenance.legacyMayPredateDeletion, true); + assert.notEqual(f.store.getState().bhr.automaticCloudBackupByAppId[appId], true); + assert.equal(server.trace.filter(event => event.legacyRead).length, 1); + assert.equal(mutations().length, 0); assert.equal((await read(source)).state, 'deleted'); +}); + +test('a malformed protected discovery reports read-error and never falls back to an available older copy', async t => { + const source = await setup(t); await server.seedLegacy(await source.payloadForCurrentImage()); + const { f, saga } = await blankRecoveryClient(t); server.clearTrace(); + server.armFault({ path: '/backup/v3/snapshot', mutateReply: value => ({ ...value, namespaceId: 'bk3_wrong' }) }); + await saga.run('getAppImageWorker', { payload: { primaryMnemonic: mnemonic } }); + assert.equal(f.store.getState().bhr.recoveryDiscovery.status, 'read-error'); + assert.equal(f.nativeRealm().objects('KeeperApp').length, 0); + assert.equal(server.trace.some(event => event.legacyRead), false); assert.equal(mutations().length, 0); +}); + +test('hosted enablement remains false for explicit enrollment and repair despite valid local authority', async t => { + await server.reset(); const f = await client(t); const previous = f.config.RELAY; + f.config.RELAY = 'https://fixture.invalid/'; + try { + await assert.rejects(f.protected.enrollProtectedBackup(mnemonic), /not enabled/); + await assert.rejects(f.protected.protectedBackupMutation(mnemonic, {}, 'repairAppBackup', {}), /not enabled/); + } finally { f.config.RELAY = previous; } + assert.equal(server.trace.length, 0); +}); + +test('a real committed mnemonic change invalidates queued authority before any old ciphertext is sent', async t => { + const f = await setup(t), request = await committedRequest(f, 'updateAppImage'); + const gate = barrier(); t.after(gate.release); + const preceding = f.transport.withBackupSession(appId, () => gate.promise); + const writing = request.invoke(); await tick(); + const other = require('bip39').entropyToMnemonic('11'.repeat(16)); + assert.equal(f.dbManager.updateObjectById(f.RealmSchema.KeeperApp, appId, { primaryMnemonic: other }), true); + gate.release(); await preceding; const result = await writing; + assert.notEqual(result.backupConfirmed, true); assert.equal(mutations().length, 0); + assert.equal(f.dbManager.getObjectById(f.RealmSchema.Wallet, 'wallet-a').presentationData.name, 'Committed wallet'); +}); + +test('a stored seed inconsistent with the Recovery Key cannot enroll or repair under an otherwise current original Keeper account', async t => { + const f = await setup(t); const walletBefore = plain(f.dbManager.getObjectById(f.RealmSchema.Wallet, 'wallet-a')); + assert.equal(f.dbManager.updateObjectById(f.RealmSchema.KeeperApp, appId, { primarySeed: 'ff'.repeat(64) }), true); + assert.equal(await f.sagas.run('runBackupInspection', true, appId, true), false); + assert.equal(server.trace.length, 0, 'Account binding must reject before any signed request'); + assert.deepEqual(plain(f.dbManager.getObjectById(f.RealmSchema.Wallet, 'wallet-a')), walletBefore); +}); + +test('two real fanout mutations use only their own preceding full ACK tuple and verify the complete committed native image', async t => { + const f = await setup(t), request = await twoWalletMutationRequest(f); + const result = await request.invoke(); assert.equal(result.backupConfirmed, true); + const writes = mutations(); assert.equal(writes.length, 2); + assert.equal(writes[0].expectedRevision, request.receipt.intent.protectedBase.revision); + assert.equal(writes[0].expectedGeneration, request.receipt.intent.protectedBase.generation); + assert.equal(writes[0].authEpoch, request.receipt.intent.protectedBase.authEpoch); + assertOwnNextTuple(writes[0], writes[1]); + assert.equal(writes[1].serverGeneration, writes[0].serverGeneration + 1); + const remote = await assertComplete(f); assert.equal(remote.snapshot.revision, writes[1].serverRevision); + assert.equal(remote.snapshot.generation, writes[1].serverGeneration); + assert.equal(f.store.getState().bhr.backupRepairStateByAppId[appId], 'verified'); +}); + +test('a real peer CAS after ACK1 rejects fanout member2 without rebasing or resubmitting; first commit and all local data survive', async t => { + const f = await setup(t), request = await twoWalletMutationRequest(f); + server.armFault({ path: '/backup/v3/mutate', async beforeReply({ body }) { + await f.protected.protectedBackupMutation(mnemonic, { namespaceId: request.receipt.intent.protectedBase.namespaceId, + revision: body.newRevision, generation: body.newGeneration, authEpoch: body.authEpoch }, 'updateAppImage', { + walletsObject: { peer: await f.image.encryptRecord(f.encryption.generateEncryptionKey(f.seed), plain(wallet(f, 'peer', 'Peer recovery'))) }, + }); + } }); + const result = await request.invoke(); assert.notEqual(result.backupConfirmed, true); pending(f); + const writes = mutations(); assert.equal(writes.length, 3); + assert.deepEqual(writes.map(event => event.status), [200, 200, 409]); + assertOwnNextTuple(writes[0], writes[2]); + assert.notEqual(writes[2].expectedRevision, writes[1].serverRevision, 'Member2 cannot borrow the peer revision'); + const remote = await read(f), decoded = await f.image.decodeImage(f.encryption.generateEncryptionKey(f.seed), remote.snapshot, remote.namespaceId); + assert.equal(decoded.wallets['wallet-a'].presentationData.name, 'First fanout local commit'); + assert.equal(decoded.wallets['retained-wallet'].presentationData.name, 'Retained local recovery record'); + assert.ok(decoded.wallets.peer); + assert.equal(await f.image.compareImages(request.image, await f.repair.captureBackupImage()), 'matched'); + await request.invoke(); assert.equal(mutations().length, 3, 'Consumed partial fanout cannot be resubmitted'); +}); + +test('a lost ACK after real fanout member2 preserves both effects but never turns matching readback into acknowledgement or a retry', async t => { + const f = await setup(t), request = await twoWalletMutationRequest(f); + server.armFault({ path: '/backup/v3/mutate', after: 1, mode: 'lost-reply' }); + const result = await request.invoke(); assert.notEqual(result.backupConfirmed, true); pending(f); + const writes = mutations(); assert.equal(writes.length, 2); assert.ok(writes.every(event => event.committed)); + assertOwnNextTuple(writes[0], writes[1]); + await assertComplete(f); pending(f); + assert.equal(await f.image.compareImages(request.image, await f.repair.captureBackupImage()), 'matched'); + await request.invoke(); assert.equal(mutations().length, 2, 'Lost final ACK cannot renew an old receipt'); +}); + +test('a changed final vault-map fingerprint cannot hide behind an unchanged revision or become Verified after a real vault CAS', async t => { + const f = await setup(t, 'updateVaultImage'), request = await committedRequest(f, 'updateVaultImage'); + let injected = false; + server.armFault({ path: '/backup/v3/snapshot', after: 1, mutateReply: value => { + assert.ok(value.vaultMaps.length > 0, 'Exercise the actual complete vault-map projection'); + assert.notEqual(value.vaultMaps[0].xfpHash, 'f'.repeat(64)); injected = true; + return { ...value, vaultMaps: value.vaultMaps.map((map, index) => index === 0 + ? { ...map, xfpHash: 'f'.repeat(64) } : map) }; + } }); + const result = await request.invoke(); assert.equal(injected, true); + assert.notEqual(result.backupConfirmed, true); pending(f); + assert.equal(mutations().length, 1); assert.equal(mutations()[0].committed, true); + assert.equal(f.dbManager.getObjectById(f.RealmSchema.Vault, 'vault-a').presentationData.name, 'Committed vault'); +}); + +for (const malformed of ['label-wire-id', 'wallet-map-alias']) { + test(`owned signed opaque ${malformed} is accepted by the real backend but protected recovery rejects it before any Realm write`, async t => { + const source = await setup(t), current = await read(source); + const key = source.encryption.generateEncryptionKey(source.seed); + const operation = malformed === 'label-wire-id' ? 'modifyLabels' : 'updateAppImage'; + const data = malformed === 'label-wire-id' + ? { addLabels: [{ id: 'f'.repeat(64), content: await source.image.encryptRecord(key, + source.defaults(source.RealmSchema.Tags, { id: 'different-plaintext-label', type: 'TX', ref: 'public', label: 'Synthetic protected label' })) }] } + : { walletsObject: { 'wrong-wire-wallet-id': await source.image.encryptRecord(key, + plain(wallet(source, 'different-plaintext-wallet-id', 'Synthetic protected wallet'))) } }; + // This is an independently admitted, genuinely signed owner write. The + // backend stores opaque ciphertext and cannot prove its plaintext record ID. + const ack = await source.protected.protectedBackupMutation(mnemonic, + source.protected.protectedBackupBase(current.snapshot), operation, data); + assert.equal(ack.updated, true); + const { f, saga } = await blankRecoveryClient(t); server.clearTrace(); + await saga.run('getAppImageWorker', { payload: { primaryMnemonic: mnemonic } }); + assert.equal(f.store.getState().bhr.recoveryDiscovery.status, 'read-error'); + assert.notEqual(f.store.getState().storage.appCreated, true); + assert.equal(f.store.getState().storage.appId, ''); + for (const name of ['KeeperApp', 'Wallet', 'Signer', 'Vault', 'Tags', 'NodeConnect']) + assert.equal(f.nativeRealm().objects(name).length, 0, `Protected ${malformed} must fail before ${name} persistence`); + assert.equal(server.trace.some(event => event.legacyRead), false); + assert.equal(mutations().length, 0, 'A failed protected recovery cannot repair or replace its source'); + }); +} + +test('actual NEW-vault producer includes committed scheme metadata and verifies a genuinely new protected row', async t => { + const f = await setup(t, 'updateVaultImage'), id = 'new-vault-production-caller'; + const before = await read(f); assert.equal(before.snapshot.allVaultImages.some(vault => vault.vaultId === id), false); + const op = f.automatic.beginBackupLocalOperation(appId, f.transport.captureBackupWriteIntent(appId), f.transport.currentBackupAccountSession()); + const receipt = f.automatic.commitBackupLocalOperation(op, realm => + realm.create('Vault', builders.vault(f, id), Realm.UpdateMode.Modified)); + assert.equal(receipt.committed, true); assert.ok(receipt.intent); + const producer = f.productionSagas(['currentBackupAppId', 'buildAutomaticVaultMutation', + 'queueCommittedBackupWorker', 'updateVaultImageWorker']); + server.clearTrace(); + const result = await producer.run('updateVaultImageWorker', { + payload: { vault: { id }, isUpdate: false }, originAppId: appId, + originBackupAccountSession: f.transport.currentBackupAccountSession(), committedOperation: receipt, + }); + if (!result.backupConfirmed) t.diagnostic(JSON.stringify({ writes: mutations(), + phase: f.store.getState().bhr.backupRepairStateByAppId[appId], localVaultRetained: !!f.dbManager.getObjectById(f.RealmSchema.Vault, id) })); + assert.equal(result.backupConfirmed, true); + assert.equal(mutations().length, 1); assert.equal(mutations()[0].committed, true); + const remote = await assertComplete(f), value = remote.snapshot.allVaultImages.find(vault => vault.vaultId === id); + assert.deepEqual(value.scheme, plain(f.dbManager.getObjectById(f.RealmSchema.Vault, id).scheme)); +}); + +test('same-account protected recovery revokes inherited canonical consent, advances its epoch and invalidates old local operation scope', async t => { + const f = await setup(t), saga = recoverySagas(f); + persist(f, f.RealmSchema.Signer, saga.scope.setupRecoveryKeySigningKey(mnemonic)); + assert.equal(await f.sagas.run('runBackupInspection', true, appId, true), true); + const oldEpoch = f.transport.backupConsentEpoch(appId), oldRevision = f.transport.backupRevision(appId); + const oldScope = f.automatic.beginBackupLocalOperation(appId, f.transport.captureBackupWriteIntent(appId), f.transport.currentBackupAccountSession()); + assert.equal(f.store.getState().bhr.automaticCloudBackupByAppId[appId], true); + f.store.dispatch(f.account.setTempDetails({ hash: f.encryption.hash256('public-disposable-passcode-fixture'), + realmId: f.nativeRealm().path, accountIdentifier: 'same account recovery' })); + server.clearTrace(); + await saga.run('getAppImageWorker', { payload: { primaryMnemonic: mnemonic } }); + assert.equal(f.store.getState().storage.appCreated, true, f.store.getState().bhr.appImageError); + assert.equal(f.store.getState().storage.appId, appId); + assert.equal(f.store.getState().bhr.automaticCloudBackupByAppId[appId], false); + assert.ok(f.transport.backupConsentEpoch(appId) > oldEpoch); + assert.ok(f.transport.backupRevision(appId) > oldRevision); + assert.equal(f.transport.captureBackupWriteIntent(appId), undefined); + let attempted = false; + const rejected = f.automatic.commitBackupLocalOperation(oldScope, () => { attempted = true; }); + assert.equal(rejected.committed, false); assert.equal(attempted, false); + assert.equal(mutations().length, 0); assert.equal(server.trace.some(event => event.path === '/backup/v3/enroll'), false); +}); + +test('different-selected-account older recovery disables the target saved true consent without borrowing the selected account or auto-uploading', async t => { + const source = await setup(t), sourceCrypto = recoverySagas(source); + persist(source, source.RealmSchema.Signer, sourceCrypto.scope.setupRecoveryKeySigningKey(mnemonic)); + assert.equal(await source.sagas.run('runBackupInspection', true, appId, true), true); + await server.seedLegacy(await source.payloadForCurrentImage()); + const f = await client(t); + f.store.dispatch(f.bhr.setAutomaticCloudBackup({ appId, enabled: true })); + const targetEpoch = f.transport.backupConsentEpoch(appId); + const otherMnemonic = require('bip39').entropyToMnemonic('22'.repeat(16)); + const otherSeed = require('bip39').mnemonicToSeedSync(otherMnemonic); + const otherId = crypto.createHash('sha256').update(otherSeed).digest('hex'); + await f.selectAccount(otherId); + assert.equal(f.dbManager.updateObjectById(f.RealmSchema.KeeperApp, otherId, { + primaryMnemonic: otherMnemonic, primarySeed: otherSeed.toString('hex'), + imageEncryptionKey: f.encryption.generateEncryptionKey(otherSeed.toString('hex')), + }), true); + f.store.dispatch(f.bhr.setAutomaticCloudBackup({ appId: otherId, enabled: true })); + assert.equal(f.store.getState().bhr.automaticCloudBackupByAppId[appId], true); + const originSession = f.transport.currentBackupAccountSession(); + const saga = recoverySagas(f); + f.realmDatabase.closeDatabase(); const filename = path.join(f.directory, 'older-copy-new-realm.realm'); + assert.equal((await f.dbManager.initializeRealm(Buffer.alloc(64, 7), filename)).success, true); + f.store.dispatch(f.account.setTempDetails({ hash: f.encryption.hash256('public-disposable-passcode-fixture'), + realmId: filename, accountIdentifier: 'older target recovery' })); + assert.equal(f.store.getState().storage.appId, otherId); + server.clearTrace(); + await saga.run('getAppImageWorker', { payload: { primaryMnemonic: mnemonic, + selection: { source: 'legacy-older', olderCopyConfirmed: true } } }); + assert.equal(f.store.getState().storage.appCreated, true, f.store.getState().bhr.appImageError); + assert.equal(f.store.getState().storage.appId, appId); + assert.equal(f.dbManager.getObjectByIndex(f.RealmSchema.KeeperApp).id, appId); + assert.ok(f.transport.currentBackupAccountSession() > originSession); + assert.equal(f.store.getState().bhr.automaticCloudBackupByAppId[appId], false); + assert.equal(f.store.getState().bhr.automaticCloudBackupByAppId[otherId], true, 'The other account consent must stay scoped'); + assert.ok(f.transport.backupConsentEpoch(appId) > targetEpoch); + assert.equal(f.transport.captureBackupWriteIntent(appId), undefined); + assert.equal(server.trace.filter(event => event.legacyRead).length, 1); + assert.equal(mutations().length, 0); assert.equal(server.trace.some(event => event.path === '/backup/v3/enroll'), false); +}); diff --git a/tests/recovery/recoveryAccountSession.test.cjs b/tests/recovery/recoveryAccountSession.test.cjs index 8d210c9844..7539d013b6 100644 --- a/tests/recovery/recoveryAccountSession.test.cjs +++ b/tests/recovery/recoveryAccountSession.test.cjs @@ -121,7 +121,8 @@ test('successful recovery returns its session before adding the Recovery Key sig return returnedSession; }; - await f.run('getAppImageWorker', { payload: { primaryMnemonic: 'disposable mnemonic' } }); + await f.run('getAppImageWorker', { payload: { primaryMnemonic: 'disposable mnemonic', + selection: { source: 'legacy-older', olderCopyConfirmed: true } } }); assert.equal(returnedSession, session); assert.ok(f.collections.Signer.some((row) => row.masterFingerprint === 'RECOVERY')); diff --git a/tests/recovery/recoveryErrorFeedback.test.cjs b/tests/recovery/recoveryErrorFeedback.test.cjs index 7071af5184..72bbb0b276 100644 --- a/tests/recovery/recoveryErrorFeedback.test.cjs +++ b/tests/recovery/recoveryErrorFeedback.test.cjs @@ -12,7 +12,7 @@ const source = ts.createSourceFile(file, let callback; function visit(node) { if (ts.isCallExpression(node) && node.expression.getText(source) === 'useEffect' && - node.arguments[1]?.getText(source) === '[appImageError]') + node.arguments[0]?.getText(source).includes('if (appImageError && isRecovery)')) callback = node.arguments[0].getText(source); ts.forEachChild(node, visit); } @@ -23,10 +23,12 @@ const code = ts.transpileModule(`(${callback})()`, { }).outputText; const seed = require('../../src/context/Localization/language/en.json').seed; -function feedback(appImageError) { +function feedback(appImageError, recoveryDiscovery = { status: 'idle', source: 'protected' }, isRecovery = true) { const observed = { loading: true, invalid: false, network: false, messages: [], actions: [] }; vm.runInNewContext(code, { appImageError, + recoveryDiscovery, + isRecovery, seed, React: { createElement: (type) => ({ type }) }, ToastErrorIcon: 'error-icon', @@ -65,7 +67,8 @@ for (const error of ['Recovery data unavailable', 'Incomplete backup response', assert.equal(result.invalid, false); assert.equal(result.network, false); assert.deepEqual(result.messages, [seed.recoveryIncomplete]); - assert.match(result.messages[0], /try again/i); + assert.match(result.messages[0], /not completed/i); + assert.match(result.messages[0], /No older copy was selected automatically/i); assert.doesNotMatch(result.messages[0], /invalid (seed|mnemonic|recovery key)/i); assert.deepEqual(result.actions, [{ type: 'setAppImageError', payload: '' }]); }); @@ -79,3 +82,174 @@ test('empty recovery error does not interrupt loading or display an error', () = assert.deepEqual(result.messages, []); assert.deepEqual(result.actions, []); }); + + +test('typed read errors keep the provenance modal as the only error surface', () => { + const result = feedback('Network Error', { status: 'read-error', source: 'protected' }); + assert.equal(result.invalid, false); + assert.equal(result.network, false); + assert.deepEqual(result.messages, []); + assert.deepEqual(result.actions, [{ type: 'setAppImageError', payload: '' }]); +}); + +test('non-recovery seed entry does not show protected backup errors', () => { + const result = feedback('Network Error', { status: 'read-error', source: 'protected' }, false); + assert.deepEqual(result.messages, []); + assert.deepEqual(result.actions, []); + assert.equal(result.network, false); +}); + +function productionExpression(name) { + let expression; + function find(node) { + if (ts.isVariableDeclaration(node) && node.name.getText(source) === name) + expression = node.initializer?.getText(source); + ts.forEachChild(node, find); + } + find(source); + assert.ok(expression, `Production ${name} exists`); + return ts.transpileModule(`(${expression})`, { + compilerOptions: { target: ts.ScriptTarget.ES2020, jsx: ts.JsxEmit.React }, + }).outputText; +} + +function discoveryEffect() { + let expression; + function find(node) { + if (ts.isCallExpression(node) && node.expression.getText(source) === 'useEffect' && + node.arguments[0]?.getText(source).includes("recoveryDiscovery.status === 'restored'")) + expression = node.arguments[0].getText(source); + ts.forEachChild(node, find); + } + find(source); + assert.ok(expression, 'Production typed recovery completion effect exists'); + return ts.transpileModule(`(${expression})()`, { + compilerOptions: { target: ts.ScriptTarget.ES2020, jsx: ts.JsxEmit.React }, + }).outputText; +} + +function discovery(recoveryDiscovery, selectedSource = 'protected', activeAppId = 'fixture-account') { + const result = { loading: true, success: false, chooser: false, source: null, messages: [], actions: [] }; + vm.runInNewContext(discoveryEffect(), { + recoveryDiscovery, recoveryLoading: true, isRecovery: true, activeAppId, + appImageError: '', pendingRecoverySource: { current: selectedSource }, seed, + React: { createElement: (type) => ({ type }) }, ToastErrorIcon: 'error-icon', + setRecoveryLoading: (value) => { result.loading = value; }, + setRecoverySuccessModal: (value) => { result.success = value; }, + setShowRecoveryDiscovery: (value) => { result.chooser = value; }, + setRecoverySuccessSource: (value) => { result.source = value; }, + showToast: (message) => result.messages.push(message), + dispatch: (action) => result.actions.push(action), + resetSeedWords: () => ({ type: 'resetSeedWords' }), + resetPasscodeTimeout: () => ({ type: 'resetPasscodeTimeout' }), + }); + return result; +} + +for (const status of ['protected-absent', 'protected-empty', 'protected-deleted', 'read-error']) { + test(`${status} opens its explanation without fetching an older copy or clearing words`, () => { + const result = discovery({ status, source: 'protected' }); + assert.equal(result.loading, false); + assert.equal(result.chooser, true); + assert.equal(result.success, false); + assert.deepEqual(result.actions, []); + const available = vm.runInNewContext(productionExpression('olderCopyAvailable'), { + recoveryDiscovery: { status, source: 'protected' }, + }); + assert.equal(available, status !== 'read-error', 'read/auth errors cannot select older recovery'); + }); +} + +test('typed invalid mnemonic never opens the backup-source chooser before the legacy error signal arrives', () => { + const result = discovery({ status: 'read-error', source: 'protected', error: 'Invalid mnemonic' }); + assert.equal(result.loading, false); + assert.equal(result.chooser, false); + assert.equal(result.success, false); + assert.deepEqual(result.actions, []); +}); + +test('found-but-not-restored protected records never report recovery success', () => { + const result = discovery({ status: 'protected-found', source: 'protected' }); + assert.equal(result.loading, true); + assert.equal(result.success, false); + assert.deepEqual(result.actions, []); +}); + +for (const source of ['protected', 'legacy-older']) { + test(`complete ${source} restoration reports its matching current-account provenance`, () => { + const result = discovery({ status: 'restored', source, provenance: { + source, keeperAppId: 'fixture-account', legacyMayPredateDeletion: source === 'legacy-older', + legacyCopiesMayRemain: true, + } }, source); + assert.equal(result.success, true); + assert.equal(result.source, source); + assert.deepEqual(result.actions, [{ type: 'resetSeedWords' }, { type: 'resetPasscodeTimeout' }]); + }); +} + +for (const provenance of [undefined, + { source: 'legacy-older', keeperAppId: 'fixture-account' }, + { source: 'protected', keeperAppId: 'other-account' }]) { + test(`missing, stale-account or unexpected-source provenance withholds success: ${JSON.stringify(provenance)}`, () => { + const result = discovery({ status: 'restored', source: 'protected', provenance }); + assert.equal(result.loading, false); + assert.equal(result.success, false); + assert.deepEqual(result.actions, [], 'entered Recovery Key is retained'); + assert.deepEqual(result.messages, [seed.recoveryIncomplete]); + }); +} + +test('only the deliberate older-copy callback supplies explicit legacy confirmation', () => { + const actions = [], pendingRecoverySource = { current: 'protected' }, ui = []; + const callback = vm.runInNewContext(productionExpression('recoverOlderCopy'), { + seedWords: [{ name: 'disposable' }, { name: 'fixture' }], isForgot: false, + pendingRecoverySource, + setShowRecoveryDiscovery: (value) => ui.push(['chooser', value]), + setRecoveryLoading: (value) => ui.push(['loading', value]), + dispatch: (action) => actions.push(action), + resetRecoveryDiscovery: () => ({ type: 'resetRecoveryDiscovery' }), + getAppImage: (primaryMnemonic, isForgot, selection) => ({ + type: 'getAppImage', primaryMnemonic, isForgot, selection, + }), + }); + assert.deepEqual(actions, [], 'rendering a choice must not start older recovery'); + callback(); + assert.equal(pendingRecoverySource.current, 'legacy-older'); + assert.equal(actions.length, 2); + assert.equal(actions[1].type, 'getAppImage'); + assert.equal(actions[1].selection.source, 'legacy-older'); + assert.equal(actions[1].selection.olderCopyConfirmed, true); + assert.deepEqual(ui, [['chooser', false], ['loading', true]]); +}); + + +test('read-error acknowledgement says OK and only closes the explanation', () => { + let label; + function find(node) { + if (ts.isJsxSelfClosingElement(node) && node.tagName.getText(source) === 'KeeperModal' && + node.attributes.properties.some((prop) => prop.name?.getText(source) === 'visible' && + prop.initializer?.expression?.getText(source) === 'isRecovery && showRecoveryDiscovery')) + label = node.attributes.properties.find((prop) => prop.name?.getText(source) === 'buttonText') + .initializer.expression.getText(source); + ts.forEachChild(node, find); + } + find(source); + assert.ok(label); + const labelCode = ts.transpileModule(`(${label})`, { compilerOptions: { + target: ts.ScriptTarget.ES2020, + } }).outputText; + for (const language of ['en', 'es']) { + const common = require(`../../src/context/Localization/language/${language}.json`).common; + assert.equal(vm.runInNewContext(labelCode, { olderCopyAvailable: false, seed, common }), common.ok); + assert.equal(vm.runInNewContext(labelCode, { olderCopyAvailable: true, seed, common }), seed.recoverOlderCopy); + } + const actions = [], ui = []; + const close = vm.runInNewContext(productionExpression('closeRecoveryDiscovery'), { + setShowRecoveryDiscovery: (value) => ui.push(value), + dispatch: (action) => actions.push(action), + resetRecoveryDiscovery: () => ({ type: 'resetRecoveryDiscovery' }), + }); + close(); + assert.deepEqual(ui, [false]); + assert.deepEqual(actions, [{ type: 'resetRecoveryDiscovery' }], 'acknowledgement cannot retry or select an older copy'); +}); diff --git a/tests/recovery/repair.test.cjs b/tests/recovery/repair.test.cjs index 6c78864787..14a58fbb2b 100644 --- a/tests/recovery/repair.test.cjs +++ b/tests/recovery/repair.test.cjs @@ -297,7 +297,7 @@ test('incremental attempt during read-only check fails closed and invalidates co assert.equal((await write).category, 'protocol'); assert.deepEqual(f.calls, ['getBackupSnapshot']); assert.deepEqual(f.phases, ['checking', 'unverified']); - assert.deepEqual(f.diagnostics, [['Assisted Server Backup check failed at stage:', 'snapshot-validation', 'non-transport']]); + assert.deepEqual(f.diagnostics, [['Assisted Server Backup check failed at stage:', 'snapshot-request', 'non-transport']]); }); test('early revision change without a queued write stays unverified', async () => { @@ -427,11 +427,12 @@ test('malformed or partial server response is unavailable, never an empty snapsh } }); -test('mutation arriving during upload invalidates completion and remains queued until readback', async () => { +test('mutation arriving during upload invalidates completion before any further readback request', async () => { let f; f = harness({ post: async ({ path }) => { if (path.endsWith('revisionedBackupMutation')) f.transport.markBackupMutation(f.app.id); } }); f.local.Wallet.push(wallet()); assert.equal(await f.run(true), 'unverified'); assert.ok(!f.phases.includes('verified')); - assert.deepEqual(f.calls, ['getBackupSnapshot', 'revisionedBackupMutation', 'getBackupSnapshot']); + assert.ok(f.remote.wallets.wallet, 'the server commit is retained despite stale local evidence'); + assert.deepEqual(f.calls, ['getBackupSnapshot', 'revisionedBackupMutation']); }); test('account switch during check cannot upload one account with another account data', async () => { diff --git a/tests/recovery/restoreIntegrity.test.cjs b/tests/recovery/restoreIntegrity.test.cjs index 594e05aa5e..e326292a07 100644 --- a/tests/recovery/restoreIntegrity.test.cjs +++ b/tests/recovery/restoreIntegrity.test.cjs @@ -62,7 +62,8 @@ function recoveryFixture() { writes, expected: { mobile, vault, label, node, btc, usdt }, restore: () => - f.run('getAppImageWorker', { payload: { primaryMnemonic: 'mock fixture only' } }), + f.run('getAppImageWorker', { payload: { primaryMnemonic: 'mock fixture only', + selection: { source: 'legacy-older', olderCopyConfirmed: true } } }), }; } const completed = (f) => f.actions.some((a) => a.type === 'setAppCreated' && a.payload === true); @@ -215,7 +216,8 @@ test('explicit empty backup keeps the established first-wallet recovery path', a recreated++; return true; }; - await f.run('getAppImageWorker', { payload: { primaryMnemonic: 'mock fixture only' } }); + await f.run('getAppImageWorker', { payload: { primaryMnemonic: 'mock fixture only', + selection: { source: 'legacy-older', olderCopyConfirmed: true } } }); assert.deepEqual(errors(f), []); assert.equal(completed(f), true); assert.equal(recreated, 1); @@ -226,7 +228,8 @@ test('failed first-wallet recreation cannot complete empty-backup recovery', asy f.scope.WalletUtilities.getDerivationPath = () => 'fixture'; f.scope.DerivationPurpose = { BIP84: 'BIP84' }; f.scope.addNewWalletsWorker = () => false; - await f.run('getAppImageWorker', { payload: { primaryMnemonic: 'mock fixture only' } }); + await f.run('getAppImageWorker', { payload: { primaryMnemonic: 'mock fixture only', + selection: { source: 'legacy-older', olderCopyConfirmed: true } } }); assert.equal(completed(f), false); assert.equal(errors(f).length, 1); }); diff --git a/tests/recovery/revisionedMutation.test.cjs b/tests/recovery/revisionedMutation.test.cjs index 3d39e8ab99..c2123c42df 100644 --- a/tests/recovery/revisionedMutation.test.cjs +++ b/tests/recovery/revisionedMutation.test.cjs @@ -51,14 +51,16 @@ test('a prepared payload cannot adopt a revision established by later repair', a test('verified baseline is session bound and invalidated by any attempted mutation', async () => { const f = fixture(); const session = f.transport.currentBackupAccountSession(); - f.transport.recordVerifiedRemoteRevision('A', 'a'.repeat(64), session); + f.transport.recordVerifiedRemoteRevision('A', 'a'.repeat(64), session, + { namespaceId: 'unit-protected-A', revision: 'a'.repeat(64), generation: 0, authEpoch: 0 }); const first = f.transport.captureBackupWriteIntent('A'); assert.equal(first.baseRevision, 'a'.repeat(64)); assert.equal(first.accountSession, session); await assert.rejects(f.transport.backupPost('fixture/updateAppImage', { appId: 'A' }), (error) => error.category === 'protocol'); assert.equal(f.transport.captureBackupWriteIntent('A'), undefined); - f.transport.recordVerifiedRemoteRevision('A', 'b'.repeat(64), session); + f.transport.recordVerifiedRemoteRevision('A', 'b'.repeat(64), session, + { namespaceId: 'unit-protected-A', revision: 'b'.repeat(64), generation: 1, authEpoch: 0 }); const next = f.transport.captureBackupWriteIntent('A'); assert.ok(next.baselineEpoch > first.baselineEpoch); f.transport.observeBackupAccount('B'); diff --git a/tests/recovery/sagaHarness.cjs b/tests/recovery/sagaHarness.cjs index 455c6813d4..b5e58f3094 100644 --- a/tests/recovery/sagaHarness.cjs +++ b/tests/recovery/sagaHarness.cjs @@ -42,7 +42,8 @@ const encryption = loadModule('src/utils/service-utilities/encryption.ts', { 'react-native-rsa-native': { RSA: {} }, }); const { image } = require('./helpers.cjs'); -const { prepareRecoveryImage } = loadModule('src/services/backup/restore.ts', { './image': image }); +const { prepareRecoveryImage, prepareRecoverySourceImage, hasRecoveryRecords } = + loadModule('src/services/backup/restore.ts', { './image': image }); const { selectWalletsForSync } = loadModule('src/store/sagas/walletSyncSelection.js'); const { acquireWalletRefresh } = loadModule('src/store/sagas/walletRefreshCoordinator.js'); const account = loadModule('src/store/reducers/account.ts', { @@ -127,13 +128,20 @@ function fixture(options = {}) { store: { getState: () => state }, isAutomaticCloudBackupEnabled: (backup, id) => !!backup.automaticCloudBackupByAppId?.[id], ...encryption, + translations: require('../../src/context/Localization/language/en.json'), ...enums, ...account, selectWalletsForSync, acquireWalletRefresh, finishRefreshRequest: (payload) => ({ type: 'finishRefreshRequest', payload }), prepareRecoveryImage, + prepareRecoverySourceImage, + hasRecoveryRecords, + // Protected discovery must be supplied explicitly by tests of that source. + // Existing read-only legacy fixtures do not confer authenticated ownership. + readProtectedBackup: async () => { throw new Error('Protected fixture not configured'); }, pauseBackup: image.pauseBackup, + validateProtectedRecoveryMetadata: image.validateProtectedRecoveryMetadata, console: { log() {}, error() {} }, Buffer, _: require('lodash'), @@ -275,7 +283,15 @@ function fixture(options = {}) { const results = new WeakMap(); const consentEpochs = new Map(); const consentCompletions = new WeakMap(); + const recoveryDisables = new WeakMap(); scope.backupConsentEpoch = (id) => consentEpochs.get(id) || 0; + // Unit model of the middleware's opaque OFF-only recovery action, not remote + // authentication. Consent epochs and canonical per-account choices stay real + // state transitions in this orchestration fixture. + scope.protectBackupRecoveryConsentDisable = (action, appId, session) => { + recoveryDisables.set(action, { appId, session }); + return action; + }; scope.protectBackupConsentCompletion = (action, id, epoch) => { consentCompletions.set(action, { appId: id, epoch }); return action; @@ -324,15 +340,17 @@ function fixture(options = {}) { scope.realm = { getDatabase: () => localRealm }; scope.currentBackupAccountSession = () => accountSession; const sessionNow = () => scope.currentBackupAccountSession(); - const markUnitPending = (id) => { + const markUnitPending = (id, markRevision = true) => { // Production automatic.ts notifies reconciliation after a failed local // commit. This adapter models that state boundary without claiming proof. - scope.markBackupMutation(id); + if (markRevision) scope.markBackupMutation(id); if (scope.invalidateBackupRepair && scope.store.dispatch) scope.store.dispatch(scope.invalidateBackupRepair(id)); if (scope.setPendingAllBackup && scope.store.dispatch) scope.store.dispatch(scope.setPendingAllBackup({ appId: id, pending: true })); }; + scope.clearVerifiedRemoteRevision = (id) => { state.bhr.backupRepairCompletedByAppId[id] = false; }; + scope.notifyBackupReconciliation = (id) => markUnitPending(id, false); scope.beginBackupLocalOperation = (id, intent, session = sessionNow()) => { const current = scope.dbManager.getObjectByIndex('KeeperApp'); if (current?.id !== id || state.storage.appId !== id || session !== sessionNow()) @@ -476,6 +494,8 @@ function fixture(options = {}) { 'relayVaultUpdateSuccess', 'relayVaultUpdateFail', 'setAppImageError', + 'setRecoveryDiscovery', + 'setProtectedBackupState', 'setSeedConfirmed', 'setBackupType', 'uaiActioned', @@ -514,6 +534,7 @@ function fixture(options = {}) { 'recoverApp', 'writeRecoveredObject', 'getAppImageWorker', + 'latestRecoveryReadRequest', ]) + '\n' + functions('src/store/sagas/wallets.ts', [ @@ -532,6 +553,12 @@ function fixture(options = {}) { scope ); const dispatch = (action) => { + const recoveryDisable = recoveryDisables.get(action); + if (recoveryDisable) { + recoveryDisables.delete(action); + if (recoveryDisable.appId !== action.payload?.appId || recoveryDisable.session !== sessionNow() || + action.type !== 'setAutomaticCloudBackup' || action.payload.enabled !== false) return; + } if (action.type === 'setAutomaticCloudBackup') { const id = action.payload.appId; const completion = consentCompletions.get(action); @@ -541,6 +568,11 @@ function fixture(options = {}) { } else consentEpochs.set(id, scope.backupConsentEpoch(id) + 1); } actions.push(action); + if (action.type === 'setAppId') state.storage.appId = action.payload; + if (action.type === 'setAppCreated') state.storage.appCreated = action.payload; + if (action.type === 'setRecoveryDiscovery') state.bhr.recoveryDiscovery = action.payload; + if (action.type === 'setProtectedBackupState') + (state.bhr.protectedBackupStateByAppId ||= {})[action.payload.appId] = action.payload.state; if (action.type === 'setBackupRepairCompleted') state.bhr.backupRepairCompletedByAppId[action.payload] = true; if (action.type === 'invalidateBackupRepair') diff --git a/tests/recovery/sagaState.test.cjs b/tests/recovery/sagaState.test.cjs index 3672da45c3..c791631dbf 100644 --- a/tests/recovery/sagaState.test.cjs +++ b/tests/recovery/sagaState.test.cjs @@ -20,6 +20,10 @@ function inspectionRunner(f, afterVerified) { (ts.isFunctionDeclaration(n) && n.name?.text === 'runBackupInspection') || (ts.isVariableStatement(n) && n.declarationList.declarations.some(d => d.name?.text === 'backupInspectionOwners')) ).map(n => n.getText(source)).join('\n'); + const protectedSource = ts.createSourceFile('protected.ts', fs.readFileSync(path.join(__dirname, + '../../src/services/backup/protected.ts'), 'utf8'), ts.ScriptTarget.Latest, true); + const baseComparison = protectedSource.statements.find(n => + ts.isFunctionDeclaration(n) && n.name?.text === 'sameProtectedBackupBase').getText(protectedSource).replace(/^export /, ''); const operations = []; let state = bhr.default(undefined,{type:'init'}); const actions = []; const getState = () => ({ bhr:state, storage:{appId:f.app.id} }); @@ -32,6 +36,7 @@ function inspectionRunner(f, afterVerified) { operations.push(operation); return operation; }, + translations: require('../../src/context/Localization/language/en.json'), RealmSchema:{KeeperApp:'KeeperApp',UAI:'UAI'}, dbManager:{ getObjectByIndex:()=>f.app, getObjectByField:()=>[] }, currentBackupAppId:()=>f.app.id, @@ -41,7 +46,7 @@ function inspectionRunner(f, afterVerified) { store:{ getState, dispatch }, uaiType:{SERVER_BACKUP_FAILURE:'failure'}, uaiActionedWorker:()=>{}, addToUaiStackWorker:()=>{}, }; - vm.runInNewContext(ts.transpileModule(code,{ compilerOptions:{ target:ts.ScriptTarget.ES2020 }}).outputText,scope); + vm.runInNewContext(ts.transpileModule(baseComparison + '\n' + code,{ compilerOptions:{ target:ts.ScriptTarget.ES2020 }}).outputText,scope); return { start(repair) { const sagaTask = saga.runSaga({getState,dispatch},scope.runBackupInspection,repair); diff --git a/tests/recovery/sharedVaultRepair.test.cjs b/tests/recovery/sharedVaultRepair.test.cjs index e2d1412027..0b00cb638b 100644 --- a/tests/recovery/sharedVaultRepair.test.cjs +++ b/tests/recovery/sharedVaultRepair.test.cjs @@ -145,7 +145,7 @@ for (const ackRevision of ['missing', 'old', 'malformed', 'other']) 'a readable copy alone must not mint write authority' ); }); -test('archive metadata mismatch is read-only until explicit backup and must match after readback', async () => { +test('protected archive metadata mismatch stays unverified and explicit backup cannot overwrite it', async () => { const { f, state, key, uploads } = fixture({ unavailable: [] }); f.local.Vault[0].archived = true; state.allVaultImages = [ @@ -155,11 +155,15 @@ test('archive metadata mismatch is read-only until explicit backup and must matc isArchived: false, }, ]; - assert.equal(await f.run(), 'different'); + const ciphertext = state.allVaultImages[0].vault; + // The protected reference is bound to decrypted archive/policy metadata. + // The former legacy repair behavior must not authorize a v3 replacement. + assert.equal(await f.run(), 'unverified'); assert.equal(uploads(), 0); - assert.equal(await f.run(true), 'verified'); - assert.equal(uploads(), 1); - assert.equal(state.allVaultImages[0].isArchived, true); + assert.equal(await f.run(true), 'unverified'); + assert.equal(uploads(), 0); + assert.equal(state.allVaultImages[0].isArchived, false); + assert.equal(state.allVaultImages[0].vault, ciphertext); }); function orphanLabelFixture(hasLocalLabel = true, malformedMarker = false) { diff --git a/tests/recovery/support/protected-authority-esm-loader.mjs b/tests/recovery/support/protected-authority-esm-loader.mjs new file mode 100644 index 0000000000..92c47b529a --- /dev/null +++ b/tests/recovery/support/protected-authority-esm-loader.mjs @@ -0,0 +1,32 @@ +// Preserve the production ESM imports and real installed crypto implementations. +// This is source transpilation only, not a native/Metro runtime acceptance lane. +import assert from 'node:assert/strict'; +import crypto from 'node:crypto'; +import fs from 'node:fs'; +import os from 'node:os'; +import path from 'node:path'; +import { createRequire } from 'node:module'; +import { fileURLToPath, pathToFileURL } from 'node:url'; + +export async function loadProtectedAuthority() { + const root = fileURLToPath(new URL('../../../', import.meta.url)); + const require = createRequire(path.join(root, 'package.json')); + const ts = require('typescript'); + const filename = path.join(root, 'src/services/backup/authority.ts'); + const source = fs.readFileSync(filename, 'utf8'); + const output = ts.transpileModule(source, { fileName: filename, reportDiagnostics: true, + compilerOptions: { module: ts.ModuleKind.ES2022, target: ts.ScriptTarget.ES2022 } }); + assert.equal(output.diagnostics.length, 0, 'Actual authority source must transpile without syntax errors'); + assert.match(output.outputText, /from ['"]@scure\/bip32['"]/); + const temporary = fs.mkdtempSync(path.join(os.tmpdir(), 'keeper-backup-authority-esm-')); + fs.symlinkSync(fs.realpathSync(path.join(root, 'node_modules')), path.join(temporary, 'node_modules'), 'dir'); + const encodingSource = fs.readFileSync(path.join(root, 'src/services/backup/encoding.ts'), 'utf8'); + const encoding = ts.transpileModule(encodingSource, { + compilerOptions: { module: ts.ModuleKind.ES2022, target: ts.ScriptTarget.ES2022 } }); + fs.writeFileSync(path.join(temporary, 'encoding.mjs'), encoding.outputText, { flag: 'wx', mode: 0o600 }); + const emitted = path.join(temporary, 'authority.mjs'); + fs.writeFileSync(emitted, output.outputText.replace("import './encoding';", "import './encoding.mjs';"), { flag: 'wx', mode: 0o600 }); + const module = await import(pathToFileURL(emitted).href); + return { ...module, source, temporary, encodingSource, + sourceSha256: crypto.createHash('sha256').update(source).digest('hex') }; +} diff --git a/tests/recovery/support/react-native-encoding-loader.mjs b/tests/recovery/support/react-native-encoding-loader.mjs new file mode 100644 index 0000000000..32d9bea785 --- /dev/null +++ b/tests/recovery/support/react-native-encoding-loader.mjs @@ -0,0 +1,12 @@ +// Node's conditional `node` encoding branch assumes its native TextDecoder. +// Select the package's actual default JS files for this absent-globals lane; +// the separate real Metro proof checks the same mobile-resolution boundary. +export function resolve(specifier, context, nextResolve) { + if (specifier.startsWith('@exodus/bytes/')) { + return nextResolve(specifier, { + ...context, + conditions: context.conditions.filter((condition) => condition !== 'node'), + }); + } + return nextResolve(specifier, context); +} diff --git a/tests/recovery/walletRefreshIsolation.test.cjs b/tests/recovery/walletRefreshIsolation.test.cjs index d4f499f0f6..2c63ccaa5e 100644 --- a/tests/recovery/walletRefreshIsolation.test.cjs +++ b/tests/recovery/walletRefreshIsolation.test.cjs @@ -3,7 +3,7 @@ const assert = require('node:assert/strict'); const vm = require('node:vm'); const ts = require('typescript'); const { fixture, enums, wallet, loadModule, functions } = require('./sagaHarness.cjs'); -const { image, encryption } = require('./helpers.cjs'); +const { image, encryption, unitProtectedFacade } = require('./helpers.cjs'); const walletState = loadModule('src/store/reducers/wallets.ts', { 'src/storage': { reduxStorage: {} }, '../sagaActions/wallets': { ADD_NEW_WALLETS: 'ADD_NEW_WALLETS' }, @@ -182,11 +182,13 @@ test('refresh marks recoverable address counters pending after Realm persistence './image': image, './localSnapshot': localSnapshot, './transport': transport, + './protected': unitProtectedFacade(transport, f.app), }); transport.recordVerifiedRemoteRevision( originAppId, 'a'.repeat(64), - transport.currentBackupAccountSession() + transport.currentBackupAccountSession(), + { namespaceId: `unit-protected-${originAppId}`, revision: 'a'.repeat(64), generation: 0, authEpoch: 0 } ); const baseline = transport.captureBackupWriteIntent(originAppId); assert.ok(automatic.isBackupVerificationCurrent(originAppId)); diff --git a/tests/store/versionHistory.test.ts b/tests/store/versionHistory.test.ts index cc9ffef90f..0bedf5f51a 100644 --- a/tests/store/versionHistory.test.ts +++ b/tests/store/versionHistory.test.ts @@ -13,7 +13,8 @@ import * as bitcoinJS from 'bitcoinjs-lib'; import ecc from 'src/services/wallets/operations/taproot-utils/noble_ecc'; const { encryption, image, loadModule } = require('../recovery/helpers.cjs'); -const { prepareRecoveryImage } = loadModule('src/services/backup/restore.ts', { './image': image }); +const { prepareRecoveryImage, prepareRecoverySourceImage, hasRecoveryRecords } = + loadModule('src/services/backup/restore.ts', { './image': image }); const BIP85 = loadModule('src/services/wallets/operations/BIP85.ts', { 'src/utils/service-utilities/config': {}, '../enums': enums, @@ -111,6 +112,9 @@ function fixture(appId = 'test-app') { objects: (schema) => records[schema], create, }; + const backupState = { automaticCloudBackupByAppId: { [appId]: false } }; + const consentEpochs = new Map(); + const recoveryDisables = new WeakMap(); const scope: any = { ...effects, ...encryption, @@ -122,7 +126,14 @@ function fixture(appId = 'test-app') { Promise, console: { log() {}, error() {} }, Realm: { UpdateMode: { Modified: 'modified' } }, - store: { getState: () => ({ storage: { appId: state.appId } }) }, + store: { getState: () => ({ storage: { appId: state.appId }, bhr: backupState }) }, + backupConsentEpoch: (id) => consentEpochs.get(id) || 0, + protectBackupRecoveryConsentDisable: (action, id, session) => { + recoveryDisables.set(action, { appId: id, session }); + return action; + }, + markBackupMutation: () => {}, + clearVerifiedRemoteRevision: () => {}, dbManager: { getObjectByIndex: () => app, getObjectById: raw.objectForPrimaryKey, @@ -185,6 +196,14 @@ function fixture(appId = 'test-app') { captureError: (error) => state.errors.push(error.message), }; const dispatch = (action) => { + if (action.type === 'setAutomaticCloudBackup') { + const owned = recoveryDisables.get(action); + recoveryDisables.delete(action); + if (!owned || owned.appId !== action.payload.appId || owned.session !== state.session || + action.payload.enabled !== false) throw new Error('Unowned recovery consent action'); + consentEpochs.set(owned.appId, scope.backupConsentEpoch(owned.appId) + 1); + backupState.automaticCloudBackupByAppId[owned.appId] = false; + } state.actions.push(action); if (action.type === 'setAppId') state.appId = action.payload; if (action.type === 'setAppImageError' && action.payload) state.errors.push(action.payload); @@ -193,6 +212,9 @@ function fixture(appId = 'test-app') { for (const name of [ 'setAppVersion', 'setAppImageError', + 'setRecoveryDiscovery', + 'setProtectedBackupState', + 'setAutomaticCloudBackup', 'setSeedConfirmed', 'setBackupType', 'uaiActioned', @@ -355,6 +377,10 @@ async function recoveryFixture(backupVersion: string) { crypto: require('crypto'), BIP85, prepareRecoveryImage, + prepareRecoverySourceImage, + hasRecoveryRecords, + validateProtectedRecoveryMetadata: image.validateProtectedRecoveryMetadata, + readProtectedBackup: async () => { throw new Error('This unit fixture selects only the older copy'); }, config: { BIP85_IMAGE_ENCRYPTIONKEY_DERIVATION_PATH: "m/83696968'/128169'/64'/0'" }, WalletUtilities: { getFingerprintFromSeed: (value) => @@ -403,6 +429,7 @@ async function recoveryFixture(backupVersion: string) { ts.transpileModule( sourceDeclarations('src/store/sagas/bhr.ts', [ 'getAppImageWorker', + 'latestRecoveryReadRequest', 'recoverApp', 'writeRecoveredObject', ]), @@ -412,7 +439,8 @@ async function recoveryFixture(backupVersion: string) { ).outputText, f.scope ); - await f.run('getAppImageWorker', { payload: { primaryMnemonic: mnemonic } }); + await f.run('getAppImageWorker', { payload: { primaryMnemonic: mnemonic, + selection: { source: 'legacy-older', olderCopyConfirmed: true } } }); return { writes: f.records.VersionHistory, requestedUpgrades, errors: f.state.errors }; } diff --git a/yarn.lock b/yarn.lock index 55464dae23..563255662a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1321,6 +1321,11 @@ resolved "https://registry.yarnpkg.com/@eslint/js/-/js-8.57.1.tgz#de633db3ec2ef6a3c89e2f19038063e8a122e2c2" integrity sha512-d9zaMRSTIKDLhctzH12MtXvJKSSUhaHcjV+2Z+GK+EEY7XKpP5yR4x+N3TAcHTcu963nIr+TMcCb4DBCYX1z6Q== +"@exodus/bytes@1.16.0": + version "1.16.0" + resolved "https://registry.yarnpkg.com/@exodus/bytes/-/bytes-1.16.0.tgz#954c9d9dd7eec15b966b2e8f2d883c04295b720c" + integrity sha512-IcpW84uEn3N7ETtNZMlxKhfl6Pec8rUNGOTBtWbK1FKhJxIFAptZyVrvVRVBimAJxJCgc3PxepxkdWWG4DVzfA== + "@expo/config-plugins@~8.0.0": version "8.0.11" resolved "https://registry.yarnpkg.com/@expo/config-plugins/-/config-plugins-8.0.11.tgz#b814395a910f4c8b7cc95d9719dccb6ca53ea4c5" @@ -2743,6 +2748,13 @@ dependencies: "@noble/hashes" "1.4.0" +"@noble/curves@2.4.0": + version "2.4.0" + resolved "https://registry.yarnpkg.com/@noble/curves/-/curves-2.4.0.tgz#367c869bfd790c3a2248152562f255b5f8898b74" + integrity sha512-P4/62zrgfH33CneE3Dn4WhJVA22YUU0eR51wKIan4NVRvwsA0YnPTwWGpNbpuacSujmSFLvyzpyuR30+fbq2Ew== + dependencies: + "@noble/hashes" "2.4.0" + "@noble/hashes@1.3.2": version "1.3.2" resolved "https://registry.yarnpkg.com/@noble/hashes/-/hashes-1.3.2.tgz#6f26dbc8fbc7205873ce3cee2f690eba0d421b39" @@ -2753,6 +2765,11 @@ resolved "https://registry.yarnpkg.com/@noble/hashes/-/hashes-1.4.0.tgz#45814aa329f30e4fe0ba49426f49dfccdd066426" integrity sha512-V1JJ1WTRUqHHrOSh597hURcMqVKVGL/ea3kv0gSnEdsEZ0/+VyPghM1lMNGc00z7CIQorSvbKpuJkxvuHbvdbg== +"@noble/hashes@2.4.0": + version "2.4.0" + resolved "https://registry.yarnpkg.com/@noble/hashes/-/hashes-2.4.0.tgz#e6a3e98edbed3c8659b28bb8f9b18ca02ec8ea4b" + integrity sha512-X5XaVWZIBCT7HHZGm5I7ZQXDwLG+bGXuSrMQAW+7Zvl87h1kmc1ZB1VSRJcpUfoUrGQp4Fkoxm5kZ+Ms+aW+eA== + "@noble/hashes@^1.2.0": version "1.8.0" resolved "https://registry.yarnpkg.com/@noble/hashes/-/hashes-1.8.0.tgz#cee43d801fcef9644b11b8194857695acd5f815a" @@ -3874,6 +3891,11 @@ resolved "https://registry.yarnpkg.com/@rtsao/scc/-/scc-1.1.0.tgz#927dd2fae9bc3361403ac2c7a00c32ddce9ad7e8" integrity sha512-zt6OdqaDoOnJ1ZYsCYGt9YmWzDXl4vQdKTyJev62gFhRGKdx7mcT54V9KIjg+d2wi9EXsPvAPKe7i7WjfVWB8g== +"@scure/base@2.4.0": + version "2.4.0" + resolved "https://registry.yarnpkg.com/@scure/base/-/base-2.4.0.tgz#61e4ed7d4447afc247f2bff9aeb8fca53267db86" + integrity sha512-thZ1TuJwFwBblOhgsjDKvvGirBxNp+wSvY/DR6tJBJOTDhdAAcHJ8Vbr2eFnqaxeca4+t0i9KBf+uHYGWwZORg== + "@scure/base@^1.1.1": version "1.2.6" resolved "https://registry.yarnpkg.com/@scure/base/-/base-1.2.6.tgz#ca917184b8231394dd8847509c67a0be522e59f6" @@ -3893,6 +3915,15 @@ "@noble/hashes" "~1.4.0" "@scure/base" "~1.1.6" +"@scure/bip32@2.4.0": + version "2.4.0" + resolved "https://registry.yarnpkg.com/@scure/bip32/-/bip32-2.4.0.tgz#4b13d0e5f60f776f17cbf5528893ee03839acb6a" + integrity sha512-i3DS0CptAocyvqE4n3SUkpzeQK4vJMFwWLofTwRiiKo2aWojBOfyMCgfKw9HVpO6fSY5AK86sHS/Uzn8kK9Few== + dependencies: + "@noble/curves" "2.4.0" + "@noble/hashes" "2.4.0" + "@scure/base" "2.4.0" + "@scure/bip39@1.3.0": version "1.3.0" resolved "https://registry.yarnpkg.com/@scure/bip39/-/bip39-1.3.0.tgz#0f258c16823ddd00739461ac31398b4e7d6a18c3" @@ -3901,6 +3932,13 @@ "@noble/hashes" "~1.4.0" "@scure/base" "~1.1.6" +"@scure/bip39@2.4.0": + version "2.4.0" + resolved "https://registry.yarnpkg.com/@scure/bip39/-/bip39-2.4.0.tgz#7b19a76d4dd2f4591c07b446a0f9695f9b1d9545" + integrity sha512-82dxFbZUYboyOf0AXiydsQrFQ5Q4h9mX+O2UkE91ROYmsc0BKMGZLwDmy96Jpa2+vrtoxomjUhy1RPIgH/r2nA== + dependencies: + "@noble/hashes" "2.4.0" + "@sentry-internal/browser-utils@10.48.0": version "10.48.0" resolved "https://registry.yarnpkg.com/@sentry-internal/browser-utils/-/browser-utils-10.48.0.tgz#e3901479410580a5eaf117232da2191edcb7ea38" From 03e4138c2391f930d9ed158c1b92b6493ba58484 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Fri, 16 Oct 2026 15:41:06 +0000 Subject: [PATCH 201/254] Verify restored node selection separately from encrypted connection flags --- tests/recovery/protectedWriteRealmRelay.proof.cjs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tests/recovery/protectedWriteRealmRelay.proof.cjs b/tests/recovery/protectedWriteRealmRelay.proof.cjs index 217db5fdae..ee568071f0 100644 --- a/tests/recovery/protectedWriteRealmRelay.proof.cjs +++ b/tests/recovery/protectedWriteRealmRelay.proof.cjs @@ -313,6 +313,10 @@ test('the actual Recovery Key worker restores a protected full image into blank assert.equal(mutations().length, 1); assert.equal(mutations()[0].replaceNodes, true); assert.equal(mutations()[0].nodeCount, 2); const uploaded = await assertComplete(source); assert.equal(uploaded.snapshot.appImage.nodes.length, 2); + const uploadedImage = await source.image.decodeImage(source.encryption.generateEncryptionKey(source.seed), + uploaded.snapshot, uploaded.namespaceId); + assert.ok(Object.values(uploadedImage.nodes).every(node => node.isConnected === false), + 'The real producer clears all transient connection flags in the encrypted backup'); const expected = await source.repair.captureBackupImage(); const { f, saga } = await blankRecoveryClient(t); server.clearTrace(); await saga.run('getAppImageWorker', { payload: { primaryMnemonic: mnemonic } }); @@ -324,7 +328,10 @@ test('the actual Recovery Key worker restores a protected full image into blank assert.equal(restoredNodes.length, 2); assert.equal(restoredNodes.find(node => node.id === defaultNode.id).host, defaultNode.host); assert.equal(restoredNodes.find(node => node.id === customNode.id).host, customNode.host); - assert.ok(restoredNodes.every(node => node.isConnected === false), 'Restore does not revive stale connection flags'); + // The unchanged recovery worker selects the first restored node when all + // uploaded flags are false. This is local selection, not a network connection. + assert.equal(restoredNodes[0].isConnected, true); + assert.ok(restoredNodes.slice(1).every(node => node.isConnected === false)); assert.notEqual(f.store.getState().bhr.automaticCloudBackupByAppId[appId], true); assert.equal(f.store.getState().bhr.recoveryDiscovery.provenance.source, 'protected'); assert.equal(server.trace.some(event => event.legacyRead), false); assert.equal(mutations().length, 0); From be89c34473bb6aab253b98325f1fe746ca391c6f Mon Sep 17 00:00:00 2001 From: antuz123 <43998049+antuz123@users.noreply.github.com> Date: Fri, 16 Oct 2026 21:38:10 +0530 Subject: [PATCH 202/254] Fence superseded recovery requests before local effects and fix fixture routing --- .../protected-backup-integration.md | 2 + release/r4-protected-backup-app.md | 6 + src/services/backup/restore.ts | 7 +- src/store/sagas/bhr.ts | 76 ++++--- .../protectedWriteRealmRelay.fixture.cjs | 11 +- .../protectedWriteRealmRelay.proof.cjs | 214 +++++++++++++++++- 6 files changed, 284 insertions(+), 32 deletions(-) diff --git a/openspec/changes/fix-recovery-backup-freshness/protected-backup-integration.md b/openspec/changes/fix-recovery-backup-freshness/protected-backup-integration.md index f2ec922f43..0e2aef3ca4 100644 --- a/openspec/changes/fix-recovery-backup-freshness/protected-backup-integration.md +++ b/openspec/changes/fix-recovery-backup-freshness/protected-backup-integration.md @@ -24,6 +24,8 @@ Turning off automatic updates keeps existing copies and is not Delete. Persisted Clean recovery checks the authenticated protected namespace first. Never automatically fall back after absence, tombstone, errors or a known-namespace conflict. A separately selected **Recover an older copy** reads retained legacy data with provenance and a missing-newer-changes warning. Decode and validate the full selected image before the first local write; never report partial/conflicted/unavailable recovery as complete. +A recovery request remains bound to its original request serial as well as account session through asynchronous decrypt/derivation and every consent, Redux and Realm write. Newer discovery cancels the old recovery task and called child generators; cancellation must not emit stale retry or completion. A superseded protected or older-copy request cannot mutate local data or consent. + After validation, revoke target-account automatic-backup consent and expire old queued/opt-in scope before any restore write or producer. This applies even when saved consent was true for a not-yet-selected account. Neither recovery source silently enables or uploads the recovered copy; a later explicit opt-in is separate. ## Acceptance and remaining gates diff --git a/release/r4-protected-backup-app.md b/release/r4-protected-backup-app.md index 04ab108e6c..30c2d38049 100644 --- a/release/r4-protected-backup-app.md +++ b/release/r4-protected-backup-app.md @@ -2,6 +2,12 @@ State: **NEEDS_LOCAL; R4 remains BLOCKED.** Use the full app SHA in the newest **Codex local handoff** on [PR #7045](https://github.com/KeeperCommunity/bitcoin-keeper/pull/7045), verify it equals the live PR head, and preserve every earlier result under its original SHA. This document cannot name its own eventual commit. The current v3 instructions here supersede the historical public-v2 fixture commands in [the earlier handoff](r4-automatic-backup-local-handoff.md). +## Superseded recovery request correction + +The03e proof did not cover a newer recovery discovery arriving during the old restore's record decryption in the **same account session**. Genuine protected and explicit-older schedules reproduced local account/consent changes. The corrected worker carries an identity-only request checkpoint through decode, post-derivation and every guarded Realm write, alongside the existing original/selected account-session checks. Each recovery Redux dispatch checks current request/session. Only GET_APP_IMAGE uses `takeLatest`; it cancels called wallet/signer/upgrade generators, and cancelled workers cannot emit retry cleanup. This does not change generic watchers, account authority or parked navigation behavior. Source RED/GREEN, fixture precondition failures and actual watcher/child cancellation evidence belong to the newest exact-head receipt. + +CodeQL's03e analysis completed but its separate security aggregate **failed** (one Critical SSRF/one High sanitizer alert) on synthetic merge784b915c27060afabc340248b459541a754fe5f4. The test-only proxy now chooses three literal loopback URLs without constructing a destination from request input; genuine raw-target rejection tests verify it. Independent review supports a false-positive classification for the unchanged ASM warning: text remains local and only its numeric element count reaches fee sizing, with no HTML sink. No alert was dismissed. A new source scan and its actual result stay separate from this classification. + ## Scope and identity Production entry points are the protected authority/transport, existing automatic-write producer/queue and repair/status paths, confirmed Delete worker, and typed Recovery Key discovery/restore. Authority is derived separately from the Recovery Key with the supported signing profile; it does not turn legacy decryption or an old account identifier into proof of ownership. Existing Keeper app identity, wallet/key derivation, encrypted payload format, provider/billing identity and local-first recovery records stay separate. Missing/stale account, session, consent, committed-generation, original-base, fingerprint, CAS or final-readback proof remains fail-closed. diff --git a/src/services/backup/restore.ts b/src/services/backup/restore.ts index 9144dc4ab8..a6ee634d94 100644 --- a/src/services/backup/restore.ts +++ b/src/services/backup/restore.ts @@ -110,8 +110,10 @@ export async function prepareRecoveryImage( appImage: any, allVaultImages: any[], labels: any[], - verifyProtectedWalletKeys = false + verifyProtectedWalletKeys = false, + checkpoint: () => void = () => {} ) { + checkpoint(); if (!appImage || appImage.appId !== appId) throw new Error('Recovery data unavailable'); // Older Relay registrations and deletions stored empty maps as empty arrays. // Keep those recoverable, but let decodeImage reject nonempty malformed arrays. @@ -129,9 +131,10 @@ export async function prepareRecoveryImage( labels, }, appId, - () => {}, + checkpoint, verifyProtectedWalletKeys ); + checkpoint(); // Legacy USDT wallets cannot recover these addresses from the retired // provider. Reject an unusable image before the first Realm write. for (const wallet of Object.values(image.wallets) as any[]) { diff --git a/src/store/sagas/bhr.ts b/src/store/sagas/bhr.ts index 093e28444d..e2ad5ec807 100644 --- a/src/store/sagas/bhr.ts +++ b/src/store/sagas/bhr.ts @@ -26,7 +26,7 @@ import { beginBackupLocalOperation, commitBackupLocalOperation, enqueueAutomatic import type { BackupLocalCommit } from 'src/services/backup/automatic'; import * as bip39 from 'bip39'; import { Wallet } from 'src/services/wallets/interfaces/wallet'; -import { call, put, select, takeMaybe, takeEvery } from 'redux-saga/effects'; +import { call, put, select, takeMaybe, takeEvery, takeLatest, cancelled } from 'redux-saga/effects'; import config from 'src/utils/service-utilities/config'; import { translations } from 'src/context/Localization/LocContext'; import { encrypt, generateEncryptionKey, hash256 } from 'src/utils/service-utilities/encryption'; @@ -105,6 +105,7 @@ import { DELETE_APP_IMAGE_ENTITY, DELETE_BACKUP, GET_APP_IMAGE, + getAppImage, HEALTH_CHECK_STATUS_UPDATE, SEED_BACKEDUP, SEED_BACKEDUP_CONFIRMED, @@ -359,7 +360,7 @@ function* seedBackedUpWorker() { let latestRecoveryReadRequest = 0; -function* getAppImageWorker({ payload }) { +function* getAppImageWorker({ payload }: ReturnType) { const request = ++latestRecoveryReadRequest; const requestSession = currentBackupAccountSession(); const { primaryMnemonic, isForgot } = payload; @@ -371,13 +372,22 @@ function* getAppImageWorker({ payload }) { let provenance: RecoveryProvenance; let protectedState: ProtectedBackupState; let recoverySession: number; + const assertRequestIdentity = () => { + if (request !== latestRecoveryReadRequest) throw new Error('Recovery request changed'); + }; + const requestIsCurrent = () => request === latestRecoveryReadRequest && + (recoverySession ?? requestSession) === currentBackupAccountSession(); + function* putCurrent(action: any) { + if (requestIsCurrent()) yield put(action); + } const assertRequestSession = () => { - if (request !== latestRecoveryReadRequest || requestSession !== currentBackupAccountSession()) + assertRequestIdentity(); + if (requestSession !== currentBackupAccountSession()) throw new Error('Backup account changed'); }; try { - yield put(setAppImageError('')); - yield put(setRecoveryDiscovery({ status: 'checking', source })); + yield* putCurrent(setAppImageError('')); + yield* putCurrent(setRecoveryDiscovery({ status: 'checking', source })); if (selection.source !== 'protected' && (selection.source !== 'legacy-older' || selection.olderCopyConfirmed !== true)) throw new Error('Older recovery copy was not selected'); @@ -403,7 +413,7 @@ function* getAppImageWorker({ payload }) { assertRequestSession(); namespaceId = result.namespaceId; if (result.state === 'neverEnrolled' || result.state === 'deleted') { - yield put(setRecoveryDiscovery({ + yield* putCurrent(setRecoveryDiscovery({ status: result.state === 'deleted' ? 'protected-deleted' : 'protected-absent', source, keeperAppId: appID, @@ -420,7 +430,7 @@ function* getAppImageWorker({ payload }) { generation: result.snapshot.generation, authEpoch: result.snapshot.authEpoch, }; if (!hasRecoveryRecords(result.snapshot)) { - yield put(setRecoveryDiscovery({ + yield* putCurrent(setRecoveryDiscovery({ status: 'protected-empty', source, keeperAppId: appID, namespaceId, legacyMayPredateDeletion: result.snapshot.suppressLegacyFallback, })); @@ -438,7 +448,7 @@ function* getAppImageWorker({ payload }) { } const { appImage, labels, allVaultImages } = prepared; provenance = prepared.provenance; - yield put(setRecoveryDiscovery({ + yield* putCurrent(setRecoveryDiscovery({ status: source === 'protected' ? 'protected-found' : 'legacy-selected', source, keeperAppId: appID, namespaceId, provenance, })); @@ -479,7 +489,8 @@ function* getAppImageWorker({ payload }) { // This callback supplies no remote baseline or upload authority. recoverySession = ownedSession; }, - source === 'protected' + source === 'protected', + assertRequestIdentity ); if (request !== latestRecoveryReadRequest || recoverySession !== currentBackupAccountSession() || currentBackupAppId() !== appID) @@ -501,6 +512,7 @@ function* getAppImageWorker({ payload }) { instanceNum: 0, }, }; + assertRequestIdentity(); const created = yield call(addNewWalletsWorker, { payload: [defaultWallet], originAppId: appID, @@ -509,6 +521,7 @@ function* getAppImageWorker({ payload }) { if (created !== true) throw new Error('Recovery data unavailable'); } + assertRequestIdentity(); const recoveryKeySigner = setupRecoveryKeySigningKey(primaryMnemonic); yield call(addSigningDeviceWorker, { payload: { signers: [recoveryKeySigner] }, @@ -544,6 +557,7 @@ function* getAppImageWorker({ payload }) { const newVersion = DeviceInfo.getVersion(); if (previousVersion && semver.lt(previousVersion, newVersion)) { console.log(`applying restore upgarde sequence - from: ${previousVersion} to ${newVersion}`); + assertRequestIdentity(); yield call(applyUpgradeSequence, { previousVersion, newVersion, @@ -554,18 +568,18 @@ function* getAppImageWorker({ payload }) { if (request !== latestRecoveryReadRequest || recoverySession !== currentBackupAccountSession() || currentBackupAppId() !== appID) throw new Error('Backup account changed'); - yield put(addAccount(appID)); - yield put(autoSyncWallets(true, true, false, appID)); - yield put( + yield* putCurrent(addAccount(appID)); + yield* putCurrent(autoSyncWallets(true, true, false, appID)); + yield* putCurrent( saveDefaultWalletState({ appId: appID, data: { [NetworkType.MAINNET]: true, [NetworkType.TESTNET]: true }, }) ); - yield put(uaiChecks([uaiType.SECURE_VAULT])); - yield put(loadConciergeUser(null)); - yield put(loadConciergeTickets([])); - yield put(setRecoveryKeyStatus({ appId: appID, status: 'confirmed' })); + yield* putCurrent(uaiChecks([uaiType.SECURE_VAULT])); + yield* putCurrent(loadConciergeUser(null)); + yield* putCurrent(loadConciergeTickets([])); + yield* putCurrent(setRecoveryKeyStatus({ appId: appID, status: 'confirmed' })); if (protectedState) { const hint: ProtectedBackupState = yield select( (state: RootState) => state.bhr.protectedBackupStateByAppId?.[appID] @@ -582,27 +596,28 @@ function* getAppImageWorker({ payload }) { hint.status === 'deleted') ); if (comparable && !older) - yield put(setProtectedBackupState({ appId: appID, state: protectedState })); + yield* putCurrent(setProtectedBackupState({ appId: appID, state: protectedState })); } - yield put(setRecoveryDiscovery({ + yield* putCurrent(setRecoveryDiscovery({ status: 'restored', source, keeperAppId: appID, namespaceId, provenance, })); - yield put(setAppCreated(true)); + yield* putCurrent(setAppCreated(true)); } catch (err) { // A late response for a previous account/request must not replace the // current recovery screen's provenance or present an error as absence. const activeSession = recoverySession ?? requestSession; if (request === latestRecoveryReadRequest && activeSession === currentBackupAccountSession()) { const error = err.message || 'Recovery data unavailable'; - yield put(setRecoveryDiscovery({ + yield* putCurrent(setRecoveryDiscovery({ status: 'read-error', source, keeperAppId, namespaceId, error, })); - yield put(setAppImageError(error)); + yield* putCurrent(setAppImageError(error)); } } finally { - if (request === latestRecoveryReadRequest && + const wasCancelled = yield cancelled(); + if (!wasCancelled && request === latestRecoveryReadRequest && (recoverySession ?? requestSession) === currentBackupAccountSession()) - yield put(appImagerecoveryRetry()); + yield* putCurrent(appImagerecoveryRetry()); } } @@ -618,12 +633,14 @@ function* recoverApp( previousVersion, originBackupAccountSession?: number, onRecoverySession?: (ownedSession: number) => void, - protectedMetadata = false + protectedMetadata = false, + assertRecoveryRequest: (() => void) | undefined = undefined ) { // Recovery selects appID after the first Realm write. Keep the session from // the initiating action across decryption and all earlier async work. const originSession = originBackupAccountSession ?? currentBackupAccountSession(); const assertOrigin = () => { + assertRecoveryRequest?.(); if (currentBackupAccountSession() !== originSession) throw new Error('Backup account changed'); }; @@ -636,7 +653,8 @@ function* recoverApp( appImage, allVaultImages, labels, - protectedMetadata + protectedMetadata, + assertOrigin ); if (protectedMetadata) validateProtectedRecoveryMetadata({allVaultImages, labels}, recovered, getKeyUID, hash256, encryptionKey); @@ -662,6 +680,7 @@ function* recoverApp( config.BIP85_IMAGE_ENCRYPTIONKEY_DERIVATION_PATH, primaryMnemonic ); + assertOrigin(); const imageEncryptionKey = generateEncryptionKey(entropy.toString('hex')); const publicId = WalletUtilities.getFingerprintFromSeed(primarySeed); const app: KeeperApp = { @@ -689,6 +708,7 @@ function* recoverApp( yield put(setAppId(appID)); const recoveryAccountSession = currentBackupAccountSession(); const assertRecoveryAccount = () => { + assertRecoveryRequest?.(); if (currentBackupAccountSession() !== recoveryAccountSession || currentBackupAppId() !== appID) throw new Error('Backup account changed'); }; @@ -1138,7 +1158,11 @@ export const bsmsCloudHealthCheckWatcher = createWatcher( export const updateAppImageWatcher = createWatcher(updateAppImageWorker, UPDATE_APP_IMAGE); export const updateVaultImageWatcher = createWatcher(updateVaultImageWorker, UPDATE_VAULT_IMAGE); -export const getAppImageWatcher = createWatcher(getAppImageWorker, GET_APP_IMAGE); +export function* getAppImageWatcher() { + // Recovery owns one intent. Cancellation also stops called wallet/signer/ + // upgrade generators; explicit request checks still guard async crypto. + yield takeLatest(GET_APP_IMAGE, getAppImageWorker); +} export const seedBackedUpWatcher = createWatcher(seedBackedUpWorker, SEED_BACKEDUP); export const seedBackeupConfirmedWatcher = createWatcher( diff --git a/tests/recovery/protectedWriteRealmRelay.fixture.cjs b/tests/recovery/protectedWriteRealmRelay.fixture.cjs index 7782d66f80..df58c2a617 100644 --- a/tests/recovery/protectedWriteRealmRelay.fixture.cjs +++ b/tests/recovery/protectedWriteRealmRelay.fixture.cjs @@ -100,9 +100,14 @@ async function createProtectedServer() { request.on('data', chunk => chunks.push(chunk)); request.on('end', () => { const raw = Buffer.concat(chunks); - const upstreamUrl = new URL(request.url, target); - if (!request.url.startsWith('/') || request.url.startsWith('//') || upstreamUrl.origin !== target.origin) { - response.writeHead(400); response.end(); return; + let upstreamUrl; + // Route only fixed fixture endpoints. Never construct an outgoing URL + // from an incoming target, including query, authority or escape forms. + switch (request.url) { + case '/backup/v3/enroll': upstreamUrl = new URL('/backup/v3/enroll', target); break; + case '/backup/v3/snapshot': upstreamUrl = new URL('/backup/v3/snapshot', target); break; + case '/backup/v3/mutate': upstreamUrl = new URL('/backup/v3/mutate', target); break; + default: response.writeHead(400); response.end(); return; } let body; try { body = JSON.parse(raw.toString('utf8')); } catch { /* malformed fault cases stay inert */ } diff --git a/tests/recovery/protectedWriteRealmRelay.proof.cjs b/tests/recovery/protectedWriteRealmRelay.proof.cjs index ee568071f0..64acf7a6df 100644 --- a/tests/recovery/protectedWriteRealmRelay.proof.cjs +++ b/tests/recovery/protectedWriteRealmRelay.proof.cjs @@ -108,8 +108,14 @@ function recoverySagas(f) { 'shared-production-recovery-crypto-setup.ts'); return scope.cryptoAndRecovery(f); } -async function blankRecoveryClient(t) { +async function blankRecoveryClient(t, { inheritedConsent = false } = {}) { const f = await client(t); const saga = recoverySagas(f); + if (inheritedConsent) { + // Establish saved consent while this account is genuinely selected. An + // unbranded enable for an unselected account is correctly rejected. + f.store.dispatch(f.bhr.setAutomaticCloudBackup({ appId, enabled: true })); await tick(); + assert.equal(f.store.getState().bhr.automaticCloudBackupByAppId[appId], true); + } f.realmDatabase.closeDatabase(); const filename = path.join(f.directory, 'protected-clean-restore.realm'); assert.equal((await f.dbManager.initializeRealm(Buffer.alloc(64, 7), filename)).success, true); f.store.dispatch(f.storage.setAppId('')); @@ -580,3 +586,209 @@ test('different-selected-account older recovery disables the target saved true c assert.equal(server.trace.filter(event => event.legacyRead).length, 1); assert.equal(mutations().length, 0); assert.equal(server.trace.some(event => event.path === '/backup/v3/enroll'), false); }); + +for (const recoverySource of ['protected', 'legacy-older']) { + test(`${recoverySource}: a newer same-session discovery invalidates an older real decrypt before any local recovery change`, { timeout: 30000 }, async t => { + const source = await setup(t), sourceCrypto = recoverySagas(source); + persist(source, source.RealmSchema.Signer, sourceCrypto.scope.setupRecoveryKeySigningKey(mnemonic)); + assert.equal(await source.sagas.run('runBackupInspection', true, appId, true), true); + if (recoverySource === 'legacy-older') await server.seedLegacy(await source.payloadForCurrentImage()); + const { f, saga } = await blankRecoveryClient(t, { inheritedConsent: true }); + assert.equal(f.store.getState().bhr.automaticCloudBackupByAppId[appId], true); + const entered = barrier(), resume = barrier(); t.after(resume.release); + const actualPause = f.image.pauseBackup; + let held = false; + // Delay one ordinary production per-record decrypt yield. The real timer, + // AES, complete decoder, HTTP and recovery worker all remain in use. + f.image.pauseBackup = async () => { + await actualPause(); + if (!held) { held = true; entered.release(); await resume.promise; } + }; + t.after(() => { f.image.pauseBackup = actualPause; }); + server.clearTrace(); + const older = saga.run('getAppImageWorker', { payload: { primaryMnemonic: mnemonic, + ...(recoverySource === 'legacy-older' + ? { selection: { source: 'legacy-older', olderCopyConfirmed: true } } : {}) } }); + await entered.promise; + assert.equal(f.nativeRealm().objects('KeeperApp').length, 0); + const originSession = f.transport.currentBackupAccountSession(); + const otherMnemonic = require('bip39').entropyToMnemonic('44'.repeat(16)); + await saga.run('getAppImageWorker', { payload: { primaryMnemonic: otherMnemonic } }); + assert.equal(f.store.getState().bhr.recoveryDiscovery.status, 'protected-absent'); + assert.equal(f.transport.currentBackupAccountSession(), originSession); + const expectedState = plain(f.store.getState()); + const expectedEpoch = f.transport.backupConsentEpoch(appId); + const expectedRevision = f.transport.backupRevision(appId); + resume.release(); await older; + t.diagnostic(JSON.stringify({ recoverySource, held, + keeperApps: f.nativeRealm().objects('KeeperApp').length, + consentStillTrue: f.store.getState().bhr.automaticCloudBackupByAppId[appId] === true, + consentEpochUnchanged: f.transport.backupConsentEpoch(appId) === expectedEpoch, + localRevisionUnchanged: f.transport.backupRevision(appId) === expectedRevision, + sessionUnchanged: f.transport.currentBackupAccountSession() === originSession, + reduxUnchanged: JSON.stringify(plain(f.store.getState())) === JSON.stringify(expectedState) })); + assert.equal(f.nativeRealm().objects('KeeperApp').length, 0, 'The superseded recovery must not perform its first local write'); + for (const schema of f.nativeRealm().schema.filter(schema => !schema.embedded)) + assert.equal(f.nativeRealm().objects(schema.name).length, 0, 'The superseded recovery must preserve the blank native Realm'); + assert.equal(f.store.getState().bhr.automaticCloudBackupByAppId[appId], true); + assert.equal(f.transport.backupConsentEpoch(appId), expectedEpoch); + assert.equal(f.transport.backupRevision(appId), expectedRevision); + assert.equal(f.transport.currentBackupAccountSession(), originSession); + assert.deepEqual(plain(f.store.getState()), expectedState); + assert.equal(mutations().length, 0); + assert.equal(server.trace.some(event => event.path === '/backup/v3/enroll'), false); + }); +} + +test('the actual recovery watcher cancels its called decrypt and recovery generators when a newer discovery arrives', { timeout: 30000 }, async t => { + const source = await setup(t), sourceCrypto = recoverySagas(source); + persist(source, source.RealmSchema.Signer, sourceCrypto.scope.setupRecoveryKeySigningKey(mnemonic)); + assert.equal(await source.sagas.run('runBackupInspection', true, appId, true), true); + const { f, saga } = await blankRecoveryClient(t, { inheritedConsent: true }); + const entered = barrier(), resume = barrier(), decoded = barrier(); + t.after(resume.release); + const actualPause = f.image.pauseBackup, actualPrepare = saga.scope.prepareRecoveryImage; + let held = false; + f.image.pauseBackup = async () => { + await actualPause(); + if (!held) { held = true; entered.release(); await resume.promise; } + }; + // Observe completion of the actual crypto/decoder even after cancellation; + // never provide a decoded record or a replacement recovery result. + saga.scope.prepareRecoveryImage = (...args) => actualPrepare(...args).finally(decoded.release); + const actions = f.load('src/store/sagaActions/bhr'); + Object.assign(saga.scope, { GET_APP_IMAGE: actions.GET_APP_IMAGE }, f.load('src/store/utilities')); + evaluate(declarations('src/store/sagas/bhr.ts', ['getAppImageWatcher']), saga.scope, + 'production-protected-recovery-watcher.ts'); + const { runSaga, stdChannel } = require('redux-saga'), channel = stdChannel(); + const called = new Map(), cancelledCalls = []; + const dispatch = action => { const result = f.store.dispatch(action); channel.put(action); return result; }; + const task = runSaga({ channel, dispatch, getState: f.store.getState, sagaMonitor: { + effectTriggered({ effectId, effect }) { + if (effect.type !== 'CALL') return; + if (effect.payload.fn === saga.scope.recoverApp) called.set(effectId, 'recoverApp'); + if (effect.payload.fn === saga.scope.prepareRecoveryImage) called.set(effectId, 'prepareRecoveryImage'); + }, + effectCancelled(effectId) { if (called.has(effectId)) cancelledCalls.push(called.get(effectId)); }, + } }, saga.scope.getAppImageWatcher); + let unsubscribe; + try { + server.clearTrace(); dispatch(actions.getAppImage(mnemonic, false)); await entered.promise; + assert.equal(f.nativeRealm().objects('KeeperApp').length, 0); + const originSession = f.transport.currentBackupAccountSession(); + const newerDone = barrier(); + unsubscribe = f.store.subscribe(() => { + if (f.store.getState().bhr.recoveryDiscovery.status === 'protected-absent') newerDone.release(); + }); + dispatch(actions.getAppImage(require('bip39').entropyToMnemonic('55'.repeat(16)), false)); + await newerDone.promise; unsubscribe(); unsubscribe = undefined; + const expectedState = plain(f.store.getState()); + const expectedEpoch = f.transport.backupConsentEpoch(appId), expectedRevision = f.transport.backupRevision(appId); + resume.release(); await decoded.promise; await tick(); + assert.ok(cancelledCalls.includes('prepareRecoveryImage'), 'The actual watcher must cancel its called decoder effect'); + assert.ok(cancelledCalls.includes('recoverApp'), 'Cancellation must propagate into the actual called recovery generator'); + for (const schema of f.nativeRealm().schema.filter(schema => !schema.embedded)) + assert.equal(f.nativeRealm().objects(schema.name).length, 0); + assert.equal(f.store.getState().bhr.automaticCloudBackupByAppId[appId], true); + assert.equal(f.transport.backupConsentEpoch(appId), expectedEpoch); + assert.equal(f.transport.backupRevision(appId), expectedRevision); + assert.equal(f.transport.currentBackupAccountSession(), originSession); + assert.deepEqual(plain(f.store.getState()), expectedState); + assert.equal(mutations().length, 0); + assert.equal(server.trace.some(event => event.path === '/backup/v3/enroll'), false); + } finally { + unsubscribe?.(); resume.release(); task.cancel(); await task.toPromise(); channel.close(); + f.image.pauseBackup = actualPause; saga.scope.prepareRecoveryImage = actualPrepare; + } +}); + +test('the actual recovery watcher cancels a called signer producer held before commit and preserves the newer discovery', { timeout: 30000 }, async t => { + const source = await setup(t), sourceCrypto = recoverySagas(source); + persist(source, source.RealmSchema.Signer, sourceCrypto.scope.setupRecoveryKeySigningKey(mnemonic)); + assert.equal(await source.sagas.run('runBackupInspection', true, appId, true), true); + const { f, saga } = await blankRecoveryClient(t); + const actions = f.load('src/store/sagaActions/bhr'); + Object.assign(saga.scope, { GET_APP_IMAGE: actions.GET_APP_IMAGE }, f.load('src/store/utilities')); + evaluate(declarations('src/store/sagas/bhr.ts', ['getAppImageWatcher']), saga.scope, + 'production-protected-recovery-child-watcher.ts'); + const { runSaga, stdChannel } = require('redux-saga'), channel = stdChannel(); + const entered = barrier(), resume = barrier(), readCompleted = barrier(); + t.after(resume.release); + const actualCollection = f.dbManager.getCollection; + let childStarted = false, held = false, childCancelled = false, childCallId, phase = 'starting'; + t.after(() => t.diagnostic(JSON.stringify({ phase, childStarted, held, childCancelled, + discoveryStatus: f.store.getState().bhr.recoveryDiscovery.status }))); + f.dbManager.getCollection = function (schema, ...args) { + const actual = actualCollection.call(this, schema, ...args); + if (childStarted && !held && schema === f.RealmSchema.Signer) { + held = true; phase = 'child-query-held'; entered.release(); + // Hold only completion of the real native query; never synthesize its + // records or replace the actual signer producer/commit implementation. + return resume.promise.then(() => { readCompleted.release(); return actual; }); + } + return actual; + }; + const dispatch = action => { const result = f.store.dispatch(action); channel.put(action); return result; }; + const task = runSaga({ channel, dispatch, getState: f.store.getState, sagaMonitor: { + effectTriggered({ effectId, effect }) { + if (effect.type === 'CALL' && effect.payload.fn === saga.scope.addSigningDeviceWorker) { + childStarted = true; childCallId = effectId; + } + }, + effectCancelled(effectId) { if (effectId === childCallId) childCancelled = true; }, + } }, saga.scope.getAppImageWatcher); + const realmFingerprint = () => crypto.createHash('sha256').update(JSON.stringify( + f.nativeRealm().schema.filter(schema => !schema.embedded).map(schema => + [schema.name, plain(Array.from(f.nativeRealm().objects(schema.name)))]) + )).digest('hex'); + let unsubscribe; + try { + server.clearTrace(); dispatch(actions.getAppImage(mnemonic, false)); await entered.promise; + assert.equal(f.nativeRealm().objects('KeeperApp').length, 1); + const originSession = f.transport.currentBackupAccountSession(), newerDone = barrier(); + unsubscribe = f.store.subscribe(() => { + if (f.store.getState().bhr.recoveryDiscovery.status === 'protected-absent') newerDone.release(); + }); + dispatch(actions.getAppImage(require('bip39').entropyToMnemonic('66'.repeat(16)), false)); + await newerDone.promise; phase = 'newer-discovery-finished'; unsubscribe(); unsubscribe = undefined; + const expectedState = plain(f.store.getState()), expectedRealm = realmFingerprint(); + const expectedEpoch = f.transport.backupConsentEpoch(appId), expectedRevision = f.transport.backupRevision(appId); + resume.release(); await readCompleted.promise; await tick(); phase = 'old-query-settled'; + assert.equal(childCancelled, true, 'Cancellation must propagate to the actual called signer producer'); + assert.equal(realmFingerprint(), expectedRealm, 'The cancelled producer must not commit a native write'); + assert.equal(f.transport.backupConsentEpoch(appId), expectedEpoch); + assert.equal(f.transport.backupRevision(appId), expectedRevision); + assert.equal(f.transport.currentBackupAccountSession(), originSession); + assert.deepEqual(plain(f.store.getState()), expectedState, 'No stale success, retry or source feedback may replace the newer discovery'); + assert.equal(mutations().length, 0); + assert.equal(server.trace.some(event => event.path === '/backup/v3/enroll'), false); + } finally { + unsubscribe?.(); resume.release(); task.cancel(); await task.toPromise(); channel.close(); + f.dbManager.getCollection = actualCollection; + } +}); + +test('the loopback fixture rejects nonliteral raw request targets before any upstream request', { timeout: 15000 }, async () => { + const target = new URL(server.url); assert.equal(target.hostname, '127.0.0.1'); + const http = require('node:http'); server.clearTrace(); + for (const rawTarget of [ + 'http://outside.invalid/backup/v3/snapshot', '//outside.invalid/backup/v3/snapshot', + '/\\outside.invalid/backup/v3/snapshot', '/backup/v3/snapshot?query=1', + '/backup%2fv3%2fsnapshot', '/backup/v3/snapshot/extra', '/not-a-backup-route', + ]) { + // Raw targets travel only over this fixed owned loopback socket. Never + // resolve or contact the authority spelled inside an invalid request path. + const status = await new Promise((resolve, reject) => { + const request = http.request({ hostname: '127.0.0.1', port: Number(target.port), method: 'POST', + path: rawTarget, headers: { 'content-type': 'application/json', 'content-length': 2 } }, response => { + response.resume(); response.once('end', () => resolve(response.statusCode)); + response.once('error', reject); + }); + request.setTimeout(3000, () => request.destroy(new Error('Owned loopback request timed out'))); + request.once('error', reject); request.end('{}'); + }); + assert.equal(status, 400); + assert.equal(server.trace.length, 0, 'No invalid target may enter an allowed fixture route'); + assert.equal(server.backend.trace.length, 0, 'No invalid target may reach the actual backend router'); + } +}); From 7ba2dc8df68327812e6cd119809932427bcd46f5 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Sun, 18 Oct 2026 03:57:32 +0000 Subject: [PATCH 203/254] ci(r5): make JavaScript checks frozen and explicit --- .github/workflows/test.yml | 39 ++++++++++++++++++++++++++++++-------- yarn.lock | 9 ++++++++- 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 5d6104f0ae..a239207625 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -22,15 +22,27 @@ jobs: steps: - name: Checkout code - uses: actions/checkout@v7 + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7 + with: + persist-credentials: false - name: Set up Node.js - uses: actions/setup-node@v7 + uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7 with: - node-version: '22' + node-version: '22.23.3' + # Root prepare invokes CocoaPods and writes a Mac SDK path; setup.sh can + # mask the pod failure. Keep JavaScript CI installation explicit on Linux. - name: Install dependencies - run: yarn install --ignore-scripts + run: | + npm install --global yarn@1.22.22 --ignore-scripts + yarn install --frozen-lockfile --ignore-scripts --non-interactive + + - name: Apply repository dependency patches + run: node_modules/.bin/patch-package --error-on-fail + + - name: Verify dependency inputs remain unchanged + run: git diff --exit-code HEAD -- package.json yarn.lock ios/Podfile.lock - name: Run lint run: npm run lint @@ -42,15 +54,26 @@ jobs: steps: - name: Checkout code - uses: actions/checkout@v7 + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7 + with: + persist-credentials: false - name: Set up Node.js - uses: actions/setup-node@v7 + uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7 with: - node-version: '22' + node-version: '22.23.3' + # Match the explicit JavaScript setup used by the lint job. - name: Install dependencies - run: yarn install + run: | + npm install --global yarn@1.22.22 --ignore-scripts + yarn install --frozen-lockfile --ignore-scripts --non-interactive + + - name: Apply repository dependency patches + run: node_modules/.bin/patch-package --error-on-fail + + - name: Verify dependency inputs remain unchanged + run: git diff --exit-code HEAD -- package.json yarn.lock ios/Podfile.lock - name: Run tests run: npm run test diff --git a/yarn.lock b/yarn.lock index d629fa670f..7ccc056f7b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7148,6 +7148,13 @@ eslint-plugin-react-native@4.1.0, eslint-plugin-react-native@^4.0.0: dependencies: eslint-plugin-react-native-globals "^0.1.1" +eslint-plugin-react-native@^5.0.0: + version "5.0.0" + resolved "https://registry.yarnpkg.com/eslint-plugin-react-native/-/eslint-plugin-react-native-5.0.0.tgz#2ee990ba4967c557183b31121578547fb5c02d5d" + integrity sha512-VyWlyCC/7FC/aONibOwLkzmyKg4j9oI8fzrk9WYNs4I8/m436JuOTAFwLvEn1CVvc7La4cPfbCyspP4OYpP52Q== + dependencies: + eslint-plugin-react-native-globals "^0.1.1" + eslint-plugin-react@7.37.2: version "7.37.2" resolved "https://registry.yarnpkg.com/eslint-plugin-react/-/eslint-plugin-react-7.37.2.tgz#cd0935987876ba2900df2f58339f6d92305acc7a" @@ -7172,7 +7179,7 @@ eslint-plugin-react@7.37.2: string.prototype.matchall "^4.0.11" string.prototype.repeat "^1.0.0" -eslint-plugin-react@^7.30.1: +eslint-plugin-react@^7.30.1, eslint-plugin-react@^7.37.5: version "7.37.5" resolved "https://registry.yarnpkg.com/eslint-plugin-react/-/eslint-plugin-react-7.37.5.tgz#2975511472bdda1b272b34d779335c9b0e877065" integrity sha512-Qteup0SqU15kdocexFNAJMvCJEfa2xUKNV4CC1xsVMrIIqEy3SQ/rqyxCWNzfrd3/ldy6HMlD2e0JDVpDg2qIA== From 625435ac26af0304670c993a639626b1117699ba Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Sun, 18 Oct 2026 03:57:32 +0000 Subject: [PATCH 204/254] fix(r5): protect deletion intent and setup callback lifetimes --- docs/r5/WORKSTREAM.md | 95 +++- .../NewKeeperAppScreen/NewKeeperAppScreen.tsx | 22 +- src/screens/SigningDevices/DeleteKeys.tsx | 141 ++++-- tests/screens/DeleteKeys.test.tsx | 456 +++++++++++++++++- tests/screens/NewKeeperApp.test.tsx | 233 +++++++-- 5 files changed, 838 insertions(+), 109 deletions(-) diff --git a/docs/r5/WORKSTREAM.md b/docs/r5/WORKSTREAM.md index c0d633c7ff..df76dafa0a 100644 --- a/docs/r5/WORKSTREAM.md +++ b/docs/r5/WORKSTREAM.md @@ -28,15 +28,17 @@ global text scaling and Ask Keeper reply delivery (Soon); #6917 guided setup (R1 ## Baseline and overlap -Inspected R4 PR #7045 at `4927ad549dd1d610d9eb84c6d9a37ed0a6f33a29`. -It is unmerged and NOT RELEASE-READY. R4 changes the backup/deletion backend, +The initial checkpoint inspected R4 PR #7045 at +`4927ad549dd1d610d9eb84c6d9a37ed0a6f33a29`. A read-only recheck on 18 October +2026 found it still an unmerged draft at +`be89c34473bb6aab253b98325f1fe746ca391c6f`. R4 changes the backup/deletion backend, Login/CreatePin, Home/navigation, ResetEmergencyKey/ResetInheritanceKey, transaction views and Android native configuration. Leave those dependencies alone now. R4, Reproducibility, Bitkey/Assisted Multisig and Claude-review branches are not implementation sources for this workstream and must not be modified. -The three initial R5 UI modules are byte-identical between the inspected R4 head -and sprint. Initial independent changes: +The three initial R5 UI modules are byte-identical between both inspected R4 +heads and sprint. No R4 implementation was imported. Initial independent changes: - Keys tab links to Manage Keys; visible eligible keys offer Delete without Hide. - Read the full Vault collection rather than the network/canary-filtered display @@ -121,3 +123,88 @@ Next: after R4 release, reconcile this draft with its exact baseline and rerun these tests; review backend reference enforcement before expanding deletion use. Then complete the bounded startup work and actual performance/native matrix. Do not claim R5 completion, close issues, merge or release this checkpoint. + +## Independent Cloud follow-up — 18 October 2026 + +The owner authorized continued independent Cloud implementation and automated +testing while R4 and its Mac remain untouched. This follow-up stays on the R5 +draft; it changes no backend, account/session, shared PIN, native, production +configuration, version/counter or contributor-pin inputs. + +Deletion review reproduced replayed and cancelled passcode callbacks dispatching +deletion, plus callbacks reading detached active/archived-reference snapshots. +Each deletion selection now has a one-use intent, consumed synchronously and +cancelled when its confirmation closes or its screen loses focus. Confirmation +reads the live Vault collection, blocking active references and retaining keys +needed by archived wallets. The screen still does not supply backend atomicity +or shared authentication/account-session provenance. + +The full safety query must include concealed and canary references, but those +records must not disclose wallet names, descriptions or counts in a deletion +warning. Only the selected key identity is retained; every render reads current +references again, including newly added references and changes to visibility. +The warning uses a generic message whenever details are concealed or unavailable, +and leaving the screen clears it. Tests exercise both callback lifetime and the actual production +signer-identity helper; native Realm, presentation and dispatch remain replaced +at the test boundary. + +Setup review reproduced late Firebase-token completion dispatching setup or a +token update after the screen unmounted. Effect-owned guards suppress those late +Redux actions. The tests now use the real login reducer and setup action with a +small test-owned Redux store, covering repeated error/Retry/success, rapid presses, +pending/rejected tokens and successful remount. They simulate `appCreated`; they +do not prove native wallet creation, wallet-worker success, persistent storage or +backend idempotency. No arbitrary notification timeout or navigation fix was added. + +### Reproducible JavaScript preparation + +The unchanged R5 frozen installation failure was reproduced first. The repair +adds only the two missing root ESLint selectors, preserving all 2,299 existing +selectors and their version/resolution/integrity/dependency fields. Manifest, +Pod lock, Nitro/Hermes and native inputs stay unchanged. + +Node 22.23.3 and Yarn 1.22.22 frozen installation with root lifecycle scripts +disabled then passed; all six explicit dependency patches passed. Yarn still +prepares the pinned Git dependencies while packaging them, so this is not a claim +that no dependency build scripts execute. The existing IAP/Nitro peer mismatch +is retained and is not a native-build pass. + +Both existing CI jobs now use verified immutable action pins, exact Node/Yarn, +frozen installation, fail-closed patch application and manifest/lock immutability +checks. Checkout credentials are not persisted. This avoids invoking the normal +Mac-specific root prepare script on Linux; that script can mask a CocoaPods +failure because it lacks `set -e`. Required `Tests`, the local-validation gate, +and inherited tolerated lint behavior are unchanged. + +### Executed follow-up validation + +- Frozen JavaScript install: PASS (250.09 seconds), after the original frozen + failure and minimal selector repair. All six dependency patches: PASS. +- Combined offline Jest: **16 suites / 181 tests PASS**, zero failures/skips. + This includes **42 deletion** and **9 setup-lifecycle** cases. Command: + `node_modules/.bin/jest --runInBand --coverage=false --testPathIgnorePatterns '/tests/services/(client|wallet|vault)\.test\.ts$'`. + The three existing live-Electrum integration suites were not run locally; + the existing CI `npm run test` command still runs the complete suite. +- Accepted regression controls: the original deletion code failed 12/30 cases; + subsequent lifecycle checks failed 2/32 and live-warning checks failed 10/42 + before their fixes. Original setup code failed 3/9 lifecycle cases. These + were assertion failures after successful test setup, not native results. +- Existing merge-gate contracts: **11/11 PASS**. Actionlint 1.7.7, workflow + YAML/shell checks, localization JSON and `git diff --check`: PASS. +- Focused ESLint: **FAIL 29 errors / 17 warnings**, exactly the original R5 + diagnostic multiset after ignoring positions, including embedded upper-scope + line/column references. No new lint findings; this is not a clean lint pass. +- Whole TypeScript: **FAIL 1,013 diagnostics**, versus 1,014 at the original R5 + checkpoint with the same installed dependencies. Normalizing positions and + linked dependency paths finds zero added/one removed diagnostic. The removed + diagnostic was the existing Vault JSON conversion assignment in DeleteKeys. +- Independent review: the two additional visible-to-hidden/canary warning probes + failed before the live-warning repair and passed afterward. No additional + blocker was found in this bounded source/test/workflow follow-up. + +The final draft handoff records the exact tested head and hosted CI results. +The initial failures above remain part of this checkpoint's history. No native +iOS/Android, physical-device, startup/first-wallet performance, real-backend or +retained-data continuity result follows from these JavaScript tests. Finish the +R4-baseline reconciliation before the affected native matrix. No Mac access, +private-data transfer, merge, signing or release occurred. diff --git a/src/screens/NewKeeperAppScreen/NewKeeperAppScreen.tsx b/src/screens/NewKeeperAppScreen/NewKeeperAppScreen.tsx index 7a53b22d71..051fcc8762 100644 --- a/src/screens/NewKeeperAppScreen/NewKeeperAppScreen.tsx +++ b/src/screens/NewKeeperAppScreen/NewKeeperAppScreen.tsx @@ -117,21 +117,25 @@ function NewKeeperApp({ navigation }: { navigation }) { const isMultiAccountFlow = !!useAppSelector((state) => state.account).allAccounts.length; useEffect(() => { + let active = true; if (appCreated) { setInitiating(false); - updateFCM(); + updateFCM(() => active); } if (appCreationError) { setModalVisible(false); setInitiating(false); } + return () => { + active = false; + }; }, [appCreated, appCreationError]); - async function updateFCM() { + async function updateFCM(isCurrent: () => boolean) { try { const app = getApp(); const token = await getToken(getMessaging(app)); - dispatch(updateFCMTokens([token])); + if (isCurrent()) dispatch(updateFCMTokens([token])); } catch (error) { // } @@ -158,19 +162,23 @@ function NewKeeperApp({ navigation }: { navigation }) { }, []); useEffect(() => { + let active = true; if (keeperInitiating) { setModalVisible(true); - createNewApp(); + createNewApp(() => active); } + return () => { + active = false; + }; }, [keeperInitiating]); - async function createNewApp() { + async function createNewApp(isCurrent: () => boolean) { try { const app = getApp(); const fcmToken = await getToken(getMessaging(app)); - dispatch(setupKeeperApp(fcmToken)); + if (isCurrent()) dispatch(setupKeeperApp(fcmToken)); } catch (error) { - dispatch(setupKeeperApp()); + if (isCurrent()) dispatch(setupKeeperApp()); } } diff --git a/src/screens/SigningDevices/DeleteKeys.tsx b/src/screens/SigningDevices/DeleteKeys.tsx index 0d4d1fad42..3a5818b0e2 100644 --- a/src/screens/SigningDevices/DeleteKeys.tsx +++ b/src/screens/SigningDevices/DeleteKeys.tsx @@ -1,4 +1,4 @@ -import React, { useContext, useEffect, useState } from 'react'; +import React, { useCallback, useContext, useEffect, useRef, useState } from 'react'; import { Box, Pressable, useColorMode } from '@gluestack-ui/themed-native-base'; import { hp, windowWidth, wp } from 'src/constants/responsive'; import ScreenWrapper from 'src/components/ScreenWrapper'; @@ -17,13 +17,12 @@ import { Signer, Vault } from 'src/services/wallets/interfaces/vault'; import { useQuery } from '@realm/react'; import { RealmSchema } from 'src/storage/realm/enum'; import { getJSONFromRealmObject } from 'src/storage/realm/utils'; -import { CommonActions, useNavigation } from '@react-navigation/native'; +import { CommonActions, useFocusEffect, useNavigation } from '@react-navigation/native'; import ActionCard from 'src/components/ActionCard'; import WalletVault from 'src/assets/images/wallet_vault.svg'; import { archiveSigningDevice, deleteSigningDevice } from 'src/store/sagaActions/vaults'; -import useArchivedVault from 'src/hooks/useArchivedVaults'; import { LocalizationContext } from 'src/context/Localization/LocContext'; -import { SignerType } from 'src/services/wallets/enums'; +import { SignerType, VaultType, VisibilityType } from 'src/services/wallets/enums'; import { RECOVERY_KEY_SIGNER_NAME } from 'src/constants/defaultData'; import KeyCard from 'src/components/SigningDevices/KeyCard'; import HexagonIcon from 'src/components/HexagonIcon'; @@ -42,6 +41,8 @@ import useToastMessage from 'src/hooks/useToastMessage'; import TickIcon from 'src/assets/images/icon_tick.svg'; import usePlan from 'src/hooks/usePlan'; +type DeletionIntent = { signer: Signer }; + function DeleteKeys({ route }) { const { colorMode } = useColorMode(); const { translations } = useContext(LocalizationContext); @@ -63,14 +64,24 @@ function DeleteKeys({ route }) { const dispatch = useDispatch(); const navigation = useNavigation(); const [unhidingKeyUID, setUnhidingKeyUID] = useState(''); - const [multipleHidden, setMultipleHidden] = useState(false); // Deletion safety must include hidden/canary wallets and both networks. // Presentation hooks intentionally filter those records out. - const allVaults: Vault[] = useQuery(RealmSchema.Vault).map(getJSONFromRealmObject); - const { archivedVaults } = useArchivedVault(); + const vaultRecords = useQuery(RealmSchema.Vault); + // Realm types toJSON as DefaultObject; this query contains Vault records. + const readVaults = (): Vault[] => + vaultRecords.map((record) => getJSONFromRealmObject(record) as unknown as Vault); const [warningEnabled, setHideWarning] = useState(false); - const [vaultsUsed, setVaultsUsed] = useState(); - const [signerToDelete, setSignerToDelete] = useState(); + const [warningKeyUID, setWarningKeyUID] = useState(null); + // Store only the key identity: names, visibility and all current references + // must be read again when the warning rerenders, including newly added ones. + const vaultsUsed = warningKeyUID === null + ? [] + : readVaults().filter( + (vault) => !vault.archived && vault.signers.some((s) => getKeyUID(s) === warningKeyUID) + ); + const [deletionIntent, setDeletionIntent] = useState(null); + const pendingDeletion = useRef(null); + const signerToDelete = deletionIntent?.signer; const [deletedSigner, setDeletedSigner] = useState(); const deletingKeyModalVisible = useAppSelector((state) => state.bhr.deletingKeyModalVisible); const keyDeletedSuccessModalVisible = useAppSelector( @@ -85,31 +96,57 @@ function DeleteKeys({ route }) { const [showAllForced, setShowAllForced] = useState(false); const [passwordMode, setPasswordMode] = useState(PasswordMode.DEFAULT); + useFocusEffect( + useCallback( + () => () => { + pendingDeletion.current = null; + setDeletionIntent(null); + setConfirmPassVisible(false); + setWarningKeyUID(null); + setHideWarning(false); + }, + [] + ) + ); + const displaySigners = showAll ? [...allUnhideSigners, ...hiddenSigners] : allUnhideSigners; + // References remain blocking even when their names must stay concealed. + const showReferenceDetails = vaultsUsed.length > 0 && vaultsUsed.every( + (vault) => + typeof vault.id === 'string' && vault.id.trim().length > 0 && + vault.type !== VaultType.CANARY && + vault.presentationData?.visibility === VisibilityType.DEFAULT + ); + const showMultipleReferences = vaultsUsed.length > 1 && showReferenceDetails; + + const cancelDeletion = (intent: DeletionIntent | null) => { + if (pendingDeletion.current !== intent) return; + pendingDeletion.current = null; + setDeletionIntent(null); + setConfirmPassVisible(false); + }; - const onSuccess = () => { - if (signerToDelete) { - // References may have changed while the passcode dialog was open. - const vaultsInvolved = getActiveReferences(signerToDelete); + const onSuccess = (intent: DeletionIntent | null) => { + if (intent && pendingDeletion.current === intent) { + // Consume synchronously: replayed/cancelled callbacks cannot reuse this + // authorization or clear a later selection before React rerenders. + cancelDeletion(intent); + const { signer } = intent; + // Read live Results at invocation; a retained callback must not use a + // render-time snapshot from before a new active or archived reference. + const references = getReferences(signer); + const vaultsInvolved = references.filter((vault) => !vault.archived); if (vaultsInvolved.length) { - setConfirmPassVisible(false); - setSignerToDelete(null); - setMultipleHidden(vaultsInvolved.length > 1); - setVaultsUsed(vaultsInvolved); + setWarningKeyUID(getKeyUID(signer)); setHideWarning(true); return; } - setConfirmPassVisible(false); - const involvedArchivedVaults = archivedVaults.filter((vault) => - vault.signers.find((s) => getKeyUID(s) === getKeyUID(signerToDelete)) - ); - if (involvedArchivedVaults.length) { - dispatch(archiveSigningDevice([signerToDelete])); + if (references.some((vault) => vault.archived)) { + dispatch(archiveSigningDevice([signer])); } else { - dispatch(deleteSigningDevice([signerToDelete])); + dispatch(deleteSigningDevice([signer])); } - setDeletedSigner(signerToDelete); - setSignerToDelete(null); + setDeletedSigner(signer); } }; const onForceProceed = () => { @@ -140,24 +177,26 @@ function DeleteKeys({ route }) { } }; - const getActiveReferences = (signer: Signer) => - allVaults.filter( - (vault) => !vault.archived && vault.signers.some((s) => getKeyUID(s) === getKeyUID(signer)) - ); + const getReferences = (signer: Signer): Vault[] => + readVaults() + .filter((vault) => vault.signers.some((s) => getKeyUID(s) === getKeyUID(signer))); const handleDelete = (signer: Signer) => { - setSignerToDelete(null); - setMultipleHidden(false); - const vaultsInvolved = getActiveReferences(signer); + pendingDeletion.current = null; + setDeletionIntent(null); + setHideWarning(false); + setWarningKeyUID(null); + const vaultsInvolved = getReferences(signer).filter((vault) => !vault.archived); if (vaultsInvolved.length > 0) { - if (vaultsInvolved.length > 1) setMultipleHidden(true); setHideWarning(true); - setVaultsUsed(vaultsInvolved); + setWarningKeyUID(getKeyUID(signer)); setConfirmPassVisible(false); return; } + const intent = { signer }; + pendingDeletion.current = intent; + setDeletionIntent(intent); setConfirmPassVisible(true); - setSignerToDelete(signer); }; useEffect(() => { @@ -212,7 +251,7 @@ function DeleteKeys({ route }) { showsHorizontalScrollIndicator={false} > {vaultsUsed.map((vault) => ( - + - {multipleHidden + {showMultipleReferences ? signerText.deleteMultipleVaultInstruction : signerText.deleteVaultInstruction} @@ -304,13 +343,17 @@ function DeleteKeys({ route }) { setHideWarning(false)} title={ - multipleHidden ? signerText.deleteMultipleVaultWarning : signerText.deleteVaultWarning + showMultipleReferences ? signerText.deleteMultipleVaultWarning : signerText.deleteVaultWarning } subTitle={ - multipleHidden ? signerText.multipleVaultWarningSubtitle : signerText.vaultWarningSubtitle + !showReferenceDetails + ? signerText.deleteVaultInstruction + : showMultipleReferences + ? signerText.multipleVaultWarningSubtitle + : signerText.vaultWarningSubtitle } buttonText={signerText.viewVault} secondaryButtonText={signerText.back} @@ -324,7 +367,9 @@ function DeleteKeys({ route }) { navigation.dispatch(CommonActions.navigate('ManageWallets')); }} textColor={`${colorMode}.textGreen`} - Content={() => } + Content={() => + showReferenceDetails ? : null + } /> { - setConfirmPassVisible(false); - setSignerToDelete(null); - }} + close={() => cancelDeletion(deletionIntent)} title={signerText.enterPasscode} subTitleWidth={wp(240)} subTitle={signerText.confirmPasscodeToDeleteKey} @@ -391,11 +433,8 @@ function DeleteKeys({ route }) { )} { - setConfirmPassVisible(false); - setSignerToDelete(null); - }} - onSuccess={onSuccess} + close={() => cancelDeletion(deletionIntent)} + onSuccess={() => onSuccess(deletionIntent)} /> )} diff --git a/tests/screens/DeleteKeys.test.tsx b/tests/screens/DeleteKeys.test.tsx index a00b2a14fd..b2a264c799 100644 --- a/tests/screens/DeleteKeys.test.tsx +++ b/tests/screens/DeleteKeys.test.tsx @@ -2,25 +2,41 @@ import React from 'react'; import renderer, { act } from 'react-test-renderer'; jest.mock('src/constants/responsive', () => ({ hp: (v) => v, wp: (v) => v, windowWidth: 400 })); -// Render the real screen and exercise its actions. Only native/presentation, -// account storage and dispatch boundaries are replaced. +// Render the real screen with the production signer-identity helper. Native, +// presentation, account storage and dispatch boundaries are replaced. const mockDispatch = jest.fn(); -const mockSigner: any = { masterFingerprint: 'abcd', type: 'COLDCARD', hidden: false }; +const mockSigner: any = { + masterFingerprint: 'abcd', + type: 'COLDCARD', + hidden: false, + networkType: 'TESTNET', + signerXpubs: { P2WSH: [{ derivationPath: "m/48'/1'/0'/2'", xpub: 'public-test-key' }] }, +}; +let mockSigners: any[] = [mockSigner]; let mockVaults: any[] = []; +// Realm Results stay live even inside a previously rendered callback. +const mockVaultQuery = { map: (callback) => mockVaults.map(callback) }; +const mockFocus: { effect?: () => () => void; cleanup?: () => void } = {}; const mockState = { bhr: {} }; jest.mock('react-redux', () => ({ useDispatch: () => mockDispatch })); -jest.mock('@realm/react', () => ({ useQuery: () => mockVaults })); +jest.mock('@realm/react', () => ({ useQuery: () => mockVaultQuery })); jest.mock('src/storage/realm/utils', () => ({ getJSONFromRealmObject: (v) => v })); jest.mock('src/hooks/useSigners', () => ({ __esModule: true, - default: () => ({ signers: [mockSigner] }), -})); -jest.mock('src/hooks/useArchivedVaults', () => ({ - __esModule: true, - default: () => ({ archivedVaults: mockVaults.filter((v) => v.archived) }), + default: () => ({ signers: mockSigners }), })); jest.mock('src/store/hooks', () => ({ useAppSelector: (select) => select(mockState) })); -jest.mock('src/utils/utilities', () => ({ getKeyUID: (s) => s.masterFingerprint })); +jest.mock('src/utils/utilities', () => ({ + getKeyUID: jest.requireActual('src/utils/utilities').getKeyUID, +})); +// These imports belong to unrelated utilities; identity uses their real enum +// constants and derivation/account logic without invoking storage or crypto. +jest.mock('src/utils/service-utilities/config', () => ({ __esModule: true, default: {}, APP_STAGE: {} })); +jest.mock('src/constants/Bitcoin', () => ({ isTestnet: jest.fn() })); +jest.mock('src/services/qr/bbqr/split', () => ({})); +jest.mock('src/storage/realm/dbManager', () => ({})); +jest.mock('src/utils/service-utilities/encryption', () => ({})); +jest.mock('src/constants/defaultData', () => ({ RECOVERY_KEY_SIGNER_NAME: 'RECOVERY_KEY_SIGNER_NAME' })); jest.mock('src/hooks/usePlan', () => ({ __esModule: true, default: () => ({}) })); jest.mock('src/hooks/useToastMessage', () => ({ __esModule: true, @@ -28,6 +44,13 @@ jest.mock('src/hooks/useToastMessage', () => ({ })); jest.mock('@react-navigation/native', () => ({ useNavigation: () => ({ dispatch: jest.fn() }), + useFocusEffect: (effect) => { + require('react').useEffect(() => { + mockFocus.effect = effect; + mockFocus.cleanup = effect(); + return () => mockFocus.cleanup?.(); + }, [effect]); + }, CommonActions: {}, })); jest.mock('src/hardware', () => ({ @@ -86,19 +109,42 @@ function mount() { screen = renderer.create(); }); } -function chooseDelete() { - act(() => find('KeyCard').props.primaryAction()); +function chooseDelete(index = 0) { + act(() => screen.root.findAllByType('KeyCard')[index].props.primaryAction()); +} +function passcodeCallbacks(index = 0) { + const Content = screen.root + .findAllByType('Modal') + .filter((m) => m.props.title === 'enterPasscode')[index].props.Content; + let content; + act(() => { + content = renderer.create(); + }); + const callbacks = content.root.findByType('Passcode').props; + act(() => content.unmount()); + return callbacks; } function verifyPasscode() { - const Content = modal('enterPasscode').props.Content; + const { onSuccess } = passcodeCallbacks(); + act(() => onSuccess()); +} +function warningContent() { + const warning = screen.root + .findAllByType('Modal') + .find((m) => m.props.visible && m.props.title.startsWith('delete')); + const Content = warning.props.Content; let content; act(() => { content = renderer.create(); }); - act(() => content.root.findByType('Passcode').props.onSuccess()); + const output = content.toJSON(); act(() => content.unmount()); + return JSON.stringify(output); } beforeEach(() => { + mockFocus.effect = undefined; + mockFocus.cleanup = undefined; + mockSigners = [mockSigner]; mockVaults = []; mockSigner.type = 'COLDCARD'; mockSigner.hidden = false; @@ -122,7 +168,7 @@ test('visible keys offer Delete without first hiding; passcode is required', () test.each(['normal', 'hidden', 'canary', 'other-network'])( 'blocks key referenced by %s wallet', (kind) => { - mockVaults = [{ kind, signers: [mockSigner], archived: false }]; + mockVaults = [{ id: 'reference', kind, signers: [mockSigner], archived: false }]; mount(); chooseDelete(); expect(modal('deleteVaultWarning').props.visible).toBe(true); @@ -134,7 +180,7 @@ test.each(['normal', 'hidden', 'canary', 'other-network'])( test('rechecks a wallet reference added while passcode was open', () => { mount(); chooseDelete(); - mockVaults = [{ signers: [mockSigner], archived: false }]; + mockVaults = [{ id: 'new-active-reference', signers: [mockSigner], archived: false }]; act(() => screen.update()); verifyPasscode(); expect(mockDispatch).not.toHaveBeenCalled(); @@ -142,7 +188,7 @@ test('rechecks a wallet reference added while passcode was open', () => { }); test('retains a key used by an archived wallet instead of deleting it', () => { - mockVaults = [{ signers: [mockSigner], archived: true }]; + mockVaults = [{ id: 'archived-reference', signers: [mockSigner], archived: true }]; mount(); chooseDelete(); verifyPasscode(); @@ -177,6 +223,34 @@ test('hidden keys remain concealed before authentication', () => { expect(screen.root.findAllByType('KeyCard')).toHaveLength(0); }); +test('cancelling hidden-key authentication leaves the key concealed', () => { + mockSigner.hidden = true; + mount(); + act(() => find('Pressable').props.onPress()); + const callbacks = passcodeCallbacks(1); + act(() => callbacks.close()); + expect(screen.root.findAllByType('KeyCard')).toHaveLength(0); + expect(mockDispatch).not.toHaveBeenCalled(); +}); + +test('an authenticated hidden key still needs its own deletion confirmation', () => { + mockSigner.hidden = true; + mount(); + act(() => find('Pressable').props.onPress()); + const callbacks = passcodeCallbacks(1); + act(() => { + callbacks.onSuccess(); + callbacks.close(); + }); + expect(find('KeyCard').props.primaryText).toBe('delete'); + expect(mockDispatch).not.toHaveBeenCalled(); + chooseDelete(); + expect(modal('enterPasscode').props.visible).toBe(true); + expect(mockDispatch).not.toHaveBeenCalled(); + verifyPasscode(); + expect(mockDispatch).toHaveBeenCalledWith({ type: 'DELETE', signers: [mockSigner] }); +}); + test('repeated confirmation does not dispatch a second deletion', () => { mount(); chooseDelete(); @@ -184,3 +258,351 @@ test('repeated confirmation does not dispatch a second deletion', () => { verifyPasscode(); expect(mockDispatch).toHaveBeenCalledTimes(1); }); + +test('the same passcode success callback can consume a deletion only once', () => { + mount(); + chooseDelete(); + const { onSuccess } = passcodeCallbacks(); + act(() => { + onSuccess(); + onSuccess(); + }); + expect(mockDispatch).toHaveBeenCalledTimes(1); +}); + +test.each(['modal', 'passcode'])('cancel through %s invalidates a captured success callback', (via) => { + mount(); + chooseDelete(); + const callbacks = passcodeCallbacks(); + act(() => { + if (via === 'modal') modal('enterPasscode').props.close(); + else callbacks.close(); + }); + act(() => callbacks.onSuccess()); + expect(mockDispatch).not.toHaveBeenCalled(); +}); + +test('success from a previous key cannot authorize or cancel the next key selection', () => { + const secondSigner = { ...mockSigner, masterFingerprint: 'ef01' }; + mockSigners = [mockSigner, secondSigner]; + mount(); + chooseDelete(); + const first = passcodeCallbacks(); + act(() => first.close()); + chooseDelete(1); + const second = passcodeCallbacks(); + act(() => { + first.onSuccess(); + first.close(); + }); + expect(mockDispatch).not.toHaveBeenCalled(); + expect(modal('enterPasscode').props.visible).toBe(true); + act(() => second.onSuccess()); + expect(mockDispatch).toHaveBeenCalledTimes(1); + expect(mockDispatch).toHaveBeenCalledWith({ type: 'DELETE', signers: [secondSigner] }); +}); + +test('success from a cancelled request cannot authorize a new request for the same key', () => { + mount(); + chooseDelete(); + const first = passcodeCallbacks(); + act(() => first.close()); + chooseDelete(); + const second = passcodeCallbacks(); + act(() => first.onSuccess()); + expect(mockDispatch).not.toHaveBeenCalled(); + act(() => second.onSuccess()); + expect(mockDispatch).toHaveBeenCalledTimes(1); +}); + +test('leaving the screen invalidates an outstanding deletion callback', () => { + mount(); + chooseDelete(); + const { onSuccess } = passcodeCallbacks(); + act(() => { + screen.unmount(); + screen = undefined; + }); + act(() => onSuccess()); + expect(mockDispatch).not.toHaveBeenCalled(); +}); + +test('blur and return require a new deletion intent and ignore the old callback', () => { + mount(); + chooseDelete(); + const first = passcodeCallbacks(); + act(() => { + mockFocus.cleanup?.(); + mockFocus.cleanup = undefined; + }); + expect(modal('enterPasscode').props.visible).toBe(false); + act(() => { + mockFocus.cleanup = mockFocus.effect?.(); + }); + chooseDelete(); + const second = passcodeCallbacks(); + act(() => first.onSuccess()); + expect(mockDispatch).not.toHaveBeenCalled(); + act(() => second.onSuccess()); + expect(mockDispatch).toHaveBeenCalledTimes(1); +}); + +test('a captured success callback rechecks references added after it was rendered', () => { + mount(); + chooseDelete(); + const { onSuccess } = passcodeCallbacks(); + mockVaults = [{ id: 'new-active-reference', signers: [mockSigner], archived: false }]; + act(() => screen.update()); + act(() => onSuccess()); + expect(mockDispatch).not.toHaveBeenCalled(); + expect(modal('deleteVaultWarning').props.visible).toBe(true); +}); + +test('reads live references at confirmation even before a query notification rerenders the screen', () => { + mount(); + chooseDelete(); + const { onSuccess } = passcodeCallbacks(); + mockVaults = [{ id: 'new-active-reference', signers: [mockSigner], archived: false }]; + act(() => onSuccess()); + expect(mockDispatch).not.toHaveBeenCalled(); + expect(modal('deleteVaultWarning').props.visible).toBe(true); +}); + +test('a captured success callback retains a reference archived while authentication was open', () => { + mount(); + chooseDelete(); + const { onSuccess } = passcodeCallbacks(); + mockVaults = [{ id: 'new-archived-reference', signers: [mockSigner], archived: true }]; + act(() => screen.update()); + act(() => onSuccess()); + expect(mockDispatch).toHaveBeenCalledTimes(1); + expect(mockDispatch).toHaveBeenCalledWith({ type: 'ARCHIVE', signers: [mockSigner] }); +}); + +test.each([ + ['hidden', 'DEFAULT', 'HIDDEN'], + ['canary', 'CANARY', 'DEFAULT'], + ['unknown visibility', 'DEFAULT', undefined], +])('blocks a %s reference without revealing its wallet name or description', (_, type, visibility) => { + mockVaults = [{ + id: 'concealed-reference', + signers: [mockSigner], + archived: false, + type, + presentationData: { name: 'PRIVATE_WALLET_NAME', description: 'PRIVATE_WALLET_DESCRIPTION', visibility }, + }]; + mount(); + chooseDelete(); + expect(modal('deleteVaultWarning').props.visible).toBe(true); + expect(modal('enterPasscode').props.visible).toBe(false); + expect(warningContent()).not.toContain('PRIVATE_WALLET'); + expect(mockDispatch).not.toHaveBeenCalled(); +}); + +test('a mix of visible and concealed references uses a generic warning without wallet details', () => { + mockVaults = ['DEFAULT', 'HIDDEN'].map((visibility, index) => ({ + id: `reference-${index}`, + signers: [mockSigner], + archived: false, + type: 'DEFAULT', + presentationData: { name: 'PRIVATE_WALLET_NAME', description: 'PRIVATE_WALLET_DESCRIPTION', visibility }, + })); + mount(); + chooseDelete(); + expect(modal('deleteVaultWarning').props.visible).toBe(true); + expect(modal('deleteVaultWarning').props.subTitle).toBe('deleteVaultInstruction'); + expect(warningContent()).not.toContain('PRIVATE_WALLET'); + expect(mockDispatch).not.toHaveBeenCalled(); +}); + +test('ordinary visible references still show the wallet that blocks deletion', () => { + mockVaults = [{ + id: 'visible-vault', + signers: [mockSigner], + archived: false, + type: 'DEFAULT', + presentationData: { name: 'Visible wallet', description: 'Visible description', visibility: 'DEFAULT' }, + }]; + mount(); + chooseDelete(); + expect(warningContent()).toContain('Visible wallet'); + expect(warningContent()).toContain('Visible description'); + expect(mockDispatch).not.toHaveBeenCalled(); +}); + +test('production identity matches a vault key with the selected signer account and network', () => { + mockVaults = [{ + id: 'identity-reference', + archived: false, + signers: [{ masterFingerprint: mockSigner.masterFingerprint, derivationPath: "m/48'/1'/0'/2'" }], + }]; + mount(); + chooseDelete(); + expect(modal('deleteVaultWarning').props.visible).toBe(true); + expect(mockDispatch).not.toHaveBeenCalled(); +}); + +test.each([ + ['account', "m/48'/1'/1'/2'"], + ['network', "m/48'/0'/0'/2'"], +])('a shared fingerprint with another %s is not the selected signer identity', (_, derivationPath) => { + mockVaults = [{ + id: 'other-identity-reference', + archived: false, + signers: [{ masterFingerprint: mockSigner.masterFingerprint, derivationPath }], + }]; + mount(); + chooseDelete(); + expect(modal('enterPasscode').props.visible).toBe(true); + verifyPasscode(); + expect(mockDispatch).toHaveBeenCalledWith({ type: 'DELETE', signers: [mockSigner] }); +}); + +test.each([ + ['hidden', 'DEFAULT', 'HIDDEN'], + ['canary', 'CANARY', 'DEFAULT'], +])('a wallet becoming %s while the warning is open immediately conceals its details', (_, type, visibility) => { + mockVaults = [{ + id: 'live-reference', + signers: [mockSigner], + archived: false, + type: 'DEFAULT', + presentationData: { name: 'BECAME_PRIVATE_NAME', description: 'BECAME_PRIVATE_DESCRIPTION', visibility: 'DEFAULT' }, + }]; + mount(); + chooseDelete(); + expect(warningContent()).toContain('BECAME_PRIVATE_NAME'); + mockVaults = [{ + ...mockVaults[0], + type, + presentationData: { ...mockVaults[0].presentationData, visibility }, + }]; + act(() => screen.update()); + expect(modal('deleteVaultWarning').props.visible).toBe(true); + expect(modal('deleteVaultWarning').props.subTitle).toBe('deleteVaultInstruction'); + expect(warningContent()).not.toContain('BECAME_PRIVATE'); + expect(mockDispatch).not.toHaveBeenCalled(); +}); + +test('warning cards use current wallet metadata rather than an old snapshot', () => { + mockVaults = [{ + id: 'renamed-reference', + signers: [mockSigner], + archived: false, + type: 'DEFAULT', + presentationData: { name: 'Old wallet name', description: 'Old description', visibility: 'DEFAULT' }, + }]; + mount(); + chooseDelete(); + mockVaults = [{ + ...mockVaults[0], + presentationData: { ...mockVaults[0].presentationData, name: 'Current wallet name', description: 'Current description' }, + }]; + act(() => screen.update()); + const output = warningContent(); + expect(output).toContain('Current wallet name'); + expect(output).toContain('Current description'); + expect(output).not.toContain('Old wallet name'); + expect(output).not.toContain('Old description'); +}); + +test.each([undefined, ''])('a missing or empty reference ID (%s) suppresses wallet details', (id) => { + mockVaults = [{ + id, + signers: [mockSigner], + archived: false, + type: 'DEFAULT', + presentationData: { name: 'UNRESOLVED_PRIVATE_NAME', description: 'UNRESOLVED_PRIVATE_DESCRIPTION', visibility: 'DEFAULT' }, + }]; + mount(); + chooseDelete(); + expect(modal('deleteVaultWarning').props.visible).toBe(true); + expect(warningContent()).not.toContain('UNRESOLVED_PRIVATE'); + expect(mockDispatch).not.toHaveBeenCalled(); +}); + +test('a missing live reference keeps a generic block until a new Delete selection', () => { + mockVaults = [{ + id: 'removed-reference', + signers: [mockSigner], + archived: false, + type: 'DEFAULT', + presentationData: { name: 'REMOVED_PRIVATE_NAME', description: 'REMOVED_PRIVATE_DESCRIPTION', visibility: 'DEFAULT' }, + }]; + mount(); + chooseDelete(); + mockVaults = []; + act(() => screen.update()); + expect(modal('deleteVaultWarning').props.visible).toBe(true); + expect(warningContent()).not.toContain('REMOVED_PRIVATE'); + expect(mockDispatch).not.toHaveBeenCalled(); + act(() => modal('deleteVaultWarning').props.close()); + chooseDelete(); + expect(modal('enterPasscode').props.visible).toBe(true); + expect(mockDispatch).not.toHaveBeenCalled(); +}); + +test('blur and return do not restore a previous wallet-reference warning', () => { + mockVaults = [{ id: 'blur-reference', signers: [mockSigner], archived: false }]; + mount(); + chooseDelete(); + expect(modal('deleteVaultWarning').props.visible).toBe(true); + act(() => { + mockFocus.cleanup?.(); + mockFocus.cleanup = undefined; + }); + expect(modal('deleteVaultWarning').props.visible).toBe(false); + act(() => { + mockFocus.cleanup = mockFocus.effect?.(); + }); + expect(modal('deleteVaultWarning').props.visible).toBe(false); + expect(mockDispatch).not.toHaveBeenCalled(); +}); + +test.each([ + ['hidden', 'DEFAULT', 'HIDDEN'], + ['canary', 'CANARY', 'DEFAULT'], +])('a new %s reference switches an open warning to generic without exposing counts', (_, type, visibility) => { + mockVaults = [{ + id: 'initial-visible-reference', + signers: [mockSigner], + archived: false, + type: 'DEFAULT', + presentationData: { name: 'Visible wallet', visibility: 'DEFAULT' }, + }]; + mount(); + chooseDelete(); + mockVaults = [...mockVaults, { + id: 'new-concealed-reference', + signers: [mockSigner], + archived: false, + type, + presentationData: { name: 'NEW_PRIVATE_NAME', description: 'NEW_PRIVATE_DESCRIPTION', visibility }, + }]; + act(() => screen.update()); + expect(modal('deleteVaultWarning').props.visible).toBe(true); + expect(modal('deleteVaultWarning').props.subTitle).toBe('deleteVaultInstruction'); + expect(warningContent()).toBe('null'); + expect(mockDispatch).not.toHaveBeenCalled(); +}); + +test('a reference ID reassigned to an unrelated key does not reveal the unrelated wallet', () => { + mockVaults = [{ + id: 'same-id', + signers: [mockSigner], + archived: false, + type: 'DEFAULT', + presentationData: { name: 'PREVIOUS_NAME', visibility: 'DEFAULT' }, + }]; + mount(); + chooseDelete(); + mockVaults = [{ + ...mockVaults[0], + signers: [{ ...mockSigner, masterFingerprint: 'ef01' }], + presentationData: { name: 'UNRELATED_NAME', visibility: 'DEFAULT' }, + }]; + act(() => screen.update()); + expect(modal('deleteVaultWarning').props.visible).toBe(true); + expect(warningContent()).toBe('null'); + expect(mockDispatch).not.toHaveBeenCalled(); +}); diff --git a/tests/screens/NewKeeperApp.test.tsx b/tests/screens/NewKeeperApp.test.tsx index ce43c2848b..4a7c80501a 100644 --- a/tests/screens/NewKeeperApp.test.tsx +++ b/tests/screens/NewKeeperApp.test.tsx @@ -1,17 +1,10 @@ import React from 'react'; import renderer, { act } from 'react-test-renderer'; +import { configureStore } from '@reduxjs/toolkit'; +import { Provider } from 'react-redux'; +import loginReducer, { setAppCreationError } from '../../src/store/reducers/login'; -const mockDispatch = jest.fn(); -const mockState = { - bhr: {}, - storage: { appCreated: false }, - login: { appCreationError: true }, - account: { allAccounts: [] }, -}; -jest.mock('src/store/hooks', () => ({ - useAppDispatch: () => mockDispatch, - useAppSelector: (select) => select(mockState), -})); +const mockGetToken = jest.fn(); jest.mock('src/constants/responsive', () => ({ hp: (v) => v, wp: (v) => v, windowWidth: 400 })); jest.mock('@gluestack-ui/themed-native-base', () => ({ Box: 'Box', @@ -22,19 +15,15 @@ jest.mock('@react-navigation/native', () => ({ useIsFocused: () => true })); jest.mock('@react-native-firebase/app', () => ({ getApp: jest.fn() })); jest.mock('@react-native-firebase/messaging', () => ({ getMessaging: jest.fn(), - getToken: async () => 'test-token', + getToken: (...args) => mockGetToken(...args), })); jest.mock('src/hooks/useToastMessage', () => ({ __esModule: true, default: () => ({ showToast: jest.fn() }), })); -jest.mock('src/store/reducers/login', () => ({ - setAppCreationError: (value) => ({ type: 'ERROR', value }), -})); -jest.mock('src/store/sagaActions/storage', () => ({ - setupKeeperApp: (token) => ({ type: 'SETUP', token }), +jest.mock('src/store/sagaActions/notifications', () => ({ + updateFCMTokens: (tokens) => ({ type: 'UPDATE_FCM', tokens }), })); -jest.mock('src/store/sagaActions/notifications', () => ({ updateFCMTokens: jest.fn() })); jest.mock('src/utils/service-utilities/config', () => ({ KEEPER_WEBSITE_BASE_URL: 'https://example.invalid', })); @@ -64,21 +53,205 @@ Object.entries({ }).forEach(([path, name]) => jest.doMock(require.resolve(`../../${path}`), () => name)); const NewKeeperApp = require('../../src/screens/NewKeeperAppScreen/NewKeeperAppScreen').default; -test('setup failure presents one actionable retry dialog and starts one retry', async () => { - let screen; +let screen; +let store; +let actions; +let view; + +function deferred() { + let resolve!: (value: string) => void; + let reject!: (reason: Error) => void; + const promise = new Promise((resolvePromise, rejectPromise) => { + resolve = resolvePromise; + reject = rejectPromise; + }); + return { promise, resolve, reject }; +} + +async function mount({ error = false, created = false } = {}) { + actions = []; + const loginState = { + ...loginReducer(undefined, { type: '@@test/init' }), + appCreationError: error, + }; + store = configureStore({ + reducer: { + login: (state = loginState, action) => loginReducer(state, action), + storage: (state = { appCreated: created }, action) => + action.type === 'TEST_APP_CREATED' ? { appCreated: true } : state, + bhr: (state = {}) => state, + account: (state = { allAccounts: [] }) => state, + }, + middleware: (getDefaultMiddleware) => + getDefaultMiddleware().concat(() => (next) => (action) => { + actions.push(action); + return next(action); + }), + }); + const navigation = { replace: jest.fn(), navigate: jest.fn() }; + view = () => ( + + + + ); await act(async () => { - screen = renderer.create(); + screen = renderer.create(view()); }); - const visible = screen.root.findAllByType('Modal').filter((m) => m.props.visible); - expect(visible).toHaveLength(1); - expect(visible[0].props.buttonText).toBe('Retry'); +} + +function visibleDialogs() { + return screen.root.findAllByType('Modal').filter((modal) => modal.props.visible); +} + +function setupActions() { + return actions.filter((action) => action.type === 'SETUP_KEEPER_APP'); +} + +async function start() { await act(async () => { - visible[0].props.buttonCallback(); - visible[0].props.buttonCallback(); + screen.root.findByProps({ testID: 'view_startNewTile' }).props.onPress(); }); - expect(mockDispatch).toHaveBeenCalledWith({ type: 'ERROR', value: false }); - expect(mockDispatch.mock.calls.filter(([a]) => a.type === 'SETUP')).toEqual([ - [{ type: 'SETUP', token: 'test-token' }], +} + +beforeEach(() => { + mockGetToken.mockReset().mockResolvedValue('test-token'); +}); + +afterEach(() => { + if (screen) { + act(() => screen.unmount()); + screen = null; + } +}); + +test('setup failure presents one retry dialog, clears its error and starts one retry', async () => { + await mount({ error: true }); + expect(visibleDialogs()).toHaveLength(1); + const retry = visibleDialogs()[0]; + expect(retry.props.buttonText).toBe('Retry'); + await act(async () => { + retry.props.buttonCallback(); + retry.props.buttonCallback(); + }); + expect(store.getState().login.appCreationError).toBe(false); + expect(visibleDialogs()).toHaveLength(1); + expect(visibleDialogs()[0].props.buttonText).toBeNull(); + expect(setupActions()).toEqual([ + { type: 'SETUP_KEEPER_APP', payload: { appName: '', fcmToken: 'test-token' } }, ]); - act(() => screen.unmount()); +}); + +test('repeated failures each allow one retry and a later success does not replay setup', async () => { + await mount(); + await start(); + expect(setupActions()).toHaveLength(1); + + for (const attempts of [2, 3]) { + await act(async () => { + store.dispatch(setAppCreationError(true)); + }); + expect(visibleDialogs()).toHaveLength(1); + expect(visibleDialogs()[0].props.buttonText).toBe('Retry'); + await act(async () => { + visibleDialogs()[0].props.buttonCallback(); + }); + expect(store.getState().login.appCreationError).toBe(false); + expect(visibleDialogs()).toHaveLength(1); + expect(visibleDialogs()[0].props.buttonText).toBeNull(); + expect(setupActions()).toHaveLength(attempts); + } + + await act(async () => { + store.dispatch({ type: 'TEST_APP_CREATED' }); + }); + expect(visibleDialogs()).toHaveLength(1); + expect(visibleDialogs()[0].props.buttonText).toBe('Next'); + expect(actions.filter((action) => action.type === 'UPDATE_FCM')).toEqual([ + { type: 'UPDATE_FCM', tokens: ['test-token'] }, + ]); + await act(async () => screen.update(view())); + expect(setupActions()).toHaveLength(3); +}); + +test('rapid Start presses and rerenders keep one pending token lookup and one setup attempt', async () => { + const token = deferred(); + mockGetToken.mockReturnValue(token.promise); + await mount(); + const pressStart = screen.root.findByProps({ testID: 'view_startNewTile' }).props.onPress; + await act(async () => { + pressStart(); + pressStart(); + }); + await act(async () => { + screen.update(view()); + pressStart(); + }); + expect(mockGetToken).toHaveBeenCalledTimes(1); + expect(setupActions()).toHaveLength(0); + expect(visibleDialogs()).toHaveLength(1); + await act(async () => token.resolve('delayed-token')); + expect(setupActions()).toEqual([ + { type: 'SETUP_KEEPER_APP', payload: { appName: '', fcmToken: 'delayed-token' } }, + ]); +}); + +test('token retrieval failure starts one setup attempt without a token', async () => { + mockGetToken.mockRejectedValue(new Error('offline token retrieval')); + await mount(); + await start(); + expect(setupActions()).toEqual([ + { type: 'SETUP_KEEPER_APP', payload: { appName: '', fcmToken: '' } }, + ]); + expect(visibleDialogs()).toHaveLength(1); + expect(visibleDialogs()[0].props.buttonText).toBeNull(); +}); + +test.each(['resolve', 'reject'])( + 'unmounting before token %s prevents a stale setup attempt', + async (completion) => { + const token = deferred(); + mockGetToken.mockReturnValue(token.promise); + await mount(); + await start(); + expect(mockGetToken).toHaveBeenCalledTimes(1); + expect(setupActions()).toHaveLength(0); + act(() => screen.unmount()); + screen = null; + await act(async () => { + if (completion === 'resolve') token.resolve('late-token'); + else token.reject(new Error('late offline failure')); + }); + expect(setupActions()).toHaveLength(0); + } +); + +test.each(['resolve', 'reject'])( + 'unmounting before the successful app token %s prevents a stale token update', + async (completion) => { + const token = deferred(); + mockGetToken.mockReturnValue(token.promise); + await mount({ created: true }); + expect(mockGetToken).toHaveBeenCalledTimes(1); + expect(actions.filter((action) => action.type === 'UPDATE_FCM')).toHaveLength(0); + act(() => screen.unmount()); + screen = null; + await act(async () => { + if (completion === 'resolve') token.resolve('late-success-token'); + else token.reject(new Error('late token refresh failure')); + }); + expect(actions.filter((action) => action.type === 'UPDATE_FCM')).toHaveLength(0); + expect(setupActions()).toHaveLength(0); + } +); + +test('a successfully created app remount does not automatically create another app', async () => { + await mount({ created: true }); + expect(visibleDialogs()).toHaveLength(0); + expect(setupActions()).toHaveLength(0); + await act(async () => { + screen.unmount(); + screen = renderer.create(view()); + }); + expect(visibleDialogs()).toHaveLength(0); + expect(setupActions()).toHaveLength(0); }); From ffb89f28ace014bc581c53895b16260f2c8f4dd8 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Mon, 19 Oct 2026 13:34:26 +0000 Subject: [PATCH 205/254] Enable hosted protected backup availability for 2.6.4 review --- .../protected-backup-integration.md | 4 ++-- release/r4-protected-backup-app.md | 12 +++++++++--- src/services/backup/automatic.ts | 12 ++++++------ src/services/backup/protected.ts | 12 +++++++----- .../automaticWriteRealmRelay.integration.cjs | 4 ++-- tests/recovery/protectedWriteRealmRelay.proof.cjs | 15 ++++++++++++--- 6 files changed, 38 insertions(+), 21 deletions(-) diff --git a/openspec/changes/fix-recovery-backup-freshness/protected-backup-integration.md b/openspec/changes/fix-recovery-backup-freshness/protected-backup-integration.md index 0e2aef3ca4..b23a274f19 100644 --- a/openspec/changes/fix-recovery-backup-freshness/protected-backup-integration.md +++ b/openspec/changes/fix-recovery-backup-freshness/protected-backup-integration.md @@ -14,7 +14,7 @@ All seven automatic operations and explicit full repair use protected v3 transpo Validate the raw complete projection and its checksum before identity adaptation. Decrypt every recovery record; verify strict protected wallet-map IDs, encrypted label IDs, vault scheme/archive/signer references and complete scoped maps, plus recovery metadata. Only fresh matching local evidence and final synchronous Redux admission can report Verified. The projection checksum is public consistency data, not ownership proof or a server signature. -Status reads do not enroll or write. Explicit opt-in or repair can enroll only within its original source/session/consent scope and approved hosted readiness. Initial absence, enrolled-empty, tombstone, authorization/decryption/protocol/network failure and previously-known namespace conflict stay distinct. +Status reads do not enroll or write. Compiled client availability is ON for the owner-directed store-review and intended 2.6.4 build; explicit opt-in or repair can enroll only within its original source/session/consent scope and unchanged authority proof. Backend readiness remains UNKNOWN; public release is HELD until actual readiness and separate owner release approval. Initial absence, enrolled-empty, tombstone, authorization/decryption/protocol/network failure and previously-known namespace conflict stay distinct. ## Delete, disable and recovery @@ -32,6 +32,6 @@ After validation, revoke target-account automatic-backup consent and expire old Historical v2 proof stays historical. Preserve the public fixture/runtime pins and their existing guard; the separately identified exact-private genuine lane must prove current authority/transport/production consumers against real encrypted Realm and transactional loopback Mongo. Unit transport mocks, V8 bundle execution and Hermes bytecode compilation have narrower meanings than that lane or native UI acceptance. -Hosted automatic transport, protected Delete and manual repair remain fenced until actual backend source/runtime/audience/provisioning/index/transaction/drain/ingress/read protection and compatible rollback readiness, review and separately approved enablement. Source tests do not establish deployment. Upgrading old writers requires actual per-platform availability and an approved denial interval; old binaries cannot be promised an upgrade prompt or truthful rejection UI. +Compiled client availability admits hosted automatic transport, protected Delete and manual repair for store review and the intended app build, with per-account consent and original authority checks unchanged. Actual backend source/runtime/audience/provisioning/index/transaction/drain/ingress/read protection and compatible rollback readiness remain UNKNOWN; app availability and source tests do not establish deployment. Public release remains HELD until actual backend readiness, current-source review and separate owner release approval. Upgrading old writers requires actual per-platform availability and an approved denial interval; old binaries cannot be promised an upgrade prompt or truthful rejection UI. Exact-new-SHA IOS and ANDROID builds/install/launch and critical recovery/backup acceptance remain required. HARDWARE stays explicit; only an exact-SHA physical-only owner decision can defer eligible unrun physical checks. No failed critical check, store/artifact/review/channel gate or release approval is waived. Parked C8/navigation work and independence from Reproducibility/Bitkey remain unchanged. diff --git a/release/r4-protected-backup-app.md b/release/r4-protected-backup-app.md index 30c2d38049..1f85adac75 100644 --- a/release/r4-protected-backup-app.md +++ b/release/r4-protected-backup-app.md @@ -2,6 +2,12 @@ State: **NEEDS_LOCAL; R4 remains BLOCKED.** Use the full app SHA in the newest **Codex local handoff** on [PR #7045](https://github.com/KeeperCommunity/bitcoin-keeper/pull/7045), verify it equals the live PR head, and preserve every earlier result under its original SHA. This document cannot name its own eventual commit. The current v3 instructions here supersede the historical public-v2 fixture commands in [the earlier handoff](r4-automatic-backup-local-handoff.md). +## Owner-directed store-review availability + +`AUTOMATIC_BACKUP_HOSTED_AVAILABLE` and `PROTECTED_BACKUP_HOSTED_AVAILABLE` enable the intended protected-backup paths in the app. These are app availability controls, not evidence of deployed backend readiness. Per-account consent remains default off and restored off; original authority, session, local-generation, CAS and complete-readback checks still apply. Failed or uncertain server responses cannot report Verified or confirmed Delete. + +The owner authorized production artifact preparation/signing and App Store/Play review submission, with public release held. Actual backend readiness remains unverified, and production deployment is not authorized by this app change. The accepted `be89c34473bb6aab253b98325f1fe746ca391c6f` owner-reduced native subset and exact-SHA physical deferral remain evidence for that head only. New-source verification should cover this admission change and final artifacts, without reopening deferred native campaigns. + ## Superseded recovery request correction The03e proof did not cover a newer recovery discovery arriving during the old restore's record decryption in the **same account session**. Genuine protected and explicit-older schedules reproduced local account/consent changes. The corrected worker carries an identity-only request checkpoint through decode, post-derivation and every guarded Realm write, alongside the existing original/selected account-session checks. Each recovery Redux dispatch checks current request/session. Only GET_APP_IMAGE uses `takeLatest`; it cancels called wallet/signer/upgrade generators, and cancelled workers cannot emit retry cleanup. This does not change generic watchers, account authority or parked navigation behavior. Source RED/GREEN, fixture precondition failures and actual watcher/child cancellation evidence belong to the newest exact-head receipt. @@ -12,7 +18,7 @@ CodeQL's03e analysis completed but its separate security aggregate **failed** (o Production entry points are the protected authority/transport, existing automatic-write producer/queue and repair/status paths, confirmed Delete worker, and typed Recovery Key discovery/restore. Authority is derived separately from the Recovery Key with the supported signing profile; it does not turn legacy decryption or an old account identifier into proof of ownership. Existing Keeper app identity, wallet/key derivation, encrypted payload format, provider/billing identity and local-first recovery records stay separate. Missing/stale account, session, consent, committed-generation, original-base, fingerprint, CAS or final-readback proof remains fail-closed. -Hosted rollout readiness stays **false**. Read-only status never enrolls or enables automatic backup. Delete removes **only the new protected copy**, preserves local wallets/Recovery Key and records its confirmed tombstone. Older encrypted copies may remain read-only. A lost/malformed/uncertain Delete reply is not success; preserve local data and check status rather than retrying uncertain ciphertext. Turning off automatic backup keeps copies. Re-enrollment after Delete is an explicit new-copy action, with no automatic legacy fallback. +Hosted app availability is enabled; backend readiness remains **unverified**. Read-only status never enrolls or enables automatic backup. Delete removes **only the new protected copy**, preserves local wallets/Recovery Key and records its confirmed tombstone. Older encrypted copies may remain read-only. A lost/malformed/uncertain Delete reply is not success; preserve local data and check status rather than retrying uncertain ciphertext. Turning off automatic backup keeps copies. Re-enrollment after Delete is an explicit new-copy action, with no automatic legacy fallback. Recovery checks the protected namespace first. Absent, empty, deleted and read/auth/network errors are distinct typed results; errors do not select legacy recovery. Only the deliberate **Recover an older copy** choice permits that read. Complete validation precedes recovery success, whose provenance must match the selected source and current account. Partial/unavailable recovery is not full success. Restore leaves automatic-backup consent **off**; enabling the protected copy is a separate explicit action. EN and reachable ES disclose the retained-copy scope. @@ -83,7 +89,7 @@ NODE This uses actual private v3 router/services plus the two exact legacy read declarations, inert provider traps and a distinct disposable database. No full backend/bootstrap/config/credentials are loaded. It binds an ephemeral loopback port; do not run the full matrix concurrently against its single-process fixture. The fixture close operation drops **only its owned disposable database**; keep a diagnosed failure process/database alive until evidence is retained. Container volumes and all existing databases remain protected. Nothing here authorizes a hosted call. -In a separate **uncommitted local QA environment file**, set `ENVIRONMENT=DEVELOPMENT` and `RELAY` to the printed exact `http://127.0.0.1:/` including trailing slash. Keep the existing reviewed local channel/SigningServer/test-only values; never print/copy hosted configuration. `src/utils/service-utilities/config.ts` consumes this field. `src/services/backup/protected.ts` and `automatic.ts` admit only explicit-port development loopback without changing their hosted false constants. For Android use `adb -s reverse tcp: tcp:`; iOS simulator uses host loopback. Record the exact QA env-file hash/path and actual ENVFILE/native configuration selection, without exporting values. +In a separate **uncommitted local QA environment file**, set `ENVIRONMENT=DEVELOPMENT` and `RELAY` to the printed exact `http://127.0.0.1:/` including trailing slash. Keep the existing reviewed local channel/SigningServer/test-only values; never print/copy hosted configuration. `src/utils/service-utilities/config.ts` consumes this field. The explicit-port development loopback exception remains available for disposable verification independently of hosted app availability. Other protected targets require HTTPS. For Android use `adb -s reverse tcp: tcp:`; iOS simulator uses host loopback. Record the exact QA env-file hash/path and actual ENVFILE/native configuration selection, without exporting values. **Boundary:** this fixture serves backups, not onboarding/channel/provider APIs. The app has one RELAY origin and no per-v3 override. Prepare a disposable consent-OFF account through the already reviewed local onboarding stack, then switch that QA configuration to this backup-only server for the exact-source native build/test; preserve the account's original identity and local records. It cannot establish full fresh onboarding against a composed v3 service. If an end-to-end onboarding gateway is needed, a fixed reviewed local-only forwarding composition remains required: do not silently invent it, start a private full service/provider graph, or claim this standalone launcher implements it. Clean protected restore is exercised from the app's recovery path, with real native success still to be established. Explicit older-copy native recovery additionally requires deliberately seeded disposable legacy ciphertext; do not point at retained customer copies. The actual encrypted fixture seeds/readback and failure injection are in `protectedWriteRealmRelay.proof.cjs` and `.fixture.cjs`; native hooks do not inherit those Node proof passes. @@ -102,4 +108,4 @@ The latest supplied Mac report is **16 October 15:30:40 UTC**, **50.9437 GiB fre Before any heavy checkout/install/fixture/native resumption and at closeout, follow available `AGENTS.override.md`, `keeper-local-memory.md` and `dev-environment/release-runbook.md`; run `python3 dev-environment/release-storage.py assess --context ` with the fresh complete remaining-peak/retry budget. Receipts expire after 30 minutes. Resolve the capacity/fit/source-binding gaps before heavy work; no timer/background cleanup. -Backend promotion/deployment/index/transaction/CAS/fleet/old-writer and ingress/protection evidence, hosted app enablement at a separately reviewed new SHA, exact-head local acceptance, store/channel/version/artifact approvals and physical follow-up remain separate open gates. No merge, production deployment/index/data/admin write, release signing, production AAB/split construction, upload/submission/publication, provider call or real transaction is authorized here. Keep R4 independent of Reproducibility and Bitkey Wallet. +Backend promotion/deployment/index/transaction/CAS/writer and ingress/protection evidence, exact-head local acceptance, store/channel/version/artifact approvals and physical follow-up remain separate open gates. The owner separately authorized Local production build/signing and store review submission, with public release held. This source change does not authorize merge, production deployment/index/data/admin write, public release/APK publication, provider calls or real transactions. Keep R4 independent of Reproducibility and Bitkey Wallet. diff --git a/src/services/backup/automatic.ts b/src/services/backup/automatic.ts index d2628bcad7..2f7028bded 100644 --- a/src/services/backup/automatic.ts +++ b/src/services/backup/automatic.ts @@ -70,11 +70,11 @@ const managedCommits = new WeakSet(); const results = new WeakMap(); const generations = new Map(); let sequence = 0; -// The genuine Realm/transactional Relay matrix permits development loopback -// validation. Hosted writes remain fenced until the separate fleet rollout, -// old-writer drainage and operator evidence have been reviewed and approved. +// Source proof and app availability are separate from per-account consent. +// Hosted availability allows protected-write attempts after explicit opt-in; +// it does not attest deployed backend readiness or authorize public release. export const AUTOMATIC_BACKUP_SOURCE_READY = true; -export const AUTOMATIC_BACKUP_RELAY_ROLLOUT_READY = false; +export const AUTOMATIC_BACKUP_HOSTED_AVAILABLE = true; let commitVerification: (appId: string, deleted: boolean, consentEpoch: number) => boolean = () => false; export const setAutomaticBackupVerificationCommit = (commit: typeof commitVerification) => { @@ -583,9 +583,9 @@ export function enqueueAutomaticBackupOperation( const nativeLoopback = config.ENVIRONMENT === 'DEVELOPMENT' && config.isDevMode() && /^http:\/\/(127\.0\.0\.1|localhost):\d+\/$/.test(config.RELAY); if ((!AUTOMATIC_BACKUP_SOURCE_READY && !loopbackFixture) || - (!AUTOMATIC_BACKUP_RELAY_ROLLOUT_READY && !loopbackFixture && + (!AUTOMATIC_BACKUP_HOSTED_AVAILABLE && !loopbackFixture && !(AUTOMATIC_BACKUP_SOURCE_READY && nativeLoopback))) - throw new Error('Automatic backup source or rollout is not enabled'); + throw new Error('Automatic backup source or availability is not enabled'); assertProtectedBackupWriteReady(); assertOwned(); const baseline = await snapshot(appId, proof.after.app, deadline, assertOwned); diff --git a/src/services/backup/protected.ts b/src/services/backup/protected.ts index aa6f118123..ea420c0935 100644 --- a/src/services/backup/protected.ts +++ b/src/services/backup/protected.ts @@ -27,17 +27,19 @@ type Operation = 'updateAppImage' | 'migrateXfps' | 'deleteAppImageEntity' | 'de type Account = { id: string; primarySeed: string }; type Path = '/backup/v3/enroll' | '/backup/v3/snapshot' | '/backup/v3/mutate'; -// This is a distinct gate from local consent. Explicit repair is not permission -// to write to a hosted backend whose readiness has not been established. -export const PROTECTED_BACKUP_HOSTED_READY = false; +// App availability is distinct from local consent and backend readiness. +// This permits protected-write attempts; it does not certify the deployed +// backend or authorize public release. The original authority/CAS guards apply. +export const PROTECTED_BACKUP_HOSTED_AVAILABLE = true; function loopback() { return /^http:\/\/(127\.0\.0\.1|localhost):\d+\/$/.test(config.RELAY) && ((typeof process !== 'undefined' && !!process.env?.KEEPER_PROTECTED_RELAY_CHECKOUT) || (config.ENVIRONMENT === 'DEVELOPMENT' && config.isDevMode())); } +/** Checks compiled client availability only; not observed server readiness. */ export function assertProtectedBackupWriteReady() { - if (!PROTECTED_BACKUP_HOSTED_READY && !loopback()) - throw new Error('Protected backup is not enabled on this server'); + if (!PROTECTED_BACKUP_HOSTED_AVAILABLE && !loopback()) + throw new Error('Protected backup is not enabled in this app'); } function endpoint(path: Path) { const base = new URL(config.RELAY); diff --git a/tests/recovery/automaticWriteRealmRelay.integration.cjs b/tests/recovery/automaticWriteRealmRelay.integration.cjs index 7e97003157..04c2d8b5e8 100644 --- a/tests/recovery/automaticWriteRealmRelay.integration.cjs +++ b/tests/recovery/automaticWriteRealmRelay.integration.cjs @@ -804,7 +804,7 @@ test('fixture authorization never permits a hosted Relay target', async (t) => { }); for (const mode of ['development-loopback', 'production-environment', 'development-disabled', 'missing-explicit-port']) { - test(`without the Node fixture override ${mode} obeys the native source and rollout gates`, async (t) => { + test(`without the Node fixture override ${mode} obeys the native source and availability gates`, async (t) => { const f = await setup(t, 'updateAppImage'); const request = await committedRequest(f, 'updateAppImage'); if (mode === 'production-environment') f.config.ENVIRONMENT = 'PRODUCTION'; @@ -824,7 +824,7 @@ for (const mode of ['development-loopback', 'production-environment', 'developme assert.equal(attemptedWrites().length, 0); assertPending(f); } - assert.equal(f.automatic.AUTOMATIC_BACKUP_RELAY_ROLLOUT_READY, false, 'Disposable source proof never enables the hosted Relay fleet'); + assert.equal(f.automatic.AUTOMATIC_BACKUP_HOSTED_AVAILABLE, true, 'App availability does not certify hosted backend readiness'); }); } diff --git a/tests/recovery/protectedWriteRealmRelay.proof.cjs b/tests/recovery/protectedWriteRealmRelay.proof.cjs index 64acf7a6df..3a4dfdbace 100644 --- a/tests/recovery/protectedWriteRealmRelay.proof.cjs +++ b/tests/recovery/protectedWriteRealmRelay.proof.cjs @@ -384,14 +384,23 @@ test('a malformed protected discovery reports read-error and never falls back to assert.equal(server.trace.some(event => event.legacyRead), false); assert.equal(mutations().length, 0); }); -test('hosted enablement remains false for explicit enrollment and repair despite valid local authority', async t => { +test('hosted availability grants no consent and preserves original authority and HTTPS requirements', async t => { await server.reset(); const f = await client(t); const previous = f.config.RELAY; f.config.RELAY = 'https://fixture.invalid/'; try { - await assert.rejects(f.protected.enrollProtectedBackup(mnemonic), /not enabled/); - await assert.rejects(f.protected.protectedBackupMutation(mnemonic, {}, 'repairAppBackup', {}), /not enabled/); + assert.equal(f.automatic.AUTOMATIC_BACKUP_HOSTED_AVAILABLE, true); + assert.equal(f.protected.PROTECTED_BACKUP_HOSTED_AVAILABLE, true); + assert.doesNotThrow(() => f.protected.assertProtectedBackupWriteReady()); + assert.equal(f.store.getState().bhr.automaticCloudBackupByAppId[appId], undefined); + // An unavailable original CAS tuple still rejects before signing or send. + await assert.rejects(f.protected.protectedBackupMutation(mnemonic, {}, 'repairAppBackup', {}), + /Invalid protected backup response/); + // No test contacts a hosted target: HTTP is rejected before any request. + f.config.RELAY = 'http://fixture.invalid/'; + await assert.rejects(f.protected.enrollProtectedBackup(mnemonic), /requires HTTPS/); } finally { f.config.RELAY = previous; } assert.equal(server.trace.length, 0); + assert.equal(f.store.getState().bhr.automaticCloudBackupByAppId[appId], undefined); }); test('a real committed mnemonic change invalidates queued authority before any old ciphertext is sent', async t => { From 1d8a86a7462d935cbd0e28b857934b5718c152fc Mon Sep 17 00:00:00 2001 From: Anant Date: Wed, 21 Oct 2026 14:30:20 +0530 Subject: [PATCH 206/254] release: refresh 2.6.4 store baselines --- release/version.json | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/release/version.json b/release/version.json index eeaf42ae08..2ee660a4d4 100644 --- a/release/version.json +++ b/release/version.json @@ -5,17 +5,17 @@ "baselines": { "android": { "applicationId": "io.hexawallet.bitcoinkeeper", - "verifiedAt": "2026-10-12T14:27:41Z", + "verifiedAt": "2026-10-21T08:57:54Z", "highestUploadedBuild": 626, "highestMarketingVersion": "2.6.3", "productionRelease": "626 (2.6.3)" }, "ios": { "applicationId": "io.hexawallet.keeper", - "verifiedAt": "2026-10-06T14:12:22Z", - "highestUploadedBuild": 618, - "highestMarketingVersion": "2.6.2", - "productionRelease": "618 (2.6.2)" + "verifiedAt": "2026-10-21T08:26:48Z", + "highestUploadedBuild": 619, + "highestMarketingVersion": "2.6.3", + "productionRelease": "619 (2.6.3)" } } } From 98c4e2e78483ec03fac7ac72d86419e6da42fc98 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Wed, 21 Oct 2026 10:38:29 +0000 Subject: [PATCH 207/254] fix(r5): decouple splash readiness and guard startup navigation --- docs/r5/STARTUP-INVESTIGATION.md | 129 +++++++++++ docs/r5/WORKSTREAM.md | 41 ++++ flows/login.yaml | 50 +++-- src/context/Localization/language/en.json | 1 + src/context/Localization/language/es.json | 1 + .../NewKeeperAppScreen/NewKeeperAppScreen.tsx | 19 +- src/screens/Splash/SplashScreen.tsx | 80 +++++-- src/storage/secure-store.ts | 5 +- .../components/TransactionLocalTime.test.tsx | 121 +++++++++++ tests/screens/NewKeeperApp.test.tsx | 58 ++++- tests/screens/SplashScreen.test.tsx | 202 ++++++++++++++++++ tests/services/startupCredentials.test.ts | 45 ++++ 12 files changed, 711 insertions(+), 41 deletions(-) create mode 100644 docs/r5/STARTUP-INVESTIGATION.md create mode 100644 tests/components/TransactionLocalTime.test.tsx create mode 100644 tests/screens/SplashScreen.test.tsx create mode 100644 tests/services/startupCredentials.test.ts diff --git a/docs/r5/STARTUP-INVESTIGATION.md b/docs/r5/STARTUP-INVESTIGATION.md new file mode 100644 index 0000000000..692a3a0b27 --- /dev/null +++ b/docs/r5/STARTUP-INVESTIGATION.md @@ -0,0 +1,129 @@ +# R5 startup and bounded investigations — 21 October 2026 + +Development evidence only. R4 remains an unmerged draft; no native acceptance, +release-readiness, issue closure or performance improvement is claimed here. + +## Newer work checked before editing + +Continued from PR #7058 head `625435ac26af0304670c993a639626b1117699ba`, preserving +its deletion/privacy, setup lifecycle and CI/lock work. `AGENTS.md` is unchanged. +R4 #7045 was an open draft at `1d8a86a7462d935cbd0e28b857934b5718c152fc`. +Its Splash, NewKeeperApp and secure-store modules are identical to the original +sprint versions. This permits independent source changes without importing R4. +R4's changed Login, Home/navigation, transaction views and backup/backend paths +remain untouched. Reconciliation is still required when R4 is released. + +Other open work includes native-validation #7060, cloud-release #7062, bootstrap +#7063, lock-selector #7064, PIN fixes #7052/#7053, reproducibility #7043 and the +Assisted Multisig preview #7041. No commits were imported from these branches; +no branch/ref, workflow, native configuration or dependency input in them was +changed. No optional Linux setup or Mac connection was awaited or started. + +## #7046: confirmed JavaScript startup defects + +1. The credential read formerly started only after the 3,000 ms animation + callback. Tests failed 9/10 against the previous Splash implementation. + Startup now starts that read after its existing default-network/theme/Tor + initialization, independently of animation completion. It still waits for + credentials before choosing Login/CreatePin and discards stale completions. + PersistGate wraps the app before Navigator, so Redux rehydration already + precedes Splash; this change does not remove that hydration gate. +2. `hasPin()` caught native Keychain/JSON failures and returned false. Two real + secure-store boundary tests reproduced this conflating an unreadable store + with an empty store. Startup now explicitly requests error propagation with + `hasPin({ throwOnError: true })`; default behavior is preserved for legacy + callers. A generic Retry dialog contains no underlying error or credential + material. Repeated Retry callbacks cannot start parallel reads. No Keychain + write, account-selection rule or authentication-policy change was made. +3. Three setup navigation tests reproduced unmounts leaving the 3-second + recreated-app and 500 ms Next timers active, and rapid Next taps scheduling + multiple replacements. Cleanup and a one-use pending timer fix those cases. + A fourth test reproduced a retained Next callback scheduling after unmount; + its screen-lifetime guard now rejects it. The delays themselves are unchanged. +4. `flows/login.yaml` now checks Proceed is visible but disabled at zero/three + digits, enabled at four, and disabled after the inline incorrect-PIN error. + It no longer expects a Retry modal on the first incorrect PIN. The existing + disposable fixture PIN remains 1111; the flow accepts old-sprint Next or R4's + direct advance and then requires Home. It is a native test specification, + YAML-parsed here but NOT executed with Maestro. Shared PIN code was not edited. + +The screen tests replace native Keychain, navigation, animation and UI boundaries; +they are not cold-start, warm-start, resume, deep-link or native persistence proof. +The credential tests execute the actual secure-store function with synthetic +Keychain results and synthetic account identifiers, without writing any storage. + +## #6813: first-wallet timing + +Source trace: Start/Retry waits for Firebase token retrieval (rejection falls back +to tokenless setup), then dispatches setup. The setup worker can derive seed/BIP85 +material, call Relay registration, persist the app/account, create the default +wallet and Recovery Key signer, then set appCreated. R4 changes some downstream +wallet/backup behavior. No stage has been established as the runtime bottleneck. +Do not introduce a notification timeout, weaken wallet setup or change worker +idempotency from this source trace. + +Native measurement recipe, on disposable state after R4 reconciliation: + +- Record exact source/build/APK hash, OS/API/ABI, screen/display/font settings, + network/Tor state, cold/warm/resume mode and free disk. Reuse the same build + mode/device for before/after comparison. +- Record raw monotonic durations for launch to usable PIN, PIN to usable Home, + and Start to completed first wallet. Separate Firebase, worker dispatch, + registration response and wallet persistence stages using test instrumentation + without logging credentials, seeds, account identifiers or private payloads. +- At least five samples per cold/warm condition; retain every sample and report + median/range rather than attributing the configured animation duration to actual + wall-clock performance. Test offline/retry/relaunch and count persisted wallets + before/after; JavaScript appCreated simulation cannot prove no duplicates. + +No measured native durations or performance percentage is available in this Cloud +workspace. Java/Jest wall times must not be substituted for app performance. + +## #7055: More → Wallets exits to launcher + +Bounded read-only trace: MenuFooter calls Home's option-change callback; Home +selects the Wallets content. Neither path invokes BackHandler.exitApp, navigation +back/reset, or an Android activity-finish call for this tap. R4 adds tab test IDs +and changes Home/Ask navigation, so it remains the required baseline. Native +MainActivity/AppState/task transitions and the report's multiuser history are not +modeled by these JavaScript tests. No root cause is confirmed and no navigation +behavior was patched. This does not dismiss the preserved Android 17 failure. + +Next reproduction should use an ordinary single-user install plus separate fresh +state on supported Android versions, then the reported backup opt-in/Delete +sequence. Verify Wallets/Keys/Ask/More, cold launch and account changes; preserve +wallets, labels, task/window state, logcat and absence of unintended backup reupload. +Use existing disposable fixtures only; retain the failed user/artifacts. Do not +reopen owner-parked #7056/#7057 or infer an R4 test waiver. + +## #6991: local transaction times + +Bitcoin transaction conversion stores UTC strings from epoch seconds; list rows +and transaction details call local Moment formatting. R4 changes the row module +but retains that formatter. The new test renders the actual current production +TransactionElement with those UTC strings, using synthetic confirmed, pending +and cached records, and checks the transaction object is unchanged. + +Four separate Node/Jest processes tested UTC, Asia/Kolkata, America/New_York and +Europe/London: 18 assertions each, 72 total. Vectors cover fractional offsets, +previous-day rollover, spring DST changes and New York's repeated autumn hour. +All passed. Existing display punctuation/hour-format conventions were preserved. +This establishes local formatting in Node for the tested rows, not Android/Hermes, +actual history/details, device-timezone changes while running, or cached-source +integrity. No timezone fix was confirmed, so no timestamp/formatter code changed. +Native reproduction remains open and bounded to the approved scope. + +## Execution limits and next native route + +Cloud has Java and approximately 24 GiB free at preflight, but no adb, emulator +or Maestro on PATH. No Android/iOS build, install, native UI or physical-device +check ran. The hosted-native PRs are separate unmerged infrastructure work, not a +reason to block this source/test work or to import their implementation here. +Use the approved Cloud native route when available; use a Mac only for remaining +platform/device checks it actually needs. Do not interrupt R4's Mac work. + +For any later native handoff: verify the exact R5 head after R4 reconciliation; +check disk (approximately 6 GiB planning minimum, more if required), one native +build at a time, reuse compatible caches and preserve worktrees, AVD users, +simulators, wallet data, artifacts and evidence. No deletion/cleanup without +approval. IOS, ANDROID and HARDWARE acceptance remain outstanding. diff --git a/docs/r5/WORKSTREAM.md b/docs/r5/WORKSTREAM.md index df76dafa0a..f6362be7da 100644 --- a/docs/r5/WORKSTREAM.md +++ b/docs/r5/WORKSTREAM.md @@ -208,3 +208,44 @@ iOS/Android, physical-device, startup/first-wallet performance, real-backend or retained-data continuity result follows from these JavaScript tests. Finish the R4-baseline reconciliation before the affected native matrix. No Mac access, private-data transfer, merge, signing or release occurred. + +## Cloud continuation — 21 October 2026 + +Continued from the latest #7058 head `625435ac26af0304670c993a639626b1117699ba` +after checking current guidance, the approved sheet rows, R4 and overlapping PRs. +The [startup investigation](STARTUP-INVESTIGATION.md) records the confirmed fixes, +source traces and limits. No optional Linux setup or Mac session was required. + +Independent implementation now includes starting the credential read without the +fixed splash-animation gate, explicit error propagation for startup-only Keychain +reads, one generic Retry action, stale-read protection, and setup navigation-timer +cleanup/repeat protection. The legacy login flow's visibility/disabled-state and +first-error assumptions are corrected as an unexecuted native test specification. +No R4 code/ref, shared PIN, navigation, wallet worker, backup/backend, native +configuration, dependency, CI or version/counter input changed in this follow-up. + +New regression controls before fixes: Splash 9 failures/10; actual secure-store +boundary 2 failures/5; setup navigation 3 new failures/12, then a retained-callback +failure/13. These are executed JavaScript assertion failures, not native results. +All final focused cases pass. The expanded offline run passes 19 suites/218 tests, +with the same three live-Electrum integration suites excluded. Four process-level +timezone runs pass 18 cases each (72 total); no Android timezone defect or +navigation root cause is established. Neither received a speculative patch. + +Focused ESLint on Splash, NewKeeperApp and secure-store still FAILS with 26 errors +and 11 warnings, versus 28/11 on the previous R5 head with the same dependencies; +no added findings after position normalization. Whole TypeScript still FAILS: +1,018 diagnostics versus 1,019 on previous head625435 with these same installed +dependencies, zero added/one removed after normalizing positions and linked paths. +These counts differ from the previous worker's 1,013 and do not supersede its +separately scoped evidence. Gate contracts 11/11, test formatting, YAML/JSON and +diff checks PASS. A transient new noImplicitReturns diagnostic in timer cleanup +was corrected before the final comparison. No clean whole-lint/typecheck claim. + +R5 remains two substantial features and within its 13k–18k estimate: a rough +changed-source/test character count divided by four is about 15k tokens. This is +an estimate, not tokenizer usage. Stop/propose a split before approximately 20k +or a third feature. Native startup/first-wallet timings, offline wallet-worker +idempotency/persistence, real Realm/authentication, #7055/#6991 native reproduction, +PIN/biometrics and R4-shipped copy reconciliation remain open. Reconcile against +R4's eventual released baseline and rerun affected checks before native acceptance. diff --git a/flows/login.yaml b/flows/login.yaml index 603b55dd51..858c1525d3 100644 --- a/flows/login.yaml +++ b/flows/login.yaml @@ -1,32 +1,56 @@ +# Disposable English-language fixture only: PIN 1111, biometric login disabled, +# no prior failed attempts, app already created. Never run against a real wallet. appId: io.hexawallet.keeper --- - extendedWaitUntil: visible: 'Enter your passcode' timeout: 20000 -- assertNotVisible: 'Proceed' +- assertVisible: + id: 'btn_primaryText' + text: 'Proceed' + enabled: false - tapOn: '1' - tapOn: '2' - tapOn: '3' +- assertVisible: + id: 'btn_primaryText' + text: 'Proceed' + enabled: false - tapOn: '7' -- assertVisible: 'Proceed' +- assertVisible: + id: 'btn_primaryText' + text: 'Proceed' + enabled: true - tapOn: 'Proceed' -- waitForAnimationToEnd: - timeout: 5000 -- assertVisible: 'Incorrect password' -- assertNotVisible: 'Proceed' -- assertVisible: 'Retry' -- tapOn: 'Retry' +- extendedWaitUntil: + visible: 'Incorrect Passcode, Try Again!' + timeout: 15000 +- assertVisible: + id: 'btn_primaryText' + text: 'Proceed' + enabled: false +# A single incorrect PIN shows an inline error, not a Retry modal. - tapOn: '1' - tapOn: '1' - tapOn: '1' - tapOn: '1' -- assertVisible: 'Proceed' +- assertVisible: + id: 'btn_primaryText' + text: 'Proceed' + enabled: true - tapOn: 'Proceed' - waitForAnimationToEnd: timeout: 5000 +# The older sprint login has a security-tip Next action; R4 advances directly. - extendedWaitUntil: - visible: 'Next' + visible: 'Next|Wallets' + timeout: 30000 +- runFlow: + when: + visible: 'Next' + commands: + - tapOn: 'Next' +- extendedWaitUntil: + visible: + id: 'text_home_current_plan' timeout: 30000 -- tapOn: 'Next' -- waitForAnimationToEnd: - timeout: 2000 diff --git a/src/context/Localization/language/en.json b/src/context/Localization/language/en.json index 5e40129d67..c2856c05ac 100644 --- a/src/context/Localization/language/en.json +++ b/src/context/Localization/language/en.json @@ -185,6 +185,7 @@ "help": "Help" }, "login": { + "startupCredentialError": "Unable to check your passcode. Please try again.", "welcomeback": "Welcome Back", "welcome": "Welcome", "welcomeToBitcoinKeeper": "Welcome to Bitcoin Keeper", diff --git a/src/context/Localization/language/es.json b/src/context/Localization/language/es.json index c8554624e5..3298a16c16 100644 --- a/src/context/Localization/language/es.json +++ b/src/context/Localization/language/es.json @@ -185,6 +185,7 @@ "help": "Help" }, "login": { + "startupCredentialError": "Unable to check your passcode. Please try again.", "welcomeback": "Welcome Back", "welcome": "Welcome", "welcomeToBitcoinKeeper": "Welcome to Bitcoin Keeper", diff --git a/src/screens/NewKeeperAppScreen/NewKeeperAppScreen.tsx b/src/screens/NewKeeperAppScreen/NewKeeperAppScreen.tsx index 051fcc8762..9b36180e8c 100644 --- a/src/screens/NewKeeperAppScreen/NewKeeperAppScreen.tsx +++ b/src/screens/NewKeeperAppScreen/NewKeeperAppScreen.tsx @@ -2,7 +2,7 @@ /* eslint-disable react/no-unstable-nested-components */ import { ActivityIndicator, StyleSheet, BackHandler , TouchableOpacity} from 'react-native'; import Text from 'src/components/KeeperText'; -import React, { useContext, useEffect, useState } from 'react'; +import React, { useContext, useEffect, useRef, useState } from 'react'; import { hp, windowWidth, wp } from 'src/constants/responsive'; import { useAppDispatch, useAppSelector } from 'src/store/hooks'; import AppIcon from 'src/assets/images/new-app-icon.svg'; @@ -114,6 +114,8 @@ function NewKeeperApp({ navigation }: { navigation }) { const { translations } = useContext(LocalizationContext); const { login, common, error: errorText, home } = translations; const isFocused = useIsFocused(); + const nextNavigation = useRef | null>(null); + const navigationActive = useRef(true); const isMultiAccountFlow = !!useAppSelector((state) => state.account).allAccounts.length; useEffect(() => { @@ -149,12 +151,22 @@ function NewKeeperApp({ navigation }: { navigation }) { useEffect(() => { if (appRecreated) { - setTimeout(() => { + const timer = setTimeout(() => { navigation.replace('App', { screen: 'Home' }); }, 3000); + return () => clearTimeout(timer); } + return undefined; }, [appRecreated]); + useEffect(() => { + navigationActive.current = true; + return () => { + navigationActive.current = false; + if (nextNavigation.current !== null) clearTimeout(nextNavigation.current); + }; + }, []); + useEffect(() => { const backAction = () => true; const backHandler = BackHandler.addEventListener('hardwareBackPress', backAction); @@ -339,8 +351,9 @@ function NewKeeperApp({ navigation }: { navigation }) { subTitleColor={`${colorMode}.modalSubtitleBlack`} buttonText={appCreated ? common.next : null} buttonCallback={() => { + if (!navigationActive.current || nextNavigation.current !== null) return; setModalVisible(false); - setTimeout(() => { + nextNavigation.current = setTimeout(() => { navigation.replace('App', { screen: 'Home' }); }, 500); }} diff --git a/src/screens/Splash/SplashScreen.tsx b/src/screens/Splash/SplashScreen.tsx index bc418b3e94..eb64ef6bda 100644 --- a/src/screens/Splash/SplashScreen.tsx +++ b/src/screens/Splash/SplashScreen.tsx @@ -1,4 +1,4 @@ -import React, { useEffect } from 'react'; +import React, { useContext, useEffect, useRef, useState } from 'react'; import { StyleSheet } from 'react-native'; import { useColorMode } from '@gluestack-ui/themed-native-base'; import RestClient from 'src/services/rest/RestClient'; @@ -6,7 +6,7 @@ import { useAppSelector } from 'src/store/hooks'; import * as SecureStore from 'src/storage/secure-store'; import Animated, { interpolate, - runOnJS, + cancelAnimation, useAnimatedStyle, useSharedValue, withSpring, @@ -19,19 +19,20 @@ import { NetworkType } from 'src/services/wallets/enums'; import { changeBitcoinNetwork } from 'src/store/sagaActions/settings'; import { setDefaultWalletCreated } from 'src/store/reducers/storage'; import ThemedSvg from 'src/components/ThemedSvg.tsx/ThemedSvg'; +import KeeperModal from 'src/components/KeeperModal'; +import { LocalizationContext } from 'src/context/Localization/LocContext'; function SplashScreen({ navigation }) { const { torEnbled, themeMode, bitcoinNetworkType } = useAppSelector((state) => state.settings); const dispatch = useDispatch(); const { toggleColorMode, colorMode } = useColorMode(); - const animate = () => { - progress.value = withTiming(4, { duration: 3000 }, (finished) => { - if (finished) { - runOnJS(navigateToApp)(); - } - }); - }; + const { translations } = useContext(LocalizationContext); + const [startupError, setStartupError] = useState(false); + const [attempt, setAttempt] = useState(0); + const readPending = useRef(true); + const mounted = useRef(false); + const progress = useSharedValue(0); useEffect(() => { if (!bitcoinNetworkType) { @@ -48,27 +49,52 @@ function SplashScreen({ navigation }) { }, []); useEffect(() => { - animate(); + mounted.current = true; + // The animation is decorative. Credential availability, rather than its + // duration, determines when startup can advance. + progress.value = withTiming(4, { duration: 3000 }); if (themeMode === 'DARK' || themeMode === 'PRIVATE') { toggleColorMode(); } RestClient.setUseTor(torEnbled); + return () => { + mounted.current = false; + cancelAnimation(progress); + }; }, []); - const navigateToApp = async () => { - try { - const hasCreds = await SecureStore.hasPin(); - if (hasCreds) { - navigation.replace('Login', { relogin: false }); - } else { - navigation.replace('CreatePin'); + useEffect(() => { + let current = true; + readPending.current = true; + const navigateToApp = async () => { + try { + const hasCreds = await SecureStore.hasPin({ throwOnError: true }); + if (!current) return; + if (hasCreds) { + navigation.replace('Login', { relogin: false }); + } else { + navigation.replace('CreatePin'); + } + } catch { + if (current) { + readPending.current = false; + setStartupError(true); + } } - } catch (err) { - console.log(err); - } + }; + navigateToApp(); + return () => { + current = false; + }; + }, [attempt, navigation]); + + const retryStartup = () => { + if (!mounted.current || readPending.current) return; + readPending.current = true; + setStartupError(false); + setAttempt((previous) => previous + 1); }; - const progress = useSharedValue(0); const inputRange = [0, 1, 2, 3, 4]; const heightRange = [0, windowHeight * 2, windowHeight * 2, windowHeight, windowHeight]; const widthRange = [0, windowHeight * 2, windowHeight * 2, windowHeight, windowWidth]; @@ -115,6 +141,18 @@ function SplashScreen({ navigation }) { + {}} + showCloseIcon={false} + dismissible={false} + title={translations.common.somethingWrong} + subTitle={translations.login.startupCredentialError} + buttonText={translations.common.retry} + buttonCallback={retryStartup} + modalBackground={`${colorMode}.modalWhiteBackground`} + textColor={`${colorMode}.textGreen`} + /> ); } diff --git a/src/storage/secure-store.ts b/src/storage/secure-store.ts index bf2b639035..88abadce26 100644 --- a/src/storage/secure-store.ts +++ b/src/storage/secure-store.ts @@ -65,7 +65,9 @@ export const fetch = async (hash_current: string) => { } }; -export const hasPin = async () => { +// Startup must distinguish an unreadable Keychain from a successfully empty +// one. Preserve legacy behavior unless the caller explicitly requests errors. +export const hasPin = async ({ throwOnError = false }: { throwOnError?: boolean } = {}) => { try { let allAccounts = reduxStore.getState().account.allAccounts; // if allAccounts is empty | upgraded app with old passcode configuration @@ -89,6 +91,7 @@ export const hasPin = async () => { } return false; } catch (err) { + if (throwOnError) throw err; return false; } }; diff --git a/tests/components/TransactionLocalTime.test.tsx b/tests/components/TransactionLocalTime.test.tsx new file mode 100644 index 0000000000..f752c2451d --- /dev/null +++ b/tests/components/TransactionLocalTime.test.tsx @@ -0,0 +1,121 @@ +import React from 'react'; +import renderer, { act } from 'react-test-renderer'; +import { EntityKind } from '../../src/services/wallets/enums'; + +jest.mock('@gluestack-ui/themed-native-base', () => ({ + Box: 'Box', + useColorMode: () => ({ colorMode: 'light' }), +})); +jest.mock('src/constants/responsive', () => ({ hp: (v) => v, wp: (v) => v })); +jest.mock('src/components/KeeperText', () => 'Text'); +jest.mock('src/screens/Home/components/CurrencyInfo', () => 'CurrencyInfo'); +jest.mock('src/hooks/useLabelsNew', () => ({ __esModule: true, default: () => ({ labels: [] }) })); +jest.mock('src/components/ThemedColor/ThemedColor', () => ({ + __esModule: true, + default: () => '#000', +})); +[ + 'icon_sent_red', + 'icon_recieved_red', + 'icon_recieved_dark', + 'transaction_pending', + 'cache_icon', + 'icon_arrow_grey', + 'icon_arrow_white', +].forEach((name) => + jest.doMock(require.resolve(`../../src/assets/images/${name}.svg`), () => () => null) +); +const TransactionElement = require('../../src/components/TransactionElement').default; + +// Run this same production-component test in separate processes with TZ set. +// Changing TZ inside a Jest worker does not reliably change Date's timezone. +const hostZone = process.env.TZ || Intl.DateTimeFormat().resolvedOptions().timeZone; +const zone = { 'Etc/UTC': 'UTC', 'Asia/Calcutta': 'Asia/Kolkata' }[hostZone] || hostZone; +const cases = { + UTC: [ + ['2026-03-08T06:30:00Z', '08 Mar 26 . 06:30 AM'], + ['2026-03-08T07:30:00Z', '08 Mar 26 . 07:30 AM'], + ['2026-03-29T00:30:00Z', '29 Mar 26 . 00:30 AM'], + ['2026-03-29T01:30:00Z', '29 Mar 26 . 01:30 AM'], + ['2026-11-01T05:30:00Z', '01 Nov 26 . 05:30 AM'], + ['2026-11-01T06:30:00Z', '01 Nov 26 . 06:30 AM'], + ], + 'Asia/Kolkata': [ + ['2026-03-08T06:30:00Z', '08 Mar 26 . 12:00 PM'], + ['2026-03-08T07:30:00Z', '08 Mar 26 . 13:00 PM'], + ['2026-03-29T00:30:00Z', '29 Mar 26 . 06:00 AM'], + ['2026-03-29T01:30:00Z', '29 Mar 26 . 07:00 AM'], + ['2026-11-01T05:30:00Z', '01 Nov 26 . 11:00 AM'], + ['2026-11-01T06:30:00Z', '01 Nov 26 . 12:00 PM'], + ], + 'America/New_York': [ + ['2026-03-08T06:30:00Z', '08 Mar 26 . 01:30 AM'], + ['2026-03-08T07:30:00Z', '08 Mar 26 . 03:30 AM'], + ['2026-03-29T00:30:00Z', '28 Mar 26 . 20:30 PM'], + ['2026-03-29T01:30:00Z', '28 Mar 26 . 21:30 PM'], + ['2026-11-01T05:30:00Z', '01 Nov 26 . 01:30 AM'], + ['2026-11-01T06:30:00Z', '01 Nov 26 . 01:30 AM'], + ], + 'Europe/London': [ + ['2026-03-08T06:30:00Z', '08 Mar 26 . 06:30 AM'], + ['2026-03-08T07:30:00Z', '08 Mar 26 . 07:30 AM'], + ['2026-03-29T00:30:00Z', '29 Mar 26 . 00:30 AM'], + ['2026-03-29T01:30:00Z', '29 Mar 26 . 02:30 AM'], + ['2026-11-01T05:30:00Z', '01 Nov 26 . 05:30 AM'], + ['2026-11-01T06:30:00Z', '01 Nov 26 . 06:30 AM'], + ], +}; + +// Keep the normal full suite portable on developer machines in other zones. +// The four explicit investigation runs use the independent fixed vectors above. +const vectors = + cases[zone] || + cases.UTC.map(([iso]) => { + const parts = Object.fromEntries( + new Intl.DateTimeFormat('en-GB', { + timeZone: zone, + day: '2-digit', + month: 'short', + year: '2-digit', + hour: '2-digit', + minute: '2-digit', + hourCycle: 'h23', + }) + .formatToParts(new Date(iso)) + .map(({ type, value }) => [type, value]) + ); + return [ + iso, + `${parts.day} ${parts.month} ${parts.year} . ${parts.hour}:${parts.minute} ${ + Number(parts.hour) >= 12 ? 'PM' : 'AM' + }`, + ]; + }); + +describe.each(['confirmed', 'pending', 'cached'])('%s timestamps', (state) => { + test.each(vectors)('%s renders as local time in ' + zone, (iso, expected) => { + const transaction = { + txid: 'public-synthetic-test', + date: new Date(iso).toUTCString(), + confirmations: state === 'confirmed' ? 6 : 0, + amount: 1, + transactionType: 'Received', + }; + const original = JSON.stringify(transaction); + let screen; + act(() => { + screen = renderer.create( + + ); + }); + expect(screen.root.findAllByType('Text').map((text) => text.props.children)).toContain( + expected + ); + expect(JSON.stringify(transaction)).toBe(original); + act(() => screen.unmount()); + }); +}); diff --git a/tests/screens/NewKeeperApp.test.tsx b/tests/screens/NewKeeperApp.test.tsx index 4a7c80501a..6df4c8b199 100644 --- a/tests/screens/NewKeeperApp.test.tsx +++ b/tests/screens/NewKeeperApp.test.tsx @@ -57,6 +57,7 @@ let screen; let store; let actions; let view; +let navigation; function deferred() { let resolve!: (value: string) => void; @@ -68,7 +69,7 @@ function deferred() { return { promise, resolve, reject }; } -async function mount({ error = false, created = false } = {}) { +async function mount({ error = false, created = false, recreated = false } = {}) { actions = []; const loginState = { ...loginReducer(undefined, { type: '@@test/init' }), @@ -79,7 +80,7 @@ async function mount({ error = false, created = false } = {}) { login: (state = loginState, action) => loginReducer(state, action), storage: (state = { appCreated: created }, action) => action.type === 'TEST_APP_CREATED' ? { appCreated: true } : state, - bhr: (state = {}) => state, + bhr: (state = { appRecreated: recreated }) => state, account: (state = { allAccounts: [] }) => state, }, middleware: (getDefaultMiddleware) => @@ -88,7 +89,7 @@ async function mount({ error = false, created = false } = {}) { return next(action); }), }); - const navigation = { replace: jest.fn(), navigate: jest.fn() }; + navigation = { replace: jest.fn(), navigate: jest.fn() }; view = () => ( @@ -122,6 +123,7 @@ afterEach(() => { act(() => screen.unmount()); screen = null; } + jest.useRealTimers(); }); test('setup failure presents one retry dialog, clears its error and starts one retry', async () => { @@ -255,3 +257,53 @@ test('a successfully created app remount does not automatically create another a expect(visibleDialogs()).toHaveLength(0); expect(setupActions()).toHaveLength(0); }); + +test('recreated-app navigation is cancelled when its screen unmounts', async () => { + jest.useFakeTimers(); + await mount({ recreated: true }); + act(() => screen.unmount()); + screen = null; + await act(async () => jest.advanceTimersByTime(4000)); + expect(navigation.replace).not.toHaveBeenCalled(); +}); + +test('Next navigation is cancelled when its screen unmounts', async () => { + jest.useFakeTimers(); + await mount(); + await start(); + await act(async () => store.dispatch({ type: 'TEST_APP_CREATED' })); + const next = visibleDialogs()[0].props.buttonCallback; + act(() => next()); + act(() => screen.unmount()); + screen = null; + await act(async () => jest.advanceTimersByTime(1000)); + expect(navigation.replace).not.toHaveBeenCalled(); +}); + +test('rapid Next taps schedule only one navigation', async () => { + jest.useFakeTimers(); + await mount(); + await start(); + await act(async () => store.dispatch({ type: 'TEST_APP_CREATED' })); + const next = visibleDialogs()[0].props.buttonCallback; + act(() => { + next(); + next(); + }); + await act(async () => jest.advanceTimersByTime(1000)); + expect(navigation.replace).toHaveBeenCalledTimes(1); + expect(navigation.replace).toHaveBeenCalledWith('App', { screen: 'Home' }); +}); + +test('a retained Next callback cannot schedule navigation after unmount', async () => { + jest.useFakeTimers(); + await mount(); + await start(); + await act(async () => store.dispatch({ type: 'TEST_APP_CREATED' })); + const next = visibleDialogs()[0].props.buttonCallback; + act(() => screen.unmount()); + screen = null; + act(() => next()); + await act(async () => jest.advanceTimersByTime(1000)); + expect(navigation.replace).not.toHaveBeenCalled(); +}); diff --git a/tests/screens/SplashScreen.test.tsx b/tests/screens/SplashScreen.test.tsx new file mode 100644 index 0000000000..ef7c7d9670 --- /dev/null +++ b/tests/screens/SplashScreen.test.tsx @@ -0,0 +1,202 @@ +import React from 'react'; +import renderer, { act } from 'react-test-renderer'; + +const mockHasPin = jest.fn(); +const mockDispatch = jest.fn(); +const mockSetUseTor = jest.fn(); +const mockToggleColorMode = jest.fn(); +const mockEvents: string[] = []; +let mockSettings; +let mockColorMode = 'light'; +let mockDev = false; + +jest.mock('src/store/hooks', () => ({ + useAppSelector: (select) => select({ settings: mockSettings }), +})); +jest.mock('react-redux', () => ({ useDispatch: () => mockDispatch })); +jest.mock('src/storage/secure-store', () => ({ hasPin: (...args) => mockHasPin(...args) })); +jest.mock('src/services/rest/RestClient', () => ({ + __esModule: true, + default: { setUseTor: (...args) => mockSetUseTor(...args) }, +})); +jest.mock('@gluestack-ui/themed-native-base', () => ({ + useColorMode: () => ({ colorMode: mockColorMode, toggleColorMode: mockToggleColorMode }), +})); +jest.mock('src/utils/service-utilities/config', () => ({ + __esModule: true, + default: { isDevMode: () => mockDev }, +})); +jest.mock('src/store/sagaActions/settings', () => ({ + changeBitcoinNetwork: (networkType) => ({ type: 'NETWORK', networkType }), +})); +jest.mock('src/store/reducers/storage', () => ({ + setDefaultWalletCreated: (payload) => ({ type: 'DEFAULT_WALLET', payload }), +})); +jest.mock('src/constants/responsive', () => ({ windowHeight: 800, windowWidth: 400 })); +jest.mock('src/components/ThemedSvg.tsx/ThemedSvg', () => 'Logo'); +jest.mock('src/components/KeeperModal', () => 'Modal'); +jest.mock('src/context/Localization/LocContext', () => ({ + LocalizationContext: require('react').createContext({ + translations: require('../../src/context/Localization/language/en.json'), + }), +})); +jest.mock('react-native-reanimated', () => ({ + __esModule: true, + default: { View: 'AnimatedView' }, + cancelAnimation: jest.fn(), + interpolate: () => 1, + runOnJS: (fn) => fn, + useAnimatedStyle: (fn) => fn(), + useSharedValue: (value) => require('react').useRef({ value }).current, + withSpring: (value) => value, + withTiming: (value, { duration }, callback) => { + if (callback) setTimeout(() => callback(true), duration); + return value; + }, +})); +const SplashScreen = require('../../src/screens/Splash/SplashScreen').default; +let screen; +let navigation; +function deferred() { + let resolve, reject; + const promise = new Promise((ok, fail) => { + resolve = ok; + reject = fail; + }); + return { promise, resolve, reject }; +} +async function mount() { + navigation = { replace: jest.fn() }; + await act(async () => { + screen = renderer.create(); + }); +} +function visibleDialogs() { + return screen.root.findAllByType('Modal').filter((modal) => modal.props.visible); +} +beforeEach(() => { + jest.useFakeTimers(); + mockSettings = { torEnbled: false, themeMode: 'LIGHT', bitcoinNetworkType: 'MAINNET' }; + mockColorMode = 'light'; + mockDev = false; + mockEvents.length = 0; + mockDispatch.mockReset().mockImplementation(() => mockEvents.push('dispatch')); + mockSetUseTor.mockReset().mockImplementation(() => mockEvents.push('tor')); + mockToggleColorMode.mockReset().mockImplementation(() => mockEvents.push('theme')); + mockHasPin.mockReset().mockImplementation(async () => { + mockEvents.push('credentials'); + return true; + }); +}); +afterEach(() => { + if (screen) act(() => screen.unmount()); + screen = null; + jest.clearAllTimers(); + jest.useRealTimers(); +}); + +test('starts the credential read without waiting for the splash animation', async () => { + const pin = deferred(); + mockHasPin.mockReturnValue(pin.promise); + await mount(); + expect(mockHasPin).toHaveBeenCalledTimes(1); + expect(mockHasPin).toHaveBeenCalledWith({ throwOnError: true }); + expect(navigation.replace).not.toHaveBeenCalled(); + await act(async () => pin.resolve(true)); + expect(navigation.replace).toHaveBeenCalledWith('Login', { relogin: false }); + await act(async () => jest.advanceTimersByTime(10000)); + expect(navigation.replace).toHaveBeenCalledTimes(1); +}); + +test('only a completed false credential read selects CreatePin', async () => { + mockHasPin.mockResolvedValue(false); + await mount(); + expect(navigation.replace).toHaveBeenCalledWith('CreatePin'); + expect(navigation.replace).toHaveBeenCalledTimes(1); +}); + +test('animation completion cannot bypass a slow credential read', async () => { + const pin = deferred(); + mockHasPin.mockReturnValue(pin.promise); + await mount(); + await act(async () => jest.advanceTimersByTime(10000)); + expect(navigation.replace).not.toHaveBeenCalled(); + await act(async () => pin.resolve(true)); + expect(navigation.replace).toHaveBeenCalledWith('Login', { relogin: false }); +}); + +test.each(['resolve', 'reject'])('unmount ignores a late credential %s', async (completion) => { + const pin = deferred(); + mockHasPin.mockReturnValue(pin.promise); + await mount(); + expect(mockHasPin).toHaveBeenCalledTimes(1); + act(() => screen.unmount()); + screen = null; + await act(async () => { + if (completion === 'resolve') pin.resolve(true); + else pin.reject(new Error('private credential detail')); + }); + expect(navigation.replace).not.toHaveBeenCalled(); +}); + +test('an old mount cannot navigate over the current startup decision', async () => { + const oldPin = deferred(); + const newPin = deferred(); + mockHasPin.mockReturnValueOnce(oldPin.promise).mockReturnValueOnce(newPin.promise); + await mount(); + const oldNavigation = navigation; + act(() => screen.unmount()); + screen = null; + await mount(); + await act(async () => newPin.resolve(true)); + await act(async () => oldPin.resolve(false)); + expect(oldNavigation.replace).not.toHaveBeenCalled(); + expect(navigation.replace).toHaveBeenCalledWith('Login', { relogin: false }); +}); + +test('credential rejection stays on Splash with one retry and no duplicate read', async () => { + const retry = deferred(); + mockHasPin + .mockRejectedValueOnce(new Error('private credential detail')) + .mockReturnValueOnce(retry.promise); + await mount(); + expect(navigation.replace).not.toHaveBeenCalled(); + expect(visibleDialogs()).toHaveLength(1); + expect(JSON.stringify(visibleDialogs()[0].props)).not.toContain('private credential detail'); + const retryAction = visibleDialogs()[0].props.buttonCallback; + await act(async () => { + retryAction(); + retryAction(); + }); + await act(async () => retryAction()); + expect(mockHasPin).toHaveBeenCalledTimes(2); + expect(visibleDialogs()).toHaveLength(0); + await act(async () => retry.resolve(true)); + expect(navigation.replace).toHaveBeenCalledWith('Login', { relogin: false }); +}); + +test.each([false, true])( + 'initializes default network before credentials (development=%s)', + async (dev) => { + mockDev = dev; + mockSettings.bitcoinNetworkType = null; + mockSettings.themeMode = 'DARK'; + mockSettings.torEnbled = true; + await mount(); + const networkType = dev ? 'TESTNET' : 'MAINNET'; + expect(mockDispatch.mock.calls).toEqual([ + [{ type: 'DEFAULT_WALLET', payload: { networkType, created: true } }], + [{ type: 'NETWORK', networkType }], + ]); + expect(mockEvents).toEqual(['dispatch', 'dispatch', 'theme', 'tor', 'credentials']); + expect(mockSetUseTor).toHaveBeenCalledWith(true); + } +); + +test('rerendering preserves the selected network and does not read credentials again', async () => { + mockSettings.bitcoinNetworkType = 'TESTNET'; + await mount(); + await act(async () => screen.update()); + expect(mockDispatch).not.toHaveBeenCalled(); + expect(mockHasPin).toHaveBeenCalledTimes(1); +}); diff --git a/tests/services/startupCredentials.test.ts b/tests/services/startupCredentials.test.ts new file mode 100644 index 0000000000..94e749585c --- /dev/null +++ b/tests/services/startupCredentials.test.ts @@ -0,0 +1,45 @@ +// Exercise the real credential-read boundary, without writing native storage. +const mockGetGenericPassword = jest.fn(); +let mockAccounts = []; +jest.mock('react-native-keychain', () => ({ + getGenericPassword: (...args) => mockGetGenericPassword(...args), +})); +jest.mock('src/store/store', () => ({ + store: { getState: () => ({ account: { allAccounts: mockAccounts } }) }, +})); +jest.mock('src/utils/service-utilities/config', () => ({ __esModule: true, default: {} })); +const { hasPin } = require('../../src/storage/secure-store'); +beforeEach(() => { + mockAccounts = []; + mockGetGenericPassword.mockReset(); +}); + +test('startup propagates a native read failure rather than treating it as no PIN', async () => { + const error = new Error('synthetic native read failure'); + mockGetGenericPassword.mockRejectedValue(error); + await expect(hasPin({ throwOnError: true })).rejects.toBe(error); +}); +test('startup propagates invalid stored credential JSON', async () => { + mockGetGenericPassword.mockResolvedValue({ password: 'invalid synthetic JSON' }); + await expect(hasPin({ throwOnError: true })).rejects.toThrow(SyntaxError); +}); +test('legacy callers retain their existing false-on-error behavior', async () => { + mockGetGenericPassword.mockRejectedValue(new Error('synthetic failure')); + await expect(hasPin()).resolves.toBe(false); +}); +test('a successfully read empty legacy store still means no PIN', async () => { + mockGetGenericPassword.mockResolvedValue(false); + await expect(hasPin({ throwOnError: true })).resolves.toBe(false); + expect(mockGetGenericPassword).toHaveBeenCalledWith({ service: undefined }); +}); +test('reads the existing account service names without writing credentials', async () => { + mockAccounts = [{ accountIdentifier: '' }, { accountIdentifier: 'synthetic-second-account' }]; + mockGetGenericPassword + .mockResolvedValueOnce(false) + .mockResolvedValueOnce({ password: '{"hash":"synthetic"}' }); + await expect(hasPin({ throwOnError: true })).resolves.toBe(true); + expect(mockGetGenericPassword.mock.calls).toEqual([ + [{ service: undefined }], + [{ service: 'synthetic-second-account' }], + ]); +}); From 5954c41a33b0abca16517c6d1c9fe8621f4c2488 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Thu, 22 Oct 2026 03:41:53 +0000 Subject: [PATCH 208/254] fix(r5): correct Emergency Key helper and record R4 preflight --- docs/r5/R4-RECONCILIATION.md | 100 ++++++++++++++++++++++ docs/r5/WORKSTREAM.md | 17 ++++ src/context/Localization/language/en.json | 1 + src/context/Localization/language/es.json | 1 + src/screens/Vault/ResetEmergencyKey.tsx | 2 +- 5 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 docs/r5/R4-RECONCILIATION.md diff --git a/docs/r5/R4-RECONCILIATION.md b/docs/r5/R4-RECONCILIATION.md new file mode 100644 index 0000000000..739c0014b2 --- /dev/null +++ b/docs/r5/R4-RECONCILIATION.md @@ -0,0 +1,100 @@ +# R4 baseline reconciliation preflight — 22 October 2026 + +Status: source comparison complete; baseline selection is awaiting clarification. +This is not a completed rebase, released-baseline verification or native result. + +## Evidence and baseline discrepancy + +The owner reported that R4 is released and authorized the next R5 step. Read-only +checks found the latest recorded store-artifact source to be +`1d8a86a7462d935cbd0e28b857934b5718c152fc`, from open/draft/unmerged PR #7045. +Its newest public record, [store checkpoint](https://github.com/KeeperCommunity/bitcoin-keeper/pull/7045#issuecomment-6313099996), +still says iOS 2.6.4/620 WAITING_FOR_REVIEW/manual release and Android 2.6.4/627 +IN_REVIEW/managed publishing; public release is held. GitHub has no v2.6.4 tag or +release, and sprint remains `def0d6a90c80287edb4f58ae272cbfc340f00d73`. +The public Apple India lookup for app1545535925 returned 2.6.3, released 7 October. +That regional lookup is not proof of all countries/platforms and does not override +newer owner evidence that may not yet be recorded. + +Clarification requested: use 1d8a86a as the settled baseline, or provide the newer +released SHA/tag. Pending that answer, R5's ancestry and PR base remain unchanged. +No R4 ref, release state, store state or backend was modified. + +## Read-only integration preview + +R5 compared: `98c4e2e78483ec03fac7ac72d86419e6da42fc98`. +R4 compared: `1d8a86a7462d935cbd0e28b857934b5718c152fc`. +Common ancestor: sprint `def0d6a90c80287edb4f58ae272cbfc340f00d73`. + +`git merge-tree --write-tree` wrote a preview object only; no checkout, index or +branch merge was performed. The preview reports one textual conflict, yarn.lock. +Application modules combine textually; this does not prove semantic compatibility. +Localization and the CI workflow auto-combine but still require review. + +Resolve a future lock conflict by preserving the selected R4 dependency graph and +retaining only necessary R5 root ESLint selector repairs. Do not reintroduce an +obsolete eslint-plugin-react resolution or discard R4's pinned backup dependencies. +Run frozen install, explicit patch application and input-immutability checks before +using reconciled dependencies. Do not copy another workstream's lockfile wholesale. + +R4 contains 337 changed paths relative to sprint. Incorporating that unmerged tree +into the sprint-based R5 PR now would also expose R4's large change set as R5's diff. +Keep the R5 review range separate and confirm the release/integration baseline +rather than silently turning R5 into a vehicle to merge R4. + +## Compatibility findings + +- The R4 deletion worker forwards original app/session/write authority and treats + unsuccessful local deletion as failure. `deleteAppImageEntityWorker` checks all + retained Vault signer references *inside* the local transaction before deleting + any signer. This complements R5's live-query/UI guard; it does not replace the + pending integrated account/authentication, callback and native Realm tests. +- R4 middleware binds destructive action origins at dispatch. After integration, + verify that an R5 confirmation created before an account/session switch cannot + be rebound to a new account merely because dispatch happens later. Do not infer + this from separate UI and backend unit tests. No backend/session guard was + changed in this preflight. +- R5's Splash, setup and secure-store changes combine without textual conflict. + R4's changed PIN/login flow remains authoritative for native validation; no + shipped PIN fix was reimplemented. The updated Maestro flow must actually run. +- R4 still uses the Inheritance Key helper on ResetEmergencyKey. The independent + R5 copy correction now selects an Emergency-specific localization entry, leaving + the Inheritance helper and every time/policy/calculation operation unchanged. + Both supplied locales retained their existing English-copy convention. +- Assisted Server Backup names/retention guarantees still require comparison with + the actual shipped app/help surfaces; do not rewrite backup guarantees from the + store-submission record alone. + +## Executed checks and limits + +R4 source was checked out **detached in a separate task-owned audit worktree**; +its tracked files remain unchanged. It reused the current R5 node_modules by +symlink, so no new dependency/environment/native setup was needed. Executed: + +`node --test tests/recovery/accountSessionDestructive.test.cjs tests/recovery/deleteResultTruthfulness.test.cjs tests/recovery/signerPersistence.test.cjs` + +Result: **31 PASS, 0 FAIL, 0 SKIP**, actual R4 source at 1d8a86a, using the existing +unit transaction/queue/Realm adapters. No production provider/backend, native +Realm, physical device, integration build or R5-on-R4 combined tree was tested. +These cases must not be presented as a reconciled R5 or native pass. + +The Emergency copy change passes localization JSON parsing and `git diff --check`. +Its focused ESLint remains **FAIL 39 errors / 1 warning**, exactly the prior R5 +screen's findings; zero added diagnostics. No new copy-mirroring test was added. +Native Emergency and Inheritance reset screens still need verification. + +The previous R5 head98c4 hosted CI39586080249 Tests job and CodeQL39586080223 are +now SUCCESS. This is recorded job status, not a new native or clean-lint result. +Detailed hosted job-log download was unavailable (403); no new full-suite count +or claim that actual full lint passed follows from the green workflow job. +Prior local R5 218-test proof retains its original source scope. + +## Next action + +Confirm the exact settled R4 source, integrate/reconcile R5 in isolation while +preserving R4 and the existing review history, resolve the lock conflict narrowly, +and rerun affected R5 plus account/session/deletion and release-baseline checks. +Only then issue the exact-head finite native plan: Cloud first where available, +Mac only where needed, one native build at a time, fresh capacity assessment and +no unapproved worktree/device/wallet/cache/evidence cleanup. No optional Linux +setup is a prerequisite. No merge/release action is authorized by this preflight. diff --git a/docs/r5/WORKSTREAM.md b/docs/r5/WORKSTREAM.md index f6362be7da..8693bb0fdc 100644 --- a/docs/r5/WORKSTREAM.md +++ b/docs/r5/WORKSTREAM.md @@ -249,3 +249,20 @@ or a third feature. Native startup/first-wallet timings, offline wallet-worker idempotency/persistence, real Realm/authentication, #7055/#6991 native reproduction, PIN/biometrics and R4-shipped copy reconciliation remain open. Reconcile against R4's eventual released baseline and rerun affected checks before native acceptance. + +## R4 reconciliation preflight — 22 October 2026 + +The owner reported R4 released and authorized moving forward. Current repository +and regional store evidence still identify open/draft #7045 source1d8a86a with +held store review, unchanged sprint and no2.6.4 tag; exact baseline clarification +is pending. The [reconciliation preflight](R4-RECONCILIATION.md) records a read-only +merge preview (one yarn.lock conflict), transactional deletion/account-binding +review and **31/31 passing R4 unit cases** in a detached, unchanged audit worktree. +This is not an integrated R5/native pass. No R4 branch or R5 ancestry was changed. + +The approved narrow Emergency Key helper correction is independently applied to +R5: a dedicated EN/ES helper now names Emergency Key, while Inheritance Key keeps +its original helper. No timelock or recovery calculation changed. JSON/diff PASS; +existing ResetEmergencyKey ESLint FAIL39/1 with zero added findings. Native copy +checks remain open. Previous R5 head98c4 hosted Tests and CodeQL are now SUCCESS; +job-log download was unavailable, so no new hosted suite count or clean-lint claim. diff --git a/src/context/Localization/language/en.json b/src/context/Localization/language/en.json index c2856c05ac..f59ff0fdeb 100644 --- a/src/context/Localization/language/en.json +++ b/src/context/Localization/language/en.json @@ -673,6 +673,7 @@ "selectActivationTime": "Select activation time", "chooseNewActivationTimeTitle": "Choose New Activation Time", "chooseNewActivationTimeDesc": "Please choose the new time for your Inheritance Key to become active", + "chooseNewEmergencyActivationTimeDesc": "Please choose the new time for your Emergency Key to become active", "scanQR": "Scan QR", "scanOrUpload": "Please scan the QR or upload an image", "nfcOnTap": "NFC on tap", diff --git a/src/context/Localization/language/es.json b/src/context/Localization/language/es.json index 3298a16c16..9266229e4d 100644 --- a/src/context/Localization/language/es.json +++ b/src/context/Localization/language/es.json @@ -661,6 +661,7 @@ "selectActivationTime": "Select activation time", "chooseNewActivationTimeTitle": "Choose New Activation Time", "chooseNewActivationTimeDesc": "Please choose the new time for your Inheritance Key to become active", + "chooseNewEmergencyActivationTimeDesc": "Please choose the new time for your Emergency Key to become active", "scanQR": "Scan QR", "scanOrUpload": "Please scan the QR or upload an image", "nfcOnTap": "NFC on tap", diff --git a/src/screens/Vault/ResetEmergencyKey.tsx b/src/screens/Vault/ResetEmergencyKey.tsx index 838e055488..78cdc7782d 100644 --- a/src/screens/Vault/ResetEmergencyKey.tsx +++ b/src/screens/Vault/ResetEmergencyKey.tsx @@ -213,7 +213,7 @@ function ResetEmergencyKey({ route }) { {vaultText.chooseNewActivationTimeTitle} - {vaultText.chooseNewActivationTimeDesc} + {vaultText.chooseNewEmergencyActivationTimeDesc} Date: Thu, 22 Oct 2026 04:17:59 +0000 Subject: [PATCH 209/254] fix(r5): bind deletion to released account-session authority --- .github/workflows/test.yml | 16 +- docs/r5/R4-RECONCILIATION.md | 220 ++++---- docs/r5/STARTUP-INVESTIGATION.md | 4 +- docs/r5/WORKSTREAM.md | 38 +- .../reconcile-r5-release-baseline/design.md | 12 +- .../reconcile-r5-release-baseline/proposal.md | 4 + .../r5-account-bound-key-deletion/spec.md | 15 + .../reconcile-r5-release-baseline/tasks.md | 23 +- .../Home/components/Keys/ManageKeys.tsx | 4 +- src/screens/SigningDevices/DeleteKeys.tsx | 38 +- tests/screens/DeleteKeys.test.tsx | 485 ++++++++++++------ 11 files changed, 584 insertions(+), 275 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 28ae7d0093..cf7f29ae26 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -80,6 +80,11 @@ jobs: - name: Verify dependency inputs remain unchanged run: git diff --exit-code HEAD -- package.json yarn.lock ios/Podfile.lock + - name: Set up Ruby for build-only release lane regressions + uses: ruby/setup-ruby@v1 + with: + ruby-version: '3.3.8' + - name: Check USDT pause and retired build inputs run: | node --test tests/services/usdtPause.test.cjs tests/recovery/usdtBackup.test.cjs \ @@ -88,17 +93,18 @@ jobs: - name: Run tests run: npm run test + # Frozen installation intentionally skips lifecycle scripts. The released + # recovery suite includes genuine encrypted Realm transaction tests. + - name: Install the Realm N-API binding for recovery tests + working-directory: node_modules/realm + run: ../.bin/prebuild-install --runtime napi --target 6 + - name: Run recovery regression tests run: npm run test:recovery - name: Verify checked-out release version consistency run: npm run version:check - - name: Set up Ruby for build-only release lane regressions - uses: ruby/setup-ruby@v1 - with: - ruby-version: '3.3.8' - - name: Run release and packaged-artifact tests run: npm run test:release diff --git a/docs/r5/R4-RECONCILIATION.md b/docs/r5/R4-RECONCILIATION.md index 739c0014b2..b640146ed8 100644 --- a/docs/r5/R4-RECONCILIATION.md +++ b/docs/r5/R4-RECONCILIATION.md @@ -1,100 +1,120 @@ -# R4 baseline reconciliation preflight — 22 October 2026 - -Status: source comparison complete; baseline selection is awaiting clarification. -This is not a completed rebase, released-baseline verification or native result. - -## Evidence and baseline discrepancy - -The owner reported that R4 is released and authorized the next R5 step. Read-only -checks found the latest recorded store-artifact source to be -`1d8a86a7462d935cbd0e28b857934b5718c152fc`, from open/draft/unmerged PR #7045. -Its newest public record, [store checkpoint](https://github.com/KeeperCommunity/bitcoin-keeper/pull/7045#issuecomment-6313099996), -still says iOS 2.6.4/620 WAITING_FOR_REVIEW/manual release and Android 2.6.4/627 -IN_REVIEW/managed publishing; public release is held. GitHub has no v2.6.4 tag or -release, and sprint remains `def0d6a90c80287edb4f58ae272cbfc340f00d73`. -The public Apple India lookup for app1545535925 returned 2.6.3, released 7 October. -That regional lookup is not proof of all countries/platforms and does not override -newer owner evidence that may not yet be recorded. - -Clarification requested: use 1d8a86a as the settled baseline, or provide the newer -released SHA/tag. Pending that answer, R5's ancestry and PR base remain unchanged. -No R4 ref, release state, store state or backend was modified. - -## Read-only integration preview - -R5 compared: `98c4e2e78483ec03fac7ac72d86419e6da42fc98`. -R4 compared: `1d8a86a7462d935cbd0e28b857934b5718c152fc`. -Common ancestor: sprint `def0d6a90c80287edb4f58ae272cbfc340f00d73`. - -`git merge-tree --write-tree` wrote a preview object only; no checkout, index or -branch merge was performed. The preview reports one textual conflict, yarn.lock. -Application modules combine textually; this does not prove semantic compatibility. -Localization and the CI workflow auto-combine but still require review. - -Resolve a future lock conflict by preserving the selected R4 dependency graph and -retaining only necessary R5 root ESLint selector repairs. Do not reintroduce an -obsolete eslint-plugin-react resolution or discard R4's pinned backup dependencies. -Run frozen install, explicit patch application and input-immutability checks before -using reconciled dependencies. Do not copy another workstream's lockfile wholesale. - -R4 contains 337 changed paths relative to sprint. Incorporating that unmerged tree -into the sprint-based R5 PR now would also expose R4's large change set as R5's diff. -Keep the R5 review range separate and confirm the release/integration baseline -rather than silently turning R5 into a vehicle to merge R4. - -## Compatibility findings - -- The R4 deletion worker forwards original app/session/write authority and treats - unsuccessful local deletion as failure. `deleteAppImageEntityWorker` checks all - retained Vault signer references *inside* the local transaction before deleting - any signer. This complements R5's live-query/UI guard; it does not replace the - pending integrated account/authentication, callback and native Realm tests. -- R4 middleware binds destructive action origins at dispatch. After integration, - verify that an R5 confirmation created before an account/session switch cannot - be rebound to a new account merely because dispatch happens later. Do not infer - this from separate UI and backend unit tests. No backend/session guard was - changed in this preflight. -- R5's Splash, setup and secure-store changes combine without textual conflict. - R4's changed PIN/login flow remains authoritative for native validation; no - shipped PIN fix was reimplemented. The updated Maestro flow must actually run. -- R4 still uses the Inheritance Key helper on ResetEmergencyKey. The independent - R5 copy correction now selects an Emergency-specific localization entry, leaving - the Inheritance helper and every time/policy/calculation operation unchanged. - Both supplied locales retained their existing English-copy convention. -- Assisted Server Backup names/retention guarantees still require comparison with - the actual shipped app/help surfaces; do not rewrite backup guarantees from the - store-submission record alone. - -## Executed checks and limits - -R4 source was checked out **detached in a separate task-owned audit worktree**; -its tracked files remain unchanged. It reused the current R5 node_modules by -symlink, so no new dependency/environment/native setup was needed. Executed: - -`node --test tests/recovery/accountSessionDestructive.test.cjs tests/recovery/deleteResultTruthfulness.test.cjs tests/recovery/signerPersistence.test.cjs` - -Result: **31 PASS, 0 FAIL, 0 SKIP**, actual R4 source at 1d8a86a, using the existing -unit transaction/queue/Realm adapters. No production provider/backend, native -Realm, physical device, integration build or R5-on-R4 combined tree was tested. -These cases must not be presented as a reconciled R5 or native pass. - -The Emergency copy change passes localization JSON parsing and `git diff --check`. -Its focused ESLint remains **FAIL 39 errors / 1 warning**, exactly the prior R5 -screen's findings; zero added diagnostics. No new copy-mirroring test was added. -Native Emergency and Inheritance reset screens still need verification. - -The previous R5 head98c4 hosted CI39586080249 Tests job and CodeQL39586080223 are -now SUCCESS. This is recorded job status, not a new native or clean-lint result. -Detailed hosted job-log download was unavailable (403); no new full-suite count -or claim that actual full lint passed follows from the green workflow job. -Prior local R5 218-test proof retains its original source scope. - -## Next action - -Confirm the exact settled R4 source, integrate/reconcile R5 in isolation while -preserving R4 and the existing review history, resolve the lock conflict narrowly, -and rerun affected R5 plus account/session/deletion and release-baseline checks. -Only then issue the exact-head finite native plan: Cloud first where available, -Mac only where needed, one native build at a time, fresh capacity assessment and -no unapproved worktree/device/wallet/cache/evidence cleanup. No optional Linux -setup is a prerequisite. No merge/release action is authorized by this preflight. +# R5 reconciliation against released R4 / 2.6.4 + +The owner explicitly confirmed on 22 October 2026 that 2.6.4 is live on the app +stores. That confirmation resolves the earlier stale-record discrepancy; GitHub +PR/tag status and regional store caches are not prerequisites for R5 work. + +## Baseline and history + +Released artifact source: `1d8a86a7462d935cbd0e28b857934b5718c152fc` (R4 / 2.6.4, +iOS620, Android627). The recorded signed-artifact/source checkpoint identifies +this exact source. The source is now an ancestor of the isolated R5 branch through +reconciliation commit `a8947c867562b6148f328289069785b18053b944`. +The earlier R5 head5954 and its history are also preserved. No R4/sprint/other +workstream ref was changed, no force push occurred and no GitHub PR was merged. + +The single yarn.lock conflict was resolved to the **exact released R4 lockfile**. +That file already includes both missing ESLint selectors repaired earlier in R5, +so retaining the obsolete R5 resolution would have been incorrect. package.json, +yarn.lock, Podfile.lock, Android/iOS native inputs, version metadata and existing +backup/backend/store/worker modules remain byte-identical to the released source. + +The PR remains based on sprint per repository guidance. Until released history is +carried to sprint by its owner, its ordinary diff includes inherited R4 history. +Review R5's own scope as `git diff 1d8a86a...` (the PR handoff links that +range). This inherited history is not new R5 scope or permission to merge R4. + +## Combined-source changes and findings + +- R4's local transaction checks **all retained Vault signer references** before + deleting a Signer. R5 retains that guard, its current-reference UI checks, + archived retention, protected-key exclusions and one-use passcode intent. +- R4 stamps action origin at dispatch. Eleven new screen regressions demonstrated + that an earlier R5 confirmation could reach that boundary after an account + switch or A → B → A round trip. Selection/confirmation now compare their original + app/session with current Redux and Realm ownership. Delete/archive actions carry + the original app/session explicitly, preventing dispatch-time rebinding. These + tests use production transport-session tracking, identity and action creators; + native UI, Realm access and the Redux state getter are modeled boundaries. +- R5's Keys entry uses the released navigation dispatch pattern; this removes the + TypeScript overload mismatch exposed by integration. No shared PIN/navigation + implementation, transaction formatting or timelock calculation changed. +- Emergency reset uses its Emergency-specific helper; Inheritance retains its + own helper. Native display checks remain open. +- Existing R5 frozen JavaScript CI preparation is retained. Its recovery lane now + explicitly installs the released Realm N-API binary, and existing Ruby setup + precedes all Ruby-dependent guards. No dependency version or workflow gate was + changed. The existing public-v2 genuine backend fixture remains baseline debt; + no protocol, readiness, pin, backend deployment or scanner waiver was introduced. + +Repository OpenSpec planning artifacts for this reconciliation were committed with +the baseline integration before the new ownership guard was implemented. DESIGN.md +was reviewed; existing components, layout, theme and critical controls are retained. + +## Executed Cloud checks + +Node22.23.3 / Yarn1.22.22: + +- Frozen install PASS; all six explicit dependency patches PASS. Released + package/lock/Pod/native/version input-immutability checks PASS. +- Full Jest **47 suites /531 tests PASS**, zero failures/skips, including the + released loopback Electrum fixtures. R5-only focused run **5 suites /99 tests + PASS**, including53 deletion cases; final affected UI rerun **71 PASS**. +- New guard controls before implementation: **11 assertion failures**, with the + other42 deletion tests deliberately deselected (not execution passes). All53 + deletion tests pass after the fix. Real account-round-trip session tracking is + used; this does not prove actual native authentication/Realm provenance. +- Full recovery **480/480 PASS**, zero skips. Initial execution had470 passes and + one file-load failure because the Realm N-API binary was absent. The exact + released binary was installed with prebuild-install; the10 encrypted Realm + cases then passed and the complete recovery rerun passed. These are Linux Realm + tests, not iOS/Android app-native acceptance. +- Gate contracts **11/11 PASS**. Static Android16KB/reproducibility checks **31 PASS**. + Version consistency, JSON/YAML/prerequisite ordering, formatting, OpenSpec strict + validation and diff checks PASS. +- Release-tool Node **28 PASS** and Python **7 PASS**. Local Ruby is unavailable: + the combined release command stops at Ruby. A related guard run recorded30 PASS + and11 failures from its missing Ruby runner; those failures are preserved and + not reported as app regressions or passes. Hosted CI now sets up the existing + Ruby3.3.8 prerequisite before these unchanged checks. +- Focused lint remains **FAIL82 errors/25 warnings**, versus85/25 on released R4 + for the same six modules/dependencies; normalized0 added,3 removed. +- Whole TypeScript remains **FAIL943 diagnostics**, versus944 on released R4 with + the same dependencies; normalized0 added,1 removed. The transient new navigation + overload finding was fixed before this comparison. Neither check is clean. + +The exact final SHA and hosted checks are recorded in the PR handoff. A full +app-native build/install/UI, performance measurement or physical-device result +must not be inferred from any JavaScript or Linux Realm result. + +## Finite native handoff + +Use the final PR head, reuse compatible released-R4 caches and preserve all data. +Cloud development remains primary; optional Linux setup is not a prerequisite. +The separate hosted-native infrastructure PR is not imported as another R5 feature. +Where that approved route is available, run its exact-head development startup +build/smoke; use the Mac only for remaining platform/feature/device checks. + +Before any Mac work, read its actual local guidance, measure free disk and use the +larger actual build forecast (approximately6GiB is only a planning floor), one +native build at a time. Never delete other worktrees, simulators/AVD users, +wallets/databases, caches, artifacts or evidence without approval. + +1. IOS and ANDROID: new development artifacts from the exact head; cold/warm/resume, + theme/network/Tor, credential error/retry, deep-link/auth and small display cases. +2. Disposable account only: unused visible/hidden key cancellation/deletion, active/ + hidden/canary/multi-wallet blocks, archived retention, new references during + confirmation and same-key account/session changes (including A → B → A). Preserve + real wallets; test against an approved disposable fixture, not production data. +3. Setup offline/failure/repeated Retry/Next, success/relaunch and persistent wallet + counts; measure at least five raw first-wallet/startup samples with exact build, + device/OS/network configuration. Do not substitute the animation duration or + unit-test wall time for native performance. +4. Bounded #7055 More→Wallets and #6991 device timezone/DST/history/details checks; + no speculative patch. Verify Emergency/Inheritance helper and shipped Assisted + Server Backup copy. Owner-parked #7056/#7057 remain excluded. +5. HARDWARE: actual physical PIN/biometric/device scenarios remain explicit. R4's + physical deferral is not an R5 pass or automatically transferable scope decision. + +No PR merge, signing, store action, deployment or release is authorized by passing +these checks. R5 remains a draft with unrun native acceptance and review visible. diff --git a/docs/r5/STARTUP-INVESTIGATION.md b/docs/r5/STARTUP-INVESTIGATION.md index 692a3a0b27..98c1e2be27 100644 --- a/docs/r5/STARTUP-INVESTIGATION.md +++ b/docs/r5/STARTUP-INVESTIGATION.md @@ -1,6 +1,8 @@ # R5 startup and bounded investigations — 21 October 2026 -Development evidence only. R4 remains an unmerged draft; no native acceptance, +Development evidence only. The owner confirms R4/2.6.4 is live; R5 is now +reconciled to its recorded source1d8a86a. Earlier unmerged/pending descriptions +below retain their original investigation scope. No native acceptance, release-readiness, issue closure or performance improvement is claimed here. ## Newer work checked before editing diff --git a/docs/r5/WORKSTREAM.md b/docs/r5/WORKSTREAM.md index 8693bb0fdc..fc4ec16a65 100644 --- a/docs/r5/WORKSTREAM.md +++ b/docs/r5/WORKSTREAM.md @@ -1,6 +1,9 @@ # R5 Cloud workstream -Status: development draft; R4 release baseline pending. No app version/date assigned. +Status: development draft reconciled to owner-confirmed live R4 / 2.6.4 source +`1d8a86a7462d935cbd0e28b857934b5718c152fc`. No R5 app version/date assigned. +The earlier pending-baseline records below are historical and superseded by the +22 October owner confirmation and completed reconciliation. ## Authority and budget @@ -266,3 +269,36 @@ its original helper. No timelock or recovery calculation changed. JSON/diff PASS existing ResetEmergencyKey ESLint FAIL39/1 with zero added findings. Native copy checks remain open. Previous R5 head98c4 hosted Tests and CodeQL are now SUCCESS; job-log download was unavailable, so no new hosted suite count or clean-lint claim. + + +## Released-baseline reconciliation completed — 22 October 2026 + +The owner's explicit confirmation that R4/2.6.4 is live resolves the earlier +metadata discrepancy. No further release/tag/source confirmation is being awaited. +Released source1d8a86a is now an ancestor of R5 through commit a8947c867; previous +R5 history remains intact. R4/sprint/other workstream refs and all released native, +dependency, backend/store/worker/version inputs remain unchanged. The one lock +conflict resolves to R4's exact lock, which already includes the two R5 selectors. + +The [current reconciliation and finite native plan](R4-RECONCILIATION.md) records +full Jest47/531 PASS, R5 focused99 PASS, recovery480/480 PASS (including10 real +Linux Realm cases after installing its missing N-API prerequisite), gate11 PASS, +static31 PASS and Node/Python release-tool28/7 PASS. Local Ruby checks remain +unavailable/failed-prerequisite, not passed; hosted CI explicitly installs Ruby +before them. Lint82/25 and TypeScript943 remain inherited failures with no added +normalized diagnostics versus released R4. Native app/UI/performance/physical +acceptance has NOT RUN here. + +Reconciliation also confirmed and fixed eleven stale-origin UI cases: key-delete +selection and confirmation now retain the original account/session, verify current +Redux/Realm ownership, and carry that origin into R4's existing middleware/worker. +No R4 reference/session guard was weakened. New tests execute real session-counter, +identity and action-creator logic with modeled screen/storage boundaries. The +Keys entry's dispatch form was adapted to the released navigation types. + +The R5-only changed source/test estimate is approximately16.5k tokens (rough +characters/4), within the approved two-feature13k–18k target. Review against the +released source rather than counting R4's inherited history as new R5 work. Do not +silently add hosted-native infrastructure, extra backend work or a third feature. +No optional Linux setup or Mac session was awaited. Next is the exact-head finite +native acceptance/performance plan and required review; no PR merge or release. diff --git a/openspec/changes/reconcile-r5-release-baseline/design.md b/openspec/changes/reconcile-r5-release-baseline/design.md index 26c178f6d7..67a52b6894 100644 --- a/openspec/changes/reconcile-r5-release-baseline/design.md +++ b/openspec/changes/reconcile-r5-release-baseline/design.md @@ -22,9 +22,11 @@ so R4 middleware cannot silently bind them to a different account. Existing wallet sagas and transactional guards remain unchanged. Affected R5 files: src/screens/SigningDevices/DeleteKeys.tsx, -tests/screens/DeleteKeys.test.tsx, docs/r5/R4-RECONCILIATION.md, +tests/screens/DeleteKeys.test.tsx, src/screens/Home/components/Keys/ManageKeys.tsx +(typed navigation dispatch adaptation), docs/r5/R4-RECONCILIATION.md, docs/r5/WORKSTREAM.md, and this OpenSpec change. The baseline integration also -combines the existing R5 CI workflow; package.json/yarn.lock/Podfile.lock and native +combines the existing R5 CI workflow and explicitly installs its Realm N-API +prerequisite before recovery tests; package.json/yarn.lock/Podfile.lock and native inputs remain exact R4 bytes. No new UI component: reuse KeyCard, KeeperModal and PasscodeVerify and existing theme tokens per DESIGN.md. Its implementation checklist is satisfied without changing layout, text scale or critical controls. @@ -42,3 +44,9 @@ switches. Run original R5 tests against the combined tree, add stale-origin regressions, rerun existing R4 deletion/session checks and disclose every unrun native/genuine fixture. The inherited public-v2 Relay fixture mismatch is baseline debt; no backend/protocol/pin workaround belongs in this reconciliation. + +Existing NewKeeperApp effects/timers also capture the production transport session. +Their active predicates combine effect lifetime and session equality. Tests use +the real counter with explicit account-transition fixtures; setup workers and +account/credential schemas remain unchanged. Affected files additionally include +src/screens/NewKeeperAppScreen/NewKeeperAppScreen.tsx and its existing screen test. diff --git a/openspec/changes/reconcile-r5-release-baseline/proposal.md b/openspec/changes/reconcile-r5-release-baseline/proposal.md index 5acdf30804..77c8117169 100644 --- a/openspec/changes/reconcile-r5-release-baseline/proposal.md +++ b/openspec/changes/reconcile-r5-release-baseline/proposal.md @@ -36,3 +36,7 @@ The R5 delta remains within the two-feature, approximately 13k–18k token estim No R4 branch modification or PR merge, release, backend/protocol change, dependency upgrade, Receive QR, shared-component rewrite, scaling, Ask Keeper delivery, private-data transfer or optional Linux setup. + +The same existing startup feature must retain its account session across pending +notification tokens and delayed navigation; this is callback ownership, not a +third feature or a change to backend account creation. diff --git a/openspec/changes/reconcile-r5-release-baseline/specs/r5-account-bound-key-deletion/spec.md b/openspec/changes/reconcile-r5-release-baseline/specs/r5-account-bound-key-deletion/spec.md index 89d61b823c..c5de22bebc 100644 --- a/openspec/changes/reconcile-r5-release-baseline/specs/r5-account-bound-key-deletion/spec.md +++ b/openspec/changes/reconcile-r5-release-baseline/specs/r5-account-bound-key-deletion/spec.md @@ -32,3 +32,18 @@ All active Vault references remain blocking. No satoshi amount or UTXO changes. - **WHEN** its current account confirms a deletion attempt - **THEN** active references block it and archived references retain the Signer; cancellation and repeated callbacks cannot produce a second destructive action + +### Requirement: First-use callbacks retain their account session + +Existing setup and notification callbacks SHALL not dispatch or navigate after +their original account session changes, even if the screen remains mounted. + +#### Scenario: Token completes after an account round trip +- **GIVEN** setup or its post-success token update pending in session A +- **WHEN** the account changes A → B → A and the token resolves or rejects +- **THEN** no old setup/token action is dispatched into the new session + +#### Scenario: Delayed navigation belongs to an old session +- **GIVEN** a recreated-app or Next navigation timer scheduled in session A +- **WHEN** the account session changes before the delay ends +- **THEN** it does not navigate over the current account's screen diff --git a/openspec/changes/reconcile-r5-release-baseline/tasks.md b/openspec/changes/reconcile-r5-release-baseline/tasks.md index b353cee10f..22f155a8d7 100644 --- a/openspec/changes/reconcile-r5-release-baseline/tasks.md +++ b/openspec/changes/reconcile-r5-release-baseline/tasks.md @@ -1,20 +1,27 @@ ## 1. UI Components -- [ ] 1.1 Preserve existing components, confirmation/cancel/privacy and helper copy against released source. +- [x] 1.1 Preserve existing components, confirmation/cancel/privacy and helper copy against released source. ## 2. Business Logic / Hooks -- [ ] 2.1 Capture and validate deletion origin before selection/confirmation, including A → B → A. +- [x] 2.1 Capture and validate deletion origin before selection/confirmation, including A → B → A. ## 3. Store (Slice + Saga) -- [ ] 3.1 Carry original app/session on delete/archive actions; retain R4 workers/middleware unchanged. +- [x] 3.1 Carry original app/session on delete/archive actions; retain R4 workers/middleware unchanged. ## 4. Storage -- [ ] 4.1 Verify no schema/MMKV/store-shape change; no Redux Persist migration bump needed. +- [x] 4.1 Verify no schema/MMKV/store-shape change; no Redux Persist migration bump needed. ## 5. Tests -- [ ] 5.1 Add stale account/session/Realm confirmation and retained-card regressions, with failing controls. -- [ ] 5.2 Run frozen setup/patches, affected Jest/recovery/security/quality and baseline comparison. -- [ ] 5.3 Record exact baseline, R5-only diff, results and unresolved native/genuine gaps. +- [x] 5.1 Add stale account/session/Realm confirmation and retained-card regressions, with failing controls. +- [x] 5.2 Run frozen setup/patches, affected Jest/recovery/security/quality and baseline comparison. +- [x] 5.3 Record exact baseline, R5-only diff, results and unresolved native/genuine gaps. ## 6. E2E Flows (Maestro) -- [ ] 6.1 Preserve login flow, document finite deletion/account-switch/native measurements. +- [x] 6.1 Preserve login flow, document finite deletion/account-switch/native measurements. - [ ] 6.2 Execute relevant IOS/ANDROID/HARDWARE scenarios when available; retain NOT RUN otherwise. + +Native task6.2 remains NOT RUN; no archive or acceptance claim. Ruby-dependent +local checks remain unavailable and are explicitly retained in the validation report. + +## 7. Existing startup callback ownership +- [ ] 7.1 Reproduce pending-token/delayed-navigation account-session crossings. +- [ ] 7.2 Guard effects/timers by their captured session and rerun focused/full Jest. diff --git a/src/screens/Home/components/Keys/ManageKeys.tsx b/src/screens/Home/components/Keys/ManageKeys.tsx index 8af3c99820..75d4035aca 100644 --- a/src/screens/Home/components/Keys/ManageKeys.tsx +++ b/src/screens/Home/components/Keys/ManageKeys.tsx @@ -7,7 +7,7 @@ import ToastErrorIcon from 'src/assets/images/toast_error.svg'; import KeeperModal from 'src/components/KeeperModal'; import { LocalizationContext } from 'src/context/Localization/LocContext'; import SignerContent from './SignerContent'; -import { useFocusEffect, useNavigation } from '@react-navigation/native'; +import { CommonActions, useFocusEffect, useNavigation } from '@react-navigation/native'; import SignerList from './SignerList'; import KeyAddedModal from 'src/components/KeyAddedModal'; @@ -110,7 +110,7 @@ const ManageKeys = ({ addedSigner }) => { navigation.navigate('DeleteKeys')} + onPress={() => navigation.dispatch(CommonActions.navigate('DeleteKeys'))} style={{ paddingVertical: hp(12) }} > {translations.signer.manageKeys} diff --git a/src/screens/SigningDevices/DeleteKeys.tsx b/src/screens/SigningDevices/DeleteKeys.tsx index 3a5818b0e2..77a1bb859e 100644 --- a/src/screens/SigningDevices/DeleteKeys.tsx +++ b/src/screens/SigningDevices/DeleteKeys.tsx @@ -6,7 +6,6 @@ import PasscodeVerifyModal from 'src/components/Modal/PasscodeVerify'; import KeeperModal from 'src/components/KeeperModal'; import useSigners from 'src/hooks/useSigners'; import { StyleSheet, ScrollView } from 'react-native'; -import { SDIcons } from '../Vault/SigningDeviceIcons'; import Text from 'src/components/KeeperText'; import { getSignerDescription, getSignerNameFromType } from 'src/hardware'; import DeleteIcon from 'src/assets/images/delete_bin.svg'; @@ -40,8 +39,25 @@ import HideWalletIcon from 'src/assets/images/hide_wallet.svg'; import useToastMessage from 'src/hooks/useToastMessage'; import TickIcon from 'src/assets/images/icon_tick.svg'; import usePlan from 'src/hooks/usePlan'; +import { store } from 'src/store/store'; +import dbManager from 'src/storage/realm/dbManager'; +import { currentBackupAccountSession } from 'src/services/backup/transport'; +import { SDIcons } from '../Vault/SigningDeviceIcons'; -type DeletionIntent = { signer: Signer }; +type DeletionOrigin = { appId: string; accountSession: number }; +type DeletionIntent = DeletionOrigin & { signer: Signer }; + +const isCurrentDeletionOrigin = (origin: DeletionOrigin) => { + if ( + !origin.appId || store.getState().storage.appId !== origin.appId || + currentBackupAccountSession() !== origin.accountSession + ) return false; + try { + return (dbManager.getObjectByIndex(RealmSchema.KeeperApp) as { id?: string } | undefined)?.id === origin.appId; + } catch { + return false; + } +}; function DeleteKeys({ route }) { const { colorMode } = useColorMode(); @@ -62,6 +78,8 @@ function DeleteKeys({ route }) { ); const dispatch = useDispatch(); + const renderedAppId = useAppSelector((state) => state.storage.appId); + const renderedSession = currentBackupAccountSession(); const navigation = useNavigation(); const [unhidingKeyUID, setUnhidingKeyUID] = useState(''); // Deletion safety must include hidden/canary wallets and both networks. @@ -131,6 +149,8 @@ function DeleteKeys({ route }) { // Consume synchronously: replayed/cancelled callbacks cannot reuse this // authorization or clear a later selection before React rerenders. cancelDeletion(intent); + // Dispatch-time origin stamping must not rebind an old confirmation. + if (!isCurrentDeletionOrigin(intent)) return; const { signer } = intent; // Read live Results at invocation; a retained callback must not use a // render-time snapshot from before a new active or archived reference. @@ -142,9 +162,15 @@ function DeleteKeys({ route }) { return; } if (references.some((vault) => vault.archived)) { - dispatch(archiveSigningDevice([signer])); + dispatch({ + ...archiveSigningDevice([signer]), originAppId: intent.appId, + originBackupAccountSession: intent.accountSession, + }); } else { - dispatch(deleteSigningDevice([signer])); + dispatch({ + ...deleteSigningDevice([signer]), originAppId: intent.appId, + originBackupAccountSession: intent.accountSession, + }); } setDeletedSigner(signer); } @@ -182,6 +208,8 @@ function DeleteKeys({ route }) { .filter((vault) => vault.signers.some((s) => getKeyUID(s) === getKeyUID(signer))); const handleDelete = (signer: Signer) => { + const origin = { appId: renderedAppId, accountSession: renderedSession }; + if (!isCurrentDeletionOrigin(origin)) return; pendingDeletion.current = null; setDeletionIntent(null); setHideWarning(false); @@ -193,7 +221,7 @@ function DeleteKeys({ route }) { setConfirmPassVisible(false); return; } - const intent = { signer }; + const intent = { ...origin, signer }; pendingDeletion.current = intent; setDeletionIntent(intent); setConfirmPassVisible(true); diff --git a/tests/screens/DeleteKeys.test.tsx b/tests/screens/DeleteKeys.test.tsx index b2a264c799..c925976cf5 100644 --- a/tests/screens/DeleteKeys.test.tsx +++ b/tests/screens/DeleteKeys.test.tsx @@ -17,7 +17,15 @@ let mockVaults: any[] = []; // Realm Results stay live even inside a previously rendered callback. const mockVaultQuery = { map: (callback) => mockVaults.map(callback) }; const mockFocus: { effect?: () => () => void; cleanup?: () => void } = {}; -const mockState = { bhr: {} }; +const mockState = { bhr: {}, storage: { appId: 'disposable-account-A' } }; +let mockRealmAppId = 'disposable-account-A'; +let mockRealmUnavailable = false; +jest.mock('src/store/store', () => ({ store: { getState: () => mockState } })); +jest.mock('src/services/rest/RestClient', () => ({ __esModule: true, default: {} })); +const { + currentBackupAccountSession, + observeBackupAccount, +} = require('../../src/services/backup/transport'); jest.mock('react-redux', () => ({ useDispatch: () => mockDispatch })); jest.mock('@realm/react', () => ({ useQuery: () => mockVaultQuery })); jest.mock('src/storage/realm/utils', () => ({ getJSONFromRealmObject: (v) => v })); @@ -31,12 +39,26 @@ jest.mock('src/utils/utilities', () => ({ })); // These imports belong to unrelated utilities; identity uses their real enum // constants and derivation/account logic without invoking storage or crypto. -jest.mock('src/utils/service-utilities/config', () => ({ __esModule: true, default: {}, APP_STAGE: {} })); +jest.mock('src/utils/service-utilities/config', () => ({ + __esModule: true, + default: {}, + APP_STAGE: {}, +})); jest.mock('src/constants/Bitcoin', () => ({ isTestnet: jest.fn() })); jest.mock('src/services/qr/bbqr/split', () => ({})); -jest.mock('src/storage/realm/dbManager', () => ({})); +jest.mock('src/storage/realm/dbManager', () => ({ + __esModule: true, + default: { + getObjectByIndex: () => { + if (mockRealmUnavailable) throw new Error('synthetic closed Realm'); + return mockRealmAppId ? { id: mockRealmAppId } : undefined; + }, + }, +})); jest.mock('src/utils/service-utilities/encryption', () => ({})); -jest.mock('src/constants/defaultData', () => ({ RECOVERY_KEY_SIGNER_NAME: 'RECOVERY_KEY_SIGNER_NAME' })); +jest.mock('src/constants/defaultData', () => ({ + RECOVERY_KEY_SIGNER_NAME: 'RECOVERY_KEY_SIGNER_NAME', +})); jest.mock('src/hooks/usePlan', () => ({ __esModule: true, default: () => ({}) })); jest.mock('src/hooks/useToastMessage', () => ({ __esModule: true, @@ -58,10 +80,6 @@ jest.mock('src/hardware', () => ({ getSignerNameFromType: () => 'Key', })); jest.mock('src/screens/Vault/SigningDeviceIcons', () => ({ SDIcons: () => ({ Icon: null }) })); -jest.mock('src/store/sagaActions/vaults', () => ({ - deleteSigningDevice: (signers) => ({ type: 'DELETE', signers }), - archiveSigningDevice: (signers) => ({ type: 'ARCHIVE', signers }), -})); jest.mock('src/store/sagaActions/wallets', () => ({ updateSignerDetails: jest.fn() })); jest.mock('src/store/reducers/bhr', () => ({ hideDeletingKeyModal: jest.fn(), @@ -128,6 +146,19 @@ function verifyPasscode() { const { onSuccess } = passcodeCallbacks(); act(() => onSuccess()); } +function expectedAction(type, signer) { + return { + type, + payload: { signers: [signer] }, + originAppId: mockState.storage.appId, + originBackupAccountSession: currentBackupAccountSession(), + }; +} +function switchAccount(appId) { + mockState.storage.appId = appId; + mockRealmAppId = appId; + observeBackupAccount(appId); +} function warningContent() { const warning = screen.root .findAllByType('Modal') @@ -142,6 +173,11 @@ function warningContent() { return JSON.stringify(output); } beforeEach(() => { + mockState.storage.appId = 'disposable-account-A'; + mockRealmAppId = mockState.storage.appId; + mockRealmUnavailable = false; + observeBackupAccount(undefined); + observeBackupAccount(mockState.storage.appId); mockFocus.effect = undefined; mockFocus.cleanup = undefined; mockSigners = [mockSigner]; @@ -162,7 +198,7 @@ test('visible keys offer Delete without first hiding; passcode is required', () expect(mockDispatch).not.toHaveBeenCalled(); expect(modal('enterPasscode').props.visible).toBe(true); verifyPasscode(); - expect(mockDispatch).toHaveBeenCalledWith({ type: 'DELETE', signers: [mockSigner] }); + expect(mockDispatch).toHaveBeenCalledWith(expectedAction('DELETE_SIGINING_DEVICE', mockSigner)); }); test.each(['normal', 'hidden', 'canary', 'other-network'])( @@ -192,7 +228,7 @@ test('retains a key used by an archived wallet instead of deleting it', () => { mount(); chooseDelete(); verifyPasscode(); - expect(mockDispatch).toHaveBeenCalledWith({ type: 'ARCHIVE', signers: [mockSigner] }); + expect(mockDispatch).toHaveBeenCalledWith(expectedAction('ARCHIVE_SIGINING_DEVICE', mockSigner)); }); test('cancel clears deletion intent', () => { @@ -248,7 +284,7 @@ test('an authenticated hidden key still needs its own deletion confirmation', () expect(modal('enterPasscode').props.visible).toBe(true); expect(mockDispatch).not.toHaveBeenCalled(); verifyPasscode(); - expect(mockDispatch).toHaveBeenCalledWith({ type: 'DELETE', signers: [mockSigner] }); + expect(mockDispatch).toHaveBeenCalledWith(expectedAction('DELETE_SIGINING_DEVICE', mockSigner)); }); test('repeated confirmation does not dispatch a second deletion', () => { @@ -270,17 +306,20 @@ test('the same passcode success callback can consume a deletion only once', () = expect(mockDispatch).toHaveBeenCalledTimes(1); }); -test.each(['modal', 'passcode'])('cancel through %s invalidates a captured success callback', (via) => { - mount(); - chooseDelete(); - const callbacks = passcodeCallbacks(); - act(() => { - if (via === 'modal') modal('enterPasscode').props.close(); - else callbacks.close(); - }); - act(() => callbacks.onSuccess()); - expect(mockDispatch).not.toHaveBeenCalled(); -}); +test.each(['modal', 'passcode'])( + 'cancel through %s invalidates a captured success callback', + (via) => { + mount(); + chooseDelete(); + const callbacks = passcodeCallbacks(); + act(() => { + if (via === 'modal') modal('enterPasscode').props.close(); + else callbacks.close(); + }); + act(() => callbacks.onSuccess()); + expect(mockDispatch).not.toHaveBeenCalled(); + } +); test('success from a previous key cannot authorize or cancel the next key selection', () => { const secondSigner = { ...mockSigner, masterFingerprint: 'ef01' }; @@ -299,7 +338,7 @@ test('success from a previous key cannot authorize or cancel the next key select expect(modal('enterPasscode').props.visible).toBe(true); act(() => second.onSuccess()); expect(mockDispatch).toHaveBeenCalledTimes(1); - expect(mockDispatch).toHaveBeenCalledWith({ type: 'DELETE', signers: [secondSigner] }); + expect(mockDispatch).toHaveBeenCalledWith(expectedAction('DELETE_SIGINING_DEVICE', secondSigner)); }); test('success from a cancelled request cannot authorize a new request for the same key', () => { @@ -376,28 +415,37 @@ test('a captured success callback retains a reference archived while authenticat act(() => screen.update()); act(() => onSuccess()); expect(mockDispatch).toHaveBeenCalledTimes(1); - expect(mockDispatch).toHaveBeenCalledWith({ type: 'ARCHIVE', signers: [mockSigner] }); + expect(mockDispatch).toHaveBeenCalledWith(expectedAction('ARCHIVE_SIGINING_DEVICE', mockSigner)); }); test.each([ ['hidden', 'DEFAULT', 'HIDDEN'], ['canary', 'CANARY', 'DEFAULT'], ['unknown visibility', 'DEFAULT', undefined], -])('blocks a %s reference without revealing its wallet name or description', (_, type, visibility) => { - mockVaults = [{ - id: 'concealed-reference', - signers: [mockSigner], - archived: false, - type, - presentationData: { name: 'PRIVATE_WALLET_NAME', description: 'PRIVATE_WALLET_DESCRIPTION', visibility }, - }]; - mount(); - chooseDelete(); - expect(modal('deleteVaultWarning').props.visible).toBe(true); - expect(modal('enterPasscode').props.visible).toBe(false); - expect(warningContent()).not.toContain('PRIVATE_WALLET'); - expect(mockDispatch).not.toHaveBeenCalled(); -}); +])( + 'blocks a %s reference without revealing its wallet name or description', + (_, type, visibility) => { + mockVaults = [ + { + id: 'concealed-reference', + signers: [mockSigner], + archived: false, + type, + presentationData: { + name: 'PRIVATE_WALLET_NAME', + description: 'PRIVATE_WALLET_DESCRIPTION', + visibility, + }, + }, + ]; + mount(); + chooseDelete(); + expect(modal('deleteVaultWarning').props.visible).toBe(true); + expect(modal('enterPasscode').props.visible).toBe(false); + expect(warningContent()).not.toContain('PRIVATE_WALLET'); + expect(mockDispatch).not.toHaveBeenCalled(); + } +); test('a mix of visible and concealed references uses a generic warning without wallet details', () => { mockVaults = ['DEFAULT', 'HIDDEN'].map((visibility, index) => ({ @@ -405,7 +453,11 @@ test('a mix of visible and concealed references uses a generic warning without w signers: [mockSigner], archived: false, type: 'DEFAULT', - presentationData: { name: 'PRIVATE_WALLET_NAME', description: 'PRIVATE_WALLET_DESCRIPTION', visibility }, + presentationData: { + name: 'PRIVATE_WALLET_NAME', + description: 'PRIVATE_WALLET_DESCRIPTION', + visibility, + }, })); mount(); chooseDelete(); @@ -416,13 +468,19 @@ test('a mix of visible and concealed references uses a generic warning without w }); test('ordinary visible references still show the wallet that blocks deletion', () => { - mockVaults = [{ - id: 'visible-vault', - signers: [mockSigner], - archived: false, - type: 'DEFAULT', - presentationData: { name: 'Visible wallet', description: 'Visible description', visibility: 'DEFAULT' }, - }]; + mockVaults = [ + { + id: 'visible-vault', + signers: [mockSigner], + archived: false, + type: 'DEFAULT', + presentationData: { + name: 'Visible wallet', + description: 'Visible description', + visibility: 'DEFAULT', + }, + }, + ]; mount(); chooseDelete(); expect(warningContent()).toContain('Visible wallet'); @@ -431,11 +489,15 @@ test('ordinary visible references still show the wallet that blocks deletion', ( }); test('production identity matches a vault key with the selected signer account and network', () => { - mockVaults = [{ - id: 'identity-reference', - archived: false, - signers: [{ masterFingerprint: mockSigner.masterFingerprint, derivationPath: "m/48'/1'/0'/2'" }], - }]; + mockVaults = [ + { + id: 'identity-reference', + archived: false, + signers: [ + { masterFingerprint: mockSigner.masterFingerprint, derivationPath: "m/48'/1'/0'/2'" }, + ], + }, + ]; mount(); chooseDelete(); expect(modal('deleteVaultWarning').props.visible).toBe(true); @@ -445,59 +507,87 @@ test('production identity matches a vault key with the selected signer account a test.each([ ['account', "m/48'/1'/1'/2'"], ['network', "m/48'/0'/0'/2'"], -])('a shared fingerprint with another %s is not the selected signer identity', (_, derivationPath) => { - mockVaults = [{ - id: 'other-identity-reference', - archived: false, - signers: [{ masterFingerprint: mockSigner.masterFingerprint, derivationPath }], - }]; - mount(); - chooseDelete(); - expect(modal('enterPasscode').props.visible).toBe(true); - verifyPasscode(); - expect(mockDispatch).toHaveBeenCalledWith({ type: 'DELETE', signers: [mockSigner] }); -}); +])( + 'a shared fingerprint with another %s is not the selected signer identity', + (_, derivationPath) => { + mockVaults = [ + { + id: 'other-identity-reference', + archived: false, + signers: [{ masterFingerprint: mockSigner.masterFingerprint, derivationPath }], + }, + ]; + mount(); + chooseDelete(); + expect(modal('enterPasscode').props.visible).toBe(true); + verifyPasscode(); + expect(mockDispatch).toHaveBeenCalledWith(expectedAction('DELETE_SIGINING_DEVICE', mockSigner)); + } +); test.each([ ['hidden', 'DEFAULT', 'HIDDEN'], ['canary', 'CANARY', 'DEFAULT'], -])('a wallet becoming %s while the warning is open immediately conceals its details', (_, type, visibility) => { - mockVaults = [{ - id: 'live-reference', - signers: [mockSigner], - archived: false, - type: 'DEFAULT', - presentationData: { name: 'BECAME_PRIVATE_NAME', description: 'BECAME_PRIVATE_DESCRIPTION', visibility: 'DEFAULT' }, - }]; - mount(); - chooseDelete(); - expect(warningContent()).toContain('BECAME_PRIVATE_NAME'); - mockVaults = [{ - ...mockVaults[0], - type, - presentationData: { ...mockVaults[0].presentationData, visibility }, - }]; - act(() => screen.update()); - expect(modal('deleteVaultWarning').props.visible).toBe(true); - expect(modal('deleteVaultWarning').props.subTitle).toBe('deleteVaultInstruction'); - expect(warningContent()).not.toContain('BECAME_PRIVATE'); - expect(mockDispatch).not.toHaveBeenCalled(); -}); +])( + 'a wallet becoming %s while the warning is open immediately conceals its details', + (_, type, visibility) => { + mockVaults = [ + { + id: 'live-reference', + signers: [mockSigner], + archived: false, + type: 'DEFAULT', + presentationData: { + name: 'BECAME_PRIVATE_NAME', + description: 'BECAME_PRIVATE_DESCRIPTION', + visibility: 'DEFAULT', + }, + }, + ]; + mount(); + chooseDelete(); + expect(warningContent()).toContain('BECAME_PRIVATE_NAME'); + mockVaults = [ + { + ...mockVaults[0], + type, + presentationData: { ...mockVaults[0].presentationData, visibility }, + }, + ]; + act(() => screen.update()); + expect(modal('deleteVaultWarning').props.visible).toBe(true); + expect(modal('deleteVaultWarning').props.subTitle).toBe('deleteVaultInstruction'); + expect(warningContent()).not.toContain('BECAME_PRIVATE'); + expect(mockDispatch).not.toHaveBeenCalled(); + } +); test('warning cards use current wallet metadata rather than an old snapshot', () => { - mockVaults = [{ - id: 'renamed-reference', - signers: [mockSigner], - archived: false, - type: 'DEFAULT', - presentationData: { name: 'Old wallet name', description: 'Old description', visibility: 'DEFAULT' }, - }]; + mockVaults = [ + { + id: 'renamed-reference', + signers: [mockSigner], + archived: false, + type: 'DEFAULT', + presentationData: { + name: 'Old wallet name', + description: 'Old description', + visibility: 'DEFAULT', + }, + }, + ]; mount(); chooseDelete(); - mockVaults = [{ - ...mockVaults[0], - presentationData: { ...mockVaults[0].presentationData, name: 'Current wallet name', description: 'Current description' }, - }]; + mockVaults = [ + { + ...mockVaults[0], + presentationData: { + ...mockVaults[0].presentationData, + name: 'Current wallet name', + description: 'Current description', + }, + }, + ]; act(() => screen.update()); const output = warningContent(); expect(output).toContain('Current wallet name'); @@ -506,29 +596,44 @@ test('warning cards use current wallet metadata rather than an old snapshot', () expect(output).not.toContain('Old description'); }); -test.each([undefined, ''])('a missing or empty reference ID (%s) suppresses wallet details', (id) => { - mockVaults = [{ - id, - signers: [mockSigner], - archived: false, - type: 'DEFAULT', - presentationData: { name: 'UNRESOLVED_PRIVATE_NAME', description: 'UNRESOLVED_PRIVATE_DESCRIPTION', visibility: 'DEFAULT' }, - }]; - mount(); - chooseDelete(); - expect(modal('deleteVaultWarning').props.visible).toBe(true); - expect(warningContent()).not.toContain('UNRESOLVED_PRIVATE'); - expect(mockDispatch).not.toHaveBeenCalled(); -}); +test.each([undefined, ''])( + 'a missing or empty reference ID (%s) suppresses wallet details', + (id) => { + mockVaults = [ + { + id, + signers: [mockSigner], + archived: false, + type: 'DEFAULT', + presentationData: { + name: 'UNRESOLVED_PRIVATE_NAME', + description: 'UNRESOLVED_PRIVATE_DESCRIPTION', + visibility: 'DEFAULT', + }, + }, + ]; + mount(); + chooseDelete(); + expect(modal('deleteVaultWarning').props.visible).toBe(true); + expect(warningContent()).not.toContain('UNRESOLVED_PRIVATE'); + expect(mockDispatch).not.toHaveBeenCalled(); + } +); test('a missing live reference keeps a generic block until a new Delete selection', () => { - mockVaults = [{ - id: 'removed-reference', - signers: [mockSigner], - archived: false, - type: 'DEFAULT', - presentationData: { name: 'REMOVED_PRIVATE_NAME', description: 'REMOVED_PRIVATE_DESCRIPTION', visibility: 'DEFAULT' }, - }]; + mockVaults = [ + { + id: 'removed-reference', + signers: [mockSigner], + archived: false, + type: 'DEFAULT', + presentationData: { + name: 'REMOVED_PRIVATE_NAME', + description: 'REMOVED_PRIVATE_DESCRIPTION', + visibility: 'DEFAULT', + }, + }, + ]; mount(); chooseDelete(); mockVaults = []; @@ -562,47 +667,125 @@ test('blur and return do not restore a previous wallet-reference warning', () => test.each([ ['hidden', 'DEFAULT', 'HIDDEN'], ['canary', 'CANARY', 'DEFAULT'], -])('a new %s reference switches an open warning to generic without exposing counts', (_, type, visibility) => { - mockVaults = [{ - id: 'initial-visible-reference', - signers: [mockSigner], - archived: false, - type: 'DEFAULT', - presentationData: { name: 'Visible wallet', visibility: 'DEFAULT' }, - }]; +])( + 'a new %s reference switches an open warning to generic without exposing counts', + (_, type, visibility) => { + mockVaults = [ + { + id: 'initial-visible-reference', + signers: [mockSigner], + archived: false, + type: 'DEFAULT', + presentationData: { name: 'Visible wallet', visibility: 'DEFAULT' }, + }, + ]; + mount(); + chooseDelete(); + mockVaults = [ + ...mockVaults, + { + id: 'new-concealed-reference', + signers: [mockSigner], + archived: false, + type, + presentationData: { + name: 'NEW_PRIVATE_NAME', + description: 'NEW_PRIVATE_DESCRIPTION', + visibility, + }, + }, + ]; + act(() => screen.update()); + expect(modal('deleteVaultWarning').props.visible).toBe(true); + expect(modal('deleteVaultWarning').props.subTitle).toBe('deleteVaultInstruction'); + expect(warningContent()).toBe('null'); + expect(mockDispatch).not.toHaveBeenCalled(); + } +); + +test('a reference ID reassigned to an unrelated key does not reveal the unrelated wallet', () => { + mockVaults = [ + { + id: 'same-id', + signers: [mockSigner], + archived: false, + type: 'DEFAULT', + presentationData: { name: 'PREVIOUS_NAME', visibility: 'DEFAULT' }, + }, + ]; mount(); chooseDelete(); - mockVaults = [...mockVaults, { - id: 'new-concealed-reference', - signers: [mockSigner], - archived: false, - type, - presentationData: { name: 'NEW_PRIVATE_NAME', description: 'NEW_PRIVATE_DESCRIPTION', visibility }, - }]; + mockVaults = [ + { + ...mockVaults[0], + signers: [{ ...mockSigner, masterFingerprint: 'ef01' }], + presentationData: { name: 'UNRELATED_NAME', visibility: 'DEFAULT' }, + }, + ]; act(() => screen.update()); expect(modal('deleteVaultWarning').props.visible).toBe(true); - expect(modal('deleteVaultWarning').props.subTitle).toBe('deleteVaultInstruction'); expect(warningContent()).toBe('null'); expect(mockDispatch).not.toHaveBeenCalled(); }); -test('a reference ID reassigned to an unrelated key does not reveal the unrelated wallet', () => { - mockVaults = [{ - id: 'same-id', - signers: [mockSigner], - archived: false, - type: 'DEFAULT', - presentationData: { name: 'PREVIOUS_NAME', visibility: 'DEFAULT' }, - }]; +test.each(['delete', 'archive'])( + 'account switch before confirmation cannot dispatch %s', + (kind) => { + if (kind === 'archive') mockVaults = [{ signers: [mockSigner], archived: true }]; + mount(); + chooseDelete(); + const old = passcodeCallbacks(); + switchAccount('disposable-account-B'); + act(() => old.onSuccess()); + expect(mockDispatch).not.toHaveBeenCalled(); + } +); + +test.each(['delete', 'archive'])( + 'account round trip before confirmation cannot dispatch %s', + (kind) => { + if (kind === 'archive') mockVaults = [{ signers: [mockSigner], archived: true }]; + mount(); + chooseDelete(); + const old = passcodeCallbacks(); + switchAccount('disposable-account-B'); + switchAccount('disposable-account-A'); + act(() => old.onSuccess()); + expect(mockDispatch).not.toHaveBeenCalled(); + } +); + +test.each(['different', 'missing', 'unavailable'])( + 'confirmation fails closed with %s Realm owner', + (kind) => { + mount(); + chooseDelete(); + const old = passcodeCallbacks(); + mockRealmAppId = kind === 'different' ? 'disposable-account-B' : ''; + mockRealmUnavailable = kind === 'unavailable'; + act(() => old.onSuccess()); + expect(mockDispatch).not.toHaveBeenCalled(); + } +); + +test.each(['different', 'missing', 'unavailable'])( + 'selection fails closed with %s Realm owner', + (kind) => { + mount(); + mockRealmAppId = kind === 'different' ? 'disposable-account-B' : ''; + mockRealmUnavailable = kind === 'unavailable'; + chooseDelete(); + expect(modal('enterPasscode').props.visible).toBe(false); + expect(mockDispatch).not.toHaveBeenCalled(); + } +); + +test('retained selection callback cannot authorize after an account round trip', () => { mount(); - chooseDelete(); - mockVaults = [{ - ...mockVaults[0], - signers: [{ ...mockSigner, masterFingerprint: 'ef01' }], - presentationData: { name: 'UNRELATED_NAME', visibility: 'DEFAULT' }, - }]; - act(() => screen.update()); - expect(modal('deleteVaultWarning').props.visible).toBe(true); - expect(warningContent()).toBe('null'); + const oldSelect = find('KeyCard').props.primaryAction; + switchAccount('disposable-account-B'); + switchAccount('disposable-account-A'); + act(() => oldSelect()); + expect(modal('enterPasscode').props.visible).toBe(false); expect(mockDispatch).not.toHaveBeenCalled(); }); From 77b72da29ac5f2cdf3f51c9a0e9f3fd1c2af7238 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Thu, 22 Oct 2026 04:23:01 +0000 Subject: [PATCH 210/254] fix(r5): discard startup callbacks from earlier account sessions --- docs/r5/R4-RECONCILIATION.md | 16 +++- docs/r5/STARTUP-INVESTIGATION.md | 10 ++- docs/r5/WORKSTREAM.md | 25 +++--- .../reconcile-r5-release-baseline/tasks.md | 4 +- .../NewKeeperAppScreen/NewKeeperAppScreen.tsx | 23 ++++-- tests/screens/NewKeeperApp.test.tsx | 78 ++++++++++++++++++- 6 files changed, 133 insertions(+), 23 deletions(-) diff --git a/docs/r5/R4-RECONCILIATION.md b/docs/r5/R4-RECONCILIATION.md index b640146ed8..41e7715afa 100644 --- a/docs/r5/R4-RECONCILIATION.md +++ b/docs/r5/R4-RECONCILIATION.md @@ -39,6 +39,10 @@ range). This inherited history is not new R5 scope or permission to merge R4. - R5's Keys entry uses the released navigation dispatch pattern; this removes the TypeScript overload mismatch exposed by integration. No shared PIN/navigation implementation, transaction formatting or timelock calculation changed. +- Six additional startup regressions reproduced token completion and delayed + navigation crossing an A → B → A session change. The existing effects/timers + now retain their originating session, including a retained Next callback. + Setup and notification workers are unchanged; native persistence remains unrun. - Emergency reset uses its Emergency-specific helper; Inheritance retains its own helper. Native display checks remain open. - Existing R5 frozen JavaScript CI preparation is retained. Its recovery lane now @@ -57,13 +61,16 @@ Node22.23.3 / Yarn1.22.22: - Frozen install PASS; all six explicit dependency patches PASS. Released package/lock/Pod/native/version input-immutability checks PASS. -- Full Jest **47 suites /531 tests PASS**, zero failures/skips, including the - released loopback Electrum fixtures. R5-only focused run **5 suites /99 tests - PASS**, including53 deletion cases; final affected UI rerun **71 PASS**. +- Final full Jest **47 suites /538 tests PASS**, zero failures/skips, including + the released loopback Electrum fixtures. Final R5-focused run **5 suites /106 + tests PASS**, including 53 deletion and 20 first-use screen cases. - New guard controls before implementation: **11 assertion failures**, with the other42 deletion tests deliberately deselected (not execution passes). All53 deletion tests pass after the fix. Real account-round-trip session tracking is used; this does not prove actual native authentication/Realm provenance. +- Startup session controls before implementation: **6 assertion failures /14 + passes**. All 20 pass after the fix, including original-session navigation and + the existing retry, duplicate-attempt and unmount cases. - Full recovery **480/480 PASS**, zero skips. Initial execution had470 passes and one file-load failure because the Realm N-API binary was absent. The exact released binary was installed with prebuild-install; the10 encrypted Realm @@ -86,6 +93,9 @@ Node22.23.3 / Yarn1.22.22: The exact final SHA and hosted checks are recorded in the PR handoff. A full app-native build/install/UI, performance measurement or physical-device result must not be inferred from any JavaScript or Linux Realm result. +The R5-only changed code/test estimate is approximately 18.1k tokens using changed +characters/4, at the upper edge of the original 13k–18k estimate. Scope remains two +substantial features; no additional investigation fix or infrastructure is added. ## Finite native handoff diff --git a/docs/r5/STARTUP-INVESTIGATION.md b/docs/r5/STARTUP-INVESTIGATION.md index 98c1e2be27..54353806aa 100644 --- a/docs/r5/STARTUP-INVESTIGATION.md +++ b/docs/r5/STARTUP-INVESTIGATION.md @@ -13,7 +13,8 @@ R4 #7045 was an open draft at `1d8a86a7462d935cbd0e28b857934b5718c152fc`. Its Splash, NewKeeperApp and secure-store modules are identical to the original sprint versions. This permits independent source changes without importing R4. R4's changed Login, Home/navigation, transaction views and backup/backend paths -remain untouched. Reconciliation is still required when R4 is released. +remained untouched during that independent checkpoint. Reconciliation with the +owner-confirmed released source is now complete; see R4-RECONCILIATION.md. Other open work includes native-validation #7060, cloud-release #7062, bootstrap #7063, lock-selector #7064, PIN fixes #7052/#7053, reproducibility #7043 and the @@ -48,6 +49,13 @@ changed. No optional Linux setup or Mac connection was awaited or started. disposable fixture PIN remains 1111; the flow accepts old-sprint Next or R4's direct advance and then requires Home. It is a native test specification, YAML-parsed here but NOT executed with Maestro. Shared PIN code was not edited. +5. After release reconciliation, six new tests reproduced pending setup/FCM tokens + and navigation timers crossing an A → B → A account-session change. Existing + effect lifetime checks now also require their captured production session; + Next retains its rendered session before scheduling and when the timer fires. + All 20 first-use screen cases pass. These tests use the real session counter + with explicit account-transition fixtures; workers and account schemas are + unchanged. They do not prove native account-switch persistence or performance. The screen tests replace native Keychain, navigation, animation and UI boundaries; they are not cold-start, warm-start, resume, deep-link or native persistence proof. diff --git a/docs/r5/WORKSTREAM.md b/docs/r5/WORKSTREAM.md index fc4ec16a65..a1b3bdd1d1 100644 --- a/docs/r5/WORKSTREAM.md +++ b/docs/r5/WORKSTREAM.md @@ -8,9 +8,10 @@ The earlier pending-baseline records below are historical and superseded by the ## Authority and budget Owner request and Master Task List, Backlog row 245 (15 October 2026 scope reset), -plus rows 7, 8, 23, 76, 231 and 263. The owner's current request allows independent -implementation now; historical instructions to wait for R4 still apply to work -that depends on R4. PR base is `sprint`; do not import the unmerged R4 candidate. +plus rows 7, 8, 23, 76, 231 and 263. The owner's 22 October confirmation that R4 +2.6.4 is live authorizes reconciliation with its recorded released source on R5 +only. Historical instructions to wait for R4 are superseded. PR base is `sprint`; +do not modify R4 or other workstream branches. Pre-implementation changed code/test estimate (not model context or runtime cost): @@ -55,7 +56,7 @@ This is an initial source checkpoint, not completed R5 or native acceptance. Backend atomic reference enforcement and account/session changes need reconciliation with the released R4 implementation before deletion can be release-ready. -## Remaining bounded work +## Original bounded worklist (current results below) 1. Reconcile with the released R4 source; rerun deletion tests and inspect every delete/archive caller and storage/backup boundary. Include references across @@ -82,8 +83,8 @@ with the released R4 implementation before deletion can be release-ready. ## Later Local resource plan -No Mac is needed for Cloud development. Do not start Local native work while R4 -is being finalized. When the baseline is settled, use a separate safe checkout, +No Mac is needed for Cloud source development. R4 is released and reconciliation +is complete. For later native work, use a separate safe checkout, check available disk (approximately 6 GiB minimum planning threshold; require more if the selected build needs it), run one native build at a time and reuse safe compatible caches/artifacts. Preserve every existing worktree, simulator, AVD user, @@ -281,7 +282,7 @@ dependency, backend/store/worker/version inputs remain unchanged. The one lock conflict resolves to R4's exact lock, which already includes the two R5 selectors. The [current reconciliation and finite native plan](R4-RECONCILIATION.md) records -full Jest47/531 PASS, R5 focused99 PASS, recovery480/480 PASS (including10 real +final full Jest 47/538 PASS, R5 focused 106 PASS, recovery 480/480 PASS (including 10 real Linux Realm cases after installing its missing N-API prerequisite), gate11 PASS, static31 PASS and Node/Python release-tool28/7 PASS. Local Ruby checks remain unavailable/failed-prerequisite, not passed; hosted CI explicitly installs Ruby @@ -296,8 +297,14 @@ No R4 reference/session guard was weakened. New tests execute real session-count identity and action-creator logic with modeled screen/storage boundaries. The Keys entry's dispatch form was adapted to the released navigation types. -The R5-only changed source/test estimate is approximately16.5k tokens (rough -characters/4), within the approved two-feature13k–18k target. Review against the +Six additional startup session-crossing regressions failed before the narrow +effect/timer guards and now pass with the existing cases (20 first-use screen +tests). Pending token callbacks, recreated-app timers and retained Next callbacks +cannot act after an A → B → A round trip. No setup worker or backup policy changed. + +The R5-only changed source/test estimate is approximately 18.1k tokens (rough +characters/4), at the upper edge of the approved two-feature 13k–18k target. Hold +scope here. Review against the released source rather than counting R4's inherited history as new R5 work. Do not silently add hosted-native infrastructure, extra backend work or a third feature. No optional Linux setup or Mac session was awaited. Next is the exact-head finite diff --git a/openspec/changes/reconcile-r5-release-baseline/tasks.md b/openspec/changes/reconcile-r5-release-baseline/tasks.md index 22f155a8d7..a08bdf3051 100644 --- a/openspec/changes/reconcile-r5-release-baseline/tasks.md +++ b/openspec/changes/reconcile-r5-release-baseline/tasks.md @@ -23,5 +23,5 @@ Native task6.2 remains NOT RUN; no archive or acceptance claim. Ruby-dependent local checks remain unavailable and are explicitly retained in the validation report. ## 7. Existing startup callback ownership -- [ ] 7.1 Reproduce pending-token/delayed-navigation account-session crossings. -- [ ] 7.2 Guard effects/timers by their captured session and rerun focused/full Jest. +- [x] 7.1 Reproduce pending-token/delayed-navigation account-session crossings (6 assertion failures /14 passes). +- [x] 7.2 Guard effects/timers by their captured session and rerun focused/full Jest (20 startup, 106 focused, 538 full PASS). diff --git a/src/screens/NewKeeperAppScreen/NewKeeperAppScreen.tsx b/src/screens/NewKeeperAppScreen/NewKeeperAppScreen.tsx index 9b36180e8c..6b2f9b489e 100644 --- a/src/screens/NewKeeperAppScreen/NewKeeperAppScreen.tsx +++ b/src/screens/NewKeeperAppScreen/NewKeeperAppScreen.tsx @@ -29,6 +29,7 @@ import Colors from 'src/theme/Colors'; import { useIsFocused } from '@react-navigation/native'; import Fonts from 'src/constants/Fonts'; import ThemedSvg from 'src/components/ThemedSvg.tsx/ThemedSvg'; +import { currentBackupAccountSession } from 'src/services/backup/transport'; export function Tile({ title, subTitle, onPress, Icon = null, loading = false }) { const { colorMode } = useColorMode(); @@ -116,13 +117,15 @@ function NewKeeperApp({ navigation }: { navigation }) { const isFocused = useIsFocused(); const nextNavigation = useRef | null>(null); const navigationActive = useRef(true); + const renderedAccountSession = currentBackupAccountSession(); const isMultiAccountFlow = !!useAppSelector((state) => state.account).allAccounts.length; useEffect(() => { let active = true; + const accountSession = currentBackupAccountSession(); if (appCreated) { setInitiating(false); - updateFCM(() => active); + updateFCM(() => active && accountSession === currentBackupAccountSession()); } if (appCreationError) { setModalVisible(false); @@ -151,8 +154,11 @@ function NewKeeperApp({ navigation }: { navigation }) { useEffect(() => { if (appRecreated) { + const accountSession = currentBackupAccountSession(); const timer = setTimeout(() => { - navigation.replace('App', { screen: 'Home' }); + if (accountSession === currentBackupAccountSession()) { + navigation.replace('App', { screen: 'Home' }); + } }, 3000); return () => clearTimeout(timer); } @@ -175,9 +181,10 @@ function NewKeeperApp({ navigation }: { navigation }) { useEffect(() => { let active = true; + const accountSession = currentBackupAccountSession(); if (keeperInitiating) { setModalVisible(true); - createNewApp(() => active); + createNewApp(() => active && accountSession === currentBackupAccountSession()); } return () => { active = false; @@ -351,10 +358,16 @@ function NewKeeperApp({ navigation }: { navigation }) { subTitleColor={`${colorMode}.modalSubtitleBlack`} buttonText={appCreated ? common.next : null} buttonCallback={() => { - if (!navigationActive.current || nextNavigation.current !== null) return; + if ( + !navigationActive.current || + nextNavigation.current !== null || + renderedAccountSession !== currentBackupAccountSession() + ) return; setModalVisible(false); nextNavigation.current = setTimeout(() => { - navigation.replace('App', { screen: 'Home' }); + if (renderedAccountSession === currentBackupAccountSession()) { + navigation.replace('App', { screen: 'Home' }); + } }, 500); }} subTitleWidth={wp(300)} diff --git a/tests/screens/NewKeeperApp.test.tsx b/tests/screens/NewKeeperApp.test.tsx index 6df4c8b199..2ea5556bb7 100644 --- a/tests/screens/NewKeeperApp.test.tsx +++ b/tests/screens/NewKeeperApp.test.tsx @@ -5,6 +5,8 @@ import { Provider } from 'react-redux'; import loginReducer, { setAppCreationError } from '../../src/store/reducers/login'; const mockGetToken = jest.fn(); +jest.mock('src/services/rest/RestClient', () => ({ __esModule: true, default: {} })); +const { observeBackupAccount } = require('../../src/services/backup/transport'); jest.mock('src/constants/responsive', () => ({ hp: (v) => v, wp: (v) => v, windowWidth: 400 })); jest.mock('@gluestack-ui/themed-native-base', () => ({ Box: 'Box', @@ -78,15 +80,20 @@ async function mount({ error = false, created = false, recreated = false } = {}) store = configureStore({ reducer: { login: (state = loginState, action) => loginReducer(state, action), - storage: (state = { appCreated: created }, action) => - action.type === 'TEST_APP_CREATED' ? { appCreated: true } : state, + storage: (state = { appCreated: created, appId: 'disposable-account-A' }, action) => { + if (action.type === 'TEST_APP_CREATED') return { ...state, appCreated: true }; + if (action.type === 'TEST_SWITCH_ACCOUNT') return { ...state, appId: action.appId }; + return state; + }, bhr: (state = { appRecreated: recreated }) => state, account: (state = { allAccounts: [] }) => state, }, middleware: (getDefaultMiddleware) => getDefaultMiddleware().concat(() => (next) => (action) => { actions.push(action); - return next(action); + const result = next(action); + observeBackupAccount(store.getState().storage.appId); + return result; }), }); navigation = { replace: jest.fn(), navigate: jest.fn() }; @@ -116,6 +123,8 @@ async function start() { beforeEach(() => { mockGetToken.mockReset().mockResolvedValue('test-token'); + observeBackupAccount(undefined); + observeBackupAccount('disposable-account-A'); }); afterEach(() => { @@ -307,3 +316,66 @@ test('a retained Next callback cannot schedule navigation after unmount', async await act(async () => jest.advanceTimersByTime(1000)); expect(navigation.replace).not.toHaveBeenCalled(); }); + +async function switchAwayAndBack() { + for (const appId of ['disposable-account-B', 'disposable-account-A']) { + await act(async () => store.dispatch({ type: 'TEST_SWITCH_ACCOUNT', appId })); + } +} + +test.each(['resolve', 'reject'])( + 'an account round trip before token %s prevents setup in the replacement session', + async (completion) => { + const token = deferred(); + mockGetToken.mockReturnValue(token.promise); + await mount(); + await start(); + await switchAwayAndBack(); + await act(async () => { + if (completion === 'resolve') token.resolve('earlier-session-token'); + else token.reject(new Error('earlier-session token failure')); + }); + expect(setupActions()).toHaveLength(0); + } +); + +test('an account round trip prevents an earlier successful-app token update', async () => { + const token = deferred(); + mockGetToken.mockReturnValue(token.promise); + await mount({ created: true }); + await switchAwayAndBack(); + await act(async () => token.resolve('earlier-session-token')); + expect(actions.filter((action) => action.type === 'UPDATE_FCM')).toHaveLength(0); +}); + +test('recreated-app navigation runs once in its original session', async () => { + jest.useFakeTimers(); + await mount({ recreated: true }); + await act(async () => jest.advanceTimersByTime(4000)); + expect(navigation.replace).toHaveBeenCalledTimes(1); + expect(navigation.replace).toHaveBeenCalledWith('App', { screen: 'Home' }); +}); + +test('an account round trip cancels pending recreated-app navigation', async () => { + jest.useFakeTimers(); + await mount({ recreated: true }); + await switchAwayAndBack(); + await act(async () => jest.advanceTimersByTime(4000)); + expect(navigation.replace).not.toHaveBeenCalled(); +}); + +test.each(['before', 'after'])( + 'an account round trip %s tapping a retained Next cannot navigate the replacement session', + async (when) => { + jest.useFakeTimers(); + await mount(); + await start(); + await act(async () => store.dispatch({ type: 'TEST_APP_CREATED' })); + const next = visibleDialogs()[0].props.buttonCallback; + if (when === 'before') await switchAwayAndBack(); + act(() => next()); + if (when === 'after') await switchAwayAndBack(); + await act(async () => jest.advanceTimersByTime(1000)); + expect(navigation.replace).not.toHaveBeenCalled(); + } +); From 3c5f8d8ace5c211ed78264b1a460c4e06e060ceb Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Thu, 22 Oct 2026 04:55:09 +0000 Subject: [PATCH 211/254] fix(backup): expose guarded retry and readable protected backup results --- docs/7066-protected-backup-correction.md | 190 +++++++++++ scripts/run-protected-upgrade-regression.cjs | 57 ++++ .../Backup/ProtectedBackupActions.tsx | 42 +++ src/context/Localization/language/en.json | 13 + src/context/Localization/language/es.json | 13 + src/hooks/useSettingKeeper.tsx | 124 +++++-- .../AppSettings/AssistedBackupStatus.tsx | 84 +++-- .../Settings/Component/SettingCard.tsx | 5 +- .../components/Settings/keeperSettings.tsx | 2 + src/services/backup/automatic.ts | 24 +- src/services/backup/repair.ts | 14 +- src/services/backup/transport.ts | 107 +++++- tests/recovery/backupDiagnostic.test.cjs | 236 +++++++++++++ tests/recovery/protectedUpgrade7066.proof.cjs | 160 +++++++++ .../screens/ProtectedBackupControls.test.tsx | 312 ++++++++++++++++++ 15 files changed, 1320 insertions(+), 63 deletions(-) create mode 100644 docs/7066-protected-backup-correction.md create mode 100644 scripts/run-protected-upgrade-regression.cjs create mode 100644 src/components/Backup/ProtectedBackupActions.tsx create mode 100644 tests/recovery/backupDiagnostic.test.cjs create mode 100644 tests/recovery/protectedUpgrade7066.proof.cjs create mode 100644 tests/screens/ProtectedBackupControls.test.tsx diff --git a/docs/7066-protected-backup-correction.md b/docs/7066-protected-backup-correction.md new file mode 100644 index 0000000000..9032b832da --- /dev/null +++ b/docs/7066-protected-backup-correction.md @@ -0,0 +1,190 @@ +# Protected-backup correction — #7066 + +## Source and scope + +This patch uses the owner-designated completed R5 implementation source +`77b72da29ac5f2cdf3f51c9a0e9f3fd1c2af7238` from [PR #7058](https://github.com/KeeperCommunity/bitcoin-keeper/pull/7058). +Live records checked on 22 October 2026 show an open draft with `NEEDS_LOCAL`, +not a publicly released 2.6.5. There is no 2.6.5 GitHub tag/release. The source +still declares 2.6.4 (Android 627, iOS 620); this patch assigns no version. +Store publication/availability has not been checked or inferred. +Its ancestry includes released R4 source +`1d8a86a7462d935cbd0e28b857934b5718c152fc` and `sprint` +`def0d6a90c80287edb4f58ae272cbfc340f00d73`. + +Work runs in the selected **Bitcoin Keeper** managed environment, in a separate +worktree and `codex/7066-protected-backup-correction` branch. Explicit fetch +successfully obtained the R5 commit missing from the previous stale attempt. +Live searches found no other #7066 PR/implementation. R5 and the original +checkout remain unchanged. The PR targets `sprint` per AGENTS.md and links a +patch-only comparison from the exact R5 SHA. + +The provisional code/test estimate was 5k–10k changed tokens; after diagnosis the +estimate is 10k–12k, including workflow/privacy regressions and the supplemental +current-backend witness. This remains one backup correction and its adjacent UI, +with no legacy all-copy purge, protocol redesign, release change, or parked +investigation. + +## Evidence and limits + +The [current Relay production receipt](https://github.com/KeeperCommunity/bitcoin-keeper-relay/pull/158#issuecomment-6321195858) +reports selected source `29597b27eeeb299205bf7d0555db0a90c38cc4be` and 14 hosted +protocol checks passing on 21 October after correcting a storage-readiness +probe. The current app and Relay v3 signing, namespace, projection, revision, +generation, epoch and tombstone contracts agree on inspection. The hosted +checks used a fresh synthetic namespace; they do not establish this upgraded +iPhone's backup state. No production operation is performed for this patch. + +An enabled never-enrolled installation has only a read-only Check Again action +on the baseline status screen. Repeated reads leave that state unchanged and +cannot finish enrollment. Existing accounts can retain automatic-backup consent +while their legacy encrypted copy has never been enrolled into v3. The backend +intentionally does not adopt a legacy copy as a protected backup. The patch +exposes Back Up Now through the existing explicit repair path, preserving its +account/session, consent, conflict, upload acknowledgement and complete readback +guards. Check Again still never enrolls or uploads. Disabled/deleted/conflicting +states do not silently repair or recreate a protected copy. + +The same action is available for the generic unverified state. This is a direct +retry route; it is not evidence that every unverified state was permanently +stuck. For example, after a committed enrollment with a lost reply, a later +read can report different, where the baseline already offered backup. The +failing-before UI controls specifically cover enabled never-enrolled and +unverified states; the supplemental service witness separates those schedules. + +An authenticated, validated never-enrolled result is absence of a protected +namespace, not evidence of deletion. Delete remains non-success in that case; +the UI explains the absence instead of showing the generic uncertain-deletion +message. It neither creates a namespace to delete nor deletes legacy copies. +Authorization, network, invalid/incomplete readback and uncertain mutation +results remain failures. + +The screenshot's generic unverified state does not establish which request +failed. The original phone's cause and corrected behavior remain **unconfirmed**. +The user can reveal the latest bounded diagnostic on the status screen: fixed +operation/stage/category, HTTP error status when available, elapsed milliseconds, +and app version/build. This result exists only in memory for the selected account +session and is cleared on account changes/new operations. It never displays or +persists caught errors, request/response bodies, headers, identifiers, ciphertext +or recovery contents. Do not request raw logs to fill the remaining evidence gap. + +## Targeted native acceptance + +Run on the exact final PR head, with the actual compatible backend selected and +recorded. Use disposable accounts and installations for every destructive step. +Record IOS, ANDROID and HARDWARE results separately, including build/backend +identity and any step not run. JavaScript, Linux Realm and fixture results do +not constitute native or original-phone acceptance. + +1. Create an older-account fixture with synthetic recovery contents and legacy + encrypted server copies. Retain saved automatic-backup consent when upgrading + through 2.6.4 to the patch; use the native app's persisted storage. Include + wallets, keys, a vault, node and label/metadata as applicable. Check the fresh + account separately as a control. +2. Interrupt an enrollment/upload or use the demonstrated failing path. Confirm + enabled and unverified are distinct facts. Check Again only reads. Back Up Now + explicitly retries through the guarded repair, then requires an acknowledged + upload plus authenticated complete matching readback. An uncertain reply must + not claim Verified. A remote recovery conflict must preserve both copies. +3. Reopen/cold launch the same account and recheck. Make an ordinary backup-relevant + metadata change, confirm it reaches the protected copy, and recheck again. +4. Recover the disposable protected backup into a separate disposable installation. + Verify complete expected recovery contents and restored consent remaining off + until the established explicit choice. Never restore over the owner's phone. +5. Turn off automatic backup. Confirm no copy was deleted and status checking + remains read-only. Separately re-enable and verify backup, then delete the + disposable protected copy. Require fresh confirmed tombstone/readback, auto + off, and unchanged local recovery contents. Fresh-read the deleted state and + explicitly recreate; deleted protected records must not resurrect. Older + read-only encrypted copies may remain. +6. Check missing enrollment, authorization failure, interrupted request and + malformed/incomplete readback. No error may become Verified/Deleted. Repeat + while switching A → B → A; stale work and diagnostics must not bind to the new + account/session. Revoke consent while backup runs and check the result. +7. On iOS and Android, check More, backup status and the affected enable/delete + dialogs in light/dark themes, small width (320–360 logical pixels) and larger + system text. Both long actions must wrap, remain fully readable and tappable; + the explanatory copy must be complete. Scroll through persistent feedback + without obscuring bottom navigation. Check retained-copy notice spacing, + progress/disabled actions, diagnostic show/hide and accessibility labels. + +## Original working iPhone — non-destructive only + +Before an approved corrected build is available, capture only version/build from +About and, if available, the user-initiated fixed diagnostic from a normal Check +Again. Share only the bounded fields listed above, never screenshots containing +account details or raw logs. After the approved build/backend pair is available, +use normal Back Up Now and Check Again, then reopen/recheck. The owner must confirm +that successful verification persists. Do not reset, uninstall, restore over, +delete a backup, or export private recovery data on that phone as a diagnostic. + +Keep #7066 open until disposable native acceptance and original-phone confirmation +are complete. No merge, signing, distribution, deployment or publication is +authorized by this patch or by a passing Cloud check. + +## Reproduce the supplemental upgrade witness + +Use Node 22.23.3+, frozen app dependencies with repository patches, the genuine +Realm 12.15.0 Linux/macOS N-API binding, a clean separately obtained Relay checkout +at `29597b27eeeb299205bf7d0555db0a90c38cc4be` with frozen dependencies and +`npm ci --ignore-scripts --prefix tests/proof-tools`, and a disposable loopback +Mongo replica set on port 27038. The wrapper verifies exact source hashes and +driver versions; it leaves the historical proof runner/pins unchanged. + +```sh +KEEPER_PROTECTED_RELAY_CHECKOUT=/absolute/path/to/current-relay \ +BACKUP_TEST_MONGO_URL=mongodb://127.0.0.1:27038 \ +node scripts/run-protected-upgrade-regression.cjs +``` + +The witness uses actual persisted Redux consent, encrypted native Realm, signed +HTTP, current Relay and disposable Mongo: old legacy account → cold rehydration +and upgrade → never-enrolled read/delete non-success → interrupted enrollment +reply → read-only recheck → explicit repair and complete comparison → disable +retains copies → protected deletion/tombstone → explicit recreation. It checks +that legacy and local recovery contents survive. It is a host integration proof, +not native app or original-phone acceptance. + +In this Cloud environment execution is **BLOCKED**: the native Realm binding is +absent and `prebuild-install --runtime napi --target 6` received proxy CONNECT +403 from `static.realm.io`. No cached binding or official GitHub release asset +was available. No network-policy bypass, mock substitute, or passing upgrade +proof is claimed. Syntax and exact-current-source admission/wrong-head rejection +were checked. Native acceptance must complete this witness and the matrix above. + +## Actual Cloud checks + +Node 22.23.3 / Yarn 1.22.22; frozen install and all six repository patches PASS. +Package, lockfile, Pod inputs and version consistency remain unchanged. + +| Check | Actual result | +| --- | --- | +| `npm run test -- --runInBand --coverage=false` | PASS: 48 suites, 550 tests, zero skips/failures | +| `node --test tests/recovery/backupDiagnostic.test.cjs tests/recovery/transport.test.cjs tests/recovery/repair.test.cjs` | PASS: 70 tests, zero skips/failures | +| UI interaction suite | PASS: 12; exact-baseline controls FAIL 2 assertions, 10 deliberately deselected (enabled never-enrolled and unverified backup action absent) | +| Diagnostic baseline controls | FAIL: all 9 before; PASS: all 9 after; these establish diagnostic coverage, not the phone's request cause | +| `npm run test:recovery` | FAIL: 479 pass, one test-file load failure, zero skips; missing Realm binding prevents the 10 encrypted-Realm scenarios from loading | +| Supplemental current-backend upgrade witness | BLOCKED by missing Realm native binding; no passing upgraded-account integration claim | +| Current Relay protected/legacy protocol tests on disposable Mongo | PASS: 45; readiness tests PASS: 7; backend only, not upgraded-app acceptance | +| Whole ESLint | FAIL: 4450 errors / 882 warnings; exact baseline 4451 / 882; normalized zero added, one removed | +| Whole TypeScript | FAIL: 943 diagnostics; exact baseline 943; normalized zero added/removed | +| Focused backup service ESLint | PASS: zero errors/warnings | +| CI USDT/retired-configuration guard command | FAIL: 30 pass / 11 fail, all 11 require unavailable Ruby | +| `npm run test:release` | FAIL at Ruby stage (exit 127); preceding Node 28 and Python 7 tests PASS | +| Local-validation/protected-runner contract tests | PASS: 14 | +| Android static/reproducibility Python tools | PASS: 31 | +| Version, JSON parsing, new proof/runner syntax, whitespace | PASS | +| iOS / Android app build, simulator/emulator, physical phone | NOT RUN; required exact-head native handoff remains NEEDS_LOCAL | + +The current-backend command, run in its separate exact checkout, was: + +```sh +KEEPER_APP_SOURCE=/absolute/path/to/keeper-patch \ +BACKUP_TEST_MONGO_URL=mongodb://127.0.0.1:27038 \ +node --test tests/protectedBackup.test.cjs tests/legacyBackupReadOnly.test.cjs +``` + +The task-owned disposable Mongo container is stopped; its data/evidence are +retained. Passing backend checks do not replace the blocked upgraded-account +witness. Existing formatting/lint/type failures are reported, not waived. No +independent cryptographic certification or release clearance is claimed. diff --git a/scripts/run-protected-upgrade-regression.cjs b/scripts/run-protected-upgrade-regression.cjs new file mode 100644 index 0000000000..b660c09649 --- /dev/null +++ b/scripts/run-protected-upgrade-regression.cjs @@ -0,0 +1,57 @@ +// Supplemental #7066 upgrade regression against the current selected Relay. +// Keep the historical proof runner and its reviewed source pins unchanged. +const assert = require('node:assert/strict'); +const crypto = require('node:crypto'); +const fs = require('node:fs'); +const path = require('node:path'); +const { createRequire } = require('node:module'); +const { spawnSync } = require('node:child_process'); +const historical = require('./run-protected-backup-genuine.cjs'); + +const BACKEND_COMMIT = '29597b27eeeb299205bf7d0555db0a90c38cc4be'; +// Every other fixture input is byte-identical to the historical reviewed pin. +const BACKEND_FILES = Object.freeze({ ...historical.BACKEND_FILES, + 'src/services/protectedBackup.ts': '1487da3778fcc39efc7d453e5ddcbb944d3b8c2912c04632dbb3076f761decd4', +}); + +function checkedCurrentBackend(checkout) { + assert.ok(checkout, 'Set KEEPER_PROTECTED_RELAY_CHECKOUT to the exact current Relay checkout'); + const directory = fs.realpathSync(checkout); + const git = (...args) => spawnSync('git', ['-C', directory, ...args], { encoding: 'utf8' }); + const head = git('rev-parse', 'HEAD'); + assert.equal(head.status, 0, 'The current Relay fixture must be a Git checkout'); + assert.equal(head.stdout.trim(), BACKEND_COMMIT, `Use current Relay ${BACKEND_COMMIT}`); + assert.equal(git('diff', '--quiet', 'HEAD').status, 0, 'Current Relay tracked files must be clean'); + for (const [relative, expected] of Object.entries(BACKEND_FILES)) { + const filename = path.join(directory, relative); + assert.equal(fs.realpathSync(filename), filename, 'Fixed Relay inputs must not be symlink aliases'); + assert.equal(crypto.createHash('sha256').update(fs.readFileSync(filename)).digest('hex'), expected, + `Current Relay input differs: ${relative}`); + } + const requireBackend = createRequire(path.join(directory, 'package.json')); + for (const [name, version] of [['mongoose', '5.13.20'], ['mongodb', '3.7.4']]) + assert.equal(requireBackend(`${name}/package.json`).version, version, 'Use frozen native driver inputs'); + return directory; +} + +// The fixture imports its admission function through this module export. Only +// admission changes: production app/backend code, crypto, HTTP and Realm stay real. +// This opt-in process never updates the historical runner or production config. +historical.checkedBackend = checkedCurrentBackend; + +function main() { + assert.ok(historical.supportedNode(process.versions.node), 'Use Node22.23.3 or a later22.x patch'); + const checkout = checkedCurrentBackend(process.env.KEEPER_PROTECTED_RELAY_CHECKOUT); + const mongo = historical.disposableMongoUrl(process.env.BACKUP_TEST_MONGO_URL); + const app = path.resolve(__dirname, '..'); + const test = path.join(app, 'tests/recovery/protectedUpgrade7066.proof.cjs'); + assert.ok(fs.existsSync(test), 'Missing focused protected-upgrade regression'); + const result = spawnSync(process.execPath, ['--require', __filename, '--test', test], { + cwd: app, stdio: 'inherit', env: { ...process.env, + KEEPER_PROTECTED_RELAY_CHECKOUT: checkout, BACKUP_TEST_MONGO_URL: mongo }, + }); + process.exitCode = result.status ?? 1; +} + +module.exports = { BACKEND_COMMIT, BACKEND_FILES, checkedCurrentBackend }; +if (require.main === module) main(); diff --git a/src/components/Backup/ProtectedBackupActions.tsx b/src/components/Backup/ProtectedBackupActions.tsx new file mode 100644 index 0000000000..035a0a62d5 --- /dev/null +++ b/src/components/Backup/ProtectedBackupActions.tsx @@ -0,0 +1,42 @@ +import React from 'react'; +import { Box, useColorMode } from '@gluestack-ui/themed-native-base'; +import { StyleSheet, TouchableOpacity } from 'react-native'; +import Text from 'src/components/KeeperText'; + +// Backup choices have long, materially different labels. Keep each choice in +// its own wrapping touch target instead of the shared single-line button row. +export function ProtectedBackupAction({ + label, + onPress, + secondary = false, +}: { + label: string; + onPress: () => void; + // eslint-disable-next-line react/require-default-props -- Defaulted in the function signature. + secondary?: boolean; +}) { + const { colorMode } = useColorMode(); + return ( + + + + {label} + + + + ); +} + +const styles = StyleSheet.create({ + touchTarget: { width: '100%', marginTop: 12 }, + button: { minHeight: 48, paddingVertical: 14, paddingHorizontal: 16, borderRadius: 10 }, + label: { fontSize: 14, textAlign: 'center', flexShrink: 1 }, +}); diff --git a/src/context/Localization/language/en.json b/src/context/Localization/language/en.json index 7292b455fc..00d0eebcac 100644 --- a/src/context/Localization/language/en.json +++ b/src/context/Localization/language/en.json @@ -2261,6 +2261,19 @@ "disclaimerCta": "Got it" }, "recoveryBackup": { + "lastOperationGuidance": "Details describe the last unsuccessful operation. Check Again performs a new read-only check and replaces these details.", + "deleteNotEnrolled": "No protected copy has been enrolled for this account, so deletion was not confirmed. Older read-only copies and local recovery data are kept. View backup status and choose Back Up Now to create a protected copy, or turn off automatic backup to keep it off.", + "viewStatus": "View backup status", + "dismiss": "Dismiss", + "automaticOn": "Automatic backup is on. Verification of the protected copy is shown below.", + "automaticOff": "Automatic backup is off. Existing copies are kept.", + "backUpGuidance": "Back Up Now checks this device’s recovery data before updating the protected copy. Records that need review will not be replaced. Check Again only reads the current backup status.", + "showDetails": "Show check details", + "hideDetails": "Hide check details", + "detailsLabel": "Last check details (no recovery data)", + "httpStatus": "HTTP status", + "elapsed": "Elapsed", + "appBuild": "App version (build)", "title": "Protected server backup", "backUp": "Back Up Now", "check": "Check Again", diff --git a/src/context/Localization/language/es.json b/src/context/Localization/language/es.json index dd2b6d77ed..4031ff1843 100644 --- a/src/context/Localization/language/es.json +++ b/src/context/Localization/language/es.json @@ -2242,6 +2242,19 @@ "disclaimerCta": "Got it" }, "recoveryBackup": { + "lastOperationGuidance": "Los detalles describen la última operación sin éxito. Comprobar de nuevo realiza una nueva consulta de solo lectura y reemplaza estos detalles.", + "deleteNotEnrolled": "Esta cuenta todavía no ha registrado una copia protegida, por lo que no se confirmó la eliminación. Se conservan las copias anteriores de solo lectura y los datos locales de recuperación. Consulta el estado y elige Crear copia ahora para crear una copia protegida, o desactiva las copias automáticas.", + "viewStatus": "Ver estado de la copia", + "dismiss": "Cerrar", + "automaticOn": "Las copias automáticas están activadas. La verificación de la copia protegida se muestra abajo.", + "automaticOff": "Las copias automáticas están desactivadas. Se conservan las copias existentes.", + "backUpGuidance": "Crear copia ahora comprueba los datos de recuperación de este dispositivo antes de actualizar la copia protegida. Los registros que necesiten revisión no se reemplazarán. Comprobar de nuevo solo consulta el estado actual.", + "showDetails": "Mostrar detalles de comprobación", + "hideDetails": "Ocultar detalles de comprobación", + "detailsLabel": "Detalles de la última comprobación (sin datos de recuperación)", + "httpStatus": "Estado HTTP", + "elapsed": "Duración", + "appBuild": "Versión de la app (compilación)", "title": "Copia protegida en el servidor", "backUp": "Guardar ahora", "check": "Volver a comprobar", diff --git a/src/hooks/useSettingKeeper.tsx b/src/hooks/useSettingKeeper.tsx index f9daa67ff6..63c4292a02 100644 --- a/src/hooks/useSettingKeeper.tsx +++ b/src/hooks/useSettingKeeper.tsx @@ -1,5 +1,6 @@ import { CommonActions, useIsFocused, useNavigation } from '@react-navigation/native'; import React, { useCallback, useContext, useEffect, useRef, useState } from 'react'; +import { StyleSheet } from 'react-native'; import UpgradeIcon from 'src/assets/images/UpgradeCTAs.svg'; import InheritanceContactIcon from 'src/assets/images/inheritancecontacticon.svg'; import InheritanceRecoveryIcon from 'src/assets/images/inheritanceRecoveryIcon.svg'; @@ -41,7 +42,7 @@ import { KeeperApp } from 'src/models/interfaces/KeeperApp'; import { useQuery } from '@realm/react'; import { RealmSchema } from 'src/storage/realm/enum'; import { getJSONFromRealmObject } from 'src/storage/realm/utils'; -import { useColorMode } from '@gluestack-ui/themed-native-base'; +import { Box, useColorMode } from '@gluestack-ui/themed-native-base'; import { SubscriptionTier } from 'src/models/enums/SubscriptionTier'; import { backupAllSignersAndVaults, deleteBackup } from 'src/store/sagaActions/bhr'; import { useAppDispatch, useAppSelector } from 'src/store/hooks'; @@ -53,15 +54,14 @@ import { setDeleteBackupFailure, isAutomaticCloudBackupEnabled, } from 'src/store/reducers/bhr'; -import useToastMessage from './useToastMessage'; -import ToastErrorIcon from 'src/assets/images/toast_error.svg'; import { setShowTipModal, setThemeMode } from 'src/store/reducers/settings'; import ThemeMode from 'src/models/enums/ThemeMode'; import { credsAuthenticated } from 'src/store/reducers/login'; import usePlan from './usePlan'; import KeeperModal from 'src/components/KeeperModal'; -import { wp } from 'src/constants/responsive'; -import Buttons from 'src/components/Buttons'; +import Text from 'src/components/KeeperText'; +import { ProtectedBackupAction } from 'src/components/Backup/ProtectedBackupActions'; +import { currentBackupAccountSession, getBackupDiagnostic } from 'src/services/backup/transport'; import ThemedSvg from 'src/components/ThemedSvg.tsx/ThemedSvg'; import config from 'src/utils/service-utilities/config'; @@ -69,7 +69,6 @@ export const useSettingKeeper = () => { const dispatch = useAppDispatch(); const { colorMode, toggleColorMode } = useColorMode(); const navigation = useNavigation(); - const { showToast } = useToastMessage(); const isFocused = useIsFocused(); const { isOnL2Above, isOnL4 } = usePlan(); @@ -77,6 +76,7 @@ export const useSettingKeeper = () => { const [confirmPass, setConfirmPass] = useState(false); const [showDeleteBackup, setShowDeleteBackup] = useState(false); const [showEnableBackup, setShowEnableBackup] = useState(false); + const [backupFeedback, setBackupFeedback] = useState<{ appId: string; message: string }>(); const themeToggleTimeoutRef = useRef | null>(null); const backupToggleTimeoutRef = useRef | null>(null); const { translations } = useContext(LocalizationContext); @@ -88,6 +88,7 @@ export const useSettingKeeper = () => { common, signer: signerText, error: errorText, + recoveryBackup: backupCopy, } = translations; const { typeBasedIndicator } = useIndicatorHook({ types: [uaiType.RECOVERY_PHRASE_HEALTH_CHECK], @@ -101,6 +102,8 @@ export const useSettingKeeper = () => { deleteBackupFailureByAppId, } = useAppSelector((state) => state.bhr); const automaticCloudBackup = isAutomaticCloudBackupEnabled({ automaticCloudBackupByAppId }, id); + const backupAccountSession = currentBackupAccountSession(); + useEffect(() => setBackupFeedback(undefined), [id, backupAccountSession]); const backupAllFailure = !!backupAllFailureByAppId?.[id]; const backupAllSuccess = !!backupAllSuccessByAppId?.[id]; const deleteBackupSuccess = !!deleteBackupSuccessByAppId?.[id]; @@ -161,14 +164,19 @@ export const useSettingKeeper = () => { if (deleteBackupSuccess && isFocused) { dispatch(setDeleteBackupSuccess({ appId: id, status: false })); dispatch(setAutomaticCloudBackup({ appId: id, enabled: false })); - showToast(settings.assistedServerDeleteBackupConfirmed); + setBackupFeedback({ appId: id, message: settings.assistedServerDeleteBackupConfirmed }); } }, [deleteBackupSuccess, id, isFocused]); useEffect(() => { if (deleteBackupFailure && isFocused) { dispatch(setDeleteBackupFailure({ appId: id, status: false })); - showToast(errorText.unableToDeleteAssistedServer, ); + const diagnostic = getBackupDiagnostic(id); + const message = + diagnostic?.operation === 'delete' && diagnostic.category === 'never-enrolled' + ? backupCopy.deleteNotEnrolled + : errorText.unableToDeleteAssistedServer; + setBackupFeedback({ appId: id, message }); } }, [deleteBackupFailure]); @@ -269,6 +277,7 @@ export const useSettingKeeper = () => { { title: settings.assistedServerBackup, description: settings.assistedServerBackupSubtitle, + expandDescription: true, icon: , onPress: () => navigation.dispatch(CommonActions.navigate('AssistedBackupStatus')), rightIcon: isOnL2Above ? ( @@ -470,25 +479,34 @@ export const useSettingKeeper = () => { closeOnOverlayClick={false} close={() => setShowDeleteBackup(false)} title={settings.assistedServerDeleteBackupTitle} - subTitleWidth={wp(240)} - subTitle={settings.assistedServerDeleteBackupSubtitle} modalBackground={`${colorMode}.modalWhiteBackground`} textColor={`${colorMode}.textGreen`} - subTitleColor={`${colorMode}.modalSubtitleBlack`} Content={() => ( - { - setShowDeleteBackup(false); - dispatch(deleteBackup(id)); - }} - secondaryCallback={() => { - setShowDeleteBackup(false); - dispatch(setAutomaticCloudBackup({ appId: id, enabled: false })); - showToast(settings.assistedServerBackupDisabled); - }} - secondaryText={settings.assistedServerDeleteBackupModalSecondaryCTA} - /> + + + {settings.assistedServerDeleteBackupSubtitle} + + { + setShowDeleteBackup(false); + dispatch(deleteBackup(id)); + }} + /> + { + setShowDeleteBackup(false); + dispatch(setAutomaticCloudBackup({ appId: id, enabled: false })); + setBackupFeedback({ appId: id, message: settings.assistedServerBackupDisabled }); + }} + /> + )} /> { closeOnOverlayClick={false} close={() => setShowEnableBackup(false)} title={settings.assistedServerEnableBackupTitle} - subTitle={settings.assistedServerEnableBackupSubtitle} - subTitleWidth={wp(240)} modalBackground={`${colorMode}.modalWhiteBackground`} textColor={`${colorMode}.textGreen`} - subTitleColor={`${colorMode}.modalSubtitleBlack`} - buttonText={settings.assistedServerEnableBackupCTA} - buttonCallback={() => { - setShowEnableBackup(false); - dispatch(backupAllSignersAndVaults(id)); - }} - secondaryButtonText={common.cancel} - secondaryCallback={() => setShowEnableBackup(false)} + Content={() => ( + + + {settings.assistedServerEnableBackupSubtitle} + + { + setShowEnableBackup(false); + dispatch(backupAllSignersAndVaults(id)); + }} + /> + setShowEnableBackup(false)} + /> + + )} /> ); + const BackupFeedback = + backupFeedback?.appId === id ? ( + + + {backupFeedback.message} + + navigation.dispatch(CommonActions.navigate('AssistedBackupStatus'))} + /> + setBackupFeedback(undefined)} + /> + + ) : null; + return { + BackupFeedback, BackAndRecovery, General, keysAndwallet, @@ -525,3 +575,9 @@ export const useSettingKeeper = () => { DeleteBackupModal, }; }; + +const styles = StyleSheet.create({ + modalBody: { fontSize: 14, marginBottom: 12 }, + feedback: { marginHorizontal: 20, marginBottom: 20, padding: 20, borderRadius: 12 }, + feedbackText: { fontSize: 14 }, +}); diff --git a/src/screens/AppSettings/AssistedBackupStatus.tsx b/src/screens/AppSettings/AssistedBackupStatus.tsx index bb225eb2e4..f9bc9ed8dc 100644 --- a/src/screens/AppSettings/AssistedBackupStatus.tsx +++ b/src/screens/AppSettings/AssistedBackupStatus.tsx @@ -3,15 +3,17 @@ import { ActivityIndicator, StyleSheet } from 'react-native'; import { Box, ScrollView, useColorMode } from '@gluestack-ui/themed-native-base'; import { useQuery } from '@realm/react'; import { useNavigation } from '@react-navigation/native'; +import DeviceInfo from 'react-native-device-info'; import { RealmSchema } from 'src/storage/realm/enum'; import { useAppDispatch, useAppSelector } from 'src/store/hooks'; import { checkBackupFreshness, repairBackup } from 'src/store/sagaActions/bhr'; import { isAutomaticCloudBackupEnabled } from 'src/store/reducers/bhr'; import { isBackupVerificationCurrent } from 'src/services/backup/automatic'; +import { getBackupDiagnostic } from 'src/services/backup/transport'; import { LocalizationContext } from 'src/context/Localization/LocContext'; import ScreenWrapper from 'src/components/ScreenWrapper'; import WalletHeader from 'src/components/WalletHeader'; -import Buttons from 'src/components/Buttons'; +import { ProtectedBackupAction } from 'src/components/Backup/ProtectedBackupActions'; import Text from 'src/components/KeeperText'; import { hp } from 'src/constants/responsive'; @@ -34,10 +36,15 @@ export default function AssistedBackupStatus() { recordedPhase === 'verified' && !isBackupVerificationCurrent(id) ? 'unverified' : recordedPhase; const running = !!backupRepairRunningByAppId[id]; const [slow, setSlow] = useState(false); + const [showDetails, setShowDetails] = useState(false); + const diagnostic = getBackupDiagnostic(id); const deletionRecorded = protectedBackupStateByAppId[id]?.status === 'deleted'; useEffect(() => { - if (id) dispatch(checkBackupFreshness(id)); + // Preserve the last failed operation when navigating here from its result. + // Check Again explicitly replaces it with a fresh, read-only check. + if (id && !getBackupDiagnostic(id)) dispatch(checkBackupFreshness(id)); }, [id]); + useEffect(() => setShowDetails(false), [id, running]); useEffect(() => { setSlow(false); if (!running) return undefined; @@ -55,10 +62,17 @@ export default function AssistedBackupStatus() { : ['checking', 'preparing', 'uploading', 'verifying'].includes(phase) ? 'unverified' : phase || 'unverified'; + const canBackUp = + automaticCloudBackup && + !deletionRecorded && + ['unverified', 'different', 'never-enrolled'].includes(state); return ( + + {automaticCloudBackup ? copy.automaticOn : copy.automaticOff} + {copy[state].title} @@ -88,38 +102,64 @@ export default function AssistedBackupStatus() { )} - + {copy.retainedCopyNotice} {!running && ( - - dispatch( - automaticCloudBackup && state === 'different' - ? repairBackup(id) - : checkBackupFreshness(id) - ) - } - /> + + {canBackUp && ( + <> + + {copy.backUpGuidance} + + dispatch(repairBackup(id))} + /> + + )} + dispatch(checkBackupFreshness(id))} + /> + {diagnostic && ( + <> + + {copy.lastOperationGuidance} + + setShowDetails((visible) => !visible)} + /> + {showDetails && ( + + {`${copy.detailsLabel}\n${diagnostic.operation} / ${diagnostic.stage} / ${ + diagnostic.category + }\n${copy.httpStatus}: ${diagnostic.httpStatus ?? '—'}; ${copy.elapsed}: ${ + diagnostic.elapsedMs + } ms\n${ + copy.appBuild + }: ${DeviceInfo.getVersion()} (${DeviceInfo.getBuildNumber()})`} + + )} + + )} + )} - navigation.goBack()} - /> + navigation.goBack()} /> ); } const styles = StyleSheet.create({ - content: { paddingVertical: hp(24) }, + content: { paddingTop: hp(16), paddingBottom: hp(40) }, + automaticStatus: { fontSize: 14, marginBottom: hp(20) }, card: { padding: 20, borderRadius: 16, marginBottom: hp(24) }, title: { fontSize: 18, marginBottom: hp(16) }, body: { fontSize: 14, marginTop: hp(12) }, + notice: { fontSize: 14, marginTop: hp(8), marginBottom: hp(24) }, }); diff --git a/src/screens/Home/components/Settings/Component/SettingCard.tsx b/src/screens/Home/components/Settings/Component/SettingCard.tsx index 089e89feab..254e7099bb 100644 --- a/src/screens/Home/components/Settings/Component/SettingCard.tsx +++ b/src/screens/Home/components/Settings/Component/SettingCard.tsx @@ -12,6 +12,7 @@ import Colors from 'src/theme/Colors'; interface SettingCardItemProps { title?: string; description?: string; + expandDescription?: boolean; icon?: React.ReactNode; rightIcon?: React.ReactNode; isDiamond?: boolean; @@ -100,6 +101,7 @@ const SettingCard: React.FC = ({ ? titleColor ?? `${colorMode}.balanceText` : `${colorMode}.secondaryLightGrey` } + allowFontScaling={!!item.expandDescription} fontSize={14} medium style={styles.title} @@ -110,7 +112,8 @@ const SettingCard: React.FC = ({ { const { signer: signerText, inheritancePlanning, settings, common } = translations; const { BackAndRecovery, + BackupFeedback, General, keysAndwallet, Tips, @@ -70,6 +71,7 @@ const KeeperSettings = ({ route }) => { icon={} showDot={false} /> + {BackupFeedback} {}) { const result = await readProtectedBackup(app.primaryMnemonic, deadline, appId, checkpoint, app); - if (result.state === 'neverEnrolled') throw new Error('Protected backup is not enrolled'); + if (result.state === 'neverEnrolled') throw backupNotEnrolledError(); return result.snapshot; } @@ -559,6 +563,8 @@ export function enqueueAutomaticBackupOperation( return withBackupSession(appId, async () => { let acknowledged = 0; const deadline = proof?.deadline ?? Date.now() + BACKUP_DEADLINE_MS; + const started = Date.now(); + let diagnosticStage: BackupDiagnosticStage = 'delete-preparation'; const assertOwned = () => { if (!intent || !proof || Date.now() >= deadline) throw new Error('Backup proof deadline expired'); @@ -588,12 +594,14 @@ export function enqueueAutomaticBackupOperation( throw new Error('Automatic backup source or availability is not enabled'); assertProtectedBackupWriteReady(); assertOwned(); + diagnosticStage = 'snapshot-request'; const baseline = await snapshot(appId, proof.after.app, deadline, assertOwned); assertOwned(); if (intent.baseRevision !== intent.protectedBase?.revision || !sameProtectedBackupBase(protectedBackupBase(baseline), intent.protectedBase)) throw new Error('Remote authority changed'); if (proof.deletion) { + diagnosticStage = 'delete-validation'; if (mutations.length !== 1 || mutations[0].operation !== 'deleteBackup') throw new Error('Invalid explicit deletion'); await decodeImage( @@ -610,12 +618,14 @@ export function enqueueAutomaticBackupOperation( let expectedBase = intent.protectedBase; for (const mutation of mutations) { assertOwned(); + diagnosticStage = 'delete-upload'; const reply = await protectedBackupMutation(proof.after.app.primaryMnemonic, expectedBase, mutation.operation, mutation.body, deadline, assertOwned, proof.after.app); expectedBase = reply.base; acknowledged += 1; } assertOwned(); + diagnosticStage = 'delete-readback'; const readback = await snapshot(appId, proof.after.app, deadline, assertOwned); assertOwned(); if (!sameProtectedBackupBase(protectedBackupBase(readback), expectedBase)) @@ -658,7 +668,9 @@ export function enqueueAutomaticBackupOperation( results.set(result, { proof, session: intent.accountSession, appId }); if (proof.deletion) clearVerifiedRemoteRevision(appId); return result; - } catch { + } catch (error) { + if (proof?.deletion && intent) + recordBackupDiagnostic(appId, intent.accountSession, 'delete', diagnosticStage, error, started); clearVerifiedRemoteRevision(appId); notifyBackupReconciliation(appId); return pending(committed, acknowledged === mutations.length && acknowledged > 0); @@ -669,6 +681,9 @@ export async function beginBackupDeleteOperation( appId: string, originSession = currentBackupAccountSession() ): Promise { + const started = Date.now(); + let diagnosticStage: BackupDiagnosticStage = 'delete-preparation'; + clearBackupDiagnostic(appId, originSession); try { const op = beginBackupLocalOperation(appId, undefined, originSession); assertProtectedBackupWriteReady(); @@ -680,6 +695,7 @@ export async function beginBackupDeleteOperation( state.generation !== op.generation || state.capture.fingerprint !== op.before.fingerprint || !canAccessBackupAccount(appId, true)) throw new Error('Deletion source changed'); }; + diagnosticStage = 'snapshot-request'; const remote = await withBackupSession(appId, () => snapshot(appId, op.before.app, deadline, assertOrigin)); const state = local(); @@ -692,6 +708,7 @@ export async function beginBackupDeleteOperation( !canAccessBackupAccount(appId, true) ) throw new Error('Deletion scope changed'); + diagnosticStage = 'delete-validation'; await decodeImage( generateEncryptionKey(op.before.app.primarySeed), remote, @@ -727,7 +744,8 @@ export async function beginBackupDeleteOperation( deadline, }); return Object.freeze({ committed: true, intent }); - } catch { + } catch (error) { + recordBackupDiagnostic(appId, originSession, 'delete', diagnosticStage, error, started); clearVerifiedRemoteRevision(appId); notifyBackupReconciliation(appId); return Object.freeze({ committed: false }); diff --git a/src/services/backup/repair.ts b/src/services/backup/repair.ts index 7fa10920bc..c6e7e8f63c 100644 --- a/src/services/backup/repair.ts +++ b/src/services/backup/repair.ts @@ -23,6 +23,8 @@ import { currentBackupAccountSession, recordVerifiedRemoteRevision, withBackupSession, + clearBackupDiagnostic, + recordBackupDiagnostic, } from './transport'; import { BackupImage, @@ -217,9 +219,11 @@ export function inspectBackup( listeners.forEach((listener) => listener(phase)); }; let deadline: number; + const started = Date.now(); const inspectOnce = async (allowRetry: boolean): Promise => { let stage: InspectionStage = 'local-capture'; let changedRevision = 0; + let uploadFailed = false; // One deadline covers both read-only attempts; a retry must not double it. deadline ??= Date.now() + BACKUP_DEADLINE_MS; const revision = backupRevision(appId); @@ -404,9 +408,11 @@ export function inspectBackup( const reply = await protectedBackupMutation(app.primaryMnemonic, expectedBase, 'repairAppBackup', {...payload, replaceCurrentState: true}, deadline, assertSource, app); acknowledgedBase = reply.base; - } catch { + } catch (error) { // An unknown outcome permits a fresh read, never a retry or a new // base for this ciphertext. A matching read alone is not its ACK. + uploadFailed = true; + recordBackupDiagnostic(appId, expectedSession, 'backup', 'repair-upload', error, started); } notify('verifying'); stage = 'repair-readback'; @@ -471,6 +477,11 @@ export function inspectBackup( failedStage === 'snapshot-decryption' && error instanceof BackupSnapshotValidationError ? 'snapshot-validation' : failedStage; + // Preserve the original redacted upload failure while still attempting + // honest readback. Missing ACK is never converted into verification. + if (!uploadFailed) + recordBackupDiagnostic(appId, expectedSession, repair ? 'backup' : 'check', + diagnosticStage, error, started); globalThis.console.warn( 'Assisted Server Backup check failed at stage:', diagnosticStage, @@ -489,6 +500,7 @@ export function inspectBackup( // Invalidate only after admission, so a later inspection cannot cancel // an earlier writer. Each admitted retry must expire its own baseline. clearVerifiedRemoteRevision(appId); + clearBackupDiagnostic(appId, expectedSession); return inspectOnce(allowRetry); }); const result: Promise = runOnce(true).then(async (phase) => diff --git a/src/services/backup/transport.ts b/src/services/backup/transport.ts index ec19b046ac..ca241cfeab 100644 --- a/src/services/backup/transport.ts +++ b/src/services/backup/transport.ts @@ -94,6 +94,7 @@ export function observeBackupAccount(appId?: string) { for (const id of verifiedRemoteRevisions.keys()) clearVerifiedRemoteRevision(id); activeAccount = appId; accountSession += 1; + lastDiagnostic = undefined; } } export const currentBackupAccountSession = () => accountSession; @@ -157,11 +158,111 @@ export type BackupRequestFailure = | 'other'; export class BackupRequestError extends Error { - constructor(public readonly category: BackupRequestFailure) { + readonly httpStatus?: number; + + constructor(public readonly category: BackupRequestFailure, httpStatus?: number) { super('Backup request could not be completed'); + if ( + typeof httpStatus === 'number' && + Number.isInteger(httpStatus) && + httpStatus >= 400 && + httpStatus <= 599 + ) + this.httpStatus = httpStatus; } } +// Construct only after an authenticated, history-checked protected read says +// the namespace has never been enrolled. This is absence, never Deleted. +const neverEnrolledErrors = new WeakSet(); +export function backupNotEnrolledError() { + const error = new Error('Protected backup is not enrolled'); + neverEnrolledErrors.add(error); + return error; +} + +export type BackupDiagnosticStage = + | 'local-capture' + | 'snapshot-request' + | 'snapshot-validation' + | 'snapshot-decryption' + | 'comparison' + | 'repair-preparation' + | 'repair-upload' + | 'repair-readback' + | 'local-recheck' + | 'delete-preparation' + | 'delete-validation' + | 'delete-upload' + | 'delete-readback'; +export type BackupDiagnostic = Readonly<{ + operation: 'check' | 'backup' | 'delete'; + stage: BackupDiagnosticStage; + category: + | BackupRequestFailure + | 'non-transport' + | 'authority-unavailable' + | 'endpoint-configuration' + | 'never-enrolled'; + httpStatus?: number; + elapsedMs: number; +}>; +// One bounded, in-memory result for the current account session. No error, +// request, body, identifier or recovery content is retained or persisted. +let lastDiagnostic: { appId: string; session: number; value: BackupDiagnostic } | undefined; +export function getBackupDiagnostic(appId: string): BackupDiagnostic | undefined { + return lastDiagnostic?.appId === appId && + lastDiagnostic.session === accountSession && + activeAccount === appId + ? lastDiagnostic.value + : undefined; +} +export function clearBackupDiagnostic(appId: string, session = accountSession) { + if (session === accountSession && appId === activeAccount) lastDiagnostic = undefined; +} +export function recordBackupDiagnostic( + appId: string, + session: number, + operation: BackupDiagnostic['operation'], + stage: BackupDiagnosticStage, + error: unknown, + started: number +) { + if (session !== accountSession || appId !== activeAccount) return; + // Equality against fixed internal sentinels only: never retain/display a + // caught message, including an Axios message containing private material. + const message = (error as { message?: unknown })?.message; + const category: BackupDiagnostic['category'] = + error !== null && typeof error === 'object' && neverEnrolledErrors.has(error) + ? 'never-enrolled' + : error instanceof BackupRequestError + ? error.category + : message === 'PROTECTED_BACKUP_AUTHORITY_UNAVAILABLE' + ? 'authority-unavailable' + : ['Invalid protected backup endpoint', 'Protected backup requires HTTPS'].includes( + message as string + ) + ? 'endpoint-configuration' + : [ + 'Invalid protected backup response', + 'Protected backup history conflicts with this device', + ].includes(message as string) + ? 'protocol' + : 'non-transport'; + const httpStatus = error instanceof BackupRequestError ? error.httpStatus : undefined; + lastDiagnostic = { + appId, + session, + value: Object.freeze({ + operation, + stage, + category, + ...(httpStatus === undefined ? {} : { httpStatus }), + elapsedMs: Math.max(0, Math.min(BACKUP_DEADLINE_MS, Math.round(Date.now() - started))), + }), + }; +} + function requestFailureCategory(error: unknown, aborted: boolean): BackupRequestFailure { if (aborted) return 'timeout'; if (!error || typeof error !== 'object') return 'other'; @@ -239,7 +340,9 @@ export async function boundedBackupPost( }); } catch (error) { // Do not propagate Axios errors: they can contain request bodies and headers. - throw new BackupRequestError(requestFailureCategory(error, controller.signal.aborted)); + const status = (error as { response?: { status?: number }; status?: number })?.response?.status ?? + (error as { status?: number })?.status; + throw new BackupRequestError(requestFailureCategory(error, controller.signal.aborted), status); } finally { clearTimeout(idle); clearTimeout(total); diff --git a/tests/recovery/backupDiagnostic.test.cjs b/tests/recovery/backupDiagnostic.test.cjs new file mode 100644 index 0000000000..cba08ae58d --- /dev/null +++ b/tests/recovery/backupDiagnostic.test.cjs @@ -0,0 +1,236 @@ +// User-initiated diagnostics retain only fixed enums, bounded timing and HTTP +// status, never the original error/body or a previous account session's result. +const { test } = require('node:test'); +const assert = require('node:assert/strict'); +const { loadModule, harness } = require('./helpers.cjs'); +const plain = (value) => JSON.parse(JSON.stringify(value)); + +function transportWithFailure(error) { + return loadModule('src/services/backup/transport.ts', { + '../rest/RestClient': { + post: async () => { + throw error; + }, + }, + }); +} + +test('HTTP failure exposes status and fixed category without retaining private transport data', async () => { + const secret = 'private-body-header-account-token'; + const t = transportWithFailure({ + response: { status: 403, data: secret }, + config: { headers: { Authorization: secret }, data: secret }, + message: secret, + }); + t.observeBackupAccount('disposable-account'); + let failure; + try { + await t.boundedBackupPost('https://disposable.invalid', {}); + } catch (error) { + failure = error; + } + t.recordBackupDiagnostic( + 'disposable-account', + t.currentBackupAccountSession(), + 'check', + 'snapshot-request', + failure, + Date.now() - 25 + ); + const diagnostic = plain(t.getBackupDiagnostic('disposable-account')); + assert.deepEqual(Object.keys(diagnostic).sort(), [ + 'category', + 'elapsedMs', + 'httpStatus', + 'operation', + 'stage', + ]); + assert.equal(diagnostic.operation, 'check'); + assert.equal(diagnostic.stage, 'snapshot-request'); + assert.equal(diagnostic.category, 'http-4xx'); + assert.equal(diagnostic.httpStatus, 403); + assert.ok(diagnostic.elapsedMs >= 25 && diagnostic.elapsedMs <= t.BACKUP_DEADLINE_MS); + assert.equal(JSON.stringify({ failure, diagnostic }).includes(secret), false); + assert.equal(Object.isFrozen(t.getBackupDiagnostic('disposable-account')), true); +}); + +test('unknown errors cannot project arbitrary messages or invalid statuses into diagnostic output', () => { + const t = transportWithFailure(); + t.observeBackupAccount('A'); + const session = t.currentBackupAccountSession(); + for (const httpStatus of ['403', 399, 600, 403.5, NaN, Infinity, undefined]) { + t.recordBackupDiagnostic( + 'A', + session, + 'delete', + 'delete-upload', + new t.BackupRequestError('other', httpStatus), + Date.now() - 1e9 + ); + const diagnostic = t.getBackupDiagnostic('A'); + assert.equal(diagnostic.httpStatus, undefined); + assert.equal(diagnostic.elapsedMs, t.BACKUP_DEADLINE_MS); + } + t.recordBackupDiagnostic( + 'A', + session, + 'delete', + 'delete-validation', + { message: 'private fixture', response: { status: 403 } }, + Date.now() + 500 + ); + assert.deepEqual(plain(t.getBackupDiagnostic('A')), { + operation: 'delete', + stage: 'delete-validation', + category: 'non-transport', + elapsedMs: 0, + }); +}); + +test('authority and endpoint sentinels have fixed categories without exposing caught messages', () => { + const t = transportWithFailure(); + t.observeBackupAccount('A'); + for (const [message, category] of [ + ['PROTECTED_BACKUP_AUTHORITY_UNAVAILABLE', 'authority-unavailable'], + ['Invalid protected backup endpoint', 'endpoint-configuration'], + ['Protected backup requires HTTPS', 'endpoint-configuration'], + ['Invalid protected backup response', 'protocol'], + ]) { + t.recordBackupDiagnostic( + 'A', + t.currentBackupAccountSession(), + 'check', + 'snapshot-request', + new Error(message), + Date.now() + ); + const diagnostic = t.getBackupDiagnostic('A'); + assert.equal(diagnostic.category, category); + assert.equal(JSON.stringify(diagnostic).includes(message), false); + } +}); + +test('not-enrolled guidance requires the internal authenticated-absence error, never a matching arbitrary message', () => { + const t = transportWithFailure(); + t.observeBackupAccount('A'); + for (const error of [ + new Error('Protected backup is not enrolled'), + { message: 'Protected backup is not enrolled' }, + ]) { + t.recordBackupDiagnostic( + 'A', + t.currentBackupAccountSession(), + 'delete', + 'snapshot-request', + error, + Date.now() + ); + assert.equal(t.getBackupDiagnostic('A').category, 'non-transport'); + } + t.recordBackupDiagnostic( + 'A', + t.currentBackupAccountSession(), + 'delete', + 'snapshot-request', + t.backupNotEnrolledError(), + Date.now() + ); + assert.equal(t.getBackupDiagnostic('A').category, 'never-enrolled'); +}); + +test('diagnostics expire across A to B to A and stale completion cannot clear or replace a newer result', () => { + const t = transportWithFailure(); + t.observeBackupAccount('A'); + const oldSession = t.currentBackupAccountSession(); + t.recordBackupDiagnostic( + 'A', + oldSession, + 'check', + 'snapshot-request', + Error('private'), + Date.now() + ); + assert.ok(t.getBackupDiagnostic('A')); + assert.equal(t.getBackupDiagnostic('B'), undefined); + t.observeBackupAccount('B'); + t.observeBackupAccount('A'); + assert.equal(t.getBackupDiagnostic('A'), undefined); + const currentSession = t.currentBackupAccountSession(); + t.recordBackupDiagnostic( + 'A', + currentSession, + 'delete', + 'delete-readback', + Error('private'), + Date.now() + ); + t.recordBackupDiagnostic( + 'A', + oldSession, + 'check', + 'snapshot-request', + Error('private'), + Date.now() + ); + t.clearBackupDiagnostic('A', oldSession); + assert.equal(t.getBackupDiagnostic('A').operation, 'delete'); + t.clearBackupDiagnostic('A', currentSession); + assert.equal(t.getBackupDiagnostic('A'), undefined); +}); + +test('actual inspection failure supplies its bounded diagnostic; a subsequent successful check clears it', async () => { + let failing = true; + const f = harness({ + post: async () => { + if (failing) throw { response: { status: 401, data: 'private response' } }; + }, + }); + f.transport.observeBackupAccount(f.app.id); + assert.equal(await f.run(), 'unverified'); + const diagnostic = f.transport.getBackupDiagnostic(f.app.id); + assert.equal(diagnostic.operation, 'check'); + assert.equal(diagnostic.stage, 'snapshot-request'); + assert.equal(diagnostic.category, 'http-4xx'); + assert.equal(diagnostic.httpStatus, 401); + failing = false; + assert.equal(await f.run(), 'verified'); + assert.equal(f.transport.getBackupDiagnostic(f.app.id), undefined); + assert.deepEqual(f.calls, ['getBackupSnapshot', 'getBackupSnapshot']); +}); + +for (const status of [401, 503]) + test(`repair preserves denied upload ${status} diagnostic after honest readback`, async () => { + const { wallet } = require('./helpers.cjs'); + const f = harness({ + post: async ({ path }) => { + if (path.endsWith('revisionedBackupMutation')) + throw { response: { status, data: 'private upload response' } }; + }, + }); + f.transport.observeBackupAccount(f.app.id); + f.local.Wallet.push(wallet()); + assert.equal(await f.run(true), 'unverified'); + const diagnostic = f.transport.getBackupDiagnostic(f.app.id); + assert.equal(diagnostic.operation, 'backup'); + assert.equal(diagnostic.stage, 'repair-upload'); + assert.equal(diagnostic.httpStatus, status); + assert.equal(diagnostic.category, status < 500 ? 'http-4xx' : 'http-5xx'); + assert.deepEqual(f.calls, [ + 'getBackupSnapshot', + 'revisionedBackupMutation', + 'getBackupSnapshot', + ]); + assert.equal(f.phases.includes('verified'), false); + }); + +test('lost upload acknowledgement retains uncertainty even when readback matches', async () => { + const { wallet } = require('./helpers.cjs'); + const f = harness({ lostResponse: true }); + f.transport.observeBackupAccount(f.app.id); + f.local.Wallet.push(wallet()); + assert.equal(await f.run(true), 'unverified'); + assert.ok(f.remote.wallets.wallet, 'disposable remote write really committed'); + assert.equal(f.transport.getBackupDiagnostic(f.app.id).stage, 'repair-upload'); + assert.equal(f.phases.includes('verified'), false); + assert.equal(f.calls.filter((call) => call === 'revisionedBackupMutation').length, 1); +}); diff --git a/tests/recovery/protectedUpgrade7066.proof.cjs b/tests/recovery/protectedUpgrade7066.proof.cjs new file mode 100644 index 0000000000..e73b66d4ac --- /dev/null +++ b/tests/recovery/protectedUpgrade7066.proof.cjs @@ -0,0 +1,160 @@ +// Opt-in disposable upgraded-account witness. Uses production Redux Persist, +// native encrypted Realm, authority, crypto, HTTP and actual protected Relay. +// This host proof does not establish iOS/Android or original-phone acceptance. +const assert = require('node:assert/strict'); +const crypto = require('node:crypto'); +const { test, after } = require('node:test'); +const { + createProtectedServer, + createProtectedRealmClient, +} = require('./protectedWriteRealmRelay.fixture.cjs'); +const { declarations, evaluate, plain, Realm } = require('./automaticWriteRealmRelay.fixture.cjs'); +const mnemonic = + 'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about'; +const seed = require('bip39').mnemonicToSeedSync(mnemonic); +const appId = crypto.createHash('sha256').update(seed).digest('hex'); +const builders = { assert }; +evaluate( + declarations('tests/recovery/automaticWriteRealmRelay.integration.cjs', [ + 'wallet', + 'signer', + 'vault', + 'persist', + 'persistVault', + ]), + builders, + 'disposable-upgrade-builders.ts' +); +after(() => Realm.shutdown()); + +test('existing legacy account with saved consent can explicitly create, recheck and delete only its protected copy', async (t) => { + const server = await createProtectedServer(); + t.after(() => server.close()); + const f = await createProtectedRealmClient(server.url, { + appId, + primarySeed: seed.toString('hex'), + primaryMnemonic: mnemonic, + persistedRedux: true, + }); + t.after(() => f.close()); + f.automatic = f.load('src/services/backup/automatic'); + f.installProductionStoreObservers(f.automatic); + builders.persist( + f, + f.RealmSchema.Wallet, + builders.wallet(f, 'upgraded-wallet', 'Disposable old wallet') + ); + builders.persist(f, f.RealmSchema.Signer, builders.signer(f)); + builders.persistVault(f, 'upgraded-vault'); + const localBefore = await f.repair.captureBackupImage(); + f.nativeRealm().write(() => { + f.nativeRealm().objectForPrimaryKey(f.RealmSchema.KeeperApp, appId).version = '2.6.3'; + }); + await server.seedLegacy(await f.payloadForCurrentImage()); + const legacy = server.backend.mongoose.connection.db.collection('appimages'); + const legacyBefore = await legacy.findOne({ appId }); + // An actual cold Redux Persist boot preserves old consent but expires saved + // Verified state. The installed upgrade keeps all existing local records. + await f.rehydratePersistedRedux({ + root: { storage: { appId }, _persist: { version: 4, rehydrated: true } }, + bhr: { + automaticCloudBackupByAppId: { [appId]: true }, + backupRepairStateByAppId: { [appId]: 'verified' }, + backupRepairCompletedByAppId: { [appId]: true }, + _persist: { version: -1, rehydrated: true }, + }, + }); + assert.equal(f.store.getState().bhr.automaticCloudBackupByAppId[appId], true); + assert.equal(f.store.getState().bhr.backupRepairStateByAppId[appId], 'unverified'); + f.nativeRealm().write(() => { + f.nativeRealm().objectForPrimaryKey(f.RealmSchema.KeeperApp, appId).version = '2.6.4'; + }); + const sagas = f.productionSagas([ + 'currentBackupAppId', + 'backupInspectionOwners', + 'runBackupInspection', + 'deleteBackupWorker', + ]); + const inspect = (repair) => sagas.run('runBackupInspection', repair, appId, true); + const read = () => + f.protected.readProtectedBackup( + mnemonic, + undefined, + appId, + undefined, + plain(f.dbManager.getObjectByIndex(f.RealmSchema.KeeperApp)) + ); + server.clearTrace(); + assert.equal(await inspect(false), false); + assert.equal(f.store.getState().bhr.backupRepairStateByAppId[appId], 'never-enrolled'); + assert.deepEqual( + server.trace.map((entry) => entry.path), + ['/backup/v3/snapshot'] + ); + assert.equal(await sagas.run('deleteBackupWorker', { appId }), false); + assert.equal(f.transport.getBackupDiagnostic(appId).category, 'never-enrolled'); + assert.equal(f.store.getState().bhr.deleteBackupSuccessByAppId[appId], false); + assert.ok(server.trace.every((entry) => entry.path === '/backup/v3/snapshot')); + assert.equal( + await f.image.compareImages(localBefore, await f.repair.captureBackupImage()), + 'matched' + ); + // Upgrade-specific failure witness: enrollment commits, but its reply is + // interrupted. The existing account stays unverified with all data/consent + // intact; repeated Check Again remains read-only and cannot finish backup. + server.armFault({ path: '/backup/v3/enroll', mode: 'lost-reply' }); + assert.equal(await inspect(true), false); + assert.equal(f.store.getState().bhr.backupRepairStateByAppId[appId], 'unverified'); + assert.equal(f.store.getState().bhr.automaticCloudBackupByAppId[appId], true); + assert.equal( + await f.image.compareImages(localBefore, await f.repair.captureBackupImage()), + 'matched' + ); + assert.deepEqual(await legacy.findOne({ appId }), legacyBefore); + assert.equal(server.trace.filter((entry) => entry.path === '/backup/v3/mutate').length, 0); + server.clearTrace(); + assert.equal(await inspect(false), false); + assert.equal(f.store.getState().bhr.backupRepairStateByAppId[appId], 'different'); + assert.ok(server.trace.every((entry) => entry.path === '/backup/v3/snapshot')); + // Back Up Now is the separate affirmative repair action. Check Again above + // never enrolls/uploads, even with saved automatic consent enabled. + server.clearTrace(); + assert.equal(await inspect(true), true); + assert.ok(server.trace.every((entry) => entry.path !== '/backup/v3/enroll')); + assert.deepEqual( + server.trace + .filter((entry) => entry.path === '/backup/v3/mutate') + .map((entry) => entry.operation), + ['repairAppBackup'] + ); + const protectedCopy = await read(); + assert.equal(protectedCopy.state, 'present'); + const decoded = await f.image.decodeImage( + f.encryption.generateEncryptionKey(f.seed), + protectedCopy.snapshot, + protectedCopy.namespaceId, + () => {}, + true + ); + assert.equal(await f.image.compareImages(localBefore, decoded), 'matched'); + assert.deepEqual(await legacy.findOne({ appId }), legacyBefore); + server.clearTrace(); + assert.equal(await inspect(false), true); + assert.ok(server.trace.every((entry) => entry.path === '/backup/v3/snapshot')); + // Stopping automatic backup retains both copies. A separate explicit delete + // obtains a fresh authenticated tuple and confirms its protected tombstone. + f.store.dispatch(f.bhr.setAutomaticCloudBackup({ appId, enabled: false })); + assert.equal((await read()).state, 'present'); + assert.deepEqual(await legacy.findOne({ appId }), legacyBefore); + assert.equal(await sagas.run('deleteBackupWorker', { appId }), true); + assert.equal((await read()).state, 'deleted'); + assert.equal(f.store.getState().bhr.automaticCloudBackupByAppId[appId], false); + assert.equal( + await f.image.compareImages(localBefore, await f.repair.captureBackupImage()), + 'matched' + ); + assert.deepEqual(await legacy.findOne({ appId }), legacyBefore); + assert.equal(await inspect(true), true); + assert.equal((await read()).state, 'present'); + assert.deepEqual(await legacy.findOne({ appId }), legacyBefore); +}); diff --git a/tests/screens/ProtectedBackupControls.test.tsx b/tests/screens/ProtectedBackupControls.test.tsx new file mode 100644 index 0000000000..7f997a3732 --- /dev/null +++ b/tests/screens/ProtectedBackupControls.test.tsx @@ -0,0 +1,312 @@ +import React from 'react'; +import renderer, { act } from 'react-test-renderer'; +import fs from 'fs'; +import path from 'path'; + +const mockDispatch = jest.fn(); +const mockNavigate = jest.fn(); +const mockToast = jest.fn(); +const mockApp = { id: 'disposable-upgraded-account' }; +let mockSession = 1; +let mockDiagnostic: any; +const mockState: any = { bhr: {}, storage: { appId: mockApp.id } }; +const copy = require('../../src/context/Localization/language/en.json'); +jest.mock('src/constants/responsive', () => ({ hp: (n) => n, wp: (n) => n, windowWidth: 320 })); +jest.mock('@gluestack-ui/themed-native-base', () => ({ + Box: 'Box', + ScrollView: 'ScrollView', + useColorMode: () => ({ colorMode: 'light', toggleColorMode: jest.fn() }), +})); +jest.mock('@realm/react', () => ({ useQuery: (name) => (name === 'KeeperApp' ? [mockApp] : []) })); +jest.mock('src/storage/realm/utils', () => ({ getJSONFromRealmObject: (value) => value })); +jest.mock('src/store/hooks', () => ({ + useAppDispatch: () => mockDispatch, + useAppSelector: (select) => select(mockState), +})); +jest.mock('@react-navigation/native', () => ({ + useNavigation: () => ({ dispatch: mockNavigate, navigate: mockNavigate, goBack: mockNavigate }), + useIsFocused: () => true, + CommonActions: { navigate: (route) => ({ route }) }, +})); +jest.mock('src/hooks/usePlan', () => ({ + __esModule: true, + default: () => ({ isOnL2Above: true }), +})); +jest.mock('src/hooks/useIndicatorHook', () => ({ + useIndicatorHook: () => ({ typeBasedIndicator: {} }), +})); +jest.mock('src/hooks/useToastMessage', () => ({ + __esModule: true, + default: () => ({ showToast: mockToast }), +})); +jest.mock('src/services/backup/automatic', () => ({ isBackupVerificationCurrent: () => false })); +jest.mock('src/services/backup/transport', () => ({ + getBackupDiagnostic: () => mockDiagnostic, + currentBackupAccountSession: () => mockSession, +})); +jest.mock('src/store/reducers/bhr', () => ({ + isAutomaticCloudBackupEnabled: (state, id) => state.automaticCloudBackupByAppId[id] === true, + ...Object.fromEntries( + [ + 'setBackupAllFailure', + 'setBackupAllSuccess', + 'setAutomaticCloudBackup', + 'setDeleteBackupSuccess', + 'setDeleteBackupFailure', + ].map((type) => [type, (payload) => ({ type, payload })]) + ), +})); +jest.mock('src/store/reducers/settings', () => ({ + setShowTipModal: (payload) => ({ type: 'setShowTipModal', payload }), + setThemeMode: (payload) => ({ type: 'setThemeMode', payload }), +})); +jest.mock('src/store/reducers/login', () => ({ credsAuthenticated: jest.fn() })); +jest.mock('src/components/Switch/Switch', () => 'Switch'); +jest.mock('src/components/KeeperText', () => 'Text'); +jest.mock('src/components/ScreenWrapper', () => 'Screen'); +jest.mock('src/components/WalletHeader', () => 'Header'); +jest.mock('src/components/ThemedSvg.tsx/ThemedSvg', () => 'ThemedSvg'); +jest.mock('src/utils/service-utilities/config', () => ({ + __esModule: true, + default: { ADDRESS: {} }, +})); +jest.mock( + 'src/components/KeeperModal', + () => + ({ visible, Content, title }) => + visible ? require('react').createElement('Modal', { title }, Content && ) : null +); +// The baseline uses shared Buttons; preserve their callback behavior so the +// before/after control checks exercise the screen rather than the test mock. +jest.mock( + 'src/components/Buttons', + () => + ({ primaryText, primaryCallback, secondaryText, secondaryCallback }) => + ( + <> + {primaryText && + require('react').createElement('Action', { + label: primaryText, + onPress: primaryCallback, + })} + {secondaryText && + require('react').createElement('Action', { + label: secondaryText, + onPress: secondaryCallback, + })} + + ) +); +jest.mock('src/context/Localization/LocContext', () => ({ + LocalizationContext: require('react').createContext({ + translations: require('../../src/context/Localization/language/en.json'), + }), +})); + +const hookFile = path.join(__dirname, '../../src/hooks/useSettingKeeper.tsx'); +const icons = fs.readFileSync(hookFile, 'utf8').matchAll(/from '(src\/assets\/[^']+\.svg)'/g); +for (const [, filename] of icons) + jest.doMock(require.resolve(`../../${filename}`), () => () => null); +const { useSettingKeeper } = require('../../src/hooks/useSettingKeeper'); +const Status = require('../../src/screens/AppSettings/AssistedBackupStatus').default; +let settings; +function SettingsHarness() { + settings = useSettingKeeper(); + return ( + <> + {settings.DeleteBackupModal} + {settings.BackupFeedback} + + ); +} +let screen; +function mount(Component = Status) { + act(() => { + screen = renderer.create(); + }); + mockDispatch.mockClear(); +} +function press(label: string) { + const action = screen.root.findAll( + (node) => node.props.label === label && typeof node.props.onPress === 'function' + )[0]; + expect(action).toBeDefined(); + act(() => action.props.onPress()); +} +function openBackupChoices() { + act(() => { + settings.BackAndRecovery.find( + (item) => item.title === copy.settings.assistedServerBackup + ).onRightPress(); + jest.runOnlyPendingTimers(); + }); +} +beforeEach(() => { + jest.useFakeTimers(); + mockApp.id = 'disposable-upgraded-account'; + mockSession = 1; + mockState.bhr = { + automaticCloudBackupByAppId: { [mockApp.id]: true }, + backupRepairStateByAppId: { [mockApp.id]: 'unverified' }, + backupRepairRunningByAppId: {}, + protectedBackupStateByAppId: {}, + }; + mockDiagnostic = undefined; + mockToast.mockClear(); + mockDispatch.mockClear(); +}); +afterEach(() => { + act(() => screen?.unmount()); + jest.useRealTimers(); +}); + +test('enabled unverified backup offers explicit backup while Check Again remains read-only', () => { + mount(); + press(copy.recoveryBackup.check); + expect(mockDispatch.mock.calls.map(([action]) => action)).toEqual([ + { type: 'CHECK_BACKUP_FRESHNESS', appId: mockApp.id }, + ]); + mockDispatch.mockClear(); + press(copy.recoveryBackup.backUp); + expect(mockDispatch.mock.calls.map(([action]) => action)).toEqual([ + { type: 'REPAIR_BACKUP', appId: mockApp.id }, + ]); +}); + +test.each(['deleted', 'conflict'])( + '%s status never offers an update that might replace recovery data', + (phase) => { + mockState.bhr.backupRepairStateByAppId[mockApp.id] = phase; + mount(); + expect( + screen.root.findAll((node) => node.props.label === copy.recoveryBackup.backUp) + ).toHaveLength(0); + press(copy.recoveryBackup.check); + expect(mockDispatch).toHaveBeenCalledWith({ + type: 'CHECK_BACKUP_FRESHNESS', + appId: mockApp.id, + }); + } +); + +test('turning off automatic backup dispatches consent-off only and leaves persistent readable feedback', () => { + mount(SettingsHarness); + openBackupChoices(); + press(copy.settings.assistedServerDeleteBackupModalSecondaryCTA); + expect(mockDispatch.mock.calls.map(([action]) => action)).toEqual([ + { type: 'setAutomaticCloudBackup', payload: { appId: mockApp.id, enabled: false } }, + ]); + expect(JSON.stringify(screen.toJSON())).toContain(copy.settings.assistedServerBackupDisabled); + expect(mockToast).not.toHaveBeenCalled(); + press(copy.recoveryBackup.dismiss); + expect(JSON.stringify(screen.toJSON())).not.toContain(copy.settings.assistedServerBackupDisabled); +}); + +test('Delete protected backup requests deletion without falsely reporting completion or turning off consent', () => { + mount(SettingsHarness); + openBackupChoices(); + press(copy.settings.assistedServerDeleteBackupModalCTA); + expect(mockDispatch.mock.calls.map(([action]) => action)).toEqual([ + { type: 'DELETE_BACKUP', appId: mockApp.id }, + ]); + expect(JSON.stringify(screen.toJSON())).not.toContain( + copy.settings.assistedServerDeleteBackupConfirmed + ); +}); + +test('last bounded check detail is shown only after an explicit request', () => { + mockDiagnostic = { + operation: 'check', + stage: 'snapshot-request', + category: 'http-4xx', + httpStatus: 401, + elapsedMs: 250, + }; + mount(); + expect(JSON.stringify(screen.toJSON())).not.toContain('snapshot-request'); + press(copy.recoveryBackup.showDetails); + expect(JSON.stringify(screen.toJSON())).toContain('check / snapshot-request / http-4xx'); + expect(JSON.stringify(screen.toJSON())).toContain('401'); + press(copy.recoveryBackup.hideDetails); + expect(JSON.stringify(screen.toJSON())).not.toContain('snapshot-request'); +}); + +test('View status preserves deletion failure detail until a read-only recheck is requested', () => { + mockDiagnostic = { + operation: 'delete', + stage: 'snapshot-request', + category: 'http-4xx', + httpStatus: 401, + elapsedMs: 75, + }; + act(() => { + screen = renderer.create(); + }); + expect(mockDispatch).not.toHaveBeenCalled(); + press(copy.recoveryBackup.showDetails); + expect(JSON.stringify(screen.toJSON())).toContain('delete / snapshot-request / http-4xx'); + press(copy.recoveryBackup.check); + expect(mockDispatch).toHaveBeenCalledWith({ type: 'CHECK_BACKUP_FRESHNESS', appId: mockApp.id }); +}); + +test('old feedback is discarded after an account session changes, including returning to the same account', () => { + mount(SettingsHarness); + openBackupChoices(); + press(copy.settings.assistedServerDeleteBackupModalSecondaryCTA); + expect(JSON.stringify(screen.toJSON())).toContain(copy.settings.assistedServerBackupDisabled); + mockApp.id = 'disposable-other-account'; + mockSession += 1; + act(() => screen.update()); + mockApp.id = 'disposable-upgraded-account'; + mockSession += 1; + act(() => screen.update()); + expect(JSON.stringify(screen.toJSON())).not.toContain(copy.settings.assistedServerBackupDisabled); +}); + +test('authenticated never-enrolled deletion feedback never claims deletion and directs explicit backup', () => { + mockState.bhr.deleteBackupFailureByAppId = { [mockApp.id]: true }; + mockDiagnostic = { + operation: 'delete', + stage: 'snapshot-request', + category: 'never-enrolled', + elapsedMs: 20, + }; + mount(SettingsHarness); + expect(JSON.stringify(screen.toJSON())).toContain(copy.recoveryBackup.deleteNotEnrolled); + expect(JSON.stringify(screen.toJSON())).not.toContain( + copy.settings.assistedServerDeleteBackupConfirmed + ); +}); + +test('consent-off status offers only a read-only recheck', () => { + mockState.bhr.automaticCloudBackupByAppId[mockApp.id] = false; + mount(); + expect( + screen.root.findAll((node) => node.props.label === copy.recoveryBackup.backUp) + ).toHaveLength(0); + press(copy.recoveryBackup.check); + expect(mockDispatch.mock.calls.map(([action]) => action)).toEqual([ + { type: 'CHECK_BACKUP_FRESHNESS', appId: mockApp.id }, + ]); +}); + +test('enabled never-enrolled status offers explicit backup without creating anything on mount', () => { + mockState.bhr.backupRepairStateByAppId[mockApp.id] = 'never-enrolled'; + mount(); + press(copy.recoveryBackup.backUp); + expect(mockDispatch.mock.calls.map(([action]) => action)).toEqual([ + { type: 'REPAIR_BACKUP', appId: mockApp.id }, + ]); +}); + +test('a retained deletion record blocks backup retry after an uncertain recheck even while automatic remains on', () => { + mockState.bhr.protectedBackupStateByAppId[mockApp.id] = { status: 'deleted' }; + mount(); + expect( + screen.root.findAll((node) => node.props.label === copy.recoveryBackup.backUp) + ).toHaveLength(0); + press(copy.recoveryBackup.check); + expect(mockDispatch.mock.calls.map(([action]) => action)).toEqual([ + { type: 'CHECK_BACKUP_FRESHNESS', appId: mockApp.id }, + ]); +}); From c1c4b803039b61b940373c4dc9d08473cf25e042 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Thu, 22 Oct 2026 05:18:45 +0000 Subject: [PATCH 212/254] fix: reconcile version history with the selected account and installed app --- docs/7066-protected-backup-correction.md | 76 ++++- src/context/Localization/language/en.json | 1 + src/context/Localization/language/es.json | 1 + src/screens/AppSettings/AppVersionHistoty.tsx | 18 +- src/store/sagas/login.ts | 43 ++- src/store/sagas/upgrade.ts | 43 ++- .../versionHistoryReconciliation.test.cjs | 322 ++++++++++++++++++ tests/screens/AppVersionHistory.test.tsx | 91 +++++ tests/store/versionHistory.test.ts | 19 +- 9 files changed, 583 insertions(+), 31 deletions(-) create mode 100644 tests/recovery/versionHistoryReconciliation.test.cjs create mode 100644 tests/screens/AppVersionHistory.test.tsx diff --git a/docs/7066-protected-backup-correction.md b/docs/7066-protected-backup-correction.md index 9032b832da..8de4b3b9e5 100644 --- a/docs/7066-protected-backup-correction.md +++ b/docs/7066-protected-backup-correction.md @@ -1,4 +1,4 @@ -# Protected-backup correction — #7066 +# Protected-backup and Version History follow-up — #7066 / #7067 ## Source and scope @@ -7,7 +7,10 @@ This patch uses the owner-designated completed R5 implementation source Live records checked on 22 October 2026 show an open draft with `NEEDS_LOCAL`, not a publicly released 2.6.5. There is no 2.6.5 GitHub tag/release. The source still declares 2.6.4 (Android 627, iOS 620); this patch assigns no version. -Store publication/availability has not been checked or inferred. +Store publication/availability has not been independently checked. The owner +reports the App Store showing 2.6.4 after updating, while Keeper's Version History +still ends at 2.6.3. The actual running binary/build is not established by that +historical row or by the store listing alone. Its ancestry includes released R4 source `1d8a86a7462d935cbd0e28b857934b5718c152fc` and `sprint` `def0d6a90c80287edb4f58ae272cbfc340f00d73`. @@ -19,11 +22,13 @@ Live searches found no other #7066 PR/implementation. R5 and the original checkout remain unchanged. The PR targets `sprint` per AGENTS.md and links a patch-only comparison from the exact R5 SHA. -The provisional code/test estimate was 5k–10k changed tokens; after diagnosis the -estimate is 10k–12k, including workflow/privacy regressions and the supplemental -current-backend witness. This remains one backup correction and its adjacent UI, -with no legacy all-copy purge, protocol redesign, release change, or parked -investigation. +The provisional code/test estimate was 5k–10k changed tokens; the first backup +patch measured approximately 12.1k, including workflow/privacy regressions and +the supplemental current-backend witness. The owner subsequently authorized the +stale Version History correction in the same follow-up (called the "5.1 patch" +in the conversation). The revised estimate is approximately 16k–17k code/test +tokens. This is a scope label, not a new app release version. No legacy all-copy +purge, protocol redesign, release change, or parked investigation is included. ## Evidence and limits @@ -68,6 +73,47 @@ session and is cleared on account changes/new operations. It never displays or persists caught errors, request/response bodies, headers, identifiers, ciphertext or recovery contents. Do not request raw logs to fill the remaining evidence gap. +## Owner-added Version History correction + +The app's history list reads local Realm records; it is separate from both the +App Store listing and the running binary's native version/build. Source diagnosis +found a shared Redux version controlling account-specific upgrades, an unguarded +last-row access, and a comparison of only the last build number. Those conditions +can skip a selected account's upgrade, fail on missing history, or leave a stale +marketing-version row when build numbers match. They are reproducible source +defects, not confirmation of the original phone's precise cause. + +Use the selected account's valid persisted version for migration decisions, +retaining the legacy fallback only when that account version is absent. That +fallback cannot establish a current account or justify an observation by itself. +Compare +the complete native version/build tuple when reconciling history. A missing +current row can be recorded as a current installation observation only after a +successful upgrade or when that account is already current. Preserve existing +recovery/installation rows and dates; do not fabricate a successful transition +after a failed migration or grant backup verification. Account/session ownership +and atomic migration rollback remain required. + +The Version History screen separately labels the running native version/build +as **Installed app version**. Historical rows are retained as historical evidence, +including when they disagree with the running version. The store listing must +never supply a fabricated installed version. + +Native acceptance for this addition: + +- Upgrade a disposable 2.6.3 account and verify the installed native version/build, + account version and history entry after normal unlock. Repeat with another + account that has already advanced the shared version value. +- Test empty/stale history, the same build across different marketing versions, + build-only changes, repeated cold launches, and an existing Recovered Wallet + record. No duplicates, invented upgrade dates, or overwritten recovery labels. +- Inject a failed local migration and switch A → B → A during the operation. + Failure must not append a success/observation row for the attempted upgrade or + write another account/session. Backup consent/readback semantics stay intact. +- Check the installed-version label and history on iOS/Android at small width and + larger text. On the owner's phone, only normal navigation/reopen/unlock and + reading version/build are permitted; no reset, reinstall or recovery operation. + ## Targeted native acceptance Run on the exact final PR head, with the actual compatible backend selected and @@ -159,14 +205,16 @@ Package, lockfile, Pod inputs and version consistency remain unchanged. | Check | Actual result | | --- | --- | -| `npm run test -- --runInBand --coverage=false` | PASS: 48 suites, 550 tests, zero skips/failures | +| `npm run test -- --runInBand --coverage=false` | PASS after history addition: 49 suites, 553 tests, zero skips/failures | | `node --test tests/recovery/backupDiagnostic.test.cjs tests/recovery/transport.test.cjs tests/recovery/repair.test.cjs` | PASS: 70 tests, zero skips/failures | | UI interaction suite | PASS: 12; exact-baseline controls FAIL 2 assertions, 10 deliberately deselected (enabled never-enrolled and unverified backup action absent) | | Diagnostic baseline controls | FAIL: all 9 before; PASS: all 9 after; these establish diagnostic coverage, not the phone's request cause | -| `npm run test:recovery` | FAIL: 479 pass, one test-file load failure, zero skips; missing Realm binding prevents the 10 encrypted-Realm scenarios from loading | +| `npm run test:recovery` | FAIL after history addition: 492 pass, one test-file load failure (493 total), zero skips; missing Realm binding prevents the 10 encrypted-Realm scenarios from loading | +| History reconciliation and upgrade/login tests | PASS: 27 Node cases (13 new); Version History Jest PASS: 9; exact-before controls FAIL: 4 actual-login cases and one actual-migration preservation case | +| Installed-version header | PASS: 2 UI regressions; both FAIL before; native version/history disagreement tested in both directions without rewriting rows | | Supplemental current-backend upgrade witness | BLOCKED by missing Realm native binding; no passing upgraded-account integration claim | | Current Relay protected/legacy protocol tests on disposable Mongo | PASS: 45; readiness tests PASS: 7; backend only, not upgraded-app acceptance | -| Whole ESLint | FAIL: 4450 errors / 882 warnings; exact baseline 4451 / 882; normalized zero added, one removed | +| Whole ESLint | FAIL: 4450 errors / 882 warnings; R5 baseline 4451 / 882, first patch 4450 / 882; normalized zero added by the history addition | | Whole TypeScript | FAIL: 943 diagnostics; exact baseline 943; normalized zero added/removed | | Focused backup service ESLint | PASS: zero errors/warnings | | CI USDT/retired-configuration guard command | FAIL: 30 pass / 11 fail, all 11 require unavailable Ruby | @@ -188,3 +236,11 @@ The task-owned disposable Mongo container is stopped; its data/evidence are retained. Passing backend checks do not replace the blocked upgraded-account witness. Existing formatting/lint/type failures are reported, not waived. No independent cryptographic certification or release clearance is claimed. + +The history before-controls use isolated source +`3c5f8d8ace5c211ed78264b1a460c4e06e060ceb` (before the owner-added history +correction). The current-backend 45+7 results and release/tooling results were +executed for that first patch and retained as prior evidence; they were not +rerun after the history-only addition. Current full Jest/recovery, lint/type, +version/input and targeted history checks are reported separately above. IOS, +ANDROID, HARDWARE and the original phone still require exact-head acceptance. diff --git a/src/context/Localization/language/en.json b/src/context/Localization/language/en.json index 00d0eebcac..35f95a0231 100644 --- a/src/context/Localization/language/en.json +++ b/src/context/Localization/language/en.json @@ -1062,6 +1062,7 @@ "ManageWalletsSub": "Hide/Unhide Wallets From Homescreen", "torSettingTitle": "Tor Settings", "torSettingSubTitle": "Configure in-app Tor and Orbot", + "installedAppVersion": "Installed app version", "versionHistoryTitle": "Version History", "versionHistorySubTitle": "You can verify the history of your app upgrades here", "torHeaderSubTitle": "Tor improves your network privacy. To learn more visit: https://www.torproject.org/", diff --git a/src/context/Localization/language/es.json b/src/context/Localization/language/es.json index 4031ff1843..5f9e0ca12b 100644 --- a/src/context/Localization/language/es.json +++ b/src/context/Localization/language/es.json @@ -1050,6 +1050,7 @@ "ManageWalletsSub": "Hide/Unhide Wallets From Homescreen", "torSettingTitle": "Tor Settings", "torSettingSubTitle": "Configure in-app Tor and Orbot", + "installedAppVersion": "Versión instalada de la app", "versionHistoryTitle": "Version History", "versionHistorySubTitle": "You can verify the history of your app upgrades here", "torHeaderSubTitle": "Tor improves your network privacy. To learn more visit: https://www.torproject.org/", diff --git a/src/screens/AppSettings/AppVersionHistoty.tsx b/src/screens/AppSettings/AppVersionHistoty.tsx index 6e411f6029..1304a8ed69 100644 --- a/src/screens/AppSettings/AppVersionHistoty.tsx +++ b/src/screens/AppSettings/AppVersionHistoty.tsx @@ -1,7 +1,8 @@ import React, { useContext, useState } from 'react'; import Text from 'src/components/KeeperText'; -import { ScrollView, useColorMode } from '@gluestack-ui/themed-native-base'; +import { Box, ScrollView, useColorMode } from '@gluestack-ui/themed-native-base'; import { Pressable, StyleSheet } from 'react-native'; +import DeviceInfo from 'react-native-device-info'; import VersionHistoryList from 'src/components/SettingComponent/VersionHistoryList'; import ScreenWrapper from 'src/components/ScreenWrapper'; import { RealmSchema } from 'src/storage/realm/enum'; @@ -35,6 +36,14 @@ function AppVersionHistory({ navigation }: any) { subTitle={settings.versionHistorySubTitle} /> + + + {settings.installedAppVersion} + + + {`${DeviceInfo.getVersion()} (${DeviceInfo.getBuildNumber()})`} + + @@ -44,6 +53,13 @@ function AppVersionHistory({ navigation }: any) { ); } const styles = StyleSheet.create({ + installedVersion: { + marginBottom: hp(24), + }, + version: { + fontSize: 16, + marginTop: hp(8), + }, textAppId: { textAlign: 'center', fontSize: 10, diff --git a/src/store/sagas/login.ts b/src/store/sagas/login.ts index 22b074b5de..0ba70addca 100644 --- a/src/store/sagas/login.ts +++ b/src/store/sagas/login.ts @@ -52,7 +52,7 @@ import { fetchExchangeRates } from '../sagaActions/send_and_receive'; import { setLoginMethod } from '../reducers/settings'; import { setSubscription } from 'src/store/sagaActions/settings'; import { uaiChecks } from '../sagaActions/uai'; -import { applyUpgradeSequence } from './upgrade'; +import { applyUpgradeSequence, observeCurrentVersionHistory } from './upgrade'; import { captureBackupWriteIntent, currentBackupAccountSession } from 'src/services/backup/transport'; import { resetSyncing } from '../reducers/wallets'; import { autoSyncWallets } from '../sagaActions/wallets'; @@ -197,7 +197,7 @@ function* credentialsAuthWorker({ payload }) { throw Error(`Failed to load the database: ${error}`); } - const previousVersion = yield select((state) => state.storage.appVersion); + const persistedVersion = yield select((state) => state.storage.appVersion); const { defaultWalletCreated } = yield select((state) => state.storage); // setting correct app id from realm at login @@ -235,28 +235,35 @@ function* credentialsAuthWorker({ payload }) { } const newVersion = DeviceInfo.getVersion(); - const versionCollection = yield call(dbManager.getCollection, RealmSchema.VersionHistory); - const lastElement = versionCollection[versionCollection.length - 1]; - const lastVersionCode = lastElement.version.split(/[()]/); const currentVersionCode = DeviceInfo.getBuildNumber(); if ( currentBackupAccountSession() !== loginAccountSession || store.getState().storage.appId !== expectedAppId ) return; - if (semver.lt(previousVersion, newVersion)) { - yield call(applyUpgradeSequence, { - previousVersion, - newVersion, - originBackupAccountSession: loginAccountSession, - originBackupWriteIntent: loginBackupWriteIntent, - }); - } else if (currentVersionCode !== lastVersionCode[1]) { - yield call(dbManager.createObject, RealmSchema.VersionHistory, { - version: `${newVersion}(${currentVersionCode})`, - date: new Date().toString(), - title: `Upgraded from ${lastVersionCode[1]} to ${currentVersionCode}`, - }); + if (keeperApp?.id) { + // Redux's version is shared by accounts. A different account may have + // upgraded already; migrations must follow this selected Realm account. + const previousVersion = keeperApp.version ?? persistedVersion; + if (!semver.valid(previousVersion)) throw new Error('Account version unavailable'); + let current = keeperApp.version === newVersion; + if (semver.lt(previousVersion, newVersion)) { + current = + (yield call(applyUpgradeSequence, { + previousVersion, + newVersion, + originBackupAccountSession: loginAccountSession, + originBackupWriteIntent: loginBackupWriteIntent, + })) === true; + } + if (current) + yield call( + observeCurrentVersionHistory, + keeperApp.id, + newVersion, + currentVersionCode, + loginAccountSession + ); } if ( currentBackupAccountSession() !== loginAccountSession || diff --git a/src/store/sagas/upgrade.ts b/src/store/sagas/upgrade.ts index fe67f77f67..876beefa20 100644 --- a/src/store/sagas/upgrade.ts +++ b/src/store/sagas/upgrade.ts @@ -2,6 +2,7 @@ import { call, put, select } from 'redux-saga/effects'; import Realm from 'realm'; import semver from 'semver'; import dbManager from 'src/storage/realm/dbManager'; +import realmDatabase from 'src/storage/realm/realm'; import { RealmSchema } from 'src/storage/realm/enum'; import DeviceInfo from 'react-native-device-info'; import { getReleaseTopic } from 'src/utils/releaseTopic'; @@ -149,7 +150,8 @@ function stageVersionUpdate( if (!app) throw new Error('Upgrade account unavailable'); app.version = newVersion; // Recovery records its own single Recovered Wallet entry. - if (!isRecovery) realm.create(RealmSchema.VersionHistory, history, Realm.UpdateMode.Modified); + if (!isRecovery && !realm.objectForPrimaryKey(RealmSchema.VersionHistory, history.version)) + realm.create(RealmSchema.VersionHistory, history, Realm.UpdateMode.Modified); }); group.appImage = true; } @@ -160,6 +162,45 @@ function updateReleaseTopics(previousVersion: string, newVersion: string) { subscribeToTopic(messaging, getReleaseTopic(newVersion)); } +// A history row is evidence of an observation, not proof that a migration ran. +// Reconcile only an account already at this installed version. Never replace +// its existing initial-install/recovery provenance or invent an upgrade date. +export function observeCurrentVersionHistory( + appId: string, + version: string, + build: string, + session = currentBackupAccountSession() +) { + try { + if (!semver.valid(version) || !/^[0-9]+(?:\.[0-9]+){0,2}$/.test(build)) return false; + const database = realmDatabase.getDatabase(); + const assertCurrent = () => { + if ( + !upgradeAccountIsActive(appId, session) || + realmDatabase.getDatabase() !== database || + database.objectForPrimaryKey(RealmSchema.KeeperApp, appId)?.version !== version + ) + throw new Error('Version history account changed'); + }; + assertCurrent(); + const key = `${version}(${build})`; + if (database.objectForPrimaryKey(RealmSchema.VersionHistory, key)) return true; + database.write(() => { + assertCurrent(); + if (!database.objectForPrimaryKey(RealmSchema.VersionHistory, key)) + database.create(RealmSchema.VersionHistory, { + version: key, + date: new Date().toString(), + title: 'Current installation observed', + }); + assertCurrent(); + }); + return true; + } catch { + return false; + } +} + export function* applyUpgradeSequence({ previousVersion, newVersion, diff --git a/tests/recovery/versionHistoryReconciliation.test.cjs b/tests/recovery/versionHistoryReconciliation.test.cjs new file mode 100644 index 0000000000..160d41b94d --- /dev/null +++ b/tests/recovery/versionHistoryReconciliation.test.cjs @@ -0,0 +1,322 @@ +const assert = require('node:assert/strict'); +const { test } = require('node:test'); +const fs = require('node:fs'); +const path = require('node:path'); +const vm = require('node:vm'); +const ts = require('typescript'); +const { runSaga } = require('redux-saga'); +const effects = require('redux-saga/effects'); +const plain = (value) => JSON.parse(JSON.stringify(value)); + +function declarations(file, names) { + const source = ts.createSourceFile( + file, + fs.readFileSync(path.join(__dirname, '../..', file), 'utf8'), + ts.ScriptTarget.Latest, + true + ); + return ts.transpileModule( + source.statements + .filter( + (node) => + (ts.isFunctionDeclaration(node) && names.includes(node.name?.text)) || + (ts.isVariableStatement(node) && + node.declarationList.declarations.some((value) => + names.includes(value.name.getText(source)) + )) + ) + .map((node) => + node + .getText(source) + .replace(/^export /, '') + .replace(/^const /, 'var ') + ) + .join('\n'), + { compilerOptions: { target: ts.ScriptTarget.ES2020 } } + ).outputText; +} + +// Production login/observation orchestration; disposable synchronous transaction +// adapter only. Native Realm and the owner's installation are not exercised. +function fixture(options = {}) { + const app = { id: 'A', version: '2.6.4', ...options.app }; + const state = { + session: 7, + writes: 0, + upgrades: [], + actions: [], + history: plain( + options.history ?? [{ version: '2.6.3(620)', date: 'old date', title: 'Old entry' }] + ), + storage: { + appId: 'A', + appVersion: options.persistedVersion ?? '2.6.4', + defaultWalletCreated: {}, + }, + account: { + allAccounts: [{ hash: 'fixture-hash', appId: 'A', realmId: 'fixture.realm' }], + backupMethodByAppId: {}, + }, + }; + let selectedDatabase; + const database = { + objectForPrimaryKey: (schema, key) => + schema === 'KeeperApp' + ? app.id === key + ? app + : undefined + : state.history.find((row) => row.version === key), + write(callback) { + const before = plain(state.history); + const previousVersion = app.version; + try { + callback(); + } catch (error) { + state.history = before; + app.version = previousVersion; + throw error; + } + }, + create(_schema, record) { + if (options.failHistory) throw Error('disposable history failure'); + const index = state.history.findIndex((row) => row.version === record.version); + if (index < 0) state.history.push(plain(record)); + else state.history[index] = plain(record); + state.writes++; + options.onCreate?.({ + state, + replaceDatabase: () => { + selectedDatabase = {}; + }, + }); + }, + }; + selectedDatabase = database; + const scope = { + ...effects, + store: { getState: () => state }, + semver: require('semver'), + RealmSchema: { KeeperApp: 'KeeperApp', VersionHistory: 'VersionHistory' }, + realmDatabase: { getDatabase: () => selectedDatabase }, + currentBackupAccountSession: () => state.session, + captureBackupWriteIntent: () => undefined, + LoginMethod: { PIN: 'PIN' }, + hash512: () => 'fixture-hash', + decrypt: () => 'fixture-key', + SecureStore: { fetch: () => 'fixture-ciphertext' }, + stringToArrayBuffer: () => new Uint8Array([1]), + DeviceInfo: { getVersion: () => '2.6.4', getBuildNumber: () => options.build ?? '620' }, + console: { log() {} }, + dbManager: { + initializeRealm: () => ({ success: true }), + getObjectByIndex: () => app, + getCollection: (schema) => (schema === 'VersionHistory' ? state.history : []), + createObject: database.create, + }, + Realm: { UpdateMode: { Modified: 'modified' } }, + beginBackupLocalOperation: (appId, _intent, session) => ({ appId, session }), + commitBackupLocalOperation: (_operation, callback) => { + if (options.migrationResult === false) return { committed: false }; + database.write(() => callback(database)); + return { committed: true }; + }, + buildAutomaticAppMutation: () => ({ operation: 'updateAppImage' }), + enqueueAutomaticBackupOperation: async () => ({ backupConfirmed: false }), + notifyBackupReconciliation() {}, + captureError() {}, + getApp: () => ({}), + getMessaging: () => ({}), + getReleaseTopic: (value) => value, + unsubscribeFromTopic() {}, + subscribeToTopic() {}, + uaiType: new Proxy({}, { get: (_, name) => name }), + }; + for (const name of [ + 'credsAuthenticatedError', + 'setRecepitVerificationError', + 'setRecepitVerificationFailed', + 'setupLoading', + 'setKey', + 'setPinHash', + 'migrateLegacyAutomaticCloudBackup', + 'setAppId', + 'setTempDetails', + 'credsAuthenticated', + 'connectToNode', + 'fetchExchangeRates', + 'fetchSignedDelayedTransaction', + 'fetchDelayedPolicyUpdate', + 'resetSyncing', + 'autoSyncWallets', + 'uaiChecks', + 'loadConciergeUserOnLogin', + 'setLoginMethod', + 'setAppVersion', + ]) + scope[name] = (payload) => ({ type: name, payload }); + vm.createContext(scope); + vm.runInContext( + declarations('src/store/sagas/upgrade.ts', [ + 'upgradeAccountIsActive', + 'observeCurrentVersionHistory', + 'beginUpgradeGroup', + 'completeUpgradeGroup', + 'stageVersionUpdate', + 'updateReleaseTopics', + 'applyUpgradeSequence', + 'LABELS_INTRODUCTION_VERSION', + 'BIP329_INTRODUCTION_VERSION', + 'KEY_MANAGEMENT_VERSION', + 'APP_KEY_UPGRADE_VERSION', + 'HEALTH_CHECK_TIMELINE_MIGRATION_VERSION', + 'SIGNER_POLICY_MIGRATION_VERSION', + ]), + scope + ); + const productionUpgrade = scope.applyUpgradeSequence; + scope.applyUpgradeSequence = function* (action) { + state.upgrades.push(plain(action)); + return yield* productionUpgrade(action); + }; + vm.runInContext(declarations('src/store/sagas/login.ts', ['credentialsAuthWorker']), scope); + const login = () => + runSaga( + { getState: () => state, dispatch: (action) => state.actions.push(action), onError() {} }, + scope.credentialsAuthWorker, + { payload: { method: 'PIN', reLogin: false } } + ).toPromise(); + const observe = (session = state.session) => + scope.observeCurrentVersionHistory('A', '2.6.4', options.build ?? '620', session); + return { app, state, login, observe }; +} + +test('selected older Realm account still upgrades when another account advanced the Redux version', async () => { + const f = fixture({ app: { version: '2.6.3' } }); + await f.login(); + assert.equal(f.state.upgrades.length, 1); + assert.equal(f.state.upgrades[0].previousVersion, '2.6.3'); + assert.equal(f.app.version, '2.6.4'); + assert.match(f.state.history.at(-1).title, /^Upgraded from 2\.6\.3 to 2\.6\.4$/); +}); + +test('missing current tuple is observed even when a stale marketing row has the same build', async () => { + const f = fixture(); + const old = plain(f.state.history[0]); + await f.login(); + assert.equal(f.state.upgrades.length, 0); + assert.equal(f.state.history.length, 2); + assert.deepEqual(f.state.history[0], old); + assert.equal(f.state.history[1].version, '2.6.4(620)'); + assert.equal(f.state.history[1].title, 'Current installation observed'); +}); + +test('empty history does not block login and records only current installation evidence', async () => { + const f = fixture({ history: [] }); + await f.login(); + assert.ok( + f.state.actions.some( + (action) => action.type === 'credsAuthenticated' && action.payload === true + ) + ); + assert.equal(f.state.history.length, 1); + assert.equal(f.state.history[0].title, 'Current installation observed'); +}); + +test('current recovery entry anywhere in history retains its provenance and date', async () => { + const history = [ + { version: '2.6.4(620)', title: 'Recovered Wallet', date: 'recovery date' }, + { version: '2.6.3(619)', title: 'Old imported entry', date: 'older date' }, + ]; + const f = fixture({ history }); + await f.login(); + assert.deepEqual(f.state.history, history); + assert.equal(f.state.writes, 0); +}); + +test('repeated login does not duplicate or replace an observed entry', async () => { + const f = fixture({ history: [] }); + await f.login(); + const observed = plain(f.state.history); + await f.login(); + assert.deepEqual(f.state.history, observed); + assert.equal(f.state.writes, 1); +}); + +test('unsuccessful migrations never append current history or claim an upgrade', async () => { + const f = fixture({ app: { version: '2.6.3' }, migrationResult: false }); + const before = plain(f.state.history); + await f.login(); + assert.equal(f.state.upgrades.length, 1); + assert.equal(f.app.version, '2.6.3'); + assert.deepEqual(f.state.history, before); +}); + +test('legacy missing account version uses the existing persisted baseline', async () => { + const f = fixture({ app: { version: undefined }, persistedVersion: '2.6.3' }); + await f.login(); + assert.equal(f.state.upgrades.length, 1); + assert.equal(f.state.upgrades[0].previousVersion, '2.6.3'); + assert.equal(f.app.version, '2.6.4'); + assert.equal(f.state.history.at(-1).title, 'Upgraded from 2.6.3 to 2.6.4'); + assert.equal(f.state.history.filter((row) => row.version === '2.6.4(620)').length, 1); +}); + +test('legacy missing account version cannot infer current history from Redux alone', async () => { + const f = fixture({ app: { version: undefined }, history: [] }); + await f.login(); + assert.equal(f.state.upgrades.length, 0); + assert.equal(f.app.version, undefined); + assert.deepEqual(f.state.history, []); + assert.ok( + f.state.actions.some( + (action) => action.type === 'credsAuthenticated' && action.payload === true + ) + ); +}); + +test('defined invalid account version cannot silently use another account version', async () => { + const f = fixture({ app: { version: 'invalid' }, persistedVersion: '2.6.3' }); + const before = plain(f.state.history); + await f.login(); + assert.equal(f.state.upgrades.length, 0); + assert.deepEqual(f.state.history, before); + assert.ok( + f.state.actions.some( + (action) => action.type === 'credsAuthenticated' && action.payload === false + ) + ); +}); + +test('history observation cannot run under an old A to B to A account session', () => { + const f = fixture(); + f.state.session += 2; + assert.equal(f.observe(7), false); + assert.equal(f.state.writes, 0); +}); + +test('history observation requires the account version to be current', () => { + const f = fixture({ app: { version: '2.6.3' } }); + assert.equal(f.observe(), false); + assert.equal(f.state.writes, 0); +}); + +test('account or Realm replacement during history transaction rolls back its row', () => { + for (const change of [ + ({ state }) => { + state.session++; + }, + ({ replaceDatabase }) => replaceDatabase(), + ]) { + const f = fixture({ onCreate: change }); + const before = plain(f.state.history); + assert.equal(f.observe(), false); + assert.deepEqual(f.state.history, before); + } +}); + +test('local history write failure leaves existing history intact', () => { + const f = fixture({ failHistory: true }); + const before = plain(f.state.history); + assert.equal(f.observe(), false); + assert.deepEqual(f.state.history, before); +}); diff --git a/tests/screens/AppVersionHistory.test.tsx b/tests/screens/AppVersionHistory.test.tsx new file mode 100644 index 0000000000..03de4a669e --- /dev/null +++ b/tests/screens/AppVersionHistory.test.tsx @@ -0,0 +1,91 @@ +import React from 'react'; +import renderer, { act } from 'react-test-renderer'; + +type HistoryRecord = { title: string; version: string; date: number }; +type HistoryListProps = { + data: HistoryRecord[]; + renderItem: (entry: { item: HistoryRecord }) => React.ReactNode; + keyExtractor: (item: HistoryRecord) => string; +}; + +let mockNativeVersion = '2.6.4'; +let mockNativeBuild = '620'; +let mockHistory: HistoryRecord[] = []; +jest.mock('react-native-device-info', () => ({ + __esModule: true, + default: { + getVersion: () => mockNativeVersion, + getBuildNumber: () => mockNativeBuild, + }, +})); +jest.mock('@gluestack-ui/themed-native-base', () => ({ + Box: 'Box', + ScrollView: 'ScrollView', + FlatList: ({ data, renderItem, keyExtractor }: HistoryListProps) => ( + <> + {data.map((item) => + require('react').createElement( + require('react').Fragment, + { key: keyExtractor(item) }, + renderItem({ item }) + ) + )} + + ), + useColorMode: () => ({ colorMode: 'light' }), +})); +jest.mock('@realm/react', () => ({ useQuery: () => mockHistory })); +jest.mock('src/storage/realm/utils', () => ({ getJSONFromRealmObject: (record) => record })); +jest.mock('src/storage/realm/dbManager', () => ({ + __esModule: true, + default: { getObjectByIndex: () => ({ publicId: 'disposable-fixture' }) }, +})); +jest.mock('src/components/ScreenWrapper', () => 'Screen'); +jest.mock('src/components/KeeperText', () => 'Text'); +jest.mock('src/components/WalletHeader', () => 'Header'); +jest.mock('src/components/DotView', () => 'Dot'); +jest.mock('src/components/ThemedColor/ThemedColor', () => ({ + __esModule: true, + default: () => '#000', +})); +jest.mock('src/constants/responsive', () => ({ hp: (value) => value, wp: (value) => value })); +jest.mock('src/context/Localization/LocContext', () => ({ + LocalizationContext: require('react').createContext({ + translations: require('../../src/context/Localization/language/en.json'), + }), +})); + +const AppVersionHistory = require('../../src/screens/AppSettings/AppVersionHistoty').default; + +let screen; +afterEach(() => act(() => screen?.unmount())); + +test.each([ + ['2.6.4', '620', '2.6.3'], + ['2.6.3', '619', '2.6.4'], +])( + 'installed app %s (%s) is shown independently of historical version %s', + (nativeVersion, build, historyVersion) => { + mockNativeVersion = nativeVersion; + mockNativeBuild = build; + mockHistory = [ + { title: 'Disposable historical upgrade', version: historyVersion, date: 1_792_454_400_000 }, + ]; + const originalHistory = JSON.stringify(mockHistory); + act(() => { + screen = renderer.create(); + }); + expect(screen.root.findByProps({ testID: 'text_installedAppVersion' }).props.children).toBe( + `${nativeVersion} (${build})` + ); + expect( + screen.root.findByProps({ testID: 'text_installedAppVersionLabel' }).props.children + ).toBe('Installed app version'); + const historicalRows = screen.root.findAllByProps({ testID: 'view_versionHistory' }); + expect(historicalRows).toHaveLength(1); + expect( + historicalRows[0].findAllByType('Text').some((node) => node.props.children === historyVersion) + ).toBe(true); + expect(JSON.stringify(mockHistory)).toBe(originalHistory); + } +); diff --git a/tests/store/versionHistory.test.ts b/tests/store/versionHistory.test.ts index 0bedf5f51a..71c3cc7798 100644 --- a/tests/store/versionHistory.test.ts +++ b/tests/store/versionHistory.test.ts @@ -108,7 +108,9 @@ function fixture(appId = 'test-app') { ? app.id === id ? app : undefined - : records[schema].find((row) => row.id === id), + : records[schema].find( + (row) => row[schema === RealmSchema.VersionHistory ? 'version' : 'id'] === id + ), objects: (schema) => records[schema], create, }; @@ -293,6 +295,21 @@ describe('Version History retains recovery provenance with local-first upgrades' expect(f.app.version).toBe(previousVersion); }); + test('actual account migration preserves an existing current recovery history entry', async () => { + const f = fixture(); + const recovery = { + version: '2.5.16(624)', + title: 'Recovered Wallet', + date: '2026-09-01T12:00:00.000Z', + releaseNote: 'Original recovery provenance', + }; + f.records.VersionHistory.push(recovery); + expect(await f.run('applyUpgradeSequence', { previousVersion, newVersion })).toBe(true); + expect(f.app.version).toBe(newVersion); + expect(f.records.VersionHistory).toEqual([recovery]); + expect(f.state.events).toEqual(['local commit', 'queue']); + }); + test('failed local history transaction does not upload or advance the app version', async () => { const f = fixture(); f.state.rollbackHistory = true; From 83e46731a47f679a7436ea9c7a2c391746ea3d95 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Sun, 18 Oct 2026 06:47:25 +0000 Subject: [PATCH 213/254] Bootstrap hosted native validation with private diagnostics and draft guards (cherry picked from commit 0984c5b97e7e8326d6856ba662274ecf325e5e0f) --- .github/native/hosted_native.py | 880 ++++++++++++++++++ .github/native/nodeify.cjs | 33 + .github/native/startup.yaml | 22 + .../tests/keeper-local-validation.test.cjs | 408 +++++++- .github/tests/native-action-pins.test.cjs | 182 ++++ .github/tests/native-draft-policy.test.cjs | 131 +++ .github/tests/nodeify.test.cjs | 38 + .github/tests/test_diagnostic_privacy.py | 136 +++ .github/tests/test_hosted_native.py | 494 ++++++++++ .github/workflows/keeper-hosted-native.yml | 150 +++ .github/workflows/keeper-local-validation.yml | 106 ++- .github/workflows/keeper-validation-tests.yml | 26 + .github/workflows/test.yml | 2 + AGENTS.md | 6 +- docs/cloud-bootstrap-preparation.md | 175 ++++ docs/hosted-native-validation.md | 198 ++++ 16 files changed, 2962 insertions(+), 25 deletions(-) create mode 100644 .github/native/hosted_native.py create mode 100644 .github/native/nodeify.cjs create mode 100644 .github/native/startup.yaml create mode 100644 .github/tests/native-action-pins.test.cjs create mode 100644 .github/tests/native-draft-policy.test.cjs create mode 100644 .github/tests/nodeify.test.cjs create mode 100644 .github/tests/test_diagnostic_privacy.py create mode 100644 .github/tests/test_hosted_native.py create mode 100644 .github/workflows/keeper-hosted-native.yml create mode 100644 .github/workflows/keeper-validation-tests.yml create mode 100644 docs/cloud-bootstrap-preparation.md create mode 100644 docs/hosted-native-validation.md diff --git a/.github/native/hosted_native.py b/.github/native/hosted_native.py new file mode 100644 index 0000000000..6573d95950 --- /dev/null +++ b/.github/native/hosted_native.py @@ -0,0 +1,880 @@ +#!/usr/bin/env python3 +"""Disposable hosted smoke harness; only allowlisted metadata leaves the runner. + +Native and Maestro output stays under RUNNER_TEMP, never in an uploaded glob. +The gate trusts protected workflow/job provenance, never this writable artifact. +""" + +import hashlib +import json +import os +from pathlib import Path +import platform +import re +import shutil +import signal +import subprocess +import sys +import tempfile +import time +import urllib.request +import xml.etree.ElementTree as ET + + +ROOT = Path(__file__).resolve().parents[2] +PLATFORM = os.environ.get('KEEPER_NATIVE_PLATFORM', '') +PRIVATE = Path(os.environ.get('RUNNER_TEMP', tempfile.gettempdir())) / 'keeper-native-private' +EVIDENCE = ROOT / 'artifacts' / 'native' / PLATFORM.lower() / 'metadata.json' +MAESTRO_VERSION = 'cli-2.11.0' +MAESTRO_SHA256 = '5384593cb4e7a106489e75a821d157dd43f4e438df6bc308b72e82c685e1283a' +APP_IDS = {'IOS': 'io.hexawallet.hexakeeper.dev', 'ANDROID': 'io.hexawallet.keeper.development'} + + +def save(data): + EVIDENCE.parent.mkdir(parents=True, exist_ok=True) + EVIDENCE.write_text(json.dumps(data, indent=2) + '\n') + + +def read(): + return json.loads(EVIDENCE.read_text()) + + +def failure_details(log): + """Classify observed tool failures; never return any part of a log line. + + Redaction cannot recognize arbitrary wallet, user or application text. Each + result must come from this fixed vocabulary, including the fallback. + """ + categories = ( + (r'lockfile needs to be updated|could not find compatible versions|^The sandbox is not in sync', + 'DEPENDENCY_LOCK_MISMATCH'), + (r'no space left on device', 'DISK_SPACE_EXHAUSTED'), + (r'command not found', 'REQUIRED_TOOL_UNAVAILABLE'), + (r'requires a higher minimum deployment target', 'IOS_DEPLOYMENT_TARGET_UNSUPPORTED'), + (r'^e: ', 'KOTLIN_COMPILER_ERROR'), + (r'^\[!\]', 'COCOAPODS_ERROR'), + (r'^FAILURE:|^A problem occurred|^Execution failed', 'BUILD_FAILURE'), + (r'^Could not ', 'DEPENDENCY_OR_TOOL_FAILURE'), + (r'\berror:|^error |^> (?!Task\b)|^Caused by:', 'TOOL_ERROR'), + ) + selected = [] + for raw in log.splitlines(): + line = re.sub(r'\x1b\[[0-9;]*[A-Za-z]', '', raw).strip() + if re.match(r'^\d+\s*\|', line): + continue + for pattern, category in categories: + if re.search(pattern, line, re.IGNORECASE): + if category not in selected: + selected.append(category) + break + return selected + + +def maestro_failure_details(log): + """Retain only fixed command labels, selectors and tool failure categories.""" + selected = [] + for raw in log.splitlines(): + line = re.sub(r'\x1b\[[0-9;]*[A-Za-z]', '', raw).strip() + if line.endswith('FAILED'): + if line.startswith(('Launch app', 'Clear state', 'Stop app')) and any(app in line for app in APP_IDS.values()): + selected.append('Maestro app launch/stop command FAILED') + elif line.startswith(('Assert', 'Wait')) and 'Create a passcode' in line: + selected.append('Maestro assertion: Create a passcode visible FAILED') + elif line.startswith(('Assert', 'Wait')) and 'Confirm your passcode' in line: + selected.append('Maestro assertion: Confirm your passcode state FAILED') + elif line.startswith('Tap on'): + match = re.search(r'\bkey_[1-4]\b', line) + if match: + selected.append('Maestro tap %s FAILED' % match.group()) + elif line.startswith(('Assertion is false:', 'Element not found:')): + # Emit only a category and fixed selector; even a first-line suffix + # can contain a hierarchy or arbitrary device text. + category = 'assertion failed' if line.startswith('Assertion is false:') else 'element not found' + for target in ['Create a passcode', 'Confirm your passcode', 'key_1', 'key_2', 'key_3', 'key_4']: + if target in line: + selected.append('Maestro %s: %s' % (category, target)) + break + elif line.startswith(('Unable to launch app', 'Failed to launch app')): + selected.append('Maestro app launch failed') + elif line.startswith(('Unable to connect', 'Failed to connect')): + selected.append('Maestro connection failed') + elif line.startswith('Failed to start'): + selected.append('Maestro driver start failed') + elif line.startswith(('No devices found', 'No connected devices', 'Unable to find device')): + selected.append('Maestro device unavailable') + if len(selected) >= 10: + break + return list(dict.fromkeys(selected))[:10] + + +def android_startup_errors(log): + categories = {'AndroidRuntime': 'ANDROID_RUNTIME_ERROR', + 'ReactNativeJS': 'REACT_NATIVE_JS_ERROR', 'ReactNative': 'REACT_NATIVE_ERROR'} + selected = [] + for line in log.splitlines(): + match = re.match(r'^[EF]/(AndroidRuntime|ReactNativeJS|ReactNative)\s*\(\s*\d+\):\s*(.*)', line) + if not match: + continue + tag, message = match.groups() + if re.search(r'(?:FATAL EXCEPTION|Caused by:|\b[\w.$]*(?:Exception|Error)\b|' + r'Invariant Violation|Unable to load script|Could not)', message): + if categories[tag] not in selected: + selected.append(categories[tag]) + return selected + + +def metro_startup_errors(log): + selected = [] + for raw in log.splitlines(): + line = re.sub(r'\x1b\[[0-9;]*[A-Za-z]', '', raw).strip() + if re.match(r'^(?:error\b|(?:Syntax|Type|Reference)Error:|Unable to resolve module)', line, re.IGNORECASE): + category = 'METRO_ERROR' + for marker, known in [('Unable to resolve module', 'METRO_MODULE_RESOLUTION_ERROR'), + ('SyntaxError:', 'METRO_SYNTAX_ERROR'), + ('TypeError:', 'METRO_TYPE_ERROR'), + ('ReferenceError:', 'METRO_REFERENCE_ERROR')]: + if marker.lower() in line.lower(): + category = known + break + if category not in selected: + selected.append(category) + return selected + + +def metro_startup_progress(log): + # Metro's TerminalReporter uses the same bare BUNDLE line for done and + # failed builds. Count observed markers without inferring completion. + lines = [re.sub(r'\x1b\[[0-9;]*[A-Za-z]', '', line).strip() for line in log.splitlines()] + bundles = sum(bool(re.match(r'^BUNDLE\s', line)) for line in lines) + prebundles = sum(bool(re.match(r'^PREBUNDLE\s', line)) for line in lines) + return {'bundle_marker_observed': bundles > 0, 'bundle_marker_count': bundles, + 'prebundle_marker_count': prebundles} + + +def private_log_prefix(path): + """Read launch-time output, discarding a partially truncated final record.""" + limit = 1024 * 1024 + with path.open('rb') as source: + payload = source.read(limit + 1) + truncated = len(payload) > limit + payload = payload[:limit] + if truncated: + payload = payload.rsplit(b'\n', 1)[0] + b'\n' if b'\n' in payload else b'' + return payload.decode(errors='replace'), truncated + + +def ios_startup_diagnostics(payload, executable): + if not re.fullmatch(r'[A-Za-z0-9_][A-Za-z0-9_.-]*', executable): + raise ValueError('Unsafe iOS executable selector.') + # log(1) compact: ISO timestamp, abbreviated type, process[PID:TID], message. + # Only prefixed own-process records qualify; never retain continuation lines. + pattern = re.compile(r'^\d{4}-\d{2}-\d{2}[ T]\d{2}:\d{2}:\d{2}(?:\.\d+)?' + r'(?:[+-]\d{2}:?\d{2}|Z)?\s+(\S+)\s+' + r'([A-Za-z0-9_.-]+)\[\d+:[A-Fa-f0-9x]+\]\s+(.*)$') + selected = [] + matched = 0 + for line in payload.splitlines(): + match = pattern.match(line) + if not match: + continue + severity, process, message = match.groups() + if process != executable: + continue + matched += 1 + if severity.lower() in ['e', 'f', 'er', 'ft', 'error', 'fault'] or re.search( + r'(?:\b(?:error|exception|fatal)\b|\b[\w.$]*(?:Exception|Error)\b)', + message, re.IGNORECASE): + if not selected: + selected.append('IOS_STARTUP_ERROR') + return {'format': 'compact', 'matching_process_records': matched, + 'errors': selected} + + +def startup_hierarchy_signals(directory): + """Classify fixed public startup selectors; never export hierarchy content.""" + # Maestro cli-2.11.0 writes standalone TreeNode snapshots in this layout. + flags = ['createPIN', 'confirmPIN', 'RNredbox', 'noScriptURL', + 'developmentServerError', 'unableLoadScript'] + result = {flag: False for flag in flags} + result.update({'snapshot_count': 0, 'parse_error_types': []}) + redbox_ids = {'redbox-error', 'redbox-dismiss', 'redbox-reload', 'rn_redbox_stack', + 'rn_redbox_dismiss_button', 'rn_redbox_reload_button', 'catalyst_redbox_title'} + fixed_messages = {'noScriptURL': 'No script URL provided.', + 'developmentServerError': 'Could not connect to development server.', + 'unableLoadScript': 'Unable to load script.'} + for index, path in enumerate(directory.glob('**/screen-hierarchy/step-*.json')): + try: + if index >= 8: + raise ValueError('Hierarchy snapshot count exceeded limit.') + result['snapshot_count'] += 1 + if path.is_symlink() or not path.resolve().is_relative_to(directory.resolve()): + raise ValueError('Hierarchy snapshot must remain in private output.') + with path.open('rb') as source: + payload = source.read(1024 * 1024 + 1) + if len(payload) > 1024 * 1024: + raise ValueError('Hierarchy snapshot exceeded byte limit.') + pending = [json.loads(payload)] + snapshot = {flag: False for flag in flags} + nodes = 0 + while pending: + node = pending.pop() + nodes += 1 + if nodes > 10000: + raise ValueError('Hierarchy snapshot exceeded node limit.') + if not isinstance(node, dict): + raise TypeError('Unexpected hierarchy node format.') + attributes = node.get('attributes') or {} + children = node.get('children') or [] + if not isinstance(attributes, dict) or not isinstance(children, list): + raise TypeError('Unexpected hierarchy attributes or children format.') + # iOS StaticText often exposes only accessibilityText. Android + # resource IDs include the package prefix before :id/. + identifier = attributes.get('resource-id', '') + if isinstance(identifier, str) and identifier.rsplit(':id/', 1)[-1] in redbox_ids: + snapshot['RNredbox'] = True + for key in ['accessibilityText', 'text', 'title', 'value']: + value = attributes.get(key) + if not isinstance(value, str): + continue + snapshot['createPIN'] |= value.strip() == 'Create a passcode' + snapshot['confirmPIN'] |= value.strip() == 'Confirm your passcode' + for flag, message in fixed_messages.items(): + snapshot[flag] |= message in value + if len(pending) + len(children) + nodes > 10000: + raise ValueError('Hierarchy snapshot exceeded node limit.') + pending.extend(children) + for flag in flags: + result[flag] |= snapshot[flag] + except Exception as error: + kind = type(error).__name__ + if kind not in result['parse_error_types']: + result['parse_error_types'].append(kind) + if index >= 8: + break + return result + + +def maestro_preflight_diagnostics(directory=None, command_label='Assert startup and disposable passcode transition'): + """Export fixed cli-2.11.0 driver markers, never subprocess error text.""" + markers = { + 'runner_install_started': '[Start] Install XCUITest runner on', + 'runner_install_launch_returned': '[Done] Install XCUITest runner on', + 'status_check_started': '[Start] Perform XCUITest driver status check on', + 'status_response_received': '[Done] Perform XCUITest driver status check on', + 'status_check_failed': '[Failed] Perform XCUITest driver status check on', + 'driver_startup_timeout': 'iOS driver not ready in time, consider increasing timeout by configuring MAESTRO_DRIVER_STARTUP_TIMEOUT env variable', + 'prebuilt_driver_selected': 'Installing pre built driver without xcodebuild', + 'xcodebuild_driver_selected': 'Installing driver with xcodebuild', + 'xctestrun_config_missing': 'xctestrun config does not exist', + 'ui_test_runner_missing': 'ui test runner does not exist', + 'xcode_test_execute_failed': '** TEST EXECUTE FAILED **', + 'xcode_test_failed': '** TEST FAILED **', + 'xcode_testing_failed': 'Testing failed:', + } + # The install/status "Done" markers precede readiness evaluation; they do + # not prove that the runner installed successfully or became ready. + result = {name: False for name in markers} + result.update({'host_port_unavailable': False, 'requested_device_disconnected': False, + 'exception_types': [], 'logs_read': 0, 'truncated_logs': 0, + 'log_limit_reached': False, 'collection_error_types': []}) + known_types = ['IOSDriverTimeoutException', 'FileNotFoundException', 'CliError', + 'IllegalStateException', 'IOException', 'ConnectException', + 'SocketTimeoutException', 'TimeoutException', 'RuntimeException', + 'UnknownHostException', 'SSLHandshakeException'] + paths = [] + for index, entry in enumerate(read()['commands'], 1): + if entry['label'] == command_label: + paths.append(PRIVATE / ('command-%03d.log' % index)) + directory = directory if directory is not None else PRIVATE / 'maestro-output' + if not directory.resolve().is_relative_to(PRIVATE.resolve()): + raise ValueError('Maestro diagnostic directory must remain private.') + for pattern in ['**/maestro.log', '**/xctest_runner_*.log']: + for path in directory.glob(pattern): + paths.append(path) + if len(paths) > 6: + break + if len(paths) > 6: + break + for path in paths[:6]: + try: + if path.is_symlink() or not path.resolve().is_relative_to(PRIVATE.resolve()): + raise ValueError('Maestro log must remain in private output.') + payload, truncated = private_log_prefix(path) + result['logs_read'] += 1 + result['truncated_logs'] += int(truncated) + for name, marker in markers.items(): + result[name] |= marker in payload + for line in payload.splitlines(): + result['host_port_unavailable'] |= ('Requested driver host port ' in line + and ' is not available' in line) + result['requested_device_disconnected'] |= (' was requested, but it is not connected.' in line) + for kind in known_types: + if re.search(r'(? 6 + return result + + +def record_startup_failure(report, device): + """Collect bounded diagnostics before destroying this job's fresh app state.""" + details = {'scope': 'Fresh startup only; no account or persisted PIN created.', + 'collection_errors': []} + + def collect(name, action): + try: + details[name] = action() + except Exception as error: + details['collection_errors'].append({'stage': name, 'error_type': type(error).__name__}) + + def private_text(path): + # No raw logs/reports are copied to the artifact directory. + with path.open('rb') as source: + source.seek(0, os.SEEK_END) + source.seek(max(0, source.tell() - 1024 * 1024)) + return source.read().decode(errors='replace') + + def maestro_details(): + result = {'failed_commands': [], 'testcase_failures': [], 'report': 'MISSING'} + for index, entry in enumerate(read()['commands'], 1): + if entry['label'] == 'Assert startup and disposable passcode transition': + result['failed_commands'] = maestro_failure_details(private_text(PRIVATE / ('command-%03d.log' % index))) + if report.exists(): + if report.stat().st_size > 1024 * 1024: + raise RuntimeError('Maestro report exceeded diagnostic size limit.') + suite = ET.parse(report).getroot() + result['report'] = 'PRESENT' + result['testcase_statuses'] = [case.get('status') if case.get('status') in + ['SUCCESS', 'ERROR', 'WARNING', 'SKIPPED'] else 'UNKNOWN' + for case in list(suite.iter('testcase'))[:10]] + for failure in list(suite.iter('failure'))[:10]: + # Pinned Maestro JUnit stores failure.message separately from UI hierarchy. + first_line = ''.join(failure.itertext()).splitlines()[:1] + result['testcase_failures'].extend(maestro_failure_details('\n'.join(first_line))) + return result + + collect('maestro', maestro_details) + collect('maestro_preflight', maestro_preflight_diagnostics) + collect('hierarchy_signals', lambda: startup_hierarchy_signals(PRIVATE / 'maestro-output')) + collect('metro_errors', lambda: metro_startup_errors(private_text(PRIVATE / 'metro.log'))) + collect('metro_progress', lambda: metro_startup_progress(private_log_prefix(PRIVATE / 'metro.log')[0])) + if PLATFORM == 'ANDROID' and device: + def android_details(): + args = ['adb', '-s', device, 'logcat', '-d', '-t', '300', '-v', 'brief', + 'AndroidRuntime:E', 'ReactNativeJS:E', 'ReactNative:E', '*:S'] + path = PRIVATE / 'android-startup-errors.log' + with path.open('wb') as output: + try: + result = subprocess.run(args, stdout=output, stderr=subprocess.STDOUT, + timeout=20, check=False) + code = result.returncode + except subprocess.TimeoutExpired: + code = 124 + return {'command': args, 'exit_code': code, + 'errors': android_startup_errors(private_text(path)) if code == 0 else []} + collect('android_errors', android_details) + elif PLATFORM == 'IOS' and device: + def ios_details(): + executable = read()['built_artifact']['executable'] + if not re.fullmatch(r'[A-Za-z0-9_][A-Za-z0-9_.-]*', executable): + raise ValueError('Unsafe iOS executable selector.') + predicate = ('process == "%s" AND (messageType == "error" OR messageType == "fault" ' + 'OR eventMessage CONTAINS[c] "error" OR eventMessage CONTAINS[c] "exception" ' + 'OR eventMessage CONTAINS[c] "fatal")') % executable + args = ['xcrun', 'simctl', 'spawn', device, 'log', 'show', '--style', 'compact', + '--last', '10m', '--predicate', predicate] + path = PRIVATE / 'ios-startup-errors.log' + # Keep log/xcrun notices private and separate from compact records. + with path.open('wb') as output, (PRIVATE / 'ios-startup-errors-stderr.log').open('wb') as stderr: + try: + result = subprocess.run(args, stdout=output, stderr=stderr, + timeout=25, check=False) + code = result.returncode + except subprocess.TimeoutExpired: + code = 124 + result = {'command': args, 'exit_code': code, 'errors': []} + if code == 0: + try: + payload, truncated = private_log_prefix(path) + result.update(ios_startup_diagnostics(payload, executable)) + result['input_truncated'] = truncated + except (ValueError, TypeError) as error: + result['parse_error'] = type(error).__name__ + return result + collect('ios_errors', ios_details) + data = read() + data['startup_diagnostics'] = details + save(data) + print('Bounded startup diagnostics: ' + json.dumps(details), flush=True) + + +def command(args, label, timeout=120, cwd=ROOT, capture=False, error_details=True): + """Record exact nonsecret argv and status; keep all process output private.""" + PRIVATE.mkdir(parents=True, exist_ok=True) + data = read() + entry = {'label': label, 'command': args, 'timeout_seconds': timeout} + data['commands'].append(entry) + save(data) + started = time.monotonic() + output_file = PRIVATE / ('command-%03d.log' % len(data['commands'])) + print(label, flush=True) + try: + with output_file.open('wb') as output: + proc = subprocess.Popen(args, cwd=cwd, stdout=output, stderr=subprocess.STDOUT, + start_new_session=True) + try: + code = proc.wait(timeout=timeout) + except subprocess.TimeoutExpired: + os.killpg(proc.pid, signal.SIGKILL) + proc.wait() + code = 124 + except OSError: + code = 127 + entry['exit_code'] = code + entry['duration_seconds'] = round(time.monotonic() - started, 1) + if code: + # Fixed diagnostics expose install blockers without publishing arbitrary logs. + log = output_file.read_text(errors='replace') if output_file.exists() else '' + known = { + 'lockfile needs to be updated': 'Manifest and yarn.lock are inconsistent; repair the lockfile separately.', + 'could not find compatible versions': 'The native dependency lock is incompatible with the manifest.', + 'no space left on device': 'The hosted runner ran out of disk space.', + 'command not found': 'A required native tool is unavailable.', + 'requires a higher minimum deployment target': 'A pod requires a higher iOS deployment target.', + } + entry['diagnostic'] = next((message for needle, message in known.items() + if needle in log.lower()), 'Inspect this command locally; raw output remains private to the runner.') + # Keep the schema-1 field name for consumers; values are now fixed + # categories, never redacted excerpts or arbitrary exception messages. + entry['redacted_tool_errors'] = failure_details(log) if error_details else [] + for line in entry['redacted_tool_errors']: + print(line, flush=True) + save(data) + raise RuntimeError('%s failed (exit %s). %s' % (label, code, entry['diagnostic'])) + save(data) + return output_file.read_text(errors='replace').strip() if capture else None + + +def record(): + source = subprocess.check_output(['git', 'rev-parse', 'HEAD'], cwd=ROOT, text=True).strip() + if source != os.environ.get('KEEPER_SOURCE_SHA'): + raise RuntimeError('Checkout does not match the exact requested PR head.') + save({ + 'schema': 1, 'platform': PLATFORM, 'source_sha': source, + 'workflow_ref': os.environ.get('GITHUB_WORKFLOW_REF'), + 'workflow_sha': os.environ.get('GITHUB_WORKFLOW_SHA'), + 'run_id': os.environ.get('GITHUB_RUN_ID'), 'run_attempt': os.environ.get('GITHUB_RUN_ATTEMPT'), + 'runner': {key: os.environ.get(key) for key in + ['RUNNER_OS', 'RUNNER_ARCH', 'ImageOS', 'ImageVersion']}, + 'os': platform.platform(), 'commands': [], 'build': 'NOT RUN', 'smoke': 'NOT RUN', + 'hardware': 'NOT RUN', 'signing': 'NOT RUN', 'app_id': APP_IDS[PLATFORM], + 'smoke_scope': 'Fresh install; render Create a passcode; type four disposable digits; assert Confirm your passcode; stop before persisting credentials.', + 'startup_timing_scope': 'Bounded cold development bundle/startup allowance; no release performance acceptance.', + 'artifact_policy': 'Metadata only. No raw logs, screenshots, app binaries, simulator data or credentials.', + }) + versions = { + 'node': command(['node', '--version'], 'Record Node version', capture=True), + 'java': command(['java', '-version'], 'Record Java version', capture=True), + } + if PLATFORM == 'IOS': + versions['xcode'] = command(['xcodebuild', '-version'], 'Record Xcode version', capture=True) + versions['sdk'] = command(['xcrun', '--sdk', 'iphonesimulator', '--show-sdk-version'], + 'Record iOS Simulator SDK', capture=True) + else: + versions['expected_sdk_build'] = 'compileSdk=36; buildTools=35.0.0; NDK=27.1.12297006' + versions['expected_emulator_target'] = 'Android API 35; google_apis; x86_64; pixel_6' + data = read() + data['toolchain'] = versions + save(data) + + +def fixtures(): + # Empty values would fall back to real development services in config.ts. + overlays = ['.env.ci', 'firebase.json'] + (ROOT / '.env.ci').write_text('''ENVIRONMENT=DEVELOPMENT +RELAY=https://127.0.0.1:9/ +SIGNING_SERVER_TESTNET=https://127.0.0.1:9/ +SIGNING_SERVER_MAINNET=https://127.0.0.1:9/ +CHANNEL_URL=https://127.0.0.1:9/ +SENTRY_DNS=https://public@127.0.0.1:9/1 +ENC_KEY_STORAGE_IDENTIFIER=KEEPER-HOSTED-DISPOSABLE +''') + (ROOT / 'firebase.json').write_text(json.dumps({'react-native': { + 'app_data_collection_default_enabled': False, + 'analytics_auto_collection_enabled': False, + 'analytics_collection_deactivated': True, + 'messaging_auto_init_enabled': False, + }}) + '\n') + if PLATFORM == 'IOS': + import plistlib + for path, bundle in [('ios/GoogleService-Info.plist', 'io.hexawallet.keeper'), + ('ios/GoogleServices/dev/GoogleService-Info.plist', APP_IDS['IOS'])]: + overlays.append(path) + with (ROOT / path).open('wb') as output: + plistlib.dump({'API_KEY': 'AIzaSy' + '0' * 33, 'GCM_SENDER_ID': '123456789012', + 'PLIST_VERSION': '1', 'BUNDLE_ID': bundle, + 'PROJECT_ID': 'keeper-disposable-ci', + 'GOOGLE_APP_ID': '1:123456789012:ios:0000000000000000000000', + 'IS_GCM_ENABLED': False, 'IS_ANALYTICS_ENABLED': False, + 'IS_ADS_ENABLED': False, 'IS_SIGNIN_ENABLED': False}, output) + else: + overlays.append('android/app/google-services.json') + (ROOT / 'android/app/google-services.json').write_text(json.dumps({ + 'project_info': {'project_number': '123456789012', 'project_id': 'keeper-disposable-ci'}, + 'client': [{'client_info': {'mobilesdk_app_id': '1:123456789012:android:0000000000000000', + 'android_client_info': {'package_name': APP_IDS['ANDROID']}}, + 'api_key': [{'current_key': 'AIzaSy' + '0' * 33}]}], + 'configuration_version': '1', + }) + '\n') + data = read() + data['configuration'] = 'Synthetic Firebase project; DEVELOPMENT/testnet; loopback-only configured services; no production secrets.' + data['configuration_overlays'] = [{'path': path, 'sha256': hashlib.sha256((ROOT / path).read_bytes()).hexdigest()} + for path in overlays] + save(data) + + +def install_maestro(): + archive = PRIVATE / 'maestro.zip' + url = 'https://github.com/mobile-dev-inc/Maestro/releases/download/%s/maestro.zip' % MAESTRO_VERSION + command(['curl', '--fail', '--location', '--silent', '--show-error', '--max-time', '120', + '--output', str(archive), url], 'Download pinned Maestro', timeout=130) + if hashlib.sha256(archive.read_bytes()).hexdigest() != MAESTRO_SHA256: + raise RuntimeError('Maestro archive checksum mismatch.') + # unzip preserves executable modes for the bundled native iOS driver; + # Python ZipFile extraction would silently strip those modes. + command(['unzip', '-q', str(archive), '-d', str(PRIVATE / 'maestro')], + 'Extract verified Maestro including native executable modes', timeout=120) + candidates = list((PRIVATE / 'maestro').rglob('bin/maestro')) + if len(candidates) != 1: + raise RuntimeError('Unexpected Maestro archive layout.') + data = read() + data['maestro'] = {'version': MAESTRO_VERSION, 'sha256': MAESTRO_SHA256, 'binary': str(candidates[0])} + save(data) + + +def android_sdk_metadata(sdk, component): + properties = (sdk / component / 'source.properties').read_text() + values = dict((key.strip(), value.strip()) for line in properties.splitlines() + if '=' in line for key, value in [line.split('=', 1)]) + if not values.get('Pkg.Revision'): + raise RuntimeError('Android SDK component %s has no Pkg.Revision metadata.' % component) + return {key: value for key, value in values.items() + if key in ['Pkg.Revision', 'AndroidVersion.ApiLevel']} + + +def prepare(): + command(['npm', 'install', '--global', 'yarn@1.22.22', '--ignore-scripts'], 'Install pinned Yarn', timeout=180) + command(['yarn', 'install', '--frozen-lockfile', '--ignore-scripts', '--non-interactive'], + 'Install immutable JavaScript dependencies', timeout=900) + command(['node_modules/.bin/patch-package', '--error-on-fail'], 'Apply repository dependency patches', timeout=180) + original_manifest = (ROOT / 'package.json').read_bytes() + command(['node', '.github/native/nodeify.cjs'], + 'Apply committed Node compatibility whitelist without installing dependencies', timeout=180) + installed_manifest = (ROOT / 'package.json').read_bytes() + original = json.loads(original_manifest) + installed = json.loads(installed_manifest) + for key in ['browser', 'react-native', 'browserify']: + original.pop(key, None) + installed.pop(key, None) + if original != installed: + raise RuntimeError('Node compatibility setup changed fields beyond module mappings.') + data = read() + data['nodeify_overlay'] = { + 'path': 'package.json', 'before_sha256': hashlib.sha256(original_manifest).hexdigest(), + 'after_sha256': hashlib.sha256(installed_manifest).hexdigest(), + 'diff_sha256': hashlib.sha256(subprocess.check_output(['git', 'diff', 'HEAD', '--', 'package.json'], cwd=ROOT)).hexdigest(), + 'allowed_fields': ['browser', 'react-native', 'browserify'], + } + save(data) + command(['git', 'diff', '--exit-code', 'HEAD', '--', 'yarn.lock', 'ios/Podfile.lock'], + 'Verify dependency locks remain immutable') + fixtures() + install_maestro() + if PLATFORM == 'IOS': + command(['gem', 'install', 'cocoapods', '--version', '1.16.2', '--no-document'], + 'Install pinned CocoaPods', timeout=300) + if os.environ.get('KEEPER_POD_LOCK_REPAIR') == '1': + # Diagnostic bootstrap only. A candidate lock is reviewed/committed, + # then a separate strict deployment run must build and test the app. + command(['pod', 'update', 'NitroIap', 'NitroModules', 'openiap', '--no-repo-update'], + 'Generate targeted CocoaPods lock candidate (not validation)', + timeout=900, cwd=ROOT / 'ios') + shutil.copyfile(ROOT / 'ios/Podfile.lock', EVIDENCE.parent / 'Podfile.lock.candidate') + raise RuntimeError('Pod lock candidate generated for review; no native validation performed.') + command(['pod', 'install', '--deployment'], 'Install immutable CocoaPods dependencies', + timeout=900, cwd=ROOT / 'ios') + # Existing PBX build phases hardcode this path. Adapt the disposable host, + # preserving the app project and the source commit being tested. + command(['sudo', 'mkdir', '-p', '/opt/homebrew/bin'], 'Prepare existing Xcode Node path') + command(['sudo', 'ln', '-sf', shutil.which('node'), '/opt/homebrew/bin/node'], + 'Connect existing Xcode Node path to pinned Node') + else: + command(['sdkmanager', 'platforms;android-36', 'build-tools;35.0.0', 'ndk;27.1.12297006'], + 'Install pinned Android build SDK and NDK', timeout=600) + sdk = Path(os.environ.get('ANDROID_HOME', os.environ.get('ANDROID_SDK_ROOT', ''))) + observed = {} + for component in ['platforms/android-36', 'build-tools/35.0.0', 'ndk/27.1.12297006']: + observed[component] = android_sdk_metadata(sdk, component) + data = read() + data['toolchain']['observed_android_sdk'] = observed + save(data) + if (ROOT / 'package.json').read_bytes() != installed_manifest: + raise RuntimeError('Native installation unexpectedly rewrote the installed manifest.') + command(['git', 'diff', '--exit-code', 'HEAD', '--', 'yarn.lock', 'ios/Podfile.lock'], + 'Verify native installation did not rewrite locks') + + +def artifact_identity(): + if PLATFORM == 'IOS': + import plistlib + bundle = PRIVATE / 'DerivedData/Build/Products/Debug-iphonesimulator/hexa_keeper_dev.app' + with (bundle / 'Info.plist').open('rb') as source: + executable = plistlib.load(source)['CFBundleExecutable'] + binary = bundle / executable + tree = hashlib.sha256() + for file in sorted(path for path in bundle.rglob('*') if path.is_file()): + tree.update(str(file.relative_to(bundle)).encode() + b'\0') + tree.update(hashlib.sha256(file.read_bytes()).digest()) + return {'path': str(bundle), 'executable': executable, + 'executable_sha256': hashlib.sha256(binary.read_bytes()).hexdigest(), + 'bundle_tree_sha256': tree.hexdigest()} + apk = ROOT / 'android/app/build/outputs/apk/development/debug/app-development-debug.apk' + return {'path': str(apk.relative_to(ROOT)), 'sha256': hashlib.sha256(apk.read_bytes()).hexdigest()} + + +def driver(): + """Fail early on iOS driver setup without installing or crediting the app.""" + if PLATFORM != 'IOS': + raise ValueError('The disposable driver preflight is iOS-only.') + state = PRIVATE / 'maestro-preflight-state' + os.environ['XDG_STATE_HOME'] = str(state) + details = {'scope': 'DRIVER PREFLIGHT/setup only; no app build, install or UI credit.', + 'result': 'FAIL', 'cleanup_errors': []} + device = None + original_error = None + label = 'Check disposable iOS driver readiness' + try: + created = command(['xcrun', 'simctl', 'create', 'Keeper-driver-preflight-ci', + 'com.apple.CoreSimulator.SimDeviceType.iPhone-16', + 'com.apple.CoreSimulator.SimRuntime.iOS-18-5'], + 'Create owned driver-preflight simulator', timeout=120, capture=True) + if not re.fullmatch(r'[A-Fa-f0-9]{8}(?:-[A-Fa-f0-9]{4}){3}-[A-Fa-f0-9]{12}', created): + raise ValueError('simctl create did not return an owned simulator UUID.') + device = created + command(['xcrun', 'simctl', 'boot', device], 'Boot owned driver-preflight simulator', timeout=120) + command(['xcrun', 'simctl', 'bootstatus', device, '-b'], + 'Wait for driver-preflight simulator boot', timeout=240) + command([read()['maestro']['binary'], '--platform', 'ios', '--device', device, 'hierarchy'], + label, timeout=300, error_details=False) + details['result'] = 'PASS' + except Exception as error: + original_error = error + details['error_type'] = type(error).__name__ + raise + finally: + if device: + for action in ['shutdown', 'delete']: + try: + subprocess.run(['xcrun', 'simctl', action, device], stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, timeout=30, check=True) + except Exception as error: + details['cleanup_errors'].append({'action': action, 'error_type': type(error).__name__}) + try: + details['diagnostics'] = maestro_preflight_diagnostics(state / 'maestro/tests', label) + except Exception as error: + details['diagnostics'] = {'collection_error_types': [type(error).__name__]} + if details['cleanup_errors']: + details['result'] = 'FAIL' + data = read() + data['driver_preflight'] = details + save(data) + if details['cleanup_errors'] and original_error is None: + raise RuntimeError('Owned driver-preflight simulator cleanup failed.') + + +def build(): + if PLATFORM == 'IOS': + architecture = platform.machine() + if architecture not in ['arm64', 'x86_64']: + raise RuntimeError('Unsupported hosted iOS simulator architecture: %s' % architecture) + data = read() + data.setdefault('toolchain', {})['ios_simulator_architecture'] = architecture + save(data) + command(['xcodebuild', '-workspace', 'ios/hexa_keeper.xcworkspace', '-scheme', 'hexa_keeper_dev', + '-configuration', 'Debug', '-sdk', 'iphonesimulator', '-destination', 'generic/platform=iOS Simulator', + '-derivedDataPath', str(PRIVATE / 'DerivedData'), 'CODE_SIGNING_ALLOWED=NO', + 'CODE_SIGNING_REQUIRED=NO', 'ARCHS=' + architecture, 'ONLY_ACTIVE_ARCH=YES', 'build'], + 'Build unsigned development simulator app', timeout=2400) + else: + observed = command(['./gradlew', '--version'], 'Record actual Gradle and JVM version', + capture=True, cwd=ROOT / 'android') + data = read() + data['toolchain']['gradle'] = observed + save(data) + command(['./gradlew', ':app:assembleDevelopmentDebug', '--no-daemon', '--max-workers=2', + '-PreactNativeArchitectures=x86_64'], 'Build debug-key-only development APK', + timeout=2100, cwd=ROOT / 'android') + data = read() + data['built_artifact'] = artifact_identity() + data['build'] = 'PASS' + save(data) + + +def warm_metro_bundle(): + """Bound cold development bundling before starting either disposable app.""" + bundle_platform = {'IOS': 'ios', 'ANDROID': 'android'}.get(PLATFORM) + if bundle_platform is None: + raise ValueError('Unsupported native bundle platform.') + url = 'http://127.0.0.1:8081/index.bundle?platform=%s&dev=true&minify=false' % bundle_platform + body = PRIVATE / ('%s-development-warmup.bundle' % bundle_platform) + details = {'scope': 'Development bundle setup only; not native or UI evidence.', + 'platform': bundle_platform, 'url': url, 'timeout_seconds': 240, + 'result': 'FAIL', 'http_status': None} + started = time.monotonic() + try: + status = command(['curl', '--fail', '--silent', '--show-error', '--max-time', '240', + '--output', str(body), '--write-out', '%{http_code}', url], + 'Warm %s development bundle (setup only)' % PLATFORM, timeout=250, capture=True) + if re.fullmatch(r'\d{3}', status): + details['http_status'] = int(status) + if status != '200' or not body.is_file() or body.stat().st_size == 0: + raise RuntimeError('Development bundle warmup did not return a nonempty HTTP 200 body.') + digest = hashlib.sha256() + with body.open('rb') as source: + for chunk in iter(lambda: source.read(1024 * 1024), b''): + digest.update(chunk) + details.update({'result': 'PASS', 'bytes': body.stat().st_size, 'sha256': digest.hexdigest()}) + finally: + details['duration_seconds'] = round(time.monotonic() - started, 1) + data = read() + data['metro_warmup'] = details + save(data) + + +def smoke(): + if read()['build'] != 'PASS': + raise RuntimeError('A successful native build is required before smoke testing.') + if artifact_identity() != read()['built_artifact']: + raise RuntimeError('Built artifact changed before the disposable startup smoke.') + metro_output = (PRIVATE / 'metro.log').open('wb') + metro = subprocess.Popen(['node', 'node_modules/react-native/cli.js', 'start', '--port', '8081'], + cwd=ROOT, stdout=metro_output, stderr=subprocess.STDOUT, start_new_session=True) + device = None + report = PRIVATE / 'smoke-junit.xml' + try: + for _ in range(60): + if metro.poll() is not None: + raise RuntimeError('Metro exited before the disposable smoke.') + try: + with urllib.request.urlopen('http://127.0.0.1:8081/status', timeout=2) as response: + if b'packager-status:running' in response.read(): + break + except OSError: + time.sleep(1) + else: + raise RuntimeError('Metro did not become ready within 60 attempts.') + warm_metro_bundle() + if PLATFORM == 'IOS': + device = command(['xcrun', 'simctl', 'create', 'Keeper-disposable-ci', + 'com.apple.CoreSimulator.SimDeviceType.iPhone-16', + 'com.apple.CoreSimulator.SimRuntime.iOS-18-5'], + 'Create fresh iPhone 16 iOS 18.5 simulator', capture=True) + command(['xcrun', 'simctl', 'boot', device], 'Boot disposable simulator') + command(['xcrun', 'simctl', 'bootstatus', device, '-b'], 'Wait for simulator boot', timeout=240) + command(['xcrun', 'simctl', 'install', device, + str(PRIVATE / 'DerivedData/Build/Products/Debug-iphonesimulator/hexa_keeper_dev.app')], + 'Install unsigned development app', timeout=120) + descriptor = 'iPhone 16; iOS 18.5' + else: + sdk = Path(os.environ.get('ANDROID_HOME', os.environ.get('ANDROID_SDK_ROOT', ''))) + # Query the installed SDK package without loading emulator GUI libraries + # on the headless host. The emulator runner already booted this image. + emulator_version = android_sdk_metadata(sdk, 'emulator') + adb_version = command(['adb', 'version'], 'Record actual adb version', capture=True) + data = read() + data['toolchain']['emulator'] = {'source': 'emulator/source.properties', **emulator_version} + data['toolchain']['adb'] = adb_version + save(data) + device = command(['adb', 'get-serialno'], 'Identify fresh emulator', capture=True) + command(['adb', '-s', device, 'reverse', 'tcp:8081', 'tcp:8081'], 'Connect emulator to Metro') + command(['adb', '-s', device, 'install', '-r', + 'android/app/build/outputs/apk/development/debug/app-development-debug.apk'], + 'Install development debug APK', timeout=120) + descriptor = command(['adb', '-s', device, 'shell', 'getprop', 'ro.build.fingerprint'], + 'Record Android emulator image', capture=True) + data = read() + data['device'] = {'id': device, 'image': descriptor, 'fresh_disposable_state': True} + data['installed_artifact_verified'] = True + save(data) + command([data['maestro']['binary'], '--device', device, 'test', '--env', 'APP_ID=' + APP_IDS[PLATFORM], + '--flatten-debug-output', '--debug-output', str(PRIVATE / 'maestro-output'), + '--format', 'junit', '--output', str(report), '--test-output-dir', str(PRIVATE / 'maestro-output'), + '.github/native/startup.yaml'], 'Assert startup and disposable passcode transition', timeout=420) + suite = ET.parse(report).getroot() + cases = list(suite.iter('testcase')) + if not cases or any(list(case.iter('failure')) or list(case.iter('error')) or list(case.iter('skipped')) + for case in cases): + raise RuntimeError('Maestro did not produce a complete successful smoke report.') + data = read() + data['smoke'] = 'PASS' + data['smoke_test_cases'] = len(cases) + save(data) + except Exception: + try: + record_startup_failure(report, device) + except Exception as error: + # Diagnostics never replace the original failed native/UI result. + data = read() + data['startup_diagnostics'] = {'collection_errors': [ + {'stage': 'collector', 'error_type': type(error).__name__}]} + save(data) + raise + finally: + if metro.poll() is None: + os.killpg(metro.pid, signal.SIGTERM) + try: + metro.wait(timeout=10) + except subprocess.TimeoutExpired: + os.killpg(metro.pid, signal.SIGKILL) + metro_output.close() + if device and PLATFORM == 'IOS': + subprocess.run(['xcrun', 'simctl', 'shutdown', device], stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, timeout=30, check=False) + subprocess.run(['xcrun', 'simctl', 'delete', device], stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, timeout=30, check=False) + elif device: + subprocess.run(['adb', '-s', device, 'uninstall', APP_IDS[PLATFORM]], stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, timeout=30, check=False) + + +def main(): + if PLATFORM not in APP_IDS or len(sys.argv) != 2 or sys.argv[1] not in ['record', 'prepare', 'driver', 'build', 'smoke']: + raise RuntimeError('Expected a hosted platform and record|prepare|driver|build|smoke.') + os.environ.update({'ENVFILE': '.env.ci', 'RCT_NO_LAUNCH_PACKAGER': '1', + 'RCT_NEW_ARCH_ENABLED': '1', 'MAESTRO_CLI_NO_ANALYTICS': '1'}) + run_phase(sys.argv[1]) + + +def run_phase(phase): + try: + globals()[phase]() + except Exception: + if EVIDENCE.exists(): + evidence = read() + evidence['failed_phase'] = phase + if phase in ['build', 'smoke']: + evidence[phase] = 'FAIL' + save(evidence) + raise + + +if __name__ == '__main__': + try: + main() + except Exception: + # Unexpected exceptions can contain private paths or process output too. + print('Hosted native validation failed; inspect phase status and fixed diagnostic categories.', file=sys.stderr) + sys.exit(1) diff --git a/.github/native/nodeify.cjs b/.github/native/nodeify.cjs new file mode 100644 index 0000000000..d24127bf11 --- /dev/null +++ b/.github/native/nodeify.cjs @@ -0,0 +1,33 @@ +#!/usr/bin/env node +'use strict'; + +const path = require('node:path'); + +// Match setup.sh's existing polyfills without invoking its dependency installer. +// The unfiltered --hack command selects every core shim, including packages this +// app does not depend on, and makes valid imports such as constants unresolvable. +const SHIMS = Object.freeze([ + 'buffer', 'events', 'process', 'stream', 'inherits', 'path', 'assert', 'crypto', +]); + +function runNodeify(root = path.resolve(__dirname, '../..')) { + if (process.cwd() !== root) { + throw new Error('Run native Node compatibility preparation from the repository root.'); + } + const cli = require.resolve('rn-nodeify/cmd.js', { paths: [root] }); + const coreList = require.resolve(path.join(path.dirname(cli), 'coreList.js')); + require(coreList); + require.cache[coreList].exports = [...SHIMS]; + const originalArguments = process.argv; + try { + // No --install flag: the pinned upstream CLI can apply its existing module + // and file compatibility fixes, but cannot enter installShims(). + process.argv = [process.execPath, cli, '--hack']; + require(cli); + } finally { + process.argv = originalArguments; + } +} + +module.exports = { runNodeify, SHIMS }; +if (require.main === module) runNodeify(); diff --git a/.github/native/startup.yaml b/.github/native/startup.yaml new file mode 100644 index 0000000000..1dc9396374 --- /dev/null +++ b/.github/native/startup.yaml @@ -0,0 +1,22 @@ +# Intentionally stop before persisting a PIN or generating any recovery material. +appId: ${APP_ID} +name: Disposable startup and passcode input +--- +- launchApp: + clearState: true + permissions: + all: deny +- extendedWaitUntil: + visible: Create a passcode + timeout: 180000 +- assertNotVisible: Confirm your passcode +- tapOn: + id: key_1 +- tapOn: + id: key_2 +- tapOn: + id: key_3 +- tapOn: + id: key_4 +- assertVisible: Confirm your passcode +- stopApp diff --git a/.github/tests/keeper-local-validation.test.cjs b/.github/tests/keeper-local-validation.test.cjs index 3de7d79945..959b8a6a70 100644 --- a/.github/tests/keeper-local-validation.test.cjs +++ b/.github/tests/keeper-local-validation.test.cjs @@ -15,6 +15,12 @@ const runWorkflow = new (Object.getPrototypeOf(async function () {}).constructor ); const sha = 'b78b480eb4223390b2d17073387f9887d2e35f95'; +const baseSha = 'a'.repeat(40); +const runBaseSha = 'c'.repeat(40); +const repository = 'KeeperCommunity/bitcoin-keeper'; +const repositoryId = 470904030; +const headRef = 'codex/example-change'; +const nativeWorkflowPath = '.github/workflows/keeper-hosted-native.yml'; const prNumber = 7045; const comment = (body, login) => ({ body, user: { login } }); const handoff = (head = sha, checks = 'IOS, ANDROID, HARDWARE') => comment( @@ -41,27 +47,102 @@ Real devices/signers: ${overrides.hardware || 'NOT RUN / POST-RELEASE by owner d 'local-writer' ); +const hostedRun = (overrides = {}) => ({ + id: 90, + workflow_id: 123, + path: nativeWorkflowPath, + head_sha: sha, + head_branch: headRef, + head_repository: { full_name: repository, id: repositoryId }, + repository: { full_name: repository, id: repositoryId }, + event: 'pull_request', + status: 'completed', + conclusion: 'success', + run_attempt: 2, + html_url: `https://github.com/${repository}/actions/runs/90`, + pull_requests: [{ + number: prNumber, + head: { sha, ref: headRef, repo: { id: repositoryId } }, + base: { sha: runBaseSha, ref: 'sprint', repo: { id: repositoryId } } + }], + ...overrides +}); +const hostedJob = (platform, overrides = {}) => ({ + name: platform === 'IOS' ? 'Hosted iOS simulator' : 'Hosted Android emulator', + head_sha: sha, + run_id: 90, + run_attempt: 2, + status: 'completed', + conclusion: 'success', + steps: ['Build development app', 'Run disposable startup smoke'].map(name => ({ + name, status: 'completed', conclusion: 'success' + })), + ...overrides +}); +const trustedTree = () => ({ + truncated: false, + tree: [ + { path: nativeWorkflowPath, mode: '100644', type: 'blob', sha: '1'.repeat(40) }, + { path: '.github/native', mode: '040000', type: 'tree', sha: '2'.repeat(40) }, + { path: '.github/native/smoke.yaml', mode: '100644', type: 'blob', sha: '3'.repeat(40) }, + { path: '.github/native/build.sh', mode: '100755', type: 'blob', sha: '4'.repeat(40) }, + { path: 'src/App.tsx', mode: '100644', type: 'blob', sha: '5'.repeat(40) } + ] +}); +const hostedOptions = (overrides = {}) => ({ + runs: [hostedRun()], jobs: [hostedJob('IOS'), hostedJob('ANDROID')], ...overrides +}); + async function evaluate(comments, options = {}) { const filesRoute = () => {}; const commentsRoute = () => {}; + const runsRoute = () => {}; + const jobsRoute = () => {}; const statuses = []; const failures = []; + const warnings = []; + const calls = []; + const pullsRequested = []; + let pullReads = 0; const github = { - paginate: async route => { + paginate: async (route, args) => { + calls.push({ route, args }); if (route === filesRoute) return options.files || [{ filename: 'src/App.tsx' }]; if (route === commentsRoute) return comments; + if (route === runsRoute) { + if (options.runsError) throw new Error(options.runsError); + return [...(options.runs || [])]; + } + if (route === jobsRoute) { + if (options.jobsError) throw new Error(options.jobsError); + return options.jobsByRun?.[args.run_id] || options.jobs || []; + } throw new Error('Unexpected paginate route'); }, rest: { pulls: { - get: async () => ({ data: { - base: { ref: 'sprint' }, - head: { sha: options.head || sha }, - body: '', user: { login: 'cloud-writer' }, - html_url: 'https://github.com/KeeperCommunity/bitcoin-keeper/pull/7045' - } }), + get: async ({ pull_number }) => { + pullsRequested.push(pull_number); + pullReads += 1; + return { data: { + base: { ref: options.baseRef || 'sprint', sha: baseSha, + repo: { full_name: repository, id: repositoryId } }, + head: { sha: pullReads > 1 && options.afterHead ? options.afterHead : options.head || sha, + ref: headRef, repo: options.headRepository || { full_name: repository, id: repositoryId } }, + body: options.body || '', user: { login: 'cloud-writer' }, + draft: options.draft ?? false, + html_url: `https://github.com/${repository}/pull/${pull_number}` + } }; + }, listFiles: filesRoute }, + actions: { listWorkflowRuns: runsRoute, listJobsForWorkflowRunAttempt: jobsRoute }, + git: { + getTree: async ({ tree_sha }) => { + if (options.treeErrors?.includes(tree_sha)) throw new Error('Tree unavailable'); + return { data: options.trees?.[tree_sha] || trustedTree() }; + } + }, issues: { listComments: commentsRoute }, repos: { getCollaboratorPermissionLevel: async ({ username }) => ({ data: { @@ -74,11 +155,14 @@ async function evaluate(comments, options = {}) { } }; const context = { repo: { owner: 'KeeperCommunity', repo: 'bitcoin-keeper' }, - issue: { number: prNumber }, payload: {} }; - const core = { info: () => {}, warning: () => {}, setFailed: text => failures.push(text) }; + issue: { number: prNumber }, payload: {}, ...options.context }; + const core = { info: () => {}, warning: text => warnings.push(text), + setFailed: text => failures.push(text) }; await runWorkflow(github, context, core); - assert.equal(statuses.length, 1); - return { status: statuses[0], failures }; + assert.equal(statuses.length, options.afterHead || options.baseRef ? 0 : 1); + return { status: statuses[0], statuses, failures, warnings, calls, pullsRequested, + runsCalls: calls.filter(c => c.route === runsRoute), + jobsCalls: calls.filter(c => c.route === jobsRoute) }; } test('current requirements still fail without an owner decision', async () => { @@ -158,3 +242,305 @@ test('a new PR head invalidates both handoff and owner decision', async () => { assert.equal(status.state, 'failure'); assert.equal(status.description, 'Trusted Cloud handoff for current SHA required'); }); + +test('trusted exact-head hosted smoke passes both platforms without a local attestation', async () => { + const result = await evaluate([handoff(sha, 'IOS, ANDROID')], hostedOptions()); + assert.equal(result.status.state, 'success'); + assert.match(result.status.description, /hosted IOS, ANDROID/); + assert.equal(result.status.sha, sha); + assert.equal(result.failures.length, 0); + assert.deepEqual(result.runsCalls[0].args, { + owner: 'KeeperCommunity', repo: 'bitcoin-keeper', workflow_id: 'keeper-hosted-native.yml', + head_sha: sha, event: 'pull_request', per_page: 100 + }); + assert.equal(result.jobsCalls[0].args.attempt_number, 2); +}); + +test('successful hosted Android can combine with local iOS when hosted iOS fails', async () => { + const { status } = await evaluate([ + handoff(sha, 'IOS, ANDROID'), report({ android: 'NOT RUN' }) + ], hostedOptions({ + runs: [hostedRun({ conclusion: 'failure' })], + jobs: [hostedJob('IOS', { conclusion: 'failure' }), hostedJob('ANDROID')] + })); + assert.equal(status.state, 'success'); + assert.match(status.description, /hosted ANDROID/); +}); + +test('one platform success cannot satisfy the other platform', async () => { + const { status } = await evaluate([handoff(sha, 'IOS, ANDROID')], hostedOptions({ + jobs: [hostedJob('ANDROID')] + })); + assert.equal(status.state, 'failure'); + assert.match(status.description, /IOS/); +}); + +test('hosted platform passes do not satisfy physical hardware checks', async () => { + const { status } = await evaluate([handoff()], hostedOptions()); + assert.equal(status.state, 'failure'); + assert.equal(status.description, 'Missing local PASS: HARDWARE'); +}); + +test('hosted passes can fill unrun platforms while exact owner rules defer hardware', async () => { + const { status } = await evaluate([handoff(), decision(), + report({ ios: 'NOT RUN', android: 'NOT RUN' })], hostedOptions()); + assert.equal(status.state, 'success'); + assert.match(status.description, /hosted IOS, ANDROID; owner-deferred HARDWARE/); +}); + +test('hosted success cannot waive hardware failures or remove later-report requirements', async () => { + for (const comments of [ + [handoff(), decision()], + [handoff(), report(), decision()], + [handoff(), decision(), report({ hardware: 'FAIL: incorrect signer address' })], + [handoff(), decision({}, 'other-writer'), report()] + ]) { + const { status } = await evaluate(comments, hostedOptions()); + assert.equal(status.state, 'failure'); + assert.match(status.description, /HARDWARE/); + } +}); + +test('latest trusted local failure or blocked report cannot be hidden by hosted passes', async () => { + for (const state of ['LOCAL_FAILED', 'BLOCKED']) { + const { status } = await evaluate([handoff(sha, 'IOS, ANDROID'), + report(), report({ state })], hostedOptions()); + assert.equal(status.state, 'failure'); + assert.equal(status.description, 'Latest local validation is not LOCAL_PASSED'); + } +}); + +test('explicit local platform failures cannot be relabeled as passed by hosted results', async () => { + for (const failedField of ['ios', 'android']) { + const { status } = await evaluate([handoff(sha, 'IOS, ANDROID'), + report({ [failedField]: 'FAIL: reproducible startup crash' })], hostedOptions()); + assert.equal(status.state, 'failure', failedField); + } +}); + +test('hosted passes still require a trusted current-head Cloud handoff', async () => { + for (const comments of [[], [handoff('d'.repeat(40), 'IOS, ANDROID')], + [comment(handoff(sha, 'IOS, ANDROID').body, 'read-only-user')]]) { + const result = await evaluate(comments, hostedOptions()); + assert.equal(result.status.state, 'failure'); + assert.equal(result.runsCalls.length, 0); + } +}); + +test('reject hosted runs with stale, wrong, missing or untrusted identity metadata', async () => { + const badRuns = [ + { head_sha: 'd'.repeat(40) }, + { head_branch: 'other-branch' }, + { head_repository: { full_name: 'attacker/bitcoin-keeper', id: 1 } }, + { path: '.github/workflows/build-only.yml' }, + { event: 'push' }, + { event: 'pull_request_target' }, + { pull_requests: [] }, + { pull_requests: [{ ...hostedRun().pull_requests[0], number: prNumber + 1 }] }, + { pull_requests: [{ ...hostedRun().pull_requests[0], + head: { sha: 'd'.repeat(40), ref: headRef, repo: { id: repositoryId } } }] }, + { pull_requests: [{ ...hostedRun().pull_requests[0], + base: { ref: 'sprint', repo: { id: repositoryId } } }] }, + { run_attempt: 0 }, + { run_attempt: '2' } + ]; + for (const change of badRuns) { + const { status } = await evaluate([handoff(sha, 'IOS, ANDROID')], + hostedOptions({ runs: [hostedRun(change)] })); + assert.equal(status.state, 'failure', JSON.stringify(change)); + } +}); + +test('reject hosted evidence whose recorded PR association has wrong refs or repositories', async () => { + const original = hostedRun().pull_requests[0]; + for (const change of [ + { base: { ...original.base, ref: 'dev' } }, + { base: { ...original.base, repo: { id: 1 } } }, + { head: { ...original.head, ref: 'other-branch' } }, + { head: { ...original.head, repo: { id: 1 } } } + ]) { + const { status } = await evaluate([handoff(sha, 'IOS, ANDROID')], + hostedOptions({ runs: [hostedRun({ pull_requests: [{ ...original, ...change }] })] })); + assert.equal(status.state, 'failure', JSON.stringify(change)); + } +}); + +test('fork-hosted runs retain local fallback but cannot supply trusted hosted evidence', async () => { + const options = hostedOptions({ headRepository: { full_name: 'contributor/bitcoin-keeper', id: 1 } }); + const hostedOnly = await evaluate([handoff(sha, 'IOS, ANDROID')], options); + assert.equal(hostedOnly.status.state, 'failure'); + const local = await evaluate([handoff(sha, 'IOS, ANDROID'), report()], options); + assert.equal(local.status.state, 'success'); +}); + +test('hosted CI requires matching workflow and entire native harness in source and both bases', async () => { + for (const ref of [sha, baseSha, runBaseSha]) { + for (const mutation of ['workflow', 'smoke', 'add', 'delete', 'mode']) { + const tree = trustedTree(); + if (mutation === 'workflow') tree.tree[0].sha = 'f'.repeat(40); + if (mutation === 'smoke') tree.tree[2].sha = 'f'.repeat(40); + if (mutation === 'add') tree.tree.push({ + path: '.github/native/extra.sh', mode: '100755', type: 'blob', sha: 'f'.repeat(40) + }); + if (mutation === 'delete') tree.tree.splice(2, 1); + if (mutation === 'mode') tree.tree[3].mode = '100644'; + const { status } = await evaluate([handoff(sha, 'IOS, ANDROID')], + hostedOptions({ trees: { [ref]: tree } })); + assert.equal(status.state, 'failure', `${ref}: ${mutation}`); + } + } +}); + +test('unrelated source changes do not invalidate immutable hosted CI definitions', async () => { + const tree = trustedTree(); + tree.tree[4].sha = 'f'.repeat(40); + const { status } = await evaluate([handoff(sha, 'IOS, ANDROID')], + hostedOptions({ trees: { [sha]: tree } })); + assert.equal(status.state, 'success'); +}); + +test('bootstrap without trusted native infrastructure and truncated trees fail closed', async () => { + for (const tree of [{ truncated: false, tree: [] }, + { ...trustedTree(), truncated: true }, + { truncated: false, tree: trustedTree().tree.filter(e => e.path !== nativeWorkflowPath) }, + { truncated: false, tree: trustedTree().tree.filter(e => !e.path.startsWith('.github/native/')) }]) { + const { status } = await evaluate([handoff(sha, 'IOS, ANDROID')], + hostedOptions({ trees: { [baseSha]: tree } })); + assert.equal(status.state, 'failure'); + } +}); + +test('hosted API failures never become a pass and preserve genuine local fallback', async () => { + for (const failure of [{ runsError: 'Actions unavailable' }, + { jobsError: 'Jobs unavailable' }, { treeErrors: [runBaseSha] }]) { + const options = hostedOptions(failure); + const hostedOnly = await evaluate([handoff(sha, 'IOS, ANDROID')], options); + assert.equal(hostedOnly.status.state, 'failure'); + assert.ok(hostedOnly.warnings.length); + const local = await evaluate([handoff(sha, 'IOS, ANDROID'), report()], options); + assert.equal(local.status.state, 'success'); + } +}); + +test('cancelled, skipped, pending or incomplete hosted runs provide no platform evidence', async () => { + for (const change of [{ status: 'queued', conclusion: null }, + { status: 'in_progress', conclusion: null }, { conclusion: 'cancelled' }, + { conclusion: 'skipped' }, { conclusion: 'timed_out' }, { conclusion: 'action_required' }, + { conclusion: 'neutral' }, { conclusion: null }]) { + const { status } = await evaluate([handoff(sha, 'IOS, ANDROID')], + hostedOptions({ runs: [hostedRun(change)] })); + assert.equal(status.state, 'failure', JSON.stringify(change)); + } +}); + +test('a newer failed or pending run supersedes an older successful run regardless of API order', async () => { + for (const newest of [hostedRun({ id: 91, conclusion: 'cancelled' }), + hostedRun({ id: 91, status: 'in_progress', conclusion: null }), + hostedRun({ id: 91, conclusion: 'failure' })]) { + const { status, jobsCalls } = await evaluate([handoff(sha, 'IOS, ANDROID')], hostedOptions({ + runs: [hostedRun(), newest], jobsByRun: { 91: [] } + })); + assert.equal(status.state, 'failure'); + assert.ok(jobsCalls.every(call => call.args.run_id === 91)); + } +}); + +test('reruns inspect only the newest attempt and do not reuse earlier success', async () => { + const result = await evaluate([handoff(sha, 'IOS, ANDROID')], hostedOptions({ + runs: [hostedRun({ run_attempt: 3 })], jobs: [hostedJob('IOS'), hostedJob('ANDROID')] + })); + assert.equal(result.jobsCalls[0].args.attempt_number, 3); + assert.equal(result.status.state, 'failure'); +}); + +test('a successful draft workflow with skipped native jobs supplies no platform or hardware pass', async () => { + for (const required of ['IOS', 'ANDROID', 'IOS, ANDROID', 'IOS, ANDROID, HARDWARE']) { + const result = await evaluate([handoff(sha, required)], hostedOptions({ + draft: true, + jobs: ['IOS', 'ANDROID'].map(platform => hostedJob(platform, { conclusion: 'skipped', steps: [] })), + })); + assert.equal(result.status.state, 'failure', required); + } +}); + +test('a newer skipped draft run cannot reuse a ready PR run or an earlier attempt', async () => { + for (const run of [hostedRun({ id: 91 }), hostedRun({ id: 91, run_attempt: 3 })]) { + const result = await evaluate([handoff(sha, 'IOS, ANDROID')], hostedOptions({ + draft: true, runs: [hostedRun(), run], + jobsByRun: { 91: ['IOS', 'ANDROID'].map(platform => hostedJob(platform, + { run_id: 91, run_attempt: run.run_attempt, conclusion: 'skipped', steps: [] })) }, + })); + assert.equal(result.status.state, 'failure'); + assert.ok(result.jobsCalls.every(call => call.args.run_id === 91 && call.args.attempt_number === run.run_attempt)); + } +}); + +test('wrong run identity and stale source on jobs cannot satisfy current platform checks', async () => { + for (const change of [{ head_sha: 'd'.repeat(40) }, { run_id: 89 }, + { run_attempt: 1 }, { status: 'in_progress' }, { conclusion: 'cancelled' }, + { conclusion: 'skipped' }, { conclusion: 'failure' }]) { + const { status } = await evaluate([handoff(sha, 'IOS')], hostedOptions({ + jobs: [hostedJob('IOS', change)] + })); + assert.equal(status.state, 'failure', JSON.stringify(change)); + } +}); + +test('a build-only job or missing, skipped, cancelled or failed smoke is not native validation', async () => { + for (const steps of [[], [hostedJob('IOS').steps[0]], [hostedJob('IOS').steps[1]], + ...['skipped', 'cancelled', 'failure'].map(conclusion => [ + hostedJob('IOS').steps[0], { ...hostedJob('IOS').steps[1], conclusion } + ]), [hostedJob('IOS').steps[0], { ...hostedJob('IOS').steps[1], status: 'in_progress' }]]) { + const { status } = await evaluate([handoff(sha, 'IOS')], hostedOptions({ + jobs: [hostedJob('IOS', { steps })] + })); + assert.equal(status.state, 'failure', JSON.stringify(steps)); + } +}); + +test('duplicate platform jobs or required steps do not ambiguously satisfy validation', async () => { + for (const jobs of [[hostedJob('IOS'), hostedJob('IOS')], + [hostedJob('IOS', { steps: [...hostedJob('IOS').steps, hostedJob('IOS').steps[1]] })]]) { + const { status } = await evaluate([handoff(sha, 'IOS')], hostedOptions({ jobs })); + assert.equal(status.state, 'failure'); + } +}); + +test('workflow_run routing evaluates the associated PR and refreshes its current head', async () => { + const routedNumber = 7059; + const run = hostedRun({ pull_requests: [{ ...hostedRun().pull_requests[0], number: routedNumber }] }); + const result = await evaluate([handoff(sha, 'IOS, ANDROID')], hostedOptions({ + runs: [run], context: { issue: {}, eventName: 'workflow_run', payload: { workflow_run: run } } + })); + assert.equal(result.status.state, 'success'); + assert.deepEqual(result.pullsRequested, [routedNumber, routedNumber]); + assert.match(result.status.target_url, /pull\/7059$/); +}); + +test('a PR head change during validation prevents posting obsolete success', async () => { + const result = await evaluate([handoff(sha, 'IOS, ANDROID')], hostedOptions({ + afterHead: 'd'.repeat(40) + })); + assert.equal(result.statuses.length, 0); + assert.match(result.failures[0], /PR head changed/); +}); + +test('documentation and GitHub metadata exemption never consumes hosted CI assertions', async () => { + const result = await evaluate([], hostedOptions({ files: [ + { filename: 'AGENTS.md' }, { filename: '.github/workflows/keeper-hosted-native.yml' } + ] })); + assert.equal(result.status.state, 'success'); + assert.equal(result.runsCalls.length, 0); + assert.equal(result.jobsCalls.length, 0); +}); + +test('hosted workflow lifecycle events refresh the privileged gate under the exact workflow name', () => { + const nativeWorkflow = fs.readFileSync(path.join(path.dirname(workflowPath), + 'keeper-hosted-native.yml'), 'utf8'); + const name = nativeWorkflow.match(/^name:\s*(.+)$/m)?.[1]; + assert.ok(name, 'hosted workflow must have a name'); + assert.ok(lines.some(line => line.trim() === `workflows: [${name}]`), + 'workflow_run must name the actual hosted workflow'); + assert.ok(lines.some(line => line.trim() === 'types: [completed, requested, in_progress]'), + 'new attempts must invalidate old hosted evidence promptly'); +}); diff --git a/.github/tests/native-action-pins.test.cjs b/.github/tests/native-action-pins.test.cjs new file mode 100644 index 0000000000..0f58e98578 --- /dev/null +++ b/.github/tests/native-action-pins.test.cjs @@ -0,0 +1,182 @@ +'use strict'; + +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const path = require('node:path'); +const { spawnSync } = require('node:child_process'); +const test = require('node:test'); + +// Scope: workflows introduced by the bootstrap. Existing gate/action policy is +// unchanged; this does not silently repin unrelated workflows. +const workflows = ['keeper-hosted-native.yml', 'keeper-validation-tests.yml']; +// Verified upstream refs/tags/v2.34.0^{} on 2026-10-18, also used by #7062. +// The annotated tag object is 324029e2f414c084d8b15ba075288885e74aef9c. +const emulatorCommit = '1dcd0090116d15e7c562f8db72807de5e036a4ed'; +const emulatorAction = 'reactivecircus/android-emulator-runner'; +// Other pins match the upstream tags noted in the workflows, read-only checked +// on the same date. Updates require a new verified commit, not just SHA syntax. +const verifiedPins = new Map([ + ['actions/checkout', 'd23441a48e516b6c34aea4fa41551a30e30af803'], + ['actions/setup-node', '249970729cb0ef3589644e2896645e5dc5ba9c38'], + ['actions/setup-java', 'b6effb05e454b25005698d916606bdc6ffcbf961'], + ['ruby/setup-ruby', '675dd7ba1b06c8786a1480d89c384f5620a42647'], + ['actions/upload-artifact', 'b7c566a772e6b6bfb58ed0dc250532a479d7789f'], + ['android-actions/setup-android', 'be39fa834029ff78f1a44aa3bb0819b8fc2bd8fd'], + [emulatorAction, emulatorCommit], +]); + +// Use the host's existing Python/PyYAML parser, never app dependencies or a +// partial YAML regex. Missing tooling, ambiguous duplicate keys and unsupported +// YAML values fail the contract; there is no permissive fallback or install. +const yamlParser = ` +import json +import sys +import yaml + +class UniqueSafeLoader(yaml.SafeLoader): + pass + +def unique_mapping(loader, node, deep=False): + loader.flatten_mapping(node) + result = {} + for key_node, value_node in node.value: + key = loader.construct_object(key_node, deep=deep) + if key in result: + raise ValueError('Duplicate YAML mapping key') + result[key] = loader.construct_object(value_node, deep=deep) + return result + +UniqueSafeLoader.add_constructor(yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, unique_mapping) +print(json.dumps(yaml.load(sys.stdin.read(), Loader=UniqueSafeLoader))) +`; + +function actionReferences(workflow, runParser = spawnSync) { + const result = runParser('python3', ['-B', '-c', yamlParser], { + input: workflow, encoding: 'utf8', timeout: 5000, maxBuffer: 1024 * 1024, + }); + assert.equal(result.status, 0, + `YAML parsing is required (Python 3 + PyYAML): ${result.error?.message || result.stderr || ''}`); + const document = JSON.parse(result.stdout); + const mapping = value => value !== null && typeof value === 'object' && !Array.isArray(value); + assert.ok(mapping(document) && mapping(document.jobs), 'workflow must have a jobs mapping'); + const references = []; + for (const job of Object.values(document.jobs)) { + assert.ok(mapping(job), 'each job must be a mapping'); + if (Object.hasOwn(job, 'uses')) references.push(job.uses); + if (Object.hasOwn(job, 'steps')) { + assert.ok(Array.isArray(job.steps), 'job steps must be a sequence'); + for (const step of job.steps) { + assert.ok(mapping(step), 'each step must be a mapping'); + if (Object.hasOwn(step, 'uses')) references.push(step.uses); + } + } + } + assert.ok(references.every(reference => typeof reference === 'string'), 'uses values must be strings'); + return references; +} + +function validateActionPins(workflow) { + const references = actionReferences(workflow); + assert.ok(references.length > 0, 'expected external actions in bootstrap workflow'); + for (const reference of references) { + assert.match(reference, /^[\w-]+\/[\w./-]+@[0-9a-f]{40}$/, + `external actions require a full immutable commit: ${reference}`); + const [action, revision] = reference.split('@'); + assert.ok(verifiedPins.has(action), `action needs a verified upstream commit: ${action}`); + assert.equal(revision, verifiedPins.get(action), + 'action must use its verified upstream commit, not an arbitrary SHA or tag object'); + } +} + +// Keep a valid companion action so a skipped mutable entry cannot pass merely +// because the parser found some other pinned reference in the same workflow. +function workflowWithStep(step) { + return `jobs: + contracts: + steps: + - uses: actions/setup-node@${verifiedPins.get('actions/setup-node')} + - ${step} +`; +} + +test('bootstrap external actions have full immutable pins and the verified emulator commit', () => { + for (const workflow of workflows) { + validateActionPins(fs.readFileSync(path.join(__dirname, '../workflows', workflow), 'utf8')); + } + const hosted = fs.readFileSync(path.join(__dirname, '../workflows', workflows[0]), 'utf8'); + assert.equal(actionReferences(hosted).filter(ref => ref.startsWith(`${emulatorAction}@`)).length, 1); +}); + +test('action pin validation rejects mutable, short, malformed and tag-object revisions', () => { + for (const revision of ['v2', 'v2.34.0', 'main', emulatorCommit.slice(0, 12), + '324029e2f414c084d8b15ba075288885e74aef9c38', + '324029e2f414c084d8b15ba075288885e74aef9c', 'a'.repeat(40), 'g'.repeat(40), + '${{ inputs.revision }}']) { + assert.throws(() => validateActionPins(workflowWithStep(`uses: ${emulatorAction}@${revision}`)), + /full immutable commit|verified upstream commit/, revision); + } + assert.throws(() => validateActionPins(workflowWithStep('uses: actions/checkout@v6')), /full immutable commit/); + assert.throws(() => validateActionPins(workflowWithStep(`uses: actions/checkout@${'a'.repeat(40)}`)), /verified upstream commit/); + assert.throws(() => validateActionPins(workflowWithStep(`uses: unknown/action@${emulatorCommit}`)), /verified upstream commit/); + assert.throws(() => validateActionPins('jobs: {}\n'), assert.AssertionError); +}); + +test('quoted keys, flow mappings and folded values cannot hide a mutable action beside pinned actions', () => { + const forms = [ + reference => `"uses": ${reference}`, + reference => `'uses': '${reference}'`, + reference => `"\\u0075ses": "${reference}"`, + reference => `!!str uses: ${reference}`, + reference => `{uses: ${reference}}`, + reference => `{"name": "checkout", "uses": "${reference}"}`, + reference => `uses: >-\n ${reference}`, + reference => `uses: |-\n ${reference}`, + ]; + for (const form of forms) { + const pinned = `actions/checkout@${verifiedPins.get('actions/checkout')}`; + const valid = workflowWithStep(form(pinned)); + assert.deepEqual(actionReferences(valid), [ + `actions/setup-node@${verifiedPins.get('actions/setup-node')}`, pinned, + ]); + validateActionPins(valid); + for (const mutable of ['actions/checkout@v6', 'actions/checkout@main']) { + const invalid = workflowWithStep(form(mutable)); + assert.ok(actionReferences(invalid).includes(mutable), 'mutable action must be inspected, not skipped'); + assert.throws(() => validateActionPins(invalid), /full immutable commit/, form(mutable)); + } + } +}); + +test('whole flow documents, aliases and job-level uses are inspected', () => { + const pinned = `actions/checkout@${verifiedPins.get('actions/checkout')}`; + const forms = [ + reference => JSON.stringify({ jobs: { contracts: { steps: [ + { uses: pinned }, { uses: reference }, + ] } } }), + reference => `jobs:\n contracts:\n steps:\n - uses: ${pinned}\n - &checkout {uses: ${reference}}\n - *checkout\n`, + reference => `jobs: {build: {steps: [{uses: ${pinned}}]}, reuse: {"uses": "${reference}"}}\n`, + ]; + for (const form of forms) { + validateActionPins(form(pinned)); + assert.ok(actionReferences(form('actions/checkout@v6')).includes('actions/checkout@v6')); + assert.throws(() => validateActionPins(form('actions/checkout@v6')), /full immutable commit/); + } +}); + +test('YAML inspection fails closed for unavailable parsers, invalid input and duplicate keys', () => { + for (const result of [ + { status: null, error: new Error('spawn python3 ENOENT') }, + { status: 1, stderr: "ModuleNotFoundError: No module named 'yaml'" }, + { status: null, error: new Error('parser timeout') }, + ]) { + assert.throws(() => actionReferences('jobs: {}', () => result), /YAML parsing is required/); + } + assert.throws(() => validateActionPins('jobs: ['), /YAML parsing is required/); + const pinned = `actions/checkout@${verifiedPins.get('actions/checkout')}`; + for (const references of [[pinned, 'actions/checkout@v6'], ['actions/checkout@v6', pinned]]) { + assert.throws(() => validateActionPins(workflowWithStep( + `uses: ${references[0]}\n "uses": ${references[1]}`)), /Duplicate YAML mapping key/); + } + assert.throws(() => validateActionPins(workflowWithStep('uses: {action: actions/checkout@v6}')), + /uses values must be strings/); +}); diff --git a/.github/tests/native-draft-policy.test.cjs b/.github/tests/native-draft-policy.test.cjs new file mode 100644 index 0000000000..979034c2be --- /dev/null +++ b/.github/tests/native-draft-policy.test.cjs @@ -0,0 +1,131 @@ +'use strict'; + +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const path = require('node:path'); +const { spawnSync } = require('node:child_process'); +const test = require('node:test'); + +const directory = path.resolve(__dirname, '../workflows'); +// BaseLoader keeps YAML's `on` key and scalar text intact for these policy +// checks. Parsing failure is fatal; this is not workflow semantic lint. +const parsed = spawnSync('python3', ['-B', '-c', ` +import json, pathlib, sys, yaml +print(json.dumps({p.name: yaml.load(p.read_text(), Loader=yaml.BaseLoader) + for p in pathlib.Path(sys.argv[1]).glob('*.yml')})) +`, directory], { encoding: 'utf8', timeout: 5000 }); +assert.equal(parsed.status, 0, parsed.stderr || parsed.error?.message); +const workflows = JSON.parse(parsed.stdout); +const native = workflows['keeper-hosted-native.yml']; +const ci = workflows['test.yml']; +const guard = "github.event_name == 'pull_request' && toJSON(github.event.pull_request.draft) == 'false'"; + +// Evaluate the actual checked-in expressions using only their two context +// paths and toJSON. Missing context is null, as in Actions. These conditions +// compare strings only, so JavaScript and Actions equality give the same result. +function allowed(condition, eventName, draft = null) { + assert.equal(typeof condition, 'string', 'an explicit condition is required'); + const expression = condition + .replaceAll('github.event_name', 'eventName') + .replaceAll('github.event.pull_request.draft', 'draft'); + return Boolean(new Function('eventName', 'draft', 'toJSON', `return (${expression});`)( + eventName, draft, JSON.stringify)); +} + +function triggered(workflow, event, action, branch) { + const config = workflow.on[event]; + if (config === undefined) return false; + if (config.branches && !config.branches.includes(branch)) return false; + const types = config.types || (event === 'pull_request' ? ['opened', 'synchronize', 'reopened'] : null); + return !types || types.includes(action); +} + +test('native opened, synchronize and reopened draft jobs are skipped before runner allocation', () => { + assert.deepEqual(Object.keys(native.jobs), ['ios', 'android']); + for (const action of ['opened', 'synchronize', 'reopened']) { + assert.ok(triggered(native, 'pull_request', action, 'sprint')); + for (const job of Object.values(native.jobs)) { + assert.equal(job.if, guard); + assert.equal(allowed(job.if, 'pull_request', true), false, action); + assert.equal(allowed(job.if, 'pull_request', false), true, action); + } + } +}); + +test('ready-for-review schedules native work and normal Tests; conversion back to draft skips native work', () => { + assert.ok(triggered(native, 'pull_request', 'ready_for_review', 'sprint')); + assert.ok(triggered(ci, 'pull_request', 'ready_for_review', 'sprint')); + assert.ok(triggered(native, 'pull_request', 'converted_to_draft', 'sprint')); + for (const job of Object.values(native.jobs)) { + assert.equal(allowed(job.if, 'pull_request', false), true); + assert.equal(allowed(job.if, 'pull_request', true), false); + } + assert.equal(native.concurrency.group, 'keeper-hosted-native-${{ github.event.pull_request.number }}'); + assert.equal(native.concurrency['cancel-in-progress'], 'true'); +}); + +test('native non-PR and missing or malformed draft contexts fail closed', () => { + assert.deepEqual(Object.keys(native.on), ['pull_request']); + for (const job of Object.values(native.jobs)) { + for (const event of ['push', 'workflow_dispatch', 'workflow_call', 'workflow_run', 'schedule', 'pull_request_target']) { + assert.equal(allowed(job.if, event, false), false, event); + } + for (const draft of [null, '', 'false', 'true', 0, 1]) { + assert.equal(allowed(job.if, 'pull_request', draft), false, JSON.stringify(draft)); + } + } +}); + +test('bootstrap has no reusable native caller, worker or dispatch entry point', () => { + assert.deepEqual(Object.keys(workflows).sort(), [ + 'codeql.yml', 'keeper-hosted-native.yml', 'keeper-local-validation.yml', + 'keeper-validation-tests.yml', 'rag-ingest-release.yml', 'test.yml', + ]); + for (const [filename, workflow] of Object.entries(workflows)) { + assert.ok(!Object.hasOwn(workflow.on, 'workflow_call'), filename); + assert.ok(!Object.hasOwn(workflow.on, 'workflow_dispatch'), filename); + for (const job of Object.values(workflow.jobs)) assert.ok(!job.uses, filename); + } + // #7062's reusable workflows are intentionally absent, not silently adopted. + // Any later addition requires an explicit extension of this entry-path review. +}); + +test('pushing the unpublished branch triggers no checked-in push workflow', () => { + for (const [filename, workflow] of Object.entries(workflows)) { + assert.equal(triggered(workflow, 'push', null, 'codex/cloud-bootstrap-privacy'), false, filename); + } +}); + +test('draft opening and updates retain the expected host checks without a release trigger', () => { + for (const action of ['opened', 'synchronize', 'reopened']) { + const triggeredFiles = Object.entries(workflows).filter(([, workflow]) => + triggered(workflow, 'pull_request', action, 'sprint')).map(([filename]) => filename).sort(); + assert.deepEqual(triggeredFiles, [ + 'codeql.yml', 'keeper-hosted-native.yml', 'keeper-validation-tests.yml', 'test.yml', + ]); + assert.ok(triggered(workflows['keeper-local-validation.yml'], 'pull_request_target', action, 'sprint')); + } +}); + +test('draft Tests install cannot invoke native prepare and does not waive install or test failure', () => { + const steps = ci.jobs.test.steps; + const installs = steps.filter(step => step.run?.startsWith('yarn install')); + assert.equal(installs.length, 2); + for (const [event, draft, expected] of [ + ['pull_request', true, 'yarn install --frozen-lockfile --ignore-scripts'], + ['pull_request', null, 'yarn install --frozen-lockfile --ignore-scripts'], + ['pull_request', 'false', 'yarn install --frozen-lockfile --ignore-scripts'], + ['pull_request', false, 'yarn install'], ['push', null, 'yarn install'], + ]) { + const active = installs.filter(step => allowed(step.if, event, draft)); + assert.equal(active.length, 1); + assert.equal(active[0].run, expected); + } + assert.ok(!ci.jobs.test.if && !ci.jobs.test['continue-on-error']); + for (const step of steps) assert.ok(!step['continue-on-error']); + const tests = steps.find(step => step.name === 'Run tests'); + assert.equal(tests.run, 'npm run test'); + assert.ok(!tests.if); + assert.equal(ci.jobs.lint.steps.find(step => step.name === 'Install dependencies').run, + 'yarn install --ignore-scripts'); +}); diff --git a/.github/tests/nodeify.test.cjs b/.github/tests/nodeify.test.cjs new file mode 100644 index 0000000000..400c365346 --- /dev/null +++ b/.github/tests/nodeify.test.cjs @@ -0,0 +1,38 @@ +'use strict'; + +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const os = require('node:os'); +const path = require('node:path'); +const { spawnSync } = require('node:child_process'); +const test = require('node:test'); + +const wrapper = path.resolve(__dirname, '../native/nodeify.cjs'); + +test('native shim preparation filters upstream defaults without requesting installation', t => { + const root = fs.mkdtempSync(path.join(os.tmpdir(), 'keeper-nodeify-')); + t.after(() => fs.rmSync(root, { recursive: true, force: true })); + const upstream = path.join(root, 'node_modules/rn-nodeify'); + fs.mkdirSync(upstream, { recursive: true }); + fs.writeFileSync(path.join(upstream, 'coreList.js'), "module.exports = ['constants', 'fs', 'net'];\n"); + fs.writeFileSync(path.join(upstream, 'cmd.js'), ` + process.stdout.write(JSON.stringify({ + shims: require('./coreList'), arguments: process.argv.slice(2) + })); + `); + const result = spawnSync(process.execPath, ['-e', + 'require(process.argv[1]).runNodeify(process.cwd())', wrapper], { cwd: root, encoding: 'utf8' }); + assert.equal(result.status, 0, result.stderr); + const output = JSON.parse(result.stdout); + assert.deepEqual(output.arguments, ['--hack']); + assert.deepEqual(output.shims, [ + 'buffer', 'events', 'process', 'stream', 'inherits', 'path', 'assert', 'crypto', + ]); + assert.ok(!output.shims.includes('constants')); +}); + +test('native shim preparation rejects an unrelated working directory', () => { + const { runNodeify } = require(wrapper); + assert.throws(() => runNodeify(path.join(os.tmpdir(), 'unrelated-keeper-checkout')), + /repository root/); +}); diff --git a/.github/tests/test_diagnostic_privacy.py b/.github/tests/test_diagnostic_privacy.py new file mode 100644 index 0000000000..923d7562d3 --- /dev/null +++ b/.github/tests/test_diagnostic_privacy.py @@ -0,0 +1,136 @@ +"""Adversarial export contracts with synthetic data; no native tools or app state.""" + +from contextlib import redirect_stdout, redirect_stderr +import io +import json +from pathlib import Path +import runpy +import subprocess +import sys +import unittest +from unittest import mock + +import test_hosted_native as harness + +native = harness.native + + +# Reuse the isolated evidence fixture without running the inherited test suite. +class DiagnosticPrivacyTest(unittest.TestCase): + setUp = harness.HostedNativeHarnessTest.setUp + + def test_arbitrary_private_text_cannot_be_exported_by_any_log_classifier(self): + payloads = [ + 'PRIVATE_SENTINEL ordinary wallet name and recovery words with no secret label', + 'PRIVATE_SENTINEL xpubSyntheticExtendedKey bc1qsyntheticaddress user@example.invalid', + 'PRIVATE_SENTINEL token = "two words" password: two words Bearer synthetic', + 'PRIVATE_SENTINEL file:///Users/private-user/Documents/wallet.txt /home/private-user/backup', + 'PRIVATE_SENTINEL https://example.invalid/private?phrase=one%20two', + 'PRIVATE_SENTINEL cHJpdmF0ZSB3b3Jkcw== \\u0070rivate synthetic', + 'PRIVATE_SENTINEL \u79d8\u5bc6 \u03bc\u03c5\u03c3\u03c4\u03b9\u03ba\u03cc \u202esecret \x00\x1b[31mprivate\x1b[0m', + 'PRIVATE_SENTINEL first line\nprivate continuation\rprivate last line', + 'PRIVATE_SENTINEL ' + 'long arbitrary private text ' * 100, + ] + routes = [ + (native.failure_details, 'error: {}', ['TOOL_ERROR']), + (native.failure_details, 'e: {}', ['KOTLIN_COMPILER_ERROR']), + (native.failure_details, 'FAILURE: {}', ['BUILD_FAILURE']), + (native.failure_details, 'Caused by: {}', ['TOOL_ERROR']), + (native.maestro_failure_details, 'Unable to launch app {}', ['Maestro app launch failed']), + (native.maestro_failure_details, 'Failed to connect {}', ['Maestro connection failed']), + (native.maestro_failure_details, 'Failed to start {}', ['Maestro driver start failed']), + (native.maestro_failure_details, 'No devices found {}', ['Maestro device unavailable']), + (native.android_startup_errors, 'E/AndroidRuntime( 123): FATAL EXCEPTION: {}', ['ANDROID_RUNTIME_ERROR']), + (native.android_startup_errors, 'E/ReactNativeJS( 123): TypeError: {}', ['REACT_NATIVE_JS_ERROR']), + (native.android_startup_errors, 'F/ReactNative( 123): Could not {}', ['REACT_NATIVE_ERROR']), + (native.metro_startup_errors, 'ERROR {}', ['METRO_ERROR']), + (native.metro_startup_errors, 'SyntaxError: {}', ['METRO_SYNTAX_ERROR']), + (native.metro_startup_errors, 'TypeError: {}', ['METRO_TYPE_ERROR']), + (native.metro_startup_errors, 'ReferenceError: {}', ['METRO_REFERENCE_ERROR']), + (native.metro_startup_errors, 'Unable to resolve module {}', ['METRO_MODULE_RESOLUTION_ERROR']), + ] + for payload in payloads: + for classify, template, expected in routes: + with self.subTest(route=template, payload=payload[:70]): + self.assertEqual(classify(template.format(payload)), expected) + with self.subTest(route='ios', payload=payload[:70]): + diagnostics = native.ios_startup_diagnostics( + '2026-10-18 06:00:00.000 E hexa_keeper_dev[123:abc] ' + payload, 'hexa_keeper_dev') + self.assertEqual(diagnostics['errors'], ['IOS_STARTUP_ERROR']) + + def test_general_failure_categories_cover_install_blockers_without_excerpts(self): + messages = [ + ('error: lockfile needs to be updated', 'DEPENDENCY_LOCK_MISMATCH'), + ('[!] could not find compatible versions', 'DEPENDENCY_LOCK_MISMATCH'), + ('The sandbox is not in sync', 'DEPENDENCY_LOCK_MISMATCH'), + ('error: no space left on device', 'DISK_SPACE_EXHAUSTED'), + ('error: command not found', 'REQUIRED_TOOL_UNAVAILABLE'), + ('[!] requires a higher minimum deployment target', 'IOS_DEPLOYMENT_TARGET_UNSUPPORTED'), + ('[!] other pod failure', 'COCOAPODS_ERROR'), + ('A problem occurred', 'BUILD_FAILURE'), + ('Execution failed', 'BUILD_FAILURE'), + ('Could not find a dependency', 'DEPENDENCY_OR_TOOL_FAILURE'), + ('> compiler detail', 'TOOL_ERROR'), + ] + for message, category in messages: + with self.subTest(message=message): + self.assertEqual(native.failure_details(message + ' PRIVATE_SENTINEL'), [category]) + self.assertEqual(native.failure_details('ordinary PRIVATE_SENTINEL\n> Task :compile\n' + ' 12 | error: PRIVATE_SENTINEL'), []) + self.assertEqual(native.failure_details('error: PRIVATE_SENTINEL\n' * 1000), ['TOOL_ERROR']) + self.assertEqual(native.metro_startup_errors('ERROR PRIVATE_SENTINEL\n' * 1000), ['METRO_ERROR']) + self.assertEqual(native.android_startup_errors('E/OtherApp( 1): Error: PRIVATE_SENTINEL'), []) + + def test_failed_command_metadata_console_and_exception_do_not_export_private_text(self): + script = self.root / 'failed-command.py' + script.write_text('print("error: PRIVATE_SENTINEL arbitrary recovery text"); raise SystemExit(3)') + console = io.StringIO() + with redirect_stdout(console), self.assertRaises(RuntimeError) as failure: + native.command([sys.executable, str(script)], 'Build fixture') + record = native.read()['commands'][0] + self.assertEqual(record['exit_code'], 3) + self.assertEqual(record['redacted_tool_errors'], ['TOOL_ERROR']) + self.assertIn('TOOL_ERROR', console.getvalue()) + self.assertNotIn('PRIVATE_SENTINEL', native.EVIDENCE.read_text() + console.getvalue() + str(failure.exception)) + self.assertIn('PRIVATE_SENTINEL', (native.PRIVATE / 'command-001.log').read_text()) + + def test_startup_collector_exports_only_categories_and_preserves_failure(self): + native.PRIVATE.mkdir() + native.save({'commands': [{'label': 'Assert startup and disposable passcode transition'}], + 'build': 'PASS', 'smoke': 'FAIL'}) + (native.PRIVATE / 'command-001.log').write_text('Unable to launch app PRIVATE_SENTINEL wallet text\n') + (native.PRIVATE / 'metro.log').write_text('ERROR PRIVATE_SENTINEL wallet text\n') + report = native.PRIVATE / 'smoke-junit.xml' + report.write_text('' + 'Failed to connect PRIVATE_SENTINEL wallet text\nUI hierarchy: PRIVATE_SENTINEL' + '') + + def android_log(args, stdout, **kwargs): + stdout.write(b'E/AndroidRuntime( 123): FATAL EXCEPTION: PRIVATE_SENTINEL wallet text\n') + return subprocess.CompletedProcess(args, 0) + + console = io.StringIO() + with redirect_stdout(console), mock.patch.object(subprocess, 'run', side_effect=android_log): + native.record_startup_failure(report, 'fixture-emulator') + data = native.read() + details = data['startup_diagnostics'] + self.assertEqual((data['build'], data['smoke']), ('PASS', 'FAIL')) + self.assertEqual(details['maestro']['testcase_statuses'], ['ERROR']) + self.assertEqual(details['maestro']['failed_commands'], ['Maestro app launch failed']) + self.assertEqual(details['maestro']['testcase_failures'], ['Maestro connection failed']) + self.assertEqual(details['metro_errors'], ['METRO_ERROR']) + self.assertEqual(details['android_errors']['errors'], ['ANDROID_RUNTIME_ERROR']) + self.assertNotIn('PRIVATE_SENTINEL', json.dumps(data) + console.getvalue()) + + def test_cli_failure_never_prints_arbitrary_exception_text(self): + console = io.StringIO() + # The record phase fails before invoking a native tool. Exercise the real + # CLI exception boundary, including its nonzero exit, with a private error. + with mock.patch.dict(native.os.environ, {'KEEPER_NATIVE_PLATFORM': 'ANDROID'}), \ + mock.patch.object(sys, 'argv', [str(Path(native.__file__)), 'record']), \ + mock.patch.object(subprocess, 'check_output', side_effect=RuntimeError('PRIVATE_SENTINEL')), \ + redirect_stderr(console), self.assertRaises(SystemExit) as failure: + runpy.run_path(native.__file__, run_name='__main__') + self.assertEqual(failure.exception.code, 1) + self.assertIn('Hosted native validation failed', console.getvalue()) + self.assertNotIn('PRIVATE_SENTINEL', console.getvalue()) diff --git a/.github/tests/test_hosted_native.py b/.github/tests/test_hosted_native.py new file mode 100644 index 0000000000..a1b9fed4f3 --- /dev/null +++ b/.github/tests/test_hosted_native.py @@ -0,0 +1,494 @@ +"""Pure hosted-harness checks: these do not claim native simulator evidence.""" + +import hashlib +import importlib.util +import json +from pathlib import Path +import subprocess +import sys +import tempfile +import unittest +from unittest import mock + + +SPEC = importlib.util.spec_from_file_location( + 'hosted_native', Path(__file__).resolve().parents[1] / 'native' / 'hosted_native.py') +native = importlib.util.module_from_spec(SPEC) +SPEC.loader.exec_module(native) + + +class HostedNativeHarnessTest(unittest.TestCase): + def setUp(self): + self.directory = tempfile.TemporaryDirectory() + self.addCleanup(self.directory.cleanup) + self.root = Path(self.directory.name) + self.patches = mock.patch.multiple(native, ROOT=self.root, PRIVATE=self.root / 'private', + EVIDENCE=self.root / 'evidence/metadata.json', PLATFORM='ANDROID') + self.patches.start() + self.addCleanup(self.patches.stop) + native.save({'commands': [], 'build': 'NOT RUN', 'smoke': 'NOT RUN'}) + + def test_success_records_command_and_duration_without_output(self): + script = self.root / 'output.py' + script.write_text('print("private output")') + result = native.command([sys.executable, str(script)], 'Success', capture=True) + self.assertEqual(result, 'private output') + record = native.read()['commands'][0] + self.assertEqual(record['exit_code'], 0) + self.assertGreaterEqual(record['duration_seconds'], 0) + self.assertNotIn('private output', native.EVIDENCE.read_text()) + + def test_failure_exposes_only_allowlisted_diagnostic(self): + script = self.root / 'failure.py' + script.write_text('print("private payload: lockfile needs to be updated"); raise SystemExit(3)') + with self.assertRaisesRegex(RuntimeError, 'Manifest and yarn.lock are inconsistent'): + native.command([sys.executable, str(script)], 'Install') + record = native.read()['commands'][0] + self.assertEqual(record['exit_code'], 3) + self.assertNotIn('private payload', native.EVIDENCE.read_text()) + + def test_hierarchy_command_failure_never_exports_arbitrary_error_lines(self): + script = self.root / 'hierarchy_failure.py' + script.write_text('print("error: PRIVATE_SENTINEL hierarchy data"); raise SystemExit(1)') + with self.assertRaisesRegex(RuntimeError, 'exit 1'): + native.command([sys.executable, str(script)], 'Driver hierarchy', error_details=False) + self.assertEqual(native.read()['commands'][0]['redacted_tool_errors'], []) + self.assertNotIn('PRIVATE_SENTINEL', native.EVIDENCE.read_text()) + + def test_tool_errors_are_deduplicated_fixed_categories(self): + output = 'private payload\nerror: missing module https://example.test/private token=secret\n' \ + + 'Caused by: password=private Bearer private\n' + 'error: detail\n' * 30 + details = native.failure_details(output) + self.assertEqual(details, ['TOOL_ERROR']) + kotlin = '> Task :compile\n' * 40 + 'e: file:///runner/App.kt:1 unresolved reference' + self.assertEqual(native.failure_details(kotlin), ['KOTLIN_COMPILER_ERROR']) + excerpts = ''.join(' %s | +(NSDictionary*)statWithError:(NSError**)error:\n' % line + for line in range(30)) + self.assertEqual(native.failure_details(excerpts + 'error: actual compiler failure'), + ['TOOL_ERROR']) + + def test_timeout_records_failure_and_terminates_process(self): + with self.assertRaisesRegex(RuntimeError, 'exit 124'): + native.command([sys.executable, '-c', 'import time; time.sleep(30)'], 'Timeout', timeout=0.05) + self.assertEqual(native.read()['commands'][0]['exit_code'], 124) + + def test_startup_diagnostics_keep_only_fixed_failed_steps_and_categories(self): + maestro = '\n'.join([ + 'Assert that "Create a passcode" is visible... FAILED', + 'Tap on id: key_2... FAILED', + 'Tap on "private recovery phrase"... FAILED', + 'UI hierarchy: private state', + 'Assertion is false: "Create a passcode" is visible; UI hierarchy: PRIVATE_SENTINEL', + 'Element not found: key_1; private arbitrary suffix', + ]) + messages = native.maestro_failure_details(maestro) + self.assertTrue(any('Create a passcode' in message for message in messages)) + self.assertIn('Maestro tap key_2 FAILED', messages) + self.assertNotIn('private', '\n'.join(messages)) + self.assertNotIn('PRIVATE_SENTINEL', '\n'.join(messages)) + self.assertNotIn('hierarchy', '\n'.join(messages)) + self.assertIn('Maestro assertion failed: Create a passcode', messages) + self.assertIn('Maestro element not found: key_1', messages) + metro = native.metro_startup_errors(' BUNDLE index.js\n ERROR Error: Unable to resolve module token=private https://example.test/private; View hierarchy: PRIVATE_SENTINEL') + self.assertEqual(metro, ['METRO_MODULE_RESOLUTION_ERROR']) + native_errors = native.android_startup_errors('\n'.join([ + 'E/AndroidRuntime( 123): FATAL EXCEPTION: main; credentials: PRIVATE_SENTINEL', + 'E/ReactNativeJS( 123): TypeError: Cannot load module token=private', + 'E/OtherApp( 124): Error: private unrelated data', + 'E/ReactNativeJS( 123): at unneeded stack frame', + ])) + self.assertEqual(native_errors, ['ANDROID_RUNTIME_ERROR', 'REACT_NATIVE_JS_ERROR']) + + def test_junit_failure_collection_omits_hierarchy_and_preserves_failed_result(self): + native.PRIVATE.mkdir() + native.save({'commands': [], 'build': 'PASS', 'smoke': 'FAIL'}) + (native.PRIVATE / 'metro.log').write_text('No bundler error\n') + report = native.PRIVATE / 'smoke-junit.xml' + report.write_text('' + 'Assertion is false: Create a passcode is visible\nUI hierarchy: private data' + '') + hierarchy = native.PRIVATE / 'maestro-output/2026-10-17_092233/Disposable startup and passcode input/screen-hierarchy' + hierarchy.mkdir(parents=True) + (hierarchy / 'step-002-assertCondition-Create_a_passcode.json').write_text(json.dumps({ + 'attributes': {'accessibilityText': 'PRIVATE_SENTINEL', 'resource-id': 'redbox-error'}, + 'children': [{'attributes': {'accessibilityText': 'No script URL provided. PRIVATE_SENTINEL'}}]})) + native.record_startup_failure(report, None) + data = native.read() + self.assertEqual(data['smoke'], 'FAIL') + self.assertEqual(data['startup_diagnostics']['maestro']['testcase_statuses'], ['ERROR']) + self.assertNotIn('private data', native.EVIDENCE.read_text()) + self.assertNotIn('PRIVATE_SENTINEL', native.EVIDENCE.read_text()) + self.assertTrue(data['startup_diagnostics']['hierarchy_signals']['RNredbox']) + self.assertTrue(data['startup_diagnostics']['hierarchy_signals']['noScriptURL']) + self.assertTrue(data['startup_diagnostics']['maestro']['testcase_failures']) + + def test_private_hierarchy_exports_only_fixed_signals_from_standalone_tree_nodes(self): + hierarchy = self.root / 'maestro-output/2026-10-17_092233/Disposable startup and passcode input/screen-hierarchy' + hierarchy.mkdir(parents=True) + (hierarchy / 'step-002-assertCondition-Create_a_passcode.json').write_text(json.dumps({ + 'attributes': {'accessibilityText': 'Create a passcode', 'value': 'PRIVATE_SENTINEL'}, + 'children': [ + {'attributes': {'accessibilityText': 'Confirm your passcode'}}, + {'attributes': {'resource-id': 'io.hexawallet.keeper.development:id/rn_redbox_stack', + 'text': 'Could not connect to development server. PRIVATE_SENTINEL'}}, + {'attributes': {'title': 'Unable to load script. PRIVATE_SENTINEL'}}, + {'attributes': {'text': 'SpringBoard PRIVATE_SENTINEL'}, 'children': None}, + ]})) + # Other JSON files are not hierarchy snapshots and must not be parsed. + (hierarchy.parent / 'commands.json').write_text('{ malformed PRIVATE_SENTINEL') + signals = native.startup_hierarchy_signals(self.root / 'maestro-output') + self.assertEqual(signals, {'createPIN': True, 'confirmPIN': True, 'RNredbox': True, + 'noScriptURL': False, 'developmentServerError': True, + 'unableLoadScript': True, 'snapshot_count': 1, 'parse_error_types': []}) + self.assertNotIn('PRIVATE_SENTINEL', json.dumps(signals)) + self.assertNotIn('SpringBoard', json.dumps(signals)) + + def test_private_hierarchy_bounds_bytes_and_nodes_and_records_only_parse_error_classes(self): + hierarchy = self.root / 'maestro-output/screen-hierarchy' + hierarchy.mkdir(parents=True) + (hierarchy / 'step-001-malformed.json').write_text('{ PRIVATE_SENTINEL') + (hierarchy / 'step-002-oversize.json').write_bytes(b'PRIVATE_SENTINEL' * (1024 * 1024 // 8)) + (hierarchy / 'step-003-too-many-nodes.json').write_text(json.dumps({ + 'attributes': {'accessibilityText': 'Create a passcode'}, 'children': [{}] * 10000})) + signals = native.startup_hierarchy_signals(self.root / 'maestro-output') + self.assertEqual(signals['snapshot_count'], 3) + self.assertEqual(set(signals['parse_error_types']), {'JSONDecodeError', 'ValueError'}) + self.assertFalse(signals['createPIN']) + self.assertNotIn('PRIVATE_SENTINEL', json.dumps(signals)) + + def test_maestro_preflight_retains_fixed_driver_markers_and_known_types_without_raw_messages(self): + output = native.PRIVATE / 'maestro-output' + output.mkdir(parents=True) + native.save({'commands': [{'label': 'Assert startup and disposable passcode transition'}], + 'smoke': 'FAIL'}) + (native.PRIVATE / 'command-001.log').write_text( + 'xcuitest.installer.LocalXCTestInstaller$IOSDriverTimeoutException: ' + 'iOS driver not ready in time, consider increasing timeout by configuring ' + 'MAESTRO_DRIVER_STARTUP_TIMEOUT env variable PRIVATE_SENTINEL\n' + 'PRIVATE_SENTINELException: arbitrary exception names must not escape\n') + (output / 'maestro.log').write_text( + '[Start] Install XCUITest runner on PRIVATE_SENTINEL\n' + '[Done] Install XCUITest runner on PRIVATE_SENTINEL\n' + '[Start] Perform XCUITest driver status check on PRIVATE_SENTINEL\n' + '[Done] Perform XCUITest driver status check on PRIVATE_SENTINEL\n' + '[Failed] Perform XCUITest driver status check on PRIVATE_SENTINEL\n' + 'Installing pre built driver without xcodebuild PRIVATE_SENTINEL\n' + 'Requested driver host port PRIVATE_SENTINEL is not available\n') + (output / 'xctest_runner_fixture.log').write_text( + 'java.io.FileNotFoundException: xctestrun config does not exist PRIVATE_SENTINEL\n' + 'ui test runner does not exist PRIVATE_SENTINEL\n' + '** TEST EXECUTE FAILED ** PRIVATE_SENTINEL\nTesting failed: PRIVATE_SENTINEL\n') + result = native.maestro_preflight_diagnostics() + self.assertTrue(result['driver_startup_timeout']) + self.assertTrue(result['runner_install_started']) + self.assertTrue(result['runner_install_launch_returned']) + self.assertTrue(result['status_response_received']) + self.assertTrue(result['status_check_failed']) + self.assertTrue(result['host_port_unavailable']) + self.assertTrue(result['xctestrun_config_missing']) + self.assertTrue(result['ui_test_runner_missing']) + self.assertTrue(result['xcode_test_execute_failed']) + self.assertTrue(result['xcode_testing_failed']) + self.assertEqual(set(result['exception_types']), {'IOSDriverTimeoutException', 'FileNotFoundException'}) + self.assertEqual(result['logs_read'], 3) + self.assertNotIn('PRIVATE_SENTINEL', json.dumps(result)) + self.assertNotIn(str(output), json.dumps(result)) + self.assertNotIn('driver_ready', result) + self.assertEqual(native.read()['smoke'], 'FAIL') + + def test_ios_driver_preflight_is_setup_only_and_cleans_only_its_created_simulator(self): + native.save({'commands': [], 'maestro': {'binary': '/private/maestro'}, + 'build': 'NOT RUN', 'smoke': 'NOT RUN'}) + owned = '12345678-1234-1234-1234-123456789abc' + with mock.patch.object(native, 'PLATFORM', 'IOS'), mock.patch.dict(native.os.environ), \ + mock.patch.object(native, 'command', side_effect=[owned, None, None, None]) as command, \ + mock.patch.object(subprocess, 'run', return_value=subprocess.CompletedProcess([], 0)) as cleanup: + native.driver() + self.assertEqual(native.os.environ['XDG_STATE_HOME'], str(native.PRIVATE / 'maestro-preflight-state')) + self.assertEqual(command.call_args.args[0], + ['/private/maestro', '--platform', 'ios', '--device', owned, 'hierarchy']) + self.assertFalse(command.call_args.kwargs['error_details']) + self.assertEqual([call.args[0] for call in cleanup.call_args_list], + [['xcrun', 'simctl', 'shutdown', owned], ['xcrun', 'simctl', 'delete', owned]]) + self.assertEqual(native.read()['driver_preflight']['result'], 'PASS') + self.assertEqual(native.read()['build'], 'NOT RUN') + self.assertEqual(native.read()['smoke'], 'NOT RUN') + + def test_ios_driver_failure_preserves_original_error_and_attempts_delete_after_cleanup_timeout(self): + native.save({'commands': [], 'maestro': {'binary': '/private/maestro'}, + 'build': 'NOT RUN', 'smoke': 'NOT RUN'}) + owned = '12345678-1234-1234-1234-123456789abc' + with mock.patch.object(native, 'PLATFORM', 'IOS'), mock.patch.dict(native.os.environ), \ + mock.patch.object(native, 'command', side_effect=[owned, None, None, RuntimeError('driver failed PRIVATE_SENTINEL')]), \ + mock.patch.object(subprocess, 'run', side_effect=[subprocess.TimeoutExpired('shutdown', 30), + subprocess.CompletedProcess([], 0)]) as cleanup, \ + self.assertRaisesRegex(RuntimeError, 'driver failed PRIVATE_SENTINEL'): + native.run_phase('driver') + self.assertEqual(cleanup.call_count, 2) + self.assertEqual(cleanup.call_args.args[0], ['xcrun', 'simctl', 'delete', owned]) + data = native.read() + self.assertEqual(data['driver_preflight']['result'], 'FAIL') + self.assertEqual(data['driver_preflight']['cleanup_errors'], [{'action': 'shutdown', 'error_type': 'TimeoutExpired'}]) + self.assertEqual(data['failed_phase'], 'driver') + self.assertEqual(data['build'], 'NOT RUN') + self.assertEqual(data['smoke'], 'NOT RUN') + self.assertNotIn('PRIVATE_SENTINEL', native.EVIDENCE.read_text()) + + def test_driver_preflight_diagnostics_cannot_contaminate_smoke_flags(self): + native.PRIVATE.mkdir() + driver_label = 'Check disposable iOS driver readiness' + native.save({'commands': [{'label': driver_label}, + {'label': 'Assert startup and disposable passcode transition'}]}) + (native.PRIVATE / 'command-001.log').write_text('IOSDriverTimeoutException PRIVATE_SENTINEL\n') + (native.PRIVATE / 'command-002.log').write_text('ordinary smoke output PRIVATE_SENTINEL\n') + state = native.PRIVATE / 'maestro-preflight-state/maestro/tests/timestamp' + state.mkdir(parents=True) + (state / 'maestro.log').write_text('** TEST EXECUTE FAILED ** PRIVATE_SENTINEL\n') + driver = native.maestro_preflight_diagnostics(state.parent, driver_label) + smoke = native.maestro_preflight_diagnostics() + self.assertTrue(driver['xcode_test_execute_failed']) + self.assertEqual(driver['exception_types'], ['IOSDriverTimeoutException']) + self.assertFalse(smoke['xcode_test_execute_failed']) + self.assertEqual(smoke['exception_types'], []) + self.assertNotIn('PRIVATE_SENTINEL', json.dumps(driver)) + + def test_maestro_preflight_bounds_files_bytes_and_collection_errors_without_changing_failure(self): + output = native.PRIVATE / 'maestro-output' + output.mkdir(parents=True) + native.save({'commands': [], 'smoke': 'FAIL'}) + (output / 'maestro.log').write_bytes( + b'PRIVATE_SENTINEL' * (1024 * 1024 // 8) + b'\nInstalling driver with xcodebuild\n') + for index in range(7): + (output / ('xctest_runner_%s.log' % index)).write_text('PRIVATE_SENTINEL\n') + result = native.maestro_preflight_diagnostics() + self.assertEqual(result['logs_read'], 6) + self.assertTrue(result['log_limit_reached']) + self.assertEqual(result['truncated_logs'], 1) + self.assertFalse(result['xcodebuild_driver_selected']) + self.assertNotIn('PRIVATE_SENTINEL', json.dumps(result)) + with mock.patch.object(native, 'private_log_prefix', side_effect=OSError('PRIVATE_SENTINEL')): + result = native.maestro_preflight_diagnostics() + self.assertEqual(result['collection_error_types'], ['OSError']) + self.assertEqual(native.read()['smoke'], 'FAIL') + + def test_ios_startup_errors_restrict_process_and_export_only_a_fixed_category(self): + payload = '\n'.join([ + 'Filtering the log data using the process predicate', + 'Timestamp Ty Process[PID:TID]', + '2026-10-17 09:22:33.123 E hexa_keeper_dev[123:abc] [com.facebook.react:javascript] NSException: module failed token=private; UI hierarchy: PRIVATE_SENTINEL', + 'private stack continuation', + '2026-10-17 09:22:34.123 E OtherApp[124:def] Error: hexa_keeper_dev private unrelated application', + '2026-10-17 09:22:35.123 Df hexa_keeper_dev[123:abc] normal startup message', + '2026-10-17 09:22:36.123 Df hexa_keeper_dev[123:abc] ReferenceError: missing startup module', + 'PRIVATE_SENTINEL multiline continuation', + '2026-10-17 09:22:37.123+0000 Df hexa_keeper_dev[123:abc] NSException: failed native startup', + 'PRIVATE_SENTINEL multiline continuation', + 'Error: hexa_keeper_dev[123:abc] missing timestamp must not be accepted', + ]) + diagnostics = native.ios_startup_diagnostics(payload, 'hexa_keeper_dev') + errors = diagnostics['errors'] + self.assertEqual(diagnostics['matching_process_records'], 4) + self.assertEqual(errors, ['IOS_STARTUP_ERROR']) + self.assertEqual(native.ios_startup_diagnostics('no compact records', 'hexa_keeper_dev')['matching_process_records'], 0) + + def test_ios_collector_keeps_launch_prefix_when_log_is_truncated_and_preserves_failure(self): + native.PRIVATE.mkdir() + native.save({'commands': [], 'smoke': 'FAIL', 'built_artifact': {'executable': 'hexa_keeper_dev'}}) + (native.PRIVATE / 'metro.log').write_text('\x1b[32m BUNDLE \x1b[0m private-path/index.js\n' + ' BUNDLE private-path/index.js\n PREBUNDLE private-path/index.js\n') + + def compact_log(args, stdout, **kwargs): + self.assertEqual(args[args.index('--style') + 1], 'compact') + self.assertEqual(args[args.index('--last') + 1], '10m') + stdout.write(b'2026-10-17 09:22:33.123 Df hexa_keeper_dev[123:abc] ReferenceError: launch module failed token=private\n') + stdout.write(b'2026-10-17 09:22:34.123 E OtherApp[124:def] Error: private unrelated data\n') + # The first MiB ends within a continuation, not a complete record. + stdout.write(b'PRIVATE_SENTINEL' * (1024 * 1024 // 8)) + stdout.write(b'\n2026-10-17 09:30:33.123 E hexa_keeper_dev[123:abc] Error: tail must not replace launch evidence\n') + return subprocess.CompletedProcess(args, 0) + + with mock.patch.object(native, 'PLATFORM', 'IOS'), \ + mock.patch.object(subprocess, 'run', side_effect=compact_log): + native.record_startup_failure(native.PRIVATE / 'missing-report.xml', 'owned-simulator') + data = native.read() + self.assertEqual(data['smoke'], 'FAIL') + diagnostics = data['startup_diagnostics']['ios_errors'] + self.assertTrue(diagnostics['input_truncated']) + self.assertEqual(diagnostics['matching_process_records'], 1) + self.assertEqual(diagnostics['errors'], ['IOS_STARTUP_ERROR']) + self.assertEqual(data['startup_diagnostics']['metro_progress'], + {'bundle_marker_observed': True, 'bundle_marker_count': 2, 'prebundle_marker_count': 1}) + self.assertNotIn('private unrelated', native.EVIDENCE.read_text()) + self.assertNotIn('PRIVATE_SENTINEL', native.EVIDENCE.read_text()) + self.assertNotIn('private-path', native.EVIDENCE.read_text()) + + def test_warmup_keeps_each_platform_body_private_and_does_not_supply_smoke_evidence(self): + native.PRIVATE.mkdir() + body = b'disposable development bundle PRIVATE_SENTINEL' + + def warmup(args, *unused, **kwargs): + self.assertEqual(args[-1], 'http://127.0.0.1:8081/index.bundle?platform=%s&dev=true&minify=false' + % platform.lower()) + Path(args[args.index('--output') + 1]).write_bytes(body) + return '200' + + for platform in ['IOS', 'ANDROID']: + with self.subTest(platform=platform), mock.patch.object(native, 'PLATFORM', platform), \ + mock.patch.object(native, 'command', side_effect=warmup): + native.warm_metro_bundle() + data = native.read() + self.assertEqual(data['smoke'], 'NOT RUN') + self.assertEqual(data['metro_warmup']['platform'], platform.lower()) + self.assertEqual(data['metro_warmup']['result'], 'PASS') + self.assertEqual(data['metro_warmup']['http_status'], 200) + self.assertEqual(data['metro_warmup']['bytes'], len(body)) + self.assertEqual(data['metro_warmup']['sha256'], hashlib.sha256(body).hexdigest()) + self.assertNotIn('PRIVATE_SENTINEL', native.EVIDENCE.read_text()) + with mock.patch.object(native, 'command', side_effect=RuntimeError('HTTP error')), \ + self.assertRaisesRegex(RuntimeError, 'HTTP error'): + native.warm_metro_bundle() + self.assertEqual(native.read()['metro_warmup']['result'], 'FAIL') + self.assertEqual(native.read()['smoke'], 'NOT RUN') + with mock.patch.object(native, 'PLATFORM', 'unsupported&private=value'), \ + mock.patch.object(native, 'command') as command, \ + self.assertRaisesRegex(ValueError, 'Unsupported native bundle platform'): + native.warm_metro_bundle() + command.assert_not_called() + + def test_ios_collector_rejects_unsafe_executable_without_running_command(self): + native.PRIVATE.mkdir() + native.save({'commands': [], 'smoke': 'FAIL', 'built_artifact': {'executable': '../unsafe"'}}) + (native.PRIVATE / 'metro.log').write_text('') + with mock.patch.object(native, 'PLATFORM', 'IOS'), mock.patch.object(subprocess, 'run') as run: + native.record_startup_failure(native.PRIVATE / 'missing-report.xml', 'owned-simulator') + run.assert_not_called() + self.assertEqual(native.read()['smoke'], 'FAIL') + self.assertIn({'stage': 'ios_errors', 'error_type': 'ValueError'}, + native.read()['startup_diagnostics']['collection_errors']) + + def test_diagnostic_failure_never_replaces_original_smoke_failure(self): + native.PRIVATE.mkdir() + identity = {'sha256': 'test artifact'} + native.save({'commands': [], 'build': 'PASS', 'smoke': 'NOT RUN', 'built_artifact': identity}) + metro = mock.Mock() + metro.poll.return_value = 1 + with mock.patch.object(native, 'artifact_identity', return_value=identity), \ + mock.patch.object(subprocess, 'Popen', return_value=metro), \ + mock.patch.object(native, 'record_startup_failure', side_effect=RuntimeError('collector failed')), \ + self.assertRaisesRegex(RuntimeError, 'Metro exited before the disposable smoke'): + native.run_phase('smoke') + self.assertEqual(native.read()['smoke'], 'FAIL') + self.assertEqual(native.read()['startup_diagnostics']['collection_errors'], + [{'stage': 'collector', 'error_type': 'RuntimeError'}]) + + def test_missing_executable_is_recorded(self): + with self.assertRaisesRegex(RuntimeError, 'exit 127'): + native.command([str(self.root / 'missing-command')], 'Missing') + self.assertEqual(native.read()['commands'][0]['exit_code'], 127) + + def test_record_rejects_wrong_source_sha_before_collecting_evidence(self): + before = native.EVIDENCE.read_bytes() + with mock.patch.dict(native.os.environ, {'KEEPER_SOURCE_SHA': 'b' * 40}), \ + mock.patch.object(subprocess, 'check_output', return_value='a' * 40 + '\n'), \ + self.assertRaisesRegex(RuntimeError, 'exact requested PR head'): + native.record() + self.assertEqual(native.EVIDENCE.read_bytes(), before) + + def test_android_fixtures_use_only_dummy_project_and_loopback_services(self): + (self.root / 'android/app').mkdir(parents=True) + native.fixtures() + environment = (self.root / '.env.ci').read_text() + self.assertIn('ENVIRONMENT=DEVELOPMENT', environment) + for line in environment.splitlines(): + if 'https://' in line: + self.assertIn('127.0.0.1:9', line) + firebase = json.loads((self.root / 'android/app/google-services.json').read_text()) + self.assertEqual(firebase['project_info']['project_id'], 'keeper-disposable-ci') + settings = json.loads((self.root / 'firebase.json').read_text())['react-native'] + self.assertFalse(settings['messaging_auto_init_enabled']) + self.assertFalse(settings['app_data_collection_default_enabled']) + for overlay in native.read()['configuration_overlays']: + self.assertEqual(overlay['sha256'], hashlib.sha256((self.root / overlay['path']).read_bytes()).hexdigest()) + self.assertEqual(len(native.read()['configuration_overlays']), 3) + + def test_ios_fixtures_cover_both_tracked_firebase_files(self): + (self.root / 'ios/GoogleServices/dev').mkdir(parents=True) + with mock.patch.object(native, 'PLATFORM', 'IOS'): + native.fixtures() + paths = {overlay['path'] for overlay in native.read()['configuration_overlays']} + self.assertEqual(paths, {'.env.ci', 'firebase.json', 'ios/GoogleService-Info.plist', + 'ios/GoogleServices/dev/GoogleService-Info.plist'}) + + def test_emulator_revision_comes_from_sdk_metadata_without_executing_binary(self): + emulator = self.root / 'emulator' + emulator.mkdir() + (emulator / 'source.properties').write_text('Pkg.Revision = 36.6.11\nUnrelated = ignored\n') + with mock.patch.object(subprocess, 'Popen') as popen: + self.assertEqual(native.android_sdk_metadata(self.root, 'emulator'), {'Pkg.Revision': '36.6.11'}) + popen.assert_not_called() + + def test_emulator_metadata_must_exist_and_contain_revision(self): + with self.assertRaises(FileNotFoundError): + native.android_sdk_metadata(self.root, 'emulator') + emulator = self.root / 'emulator' + emulator.mkdir() + for text in ['Pkg.Desc = Android Emulator\n', 'Pkg.Revision = \n']: + with self.subTest(properties=text): + (emulator / 'source.properties').write_text(text) + with self.assertRaisesRegex(RuntimeError, 'no Pkg.Revision metadata'): + native.android_sdk_metadata(self.root, 'emulator') + + def test_smoke_requires_successful_build(self): + with mock.patch.object(subprocess, 'Popen') as popen, \ + self.assertRaisesRegex(RuntimeError, 'successful native build'): + native.smoke() + popen.assert_not_called() + + def test_ios_build_rejects_unsupported_architecture_before_invoking_xcode(self): + with mock.patch.object(native, 'PLATFORM', 'IOS'), \ + mock.patch.object(native.platform, 'machine', return_value='riscv64'), \ + mock.patch.object(native, 'command') as command, \ + self.assertRaisesRegex(RuntimeError, 'Unsupported hosted iOS simulator architecture'): + native.build() + command.assert_not_called() + + def test_ios_build_uses_only_the_supported_host_simulator_architecture(self): + for architecture in ['arm64', 'x86_64']: + with self.subTest(architecture=architecture), \ + mock.patch.object(native, 'PLATFORM', 'IOS'), \ + mock.patch.object(native.platform, 'machine', return_value=architecture), \ + mock.patch.object(native, 'artifact_identity', return_value={'sha256': 'test artifact'}), \ + mock.patch.object(native, 'command') as command: + native.build() + arguments = command.call_args.args[0] + self.assertIn('ARCHS=' + architecture, arguments) + self.assertIn('ONLY_ACTIVE_ARCH=YES', arguments) + self.assertIn('CODE_SIGNING_ALLOWED=NO', arguments) + self.assertEqual(command.call_args.kwargs['timeout'], 2400) + self.assertEqual(native.read()['toolchain']['ios_simulator_architecture'], architecture) + + def test_artifact_hash_detects_changed_apk_before_smoke(self): + apk = self.root / 'android/app/build/outputs/apk/development/debug/app-development-debug.apk' + apk.parent.mkdir(parents=True) + apk.write_bytes(b'first disposable artifact') + identity = native.artifact_identity() + self.assertEqual(identity['sha256'], hashlib.sha256(apk.read_bytes()).hexdigest()) + native.save({'commands': [], 'build': 'PASS', 'smoke': 'NOT RUN', 'built_artifact': identity}) + apk.write_bytes(b'replacement artifact') + with mock.patch.object(subprocess, 'Popen') as popen, \ + self.assertRaisesRegex(RuntimeError, 'artifact changed'): + native.smoke() + popen.assert_not_called() + + def test_failed_build_and_smoke_are_never_recorded_as_pass(self): + for phase in ['build', 'smoke']: + with self.subTest(phase=phase), \ + mock.patch.object(native, phase, side_effect=RuntimeError('failure')), \ + self.assertRaisesRegex(RuntimeError, 'failure'): + native.run_phase(phase) + self.assertEqual(native.read()[phase], 'FAIL') + self.assertEqual(native.read()['failed_phase'], phase) + + +if __name__ == '__main__': + unittest.main() diff --git a/.github/workflows/keeper-hosted-native.yml b/.github/workflows/keeper-hosted-native.yml new file mode 100644 index 0000000000..3d198b099c --- /dev/null +++ b/.github/workflows/keeper-hosted-native.yml @@ -0,0 +1,150 @@ +name: Keeper hosted native validation + +on: + pull_request: + branches: [sprint] + types: [opened, synchronize, reopened, ready_for_review, converted_to_draft] + +# PR code runs only in disposable, unprivileged jobs. No signing or service secrets. +permissions: + contents: read + +concurrency: + group: keeper-hosted-native-${{ github.event.pull_request.number }} + cancel-in-progress: true + +env: + CI: 'true' + HUSKY: '0' + KEEPER_SOURCE_SHA: ${{ github.event.pull_request.head.sha }} + MAESTRO_CLI_NO_ANALYTICS: '1' + +jobs: + ios: + name: Hosted iOS simulator + # Skip before allocating a runner; missing/non-boolean draft state fails closed. + if: github.event_name == 'pull_request' && toJSON(github.event.pull_request.draft) == 'false' + runs-on: macos-15 + timeout-minutes: 65 + env: + KEEPER_NATIVE_PLATFORM: IOS + DEVELOPER_DIR: /Applications/Xcode_16.4.app/Contents/Developer + steps: + - name: Check out exact app source + uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6 + with: + ref: ${{ github.event.pull_request.head.sha }} + persist-credentials: false + - name: Set up Node + uses: actions/setup-node@249970729cb0ef3589644e2896645e5dc5ba9c38 # v6 + with: + node-version: '22.22.0' + cache: yarn + - name: Set up Java for Maestro + uses: actions/setup-java@b6effb05e454b25005698d916606bdc6ffcbf961 # v5 + with: + distribution: temurin + java-version: '17' + - name: Set up Ruby + uses: ruby/setup-ruby@675dd7ba1b06c8786a1480d89c384f5620a42647 # v1.281.0 + with: + ruby-version: '3.3.8' + - name: Record runner and toolchain + run: python3 .github/native/hosted_native.py record + - name: Install immutable native dependencies + run: python3 .github/native/hosted_native.py prepare + - name: Check disposable iOS driver readiness + run: | + test "$(git rev-parse HEAD)" = '${{ github.event.pull_request.head.sha }}' + git diff --exit-code HEAD -- .github/native .github/workflows/keeper-hosted-native.yml + python3 .github/native/hosted_native.py driver + - name: Build development app + run: | + test "$(git rev-parse HEAD)" = '${{ github.event.pull_request.head.sha }}' + git diff --exit-code HEAD -- .github/native .github/workflows/keeper-hosted-native.yml + python3 .github/native/hosted_native.py build + - name: Run disposable startup smoke + run: | + test "$(git rev-parse HEAD)" = '${{ github.event.pull_request.head.sha }}' + git diff --exit-code HEAD -- .github/native .github/workflows/keeper-hosted-native.yml + python3 .github/native/hosted_native.py smoke + - name: Retain safe native evidence + if: always() + uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6 + with: + name: keeper-native-ios-${{ github.event.pull_request.head.sha }} + path: | + artifacts/native/ios/metadata.json + artifacts/native/ios/Podfile.lock.candidate + if-no-files-found: warn + retention-days: 7 + + android: + name: Hosted Android emulator + if: github.event_name == 'pull_request' && toJSON(github.event.pull_request.draft) == 'false' + runs-on: ubuntu-24.04 + timeout-minutes: 60 + env: + KEEPER_NATIVE_PLATFORM: ANDROID + steps: + - name: Check out exact app source + uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6 + with: + ref: ${{ github.event.pull_request.head.sha }} + persist-credentials: false + - name: Set up Node + uses: actions/setup-node@249970729cb0ef3589644e2896645e5dc5ba9c38 # v6 + with: + node-version: '22.22.0' + cache: yarn + - name: Set up Java + uses: actions/setup-java@b6effb05e454b25005698d916606bdc6ffcbf961 # v5 + with: + distribution: temurin + java-version: '17' + cache: gradle + - name: Set up Android command-line tools + uses: android-actions/setup-android@be39fa834029ff78f1a44aa3bb0819b8fc2bd8fd # v4 + with: + cmdline-tools-version: '13114758' + packages: platform-tools + log-accepted-android-sdk-licenses: false + - name: Record runner and toolchain + run: python3 .github/native/hosted_native.py record + - name: Install immutable native dependencies + run: python3 .github/native/hosted_native.py prepare + - name: Build development app + run: | + test "$(git rev-parse HEAD)" = '${{ github.event.pull_request.head.sha }}' + git diff --exit-code HEAD -- .github/native .github/workflows/keeper-hosted-native.yml + python3 .github/native/hosted_native.py build + - name: Enable emulator acceleration + run: | + echo 'KERNEL=="kvm", GROUP="kvm", MODE="0666", OPTIONS+="static_node=kvm"' | sudo tee /etc/udev/rules.d/99-keeper-kvm.rules + sudo udevadm control --reload-rules + sudo udevadm trigger --name-match=kvm + - name: Run disposable startup smoke + uses: reactivecircus/android-emulator-runner@1dcd0090116d15e7c562f8db72807de5e036a4ed # v2.34.0 (peeled commit) + with: + api-level: 35 + target: google_apis + arch: x86_64 + profile: pixel_6 + cores: 2 + ram-size: 3072M + force-avd-creation: true + disable-animations: true + emulator-boot-timeout: 240 + emulator-options: -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim -no-snapshot -camera-back none -camera-front none + script: | + test "$(git rev-parse HEAD)" = '${{ github.event.pull_request.head.sha }}' + git diff --exit-code HEAD -- .github/native .github/workflows/keeper-hosted-native.yml + python3 .github/native/hosted_native.py smoke + - name: Retain safe native evidence + if: always() + uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6 + with: + name: keeper-native-android-${{ github.event.pull_request.head.sha }} + path: artifacts/native/android/metadata.json + if-no-files-found: warn + retention-days: 7 diff --git a/.github/workflows/keeper-local-validation.yml b/.github/workflows/keeper-local-validation.yml index d66fc70f88..af64b8c8a0 100644 --- a/.github/workflows/keeper-local-validation.yml +++ b/.github/workflows/keeper-local-validation.yml @@ -7,21 +7,25 @@ on: types: [opened, reopened, synchronize, edited, ready_for_review] issue_comment: types: [created, edited, deleted] + workflow_run: + workflows: [Keeper hosted native validation] + types: [completed, requested, in_progress] permissions: contents: read issues: read pull-requests: read + actions: read statuses: write concurrency: - group: keeper-local-validation-${{ github.event.pull_request.number || github.event.issue.number }} + group: keeper-local-validation-${{ github.event.pull_request.number || github.event.issue.number || github.event.workflow_run.pull_requests[0].number || github.run_id }} cancel-in-progress: false jobs: keeper-local-validation: name: Keeper local validation gate - if: github.event_name == 'pull_request_target' || github.event.issue.pull_request != null + if: github.event_name == 'pull_request_target' || github.event.issue.pull_request != null || github.event.workflow_run.event == 'pull_request' runs-on: ubuntu-latest steps: - name: Verify local validation for exact PR commit @@ -30,7 +34,9 @@ jobs: script: | const owner = context.repo.owner; const repo = context.repo.repo; - const pull_number = context.payload.pull_request?.number ?? context.issue.number; + const pull_number = context.payload.pull_request?.number ?? + context.payload.workflow_run?.pull_requests?.[0]?.number ?? context.issue.number; + if (!pull_number) return; const { data: pr } = await github.rest.pulls.get({ owner, repo, pull_number }); if (pr.base.ref !== 'sprint') { core.info('Only PRs targeting sprint use this validation gate.'); @@ -87,6 +93,75 @@ jobs: return allowed; }; + // PR workflows are editable by the PR author. A successful check name + // alone is NOT evidence. All executable CI files must match both the + // protected current base and the base used by that particular run. + // This intentionally rejects this infrastructure PR's bootstrap runs. + const hostedPasses = async () => { + const passed = new Set(); + try { + const workflowPath = '.github/workflows/keeper-hosted-native.yml'; + const repository = owner + '/' + repo; + if (pr.head.repo?.full_name !== repository) return passed; + const runs = await github.paginate(github.rest.actions.listWorkflowRuns, { + owner, repo, workflow_id: 'keeper-hosted-native.yml', + head_sha: sha, event: 'pull_request', per_page: 100 + }); + // Do not fall back to an older success after a failed/cancelled + // rerun or while newer evidence is pending. + const run = runs.sort((a, b) => b.id - a.id)[0]; + if (!run || run.head_sha !== sha || run.event !== 'pull_request' || + run.path !== workflowPath || run.head_branch !== pr.head.ref || + run.head_repository?.full_name !== repository || + run.status !== 'completed' || !['success', 'failure'].includes(run.conclusion)) { + return passed; + } + const linked = run.pull_requests?.find(p => p.number === pull_number); + if (!linked || linked.head?.sha !== sha || linked.head?.ref !== pr.head.ref || + linked.base?.ref !== 'sprint' || !linked.base?.sha || + linked.head?.repo?.id !== pr.head.repo.id || + linked.base?.repo?.id !== pr.base.repo?.id || + !pr.head.repo.id || !pr.base.repo?.id || + !Number.isSafeInteger(run.run_attempt) || run.run_attempt < 1) return passed; + const fingerprint = async ref => { + const { data } = await github.rest.git.getTree({ owner, repo, tree_sha: ref, recursive: '1' }); + if (data.truncated) throw new Error('Truncated trusted CI tree'); + const entries = data.tree.filter(e => e.path === workflowPath || + e.path.startsWith('.github/native/')); + if (!entries.some(e => e.path === workflowPath) || + !entries.some(e => e.path.startsWith('.github/native/') && e.type === 'blob')) { + throw new Error('Hosted native infrastructure not present in trusted base'); + } + return JSON.stringify(entries.map(e => [e.path, e.mode, e.type, e.sha]) + .sort((a, b) => a[0].localeCompare(b[0]))); + }; + const [headFiles, baseFiles, runBaseFiles] = await Promise.all([ + fingerprint(sha), fingerprint(pr.base.sha), fingerprint(linked.base.sha) + ]); + if (headFiles !== baseFiles || headFiles !== runBaseFiles) return passed; + const jobs = await github.paginate(github.rest.actions.listJobsForWorkflowRunAttempt, { + owner, repo, run_id: run.id, attempt_number: run.run_attempt, per_page: 100 + }); + for (const [platform, name] of [['IOS', 'Hosted iOS simulator'], + ['ANDROID', 'Hosted Android emulator']]) { + const matches = jobs.filter(j => j.name === name); + if (matches.length !== 1) continue; + const job = matches[0]; + if (job.head_sha !== sha || job.run_id !== run.id || job.run_attempt !== run.run_attempt || + job.status !== 'completed' || job.conclusion !== 'success') continue; + const steps = ['Build development app', 'Run disposable startup smoke']; + if (steps.every(name => job.steps?.filter(s => s.name === name && + s.status === 'completed' && s.conclusion === 'success').length === 1)) { + passed.add(platform); + core.info(platform + ' hosted evidence: ' + run.html_url + ', attempt ' + run.run_attempt); + } + } + } catch (err) { + core.warning('Hosted evidence unavailable; local fallback retained: ' + err.message); + } + return passed; + }; + let state = 'failure'; let description = 'Awaiting local Mac validation'; if (docsOnly) { @@ -129,6 +204,7 @@ jobs: if (malformed) { description = 'Specify Required local checks: IOS, ANDROID, HARDWARE'; } else { + const hosted = await hostedPasses(); // Only the release owner can defer unrun physical checks. Keep // every original handoff intact, and bind the newest owner // decision to this PR and this exact head commit. @@ -165,9 +241,7 @@ jobs: break; } } - if (!report) { - description = 'Awaiting trusted Mac validation of current SHA'; - } else if (field(report, 'State') !== 'LOCAL_PASSED') { + if (report && field(report, 'State') !== 'LOCAL_PASSED') { description = 'Latest local validation is not LOCAL_PASSED'; } else { const reportFields = { @@ -179,10 +253,12 @@ jobs: const hardwareDeferred = required.has('HARDWARE') && hardwareDeferral && reportIndex > ownerDecisionIndex && /^(not run|deferred)\b/i.test(hardwareResult); - const notPassed = [...required].filter(x => - !/^pass\b/i.test(field(report, reportFields[x])) && - !(x === 'HARDWARE' && hardwareDeferred) - ); + const notPassed = [...required].filter(x => { + const result = field(report, reportFields[x]); + if (/^fail(?:ed)?\b/i.test(result)) return true; + return !/^pass\b/i.test(result) && !hosted.has(x) && + !(x === 'HARDWARE' && hardwareDeferred); + }); if (hardwareDeferred && !required.has('IOS') && !required.has('ANDROID')) { description = 'IOS or ANDROID required before HARDWARE deferral'; } else if (notPassed.length) { @@ -190,9 +266,10 @@ jobs: } else { state = 'success'; const passed = [...required].filter(x => - /^pass\b/i.test(field(report, reportFields[x])) + /^pass\b/i.test(field(report, reportFields[x])) || hosted.has(x) ); description = 'Validated at current SHA: ' + passed.join(', ') + + (hosted.size ? '; hosted ' + [...hosted].join(', ') : '') + (hardwareDeferred ? '; owner-deferred HARDWARE' : ''); } } @@ -200,6 +277,13 @@ jobs: } } + // API reads above can take time. Never post a success for a head that + // changed while evidence was being collected. + const { data: current } = await github.rest.pulls.get({ owner, repo, pull_number }); + if (current.head.sha !== sha) { + core.setFailed('PR head changed during validation; awaiting new-head evaluation'); + return; + } await github.rest.repos.createCommitStatus({ owner, repo, sha, state, context: contextName, diff --git a/.github/workflows/keeper-validation-tests.yml b/.github/workflows/keeper-validation-tests.yml new file mode 100644 index 0000000000..7bbc3b67da --- /dev/null +++ b/.github/workflows/keeper-validation-tests.yml @@ -0,0 +1,26 @@ +name: Native validation contracts + +on: + pull_request: + branches: [sprint] + push: + branches: [sprint] + paths: ['.github/**'] + +permissions: + contents: read + +jobs: + contracts: + name: Native validation contracts + runs-on: ubuntu-24.04 + timeout-minutes: 5 + steps: + - uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6 + with: + persist-credentials: false + - uses: actions/setup-node@249970729cb0ef3589644e2896645e5dc5ba9c38 # v6 + with: + node-version: '22' + - run: node --test .github/tests/*.test.cjs + - run: python3 -B -m unittest discover -s .github/tests -p 'test_*.py' diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index cf7f29ae26..eb40cb71af 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -8,6 +8,7 @@ on: - main - release/2.6.3 - release/2.6.4 + types: [opened, synchronize, reopened, ready_for_review] pull_request: branches: - sprint @@ -16,6 +17,7 @@ on: - main - release/2.6.3 - release/2.6.4 + types: [opened, synchronize, reopened, ready_for_review] permissions: contents: read diff --git a/AGENTS.md b/AGENTS.md index db639d5817..871210bf4b 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -1,6 +1,6 @@ # Bitcoin Keeper: Codex workflow -This repository uses a **ChatGPT → Codex Cloud → Codex Local (Mac)** workflow for coding tasks. The GitHub pull request is the durable handoff between stages. Do **not** rely on one agent having access to another agent's conversation, unpushed changes, machine, or installed tools. +The prepared workflow is **Cloud code complete → hosted native validation → authorized physical checks → release review**. Until the hosted infrastructure is reviewed, installed and verified, retain the existing Local path below as the safe fallback; do not assume the owner's Mac is available. Signing remains a separate approved action. Follow the [bootstrap adoption order](docs/cloud-bootstrap-preparation.md), including the later restacking of Phase A #7062. The GitHub pull request is the durable handoff between stages. Do **not** rely on one agent having access to another agent's conversation, unpushed changes, machine, or installed tools. Repository: `KeeperCommunity/bitcoin-keeper`. Use `sprint` as the PR base unless the task explicitly specifies another base. @@ -26,7 +26,7 @@ Repository: `KeeperCommunity/bitcoin-keeper`. Use `sprint` as the PR base unless ``` 4. For app/code changes, `Required local checks` must list one or more of `IOS`, `ANDROID`, `HARDWARE` (comma-separated). Use both iOS and Android for cross-platform changes; add hardware for signer/device interactions. If no Mac tests are necessary, explain why, but the GitHub gate exempts only documentation and `.github/` changes. The handoff must name the actual PR head commit SHA after the final cloud push. -5. Return the PR URL/number and state `NEEDS_LOCAL` to the user so ChatGPT can generate the exact local handoff prompt. Continue unrelated cloud tasks while waiting for a Mac. +5. Return the PR URL/number and actual platform `PASS`/`FAIL`/`NOT RUN` results for the current head. Use `NEEDS_LOCAL` for remaining native or physical requirements; successful trusted hosted evidence may satisfy `IOS`/`ANDROID` without a Mac login. Continue unrelated cloud tasks while waiting for a Mac. Follow [hosted native validation](docs/hosted-native-validation.md) for trust, bootstrap, scope and artifact limitations. 6. If a task needs only code review, explicitly state why local testing is not required rather than silently skipping it. ## Codex Local (Mac): validate the PR @@ -84,6 +84,6 @@ A known critical defect is not eligible for post-release deferral. In particular ## GitHub merge gate -The `.github/workflows/keeper-local-validation.yml` workflow posts commit status `keeper/local-validation` for the current PR head SHA. For code changes, it remains failing until a repository collaborator posts a `Codex local validation` comment with `State: LOCAL_PASSED`, the **exact current commit SHA**, and `pass` for every platform named by `Required local checks`, except `HARDWARE` when the exact-SHA owner decision above defers unrun physical checks. New commits invalidate previous local sign-off and deferral. Documentation and `.github/`-only PRs are exempt from Mac tests. +The `.github/workflows/keeper-local-validation.yml` workflow posts commit status `keeper/local-validation` for the current PR head SHA. For code changes, it requires a trusted exact-head Cloud handoff. Each required `IOS`/`ANDROID` platform needs either a genuine successful hosted native build **and** disposable UI smoke from `.github/workflows/keeper-hosted-native.yml`, or a repository collaborator's exact-head `LOCAL_PASSED` report. A hosted run is trusted only when its workflow and complete `.github/native/` harness match the protected current base and the run's original base; PR-authored check names, comments and artifacts alone are not CI evidence. New or changed CI infrastructure must first be reviewed and merged before it can provide authoritative native evidence. The latest run/attempt must contain successful build and smoke steps for that platform; stale, skipped, cancelled, pending, build-only or untrusted evidence supplies no hosted pass. A latest explicit local failure remains blocking. `HARDWARE` still requires the existing local physical result or exact-SHA owner decision and subsequent honest local report above. New commits invalidate previous native evidence, local sign-off and deferral. Documentation and `.github/`-only PRs are exempt from Mac tests. This workflow **reports** a status; to **enforce** it, repository administrators must require the `keeper/local-validation` commit status for merges to `sprint` in GitHub Rulesets or branch protection. Require the existing `Tests` CI check as well. This is a review/test gate, not a guarantee that tests were actually executed; agents must report actual results truthfully. Release signing, TestFlight, Play Store, and production deployment remain separate and require explicit user approval. diff --git a/docs/cloud-bootstrap-preparation.md b/docs/cloud-bootstrap-preparation.md new file mode 100644 index 0000000000..70a5b4559a --- /dev/null +++ b/docs/cloud-bootstrap-preparation.md @@ -0,0 +1,175 @@ +# Cloud bootstrap preparation + +This is an unpublished infrastructure-only preparation from known committed +sources. It carries no native pass, app acceptance, signing or release approval. +No unpublished files from the original task or the owner's Mac were migrated or +backed up. The original task's working-tree state remains unknown. + +## Source identities + +Fresh branch/PR refs matched these checkpoints on 18 October 2026: + +| Source | Commit | +| --- | --- | +| sprint / bootstrap base | `def0d6a90c80287edb4f58ae272cbfc340f00d73` | +| #7060 `codex/7059-hosted-native-validation` | `d865ca9c4f91532a1524443cc6da42d8d7b07e1a` | +| #7062 `codex/7061-cloud-release-phase-a` | `59c821ce195a5c063d99045fd97b5ac605433f62` | +| R5 #7058 | `625435ac26af0304670c993a639626b1117699ba` | +| R4 #7045, excluded | `be89c34473bb6aab253b98325f1fe746ca391c6f` | + +#7060 descends from the verified sprint commit; #7062 is stacked on #7060. +The preparation reuses #7060's `.github/` infrastructure and relevant Markdown, +with diagnostic privacy corrections, action-pin regressions and these adoption +notes. Its gate implementation is unchanged from #7060. Original gate contract +cases are retained, with additional skipped-draft/latest-attempt regressions. +Provenance, latest-attempt, explicit-failure and hardware requirements +remain intact. No live protection, permission or credential setting changes. + +`package.json`, `yarn.lock`, `ios/Podfile.lock` and +`ios/hexa_keeper/AppDelegate.mm` remain at sprint. R4's assessment document, +source, refs, private fixtures and Mac work are not imported or changed. Phase A +app probes/scenario matrices and Phase B signing/distribution are excluded. + +## Diagnostic and action-pin correction + +General tool failures and Maestro, Android, Metro and iOS startup collectors +export fixed categories or fixed public startup labels. They never return +arbitrary log excerpts, including unknown errors. The legacy metadata field +`redacted_tool_errors` now contains category identifiers only. Synthetic privacy +regressions exercise unlabelled private text, wallet-like data, spaced secrets, +URLs/paths, encoded text, Unicode/control characters, multiline input and repeated +messages. Collector tests check metadata and console output while retaining +failed command and smoke outcomes. Raw logs remain private to the disposable +runner. Categories indicate observed markers, not a diagnosis or a platform pass. + +The emulator action uses upstream commit +`1dcd0090116d15e7c562f8db72807de5e036a4ed`, matching #7062. Read-only verification +of `ReactiveCircus/android-emulator-runner` on 18 October 2026 returned: + +```text +refs/tags/v2.34.0 324029e2f414c084d8b15ba075288885e74aef9c +refs/tags/v2.34.0^{} 1dcd0090116d15e7c562f8db72807de5e036a4ed +``` + +The old #7060 value appended `38` to the tag object's SHA and was malformed. +Host contracts require full 40-character immutable action pins in the two newly +introduced workflows, matching the upstream revisions verified on the same date, +including this exact emulator commit. They reject mutable tags, short/malformed +revisions, unverified actions/SHAs and the annotated tag object. Existing action policy +in unrelated workflows and the inherited gate is unchanged. + +Pin inspection parses YAML with the host's existing Python 3 and PyYAML, then +checks every job-level and step-level `uses` value. Quoted/escaped keys, flow +mappings, folded scalars and aliases cannot hide mutable actions alongside valid +pins. Missing parser tooling, invalid YAML, duplicate mapping keys and non-string +action references fail the contract; there is no regex fallback or dependency +installation. Future host-contract environments must provide Python 3 and PyYAML. + +## Draft publication behavior + +Both hosted native jobs require a `pull_request` event with a literal boolean +`draft: false` before allocating a runner. Drafts and missing/malformed draft +state cannot start either native job. `ready_for_review` starts a new eligible +native run; marking a PR ready therefore requires separate native-run/budget +authorization. `converted_to_draft` triggers a skipped run in the same PR +concurrency group, configured to cancel an older native run. Cancellation is +best effort and cannot undo work already started while the PR was ready. + +The existing CI `Tests` install would otherwise invoke `prepare` and CocoaPods. +For draft PRs only, it now uses frozen Yarn with lifecycle scripts disabled. +The normal `Tests` job still runs and any install/Jest failure still fails it. +No app manifest, lock or test changes are included. Ready/non-PR installs retain +their existing behavior, and `ready_for_review` reruns CI so a draft installation +result cannot replace the normal ready-PR test run. Lint's existing install +already disables lifecycle scripts. The frozen dependency blocker is not waived. + +Automatic checks for this branch targeting sprint, from the checked-in triggers: + +| Event | Automatic behavior | +| --- | --- | +| Push `codex/cloud-bootstrap-privacy` with no PR | No checked-in workflow matches this branch's push event | +| Open/reopen a draft; push new commits to an open draft (`synchronize`) | Linux CodeQL; CI Lint and Tests; Node/Python Native validation contracts; existing trusted local-validation gate. Hosted iOS/Android jobs are skipped before runner allocation | +| Edit the draft PR description | Existing trusted local-validation gate refreshes; no native workflow trigger | +| Mark ready for review | Native jobs become eligible; CI reruns the normal install/tests; trusted gate refreshes. CodeQL and host contracts retain their existing default PR event types | +| Convert back to draft | Hosted-native concurrency cancellation/skip only; no new native runner. Previously started ready-PR CI is not retroactively undone | + +The trusted gate's existing docs/`.github` exemption can report success for this +infrastructure PR. That is not native acceptance or installation/merge approval. +For app PRs, skipped jobs/steps, even inside an overall successful workflow, supply +no native pass; a newer skipped run/attempt cannot reuse an older native success. +All provenance, explicit-failure and HARDWARE checks remain unchanged. + +This bootstrap contains no `workflow_call`, `workflow_dispatch` or job-level +reusable-workflow call. The entry-path contracts check that boundary. #7062's +`keeper-release-acceptance.yml` callers and `keeper-native-platform.yml` worker +remain outside this patch. Before their separate restack/adoption, add the same +draft guard at both caller jobs and the worker, retaining source/provenance/R4 +checks. The reusable worker receives the [caller's GitHub event context](https://docs.github.com/en/actions/reference/workflows-and-actions/reusing-workflow-configurations#github-context); a direct +non-PR caller must not bypass the guard. These future paths are not claimed fixed +or installed by this bootstrap. + +The remaining Linux checks consume hosted runner time. Available included minutes +and billing limits have not been verified, so this is not a zero-cost guarantee. +Confirm allowance before publishing under a no-paid-resources restriction; do not +disable required checks or change billing/security settings to bypass it. No +workflow has been dispatched or run as part of this source-only preparation. + +## Adoption order and remaining gates + +1. Independently review this infrastructure diff and its host contracts. Any + publication, PR change or merge requires separate owner authorization. Only a + reviewed installation on protected sprint can establish the workflow/harness + baseline. Candidate runs cannot authorize their own changed infrastructure. +2. Reconcile the app and dependency changes separately against the approved + source baseline. This bootstrap deliberately leaves frozen-install blockers + unresolved. Do not unlock dependency installation, weaken gates or treat host + contracts as native evidence. R4 work and the owner's Mac remain untouched. +3. Restack #7062's Phase A delta onto the adopted bootstrap in separately + authorized work. Do not merge its existing #7060 ancestry wholesale: that + would also import the four excluded app/dependency files. Reconcile + `hosted_native.py` changes while preserving the fixed-category exports and + privacy regressions; retain the corrected emulator pin and extend pin/draft + entry-path contracts and guards to Phase A's callers and worker. Update #7062's bootstrap references to + the actual adopted source. Phase A requires its own review and protected-base + installation before it can supply authoritative evidence. +4. After the source baseline and frozen dependencies are reconciled, obtain + authorization and budget for actual exact-source native runs using unchanged + reviewed infrastructure. Record source SHA, workflow/run/attempt, each real + build/UI outcome and safe artifact identities. New source commits invalidate + old passes. Missing, skipped, cancelled, untrusted or failed evidence does not + become a pass; explicit local failures and HARDWARE gates still apply. R5's + feature acceptance remains separate from this startup smoke. + +Signing, distribution, uploads, deployment and publication remain separately +approved operations. Phase B is not implemented or verified by this preparation. +The optional private signing follow-up note is not a Phase A prerequisite. + +## Validation scope + +Use `node --test .github/tests/*.test.cjs` and +`python3 -B -m unittest discover -s .github/tests -p 'test_*.py'`, plus available +workflow lint, syntax and diff checks. The host suite uses synthetic fixtures and +mocked native commands; it requires no app dependency installation, emulator, +native compilation or Actions dispatch. Report actual results in the private +handoff. A missing lint tool is a validation-tool blocker, distinct from a failed +host contract or the setup-reported frozen app dependency installation failure. + +Private preparation results on 18 October 2026: + +| Check | Result | +| --- | --- | +| Node host contracts | PASS: 53 tests, including gate, shim, action-pin and draft-entry contracts | +| Python host contracts | PASS: 35 tests, including 5 adversarial privacy tests with subcases | +| JavaScript and Python syntax | PASS | +| Changed YAML syntax | PASS: installed PyYAML, 5 files; not workflow semantic lint | +| Staged diff whitespace and scope | PASS: only `.github/` and relevant Markdown; excluded app files unchanged | +| Workflow semantic lint (`actionlint`) | NOT RUN: executable unavailable; nothing installed | +| App dependency installation / app tests | NOT RUN here; setup-reported frozen-install failure remains unresolved | +| Native builds, simulators/emulators, hardware | NOT RUN | +| Actions dispatch, signing/distribution, deployment | NOT RUN | + +The privacy regression suite was also applied to the original #7060 harness as a +negative control: all 5 tests failed (168 assertion/subcase failures, no execution +errors), confirming the tests detect the original exports. Those are intentional +baseline failures, not failures of this prepared diff. No live native evidence or +CI run from another SHA is credited to this preparation. diff --git a/docs/hosted-native-validation.md b/docs/hosted-native-validation.md new file mode 100644 index 0000000000..0191244736 --- /dev/null +++ b/docs/hosted-native-validation.md @@ -0,0 +1,198 @@ +# Hosted native validation + +This infrastructure-only bootstrap reuses issue #7059 / PR #7060's bounded hosted +development builds and disposable first-use UI smoke. It excludes that PR's app +and dependency changes. See [bootstrap preparation and adoption order](cloud-bootstrap-preparation.md) +for exact committed sources, exclusions and the relationship to Phase A #7062. +It does not change R4 #7045 or R5 #7058 app code, merge either branch, change branch +protection, or approve a release. Adopt it for R5 after R4's released baseline is +settled and R5 is reconciled against that baseline. + +## Handoff + +Draft publication skips both native jobs before runner allocation. The existing +host checks still run; the draft `Tests` dependency install disables lifecycle +scripts to avoid invoking native setup. See the [draft event table and budget +limits](cloud-bootstrap-preparation.md#draft-publication-behavior). Skipped native +jobs supply no platform pass. Marking ready for review enables native work and +requires separate authorization and budget; a draft is not a trust installation. + +1. **Cloud code complete:** push the final code, run suitable regression checks, + and record the full PR head in the existing `Codex local handoff`. Keep + `Required local checks: IOS, ANDROID` and add `HARDWARE` when applicable. +2. **Hosted native validation:** the PR workflow checks out that exact head with + read-only permissions and no persisted checkout credential. Both platform jobs + install locked dependencies, build the development app and run the disposable + startup smoke. Report each platform as `PASS`, `FAIL` or `NOT RUN` with the run + URL, attempt and tested SHA. A successful build without a successful UI smoke + is not a platform pass. Any new commit starts this step again. +3. **Remaining native and physical checks:** assign an authorized tester to the + remaining feature-specific cases, fallback builds/UI when hosted setup fails, + and physical-device cases; do not assume the owner's Mac is available. Real + biometrics, phones/cameras, hardware signers, NFC/USB and signing prompts remain + explicit. Preserve the existing owner-scoped HARDWARE deferral procedure. A + later local `LOCAL_FAILED`/`BLOCKED` report cannot be hidden by a hosted pass. +4. **Release review:** inspect the actual exact-head evidence and unresolved + issues, obtain independent review and explicit owner approval for any signing, + distribution or submission. CI success grants none of those approvals. + +Hosted coverage is deliberately narrow: cold startup to the passcode screen and +entry of a throwaway first passcode through the confirmation screen. Stop before +account/key generation, backup, wallet funding, live transactions or signer use. +Do not mistake this smoke for the R5 deletion matrix, recovery/upgrade validation, +startup performance acceptance or complete release readiness. + +## Trust and gate behavior + +The existing trusted base-branch gate never checks out or executes PR code and +retains its `keeper/local-validation` status name. The new native workflow uses +`pull_request`, never elevated `pull_request_target` execution. No signing, +environment secrets, release credentials or production configuration are passed +to its jobs. + +The gate reads Actions metadata itself. It selects the newest run at the current +source SHA, verifies the workflow path, event, source repository/branch, associated +PR and SHA, and compares every workflow/harness file (including additions and +deletions) against both the current base and the run's base. The latest attempt +must have exactly one successful platform job with successful `Build development +app` and `Run disposable startup smoke` steps. A cancelled/pending run supplies no +pass. A completed run whose other platform failed can still supply its genuinely +successful platform. Partial reruns may lack the other platform in the latest +attempt; use **Re-run all jobs** to establish both platforms together. + +Missing metadata, API failures, untrusted changed CI files and incomplete evidence +produce zero hosted passes. Existing local reports remain a fallback. HARDWARE +cannot be filled by simulator evidence. New commits also invalidate owner scope +decisions. Owner deferral still needs a later honest local report recording +physical checks as NOT RUN/DEFERRED; an actual failed physical test cannot be +deferred. No PR-author assertion of a hosted result is parsed as evidence. + +This verifies reviewed CI provenance and actual GitHub step results. The native +build still executes PR-controlled Gradle/Xcode/Metro code on its disposable +runner. Integrity checks detect tracked harness changes between phases, but do +not provide an adversarial sandbox against malicious same-user build scripts. +Review native source and CI changes before merging; branch review requirements +remain necessary. The gate does not make an unreviewed app trustworthy. + +Bootstrap is intentionally staged: this infrastructure PR can run diagnostics and +test the verifier, but its new workflow is not yet in the protected base, so its +hosted results cannot authorize app PRs. Review and merge the CI-only PR only when +the owner approves. Then a new same-repository PR run using unchanged reviewed CI +files can provide evidence. Fork runs and modified native CI need review/local +fallback. The workflow-run listener is also effective only after the trusted gate +is merged; pre-merge gate tests execute the candidate inline script with mocked +API responses, not a privileged checkout of the candidate. + +## Dependency and native-install failures + +Use Yarn 1's frozen lock install with lifecycle scripts disabled, followed by the +explicit CI preparation in `.github/native/`. The repository's normal `prepare` +unconditionally invokes CocoaPods and writes a Mac-specific Android SDK path; +running it on Linux is not supported. Do not silently regenerate `yarn.lock`, use +an unlocked install, skip patch failures or replace a failed native build with +Jest. CocoaPods deployment mode likewise exposes Podfile/lock drift. + +The pinned Node compatibility tool is restricted to setup.sh's existing shim +whitelist without invoking its installer. Unrestricted `--hack` rewrites imports +to optional packages that the manifest does not contain; the bounded wrapper +keeps both platform Metro graphs resolvable using committed dependencies. Android +command-line tools are provisioned explicitly before SDK/NDK installation. + +The #7060 source checkpoint documents dependency drift at sprint +`def0d6a90c80287edb4f58ae272cbfc340f00d73`: missing Yarn lock selectors and older +IAP/Nitro Pod-lock selections. Those app/dependency repairs are excluded here. +The private environment's setup reported a failed frozen app dependency install; +this preparation neither repeats nor repairs it. Host contracts do not validate +app installation or either native platform. + +The inherited native lock-repair path is diagnostic only: if separately +authorized, it fails without building/testing and retains a candidate lock for +review. It is not enabled or run by this bootstrap. Dependency repair belongs in +a separate reviewed source change; an authoritative native run must use frozen +Yarn and `pod install --deployment`. New source commits require new native results. +Do not import the ongoing R4 diagnostic/reproducibility branch or its private +fixtures. Its existing Android build diagnostics do not constitute emulator UI +evidence. + +## Evidence, runner budget and remaining work + +Each platform retains allowlisted source/toolchain/runner metadata, actual command +exit outcomes and fixed diagnostic categories. Diagnostic Pod-lock repair +may also retain its dependency-lock candidate; it cannot report a platform pass. +Development configuration points service integrations at +loopback and uses dummy Firebase identities. Only the bounded first-use scenario +runs on fresh hosted devices. Raw build/device/Metro/Maestro logs, screenshots, +app bundles, keystores and generated account data are not uploaded. Failed raw +command output stays private to the ephemeral runner; metadata records which +command failed. Keep artifacts for seven days. + +On startup failure, metadata may include fixed Maestro command/assertion labels, +Metro failure categories and Android/iOS error categories from this job's fresh +account-free device. No error message, path, arbitrary exception name or suffix is +copied from those logs. The schema-1 `redacted_tool_errors` field retains its name +for compatibility but now holds only fixed category identifiers. Unrecognized +text supplies no category; an empty diagnostic list never means success. CLI +failures also omit arbitrary exception text. Diagnostic collection cannot change +a failed result. Cold development +bundling has a three-minute initial-screen allowance and seven-minute Maestro +command limit, with all UI assertions still required. This is not release +performance acceptance. The iOS build command is limited to the hosted +simulator's architecture and 40 minutes; Android's build command is capped at +35 minutes. + +Before the iOS build, a separate disposable simulator checks Maestro's driver +with a bounded `hierarchy` command. This is setup only: it installs no Keeper app +and supplies no app build or UI credit. Its private XDG state and fixed diagnostic +flags remain separate from the later smoke. The command is capped at five +minutes, with Maestro's driver timeout unchanged; owned simulator cleanup runs +on success or failure. + +For both platforms, a bounded loopback request compiles the development bundle +before iOS simulator creation or Android app installation; only its HTTP status, +byte count and hash are retained. +This setup step supplies no native/UI pass. On failure, fixed startup-screen +flags may be derived from bounded private Maestro hierarchy snapshots, without +retaining their text or attributes. The iOS exception collector reads the first +MiB of compact own-process output over the launch window, preserving early +errors instead of parsing a truncated JSON array. Missing diagnostics remain +explicit and do not imply a clean runtime. + +The iOS job is capped at 65 minutes and Android at 60 minutes, with command and +emulator boot limits inside those budgets. The jobs have no automatic retries; +new pushes cancel only +this workflow's runs for that PR. Cold native compilation, SDK installation and +CocoaPods can consume substantial hosted minutes. Use job timestamps and the +Actions usage page to report observed runtime and cost; do not describe a failed +dependency preflight's short duration as native-build timing. Current pricing, +included minutes and spending limits depend on the repository's plan. +As checked on 2026-10-17, GitHub's [standard runner rates](https://docs.github.com/en/billing/reference/actions-runner-pricing) +are $0.062/minute for macOS and $0.006/minute for Linux x64. At those rates a +30-minute iOS plus 30-minute Android attempt is about $2.04; reaching both job +caps is about $4.39, before included minutes, artifact storage, rounding and the +existing CI jobs. These are planning estimates, not measured native timings or +an account bill. These rates were inherited from #7060 and were not rechecked in +this source-only preparation. Observed bootstrap results belong in the validation report. +Enable spending limits before routine use if needed. + +Any later validation report must record the actual source/run/attempt and each +platform's build/UI outcome. This preparation runs no native workflow. Until both +strict native builds and actual UI runs +work reliably, use the existing local fallback without interrupting an active Mac +session. A passing startup smoke does not transfer to another PR or prove its +feature matrix. R4 and its existing Local evidence remain outside this bootstrap. +Hardware and release approval remain separate. + +## Administrative configuration + +Require `keeper/local-validation` and existing `Tests` for merges to `sprint`, +and require review for changes to the gate/native harness. `Native validation +contracts` is a useful additional required check. Do not require both native jobs +unconditionally when local fallback is intended: the combined gate resolves +platform evidence. Administrators should verify the required status app/source, +default-branch workflow availability, Actions runner permissions and private-repo +minute budget. The inherited #7060 report states that on 2026-10-17 the active +branch-rules endpoint reported required +`keeper/local-validation` and `Tests` checks from GitHub Actions (integration +15368). The legacy branch-protection endpoint returned HTTP 403; broader +administrator and bypass settings remain unverified. These settings were not +rechecked or changed by this source-only preparation. From c6684542491c098f2101a4bf4eb3f9cebffd3e88 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Sun, 18 Oct 2026 05:03:10 +0000 Subject: [PATCH 214/254] ci: reusable cloud release acceptance foundation (#7061) (cherry picked from commit 59c821ce195a5c063d99045fd97b5ac605433f62) --- .github/native/hosted_native.py | 82 +++-- .github/native/release_acceptance.cjs | 32 ++ .github/native/release_evidence.cjs | 48 +++ .github/native/release_metro.cjs | 19 + .github/native/release_preflight.cjs | 58 +++ .github/native/release_probe.js | 38 ++ .github/native/release_probe.yaml | 32 ++ .github/native/release_probe_core.js | 144 ++++++++ .github/tests/release-evidence.test.cjs | 61 ++++ .github/tests/release-pipeline.test.cjs | 334 ++++++++++++++++++ .github/tests/release-probe.test.cjs | 135 +++++++ .github/tests/test_release_native.py | 150 ++++++++ .github/workflows/keeper-native-platform.yml | 121 +++++++ .../workflows/keeper-release-acceptance.yml | 83 +++++ AGENTS.md | 34 ++ docs/cloud-release-pipeline.md | 191 ++++++++++ 16 files changed, 1542 insertions(+), 20 deletions(-) create mode 100644 .github/native/release_acceptance.cjs create mode 100644 .github/native/release_evidence.cjs create mode 100644 .github/native/release_metro.cjs create mode 100644 .github/native/release_preflight.cjs create mode 100644 .github/native/release_probe.js create mode 100644 .github/native/release_probe.yaml create mode 100644 .github/native/release_probe_core.js create mode 100644 .github/tests/release-evidence.test.cjs create mode 100644 .github/tests/release-pipeline.test.cjs create mode 100644 .github/tests/release-probe.test.cjs create mode 100644 .github/tests/test_release_native.py create mode 100644 .github/workflows/keeper-native-platform.yml create mode 100644 .github/workflows/keeper-release-acceptance.yml create mode 100644 docs/cloud-release-pipeline.md diff --git a/.github/native/hosted_native.py b/.github/native/hosted_native.py index 6573d95950..e9c95a3169 100644 --- a/.github/native/hosted_native.py +++ b/.github/native/hosted_native.py @@ -469,7 +469,7 @@ def record(): 'run_id': os.environ.get('GITHUB_RUN_ID'), 'run_attempt': os.environ.get('GITHUB_RUN_ATTEMPT'), 'runner': {key: os.environ.get(key) for key in ['RUNNER_OS', 'RUNNER_ARCH', 'ImageOS', 'ImageVersion']}, - 'os': platform.platform(), 'commands': [], 'build': 'NOT RUN', 'smoke': 'NOT RUN', + 'os': platform.platform(), 'commands': [], 'build': 'NOT RUN', 'smoke': 'NOT RUN', 'native_probe': 'NOT RUN', 'hardware': 'NOT RUN', 'signing': 'NOT RUN', 'app_id': APP_IDS[PLATFORM], 'smoke_scope': 'Fresh install; render Create a passcode; type four disposable digits; assert Confirm your passcode; stop before persisting credentials.', 'startup_timing_scope': 'Bounded cold development bundle/startup allowance; no release performance acceptance.', @@ -723,7 +723,7 @@ def build(): save(data) -def warm_metro_bundle(): +def warm_metro_bundle(suite='startup'): """Bound cold development bundling before starting either disposable app.""" bundle_platform = {'IOS': 'ios', 'ANDROID': 'android'}.get(PLATFORM) if bundle_platform is None: @@ -750,20 +750,30 @@ def warm_metro_bundle(): finally: details['duration_seconds'] = round(time.monotonic() - started, 1) data = read() - data['metro_warmup'] = details + data['metro_warmup' if suite == 'startup' else 'probe_bundle'] = details save(data) -def smoke(): +def smoke(suite='startup'): + if suite not in ['startup', 'probe']: + raise ValueError('Unknown disposable native suite.') + is_probe = suite == 'probe' + result_key = 'native_probe' if is_probe else 'smoke' + if is_probe and read()['smoke'] != 'PASS': + raise RuntimeError('The real app startup smoke must pass before module probes.') if read()['build'] != 'PASS': raise RuntimeError('A successful native build is required before smoke testing.') if artifact_identity() != read()['built_artifact']: raise RuntimeError('Built artifact changed before the disposable startup smoke.') + report = PRIVATE / ('probe-junit.xml' if is_probe else 'smoke-junit.xml') + report.unlink(missing_ok=True) metro_output = (PRIVATE / 'metro.log').open('wb') - metro = subprocess.Popen(['node', 'node_modules/react-native/cli.js', 'start', '--port', '8081'], + metro_args = ['node', 'node_modules/react-native/cli.js', 'start', '--port', '8081'] + if is_probe: + metro_args += ['--config', str(ROOT / '.github/native/release_metro.cjs')] + metro = subprocess.Popen(metro_args, cwd=ROOT, stdout=metro_output, stderr=subprocess.STDOUT, start_new_session=True) device = None - report = PRIVATE / 'smoke-junit.xml' try: for _ in range(60): if metro.poll() is not None: @@ -776,7 +786,7 @@ def smoke(): time.sleep(1) else: raise RuntimeError('Metro did not become ready within 60 attempts.') - warm_metro_bundle() + warm_metro_bundle(suite) if PLATFORM == 'IOS': device = command(['xcrun', 'simctl', 'create', 'Keeper-disposable-ci', 'com.apple.CoreSimulator.SimDeviceType.iPhone-16', @@ -812,19 +822,31 @@ def smoke(): command([data['maestro']['binary'], '--device', device, 'test', '--env', 'APP_ID=' + APP_IDS[PLATFORM], '--flatten-debug-output', '--debug-output', str(PRIVATE / 'maestro-output'), '--format', 'junit', '--output', str(report), '--test-output-dir', str(PRIVATE / 'maestro-output'), - '.github/native/startup.yaml'], 'Assert startup and disposable passcode transition', timeout=420) - suite = ET.parse(report).getroot() - cases = list(suite.iter('testcase')) - if not cases or any(list(case.iter('failure')) or list(case.iter('error')) or list(case.iter('skipped')) - for case in cases): - raise RuntimeError('Maestro did not produce a complete successful smoke report.') + '.github/native/release_probe.yaml' if is_probe else '.github/native/startup.yaml'], + 'Assert native persistence across process launches' if is_probe else + 'Assert startup and disposable passcode transition', timeout=300 if is_probe else 420, + error_details=not is_probe) + cases = successful_junit(report) + if artifact_identity() != read()['built_artifact']: + raise RuntimeError('Built artifact changed during native execution.') data = read() - data['smoke'] = 'PASS' - data['smoke_test_cases'] = len(cases) + data[result_key] = 'PASS' + data[result_key + '_test_cases'] = cases + if is_probe: + data['probe_scope'] = ('CI-only Metro entrypoint; actual encrypted Realm/current schemas and production MMKV; ' + 'three process launches, rollback, wrong-key rejection, targeted primitive deletion, ' + 'separate database files and cleanup. NOT app key deletion, account isolation, ' + 'setup/recovery or release performance acceptance.') save(data) except Exception: try: - record_startup_failure(report, device) + if not is_probe: + record_startup_failure(report, device) + else: + data = read() + data['native_probe'] = 'FAIL' + data['probe_diagnostics'] = 'Native module probe failed; raw output retained only on ephemeral runner.' + save(data) except Exception as error: # Diagnostics never replace the original failed native/UI result. data = read() @@ -850,9 +872,29 @@ def smoke(): stderr=subprocess.DEVNULL, timeout=30, check=False) +def successful_junit(report): + if not report.is_file() or report.stat().st_size > 1024 * 1024: + raise RuntimeError('Missing or oversized native JUnit report.') + root = ET.parse(report).getroot() + for suite in root.iter(): + if suite.tag in ['testsuite', 'testsuites']: + for attribute in ['failures', 'errors', 'skipped', 'disabled']: + if int(suite.get(attribute, '0')) != 0: + raise RuntimeError('Native JUnit declares unsuccessful cases.') + cases = list(root.iter('testcase')) + if not cases or any(list(root.iter(tag)) for tag in ['failure', 'error', 'skipped']) or any( + case.get('status', 'SUCCESS').upper() != 'SUCCESS' for case in cases): + raise RuntimeError('Maestro did not produce a complete successful native report.') + return len(cases) + + +def probe(): + smoke('probe') + + def main(): - if PLATFORM not in APP_IDS or len(sys.argv) != 2 or sys.argv[1] not in ['record', 'prepare', 'driver', 'build', 'smoke']: - raise RuntimeError('Expected a hosted platform and record|prepare|driver|build|smoke.') + if PLATFORM not in APP_IDS or len(sys.argv) != 2 or sys.argv[1] not in ['record', 'prepare', 'driver', 'build', 'smoke', 'probe']: + raise RuntimeError('Expected a hosted platform and record|prepare|driver|build|smoke|probe.') os.environ.update({'ENVFILE': '.env.ci', 'RCT_NO_LAUNCH_PACKAGER': '1', 'RCT_NEW_ARCH_ENABLED': '1', 'MAESTRO_CLI_NO_ANALYTICS': '1'}) run_phase(sys.argv[1]) @@ -865,8 +907,8 @@ def run_phase(phase): if EVIDENCE.exists(): evidence = read() evidence['failed_phase'] = phase - if phase in ['build', 'smoke']: - evidence[phase] = 'FAIL' + if phase in ['build', 'smoke', 'probe']: + evidence['native_probe' if phase == 'probe' else phase] = 'FAIL' save(evidence) raise diff --git a/.github/native/release_acceptance.cjs b/.github/native/release_acceptance.cjs new file mode 100644 index 0000000000..01a8c5e0b0 --- /dev/null +++ b/.github/native/release_acceptance.cjs @@ -0,0 +1,32 @@ +// Common release obligations. Extend with real executable scenarios, never status-only waivers. +// Native module checks are deliberately distinct from app-account or feature acceptance. +const REQUIRED = Object.freeze([ + 'native-build-and-startup', + 'native-realm-mmkv-process-persistence', + 'key-deletion-live-reference-and-privacy', + 'setup-offline-retry-relaunch-no-duplicates', + 'app-account-session-isolation', + 'normal-page-runtime-and-recovery-regression', + 'cold-start-and-first-wallet-performance', +]); +function acceptance({ trusted, ios, android }) { + const executed = trusted === true && ios === 'success' && android === 'success'; + const cases = REQUIRED.map((id, index) => ({ + id, + status: index < 2 ? (executed ? 'PASS' : 'NOT VERIFIED') : 'NOT IMPLEMENTED', + scope: index === 1 ? 'CI entrypoint; actual native storage, not application account isolation' : + index > 1 ? 'Requires reviewed app scenarios; R5 R4-dependent checks wait for reconciliation' : + 'Unsigned simulator / development debug-key emulator; narrow passcode smoke', + })); + return { release_ready: false, phase_a: 'BLOCKED', cases, + signing: 'NOT IMPLEMENTED / NOT VERIFIED (separate Phase B)', + physical: 'NOT RUN (authorized tester and exact distributed artifact required)' }; +} +function markdown(result) { + return '# Keeper Phase A release acceptance: BLOCKED\n\n' + + 'This check cannot approve a release. Startup and native module probes do not cover the app matrix.\n\n' + + '| Required scenario | Result | Scope |\n| --- | --- | --- |\n' + + result.cases.map(c => `| ${c.id} | ${c.status} | ${c.scope} |`).join('\n') + + `\n\nSigning/distribution: ${result.signing}.\n\nPhysical: ${result.physical}.\n`; +} +module.exports = { REQUIRED, acceptance, markdown }; diff --git a/.github/native/release_evidence.cjs b/.github/native/release_evidence.cjs new file mode 100644 index 0000000000..542f6dff95 --- /dev/null +++ b/.github/native/release_evidence.cjs @@ -0,0 +1,48 @@ +// Read-only Actions provenance. Writable PR assertions/artifacts supply no credit. +async function evidence(github, context, { ios, android, attempt }) { + const pr = context.payload.pull_request; + const { owner, repo } = context.repo; + const repository = owner + '/' + repo; + if (ios !== 'success' || android !== 'success' || context.eventName !== 'pull_request' || + !pr || pr.number === 7045 || pr.base.ref !== 'sprint' || + pr.head.repo?.full_name !== repository || pr.base.repo?.full_name !== repository || + !pr.head.repo.id || !pr.base.repo.id || !/^[a-f0-9]{40}$/.test(pr.head.sha) || + !Number.isSafeInteger(attempt) || attempt < 1) return false; + try { + const runs = await github.paginate(github.rest.actions.listWorkflowRuns, { + owner, repo, workflow_id: 'keeper-release-acceptance.yml', + head_sha: pr.head.sha, event: 'pull_request', per_page: 100, + }); + const run = runs.sort((a, b) => b.id - a.id)[0]; + if (!run || run.id !== context.runId || run.run_attempt !== attempt || + run.path !== '.github/workflows/keeper-release-acceptance.yml' || + run.event !== 'pull_request' || run.head_sha !== pr.head.sha || + run.head_branch !== pr.head.ref || run.head_repository?.full_name !== repository || + run.head_repository?.id !== pr.head.repo.id || + (!['in_progress', 'completed'].includes(run.status) || + (run.status === 'completed' && !['success', 'failure'].includes(run.conclusion)))) return false; + const linked = run.pull_requests?.find(item => item.number === pr.number); + if (!linked || linked.head?.sha !== pr.head.sha || linked.head?.ref !== pr.head.ref || + linked.head?.repo?.id !== pr.head.repo.id || linked.base?.ref !== 'sprint' || + linked.base?.sha !== pr.base.sha || linked.base?.repo?.id !== pr.base.repo.id) return false; + const jobs = await github.paginate(github.rest.actions.listJobsForWorkflowRunAttempt, { + owner, repo, run_id: run.id, attempt_number: attempt, per_page: 100, + }); + return ['IOS', 'ANDROID'].every(platform => { + const matches = jobs.filter(job => job.name === + `${platform.toLowerCase()} / Native ${platform} build and probes`); + if (matches.length !== 1) return false; + const job = matches[0]; + if (job.head_sha !== pr.head.sha || job.run_id !== run.id || job.run_attempt !== attempt || + job.status !== 'completed' || job.conclusion !== 'success') return false; + const steps = ['Build development app', ...(platform === 'IOS' + ? ['Run disposable startup smoke', 'Run disposable native persistence probe'] + : ['Run Android startup and native persistence probes'])]; + return steps.every(name => { + const found = job.steps?.filter(step => step.name === name) ?? []; + return found.length === 1 && found[0].status === 'completed' && found[0].conclusion === 'success'; + }); + }); + } catch { return false; } +} +module.exports = { evidence }; diff --git a/.github/native/release_metro.cjs b/.github/native/release_metro.cjs new file mode 100644 index 0000000000..df8c39ad61 --- /dev/null +++ b/.github/native/release_metro.cjs @@ -0,0 +1,19 @@ +// CI-only entrypoint for native module persistence. No product source is rewritten. +const path = require('node:path'); +const base = require('../../metro.config'); +const enhance = base.server?.enhanceMiddleware; +module.exports = { + ...base, + projectRoot: path.resolve(__dirname, '../..'), + server: { + ...base.server, + enhanceMiddleware(middleware, server) { + const inner = enhance ? enhance(middleware, server) : middleware; + return (req, res, next) => { + req.url = req.url.replace(/^\/index\.(bundle|map)(?=\?|$)/, + '/.github/native/release_probe.$1'); + return inner(req, res, next); + }; + }, + }, +}; diff --git a/.github/native/release_preflight.cjs b/.github/native/release_preflight.cjs new file mode 100644 index 0000000000..b95377b4ea --- /dev/null +++ b/.github/native/release_preflight.cjs @@ -0,0 +1,58 @@ +// Unprivileged PR bootstrap check, not a release authorization or commit-status writer. +const WORKFLOWS = [ + '.github/workflows/keeper-hosted-native.yml', + '.github/workflows/keeper-release-acceptance.yml', + '.github/workflows/keeper-native-platform.yml', +]; +const NATIVE_FILES = [ + 'hosted_native.py', 'nodeify.cjs', 'startup.yaml', 'release_preflight.cjs', + 'release_acceptance.cjs', 'release_evidence.cjs', 'release_metro.cjs', 'release_probe.js', + 'release_probe_core.js', 'release_probe.yaml', +].map(name => '.github/native/' + name); +function fingerprint(tree) { + if (tree.truncated) throw new Error('Truncated CI tree'); + const entries = tree.tree.filter(e => WORKFLOWS.includes(e.path) || e.path.startsWith('.github/native/')); + if (![...WORKFLOWS, ...NATIVE_FILES].every(p => entries.some(e => e.path === p && e.type === 'blob' && ['100644', '100755'].includes(e.mode)))) { + throw new Error('Reviewed Phase A / #7060 infrastructure is not installed'); + } + return JSON.stringify(entries.map(e => [e.path, e.mode, e.type, e.sha]) + .sort((a, b) => a[0].localeCompare(b[0]))); +} +async function preflight(github, context) { + const original = context.payload.pull_request; + if (context.eventName !== 'pull_request' || !original || original.number === 7045) { + return { trusted: false, reason: 'Outside Phase A scope; frozen R4 excluded' }; + } + const { owner, repo } = context.repo; + const { data: pr } = await github.rest.pulls.get({ owner, repo, pull_number: original.number }); + const repository = owner + '/' + repo; + if (pr.base.ref !== 'sprint' || pr.head.repo?.full_name !== repository || + pr.base.repo?.full_name !== repository || pr.head.sha !== original.head.sha || + pr.head.ref !== original.head.ref || !/^[a-f0-9]{40}$/.test(pr.head.sha) || + !pr.head.repo.id || pr.head.repo.id !== original.head.repo?.id || + !pr.base.repo.id || pr.base.repo.id !== original.base.repo?.id || + original.base.ref !== 'sprint') { + return { trusted: false, reason: 'Exact current same-repository PR provenance is unavailable' }; + } + try { + const snapshots = await Promise.all([pr.head.sha, pr.base.sha, original.base.sha].map(async ref => { + const { data } = await github.rest.git.getTree({ owner, repo, tree_sha: ref, recursive: '1' }); + return fingerprint(data); + })); + if (!snapshots.every(value => value === snapshots[0])) { + return { trusted: false, reason: 'CI differs from current or run base; review/install before native acceptance' }; + } + // Re-read after API work so a concurrent push cannot supply current-head credit. + const { data: latest } = await github.rest.pulls.get({ owner, repo, pull_number: original.number }); + if (latest.head.sha !== pr.head.sha || latest.base.sha !== pr.base.sha || + latest.base.ref !== pr.base.ref || latest.head.ref !== pr.head.ref || + latest.head.repo?.id !== pr.head.repo.id || latest.base.repo?.id !== pr.base.repo.id || + latest.head.repo?.full_name !== repository || latest.base.repo?.full_name !== repository) { + return { trusted: false, reason: 'PR source/base changed during provenance verification' }; + } + return { trusted: true, reason: 'Exact-head CI matches both protected base snapshots' }; + } catch { + return { trusted: false, reason: 'Reviewed Phase A / #7060 infrastructure unavailable; no native credit' }; + } +} +module.exports = { WORKFLOWS, NATIVE_FILES, fingerprint, preflight }; diff --git a/.github/native/release_probe.js b/.github/native/release_probe.js new file mode 100644 index 0000000000..827687c28d --- /dev/null +++ b/.github/native/release_probe.js @@ -0,0 +1,38 @@ +// Alternative Metro entrypoint, used only by the disposable release acceptance job. +// The production app entrypoint and bundle remain unchanged on disk. +import 'react-native-get-random-values'; +import React, { useEffect, useState } from 'react'; +import { AppRegistry, SafeAreaView, Text } from 'react-native'; +import Realm from 'realm'; +import schema from '../../src/storage/realm/schema'; +import * as storage from '../../src/storage'; +import { name as appName } from '../../app.json'; +import { runProbe, ProbeFailure } from './release_probe_core'; + +// No app services, Sentry, account creation, keychain, migrations or network imports. +let attempt; +function ReleaseProbe() { + const [result, setResult] = useState({ stage: 'running', code: '' }); + useEffect(() => { + let active = true; + // A remount must not advance the persistence phase in the same JS process. + if (!attempt) { + attempt = __DEV__ + ? runProbe({ Realm, schema, storage }).then( + (stage) => ({ stage, code: '' }), + (error) => ({ stage: 'failed', code: error instanceof ProbeFailure ? error.code : 'native-exception' }) + ) + : Promise.resolve({ stage: 'failed', code: 'development-only' }); + } + attempt.then((value) => { if (active) setResult(value); }); + return () => { active = false; }; + }, []); + return ( + + Keeper native probe {result.stage} + {result.code} + + ); +} + +AppRegistry.registerComponent(appName, () => ReleaseProbe); diff --git a/.github/native/release_probe.yaml b/.github/native/release_probe.yaml new file mode 100644 index 0000000000..bc9fad6f5a --- /dev/null +++ b/.github/native/release_probe.yaml @@ -0,0 +1,32 @@ +appId: ${APP_ID} +name: Disposable native module persistence across process relaunch +--- +- launchApp: + clearState: true + permissions: + all: deny +- extendedWaitUntil: + visible: + id: native-probe-seeded + timeout: 60000 +- assertNotVisible: + id: native-probe-failed +- stopApp +- launchApp: + clearState: false +- extendedWaitUntil: + visible: + id: native-probe-mutated + timeout: 60000 +- assertNotVisible: + id: native-probe-failed +- stopApp +- launchApp: + clearState: false +- extendedWaitUntil: + visible: + id: native-probe-pass + timeout: 60000 +- assertNotVisible: + id: native-probe-failed +- stopApp diff --git a/.github/native/release_probe_core.js b/.github/native/release_probe_core.js new file mode 100644 index 0000000000..c9903e122e --- /dev/null +++ b/.github/native/release_probe_core.js @@ -0,0 +1,144 @@ +/* CI-only native persistence acceptance. No accounts, network or fundable keys. */ +const PHASE_KEY = 'keeper-ci-release-probe-v1-phase'; +const MARKER_KEY = 'keeper-ci-release-probe-v1-marker'; +const PRIMARY = 'keeper-ci-synthetic-wallet'; +const OTHER = 'keeper-ci-synthetic-unrelated-wallet'; +const ROLLBACK = 'keeper-ci-synthetic-rolled-back-wallet'; +const SCHEMA_VERSION = 1; // Fresh current schema only; never an upgrade/migration claim. + +class ProbeFailure extends Error { + constructor(code) { + super(code); + this.code = code; + } +} + +function check(condition, code) { + if (!condition) throw new ProbeFailure(code); +} + +function wallet(id, name) { + return { + id, + entityKind: 'WALLET', + type: 'DEFAULT', + networkType: 'TESTNET', + scriptType: 'P2WPKH', + // Invalid public sentinel, deliberately unusable as an account/key fixture. + specs: { + xpub: 'CI-SYNTHETIC-NOT-A-KEY', + nextFreeAddressIndex: 0, + nextFreeChangeAddressIndex: 0, + totalExternalAddresses: 0, + confirmedUTXOs: [], + unconfirmedUTXOs: [], + balances: { confirmed: 0, unconfirmed: 0 }, + transactions: [], + hasNewUpdates: false, + lastSynched: 0, + }, + presentationData: { name, description: 'Disposable CI fixture', visibility: 'DEFAULT' }, + }; +} + +// Realm and storage are the actual native modules in release_probe.js. +// Injection exists solely to test failure/phase orchestration on non-native hosts. +async function runProbe({ Realm, schema, storage }) { + const phase = storage.getString(PHASE_KEY); + check(phase === undefined || phase === 'seeded' || phase === 'mutated', 'invalid-phase'); + const directory = Realm.defaultPath.slice(0, Realm.defaultPath.lastIndexOf('/') + 1); + check(directory.length > 0, 'missing-native-directory'); + // Published constants encrypt synthetic disposable data, never product secrets. + const config = (suffix, keyByte = 7) => ({ + path: `${directory}keeper-ci-release-probe-v1-${suffix}.realm`, + schema, + schemaVersion: SCHEMA_VERSION, + encryptionKey: new Uint8Array(64).fill(keyByte), + }); + const aConfig = config('a'); + const bConfig = config('b', 8); + let a; + let b; + try { + a = await Realm.open(aConfig); + b = await Realm.open(bConfig); + const get = (realm, id) => realm.objectForPrimaryKey('Wallet', id); + if (phase === undefined) { + check(a.objects('Wallet').length === 0 && b.objects('Wallet').length === 0, 'fixture-not-fresh'); + a.write(() => { + a.create('Wallet', wallet(PRIMARY, 'CI primary')); + a.create('Wallet', wallet(OTHER, 'CI unrelated')); + }); + b.write(() => b.create('Wallet', wallet(PRIMARY, 'CI separate database'))); + storage.setItem(MARKER_KEY, 'synthetic-marker-v1'); + storage.setItem(PHASE_KEY, 'seeded'); + return 'seeded'; + } + + check(storage.getString(MARKER_KEY) === 'synthetic-marker-v1', 'mmkv-marker-not-persisted'); + check(get(a, OTHER)?.presentationData.name === 'CI unrelated', 'unrelated-wallet-not-persisted'); + const otherSpecs = get(a, OTHER).specs; + check(otherSpecs.xpub === 'CI-SYNTHETIC-NOT-A-KEY' + && otherSpecs.balances.confirmed === 0 && otherSpecs.balances.unconfirmed === 0 + && otherSpecs.confirmedUTXOs.length === 0 && otherSpecs.transactions.length === 0, + 'embedded-wallet-state-not-persisted'); + check(get(b, PRIMARY)?.presentationData.name === 'CI separate database', 'separate-file-not-persisted'); + check(a.objects('Wallet').length === (phase === 'seeded' ? 2 : 1), 'unexpected-primary-record-count'); + check(b.objects('Wallet').length === 1, 'unexpected-separate-record-count'); + + if (phase === 'seeded') { + check(get(a, PRIMARY)?.presentationData.name === 'CI primary', 'primary-wallet-not-persisted'); + const aborted = new Error('intentional-synthetic-transaction-abort'); + let rolledBack = false; + try { + a.write(() => { + a.create('Wallet', wallet(ROLLBACK, 'CI rollback')); + throw aborted; + }); + } catch (error) { + if (error !== aborted) throw error; + rolledBack = true; + } + check(rolledBack && !get(a, ROLLBACK), 'transaction-rollback-failed'); + + // Close the correct-key handle first so rejection checks persisted file encryption. + a.close(); + a = undefined; + let rejected = false; + let wrongKeyRealm; + try { + wrongKeyRealm = await Realm.open(config('a', 9)); + } catch { + rejected = true; + } finally { + if (wrongKeyRealm) wrongKeyRealm.close(); + } + check(rejected, 'wrong-encryption-key-accepted'); + a = await Realm.open(aConfig); + check(get(a, PRIMARY)?.presentationData.name === 'CI primary', 'correct-key-reopen-failed'); + // Deliberately only Realm deletion semantics; no claim about product key deletion. + a.write(() => a.delete(get(a, PRIMARY))); + check(!get(a, PRIMARY) && get(a, OTHER), 'targeted-wallet-delete-failed'); + storage.setItem(PHASE_KEY, 'mutated'); + return 'mutated'; + } + + check(!get(a, PRIMARY) && !get(a, ROLLBACK), 'delete-or-rollback-not-persisted'); + a.close(); + b.close(); + a = undefined; + b = undefined; + Realm.deleteFile(aConfig); + Realm.deleteFile(bConfig); + storage.deleteItem(MARKER_KEY); + storage.deleteItem(PHASE_KEY); + check(storage.getString(MARKER_KEY) === undefined && storage.getString(PHASE_KEY) === undefined, + 'fixture-cleanup-failed'); + return 'pass'; + } finally { + if (a) a.close(); + if (b) b.close(); + } +} + +module.exports = { runProbe, ProbeFailure, PHASE_KEY, MARKER_KEY, PRIMARY, OTHER, ROLLBACK }; diff --git a/.github/tests/release-evidence.test.cjs b/.github/tests/release-evidence.test.cjs new file mode 100644 index 0000000000..fd818601f0 --- /dev/null +++ b/.github/tests/release-evidence.test.cjs @@ -0,0 +1,61 @@ +const assert = require('node:assert/strict'); +const { test } = require('node:test'); +const { evidence } = require('../native/release_evidence.cjs'); +function fixture() { + const repository = 'KeeperCommunity/bitcoin-keeper'; + const pr = { number: 7058, head: { sha: 'a'.repeat(40), ref: 'future-release', + repo: { full_name: repository, id: 1 } }, base: { sha: 'b'.repeat(40), ref: 'sprint', + repo: { full_name: repository, id: 1 } } }; + const context = { repo: { owner: 'KeeperCommunity', repo: 'bitcoin-keeper' }, + eventName: 'pull_request', runId: 90, payload: { pull_request: pr } }; + const run = { id: 90, run_attempt: 2, path: '.github/workflows/keeper-release-acceptance.yml', + event: 'pull_request', head_sha: pr.head.sha, head_branch: pr.head.ref, + head_repository: pr.head.repo, pull_requests: [structuredClone(pr)], status: 'in_progress' }; + const jobs = ['IOS', 'ANDROID'].map(platform => ({ + name: `${platform.toLowerCase()} / Native ${platform} build and probes`, head_sha: pr.head.sha, + run_id: 90, run_attempt: 2, status: 'completed', conclusion: 'success', + steps: ['Build development app', ...(platform === 'IOS' + ? ['Run disposable startup smoke', 'Run disposable native persistence probe'] + : ['Run Android startup and native persistence probes'])].map(name => + ({ name, status: 'completed', conclusion: 'success' })), + })); + const state = { runs: [run], jobs, context, input: { ios: 'success', android: 'success', attempt: 2 }, calls: [] }; + const runsAPI = () => {}; const jobsAPI = () => {}; + state.github = { rest: { actions: { listWorkflowRuns: runsAPI, listJobsForWorkflowRunAttempt: jobsAPI } }, + paginate: async (api, args) => { state.calls.push(args); if (state.error) throw new Error('private'); + return api === runsAPI ? [...state.runs] : state.jobs; } }; + state.check = () => evidence(state.github, state.context, state.input); + return state; +} +test('current in-progress summary credits both completed exact-attempt native jobs only', async () => { + const state = fixture(); assert.equal(await state.check(), true); + assert.equal(state.calls[1].attempt_number, 2); + assert.equal(state.calls[0].head_sha, state.context.payload.pull_request.head.sha); +}); +test('latest run and attempt prevent summary-only partial reruns and older-run reuse', async () => { + for (const mutate of [s => { s.jobs.pop(); }, s => { s.jobs[0].run_attempt = 1; }, + s => { s.runs[0].run_attempt = 3; }, s => { s.runs.push({ ...s.runs[0], id: 91 }); }]) { + const state = fixture(); mutate(state); assert.equal(await state.check(), false); + } +}); +test('wrong source, workflow, event, PR association and repository provide no evidence', async () => { + for (const mutate of [s => { s.runs[0].head_sha = 'c'.repeat(40); }, + s => { s.runs[0].path = 'other.yml'; }, s => { s.runs[0].event = 'push'; }, + s => { s.runs[0].head_branch = 'other'; }, s => { s.runs[0].head_repository = { id: 9 }; }, + s => { s.runs[0].pull_requests[0].number += 1; }, + s => { s.runs[0].pull_requests[0].base.sha = 'c'.repeat(40); }]) { + const state = fixture(); mutate(state); assert.equal(await state.check(), false); + } +}); +test('failed/skipped/pending jobs, build-only results and ambiguous steps cannot pass', async () => { + for (const mutate of [s => { s.jobs[0].conclusion = 'failure'; }, + s => { s.jobs[1].status = 'in_progress'; }, s => { s.jobs[0].steps.pop(); }, + s => { s.jobs[1].steps[1].conclusion = 'skipped'; }, s => { s.jobs.push(s.jobs[0]); }, + s => { s.jobs[0].steps.push(s.jobs[0].steps[0]); }, s => { s.input.android = 'skipped'; }, + s => { s.context.payload.pull_request.number = 7045; }, + s => { s.context.payload.pull_request.head.repo.full_name = 'fork/bitcoin-keeper'; }, + s => { s.runs[0].status = 'queued'; }, + s => { Object.assign(s.runs[0], { status: 'completed', conclusion: 'cancelled' }); }, s => { s.error = true; }]) { + const state = fixture(); mutate(state); assert.equal(await state.check(), false); + } +}); diff --git a/.github/tests/release-pipeline.test.cjs b/.github/tests/release-pipeline.test.cjs new file mode 100644 index 0000000000..459d06002b --- /dev/null +++ b/.github/tests/release-pipeline.test.cjs @@ -0,0 +1,334 @@ +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const path = require('node:path'); +const vm = require('node:vm'); +const { test } = require('node:test'); +const { WORKFLOWS, fingerprint, preflight } = require('../native/release_preflight.cjs'); +const { REQUIRED, acceptance, markdown } = require('../native/release_acceptance.cjs'); + +// GitHub metadata models only: these tests supply no native or release evidence. +const repository = 'KeeperCommunity/bitcoin-keeper'; +const repositoryId = 470904030; +const headSha = 'a'.repeat(40); +const baseSha = 'b'.repeat(40); +const runBaseSha = 'c'.repeat(40); +const NATIVE_FILES = [ + 'hosted_native.py', 'startup.yaml', 'nodeify.cjs', 'release_preflight.cjs', + 'release_acceptance.cjs', 'release_evidence.cjs', 'release_metro.cjs', 'release_probe.js', + 'release_probe_core.js', 'release_probe.yaml', +].map(name => '.github/native/' + name); +const clone = value => structuredClone(value); + +function trustedTree() { + const paths = [...WORKFLOWS, ...NATIVE_FILES, 'src/index.ts']; + return { + truncated: false, + tree: paths.map((path, index) => ({ + path, mode: '100644', type: 'blob', sha: (index + 1).toString(16).padStart(40, '0'), + })), + }; +} + +function pullRequest() { + return { + number: 7058, + head: { sha: headSha, ref: 'codex/future-release', + repo: { full_name: repository, id: repositoryId } }, + base: { sha: baseSha, ref: 'sprint', + repo: { full_name: repository, id: repositoryId } }, + }; +} + +async function evaluate(options = {}) { + const original = pullRequest(); + original.base.sha = runBaseSha; + const current = pullRequest(); + options.original?.(original); + options.current?.(current); + const latest = clone(current); + options.latest?.(latest); + const calls = []; + let reads = 0; + const github = { + rest: { + pulls: { + get: async args => { + calls.push({ api: 'pulls.get', ...args }); + reads += 1; + if (options.pullError) throw new Error('Private API detail must not be reported'); + return { data: clone(reads === 1 ? current : latest) }; + }, + }, + git: { + getTree: async args => { + calls.push({ api: 'git.getTree', ...args }); + if (options.treeError === args.tree_sha) throw new Error('Private API detail'); + return { data: clone(options.trees?.[args.tree_sha] ?? trustedTree()) }; + }, + }, + }, + }; + const context = { + eventName: options.eventName ?? 'pull_request', + repo: { owner: 'KeeperCommunity', repo: 'bitcoin-keeper' }, + payload: { pull_request: options.withoutPR ? undefined : original }, + }; + return { result: await preflight(github, context), calls }; +} + +test('unchanged reviewed CI on app/current-base/run-base yields exact-head preflight only', async () => { + const { result, calls } = await evaluate(); + assert.equal(result.trusted, true); + assert.equal(calls.filter(call => call.api === 'pulls.get').length, 2); + assert.deepEqual(calls.filter(call => call.api === 'git.getTree').map(call => call.tree_sha), + [headSha, baseSha, runBaseSha]); + assert.ok(calls.filter(call => call.api === 'git.getTree').every(call => call.recursive === '1')); + assert.ok(calls.every(call => !/create|update|dispatch|status/.test(call.api))); +}); + +test('all later same-repository release PRs use the same preflight without release-number routing', async () => { + for (const number of [7058, 8001, 12000]) { + const { result } = await evaluate({ + original: pr => { pr.number = number; }, + current: pr => { pr.number = number; }, + }); + assert.equal(result.trusted, true, String(number)); + } +}); + +test('R4 is excluded before any API read, even when its source otherwise matches', async () => { + const { result, calls } = await evaluate({ original: pr => { pr.number = 7045; } }); + assert.equal(result.trusted, false); + assert.match(result.reason, /R4/); + assert.deepEqual(calls, []); +}); + +test('non-PR events and missing PR metadata never trigger native authorization', async () => { + for (const options of [{ eventName: 'workflow_dispatch' }, { eventName: 'pull_request_target' }, + { eventName: 'push' }, { withoutPR: true }]) { + const { result, calls } = await evaluate(options); + assert.equal(result.trusted, false); + assert.deepEqual(calls, []); + } +}); + +test('forks, wrong target branches, stale payloads and repository-ID drift fail before tree reads', async () => { + const mutations = [ + pr => { pr.head.repo = { full_name: 'fork/bitcoin-keeper', id: 123 }; }, + pr => { pr.base.repo.full_name = 'fork/bitcoin-keeper'; }, + pr => { pr.base.ref = 'other-base'; }, + pr => { pr.head.ref = 'other-head'; }, + pr => { pr.head.sha = 'd'.repeat(40); }, + pr => { pr.head.sha = 'not-a-sha'; }, + pr => { pr.head.repo.id += 1; }, + pr => { pr.base.repo.id += 1; }, + pr => { delete pr.head.repo; }, + pr => { delete pr.base.repo.id; }, + ]; + for (const current of mutations) { + const { result, calls } = await evaluate({ current }); + assert.equal(result.trusted, false); + assert.equal(calls.filter(call => call.api === 'git.getTree').length, 0); + } +}); + +test('source or base movement while checking provenance cannot receive current-head credit', async () => { + for (const latest of [pr => { pr.head.sha = 'd'.repeat(40); }, + pr => { pr.base.sha = 'd'.repeat(40); }]) { + const { result } = await evaluate({ latest }); + assert.equal(result.trusted, false); + assert.match(result.reason, /changed|provenance/); + } +}); + +test('retargeting or repository/ref identity drift at unchanged SHAs is still a race', async () => { + for (const latest of [pr => { pr.base.ref = 'another-branch-at-same-sha'; }, + pr => { pr.head.ref = 'another-head-at-same-sha'; }, + pr => { pr.base.repo.id += 1; }, pr => { pr.head.repo.id += 1; }, + pr => { pr.base.repo.full_name = 'fork/bitcoin-keeper'; }, + pr => { pr.head.repo.full_name = 'fork/bitcoin-keeper'; }]) { + const { result } = await evaluate({ latest }); + assert.equal(result.trusted, false); + } +}); + +test('every workflow/harness addition, deletion, content, mode or type mutation denies trust in each snapshot', async () => { + for (const ref of [headSha, baseSha, runBaseSha]) { + for (const target of [...WORKFLOWS, ...NATIVE_FILES]) { + for (const mutation of ['content', 'delete', 'mode', 'type']) { + const tree = trustedTree(); + const index = tree.tree.findIndex(entry => entry.path === target); + if (mutation === 'content') tree.tree[index].sha = 'f'.repeat(40); + if (mutation === 'delete') tree.tree.splice(index, 1); + if (mutation === 'mode') tree.tree[index].mode = '100755'; + if (mutation === 'type') tree.tree[index].type = 'commit'; + const { result } = await evaluate({ trees: { [ref]: tree } }); + assert.equal(result.trusted, false, `${ref}:${target}:${mutation}`); + } + } + const tree = trustedTree(); + tree.tree.push({ path: '.github/native/unreviewed-helper.cjs', mode: '100644', + type: 'blob', sha: 'f'.repeat(40) }); + assert.equal((await evaluate({ trees: { [ref]: tree } })).result.trusted, false); + } +}); + +test('bootstrap is unavailable when any transitive workflow/native dependency is absent from every snapshot', async () => { + for (const missing of [...WORKFLOWS, ...NATIVE_FILES]) { + const tree = trustedTree(); + tree.tree = tree.tree.filter(entry => entry.path !== missing); + const { result } = await evaluate({ trees: { + [headSha]: tree, [baseSha]: tree, [runBaseSha]: tree, + } }); + assert.equal(result.trusted, false, missing); + } +}); + +test('required infrastructure symlinks cannot move executable dependencies outside the fingerprint', async () => { + for (const target of [...WORKFLOWS, ...NATIVE_FILES]) { + const tree = trustedTree(); + tree.tree.find(entry => entry.path === target).mode = '120000'; + const { result } = await evaluate({ trees: { + [headSha]: tree, [baseSha]: tree, [runBaseSha]: tree, + } }); + assert.equal(result.trusted, false, target); + } +}); + +test('truncated or unavailable GitHub trees fail closed with bounded public reasons', async () => { + for (const ref of [headSha, baseSha, runBaseSha]) { + const tree = trustedTree(); + tree.truncated = true; + for (const options of [{ trees: { [ref]: tree } }, { treeError: ref }]) { + const { result } = await evaluate(options); + assert.equal(result.trusted, false); + assert.doesNotMatch(result.reason, /Private API detail/); + } + } +}); + +test('fingerprinting is order independent and ordinary app changes are outside CI provenance', async () => { + const tree = trustedTree(); + tree.tree.reverse(); + assert.equal(fingerprint(tree), fingerprint(trustedTree())); + tree.tree.find(entry => entry.path === 'src/index.ts').sha = 'f'.repeat(40); + const { result } = await evaluate({ trees: { [headSha]: tree } }); + assert.equal(result.trusted, true); +}); + +test('two successful native jobs record only their implemented scope and never imply release readiness', () => { + const result = acceptance({ trusted: true, ios: 'success', android: 'success' }); + assert.equal(result.release_ready, false); + assert.equal(result.phase_a, 'BLOCKED'); + assert.deepEqual(result.cases.map(scenario => scenario.id), REQUIRED); + assert.deepEqual(result.cases.slice(0, 2).map(scenario => scenario.status), ['PASS', 'PASS']); + assert.ok(result.cases.slice(2).every(scenario => scenario.status === 'NOT IMPLEMENTED')); + assert.match(result.cases[1].scope, /not application account isolation/); + assert.match(result.signing, /NOT IMPLEMENTED.*NOT VERIFIED/); + assert.match(result.physical, /NOT RUN/); +}); + +test('failed, skipped, cancelled, pending or missing platform jobs provide no matrix credit', () => { + for (const value of [undefined, null, '', 'failure', 'skipped', 'cancelled', 'pending', + 'in_progress', 'neutral', 'SUCCESS', true]) { + for (const platform of ['ios', 'android']) { + const result = acceptance({ trusted: true, ios: 'success', android: 'success', [platform]: value }); + assert.ok(result.cases.slice(0, 2).every(scenario => scenario.status === 'NOT VERIFIED'), + `${platform}:${value}`); + assert.equal(result.release_ready, false); + assert.equal(result.phase_a, 'BLOCKED'); + } + } +}); + +test('only a boolean trusted preflight permits implemented native scope to receive PASS', () => { + for (const trusted of [undefined, null, false, 'true', 1, {}]) { + const result = acceptance({ trusted, ios: 'success', android: 'success' }); + assert.ok(result.cases.slice(0, 2).every(scenario => scenario.status === 'NOT VERIFIED')); + assert.equal(result.release_ready, false); + } +}); + +test('human-readable acceptance preserves all blocked scenarios and separate signing/physical requirements', () => { + const result = acceptance({ trusted: true, ios: 'success', android: 'success' }); + const text = markdown(result); + assert.match(text, /Phase A release acceptance: BLOCKED/); + assert.match(text, /cannot approve a release/); + for (const id of REQUIRED) assert.ok(text.includes(id)); + assert.match(text, /NOT IMPLEMENTED/); + assert.match(text, /exact distributed artifact required/); +}); + +test('Phase A caller and reusable worker expose no production-secret, protected-environment or privileged-event path', () => { + // The inherited #7060 workflow is independently reviewed; protect new Phase A execution here. + for (const filename of WORKFLOWS.filter(name => !name.endsWith('/keeper-hosted-native.yml'))) { + const source = fs.readFileSync(path.join(__dirname, '../..', filename), 'utf8') + .split('\n').filter(line => !line.trimStart().startsWith('#')).join('\n'); + // Static security policy: these constructs change the runner trust boundary. + assert.doesNotMatch(source, /^\s*(?:secrets|environment)\s*:/m, filename); + assert.doesNotMatch(source, /\bsecrets\s*\./, filename); + assert.doesNotMatch(source, /^\s*(?:pull_request_target|workflow_dispatch|repository_dispatch)\s*:/m, + filename); + assert.doesNotMatch(source, /^\s*[\w-]+:\s*write\s*$/m, filename); + assert.doesNotMatch(source, /\bfastlane\b|\bgh\s+release\b/, filename); + for (const match of source.matchAll(/^\s*(?:-\s*)?uses:\s*(\S+)/gm)) { + assert.ok(match[1].startsWith('./.github/workflows/') || /@[a-f0-9]{40}$/.test(match[1]), + `${filename}: unpinned dependency ${match[1]}`); + } + } +}); + +function probeMiddleware(enhanced) { + const seen = []; + const original = (request, response, next) => { + seen.push({ url: request.url, response, next }); + return 'served'; + }; + const base = { resolver: { preserved: true }, server: { preserveSetting: true } }; + let enhancementCalls = 0; + if (enhanced) base.server.enhanceMiddleware = (inner, server) => { + enhancementCalls += 1; + assert.equal(inner, original); + assert.equal(server.marker, true); + return (request, response, next) => original(request, response, next); + }; + const file = path.join(__dirname, '../native/release_metro.cjs'); + const context = { + __dirname: path.dirname(file), module: { exports: {} }, + require: name => { + if (name === '../../metro.config') return base; + if (name === 'node:path') return path; + throw new Error('Unexpected Metro dependency: ' + name); + }, + }; + vm.runInNewContext(fs.readFileSync(file, 'utf8'), context, { filename: file }); + const config = context.module.exports; + assert.equal(config.resolver, base.resolver); + assert.equal(config.server.preserveSetting, true); + assert.equal(config.projectRoot, path.resolve(__dirname, '../..')); + assert.equal(base.projectRoot, undefined); + return { middleware: config.server.enhanceMiddleware(original, { marker: true }), + seen, getEnhancementCalls: () => enhancementCalls }; +} + +test('CI Metro remaps only the native entrypoint while preserving query, source maps and base middleware', () => { + for (const enhanced of [true, false]) { + const state = probeMiddleware(enhanced); + const cases = [ + ['/index.bundle?platform=ios&dev=true', '/.github/native/release_probe.bundle?platform=ios&dev=true'], + ['/index.map?platform=android', '/.github/native/release_probe.map?platform=android'], + ['/index.bundle', '/.github/native/release_probe.bundle'], + ['/status', '/status'], + ['/nested/index.bundle?platform=ios', '/nested/index.bundle?platform=ios'], + ['/index.bundle.other?platform=ios', '/index.bundle.other?platform=ios'], + ['/another.bundle?platform=android', '/another.bundle?platform=android'], + ]; + for (const [url, expected] of cases) { + const response = {}; + const next = () => {}; + assert.equal(state.middleware({ url }, response, next), 'served'); + assert.deepEqual(state.seen.at(-1), { url: expected, response, next }); + } + assert.equal(state.getEnhancementCalls(), enhanced ? 1 : 0); + } +}); diff --git a/.github/tests/release-probe.test.cjs b/.github/tests/release-probe.test.cjs new file mode 100644 index 0000000000..5f702b7e1d --- /dev/null +++ b/.github/tests/release-probe.test.cjs @@ -0,0 +1,135 @@ +/* Host-only orchestration tests. This model supplies NO native platform evidence. */ +const assert = require('node:assert/strict'); +const { test } = require('node:test'); +const { runProbe, PHASE_KEY, MARKER_KEY, PRIMARY, OTHER, ROLLBACK } = require('../native/release_probe_core'); + +function fixture(bugs = {}) { + const disk = new Map(); + const values = new Map([['unrelated-storage-key', 'preserve']]); + const handles = new Set(); + const storage = { + getString: (key) => values.get(key), + setItem: (key, value) => values.set(key, value), + deleteItem: (key) => values.delete(key), + }; + const Realm = { + defaultPath: '/synthetic-device/default.realm', + async open(config) { + const key = config.encryptionKey[0]; + if (!disk.has(config.path)) disk.set(config.path, { key, wallets: new Map() }); + const file = disk.get(config.path); + if (key !== file.key && !bugs.acceptWrongKey) throw new Error('modeled wrong key'); + const handle = { + objectForPrimaryKey: (_table, id) => file.wallets.get(id), + objects: () => [...file.wallets.values()], + write(callback) { + const before = structuredClone(file.wallets); + try { + callback(); + } catch (error) { + if (!bugs.skipRollback) file.wallets = before; + throw error; + } + }, + create(_table, value) { file.wallets.set(value.id, structuredClone(value)); }, + delete(value) { if (!bugs.skipDelete) file.wallets.delete(value.id); }, + close() { handles.delete(handle); }, + }; + handles.add(handle); + return handle; + }, + deleteFile(config) { + if (bugs.cleanupFailure) throw new Error('modeled cleanup failure'); + disk.delete(config.path); + }, + }; + return { + options: { Realm, schema: [], storage }, disk, values, handles, + a: () => disk.get('/synthetic-device/keeper-ci-release-probe-v1-a.realm'), + }; +} + +test('three process phases require persisted state and clean only disposable fixture', async () => { + const state = fixture(); + assert.equal(await runProbe(state.options), 'seeded'); + assert.equal(state.handles.size, 0); + assert.equal(state.values.get(PHASE_KEY), 'seeded'); + assert.equal(await runProbe(state.options), 'mutated'); + assert.equal(state.handles.size, 0); + assert.equal(state.a().wallets.has(PRIMARY), false); + assert.equal(state.a().wallets.has(OTHER), true); + assert.equal(state.a().wallets.has(ROLLBACK), false); + assert.equal(await runProbe(state.options), 'pass'); + assert.equal(state.handles.size, 0); + assert.equal(state.disk.size, 0); + assert.deepEqual([...state.values], [['unrelated-storage-key', 'preserve']]); +}); + +test('missing persisted MMKV marker cannot progress after relaunch', async () => { + const state = fixture(); + await runProbe(state.options); + state.values.delete(MARKER_KEY); + await assert.rejects(runProbe(state.options), { code: 'mmkv-marker-not-persisted' }); + assert.equal(state.values.get(PHASE_KEY), 'seeded'); + assert.equal(state.handles.size, 0); +}); + +test('wrong-key acceptance fails closed before wallet deletion', async () => { + const state = fixture({ acceptWrongKey: true }); + await runProbe(state.options); + await assert.rejects(runProbe(state.options), { code: 'wrong-encryption-key-accepted' }); + assert.equal(state.a().wallets.has(PRIMARY), true); + assert.equal(state.handles.size, 0); +}); + +test('corruption of embedded wallet balances cannot pass', async () => { + const state = fixture(); + await runProbe(state.options); + state.a().wallets.get(OTHER).specs.balances.confirmed = 1; + await assert.rejects(runProbe(state.options), { code: 'embedded-wallet-state-not-persisted' }); + assert.equal(state.values.get(PHASE_KEY), 'seeded'); + assert.equal(state.handles.size, 0); +}); + +test('failed transaction rollback cannot progress', async () => { + const state = fixture({ skipRollback: true }); + await runProbe(state.options); + await assert.rejects(runProbe(state.options), { code: 'transaction-rollback-failed' }); + assert.equal(state.values.get(PHASE_KEY), 'seeded'); + assert.equal(state.handles.size, 0); +}); + +test('failed deletion cannot advance persistence marker', async () => { + const state = fixture({ skipDelete: true }); + await runProbe(state.options); + await assert.rejects(runProbe(state.options), { code: 'targeted-wallet-delete-failed' }); + assert.equal(state.values.get(PHASE_KEY), 'seeded'); + assert.equal(state.handles.size, 0); +}); + +test('lost deletion after second relaunch cannot pass', async () => { + const state = fixture(); + await runProbe(state.options); + const original = structuredClone(state.a().wallets.get(PRIMARY)); + await runProbe(state.options); + state.a().wallets.set(PRIMARY, original); + await assert.rejects(runProbe(state.options), { code: 'unexpected-primary-record-count' }); + assert.equal(state.values.get(PHASE_KEY), 'mutated'); + assert.equal(state.handles.size, 0); +}); + +test('fixture cleanup failure cannot produce pass', async () => { + const state = fixture({ cleanupFailure: true }); + await runProbe(state.options); + await runProbe(state.options); + await assert.rejects(runProbe(state.options), /modeled cleanup failure/); + assert.equal(state.values.get(PHASE_KEY), 'mutated'); + assert.equal(state.handles.size, 0); +}); + +test('unknown phase stops before accessing native databases', async () => { + const state = fixture(); + state.values.set(PHASE_KEY, 'unexpected'); + await assert.rejects(runProbe(state.options), { code: 'invalid-phase' }); + assert.equal(state.disk.size, 0); +}); diff --git a/.github/tests/test_release_native.py b/.github/tests/test_release_native.py new file mode 100644 index 0000000000..86e1779498 --- /dev/null +++ b/.github/tests/test_release_native.py @@ -0,0 +1,150 @@ +"""Host-only Phase A harness contracts; no native runtime or platform credit.""" +import contextlib +import importlib.util +from pathlib import Path +import tempfile +import unittest +from unittest import mock + + +SPEC = importlib.util.spec_from_file_location( + 'release_native', Path(__file__).resolve().parents[1] / 'native' / 'hosted_native.py') +native = importlib.util.module_from_spec(SPEC) +SPEC.loader.exec_module(native) + + +class ReleaseJunitTest(unittest.TestCase): + def setUp(self): + directory = tempfile.TemporaryDirectory() + self.addCleanup(directory.cleanup) + self.report = Path(directory.name) / 'probe.xml' + + def test_actual_success_cases_are_required(self): + self.report.write_text('' + '') + self.assertEqual(native.successful_junit(self.report), 1) + + def test_missing_empty_oversized_or_malformed_report_never_passes(self): + with self.assertRaises(RuntimeError): + native.successful_junit(self.report) + for content in ['', '', 'x' * (1024 * 1024 + 1), '<{tag}/>') + with self.assertRaises(RuntimeError): + native.successful_junit(self.report) + for status in ['ERROR', 'FAILED', 'SKIPPED', 'PENDING', '']: + with self.subTest(status=status): + self.report.write_text(f'') + with self.assertRaises(RuntimeError): + native.successful_junit(self.report) + + def test_declared_suite_failures_without_child_details_never_pass(self): + for field in ['failures', 'errors', 'skipped']: + with self.subTest(field=field): + self.report.write_text(f'') + with self.assertRaises(RuntimeError): + native.successful_junit(self.report) + + +class ReleaseNativeExecutionTest(unittest.TestCase): + def setUp(self): + directory = tempfile.TemporaryDirectory() + self.addCleanup(directory.cleanup) + self.root = Path(directory.name) + self.private = self.root / 'private' + self.private.mkdir() + patch = mock.patch.multiple(native, ROOT=self.root, PRIVATE=self.private, + EVIDENCE=self.root / 'metadata.json', PLATFORM='ANDROID') + patch.start() + self.addCleanup(patch.stop) + self.identity = {'sha256': 'synthetic-artifact-identity'} + native.save({'commands': [], 'build': 'PASS', 'smoke': 'PASS', 'native_probe': 'NOT RUN', + 'built_artifact': self.identity, 'maestro': {'binary': 'synthetic-maestro'}, + 'toolchain': {}}) + + def exercise(self, after_identity=None, maestro_failure=False, report_failure=False, report_missing=False): + def command(args, label, **kwargs): + if '--format' in args: + if maestro_failure: + raise RuntimeError('PRIVATE_SENTINEL native failure') + if not report_missing: + (self.private / 'probe-junit.xml').write_text( + '' + + ('' if report_failure else '') + '') + return 'synthetic-device' + + process = mock.Mock(pid=12345) + process.poll.return_value = None + response = mock.MagicMock() + response.__enter__.return_value.read.return_value = b'packager-status:running' + stack = contextlib.ExitStack() + self.addCleanup(stack.close) + stack.enter_context(mock.patch.object(native.subprocess, 'check_call')) + stack.enter_context(mock.patch.object(native.subprocess, 'Popen', return_value=process)) + stack.enter_context(mock.patch.object(native.subprocess, 'run')) + stack.enter_context(mock.patch.object(native.os, 'killpg')) + stack.enter_context(mock.patch.object(native.urllib.request, 'urlopen', return_value=response)) + stack.enter_context(mock.patch.object(native, 'command', side_effect=command)) + stack.enter_context(mock.patch.object(native, 'warm_metro_bundle')) + stack.enter_context(mock.patch.object(native, 'android_sdk_metadata', return_value={})) + stack.enter_context(mock.patch.object(native, 'artifact_identity', + side_effect=[self.identity, after_identity or self.identity])) + native.run_phase('probe') + + def test_probe_requires_actual_app_smoke_first(self): + data = native.read() + data['smoke'] = 'NOT RUN' + native.save(data) + with mock.patch.object(native.subprocess, 'check_call'), \ + mock.patch.object(native.subprocess, 'Popen') as popen: + with self.assertRaisesRegex(RuntimeError, 'app startup smoke'): + native.run_phase('probe') + popen.assert_not_called() + self.assertEqual(native.read()['native_probe'], 'FAIL') + + def test_changed_artifact_before_probe_stops_before_runtime(self): + with mock.patch.object(native.subprocess, 'check_call'), \ + mock.patch.object(native, 'artifact_identity', return_value={'sha256': 'changed'}), \ + mock.patch.object(native.subprocess, 'Popen') as popen: + with self.assertRaisesRegex(RuntimeError, 'artifact changed'): + native.run_phase('probe') + popen.assert_not_called() + self.assertEqual(native.read()['native_probe'], 'FAIL') + + def test_probe_success_is_distinct_from_application_acceptance(self): + self.exercise() + data = native.read() + self.assertEqual(data['native_probe'], 'PASS') + self.assertEqual(data['native_probe_test_cases'], 1) + self.assertIn('NOT app key deletion', data['probe_scope']) + self.assertNotIn('release_ready', data) + + def test_artifact_change_during_execution_cannot_pass(self): + with self.assertRaisesRegex(RuntimeError, 'artifact changed during'): + self.exercise(after_identity={'sha256': 'changed'}) + self.assertEqual(native.read()['native_probe'], 'FAIL') + + def test_missing_new_report_cannot_reuse_stale_success(self): + (self.private / 'probe-junit.xml').write_text('') + with self.assertRaisesRegex(RuntimeError, 'Missing or oversized'): + self.exercise(report_missing=True) + self.assertEqual(native.read()['native_probe'], 'FAIL') + + def test_failed_native_command_or_report_never_exports_raw_data_or_passes(self): + for command_failure in [True, False]: + with self.subTest(command_failure=command_failure): + with self.assertRaises(RuntimeError): + self.exercise(maestro_failure=command_failure, report_failure=not command_failure) + self.assertEqual(native.read()['native_probe'], 'FAIL') + self.assertNotIn('PRIVATE_SENTINEL', native.EVIDENCE.read_text()) + + +if __name__ == '__main__': + unittest.main() diff --git a/.github/workflows/keeper-native-platform.yml b/.github/workflows/keeper-native-platform.yml new file mode 100644 index 0000000000..08e14b6986 --- /dev/null +++ b/.github/workflows/keeper-native-platform.yml @@ -0,0 +1,121 @@ +name: Keeper reusable native platform + +on: + workflow_call: + inputs: + source_sha: + type: string + required: true + platform: + type: string + required: true + +# This worker has no environment, release credentials, secret inputs or dispatch. +permissions: + contents: read + +jobs: + native: + name: Native ${{ inputs.platform }} build and probes + if: github.event_name == 'pull_request' && github.event.pull_request.number != 7045 && inputs.source_sha == github.event.pull_request.head.sha + runs-on: ${{ inputs.platform == 'IOS' && 'macos-15' || 'ubuntu-24.04' }} + timeout-minutes: 75 + env: + CI: 'true' + HUSKY: '0' + KEEPER_SOURCE_SHA: ${{ inputs.source_sha }} + KEEPER_NATIVE_PLATFORM: ${{ inputs.platform }} + MAESTRO_CLI_NO_ANALYTICS: '1' + steps: + - name: Check out exact app source + uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6 + with: + ref: ${{ inputs.source_sha }} + persist-credentials: false + - name: Set up Node + uses: actions/setup-node@249970729cb0ef3589644e2896645e5dc5ba9c38 # v6 + with: + node-version: '22.23.3' + cache: yarn + - name: Set up Java + uses: actions/setup-java@b6effb05e454b25005698d916606bdc6ffcbf961 # v5 + with: + distribution: temurin + java-version: '17' + cache: gradle + - name: Set up Ruby + if: inputs.platform == 'IOS' + uses: ruby/setup-ruby@675dd7ba1b06c8786a1480d89c384f5620a42647 # v1.281.0 + with: + ruby-version: '3.3.8' + - name: Select reviewed Xcode + if: inputs.platform == 'IOS' + run: echo 'DEVELOPER_DIR=/Applications/Xcode_16.4.app/Contents/Developer' >> "$GITHUB_ENV" + - name: Set up Android command-line tools + if: inputs.platform == 'ANDROID' + uses: android-actions/setup-android@be39fa834029ff78f1a44aa3bb0819b8fc2bd8fd # v4 + with: + cmdline-tools-version: '13114758' + packages: platform-tools + log-accepted-android-sdk-licenses: false + - name: Record runner and toolchain + run: python3 .github/native/hosted_native.py record + - name: Install immutable native dependencies + run: python3 .github/native/hosted_native.py prepare + - name: Check disposable iOS driver readiness + if: inputs.platform == 'IOS' + run: | + test "$(git rev-parse HEAD)" = '${{ inputs.source_sha }}' + git diff --exit-code HEAD -- .github/native .github/workflows + python3 .github/native/hosted_native.py driver + - name: Build development app + run: | + test "$(git rev-parse HEAD)" = '${{ inputs.source_sha }}' + git diff --exit-code HEAD -- .github/native .github/workflows + python3 .github/native/hosted_native.py build + - name: Run disposable startup smoke + if: inputs.platform == 'IOS' + run: | + test "$(git rev-parse HEAD)" = '${{ inputs.source_sha }}' + git diff --exit-code HEAD -- .github/native .github/workflows + python3 .github/native/hosted_native.py smoke + - name: Run disposable native persistence probe + if: inputs.platform == 'IOS' + run: | + test "$(git rev-parse HEAD)" = '${{ inputs.source_sha }}' + git diff --exit-code HEAD -- .github/native .github/workflows + python3 .github/native/hosted_native.py probe + - name: Enable emulator acceleration + if: inputs.platform == 'ANDROID' + run: | + echo 'KERNEL=="kvm", GROUP="kvm", MODE="0666", OPTIONS+="static_node=kvm"' | sudo tee /etc/udev/rules.d/99-keeper-kvm.rules + sudo udevadm control --reload-rules + sudo udevadm trigger --name-match=kvm + - name: Run Android startup and native persistence probes + if: inputs.platform == 'ANDROID' + uses: reactivecircus/android-emulator-runner@1dcd0090116d15e7c562f8db72807de5e036a4ed # v2.34.0 + with: + api-level: 35 + target: google_apis + arch: x86_64 + profile: pixel_6 + cores: 2 + ram-size: 3072M + force-avd-creation: true + disable-animations: true + emulator-boot-timeout: 240 + emulator-options: -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim -no-snapshot -camera-back none -camera-front none + script: | + test "$(git rev-parse HEAD)" = '${{ inputs.source_sha }}' + git diff --exit-code HEAD -- .github/native .github/workflows + python3 .github/native/hosted_native.py smoke + git diff --exit-code HEAD -- .github/native .github/workflows + python3 .github/native/hosted_native.py probe + - name: Retain safe Phase A evidence + if: always() + uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6 + with: + name: keeper-release-native-${{ inputs.platform }}-${{ inputs.source_sha }} + path: artifacts/native/*/metadata.json + if-no-files-found: error + retention-days: 7 diff --git a/.github/workflows/keeper-release-acceptance.yml b/.github/workflows/keeper-release-acceptance.yml new file mode 100644 index 0000000000..06055cb941 --- /dev/null +++ b/.github/workflows/keeper-release-acceptance.yml @@ -0,0 +1,83 @@ +name: Keeper release acceptance + +on: + pull_request: + branches: [sprint] + types: [opened, synchronize, reopened, ready_for_review] + +permissions: + contents: read + pull-requests: read + actions: read + +concurrency: + group: keeper-release-acceptance-${{ github.event.pull_request.number }} + cancel-in-progress: true + +jobs: + provenance: + name: Reviewed CI bootstrap and exact-head provenance + # Freeze the existing R4 release and its gate, even after future CI installation. + if: github.event.pull_request.number != 7045 + runs-on: ubuntu-24.04 + timeout-minutes: 5 + outputs: + trusted: ${{ steps.check.outputs.trusted }} + steps: + - uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6 + with: + ref: ${{ github.event.pull_request.head.sha }} + persist-credentials: false + - id: check + uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8 + with: + script: | + const { preflight } = require('./.github/native/release_preflight.cjs'); + const result = await preflight(github, context); + core.setOutput('trusted', String(result.trusted)); + await core.summary.addHeading('Phase A provenance').addRaw(result.reason).write(); + + ios: + needs: provenance + if: needs.provenance.outputs.trusted == 'true' + uses: ./.github/workflows/keeper-native-platform.yml + with: + source_sha: ${{ github.event.pull_request.head.sha }} + platform: IOS + + android: + needs: provenance + if: needs.provenance.outputs.trusted == 'true' + uses: ./.github/workflows/keeper-native-platform.yml + with: + source_sha: ${{ github.event.pull_request.head.sha }} + platform: ANDROID + + acceptance: + name: Phase A release acceptance (incomplete matrix blocks) + needs: [provenance, ios, android] + if: always() && github.event.pull_request.number != 7045 + runs-on: ubuntu-24.04 + timeout-minutes: 5 + steps: + - uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6 + with: + ref: ${{ github.event.pull_request.head.sha }} + persist-credentials: false + - uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8 + env: + IOS_RESULT: ${{ needs.ios.result }} + ANDROID_RESULT: ${{ needs.android.result }} + with: + script: | + const { preflight } = require('./.github/native/release_preflight.cjs'); + const { acceptance, markdown } = require('./.github/native/release_acceptance.cjs'); + const { evidence } = require('./.github/native/release_evidence.cjs'); + const verified = await preflight(github, context); + const currentAttempt = verified.trusted && await evidence(github, context, { + ios: process.env.IOS_RESULT, android: process.env.ANDROID_RESULT, + attempt: Number(process.env.GITHUB_RUN_ATTEMPT) }); + const result = acceptance({ trusted: currentAttempt, + ios: process.env.IOS_RESULT, android: process.env.ANDROID_RESULT }); + await core.summary.addRaw(markdown(result)).write(); + core.setFailed('BLOCKED: full app acceptance is NOT IMPLEMENTED / NOT VERIFIED; no release approval'); diff --git a/AGENTS.md b/AGENTS.md index 871210bf4b..9f8628aa9b 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -4,6 +4,40 @@ The prepared workflow is **Cloud code complete → hosted native validation → Repository: `KeeperCommunity/bitcoin-keeper`. Use `sprint` as the PR base unless the task explicitly specifies another base. +## R5 and future releases: permanent cloud-first default + +Follow [the cloud release pipeline](docs/cloud-release-pipeline.md) for R5 and every +subsequent release. Complete code and representative native acceptance on hosted +iOS simulators/Android emulators, then assign remaining real-device checks to a +named authorized tester. The release owner's personal Mac is not the default +handoff destination. Keep the existing Local path below as the safe fallback +until each hosted capability is reviewed, installed and verified; its current +`Codex local handoff` / `State: NEEDS_LOCAL` syntax remains the gate protocol, not +a claim that a Mac session is always required. In that handoff, put hosted work +and run URLs in `Cloud checks`, and set `Local checks requested: NONE` only when +trusted hosted evidence satisfies all requested native checks and no physical +checks remain. Otherwise name the actual remaining checks and who can run them. + +Bootstrap depends on reviewed [PR #7060](https://github.com/KeeperCommunity/bitcoin-keeper/pull/7060); +check its current state. The initial Phase A candidate is stacked on that PR. +Its reusable `keeper-native-platform.yml` workflow is called by +`keeper-release-acceptance.yml` automatically for future PRs targeting `sprint`; +there is no R5-only release selector or manual copying step. The active R4 PR +#7045 is excluded. Do not change R4 source, refs, private fixtures, Mac work, +required gate or approval process. Installing #7060 and this infrastructure on +`sprint` requires independent review and owner approval. Diagnostic candidate +runs are not trusted acceptance for R5 or another source SHA. + +Record the exact source SHA, workflow run/attempt, both platforms' actual +build/UI outcomes, safe artifact hashes and unresolved feature acceptance. +A startup smoke or representative probe does not prove R5's deletion, recovery, +upgrade or security matrix. R4-dependent app checks wait for R4 baseline +reconciliation; unresolved high-risk cases remain blocking. Phase B protected +signing and distribution are **NOT IMPLEMENTED / NOT VERIFIED** until their +separate reviewed workflow and exact-artifact controls are proven. Do not merge, +sign, upload a beta, submit to a store or publish without explicit owner approval +for that action and artifact. Passing CI provides none of these approvals. + ## Codex Cloud: implement and prepare handoff 1. Work in a task-specific branch; investigate, implement, add or update tests, and run suitable checks (for example, `npm run test` and `npm run lint`). Report actual pass/fail results; do not claim a native simulator was run unless it really was. diff --git a/docs/cloud-release-pipeline.md b/docs/cloud-release-pipeline.md new file mode 100644 index 0000000000..387bf3c353 --- /dev/null +++ b/docs/cloud-release-pipeline.md @@ -0,0 +1,191 @@ +# Permanent cloud release pipeline + +[Issue #7061](https://github.com/KeeperCommunity/bitcoin-keeper/issues/7061) +establishes the default process for R5 and every later Keeper release: cloud code +completion, hosted native acceptance, authorized physical testing, independent +review, and separately approved signing/distribution/publication. R5 is the first +rollout, not a special workflow. The target is zero ongoing dependency on the +release owner's personal Mac; hosted iOS jobs still use macOS and physical +checks still need suitable phones/signers and authorized people. + +This document describes the Phase A candidate and the separate Phase B plan. +It does not grant merge, signing, upload, submission or publication approval. + +## Current capability and adoption + +Status snapshot: 2026-10-18. Recheck linked PRs and current trusted workflows at +each handoff; these initial blockers are not evidence about a later head. + +| Capability | Current state and evidence boundary | +| --- | --- | +| Hosted development builds and account-free startup smoke | #7060 has actual iOS and Android build/UI passes at `d865ca9c4f91532a1524443cc6da42d8d7b07e1a`, run [39024530330](https://github.com/KeeperCommunity/bitcoin-keeper/actions/runs/39024530330). It remains an unmerged draft. That evidence belongs only to its tested source. | +| Reusable Phase A release acceptance | Candidate infrastructure stacked on #7060. Its local contract checks are distinct from actual hosted builds/UI execution. R5 native and release acceptance remain **NOT VERIFIED** until R5's own exact head runs after trusted installation and baseline reconciliation. | +| R5 feature/security matrix | **BLOCKED / NOT VERIFIED** until the relevant R4 baseline is reconciled and R5's own deletion/reference, setup/persistence, account-isolation and recovery/regression cases actually run. Representative probes do not waive missing or failed cases. | +| Protected signed beta and store delivery | Phase B: **NOT IMPLEMENTED / NOT VERIFIED**. Existing Fastlane lanes are not a validated cloud release workflow. | +| Physical phone/signer acceptance | **NOT VERIFIED** without named authorized tester reports for the exact approved distributed artifact. Hosted simulators/emulators supply no physical credit. | + +The Phase A action-pin contract also found a malformed inherited Android +emulator-action reference in #7060. The new worker uses the verified v2.34.0 +commit; resolve/review the dependency reference in #7060 before installation. +Its historical successful run is not proof that the malformed literal works. + +The active R4 PR [#7045](https://github.com/KeeperCommunity/bitcoin-keeper/pull/7045) +and its existing process remain untouched. Do not update its source, refs, +fixtures, Mac work, gate or approvals to adopt this pipeline. Do not transfer +#7060's or this candidate's results to R4 or +[R5 #7058](https://github.com/KeeperCommunity/bitcoin-keeper/pull/7058). + +Before adoption, independently review #7060's workflow, dependency and iOS-native +changes against R4's eventual released baseline. Obtain owner approval before +merging it. Then review the separate Phase A candidate and install its reviewed +workflows on `sprint` only with approval. A stacked candidate can test its +infrastructure, but cannot certify its own untrusted CI or bypass the currently +required `keeper/local-validation` and `Tests` checks. Preserve the existing safe +Local fallback until the applicable hosted capability is proven. + +## Phase A: reusable hosted acceptance + +The shared platform job is `.github/workflows/keeper-native-platform.yml` +(`workflow_call`); `.github/workflows/keeper-release-acceptance.yml` invokes it for +PRs targeting `sprint`. The trigger applies to future release/app PRs without a +version-specific selector or manual workflow copy. The explicit active-R4 #7045 +exclusion prevents this rollout from starting new work on that release. + +Each run must use the full current PR head SHA and a disposable development +configuration, with read-only repository access, no persisted checkout credential +and no production signing/service secrets. Native builds execute PR-controlled +source on disposable runners; independent source/CI review remains necessary. +Reviewed workflow/harness provenance and actual step outcomes must be checked +before treating a run as trusted evidence. Changing CI infrastructure requires +review before it supplies authoritative acceptance. + +Preflight compares the caller, shared worker, existing #7060 workflow and the +complete native harness against the exact app source, the current `sprint` base +and the event's original base. The summary also verifies the newest Actions run, +its exact attempt, PR association and actual native job/step results through the +read-only Actions API. A partial rerun cannot reuse an earlier attempt's pass. +Required executable files must exist as ordinary +files in all three snapshots. Missing installation, changed CI, fork provenance +or a concurrent source/base change supplies no native credit. Consequently this +stacked candidate **skips its new native jobs** before reviewed installation. +The full acceptance summary deliberately fails even if both primitive probe jobs +pass: five required app scenarios are still **NOT IMPLEMENTED**. Extending those +scenarios requires executable tests and review, not changing a status/waiver. + +The bounded representative matrix extends #7060's startup smoke with a separate +development-only probe entrypoint using real native Realm/MMKV modules and the +current app schema. Across three process launches it seeds synthetic wallet +records, checks persistence and separate Realm-file isolation, rejects a wrong +Realm encryption key, checks transaction rollback and targeted Realm deletion, +then verifies persistence and deletes its disposable state. Its wallet records +contain an invalid public sentinel, with no fundable keys. This verifies native +storage primitives, not product key deletion/reference protections, app account +isolation, setup/retry behavior, recovery, upgrade or release performance. + +Use only job-created throwaway state: no Mac accounts, keystores, private signer +material, historical fixtures, funding or live signing. The summary must fail +full acceptance when required R5 app/security cases are missing, blocked or +failed. Keep the matrix small enough to fit the hosted job budget; expand +coverage only for a concrete release risk. + +For every release, the Cloud handoff records: + +- Full PR head SHA and PR URL; required `IOS`, `ANDROID` and, when applicable, + `HARDWARE` checks. +- Workflow run URL/attempt and actual build, startup, integration and timing + outcomes for each platform: `PASS`, `FAIL`, `NOT RUN` or `BLOCKED`. +- Safe artifact identity/hashes and the exact scenarios exercised, with missing + feature/security cases and failures kept explicit. +- Remaining physical checks, assigned tester and any required owner decision. + +Use the existing **Codex local handoff**, `State: NEEDS_LOCAL` and `Required local +checks` fields in [AGENTS.md](../AGENTS.md). These are the current gate's protocol; +the default execution destination for R5/future native work is hosted CI. +`Local checks requested: NONE` is appropriate only when trusted hosted evidence +satisfies all required native checks and no physical checks remain. Otherwise +describe the actual fallback or physical work. Do not invent a passing Local +report to represent a hosted run. + +A new head commit invalidates previous native evidence and owner scope decisions. +Rerun both platforms on that head. Retain bounded safe metadata and diagnostic +outcomes on failure; do not upload raw logs, credentials, generated account data, +screenshots or development app bundles just to document a pass. A development +artifact hash is not a signed beta identity. Follow the trust, bootstrap, runner +budget and evidence rules in [hosted native validation](hosted-native-validation.md). + +### Staged runner budget + +The current R4 gate and #7060 startup workflow stay unchanged. During staged +adoption, each eligible app PR push can therefore run the original startup pair +as well as the new build/probe pair. The original caps are 65 iOS / 60 Android +minutes; the new shared worker caps each platform at 75 minutes, builds once and +runs startup plus native storage probes. Neither has automatic retries. The +combined ceiling is 275 runner-minutes per push, approximately $9.49 if billable +at the reference rates in [hosted validation](hosted-native-validation.md), before +allowances/storage/other CI. This is a planning ceiling, not observed runtime or +an account bill. The uninstalled candidate starts no new native jobs. + +Consolidating those platform builds and their trusted gate consumer needs a later +reviewed change after the R4 noninterference constraint permits it. Do not alter +R4's current gate or discard failed evidence to reduce this rollout's cost. + +## Phase B: separate protected delivery workflow + +**NOT IMPLEMENTED / NOT VERIFIED.** Deliver this in a separate PR with a short +scope estimate before implementation. Keep Phase A focused; split or defer work +if a phase approaches approximately 20,000 changed code/test tokens or contains +two substantial independent features. + +Review the existing `ios/fastlane/Fastfile` and `android/fastlane/Fastfile` before +choosing hosted macOS/Linux delivery, or Xcode Cloud for iOS. Inventory these +prerequisites with the owner; record credential owners and availability without +copying secret values into issues, PRs, logs or artifacts: + +| Area | Setup to resolve before signing/distribution | +| --- | --- | +| iOS | Apple team/app identifiers, distribution certificate/private-key custody, provisioning profiles, App Store Connect upload authorization, TestFlight groups and approved channel mappings. | +| Android | Package identity, existing upload-key custody and alias, Play App Signing status, least-privileged Play service account, internal-test track and tester access. Never regenerate an upload key without authorization. | +| Both | Reviewed production configuration, immutable commit/version/build identity, fresh store histories/counters, protected environments and approvers, retained artifact hashes/signatures/provenance and distribution receipts. | + +Audit tracked native signing configuration for embedded credential fields without +printing their values. If values may be live, use the project's private security +process and plan replacement/rotation with the owner. Do not assume tracked +values are valid, usable or safe to reuse. Existing Fastlane build-number +mutation and immediate upload behavior must be reviewed for exact-artifact and +approval controls before being used remotely. + +The release-only workflow must accept trusted reviewed release source and bind +its exact commit, version, build and channel. Production secrets belong only in +least-privileged protected environments, never a PR job. Require explicit owner +approval before signing and before any TestFlight/Play internal upload or other +distribution. Preserve the signed artifact's hash, signature identity, provenance +and upload receipt; testers and publication decisions must refer to that same +artifact. Require separate explicit owner approval for external submission and +public publication. Never treat a passing build, gate or beta test as approval. + +One-time account/provisioning/key-custody setup may require the owner. Describe +that setup honestly; it is separate from an ongoing requirement to unlock the +owner's Mac for each release. + +## Physical checks and final review + +Assign a named trusted tester/maintainer, or a securely operated device facility +for supported cases, to install the approved TestFlight/Play internal artifact. +Record the artifact hash/build/channel and head SHA, tester, phone OS/model, +signer model/firmware, requested scenarios, actual outcomes and unresolved cases. +Exercise real biometrics, cameras, NFC/USB/BLE, hardware signers and relevant +policy/recovery behavior on the devices that support them. A simulator report +cannot stand in for those results. + +Use owner-approved physical scope and retain [the existing exact-SHA HARDWARE +deferral and critical-defect restrictions](../AGENTS.md#owner-directed-post-release-test-scope). +A failed check or high-risk issue involving keys, recovery, funds, backups or +incorrect signing stays blocking. Owner deferral must remain honest about +unrun cases and cannot waive a required failed platform or physical result. + +Independent review, feature/security acceptance, artifact/version checks and +explicit per-release approvals remain separate from CI. Report each unproven +capability as **NOT IMPLEMENTED / NOT VERIFIED**, preserve current safe fallback, +and state the next concrete action. Future releases reuse these reviewed shared +workflows; only their source, versions, relevant acceptance cases, artifact and +approvals change. From e4245b01af4f48d3812f8e2abcae8bcfe65ae06a Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Thu, 22 Oct 2026 06:06:35 +0000 Subject: [PATCH 215/254] docs(r5): record isolated integration scope and source continuity --- docs/r5-cloud-integration.md | 79 ++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 docs/r5-cloud-integration.md diff --git a/docs/r5-cloud-integration.md b/docs/r5-cloud-integration.md new file mode 100644 index 0000000000..2785c0c9be --- /dev/null +++ b/docs/r5-cloud-integration.md @@ -0,0 +1,79 @@ +# R5 cloud integration and acceptance record + +This is an isolated candidate, not release approval. Ordinary development and +native testing use Cloud, including hosted macOS; the owner's personal Mac is +not a prerequisite. No producer branch or shared branch is rewritten. + +## Source continuity + +Observed 22 October 2026. Recheck producer heads before candidate freeze. + +| Input | Verified commit | Integration | +| --- | --- | --- | +| Released R4 | `1d8a86a7462d935cbd0e28b857934b5718c152fc` | Ancestor of core and 5.1; no further R4 confirmation required | +| Core #7058, `codex/r5-key-deletion-startup` | `77b72da29ac5f2cdf3f51c9a0e9f3fd1c2af7238` | Included by ancestry | +| 5.1 #7067, `codex/7066-protected-backup-correction` | `c1c4b803039b61b940373c4dc9d08473cf25e042` | Integration starts here; includes backup and Version History corrections | +| 5.2 | Identity and authoritative checkpoint unresolved | Owner reports running; do not infer identity/completion or duplicate its implementation | + +Patch-only review links remain separate: + +- [Core changes after released R4](https://github.com/KeeperCommunity/bitcoin-keeper/compare/1d8a86a7462d935cbd0e28b857934b5718c152fc...77b72da29ac5f2cdf3f51c9a0e9f3fd1c2af7238) +- [5.1 changes after core](https://github.com/KeeperCommunity/bitcoin-keeper/compare/77b72da29ac5f2cdf3f51c9a0e9f3fd1c2af7238...c1c4b803039b61b940373c4dc9d08473cf25e042) + +5.1 contains exactly two commits after core: protected-backup correction +`3c5f8d8ace5c211ed78264b1a460c4e06e060ceb`, then Version History correction +`c1c4b803039b61b940373c4dc9d08473cf25e042`. Package names 5.1 and 5.2 do not +assign app versions. Existing native inputs remain 2.6.4 / Android627 / iOS620; +the recorded R5 version intent does not establish new build counters. + +## Approved scope and package boundaries + +Master Task List Backlog245 and relevant R05 rows select safe discoverable key +deletion, wallet-reference protections and startup/first-use reliability; +bounded navigation/timezone reproduction; PIN/biometric verification; Emergency +Key and Assisted Server Backup copy. Later owner additions select protected +backup/Version History correction, cloud testing, bounded signing preparation, +PHP exposure research (remediation belongs to R6), applicable Claude review and +the separate read-only timelock-assessor candidate with its remaining safeguards. + +Core and 5.1 already total roughly 35k changed code/test tokens. This integration +PR assembles reviewed inputs and evidence. Further implementation stays in +separate patch packages: CI reconciliation, executable native feature scenarios, +confirmed biometric callback correction if reproduced, current-protocol witness +repair if needed, and protected-release/non-app readiness. Re-estimate any package +approaching 20k changed code/test tokens or more than two substantial features. + +Excluded scope stays excluded: all-copy legacy backup purge, parked #7056 second +POST investigation, storage/passcode redesign, Receive QR, broad navigation, +terminology/scaling redesign and Assisted Multisig implementation. Preserve the +existing assessor candidate; do not infer that it is 5.2. + +## Acceptance rules + +- An implementation checkpoint is not native acceptance or backend compatibility. +- Tests and artifact evidence belong to the exact tested checkout. Never transfer + results from infrastructure-only source or an earlier candidate SHA. +- Run disposable current-protocol backup, recovery, deletion and account-isolation + checks. Do not operate production accounts. +- Native acceptance requires feature checks and at least five raw startup and + first-wallet observations, not only a startup/passcode smoke. +- Real biometrics, phones and signers require actual devices/testers. Emulator + evidence cannot satisfy them. +- Hosted diagnostics of unmerged infrastructure cannot satisfy a gate that + requires reviewed workflow ancestry. Keep trusted gates fail-closed. +- Signing, upload, distribution, deployment, publication, merges and shared-branch + promotion require separate explicit owner approval. No such action is performed. + +## Inherited evidence and unresolved findings + +The original core and 5.1 Tests/public-backend jobs pass on their recorded heads. +Their old genuine automatic-backup matrix fails; the historical fixture's result +must remain visible alongside the current-v3 witness. CodeQL analysis execution +passes, while the separate security result fails on an inherited high-severity +incomplete sanitization finding at `src/services/wallets/operations/index.ts`. +No independent review or severity waiver is established. Lint jobs tolerate +command failures; green jobs do not mean lint is clean. + +At initial integration there is no combined native acceptance, no five raw +measurements and no identified 5.2 checkpoint. Candidate freeze is blocked until +the authoritative 5.2 handoff is rechecked and all package results are recorded. From bc9244bda22a872e06bb28aa0a04936472d8485f Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Thu, 22 Oct 2026 06:11:36 +0000 Subject: [PATCH 216/254] docs(r5): record current disposable protected protocol results --- docs/r5/PROTOCOL-ACCEPTANCE-20261022.md | 101 ++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 docs/r5/PROTOCOL-ACCEPTANCE-20261022.md diff --git a/docs/r5/PROTOCOL-ACCEPTANCE-20261022.md b/docs/r5/PROTOCOL-ACCEPTANCE-20261022.md new file mode 100644 index 0000000000..c441a0b31d --- /dev/null +++ b/docs/r5/PROTOCOL-ACCEPTANCE-20261022.md @@ -0,0 +1,101 @@ +# R5 disposable protected-protocol acceptance — 22 October 2026 + +This receipt covers the actual source and commands below. It does not establish +native app acceptance, the original phone's backup state, or release readiness. +No production endpoint, account, configuration, credential or deployment was +used or changed. + +## Exact source and pairing + +- App source: `c1c4b803039b61b940373c4dc9d08473cf25e042`, the completed + [5.1 package](https://github.com/KeeperCommunity/bitcoin-keeper/pull/7067). + It contains R5 core `77b72da29ac5f2cdf3f51c9a0e9f3fd1c2af7238` and released + R4 `1d8a86a7462d935cbd0e28b857934b5718c152fc` by verified ancestry. +- Relay: `29597b27eeeb299205bf7d0555db0a90c38cc4be`, verified against the + current PR158 head and its [latest owner receipt](https://github.com/KeeperCommunity/bitcoin-keeper-relay/pull/158#issuecomment-6321195858). + The PR description's earlier `7693ce4b` is stale. The current source includes + the driver-3 collection-readiness correction. All twelve inputs pinned by + `scripts/run-protected-upgrade-regression.cjs` match this clean checkout. +- Public SigningServer source remains + `130d74b5336c40901933d4882db745f0cca811f1`. Its merged source work and the + unmerged lock-only PR6 (`df9485e9945cdf0bdc231c70329d8905125d42ff`) are distinct + from deployed service identity. The protected-backup fixture does not invoke + SigningServer; these results do not attest that service's hosted runtime. + +The historical `run-protected-backup-genuine.cjs` pin remains unchanged. Its +`7693ce4b` source result must not be relabeled as a current-source pass. + +## Actual environment and checks + +Node `22.23.3`, Yarn `1.22.22`, frozen Relay dependencies, TypeScript `5.4.2`, +Mongoose `5.13.20`, and native Mongo driver `3.7.4` were used. MongoDB `7.0.16` +runs in a disposable `keeperQA` replica set, published only at +`127.0.0.1:27038`, with no credentials. The image is pinned to +`mongo@sha256:c630c59342c1493d50345136df2af14a76b9e827dd5316bfabee07a0880a5f3a`. +Provider and full-server bootstrap imports are blocked by the fixture. + +The backend fixture verifies the exact app AES source hash. Its pure AES +entry points use the app-declared `crypto-js@4.2.0`, installed in an independent +non-secret tool directory while the full app dependency install proceeds. +This is backend/crypto interoperability evidence, not a native app run. + +| Check | Actual result | +| --- | --- | +| Current Relay protected/legacy/readiness suite | **52/52 PASS**, zero failures/skips/cancellations, 19.239 seconds: 39 protected, 6 retained-legacy-read, 7 readiness | +| Current Relay TypeScript `--noEmit` | **PASS**, exit 0, no diagnostics | +| Existing app protected-runner guard tests | **3/3 PASS**, zero failures/skips | +| First backend attempt before AES dependency was available | **13 PASS / 39 hook failures**, retained; missing `crypto-js` prevented protected test bodies from running | +| Current app encrypted-Realm integration proofs | **BLOCKED locally**: actual normal N-API install exits 1 with proxy CONNECT 403; hosted fixed-pair route prepared separately, no app proof pass claimed | + +The successful backend suite exercises real signed HTTP, native signature +verification and transactional Mongo state: enrollment; read-only absence; +all seven mutation operations; full encrypted readback; account/namespace +isolation and tamper denial; original CAS/generation/epoch constraints; +concurrent and lost-acknowledgement cases; rollback; protected-only deletion, +tombstone and explicit recreation; retained legacy recovery reads and hard +legacy-writer fences. Readiness tests reject missing/wrong runtime identity. + +Commands, run in the exact Relay checkout: + +```sh +yarn install --frozen-lockfile --non-interactive --ignore-scripts +npm ci --ignore-scripts --prefix tests/proof-tools +NODE_PATH=/absolute/path/to/crypto-js-tools/node_modules \ +KEEPER_APP_SOURCE=/absolute/path/to/exact-app \ +BACKUP_TEST_MONGO_URL=mongodb://127.0.0.1:27038 \ +node --test tests/protectedBackup.test.cjs \ + tests/legacyBackupReadOnly.test.cjs tests/protectedBackupReadiness.test.cjs +node node_modules/typescript/bin/tsc --noEmit +``` + +Successful suite log SHA-256: +`2e377eb9b0f7339726b7bf1a650aad5f0a0ce87a28210f63073e3adbd9776bde`. +Initial environment-failure log SHA-256: +`07712049121a93035385af19e06d0a2cbf82c45f93262a583e13295f3c72f258`. +App runner-guard log SHA-256: +`abefd85be29e45f5adcca8ccdd53e74850d03cadca19ea32bdd38f88046c361f`. +The raw logs remain in the Cloud workspace; private backend source is not copied +into this repository. + +## Remaining acceptance + +After the full frozen app install and six patches completed in Cloud, the normal +Realm prerequisite install was attempted with Node 22.23.3: + +```sh +cd node_modules/realm +../.bin/prebuild-install --runtime napi --target 6 +``` + +It exited 1 with `tunneling socket could not be established, statusCode=403`. +No policy bypass or mock substitute was used. A separate private fixed-pair +hosted CI package uses the existing PR159 route to obtain the genuine binding, +run the complete protected proof and the upgraded-account witness, and retain +private fixture source in the private repository. + +Run the existing current-Relay upgrade witness and complete current protected-v3 +app proof using that genuine encrypted Realm binding. Preserve the public-v2 +matrix's inherited failures separately. Rerun affected combined checks after +verified 5.2 integration; this receipt does not cover that package or any later +candidate SHA. Hosted iOS/Android feature acceptance and physical-device results +remain separate requirements. From 257fbe17e1a85d0e5d7e5d8ab97e1a69edbd9628 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Thu, 22 Oct 2026 06:12:28 +0000 Subject: [PATCH 217/254] fix: discard stale biometric login callbacks --- docs/r5/BIOMETRIC-LIFECYCLE.md | 88 ++++++++++++++++++++++++++++ src/screens/LoginScreen/Login.tsx | 73 ++++++++++++----------- tests/screens/PinEntry.test.tsx | 96 ++++++++++++++++++++++++++++++- 3 files changed, 222 insertions(+), 35 deletions(-) create mode 100644 docs/r5/BIOMETRIC-LIFECYCLE.md diff --git a/docs/r5/BIOMETRIC-LIFECYCLE.md b/docs/r5/BIOMETRIC-LIFECYCLE.md new file mode 100644 index 0000000000..be44a76e4b --- /dev/null +++ b/docs/r5/BIOMETRIC-LIFECYCLE.md @@ -0,0 +1,88 @@ +# R5 biometric callback correction and bounded triage + +Source baseline: `c1c4b803039b61b940373c4dc9d08473cf25e042` (R5 core plus the +5.1 protected-backup and Version History corrections). This is a separate narrow +follow-up, estimated at 1k–3k changed code/test tokens. It does not change +credentials, signature verification, PIN attempt rules, storage or native inputs. + +## Demonstrated login defect + +The prior login function placed an asynchronous native biometric request inside +a delayed callback, with a `try/catch` around timer scheduling. A rejected native +promise therefore escaped that catch. The timer and pending result also survived +the login screen's unmount, account changes and successful PIN authentication. + +Six new component cases invoke the actual production timer callback with a +controlled native boundary. With the baseline login source, five fail and six +pass (including the five existing PIN regressions). The failures demonstrate an +unhandled native rejection, a queued prompt after unmount, and an authentication +dispatch from a late successful prompt after unmount, account change or PIN +authentication. They do not establish a native-device failure or an account +isolation exploit; the downstream authentication checks remain separate. + +The correction owns the delay and pending-result lifetime in one React effect. +Cleanup cancels the delay and makes an outstanding result inactive. Native +rejection is caught inside the asynchronous callback; PIN login remains +available. Only a successful active prompt dispatches authentication, using its +captured originating account. A login already in progress or authenticated does +not schedule a new prompt. The native cancellation/error object is not logged. + +Actual check: `jest tests/screens/PinEntry.test.tsx --runInBand --coverage=false` +passes **11/11**, including the successful-current-prompt control and all existing +rapid-entry, deletion, confirmation and navigation cases. This uses mocked +native biometric/Firebase/storage boundaries, not real biometric evidence. + +Focused Login ESLint remains **FAIL: 12 errors / 4 warnings**, with the same +rules/messages as the baseline after normalizing line offsets. Explicit +`eslint --no-ignore` on the modified test is **PASS: 0 errors / 0 warnings**. +Whole TypeScript remains **FAIL: 943 diagnostics** both before and after, with +zero added/removed normalized diagnostics. Formatting and whitespace checks pass. +These comparisons use the same installed dependencies; no clean whole-lint or +typecheck claim is made. + +## Other approved app investigations + +- **#7055 navigation: unresolved / no speculative fix.** The current footer uses + `home_tab_wallets`, `home_tab_keys`, `home_tab_ask` and `home_tab_more` test IDs. + Its Home callback navigates to `Home` with `selectedOption`; the tab content has + a keyed, non-collapsable native parent inherited from R4. Neither module calls + BackHandler/exitApp or finishes an activity. The preserved Android 17/API 37 + multiuser failure is not dismissed by this source trace. The native matrix must + still exercise ordinary single-user Wallets → More → Wallets after explicit + backup opt-in/Delete, cold launch and account changes, preserving wallets, + labels and the disabled/deleted backup state. No navigation runtime pass is + claimed here. +- **#6991 timezone: not reproduced / no speculative fix.** Production transaction + rows use local `moment(date)` formatting; transaction details also use local + `moment(transaction.date)`. Existing four-process UTC/Kolkata/New York/London + tests cover confirmed, pending and cached rows plus DST/rollover vectors. + Those Node results do not prove Android/Hermes behavior, device-timezone changes + while the app runs, or correct persisted source timestamps. Native list/details + reproduction remains open. +- **#7007 PIN: shipped correction retained.** The issue's current owner evidence + records the core rapid-entry and fixed-Proceed correction as shipped in 2.5.17. + The five existing component cases still pass alongside this patch. Current + exact-candidate native PIN/login and physical biometric/accessibility checks + remain required. No shipped PIN rewrite was introduced. +- **Emergency Key copy: existing correction retained.** Both EN and ES resources + have a distinct `resetEKDesc` naming Emergency Key and `resetIKDesc` naming + Inheritance Key; the corresponding reset screens select the matching helper. + Fresh JSON distinction assertions pass for both resources. This verifies + identifiers and the scoped distinction, not translation quality or native + layout. Timelock calculations are unchanged. +- **#6840 backup naming: 5.1 retained.** Protected-backup terminology, status and + consent/delete explanations are already in the 5.1 package and its evidence + document. No availability or successful live-backup claim is added here. + +## Remaining acceptance + +Hosted iOS/Android feature acceptance must test the final combined candidate, +including native prompt failure/cancel → PIN fallback, account switch/relock, +and no second authentication from a late result. Actual physical biometrics +require actual devices and an authorized tester. These checks are **NOT RUN** by +this package; emulator evidence cannot replace physical evidence. The owner's +personal Mac is not a prerequisite for the Cloud route. + +The separate 5.2 producer/handoff must be identified and reviewed for overlap +before integration. No 5.2 completion or combined native acceptance is inferred +from these tests. No merge, signing, upload, distribution or deployment occurred. diff --git a/src/screens/LoginScreen/Login.tsx b/src/screens/LoginScreen/Login.tsx index b6970cef1a..96230889fa 100644 --- a/src/screens/LoginScreen/Login.tsx +++ b/src/screens/LoginScreen/Login.tsx @@ -167,8 +167,46 @@ function LoginScreen({ navigation, route }) { }, [failedAttempts, lastLoginFailedAt]); useEffect(() => { - biometricAuth(); - }, [canLogin]); + const biometricEnabled = + (allAccounts.length === 0 && loginMethod === LoginMethod.BIOMETRIC) || + (allAccounts.length > 0 && biometricEnabledAppId !== null); + if (!biometricEnabled || !canLogin || isAuthenticated || loggingIn) return undefined; + + const originAppId = biometricEnabledAppId ?? appId; + let active = true; + const timer = setTimeout(async () => { + if (!active) return; + try { + const { success, signature } = await RNBiometrics.createSignature({ + promptMessage: 'Authenticate', + payload: originAppId, + cancelButtonText: common.usePin, + }); + if (!active || !success) return; + setIsBiometric(true); + setLoginModal(true); + setLogging(true); + setLoginError(false); + dispatch(credsAuth(signature, LoginMethod.BIOMETRIC, false, originAppId)); + } catch { + // A failed/cancelled native prompt leaves PIN login available. + } + }, 200); + return () => { + active = false; + clearTimeout(timer); + }; + }, [ + allAccounts.length, + appId, + biometricEnabledAppId, + canLogin, + common.usePin, + dispatch, + isAuthenticated, + loggingIn, + loginMethod, + ]); useEffect(() => { requestUserPermission(); @@ -184,37 +222,6 @@ function LoginScreen({ navigation, route }) { } }, [recepitVerificationError]); - const biometricAuth = async () => { - if ( - (allAccounts.length === 0 && loginMethod === LoginMethod.BIOMETRIC) || - (allAccounts.length > 0 && biometricEnabledAppId !== null) - ) { - try { - setTimeout(async () => { - if (canLogin) { - const { success, signature } = await RNBiometrics.createSignature({ - promptMessage: 'Authenticate', - payload: biometricEnabledAppId ?? appId, - cancelButtonText: common.usePin, - }); - if (success) { - setIsBiometric(true); - setLoginModal(true); - setLogging(true); - setLoginError(false); - dispatch( - credsAuth(signature, LoginMethod.BIOMETRIC, false, biometricEnabledAppId ?? appId) - ); - } - } - }, 200); - } catch (error) { - // - console.log(error); - } - } - }; - const onPressNumber = (text) => { if (text === 'x') { setPasscode((current) => current.slice(0, -1)); diff --git a/tests/screens/PinEntry.test.tsx b/tests/screens/PinEntry.test.tsx index 8c47e37b08..685f461659 100644 --- a/tests/screens/PinEntry.test.tsx +++ b/tests/screens/PinEntry.test.tsx @@ -10,10 +10,11 @@ import PinInputsView from 'src/components/AppPinInput/PinInputsView'; import Buttons from 'src/components/Buttons'; const mockDispatch = jest.fn(); +const mockCreateSignature = jest.fn(); const mockState = { login: { hasCreds: false, authenticationFailed: false, isAuthenticated: false }, settings: { loginMethod: 'PIN' }, - account: { allAccounts: [] }, + account: { allAccounts: [], biometricEnabledAppId: null as string | null }, storage: { appId: 'disposable-test', failedAttempts: 0, lastLoginFailedAt: 0 }, notifications: {}, bhr: {}, @@ -60,7 +61,9 @@ jest.mock('src/services/backend/Relay', () => ({ __esModule: true, default: { getAccountManagerDetails: jest.fn(async () => null) }, })); -jest.mock('react-native-biometrics', () => jest.fn(() => ({}))); +jest.mock('react-native-biometrics', () => + jest.fn(() => ({ createSignature: (...args: unknown[]) => mockCreateSignature(...args) })) +); jest.mock('@react-native-firebase/app', () => ({ getApp: jest.fn() })); jest.mock('@react-native-firebase/messaging', () => ({ AuthorizationStatus: { AUTHORIZED: 1, PROVISIONAL: 2 }, @@ -91,14 +94,103 @@ function mount(component: React.ReactElement) { beforeEach(() => { jest.useFakeTimers(); mockDispatch.mockClear(); + mockCreateSignature.mockReset(); mockState.login.isAuthenticated = false; + mockState.settings.loginMethod = 'PIN'; + mockState.storage.appId = 'disposable-test'; + mockState.account.biometricEnabledAppId = null; }); afterEach(() => { act(() => screen?.unmount()); jest.clearAllTimers(); + jest.restoreAllMocks(); jest.useRealTimers(); }); +describe('biometric login callback lifetime', () => { + let timers: jest.SpyInstance; + const navigation = { replace: jest.fn() }; + const route = { params: {} }; + const authCalls = () => + mockDispatch.mock.calls.filter(([action]) => action.type === 'CREDS_AUTH'); + const startPrompt = () => { + // Invoke the actual delayed production callback so its promise is observable. + // Timer APIs otherwise discard asynchronous callback rejections. + const callbacks = timers.mock.calls.filter(([, delay]) => delay === 200); + expect(callbacks.length).toBeGreaterThan(0); + return callbacks[callbacks.length - 1][0](); + }; + beforeEach(() => { + timers = jest.spyOn(global, 'setTimeout'); + mockCreateSignature.mockResolvedValue({ success: false }); + mockState.settings.loginMethod = 'BIOMETRIC'; + mount(); + }); + + test('native rejection is handled and leaves PIN login available', async () => { + mockCreateSignature.mockRejectedValue(new Error('synthetic native cancellation')); + await act(async () => { + await expect(startPrompt()).resolves.toBeUndefined(); + }); + expect(authCalls()).toHaveLength(0); + enter('1111'); + expect(input()).toBe('1111'); + expect(screen.root.findByType(Buttons).props.primaryDisable).toBe(false); + }); + + test('a successful current prompt authenticates its originating account once', async () => { + mockCreateSignature.mockResolvedValue({ success: true, signature: 'synthetic-signature' }); + await act(async () => { + await startPrompt(); + }); + expect(authCalls()).toHaveLength(1); + expect(authCalls()[0][0].payload).toEqual({ + passcode: 'synthetic-signature', + method: 'BIOMETRIC', + reLogin: false, + appId: 'disposable-test', + }); + }); + + test('unmount cancels a queued prompt', async () => { + act(() => screen.unmount()); + await act(async () => jest.runOnlyPendingTimers()); + expect(mockCreateSignature).not.toHaveBeenCalled(); + expect(authCalls()).toHaveLength(0); + }); + + test.each(['unmount', 'account change', 'PIN authentication'])( + 'late native success after %s cannot authenticate', + async (transition) => { + let complete = (_result: { success: boolean; signature: string }) => {}; + mockCreateSignature.mockImplementation( + () => + new Promise((resolve) => { + complete = resolve; + }) + ); + let pending: Promise = Promise.resolve(); + act(() => { + pending = startPrompt(); + }); + expect(mockCreateSignature).toHaveBeenCalledTimes(1); + act(() => { + if (transition === 'unmount') screen.unmount(); + else { + if (transition === 'account change') mockState.storage.appId = 'other-disposable-account'; + else mockState.login.isAuthenticated = true; + screen.update(); + } + }); + await act(async () => { + complete({ success: true, signature: 'stale-synthetic-signature' }); + await pending; + }); + expect(authCalls()).toHaveLength(0); + } + ); +}); + describe('PIN entry under batched rapid taps', () => { test('successful local authentication enters the app without a second tap', async () => { const navigation = { replace: jest.fn() }; From 486810052f90550d55dd6ddd541ff53ebd875caa Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Thu, 22 Oct 2026 06:13:08 +0000 Subject: [PATCH 218/254] test(r5): add isolated production-worker native storage witness --- .github/native/.gitignore | 1 + .github/native/r5_features.js | 86 +++++++ .github/native/r5_features.md | 15 ++ .github/native/r5_features.yaml | 35 +++ .github/native/r5_features_core.js | 401 +++++++++++++++++++++++++++++ .github/native/r5_metro.cjs | 21 ++ .github/native/r5_prepare.cjs | 127 +++++++++ .github/tests/r5_features.test.cjs | 140 ++++++++++ 8 files changed, 826 insertions(+) create mode 100644 .github/native/.gitignore create mode 100644 .github/native/r5_features.js create mode 100644 .github/native/r5_features.md create mode 100644 .github/native/r5_features.yaml create mode 100644 .github/native/r5_features_core.js create mode 100644 .github/native/r5_metro.cjs create mode 100644 .github/native/r5_prepare.cjs create mode 100644 .github/tests/r5_features.test.cjs diff --git a/.github/native/.gitignore b/.github/native/.gitignore new file mode 100644 index 0000000000..ecb842d705 --- /dev/null +++ b/.github/native/.gitignore @@ -0,0 +1 @@ +.generated/ diff --git a/.github/native/r5_features.js b/.github/native/r5_features.js new file mode 100644 index 0000000000..ade865c7aa --- /dev/null +++ b/.github/native/r5_features.js @@ -0,0 +1,86 @@ +// CI-only entrypoint: never imported by the production app entrypoint. +import 'react-native-get-random-values'; +import React, { useEffect, useState } from 'react'; +import { AppRegistry, SafeAreaView, Text } from 'react-native'; +import Realm from 'realm'; +import RNFS from 'react-native-fs'; +import DeviceInfo from 'react-native-device-info'; +import { runSaga } from 'redux-saga'; +import * as effects from 'redux-saga/effects'; +import schema from '../../src/storage/realm/schema'; +import * as storage from '../../src/storage'; +import * as enums from '../../src/services/wallets/enums'; +import * as subscription from '../../src/models/enums/SubscriptionTier'; +import { RealmSchema } from '../../src/storage/realm/enum'; +import { name as appName } from '../../app.json'; +import { createWorkers, provenance } from './.generated/r5_workers'; +import { runFeatures, FeatureFailure, SCENARIOS, LIMITS } from './r5_features_core'; + +const startedAt = performance.now(); +let attempt; +function FeatureProbe() { + const [stage, setStage] = useState('running'); + useEffect(() => { + let active = true; + if (!attempt) { + attempt = (async () => { + let result; + let failure; + try { + if (!__DEV__) throw new FeatureFailure('development-only'); + result = await runFeatures({ + Realm, + schema, + storage, + createWorkers, + effects, + runSaga, + enums, + subscription, + RealmSchema, + DeviceInfo, + now: () => performance.now(), + startedAt, + }); + } catch (error) { + failure = error instanceof FeatureFailure ? error.code : 'native-exception'; + result = { + stage: 'failed', + measurements: [], + scenarios: SCENARIOS.map((id) => ({ id, status: 'FAIL' })), + }; + } + const evidence = { + schema: 1, + scope: 'production-worker-native-storage', + status: + result.stage === 'pass' ? 'PASS' : result.stage === 'failed' ? 'FAIL' : 'IN PROGRESS', + stage: result.stage, + scenarios: result.scenarios, + measurements: result.measurements, + limits: LIMITS, + source_functions: provenance, + ...(failure ? { failure } : {}), + }; + await RNFS.writeFile( + `${RNFS.DocumentDirectoryPath}/keeper-r5-native-evidence.json`, + JSON.stringify(evidence), + 'utf8' + ); + return result.stage; + })().catch(() => 'failed'); + } + attempt.then((value) => { + if (active) setStage(value); + }); + return () => { + active = false; + }; + }, []); + return ( + + Keeper R5 native worker probe {stage} + + ); +} +AppRegistry.registerComponent(appName, () => FeatureProbe); diff --git a/.github/native/r5_features.md b/.github/native/r5_features.md new file mode 100644 index 0000000000..95c1b581a1 --- /dev/null +++ b/.github/native/r5_features.md @@ -0,0 +1,15 @@ +# R5 production-worker native witness + +This isolated diagnostic adds native storage evidence to the R5 worker regressions. It never replaces the ordinary application entrypoint. Select `r5_metro.cjs` only in a disposable development simulator/emulator job, after running `node .github/native/r5_prepare.cjs`. + +The preparation script extracts the current candidate's unchanged setup and deletion worker definitions, signer identity functions and account-session observer. It records the SHA-256 of every containing source module and fails if a selected definition disappears or becomes ambiguous. The generated module is ignored by Git; it is regenerated from the checked-out candidate before Metro starts. The native entrypoint imports the actual current Realm schemas, Realm, MMKV, Redux Saga and native DeviceInfo. + +The six-process Maestro flow runs five independent disposable cases and then verifies their persisted counts. Each case tests the production setup worker's offline/error/retry decisions and preservation of its one native wallet; the production deletion workers' live, hidden, canary and archived reference checks; native transaction rollback when a later deletion target is absent; and rejection after an A → B → A session change. Another encrypted Realm remains untouched. The last process reopens all five pairs of files, verifies exact wallet/key counts, and removes the fixtures and its MMKV marker. + +The test adapters deliberately control Relay responses, wallet/key generation, dispatch, database selection and the backup local-commit boundary. They persist invalid public sentinel records, with no valid seed, private key, address, network call or remote backup confirmation. These checks cover production worker behavior over native storage. They do **not** establish product navigation, concealed UI, PIN/biometric behavior, account login, cryptographic derivation, genuine protected-backup protocol compatibility, old-schema migration or release readiness. Keep those acceptance rows open until their own executable checks pass. + +`keeper-r5-native-evidence.json` contains only fixed scenario/status/limitation identifiers, source paths/hashes/function names and five raw timing pairs. `probe_ready_ms` measures this alternate probe's preparation; `setup_first_wallet_ms` measures the controlled worker's first native synthetic-wallet write. Neither is the full app's cold-start or first-wallet UX latency. The separate full-product measurement flow must supply those user-facing measurements. Native host orchestration must validate this JSON and the unchanged installed artifact identity; a Maestro result alone is insufficient. + +Initial source package: based on `c1c4b803039b61b940373c4dc9d08473cf25e042` (core `77b72da29ac5f2cdf3f51c9a0e9f3fd1c2af7238` plus 5.1). Approximately 7.5k changed code/test tokens, two related worker scenario areas. No producer branch, app/native dependency input or release configuration changed. + +Actual local checks: six host-only extraction/schema/phase/limitation contract tests PASS; Babel transforms PASS; formatting and whitespace PASS. No native application build or simulator/emulator run was performed for this initial commit. Run the integrated exact candidate through both hosted platforms before assigning native PASS. Physical-device and independent security review evidence remain separate. diff --git a/.github/native/r5_features.yaml b/.github/native/r5_features.yaml new file mode 100644 index 0000000000..f517b6ed75 --- /dev/null +++ b/.github/native/r5_features.yaml @@ -0,0 +1,35 @@ +appId: ${APP_ID} +name: Production R5 workers over disposable native storage across six processes +--- +- launchApp: + clearState: true + permissions: + all: deny +- extendedWaitUntil: + visible: + id: r5-features-next + timeout: 90000 +- assertNotVisible: + id: r5-features-failed +- repeat: + times: 4 + commands: + - stopApp + - launchApp: + clearState: false + - extendedWaitUntil: + visible: + id: r5-features-next + timeout: 90000 + - assertNotVisible: + id: r5-features-failed +- stopApp +- launchApp: + clearState: false +- extendedWaitUntil: + visible: + id: r5-features-pass + timeout: 90000 +- assertNotVisible: + id: r5-features-failed +- stopApp diff --git a/.github/native/r5_features_core.js b/.github/native/r5_features_core.js new file mode 100644 index 0000000000..d75d8ec490 --- /dev/null +++ b/.github/native/r5_features_core.js @@ -0,0 +1,401 @@ +// CI-only production-worker/native-storage witness. All transport, generation, +// dispatch and database selection adapters below are explicit test boundaries. +// This is not product UI, cryptography, backend or account-login acceptance. +const PHASE = 'keeper-r5-worker-native-phase-v1'; +const SCENARIOS = Object.freeze([ + 'production-setup-offline-retry-native-counts', + 'production-deletion-retained-native-references', + 'production-deletion-native-rollback', + 'production-worker-stale-session-rejection', + 'native-worker-process-relaunch-counts', +]); +const LIMITS = Object.freeze([ + 'controlled-transport-no-backend-acceptance', + 'synthetic-wallet-generation-no-crypto-acceptance', + 'injected-dispatch-selection-no-login-acceptance', + 'no-product-ui-navigation-pin-or-biometric-acceptance', + 'probe-and-worker-timings-not-product-startup-ux', +]); +class FeatureFailure extends Error { + constructor(code) { + super(code); + this.code = code; + } +} +function check(value, code) { + if (!value) throw new FeatureFailure(code); +} + +// Build invalid public sentinel records from the *actual current* Realm schema. +// Required links are instantiated, arrays stay empty, optional fields omitted. +// No key-generation path or valid seed is used by this probe. +function recordBuilder(schema) { + const schemas = new Map(schema.map((item) => [item.name, item])); + function value(type, depth) { + if (depth > 15) throw new FeatureFailure('schema-depth'); + if (typeof type === 'object') { + if (Object.hasOwn(type, 'default')) return type.default; + if (type.optional) return undefined; + if (type.type === 'list' || type.type === 'set') return []; + if (type.type === 'dictionary') return {}; + return value(type.objectType || type.type, depth + 1); + } + if (type.endsWith('?')) return undefined; + if (type.endsWith('[]')) return []; + if (schemas.has(type)) return record(type, {}, depth + 1); + if (type === 'string') return ''; + if (['int', 'float', 'double'].includes(type)) return 0; + if (type === 'bool') return false; + if (type === 'date') return new Date(0); + if (type === 'data') return new ArrayBuffer(0); + if (type === 'mixed') return null; + throw new FeatureFailure('unsupported-schema-property'); + } + function record(name, overrides = {}, depth = 0) { + const item = schemas.get(name); + if (!item) throw new FeatureFailure('schema-missing'); + const result = {}; + for (const [key, type] of Object.entries(item.properties)) { + const field = value(type, depth + 1); + if (field !== undefined) result[key] = field; + } + return Object.assign(result, overrides); + } + return record; +} + +async function runFeatures({ + Realm, + schema, + storage, + createWorkers, + effects, + runSaga, + enums, + RealmSchema, + subscription, + DeviceInfo, + now, + startedAt, +}) { + const make = recordBuilder(schema); + const directory = Realm.defaultPath.slice(0, Realm.defaultPath.lastIndexOf('/') + 1); + check(directory, 'native-directory-missing'); + const config = (iteration, suffix) => ({ + path: `${directory}keeper-r5-worker-${iteration}-${suffix}.realm`, + schema, + schemaVersion: 1, + encryptionKey: new Uint8Array(64).fill(suffix === 'a' ? 17 : 18), + }); + let saved; + try { + saved = JSON.parse(storage.getString(PHASE) || '{"iteration":0,"measurements":[]}'); + } catch { + throw new FeatureFailure('invalid-phase'); + } + check( + Number.isInteger(saved.iteration) && + saved.iteration >= 0 && + saved.iteration <= 5 && + Array.isArray(saved.measurements) && + saved.measurements.length === saved.iteration, + 'invalid-phase' + ); + // Every process verifies the previous process's committed wallet/key counts. + for (let index = 1; index <= saved.iteration; index += 1) { + for (const suffix of ['a', 'b']) { + const prior = await Realm.open(config(index, suffix)); + try { + check(prior.objects('Wallet').length === (suffix === 'a' ? 1 : 0), 'relaunch-wallet-count'); + check(prior.objects('Signer').length === (suffix === 'a' ? 2 : 0), 'relaunch-signer-count'); + check(prior.objects('Vault').length === 0, 'relaunch-vault-count'); + } finally { + prior.close(); + } + } + } + if (saved.iteration === 5) { + for (let index = 1; index <= 5; index += 1) { + Realm.deleteFile(config(index, 'a')); + Realm.deleteFile(config(index, 'b')); + } + storage.deleteItem(PHASE); + return { + stage: 'pass', + measurements: saved.measurements, + scenarios: SCENARIOS.map((id) => ({ id, status: 'PASS' })), + }; + } + const iteration = saved.iteration + 1; + let a; + let b; + try { + a = await Realm.open(config(iteration, 'a')); + b = await Realm.open(config(iteration, 'b')); + check( + a.objects('KeeperApp').length === 0 && b.objects('KeeperApp').length === 0, + 'fixture-not-fresh' + ); + const appA = 'keeper-ci-r5-account-a'; + const appB = 'keeper-ci-r5-account-b'; + const appRecord = (id) => + make('KeeperApp', { + id, + publicId: 'CI-NOT-A-PUBLIC-KEY', + primaryMnemonic: 'CI INVALID NOT A MNEMONIC', + primarySeed: 'CI INVALID NOT A SEED', + imageEncryptionKey: 'CI INVALID NOT A KEY', + networkType: 'TESTNET', + version: 'CI', + }); + a.write(() => a.create('KeeperApp', appRecord(appA))); + b.write(() => b.create('KeeperApp', appRecord(appB))); + let active = a; + const state = { + settings: { bitcoinNetworkType: 'TESTNET', loginMethod: 'PIN' }, + storage: { appId: appA, appCreated: false }, + account: { allAccounts: [] }, + }; + const actions = []; + const action = (name) => (payload) => ({ type: name, payload }); + const dispatch = (item) => { + actions.push(item); + if (item.type === 'setAppId') state.storage.appId = item.payload; + if (item.type === 'setAppCreated') state.storage.appCreated = item.payload; + if (item.type === 'addAccount') state.account.allAccounts = [{ appId: item.payload }]; + return item; + }; + const dbManager = { + getObjectByIndex: (name) => active.objects(name)[0], + getCollection: (name) => Array.from(active.objects(name)), + createObject: (name, object) => { + active.write(() => active.create(name, object, Realm.UpdateMode.All)); + return true; + }, + }; + let online = false; + let allowKey = false; + let generationCount = 0; + let firstWalletAt; + const signer = (fingerprint) => + make('Signer', { + id: `${fingerprint}0T`, + masterFingerprint: fingerprint, + type: 'SEED_WORDS', + networkType: 'TESTNET', + signerXpubs: { + P2WPKH: [{ xpub: `CI INVALID XPUB ${fingerprint}`, derivationPath: "m/84'/1'/0'" }], + }, + }); + const recovery = signer('C1C1C1C1'); + const deps = { + ...effects, + RealmSchema, + ...enums, + ...subscription, + DeviceInfo, + dbManager, + realm: { getDatabase: () => active }, + store: { getState: () => state, dispatch }, + getJSONFromRealmObject: (item) => (item.toJSON ? item.toJSON() : item), + Relay: { createNewApp: async () => ({ created: online }) }, + WalletUtilities: { getDerivationPath: () => "m/84'/1'/0'" }, + setupRecoveryKeySigningKey: () => recovery, + addNewWalletsWorker: function* () { + generationCount += 1; + dbManager.createObject( + 'Wallet', + make('Wallet', { + id: 'keeper-ci-r5-mobile-wallet', + type: 'DEFAULT', + networkType: 'TESTNET', + entityKind: 'WALLET', + scriptType: 'P2WPKH', + derivationDetails: make('WalletDerivationDetails', { + instanceNum: 0, + xDerivationPath: "m/84'/1'/0'", + mnemonic: 'CI INVALID', + }), + specs: make('WalletSpecs', { xpub: 'CI INVALID XPUB' }), + }) + ); + firstWalletAt = now(); + return true; + }, + addSigningDeviceWorker: function* () { + if (allowKey) dbManager.createObject('Signer', recovery); + return allowKey; + }, + LoginMethod: { BIOMETRIC: 'BIOMETRIC' }, + beginBackupLocalOperation: (appId, _origin, session) => ({ + appId, + session, + database: active, + }), + commitBackupLocalOperation: (operation, write) => { + if ( + operation.database !== active || + operation.appId !== state.storage.appId || + operation.session !== workers.currentBackupAccountSession() + ) + return { committed: false }; + try { + active.write(() => write(active)); + return { committed: true }; + } catch { + return { committed: false }; + } + }, + // Deliberately no remote request and no Verified/backup-confirmed assertion. + queueCommittedBackupWorker: function* () { + return { updated: true, backupPending: true }; + }, + captureError: () => undefined, + uaiType: { SIGNING_DEVICES_HEALTH_CHECK: 'health' }, + }; + for (const name of [ + 'setAppId', + 'addAccount', + 'updateDefaultWalletCreatedByAppId', + 'setAppCreated', + 'resetRealyWalletState', + 'loadConciergeUser', + 'loadConciergeTickets', + 'setBiometricEnabledAppId', + 'setAppCreationError', + 'uaiChecks', + 'showDeletingKeyModal', + 'hideDeletingKeyModal', + 'showKeyDeletedSuccessModal', + 'relaySignersUpdateFail', + ]) + deps[name] = action(name); + const workers = createWorkers(deps); + workers.observeBackupAccount(appA); + const run = (name, args) => + runSaga({ getState: () => state, dispatch }, workers[name], args).toPromise(); + const setup = () => + run('setupKeeperAppWorker', { payload: { appName: 'CI disposable', fcmToken: '' } }); + const readyAt = now(); + await setup(); + check( + a.objects('Wallet').length === 0 && state.storage.appCreated === false, + 'offline-setup-ready' + ); + online = true; + const setupAt = now(); + await setup(); + check( + a.objects('Wallet').length === 1 && state.storage.appCreated === false, + 'key-failure-ready' + ); + allowKey = true; + await setup(); + check( + a.objects('Wallet').length === 1 && generationCount === 1 && state.storage.appCreated, + 'retry-wallet-duplicate' + ); + check( + a.objects('Signer').length === 1 && state.account.allAccounts.length === 1, + 'retry-account-duplicate' + ); + check(Number.isFinite(firstWalletAt) && firstWalletAt >= setupAt, 'first-wallet-clock'); + + const target = signer('D1D1D1D1'); + const unrelated = signer('E1E1E1E1'); + dbManager.createObject('Signer', target); + dbManager.createObject('Signer', unrelated); + const request = (signers) => ({ + payload: { signers }, + originAppId: appA, + originBackupAccountSession: workers.currentBackupAccountSession(), + }); + for (const kind of ['active', 'hidden', 'canary', 'archived']) { + dbManager.createObject( + 'Vault', + make('Vault', { + id: 'keeper-ci-r5-reference', + entityKind: 'VAULT', + type: kind === 'canary' ? 'CANARY' : 'DEFAULT', + networkType: 'TESTNET', + archived: kind === 'archived', + presentationData: { + name: 'CI concealed sentinel', + description: '', + visibility: kind === 'hidden' ? 'HIDDEN' : 'DEFAULT', + }, + signers: [ + make('VaultSigner', { + masterFingerprint: target.masterFingerprint, + xpub: 'CI INVALID VAULT XPUB', + xfp: target.masterFingerprint, + derivationPath: "m/48'/1'/0'/2'", + }), + ], + }) + ); + check( + (await run('deleteSigningDeviceWorker', request([target]))) === false, + 'retained-reference-accepted' + ); + check( + a.objectForPrimaryKey('Signer', target.id) && a.objectForPrimaryKey('Signer', unrelated.id), + 'retained-reference-deleted' + ); + active.write(() => active.delete(active.objects('Vault'))); + } + // Missing later target must roll back an earlier native deletion atomically. + const missing = signer('F1F1F1F1'); + check( + (await run('deleteSigningDeviceWorker', request([target, missing]))) === false, + 'partial-delete-accepted' + ); + check(a.objectForPrimaryKey('Signer', target.id), 'partial-delete-not-rolled-back'); + const stale = request([target]); + active = b; + state.storage.appId = appB; + workers.observeBackupAccount(appB); + active = a; + state.storage.appId = appA; + workers.observeBackupAccount(appA); + check((await run('deleteSigningDeviceWorker', stale)) === false, 'stale-session-accepted'); + check(a.objectForPrimaryKey('Signer', target.id), 'stale-session-mutated'); + check( + (await run('deleteSigningDeviceWorker', request([target]))) === true, + 'unreferenced-delete-rejected' + ); + check( + !a.objectForPrimaryKey('Signer', target.id) && a.objectForPrimaryKey('Signer', unrelated.id), + 'targeted-delete-wrong-record' + ); + check( + b.objects('Wallet').length === 0 && b.objects('Signer').length === 0, + 'other-native-account-mutated' + ); + const measurement = { + iteration, + probe_ready_ms: readyAt - startedAt, + setup_first_wallet_ms: firstWalletAt - setupAt, + }; + check( + Object.values(measurement).every(Number.isFinite) && + measurement.probe_ready_ms >= 0 && + measurement.setup_first_wallet_ms >= 0, + 'invalid-clock-measurement' + ); + const measurements = [...saved.measurements, measurement]; + storage.setItem(PHASE, JSON.stringify({ iteration, measurements })); + return { + stage: 'next', + measurements, + scenarios: SCENARIOS.map((id) => ({ + id, + status: id === 'native-worker-process-relaunch-counts' ? 'NOT RUN' : 'PASS', + })), + }; + } finally { + if (a && !a.isClosed) a.close(); + if (b && !b.isClosed) b.close(); + } +} +module.exports = { runFeatures, recordBuilder, FeatureFailure, PHASE, SCENARIOS, LIMITS }; diff --git a/.github/native/r5_metro.cjs b/.github/native/r5_metro.cjs new file mode 100644 index 0000000000..f77b6328f8 --- /dev/null +++ b/.github/native/r5_metro.cjs @@ -0,0 +1,21 @@ +// Only the disposable native-worker job selects this alternate entrypoint. +const path = require('node:path'); +const base = require('../../metro.config'); +const enhance = base.server?.enhanceMiddleware; +module.exports = { + ...base, + projectRoot: path.resolve(__dirname, '../..'), + server: { + ...base.server, + enhanceMiddleware(middleware, server) { + const inner = enhance ? enhance(middleware, server) : middleware; + return (req, res, next) => { + req.url = req.url.replace( + /^\/index\.(bundle|map)(?=\?|$)/, + '/.github/native/r5_features.$1' + ); + return inner(req, res, next); + }; + }, + }, +}; diff --git a/.github/native/r5_prepare.cjs b/.github/native/r5_prepare.cjs new file mode 100644 index 0000000000..dba8198209 --- /dev/null +++ b/.github/native/r5_prepare.cjs @@ -0,0 +1,127 @@ +// Build-time extraction of unchanged production workers for a CI-only native probe. +// No eval, source rewriting or production entrypoint changes occur in the app. +const fs = require('node:fs'); +const path = require('node:path'); +const crypto = require('node:crypto'); +const ts = require('typescript'); +const ROOT = path.resolve(__dirname, '../..'); +const SELECTION = Object.freeze({ + 'src/utils/utilities.ts': ['getAccountFromSigner', 'getKeyUID'], + 'src/store/sagas/storage.ts': ['setupKeeperAppWorker'], + 'src/store/sagas/wallets.ts': [ + 'currentWalletAccountId', + 'assertWalletAccount', + 'deleteSigningDeviceWorker', + ], + 'src/store/sagas/bhr.ts': ['currentBackupAppId', 'deleteAppImageEntityWorker'], + 'src/services/backup/transport.ts': [ + 'observeBackupAccount', + 'currentBackupAccountSession', + 'clearVerifiedRemoteRevision', + ], +}); +const DEPENDENCIES = [ + 'call', + 'put', + 'select', + 'dbManager', + 'realm', + 'store', + 'RealmSchema', + 'SignerType', + 'NetworkType', + 'XpubTypes', + 'WalletType', + 'DerivationPurpose', + 'getJSONFromRealmObject', + 'beginBackupLocalOperation', + 'commitBackupLocalOperation', + 'queueCommittedBackupWorker', + 'captureError', + 'uaiType', + 'uaiChecks', + 'showDeletingKeyModal', + 'hideDeletingKeyModal', + 'showKeyDeletedSuccessModal', + 'relaySignersUpdateFail', + 'Relay', + 'WalletUtilities', + 'setupRecoveryKeySigningKey', + 'addNewWalletsWorker', + 'addSigningDeviceWorker', + 'SubscriptionTier', + 'AppSubscriptionLevel', + 'DeviceInfo', + 'setAppId', + 'addAccount', + 'updateDefaultWalletCreatedByAppId', + 'setAppCreated', + 'resetRealyWalletState', + 'loadConciergeUser', + 'loadConciergeTickets', + 'LoginMethod', + 'setBiometricEnabledAppId', + 'setAppCreationError', +]; + +function extract(file, names) { + const source = fs.readFileSync(file, 'utf8'); + const parsed = ts.createSourceFile(file, source, ts.ScriptTarget.Latest, true); + const found = new Map(); + for (const statement of parsed.statements) { + if ( + ts.isFunctionDeclaration(statement) && + statement.name && + names.includes(statement.name.text) + ) { + if (found.has(statement.name.text)) throw new Error('Duplicate selected function'); + found.set(statement.name.text, statement.getText(parsed).replace(/^export\s+/, '')); + } else if (ts.isVariableStatement(statement)) { + for (const declaration of statement.declarationList.declarations) { + if (ts.isIdentifier(declaration.name) && names.includes(declaration.name.text)) { + if (found.has(declaration.name.text)) throw new Error('Duplicate selected variable'); + found.set(declaration.name.text, `const ${declaration.getText(parsed)};`); + } + } + } + } + if (found.size !== names.length) throw new Error('Production probe selection changed'); + return { + source: names.map((name) => found.get(name)).join('\n'), + sha256: crypto.createHash('sha256').update(source).digest('hex'), + }; +} + +function prepare(root = ROOT) { + const sources = []; + const provenance = []; + for (const [relative, names] of Object.entries(SELECTION)) { + const extracted = extract(path.join(root, relative), names); + sources.push(extracted.source); + provenance.push({ path: relative, sha256: extracted.sha256, names }); + } + const input = `function createWorkers(deps) { + const { ${DEPENDENCIES.join(',')} } = deps; + let activeAccount; let accountSession = 0; let lastDiagnostic; + const verifiedRemoteRevisions = new Map(); const baselineEpochs = new Map(); + ${sources.join('\n')} + return { setupKeeperAppWorker, deleteSigningDeviceWorker, deleteAppImageEntityWorker, + getKeyUID, observeBackupAccount, currentBackupAccountSession }; + } + module.exports = { createWorkers, provenance: ${JSON.stringify(provenance)} };`; + const result = ts.transpileModule(input, { + reportDiagnostics: true, + compilerOptions: { target: ts.ScriptTarget.ES2020, module: ts.ModuleKind.CommonJS }, + }); + if (result.diagnostics.some((d) => d.category === ts.DiagnosticCategory.Error)) + throw new Error('Probe extraction syntax failed'); + const directory = path.join(root, '.github/native/.generated'); + fs.mkdirSync(directory, { recursive: true }); + fs.writeFileSync(path.join(directory, 'r5_workers.js'), result.outputText); + return provenance; +} +if (require.main === module) { + prepare(); + process.stdout.write('R5 production worker extraction prepared\n'); +} +module.exports = { prepare, extract, SELECTION, DEPENDENCIES }; diff --git a/.github/tests/r5_features.test.cjs b/.github/tests/r5_features.test.cjs new file mode 100644 index 0000000000..169e5ebd6c --- /dev/null +++ b/.github/tests/r5_features.test.cjs @@ -0,0 +1,140 @@ +// Host-only contract checks. They do not execute a native Realm or grant app acceptance. +const test = require('node:test'); +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const os = require('node:os'); +const path = require('node:path'); +const vm = require('node:vm'); +const ts = require('typescript'); +const { extract, prepare, SELECTION } = require('../native/r5_prepare.cjs'); +const { + recordBuilder, + runFeatures, + FeatureFailure, + SCENARIOS, + LIMITS, +} = require('../native/r5_features_core'); +const root = path.resolve(__dirname, '../..'); + +test('build-time probe extracts every selected production definition and records its source hash', () => { + const provenance = prepare(); + assert.equal(provenance.length, Object.keys(SELECTION).length); + for (const item of provenance) { + assert.match(item.sha256, /^[a-f0-9]{64}$/); + assert.deepEqual(item.names, SELECTION[item.path]); + } + const generated = require('../native/.generated/r5_workers'); + const workers = generated.createWorkers({}); + for (const name of [ + 'setupKeeperAppWorker', + 'deleteSigningDeviceWorker', + 'deleteAppImageEntityWorker', + 'getKeyUID', + 'observeBackupAccount', + 'currentBackupAccountSession', + ]) + assert.equal(typeof workers[name], 'function'); + assert.deepEqual(generated.provenance, provenance); +}); + +test('missing or duplicate production definitions fail closed rather than execute a substitute', () => { + const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'keeper-r5-extraction-')); + try { + const file = path.join(directory, 'source.ts'); + fs.writeFileSync(file, 'export const retained = () => 1;'); + assert.throws(() => extract(file, ['missing']), /selection changed/); + fs.writeFileSync(file, 'function retained(){}; function retained(){};'); + assert.throws(() => extract(file, ['retained']), /Duplicate/); + } finally { + fs.rmSync(directory, { recursive: true }); + } +}); + +test('actual production session observer invalidates an A to B to A worker scope', () => { + prepare(); + const { createWorkers } = require('../native/.generated/r5_workers'); + const workers = createWorkers({}); + workers.observeBackupAccount('A'); + const first = workers.currentBackupAccountSession(); + workers.observeBackupAccount('A'); + assert.equal(workers.currentBackupAccountSession(), first); + workers.observeBackupAccount('B'); + workers.observeBackupAccount('A'); + assert.ok(workers.currentBackupAccountSession() > first); +}); + +test('native fixture builder uses actual current schemas, required linked objects and sentinel-only inputs', () => { + const cache = new Map(); + function load(relative) { + let file = path.resolve(root, relative); + if (fs.existsSync(file) && fs.statSync(file).isDirectory()) file = path.join(file, 'index.ts'); + else if (!fs.existsSync(file)) file = `${file}.ts`; + if (cache.has(file)) return cache.get(file).exports; + const module = { exports: {} }; + cache.set(file, module); + const source = fs.readFileSync(file, 'utf8'); + const javascript = ts.transpileModule(source, { + compilerOptions: { + target: ts.ScriptTarget.ES2020, + module: ts.ModuleKind.CommonJS, + }, + }).outputText; + vm.runInNewContext( + javascript, + { + exports: module.exports, + module, + Date, + require(name) { + if (name.startsWith('src/')) return load(name); + if (name.startsWith('.')) + return load(path.relative(root, path.resolve(path.dirname(file), name))); + throw new Error(`Unexpected schema import ${name}`); + }, + }, + { filename: file } + ); + return module.exports; + } + const schema = load('src/storage/realm/schema').default; + const make = recordBuilder(schema); + const wallet = make('Wallet'); + assert.equal(wallet.specs.xpub, ''); + assert.equal(wallet.specs.balances.confirmed, 0); + assert.deepEqual(wallet.specs.transactions, []); + assert.equal(make('Vault').archived, false); + assert.equal(make('KeeperApp').subscription.level, 0); + assert.equal(make('Signer').lastHealthCheck.valueOf(), 0); + assert.throws(() => make('Missing'), /schema-missing/); +}); + +test('invalid persistent process state fails before opening any native database', async () => { + let opened = false; + const Realm = { + defaultPath: '/disposable/default.realm', + open() { + opened = true; + }, + }; + for (const serialized of [ + 'broken', + '{"iteration":6,"measurements":[]}', + '{"iteration":1,"measurements":[]}', + ]) { + await assert.rejects( + runFeatures({ Realm, schema: [], storage: { getString: () => serialized } }), + (error) => error instanceof FeatureFailure && error.code === 'invalid-phase' + ); + } + assert.equal(opened, false); +}); + +test('probe reports a bounded worker scope and cannot imply product startup, UI or backend coverage', () => { + assert.equal(SCENARIOS.length, 5); + assert.ok(SCENARIOS.every((id) => /^(production-|native-worker-)/.test(id))); + assert.ok(LIMITS.includes('no-product-ui-navigation-pin-or-biometric-acceptance')); + assert.ok(LIMITS.includes('probe-and-worker-timings-not-product-startup-ux')); + const entry = fs.readFileSync(path.join(root, '.github/native/r5_features.js'), 'utf8'); + assert.ok(entry.includes('production-worker-native-storage')); + assert.ok(!/from ['"].*(?:App|store\/store|services\/backend)/.test(entry)); +}); From 2e0a8fe0e9b26ac8d1077e513bc4361d9e0c3619 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Thu, 22 Oct 2026 06:13:41 +0000 Subject: [PATCH 219/254] ci: reconcile R5 hosted diagnostics and exact-head native evidence Adopts narrow native repairs from #7060 d865ca9c4f91532a1524443cc6da42d8d7b07e1a; preserves #7063 privacy and #7062 foundation as separately source-linked commits. Diagnostics cannot authorize candidate acceptance or release. --- .github/native/hosted_native.py | 3 +- .../tests/keeper-local-validation.test.cjs | 64 ++++++++++++- .github/tests/native-action-pins.test.cjs | 5 +- .../tests/native-diagnostic-policy.test.cjs | 89 +++++++++++++++++++ .github/tests/native-draft-policy.test.cjs | 80 +++++++++-------- .../workflows/android-diagnostic-build.yml | 2 + .github/workflows/keeper-hosted-native.yml | 7 +- .github/workflows/keeper-local-validation.yml | 34 ++++--- .../workflows/keeper-native-diagnostic.yml | 33 +++++++ .github/workflows/keeper-native-platform.yml | 22 ++++- .../workflows/keeper-release-acceptance.yml | 8 +- .github/workflows/test.yml | 1 - AGENTS.md | 9 ++ docs/r5-cloud-native-reconciliation.md | 37 ++++++++ ios/Podfile.lock | 6 +- ios/hexa_keeper/AppDelegate.mm | 4 +- package.json | 2 +- yarn.lock | 8 +- 18 files changed, 344 insertions(+), 70 deletions(-) create mode 100644 .github/tests/native-diagnostic-policy.test.cjs create mode 100644 .github/workflows/keeper-native-diagnostic.yml create mode 100644 docs/r5-cloud-native-reconciliation.md diff --git a/.github/native/hosted_native.py b/.github/native/hosted_native.py index e9c95a3169..3e421b53ed 100644 --- a/.github/native/hosted_native.py +++ b/.github/native/hosted_native.py @@ -464,6 +464,7 @@ def record(): raise RuntimeError('Checkout does not match the exact requested PR head.') save({ 'schema': 1, 'platform': PLATFORM, 'source_sha': source, + 'diagnostic_only': os.environ.get('KEEPER_DIAGNOSTIC') == 'true', 'workflow_ref': os.environ.get('GITHUB_WORKFLOW_REF'), 'workflow_sha': os.environ.get('GITHUB_WORKFLOW_SHA'), 'run_id': os.environ.get('GITHUB_RUN_ID'), 'run_attempt': os.environ.get('GITHUB_RUN_ATTEMPT'), @@ -495,7 +496,7 @@ def fixtures(): # Empty values would fall back to real development services in config.ts. overlays = ['.env.ci', 'firebase.json'] (ROOT / '.env.ci').write_text('''ENVIRONMENT=DEVELOPMENT -RELAY=https://127.0.0.1:9/ +RELAY=http://127.0.0.1:8765/ SIGNING_SERVER_TESTNET=https://127.0.0.1:9/ SIGNING_SERVER_MAINNET=https://127.0.0.1:9/ CHANNEL_URL=https://127.0.0.1:9/ diff --git a/.github/tests/keeper-local-validation.test.cjs b/.github/tests/keeper-local-validation.test.cjs index 959b8a6a70..c9abef7ddb 100644 --- a/.github/tests/keeper-local-validation.test.cjs +++ b/.github/tests/keeper-local-validation.test.cjs @@ -111,7 +111,7 @@ async function evaluate(comments, options = {}) { if (route === commentsRoute) return comments; if (route === runsRoute) { if (options.runsError) throw new Error(options.runsError); - return [...(options.runs || [])]; + return [...(options.runsByWorkflow?.[args.workflow_id] || options.runs || [])]; } if (route === jobsRoute) { if (options.jobsError) throw new Error(options.jobsError); @@ -539,8 +539,68 @@ test('hosted workflow lifecycle events refresh the privileged gate under the exa 'keeper-hosted-native.yml'), 'utf8'); const name = nativeWorkflow.match(/^name:\s*(.+)$/m)?.[1]; assert.ok(name, 'hosted workflow must have a name'); - assert.ok(lines.some(line => line.trim() === `workflows: [${name}]`), + assert.ok(lines.some(line => line.trim() === `workflows: [${name}, Keeper release acceptance]`), 'workflow_run must name the actual hosted workflow'); assert.ok(lines.some(line => line.trim() === 'types: [completed, requested, in_progress]'), 'new attempts must invalidate old hosted evidence promptly'); }); + +const reusableRun = overrides => hostedRun({ + path: '.github/workflows/keeper-release-acceptance.yml', + pull_requests: [{ ...hostedRun().pull_requests[0], number: 8000 }], ...overrides, +}); +const reusableJob = (platform, overrides) => hostedJob(platform, { + name: `${platform.toLowerCase()} / Native ${platform} build and probes`, + steps: ['Build development app', ...(platform === 'IOS' + ? ['Run disposable startup smoke', 'Run disposable native persistence probe'] + : ['Run Android startup and native persistence probes'])].map(name => ({ name, status: 'completed', conclusion: 'success' })), + ...overrides, +}); +const reusableTree = () => ({ ...trustedTree(), tree: [...trustedTree().tree, + { path: '.github/workflows/keeper-release-acceptance.yml', mode: '100644', type: 'blob', sha: '6'.repeat(40) }, + { path: '.github/workflows/keeper-native-platform.yml', mode: '100644', type: 'blob', sha: '7'.repeat(40) }, +] }); +const reusableOptions = overrides => ({ + context: { issue: { number: 8000 } }, + runs: [reusableRun()], jobs: [reusableJob('IOS'), reusableJob('ANDROID')], + trees: { [sha]: reusableTree(), [baseSha]: reusableTree(), [runBaseSha]: reusableTree() }, + ...overrides, +}); +test('reusable exact-head native scope can satisfy platform gate without claiming full matrix or hardware', async () => { + const { status } = await evaluate([handoff(sha, 'IOS, ANDROID')], reusableOptions()); + assert.equal(status.state, 'success'); + assert.match(status.description, /hosted IOS, ANDROID/); + const hardware = await evaluate([handoff()], reusableOptions()); + assert.equal(hardware.status.state, 'failure'); + assert.match(hardware.status.description, /HARDWARE/); +}); +test('reusable evidence requires probe steps, latest attempt and matching protected worker in both bases', async () => { + for (const overrides of [ + { jobs: [reusableJob('IOS', { steps: hostedJob('IOS').steps }), reusableJob('ANDROID')] }, + { jobs: [reusableJob('IOS', { run_attempt: 1 }), reusableJob('ANDROID')] }, + { runs: [reusableRun({ id: 91, conclusion: 'cancelled' }), reusableRun()] }, + { runs: [reusableRun({ id: 91, conclusion: 'skipped' }), reusableRun()] }, + { runs: [reusableRun({ path: '.github/workflows/keeper-native-diagnostic.yml' })] }, + ]) { + const { status } = await evaluate([handoff(sha, 'IOS, ANDROID')], reusableOptions(overrides)); + assert.equal(status.state, 'failure'); + } + for (const ref of [baseSha, runBaseSha]) { + const changed = reusableTree(); + changed.tree.find(e => e.path.endsWith('keeper-native-platform.yml')).sha = '8'.repeat(40); + const { status } = await evaluate([handoff(sha, 'IOS, ANDROID')], reusableOptions({ + trees: { [sha]: reusableTree(), [baseSha]: reusableTree(), [runBaseSha]: reusableTree(), [ref]: changed }, + })); + assert.equal(status.state, 'failure'); + } +}); +test('a newer unsuccessful reusable run supersedes older legacy successes across workflow routes', async () => { + const linked = { ...hostedRun().pull_requests[0], number: 8000 }; + const { status } = await evaluate([handoff(sha, 'IOS, ANDROID')], reusableOptions({ + runsByWorkflow: { + 'keeper-hosted-native.yml': [hostedRun({ pull_requests: [linked] })], + 'keeper-release-acceptance.yml': [reusableRun({ id: 91, conclusion: 'cancelled' })], + }, + })); + assert.equal(status.state, 'failure'); +}); diff --git a/.github/tests/native-action-pins.test.cjs b/.github/tests/native-action-pins.test.cjs index 0f58e98578..afb882b0bb 100644 --- a/.github/tests/native-action-pins.test.cjs +++ b/.github/tests/native-action-pins.test.cjs @@ -8,7 +8,8 @@ const test = require('node:test'); // Scope: workflows introduced by the bootstrap. Existing gate/action policy is // unchanged; this does not silently repin unrelated workflows. -const workflows = ['keeper-hosted-native.yml', 'keeper-validation-tests.yml']; +const workflows = ['keeper-hosted-native.yml', 'keeper-validation-tests.yml', + 'keeper-native-platform.yml', 'keeper-release-acceptance.yml', 'keeper-native-diagnostic.yml']; // Verified upstream refs/tags/v2.34.0^{} on 2026-10-18, also used by #7062. // The annotated tag object is 324029e2f414c084d8b15ba075288885e74aef9c. const emulatorCommit = '1dcd0090116d15e7c562f8db72807de5e036a4ed'; @@ -16,6 +17,7 @@ const emulatorAction = 'reactivecircus/android-emulator-runner'; // Other pins match the upstream tags noted in the workflows, read-only checked // on the same date. Updates require a new verified commit, not just SHA syntax. const verifiedPins = new Map([ + ['actions/github-script', 'ed597411d8f924073f98dfc5c65a23a2325f34cd'], ['actions/checkout', 'd23441a48e516b6c34aea4fa41551a30e30af803'], ['actions/setup-node', '249970729cb0ef3589644e2896645e5dc5ba9c38'], ['actions/setup-java', 'b6effb05e454b25005698d916606bdc6ffcbf961'], @@ -79,6 +81,7 @@ function validateActionPins(workflow) { const references = actionReferences(workflow); assert.ok(references.length > 0, 'expected external actions in bootstrap workflow'); for (const reference of references) { + if (reference === './.github/workflows/keeper-native-platform.yml') continue; assert.match(reference, /^[\w-]+\/[\w./-]+@[0-9a-f]{40}$/, `external actions require a full immutable commit: ${reference}`); const [action, revision] = reference.split('@'); diff --git a/.github/tests/native-diagnostic-policy.test.cjs b/.github/tests/native-diagnostic-policy.test.cjs new file mode 100644 index 0000000000..44de34a627 --- /dev/null +++ b/.github/tests/native-diagnostic-policy.test.cjs @@ -0,0 +1,89 @@ +'use strict'; +// Metadata-only policy checks; this file cannot supply native evidence. +const assert = require('node:assert/strict'); +const { test } = require('node:test'); +const fs = require('node:fs'); +const { spawnSync } = require('node:child_process'); +const vm = require('node:vm'); +const path = require('node:path'); +const parsed = spawnSync('python3', ['-B', '-c', ` +import json,pathlib,sys,yaml +print(json.dumps({p.name:yaml.load(p.read_text(),Loader=yaml.BaseLoader) for p in pathlib.Path(sys.argv[1]).glob('*.yml')})) +`, path.resolve(__dirname, '../workflows')], { encoding: 'utf8', timeout: 5000 }); +assert.equal(parsed.status, 0, parsed.stderr); +const workflows = JSON.parse(parsed.stdout); +const diagnostic = workflows['keeper-native-diagnostic.yml']; +const worker = workflows['keeper-native-platform.yml']; +function enabled(condition, { draft = null, labeled = false, sameRepo = true, number = 8000, eventName = 'pull_request', diagnostic = false, sameSha = true } = {}) { + const expression = condition + .replaceAll('github.event_name', 'eventName') + .replaceAll('github.event.pull_request.number', 'number') + .replaceAll('github.event.pull_request.head.repo.full_name == github.repository', 'sameRepo') + .replaceAll('github.event.pull_request.draft', 'draft') + .replaceAll("contains(github.event.pull_request.labels.*.name, 'keeper/native-diagnostic')", 'labeled') + .replaceAll('inputs.source_sha == github.event.pull_request.head.sha', 'sameSha') + .replaceAll('inputs.diagnostic', 'diagnostic') + .replaceAll('always()', 'true'); + return new Function('draft', 'labeled', 'sameRepo', 'number', 'eventName', 'diagnostic', 'sameSha', 'toJSON', `return (${expression});`)(draft, labeled, sameRepo, number, eventName, diagnostic, sameSha, JSON.stringify); +} +test('candidate diagnostics require explicit same-repository opt-in and serialize platforms', () => { + for (const name of ['ios', 'android']) { + const condition = diagnostic.jobs[name].if; + assert.equal(enabled(condition, { draft: true, labeled: true }), true); + for (const override of [{ labeled: false }, { sameRepo: false }, { number: 7045 }]) { + assert.equal(enabled(condition, { draft: true, labeled: true, ...override }), false); + } + } + assert.equal(diagnostic.jobs.android.needs, 'ios'); + assert.equal(diagnostic.concurrency['cancel-in-progress'], 'false'); + assert.equal(diagnostic.permissions.contents, 'read'); + assert.equal(diagnostic.permissions['pull-requests'], 'read'); +}); +test('reusable worker cannot silently run a draft, non-PR or wrong SHA', () => { + const condition = worker.jobs.native.if; + assert.equal(enabled(condition, { draft: false }), true); + assert.equal(enabled(condition, { draft: true, labeled: true, diagnostic: true }), true); + for (const override of [{ labeled: false }, { diagnostic: false }, { sameSha: false }, + { eventName: 'workflow_dispatch' }, { eventName: 'pull_request_target' }]) { + assert.equal(enabled(condition, { draft: true, labeled: true, diagnostic: true, ...override }), false); + } + for (const draft of [null, undefined, '', 'false', 'true', 0, 1, true]) { + assert.equal(enabled(condition, { draft }), false); + } +}); +test('current-source recheck denies moved head, removed opt-in and foreign repository', async () => { + const step = worker.jobs.native.steps.find(step => step.name === 'Verify current diagnostic source'); + const script = new vm.Script(`(async()=>{${step.with.script}})()`); + for (const mutation of [null, 'sha', 'labels', 'repo']) { + const pr = { head: { sha: 'a'.repeat(40), repo: { full_name: 'KeeperCommunity/bitcoin-keeper' } }, labels: [{ name: 'keeper/native-diagnostic' }] }; + if (mutation === 'sha') pr.head.sha = 'b'.repeat(40); + if (mutation === 'labels') pr.labels = []; + if (mutation === 'repo') pr.head.repo.full_name = 'fork/bitcoin-keeper'; + const failures = []; + await script.runInNewContext({ + context: { repo: { owner: 'KeeperCommunity', repo: 'bitcoin-keeper' }, payload: { pull_request: { number: 8000 } } }, + process: { env: { EXPECTED_SOURCE: 'a'.repeat(40) } }, + core: { setFailed: message => failures.push(message) }, + github: { rest: { pulls: { get: async () => ({ data: pr }) } } }, + }); + assert.equal(failures.length, mutation === null ? 0 : 1, String(mutation)); + } +}); +test('diagnostic workflow identity is rejected by the trusted release evidence verifier', async () => { + const { evidence } = require('../native/release_evidence.cjs'); + const sha = 'a'.repeat(40); + const repository = 'KeeperCommunity/bitcoin-keeper'; + const result = await evidence({ + rest: { actions: { listWorkflowRuns: 'runs' } }, + paginate: async () => [{ id: 12, run_attempt: 1, path: '.github/workflows/keeper-native-diagnostic.yml' }], + }, { repo: { owner: 'KeeperCommunity', repo: 'bitcoin-keeper' }, eventName: 'pull_request', runId: 12, + payload: { pull_request: { number: 8000, head: { sha, repo: { id: 1, full_name: repository } }, + base: { ref: 'sprint', repo: { id: 1, full_name: repository } } } } }, + { ios: 'success', android: 'success', attempt: 1 }); + assert.equal(result, false); + assert.ok(!diagnostic.jobs.acceptance); + for (const source of [diagnostic, worker]) { + assert.ok(!source.environment && !source.secrets); + assert.ok(Object.values(source.permissions).every(value => value === 'read')); + } +}); diff --git a/.github/tests/native-draft-policy.test.cjs b/.github/tests/native-draft-policy.test.cjs index 979034c2be..7af8f96d78 100644 --- a/.github/tests/native-draft-policy.test.cjs +++ b/.github/tests/native-draft-policy.test.cjs @@ -23,13 +23,16 @@ const guard = "github.event_name == 'pull_request' && toJSON(github.event.pull_r // Evaluate the actual checked-in expressions using only their two context // paths and toJSON. Missing context is null, as in Actions. These conditions // compare strings only, so JavaScript and Actions equality give the same result. -function allowed(condition, eventName, draft = null) { +function allowed(condition, eventName, draft = null, legacy = true) { assert.equal(typeof condition, 'string', 'an explicit condition is required'); const expression = condition + .replaceAll("contains(github.event.pull_request.labels.*.name, 'keeper/legacy-native')", 'legacy') + .replaceAll("contains(github.event.pull_request.labels.*.name, 'keeper/native-diagnostic')", 'false') + .replaceAll('always()', 'true') .replaceAll('github.event_name', 'eventName') .replaceAll('github.event.pull_request.draft', 'draft'); - return Boolean(new Function('eventName', 'draft', 'toJSON', `return (${expression});`)( - eventName, draft, JSON.stringify)); + return Boolean(new Function('eventName', 'draft', 'legacy', 'toJSON', `return (${expression});`)( + eventName, draft, legacy, JSON.stringify)); } function triggered(workflow, event, action, branch) { @@ -45,7 +48,8 @@ test('native opened, synchronize and reopened draft jobs are skipped before runn for (const action of ['opened', 'synchronize', 'reopened']) { assert.ok(triggered(native, 'pull_request', action, 'sprint')); for (const job of Object.values(native.jobs)) { - assert.equal(job.if, guard); + assert.ok(job.if.includes(guard)); + assert.equal(allowed(job.if, 'pull_request', false, false), false, 'legacy lane needs explicit label'); assert.equal(allowed(job.if, 'pull_request', true), false, action); assert.equal(allowed(job.if, 'pull_request', false), true, action); } @@ -76,18 +80,28 @@ test('native non-PR and missing or malformed draft contexts fail closed', () => } }); -test('bootstrap has no reusable native caller, worker or dispatch entry point', () => { - assert.deepEqual(Object.keys(workflows).sort(), [ - 'codeql.yml', 'keeper-hosted-native.yml', 'keeper-local-validation.yml', - 'keeper-validation-tests.yml', 'rag-ingest-release.yml', 'test.yml', - ]); - for (const [filename, workflow] of Object.entries(workflows)) { - assert.ok(!Object.hasOwn(workflow.on, 'workflow_call'), filename); - assert.ok(!Object.hasOwn(workflow.on, 'workflow_dispatch'), filename); - for (const job of Object.values(workflow.jobs)) assert.ok(!job.uses, filename); +test('reusable paths keep explicit draft and diagnostic boundaries with no dispatch', () => { + const worker = workflows['keeper-native-platform.yml']; + const acceptance = workflows['keeper-release-acceptance.yml']; + const diagnostic = workflows['keeper-native-diagnostic.yml']; + assert.deepEqual(Object.keys(worker.on), ['workflow_call']); + assert.equal(worker.on.workflow_call.inputs.diagnostic.default, 'false'); + assert.match(worker.jobs.native.if, /toJSON\(github.event.pull_request.draft\) == 'false'/); + assert.match(worker.jobs.native.if, /inputs.diagnostic && contains/); + for (const platform of ['ios', 'android']) { + assert.match(acceptance.jobs[platform].if, /toJSON\(github.event.pull_request.draft\) == 'false'/); + assert.equal(diagnostic.jobs[platform].with.diagnostic, 'true'); + assert.match(diagnostic.jobs[platform].if, /keeper\/native-diagnostic/); + } + assert.deepEqual(acceptance.jobs.android.needs, ['provenance', 'ios']); + assert.equal(diagnostic.jobs.android.needs, 'ios'); + assert.equal(diagnostic.concurrency['cancel-in-progress'], 'false'); + for (const filename of ['keeper-hosted-native.yml', 'keeper-native-platform.yml', + 'keeper-release-acceptance.yml', 'keeper-native-diagnostic.yml']) { + assert.ok(!Object.hasOwn(workflows[filename].on, 'workflow_dispatch'), filename); } - // #7062's reusable workflows are intentionally absent, not silently adopted. - // Any later addition requires an explicit extension of this entry-path review. + assert.match(workflows['android-diagnostic-build.yml'].jobs.build.if, /draft\) == 'false'/); + assert.match(workflows['android-diagnostic-build.yml'].jobs.build.if, /!contains/); }); test('pushing the unpublished branch triggers no checked-in push workflow', () => { @@ -100,32 +114,26 @@ test('draft opening and updates retain the expected host checks without a releas for (const action of ['opened', 'synchronize', 'reopened']) { const triggeredFiles = Object.entries(workflows).filter(([, workflow]) => triggered(workflow, 'pull_request', action, 'sprint')).map(([filename]) => filename).sort(); - assert.deepEqual(triggeredFiles, [ - 'codeql.yml', 'keeper-hosted-native.yml', 'keeper-validation-tests.yml', 'test.yml', - ]); + const expected = [ + 'android-diagnostic-build.yml', 'codeql.yml', 'keeper-hosted-native.yml', + 'keeper-release-acceptance.yml', 'keeper-validation-tests.yml', 'public-backend.yml', 'test.yml', + ]; + if (action === 'synchronize' || action === 'reopened') expected.push('keeper-native-diagnostic.yml'); + assert.deepEqual(triggeredFiles, expected.sort()); assert.ok(triggered(workflows['keeper-local-validation.yml'], 'pull_request_target', action, 'sprint')); } }); -test('draft Tests install cannot invoke native prepare and does not waive install or test failure', () => { +test('all Tests installs preserve current immutable script-disabled R4 setup and patches', () => { const steps = ci.jobs.test.steps; - const installs = steps.filter(step => step.run?.startsWith('yarn install')); - assert.equal(installs.length, 2); - for (const [event, draft, expected] of [ - ['pull_request', true, 'yarn install --frozen-lockfile --ignore-scripts'], - ['pull_request', null, 'yarn install --frozen-lockfile --ignore-scripts'], - ['pull_request', 'false', 'yarn install --frozen-lockfile --ignore-scripts'], - ['pull_request', false, 'yarn install'], ['push', null, 'yarn install'], - ]) { - const active = installs.filter(step => allowed(step.if, event, draft)); - assert.equal(active.length, 1); - assert.equal(active[0].run, expected); - } + const install = steps.find(step => step.name === 'Install dependencies'); + assert.match(install.run, /yarn install --frozen-lockfile --ignore-scripts --non-interactive/); + assert.equal(install.if, undefined); + assert.equal(steps.find(step => step.name === 'Apply repository dependency patches').run, + 'node_modules/.bin/patch-package --error-on-fail'); + assert.match(steps.find(step => step.name === 'Verify dependency inputs remain unchanged').run, + /git diff --exit-code HEAD/); assert.ok(!ci.jobs.test.if && !ci.jobs.test['continue-on-error']); for (const step of steps) assert.ok(!step['continue-on-error']); - const tests = steps.find(step => step.name === 'Run tests'); - assert.equal(tests.run, 'npm run test'); - assert.ok(!tests.if); - assert.equal(ci.jobs.lint.steps.find(step => step.name === 'Install dependencies').run, - 'yarn install --ignore-scripts'); + assert.equal(steps.find(step => step.name === 'Run tests').run, 'npm run test'); }); diff --git a/.github/workflows/android-diagnostic-build.yml b/.github/workflows/android-diagnostic-build.yml index 549cb39f92..06315cf8bb 100644 --- a/.github/workflows/android-diagnostic-build.yml +++ b/.github/workflows/android-diagnostic-build.yml @@ -18,6 +18,8 @@ concurrency: jobs: build: + # Candidate native diagnostics own their sequential platform schedule. + if: github.event_name != 'pull_request' || (toJSON(github.event.pull_request.draft) == 'false' && !contains(github.event.pull_request.labels.*.name, 'keeper/native-diagnostic')) runs-on: ubuntu-24.04 timeout-minutes: 90 diff --git a/.github/workflows/keeper-hosted-native.yml b/.github/workflows/keeper-hosted-native.yml index 3d198b099c..6fd390996e 100644 --- a/.github/workflows/keeper-hosted-native.yml +++ b/.github/workflows/keeper-hosted-native.yml @@ -3,7 +3,7 @@ name: Keeper hosted native validation on: pull_request: branches: [sprint] - types: [opened, synchronize, reopened, ready_for_review, converted_to_draft] + types: [opened, synchronize, reopened, ready_for_review, converted_to_draft, labeled, unlabeled] # PR code runs only in disposable, unprivileged jobs. No signing or service secrets. permissions: @@ -23,7 +23,7 @@ jobs: ios: name: Hosted iOS simulator # Skip before allocating a runner; missing/non-boolean draft state fails closed. - if: github.event_name == 'pull_request' && toJSON(github.event.pull_request.draft) == 'false' + if: github.event_name == 'pull_request' && toJSON(github.event.pull_request.draft) == 'false' && contains(github.event.pull_request.labels.*.name, 'keeper/legacy-native') && !contains(github.event.pull_request.labels.*.name, 'keeper/native-diagnostic') runs-on: macos-15 timeout-minutes: 65 env: @@ -80,8 +80,9 @@ jobs: retention-days: 7 android: + needs: ios name: Hosted Android emulator - if: github.event_name == 'pull_request' && toJSON(github.event.pull_request.draft) == 'false' + if: always() && github.event_name == 'pull_request' && toJSON(github.event.pull_request.draft) == 'false' && contains(github.event.pull_request.labels.*.name, 'keeper/legacy-native') && !contains(github.event.pull_request.labels.*.name, 'keeper/native-diagnostic') runs-on: ubuntu-24.04 timeout-minutes: 60 env: diff --git a/.github/workflows/keeper-local-validation.yml b/.github/workflows/keeper-local-validation.yml index af64b8c8a0..35f5fe018a 100644 --- a/.github/workflows/keeper-local-validation.yml +++ b/.github/workflows/keeper-local-validation.yml @@ -8,7 +8,7 @@ on: issue_comment: types: [created, edited, deleted] workflow_run: - workflows: [Keeper hosted native validation] + workflows: [Keeper hosted native validation, Keeper release acceptance] types: [completed, requested, in_progress] permissions: @@ -100,16 +100,20 @@ jobs: const hostedPasses = async () => { const passed = new Set(); try { - const workflowPath = '.github/workflows/keeper-hosted-native.yml'; + const workflowNames = ['keeper-hosted-native.yml', 'keeper-release-acceptance.yml']; const repository = owner + '/' + repo; if (pr.head.repo?.full_name !== repository) return passed; - const runs = await github.paginate(github.rest.actions.listWorkflowRuns, { - owner, repo, workflow_id: 'keeper-hosted-native.yml', - head_sha: sha, event: 'pull_request', per_page: 100 - }); + const groups = await Promise.all(workflowNames.map(workflow_id => + github.paginate(github.rest.actions.listWorkflowRuns, { + owner, repo, workflow_id, head_sha: sha, event: 'pull_request', per_page: 100 + }))); + const runs = [...new Map(groups.flat().map(run => [run.id, run])).values()]; // Do not fall back to an older success after a failed/cancelled // rerun or while newer evidence is pending. const run = runs.sort((a, b) => b.id - a.id)[0]; + const reusable = run?.path === '.github/workflows/keeper-release-acceptance.yml'; + const workflowPath = reusable ? '.github/workflows/keeper-release-acceptance.yml' : '.github/workflows/keeper-hosted-native.yml'; + if (reusable && pull_number === 7045) return passed; if (!run || run.head_sha !== sha || run.event !== 'pull_request' || run.path !== workflowPath || run.head_branch !== pr.head.ref || run.head_repository?.full_name !== repository || @@ -126,9 +130,12 @@ jobs: const fingerprint = async ref => { const { data } = await github.rest.git.getTree({ owner, repo, tree_sha: ref, recursive: '1' }); if (data.truncated) throw new Error('Truncated trusted CI tree'); - const entries = data.tree.filter(e => e.path === workflowPath || + const requiredWorkflows = reusable + ? [workflowPath, '.github/workflows/keeper-native-platform.yml', '.github/workflows/keeper-hosted-native.yml'] + : [workflowPath]; + const entries = data.tree.filter(e => requiredWorkflows.includes(e.path) || e.path.startsWith('.github/native/')); - if (!entries.some(e => e.path === workflowPath) || + if (!requiredWorkflows.every(path => entries.some(e => e.path === path && e.type === 'blob' && ['100644', '100755'].includes(e.mode))) || !entries.some(e => e.path.startsWith('.github/native/') && e.type === 'blob')) { throw new Error('Hosted native infrastructure not present in trusted base'); } @@ -142,14 +149,19 @@ jobs: const jobs = await github.paginate(github.rest.actions.listJobsForWorkflowRunAttempt, { owner, repo, run_id: run.id, attempt_number: run.run_attempt, per_page: 100 }); - for (const [platform, name] of [['IOS', 'Hosted iOS simulator'], + for (const [platform, legacyName] of [['IOS', 'Hosted iOS simulator'], ['ANDROID', 'Hosted Android emulator']]) { + const name = reusable ? `${platform.toLowerCase()} / Native ${platform} build and probes` : legacyName; const matches = jobs.filter(j => j.name === name); if (matches.length !== 1) continue; const job = matches[0]; if (job.head_sha !== sha || job.run_id !== run.id || job.run_attempt !== run.run_attempt || job.status !== 'completed' || job.conclusion !== 'success') continue; - const steps = ['Build development app', 'Run disposable startup smoke']; + const steps = reusable + ? ['Build development app', ...(platform === 'IOS' + ? ['Run disposable startup smoke', 'Run disposable native persistence probe'] + : ['Run Android startup and native persistence probes'])] + : ['Build development app', 'Run disposable startup smoke']; if (steps.every(name => job.steps?.filter(s => s.name === name && s.status === 'completed' && s.conclusion === 'success').length === 1)) { passed.add(platform); @@ -163,7 +175,7 @@ jobs: }; let state = 'failure'; - let description = 'Awaiting local Mac validation'; + let description = 'Awaiting exact-head hosted or local native validation'; if (docsOnly) { state = 'success'; description = 'Documentation/GitHub metadata only; local testing exempt'; diff --git a/.github/workflows/keeper-native-diagnostic.yml b/.github/workflows/keeper-native-diagnostic.yml new file mode 100644 index 0000000000..afb67b47f7 --- /dev/null +++ b/.github/workflows/keeper-native-diagnostic.yml @@ -0,0 +1,33 @@ +name: Keeper native candidate diagnostics + +on: + pull_request: + branches: [sprint] + types: [labeled, synchronize, reopened] + +# This explicit opt-in never writes status or supplies trusted native acceptance. +permissions: + contents: read + pull-requests: read + +concurrency: + group: keeper-native-diagnostic-${{ github.event.pull_request.number }} + cancel-in-progress: false + +jobs: + ios: + if: github.event.pull_request.number != 7045 && github.event.pull_request.head.repo.full_name == github.repository && contains(github.event.pull_request.labels.*.name, 'keeper/native-diagnostic') + uses: ./.github/workflows/keeper-native-platform.yml + with: + source_sha: ${{ github.event.pull_request.head.sha }} + platform: IOS + diagnostic: true + + android: + needs: ios + if: always() && github.event.pull_request.number != 7045 && github.event.pull_request.head.repo.full_name == github.repository && contains(github.event.pull_request.labels.*.name, 'keeper/native-diagnostic') + uses: ./.github/workflows/keeper-native-platform.yml + with: + source_sha: ${{ github.event.pull_request.head.sha }} + platform: ANDROID + diagnostic: true diff --git a/.github/workflows/keeper-native-platform.yml b/.github/workflows/keeper-native-platform.yml index 08e14b6986..c4004ee1ce 100644 --- a/.github/workflows/keeper-native-platform.yml +++ b/.github/workflows/keeper-native-platform.yml @@ -10,14 +10,20 @@ on: type: string required: true + diagnostic: + type: boolean + default: false + required: false + # This worker has no environment, release credentials, secret inputs or dispatch. permissions: contents: read + pull-requests: read jobs: native: name: Native ${{ inputs.platform }} build and probes - if: github.event_name == 'pull_request' && github.event.pull_request.number != 7045 && inputs.source_sha == github.event.pull_request.head.sha + if: github.event_name == 'pull_request' && github.event.pull_request.number != 7045 && inputs.source_sha == github.event.pull_request.head.sha && (toJSON(github.event.pull_request.draft) == 'false' || (inputs.diagnostic && contains(github.event.pull_request.labels.*.name, 'keeper/native-diagnostic'))) runs-on: ${{ inputs.platform == 'IOS' && 'macos-15' || 'ubuntu-24.04' }} timeout-minutes: 75 env: @@ -26,12 +32,24 @@ jobs: KEEPER_SOURCE_SHA: ${{ inputs.source_sha }} KEEPER_NATIVE_PLATFORM: ${{ inputs.platform }} MAESTRO_CLI_NO_ANALYTICS: '1' + KEEPER_DIAGNOSTIC: ${{ inputs.diagnostic }} steps: - name: Check out exact app source uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6 with: ref: ${{ inputs.source_sha }} persist-credentials: false + - name: Verify current diagnostic source + if: inputs.diagnostic + uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8 + env: + EXPECTED_SOURCE: ${{ inputs.source_sha }} + with: + script: | + const { data: pr } = await github.rest.pulls.get({ ...context.repo, pull_number: context.payload.pull_request.number }); + if (pr.head.sha !== process.env.EXPECTED_SOURCE || pr.head.repo.full_name !== `${context.repo.owner}/${context.repo.repo}` || !pr.labels.some(label => label.name === 'keeper/native-diagnostic')) { + core.setFailed('Diagnostic source moved or opt-in removed; no native credit'); + } - name: Set up Node uses: actions/setup-node@249970729cb0ef3589644e2896645e5dc5ba9c38 # v6 with: @@ -115,7 +133,7 @@ jobs: if: always() uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6 with: - name: keeper-release-native-${{ inputs.platform }}-${{ inputs.source_sha }} + name: keeper-${{ inputs.diagnostic && 'diagnostic' || 'release' }}-native-${{ inputs.platform }}-${{ inputs.source_sha }} path: artifacts/native/*/metadata.json if-no-files-found: error retention-days: 7 diff --git a/.github/workflows/keeper-release-acceptance.yml b/.github/workflows/keeper-release-acceptance.yml index 06055cb941..313e68737e 100644 --- a/.github/workflows/keeper-release-acceptance.yml +++ b/.github/workflows/keeper-release-acceptance.yml @@ -3,7 +3,7 @@ name: Keeper release acceptance on: pull_request: branches: [sprint] - types: [opened, synchronize, reopened, ready_for_review] + types: [opened, synchronize, reopened, ready_for_review, converted_to_draft, labeled, unlabeled] permissions: contents: read @@ -39,15 +39,15 @@ jobs: ios: needs: provenance - if: needs.provenance.outputs.trusted == 'true' + if: toJSON(github.event.pull_request.draft) == 'false' && !contains(github.event.pull_request.labels.*.name, 'keeper/legacy-native') && !contains(github.event.pull_request.labels.*.name, 'keeper/native-diagnostic') && needs.provenance.outputs.trusted == 'true' uses: ./.github/workflows/keeper-native-platform.yml with: source_sha: ${{ github.event.pull_request.head.sha }} platform: IOS android: - needs: provenance - if: needs.provenance.outputs.trusted == 'true' + needs: [provenance, ios] + if: toJSON(github.event.pull_request.draft) == 'false' && !contains(github.event.pull_request.labels.*.name, 'keeper/legacy-native') && !contains(github.event.pull_request.labels.*.name, 'keeper/native-diagnostic') && needs.provenance.outputs.trusted == 'true' uses: ./.github/workflows/keeper-native-platform.yml with: source_sha: ${{ github.event.pull_request.head.sha }} diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index eb40cb71af..1db67499cf 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -8,7 +8,6 @@ on: - main - release/2.6.3 - release/2.6.4 - types: [opened, synchronize, reopened, ready_for_review] pull_request: branches: - sprint diff --git a/AGENTS.md b/AGENTS.md index 9f8628aa9b..2f1b4d83e5 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -1,5 +1,14 @@ # Bitcoin Keeper: Codex workflow +## Current R5 owner direction (22 October 2026) + +R5 and subsequent ordinary development/native validation run in Cloud. Use hosted macOS/Xcode for iOS and hosted Android development/emulator runners; the owner's personal Mac is not a prerequisite. Preserve genuine phone/signer checks separately. The older Local handoff format below remains a compatibility record for the existing gate, not a request for the owner's Mac. + +Candidate diagnostics are explicit read-only runs on the exact PR head. Add `keeper/native-diagnostic` only when scheduling the isolated integration candidate; the new workflow serializes iOS then Android and does not cancel active native work. Pre-merge diagnostic success never grants protected-base native acceptance. After approved installation, `Keeper release acceptance` is the default reusable worker route; `keeper/legacy-native` selects the serialized legacy startup fallback instead. New commits invalidate previous source evidence. Missing app feature/UI/backend/physical checks stay explicit and blocking where required. + +Never merge, promote a shared branch, release-sign, upload, distribute, deploy or publish without the owner's explicit approval. Draft PR creation and isolated branch pushes are preparation, not release approval. See `docs/r5-cloud-native-reconciliation.md` for source identities and outstanding acceptance. + + The prepared workflow is **Cloud code complete → hosted native validation → authorized physical checks → release review**. Until the hosted infrastructure is reviewed, installed and verified, retain the existing Local path below as the safe fallback; do not assume the owner's Mac is available. Signing remains a separate approved action. Follow the [bootstrap adoption order](docs/cloud-bootstrap-preparation.md), including the later restacking of Phase A #7062. The GitHub pull request is the durable handoff between stages. Do **not** rely on one agent having access to another agent's conversation, unpushed changes, machine, or installed tools. Repository: `KeeperCommunity/bitcoin-keeper`. Use `sprint` as the PR base unless the task explicitly specifies another base. diff --git a/docs/r5-cloud-native-reconciliation.md b/docs/r5-cloud-native-reconciliation.md new file mode 100644 index 0000000000..a8ddf3cbe9 --- /dev/null +++ b/docs/r5-cloud-native-reconciliation.md @@ -0,0 +1,37 @@ +# R5 Cloud native infrastructure reconciliation + +This isolated package adopts completed infrastructure into the released-R4/R5 source. It does not merge or alter producer branches, install a trusted baseline, grant full app acceptance, or perform release operations. Ordinary iOS and Android validation uses hosted runners; the owner's personal Mac is not required. + +## Sources and separate reviews + +- App input: R5 core #7058 through `77b72da29ac5f2cdf3f51c9a0e9f3fd1c2af7238`, plus completed 5.1 #7067 through `c1c4b803039b61b940373c4dc9d08473cf25e042`. Released R4 `1d8a86a7462d935cbd0e28b857934b5718c152fc` is already reconciled. Work-package labels are not app versions. +- [#7063 bootstrap/privacy patch](https://github.com/KeeperCommunity/bitcoin-keeper/pull/7063/files), source `0984c5b97e7e8326d6856ba662274ecf325e5e0f`: source-linked cherry-pick preserves fixed diagnostic vocabularies, privacy regressions, draft guards and corrected emulator pin. Newer R4 frozen/script-disabled installs, dependency patching and release guards are preserved in `test.yml`. +- [#7062 reusable foundation delta](https://github.com/KeeperCommunity/bitcoin-keeper/compare/d865ca9c4f91532a1524443cc6da42d8d7b07e1a...59c821ce195a5c063d99045fd97b5ac605433f62), source `59c821ce195a5c063d99045fd97b5ac605433f62`: source-linked cherry-pick adds shared worker, provenance verifier and separate native storage probe without reverting #7063 privacy fixes. +- [#7060 native repairs](https://github.com/KeeperCommunity/bitcoin-keeper/pull/7060/files), source `d865ca9c4f91532a1524443cc6da42d8d7b07e1a`: narrow adoption aligns NitroModules 0.36.5 with existing IAP 16.5.0 and installs the standard RN dependency provider. Newer R4 crypto dependencies, app/release versions, Podfile retired-credential guard and current Podfile/Hermes checksums remain preserved; CocoaPods is matched to the runner's 1.16.2. Exact-candidate deployment-mode installation must prove those inputs. + +These inherited packages exceed one small review; their original patch-only reviews and source-linked commits remain authoritative review units. The new reconciliation delta is separately committed and bounded to orchestration/gate/source setup. Product native feature scenarios and full-product timing are separate implementation packages, as is protected signing/distribution preparation. + +## Execution routes + +`Keeper release acceptance` is the reusable default after approved protected-base installation. Native jobs require a real non-draft PR, exact source checkout and reviewed CI fingerprints matching current and run bases. Platforms execute sequentially. Full Phase A acceptance remains BLOCKED until each product requirement has genuine evidence; native-storage or helper probes cannot grant full app, backend, crypto, UI or release acceptance. + +`Keeper native candidate diagnostics` allows pre-merge execution on a same-repository draft with explicit `keeper/native-diagnostic` label. The coordinator verifies current head and existing active native jobs before labeling. The worker rechecks current source and opt-in before setup. iOS runs first, then Android; concurrency does not cancel an active candidate job. A synchronize event creates a new exact-source run rather than transferring evidence. Remove the label before changing the candidate if another run should not be queued. Diagnostic results are metadata only, have separate artifact names, and are excluded from trusted gate evidence. + +The former startup workflow requires `keeper/legacy-native`; this selects the serialized startup fallback instead of the reusable default. Inherited Android production-debug diagnostic builds skip drafts and the candidate-diagnostic label, preventing a second automatic candidate build. The coordinator must still check other already-running workflows and schedule only one native build at a time. No active job was cancelled by this package. + +The trusted validation gate accepts exact-head successful platform build/UI/probe jobs from the reviewed reusable workflow or legacy startup fallback. It queries both routes, uses only the newest run and current attempt, and checks the full native harness plus executable worker against protected current/run bases. A newer skipped/cancelled/failed run cannot reuse an older successful run. A diagnostic run, absent worker, changed CI, stale SHA or build-only result provides no credit. Hardware requirements, actual failures, owner-scoped deferrals and local fallback remain binding. This status is platform scope only; it cannot approve full feature acceptance or release. + +## Disposable data, evidence and limits + +The app is unsigned on iOS Simulator and uses the public debug key on Android. Synthetic Firebase identities and blocked loopback service configuration isolate product accounts. The loopback Relay URL is `http://127.0.0.1:8765/` solely for the separately reviewed disposable product UI measurement responder; without that responder requests fail. That responder is controlled transport and supplies no paired-backend/protected-backup compatibility evidence. Other configured services remain blocked loopback destinations. + +Only bounded metadata is uploaded, with source/run/attempt, toolchain, artifact hashes, safe scenario statuses and raw measurement numbers. Raw logs, screenshots, UI hierarchies, application databases, generated key material and app bundles remain on the disposable runner. Frozen lockfiles and repository dependency patches stay required. Existing Yarn/Gradle caches hold development dependencies, never accounts, keychains or signing material. No production account operation or credential copying occurs. + +## Current evidence + +- #7060 historical native success at its own `d865ca9c4f91532a1524443cc6da42d8d7b07e1a`, run `39024530330`, is retained solely as infrastructure history. It does not validate R5 source. +- #7063 source `0984c5b97e7e8326d6856ba662274ecf325e5e0f`: native skipped; CI Tests failed frozen install before tests (run `39105347518`, job `117609865702`). The stale excluded dependency baseline is inherited, not a privacy-regression pass. +- #7062 source `59c821ce195a5c063d99045fd97b5ac605433f62`: no PR native run/status returned at readback. Its full product matrix was explicitly unimplemented. +- This reconciliation: host policy/negative/provenance tests and semantic workflow lint are verified separately at its final commit. Actual R5 iOS/Android native build, feature checks, full-product startup/first-wallet timing and physical-device results remain NOT RUN until exact-candidate execution. Full Phase A and release readiness remain BLOCKED. + +Next: integrate independently reviewed scenario/product-measurement packages and completed 5.2 only after its authoritative checkpoint; verify ancestry and combined head; finish active inherited native jobs; schedule one candidate diagnostic run; retain all failures and rerun affected checks after any correction. Protected-base adoption/required-check configuration needs explicit approval and administrative verification. No merge, signing, upload, distribution, deployment or publication is authorized here. diff --git a/ios/Podfile.lock b/ios/Podfile.lock index f53f6d2a91..7fa62b9bbc 100644 --- a/ios/Podfile.lock +++ b/ios/Podfile.lock @@ -193,7 +193,7 @@ PODS: - ReactCommon/turbomodule/core - SocketRocket - Yoga - - NitroModules (0.35.4): + - NitroModules (0.36.5): - boost - DoubleConversion - fast_float @@ -3995,7 +3995,7 @@ SPEC CHECKSUMS: nanopb: fad817b59e0457d11a5dfbde799381cd727c1275 NitroIap: 2aab68488ccce3c0d6046f0e896b3ed4a5838314 NitroMmkv: 1f66a0e71a3a3c5677921aca853aabf7911d92c1 - NitroModules: b632f83915f059473b72b5d6fb90ff2c2ca674c1 + NitroModules: 5115360c2954442d8a6a4fafe836f7a5458815df openiap: 253070b2afe71078a2cae63a2b8e282bdd44a059 PDFGenerator: 17d6bc525db0a3fcd156fbf00f9d1b8d5cc75d1e PromisesObjC: f5707f49cb48b9636751c5b2e7d227e43fba9f47 @@ -4115,4 +4115,4 @@ SPEC CHECKSUMS: PODFILE CHECKSUM: 79b67f9496d66b3a6a618591c809ed441d1032f6 -COCOAPODS: 1.15.2 +COCOAPODS: 1.16.2 diff --git a/ios/hexa_keeper/AppDelegate.mm b/ios/hexa_keeper/AppDelegate.mm index 2e26ca1a97..ff82bdb835 100644 --- a/ios/hexa_keeper/AppDelegate.mm +++ b/ios/hexa_keeper/AppDelegate.mm @@ -2,12 +2,14 @@ #import #import +#import #import @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { + self.dependencyProvider = [RCTAppDependencyProvider new]; self.moduleName = @"hexa_keeper"; // You can add your custom initial props in the dictionary below. // They will be passed down to the ViewController used by React Native. @@ -62,4 +64,4 @@ - (BOOL)application:(UIApplication *)application continueUserActivity:(nonnull N restorationHandler:restorationHandler]; } -@end \ No newline at end of file +@end diff --git a/package.json b/package.json index 73041aa613..a5bb6e05a5 100644 --- a/package.json +++ b/package.json @@ -106,7 +106,7 @@ "react-native-mmkv": "4.3.0", "react-native-modal": "^13.0.1", "react-native-nfc-manager": "3.16.0", - "react-native-nitro-modules": "0.35.4", + "react-native-nitro-modules": "0.36.5", "react-native-pdf": "7.0.4", "react-native-qrcode-svg": "6.3.2", "react-native-randombytes": "3.6.1", diff --git a/yarn.lock b/yarn.lock index 563255662a..f9694174ee 100644 --- a/yarn.lock +++ b/yarn.lock @@ -11094,10 +11094,10 @@ react-native-nfc-manager@3.16.0: dependencies: "@expo/config-plugins" "~8.0.0" -react-native-nitro-modules@0.35.4: - version "0.35.4" - resolved "https://registry.yarnpkg.com/react-native-nitro-modules/-/react-native-nitro-modules-0.35.4.tgz#eff7f3e1e279b44568850300e9789af3fc30facd" - integrity sha512-4qZa+1kgR/sPRNZv+UShxyArEPpovWxw76Dfd/DtCVtkQ92wOOxGIzdYvndprabd+t+r8zNYgYEPYE74gzkuVQ== +react-native-nitro-modules@0.36.5: + version "0.36.5" + resolved "https://registry.yarnpkg.com/react-native-nitro-modules/-/react-native-nitro-modules-0.36.5.tgz#438c77e4f1e77bd1d3e901eb5b4953762e68a078" + integrity sha512-SJby84+hovD70JmQ1fq+56tw3oapJeChehUqXA4Gqjm+ktJhMAcj8LGwoAjStSOUwRaK7czbzBPB0Widj+EDoQ== react-native-pdf@7.0.4: version "7.0.4" From 90cc528e81021ca6b7415c1c7d698b49584c0ce4 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Thu, 22 Oct 2026 06:15:45 +0000 Subject: [PATCH 220/254] ci: prepare protected cloud release authorization and readiness --- .../keeper-protected-release-plan.yml | 100 +++++++++++++ docs/r5-cloud-release-readiness.md | 113 +++++++++++++++ docs/r5-support-copy-draft.md | 28 ++++ scripts/cloud_release_inventory.py | 135 ++++++++++++++++++ scripts/protected-release-plan.cjs | 122 ++++++++++++++++ scripts/protected-release-plan.test.cjs | 82 +++++++++++ scripts/test_cloud_release_inventory.py | 86 +++++++++++ 7 files changed, 666 insertions(+) create mode 100644 .github/workflows/keeper-protected-release-plan.yml create mode 100644 docs/r5-cloud-release-readiness.md create mode 100644 docs/r5-support-copy-draft.md create mode 100644 scripts/cloud_release_inventory.py create mode 100644 scripts/protected-release-plan.cjs create mode 100644 scripts/protected-release-plan.test.cjs create mode 100644 scripts/test_cloud_release_inventory.py diff --git a/.github/workflows/keeper-protected-release-plan.yml b/.github/workflows/keeper-protected-release-plan.yml new file mode 100644 index 0000000000..e14b026625 --- /dev/null +++ b/.github/workflows/keeper-protected-release-plan.yml @@ -0,0 +1,100 @@ +name: Protected release preparation (no signing or upload) + +on: + workflow_dispatch: + inputs: + source_sha: + description: Full reviewed immutable candidate commit + required: true + release_ref: + description: Protected release/version branch at that exact commit + required: true + operation: + description: Operation to prepare; this workflow cannot execute it + type: choice + options: [ios-signed-build, android-signed-build, testflight-upload, play-internal-upload] + required: true + version: + description: Exact marketing version + required: true + build: + description: Exact platform build counter + required: true + artifact_sha256: + description: Artifact digest for upload preparation; pending for signed-build preparation + required: true + default: pending + approval_comment_id: + description: Existing owner approval comment ID with exact scope and expiry + required: true + +permissions: + contents: read + actions: read + checks: read + statuses: read + issues: read + +concurrency: + group: keeper-protected-release-preparation + cancel-in-progress: false + +jobs: + authorize: + runs-on: ubuntu-latest + timeout-minutes: 10 + outputs: + environment: ${{ steps.plan.outputs.environment }} + steps: + - name: Checkout installed trusted driver + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7 + with: + ref: ${{ github.sha }} + path: driver + persist-credentials: false + - name: Use the reviewed Node runtime + uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7 + with: + node-version: '22.23.3' + - name: Validate request before candidate checkout + env: + KEEPER_SOURCE_SHA: ${{ inputs.source_sha }} + KEEPER_RELEASE_REF: ${{ inputs.release_ref }} + KEEPER_OPERATION: ${{ inputs.operation }} + KEEPER_VERSION: ${{ inputs.version }} + KEEPER_BUILD: ${{ inputs.build }} + KEEPER_ARTIFACT_SHA256: ${{ inputs.artifact_sha256 }} + KEEPER_APPROVAL_COMMENT_ID: ${{ inputs.approval_comment_id }} + run: | + node -e "require('./driver/scripts/protected-release-plan.cjs').requestFromEnvironment(process.env)" 2>/dev/null || { echo 'Invalid preparation request'; exit 1; } + - name: Read candidate files without executing candidate scripts + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7 + with: + ref: ${{ inputs.source_sha }} + path: candidate + persist-credentials: false + - name: Check exact source, owner receipt and protected setup + id: plan + env: + GH_TOKEN: ${{ github.token }} + KEEPER_CANDIDATE_PATH: candidate + KEEPER_SOURCE_SHA: ${{ inputs.source_sha }} + KEEPER_RELEASE_REF: ${{ inputs.release_ref }} + KEEPER_OPERATION: ${{ inputs.operation }} + KEEPER_VERSION: ${{ inputs.version }} + KEEPER_BUILD: ${{ inputs.build }} + KEEPER_ARTIFACT_SHA256: ${{ inputs.artifact_sha256 }} + KEEPER_APPROVAL_COMMENT_ID: ${{ inputs.approval_comment_id }} + run: node driver/scripts/protected-release-plan.cjs + + protected-plan: + needs: authorize + runs-on: ubuntu-latest + timeout-minutes: 5 + environment: ${{ needs.authorize.outputs.environment }} + permissions: {} + steps: + - name: Record preparation boundary + run: | + echo 'Protected authorization plan prepared. Signing and upload workers are NOT IMPLEMENTED.' + echo 'Artifact bytes/signatures remain unverified. No release action is permitted or executed here.' diff --git a/docs/r5-cloud-release-readiness.md b/docs/r5-cloud-release-readiness.md new file mode 100644 index 0000000000..f47d749af6 --- /dev/null +++ b/docs/r5-cloud-release-readiness.md @@ -0,0 +1,113 @@ +# R5 protected cloud release preparation + +This is the separate Phase B preparation package for [#7061](https://github.com/KeeperCommunity/bitcoin-keeper/issues/7061), following [#7065](https://github.com/KeeperCommunity/bitcoin-keeper/issues/7065). It does not install or execute signing or distribution. **Hosted signing/distribution: NOT IMPLEMENTED / NOT VERIFIED.** The personal Mac is not a prerequisite in the proposed route. Existing app, native acceptance, exact-head gate and physical-device requirements remain separate. + +Source inspected: R5 core `77b72da29ac5f2cdf3f51c9a0e9f3fd1c2af7238` and 5.1 `c1c4b803039b61b940373c4dc9d08473cf25e042`, containing released R4 `1d8a86a7462d935cbd0e28b857934b5718c152fc`. This package does not establish whether 5.2 is complete or integrated. App inputs are still 2.6.4 / Android627 / iOS620; no R5 version/build counters are assigned here. Work-package names 5.1 and 5.2 are not versions. + +## Verified inventory, 22 October 2026 + +| Existing source/capability | Evidence and consequence | +| --- | --- | +| iOS `build_only` | Calls `get_certificates`, `get_provisioning_profile`, project provisioning and `build_app`; this is a **signed build**, requiring approval before invocation. It is not a safe unsigned preflight. | +| iOS `live` / `dev` | Invoke TestFlight upload. `dev` also downloads environment configuration and increments a build number. Do not use either as a native test command. | +| Android `build_only` | Builds signed production AAB/APK using keystore/password inputs and verifies resulting artifact versions. Requires signing approval. | +| Android `live` | Calls `upload_to_play_store` for the internal track after its build. Internal upload is distribution and requires separate explicit approval. | +| Android release signing block | Reads keystore/password/alias from environment. No literal production password/alias was found in the inspected release block. Android public debug credentials remain separate. This bounded review is not proof that every historical credential is safe or current. | +| Protected GitHub environments | Read-only environment API returned `copilot`, `Env`, `keeper-claude-review`. No dedicated iOS/Android release environment was present. No environments or rules were changed. | +| Secret/variable inventory | Repository secret-name and variable-name API reads returned HTTP403. Contents were never requested. Existing values, owners, scope and usability are **UNKNOWN**, not proven absent. Managed task runtime reports no configured secrets/identities; that does not describe store or repository credentials. | +| Reviewer route | `keeper-claude-review` exists with a branch policy. This does not prove Claude is enabled, paid capacity approved, review executed or findings resolved. Existing independent/Claude review requirement remains open; no paid request was initiated. | + +The read-only helper can refresh bounded source/configuration evidence without reading keys or secret values: + +```sh +python3 scripts/cloud_release_inventory.py --root . +gh api repos/KeeperCommunity/bitcoin-keeper/environments > /tmp/keeper-environments.json +python3 scripts/cloud_release_inventory.py --root . --environment-metadata /tmp/keeper-environments.json +python3 -B -m unittest discover -s scripts -p 'test_cloud_release_inventory.py' +``` + +The helper never invokes Fastlane, builds, signs, logs in, uploads, changes configuration or grants permission. Its JSON always has `execution_allowed: false`; a named environment and a reviewer rule cannot prove exact source restriction, credential custody or functioning enforcement. Missing environment metadata stays UNKNOWN. Literal-credential findings include only field/line, never values; investigate any finding through [private vulnerability reporting](../SECURITY.md). This is a narrow source inventory, not a secret scanner or release gate. + +## Prepared reusable authorization workflow + +The candidate [protected preparation workflow](../.github/workflows/keeper-protected-release-plan.yml) implements manual dispatch, a secret-free trusted-driver authorization job, and a protected environment review boundary. It is **not installed on sprint and was not dispatched**. It has no signing or upload command and passes no production secrets to either job. + +The executable [gate](../scripts/protected-release-plan.cjs) requires all of: + +- Execution from the current protected `sprint` driver commit; no PR/mutable driver acceptance. +- An exact immutable candidate SHA at the current protected `release/` head; candidate files are read as data and no candidate script executes. +- Fresh authenticated-store baseline metadata and consistent package, Android and iOS versions/builds using the existing release-version validator. +- A current writer-authorized release-owner (`antuz123`) approval comment binding driver SHA, candidate SHA, version/build, specific operation and artifact digest (or `pending` for build preparation), expiring within24hours. An approval for one operation cannot authorize another. +- The matching protected release environment already configured with independent reviewers, prevented self review, no administrative bypass, and protected-branch restriction. An absent/unreadable environment blocks before the environment job; no implicit unprotected environment is created by a successful path. +- Exact-head `Tests`, `CodeQL` and `keeper/local-validation` success, with no failed/skipped/in-progress/wrong-SHA check, and complete bounded check evidence. These fixed required names need confirmation against the final installed acceptance route; an unmatched name fails closed. + +The final protected job records the preparation boundary only. Its result always reports `execution_allowed: false`, `release_worker_implemented: false` and `artifact_bytes_and_signature_verified: false`. The artifact approval digest is bound to the request; this preparation package does not download/verify actual release bytes or signatures. Environment selection and shape-valid metadata alone never produce a release pass. + +The owner approval receipt has this exact form (use actual reviewed identities; no receipt is created or requested by this package): + +```text +## Keeper protected release approval +State: APPROVED +Source commit: +Version: +Build: +Operation: +Artifact SHA256: <64-character digest, or pending for signed-build preparation> +Workflow commit: +Expires at: +``` + +This template is not owner approval. Do not post it on the owner's behalf. Tests use synthetic receipts and perform no remote action. Run the offline contracts with `node --test scripts/protected-release-plan.test.cjs`. + +## Remaining execution worker implementation + +Implement the actual Phase B signing/artifact/distribution workers in a further separate PR, estimated 5k–10k changed code/test tokens plus platform integration fixes. Trim/split if it approaches 20k or adds another substantial capability. Native acceptance belongs to Phase A. No workflow that can reach signing or stores is installed by this preparation package. + +1. A reusable manual release driver, installed on reviewed trusted source, accepts an immutable full source SHA, the approved operation/channel, version/build tuple, and exact evidence/artifact identities. Its initial job has read-only repository permissions and no signing/store secrets. Resolve and compare the source against the reviewed candidate; reject PR merge refs, mutable refs, wrong repositories, stale head/evidence, missing required tests/security/review, and changed inputs. Verify that the workflow driver itself is the installed trusted revision. Do not accept a PR-authored JSON assertion as trusted CI evidence. +2. Build/test with development/disposable configuration first on hosted macOS/Xcode and Linux Android runners, one native build at a time. Retain source/tree/input identities, exact test run/attempt, platform/tool versions and artifact hashes. Rerun candidate acceptance when source changes. An infrastructure smoke cannot satisfy R5 feature acceptance. +3. Proposed environments `keeper-release-ios` and `keeper-release-android` restrict deployment source to explicitly reviewed release refs, require an independent named approver, prevent self approval and review administrative bypass policy. Configure least-privileged secrets only after a credential custodian and owner approve provisioning. Test rejection of wrong source and absent approval without secrets before testing signing. Environment creation or selection alone does not establish enforcement. +4. Sign only after owner approval naming source, version/build, platform and operation. Prefer separate short-lived signing workers with narrowly scoped credentials and transient keychain/keystore storage. Do not run unreviewed PR/dependency scripts with production secrets. Current `build_only` lanes need adaptation and actual hosted verification; iOS automatic certificate/profile acquisition must not create or rotate material implicitly. Preserve Android upload-key and signing-certificate continuity. Do not regenerate keys. +5. Verify signed artifacts before any upload: source/build manifest, application ID, version/build, SHA256, iOS signing team/profile/entitlements and Android certificate/signature/16KB evidence. Retain provenance and safe receipts. Secret files, recovery data and unredacted native logs must never enter public artifacts or caches. Clean transient signing material as part of the same job, including failure paths, without deleting unrelated caches or owner artifacts. +6. Upload the verified immutable artifact only after a separate approval for TestFlight or Play internal. An upload worker uses least-privileged channel-specific credentials and the approved artifact digest; it never rebuilds from a moving branch. Compare the returned store-selected version/build/artifact receipt. No production rollout, review submission or public release follows automatically. Each remains a separate explicitly approved operation. + +This is a concrete proposed boundary, not proven remote signing capability. Required native/security failures remain blocking; physical-device results must come from actual testers/devices tied to the distributed artifact. No personal Mac session is needed for build, certificate custody service, store API or tester installation in this route. + +## One-time setup and custody inventory + +All entries below need a named maintainer/custodian and evidence of actual readiness. Do not place values in PRs, task sheets, job inputs or this document. + +| Capability | Required input/setup | Observed state / next action | +| --- | --- | --- | +| Trusted release source/driver | Approved release ref policy, protected environments, independent approval, exact-SHA evidence/check enforcement | Dedicated environments absent at read; authorization workflow is a candidate, uninstalled/unrun; execution workers unimplemented. Review/approve separate worker PR and administrative rules. No shared-branch promotion authorized. | +| Apple signing | Existing authorized signing certificate/private-key custody, matching profile and team/entitlements; approve hosted secret/signing-service scope | UNKNOWN. Existing Fastlane acquisition is not proof of authorized custody. Provision through authorized custodian; do not copy a personal keychain or create a new certificate by default. | +| Apple upload | App Store Connect identity/API key with app/channel privileges, existing app identity and agreements | UNKNOWN. API upload authorization is distinct from code signing. Inventory and test via approved operation; no interactive owner-Mac dependency. | +| Android signing | Existing upload keystore/service and alias, `STORE_PASSWORD`, `KEY_PASSWORD`, `KEEPER_ANDROID_KEYSTORE`; verify expected certificate fingerprint | UNKNOWN. Current code consumes these names; existence/current validity is unverified. Preserve existing-user update continuity. Play App Signing key is distinct from upload key. | +| Play internal upload | Existing app/package, least-privileged approved service account, selected internal track/testers, current store counters | UNKNOWN. Existing Appfile refers to a JSON file; no file contents inspected. Move custody into reviewed protected storage only with authorization. | +| Production app configuration | Reviewed exact allowlist of client-safe fields, production service identities/channel and successful version/config guards | UNKNOWN for R5 signed candidate. `react-native-config` embeds supplied fields: never place server/signing/upload secrets there. Preserve the existing retired-credential rejection. | +| Artifact retention/provenance | Approved safe retention, authenticated checksums/signature receipts and trusted artifact transport | Source helpers exist; full signed hosted chain NOT VERIFIED. Do not transfer a pass between SHAs. | +| Physical acceptance | Named trusted testers, actual supported phones/signers, firmware/artifact identity and results | NOT RUN by this package. Emulator biometric simulation cannot establish physical biometrics/NFC/USB/camera/signers. | +| Independent and Claude review | Applicable approved review route, authorized capacity, exact-source report and finding resolution | Required; execution/approval NOT VERIFIED. No paid usage requested. Preserve inherited security findings and failed checks. | + +Before signing, refresh authenticated complete Apple/Play uploaded-build baselines using the existing release-version tooling; do not infer the next counters from current source. Preserve approval and channel receipts separately from native acceptance. No merge, signing, upload, distribution, deployment or publication was performed in this package. + +## PHP security research and exposure assessment + +The DigitalOcean bulletin itself has not been provided/located; its date alone cannot identify a vulnerability or Keeper exposure. Independent official upstream research did identify two current critical PHP advisories published 21 October 2026: + +- [CVE-2026-109618 / GHSA-w8hw-w9fx-j79w](https://github.com/php/php-src/security/advisories/GHSA-w8hw-w9fx-j79w), “RCE via OOM corruption”. +- [CVE-2026-109619 / GHSA-6wmf-4rv5-p3rp](https://github.com/php/php-src/security/advisories/GHSA-6wmf-4rv5-p3rp), “RCE via stale global state”. + +Both official advisories list patched branch versions **8.2.35, 8.3.36, 8.4.27, 8.5.12**. The [official 8.5.12 release](https://github.com/php/php-src/releases/tag/php-8.5.12) describes unauthenticated RCE, no complete mitigation and strongly encourages affected PHP8.5 users to update. [Tagged NEWS](https://github.com/php/php-src/blob/php-8.5.12/NEWS) records both fixes; [8.2.35 NEWS](https://github.com/php/php-src/blob/php-8.2.35/NEWS) confirms the backport. This identifies relevant upstream research, **not the missing bulletin's exact contents or an impact determination**. + +Read-only complete Git tree and dependency-manifest inspection found: + +| Public source audited | Exact source | PHP/Composer evidence | +| --- | --- | --- | +| R5 app | `c1c4b803039b61b940373c4dc9d08473cf25e042` | 0 tracked PHP files, 0 Composer manifests; React Native/JS/TS source. No PHP-named direct dependency. | +| Website | `ddba13bd289e8899b16a6f653cf5c1ea66be6243` | Complete tree193 files; 0 PHP/Composer; React/Vite/Express package manifest. | +| Public SigningServer | `130d74b5336c40901933d4882db745f0cca811f1` | Complete tree62 files; 0 PHP/Composer; Express/TypeScript package manifest. | +| Public Relay | `1a6b9ff19c82633a15939ff4e74ccd47d535e534` | Complete tree87 files; 0 PHP/Composer; Node22/Express/TypeScript package manifest. | + +Authorized private-service source was also inspected read-only; no PHP/Composer source was observed there. Private paths/source details remain outside this public document. A source tree does not inventory deployed hosts, OS packages, reverse proxies, images, control panels, unrelated WordPress/PHP endpoints or provider-managed runtime. **Keeper exposure remains NOT ESTABLISHED; deployed runtime inventory is UNKNOWN.** No live probing, production account operation, exploit test or configuration change was performed. + +Next research step: use existing authorized read-only operator inventory to map relevant deployed assets/images and any installed PHP versions/SAPIs to these advisories; obtain the actual bulletin for correlation. Communicate the evidence and uncertainties to the owner privately. Do not infer “not affected” from JavaScript manifests. R5 owns research/exposure investigation and owner communication only; any resulting PHP remediation and verification is assigned to R6 by the latest scope decision. Confirmed urgent exposure must be brought to the owner promptly without silently implementing or publishing a change. diff --git a/docs/r5-support-copy-draft.md b/docs/r5-support-copy-draft.md new file mode 100644 index 0000000000..c9161e8816 --- /dev/null +++ b/docs/r5-support-copy-draft.md @@ -0,0 +1,28 @@ +# R5 support and release copy — unsent draft + +For owner review after the combined candidate passes its required acceptance. This draft is not published, sent, or a statement that R5 is released. Verify final 5.2 scope, source, versions, channels and exact acceptance before using it. 5.1/5.2 are internal work-package labels; source still identifies 2.6.4. Do not claim native, physical or backend acceptance from source completion alone. + +## Proposed release-note text + +Keeper's next stability update improves safe key deletion and startup recovery, with clearer backup actions and Version History. Review the final release source and acceptance record before describing these as shipped improvements. + +- Key deletion is easier to find and checks current wallet references before removing a key, including retained wallet data. Follow the in-app warning and retain the recovery material you need. +- Startup and first-wallet setup give clearer retry behavior when required local credentials or connectivity are unavailable. No timing claim is included until the exact candidate's raw measurements are reviewed. +- Assisted Server Backup has clearer status, an explicit Back Up Now repair path where eligible, read-only Check Again, and readable results. Automatic backup consent, a successful protected backup, and deletion of an existing protected copy are distinct states. +- Version History distinguishes the installed app version from historical entries. A “Current installation observed” entry records the app observed on that account; it does not invent a store update or a successful migration. + +Add or amend the 5.2 paragraph only from its authoritative completed handoff. Do not describe an unknown package identity or unfinished feature as complete. + +## Ask Keeper / website help proposed changes + +**Checking a backup:** Check Again reads the current protected-backup state. It does not create a backup. An eligible account with automatic backup enabled may use Back Up Now to attempt the explicit repair/upload; wait for its acknowledged result. A conflict, deleted namespace, failure or uncertain result must not be described as a verified new backup. + +**Deleting a backup:** Turning automatic backup off stops automatic uploads; it does not itself delete a saved protected copy. Deleting a protected backup applies to that protected copy and is not a promise to erase every legacy copy. A never-enrolled account has no protected copy to delete and is not enrolled merely to perform deletion. Keep the Recovery Key safe. + +**Version History:** Installed app version comes from the running native app. Historical records remain historical. The store listing is separate evidence; it must not replace the installed version or manufacture a past update date. The owner-confirmed 2.6.4 installation with stale 2.6.3 history is already accepted evidence; do not ask the owner to recheck that version. + +**Support diagnostics:** Share only the bounded operation/stage/category, HTTP status if shown, elapsed time and app version/build through the approved support route. Do not request raw logs, Recovery Keys, PINs, headers, bodies, account identifiers or signing material. Do not request a reset, reinstall, restore-over or backup deletion on the working phone to investigate the old report. + +**Emergency Key wording:** Only use the reviewed wording included in the final candidate. Do not expand key-role terminology or advertise a new reset/recovery capability. Hardware/biometric behavior needs actual device evidence; simulator results must be labelled accurately. + +Publication remains owner controlled. Update public release notes, website and Ask Keeper together only when required for the actual accepted deliverable, after the final channel/version/source evidence and explicit publication approval. No communications were sent by this package. diff --git a/scripts/cloud_release_inventory.py b/scripts/cloud_release_inventory.py new file mode 100644 index 0000000000..74cea0b7e5 --- /dev/null +++ b/scripts/cloud_release_inventory.py @@ -0,0 +1,135 @@ +#!/usr/bin/env python3 +"""Read-only Phase B source/configuration inventory. Never handles secret values. + +The output is preparation evidence, not an execution gate or release approval. +Only explicitly allowlisted metadata is included in the report. +""" +import argparse +import json +import pathlib +import re +import subprocess + + +PROPOSED_ENVIRONMENTS = ("keeper-release-ios", "keeper-release-android") +SOURCE_FILES = ( + "ios/fastlane/Fastfile", "android/fastlane/Fastfile", + "ios/fastlane/Appfile", "android/fastlane/Appfile", "android/app/build.gradle", +) + + +def git(root, *args): + return subprocess.check_output( + ["git", "-C", str(root), *args], text=True, stderr=subprocess.DEVNULL + ).strip() + + +def release_credential_literals(text): + # Scope to the release signing block; Android's public debug credentials + # intentionally remain distinct. Return field/line only, never its value. + start = re.search(r"signingConfigs\s*\{\s*release\s*\{", text) + if not start: + return None + position = start.end() + depth = 1 + while position < len(text) and depth: + depth += (text[position] == "{") - (text[position] == "}") + position += 1 + if depth: + return None + body = text[start.end():position - 1] + findings = [] + for match in re.finditer(r"\b(storePassword|keyPassword|keyAlias)\s*(?:=\s*)?['\"]", body): + line = text[:start.end() + match.start()].count("\n") + 1 + findings.append({"field": match.group(1), "line": line}) + return findings + + +def environment_inventory(document): + results = [] + if document is not None and ( + not isinstance(document, dict) or not isinstance(document.get("environments"), list) + ): + raise ValueError("invalid environment metadata") + environments = document.get("environments", []) if document is not None else [] + for name in PROPOSED_ENVIRONMENTS: + matches = [env for env in environments if isinstance(env, dict) and env.get("name") == name] + if document is None: + state = "UNKNOWN" + reviewers = "UNKNOWN" + branch_policy = "UNKNOWN" + elif len(matches) != 1: + state = "NOT_CONFIGURED" if not matches else "AMBIGUOUS" + reviewers = branch_policy = "NOT_VERIFIED" + else: + state = "CONFIGURED_NOT_VERIFIED" + env = matches[0] + rules = env.get("protection_rules", []) + review_rules = [rule for rule in rules if isinstance(rule, dict) and rule.get("type") == "required_reviewers"] if isinstance(rules, list) else [] + protected = len(review_rules) == 1 and review_rules[0].get("prevent_self_review") is True and bool(review_rules[0].get("reviewers")) + reviewers = "PRESENT" if protected else "NOT_VERIFIED" + policy = env.get("deployment_branch_policy") + restricted = isinstance(policy, dict) and (policy.get("custom_branch_policies") is True or policy.get("protected_branches") is True) + # A collection response cannot prove which exact branch is allowed, + # bypass policy, or that reviewer enforcement actually works. + branch_policy = "RESTRICTION_PRESENT_EXACT_RULES_UNVERIFIED" if restricted else "NOT_VERIFIED" + results.append({"proposed_name": name, "state": state, + "independent_reviewers": reviewers, "source_restriction": branch_policy}) + return results + + +def inventory(root, environments=None): + root = pathlib.Path(root) + tracked = git(root, "ls-files", "-z").split("\0") + php_count = sum(path.lower().endswith(".php") for path in tracked) + composer_count = sum(pathlib.PurePosixPath(path).name in ("composer.json", "composer.lock") for path in tracked) + source = {} + for name in SOURCE_FILES: + path = root / name + source[name] = path.read_text() if path.is_file() else "" + signing = release_credential_literals(source["android/app/build.gradle"]) + return { + "schema": 1, + "source_sha": git(root, "rev-parse", "HEAD"), + "tracked_tree_dirty": bool(git(root, "status", "--porcelain", "--untracked-files=no")), + "scope": "PHASE_B_PREPARATION_ONLY", + "execution_allowed": False, + "hosted_signing_distribution": "NOT_IMPLEMENTED_NOT_VERIFIED", + "source_inventory": {"tracked_php_files": php_count, "tracked_composer_manifests": composer_count, + "deployed_runtime_inventory": "UNKNOWN", "php_exposure": "NOT_ESTABLISHED"}, + "android_release_literal_credentials": {"state": "UNKNOWN" if signing is None else "FOUND_REQUIRES_PRIVATE_TRIAGE" if signing else "NONE_IN_INSPECTED_BLOCK", + "locations": signing or []}, + "existing_lanes": { + "ios_build_only_acquires_signing_material": all(token in source["ios/fastlane/Fastfile"] for token in ("get_certificates(", "get_provisioning_profile(")), + "ios_upload_action_present": "upload_to_testflight(" in source["ios/fastlane/Fastfile"], + "android_upload_action_present": "upload_to_play_store(" in source["android/fastlane/Fastfile"], + "lane_execution": "NOT_RUN", + }, + "proposed_protected_environments": environment_inventory(environments), + "credential_custody_and_authorization": "UNKNOWN_NOT_INSPECTED", + "exact_head_native_security_physical_acceptance": "REQUIRES_SEPARATE_EVIDENCE", + "owner_release_approval": "REQUIRES_SEPARATE_EXPLICIT_APPROVAL", + } + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--root", default=".") + parser.add_argument("--environment-metadata", help="Optional read-only GitHub environments JSON; no credentials") + args = parser.parse_args() + try: + metadata = None + if args.environment_metadata: + path = pathlib.Path(args.environment_metadata) + if path.stat().st_size > 1024 * 1024: + raise ValueError("metadata too large") + metadata = json.loads(path.read_text()) + result = inventory(args.root, metadata) + except (OSError, ValueError, TypeError, subprocess.CalledProcessError): + # Do not echo exception text, input, file paths or credential-like data. + parser.exit(2, "Release inventory unavailable: invalid source or metadata; no release action performed.\n") + print(json.dumps(result, indent=2, sort_keys=True)) + + +if __name__ == "__main__": + main() diff --git a/scripts/protected-release-plan.cjs b/scripts/protected-release-plan.cjs new file mode 100644 index 0000000000..8dd7f1a298 --- /dev/null +++ b/scripts/protected-release-plan.cjs @@ -0,0 +1,122 @@ +#!/usr/bin/env node +'use strict'; + +// Trusted-driver preparation gate only. This file has no signing/upload path. +const fs = require('node:fs'); +const path = require('node:path'); +const { validateIntent, validateSources } = require('./release-version.cjs'); +const repository = 'KeeperCommunity/bitcoin-keeper'; +const operations = { + 'ios-signed-build': 'keeper-release-ios', + 'android-signed-build': 'keeper-release-android', + 'testflight-upload': 'keeper-release-ios', + 'play-internal-upload': 'keeper-release-android', +}; +const sha = /^[a-f0-9]{40}$/; +const digest = /^[a-f0-9]{64}$/; +function requireCondition(condition, code) { if (!condition) throw new Error(code); } + +function requestFromEnvironment(env) { + const request = { + repository: env.GITHUB_REPOSITORY, ref: env.GITHUB_REF, driverSha: env.GITHUB_SHA, + sourceSha: env.KEEPER_SOURCE_SHA, releaseRef: env.KEEPER_RELEASE_REF, + operation: env.KEEPER_OPERATION, version: env.KEEPER_VERSION, + build: env.KEEPER_BUILD, artifactSha256: env.KEEPER_ARTIFACT_SHA256, + approvalCommentId: env.KEEPER_APPROVAL_COMMENT_ID, + }; + requireCondition(request.repository === repository && request.ref === 'refs/heads/sprint', 'UNTRUSTED_DRIVER_REF'); + requireCondition(sha.test(request.driverSha || '') && sha.test(request.sourceSha || ''), 'INVALID_IMMUTABLE_SOURCE'); + requireCondition(Object.hasOwn(operations, request.operation || ''), 'UNSUPPORTED_OPERATION'); + requireCondition(/^\d+\.\d+\.\d+$/.test(request.version || '') && request.releaseRef === `release/${request.version}`, 'INVALID_RELEASE_REF_VERSION'); + requireCondition(/^[1-9]\d{0,9}$/.test(request.build || '') && Number(request.build) <= 2100000000, 'INVALID_BUILD'); + const upload = request.operation.endsWith('-upload'); + requireCondition(upload ? digest.test(request.artifactSha256 || '') : request.artifactSha256 === 'pending', 'INVALID_ARTIFACT_BINDING'); + requireCondition(/^[1-9]\d{0,18}$/.test(request.approvalCommentId || ''), 'INVALID_APPROVAL_REFERENCE'); + return request; +} + +function approvalBody(request, expiresAt) { + return [ + '## Keeper protected release approval', 'State: APPROVED', + `Source commit: ${request.sourceSha}`, `Version: ${request.version}`, `Build: ${request.build}`, + `Operation: ${request.operation}`, `Artifact SHA256: ${request.artifactSha256}`, + `Workflow commit: ${request.driverSha}`, `Expires at: ${expiresAt}`, + ].join('\n'); +} + +function validateEvidence(request, evidence, now = Date.now()) { + requireCondition(evidence.driver?.protected === true && evidence.driver?.commit?.sha === request.driverSha, 'DRIVER_MOVED_OR_UNPROTECTED'); + requireCondition(evidence.branch?.protected === true && evidence.branch?.commit?.sha === request.sourceSha, 'SOURCE_MOVED_OR_UNPROTECTED'); + const comment = evidence.comment; + requireCondition(comment?.user?.login === 'antuz123' && ['admin', 'maintain', 'write'].includes(evidence.ownerPermission?.permission), 'APPROVAL_AUTHOR_UNAUTHORIZED'); + const expiration = comment?.body?.match(/\nExpires at: (\d{4}-\d\d-\d\dT\d\d:\d\d:\d\dZ)$/)?.[1]; + const expires = Date.parse(expiration); + const updated = Date.parse(comment?.updated_at); + requireCondition(Number.isFinite(expires) && expires > now && expires <= now + 86400000 && Number.isFinite(updated) && updated <= now && updated >= now - 86400000, 'APPROVAL_STALE_OR_INVALID'); + requireCondition(comment.body === approvalBody(request, expiration), 'APPROVAL_SCOPE_MISMATCH'); + const environment = evidence.environment; + requireCondition(environment?.name === operations[request.operation] && environment?.can_admins_bypass === false, 'ENVIRONMENT_MISSING_OR_BYPASS_UNVERIFIED'); + const reviews = environment.protection_rules?.filter(rule => rule.type === 'required_reviewers') || []; + requireCondition(reviews.length === 1 && reviews[0].prevent_self_review === true && Array.isArray(reviews[0].reviewers) && reviews[0].reviewers.length > 0, 'INDEPENDENT_ENVIRONMENT_REVIEW_REQUIRED'); + requireCondition(environment.deployment_branch_policy?.protected_branches === true && environment.deployment_branch_policy?.custom_branch_policies === false, 'ENVIRONMENT_SOURCE_POLICY_UNVERIFIED'); + const statuses = evidence.statuses; + const checks = evidence.checks; + requireCondition(statuses?.sha === request.sourceSha && statuses.state === 'success' && Array.isArray(statuses.statuses), 'STATUS_NOT_EXACT_SUCCESS'); + requireCondition(Number.isSafeInteger(checks?.total_count) && checks.total_count > 0 && checks.total_count <= 100 && checks.total_count === checks.check_runs?.length, 'INCOMPLETE_CHECK_EVIDENCE'); + requireCondition(checks.check_runs.every(check => check.head_sha === request.sourceSha && check.status === 'completed' && check.conclusion === 'success'), 'CHECKS_NOT_EXACT_SUCCESS'); + const passedNames = new Set([...statuses.statuses.filter(status => status.state === 'success').map(status => status.context), ...checks.check_runs.map(check => check.name)]); + requireCondition(['Tests', 'CodeQL', 'keeper/local-validation'].every(name => passedNames.has(name)), 'REQUIRED_CHECK_MISSING'); + requireCondition(evidence.manifest?.version === request.version && String(evidence.manifest[request.operation.startsWith('ios-') || request.operation === 'testflight-upload' ? 'iosBuildNumber' : 'androidVersionCode']) === request.build, 'SOURCE_VERSION_BUILD_MISMATCH'); + requireCondition(validateIntent(evidence.manifest, { now }).length === 0 && validateSources(evidence.manifest, evidence.sources).length === 0, 'RELEASE_SOURCE_PREFLIGHT_FAILED'); + return { + schema: 1, repository, sourceSha: request.sourceSha, driverSha: request.driverSha, + operation: request.operation, version: request.version, build: request.build, + artifactSha256: request.artifactSha256, environment: operations[request.operation], + state: 'AUTHORIZATION_CHECKED_PREPARATION_ONLY', + artifact_bytes_and_signature_verified: false, + release_worker_implemented: false, execution_allowed: false, + }; +} + +async function api(endpoint, token) { + const response = await fetch(`https://api.github.com/repos/${repository}/${endpoint}`, { + headers: { Authorization: `Bearer ${token}`, Accept: 'application/vnd.github+json', 'X-GitHub-Api-Version': '2022-11-28' }, + }); + requireCondition(response.ok, 'READ_ONLY_API_UNAVAILABLE'); + return response.json(); +} + +async function main(env = process.env) { + const request = requestFromEnvironment(env); + requireCondition(env.GH_TOKEN && env.KEEPER_CANDIDATE_PATH, 'DRIVER_CONFIGURATION_MISSING'); + const root = path.resolve(env.KEEPER_CANDIDATE_PATH); + const read = name => fs.readFileSync(path.join(root, name), 'utf8'); + const evidence = { + // Only trusted driver code is executed. Candidate files are data. + manifest: JSON.parse(read('release/version.json')), + sources: { + packageText: read('package.json'), android: read('android/app/build.gradle'), + ios: read('ios/hexa_keeper.xcodeproj/project.pbxproj'), + iosPlists: ['ios/hexa_keeper/Info.plist', 'ios/hexa_keeper_dev-Info.plist'].map(read), + }, + }; + [evidence.driver, evidence.branch, evidence.comment, evidence.ownerPermission, evidence.environment, evidence.statuses, evidence.checks] = await Promise.all([ + api('branches/sprint', env.GH_TOKEN), + api(`branches/${encodeURIComponent(request.releaseRef)}`, env.GH_TOKEN), + api(`issues/comments/${request.approvalCommentId}`, env.GH_TOKEN), + api('collaborators/antuz123/permission', env.GH_TOKEN), + api(`environments/${operations[request.operation]}`, env.GH_TOKEN), + api(`commits/${request.sourceSha}/status?per_page=100`, env.GH_TOKEN), + api(`commits/${request.sourceSha}/check-runs?per_page=100`, env.GH_TOKEN), + ]); + const result = validateEvidence(request, evidence); + console.log(JSON.stringify(result, null, 2)); + if (env.GITHUB_OUTPUT) fs.appendFileSync(env.GITHUB_OUTPUT, `environment=${result.environment}\n`); +} + +module.exports = { requestFromEnvironment, approvalBody, validateEvidence }; +if (require.main === module) main().catch(() => { + // Never echo tokens, API payloads, user inputs, paths or exception details. + console.error('Protected release preparation blocked: source, approval, protection or acceptance evidence is unavailable/invalid. No release action performed.'); + process.exitCode = 1; +}); diff --git a/scripts/protected-release-plan.test.cjs b/scripts/protected-release-plan.test.cjs new file mode 100644 index 0000000000..e0079434fe --- /dev/null +++ b/scripts/protected-release-plan.test.cjs @@ -0,0 +1,82 @@ +'use strict'; +const test = require('node:test'); +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const path = require('node:path'); +const { requestFromEnvironment, approvalBody, validateEvidence } = require('./protected-release-plan.cjs'); +const root = path.join(__dirname, '..'); +const read = name => fs.readFileSync(path.join(root, name), 'utf8'); +const now = Date.parse('2026-10-22T12:00:00Z'); + +function fixture() { + const manifest = JSON.parse(read('release/version.json')); + for (const baseline of Object.values(manifest.baselines)) baseline.verifiedAt = '2026-10-22T11:00:00Z'; + const env = { + GITHUB_REPOSITORY: 'KeeperCommunity/bitcoin-keeper', GITHUB_REF: 'refs/heads/sprint', + GITHUB_SHA: 'a'.repeat(40), KEEPER_SOURCE_SHA: 'b'.repeat(40), + KEEPER_RELEASE_REF: `release/${manifest.version}`, KEEPER_OPERATION: 'testflight-upload', + KEEPER_VERSION: manifest.version, KEEPER_BUILD: String(manifest.iosBuildNumber), + KEEPER_ARTIFACT_SHA256: 'c'.repeat(64), KEEPER_APPROVAL_COMMENT_ID: '123', + }; + const request = requestFromEnvironment(env); + const evidence = { + driver: { protected: true, commit: { sha: request.driverSha } }, + branch: { protected: true, commit: { sha: request.sourceSha } }, + comment: { user: { login: 'antuz123' }, body: approvalBody(request, '2026-10-22T13:00:00Z'), updated_at: '2026-10-22T11:30:00Z' }, + ownerPermission: { permission: 'write' }, + environment: { name: 'keeper-release-ios', can_admins_bypass: false, + protection_rules: [{ type: 'required_reviewers', prevent_self_review: true, reviewers: [{ id: 1 }] }], + deployment_branch_policy: { protected_branches: true, custom_branch_policies: false } }, + statuses: { sha: request.sourceSha, state: 'success', statuses: [{ context: 'keeper/local-validation', state: 'success' }] }, + checks: { total_count: 2, check_runs: ['Tests', 'CodeQL'].map(name => ({ name, head_sha: request.sourceSha, status: 'completed', conclusion: 'success' })) }, + manifest, + sources: { packageText: read('package.json'), android: read('android/app/build.gradle'), ios: read('ios/hexa_keeper.xcodeproj/project.pbxproj'), + iosPlists: ['ios/hexa_keeper/Info.plist', 'ios/hexa_keeper_dev-Info.plist'].map(read) }, + }; + return { env, request, evidence }; +} + +test('even complete synthetic evidence prepares only, never authorizes execution or artifact trust', () => { + const { request, evidence } = fixture(); + const result = validateEvidence(request, evidence, now); + assert.equal(result.execution_allowed, false); + assert.equal(result.release_worker_implemented, false); + assert.equal(result.artifact_bytes_and_signature_verified, false); + assert.equal(result.sourceSha, request.sourceSha); +}); + +for (const [name, mutate] of [ + ['moving driver', e => { e.driver.commit.sha = 'd'.repeat(40); }], + ['moving release source', e => { e.branch.commit.sha = 'd'.repeat(40); }], + ['unprotected source', e => { e.branch.protected = false; }], + ['author assertion', e => { e.comment.user.login = 'someone-else'; }], + ['expired owner approval', e => { e.comment.body = e.comment.body.replace('13:00:00Z', '10:00:00Z'); }], + ['wrong artifact approval', e => { e.comment.body = e.comment.body.replace('c'.repeat(64), 'd'.repeat(64)); }], + ['administrative bypass', e => { e.environment.can_admins_bypass = true; }], + ['unverified bypass', e => { delete e.environment.can_admins_bypass; }], + ['self approval', e => { e.environment.protection_rules[0].prevent_self_review = false; }], + ['missing environment', e => { e.environment = null; }], + ['stale native status', e => { e.statuses.sha = 'd'.repeat(40); }], + ['failed security check', e => { e.checks.check_runs[1].conclusion = 'failure'; }], + ['skipped check', e => { e.checks.check_runs[0].conclusion = 'skipped'; }], + ['wrong-SHA check', e => { e.checks.check_runs[0].head_sha = 'd'.repeat(40); }], + ['incomplete check pagination', e => { e.checks.total_count = 101; }], + ['missing required check', e => { e.checks.check_runs[1].name = 'Other'; }], + ['stale store counter evidence', e => { e.manifest.baselines.ios.verifiedAt = '2026-10-20T11:00:00Z'; }], + ['source version mismatch', e => { e.sources.packageText = '{"version":"0.0.0"}'; }], +]) test(`fails closed: ${name}`, () => { + const { request, evidence } = fixture(); + mutate(evidence); + assert.throws(() => validateEvidence(request, evidence, now)); +}); + +test('rejects uninstalled/PR driver, injection, mutable source, invalid artifact and public operation', () => { + for (const change of [ + { GITHUB_REF: 'refs/pull/1/merge' }, { GITHUB_REPOSITORY: 'other/repo' }, + { KEEPER_SOURCE_SHA: 'sprint' }, { KEEPER_VERSION: '2.6.5;echo secret' }, + { KEEPER_ARTIFACT_SHA256: 'pending' }, { KEEPER_OPERATION: 'public-release' }, + ]) { + const { env } = fixture(); + assert.throws(() => requestFromEnvironment({ ...env, ...change })); + } +}); diff --git a/scripts/test_cloud_release_inventory.py b/scripts/test_cloud_release_inventory.py new file mode 100644 index 0000000000..c243da6226 --- /dev/null +++ b/scripts/test_cloud_release_inventory.py @@ -0,0 +1,86 @@ +"""Security boundaries for the read-only release inventory; no release actions.""" +import contextlib +import importlib.util +import io +import json +import pathlib +import subprocess +import sys +import tempfile +import unittest +from unittest import mock + +spec = importlib.util.spec_from_file_location("inventory", pathlib.Path(__file__).with_name("cloud_release_inventory.py")) +module = importlib.util.module_from_spec(spec) +spec.loader.exec_module(module) + + +class InventoryTests(unittest.TestCase): + def test_release_secret_is_flagged_without_disclosure_and_debug_is_separate(self): + text = """signingConfigs { +release { +storePassword 'synthetic-private-secret' +keyAlias 'synthetic-private-alias' +keyPassword System.getenv('KEY_PASSWORD') +} +debug { storePassword 'public-debug-secret' } +}""" + findings = module.release_credential_literals(text) + self.assertEqual(findings, [{"field": "storePassword", "line": 3}, {"field": "keyAlias", "line": 4}]) + self.assertNotIn("synthetic-private", json.dumps(findings)) + self.assertNotIn("public-debug", json.dumps(findings)) + + def test_unreadable_signing_block_is_unknown(self): + self.assertIsNone(module.release_credential_literals("release {")) + self.assertIsNone(module.release_credential_literals("signingConfigs { release { storePassword 'secret'")) + + def test_missing_inventory_is_unknown_not_missing_credentials(self): + result = module.environment_inventory(None) + self.assertTrue(all(row["state"] == "UNKNOWN" for row in result)) + + def test_named_environment_alone_never_proves_protection(self): + result = module.environment_inventory({"environments": [{"name": name} for name in module.PROPOSED_ENVIRONMENTS]}) + self.assertTrue(all(row["state"] == "CONFIGURED_NOT_VERIFIED" for row in result)) + self.assertTrue(all(row["independent_reviewers"] == "NOT_VERIFIED" for row in result)) + + def test_protection_metadata_never_becomes_release_permission(self): + result = module.environment_inventory({"environments": [{ + "name": module.PROPOSED_ENVIRONMENTS[0], + "protection_rules": [{"type": "required_reviewers", "prevent_self_review": True, "reviewers": [{"id": 1}]}], + "deployment_branch_policy": {"custom_branch_policies": True}, + "unexpected_secret": "never-copy-me", + }]}) + self.assertEqual(result[0]["independent_reviewers"], "PRESENT") + self.assertEqual(result[0]["source_restriction"], "RESTRICTION_PRESENT_EXACT_RULES_UNVERIFIED") + self.assertNotIn("never-copy-me", json.dumps(result)) + self.assertEqual(result[1]["state"], "NOT_CONFIGURED") + + def test_source_identity_and_no_false_execution_or_runtime_claim(self): + with tempfile.TemporaryDirectory() as tmp: + root = pathlib.Path(tmp) + subprocess.run(["git", "init", "-q", tmp], check=True) + (root / "example.php").write_text("synthetic-only") + subprocess.run(["git", "-C", tmp, "add", "example.php"], check=True) + subprocess.run(["git", "-C", tmp, "-c", "user.name=Test", "-c", "user.email=test@example.invalid", "commit", "-qm", "fixture"], check=True) + result = module.inventory(root) + self.assertEqual(result["source_sha"], module.git(root, "rev-parse", "HEAD")) + self.assertEqual(result["source_inventory"]["tracked_php_files"], 1) + self.assertEqual(result["source_inventory"]["php_exposure"], "NOT_ESTABLISHED") + self.assertFalse(result["execution_allowed"]) + self.assertEqual(result["credential_custody_and_authorization"], "UNKNOWN_NOT_INSPECTED") + + def test_invalid_metadata_does_not_echo_input_or_exception(self): + with tempfile.TemporaryDirectory() as tmp: + metadata = pathlib.Path(tmp) / "input.json" + metadata.write_text('{"credential":"synthetic-do-not-print"') + error = io.StringIO() + with mock.patch.object(sys, "argv", ["inventory", "--environment-metadata", str(metadata)]), contextlib.redirect_stderr(error): + with self.assertRaises(SystemExit) as raised: + module.main() + self.assertEqual(raised.exception.code, 2) + self.assertNotIn("synthetic-do-not-print", error.getvalue()) + self.assertNotIn(tmp, error.getvalue()) + + +if __name__ == "__main__": + unittest.main() From 2a403e065ab908e00d92ac70418a0a1606e68cdb Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Thu, 22 Oct 2026 06:16:48 +0000 Subject: [PATCH 221/254] ci: run release preparation contracts without secrets or native builds --- .../keeper-release-preparation-contracts.yml | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 .github/workflows/keeper-release-preparation-contracts.yml diff --git a/.github/workflows/keeper-release-preparation-contracts.yml b/.github/workflows/keeper-release-preparation-contracts.yml new file mode 100644 index 0000000000..2f17849b78 --- /dev/null +++ b/.github/workflows/keeper-release-preparation-contracts.yml @@ -0,0 +1,37 @@ +name: Protected release preparation contracts + +on: + pull_request: + paths: + - '.github/workflows/keeper-protected-release-plan.yml' + - '.github/workflows/keeper-release-preparation-contracts.yml' + - 'scripts/protected-release-plan*' + - 'scripts/cloud_release_inventory.py' + - 'scripts/test_cloud_release_inventory.py' + - 'scripts/release-version.cjs' + workflow_dispatch: + +permissions: + contents: read + +concurrency: + group: keeper-release-preparation-contracts-${{ github.event.pull_request.number || github.ref }} + cancel-in-progress: false + +jobs: + contracts: + runs-on: ubuntu-latest + timeout-minutes: 5 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7 + with: + ref: ${{ github.event.pull_request.head.sha || github.sha }} + persist-credentials: false + - uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7 + with: + node-version: '22.23.3' + - name: Offline gate and inventory contracts + run: | + node --test scripts/protected-release-plan.test.cjs scripts/release-version.test.cjs scripts/verify-channel-config.test.cjs + python3 -B -m unittest discover -s scripts -p 'test_cloud_release_inventory.py' + python3 -B -m unittest discover -s scripts -p 'test_release_artifact.py' From 3efef81861727809fac8d8c465501b73c4d57eeb Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Thu, 22 Oct 2026 06:17:03 +0000 Subject: [PATCH 222/254] ci: preserve draft and active native diagnostic work --- .github/workflows/android-diagnostic-build.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/android-diagnostic-build.yml b/.github/workflows/android-diagnostic-build.yml index 549cb39f92..bd16101ac1 100644 --- a/.github/workflows/android-diagnostic-build.yml +++ b/.github/workflows/android-diagnostic-build.yml @@ -14,10 +14,12 @@ permissions: concurrency: group: android-diagnostic-${{ github.event.pull_request.number || github.ref }} - cancel-in-progress: true + cancel-in-progress: false jobs: build: + # Candidate native diagnostics own their sequential platform schedule. + if: github.event_name != 'pull_request' || (toJSON(github.event.pull_request.draft) == 'false' && !contains(github.event.pull_request.labels.*.name, 'keeper/native-diagnostic')) runs-on: ubuntu-24.04 timeout-minutes: 90 From d66bcf6cad307b8e23fe499d1c7212d89291ed38 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Thu, 22 Oct 2026 06:16:52 +0000 Subject: [PATCH 223/254] ci: reserve standalone Android package diagnostics for explicit dispatch --- .github/workflows/android-diagnostic-build.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/android-diagnostic-build.yml b/.github/workflows/android-diagnostic-build.yml index 549cb39f92..9ebe30f1f8 100644 --- a/.github/workflows/android-diagnostic-build.yml +++ b/.github/workflows/android-diagnostic-build.yml @@ -14,10 +14,12 @@ permissions: concurrency: group: android-diagnostic-${{ github.event.pull_request.number || github.ref }} - cancel-in-progress: true + cancel-in-progress: false jobs: build: + # Hosted feature acceptance owns PR native scheduling. Retain explicit package diagnostics only. + if: github.event_name == 'workflow_dispatch' runs-on: ubuntu-24.04 timeout-minutes: 90 From a431e1088249d5efb97c0ee2d6a49f8815b3da0c Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Thu, 22 Oct 2026 06:17:32 +0000 Subject: [PATCH 224/254] ci: prevent automatic competing Android package builds Source guard: 239096c30de9e23e1e080f1e817a91efb246c6d3. Existing manual package dispatch remains; no in-flight native jobs are cancelled. --- .github/workflows/android-diagnostic-build.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/android-diagnostic-build.yml b/.github/workflows/android-diagnostic-build.yml index 06315cf8bb..9ebe30f1f8 100644 --- a/.github/workflows/android-diagnostic-build.yml +++ b/.github/workflows/android-diagnostic-build.yml @@ -14,12 +14,12 @@ permissions: concurrency: group: android-diagnostic-${{ github.event.pull_request.number || github.ref }} - cancel-in-progress: true + cancel-in-progress: false jobs: build: - # Candidate native diagnostics own their sequential platform schedule. - if: github.event_name != 'pull_request' || (toJSON(github.event.pull_request.draft) == 'false' && !contains(github.event.pull_request.labels.*.name, 'keeper/native-diagnostic')) + # Hosted feature acceptance owns PR native scheduling. Retain explicit package diagnostics only. + if: github.event_name == 'workflow_dispatch' runs-on: ubuntu-24.04 timeout-minutes: 90 From 984e01eed674be30672012e3a0ca380b64225969 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Thu, 22 Oct 2026 06:16:52 +0000 Subject: [PATCH 225/254] ci: reserve standalone Android package diagnostics for explicit dispatch --- .github/workflows/android-diagnostic-build.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/android-diagnostic-build.yml b/.github/workflows/android-diagnostic-build.yml index 549cb39f92..9ebe30f1f8 100644 --- a/.github/workflows/android-diagnostic-build.yml +++ b/.github/workflows/android-diagnostic-build.yml @@ -14,10 +14,12 @@ permissions: concurrency: group: android-diagnostic-${{ github.event.pull_request.number || github.ref }} - cancel-in-progress: true + cancel-in-progress: false jobs: build: + # Hosted feature acceptance owns PR native scheduling. Retain explicit package diagnostics only. + if: github.event_name == 'workflow_dispatch' runs-on: ubuntu-24.04 timeout-minutes: 90 From 0d0106471c67d5b9706f54a2453548797f902f31 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Thu, 22 Oct 2026 06:17:41 +0000 Subject: [PATCH 226/254] ci: bind native credit to the selected route and stable PR identity --- .../tests/keeper-local-validation.test.cjs | 44 +++++++++++++++++-- .github/tests/native-draft-policy.test.cjs | 4 +- .github/workflows/keeper-local-validation.yml | 24 +++++++--- docs/r5-cloud-native-reconciliation.md | 4 +- 4 files changed, 61 insertions(+), 15 deletions(-) diff --git a/.github/tests/keeper-local-validation.test.cjs b/.github/tests/keeper-local-validation.test.cjs index c9abef7ddb..c3d6c0a712 100644 --- a/.github/tests/keeper-local-validation.test.cjs +++ b/.github/tests/keeper-local-validation.test.cjs @@ -124,15 +124,18 @@ async function evaluate(comments, options = {}) { get: async ({ pull_number }) => { pullsRequested.push(pull_number); pullReads += 1; - return { data: { + const result = { base: { ref: options.baseRef || 'sprint', sha: baseSha, repo: { full_name: repository, id: repositoryId } }, head: { sha: pullReads > 1 && options.afterHead ? options.afterHead : options.head || sha, ref: headRef, repo: options.headRepository || { full_name: repository, id: repositoryId } }, body: options.body || '', user: { login: 'cloud-writer' }, draft: options.draft ?? false, + labels: options.labels ?? [{ name: 'keeper/legacy-native' }], html_url: `https://github.com/${repository}/pull/${pull_number}` - } }; + }; + if (pullReads > 1) options.afterMutation?.(result); + return { data: result }; }, listFiles: filesRoute }, @@ -159,7 +162,7 @@ async function evaluate(comments, options = {}) { const core = { info: () => {}, warning: text => warnings.push(text), setFailed: text => failures.push(text) }; await runWorkflow(github, context, core); - assert.equal(statuses.length, options.afterHead || options.baseRef ? 0 : 1); + assert.equal(statuses.length, options.afterHead || options.baseRef || options.afterMutation ? 0 : 1); return { status: statuses[0], statuses, failures, warnings, calls, pullsRequested, runsCalls: calls.filter(c => c.route === runsRoute), jobsCalls: calls.filter(c => c.route === jobsRoute) }; @@ -561,7 +564,7 @@ const reusableTree = () => ({ ...trustedTree(), tree: [...trustedTree().tree, { path: '.github/workflows/keeper-native-platform.yml', mode: '100644', type: 'blob', sha: '7'.repeat(40) }, ] }); const reusableOptions = overrides => ({ - context: { issue: { number: 8000 } }, + context: { issue: { number: 8000 } }, labels: [], runs: [reusableRun()], jobs: [reusableJob('IOS'), reusableJob('ANDROID')], trees: { [sha]: reusableTree(), [baseSha]: reusableTree(), [runBaseSha]: reusableTree() }, ...overrides, @@ -604,3 +607,36 @@ test('a newer unsuccessful reusable run supersedes older legacy successes across })); assert.equal(status.state, 'failure'); }); +test('a deliberately skipped alternate route cannot shadow actual selected-route evidence', async () => { + const linked = { ...hostedRun().pull_requests[0], number: 8000 }; + for (const legacy of [false, true]) { + const { status } = await evaluate([handoff(sha, 'IOS, ANDROID')], reusableOptions({ + labels: legacy ? [{ name: 'keeper/legacy-native' }] : [], + jobs: legacy ? [hostedJob('IOS'), hostedJob('ANDROID')] : [reusableJob('IOS'), reusableJob('ANDROID')], + runsByWorkflow: { + 'keeper-hosted-native.yml': [hostedRun({ id: legacy ? 90 : 92, pull_requests: [linked], conclusion: legacy ? 'success' : 'skipped' })], + 'keeper-release-acceptance.yml': [reusableRun({ id: legacy ? 92 : 90, conclusion: legacy ? 'skipped' : 'failure' })], + }, + })); + assert.equal(status.state, 'success', `legacy=${legacy}`); + } +}); +test('diagnostic label cannot consume prior trusted selected-route evidence', async () => { + const { status, runsCalls } = await evaluate([handoff(sha, 'IOS, ANDROID')], reusableOptions({ + labels: [{ name: 'keeper/native-diagnostic' }], + })); + assert.equal(status.state, 'failure'); + assert.equal(runsCalls.length, 0); +}); +test('base, repository/ref or route movement at unchanged head cannot publish stale success', async () => { + for (const afterMutation of [ + pr => { pr.base.sha = 'd'.repeat(40); }, pr => { pr.base.ref = 'other'; }, + pr => { pr.base.repo.id += 1; }, pr => { pr.base.repo.full_name = 'other/repo'; }, + pr => { pr.head.ref = 'other'; }, pr => { pr.head.repo.id += 1; }, + pr => { pr.head.repo.full_name = 'other/repo'; }, pr => { pr.labels = [{ name: 'keeper/native-diagnostic' }]; }, + ]) { + const { statuses, failures } = await evaluate([handoff(sha, 'IOS, ANDROID')], reusableOptions({ afterMutation })); + assert.equal(statuses.length, 0); + assert.match(failures[0], /changed during validation/); + } +}); diff --git a/.github/tests/native-draft-policy.test.cjs b/.github/tests/native-draft-policy.test.cjs index 7af8f96d78..0246de416e 100644 --- a/.github/tests/native-draft-policy.test.cjs +++ b/.github/tests/native-draft-policy.test.cjs @@ -100,8 +100,8 @@ test('reusable paths keep explicit draft and diagnostic boundaries with no dispa 'keeper-release-acceptance.yml', 'keeper-native-diagnostic.yml']) { assert.ok(!Object.hasOwn(workflows[filename].on, 'workflow_dispatch'), filename); } - assert.match(workflows['android-diagnostic-build.yml'].jobs.build.if, /draft\) == 'false'/); - assert.match(workflows['android-diagnostic-build.yml'].jobs.build.if, /!contains/); + assert.equal(workflows['android-diagnostic-build.yml'].jobs.build.if, "github.event_name == 'workflow_dispatch'"); + assert.equal(workflows['android-diagnostic-build.yml'].concurrency['cancel-in-progress'], 'false'); }); test('pushing the unpublished branch triggers no checked-in push workflow', () => { diff --git a/.github/workflows/keeper-local-validation.yml b/.github/workflows/keeper-local-validation.yml index 35f5fe018a..09ffb6559f 100644 --- a/.github/workflows/keeper-local-validation.yml +++ b/.github/workflows/keeper-local-validation.yml @@ -97,22 +97,25 @@ jobs: // alone is NOT evidence. All executable CI files must match both the // protected current base and the base used by that particular run. // This intentionally rejects this infrastructure PR's bootstrap runs. + const nativeRoute = value => value.labels?.some(label => label.name === 'keeper/native-diagnostic') + ? 'diagnostic' : value.labels?.some(label => label.name === 'keeper/legacy-native') + ? 'keeper-hosted-native.yml' : 'keeper-release-acceptance.yml'; const hostedPasses = async () => { const passed = new Set(); try { - const workflowNames = ['keeper-hosted-native.yml', 'keeper-release-acceptance.yml']; + const selectedWorkflow = nativeRoute(pr); + if (selectedWorkflow === 'diagnostic') return passed; const repository = owner + '/' + repo; if (pr.head.repo?.full_name !== repository) return passed; - const groups = await Promise.all(workflowNames.map(workflow_id => - github.paginate(github.rest.actions.listWorkflowRuns, { - owner, repo, workflow_id, head_sha: sha, event: 'pull_request', per_page: 100 - }))); - const runs = [...new Map(groups.flat().map(run => [run.id, run])).values()]; + const runs = await github.paginate(github.rest.actions.listWorkflowRuns, { + owner, repo, workflow_id: selectedWorkflow, + head_sha: sha, event: 'pull_request', per_page: 100 + }); // Do not fall back to an older success after a failed/cancelled // rerun or while newer evidence is pending. const run = runs.sort((a, b) => b.id - a.id)[0]; const reusable = run?.path === '.github/workflows/keeper-release-acceptance.yml'; - const workflowPath = reusable ? '.github/workflows/keeper-release-acceptance.yml' : '.github/workflows/keeper-hosted-native.yml'; + const workflowPath = '.github/workflows/' + selectedWorkflow; if (reusable && pull_number === 7045) return passed; if (!run || run.head_sha !== sha || run.event !== 'pull_request' || run.path !== workflowPath || run.head_branch !== pr.head.ref || @@ -296,6 +299,13 @@ jobs: core.setFailed('PR head changed during validation; awaiting new-head evaluation'); return; } + if (current.base.sha !== pr.base.sha || current.base.ref !== pr.base.ref || + current.base.repo?.id !== pr.base.repo?.id || current.base.repo?.full_name !== pr.base.repo?.full_name || + current.head.ref !== pr.head.ref || current.head.repo?.id !== pr.head.repo?.id || + current.head.repo?.full_name !== pr.head.repo?.full_name || nativeRoute(current) !== nativeRoute(pr)) { + core.setFailed('PR base, repository, ref or native route changed during validation; awaiting fresh evaluation'); + return; + } await github.rest.repos.createCommitStatus({ owner, repo, sha, state, context: contextName, diff --git a/docs/r5-cloud-native-reconciliation.md b/docs/r5-cloud-native-reconciliation.md index a8ddf3cbe9..abf65be0b0 100644 --- a/docs/r5-cloud-native-reconciliation.md +++ b/docs/r5-cloud-native-reconciliation.md @@ -17,9 +17,9 @@ These inherited packages exceed one small review; their original patch-only revi `Keeper native candidate diagnostics` allows pre-merge execution on a same-repository draft with explicit `keeper/native-diagnostic` label. The coordinator verifies current head and existing active native jobs before labeling. The worker rechecks current source and opt-in before setup. iOS runs first, then Android; concurrency does not cancel an active candidate job. A synchronize event creates a new exact-source run rather than transferring evidence. Remove the label before changing the candidate if another run should not be queued. Diagnostic results are metadata only, have separate artifact names, and are excluded from trusted gate evidence. -The former startup workflow requires `keeper/legacy-native`; this selects the serialized startup fallback instead of the reusable default. Inherited Android production-debug diagnostic builds skip drafts and the candidate-diagnostic label, preventing a second automatic candidate build. The coordinator must still check other already-running workflows and schedule only one native build at a time. No active job was cancelled by this package. +The former startup workflow requires `keeper/legacy-native`; this selects the serialized startup fallback instead of the reusable default. Inherited Android production-debug package diagnostics now require their existing explicit manual dispatch, preventing a second automatic PR candidate build. Their concurrency no longer cancels an active job. The coordinator must still check other already-running workflows and schedule only one native build at a time. No active job was cancelled by this package. -The trusted validation gate accepts exact-head successful platform build/UI/probe jobs from the reviewed reusable workflow or legacy startup fallback. It queries both routes, uses only the newest run and current attempt, and checks the full native harness plus executable worker against protected current/run bases. A newer skipped/cancelled/failed run cannot reuse an older successful run. A diagnostic run, absent worker, changed CI, stale SHA or build-only result provides no credit. Hardware requirements, actual failures, owner-scoped deferrals and local fallback remain binding. This status is platform scope only; it cannot approve full feature acceptance or release. +The trusted validation gate accepts exact-head successful platform build/UI/probe jobs from the reviewed reusable workflow or legacy startup fallback. It selects the current label route, then uses only that route’s newest run and current attempt, and checks the full native harness plus executable worker against protected current/run bases. A newer skipped/cancelled/failed run within the selected route cannot reuse an older successful run. The intentionally skipped alternate route cannot shadow genuine evidence. Current base, repository/ref identity and selected route are rechecked immediately before status publication. A diagnostic run, absent worker, changed CI, stale SHA or build-only result provides no credit. Hardware requirements, actual failures, owner-scoped deferrals and local fallback remain binding. This status is platform scope only; it cannot approve full feature acceptance or release. ## Disposable data, evidence and limits From 3ad3a6b601de7655b8a31200aeca41225a5d5c30 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Thu, 22 Oct 2026 06:16:52 +0000 Subject: [PATCH 227/254] ci: reserve standalone Android package diagnostics for explicit dispatch --- .github/workflows/android-diagnostic-build.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/android-diagnostic-build.yml b/.github/workflows/android-diagnostic-build.yml index bd16101ac1..9ebe30f1f8 100644 --- a/.github/workflows/android-diagnostic-build.yml +++ b/.github/workflows/android-diagnostic-build.yml @@ -18,8 +18,8 @@ concurrency: jobs: build: - # Candidate native diagnostics own their sequential platform schedule. - if: github.event_name != 'pull_request' || (toJSON(github.event.pull_request.draft) == 'false' && !contains(github.event.pull_request.labels.*.name, 'keeper/native-diagnostic')) + # Hosted feature acceptance owns PR native scheduling. Retain explicit package diagnostics only. + if: github.event_name == 'workflow_dispatch' runs-on: ubuntu-24.04 timeout-minutes: 90 From f2fc0cb557f0e5303335dadc9e98152640795e6c Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Thu, 22 Oct 2026 06:17:54 +0000 Subject: [PATCH 228/254] docs: require fresh authorization after protected environment approval --- docs/r5-cloud-release-readiness.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/r5-cloud-release-readiness.md b/docs/r5-cloud-release-readiness.md index f47d749af6..a6747e937e 100644 --- a/docs/r5-cloud-release-readiness.md +++ b/docs/r5-cloud-release-readiness.md @@ -43,6 +43,8 @@ The executable [gate](../scripts/protected-release-plan.cjs) requires all of: The final protected job records the preparation boundary only. Its result always reports `execution_allowed: false`, `release_worker_implemented: false` and `artifact_bytes_and_signature_verified: false`. The artifact approval digest is bound to the request; this preparation package does not download/verify actual release bytes or signatures. Environment selection and shape-valid metadata alone never produce a release pass. +**Future execution workers must revalidate after the protected environment approval**, immediately before any signing or upload: refresh driver/source ref identity, exact-head check and review state, approval author/scope/expiry, fresh store/version baseline, and the actual immutable artifact bytes/digest/signature/provenance. A source movement, new failure, expired/revoked approval or artifact mismatch while waiting blocks execution. The earlier preparation result cannot serve as final execution authorization, and a successful environment review cannot bypass this fresh validation. Artifact verification must use trusted driver code and must not run artifact-provided code. + The owner approval receipt has this exact form (use actual reviewed identities; no receipt is created or requested by this package): ```text From 3a584fa45fc5f6218c86104b92a206819693250d Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Thu, 22 Oct 2026 06:18:53 +0000 Subject: [PATCH 229/254] test: measure full-product native startup and first-wallet persistence --- .github/native/product_first_wallet.yaml | 122 ++++++++ .github/native/product_measurements.py | 290 ++++++++++++++++++ .github/native/product_metric.js | 6 + .github/tests/test_product_measurements.py | 100 ++++++ docs/r5/FULL-PRODUCT-MEASUREMENTS.md | 90 ++++++ .../Home/components/Wallet/HomeWallet.tsx | 1 + 6 files changed, 609 insertions(+) create mode 100644 .github/native/product_first_wallet.yaml create mode 100644 .github/native/product_measurements.py create mode 100644 .github/native/product_metric.js create mode 100644 .github/tests/test_product_measurements.py create mode 100644 docs/r5/FULL-PRODUCT-MEASUREMENTS.md diff --git a/.github/native/product_first_wallet.yaml b/.github/native/product_first_wallet.yaml new file mode 100644 index 0000000000..b8c0d1ed7e --- /dev/null +++ b/.github/native/product_first_wallet.yaml @@ -0,0 +1,122 @@ +# Full packaged app, ordinary index.js/production reducers/sagas/Realm. PIN1111 +# belongs only to the runner-created disposable installation. Never reveal seeds. +appId: ${APP_ID} +name: Full app startup first wallet and cold persistence +--- +- runScript: + file: product_metric.js + env: + STAGE: startup_start +- launchApp: + permissions: + all: deny +- extendedWaitUntil: + visible: Create a passcode + timeout: 180000 +- runScript: + file: product_metric.js + env: + STAGE: startup_ready +- repeat: + times: 4 + commands: + - tapOn: + id: key_1 +- assertVisible: Confirm your passcode +- repeat: + times: 4 + commands: + - tapOn: + id: key_1 +- tapOn: Create +- extendedWaitUntil: + visible: Continue + timeout: 30000 +- tapOn: Continue +- extendedWaitUntil: + visible: Cancel + timeout: 30000 +- tapOn: Cancel +- extendedWaitUntil: + visible: + id: btn_skip + timeout: 30000 +- tapOn: + id: btn_skip +- extendedWaitUntil: + visible: + id: view_startNewTile + timeout: 30000 +- runScript: + file: product_metric.js + env: + STAGE: wallet_start +- tapOn: + id: view_startNewTile +- extendedWaitUntil: + visible: Next + timeout: 180000 +- runScript: + file: product_metric.js + env: + STAGE: wallet_ready +- tapOn: Next +- extendedWaitUntil: + visible: Protect Your Recovery Key + timeout: 30000 +- tapOn: Skip for Now +- extendedWaitUntil: + visible: Continue Without Backup? + timeout: 30000 +- tapOn: Skip Backup +- extendedWaitUntil: + visible: + id: home_visible_wallet_count_1 + timeout: 30000 +- assertVisible: Mobile Wallet +- runScript: + file: product_metric.js + env: + STAGE: home_ready +- stopApp +- runScript: + file: product_metric.js + env: + STAGE: relaunch_start +- launchApp +- extendedWaitUntil: + visible: Enter your passcode + timeout: 60000 +- runScript: + file: product_metric.js + env: + STAGE: relaunch_login +- repeat: + times: 4 + commands: + - tapOn: + id: key_1 +- tapOn: Proceed +- extendedWaitUntil: + visible: + id: home_visible_wallet_count_1 + timeout: 60000 +- assertVisible: Mobile Wallet +- runScript: + file: product_metric.js + env: + STAGE: relaunch_home +- tapOn: + id: home_tab_more +- tapOn: + id: home_tab_wallets +- extendedWaitUntil: + visible: + id: home_visible_wallet_count_1 + timeout: 30000 +- assertVisible: Mobile Wallet +- runScript: + file: product_metric.js + env: + STAGE: navigation_verified +- stopApp diff --git a/.github/native/product_measurements.py b/.github/native/product_measurements.py new file mode 100644 index 0000000000..fdee1b8345 --- /dev/null +++ b/.github/native/product_measurements.py @@ -0,0 +1,290 @@ +#!/usr/bin/env python3 +"""Five full-product UI samples on a task-owned device; metadata-only evidence. + +Requires the hosted_native.py foundation and its successful exact-source build. +Normal Metro entrypoint is mandatory. The loopback responder is a controlled +createNewApp transport fixture, NEVER evidence of Relay compatibility. +""" +import importlib.util +import json +import os +from pathlib import Path +import shutil +import signal +import subprocess +import tempfile +import threading +import time +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer +import urllib.request + +STAGES = ('startup_start', 'startup_ready', 'wallet_start', 'wallet_ready', + 'home_ready', 'relaunch_start', 'relaunch_login', 'relaunch_home', + 'navigation_verified') +PORT = 8765 +SAMPLE_COUNT = 5 + + +class Receipt: + """Fixed vocabulary and numbers only; never store a request body/identifier.""" + def __init__(self): + self.lock = threading.Lock() + self.reset(1) + + def reset(self, sample): + with self.lock: + self.sample, self.times = sample, [] + self.create_calls, self.blocked_calls = 0, 0 + + def mark(self, stage): + with self.lock: + if len(self.times) >= len(STAGES) or stage != STAGES[len(self.times)]: + return False + self.times.append(time.monotonic_ns()) + return True + + def snapshot(self, passed=False): + with self.lock: + if passed and (len(self.times) != len(STAGES) or self.create_calls != 1): + raise RuntimeError('PRODUCT_UI_INCOMPLETE_OR_DUPLICATE_SETUP') + stamps = dict(zip(STAGES, self.times)) + pairs = { + 'startup_to_passcode_ms': ('startup_start', 'startup_ready'), + 'start_new_to_ready_ms': ('wallet_start', 'wallet_ready'), + 'start_new_to_home_ms': ('wallet_start', 'home_ready'), + 'cold_relaunch_to_login_ms': ('relaunch_start', 'relaunch_login'), + 'cold_relaunch_to_authenticated_home_ms': ('relaunch_start', 'relaunch_home'), + } + durations = {name: round((stamps[end] - stamps[start]) / 1_000_000, 3) + for name, (start, end) in pairs.items() if end in stamps and start in stamps} + return {'sample': self.sample, 'status': 'PASS' if passed else 'FAIL', + 'completed_stages': list(STAGES[:len(self.times)]), 'raw_durations': durations, + 'create_new_app_calls': self.create_calls, 'other_backend_calls_rejected': self.blocked_calls, + 'visible_wallet_count_before_relaunch': 1 if 'home_ready' in stamps else None, + 'visible_wallet_count_after_relaunch': 1 if 'relaunch_home' in stamps else None, + 'more_wallets_navigation': 'PASS' if 'navigation_verified' in stamps else 'NOT RUN'} + + +def make_server(receipt, port=PORT): + class Handler(BaseHTTPRequestHandler): + def log_message(self, *_): + pass + + def reply(self, status, value): + raw = json.dumps(value).encode() + self.send_response(status) + self.send_header('Content-Type', 'application/json') + self.send_header('Content-Length', str(len(raw))) + self.end_headers() + self.wfile.write(raw) + + def do_POST(self): + try: + length = int(self.headers.get('Content-Length', '0')) + if not 0 < length <= 8192: + self.close_connection = True + return self.reply(413, {'error': 'FIXTURE_BODY_REJECTED'}) + # createNewApp carries disposable public identifiers/FCM data. + # Discard without parsing, logging, echoing or retaining them. + raw = self.rfile.read(length) + if self.path == '/createNewApp': + with receipt.lock: + receipt.create_calls += 1 + return self.reply(200, {'created': True}) + if self.path == '/metric' and length <= 128: + metric = json.loads(raw) + if (isinstance(metric, dict) and set(metric) == {'stage'} and + receipt.mark(metric['stage'])): + return self.reply(200, {'accepted': True}) + return self.reply(422, {'error': 'FIXED_METRIC_REJECTED'}) + except (ValueError, TypeError): + return self.reply(422, {'error': 'FIXED_METRIC_REJECTED'}) + with receipt.lock: + receipt.blocked_calls += 1 + self.reply(503, {'error': 'DISPOSABLE_FIXTURE_UNSUPPORTED_ROUTE'}) + + def do_GET(self): + self.reply(503, {'error': 'DISPOSABLE_FIXTURE_UNSUPPORTED_ROUTE'}) + + class Server(ThreadingHTTPServer): + allow_reuse_address = False + daemon_threads = True + + return Server(('127.0.0.1', port), Handler) + + +def run(h): + if (os.environ.get('GITHUB_ACTIONS') != 'true' or + os.environ.get('RUNNER_ENVIRONMENT') != 'github-hosted'): + raise RuntimeError('PRODUCT_UI_DISPOSABLE_HOSTED_RUNNER_REQUIRED') + data = h.read() + first_private_command = len(data.get('commands', [])) + 1 + if (data['build'] != 'PASS' or data['source_sha'] != os.environ.get('KEEPER_SOURCE_SHA') or + h.artifact_identity() != data['built_artifact']): + raise RuntimeError('PRODUCT_UI_EXACT_BUILD_REQUIRED') + expected_environment = { + 'ENVIRONMENT': 'DEVELOPMENT', 'RELAY': 'http://127.0.0.1:8765/', + 'SIGNING_SERVER_TESTNET': 'https://127.0.0.1:9/', + 'SIGNING_SERVER_MAINNET': 'https://127.0.0.1:9/', + 'CHANNEL_URL': 'https://127.0.0.1:9/', + 'SENTRY_DNS': 'https://public@127.0.0.1:9/1', + 'ENC_KEY_STORAGE_IDENTIFIER': 'KEEPER-HOSTED-DISPOSABLE', + } + environment_lines = (h.ROOT / '.env.ci').read_text().splitlines() + if (len(environment_lines) != len(expected_environment) or + dict(line.split('=', 1) for line in environment_lines) != expected_environment): + raise RuntimeError('PRODUCT_UI_LOOPBACK_BUILD_REQUIRED') + private = Path(tempfile.mkdtemp(prefix='keeper-product-ui-', dir=h.PRIVATE)) + private.chmod(0o700) + receipt = Receipt() + server, metro, device = None, None, None + # Only app/test-specific data created here is removed. Never touch shared + # caches, another worktree, an existing iOS simulator or a real phone. + result = {'status': 'RUNNING', 'samples': [], 'requested_samples': SAMPLE_COUNT, + 'entrypoint': 'index.js', 'network': 'TESTNET', 'backend': 'CONTROLLED_LOOPBACK_CREATE_ONLY', + 'backend_compatibility': 'NOT TESTED', 'physical_devices': 'NOT RUN', + 'performance_scope': 'Development app with warm Metro bundle; cold process/UI timing includes Maestro driver overhead. Not release-build performance.', + 'count_scope': 'Actual rendered visible wallet list; disposable one-wallet account, not hidden/archived wallet counts.', + 'network_scope': 'Fresh production Splash selects Testnet for DEVELOPMENT. Account backend is loopback fixture; signing/channel/Sentry endpoints blocked. Public Testnet Electrum connectivity may fail or connect; no faucet, funded wallet, send, signing or backup opt-in.'} + + def save(): + current = h.read() + current['product_ui'] = result + h.save(current) + + save() + try: + server = make_server(receipt) + thread = threading.Thread(target=server.serve_forever, daemon=True) + thread.start() + # Ordinary product bundle: no --config override and no probe entrypoint. + with (private / 'metro.log').open('wb') as log: + metro = subprocess.Popen(['node', 'node_modules/react-native/cli.js', 'start', '--port', '8081'], + cwd=h.ROOT, stdout=log, stderr=subprocess.STDOUT, + start_new_session=True) + for _ in range(60): + if metro.poll() is not None: + raise RuntimeError('PRODUCT_UI_METRO_FAILED') + try: + with urllib.request.urlopen('http://127.0.0.1:8081/status', timeout=2) as response: + if b'packager-status:running' in response.read(): + break + except OSError: + time.sleep(1) + else: + raise RuntimeError('PRODUCT_UI_METRO_NOT_READY') + h.warm_metro_bundle('startup') + if h.PLATFORM == 'IOS': + device = h.command(['xcrun', 'simctl', 'create', 'Keeper-product-ui-disposable', + 'com.apple.CoreSimulator.SimDeviceType.iPhone-16', + 'com.apple.CoreSimulator.SimRuntime.iOS-18-5'], + 'Create task-owned product UI simulator', capture=True) + h.command(['xcrun', 'simctl', 'boot', device], 'Boot product UI simulator') + h.command(['xcrun', 'simctl', 'bootstatus', device, '-b'], 'Wait for product UI simulator', timeout=240) + artifact = str(h.PRIVATE / 'DerivedData/Build/Products/Debug-iphonesimulator/hexa_keeper_dev.app') + result['device'] = {'profile': 'iPhone 16', 'runtime': 'iOS 18.5', 'kind': 'SIMULATOR'} + else: + device = h.command(['adb', 'get-serialno'], 'Select disposable CI emulator', capture=True) + if not device.startswith('emulator-'): + raise RuntimeError('PRODUCT_UI_REAL_ANDROID_DEVICE_REJECTED') + for port in ('8081', str(PORT)): + h.command(['adb', '-s', device, 'reverse', 'tcp:' + port, 'tcp:' + port], + 'Connect disposable emulator to local fixture') + artifact = str(h.ROOT / 'android/app/build/outputs/apk/development/debug/app-development-debug.apk') + result['device'] = {'kind': 'EMULATOR', 'image': h.command( + ['adb', '-s', device, 'shell', 'getprop', 'ro.build.fingerprint'], + 'Record actual product UI emulator image', capture=True)} + app = h.APP_IDS[h.PLATFORM] + for sample in range(1, SAMPLE_COUNT + 1): + receipt.reset(sample) + output = private / ('sample-%d' % sample) + output.mkdir() + report = output / 'junit.xml' + if h.PLATFORM == 'IOS': + # Uninstall plus reset of this newly-created simulator's + # keychain prevents prior sample credentials persisting. + subprocess.run(['xcrun', 'simctl', 'uninstall', device, app], + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=30, check=False) + h.command(['xcrun', 'simctl', 'keychain', device, 'reset'], 'Reset task-owned simulator credentials') + h.command(['xcrun', 'simctl', 'install', device, artifact], 'Install exact product UI app', timeout=120) + else: + h.command(['adb', '-s', device, 'install', '-r', artifact], 'Install exact product UI APK', timeout=120) + h.command(['adb', '-s', device, 'shell', 'pm', 'clear', app], 'Clear only disposable development app') + try: + h.command([data['maestro']['binary'], '--device', device, 'test', + '--env', 'APP_ID=' + app, '--flatten-debug-output', + '--debug-output', str(output), '--test-output-dir', str(output), + '--format', 'junit', '--output', str(report), + '.github/native/product_first_wallet.yaml'], + 'Full-product first-wallet and relaunch UI sample', timeout=600, error_details=False) + h.successful_junit(report) + if h.artifact_identity() != data['built_artifact']: + raise RuntimeError('PRODUCT_UI_ARTIFACT_CHANGED') + result['samples'].append(receipt.snapshot(passed=True)) + except Exception: + result['samples'].append(receipt.snapshot()) + raise + finally: + # Maestro can capture automatic failure diagnostics. Keep no + # screenshots, hierarchy, PIN actions or app output artifacts. + shutil.rmtree(output, ignore_errors=True) + save() + result['status'] = 'PASS' + except Exception as error: + result['status'] = 'FAIL' + result['failure_category'] = type(error).__name__ # never raw error text + raise + finally: + # Save original outcome before best-effort disposal so a cleanup issue + # cannot erase a native failure or the completed raw numeric samples. + save() + if metro and metro.poll() is None: + os.killpg(metro.pid, signal.SIGTERM) + try: + metro.wait(timeout=10) + except subprocess.TimeoutExpired: + os.killpg(metro.pid, signal.SIGKILL) + if server: + server.shutdown() + server.server_close() + cleanup_failed = False + + def cleanup(args): + nonlocal cleanup_failed + try: + code = subprocess.run(args, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, + timeout=30, check=False).returncode + if code and result['status'] == 'PASS': + cleanup_failed = True + except (OSError, subprocess.TimeoutExpired): + cleanup_failed = True + + if device and h.PLATFORM == 'IOS': + for action in ('shutdown', 'delete'): + cleanup(['xcrun', 'simctl', action, device]) + elif device and device.startswith('emulator-'): + cleanup(['adb', '-s', device, 'uninstall', h.APP_IDS[h.PLATFORM]]) + cleanup(['adb', '-s', device, 'reverse', '--remove', 'tcp:' + str(PORT)]) + shutil.rmtree(private, ignore_errors=True) + # Foundation command metadata is safe/nonsecret; its process logs from + # this suite are not retained even on the ephemeral runner. + for index in range(first_private_command, len(h.read().get('commands', [])) + 1): + (h.PRIVATE / ('command-%03d.log' % index)).unlink(missing_ok=True) + if cleanup_failed: + result['status'] = 'FAIL' + result['cleanup'] = 'FAILED' + save() + if cleanup_failed: + raise RuntimeError('PRODUCT_UI_DISPOSABLE_CLEANUP_FAILED') + + +if __name__ == '__main__': + import sys + if sys.argv[1:] != ['run']: + raise SystemExit('Expected run; hosted exact-source build prerequisite required.') + spec = importlib.util.spec_from_file_location('hosted_native', Path(__file__).with_name('hosted_native.py')) + foundation = importlib.util.module_from_spec(spec) + spec.loader.exec_module(foundation) + if foundation.PLATFORM not in foundation.APP_IDS: + raise SystemExit('Expected hosted IOS or ANDROID platform.') + run(foundation) diff --git a/.github/native/product_metric.js b/.github/native/product_metric.js new file mode 100644 index 0000000000..e98170a706 --- /dev/null +++ b/.github/native/product_metric.js @@ -0,0 +1,6 @@ +// Host-side Maestro script. Send fixed stage labels only, never hierarchy/app data. +const reply = http.post('http://127.0.0.1:8765/metric', { + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ stage: STAGE }), +}); +if (reply.status !== 200) throw new Error('PRODUCT_METRIC_REJECTED'); diff --git a/.github/tests/test_product_measurements.py b/.github/tests/test_product_measurements.py new file mode 100644 index 0000000000..0401d13ddb --- /dev/null +++ b/.github/tests/test_product_measurements.py @@ -0,0 +1,100 @@ +"""HTTP controls and fail-closed measurement receipts; these are not native tests.""" +import importlib.util +import json +from pathlib import Path +import threading +import unittest +import urllib.error +import urllib.request + +SPEC = importlib.util.spec_from_file_location('product_measurements', + Path(__file__).resolve().parents[1] / 'native/product_measurements.py') +product = importlib.util.module_from_spec(SPEC) +SPEC.loader.exec_module(product) + + +class FixtureTests(unittest.TestCase): + def setUp(self): + self.receipt = product.Receipt() + self.server = product.make_server(self.receipt, port=0) + self.thread = threading.Thread(target=self.server.serve_forever, daemon=True) + self.thread.start() + self.base = 'http://127.0.0.1:%d' % self.server.server_port + + def tearDown(self): + self.server.shutdown() + self.server.server_close() + self.thread.join(timeout=2) + + def post(self, path, value): + raw = value if isinstance(value, bytes) else json.dumps(value).encode() + request = urllib.request.Request(self.base + path, data=raw, method='POST', + headers={'Content-Type': 'application/json'}) + try: + with urllib.request.urlopen(request, timeout=2) as response: + return response.status, json.loads(response.read()) + except urllib.error.HTTPError as error: + return error.code, json.loads(error.read()) + + def complete(self): + self.post('/createNewApp', {'publicId': 'DO_NOT_RETAIN', 'fcmToken': 'DO_NOT_RETAIN'}) + for stage in product.STAGES: + self.assertEqual(self.post('/metric', {'stage': stage}), (200, {'accepted': True})) + + def test_loopback_only(self): + self.assertEqual(self.server.server_address[0], '127.0.0.1') + + def test_complete_numeric_receipt_does_not_retain_account_data(self): + self.complete() + evidence = self.receipt.snapshot(passed=True) + self.assertEqual(evidence['status'], 'PASS') + self.assertEqual(evidence['visible_wallet_count_after_relaunch'], 1) + self.assertEqual(len(evidence['raw_durations']), 5) + self.assertNotIn('DO_NOT_RETAIN', json.dumps(evidence)) + self.assertTrue(all(value >= 0 for value in evidence['raw_durations'].values())) + + def test_out_of_order_and_duplicate_markers_are_rejected(self): + self.assertEqual(self.post('/metric', {'stage': 'wallet_ready'})[0], 422) + self.assertEqual(self.post('/metric', {'stage': 'startup_start'})[0], 200) + self.assertEqual(self.post('/metric', {'stage': 'startup_start'})[0], 422) + self.assertEqual(self.receipt.snapshot()['completed_stages'], ['startup_start']) + + def test_unknown_marker_and_additional_fields_are_rejected_without_echo(self): + for payload in ({'stage': 'DO_NOT_RETAIN'}, {'stage': 'startup_start', 'secret': 'DO_NOT_RETAIN'}): + status, response = self.post('/metric', payload) + self.assertEqual(status, 422) + self.assertNotIn('DO_NOT_RETAIN', json.dumps(response)) + self.assertEqual(self.receipt.snapshot()['completed_stages'], []) + + def test_duplicate_setup_fails_even_after_every_ui_stage(self): + self.complete() + self.post('/createNewApp', b'{}') + with self.assertRaisesRegex(RuntimeError, 'DUPLICATE_SETUP'): + self.receipt.snapshot(passed=True) + + def test_missing_setup_or_partial_flow_cannot_pass(self): + self.receipt.mark('startup_start') + with self.assertRaises(RuntimeError): + self.receipt.snapshot(passed=True) + self.assertEqual(self.receipt.snapshot()['status'], 'FAIL') + + def test_unknown_backend_route_fails_closed(self): + status, response = self.post('/deleteBackup', b'{}') + self.assertEqual(status, 503) + self.assertNotIn('deleted', response) + self.assertEqual(self.receipt.snapshot()['other_backend_calls_rejected'], 1) + + def test_oversized_payload_is_rejected_without_retention(self): + self.assertEqual(self.post('/createNewApp', b'x' * 8193)[0], 413) + self.assertEqual(self.receipt.snapshot()['create_new_app_calls'], 0) + + def test_reset_discards_prior_sample_receipt(self): + self.complete() + self.receipt.reset(2) + state = self.receipt.snapshot() + self.assertEqual((state['sample'], state['create_new_app_calls']), (2, 0)) + self.assertEqual(state['completed_stages'], []) + + +if __name__ == '__main__': + unittest.main() diff --git a/docs/r5/FULL-PRODUCT-MEASUREMENTS.md b/docs/r5/FULL-PRODUCT-MEASUREMENTS.md new file mode 100644 index 0000000000..4e99b82a32 --- /dev/null +++ b/docs/r5/FULL-PRODUCT-MEASUREMENTS.md @@ -0,0 +1,90 @@ +# Full-product startup and first-wallet UI measurements + +This separately reviewable R5 acceptance package runs the ordinary packaged app +through `index.js`, its real PIN/storage setup, production account-creation saga, +wallet/key generation and encrypted persistence. It complements the CI-only +native module/worker probes. Estimates: approximately 4k–6k changed code/test +tokens, one acceptance-infrastructure feature. No app version is allocated here. + +The only product change is a FlatList test ID derived from the already-rendered +visible wallet list: `home_visible_wallet_count_N`. Hidden and archived counts +are not exposed. In a fresh one-wallet fixture, count1 before/after a cold process +relaunch demonstrates visible-list persistence without exporting wallet IDs. + +## Execution contract + +The reconciled hosted foundation must first `record`, `prepare` and `build` the +exact candidate. Before that build its `.env.ci` must use +`RELAY=http://127.0.0.1:8765/`; retain the foundation's DEVELOPMENT, blocked +signing/channel/Sentry endpoints and disposable credential namespace. This +runner checks that exact nonsecret configuration. DEVELOPMENT makes production +Splash select Testnet on the fresh installation. No faucet request, funded +wallet, transaction, signing or server-backup enrollment is performed. + +After the hosted smoke/probes finish, run on the same hosted platform: + +```sh +python3 .github/native/product_measurements.py run +``` + +The runner requires a disposable GitHub-hosted runner (`GITHUB_ACTIONS=true`, +`RUNNER_ENVIRONMENT=github-hosted`) to prevent accidental local data clearing. +It imports `.github/native/hosted_native.py`, verifies its successful +build and artifact fingerprint, uses its installed pinned Maestro, warms the +ordinary Metro bundle, then creates its own iOS simulator or uses the CI-owned +Android emulator. Physical Android devices are rejected. Android forwards only +Metro and the fixture ports. It removes only its new iOS simulator or disposable +development app and its own temporary measurement files. Existing personal +devices, simulator profiles, other worktrees, caches and evidence are untouched. + +The setup responder binds only127.0.0.1:8765, answers only `createNewApp` with the +controlled `{created:true}` response, discards request bodies without logging or +retaining identifiers, and rejects every other backend operation. A separate +fixed-vocabulary metric route records host monotonic timestamps for UI actions. +This deliberately supplies **no backend-protocol/compatibility acceptance**. +Public Testnet Electrum connections still follow source defaults and may succeed +or fail; that network condition is part of this development fixture, not a +controlled release-performance benchmark. Paired genuine Relay coverage remains +a separate package. + +Each of five samples clears only the disposable app and its credentials, then: + +1. Launches the full app and waits for Create a passcode. +2. Creates public fixture PIN1111, declines biometrics and skips introduction. +3. Taps Start New and waits for production setup's Next, then reaches the home + list after explicitly skipping backup. Never opens a seed/recovery-word view. +4. Requires visible-wallet count1 and Mobile Wallet, stops the process, launches + again, enters PIN1111 and requires the same visible count. +5. Exercises More→Wallets and requires the one-wallet list again. This is a + bounded fresh-state navigation check; it does not reproduce #7055's original + backup-delete/multiuser trigger or close that investigation. + +Only metadata.json receives bounded raw durations/counts/stage labels. Five +separate startup-to-passcode and StartNew-to-ready values are retained; no median +can hide an individual failure. Timings include Maestro driver and React Native +development overhead, with a warm host bundle and cold app process. They are +actual UI timings when run, not release-build performance or module microbenchmarks. +The authenticated-home duration includes the automated PIN taps. The setup-to-home +duration includes the explicit recovery-backup skip interaction. + +All stages must occur once in order, JUnit must fully pass, the artifact must stay +unchanged and exactly one createNewApp call may occur across setup and cold +relaunch. Any missing stage, duplicate request, changed artifact, native/tool error +or incomplete sample keeps the suite failing. Failure metadata retains only +completed fixed stages/numeric durations/error class; it never exports raw errors. +Maestro's temporary diagnostics (including automatic failure screenshots), Metro +logs and app/credential state are deleted; upload only the existing bounded +`artifacts/native/*/metadata.json` path. No screenshot, hierarchy, raw command/app +log or database artifact belongs in uploaded evidence. + +## Verification and limits + +`python3 -m unittest discover -s .github/tests -p test_product_measurements.py` +tests real loopback HTTP allowlisting, rejection and nonretention, ordered receipt +validation, missing/duplicate setup failure and partial-flow preservation. +Those tests provide no native acceptance. Before the first hosted execution, +the full-app Maestro selectors and per-platform credential-reset behavior remain +unverified. Record actual execution outcomes for each exact candidate; never +copy passes or raw timings from another SHA. Physical biometrics and devices, +offline/error/retry scenarios, full deletion/reference protection, actual +protected-backup operations and release readiness remain separate evidence. diff --git a/src/screens/Home/components/Wallet/HomeWallet.tsx b/src/screens/Home/components/Wallet/HomeWallet.tsx index 4bcb0469ad..ff03b8b059 100644 --- a/src/screens/Home/components/Wallet/HomeWallet.tsx +++ b/src/screens/Home/components/Wallet/HomeWallet.tsx @@ -203,6 +203,7 @@ const HomeWallet = () => { onPress={collaborativeHandoff.openAdd} /> } From 749f048c63c80a955d1687c63ea45c9932505e77 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Thu, 22 Oct 2026 06:19:10 +0000 Subject: [PATCH 230/254] ci: revoke stale same-head native success on identity or route races --- .github/tests/keeper-local-validation.test.cjs | 9 +++++++-- .github/workflows/keeper-local-validation.yml | 8 +++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/.github/tests/keeper-local-validation.test.cjs b/.github/tests/keeper-local-validation.test.cjs index c3d6c0a712..36d0c74595 100644 --- a/.github/tests/keeper-local-validation.test.cjs +++ b/.github/tests/keeper-local-validation.test.cjs @@ -162,7 +162,7 @@ async function evaluate(comments, options = {}) { const core = { info: () => {}, warning: text => warnings.push(text), setFailed: text => failures.push(text) }; await runWorkflow(github, context, core); - assert.equal(statuses.length, options.afterHead || options.baseRef || options.afterMutation ? 0 : 1); + assert.equal(statuses.length, options.afterHead || options.baseRef ? 0 : 1); return { status: statuses[0], statuses, failures, warnings, calls, pullsRequested, runsCalls: calls.filter(c => c.route === runsRoute), jobsCalls: calls.filter(c => c.route === jobsRoute) }; @@ -636,7 +636,12 @@ test('base, repository/ref or route movement at unchanged head cannot publish st pr => { pr.head.repo.full_name = 'other/repo'; }, pr => { pr.labels = [{ name: 'keeper/native-diagnostic' }]; }, ]) { const { statuses, failures } = await evaluate([handoff(sha, 'IOS, ANDROID')], reusableOptions({ afterMutation })); - assert.equal(statuses.length, 0); + assert.equal(statuses.length, 1); + assert.equal(statuses[0].state, 'failure', 'same-head race explicitly supersedes prior success'); assert.match(failures[0], /changed during validation/); } }); + +test('route-label changes immediately reevaluate the trusted gate', () => { + assert.ok(lines.some(line => line.includes('ready_for_review, labeled, unlabeled]'))); +}); diff --git a/.github/workflows/keeper-local-validation.yml b/.github/workflows/keeper-local-validation.yml index 09ffb6559f..955aeeb775 100644 --- a/.github/workflows/keeper-local-validation.yml +++ b/.github/workflows/keeper-local-validation.yml @@ -4,7 +4,7 @@ name: Keeper local validation gate on: pull_request_target: branches: [sprint] - types: [opened, reopened, synchronize, edited, ready_for_review] + types: [opened, reopened, synchronize, edited, ready_for_review, labeled, unlabeled] issue_comment: types: [created, edited, deleted] workflow_run: @@ -303,6 +303,12 @@ jobs: current.base.repo?.id !== pr.base.repo?.id || current.base.repo?.full_name !== pr.base.repo?.full_name || current.head.ref !== pr.head.ref || current.head.repo?.id !== pr.head.repo?.id || current.head.repo?.full_name !== pr.head.repo?.full_name || nativeRoute(current) !== nativeRoute(pr)) { + // The head is unchanged, so explicitly revoke any prior success on this SHA. + await github.rest.repos.createCommitStatus({ + owner, repo, sha, state: 'failure', context: contextName, + description: 'PR base, identity or native route changed; fresh evaluation required', + target_url: pr.html_url + }); core.setFailed('PR base, repository, ref or native route changed during validation; awaiting fresh evaluation'); return; } From 6a78b73084fc9575f8f2b6b98c6080ad57465d49 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Thu, 22 Oct 2026 06:19:35 +0000 Subject: [PATCH 231/254] docs: reflect reconciled R4 in current R5 acceptance obligations --- .github/native/release_acceptance.cjs | 2 +- AGENTS.md | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/native/release_acceptance.cjs b/.github/native/release_acceptance.cjs index 01a8c5e0b0..da53bd7e14 100644 --- a/.github/native/release_acceptance.cjs +++ b/.github/native/release_acceptance.cjs @@ -15,7 +15,7 @@ function acceptance({ trusted, ios, android }) { id, status: index < 2 ? (executed ? 'PASS' : 'NOT VERIFIED') : 'NOT IMPLEMENTED', scope: index === 1 ? 'CI entrypoint; actual native storage, not application account isolation' : - index > 1 ? 'Requires reviewed app scenarios; R5 R4-dependent checks wait for reconciliation' : + index > 1 ? 'Requires reviewed app scenarios on the exact combined candidate' : 'Unsigned simulator / development debug-key emulator; narrow passcode smoke', })); return { release_ready: false, phase_a: 'BLOCKED', cases, diff --git a/AGENTS.md b/AGENTS.md index 2f1b4d83e5..2cd59809bf 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -40,8 +40,8 @@ runs are not trusted acceptance for R5 or another source SHA. Record the exact source SHA, workflow run/attempt, both platforms' actual build/UI outcomes, safe artifact hashes and unresolved feature acceptance. A startup smoke or representative probe does not prove R5's deletion, recovery, -upgrade or security matrix. R4-dependent app checks wait for R4 baseline -reconciliation; unresolved high-risk cases remain blocking. Phase B protected +upgrade or security matrix. Released R4 is already reconciled. Product checks must run on the exact combined +R5 candidate; unresolved high-risk cases remain blocking. Phase B protected signing and distribution are **NOT IMPLEMENTED / NOT VERIFIED** until their separate reviewed workflow and exact-artifact controls are proven. Do not merge, sign, upload a beta, submit to a store or publish without explicit owner approval @@ -69,7 +69,7 @@ for that action and artifact. Passing CI provides none of these approvals. ``` 4. For app/code changes, `Required local checks` must list one or more of `IOS`, `ANDROID`, `HARDWARE` (comma-separated). Use both iOS and Android for cross-platform changes; add hardware for signer/device interactions. If no Mac tests are necessary, explain why, but the GitHub gate exempts only documentation and `.github/` changes. The handoff must name the actual PR head commit SHA after the final cloud push. -5. Return the PR URL/number and actual platform `PASS`/`FAIL`/`NOT RUN` results for the current head. Use `NEEDS_LOCAL` for remaining native or physical requirements; successful trusted hosted evidence may satisfy `IOS`/`ANDROID` without a Mac login. Continue unrelated cloud tasks while waiting for a Mac. Follow [hosted native validation](docs/hosted-native-validation.md) for trust, bootstrap, scope and artifact limitations. +5. Return the PR URL/number and actual platform `PASS`/`FAIL`/`NOT RUN` results for the current head. Use `NEEDS_LOCAL` for remaining native or physical requirements; successful trusted hosted evidence may satisfy `IOS`/`ANDROID` without a Mac login. Continue unrelated cloud tasks while remaining native or physical checks are scheduled. Follow [hosted native validation](docs/hosted-native-validation.md) for trust, bootstrap, scope and artifact limitations. 6. If a task needs only code review, explicitly state why local testing is not required rather than silently skipping it. ## Codex Local (Mac): validate the PR @@ -127,6 +127,6 @@ A known critical defect is not eligible for post-release deferral. In particular ## GitHub merge gate -The `.github/workflows/keeper-local-validation.yml` workflow posts commit status `keeper/local-validation` for the current PR head SHA. For code changes, it requires a trusted exact-head Cloud handoff. Each required `IOS`/`ANDROID` platform needs either a genuine successful hosted native build **and** disposable UI smoke from `.github/workflows/keeper-hosted-native.yml`, or a repository collaborator's exact-head `LOCAL_PASSED` report. A hosted run is trusted only when its workflow and complete `.github/native/` harness match the protected current base and the run's original base; PR-authored check names, comments and artifacts alone are not CI evidence. New or changed CI infrastructure must first be reviewed and merged before it can provide authoritative native evidence. The latest run/attempt must contain successful build and smoke steps for that platform; stale, skipped, cancelled, pending, build-only or untrusted evidence supplies no hosted pass. A latest explicit local failure remains blocking. `HARDWARE` still requires the existing local physical result or exact-SHA owner decision and subsequent honest local report above. New commits invalidate previous native evidence, local sign-off and deferral. Documentation and `.github/`-only PRs are exempt from Mac tests. +The `.github/workflows/keeper-local-validation.yml` workflow posts commit status `keeper/local-validation` for the current PR head SHA. For code changes, it requires a trusted exact-head Cloud handoff. Each required `IOS`/`ANDROID` platform needs either a genuine successful hosted native build **and** disposable UI smoke from the selected reviewed reusable or legacy hosted route, or a repository collaborator's exact-head `LOCAL_PASSED` report. A hosted run is trusted only when its workflow and complete `.github/native/` harness match the protected current base and the run's original base; PR-authored check names, comments and artifacts alone are not CI evidence. New or changed CI infrastructure must first be reviewed and merged before it can provide authoritative native evidence. The latest run/attempt must contain successful build and smoke steps for that platform; stale, skipped, cancelled, pending, build-only or untrusted evidence supplies no hosted pass. A latest explicit local failure remains blocking. `HARDWARE` still requires the existing local physical result or exact-SHA owner decision and subsequent honest local report above. New commits invalidate previous native evidence, local sign-off and deferral. Documentation and `.github/`-only PRs are exempt from Mac tests. This workflow **reports** a status; to **enforce** it, repository administrators must require the `keeper/local-validation` commit status for merges to `sprint` in GitHub Rulesets or branch protection. Require the existing `Tests` CI check as well. This is a review/test gate, not a guarantee that tests were actually executed; agents must report actual results truthfully. Release signing, TestFlight, Play Store, and production deployment remain separate and require explicit user approval. From 215ce814fa96f836e9280be31df0c0b4243e7f65 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Thu, 22 Oct 2026 06:21:35 +0000 Subject: [PATCH 232/254] test: reject stale or modified product measurement source --- .github/native/product_measurements.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/native/product_measurements.py b/.github/native/product_measurements.py index fdee1b8345..28811f5a68 100644 --- a/.github/native/product_measurements.py +++ b/.github/native/product_measurements.py @@ -119,9 +119,13 @@ def run(h): raise RuntimeError('PRODUCT_UI_DISPOSABLE_HOSTED_RUNNER_REQUIRED') data = h.read() first_private_command = len(data.get('commands', [])) + 1 + actual_source = subprocess.check_output(['git', 'rev-parse', 'HEAD'], cwd=h.ROOT, text=True).strip() if (data['build'] != 'PASS' or data['source_sha'] != os.environ.get('KEEPER_SOURCE_SHA') or - h.artifact_identity() != data['built_artifact']): + actual_source != data['source_sha'] or h.artifact_identity() != data['built_artifact']): raise RuntimeError('PRODUCT_UI_EXACT_BUILD_REQUIRED') + if subprocess.run(['git', 'diff', '--quiet', 'HEAD', '--', '.github/native', 'src', 'index.js'], + cwd=h.ROOT, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).returncode: + raise RuntimeError('PRODUCT_UI_COMMITTED_SOURCE_REQUIRED') expected_environment = { 'ENVIRONMENT': 'DEVELOPMENT', 'RELAY': 'http://127.0.0.1:8765/', 'SIGNING_SERVER_TESTNET': 'https://127.0.0.1:9/', From 0c21c5b4ae4784b94afe101eaf68b23f84eab720 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Thu, 22 Oct 2026 06:22:27 +0000 Subject: [PATCH 233/254] test: bind product measurements to committed bundle configuration --- .github/native/product_measurements.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/native/product_measurements.py b/.github/native/product_measurements.py index 28811f5a68..197769970b 100644 --- a/.github/native/product_measurements.py +++ b/.github/native/product_measurements.py @@ -123,7 +123,10 @@ def run(h): if (data['build'] != 'PASS' or data['source_sha'] != os.environ.get('KEEPER_SOURCE_SHA') or actual_source != data['source_sha'] or h.artifact_identity() != data['built_artifact']): raise RuntimeError('PRODUCT_UI_EXACT_BUILD_REQUIRED') - if subprocess.run(['git', 'diff', '--quiet', 'HEAD', '--', '.github/native', 'src', 'index.js'], + source_paths = ['.github/native', 'src', 'index.js', 'App.tsx', 'App.js', 'shim.js', + 'AndroidFontFix.js', 'metro.config.js', 'babel.config.js', 'app.json', + 'react-native.config.js', 'tsconfig.json', 'package.json', 'yarn.lock'] + if subprocess.run(['git', 'diff', '--quiet', 'HEAD', '--', *source_paths], cwd=h.ROOT, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).returncode: raise RuntimeError('PRODUCT_UI_COMMITTED_SOURCE_REQUIRED') expected_environment = { From 14a2459b30e31ea3099d56f6c6928a4a3b7d6995 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Thu, 22 Oct 2026 06:23:37 +0000 Subject: [PATCH 234/254] fix(r5): retain PIN fallback after biometric attempts --- docs/r5/BIOMETRIC-LIFECYCLE.md | 36 +++++++++++ src/screens/LoginScreen/Login.tsx | 29 ++++++++- tests/screens/PinEntry.test.tsx | 100 +++++++++++++++++++++++++++++- 3 files changed, 161 insertions(+), 4 deletions(-) diff --git a/docs/r5/BIOMETRIC-LIFECYCLE.md b/docs/r5/BIOMETRIC-LIFECYCLE.md index be44a76e4b..da2ad2f9b4 100644 --- a/docs/r5/BIOMETRIC-LIFECYCLE.md +++ b/docs/r5/BIOMETRIC-LIFECYCLE.md @@ -5,6 +5,42 @@ Source baseline: `c1c4b803039b61b940373c4dc9d08473cf25e042` (R5 core plus the follow-up, estimated at 1k–3k changed code/test tokens. It does not change credentials, signature verification, PIN attempt rules, storage or native inputs. +## Independent review follow-up + +Review of the initial callback correction at +`257fbe17e1a85d0e5d7e5d8ab97e1a69edbd9628` found that its new `loggingIn` +dependency reopened native biometrics after an authentication failure. The +failure handler sets `loggingIn` back to false, so a failed biometric credential +check, or an incorrect PIN after cancelling biometrics, scheduled another prompt +and interrupted the PIN fallback. A batched A → B → A account transition also +left a queued/pending callback active if React only rendered the final A. The +downstream authentication worker captures its epoch after dispatch, so it could +not reject that callback on the basis of its earlier prompt epoch. These are +component reproduction results, not proof of a successful native unlock or an +authentication bypass. + +The narrow follow-up retains effect cleanup and allows one automatic prompt per +eligible principal/session. A PIN submission consumes that scope's automatic +attempt. Cancellation or authentication failure leaves PIN fallback available; +ordinary logging state changes cannot open another prompt. A real principal or +login-method change and a completed cooldown may permit a fresh prompt. The +queued callback checks the existing live backup-account epoch before invoking +native biometrics and again before dispatching its result. No authentication +protocol or signature verification rule changed. + +Six added component controls preserve a failing-before comparison against the +initial correction: **4 FAIL / 13 PASS** before, **17 PASS / 0 FAIL / 0 SKIP** +after. They cover biometric-authentication failure, cancelled-prompt → incorrect +PIN fallback, queued and pending batched account cycles, a new principal, and a +completed cooldown. The existing initial-correction and PIN cases also pass. +Focused Login ESLint remains **FAIL: 12 errors / 4 warnings** on both sources, +with zero added/removed findings after line-offset normalization. Modified-test +ESLint, formatting and whitespace checks pass. Whole TypeScript and native +checks were not rerun by this follow-up; the initial patch's 943-diagnostic +TypeScript comparison below remains historical until the combined candidate is +checked. The new source/test delta remains within approximately 1k–3k changed +tokens and is kept as a separate review commit. + ## Demonstrated login defect The prior login function placed an asynchronous native biometric request inside diff --git a/src/screens/LoginScreen/Login.tsx b/src/screens/LoginScreen/Login.tsx index 96230889fa..cf5542c4e3 100644 --- a/src/screens/LoginScreen/Login.tsx +++ b/src/screens/LoginScreen/Login.tsx @@ -61,6 +61,7 @@ import ThemedColor from 'src/components/ThemedColor/ThemedColor'; import ThemedSvg from 'src/components/ThemedSvg.tsx/ThemedSvg'; import { setInitialNodesSaved } from 'src/store/reducers/network'; import { saveBackupMethodByAppId } from 'src/store/sagaActions/account'; +import { currentBackupAccountSession } from 'src/services/backup/transport'; const RNBiometrics = new ReactNativeBiometrics(); @@ -111,6 +112,8 @@ function LoginScreen({ navigation, route }) { const { allAccounts, biometricEnabledAppId } = useAppSelector((state) => state.account); const [forgotModal, setForgotModal] = useState(false); const completedLogin = useRef(false); + const biometricAttemptScope = useRef(null); + const renderedAccountSession = currentBackupAccountSession(); const onChangeTorStatus = (status: TorStatus) => { settorStatus(status); @@ -170,19 +173,38 @@ function LoginScreen({ navigation, route }) { const biometricEnabled = (allAccounts.length === 0 && loginMethod === LoginMethod.BIOMETRIC) || (allAccounts.length > 0 && biometricEnabledAppId !== null); - if (!biometricEnabled || !canLogin || isAuthenticated || loggingIn) return undefined; + if (!biometricEnabled || !canLogin) { + biometricAttemptScope.current = null; + return undefined; + } + + const scope = JSON.stringify([ + appId, + biometricEnabledAppId, + loginMethod, + allAccounts.length, + renderedAccountSession, + ]); + if (isAuthenticated || loggingIn) { + // An explicit PIN attempt owns this login; failure must keep its fallback + // available rather than reopening the biometric prompt automatically. + biometricAttemptScope.current = scope; + return undefined; + } + if (biometricAttemptScope.current === scope) return undefined; const originAppId = biometricEnabledAppId ?? appId; let active = true; const timer = setTimeout(async () => { - if (!active) return; + if (!active || currentBackupAccountSession() !== renderedAccountSession) return; + biometricAttemptScope.current = scope; try { const { success, signature } = await RNBiometrics.createSignature({ promptMessage: 'Authenticate', payload: originAppId, cancelButtonText: common.usePin, }); - if (!active || !success) return; + if (!active || !success || currentBackupAccountSession() !== renderedAccountSession) return; setIsBiometric(true); setLoginModal(true); setLogging(true); @@ -206,6 +228,7 @@ function LoginScreen({ navigation, route }) { isAuthenticated, loggingIn, loginMethod, + renderedAccountSession, ]); useEffect(() => { diff --git a/tests/screens/PinEntry.test.tsx b/tests/screens/PinEntry.test.tsx index 685f461659..6f8ab0aa70 100644 --- a/tests/screens/PinEntry.test.tsx +++ b/tests/screens/PinEntry.test.tsx @@ -11,8 +11,14 @@ import Buttons from 'src/components/Buttons'; const mockDispatch = jest.fn(); const mockCreateSignature = jest.fn(); +let mockBackupSession = 0; const mockState = { - login: { hasCreds: false, authenticationFailed: false, isAuthenticated: false }, + login: { + hasCreds: false, + authenticationFailed: false, + isAuthenticated: false, + credsAuthenticatedError: '', + }, settings: { loginMethod: 'PIN' }, account: { allAccounts: [], biometricEnabledAppId: null as string | null }, storage: { appId: 'disposable-test', failedAttempts: 0, lastLoginFailedAt: 0 }, @@ -23,6 +29,9 @@ jest.mock('src/store/hooks', () => ({ useAppDispatch: () => mockDispatch, useAppSelector: (selector) => selector(mockState), })); +jest.mock('src/services/backup/transport', () => ({ + currentBackupAccountSession: () => mockBackupSession, +})); jest.mock('src/context/Localization/LocContext', () => ({ LocalizationContext: require('react').createContext({ translations: { @@ -96,6 +105,11 @@ beforeEach(() => { mockDispatch.mockClear(); mockCreateSignature.mockReset(); mockState.login.isAuthenticated = false; + mockState.login.authenticationFailed = false; + mockState.login.credsAuthenticatedError = ''; + mockState.storage.failedAttempts = 0; + mockState.storage.lastLoginFailedAt = 0; + mockBackupSession = 0; mockState.settings.loginMethod = 'PIN'; mockState.storage.appId = 'disposable-test'; mockState.account.biometricEnabledAppId = null; @@ -152,6 +166,90 @@ describe('biometric login callback lifetime', () => { }); }); + test('failed biometric authentication keeps PIN fallback without another automatic prompt', async () => { + mockCreateSignature.mockResolvedValue({ success: true, signature: 'synthetic-signature' }); + await act(async () => jest.advanceTimersByTime(200)); + expect(mockCreateSignature).toHaveBeenCalledTimes(1); + act(() => { + mockState.login.authenticationFailed = true; + (mockState.login as any).credsAuthenticatedError = 'Biometric Auth Failed'; + screen.update(); + }); + await act(async () => jest.advanceTimersByTime(200)); + mockState.login.authenticationFailed = false; + (mockState.login as any).credsAuthenticatedError = ''; + expect(mockCreateSignature).toHaveBeenCalledTimes(1); + }); + + test('cancelled native prompt preserves PIN fallback after an incorrect PIN', async () => { + await act(async () => jest.advanceTimersByTime(200)); + expect(mockCreateSignature).toHaveBeenCalledTimes(1); + enter('1111'); + act(() => screen.root.findByType(Buttons).props.primaryCallback()); + expect(authCalls()).toHaveLength(1); + expect(authCalls()[0][0].payload.method).toBe('PIN'); + act(() => { + mockState.login.authenticationFailed = true; + mockState.login.credsAuthenticatedError = 'Incorrect Passcode'; + screen.update(); + }); + await act(async () => jest.advanceTimersByTime(200)); + expect(mockCreateSignature).toHaveBeenCalledTimes(1); + expect(screen.root.findByType(Buttons).props.primaryDisable).toBe(true); + }); + + test('a batched A to B to A session change invalidates a pending native result', async () => { + let complete = (_result: { success: boolean; signature: string }) => {}; + mockCreateSignature.mockImplementation( + () => + new Promise((resolve) => { + complete = resolve; + }) + ); + await act(async () => jest.advanceTimersByTime(200)); + expect(mockCreateSignature).toHaveBeenCalledTimes(1); + // Redux observes both transitions even if React only renders the final A. + mockBackupSession += 2; + await act(async () => complete({ success: true, signature: 'stale-synthetic-signature' })); + expect(authCalls()).toHaveLength(0); + }); + + test('a batched session change cancels the queued prompt before invoking native biometrics', async () => { + mockBackupSession += 2; + await act(async () => jest.advanceTimersByTime(200)); + expect(mockCreateSignature).not.toHaveBeenCalled(); + expect(authCalls()).toHaveLength(0); + }); + + test('a real principal change permits one new automatic prompt', async () => { + await act(async () => jest.advanceTimersByTime(200)); + act(() => { + mockBackupSession += 1; + mockState.storage.appId = 'other-disposable-account'; + screen.update(); + }); + await act(async () => jest.advanceTimersByTime(200)); + expect(mockCreateSignature).toHaveBeenCalledTimes(2); + expect(mockCreateSignature.mock.calls[1][0].payload).toBe('other-disposable-account'); + }); + + test('a completed cooldown permits one new automatic prompt', async () => { + await act(async () => jest.advanceTimersByTime(200)); + act(() => { + mockState.storage.failedAttempts = 1; + mockState.storage.lastLoginFailedAt = Date.now(); + screen.update(); + }); + await act(async () => jest.advanceTimersByTime(1000)); + expect(mockCreateSignature).toHaveBeenCalledTimes(1); + act(() => { + mockState.storage.lastLoginFailedAt = Date.now() - 301000; + screen.update(); + }); + await act(async () => jest.advanceTimersByTime(200)); + expect(mockCreateSignature).toHaveBeenCalledTimes(2); + }); + test('unmount cancels a queued prompt', async () => { act(() => screen.unmount()); await act(async () => jest.runOnlyPendingTimers()); From 38858e2608c385ea4134a4051fbff45a8185a2ae Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Thu, 22 Oct 2026 06:23:41 +0000 Subject: [PATCH 235/254] ci: execute separate R5 native features and full-product measurement packages --- .github/native/hosted_native.py | 136 ++++++++++++++++--- .github/tests/test_feature_runner.py | 104 ++++++++++++++ .github/tests/test_hosted_native.py | 2 + .github/workflows/keeper-native-platform.yml | 18 ++- AGENTS.md | 10 +- docs/r5-cloud-native-reconciliation.md | 11 ++ 6 files changed, 256 insertions(+), 25 deletions(-) create mode 100644 .github/tests/test_feature_runner.py diff --git a/.github/native/hosted_native.py b/.github/native/hosted_native.py index 3e421b53ed..855e4789ad 100644 --- a/.github/native/hosted_native.py +++ b/.github/native/hosted_native.py @@ -7,6 +7,7 @@ import hashlib import json +import math import os from pathlib import Path import platform @@ -470,7 +471,7 @@ def record(): 'run_id': os.environ.get('GITHUB_RUN_ID'), 'run_attempt': os.environ.get('GITHUB_RUN_ATTEMPT'), 'runner': {key: os.environ.get(key) for key in ['RUNNER_OS', 'RUNNER_ARCH', 'ImageOS', 'ImageVersion']}, - 'os': platform.platform(), 'commands': [], 'build': 'NOT RUN', 'smoke': 'NOT RUN', 'native_probe': 'NOT RUN', + 'os': platform.platform(), 'commands': [], 'build': 'NOT RUN', 'smoke': 'NOT RUN', 'native_probe': 'NOT RUN', 'r5_features': 'NOT RUN', 'hardware': 'NOT RUN', 'signing': 'NOT RUN', 'app_id': APP_IDS[PLATFORM], 'smoke_scope': 'Fresh install; render Create a passcode; type four disposable digits; assert Confirm your passcode; stop before persisting credentials.', 'startup_timing_scope': 'Bounded cold development bundle/startup allowance; no release performance acceptance.', @@ -490,6 +491,7 @@ def record(): data = read() data['toolchain'] = versions save(data) + disk_preflight('initial') def fixtures(): @@ -567,6 +569,7 @@ def android_sdk_metadata(sdk, component): def prepare(): + disk_preflight('before_dependencies') command(['npm', 'install', '--global', 'yarn@1.22.22', '--ignore-scripts'], 'Install pinned Yarn', timeout=180) command(['yarn', 'install', '--frozen-lockfile', '--ignore-scripts', '--non-interactive'], 'Install immutable JavaScript dependencies', timeout=900) @@ -696,7 +699,21 @@ def driver(): raise RuntimeError('Owned driver-preflight simulator cleanup failed.') +def disk_preflight(stage): + free = shutil.disk_usage(ROOT).free + minimum = 12 * 1024 ** 3 + data = read() + data.setdefault('disk_preflight', []).append({ + 'stage': stage, 'free_bytes': free, 'minimum_free_bytes': minimum, + 'result': 'PASS' if free >= minimum else 'FAIL', + }) + save(data) + if free < minimum: + raise RuntimeError('Insufficient disposable-runner disk reserve; no cache or user data was removed.') + + def build(): + disk_preflight('before_native_build') if PLATFORM == 'IOS': architecture = platform.machine() if architecture not in ['arm64', 'x86_64']: @@ -751,27 +768,102 @@ def warm_metro_bundle(suite='startup'): finally: details['duration_seconds'] = round(time.monotonic() - started, 1) data = read() - data['metro_warmup' if suite == 'startup' else 'probe_bundle'] = details + data['metro_warmup' if suite == 'startup' else suite + '_bundle'] = details save(data) +R5_SCENARIOS = [ + 'production-setup-offline-retry-native-counts', + 'production-deletion-retained-native-references', + 'production-deletion-native-rollback', + 'production-worker-stale-session-rejection', + 'native-worker-process-relaunch-counts', +] +R5_LIMITS = [ + 'controlled-transport-no-backend-acceptance', + 'synthetic-wallet-generation-no-crypto-acceptance', + 'injected-dispatch-selection-no-login-acceptance', + 'no-product-ui-navigation-pin-or-biometric-acceptance', + 'probe-and-worker-timings-not-product-startup-ux', +] +R5_SOURCES = { + 'src/utils/utilities.ts': ['getAccountFromSigner', 'getKeyUID'], + 'src/store/sagas/storage.ts': ['setupKeeperAppWorker'], + 'src/store/sagas/wallets.ts': ['currentWalletAccountId', 'assertWalletAccount', 'deleteSigningDeviceWorker'], + 'src/store/sagas/bhr.ts': ['currentBackupAppId', 'deleteAppImageEntityWorker'], + 'src/services/backup/transport.ts': ['observeBackupAccount', 'currentBackupAccountSession', 'clearVerifiedRemoteRevision'], +} + + +def validate_r5_feature_evidence(raw): + if not isinstance(raw, str) or len(raw.encode()) > 65536: + raise ValueError('Feature evidence absent or oversized.') + def invalid_constant(value): + raise ValueError('Non-finite feature measurement.') + payload = json.loads(raw, parse_constant=invalid_constant) + if (payload.get('schema') != 1 or payload.get('scope') != 'production-worker-native-storage' + or payload.get('status') != 'PASS' or payload.get('stage') != 'pass' + or payload.get('scenarios') != [{'id': item, 'status': 'PASS'} for item in R5_SCENARIOS] + or payload.get('limits') != R5_LIMITS): + raise ValueError('Feature scenario evidence is incomplete or outside reviewed scope.') + measurements = payload.get('measurements') + if not isinstance(measurements, list) or len(measurements) != 5: + raise ValueError('Five raw helper measurements are required.') + for iteration, row in enumerate(measurements, 1): + if not isinstance(row, dict) or set(row) != {'iteration', 'probe_ready_ms', 'setup_first_wallet_ms'} or row['iteration'] != iteration: + raise ValueError('Invalid feature measurement shape.') + for field in ['probe_ready_ms', 'setup_first_wallet_ms']: + value = row[field] + if type(value) not in [int, float] or not math.isfinite(value) or not 0 <= value <= 600000: + raise ValueError('Invalid bounded feature measurement.') + sources = payload.get('source_functions') + expected = [{'path': path, 'sha256': hashlib.sha256((ROOT / path).read_bytes()).hexdigest(), 'names': names} + for path, names in R5_SOURCES.items()] + if sources != expected: + raise ValueError('Feature workers do not match exact-candidate production source.') + # Export only fixed scope/status, public source digests and bounded numbers. + # Never forward arbitrary payload fields or native failure messages. + return {'schema': 1, 'scope': 'production-worker-native-storage', 'status': 'PASS', + 'scenarios': payload['scenarios'], 'measurements': measurements, + 'limits': R5_LIMITS, 'source_functions': sources} + + +def collect_r5_feature_evidence(device): + filename = 'keeper-r5-native-evidence.json' + if PLATFORM == 'IOS': + container = Path(command(['xcrun', 'simctl', 'get_app_container', device, APP_IDS[PLATFORM], 'data'], + 'Locate disposable feature evidence', capture=True)) + path = container / 'Documents' / filename + if path.stat().st_size > 65536: + raise ValueError('Oversized native feature evidence.') + raw = path.read_text() + else: + raw = command(['adb', '-s', device, 'exec-out', 'run-as', APP_IDS[PLATFORM], + 'head', '-c', '65537', 'files/' + filename], + 'Read bounded disposable feature evidence', capture=True, error_details=False) + return validate_r5_feature_evidence(raw) + + def smoke(suite='startup'): - if suite not in ['startup', 'probe']: + if suite not in ['startup', 'probe', 'features']: raise ValueError('Unknown disposable native suite.') is_probe = suite == 'probe' - result_key = 'native_probe' if is_probe else 'smoke' - if is_probe and read()['smoke'] != 'PASS': + is_features = suite == 'features' + result_key = 'r5_features' if is_features else 'native_probe' if is_probe else 'smoke' + if (is_probe or is_features) and read()['smoke'] != 'PASS': raise RuntimeError('The real app startup smoke must pass before module probes.') if read()['build'] != 'PASS': raise RuntimeError('A successful native build is required before smoke testing.') if artifact_identity() != read()['built_artifact']: raise RuntimeError('Built artifact changed before the disposable startup smoke.') - report = PRIVATE / ('probe-junit.xml' if is_probe else 'smoke-junit.xml') + if is_features: + command(['node', '.github/native/r5_prepare.cjs'], 'Prepare exact-source R5 production worker probe', timeout=60, error_details=False) + report = PRIVATE / ('features-junit.xml' if is_features else 'probe-junit.xml' if is_probe else 'smoke-junit.xml') report.unlink(missing_ok=True) metro_output = (PRIVATE / 'metro.log').open('wb') metro_args = ['node', 'node_modules/react-native/cli.js', 'start', '--port', '8081'] - if is_probe: - metro_args += ['--config', str(ROOT / '.github/native/release_metro.cjs')] + if is_probe or is_features: + metro_args += ['--config', str(ROOT / ('.github/native/r5_metro.cjs' if is_features else '.github/native/release_metro.cjs'))] metro = subprocess.Popen(metro_args, cwd=ROOT, stdout=metro_output, stderr=subprocess.STDOUT, start_new_session=True) device = None @@ -823,16 +915,18 @@ def smoke(suite='startup'): command([data['maestro']['binary'], '--device', device, 'test', '--env', 'APP_ID=' + APP_IDS[PLATFORM], '--flatten-debug-output', '--debug-output', str(PRIVATE / 'maestro-output'), '--format', 'junit', '--output', str(report), '--test-output-dir', str(PRIVATE / 'maestro-output'), - '.github/native/release_probe.yaml' if is_probe else '.github/native/startup.yaml'], - 'Assert native persistence across process launches' if is_probe else - 'Assert startup and disposable passcode transition', timeout=300 if is_probe else 420, - error_details=not is_probe) + '.github/native/r5_features.yaml' if is_features else '.github/native/release_probe.yaml' if is_probe else '.github/native/startup.yaml'], + 'Assert R5 production workers over native storage' if is_features else 'Assert native persistence across process launches' if is_probe else + 'Assert startup and disposable passcode transition', timeout=660 if is_features else 300 if is_probe else 420, + error_details=not (is_probe or is_features)) cases = successful_junit(report) if artifact_identity() != read()['built_artifact']: raise RuntimeError('Built artifact changed during native execution.') data = read() data[result_key] = 'PASS' data[result_key + '_test_cases'] = cases + if is_features: + data['r5_feature_evidence'] = collect_r5_feature_evidence(device) if is_probe: data['probe_scope'] = ('CI-only Metro entrypoint; actual encrypted Realm/current schemas and production MMKV; ' 'three process launches, rollback, wrong-key rejection, targeted primitive deletion, ' @@ -841,12 +935,12 @@ def smoke(suite='startup'): save(data) except Exception: try: - if not is_probe: + if not (is_probe or is_features): record_startup_failure(report, device) else: data = read() - data['native_probe'] = 'FAIL' - data['probe_diagnostics'] = 'Native module probe failed; raw output retained only on ephemeral runner.' + data[result_key] = 'FAIL' + data['probe_diagnostics'] = 'Native module or worker probe failed; raw output retained only on ephemeral runner.' save(data) except Exception as error: # Diagnostics never replace the original failed native/UI result. @@ -893,9 +987,13 @@ def probe(): smoke('probe') +def features(): + smoke('features') + + def main(): - if PLATFORM not in APP_IDS or len(sys.argv) != 2 or sys.argv[1] not in ['record', 'prepare', 'driver', 'build', 'smoke', 'probe']: - raise RuntimeError('Expected a hosted platform and record|prepare|driver|build|smoke|probe.') + if PLATFORM not in APP_IDS or len(sys.argv) != 2 or sys.argv[1] not in ['record', 'prepare', 'driver', 'build', 'smoke', 'probe', 'features']: + raise RuntimeError('Expected a hosted platform and record|prepare|driver|build|smoke|probe|features.') os.environ.update({'ENVFILE': '.env.ci', 'RCT_NO_LAUNCH_PACKAGER': '1', 'RCT_NEW_ARCH_ENABLED': '1', 'MAESTRO_CLI_NO_ANALYTICS': '1'}) run_phase(sys.argv[1]) @@ -908,8 +1006,8 @@ def run_phase(phase): if EVIDENCE.exists(): evidence = read() evidence['failed_phase'] = phase - if phase in ['build', 'smoke', 'probe']: - evidence['native_probe' if phase == 'probe' else phase] = 'FAIL' + if phase in ['build', 'smoke', 'probe', 'features']: + evidence['r5_features' if phase == 'features' else 'native_probe' if phase == 'probe' else phase] = 'FAIL' save(evidence) raise diff --git a/.github/tests/test_feature_runner.py b/.github/tests/test_feature_runner.py new file mode 100644 index 0000000000..7aa7e040a4 --- /dev/null +++ b/.github/tests/test_feature_runner.py @@ -0,0 +1,104 @@ +"""Host contracts for native evidence validation; no native acceptance.""" +import copy +import hashlib +import importlib.util +import json +from pathlib import Path +import tempfile +import unittest +from unittest import mock + +SPEC = importlib.util.spec_from_file_location('native_features', Path(__file__).resolve().parents[1] / 'native/hosted_native.py') +native = importlib.util.module_from_spec(SPEC) +SPEC.loader.exec_module(native) + + +class FeatureEvidenceTest(unittest.TestCase): + def setUp(self): + self.directory = tempfile.TemporaryDirectory() + self.addCleanup(self.directory.cleanup) + self.root = Path(self.directory.name) + for path in native.R5_SOURCES: + file = self.root / path + file.parent.mkdir(parents=True, exist_ok=True) + file.write_text('public synthetic current source') + patch = mock.patch.multiple(native, ROOT=self.root, EVIDENCE=self.root / 'metadata.json') + patch.start() + self.addCleanup(patch.stop) + self.payload = { + 'schema': 1, 'scope': 'production-worker-native-storage', 'status': 'PASS', 'stage': 'pass', + 'scenarios': [{'id': item, 'status': 'PASS'} for item in native.R5_SCENARIOS], + 'measurements': [{'iteration': index, 'probe_ready_ms': 100.5, 'setup_first_wallet_ms': 25} for index in range(1, 6)], + 'limits': native.R5_LIMITS, + 'source_functions': [{'path': path, 'sha256': hashlib.sha256((self.root / path).read_bytes()).hexdigest(), 'names': names} + for path, names in native.R5_SOURCES.items()], + } + + def test_only_bounded_metadata_survives_exact_source_validation(self): + self.payload['private_message'] = 'PRIVATE_SENTINEL' + evidence = native.validate_r5_feature_evidence(json.dumps(self.payload)) + self.assertEqual(evidence['status'], 'PASS') + self.assertEqual(len(evidence['measurements']), 5) + self.assertNotIn('PRIVATE_SENTINEL', json.dumps(evidence)) + self.assertIn('probe-and-worker-timings-not-product-startup-ux', evidence['limits']) + + def test_incomplete_wrong_scope_or_failed_scenarios_cannot_supply_credit(self): + for field, value in [('schema', 2), ('scope', 'full-app-acceptance'), ('status', 'FAIL'), + ('stage', 'running'), ('scenarios', self.payload['scenarios'][:-1]), + ('measurements', self.payload['measurements'][:-1]), ('limits', [])]: + with self.subTest(field=field): + payload = {**self.payload, field: value} + with self.assertRaises(ValueError): + native.validate_r5_feature_evidence(json.dumps(payload)) + + def test_failed_skipped_or_duplicate_scenario_is_rejected(self): + for mutation in ['failure', 'skip', 'duplicate']: + payload = copy.deepcopy(self.payload) + if mutation == 'duplicate': + payload['scenarios'][1] = payload['scenarios'][0] + else: + payload['scenarios'][0]['status'] = mutation + with self.assertRaises(ValueError): + native.validate_r5_feature_evidence(json.dumps(payload)) + + def test_measurements_must_be_raw_ordered_finite_bounded_numbers(self): + for field, value in [('iteration', 2), ('probe_ready_ms', float('nan')), ('probe_ready_ms', True), + ('probe_ready_ms', -1), ('setup_first_wallet_ms', 600001), + ('setup_first_wallet_ms', '25')]: + with self.subTest(field=field, value=value): + payload = copy.deepcopy(self.payload) + payload['measurements'][0][field] = value + with self.assertRaises(ValueError): + native.validate_r5_feature_evidence(json.dumps(payload)) + + def test_stale_source_wrong_function_selection_and_oversized_data_fail_closed(self): + payload = copy.deepcopy(self.payload) + payload['source_functions'][0]['sha256'] = '0' * 64 + with self.assertRaises(ValueError): + native.validate_r5_feature_evidence(json.dumps(payload)) + payload = copy.deepcopy(self.payload) + payload['source_functions'][0]['names'] = ['otherFunction'] + with self.assertRaises(ValueError): + native.validate_r5_feature_evidence(json.dumps(payload)) + with self.assertRaises(ValueError): + native.validate_r5_feature_evidence(' ' * 65537) + + def test_disk_reserve_is_recorded_and_exhaustion_stops_without_cleanup(self): + native.save({}) + with mock.patch.object(native.shutil, 'disk_usage', return_value=mock.Mock(free=1024)), \ + mock.patch.object(native.shutil, 'rmtree') as cleanup: + with self.assertRaisesRegex(RuntimeError, 'disk reserve'): + native.disk_preflight('before_native_build') + cleanup.assert_not_called() + evidence = native.read()['disk_preflight'][0] + self.assertEqual(evidence['result'], 'FAIL') + self.assertEqual(evidence['free_bytes'], 1024) + self.assertEqual(evidence['minimum_free_bytes'], 12 * 1024 ** 3) + + def test_feature_runtime_requires_successful_product_startup_before_extraction(self): + native.save({'build': 'PASS', 'smoke': 'NOT RUN'}) + with mock.patch.object(native.subprocess, 'Popen') as process: + with self.assertRaisesRegex(RuntimeError, 'app startup smoke'): + native.run_phase('features') + process.assert_not_called() + self.assertEqual(native.read()['r5_features'], 'FAIL') diff --git a/.github/tests/test_hosted_native.py b/.github/tests/test_hosted_native.py index a1b9fed4f3..d7105f0bfd 100644 --- a/.github/tests/test_hosted_native.py +++ b/.github/tests/test_hosted_native.py @@ -446,6 +446,7 @@ def test_smoke_requires_successful_build(self): def test_ios_build_rejects_unsupported_architecture_before_invoking_xcode(self): with mock.patch.object(native, 'PLATFORM', 'IOS'), \ + mock.patch.object(native, 'disk_preflight'), \ mock.patch.object(native.platform, 'machine', return_value='riscv64'), \ mock.patch.object(native, 'command') as command, \ self.assertRaisesRegex(RuntimeError, 'Unsupported hosted iOS simulator architecture'): @@ -455,6 +456,7 @@ def test_ios_build_rejects_unsupported_architecture_before_invoking_xcode(self): def test_ios_build_uses_only_the_supported_host_simulator_architecture(self): for architecture in ['arm64', 'x86_64']: with self.subTest(architecture=architecture), \ + mock.patch.object(native, 'disk_preflight'), \ mock.patch.object(native, 'PLATFORM', 'IOS'), \ mock.patch.object(native.platform, 'machine', return_value=architecture), \ mock.patch.object(native, 'artifact_identity', return_value={'sha256': 'test artifact'}), \ diff --git a/.github/workflows/keeper-native-platform.yml b/.github/workflows/keeper-native-platform.yml index c4004ee1ce..b6d440157a 100644 --- a/.github/workflows/keeper-native-platform.yml +++ b/.github/workflows/keeper-native-platform.yml @@ -25,7 +25,7 @@ jobs: name: Native ${{ inputs.platform }} build and probes if: github.event_name == 'pull_request' && github.event.pull_request.number != 7045 && inputs.source_sha == github.event.pull_request.head.sha && (toJSON(github.event.pull_request.draft) == 'false' || (inputs.diagnostic && contains(github.event.pull_request.labels.*.name, 'keeper/native-diagnostic'))) runs-on: ${{ inputs.platform == 'IOS' && 'macos-15' || 'ubuntu-24.04' }} - timeout-minutes: 75 + timeout-minutes: ${{ inputs.platform == 'IOS' && 105 || 90 }} env: CI: 'true' HUSKY: '0' @@ -103,6 +103,18 @@ jobs: test "$(git rev-parse HEAD)" = '${{ inputs.source_sha }}' git diff --exit-code HEAD -- .github/native .github/workflows python3 .github/native/hosted_native.py probe + - name: Run R5 production worker native scenarios + if: inputs.platform == 'IOS' + run: | + test "$(git rev-parse HEAD)" = '${{ inputs.source_sha }}' + git diff --exit-code HEAD -- .github/native .github/workflows + python3 .github/native/hosted_native.py features + - name: Measure five full-product startup and first-wallet runs + if: inputs.platform == 'IOS' + run: | + test "$(git rev-parse HEAD)" = '${{ inputs.source_sha }}' + git diff --exit-code HEAD -- .github/native .github/workflows + python3 .github/native/product_measurements.py run - name: Enable emulator acceleration if: inputs.platform == 'ANDROID' run: | @@ -129,6 +141,10 @@ jobs: python3 .github/native/hosted_native.py smoke git diff --exit-code HEAD -- .github/native .github/workflows python3 .github/native/hosted_native.py probe + git diff --exit-code HEAD -- .github/native .github/workflows + python3 .github/native/hosted_native.py features + git diff --exit-code HEAD -- .github/native .github/workflows + python3 .github/native/product_measurements.py run - name: Retain safe Phase A evidence if: always() uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6 diff --git a/AGENTS.md b/AGENTS.md index 2cd59809bf..6947232bab 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -9,7 +9,7 @@ Candidate diagnostics are explicit read-only runs on the exact PR head. Add `kee Never merge, promote a shared branch, release-sign, upload, distribute, deploy or publish without the owner's explicit approval. Draft PR creation and isolated branch pushes are preparation, not release approval. See `docs/r5-cloud-native-reconciliation.md` for source identities and outstanding acceptance. -The prepared workflow is **Cloud code complete → hosted native validation → authorized physical checks → release review**. Until the hosted infrastructure is reviewed, installed and verified, retain the existing Local path below as the safe fallback; do not assume the owner's Mac is available. Signing remains a separate approved action. Follow the [bootstrap adoption order](docs/cloud-bootstrap-preparation.md), including the later restacking of Phase A #7062. The GitHub pull request is the durable handoff between stages. Do **not** rely on one agent having access to another agent's conversation, unpushed changes, machine, or installed tools. +The default workflow is **Cloud code complete → hosted native validation → authorized physical checks → release review**. Before protected-base installation, run exact-candidate diagnostics and keep trusted acceptance fail-closed. The Local format below remains a compatibility fallback for an available authorized tester; the owner's personal Mac is not a prerequisite. Signing remains a separate approved action. Follow the [bootstrap adoption order](docs/cloud-bootstrap-preparation.md), including the later restacking of Phase A #7062. The GitHub pull request is the durable handoff between stages. Do **not** rely on one agent having access to another agent's conversation, unpushed changes, machine, or installed tools. Repository: `KeeperCommunity/bitcoin-keeper`. Use `sprint` as the PR base unless the task explicitly specifies another base. @@ -68,11 +68,11 @@ for that action and artifact. Passing CI provides none of these approvals. Risks / known gaps: ``` -4. For app/code changes, `Required local checks` must list one or more of `IOS`, `ANDROID`, `HARDWARE` (comma-separated). Use both iOS and Android for cross-platform changes; add hardware for signer/device interactions. If no Mac tests are necessary, explain why, but the GitHub gate exempts only documentation and `.github/` changes. The handoff must name the actual PR head commit SHA after the final cloud push. +4. For app/code changes, `Required local checks` must list one or more of `IOS`, `ANDROID`, `HARDWARE` (comma-separated). Use both iOS and Android for cross-platform changes; add hardware for signer/device interactions. If a reported native or physical check is unnecessary, explain why, but the GitHub gate exempts only documentation and `.github/` changes. The handoff must name the actual PR head commit SHA after the final cloud push. 5. Return the PR URL/number and actual platform `PASS`/`FAIL`/`NOT RUN` results for the current head. Use `NEEDS_LOCAL` for remaining native or physical requirements; successful trusted hosted evidence may satisfy `IOS`/`ANDROID` without a Mac login. Continue unrelated cloud tasks while remaining native or physical checks are scheduled. Follow [hosted native validation](docs/hosted-native-validation.md) for trust, bootstrap, scope and artifact limitations. 6. If a task needs only code review, explicitly state why local testing is not required rather than silently skipping it. -## Codex Local (Mac): validate the PR +## Legacy Local validation format (available authorized testers) When given **"Validate Keeper PR #"**, first read the newest **Codex local handoff** section and inspect the PR head SHA. @@ -123,10 +123,10 @@ A known critical defect is not eligible for post-release deferral. In particular - `LOCAL_PASSED` means only the reported checks passed on the reported commit. It is **not** approval to merge, sign, distribute, upload to TestFlight/Play Store, or publish a release. - Release operations require **explicit user approval** for the intended build, environment, and distribution channel. - ChatGPT is the coordinator: when the user shares a PR or asks for a handoff/status, inspect the latest PR details and comments; then provide the next exact copy-and-paste prompt for Cloud or Local Codex. ChatGPT should not claim to know Codex's private session state or that the Mac is currently available. -- If the Mac is offline, the state remains `NEEDS_LOCAL`; cloud work does not have to stop. +- `NEEDS_LOCAL` is the legacy status label for outstanding checks; it does not require the owner's personal Mac. Continue hosted native work and name genuine physical-device dependencies explicitly. ## GitHub merge gate -The `.github/workflows/keeper-local-validation.yml` workflow posts commit status `keeper/local-validation` for the current PR head SHA. For code changes, it requires a trusted exact-head Cloud handoff. Each required `IOS`/`ANDROID` platform needs either a genuine successful hosted native build **and** disposable UI smoke from the selected reviewed reusable or legacy hosted route, or a repository collaborator's exact-head `LOCAL_PASSED` report. A hosted run is trusted only when its workflow and complete `.github/native/` harness match the protected current base and the run's original base; PR-authored check names, comments and artifacts alone are not CI evidence. New or changed CI infrastructure must first be reviewed and merged before it can provide authoritative native evidence. The latest run/attempt must contain successful build and smoke steps for that platform; stale, skipped, cancelled, pending, build-only or untrusted evidence supplies no hosted pass. A latest explicit local failure remains blocking. `HARDWARE` still requires the existing local physical result or exact-SHA owner decision and subsequent honest local report above. New commits invalidate previous native evidence, local sign-off and deferral. Documentation and `.github/`-only PRs are exempt from Mac tests. +The `.github/workflows/keeper-local-validation.yml` workflow posts commit status `keeper/local-validation` for the current PR head SHA. For code changes, it requires a trusted exact-head Cloud handoff. Each required `IOS`/`ANDROID` platform needs either a genuine successful hosted native build **and** disposable UI smoke from the selected reviewed reusable or legacy hosted route, or a repository collaborator's exact-head `LOCAL_PASSED` report. A hosted run is trusted only when its workflow and complete `.github/native/` harness match the protected current base and the run's original base; PR-authored check names, comments and artifacts alone are not CI evidence. New or changed CI infrastructure must first be reviewed and merged before it can provide authoritative native evidence. The latest run/attempt must contain successful build and smoke steps for that platform; stale, skipped, cancelled, pending, build-only or untrusted evidence supplies no hosted pass. A latest explicit local failure remains blocking. `HARDWARE` still requires the existing local physical result or exact-SHA owner decision and subsequent honest local report above. New commits invalidate previous native evidence, local sign-off and deferral. Documentation and `.github/`-only PRs are exempt from native tests. This workflow **reports** a status; to **enforce** it, repository administrators must require the `keeper/local-validation` commit status for merges to `sprint` in GitHub Rulesets or branch protection. Require the existing `Tests` CI check as well. This is a review/test gate, not a guarantee that tests were actually executed; agents must report actual results truthfully. Release signing, TestFlight, Play Store, and production deployment remain separate and require explicit user approval. diff --git a/docs/r5-cloud-native-reconciliation.md b/docs/r5-cloud-native-reconciliation.md index abf65be0b0..8c4bf722ef 100644 --- a/docs/r5-cloud-native-reconciliation.md +++ b/docs/r5-cloud-native-reconciliation.md @@ -35,3 +35,14 @@ Only bounded metadata is uploaded, with source/run/attempt, toolchain, artifact - This reconciliation: host policy/negative/provenance tests and semantic workflow lint are verified separately at its final commit. Actual R5 iOS/Android native build, feature checks, full-product startup/first-wallet timing and physical-device results remain NOT RUN until exact-candidate execution. Full Phase A and release readiness remain BLOCKED. Next: integrate independently reviewed scenario/product-measurement packages and completed 5.2 only after its authoritative checkpoint; verify ancestry and combined head; finish active inherited native jobs; schedule one candidate diagnostic run; retain all failures and rerun affected checks after any correction. Protected-base adoption/required-check configuration needs explicit approval and administrative verification. No merge, signing, upload, distribution, deployment or publication is authorized here. + + +## Feature and product UI hooks + +The reusable worker invokes `.github/native/hosted_native.py features` and then `.github/native/product_measurements.py run` after startup and primitive-storage probes. The first hook depends on the separately reviewed native-feature package; the second depends on the separate full-product measurement package. Those source packages are intentionally absent from the standalone foundation branch. A missing dependency fails execution; it cannot be treated as a feature pass. The integration candidate must include both before scheduling a native run. + +Worker evidence requires all five fixed scenarios, actual native persistence across six processes, five bounded raw helper measurements and source-function hashes matching the current candidate files. Only an allowlist of fixed metadata and numbers is exported. Helper timings remain explicitly different from the full-product measurements, whose runner uses the normal app/Metro entrypoint and records five raw startup/passcode/first-wallet/relaunch/UI samples. Controlled loopback transport in either runner grants no genuine paired-backend evidence. + +Runner disk preflight records available bytes and a 12 GiB minimum reserve before dependency preparation and native compilation; insufficient reserve fails without deleting another task's caches or data. iOS and Android job ceilings are 105 and 90 minutes respectively, with no automatic retry. These are upper bounds, not observed runtime or cost. Actual combined native execution remains NOT RUN here. + +The inherited infrastructure adoption is approximately 53.6k changed code/test tokens by the characters/4 estimate, with the new reconciliation delta approximately 6.7k before these hooks. The 19k estimate belongs to the original #7062 delta alone. Keep original source review units and the new reconciliation-only comparison separate; do not describe the aggregate as one sub-20k package. From 235df3be21714b45257e4e3ecb86dae1b27aaeed Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Thu, 22 Oct 2026 06:24:45 +0000 Subject: [PATCH 236/254] docs(r5): record genuine current-pair host integration passes --- docs/r5/PROTOCOL-ACCEPTANCE-20261022.md | 71 ++++++++++++++++++++----- 1 file changed, 58 insertions(+), 13 deletions(-) diff --git a/docs/r5/PROTOCOL-ACCEPTANCE-20261022.md b/docs/r5/PROTOCOL-ACCEPTANCE-20261022.md index c441a0b31d..bcaff2450e 100644 --- a/docs/r5/PROTOCOL-ACCEPTANCE-20261022.md +++ b/docs/r5/PROTOCOL-ACCEPTANCE-20261022.md @@ -7,10 +7,16 @@ used or changed. ## Exact source and pairing -- App source: `c1c4b803039b61b940373c4dc9d08473cf25e042`, the completed +- App AES source for the local backend suite: + `c1c4b803039b61b940373c4dc9d08473cf25e042`, the completed [5.1 package](https://github.com/KeeperCommunity/bitcoin-keeper/pull/7067). It contains R5 core `77b72da29ac5f2cdf3f51c9a0e9f3fd1c2af7238` and released R4 `1d8a86a7462d935cbd0e28b857934b5718c152fc` by verified ancestry. +- Hosted app integration source: + `e4245b01af4f48d3812f8e2abcae8bcfe65ae06a` from mobile draft #7068. + Verified ancestry includes the completed 5.1 source above. This is an interim + integration checkpoint; 5.2 identity/completion remains unverified and this is + not a final candidate freeze. - Relay: `29597b27eeeb299205bf7d0555db0a90c38cc4be`, verified against the current PR158 head and its [latest owner receipt](https://github.com/KeeperCommunity/bitcoin-keeper-relay/pull/158#issuecomment-6321195858). The PR description's earlier `7693ce4b` is stale. The current source includes @@ -45,7 +51,10 @@ This is backend/crypto interoperability evidence, not a native app run. | Current Relay TypeScript `--noEmit` | **PASS**, exit 0, no diagnostics | | Existing app protected-runner guard tests | **3/3 PASS**, zero failures/skips | | First backend attempt before AES dependency was available | **13 PASS / 39 hook failures**, retained; missing `crypto-js` prevented protected test bodies from running | -| Current app encrypted-Realm integration proofs | **BLOCKED locally**: actual normal N-API install exits 1 with proxy CONNECT 403; hosted fixed-pair route prepared separately, no app proof pass claimed | +| Current app full protected-v3 proof | **Hosted PASS: 43/43**, 65.869 seconds, on exact app `e4245b01` / Relay `29597b27` pair; zero failures/skips/cancellations/todo | +| Current app 5.1 upgraded-account witness | **Hosted PASS: 1/1**, 8.840 seconds, on the same exact pair; zero failures/skips/cancellations/todo | +| Existing runner guards in that hosted job | **PASS: 3/3**, 85ms; zero failures/skips/cancellations/todo | +| Same app proofs on this managed worker | **BLOCKED locally**: actual normal N-API install exits 1 with proxy CONNECT 403; the successful hosted route supplies the genuine binding | The successful backend suite exercises real signed HTTP, native signature verification and transactional Mongo state: enrollment; read-only absence; @@ -77,7 +86,7 @@ App runner-guard log SHA-256: The raw logs remain in the Cloud workspace; private backend source is not copied into this repository. -## Remaining acceptance +## Verified hosted route After the full frozen app install and six patches completed in Cloud, the normal Realm prerequisite install was attempted with Node 22.23.3: @@ -88,14 +97,50 @@ cd node_modules/realm ``` It exited 1 with `tunneling socket could not be established, statusCode=403`. -No policy bypass or mock substitute was used. A separate private fixed-pair -hosted CI package uses the existing PR159 route to obtain the genuine binding, -run the complete protected proof and the upgraded-account witness, and retain -private fixture source in the private repository. - -Run the existing current-Relay upgrade witness and complete current protected-v3 -app proof using that genuine encrypted Realm binding. Preserve the public-v2 -matrix's inherited failures separately. Rerun affected combined checks after -verified 5.2 integration; this receipt does not cover that package or any later -candidate SHA. Hosted iOS/Android feature acceptance and physical-device results +No policy bypass or mock substitute was used. The separate private fixed-pair +hosted package [Relay PR160](https://github.com/KeeperCommunity/bitcoin-keeper-relay/pull/160) +uses the existing PR159 route. Its exact control source +`4db6a758a307111137ad6bb4f35a32fa15b17a9d` ran +[Actions39732797761](https://github.com/KeeperCommunity/bitcoin-keeper-relay/actions/runs/39732797761), +job `119671456169`, successfully against the fixed full app and Relay SHAs above. +Frozen installs, all six app patches, actual Realm N-API binding installation, +three exact-source verifications and owned disposable Mongo cleanup passed. +Private fixture source remains in the private repository; no artifact or status +upload, production secret, deployment or publication occurs in this workflow. + +The runner was Ubuntu `24.04.5`, image `20261012.334.1`, runner `2.338.0`. +Tests used verified Node `22.23.3`. GitHub warned that the pinned checkout and +setup-node actions declare Node20 and forced their action runtime to Node24; +this did not change the verified test runtime. Any action-pin update needs its +own review. The decoded job log is retained in Cloud; its materialized file +SHA-256 is `05f1f154bc0b4ff11bd60c0c4527dbf32810c89f2a55f7d9cc76ad6dbbe799b7`. + +The first hosted attempt +[39732600913](https://github.com/KeeperCommunity/bitcoin-keeper-relay/actions/runs/39732600913) +failed before dependencies/tests: the new control-cleanliness check returned128 +because the sparse control checkout omitted unchanged ignore metadata. The +follow-up retains `.gitignore` and adds bounded Git failure categories. No +source guard was relaxed. The unrelated uploader was prevented from starting +for this CI-only branch; its two runs were skipped, and no active job was +canceled. Nine actual local verifier outcomes passed, including rejection of +an unexpected committed runtime path anywhere since the fixed control base. + +The 43-case proof covers genuine encrypted Realm, production authority, +account/session isolation, queue and mutation receipts, all seven operations, +complete readback, recovery and retained legacy selection, tombstone/recreation, +CAS conflicts, delayed/lost acknowledgements and native-state interruptions. +The additional 5.1 witness cold-rehydrates legacy saved consent, verifies +never-enrolled read/delete behavior, loses an enrollment response, explicitly +repairs, disables backup, deletes only the protected copy and explicitly +recreates it while preserving local and legacy recovery contents. + +## Remaining acceptance + +These host integration proofs are not native iOS/Android feature acceptance or +original-phone confirmation. Preserve the public-v2 matrix's inherited +2 PASS / 293 FAIL separately; no test or historical pin was weakened to obtain +these results. Rerun the fixed-pair job on the eventual combined candidate, +including verified 5.2 work when available. The passes above cover only the +reported exact app source. Hosted iOS/Android feature acceptance, genuine +physical-device results, independent security review and release approval remain separate requirements. From f73e3079b26ad0fab0e51216973207fb6e6c6445 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Thu, 22 Oct 2026 06:26:42 +0000 Subject: [PATCH 237/254] test(r5): reconcile offline release preparation CI policy --- .github/tests/native-draft-policy.test.cjs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/.github/tests/native-draft-policy.test.cjs b/.github/tests/native-draft-policy.test.cjs index 0246de416e..e91cdfa123 100644 --- a/.github/tests/native-draft-policy.test.cjs +++ b/.github/tests/native-draft-policy.test.cjs @@ -116,7 +116,8 @@ test('draft opening and updates retain the expected host checks without a releas triggered(workflow, 'pull_request', action, 'sprint')).map(([filename]) => filename).sort(); const expected = [ 'android-diagnostic-build.yml', 'codeql.yml', 'keeper-hosted-native.yml', - 'keeper-release-acceptance.yml', 'keeper-validation-tests.yml', 'public-backend.yml', 'test.yml', + 'keeper-release-acceptance.yml', 'keeper-release-preparation-contracts.yml', + 'keeper-validation-tests.yml', 'public-backend.yml', 'test.yml', ]; if (action === 'synchronize' || action === 'reopened') expected.push('keeper-native-diagnostic.yml'); assert.deepEqual(triggeredFiles, expected.sort()); @@ -124,6 +125,22 @@ test('draft opening and updates retain the expected host checks without a releas } }); +test('release preparation PR checks stay offline and separate from the protected manual plan', () => { + const contracts = workflows['keeper-release-preparation-contracts.yml']; + assert.deepEqual(contracts.permissions, { contents: 'read' }); + assert.equal(contracts.on.pull_request_target, undefined); + assert.equal(contracts.concurrency['cancel-in-progress'], 'false'); + const job = contracts.jobs.contracts; + assert.equal(job.environment, undefined); + assert.equal(job.secrets, undefined); + assert.equal(job.steps.filter(step => step.run).length, 1); + assert.match(job.steps.find(step => step.run).run, /node --test scripts\/protected-release-plan\.test\.cjs/); + const plan = workflows['keeper-protected-release-plan.yml']; + assert.deepEqual(Object.keys(plan.on), ['workflow_dispatch']); + assert.equal(triggered(plan, 'pull_request', 'opened', 'sprint'), false); + assert.equal(triggered(plan, 'push', null, 'sprint'), false); +}); + test('all Tests installs preserve current immutable script-disabled R4 setup and patches', () => { const steps = ci.jobs.test.steps; const install = steps.find(step => step.name === 'Install dependencies'); From 082d616d3646ead096c00d6889ae57c677e1fbf0 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Thu, 22 Oct 2026 06:28:29 +0000 Subject: [PATCH 238/254] ci: reject boolean fields in native feature measurement metadata --- .github/native/hosted_native.py | 4 ++-- .github/tests/test_feature_runner.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/native/hosted_native.py b/.github/native/hosted_native.py index 855e4789ad..f1986e4e0a 100644 --- a/.github/native/hosted_native.py +++ b/.github/native/hosted_native.py @@ -801,7 +801,7 @@ def validate_r5_feature_evidence(raw): def invalid_constant(value): raise ValueError('Non-finite feature measurement.') payload = json.loads(raw, parse_constant=invalid_constant) - if (payload.get('schema') != 1 or payload.get('scope') != 'production-worker-native-storage' + if (type(payload.get('schema')) is not int or payload.get('schema') != 1 or payload.get('scope') != 'production-worker-native-storage' or payload.get('status') != 'PASS' or payload.get('stage') != 'pass' or payload.get('scenarios') != [{'id': item, 'status': 'PASS'} for item in R5_SCENARIOS] or payload.get('limits') != R5_LIMITS): @@ -810,7 +810,7 @@ def invalid_constant(value): if not isinstance(measurements, list) or len(measurements) != 5: raise ValueError('Five raw helper measurements are required.') for iteration, row in enumerate(measurements, 1): - if not isinstance(row, dict) or set(row) != {'iteration', 'probe_ready_ms', 'setup_first_wallet_ms'} or row['iteration'] != iteration: + if not isinstance(row, dict) or set(row) != {'iteration', 'probe_ready_ms', 'setup_first_wallet_ms'} or type(row['iteration']) is not int or row['iteration'] != iteration: raise ValueError('Invalid feature measurement shape.') for field in ['probe_ready_ms', 'setup_first_wallet_ms']: value = row[field] diff --git a/.github/tests/test_feature_runner.py b/.github/tests/test_feature_runner.py index 7aa7e040a4..5d6075d4bf 100644 --- a/.github/tests/test_feature_runner.py +++ b/.github/tests/test_feature_runner.py @@ -62,7 +62,7 @@ def test_failed_skipped_or_duplicate_scenario_is_rejected(self): native.validate_r5_feature_evidence(json.dumps(payload)) def test_measurements_must_be_raw_ordered_finite_bounded_numbers(self): - for field, value in [('iteration', 2), ('probe_ready_ms', float('nan')), ('probe_ready_ms', True), + for field, value in [('iteration', 2), ('iteration', True), ('probe_ready_ms', float('nan')), ('probe_ready_ms', True), ('probe_ready_ms', -1), ('setup_first_wallet_ms', 600001), ('setup_first_wallet_ms', '25')]: with self.subTest(field=field, value=value): From 0c8e4dc17fa98937209c34b61c316858ded73591 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Thu, 22 Oct 2026 06:29:18 +0000 Subject: [PATCH 239/254] test: validate recorded native module-mapping preparation overlay --- .github/native/product_measurements.py | 27 ++++++++++- .github/tests/test_product_measurements.py | 52 ++++++++++++++++++++++ docs/r5/FULL-PRODUCT-MEASUREMENTS.md | 7 +++ 3 files changed, 85 insertions(+), 1 deletion(-) diff --git a/.github/native/product_measurements.py b/.github/native/product_measurements.py index 197769970b..4d64bf57bb 100644 --- a/.github/native/product_measurements.py +++ b/.github/native/product_measurements.py @@ -6,6 +6,7 @@ createNewApp transport fixture, NEVER evidence of Relay compatibility. """ import importlib.util +import hashlib import json import os from pathlib import Path @@ -113,6 +114,27 @@ class Server(ThreadingHTTPServer): return Server(('127.0.0.1', port), Handler) +def verify_package_overlay(original_bytes, installed_bytes, overlay): + """Accept only the foundation's exact recorded module-mapping overlay.""" + allowed = ['browser', 'react-native', 'browserify'] + if (not isinstance(overlay, dict) or overlay.get('path') != 'package.json' or + overlay.get('allowed_fields') != allowed or + overlay.get('before_sha256') != hashlib.sha256(original_bytes).hexdigest() or + overlay.get('after_sha256') != hashlib.sha256(installed_bytes).hexdigest()): + raise RuntimeError('PRODUCT_UI_PACKAGE_OVERLAY_UNRECORDED') + try: + original, installed = json.loads(original_bytes), json.loads(installed_bytes) + if not isinstance(original, dict) or not isinstance(installed, dict): + raise ValueError('Expected manifest objects') + for field in allowed: + original.pop(field, None) + installed.pop(field, None) + if original != installed: + raise ValueError('Protected manifest fields changed') + except (ValueError, TypeError, UnicodeError): + raise RuntimeError('PRODUCT_UI_PACKAGE_PROTECTED_FIELDS_CHANGED') from None + + def run(h): if (os.environ.get('GITHUB_ACTIONS') != 'true' or os.environ.get('RUNNER_ENVIRONMENT') != 'github-hosted'): @@ -125,10 +147,13 @@ def run(h): raise RuntimeError('PRODUCT_UI_EXACT_BUILD_REQUIRED') source_paths = ['.github/native', 'src', 'index.js', 'App.tsx', 'App.js', 'shim.js', 'AndroidFontFix.js', 'metro.config.js', 'babel.config.js', 'app.json', - 'react-native.config.js', 'tsconfig.json', 'package.json', 'yarn.lock'] + 'react-native.config.js', 'tsconfig.json', 'yarn.lock'] if subprocess.run(['git', 'diff', '--quiet', 'HEAD', '--', *source_paths], cwd=h.ROOT, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).returncode: raise RuntimeError('PRODUCT_UI_COMMITTED_SOURCE_REQUIRED') + verify_package_overlay( + subprocess.check_output(['git', 'show', 'HEAD:package.json'], cwd=h.ROOT), + (h.ROOT / 'package.json').read_bytes(), data.get('nodeify_overlay')) expected_environment = { 'ENVIRONMENT': 'DEVELOPMENT', 'RELAY': 'http://127.0.0.1:8765/', 'SIGNING_SERVER_TESTNET': 'https://127.0.0.1:9/', diff --git a/.github/tests/test_product_measurements.py b/.github/tests/test_product_measurements.py index 0401d13ddb..010474e1ce 100644 --- a/.github/tests/test_product_measurements.py +++ b/.github/tests/test_product_measurements.py @@ -1,5 +1,6 @@ """HTTP controls and fail-closed measurement receipts; these are not native tests.""" import importlib.util +import hashlib import json from pathlib import Path import threading @@ -96,5 +97,56 @@ def test_reset_discards_prior_sample_receipt(self): self.assertEqual(state['completed_stages'], []) +class PackageOverlayTests(unittest.TestCase): + def setUp(self): + self.original = {'name': 'fixture', 'dependencies': {'crypto-shim': '1.0.0'}, + 'scripts': {'start': 'react-native start'}, 'browser': {'path': 'before'}} + self.installed = {**self.original, 'browser': {'path': 'react-native-path'}, + 'react-native': {'crypto': 'crypto-shim'}, 'browserify': {'transform': []}} + + def evidence(self, original=None, installed=None): + original_bytes = json.dumps(self.original if original is None else original).encode() + installed_bytes = json.dumps(self.installed if installed is None else installed).encode() + return original_bytes, installed_bytes, { + 'path': 'package.json', 'allowed_fields': ['browser', 'react-native', 'browserify'], + 'before_sha256': hashlib.sha256(original_bytes).hexdigest(), + 'after_sha256': hashlib.sha256(installed_bytes).hexdigest(), + } + + def test_recorded_mapping_overlay_is_accepted(self): + product.verify_package_overlay(*self.evidence()) + + def test_recorded_unchanged_mapping_is_accepted(self): + product.verify_package_overlay(*self.evidence(installed=self.original)) + + def test_unrecorded_changes_are_rejected(self): + original, installed, overlay = self.evidence() + for missing in (None, {}, {**overlay, 'after_sha256': '0' * 64}): + with self.assertRaisesRegex(RuntimeError, 'UNRECORDED'): + product.verify_package_overlay(original, installed, missing) + + def test_recording_must_match_committed_original_bytes(self): + original, installed, overlay = self.evidence() + with self.assertRaisesRegex(RuntimeError, 'UNRECORDED'): + product.verify_package_overlay(original + b' ', installed, overlay) + + def test_hash_matching_protected_dependency_change_is_rejected(self): + installed = {**self.installed, 'dependencies': {'crypto-shim': '2.0.0'}} + with self.assertRaisesRegex(RuntimeError, 'PROTECTED_FIELDS_CHANGED'): + product.verify_package_overlay(*self.evidence(installed=installed)) + + def test_hash_matching_script_change_is_rejected(self): + installed = {**self.installed, 'scripts': {'start': 'different-entrypoint'}} + with self.assertRaisesRegex(RuntimeError, 'PROTECTED_FIELDS_CHANGED'): + product.verify_package_overlay(*self.evidence(installed=installed)) + + def test_expanded_field_allowlist_and_wrong_path_are_rejected(self): + original, installed, overlay = self.evidence() + for changed in ({**overlay, 'allowed_fields': overlay['allowed_fields'] + ['dependencies']}, + {**overlay, 'path': 'another-package.json'}): + with self.assertRaisesRegex(RuntimeError, 'UNRECORDED'): + product.verify_package_overlay(original, installed, changed) + + if __name__ == '__main__': unittest.main() diff --git a/docs/r5/FULL-PRODUCT-MEASUREMENTS.md b/docs/r5/FULL-PRODUCT-MEASUREMENTS.md index 4e99b82a32..71d915108a 100644 --- a/docs/r5/FULL-PRODUCT-MEASUREMENTS.md +++ b/docs/r5/FULL-PRODUCT-MEASUREMENTS.md @@ -37,6 +37,13 @@ Metro and the fixture ports. It removes only its new iOS simulator or disposable development app and its own temporary measurement files. Existing personal devices, simulator profiles, other worktrees, caches and evidence are untouched. +Production source and bundle configuration must match committed HEAD. The sole +manifest exception is the foundation's recorded rn-nodeify module-mapping overlay: +its before hash must match `git show HEAD:package.json`, its after hash must match +current bytes, its allowed fields must be exactly browser/react-native/browserify, +and all other parsed manifest fields must remain equal. Unrecorded mapping changes, +dependency/script changes and expanded allowlists fail closed. + The setup responder binds only127.0.0.1:8765, answers only `createNewApp` with the controlled `{created:true}` response, discards request bodies without logging or retaining identifiers, and rejects every other backend operation. A separate From 17baf3bff3a5cd96787218b9e8e1fee75f549967 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Thu, 22 Oct 2026 06:29:43 +0000 Subject: [PATCH 240/254] ci: anchor worker evidence to committed source and scope install claims --- .github/native/hosted_native.py | 14 +++++++++++--- .github/tests/test_feature_runner.py | 13 +++++++++++++ docs/r5-cloud-native-reconciliation.md | 2 ++ 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/.github/native/hosted_native.py b/.github/native/hosted_native.py index f1986e4e0a..40a986f8d2 100644 --- a/.github/native/hosted_native.py +++ b/.github/native/hosted_native.py @@ -817,8 +817,12 @@ def invalid_constant(value): if type(value) not in [int, float] or not math.isfinite(value) or not 0 <= value <= 600000: raise ValueError('Invalid bounded feature measurement.') sources = payload.get('source_functions') - expected = [{'path': path, 'sha256': hashlib.sha256((ROOT / path).read_bytes()).hexdigest(), 'names': names} - for path, names in R5_SOURCES.items()] + expected = [] + for path, names in R5_SOURCES.items(): + committed = subprocess.check_output(['git', 'show', 'HEAD:' + path], cwd=ROOT) + if (ROOT / path).read_bytes() != committed: + raise ValueError('Feature production source differs from committed candidate.') + expected.append({'path': path, 'sha256': hashlib.sha256(committed).hexdigest(), 'names': names}) if sources != expected: raise ValueError('Feature workers do not match exact-candidate production source.') # Export only fixed scope/status, public source digests and bounded numbers. @@ -857,6 +861,8 @@ def smoke(suite='startup'): if artifact_identity() != read()['built_artifact']: raise RuntimeError('Built artifact changed before the disposable startup smoke.') if is_features: + command(['git', 'diff', '--exit-code', 'HEAD', '--', *R5_SOURCES], + 'Verify committed R5 worker source before extraction', error_details=False) command(['node', '.github/native/r5_prepare.cjs'], 'Prepare exact-source R5 production worker probe', timeout=60, error_details=False) report = PRIVATE / ('features-junit.xml' if is_features else 'probe-junit.xml' if is_probe else 'smoke-junit.xml') report.unlink(missing_ok=True) @@ -910,7 +916,9 @@ def smoke(suite='startup'): 'Record Android emulator image', capture=True) data = read() data['device'] = {'id': device, 'image': descriptor, 'fresh_disposable_state': True} - data['installed_artifact_verified'] = True + data['install_succeeded'] = True + data['installed_artifact_verified'] = False + data['installed_artifact_scope'] = 'Installed from unchanged candidate build path; installed bytes not independently hashed.' save(data) command([data['maestro']['binary'], '--device', device, 'test', '--env', 'APP_ID=' + APP_IDS[PLATFORM], '--flatten-debug-output', '--debug-output', str(PRIVATE / 'maestro-output'), diff --git a/.github/tests/test_feature_runner.py b/.github/tests/test_feature_runner.py index 5d6075d4bf..7bba343d48 100644 --- a/.github/tests/test_feature_runner.py +++ b/.github/tests/test_feature_runner.py @@ -22,6 +22,11 @@ def setUp(self): file = self.root / path file.parent.mkdir(parents=True, exist_ok=True) file.write_text('public synthetic current source') + committed_sources = {path: (self.root / path).read_bytes() for path in native.R5_SOURCES} + committed_read = mock.patch.object(native.subprocess, 'check_output', + side_effect=lambda args, **kwargs: committed_sources[args[-1].removeprefix('HEAD:')]) + committed_read.start() + self.addCleanup(committed_read.stop) patch = mock.patch.multiple(native, ROOT=self.root, EVIDENCE=self.root / 'metadata.json') patch.start() self.addCleanup(patch.stop) @@ -102,3 +107,11 @@ def test_feature_runtime_requires_successful_product_startup_before_extraction(s native.run_phase('features') process.assert_not_called() self.assertEqual(native.read()['r5_features'], 'FAIL') + + def test_mutated_working_source_and_matching_probe_hash_cannot_impersonate_head(self): + path = self.payload['source_functions'][0]['path'] + changed = b'altered public source after build' + (self.root / path).write_bytes(changed) + self.payload['source_functions'][0]['sha256'] = hashlib.sha256(changed).hexdigest() + with self.assertRaisesRegex(ValueError, 'committed candidate'): + native.validate_r5_feature_evidence(json.dumps(self.payload)) diff --git a/docs/r5-cloud-native-reconciliation.md b/docs/r5-cloud-native-reconciliation.md index 8c4bf722ef..5046db9448 100644 --- a/docs/r5-cloud-native-reconciliation.md +++ b/docs/r5-cloud-native-reconciliation.md @@ -46,3 +46,5 @@ Worker evidence requires all five fixed scenarios, actual native persistence acr Runner disk preflight records available bytes and a 12 GiB minimum reserve before dependency preparation and native compilation; insufficient reserve fails without deleting another task's caches or data. iOS and Android job ceilings are 105 and 90 minutes respectively, with no automatic retry. These are upper bounds, not observed runtime or cost. Actual combined native execution remains NOT RUN here. The inherited infrastructure adoption is approximately 53.6k changed code/test tokens by the characters/4 estimate, with the new reconciliation delta approximately 6.7k before these hooks. The 19k estimate belongs to the original #7062 delta alone. Keep original source review units and the new reconciliation-only comparison separate; do not describe the aggregate as one sub-20k package. + +Installed-artifact scope: successful install consumes the unchanged candidate build path. Metadata reports install_succeeded and explicitly leaves installed_artifact_verified false because installed bytes have not been independently hashed. Built artifact hashes and exact production source checks remain required; do not describe this as installed-byte hash verification. From 5457cb08194495ed35767db92110727ce54ef9a1 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Thu, 22 Oct 2026 06:35:23 +0000 Subject: [PATCH 241/254] test: visit native Version History and key-management entry after relaunch --- .github/native/product_first_wallet.yaml | 73 ++++++++++++++++++++++ .github/native/product_measurements.py | 31 ++++++++- .github/tests/test_product_measurements.py | 23 +++++++ docs/r5/FULL-PRODUCT-MEASUREMENTS.md | 11 ++++ 4 files changed, 135 insertions(+), 3 deletions(-) diff --git a/.github/native/product_first_wallet.yaml b/.github/native/product_first_wallet.yaml index b8c0d1ed7e..42db8ec816 100644 --- a/.github/native/product_first_wallet.yaml +++ b/.github/native/product_first_wallet.yaml @@ -115,6 +115,79 @@ name: Full app startup first wallet and cold persistence id: home_visible_wallet_count_1 timeout: 30000 - assertVisible: Mobile Wallet +- tapOn: + id: home_tab_more +- scrollUntilVisible: + element: + id: btn_setting_App Settings + direction: DOWN + timeout: 30000 +- tapOn: + id: btn_setting_App Settings +- extendedWaitUntil: + visible: + id: btn_setting_Version History + timeout: 30000 +- tapOn: + id: btn_setting_Version History +- extendedWaitUntil: + visible: + id: text_installedAppVersionLabel + text: Installed app version + timeout: 30000 +- assertVisible: + id: text_installedAppVersion + text: ${EXPECTED_INSTALLED_REGEX} +- assertVisible: + id: view_VersionHistoryList +- assertVisible: Initially installed +- assertVisible: + text: ${EXPECTED_HISTORY_REGEX} +- runScript: + file: product_metric.js + env: + STAGE: version_history_verified +- tapOn: + id: btn_back +- extendedWaitUntil: + visible: + id: btn_setting_Version History + timeout: 30000 +- tapOn: + id: btn_back +- tapOn: + id: home_tab_keys +- extendedWaitUntil: + visible: + id: manage_keys_actions + timeout: 30000 +- tapOn: + id: manage_keys_actions +- extendedWaitUntil: + visible: + id: header_title + text: Manage Keys + timeout: 30000 +# The only fresh signer is Recovery Key, excluded by DeleteKeys' production +# filters. Check safe discovery/back navigation; never create a deletion intent. +- assertNotVisible: Delete +- tapOn: + id: btn_back +- extendedWaitUntil: + visible: + id: manage_keys_actions + timeout: 30000 +- runScript: + file: product_metric.js + env: + STAGE: key_management_entry_verified +- tapOn: + id: home_tab_wallets +- extendedWaitUntil: + visible: + id: home_visible_wallet_count_1 + timeout: 30000 +- assertVisible: Mobile Wallet - runScript: file: product_metric.js env: diff --git a/.github/native/product_measurements.py b/.github/native/product_measurements.py index 4d64bf57bb..b15bdfc10e 100644 --- a/.github/native/product_measurements.py +++ b/.github/native/product_measurements.py @@ -10,6 +10,7 @@ import json import os from pathlib import Path +import re import shutil import signal import subprocess @@ -21,7 +22,7 @@ STAGES = ('startup_start', 'startup_ready', 'wallet_start', 'wallet_ready', 'home_ready', 'relaunch_start', 'relaunch_login', 'relaunch_home', - 'navigation_verified') + 'version_history_verified', 'key_management_entry_verified', 'navigation_verified') PORT = 8765 SAMPLE_COUNT = 5 @@ -63,6 +64,8 @@ def snapshot(self, passed=False): 'create_new_app_calls': self.create_calls, 'other_backend_calls_rejected': self.blocked_calls, 'visible_wallet_count_before_relaunch': 1 if 'home_ready' in stamps else None, 'visible_wallet_count_after_relaunch': 1 if 'relaunch_home' in stamps else None, + 'installed_version_header_and_initial_history': 'PASS' if 'version_history_verified' in stamps else 'NOT RUN', + 'key_management_entry_and_exit': 'PASS' if 'key_management_entry_verified' in stamps else 'NOT RUN', 'more_wallets_navigation': 'PASS' if 'navigation_verified' in stamps else 'NOT RUN'} @@ -135,6 +138,20 @@ def verify_package_overlay(original_bytes, installed_bytes, overlay): raise RuntimeError('PRODUCT_UI_PACKAGE_PROTECTED_FIELDS_CHANGED') from None +def version_expectation(manifest, platform): + field = {'IOS': 'iosBuildNumber', 'ANDROID': 'androidVersionCode'}.get(platform) + version = manifest.get('version') + build = manifest.get(field) + if (field is None or not isinstance(version, str) or + not re.fullmatch(r'[0-9]+\.[0-9]+\.[0-9]+', version) or + type(build) is not int or not 0 < build <= 2100000000): + raise RuntimeError('PRODUCT_UI_SOURCE_VERSION_INVALID') + escaped = version.replace('.', r'\.') + return {'version': version, 'build': build, + 'installed_regex': '^' + escaped + r' \(' + str(build) + r'\)$', + 'history_regex': '^' + escaped + r'\(' + str(build) + r'\)$'} + + def run(h): if (os.environ.get('GITHUB_ACTIONS') != 'true' or os.environ.get('RUNNER_ENVIRONMENT') != 'github-hosted'): @@ -147,13 +164,14 @@ def run(h): raise RuntimeError('PRODUCT_UI_EXACT_BUILD_REQUIRED') source_paths = ['.github/native', 'src', 'index.js', 'App.tsx', 'App.js', 'shim.js', 'AndroidFontFix.js', 'metro.config.js', 'babel.config.js', 'app.json', - 'react-native.config.js', 'tsconfig.json', 'yarn.lock'] + 'react-native.config.js', 'tsconfig.json', 'yarn.lock', 'release/version.json'] if subprocess.run(['git', 'diff', '--quiet', 'HEAD', '--', *source_paths], cwd=h.ROOT, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).returncode: raise RuntimeError('PRODUCT_UI_COMMITTED_SOURCE_REQUIRED') verify_package_overlay( subprocess.check_output(['git', 'show', 'HEAD:package.json'], cwd=h.ROOT), (h.ROOT / 'package.json').read_bytes(), data.get('nodeify_overlay')) + expected_version = version_expectation(json.loads((h.ROOT / 'release/version.json').read_text()), h.PLATFORM) expected_environment = { 'ENVIRONMENT': 'DEVELOPMENT', 'RELAY': 'http://127.0.0.1:8765/', 'SIGNING_SERVER_TESTNET': 'https://127.0.0.1:9/', @@ -174,6 +192,10 @@ def run(h): # caches, another worktree, an existing iOS simulator or a real phone. result = {'status': 'RUNNING', 'samples': [], 'requested_samples': SAMPLE_COUNT, 'entrypoint': 'index.js', 'network': 'TESTNET', 'backend': 'CONTROLLED_LOOPBACK_CREATE_ONLY', + 'expected_installed_version': expected_version['version'], + 'expected_installed_build': expected_version['build'], + 'version_history_scope': 'Installed native tuple and retained Initially installed row after cold login. Empty/stale history observation insertion and migration not exercised.', + 'key_management_scope': 'Discoverable Keys→Manage Keys screen entry and back navigation only; no eligible disposable signer, deletion confirmation or seed view.', 'backend_compatibility': 'NOT TESTED', 'physical_devices': 'NOT RUN', 'performance_scope': 'Development app with warm Metro bundle; cold process/UI timing includes Maestro driver overhead. Not release-build performance.', 'count_scope': 'Actual rendered visible wallet list; disposable one-wallet account, not hidden/archived wallet counts.', @@ -244,7 +266,10 @@ def save(): h.command(['adb', '-s', device, 'shell', 'pm', 'clear', app], 'Clear only disposable development app') try: h.command([data['maestro']['binary'], '--device', device, 'test', - '--env', 'APP_ID=' + app, '--flatten-debug-output', + '--env', 'APP_ID=' + app, + '--env', 'EXPECTED_INSTALLED_REGEX=' + expected_version['installed_regex'], + '--env', 'EXPECTED_HISTORY_REGEX=' + expected_version['history_regex'], + '--flatten-debug-output', '--debug-output', str(output), '--test-output-dir', str(output), '--format', 'junit', '--output', str(report), '.github/native/product_first_wallet.yaml'], diff --git a/.github/tests/test_product_measurements.py b/.github/tests/test_product_measurements.py index 010474e1ce..182a76fdfc 100644 --- a/.github/tests/test_product_measurements.py +++ b/.github/tests/test_product_measurements.py @@ -148,5 +148,28 @@ def test_expanded_field_allowlist_and_wrong_path_are_rejected(self): product.verify_package_overlay(original, installed, changed) +class VersionExpectationTests(unittest.TestCase): + def test_installed_and_history_match_their_actual_native_format(self): + manifest = {'version': '2.6.4', 'androidVersionCode': 627, 'iosBuildNumber': 620} + for platform, build in [('ANDROID', 627), ('IOS', 620)]: + expected = product.version_expectation(manifest, platform) + self.assertRegex('2.6.4 (%d)' % build, expected['installed_regex']) + self.assertRegex('2.6.4(%d)' % build, expected['history_regex']) + self.assertNotRegex('2x6x4 (%d)' % build, expected['installed_regex']) + + def test_new_source_versions_do_not_reuse_previous_expected_tuple(self): + expected = product.version_expectation({'version': '2.7.0', 'iosBuildNumber': 701}, 'IOS') + self.assertRegex('2.7.0 (701)', expected['installed_regex']) + self.assertNotRegex('2.6.4 (620)', expected['installed_regex']) + + def test_work_package_labels_or_missing_invalid_builds_are_rejected(self): + for manifest in ({'version': '5.1', 'iosBuildNumber': 620}, + {'version': '2.6.4'}, + {'version': '2.6.4', 'iosBuildNumber': True}, + {'version': '2.6.4', 'iosBuildNumber': -1}): + with self.assertRaises(RuntimeError): + product.version_expectation(manifest, 'IOS') + + if __name__ == '__main__': unittest.main() diff --git a/docs/r5/FULL-PRODUCT-MEASUREMENTS.md b/docs/r5/FULL-PRODUCT-MEASUREMENTS.md index 71d915108a..a8ec013c0d 100644 --- a/docs/r5/FULL-PRODUCT-MEASUREMENTS.md +++ b/docs/r5/FULL-PRODUCT-MEASUREMENTS.md @@ -65,6 +65,17 @@ Each of five samples clears only the disposable app and its credentials, then: 5. Exercises More→Wallets and requires the one-wallet list again. This is a bounded fresh-state navigation check; it does not reproduce #7055's original backup-delete/multiuser trigger or close that investigation. +6. Opens More→App Settings→Version History and asserts the native Installed app + version header and the retained Initially installed row. Expected version/build + regexes come from the exact source's committed release/version.json (currently + Android2.6.4/627 and iOS2.6.4/620); no work-package label is an app version. + A fresh account already has that tuple's initial-install row, so cold login + preserves it and does not insert Current installation observed. Empty/stale + history observation insertion and migrations remain separate unproven cases. +7. Enters Keys→Manage Keys and exits with Back, then verifies the wallet list + again. The fresh Recovery Key is excluded from DeleteKeys' list; no deletion + intent, confirmation, recovery material or seed-word screen is opened. This + checks discoverability/return navigation only, not safe-deletion acceptance. Only metadata.json receives bounded raw durations/counts/stage labels. Five separate startup-to-passcode and StartNew-to-ready values are retained; no median From 14fe9c86a874ad15d636985474c3891685479fac Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Thu, 22 Oct 2026 06:35:43 +0000 Subject: [PATCH 242/254] docs(r5): enumerate app and non-app delivery checkpoints --- docs/r5-cloud-integration.md | 40 ++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/docs/r5-cloud-integration.md b/docs/r5-cloud-integration.md index 2785c0c9be..86583813cc 100644 --- a/docs/r5-cloud-integration.md +++ b/docs/r5-cloud-integration.md @@ -77,3 +77,43 @@ command failures; green jobs do not mean lint is clean. At initial integration there is no combined native acceptance, no five raw measurements and no identified 5.2 checkpoint. Candidate freeze is blocked until the authoritative 5.2 handoff is rechecked and all package results are recorded. + +## Separately reviewable delivery packages + +These are implementation checkpoints included or tracked by this integration, +not transferred acceptance results. Current exact-head results belong in this +PR's handoff and the referenced immutable workflow receipts. Do not commit a +post-test evidence update and silently transfer results to the resulting SHA. + +| Package | Review / implementation checkpoint | Status and next action | +| --- | --- | --- | +| Core deletion/startup | #7058 / `77b72da29ac5f2cdf3f51c9a0e9f3fd1c2af7238` | Included; complete combined native feature acceptance | +| 5.1 backup and Version History | #7067 / `c1c4b803039b61b940373c4dc9d08473cf25e042` | Included; current-v3 genuine witness passed on earlier exact app checkpoint, rerun combined | +| 5.2 | Owner-reported running, authoritative task/branch/PR unavailable | Running dependency; obtain checkpoint, inspect overlaps, integrate verified commits, rerun affected checks before freeze | +| Cloud reconciliation | #7070 / `17baf3bff3a5cd96787218b9e8e1fee75f549967`; inputs #7060/#7063/#7062 | Included with original patch review links; installed-base trust and required branch protections remain separate from diagnostic execution | +| Native production-worker witness | #7071 / `486810052f90550d55dd6ddd541ff53ebd875caa` | Included; actual Realm/MMKV worker scenarios, explicitly controlled generation/transport/dispatch; execute on exact combined source | +| Full-product UI measurements | #7073 / `0c8e4dc17fa98937209c34b61c316858ded73591` and reviewed follow-ups | Included; require all five numeric samples, actual normal app, cold relaunch/counts/navigation; no helper timing substitution | +| Confirmed biometric callback defects | #7069 / `257fbe17e1a85d0e5d7e5d8ab97e1a69edbd9628`, review correction `14a2459b30e31ea3099d56f6c6928a4a3b7d6995` | Included; independent review reproduced prompt repetition and stale epoch cases; 17 component controls pass, actual biometrics remain unrun | +| Current protocol pairing | Private Relay #160 / `4db6a758a307111137ad6bb4f35a32fa15b17a9d` | 52 backend + 43 protected app + 1 upgraded-account + 3 guard cases pass at their exact documented pair; pin/retest combined app SHA | +| Protected release preparation | #7072 / `f2fc0cb557f0e5303335dadc9e98152640795e6c` | Included; authorization/inventory preparation implemented, actual signing/distribution not verified; separate bounded worker preparation and protected setup remain | +| PHP exposure research | #7072, `docs/r5-cloud-release-readiness.md` | Official advisories/source assessment documented; deployed runtime inventory unknown, no Keeper exposure assertion; remedies stay R6 | +| Navigation/timezone/PIN/copy | `docs/r5/BIOMETRIC-LIFECYCLE.md` and source tests | No speculative #7055/#6991 fix; bounded native reproduction/device checks remain. Retain shipped PIN and scoped EN/ES corrections | +| Read-only timelock assessor | Separately recorded candidate, not identified as 5.2 | Source/handoff and complete key/origin/script/intent binding and native isolation acceptance remain; no activation or wallet clearance | +| Independent/Claude review | Source review receipts on package PRs | Independent agent reviews performed; human/security and applicable Claude review not established; no paid activation implied | +| Physical acceptance / release evidence | Exact artifact and authorized tester required | Not run; no physical deferral inferred. Version/build allocation, protected signing and distribution require the specified fresh evidence and explicit approvals | + +The infrastructure adoption is about **54k changed code/test characters/4** across +the existing packages, much larger than an older 19k estimate. The initial new +reconciliation was about 6.7k, with a small additional runner hook. Preserve the +original #7060/#7063/#7062 reviews and the reconciliation-only comparison; this +aggregate is an integration input, not a request to approve one new 54k patch. +Native worker (~7.8k), product measurements (~6k), biometric correction (~4k) +and release preparation (~7.2k code/test, ~12.7k including research/docs) stay +separate. Further signing workers are a separate estimated 5–10k preparation +package. The unknown 5.2 delta is not estimated by guessing. + +The first combined preflight exposed an expected-workflow allowlist mismatch +after adding the offline release-preparation contracts. It is retained as an +integration failure and corrected by explicitly admitting that read-only test +workflow and asserting that the protected release plan remains manual-only. +The corrected policy controls pass; no release gate or test was removed. From 7785c741be203cc348726293537238b54183861a Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Thu, 22 Oct 2026 06:37:06 +0000 Subject: [PATCH 243/254] docs(r5): pin the reviewed product acceptance package --- docs/r5-cloud-integration.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/r5-cloud-integration.md b/docs/r5-cloud-integration.md index 86583813cc..609533eb58 100644 --- a/docs/r5-cloud-integration.md +++ b/docs/r5-cloud-integration.md @@ -92,7 +92,7 @@ post-test evidence update and silently transfer results to the resulting SHA. | 5.2 | Owner-reported running, authoritative task/branch/PR unavailable | Running dependency; obtain checkpoint, inspect overlaps, integrate verified commits, rerun affected checks before freeze | | Cloud reconciliation | #7070 / `17baf3bff3a5cd96787218b9e8e1fee75f549967`; inputs #7060/#7063/#7062 | Included with original patch review links; installed-base trust and required branch protections remain separate from diagnostic execution | | Native production-worker witness | #7071 / `486810052f90550d55dd6ddd541ff53ebd875caa` | Included; actual Realm/MMKV worker scenarios, explicitly controlled generation/transport/dispatch; execute on exact combined source | -| Full-product UI measurements | #7073 / `0c8e4dc17fa98937209c34b61c316858ded73591` and reviewed follow-ups | Included; require all five numeric samples, actual normal app, cold relaunch/counts/navigation; no helper timing substitution | +| Full-product UI measurements | #7073 / `5457cb08194495ed35767db92110727ce54ef9a1` | Included; require all five numeric samples, actual normal app, cold relaunch/counts/navigation; no helper timing substitution | | Confirmed biometric callback defects | #7069 / `257fbe17e1a85d0e5d7e5d8ab97e1a69edbd9628`, review correction `14a2459b30e31ea3099d56f6c6928a4a3b7d6995` | Included; independent review reproduced prompt repetition and stale epoch cases; 17 component controls pass, actual biometrics remain unrun | | Current protocol pairing | Private Relay #160 / `4db6a758a307111137ad6bb4f35a32fa15b17a9d` | 52 backend + 43 protected app + 1 upgraded-account + 3 guard cases pass at their exact documented pair; pin/retest combined app SHA | | Protected release preparation | #7072 / `f2fc0cb557f0e5303335dadc9e98152640795e6c` | Included; authorization/inventory preparation implemented, actual signing/distribution not verified; separate bounded worker preparation and protected setup remain | @@ -107,7 +107,7 @@ the existing packages, much larger than an older 19k estimate. The initial new reconciliation was about 6.7k, with a small additional runner hook. Preserve the original #7060/#7063/#7062 reviews and the reconciliation-only comparison; this aggregate is an integration input, not a request to approve one new 54k patch. -Native worker (~7.8k), product measurements (~6k), biometric correction (~4k) +Native worker (~7.8k), product measurements (~8.1k), biometric correction (~4k) and release preparation (~7.2k code/test, ~12.7k including research/docs) stay separate. Further signing workers are a separate estimated 5–10k preparation package. The unknown 5.2 delta is not estimated by guessing. From aaf79f9a060be4b09a96e8f1eed82f770ca2a184 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Thu, 22 Oct 2026 06:42:56 +0000 Subject: [PATCH 244/254] ci(r5): install locked tools for standalone native contracts --- .../native/contract-tools/package-lock.json | 28 +++++++++++++++++++ .github/native/contract-tools/package.json | 8 ++++++ .github/tests/native-draft-policy.test.cjs | 15 ++++++++++ .github/workflows/keeper-validation-tests.yml | 9 +++++- docs/r5-cloud-integration.md | 7 +++++ 5 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 .github/native/contract-tools/package-lock.json create mode 100644 .github/native/contract-tools/package.json diff --git a/.github/native/contract-tools/package-lock.json b/.github/native/contract-tools/package-lock.json new file mode 100644 index 0000000000..255413586f --- /dev/null +++ b/.github/native/contract-tools/package-lock.json @@ -0,0 +1,28 @@ +{ + "name": "keeper-native-contract-tools", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "keeper-native-contract-tools", + "version": "1.0.0", + "dependencies": { + "typescript": "5.0.4" + } + }, + "node_modules/typescript": { + "version": "5.0.4", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.0.4.tgz", + "integrity": "sha512-cW9T5W9xY37cc+jfEnaUvX91foxtHkza3Nw3wkoF4sSlKn0MONdkdEndig/qPBWXNkmplh3NzayQzCiHM4/hqw==", + "license": "Apache-2.0", + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + }, + "engines": { + "node": ">=12.20" + } + } + } +} diff --git a/.github/native/contract-tools/package.json b/.github/native/contract-tools/package.json new file mode 100644 index 0000000000..215add3f49 --- /dev/null +++ b/.github/native/contract-tools/package.json @@ -0,0 +1,8 @@ +{ + "name": "keeper-native-contract-tools", + "version": "1.0.0", + "private": true, + "dependencies": { + "typescript": "5.0.4" + } +} diff --git a/.github/tests/native-draft-policy.test.cjs b/.github/tests/native-draft-policy.test.cjs index e91cdfa123..6cfc1cd83b 100644 --- a/.github/tests/native-draft-policy.test.cjs +++ b/.github/tests/native-draft-policy.test.cjs @@ -154,3 +154,18 @@ test('all Tests installs preserve current immutable script-disabled R4 setup and for (const step of steps) assert.ok(!step['continue-on-error']); assert.equal(steps.find(step => step.name === 'Run tests').run, 'npm run test'); }); + +test('standalone native contracts install a locked isolated compiler without app lifecycle hooks', () => { + const job = workflows['keeper-validation-tests.yml'].jobs.contracts; + const install = job.steps.find(step => step.name === 'Install isolated locked contract tools without lifecycle scripts'); + assert.match(install.run, /npm ci --prefix \.github\/native\/contract-tools --ignore-scripts --no-audit --no-fund/); + assert.match(install.run, /NODE_PATH=/); + assert.equal(job.environment, undefined); + assert.equal(job.secrets, undefined); + const compiler = require('../native/contract-tools/package.json'); + const app = require('../../package.json'); + assert.equal(compiler.dependencies.typescript, app.devDependencies.typescript); + const lock = require('../native/contract-tools/package-lock.json'); + assert.equal(lock.packages['node_modules/typescript'].version, app.devDependencies.typescript); + assert.match(lock.packages['node_modules/typescript'].integrity, /^sha512-/); +}); diff --git a/.github/workflows/keeper-validation-tests.yml b/.github/workflows/keeper-validation-tests.yml index 7bbc3b67da..834cfae086 100644 --- a/.github/workflows/keeper-validation-tests.yml +++ b/.github/workflows/keeper-validation-tests.yml @@ -21,6 +21,13 @@ jobs: persist-credentials: false - uses: actions/setup-node@249970729cb0ef3589644e2896645e5dc5ba9c38 # v6 with: - node-version: '22' + node-version: '22.23.3' + - name: Install isolated locked contract tools without lifecycle scripts + run: | + npm ci --prefix .github/native/contract-tools --ignore-scripts --no-audit --no-fund + echo "NODE_PATH=$GITHUB_WORKSPACE/.github/native/contract-tools/node_modules" >> "$GITHUB_ENV" + - name: Verify contract compiler matches the candidate + run: | + node -e "require('node:assert/strict').equal(require('typescript/package.json').version, require('./package.json').devDependencies.typescript)" - run: node --test .github/tests/*.test.cjs - run: python3 -B -m unittest discover -s .github/tests -p 'test_*.py' diff --git a/docs/r5-cloud-integration.md b/docs/r5-cloud-integration.md index 609533eb58..6a3def2979 100644 --- a/docs/r5-cloud-integration.md +++ b/docs/r5-cloud-integration.md @@ -117,3 +117,10 @@ after adding the offline release-preparation contracts. It is retained as an integration failure and corrected by explicitly admitting that read-only test workflow and asserting that the protected release plan remains manual-only. The corrected policy controls pass; no release gate or test was removed. + +The first hosted contract job on interim `7785c741be203cc348726293537238b54183861a` +(run39735104974) failed because the lightweight runner lacked TypeScript after +worker-extraction contracts were included. The correction installs only a locked, +script-disabled TypeScript5.0.4 test tool, matching the frozen app lock integrity, +and verifies that its version matches the candidate manifest. The failed receipt +remains preserved; this does not narrow or skip the worker tests. From 040955d0caca38bb3cb386ca2918adf60f0f30db Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Thu, 22 Oct 2026 06:57:36 +0000 Subject: [PATCH 245/254] test: exercise full-product setup disconnect and explicit Retry --- .github/native/product_first_wallet.yaml | 31 +------- .github/native/product_measurements.py | 71 +++++++++++++++--- .github/native/product_prepare_pin.yaml | 32 ++++++++ .github/native/product_retry.yaml | 87 ++++++++++++++++++++++ .github/tests/test_product_measurements.py | 46 ++++++++++++ docs/r5/FULL-PRODUCT-MEASUREMENTS.md | 20 ++++- 6 files changed, 245 insertions(+), 42 deletions(-) create mode 100644 .github/native/product_prepare_pin.yaml create mode 100644 .github/native/product_retry.yaml diff --git a/.github/native/product_first_wallet.yaml b/.github/native/product_first_wallet.yaml index 42db8ec816..d2f409f235 100644 --- a/.github/native/product_first_wallet.yaml +++ b/.github/native/product_first_wallet.yaml @@ -17,36 +17,7 @@ name: Full app startup first wallet and cold persistence file: product_metric.js env: STAGE: startup_ready -- repeat: - times: 4 - commands: - - tapOn: - id: key_1 -- assertVisible: Confirm your passcode -- repeat: - times: 4 - commands: - - tapOn: - id: key_1 -- tapOn: Create -- extendedWaitUntil: - visible: Continue - timeout: 30000 -- tapOn: Continue -- extendedWaitUntil: - visible: Cancel - timeout: 30000 -- tapOn: Cancel -- extendedWaitUntil: - visible: - id: btn_skip - timeout: 30000 -- tapOn: - id: btn_skip -- extendedWaitUntil: - visible: - id: view_startNewTile - timeout: 30000 +- runFlow: product_prepare_pin.yaml - runScript: file: product_metric.js env: diff --git a/.github/native/product_measurements.py b/.github/native/product_measurements.py index b15bdfc10e..acc43a5170 100644 --- a/.github/native/product_measurements.py +++ b/.github/native/product_measurements.py @@ -1,5 +1,5 @@ #!/usr/bin/env python3 -"""Five full-product UI samples on a task-owned device; metadata-only evidence. +"""Five full-product UI samples and one separate controlled Retry fixture. Requires the hosted_native.py foundation and its successful exact-source build. Normal Metro entrypoint is mandatory. The loopback responder is a controlled @@ -13,6 +13,7 @@ import re import shutil import signal +import socket import subprocess import tempfile import threading @@ -23,6 +24,8 @@ STAGES = ('startup_start', 'startup_ready', 'wallet_start', 'wallet_ready', 'home_ready', 'relaunch_start', 'relaunch_login', 'relaunch_home', 'version_history_verified', 'key_management_entry_verified', 'navigation_verified') +RETRY_STAGES = ('retry_fixture_start', 'retry_error_visible', 'retry_submit', 'retry_home_ready', + 'retry_relaunch_start', 'retry_relaunch_login', 'retry_relaunch_home') PORT = 8765 SAMPLE_COUNT = 5 @@ -33,20 +36,51 @@ def __init__(self): self.lock = threading.Lock() self.reset(1) - def reset(self, sample): + def reset(self, sample, retry=False): with self.lock: self.sample, self.times = sample, [] self.create_calls, self.blocked_calls = 0, 0 + self.retry, self.stages = retry, RETRY_STAGES if retry else STAGES + self.dropped, self.acknowledged, self.unexpected = 0, 0, 0 + + def create_attempt(self): + with self.lock: + self.create_calls += 1 + if self.retry and self.create_calls == 1: + self.dropped += 1 + return False + if self.retry and self.stages[len(self.times) - 1] != 'retry_submit': + self.unexpected += 1 + self.acknowledged += 1 + return True def mark(self, stage): with self.lock: - if len(self.times) >= len(STAGES) or stage != STAGES[len(self.times)]: + if len(self.times) >= len(self.stages) or stage != self.stages[len(self.times)]: + return False + if self.retry and stage in ('retry_error_visible', 'retry_submit') and ( + self.create_calls, self.dropped, self.acknowledged, self.unexpected) != (1, 1, 0, 0): return False self.times.append(time.monotonic_ns()) return True def snapshot(self, passed=False): with self.lock: + if self.retry: + if passed and (len(self.times) != len(RETRY_STAGES) or + (self.create_calls, self.dropped, self.acknowledged, self.unexpected) != (2, 1, 1, 0)): + raise RuntimeError('PRODUCT_UI_RETRY_INCOMPLETE_OR_UNEXPECTED_REQUEST') + stages = list(RETRY_STAGES[:len(self.times)]) + return {'sample': self.sample, 'status': 'PASS' if passed else 'FAIL', + 'scenario': 'CONTROLLED_DISCONNECT_BEFORE_COMMIT_NOT_WAN_OFFLINE', + 'completed_stages': stages, 'create_new_app_calls': self.create_calls, + 'transport_requests_dropped_without_commit': self.dropped, + 'acknowledged_create_responses': self.acknowledged, + 'unexpected_create_attempts': self.unexpected, + 'other_backend_calls_rejected': self.blocked_calls, + 'visible_wallet_count_before_relaunch': 1 if 'retry_home_ready' in stages else None, + 'visible_wallet_count_after_relaunch': 1 if 'retry_relaunch_home' in stages else None, + 'performance_sample': False} if passed and (len(self.times) != len(STAGES) or self.create_calls != 1): raise RuntimeError('PRODUCT_UI_INCOMPLETE_OR_DUPLICATE_SETUP') stamps = dict(zip(STAGES, self.times)) @@ -92,8 +126,16 @@ def do_POST(self): # Discard without parsing, logging, echoing or retaining them. raw = self.rfile.read(length) if self.path == '/createNewApp': - with receipt.lock: - receipt.create_calls += 1 + if not receipt.create_attempt(): + # Controlled transport failure only: no account storage + # or simulated server commit exists in this responder. + self.close_connection = True + try: + self.connection.shutdown(socket.SHUT_RDWR) + except OSError: + pass + self.connection.close() + return return self.reply(200, {'created': True}) if self.path == '/metric' and length <= 128: metric = json.loads(raw) @@ -191,6 +233,7 @@ def run(h): # Only app/test-specific data created here is removed. Never touch shared # caches, another worktree, an existing iOS simulator or a real phone. result = {'status': 'RUNNING', 'samples': [], 'requested_samples': SAMPLE_COUNT, + 'controlled_disconnect_retry': {'status': 'NOT RUN', 'performance_sample': False}, 'entrypoint': 'index.js', 'network': 'TESTNET', 'backend': 'CONTROLLED_LOOPBACK_CREATE_ONLY', 'expected_installed_version': expected_version['version'], 'expected_installed_build': expected_version['build'], @@ -249,8 +292,9 @@ def save(): ['adb', '-s', device, 'shell', 'getprop', 'ro.build.fingerprint'], 'Record actual product UI emulator image', capture=True)} app = h.APP_IDS[h.PLATFORM] - for sample in range(1, SAMPLE_COUNT + 1): - receipt.reset(sample) + for sample in range(1, SAMPLE_COUNT + 2): + retry_fixture = sample == SAMPLE_COUNT + 1 + receipt.reset(sample, retry=retry_fixture) output = private / ('sample-%d' % sample) output.mkdir() report = output / 'junit.xml' @@ -272,14 +316,23 @@ def save(): '--flatten-debug-output', '--debug-output', str(output), '--test-output-dir', str(output), '--format', 'junit', '--output', str(report), + '.github/native/product_retry.yaml' if retry_fixture else '.github/native/product_first_wallet.yaml'], + 'Full-product controlled disconnect and Retry fixture' if retry_fixture else 'Full-product first-wallet and relaunch UI sample', timeout=600, error_details=False) h.successful_junit(report) if h.artifact_identity() != data['built_artifact']: raise RuntimeError('PRODUCT_UI_ARTIFACT_CHANGED') - result['samples'].append(receipt.snapshot(passed=True)) + outcome = receipt.snapshot(passed=True) + if retry_fixture: + result['controlled_disconnect_retry'] = outcome + else: + result['samples'].append(outcome) except Exception: - result['samples'].append(receipt.snapshot()) + if retry_fixture: + result['controlled_disconnect_retry'] = receipt.snapshot() + else: + result['samples'].append(receipt.snapshot()) raise finally: # Maestro can capture automatic failure diagnostics. Keep no diff --git a/.github/native/product_prepare_pin.yaml b/.github/native/product_prepare_pin.yaml new file mode 100644 index 0000000000..87a527fb90 --- /dev/null +++ b/.github/native/product_prepare_pin.yaml @@ -0,0 +1,32 @@ +appId: ${APP_ID} +--- +- repeat: + times: 4 + commands: + - tapOn: + id: key_1 +- assertVisible: Confirm your passcode +- repeat: + times: 4 + commands: + - tapOn: + id: key_1 +- tapOn: Create +- extendedWaitUntil: + visible: Continue + timeout: 30000 +- tapOn: Continue +- extendedWaitUntil: + visible: Cancel + timeout: 30000 +- tapOn: Cancel +- extendedWaitUntil: + visible: + id: btn_skip + timeout: 30000 +- tapOn: + id: btn_skip +- extendedWaitUntil: + visible: + id: view_startNewTile + timeout: 30000 diff --git a/.github/native/product_retry.yaml b/.github/native/product_retry.yaml new file mode 100644 index 0000000000..cd2b5e235f --- /dev/null +++ b/.github/native/product_retry.yaml @@ -0,0 +1,87 @@ +# Sixth fresh fixture, outside all five performance samples. First HTTP body is +# read then disconnected before any server commit; only explicit UI Retry succeeds. +appId: ${APP_ID} +name: Full app controlled setup disconnect Retry and cold persistence +--- +- runScript: + file: product_metric.js + env: + STAGE: retry_fixture_start +- launchApp: + permissions: + all: deny +- extendedWaitUntil: + visible: Create a passcode + timeout: 180000 +- runFlow: product_prepare_pin.yaml +- tapOn: + id: view_startNewTile +- extendedWaitUntil: + visible: '^Something went wrong$' + timeout: 60000 +- assertVisible: '^Please check your internet connection and try again\.$' +- assertVisible: '^Retry$' +- assertNotVisible: + text: '^Something went wrong$' + index: 1 +- assertNotVisible: '^Next$' +- runScript: + file: product_metric.js + env: + STAGE: retry_error_visible +- runScript: + file: product_metric.js + env: + STAGE: retry_submit +- tapOn: '^Retry$' +- extendedWaitUntil: + visible: '^Next$' + timeout: 180000 +- assertNotVisible: '^Something went wrong$' +- tapOn: '^Next$' +- extendedWaitUntil: + visible: Protect Your Recovery Key + timeout: 30000 +- tapOn: Skip for Now +- extendedWaitUntil: + visible: Continue Without Backup? + timeout: 30000 +- tapOn: Skip Backup +- extendedWaitUntil: + visible: + id: home_visible_wallet_count_1 + timeout: 30000 +- assertVisible: Mobile Wallet +- runScript: + file: product_metric.js + env: + STAGE: retry_home_ready +- stopApp +- runScript: + file: product_metric.js + env: + STAGE: retry_relaunch_start +- launchApp +- extendedWaitUntil: + visible: Enter your passcode + timeout: 60000 +- runScript: + file: product_metric.js + env: + STAGE: retry_relaunch_login +- repeat: + times: 4 + commands: + - tapOn: + id: key_1 +- tapOn: Proceed +- extendedWaitUntil: + visible: + id: home_visible_wallet_count_1 + timeout: 60000 +- assertVisible: Mobile Wallet +- runScript: + file: product_metric.js + env: + STAGE: retry_relaunch_home +- stopApp diff --git a/.github/tests/test_product_measurements.py b/.github/tests/test_product_measurements.py index 182a76fdfc..f542fb502e 100644 --- a/.github/tests/test_product_measurements.py +++ b/.github/tests/test_product_measurements.py @@ -1,6 +1,7 @@ """HTTP controls and fail-closed measurement receipts; these are not native tests.""" import importlib.util import hashlib +import http.client import json from pathlib import Path import threading @@ -96,6 +97,51 @@ def test_reset_discards_prior_sample_receipt(self): self.assertEqual((state['sample'], state['create_new_app_calls']), (2, 0)) self.assertEqual(state['completed_stages'], []) + def start_retry_fixture(self): + self.receipt.reset(6, retry=True) + self.assertEqual(self.post('/metric', {'stage': 'retry_fixture_start'})[0], 200) + with self.assertRaises(http.client.RemoteDisconnected): + self.post('/createNewApp', {'publicId': 'DO_NOT_RETAIN'}) + + def finish_retry_fixture(self): + self.start_retry_fixture() + for stage in ('retry_error_visible', 'retry_submit'): + self.assertEqual(self.post('/metric', {'stage': stage})[0], 200) + self.assertEqual(self.post('/createNewApp', b'{}'), (200, {'created': True})) + for stage in product.RETRY_STAGES[3:]: + self.assertEqual(self.post('/metric', {'stage': stage})[0], 200) + + def test_real_disconnect_then_explicit_retry_receipt_is_separate_from_timings(self): + self.finish_retry_fixture() + result = self.receipt.snapshot(passed=True) + self.assertEqual(result['create_new_app_calls'], 2) + self.assertEqual(result['transport_requests_dropped_without_commit'], 1) + self.assertEqual(result['acknowledged_create_responses'], 1) + self.assertEqual(result['visible_wallet_count_after_relaunch'], 1) + self.assertFalse(result['performance_sample']) + self.assertNotIn('raw_durations', result) + self.assertNotIn('DO_NOT_RETAIN', json.dumps(result)) + + def test_automatic_hidden_retry_prevents_error_marker_and_acceptance(self): + self.start_retry_fixture() + self.post('/createNewApp', b'{}') # without explicit UI Retry marker + self.assertEqual(self.post('/metric', {'stage': 'retry_error_visible'})[0], 422) + self.assertEqual(self.receipt.snapshot()['unexpected_create_attempts'], 1) + with self.assertRaises(RuntimeError): + self.receipt.snapshot(passed=True) + + def test_extra_creation_after_relaunch_invalidates_retry_fixture(self): + self.finish_retry_fixture() + self.post('/createNewApp', b'{}') + with self.assertRaises(RuntimeError): + self.receipt.snapshot(passed=True) + + def test_ui_error_cannot_be_claimed_without_real_first_disconnect(self): + self.receipt.reset(6, retry=True) + self.post('/metric', {'stage': 'retry_fixture_start'}) + self.assertEqual(self.post('/metric', {'stage': 'retry_error_visible'})[0], 422) + self.assertEqual(self.receipt.snapshot()['transport_requests_dropped_without_commit'], 0) + class PackageOverlayTests(unittest.TestCase): def setUp(self): diff --git a/docs/r5/FULL-PRODUCT-MEASUREMENTS.md b/docs/r5/FULL-PRODUCT-MEASUREMENTS.md index a8ec013c0d..8c5bd8b50a 100644 --- a/docs/r5/FULL-PRODUCT-MEASUREMENTS.md +++ b/docs/r5/FULL-PRODUCT-MEASUREMENTS.md @@ -77,6 +77,19 @@ Each of five samples clears only the disposable app and its credentials, then: intent, confirmation, recovery material or seed-word screen is opened. This checks discoverability/return navigation only, not safe-deletion acceptance. +A sixth fresh fixture runs separately from the five performance samples. Its +first bounded createNewApp body is read and discarded, then the loopback server +closes the connection without any account storage/commit. The actual app must +show exactly one creation-error title, its internet/retry explanation and Retry, +with no Next action. Only after those UI assertions may the fixed Retry marker +and explicit Retry tap precede the second successful HTTP response. Success/home, +cold process relaunch, PIN login and persistent visible-wallet count1 are required. +Exactly one dropped attempt and one acknowledged response are allowed; a hidden +native HTTP retry, extra setup call or error marker without the transport failure +fails closed. This is **controlled loopback disconnection before commit**, not +physical WAN-offline or uncertain-after-commit acceptance. Its receipt is stored +under `controlled_disconnect_retry`, never in the five raw performance samples. + Only metadata.json receives bounded raw durations/counts/stage labels. Five separate startup-to-passcode and StartNew-to-ready values are retained; no median can hide an individual failure. Timings include Maestro driver and React Native @@ -98,11 +111,12 @@ log or database artifact belongs in uploaded evidence. ## Verification and limits `python3 -m unittest discover -s .github/tests -p test_product_measurements.py` -tests real loopback HTTP allowlisting, rejection and nonretention, ordered receipt -validation, missing/duplicate setup failure and partial-flow preservation. +tests real loopback HTTP allowlisting, disconnect/retry, rejection and nonretention, +ordered receipts, hidden/extra retry rejection, approved manifest overlays, native +version expectations and partial-flow preservation. Those tests provide no native acceptance. Before the first hosted execution, the full-app Maestro selectors and per-platform credential-reset behavior remain unverified. Record actual execution outcomes for each exact candidate; never copy passes or raw timings from another SHA. Physical biometrics and devices, -offline/error/retry scenarios, full deletion/reference protection, actual +physical WAN-offline/other error-retry scenarios, full deletion/reference protection, actual protected-backup operations and release readiness remain separate evidence. From 551198c6a5d55d05b5ecd2cae57505ca69270264 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Thu, 22 Oct 2026 07:03:18 +0000 Subject: [PATCH 246/254] ci: fail closed until protected release CI origin is authenticated --- docs/r5-cloud-release-readiness.md | 2 ++ scripts/protected-release-plan.cjs | 6 ++++++ scripts/protected-release-plan.test.cjs | 17 +++++++++++------ 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/docs/r5-cloud-release-readiness.md b/docs/r5-cloud-release-readiness.md index a6747e937e..8ae61635be 100644 --- a/docs/r5-cloud-release-readiness.md +++ b/docs/r5-cloud-release-readiness.md @@ -41,6 +41,8 @@ The executable [gate](../scripts/protected-release-plan.cjs) requires all of: - The matching protected release environment already configured with independent reviewers, prevented self review, no administrative bypass, and protected-branch restriction. An absent/unreadable environment blocks before the environment job; no implicit unprotected environment is created by a successful path. - Exact-head `Tests`, `CodeQL` and `keeper/local-validation` success, with no failed/skipped/in-progress/wrong-SHA check, and complete bounded check evidence. These fixed required names need confirmation against the final installed acceptance route; an unmatched name fails closed. +**Security correction: authorization planning is disabled fail-closed until trusted CI origin is implemented and verified.** A check/status name or bot/app creator alone does not prove the installed workflow ran; same-repository PR workflows may produce misleading status names. The collector currently cannot mint the module-private verified-origin capability, so even otherwise complete synthetic or name-matching evidence cannot produce an authorization plan. No plan or release operation is claimed verified. A separately reviewed exact-canonical/selected-route attestation must bind genuine workflow/run/attempt/source evidence before this boundary can be enabled. Existing failed/security/native checks remain intact; no PR pass transfers to a merge SHA. + The final protected job records the preparation boundary only. Its result always reports `execution_allowed: false`, `release_worker_implemented: false` and `artifact_bytes_and_signature_verified: false`. The artifact approval digest is bound to the request; this preparation package does not download/verify actual release bytes or signatures. Environment selection and shape-valid metadata alone never produce a release pass. **Future execution workers must revalidate after the protected environment approval**, immediately before any signing or upload: refresh driver/source ref identity, exact-head check and review state, approval author/scope/expiry, fresh store/version baseline, and the actual immutable artifact bytes/digest/signature/provenance. A source movement, new failure, expired/revoked approval or artifact mismatch while waiting blocks execution. The earlier preparation result cannot serve as final execution authorization, and a successful environment review cannot bypass this fresh validation. Artifact verification must use trusted driver code and must not run artifact-provided code. diff --git a/scripts/protected-release-plan.cjs b/scripts/protected-release-plan.cjs index 8dd7f1a298..2acd1411a9 100644 --- a/scripts/protected-release-plan.cjs +++ b/scripts/protected-release-plan.cjs @@ -14,6 +14,11 @@ const operations = { }; const sha = /^[a-f0-9]{40}$/; const digest = /^[a-f0-9]{64}$/; +// Status/check names and bot/app names do not authenticate their workflow +// origin. No caller/API JSON can mint this module-private capability. The +// installed canonical/native provenance route is not implemented/verified yet, +// so collectEvidence deliberately never sets it and authorization fails closed. +const verifiedCIOrigin = Symbol('verified-installed-ci-origin'); function requireCondition(condition, code) { if (!condition) throw new Error(code); } function requestFromEnvironment(env) { @@ -68,6 +73,7 @@ function validateEvidence(request, evidence, now = Date.now()) { requireCondition(['Tests', 'CodeQL', 'keeper/local-validation'].every(name => passedNames.has(name)), 'REQUIRED_CHECK_MISSING'); requireCondition(evidence.manifest?.version === request.version && String(evidence.manifest[request.operation.startsWith('ios-') || request.operation === 'testflight-upload' ? 'iosBuildNumber' : 'androidVersionCode']) === request.build, 'SOURCE_VERSION_BUILD_MISMATCH'); requireCondition(validateIntent(evidence.manifest, { now }).length === 0 && validateSources(evidence.manifest, evidence.sources).length === 0, 'RELEASE_SOURCE_PREFLIGHT_FAILED'); + requireCondition(evidence[verifiedCIOrigin] === true, 'TRUSTED_CI_ORIGIN_NOT_VERIFIED'); return { schema: 1, repository, sourceSha: request.sourceSha, driverSha: request.driverSha, operation: request.operation, version: request.version, build: request.build, diff --git a/scripts/protected-release-plan.test.cjs b/scripts/protected-release-plan.test.cjs index e0079434fe..706b189ef3 100644 --- a/scripts/protected-release-plan.test.cjs +++ b/scripts/protected-release-plan.test.cjs @@ -36,13 +36,18 @@ function fixture() { return { env, request, evidence }; } -test('even complete synthetic evidence prepares only, never authorizes execution or artifact trust', () => { +test('complete synthetic names/shape cannot establish trusted CI origin or authorize a plan', () => { const { request, evidence } = fixture(); - const result = validateEvidence(request, evidence, now); - assert.equal(result.execution_allowed, false); - assert.equal(result.release_worker_implemented, false); - assert.equal(result.artifact_bytes_and_signature_verified, false); - assert.equal(result.sourceSha, request.sourceSha); + assert.throws(() => validateEvidence(request, evidence, now), /TRUSTED_CI_ORIGIN_NOT_VERIFIED/); +}); + +test('forged successful status names, creators and apps never count as authenticated execution evidence', () => { + const { request, evidence } = fixture(); + evidence.statuses.statuses = ['Tests', 'CodeQL', 'keeper/local-validation'].map(context => ({ context, state: 'success', creator: { login: 'github-actions[bot]' } })); + evidence.checks = { total_count: 1, check_runs: [{ name: 'Unrelated', head_sha: request.sourceSha, status: 'completed', conclusion: 'success', app: { slug: 'github-actions' } }] }; + evidence.trustedCIOrigin = true; + evidence.originVerified = true; + assert.throws(() => validateEvidence(request, evidence, now), /TRUSTED_CI_ORIGIN_NOT_VERIFIED/); }); for (const [name, mutate] of [ From f15f7c4c705240a5fe07a8135ddcaa453aa7f659 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Thu, 22 Oct 2026 07:06:15 +0000 Subject: [PATCH 247/254] test(r5): count rejected GET requests in product fixture receipts --- .github/native/product_measurements.py | 2 ++ .github/tests/test_product_measurements.py | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/.github/native/product_measurements.py b/.github/native/product_measurements.py index acc43a5170..9dbb9e8f6c 100644 --- a/.github/native/product_measurements.py +++ b/.github/native/product_measurements.py @@ -150,6 +150,8 @@ def do_POST(self): self.reply(503, {'error': 'DISPOSABLE_FIXTURE_UNSUPPORTED_ROUTE'}) def do_GET(self): + with receipt.lock: + receipt.blocked_calls += 1 self.reply(503, {'error': 'DISPOSABLE_FIXTURE_UNSUPPORTED_ROUTE'}) class Server(ThreadingHTTPServer): diff --git a/.github/tests/test_product_measurements.py b/.github/tests/test_product_measurements.py index f542fb502e..ffaa6bfd6b 100644 --- a/.github/tests/test_product_measurements.py +++ b/.github/tests/test_product_measurements.py @@ -86,6 +86,14 @@ def test_unknown_backend_route_fails_closed(self): self.assertNotIn('deleted', response) self.assertEqual(self.receipt.snapshot()['other_backend_calls_rejected'], 1) + def test_rejected_get_is_counted_without_echoing_path_or_query(self): + with self.assertRaises(urllib.error.HTTPError) as failure: + urllib.request.urlopen(self.base + '/DO_NOT_RETAIN?secret=DO_NOT_RETAIN', timeout=2) + self.assertEqual(failure.exception.code, 503) + self.assertNotIn('DO_NOT_RETAIN', failure.exception.read().decode()) + self.assertEqual(self.receipt.snapshot()['other_backend_calls_rejected'], 1) + self.assertNotIn('DO_NOT_RETAIN', json.dumps(self.receipt.snapshot())) + def test_oversized_payload_is_rejected_without_retention(self): self.assertEqual(self.post('/createNewApp', b'x' * 8193)[0], 413) self.assertEqual(self.receipt.snapshot()['create_new_app_calls'], 0) From 76150f455978c1a565f2c670774672bc18789877 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Thu, 22 Oct 2026 07:08:48 +0000 Subject: [PATCH 248/254] test: assert specific protected release rejection boundaries --- scripts/protected-release-plan.test.cjs | 40 ++++++++++++------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/scripts/protected-release-plan.test.cjs b/scripts/protected-release-plan.test.cjs index 706b189ef3..e6a28430eb 100644 --- a/scripts/protected-release-plan.test.cjs +++ b/scripts/protected-release-plan.test.cjs @@ -50,29 +50,29 @@ test('forged successful status names, creators and apps never count as authentic assert.throws(() => validateEvidence(request, evidence, now), /TRUSTED_CI_ORIGIN_NOT_VERIFIED/); }); -for (const [name, mutate] of [ - ['moving driver', e => { e.driver.commit.sha = 'd'.repeat(40); }], - ['moving release source', e => { e.branch.commit.sha = 'd'.repeat(40); }], - ['unprotected source', e => { e.branch.protected = false; }], - ['author assertion', e => { e.comment.user.login = 'someone-else'; }], - ['expired owner approval', e => { e.comment.body = e.comment.body.replace('13:00:00Z', '10:00:00Z'); }], - ['wrong artifact approval', e => { e.comment.body = e.comment.body.replace('c'.repeat(64), 'd'.repeat(64)); }], - ['administrative bypass', e => { e.environment.can_admins_bypass = true; }], - ['unverified bypass', e => { delete e.environment.can_admins_bypass; }], - ['self approval', e => { e.environment.protection_rules[0].prevent_self_review = false; }], - ['missing environment', e => { e.environment = null; }], - ['stale native status', e => { e.statuses.sha = 'd'.repeat(40); }], - ['failed security check', e => { e.checks.check_runs[1].conclusion = 'failure'; }], - ['skipped check', e => { e.checks.check_runs[0].conclusion = 'skipped'; }], - ['wrong-SHA check', e => { e.checks.check_runs[0].head_sha = 'd'.repeat(40); }], - ['incomplete check pagination', e => { e.checks.total_count = 101; }], - ['missing required check', e => { e.checks.check_runs[1].name = 'Other'; }], - ['stale store counter evidence', e => { e.manifest.baselines.ios.verifiedAt = '2026-10-20T11:00:00Z'; }], - ['source version mismatch', e => { e.sources.packageText = '{"version":"0.0.0"}'; }], +for (const [name, mutate, expectedCode] of [ + ['moving driver', e => { e.driver.commit.sha = 'd'.repeat(40); }, 'DRIVER_MOVED_OR_UNPROTECTED'], + ['moving release source', e => { e.branch.commit.sha = 'd'.repeat(40); }, 'SOURCE_MOVED_OR_UNPROTECTED'], + ['unprotected source', e => { e.branch.protected = false; }, 'SOURCE_MOVED_OR_UNPROTECTED'], + ['author assertion', e => { e.comment.user.login = 'someone-else'; }, 'APPROVAL_AUTHOR_UNAUTHORIZED'], + ['expired owner approval', e => { e.comment.body = e.comment.body.replace('13:00:00Z', '10:00:00Z'); }, 'APPROVAL_STALE_OR_INVALID'], + ['wrong artifact approval', e => { e.comment.body = e.comment.body.replace('c'.repeat(64), 'd'.repeat(64)); }, 'APPROVAL_SCOPE_MISMATCH'], + ['administrative bypass', e => { e.environment.can_admins_bypass = true; }, 'ENVIRONMENT_MISSING_OR_BYPASS_UNVERIFIED'], + ['unverified bypass', e => { delete e.environment.can_admins_bypass; }, 'ENVIRONMENT_MISSING_OR_BYPASS_UNVERIFIED'], + ['self approval', e => { e.environment.protection_rules[0].prevent_self_review = false; }, 'INDEPENDENT_ENVIRONMENT_REVIEW_REQUIRED'], + ['missing environment', e => { e.environment = null; }, 'ENVIRONMENT_MISSING_OR_BYPASS_UNVERIFIED'], + ['stale native status', e => { e.statuses.sha = 'd'.repeat(40); }, 'STATUS_NOT_EXACT_SUCCESS'], + ['failed security check', e => { e.checks.check_runs[1].conclusion = 'failure'; }, 'CHECKS_NOT_EXACT_SUCCESS'], + ['skipped check', e => { e.checks.check_runs[0].conclusion = 'skipped'; }, 'CHECKS_NOT_EXACT_SUCCESS'], + ['wrong-SHA check', e => { e.checks.check_runs[0].head_sha = 'd'.repeat(40); }, 'CHECKS_NOT_EXACT_SUCCESS'], + ['incomplete check pagination', e => { e.checks.total_count = 101; }, 'INCOMPLETE_CHECK_EVIDENCE'], + ['missing required check', e => { e.checks.check_runs[1].name = 'Other'; }, 'REQUIRED_CHECK_MISSING'], + ['stale store counter evidence', e => { e.manifest.baselines.ios.verifiedAt = '2026-10-20T11:00:00Z'; }, 'RELEASE_SOURCE_PREFLIGHT_FAILED'], + ['source version mismatch', e => { e.sources.packageText = '{"version":"0.0.0"}'; }, 'RELEASE_SOURCE_PREFLIGHT_FAILED'], ]) test(`fails closed: ${name}`, () => { const { request, evidence } = fixture(); mutate(evidence); - assert.throws(() => validateEvidence(request, evidence, now)); + assert.throws(() => validateEvidence(request, evidence, now), new RegExp(expectedCode)); }); test('rejects uninstalled/PR driver, injection, mutable source, invalid artifact and public operation', () => { From 213d5a87daee03fcfe6d4c59642f94908b05400b Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Thu, 22 Oct 2026 07:08:51 +0000 Subject: [PATCH 249/254] docs(r5): record reviewed retry and fail-closed preparation boundaries --- docs/r5-cloud-integration.md | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/docs/r5-cloud-integration.md b/docs/r5-cloud-integration.md index 6a3def2979..da27c95792 100644 --- a/docs/r5-cloud-integration.md +++ b/docs/r5-cloud-integration.md @@ -92,14 +92,14 @@ post-test evidence update and silently transfer results to the resulting SHA. | 5.2 | Owner-reported running, authoritative task/branch/PR unavailable | Running dependency; obtain checkpoint, inspect overlaps, integrate verified commits, rerun affected checks before freeze | | Cloud reconciliation | #7070 / `17baf3bff3a5cd96787218b9e8e1fee75f549967`; inputs #7060/#7063/#7062 | Included with original patch review links; installed-base trust and required branch protections remain separate from diagnostic execution | | Native production-worker witness | #7071 / `486810052f90550d55dd6ddd541ff53ebd875caa` | Included; actual Realm/MMKV worker scenarios, explicitly controlled generation/transport/dispatch; execute on exact combined source | -| Full-product UI measurements | #7073 / `5457cb08194495ed35767db92110727ce54ef9a1` | Included; require all five numeric samples, actual normal app, cold relaunch/counts/navigation; no helper timing substitution | +| Full-product UI measurements | #7073 / `040955d0caca38bb3cb386ca2918adf60f0f30db` | Included; require five healthy numeric samples plus a separate failed-transport/explicit-Retry fixture, actual normal app and cold relaunch/counts/navigation; no helper timing substitution or WAN-offline claim | | Confirmed biometric callback defects | #7069 / `257fbe17e1a85d0e5d7e5d8ab97e1a69edbd9628`, review correction `14a2459b30e31ea3099d56f6c6928a4a3b7d6995` | Included; independent review reproduced prompt repetition and stale epoch cases; 17 component controls pass, actual biometrics remain unrun | | Current protocol pairing | Private Relay #160 / `4db6a758a307111137ad6bb4f35a32fa15b17a9d` | 52 backend + 43 protected app + 1 upgraded-account + 3 guard cases pass at their exact documented pair; pin/retest combined app SHA | -| Protected release preparation | #7072 / `f2fc0cb557f0e5303335dadc9e98152640795e6c` | Included; authorization/inventory preparation implemented, actual signing/distribution not verified; separate bounded worker preparation and protected setup remain | +| Protected release preparation | #7072 / `f2fc0cb557f0e5303335dadc9e98152640795e6c`; origin correction #7074 / `551198c6a5d55d05b5ecd2cae57505ca69270264` | Included; inventory and plan controls implemented, authorization planning deliberately disabled until installed CI origin is verified. Review reproduced forged check-name acceptance; 21 corrected plan controls pass. Separate future workers/protected setup remain, with no signing/upload executed | | PHP exposure research | #7072, `docs/r5-cloud-release-readiness.md` | Official advisories/source assessment documented; deployed runtime inventory unknown, no Keeper exposure assertion; remedies stay R6 | | Navigation/timezone/PIN/copy | `docs/r5/BIOMETRIC-LIFECYCLE.md` and source tests | No speculative #7055/#6991 fix; bounded native reproduction/device checks remain. Retain shipped PIN and scoped EN/ES corrections | | Read-only timelock assessor | Separately recorded candidate, not identified as 5.2 | Source/handoff and complete key/origin/script/intent binding and native isolation acceptance remain; no activation or wallet clearance | -| Independent/Claude review | Source review receipts on package PRs | Independent agent reviews performed; human/security and applicable Claude review not established; no paid activation implied | +| Independent/Claude review | Source review receipts on package PRs; policy #7034 / reviewer #7049 | Independent agent reviews performed. Apply risk-based conditional review; policy/auth/recovery/CI changes require adequate review. Applicable Claude provider review remains unavailable without trusted installation and verified existing authorization/budget; no new paid activation or blanket human-review requirement inferred | | Physical acceptance / release evidence | Exact artifact and authorized tester required | Not run; no physical deferral inferred. Version/build allocation, protected signing and distribution require the specified fresh evidence and explicit approvals | The infrastructure adoption is about **54k changed code/test characters/4** across @@ -107,10 +107,14 @@ the existing packages, much larger than an older 19k estimate. The initial new reconciliation was about 6.7k, with a small additional runner hook. Preserve the original #7060/#7063/#7062 reviews and the reconciliation-only comparison; this aggregate is an integration input, not a request to approve one new 54k patch. -Native worker (~7.8k), product measurements (~8.1k), biometric correction (~4k) +Native worker (~7.8k), product measurements (~10.1k), biometric correction (~4k) and release preparation (~7.2k code/test, ~12.7k including research/docs) stay -separate. Further signing workers are a separate estimated 5–10k preparation -package. The unknown 5.2 delta is not estimated by guessing. +separate. Signing worker preparation grew during independent review and is split +into bounded core authorization/platform adapters and separate orchestration/ +uploader packages, with a small locked Ruby-toolchain package. None is integrated +into this native candidate or executed. Exact canonical-source native attestation, +protected environments and one-time credentials remain real dependencies. The +unknown 5.2 delta is not estimated by guessing. The first combined preflight exposed an expected-workflow allowlist mismatch after adding the offline release-preparation contracts. It is retained as an From 5c4c211339aeee3216faa0663c309314a1627123 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Thu, 22 Oct 2026 07:11:36 +0000 Subject: [PATCH 250/254] test(r5): exercise unused key deletion and PIN rejection in normal app UI --- .github/native/product_key_acceptance.py | 156 +++++++++++ .github/native/product_key_acceptance.yaml | 246 ++++++++++++++++++ .github/native/product_key_metric.js | 6 + .github/native/product_measurements.py | 12 + .github/tests/test_product_key_acceptance.py | 154 +++++++++++ docs/r5/NATIVE-KEY-UI-ACCEPTANCE.md | 61 +++++ src/components/Fab.tsx | 6 +- .../Home/components/Keys/ManageKeys.tsx | 1 + 8 files changed, 640 insertions(+), 2 deletions(-) create mode 100644 .github/native/product_key_acceptance.py create mode 100644 .github/native/product_key_acceptance.yaml create mode 100644 .github/native/product_key_metric.js create mode 100644 .github/tests/test_product_key_acceptance.py create mode 100644 docs/r5/NATIVE-KEY-UI-ACCEPTANCE.md diff --git a/.github/native/product_key_acceptance.py b/.github/native/product_key_acceptance.py new file mode 100644 index 0000000000..3635bb6b70 --- /dev/null +++ b/.github/native/product_key_acceptance.py @@ -0,0 +1,156 @@ +"""Normal-app unused-key/PIN companion on the product runner's owned device. + +No standalone device selector or fixture/database/credential injection is exposed. +This runs after the five healthy samples and separate setup Retry fixture, while +the same ordinary Metro process and disposable backend responder remain alive. +""" +import json +import os +from pathlib import Path +import shutil +import subprocess +import threading +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer + +PORT = 8766 +STAGES = ( + 'existing_disposable_home_verified', 'empty_deletion_entry_verified', + 'unused_mobile_key_created', 'delete_confirmation_scoped', + 'cancelled_confirmation_preserved_key', 'wrong_deletion_pin_rejected', + 'unused_mobile_key_deleted', 'unrelated_wallet_preserved', + 'cold_wrong_login_pin_rejected', 'deletion_persisted_after_relaunch', + 'final_wallet_verified', +) + + +class Receipt: + def __init__(self): + self.lock = threading.Lock() + self.completed = [] + + def mark(self, stage): + with self.lock: + if len(self.completed) >= len(STAGES) or stage != STAGES[len(self.completed)]: + return False + self.completed.append(stage) + return True + + def snapshot(self, passed=False): + with self.lock: + if passed and self.completed != list(STAGES): + raise RuntimeError('PRODUCT_KEY_UI_INCOMPLETE') + return {'status': 'PASS' if passed else 'FAIL', + 'completed_stages': list(self.completed), + 'entrypoint': 'index.js', 'network': 'TESTNET', + 'scenario': 'UNUSED_MOBILE_KEY_UI_DELETE_AND_NATIVE_PIN', + 'visible_eligible_keys_after_deletion': 0 if passed else None, + 'unrelated_visible_wallet_count_after_relaunch': 1 if passed else None, + 'referenced_vault_ui': 'NOT IMPLEMENTED', + 'credential_store_read_fault': 'NOT RUN', + 'physical_biometrics': 'NOT RUN', + 'protected_backup_deletion': 'NOT TESTED', + 'recovery_key_preservation': 'Not selected or exported; excluded by production deletion filters.'} + + +def make_server(receipt, port=PORT): + class Handler(BaseHTTPRequestHandler): + def log_message(self, *_): + pass + + def reply(self, status): + self.send_response(status) + self.send_header('Content-Length', '0') + self.end_headers() + + def do_POST(self): + try: + length = int(self.headers.get('Content-Length', '0')) + if self.path != '/metric' or not 0 < length <= 128: + self.close_connection = True + return self.reply(422) + metric = json.loads(self.rfile.read(length)) + if (isinstance(metric, dict) and set(metric) == {'stage'} and + receipt.mark(metric['stage'])): + return self.reply(200) + except (ValueError, TypeError): + pass + self.reply(422) + + def do_GET(self): + self.reply(405) + + class Server(ThreadingHTTPServer): + allow_reuse_address = False + daemon_threads = True + + return Server(('127.0.0.1', port), Handler) + + +def run_companion(h, data, device, private, backend_receipt): + if (os.environ.get('GITHUB_ACTIONS') != 'true' or + os.environ.get('RUNNER_ENVIRONMENT') != 'github-hosted' or + h.PLATFORM not in ['IOS', 'ANDROID'] or + (h.PLATFORM == 'ANDROID' and not device.startswith('emulator-'))): + raise RuntimeError('PRODUCT_KEY_UI_OWNED_HOSTED_DEVICE_REQUIRED') + actual = subprocess.check_output(['git', 'rev-parse', 'HEAD'], cwd=h.ROOT, text=True).strip() + if (data['build'] != 'PASS' or actual != data['source_sha'] or + actual != os.environ.get('KEEPER_SOURCE_SHA') or + h.artifact_identity() != data['built_artifact']): + raise RuntimeError('PRODUCT_KEY_UI_EXACT_BUILD_REQUIRED') + if subprocess.run(['git', 'diff', '--quiet', 'HEAD', '--', '.github/native', 'src', + 'index.js', 'App.tsx', 'metro.config.js', 'babel.config.js'], + cwd=h.ROOT, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).returncode: + raise RuntimeError('PRODUCT_KEY_UI_COMMITTED_SOURCE_REQUIRED') + # Do not adopt another installation or turn this companion into account setup. + before = backend_receipt.snapshot(passed=True) + if (before.get('status') != 'PASS' or before.get('sample') != 6 or + before.get('scenario') != 'CONTROLLED_DISCONNECT_BEFORE_COMMIT_NOT_WAN_OFFLINE' or + before.get('create_new_app_calls') != 2 or + before.get('visible_wallet_count_after_relaunch') != 1): + raise RuntimeError('PRODUCT_KEY_UI_DISPOSABLE_RETRY_FIXTURE_REQUIRED') + output = Path(private) / 'key-ui' + output.mkdir(mode=0o700) + report = output / 'junit.xml' + receipt = Receipt() + result = receipt.snapshot() + result['status'] = 'RUNNING' + server = None + + def save(): + current = h.read() + current['product_key_ui'] = result + h.save(current) + + save() + try: + server = make_server(receipt) + thread = threading.Thread(target=server.serve_forever, daemon=True) + thread.start() + h.command([data['maestro']['binary'], '--device', device, 'test', + '--env', 'APP_ID=' + h.APP_IDS[h.PLATFORM], '--flatten-debug-output', + '--debug-output', str(output), '--test-output-dir', str(output), + '--format', 'junit', '--output', str(report), + '.github/native/product_key_acceptance.yaml'], + 'Normal-app unused-key deletion and native PIN rejection', + timeout=720, error_details=False) + h.successful_junit(report) + if h.artifact_identity() != data['built_artifact']: + raise RuntimeError('PRODUCT_KEY_UI_ARTIFACT_CHANGED') + after = backend_receipt.snapshot(passed=True) + if after['create_new_app_calls'] != before['create_new_app_calls']: + raise RuntimeError('PRODUCT_KEY_UI_UNEXPECTED_ACCOUNT_SETUP') + result = receipt.snapshot(passed=True) + result['new_account_requests_during_key_flow'] = 0 + except Exception as error: + result = receipt.snapshot() + result['failure_category'] = type(error).__name__ + raise + finally: + save() + if server: + server.shutdown() + server.server_close() + # Maestro output can contain transient screenshots/hierarchy/PIN actions; + # retain no such files. The parent also deletes this suite's process logs. + shutil.rmtree(output, ignore_errors=True) + return result diff --git a/.github/native/product_key_acceptance.yaml b/.github/native/product_key_acceptance.yaml new file mode 100644 index 0000000000..e13240e0cd --- /dev/null +++ b/.github/native/product_key_acceptance.yaml @@ -0,0 +1,246 @@ +# Ordinary app entrypoint, current native credentials/reducers/sagas/Realm. +# Runs only on the task-owned disposable one-wallet account from retry fixture6. +# Do not open seed/QR/export screens, opt into backup, fund or sign anything. +appId: ${APP_ID} +name: Normal app unused key deletion PIN rejection and cold persistence +--- +- launchApp +- extendedWaitUntil: + visible: Enter your passcode + timeout: 60000 +- repeat: + times: 4 + commands: + - tapOn: + id: key_1 +- tapOn: Proceed +- extendedWaitUntil: + visible: + id: home_visible_wallet_count_1 + timeout: 60000 +- assertVisible: Mobile Wallet +- runScript: + file: product_key_metric.js + env: + STAGE: existing_disposable_home_verified +- tapOn: + id: home_tab_keys +- extendedWaitUntil: + visible: + id: manage_keys_actions + timeout: 30000 +- tapOn: + id: manage_keys_actions +- extendedWaitUntil: + visible: + id: header_title + text: Manage Keys + timeout: 30000 +- assertVisible: All the keys are hidden +- assertNotVisible: Delete +- runScript: + file: product_key_metric.js + env: + STAGE: empty_deletion_entry_verified +- tapOn: + id: btn_back +- tapOn: + id: keys_add_key +- extendedWaitUntil: + visible: Add a software key + timeout: 30000 +- tapOn: Add a software key +- extendedWaitUntil: + visible: + id: btn_MY_KEEPER + timeout: 30000 +- tapOn: + id: btn_MY_KEEPER +- extendedWaitUntil: + visible: Setting up Mobile Key \(Hot\) + timeout: 30000 +- tapOn: Proceed +- extendedWaitUntil: + visible: Confirm passcode to generate key + timeout: 30000 +- assertVisible: + id: modal_passcode_verify +- repeat: + times: 4 + commands: + - tapOn: + id: key_1 +- tapOn: Proceed +- extendedWaitUntil: + visible: Mobile Key Added Successfully! + timeout: 60000 +- assertNotVisible: + id: seed_item_0 +- tapOn: Cancel +- extendedWaitUntil: + visible: + id: manage_keys_actions + timeout: 30000 +- tapOn: + id: manage_keys_actions +- extendedWaitUntil: + visible: + id: btnDelete + timeout: 30000 +- assertVisible: Mobile Key +- runScript: + file: product_key_metric.js + env: + STAGE: unused_mobile_key_created +- tapOn: + id: btnDelete +- extendedWaitUntil: + visible: Confirm passcode to delete key + timeout: 30000 +- assertVisible: + id: modal_passcode_verify +- runScript: + file: product_key_metric.js + env: + STAGE: delete_confirmation_scoped +# A cancelled partially entered confirmation must preserve the eligible key. +- repeat: + times: 3 + commands: + - tapOn: + id: key_1 +- tapOn: + id: btn_close_modal +- extendedWaitUntil: + visible: + id: btnDelete + timeout: 30000 +- assertVisible: Mobile Key +- assertNotVisible: Key Deleted Successfully +- runScript: + file: product_key_metric.js + env: + STAGE: cancelled_confirmation_preserved_key +- tapOn: + id: btnDelete +- extendedWaitUntil: + visible: + id: modal_passcode_verify + timeout: 30000 +# The only created account uses PIN1111. PIN0000 must be rejected natively. +- repeat: + times: 4 + commands: + - tapOn: + id: key_0 +- tapOn: Proceed +- extendedWaitUntil: + visible: Incorrect Passcode, Try Again! + timeout: 30000 +- assertVisible: + id: modal_passcode_verify +- assertNotVisible: Key Deleted Successfully +- runScript: + file: product_key_metric.js + env: + STAGE: wrong_deletion_pin_rejected +- repeat: + times: 4 + commands: + - tapOn: + id: key_1 +- extendedWaitUntil: + visible: + id: btn_primaryText + enabled: true + timeout: 10000 +- tapOn: Proceed +- extendedWaitUntil: + visible: Key Deleted Successfully + timeout: 60000 +- assertVisible: Your Key has been deleted successfully. You will have to add the Key again +- runScript: + file: product_key_metric.js + env: + STAGE: unused_mobile_key_deleted +- tapOn: Continue +- extendedWaitUntil: + visible: All the keys are hidden + timeout: 30000 +- assertNotVisible: Delete +- assertNotVisible: Mobile Key +- tapOn: + id: btn_back +- tapOn: + id: home_tab_wallets +- extendedWaitUntil: + visible: + id: home_visible_wallet_count_1 + timeout: 30000 +- assertVisible: Mobile Wallet +- runScript: + file: product_key_metric.js + env: + STAGE: unrelated_wallet_preserved +- stopApp +- launchApp +- extendedWaitUntil: + visible: Enter your passcode + timeout: 60000 +- repeat: + times: 4 + commands: + - tapOn: + id: key_0 +- tapOn: Proceed +- extendedWaitUntil: + visible: Incorrect Passcode, Try Again! + timeout: 30000 +- assertNotVisible: + id: home_visible_wallet_count_1 +- runScript: + file: product_key_metric.js + env: + STAGE: cold_wrong_login_pin_rejected +- repeat: + times: 4 + commands: + - tapOn: + id: key_1 +- tapOn: Proceed +- extendedWaitUntil: + visible: + id: home_visible_wallet_count_1 + timeout: 60000 +- assertVisible: Mobile Wallet +- tapOn: + id: home_tab_keys +- extendedWaitUntil: + visible: + id: manage_keys_actions + timeout: 30000 +- tapOn: + id: manage_keys_actions +- extendedWaitUntil: + visible: All the keys are hidden + timeout: 30000 +- assertNotVisible: Delete +- assertNotVisible: Mobile Key +- runScript: + file: product_key_metric.js + env: + STAGE: deletion_persisted_after_relaunch +- tapOn: + id: btn_back +- tapOn: + id: home_tab_wallets +- extendedWaitUntil: + visible: + id: home_visible_wallet_count_1 + timeout: 30000 +- assertVisible: Mobile Wallet +- runScript: + file: product_key_metric.js + env: + STAGE: final_wallet_verified +- stopApp diff --git a/.github/native/product_key_metric.js b/.github/native/product_key_metric.js new file mode 100644 index 0000000000..5e8af4f221 --- /dev/null +++ b/.github/native/product_key_metric.js @@ -0,0 +1,6 @@ +// Maestro runs this on the host. Export fixed stage names only, never app data. +const reply = http.post('http://127.0.0.1:8766/metric', { + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ stage: STAGE }), +}); +if (reply.status !== 200) throw new Error('PRODUCT_KEY_METRIC_REJECTED'); diff --git a/.github/native/product_measurements.py b/.github/native/product_measurements.py index acc43a5170..0b3c05c2f7 100644 --- a/.github/native/product_measurements.py +++ b/.github/native/product_measurements.py @@ -339,6 +339,18 @@ def save(): # screenshots, hierarchy, PIN actions or app output artifacts. shutil.rmtree(output, ignore_errors=True) save() + # Separate normal-app UI companion; never part of the five latency + # samples or the controlled setup-disconnect receipt vocabulary. + spec = importlib.util.spec_from_file_location( + 'product_key_acceptance', Path(__file__).with_name('product_key_acceptance.py')) + key_companion = importlib.util.module_from_spec(spec) + spec.loader.exec_module(key_companion) + result['key_management_ui'] = key_companion.run_companion(h, data, device, private, receipt) + result['key_management_scope'] = ( + 'Normal-app unused Mobile Key creation/deletion, cancellation, wrong PIN rejection, ' + 'correct PIN retry, cold deletion persistence and unrelated wallet retention. ' + 'Referenced-Vault UI, credential-store read faults and physical biometrics remain unverified.') + save() result['status'] = 'PASS' except Exception as error: result['status'] = 'FAIL' diff --git a/.github/tests/test_product_key_acceptance.py b/.github/tests/test_product_key_acceptance.py new file mode 100644 index 0000000000..74841ad4f7 --- /dev/null +++ b/.github/tests/test_product_key_acceptance.py @@ -0,0 +1,154 @@ +"""Host-only normal-app companion contracts; no native acceptance credit.""" +import contextlib +import copy +import importlib.util +import json +from pathlib import Path +import re +import tempfile +import threading +import types +import unittest +from unittest import mock +import urllib.error +import urllib.request + +SPEC = importlib.util.spec_from_file_location( + 'product_key_acceptance', Path(__file__).resolve().parents[1] / 'native/product_key_acceptance.py') +product = importlib.util.module_from_spec(SPEC) +SPEC.loader.exec_module(product) + + +class ReceiptTests(unittest.TestCase): + def test_exact_stage_order_is_required_without_substitute_or_repeated_marks(self): + receipt = product.Receipt() + self.assertFalse(receipt.mark('unused_mobile_key_deleted')) + for stage in product.STAGES: + self.assertTrue(receipt.mark(stage)) + self.assertFalse(receipt.mark(stage)) + evidence = receipt.snapshot(passed=True) + self.assertEqual(evidence['status'], 'PASS') + self.assertEqual(evidence['referenced_vault_ui'], 'NOT IMPLEMENTED') + self.assertEqual(evidence['credential_store_read_fault'], 'NOT RUN') + + def test_missing_checks_never_pass(self): + receipt = product.Receipt() + receipt.mark(product.STAGES[0]) + with self.assertRaisesRegex(RuntimeError, 'INCOMPLETE'): + receipt.snapshot(passed=True) + self.assertEqual(receipt.snapshot()['status'], 'FAIL') + + def test_loopback_server_rejects_extra_fields_oversize_and_unknown_stage_without_retaining_data(self): + receipt = product.Receipt() + server = product.make_server(receipt, port=0) + thread = threading.Thread(target=server.serve_forever, daemon=True) + thread.start() + try: + url = 'http://127.0.0.1:%d/metric' % server.server_port + for payload in [{'stage': 'PRIVATE_SENTINEL'}, {'stage': product.STAGES[0], 'private': 'PRIVATE_SENTINEL'}, + {'private': 'PRIVATE_SENTINEL' * 100}]: + request = urllib.request.Request(url, data=json.dumps(payload).encode(), method='POST') + with self.assertRaises(urllib.error.HTTPError) as failure: + urllib.request.urlopen(request) + self.assertEqual(failure.exception.code, 422) + request = urllib.request.Request(url, data=json.dumps({'stage': product.STAGES[0]}).encode(), method='POST') + with urllib.request.urlopen(request) as response: + self.assertEqual(response.status, 200) + self.assertNotIn('PRIVATE_SENTINEL', json.dumps(receipt.snapshot())) + finally: + server.shutdown() + server.server_close() + + def test_reviewed_flow_has_every_receipt_mark_and_no_export_or_credential_injection_path(self): + flow = (Path(__file__).resolve().parents[1] / 'native/product_key_acceptance.yaml').read_text() + self.assertEqual(re.findall(r'STAGE: ([a-z_]+)', flow), list(product.STAGES)) + self.assertIn('id: btn_MY_KEEPER', flow) + self.assertIn('id: keys_add_key', flow) + self.assertIn('id: btnDelete', flow) + for command in ['takeScreenshot:', 'copyTextFrom:', 'inputText:', 'setClipboard:', 'clearKeychain:']: + self.assertNotIn(command, flow) + + +class RunnerTests(unittest.TestCase): + def exercise(self, *, omit_last=False, artifact_changed=False, command_failure=False, + source_dirty=False, device='emulator-5554', extra_create=False): + directory = tempfile.TemporaryDirectory() + self.addCleanup(directory.cleanup) + root = Path(directory.name) + private = root / 'private' + private.mkdir() + sha = '1' * 40 + artifact = {'sha256': 'f' * 64} + state = {'build': 'PASS', 'source_sha': sha, 'built_artifact': artifact, + 'maestro': {'binary': 'synthetic-maestro'}} + runtime = {} + backend_calls = 0 + + def backend_snapshot(passed=False): + nonlocal backend_calls + backend_calls += 1 + return {'status': 'PASS', 'sample': 6, 'visible_wallet_count_after_relaunch': 1, + 'scenario': 'CONTROLLED_DISCONNECT_BEFORE_COMMIT_NOT_WAN_OFFLINE', + 'create_new_app_calls': 3 if extra_create and backend_calls > 1 else 2} + + def command(_args, _label, **kwargs): + self.assertFalse(kwargs['error_details']) + output = private / 'key-ui' + (output / 'PRIVATE_SCREENSHOT_SENTINEL.png').write_text('PRIVATE_SENTINEL') + if command_failure: + raise RuntimeError('PRIVATE_SENTINEL') + for stage in product.STAGES[:-1] if omit_last else product.STAGES: + self.assertTrue(runtime['receipt'].mark(stage)) + + def server(receipt): + runtime['receipt'] = receipt + return types.SimpleNamespace(serve_forever=lambda: None, shutdown=lambda: None, + server_close=lambda: None) + + identity = mock.Mock(side_effect=[artifact, {'sha256': 'changed'}] if artifact_changed else None, + return_value=artifact) + h = types.SimpleNamespace(ROOT=root, PLATFORM='ANDROID', APP_IDS={'ANDROID': 'disposable.app'}, + read=lambda: copy.deepcopy(state), save=lambda value: state.update(copy.deepcopy(value)), + artifact_identity=identity, command=command, successful_junit=lambda report: 1) + backend = types.SimpleNamespace(snapshot=backend_snapshot) + with contextlib.ExitStack() as stack: + stack.enter_context(mock.patch.dict(product.os.environ, {'GITHUB_ACTIONS': 'true', + 'RUNNER_ENVIRONMENT': 'github-hosted', 'KEEPER_SOURCE_SHA': sha})) + stack.enter_context(mock.patch.object(product.subprocess, 'check_output', return_value=sha)) + stack.enter_context(mock.patch.object(product.subprocess, 'run', return_value=types.SimpleNamespace( + returncode=1 if source_dirty else 0))) + stack.enter_context(mock.patch.object(product, 'make_server', side_effect=server)) + try: + value = product.run_companion(h, state, device, private, backend) + return value, state, private + except Exception as error: + return error, state, private + + def test_complete_flow_keeps_scope_separate_and_removes_all_private_ui_output(self): + value, state, private = self.exercise() + self.assertEqual(value['status'], 'PASS') + self.assertEqual(value['new_account_requests_during_key_flow'], 0) + self.assertEqual(value['physical_biometrics'], 'NOT RUN') + self.assertFalse((private / 'key-ui').exists()) + self.assertNotIn('PRIVATE_SENTINEL', json.dumps(state)) + + def test_missing_stage_artifact_drift_driver_failure_and_new_setup_each_fail_closed(self): + for option in ['omit_last', 'artifact_changed', 'command_failure', 'extra_create']: + with self.subTest(option=option): + value, state, private = self.exercise(**{option: True}) + self.assertIsInstance(value, RuntimeError) + self.assertEqual(state['product_key_ui']['status'], 'FAIL') + self.assertFalse((private / 'key-ui').exists()) + self.assertNotIn('PRIVATE_SENTINEL', json.dumps(state)) + + def test_real_device_or_dirty_source_is_rejected_before_ui_execution(self): + for options in [{'device': 'physical-device-id'}, {'source_dirty': True}]: + with self.subTest(options=options): + value, state, private = self.exercise(**options) + self.assertIsInstance(value, RuntimeError) + self.assertNotIn('product_key_ui', state) + self.assertFalse((private / 'key-ui').exists()) + + +if __name__ == '__main__': + unittest.main() diff --git a/docs/r5/NATIVE-KEY-UI-ACCEPTANCE.md b/docs/r5/NATIVE-KEY-UI-ACCEPTANCE.md new file mode 100644 index 0000000000..808ba85c22 --- /dev/null +++ b/docs/r5/NATIVE-KEY-UI-ACCEPTANCE.md @@ -0,0 +1,61 @@ +# R5 normal-app key/PIN UI companion + +This separately reviewable package adds one normal-app flow after the five healthy +startup/first-wallet samples and the separate controlled setup Retry fixture. +The same task-owned simulator/emulator, ordinary `index.js` Metro entrypoint, +current reducers/sagas, native Keychain and encrypted Realm remain in use. The +latency samples and Retry fixture keep their own receipts and outcomes. + +The companion starts with the disposable Testnet account created by fixture six +(PIN1111, one unfunded Mobile Wallet, backup consent off). It uses the visible +Keys → Manage Keys action, verifies that no eligible deletion key is initially +shown, and creates one extra unused Mobile Key through Software Keys and the +normal PIN confirmation. That path derives locally and does not open seed/QR or +export screens. A generic `keys_add_key` selector on the existing floating action +button is the only app instrumentation; it contains no account/key identifier. + +The executable flow checks the scoped deletion confirmation, cancellation after +partial PIN entry, rejection of PIN0000, correct PIN retry and the deletion +success/empty eligible-key list. The unrelated one-wallet list remains present. +After a cold process restart it also rejects the wrong login PIN, accepts the +correct PIN, verifies the eligible key remains absent and retains the one Mobile +Wallet. The hidden Recovery Key is excluded by the existing production deletion +filters and is never selected or exported. + +The loopback backend remains create-only; the companion requires the completed +fixture-six receipt and rejects any additional account-creation request. It has +its own host-loopback metric receiver on port8766 with fixed ordered stage names +and no app data. PASS requires successful Maestro JUnit, every ordered stage, +unchanged candidate build identity and the unchanged account-creation count. +Only fixed outcome/scope identifiers are retained. Maestro's temporary +screenshots/hierarchy/PIN actions are deleted; the parent removes this suite's +process logs and task-owned installation. No raw error or key material is +copied to evidence or uploaded. + +Scope remains explicit: + +- Actual referenced-Vault UI protection is **NOT IMPLEMENTED** by this companion. + Creating a real retained Vault using the present UI would require additional + signer/hardware/seed/import journeys. The separate production-worker native + reference/rollback/session probe keeps its controlled-boundary label. +- Native credential rejection is exercised through wrong PINs; a credential-store + **read fault** is **NOT RUN**. No hidden credential injection or fault hook was + added to production. +- Physical biometrics, protected-backup protocol/deletion, archived/concealed + Vault UI, hardware signing, recovery cryptography and release readiness remain + separately unverified. No simulator result may be reported as a physical pass. + +Initial source: root integration checkpoint +`cad01c3ddba3a394bda33f969e0a177d153ba6d8`. The package adds roughly 4k–7k +changed code/test tokens, a small runner hook and two nonfunctional selector +changes. No producer branch or release operation is changed. + +Actual host checks: seven companion HTTP/receipt/runner/privacy/failure contracts +PASS; the 23 existing product measurement/Retry contracts and 14 existing wallet +UI component regressions PASS. Babel and YAML parsing pass. Focused selector-file +ESLint retains its baseline failures (9 errors / 2 warnings); no added findings. +These contracts +use modeled driver/artifact boundaries and provide **no native acceptance**. +Both platform executions remain **NOT RUN** until the coordinator publishes and +runs the combined exact candidate sequentially. Keep failed native attempts and +all unimplemented rows visible; no acceptance transfers from another SHA. diff --git a/src/components/Fab.tsx b/src/components/Fab.tsx index 15b74b8e42..8fd99024c1 100644 --- a/src/components/Fab.tsx +++ b/src/components/Fab.tsx @@ -7,11 +7,13 @@ interface FabProps { icon: React.ReactNode; onPress: () => void; containerStyle?: object; + // eslint-disable-next-line react/require-default-props -- Omitted test selectors must stay absent from the native view. + testID?: string; } -const Fab = ({ icon, onPress, containerStyle }: FabProps) => { +const Fab = ({ icon, onPress, containerStyle, testID }: FabProps) => { return ( - + {icon} ); diff --git a/src/screens/Home/components/Keys/ManageKeys.tsx b/src/screens/Home/components/Keys/ManageKeys.tsx index 75d4035aca..0aaab4449a 100644 --- a/src/screens/Home/components/Keys/ManageKeys.tsx +++ b/src/screens/Home/components/Keys/ManageKeys.tsx @@ -103,6 +103,7 @@ const ManageKeys = ({ addedSigner }) => { <> } onPress={handleModalOpen} containerStyle={{ right: wp(22.5) }} From 551ce622e94315091e2cf073736c2a5ad8d9ba27 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Thu, 22 Oct 2026 07:21:26 +0000 Subject: [PATCH 251/254] Verify owned installed native artifact bytes before acceptance --- .github/native/hosted_native.py | 113 +++++++++++++++++- .github/tests/test_installed_artifact.py | 146 +++++++++++++++++++++++ .github/tests/test_release_native.py | 3 +- 3 files changed, 259 insertions(+), 3 deletions(-) create mode 100644 .github/tests/test_installed_artifact.py diff --git a/.github/native/hosted_native.py b/.github/native/hosted_native.py index 40a986f8d2..4b9aab1f3f 100644 --- a/.github/native/hosted_native.py +++ b/.github/native/hosted_native.py @@ -649,6 +649,110 @@ def artifact_identity(): return {'path': str(apk.relative_to(ROOT)), 'sha256': hashlib.sha256(apk.read_bytes()).hexdigest()} +def _file_sha256(path): + digest = hashlib.sha256() + with path.open('rb') as source: + for block in iter(lambda: source.read(1024 * 1024), b''): + digest.update(block) + return digest.hexdigest() + + +def _bundle_inventory(bundle): + """Private typed tree; internal links must remain inside this bundle.""" + import plistlib + if bundle.is_symlink() or not bundle.is_dir(): + raise ValueError('Invalid app bundle.') + root = bundle.resolve(strict=True) + with (bundle / 'Info.plist').open('rb') as source: + info = plistlib.load(source) + executable = info['CFBundleExecutable'] + if (info['CFBundleIdentifier'] != APP_IDS['IOS'] or + not re.fullmatch(r'[A-Za-z0-9_][A-Za-z0-9_.-]*', executable)): + raise ValueError('Unexpected app identity.') + tree = {} + for entry in sorted(bundle.rglob('*')): + if not entry.resolve(strict=True).is_relative_to(root): + raise ValueError('Bundle entry escaped app.') + if entry.is_symlink(): + value = ['link', os.readlink(entry)] + elif entry.is_file(): + value = ['file', _file_sha256(entry)] + elif entry.is_dir(): + value = ['directory'] + else: + raise ValueError('Unsupported bundle entry.') + tree[entry.relative_to(bundle).as_posix()] = value + if tree.get(executable, [None])[0] != 'file': + raise ValueError('Missing regular app executable.') + return tree, executable + + +def verify_installed_artifact(device): + """Attest owned installed bytes, never installation argv or app assertions.""" + data = read() + data['installed_artifact_verified'] = False + data['installed_artifact_scope'] = 'NOT VERIFIED: installed bytes require full comparison.' + data.pop('installed_artifact', None) + save(data) + try: + expected = data['built_artifact'] + if PLATFORM == 'IOS': + uuid = r'[A-Fa-f0-9]{8}(?:-[A-Fa-f0-9]{4}){3}-[A-Fa-f0-9]{12}' + if not re.fullmatch(uuid, device): + raise ValueError('Expected owned simulator UUID.') + built = PRIVATE / 'DerivedData/Build/Products/Debug-iphonesimulator/hexa_keeper_dev.app' + original, executable = _bundle_inventory(built) + installed = Path(command(['xcrun', 'simctl', 'get_app_container', device, APP_IDS['IOS'], 'app'], + 'Locate owned installed app bundle', capture=True, error_details=False)) + base = Path.home() / 'Library/Developer/CoreSimulator/Devices' / device / 'data/Containers/Bundle/Application' + if (not installed.is_absolute() or installed.name != built.name or + not re.fullmatch(uuid, installed.parent.name) or + installed.parent.parent.resolve() != base.resolve() or + installed.resolve(strict=True).parent.parent != base.resolve()): + raise ValueError('Unexpected installed app container.') + actual, installed_executable = _bundle_inventory(installed) + if original != actual or executable != installed_executable: + raise ValueError('Installed bundle differs; no installer normalization is accepted.') + result = {'method': 'FULL_SIMULATOR_BUNDLE_TREE_AND_EXECUTABLE_SHA256', + 'inventory_sha256': hashlib.sha256(json.dumps(original, sort_keys=True).encode()).hexdigest(), + 'executable_sha256': actual[executable][1]} + elif PLATFORM == 'ANDROID': + if not re.fullmatch(r'emulator-[0-9]+', device): + raise ValueError('Expected owned emulator serial.') + listing = command(['adb', '-s', device, 'shell', 'pm', 'path', APP_IDS['ANDROID']], + 'Locate exact installed development package', capture=True, error_details=False) + pattern = (r'package:(/data/app/(?:~~[A-Za-z0-9_=-]+/)?' + re.escape(APP_IDS['ANDROID']) + + r'-[A-Za-z0-9_=-]+/base\.apk)') + match = re.fullmatch(pattern, listing) + if not match: + raise ValueError('Expected one exact base APK; split or unexpected paths rejected.') + built = ROOT / 'android/app/build/outputs/apk/development/debug/app-development-debug.apk' + if built.is_symlink() or not built.is_file(): + raise ValueError('Invalid built APK.') + PRIVATE.mkdir(parents=True, exist_ok=True) + with tempfile.TemporaryDirectory(prefix='installed-artifact-', dir=PRIVATE) as temporary: + pulled = Path(temporary) / 'base.apk' + # Package paths and pulled bytes stay private, including tool output. + copied = subprocess.run(['adb', '-s', device, 'pull', match[1], str(pulled)], + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, + timeout=120, check=False) + if (copied.returncode or pulled.is_symlink() or not pulled.is_file() or + pulled.stat().st_size != built.stat().st_size or _file_sha256(pulled) != expected['sha256']): + raise ValueError('Installed APK bytes differ or are unavailable.') + result = {'method': 'SINGLE_INSTALLED_BASE_APK_SHA256', 'sha256': expected['sha256']} + else: + raise ValueError('Unsupported artifact platform.') + if artifact_identity() != expected: + raise ValueError('Built artifact changed during installed comparison.') + except Exception: + raise RuntimeError('Installed artifact bytes were not verified; no equivalence claimed.') from None + data = read() + data.update(installed_artifact_verified=True, installed_artifact=result, + installed_artifact_scope='Full installed artifact byte comparison; no installer transformations accepted.') + save(data) + return result + + def driver(): """Fail early on iOS driver setup without installing or crediting the app.""" if PLATFORM != 'IOS': @@ -891,6 +995,8 @@ def smoke(suite='startup'): 'com.apple.CoreSimulator.SimDeviceType.iPhone-16', 'com.apple.CoreSimulator.SimRuntime.iOS-18-5'], 'Create fresh iPhone 16 iOS 18.5 simulator', capture=True) + if not re.fullmatch(r'[A-Fa-f0-9]{8}(?:-[A-Fa-f0-9]{4}){3}-[A-Fa-f0-9]{12}', device): + raise ValueError('Expected owned simulator UUID before installation.') command(['xcrun', 'simctl', 'boot', device], 'Boot disposable simulator') command(['xcrun', 'simctl', 'bootstatus', device, '-b'], 'Wait for simulator boot', timeout=240) command(['xcrun', 'simctl', 'install', device, @@ -908,6 +1014,8 @@ def smoke(suite='startup'): data['toolchain']['adb'] = adb_version save(data) device = command(['adb', 'get-serialno'], 'Identify fresh emulator', capture=True) + if not re.fullmatch(r'emulator-[0-9]+', device): + raise ValueError('Expected owned emulator before installation.') command(['adb', '-s', device, 'reverse', 'tcp:8081', 'tcp:8081'], 'Connect emulator to Metro') command(['adb', '-s', device, 'install', '-r', 'android/app/build/outputs/apk/development/debug/app-development-debug.apk'], @@ -917,9 +1025,9 @@ def smoke(suite='startup'): data = read() data['device'] = {'id': device, 'image': descriptor, 'fresh_disposable_state': True} data['install_succeeded'] = True - data['installed_artifact_verified'] = False - data['installed_artifact_scope'] = 'Installed from unchanged candidate build path; installed bytes not independently hashed.' save(data) + verify_installed_artifact(device) + data = read() command([data['maestro']['binary'], '--device', device, 'test', '--env', 'APP_ID=' + APP_IDS[PLATFORM], '--flatten-debug-output', '--debug-output', str(PRIVATE / 'maestro-output'), '--format', 'junit', '--output', str(report), '--test-output-dir', str(PRIVATE / 'maestro-output'), @@ -930,6 +1038,7 @@ def smoke(suite='startup'): cases = successful_junit(report) if artifact_identity() != read()['built_artifact']: raise RuntimeError('Built artifact changed during native execution.') + verify_installed_artifact(device) data = read() data[result_key] = 'PASS' data[result_key + '_test_cases'] = cases diff --git a/.github/tests/test_installed_artifact.py b/.github/tests/test_installed_artifact.py new file mode 100644 index 0000000000..17299ab53c --- /dev/null +++ b/.github/tests/test_installed_artifact.py @@ -0,0 +1,146 @@ +"""Host byte/transport controls; no native acceptance credit.""" +import importlib.util +from pathlib import Path +import plistlib +import shutil +import tempfile +import unittest +from unittest import mock + +SPEC = importlib.util.spec_from_file_location( + 'installed_native', Path(__file__).resolve().parents[1] / 'native/hosted_native.py') +h = importlib.util.module_from_spec(SPEC) +SPEC.loader.exec_module(h) + + +class InstalledArtifactTest(unittest.TestCase): + def setUp(self): + temporary = tempfile.TemporaryDirectory() + self.addCleanup(temporary.cleanup) + self.root = Path(temporary.name) + self.private = self.root / 'private' + patch = mock.patch.multiple(h, ROOT=self.root, PRIVATE=self.private, + EVIDENCE=self.root / 'metadata.json', PLATFORM='ANDROID') + patch.start() + self.addCleanup(patch.stop) + apk = self.root / 'android/app/build/outputs/apk/development/debug/app-development-debug.apk' + apk.parent.mkdir(parents=True) + apk.write_bytes(b'PRIVATE_APK') + self.pm = '/data/app/~~ABC==/io.hexawallet.keeper.development-XYZ==/base.apk' + self.seed() + + def seed(self): + h.save({'built_artifact': h.artifact_identity(), 'commands': [], + 'installed_artifact_verified': True, 'installed_artifact': {'stale': True}}) + + def reject(self, device): + with self.assertRaisesRegex(RuntimeError, 'not verified; no equivalence claimed'): + h.verify_installed_artifact(device) + self.assertFalse(h.read()['installed_artifact_verified']) + self.assertNotIn('installed_artifact', h.read()) + self.assertNotIn('PRIVATE_', h.EVIDENCE.read_text()) + + def reply(self, value): + return mock.patch.object(h, 'command', return_value=value) + + def pull(self, content=b'PRIVATE_APK', code=0): + def copy(args, **kwargs): + self.assertEqual(args[:5], ['adb', '-s', 'emulator-5554', 'pull', self.pm]) + self.pulled = Path(args[5]) + if content is not None: + self.pulled.write_bytes(content) + for field in ['stdout', 'stderr']: + self.assertEqual(kwargs[field], h.subprocess.DEVNULL) + return mock.Mock(returncode=code) + return mock.patch.object(h.subprocess, 'run', side_effect=copy) + + def test_android_match_and_private_cleanup(self): + with self.reply('package:' + self.pm) as command, self.pull(): + proof = h.verify_installed_artifact('emulator-5554') + self.assertEqual(command.call_args.args[0][-2:], ['path', h.APP_IDS['ANDROID']]) + self.assertEqual(proof['sha256'], h.artifact_identity()['sha256']) + self.assertTrue(h.read()['installed_artifact_verified']) + self.assertFalse(self.pulled.parent.exists()) + self.assertNotIn(self.pm, h.EVIDENCE.read_text()) + + def test_android_corrupt_missing_failed_pull(self): + for content, code in [(b'PRIVATE_APX', 0), (None, 0), (b'PRIVATE_APK', 1)]: + with self.subTest(content=content), self.reply('package:' + self.pm), self.pull(content, code): + self.reject('emulator-5554') + self.assertFalse(self.pulled.parent.exists()) + + def test_android_invalid_paths_or_splits(self): + for value in ['', self.pm + '\npackage:/data/app/split.apk', + self.pm.replace('development', 'production'), + self.pm.replace('/data/app/', '/sdcard/'), self.pm.replace('base.apk', 'base\\xapk')]: + with self.subTest(value=value), self.reply('package:' + value), mock.patch.object(h.subprocess, 'run') as pull: + self.reject('emulator-5554') + pull.assert_not_called() + + def ios(self): + h.PLATFORM = 'IOS' + self.device = '12345678-1234-1234-1234-123456789ABC' + self.home = self.root / 'home' + self.bundle = self.private / 'DerivedData/Build/Products/Debug-iphonesimulator/hexa_keeper_dev.app' + self.bundle.mkdir(parents=True) + (self.bundle / 'Info.plist').write_bytes(plistlib.dumps( + {'CFBundleIdentifier': h.APP_IDS['IOS'], 'CFBundleExecutable': 'hexa_keeper_dev'})) + (self.bundle / 'hexa_keeper_dev').write_bytes(b'PRIVATE_EXE') + (self.bundle / 'asset').write_bytes(b'PRIVATE_ASSET') + self.app = (self.home / 'Library/Developer/CoreSimulator/Devices' / self.device / + 'data/Containers/Bundle/Application/ABCDEFAB-1234-1234-1234-123456789ABC' / self.bundle.name) + shutil.copytree(self.bundle, self.app) + self.seed() + patch = mock.patch.object(h.Path, 'home', return_value=self.home) + patch.start() + self.addCleanup(patch.stop) + + def test_ios_match(self): + self.ios() + with self.reply(str(self.app)) as command: + proof = h.verify_installed_artifact(self.device) + self.assertEqual(command.call_args.args[0][-2:], [h.APP_IDS['IOS'], 'app']) + self.assertEqual(proof['executable_sha256'], h.artifact_identity()['executable_sha256']) + self.assertTrue(h.read()['installed_artifact_verified']) + self.assertNotIn(str(self.app), h.EVIDENCE.read_text()) + + def test_ios_corrupt_missing_added_bytes(self): + self.ios() + for name, value in [('hexa_keeper_dev', b'PRIVATE_EXX'), ('asset', b'PRIVATE_ASSXT'), + ('asset', None), ('unexpected', b'PRIVATE_EXTRA')]: + with self.subTest(name=name, value=value), self.reply(str(self.app)): + shutil.rmtree(self.app) + shutil.copytree(self.bundle, self.app) + target = self.app / name + target.unlink() if value is None else target.write_bytes(value) + self.reject(self.device) + + def test_ios_foreign_paths_and_escaping_container(self): + self.ios() + for path in [str(self.bundle), str(self.app).replace(self.device, 'FOREIGN'), str(self.app) + '\n' + str(self.app)]: + with self.subTest(path=path), self.reply(path): + self.reject(self.device) + outside = self.root / 'outside' + shutil.move(str(self.app.parent), outside) + self.app.parent.symlink_to(outside, target_is_directory=True) + with self.reply(str(self.app)): + self.reject(self.device) + + def test_ios_unsafe_tree(self): + self.ios() + (self.app / 'asset').unlink() + (self.app / 'asset').symlink_to(self.bundle / 'asset') + with self.reply(str(self.app)): + self.reject(self.device) + (self.bundle / 'Info.plist').write_bytes(plistlib.dumps( + {'CFBundleIdentifier': h.APP_IDS['IOS'], 'CFBundleExecutable': '../outside'})) + with mock.patch.object(h, 'command') as command: + self.reject(self.device) + command.assert_not_called() + + def test_non_owned_device(self): + for platform, device in [('ANDROID', 'physical-serial'), ('IOS', 'booted')]: + h.PLATFORM = platform + with self.subTest(platform=platform), mock.patch.object(h, 'command') as command: + self.reject(device) + command.assert_not_called() diff --git a/.github/tests/test_release_native.py b/.github/tests/test_release_native.py index 86e1779498..efe847e5ca 100644 --- a/.github/tests/test_release_native.py +++ b/.github/tests/test_release_native.py @@ -78,7 +78,7 @@ def command(args, label, **kwargs): (self.private / 'probe-junit.xml').write_text( '' + ('' if report_failure else '') + '') - return 'synthetic-device' + return 'emulator-5554' process = mock.Mock(pid=12345) process.poll.return_value = None @@ -94,6 +94,7 @@ def command(args, label, **kwargs): stack.enter_context(mock.patch.object(native, 'command', side_effect=command)) stack.enter_context(mock.patch.object(native, 'warm_metro_bundle')) stack.enter_context(mock.patch.object(native, 'android_sdk_metadata', return_value={})) + stack.enter_context(mock.patch.object(native, 'verify_installed_artifact')) stack.enter_context(mock.patch.object(native, 'artifact_identity', side_effect=[self.identity, after_identity or self.identity])) native.run_phase('probe') From eee849144ddf706f7786f482b744596cd395a60d Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Thu, 22 Oct 2026 07:22:42 +0000 Subject: [PATCH 252/254] Verify installed bytes around R5 product UI checks --- .github/native/product_key_acceptance.py | 27 ++++++++++++ .github/native/product_measurements.py | 14 ++++-- .github/tests/test_product_key_acceptance.py | 45 ++++++++++++++++---- 3 files changed, 73 insertions(+), 13 deletions(-) diff --git a/.github/native/product_key_acceptance.py b/.github/native/product_key_acceptance.py index 3635bb6b70..eb18b2f0c6 100644 --- a/.github/native/product_key_acceptance.py +++ b/.github/native/product_key_acceptance.py @@ -7,6 +7,7 @@ import json import os from pathlib import Path +import re import shutil import subprocess import threading @@ -23,6 +24,26 @@ ) +def installed_proof(h, device): + proof = h.verify_installed_artifact(device) + method, hashes = { + 'ANDROID': ('SINGLE_INSTALLED_BASE_APK_SHA256', ['sha256']), + 'IOS': ('FULL_SIMULATOR_BUNDLE_TREE_AND_EXECUTABLE_SHA256', + ['inventory_sha256', 'executable_sha256']), + }[h.PLATFORM] + metadata = h.read() + if (not isinstance(proof, dict) or set(proof) != {'method', *hashes} or + proof['method'] != method or + any(not isinstance(proof[field], str) or not re.fullmatch(r'[a-f0-9]{64}', proof[field]) + for field in hashes) or metadata.get('installed_artifact_verified') is not True or + metadata.get('installed_artifact') != proof): + metadata['installed_artifact_verified'] = False + metadata.pop('installed_artifact', None) + h.save(metadata) + raise RuntimeError('PRODUCT_UI_INSTALLED_BYTE_PROOF_REQUIRED') + return proof + + class Receipt: def __init__(self): self.lock = threading.Lock() @@ -123,6 +144,7 @@ def save(): save() try: + installed_before = installed_proof(h, device) server = make_server(receipt) thread = threading.Thread(target=server.serve_forever, daemon=True) thread.start() @@ -136,11 +158,16 @@ def save(): h.successful_junit(report) if h.artifact_identity() != data['built_artifact']: raise RuntimeError('PRODUCT_KEY_UI_ARTIFACT_CHANGED') + installed_after = installed_proof(h, device) + if installed_after != installed_before: + raise RuntimeError('PRODUCT_KEY_UI_INSTALLED_BYTES_CHANGED') after = backend_receipt.snapshot(passed=True) if after['create_new_app_calls'] != before['create_new_app_calls']: raise RuntimeError('PRODUCT_KEY_UI_UNEXPECTED_ACCOUNT_SETUP') result = receipt.snapshot(passed=True) result['new_account_requests_during_key_flow'] = 0 + result['installed_artifact_verified'] = True + result['installed_artifact'] = installed_after except Exception as error: result = receipt.snapshot() result['failure_category'] = type(error).__name__ diff --git a/.github/native/product_measurements.py b/.github/native/product_measurements.py index cacc2e6f35..67fdd68f53 100644 --- a/.github/native/product_measurements.py +++ b/.github/native/product_measurements.py @@ -294,6 +294,10 @@ def save(): ['adb', '-s', device, 'shell', 'getprop', 'ro.build.fingerprint'], 'Record actual product UI emulator image', capture=True)} app = h.APP_IDS[h.PLATFORM] + spec = importlib.util.spec_from_file_location( + 'product_key_acceptance', Path(__file__).with_name('product_key_acceptance.py')) + key_companion = importlib.util.module_from_spec(spec) + spec.loader.exec_module(key_companion) for sample in range(1, SAMPLE_COUNT + 2): retry_fixture = sample == SAMPLE_COUNT + 1 receipt.reset(sample, retry=retry_fixture) @@ -311,6 +315,7 @@ def save(): h.command(['adb', '-s', device, 'install', '-r', artifact], 'Install exact product UI APK', timeout=120) h.command(['adb', '-s', device, 'shell', 'pm', 'clear', app], 'Clear only disposable development app') try: + installed_before = key_companion.installed_proof(h, device) h.command([data['maestro']['binary'], '--device', device, 'test', '--env', 'APP_ID=' + app, '--env', 'EXPECTED_INSTALLED_REGEX=' + expected_version['installed_regex'], @@ -325,7 +330,12 @@ def save(): h.successful_junit(report) if h.artifact_identity() != data['built_artifact']: raise RuntimeError('PRODUCT_UI_ARTIFACT_CHANGED') + installed_after = key_companion.installed_proof(h, device) + if installed_before != installed_after: + raise RuntimeError('PRODUCT_UI_INSTALLED_BYTES_CHANGED') outcome = receipt.snapshot(passed=True) + outcome['installed_artifact_verified'] = True + outcome['installed_artifact'] = installed_after if retry_fixture: result['controlled_disconnect_retry'] = outcome else: @@ -343,10 +353,6 @@ def save(): save() # Separate normal-app UI companion; never part of the five latency # samples or the controlled setup-disconnect receipt vocabulary. - spec = importlib.util.spec_from_file_location( - 'product_key_acceptance', Path(__file__).with_name('product_key_acceptance.py')) - key_companion = importlib.util.module_from_spec(spec) - spec.loader.exec_module(key_companion) result['key_management_ui'] = key_companion.run_companion(h, data, device, private, receipt) result['key_management_scope'] = ( 'Normal-app unused Mobile Key creation/deletion, cancellation, wrong PIN rejection, ' diff --git a/.github/tests/test_product_key_acceptance.py b/.github/tests/test_product_key_acceptance.py index 74841ad4f7..67568db378 100644 --- a/.github/tests/test_product_key_acceptance.py +++ b/.github/tests/test_product_key_acceptance.py @@ -71,7 +71,9 @@ def test_reviewed_flow_has_every_receipt_mark_and_no_export_or_credential_inject class RunnerTests(unittest.TestCase): def exercise(self, *, omit_last=False, artifact_changed=False, command_failure=False, - source_dirty=False, device='emulator-5554', extra_create=False): + source_dirty=False, device='emulator-5554', extra_create=False, + installed_failure=False, installed_mismatch=False, installed_unverified=False, + installed_private_extra=False): directory = tempfile.TemporaryDirectory() self.addCleanup(directory.cleanup) root = Path(directory.name) @@ -107,9 +109,30 @@ def server(receipt): identity = mock.Mock(side_effect=[artifact, {'sha256': 'changed'}] if artifact_changed else None, return_value=artifact) + installed_calls = 0 + + def verify_installed(device): + nonlocal installed_calls + installed_calls += 1 + state['installed_artifact_verified'] = False + state.pop('installed_artifact', None) + if installed_failure and installed_calls == 2: + raise RuntimeError('PRIVATE_SENTINEL') + proof = {'method': 'SINGLE_INSTALLED_BASE_APK_SHA256', + 'sha256': ('e' if installed_mismatch and installed_calls == 2 else 'f') * 64} + if installed_private_extra: + proof['unreviewed'] = 'PRIVATE_SENTINEL' + state['installed_artifact'] = copy.deepcopy(proof) + state['installed_artifact_verified'] = not installed_unverified + return proof + + def save(value): + state.clear() + state.update(copy.deepcopy(value)) + h = types.SimpleNamespace(ROOT=root, PLATFORM='ANDROID', APP_IDS={'ANDROID': 'disposable.app'}, - read=lambda: copy.deepcopy(state), save=lambda value: state.update(copy.deepcopy(value)), - artifact_identity=identity, command=command, successful_junit=lambda report: 1) + read=lambda: copy.deepcopy(state), save=save, artifact_identity=identity, + verify_installed_artifact=verify_installed, command=command, successful_junit=lambda report: 1) backend = types.SimpleNamespace(snapshot=backend_snapshot) with contextlib.ExitStack() as stack: stack.enter_context(mock.patch.dict(product.os.environ, {'GITHUB_ACTIONS': 'true', @@ -120,22 +143,26 @@ def server(receipt): stack.enter_context(mock.patch.object(product, 'make_server', side_effect=server)) try: value = product.run_companion(h, state, device, private, backend) - return value, state, private + return value, state, private, installed_calls except Exception as error: - return error, state, private + return error, state, private, installed_calls def test_complete_flow_keeps_scope_separate_and_removes_all_private_ui_output(self): - value, state, private = self.exercise() + value, state, private, installed_calls = self.exercise() self.assertEqual(value['status'], 'PASS') self.assertEqual(value['new_account_requests_during_key_flow'], 0) self.assertEqual(value['physical_biometrics'], 'NOT RUN') + self.assertEqual(installed_calls, 2) + self.assertTrue(value['installed_artifact_verified']) + self.assertEqual(value['installed_artifact']['sha256'], 'f' * 64) self.assertFalse((private / 'key-ui').exists()) self.assertNotIn('PRIVATE_SENTINEL', json.dumps(state)) def test_missing_stage_artifact_drift_driver_failure_and_new_setup_each_fail_closed(self): - for option in ['omit_last', 'artifact_changed', 'command_failure', 'extra_create']: + for option in ['omit_last', 'artifact_changed', 'command_failure', 'extra_create', + 'installed_failure', 'installed_mismatch', 'installed_unverified', 'installed_private_extra']: with self.subTest(option=option): - value, state, private = self.exercise(**{option: True}) + value, state, private, _calls = self.exercise(**{option: True}) self.assertIsInstance(value, RuntimeError) self.assertEqual(state['product_key_ui']['status'], 'FAIL') self.assertFalse((private / 'key-ui').exists()) @@ -144,7 +171,7 @@ def test_missing_stage_artifact_drift_driver_failure_and_new_setup_each_fail_clo def test_real_device_or_dirty_source_is_rejected_before_ui_execution(self): for options in [{'device': 'physical-device-id'}, {'source_dirty': True}]: with self.subTest(options=options): - value, state, private = self.exercise(**options) + value, state, private, _calls = self.exercise(**options) self.assertIsInstance(value, RuntimeError) self.assertNotIn('product_key_ui', state) self.assertFalse((private / 'key-ui').exists()) From 3eb92fc8a622a3e09926d49f47243c163e32d009 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Thu, 22 Oct 2026 17:36:01 +0000 Subject: [PATCH 253/254] docs(r5): pin native UI and installed byte acceptance inputs --- docs/r5-cloud-integration.md | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/docs/r5-cloud-integration.md b/docs/r5-cloud-integration.md index da27c95792..1753f35262 100644 --- a/docs/r5-cloud-integration.md +++ b/docs/r5-cloud-integration.md @@ -74,6 +74,15 @@ incomplete sanitization finding at `src/services/wallets/operations/index.ts`. No independent review or severity waiver is established. Lint jobs tolerate command failures; green jobs do not mean lint is clean. +Read-only alert triage confirmed the flagged module blob +`db29ee432496e84bc70be82b0d9a3017e154888d` is identical in released R4, core, +5.1 and the inspected interim candidate. The flagged string feeds a numeric +witness-size estimate; no HTML sink was established in the traced callers. +An uncommitted literal-selector counting candidate matched 140 pinned fixture +witnesses and 104 production-helper comparisons. This is bounded triage, not +an exploitability clearance or signing-security review. No financial-source +refactor, alert dismissal or waiver is included; the failed check remains open. + At initial integration there is no combined native acceptance, no five raw measurements and no identified 5.2 checkpoint. Candidate freeze is blocked until the authoritative 5.2 handoff is rechecked and all package results are recorded. @@ -93,9 +102,11 @@ post-test evidence update and silently transfer results to the resulting SHA. | Cloud reconciliation | #7070 / `17baf3bff3a5cd96787218b9e8e1fee75f549967`; inputs #7060/#7063/#7062 | Included with original patch review links; installed-base trust and required branch protections remain separate from diagnostic execution | | Native production-worker witness | #7071 / `486810052f90550d55dd6ddd541ff53ebd875caa` | Included; actual Realm/MMKV worker scenarios, explicitly controlled generation/transport/dispatch; execute on exact combined source | | Full-product UI measurements | #7073 / `040955d0caca38bb3cb386ca2918adf60f0f30db` | Included; require five healthy numeric samples plus a separate failed-transport/explicit-Retry fixture, actual normal app and cold relaunch/counts/navigation; no helper timing substitution or WAN-offline claim | +| Native key/PIN UI companion | #7079 / `5c4c211339aeee3216faa0663c309314a1627123` | Included; normal app unused Mobile Key creation/deletion, cancellation, wrong-PIN rejection/retry and cold persistence. Seven independent controls pass; referenced-Vault UI, credential-store read faults and real biometrics are not covered | +| Installed native bytes | `551ce622e94315091e2cf073736c2a5ad8d9ba27`; UI wiring `eee849144ddf706f7786f482b744596cd395a60d` | Included; full owned iOS bundle/executable and single Android installed-APK comparison before/after probes and product UI. Eight independent file/transport controls pass; actual native verification remains unrun until this candidate executes. Installer transformations do not receive an equivalence waiver | | Confirmed biometric callback defects | #7069 / `257fbe17e1a85d0e5d7e5d8ab97e1a69edbd9628`, review correction `14a2459b30e31ea3099d56f6c6928a4a3b7d6995` | Included; independent review reproduced prompt repetition and stale epoch cases; 17 component controls pass, actual biometrics remain unrun | | Current protocol pairing | Private Relay #160 / `4db6a758a307111137ad6bb4f35a32fa15b17a9d` | 52 backend + 43 protected app + 1 upgraded-account + 3 guard cases pass at their exact documented pair; pin/retest combined app SHA | -| Protected release preparation | #7072 / `f2fc0cb557f0e5303335dadc9e98152640795e6c`; origin correction #7074 / `551198c6a5d55d05b5ecd2cae57505ca69270264` | Included; inventory and plan controls implemented, authorization planning deliberately disabled until installed CI origin is verified. Review reproduced forged check-name acceptance; 21 corrected plan controls pass. Separate future workers/protected setup remain, with no signing/upload executed | +| Protected release preparation | #7072 / `f2fc0cb557f0e5303335dadc9e98152640795e6c`; origin correction #7074 / `76150f455978c1a565f2c670774672bc18789877` | Included; inventory and plan controls implemented, authorization planning deliberately disabled until installed CI origin is verified. Review reproduced forged check-name acceptance; 21 corrected plan controls pass with specific rejection-code assertions. Separate future workers/protected setup remain, with no signing/upload executed | | PHP exposure research | #7072, `docs/r5-cloud-release-readiness.md` | Official advisories/source assessment documented; deployed runtime inventory unknown, no Keeper exposure assertion; remedies stay R6 | | Navigation/timezone/PIN/copy | `docs/r5/BIOMETRIC-LIFECYCLE.md` and source tests | No speculative #7055/#6991 fix; bounded native reproduction/device checks remain. Retain shipped PIN and scoped EN/ES corrections | | Read-only timelock assessor | Separately recorded candidate, not identified as 5.2 | Source/handoff and complete key/origin/script/intent binding and native isolation acceptance remain; no activation or wallet clearance | @@ -107,7 +118,7 @@ the existing packages, much larger than an older 19k estimate. The initial new reconciliation was about 6.7k, with a small additional runner hook. Preserve the original #7060/#7063/#7062 reviews and the reconciliation-only comparison; this aggregate is an integration input, not a request to approve one new 54k patch. -Native worker (~7.8k), product measurements (~10.1k), biometric correction (~4k) +Native worker (~7.8k), product measurements (~10.1k), key/PIN UI (~6.5k), biometric correction (~4k) and release preparation (~7.2k code/test, ~12.7k including research/docs) stay separate. Signing worker preparation grew during independent review and is split into bounded core authorization/platform adapters and separate orchestration/ From 87d21fa34ee7d55e2b49c91ccb83bc9a1c1346c7 Mon Sep 17 00:00:00 2001 From: Antuz tap Date: Thu, 22 Oct 2026 18:58:07 +0000 Subject: [PATCH 254/254] ci(r5): add private Android startup diagnosis metadata --- .github/native/android_startup_diagnosis.py | 274 ++++++++++++++++++ .github/native/hosted_native.py | 8 + .../tests/test_android_startup_diagnosis.py | 231 +++++++++++++++ docs/r5-android-startup-diagnosis.md | 60 ++++ 4 files changed, 573 insertions(+) create mode 100644 .github/native/android_startup_diagnosis.py create mode 100644 .github/tests/test_android_startup_diagnosis.py create mode 100644 docs/r5-android-startup-diagnosis.md diff --git a/.github/native/android_startup_diagnosis.py b/.github/native/android_startup_diagnosis.py new file mode 100644 index 0000000000..487cb040b1 --- /dev/null +++ b/.github/native/android_startup_diagnosis.py @@ -0,0 +1,274 @@ +"""Read-only Android startup facts. No raw device or application text escapes.""" +from pathlib import Path +import re +import shutil +import subprocess + +APP = 'io.hexawallet.keeper.development' +LIMIT = 256 * 1024 +TYPES = ('TypeError', 'SyntaxError', 'ReferenceError', 'RangeError', 'EvalError', + 'URIError', 'AggregateError', 'TransformError', 'UnableToResolveError', + 'InternalError', 'RuntimeException', 'IllegalStateException', + 'IllegalArgumentException', 'NullPointerException', 'SecurityException', + 'UnsatisfiedLinkError', 'NoClassDefFoundError', 'ClassNotFoundException', + 'OutOfMemoryError', 'Error') +FAULTS = { + 'JS_TYPE_ERROR': r'\bTypeError\b|undefined is not a function', + 'JS_SYNTAX_ERROR': r'\bSyntaxError\b', + 'JS_REFERENCE_ERROR': r'\bReferenceError\b', + 'JS_RANGE_ERROR': r'\bRangeError\b', + 'MODULE_RESOLUTION_ERROR': r'Unable to resolve module|\bUnableToResolveError\b', + 'TRANSFORM_ERROR': r'\bTransformError\b', + 'APP_NOT_REGISTERED': r'has not been registered', + 'NATIVE_MODULE_UNAVAILABLE': r'TurboModuleRegistry\.getEnforcing|requireNativeComponent|native module cannot be null', + 'NATIVE_LIBRARY_LOAD_ERROR': r'\bUnsatisfiedLinkError\b|dlopen failed|could not load library', + 'NATIVE_FATAL_SIGNAL': r'Fatal signal \d+\b', + 'JAVA_FATAL_EXCEPTION': r'FATAL EXCEPTION:', + 'MEMORY_EXHAUSTION': r'\bOutOfMemoryError\b|out of memory', + 'TRANSPORT_ERROR': r'\b(?:EADDRINUSE|ECONNREFUSED|ECONNRESET)\b', + 'FILESYSTEM_ERROR': r'\b(?:ENOENT|EACCES|ENOSPC)\b', + 'WORKER_HEAP_EXHAUSTION': r'JavaScript heap out of memory|Reached heap limit|Ineffective mark-compacts', + 'PROCESS_KILLED_MARKER': r'\bSIGKILL\b|worker process.*terminated|worker.*killed', +} +EXIT_REASONS = {0: 'UNKNOWN', 1: 'EXIT_SELF', 2: 'SIGNALED', 3: 'LOW_MEMORY', + 4: 'JAVA_CRASH', 5: 'NATIVE_CRASH', 6: 'ANR', 7: 'INITIALIZATION_FAILURE', + 8: 'PERMISSION_CHANGE', 9: 'EXCESSIVE_RESOURCE_USAGE', + 10: 'USER_REQUESTED', 11: 'USER_STOPPED', 12: 'DEPENDENCY_DIED', + 13: 'OTHER', 14: 'FREEZER', 15: 'PACKAGE_STATE_CHANGE', 16: 'PACKAGE_UPDATED'} + + +def safe_error(error): + name = type(error).__name__ + return name if name in {'OSError', 'FileNotFoundError', 'PermissionError', + 'TimeoutExpired', 'ValueError', 'TypeError'} else 'OTHER_ERROR' + + +def bounded(path, private): + if path.is_symlink() or not path.resolve().is_relative_to(private.resolve()): + raise ValueError('Expected a private regular file.') + size = path.stat().st_size + with path.open('rb') as source: + prefix = source.read(LIMIT) + tail = b'' + if size > LIMIT: + source.seek(max(LIMIT, size - LIMIT)) + tail = source.read(LIMIT) + truncated = size > len(prefix) + len(tail) + if truncated: + prefix = prefix.rsplit(b'\n', 1)[0] if b'\n' in prefix else b'' + tail = tail.split(b'\n', 1)[1] if b'\n' in tail else b'' + return (prefix + (b'\n' if truncated else b'') + tail).decode(errors='replace'), { + 'bytes_observed': size, 'bytes_read': len(prefix) + len(tail), + 'input_truncated': truncated, 'read_scope': 'PREFIX_AND_TAIL' if truncated else 'FULL'} + + +def faults(payload): + return {'exception_types': [kind for kind in TYPES if re.search(r'\b' + kind + r'\b', payload)], + 'fault_categories': [name for name, pattern in FAULTS.items() + if re.search(pattern, payload, re.IGNORECASE)]} + + +def processes(payload, usable=True): + lines = [line.strip() for line in payload.splitlines() if line.strip()] + result = {'state': 'UNKNOWN', 'matching_process_count': None, 'parsed_records': 0, + 'unparsed_records': 0} + owned = set() + seen = set() + if not usable or not lines or lines[0].split() != ['PID', 'NAME']: + return result, owned + for line in lines[1:]: + match = re.fullmatch(r'([1-9]\d*)\s+(\S+)', line) + if not match or int(match[1]) in seen: + result['unparsed_records'] += 1 + continue + seen.add(int(match[1])) + result['parsed_records'] += 1 + if match[2] == APP or match[2].startswith(APP + ':'): + owned.add(int(match[1])) + if result['unparsed_records'] == 0 and result['parsed_records'] > 0: + result.update(state='PRESENT' if owned else 'ABSENT', matching_process_count=len(owned)) + else: + owned.clear() # Malformed inventory cannot establish process attribution. + return result, owned + + +def foreground(payload, usable=True): + relations = set() + parsed, unparsed = 0, 0 + if usable: + for line in payload.splitlines(): + if not re.search(r'\b(?:topResumedActivity|mResumedActivity)\s*[:=]', line): + continue + if re.search(r'[:=]\s*(?:null|None)\s*$', line): + relations.add('NONE') + parsed += 1 + elif match := re.search(r'ActivityRecord\{.*?\bu\d+\s+([A-Za-z0-9_.]+)/(?:[^\s}]+)', line): + relations.add('OWN_APP' if match[1] == APP else 'OTHER_APP') + parsed += 1 + else: + unparsed += 1 + return {'relation': next(iter(relations)) if len(relations) == 1 and not unparsed else 'UNKNOWN', + 'parsed_records': parsed, 'unparsed_records': unparsed, + 'scope': 'RESUMED_ACTIVITY_ONLY_NOT_WINDOW_VISIBILITY'} + + +def exit_history(payload, usable=True): + reasons = [] + matched = 0 + if usable: + for line in payload.splitlines(): + match = re.search(r'\bprocess=(\S+)\s+reason=(\d+)\s', line) + if match and (match[1] == APP or match[1].startswith(APP + ':')): + matched += 1 + reason = EXIT_REASONS.get(int(match[2]), 'UNRECOGNIZED_REASON') + if reason not in reasons: + reasons.append(reason) + return {'matching_records': matched, 'reason_categories': reasons, + 'parse_status': 'PARSED' if matched else 'UNKNOWN', + 'freshness': 'UNKNOWN', 'scope': 'APP_HISTORY_NOT_PROOF_OF_CURRENT_FAILURE_CAUSE'} + + +def android_log(payload, owned): + result = {'parsed_error_records': 0, 'own_error_records': 0, 'unclassified_own_error_records': 0, + 'unparsed_nonempty_lines': 0, 'exception_types': [], 'fault_categories': [], + 'attribution': 'CURRENT_OWN_PID_MATCH_HISTORY_FRESHNESS_UNKNOWN' if owned + else 'UNKNOWN_NO_CURRENT_OWN_PROCESS'} + pattern = r'^\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d+\s+(\d+)\s+\d+\s+[EF]\s+[^:]+:\s*(.*)$' + for line in payload.splitlines(): + match = re.match(pattern, line) + if not match: + if line.strip() and not line.startswith('--------- beginning of '): + result['unparsed_nonempty_lines'] += 1 + continue + result['parsed_error_records'] += 1 + if int(match[1]) not in owned: + continue + result['own_error_records'] += 1 + observed = faults(match[2]) + if not any(observed.values()): + result['unclassified_own_error_records'] += 1 + for key in observed: + result[key] = list(dict.fromkeys(result[key] + observed[key])) + return result + + +def metro(payload): + selected = [] + headers, remaining = 0, 0 + for raw in payload.splitlines(): + line = re.sub(r'\x1b\[[0-9;]*[A-Za-z]', '', raw).strip() + if re.match(r'^(?:ERROR\b|FATAL ERROR:|(?:\w*Error):|Unable to resolve module)', line, re.IGNORECASE): + headers += 1 + remaining = 4 + elif remaining: + remaining -= 1 + else: + continue + if re.match(r'^(?:(?:>\s*)?\d+\s*\||\^|at\s)', line): + continue + selected.append(line) + return {'observed_error_headers': headers, 'scanned_error_context_lines': len(selected), + 'unclassified_error_context_lines': sum(not faults(line)['fault_categories'] for line in selected), + **faults('\n'.join(selected)), 'scope': 'ERROR_MARKERS_NOT_BUNDLE_OR_STARTUP_ACCEPTANCE'} + + +def host_memory(root=Path('/')): + result = {'scope': 'HOST_SNAPSHOT_AND_HISTORICAL_CGROUP_EVENTS_NOT_FAILURE_CAUSE', + 'meminfo': {'lookup_status': 'UNKNOWN'}, 'cgroup': {}} + + def small(path): + with path.open('rb') as source: + value = source.read(8193) + if len(value) > 8192: + raise ValueError('Memory record exceeded size limit.') + return value.decode('ascii').strip() + + try: + payload = small(root / 'proc/meminfo') + matches = re.findall(r'^(MemTotal|MemAvailable|SwapTotal|SwapFree):\s+(\d+) kB$', payload, re.MULTILINE) + fields = dict(matches) + recognized = sum(bool(re.match(r'^(?:MemTotal|MemAvailable|SwapTotal|SwapFree)\b', line)) + for line in payload.splitlines()) + if len(fields) == len(matches) == recognized == 4: + result['meminfo'] = {'lookup_status': 'SUCCESS', + 'bytes': {name: int(value) * 1024 for name, value in fields.items()}} + except Exception as error: + result['meminfo']['error_type'] = safe_error(error) + for name in ['memory.current', 'memory.max', 'memory.events']: + observed = {'lookup_status': 'UNKNOWN'} + try: + value = small(root / 'sys/fs/cgroup' / name) + if name == 'memory.events': + matches = re.findall(r'^(oom|oom_kill) (\d+)$', value, re.MULTILINE) + events = dict(matches) + recognized = sum(bool(re.match(r'^(?:oom|oom_kill)\b', line)) for line in value.splitlines()) + if len(events) == len(matches) == recognized == 2: + observed = {'lookup_status': 'SUCCESS', 'counts': {k: int(v) for k, v in events.items()}} + elif value.isascii() and value.isdecimal(): + observed = {'lookup_status': 'SUCCESS', 'bytes': int(value)} + elif name == 'memory.max' and value == 'max': + observed = {'lookup_status': 'SUCCESS', 'limit': 'UNLIMITED'} + except Exception as error: + observed['error_type'] = safe_error(error) + result['cgroup'][name] = observed + return result + + +def collect(private, device): + private = Path(private) + if not re.fullmatch(r'emulator-\d+', device): + raise ValueError('Expected the owned emulator selector.') + work = private / 'android-startup-diagnosis' + work.mkdir(mode=0o700) + result = {'diagnostic_only': True, 'cause': 'UNDETERMINED', 'host_memory': host_memory(), 'commands': [], + 'collection_errors': [], 'cleanup_status': 'UNKNOWN'} + + def capture(label, arguments): + path = work / (label + '.log') + facts = {'label': label, 'lookup_status': 'UNKNOWN', 'exit_code': None, + 'bytes_observed': None, 'bytes_read': 0, 'input_truncated': None, 'read_scope': 'NOT_READ'} + payload = '' + try: + with path.open('xb') as output: + proc = subprocess.run(['adb', '-s', device, *arguments], stdout=output, + stderr=subprocess.STDOUT, timeout=15, check=False) + facts['exit_code'] = proc.returncode + payload, observed = bounded(path, private) + facts.update(observed) + if proc.returncode == 0: + facts['lookup_status'] = 'SUCCESS' + else: + payload = '' + except Exception as error: + facts['error_type'] = safe_error(error) + payload = '' + finally: + path.unlink(missing_ok=True) + result['commands'].append(facts) + return payload, facts.get('lookup_status') == 'SUCCESS' and not facts.get('input_truncated', True) + + try: + payload, usable = capture('processes', ['shell', 'ps', '-A', '-o', 'PID,NAME']) + result['processes'], owned = processes(payload, usable) + payload, usable = capture('foreground', ['shell', 'dumpsys', 'activity', 'activities']) + result['foreground_activity'] = foreground(payload, usable) + payload, usable = capture('exit-history', ['shell', 'dumpsys', 'activity', 'exit-info', APP]) + result['exit_history'] = exit_history(payload, usable) + payload, _ = capture('runtime-errors', ['logcat', '-d', '-b', 'main', '-b', 'crash', + '-t', '1200', '-v', 'threadtime', '*:E']) + result['runtime_errors'] = android_log(payload, owned) + result['runtime_errors'].update( + lookup_status=result['commands'][-1]['lookup_status'], + input_truncated=result['commands'][-1]['input_truncated']) + try: + payload, observed = bounded(private / 'metro.log', private) + result['metro'] = {'lookup_status': 'SUCCESS', **observed, **metro(payload)} + except Exception as error: + result['collection_errors'].append({'stage': 'metro', 'error_type': safe_error(error)}) + finally: + try: + shutil.rmtree(work) + result['cleanup_status'] = 'DELETED' + except Exception as error: + result['collection_errors'].append({'stage': 'cleanup', 'error_type': safe_error(error)}) + return result diff --git a/.github/native/hosted_native.py b/.github/native/hosted_native.py index 4b9aab1f3f..22cf48dc5f 100644 --- a/.github/native/hosted_native.py +++ b/.github/native/hosted_native.py @@ -6,6 +6,7 @@ """ import hashlib +import importlib.util import json import math import os @@ -377,6 +378,13 @@ def android_details(): return {'command': args, 'exit_code': code, 'errors': android_startup_errors(private_text(path)) if code == 0 else []} collect('android_errors', android_details) + def android_runtime_details(): + spec = importlib.util.spec_from_file_location( + 'android_startup_diagnosis', Path(__file__).with_name('android_startup_diagnosis.py')) + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module.collect(PRIVATE, device) + collect('android_runtime_diagnosis', android_runtime_details) elif PLATFORM == 'IOS' and device: def ios_details(): executable = read()['built_artifact']['executable'] diff --git a/.github/tests/test_android_startup_diagnosis.py b/.github/tests/test_android_startup_diagnosis.py new file mode 100644 index 0000000000..9e407aff3f --- /dev/null +++ b/.github/tests/test_android_startup_diagnosis.py @@ -0,0 +1,231 @@ +"""Synthetic metadata/privacy controls; no Android execution or startup credit.""" +from contextlib import redirect_stdout +import importlib.util +import io +import json +from pathlib import Path +import subprocess +import tempfile +import unittest +from unittest import mock + +NATIVE = Path(__file__).resolve().parents[1] / 'native' + + +def load(name, filename): + spec = importlib.util.spec_from_file_location(name, NATIVE / filename) + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module + + +diag = load('android_diagnosis_test', 'android_startup_diagnosis.py') +native = load('hosted_android_diagnosis_test', 'hosted_native.py') +APP = diag.APP + + +class ParserTests(unittest.TestCase): + def test_fixed_types_and_fault_categories_never_export_unknown_names_or_messages(self): + for kind in diag.TYPES: + observed = diag.faults(kind + ': PRIVATE_SENTINEL /private/wallet seed=x') + self.assertIn(kind, observed['exception_types']) + self.assertNotIn('PRIVATE_SENTINEL', json.dumps(observed)) + self.assertEqual(diag.faults('PrivateWalletError: PRIVATE_SENTINEL')['exception_types'], []) + + def test_metro_context_recognizes_new_types_without_codeframes_or_bundle_inference(self): + result = diag.metro('ERROR PRIVATE_SENTINEL\n /private/file: TypeError: PRIVATE_SENTINEL\n' + ' 12 | ReferenceError: PRIVATE_SENTINEL\n' + ' at /private/RangeError:12\nRangeError: PRIVATE_SENTINEL\n' + 'TransformError: PRIVATE_SENTINEL\nBUNDLE PRIVATE_SENTINEL') + self.assertEqual(result['observed_error_headers'], 3) + self.assertEqual(result['exception_types'], ['TypeError', 'RangeError', 'TransformError']) + self.assertNotIn('PRIVATE_SENTINEL', json.dumps(result)) + self.assertNotIn('PASS', json.dumps(result)) + source = diag.metro('ERROR PRIVATE_SENTINEL\n> 14 | throw new TypeError("PRIVATE_SENTINEL")\n') + self.assertEqual(source['exception_types'], []) + self.assertEqual(source['fault_categories'], []) + oom = diag.metro('FATAL ERROR: Reached heap limit PRIVATE_SENTINEL') + self.assertEqual(oom['fault_categories'], ['WORKER_HEAP_EXHAUSTION']) + + def test_inventory_distinguishes_actual_absence_from_failed_malformed_or_partial_lookup(self): + data, owned = diag.processes('PID NAME\n100 ' + APP + '\n101 ' + APP + ':worker\n102 private.other\n') + self.assertEqual((data['state'], data['matching_process_count']), ('PRESENT', 2)) + self.assertEqual(owned, {100, 101}) + data, _ = diag.processes('PID NAME\n102 ' + APP + '.other\n') + self.assertEqual(data['state'], 'ABSENT') + for payload, usable in [('', True), ('PID NAME', True), ('permission denied', True), + ('PID NAME\n100 ' + APP + '\nmalformed', True), + ('PID NAME\n100 ' + APP + '\n100 private.other', True), + ('PID NAME\n100 ' + APP, False)]: + with self.subTest(payload=payload): + data, owned = diag.processes(payload, usable) + self.assertEqual(data['state'], 'UNKNOWN') + self.assertIsNone(data['matching_process_count']) + self.assertEqual(owned, set()) + + def test_resumed_activity_is_unknown_on_missing_failed_ambiguous_or_malformed_output(self): + own = 'topResumedActivity=ActivityRecord{abc u0 ' + APP + '/.MainActivity t1}' + other = 'mResumedActivity: ActivityRecord{def u0 private.other/.Private t2}' + self.assertEqual(diag.foreground(own)['relation'], 'OWN_APP') + self.assertEqual(diag.foreground(other)['relation'], 'OTHER_APP') + self.assertEqual(diag.foreground('topResumedActivity=null')['relation'], 'NONE') + for payload, usable in [('', True), (own, False), (own + '\n' + other, True), + (own + '\nmResumedActivity: PRIVATE_SENTINEL', True)]: + self.assertEqual(diag.foreground(payload, usable)['relation'], 'UNKNOWN') + self.assertNotIn('private.other', json.dumps(diag.foreground(other))) + + def test_android_log_requires_current_own_process_and_counts_unclassified_records(self): + payload = ('10-22 12:00:00.000 100 101 E ReactNativeJS: undefined is not a function PRIVATE_SENTINEL\n' + '10-22 12:00:00.001 100 101 F libc: Fatal signal 11 PRIVATE_SENTINEL\n' + '10-22 12:00:00.002 100 101 E NewPrivateTag: PRIVATE_SENTINEL\n' + '10-22 12:00:00.003 200 201 E ReactNativeJS: RangeError: PRIVATE_SENTINEL\n' + 'malformed PRIVATE_SENTINEL\n') + result = diag.android_log(payload, {100}) + self.assertEqual(result['parsed_error_records'], 4) + self.assertEqual(result['own_error_records'], 3) + self.assertEqual(result['unclassified_own_error_records'], 1) + self.assertEqual(result['unparsed_nonempty_lines'], 1) + self.assertEqual(result['fault_categories'], ['JS_TYPE_ERROR', 'NATIVE_FATAL_SIGNAL']) + self.assertEqual(result['exception_types'], []) + unknown = diag.android_log(payload, set()) + self.assertEqual(unknown['fault_categories'], []) + self.assertEqual(unknown['attribution'], 'UNKNOWN_NO_CURRENT_OWN_PROCESS') + self.assertNotIn('PRIVATE_SENTINEL', json.dumps(result)) + self.assertNotIn('NewPrivateTag', json.dumps(result)) + + def test_exit_reasons_are_app_scoped_history_with_unknown_freshness_not_current_cause(self): + payload = ('process=' + APP + ' reason=5 (APP CRASH(NATIVE)) status=11 PRIVATE_SENTINEL\n' + 'process=' + APP + ':worker reason=777 (PRIVATE_SENTINEL)\n' + 'process=private.other reason=6 (ANR)\n') + result = diag.exit_history(payload) + self.assertEqual(result['matching_records'], 2) + self.assertEqual(result['reason_categories'], ['NATIVE_CRASH', 'UNRECOGNIZED_REASON']) + self.assertEqual(result['freshness'], 'UNKNOWN') + self.assertEqual(diag.exit_history(payload, False)['reason_categories'], []) + self.assertNotIn('PRIVATE_SENTINEL', json.dumps(result)) + + +class CollectionTests(unittest.TestCase): + def setUp(self): + self.directory = tempfile.TemporaryDirectory() + self.addCleanup(self.directory.cleanup) + self.root = Path(self.directory.name) + self.private = self.root / 'private' + self.private.mkdir() + (self.private / 'metro.log').write_text('ERROR RangeError: PRIVATE_SENTINEL\n') + + def responder(self, args, stdout, **kwargs): + if 'ps' in args: + payload = 'PID NAME\n100 ' + APP + '\n200 private.other\n' + elif 'activities' in args: + payload = 'topResumedActivity=ActivityRecord{abc u0 ' + APP + '/.MainActivity t1}\n' + elif 'exit-info' in args: + payload = 'process=' + APP + ' reason=5 (APP CRASH(NATIVE)) PRIVATE_SENTINEL\n' + else: + payload = '10-22 12:00:00.000 100 101 E ReactNativeJS: TypeError: PRIVATE_SENTINEL\n' + stdout.write(payload.encode()) + return subprocess.CompletedProcess(args, 0) + + def test_collection_exports_fixed_facts_and_immediately_purges_private_output(self): + with mock.patch.object(subprocess, 'run', side_effect=self.responder): + result = diag.collect(self.private, 'emulator-5554') + self.assertEqual(result['processes']['state'], 'PRESENT') + self.assertEqual(result['foreground_activity']['relation'], 'OWN_APP') + self.assertEqual(result['runtime_errors']['fault_categories'], ['JS_TYPE_ERROR']) + self.assertEqual(result['metro']['fault_categories'], ['JS_RANGE_ERROR']) + self.assertEqual(result['cause'], 'UNDETERMINED') + self.assertEqual(result['cleanup_status'], 'DELETED') + self.assertFalse((self.private / 'android-startup-diagnosis').exists()) + self.assertNotIn('PRIVATE_SENTINEL', json.dumps(result)) + self.assertNotIn('private.other', json.dumps(result)) + self.assertNotIn(APP, json.dumps(result)) + + def test_failed_and_timed_out_commands_never_claim_absence_or_foreground_state(self): + for timed_out in [False, True]: + def failure(args, stdout, **kwargs): + self.responder(args, stdout, **kwargs) + if timed_out: + raise subprocess.TimeoutExpired(args, 15, output='PRIVATE_SENTINEL') + return subprocess.CompletedProcess(args, 1) + with self.subTest(timed_out=timed_out), mock.patch.object(subprocess, 'run', side_effect=failure): + result = diag.collect(self.private, 'emulator-5554') + self.assertEqual(result['processes']['state'], 'UNKNOWN') + self.assertEqual(result['foreground_activity']['relation'], 'UNKNOWN') + self.assertTrue(all(command['lookup_status'] == 'UNKNOWN' for command in result['commands'])) + self.assertEqual(result['runtime_errors']['fault_categories'], []) + self.assertEqual(result['exit_history']['reason_categories'], []) + self.assertFalse((self.private / 'android-startup-diagnosis').exists()) + self.assertNotIn('PRIVATE_SENTINEL', json.dumps(result)) + + def test_truncated_inventory_never_establishes_absence_and_reports_read_coverage(self): + def oversize(args, stdout, **kwargs): + stdout.write(b'PID NAME\n200 private.other\n' + b'PRIVATE_SENTINEL\n' * 40) + return subprocess.CompletedProcess(args, 0) + with mock.patch.object(diag, 'LIMIT', 32), mock.patch.object(subprocess, 'run', side_effect=oversize): + result = diag.collect(self.private, 'emulator-5554') + self.assertEqual(result['processes']['state'], 'UNKNOWN') + self.assertTrue(all(command['input_truncated'] for command in result['commands'])) + self.assertTrue(all(command['bytes_read'] <= 64 for command in result['commands'])) + self.assertNotIn('PRIVATE_SENTINEL', json.dumps(result)) + + def test_bounded_reads_reject_symlink_and_preserve_contiguous_medium_files(self): + target = self.root / 'private-target' + target.write_text('PRIVATE_SENTINEL') + link = self.private / 'link' + link.symlink_to(target) + with self.assertRaises(ValueError): + diag.bounded(link, self.private) + path = self.private / 'medium' + path.write_text('x' * 60) + with mock.patch.object(diag, 'LIMIT', 32): + payload, coverage = diag.bounded(path, self.private) + self.assertEqual(payload, 'x' * 60) + self.assertEqual(coverage['read_scope'], 'FULL') + self.assertFalse(coverage['input_truncated']) + + def test_host_memory_reports_only_fixed_numeric_fields_and_unknown_on_invalid_or_missing_files(self): + (self.root / 'proc').mkdir() + cgroup = self.root / 'sys/fs/cgroup' + cgroup.mkdir(parents=True) + (self.root / 'proc/meminfo').write_text('MemTotal: 100 kB\nMemAvailable: 50 kB\nSwapTotal: 0 kB\nSwapFree: 0 kB\n') + (cgroup / 'memory.current').write_text('1234') + (cgroup / 'memory.max').write_text('max') + (cgroup / 'memory.events').write_text('oom 2\noom_kill 1\nprivate PRIVATE_SENTINEL') + result = diag.host_memory(self.root) + self.assertEqual(result['meminfo']['bytes']['MemAvailable'], 50 * 1024) + self.assertEqual(result['cgroup']['memory.max']['limit'], 'UNLIMITED') + self.assertEqual(result['cgroup']['memory.events']['counts'], {'oom': 2, 'oom_kill': 1}) + self.assertNotIn('PRIVATE_SENTINEL', json.dumps(result)) + for extra in ['MemTotal: 200 kB\n', 'MemTotal: PRIVATE_SENTINEL\n']: + with (self.root / 'proc/meminfo').open('a') as output: + output.write(extra) + self.assertEqual(diag.host_memory(self.root)['meminfo']['lookup_status'], 'UNKNOWN') + for extra in ['oom 3\n', 'oom PRIVATE_SENTINEL\n']: + (cgroup / 'memory.events').write_text('oom 2\noom_kill 1\n' + extra) + self.assertEqual(diag.host_memory(self.root)['cgroup']['memory.events']['lookup_status'], 'UNKNOWN') + (self.root / 'proc/meminfo').write_text('PRIVATE_SENTINEL') + (cgroup / 'memory.current').unlink() + result = diag.host_memory(self.root) + self.assertEqual(result['meminfo']['lookup_status'], 'UNKNOWN') + self.assertEqual(result['cgroup']['memory.current']['lookup_status'], 'UNKNOWN') + + def test_unsafe_device_selector_never_invokes_adb(self): + with mock.patch.object(subprocess, 'run') as run, self.assertRaises(ValueError): + diag.collect(self.private, 'physical-or-unowned-device') + run.assert_not_called() + + def test_existing_failure_hook_preserves_smoke_failure_and_privacy(self): + metadata = self.root / 'metadata.json' + console = io.StringIO() + with mock.patch.multiple(native, PRIVATE=self.private, EVIDENCE=metadata, PLATFORM='ANDROID'), \ + mock.patch.object(subprocess, 'run', side_effect=self.responder), redirect_stdout(console): + native.save({'commands': [], 'build': 'PASS', 'smoke': 'FAIL'}) + native.record_startup_failure(self.private / 'missing.xml', 'emulator-5554') + result = native.read() + self.assertEqual(result['smoke'], 'FAIL') + self.assertEqual(result['startup_diagnostics']['android_runtime_diagnosis']['cause'], 'UNDETERMINED') + self.assertNotIn('PRIVATE_SENTINEL', metadata.read_text() + console.getvalue()) + + +if __name__ == '__main__': + unittest.main() diff --git a/docs/r5-android-startup-diagnosis.md b/docs/r5-android-startup-diagnosis.md new file mode 100644 index 0000000000..3c14bf8276 --- /dev/null +++ b/docs/r5-android-startup-diagnosis.md @@ -0,0 +1,60 @@ +# Android startup diagnosis without exporting app data + +At exact R5 source `3eb92fc8a622a3e09926d49f47243c163e32d009`, run 39824790868 +attempt 1 built and installed the Android development APK and verified its actual +installed bytes. The initial **Create a passcode** assertion failed before any +credentials were created. Its metadata has one hierarchy snapshot with no known +startup/redbox signals, generic `METRO_ERROR`, and no recognized Android errors. +These observations establish neither the cause nor a clean runtime. + +Normal and lazy Metro bundles and Hermes compilation subsequently passed in the +coordinator's Linux/Node 22 investigation. Those checks are not Android startup +acceptance. Resolver changes, app fixes and timeout increases are unsupported by +the available evidence. Resource pressure is an investigative lead only. + +This isolated package adds a metadata-only collector at the existing Android +startup-failure hook. The cause remains `UNDETERMINED`; original build/smoke and +acceptance outcomes are unchanged. There is no new workflow, native scheduling, +app behavior, dependency, Metro worker, timeout or gate change. The separately +active iOS diagnostic [#7094](https://github.com/KeeperCommunity/bitcoin-keeper/pull/7094) +and native retry are preserved. Its bounded 3eb92→ffb213 comparison has no +`hosted_native.py` overlap. Vault UI producer #7097 is unchanged. + +The new receipt records: + +- Process inventory counts and `PRESENT`/`ABSENT` only from a complete successful + parse; failed/timed-out/truncated/malformed lookups stay `UNKNOWN`. +- Resumed-activity relation `OWN_APP`, `OTHER_APP`, `NONE` or `UNKNOWN`. This is + activity metadata, not proof of actual window visibility or UI readiness. +- App-scoped Android exit-history reason enums, with freshness explicitly + unknown. The parser matches [Android 15's ApplicationExitInfo.dump format](https://github.com/aosp-mirror/platform_frameworks_base/blob/android-15.0.0_r1/core/java/android/app/ApplicationExitInfo.java). +- Fixed JS/native fault categories and known exception types from error records + matching currently owned process IDs. Historical logcat freshness is unknown; + PID matching does not prove the process instance or current failure cause. + Unknown types/tags/messages are never exported; unclassified and unparsed + record counts remain visible. Failed log queries retain `UNKNOWN`. +- Metro error contexts, known fault/type markers and bounded read coverage. + Source codeframes and stack paths are excluded. Bundle markers do not imply + success, and an error marker does not by itself identify a cause. +- Numeric host memory/swap availability and cgroup current/max/OOM counters. + Missing, failed or malformed reads stay unknown; `max` is explicitly unlimited. + Snapshots and historical counters do not prove a current OOM/kill event. + +No process ID, package/activity text, exception message, path, user data, raw log, +hierarchy, screenshot or secret leaves the new collector. Four read-only adb +queries are limited to 15 seconds each; each parser reads at most a 256KiB prefix +and 256KiB tail, reporting omitted data and parse coverage. New command files are +unlinked immediately and their private directory is removed in `finally`. +Existing Metro/Maestro and legacy Android-error logs retain the established +parent-runner private cleanup boundary; they are never uploaded. + +Cloud checks: 105 Python contracts PASS, including 13 new Android controls; +four existing native-diagnostic policy tests PASS with observed Node 24.19.0; +Python syntax and whitespace PASS. Independent review found no remaining +confirmed blocker after source-frame and malformed-record corrections. + +Host tests use synthetic subprocesses and adversarial private sentinels. They +cover unknown lookups, truncation, process attribution, source-frame exclusion, +memory values, private disposal and preservation of the failed smoke result. +**Native collection NOT RUN.** Only a new exact-candidate hosted run can produce +actual Android metadata. No passes transfer from source 3eb92 or another SHA.