diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS new file mode 100644 index 0000000000..baadc36f3e --- /dev/null +++ b/.github/CODEOWNERS @@ -0,0 +1,2 @@ +# Request the three developers for all changes; one approval is required. +* @ben-kaufman @cakesoft-utkarsh @Parsh diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md new file mode 100644 index 0000000000..91cd9f43e4 --- /dev/null +++ b/.github/pull_request_template.md @@ -0,0 +1,35 @@ +## 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. + +## Signoff + +- Copilot review findings: +- Developer approval: +- Product owner signoff: diff --git a/.github/workflows/android-diagnostic-build.yml b/.github/workflows/android-diagnostic-build.yml new file mode 100644 index 0000000000..549cb39f92 --- /dev/null +++ b/.github/workflows/android-diagnostic-build.yml @@ -0,0 +1,151 @@ +name: Android diagnostic build + +on: + pull_request: + branches: + - 'release/**' + - 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 + with: + ref: ${{ github.event.pull_request.head.sha || github.sha }} + persist-credentials: false + + - 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.0.19' + + - 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.0.12077973' '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: 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" + 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") + 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" + 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" + 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/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index a9dbb36c04..645d24fbd7 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -6,11 +6,14 @@ on: - sprint - dev - main + - release/2.6.4 pull_request: branches: - sprint + - codex/r3-release-baseline-to-sprint - dev - main + - release/2.6.4 permissions: actions: read 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/.github/workflows/test.yml b/.github/workflows/test.yml index 5d6104f0ae..cf7f29ae26 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -6,11 +6,16 @@ on: - sprint - dev - main + - release/2.6.3 + - release/2.6.4 pull_request: branches: - sprint + - codex/r3-release-baseline-to-sprint - dev - main + - release/2.6.3 + - release/2.6.4 permissions: contents: read @@ -22,15 +27,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 +59,123 @@ 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: 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 \ + scripts/verify-channel-config.test.cjs ios/scripts/verify-retired-env.test.cjs - 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: 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 + + 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: 838049e537e07eb646a2a0393eb969fba43d0739 + 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: yarn install --frozen-lockfile --non-interactive --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/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000000..d7cec89098 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,128 @@ +# 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. + +`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 +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. + +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, 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. + +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/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/Readme.md b/Readme.md index 3f1f9d298a..0db9aef789 100644 --- a/Readme.md +++ b/Readme.md @@ -55,11 +55,23 @@ 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 ``` +## 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/android/app/build.gradle b/android/app/build.gradle index e8ead00b0a..8c6fc44d1c 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' /** @@ -98,8 +105,8 @@ android { applicationId "io.hexawallet.keeper" minSdkVersion rootProject.ext.minSdkVersion targetSdkVersion rootProject.ext.targetSdkVersion - versionCode 621 - versionName "2.3.15" + versionCode 627 + versionName "2.6.4" missingDimensionStrategy 'react-native-camera', 'general' missingDimensionStrategy 'store', 'play' multiDexEnabled true @@ -115,12 +122,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 +140,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 @@ -161,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/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 @@ - + + + + + + + + + + + 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/android/fastlane/Fastfile b/android/fastlane/Fastfile index 6765414875..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,45 +95,17 @@ platform :android do ) end - desc "Push a new live build to Slack and internal track on Play Store" + 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 - 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}'" - ) - 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() - 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, - } - ) + artifacts = build_verified_production_android_artifacts! upload_to_play_store( - track: 'internal' - ) - 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, - } + track: 'internal', + aab: artifacts[:aab] ) end end @@ -93,15 +113,21 @@ 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 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| +error do |lane, exception| + UI.error("Fastlane failed: " + exception.message.to_s) + if lane.to_s.split.last != 'build_only' && 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/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..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/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..07d2fb2f5e --- /dev/null +++ b/dev/local-backend/README.md @@ -0,0 +1,177 @@ +# 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. 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 + +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. + +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 +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, 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. + +## 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 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; `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. + +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 + +- 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..a22f83ad33 --- /dev/null +++ b/dev/local-backend/VERIFICATION.md @@ -0,0 +1,303 @@ +# 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 + +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 +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 9 October source lock or attribute +earlier private-source failures to the current public backend. + +## 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, 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 + +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. +- 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. 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 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 | +| 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 | + +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 + +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 +``` + +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/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/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..be79d65d3a --- /dev/null +++ b/dev/local-backend/compose.yaml @@ -0,0 +1,99 @@ +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, "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 + 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?replicaSet=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?replicaSet=keeper-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..fbb93a8dca --- /dev/null +++ b/dev/local-backend/keeper.py @@ -0,0 +1,192 @@ +#!/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 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} + + +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 + 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}') + 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']: + 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') + 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() + 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: + 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..4d5d02b531 --- /dev/null +++ b/dev/local-backend/sources.lock.json @@ -0,0 +1,20 @@ +{ + "relay": { + "url": "https://github.com/KeeperCommunity/Relay.git", + "revision": "ee935359f22ff7932ea0a62e9084d4b49801f4aa", + "assets": { + "files/Dockerfile.local": "6dde6c692430b1a91a10cdd82e6f1562023cc5974ffd4d4f58fe57fa1994407a" + } + }, + "signing": { + "url": "https://github.com/KeeperCommunity/SigningServer.git", + "revision": "3cc7728c27328e7b0358d1f91a32d8d13feba90a", + "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..b5bc059b51 --- /dev/null +++ b/dev/local-backend/test_keeper.py @@ -0,0 +1,146 @@ +"""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 +import verify + + +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_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) + (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_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_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) + 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_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', + '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..00922ffea3 --- /dev/null +++ b/dev/local-backend/verify.py @@ -0,0 +1,206 @@ +#!/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 re +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 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') + 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 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') + 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') + 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'}, + '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') + verify_invalid_two_fa(signing, auth) + 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') + 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') + 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/docs/7066-protected-backup-correction.md b/docs/7066-protected-backup-correction.md new file mode 100644 index 0000000000..8de4b3b9e5 --- /dev/null +++ b/docs/7066-protected-backup-correction.md @@ -0,0 +1,246 @@ +# Protected-backup and Version History follow-up — #7066 / #7067 + +## 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 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`. + +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; 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 + +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. + +## 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 +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 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 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; 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 | +| `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. + +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/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/docs/r5/R4-RECONCILIATION.md b/docs/r5/R4-RECONCILIATION.md new file mode 100644 index 0000000000..41e7715afa --- /dev/null +++ b/docs/r5/R4-RECONCILIATION.md @@ -0,0 +1,130 @@ +# 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. +- 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 + 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. +- 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 + 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. +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 + +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 new file mode 100644 index 0000000000..54353806aa --- /dev/null +++ b/docs/r5/STARTUP-INVESTIGATION.md @@ -0,0 +1,139 @@ +# R5 startup and bounded investigations — 21 October 2026 + +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 + +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 +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 +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. +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. +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 new file mode 100644 index 0000000000..a1b3bdd1d1 --- /dev/null +++ b/docs/r5/WORKSTREAM.md @@ -0,0 +1,311 @@ +# R5 Cloud workstream + +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 + +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 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): + +| 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 + +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 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 + 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. + +## 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 + 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 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, +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. + +## 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. + +## 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. + +## 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. + + +## 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 +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 +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. + +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 +native acceptance/performance plan and required review; no PR merge or release. 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/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/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/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/Podfile.lock b/ios/Podfile.lock index 85cbf1ecf8..f53f6d2a91 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 @@ -3986,17 +3986,17 @@ SPEC CHECKSUMS: glog: 5683914934d5b6e4240e497e0f4a3b42d1854183 GoogleDataTransport: aae35b7ea0c09004c3797d53c8c41f66f219d6a7 GoogleUtilities: 00c88b9a86066ef77f0da2fab05f65d7768ed8e1 - hermes-engine: 4ec27e7c84ed7950da6e65008539685455d8bef4 - HtmlToPdf: 6a9c28f54ec810d1d120a698a9b3c83e2bdb1672 + hermes-engine: 06d1c4969de42f925f6614a3c2e049b3a000331d + 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 +PODFILE CHECKSUM: 79b67f9496d66b3a6a618591c809ed441d1032f6 -COCOAPODS: 1.16.2 +COCOAPODS: 1.15.2 diff --git a/ios/fastlane/Fastfile b/ios/fastlane/Fastfile index 7147a897aa..29d1c67ee1 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 @@ -58,6 +54,60 @@ 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 + 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 @@ -106,48 +156,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 - 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") - - 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") + build_verified_production_ipa! upload_build_to_testflight! end end diff --git a/ios/hexa_keeper.xcodeproj/project.pbxproj b/ios/hexa_keeper.xcodeproj/project.pbxproj index 7f0b8da754..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 = 614; + CURRENT_PROJECT_VERSION = 620; DEVELOPMENT_TEAM = Y5TCB759QL; ENABLE_BITCODE = NO; HEADER_SEARCH_PATHS = ( @@ -849,7 +849,7 @@ "$(inherited)", "\"$(SRCROOT)\"", ); - MARKETING_VERSION = 2.5.14; + 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 = 614; + CURRENT_PROJECT_VERSION = 620; 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.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 = 614; + 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.5.14; + 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 = 614; + 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.5.14; + MARKETING_VERSION = 2.6.4; 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/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-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/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/ios/scripts/verify-retired-env.rb b/ios/scripts/verify-retired-env.rb new file mode 100644 index 0000000000..ff11a414fc --- /dev/null +++ b/ios/scripts/verify-retired-env.rb @@ -0,0 +1,28 @@ +#!/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, 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}" } +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..4e496e11e0 --- /dev/null +++ b/ios/scripts/verify-retired-env.test.cjs @@ -0,0 +1,100 @@ +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/); + }); + +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/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-ask-tab-navigation/design.md b/openspec/changes/fix-ask-tab-navigation/design.md new file mode 100644 index 0000000000..244040a94b --- /dev/null +++ b/openspec/changes/fix-ask-tab-navigation/design.md @@ -0,0 +1,48 @@ +## 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. + +## 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. 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/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..911b9cba12 --- /dev/null +++ b/openspec/changes/fix-recovery-backup-freshness/automatic-write-readiness.md @@ -0,0 +1,43 @@ +> 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. + +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` 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. + +## 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. +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 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. + +## Operation-specific proof at the queue head + +| 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 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 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 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 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 new file mode 100644 index 0000000000..cb308ad06d --- /dev/null +++ b/openspec/changes/fix-recovery-backup-freshness/automatic-write-validation.md @@ -0,0 +1,105 @@ +> 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. + +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 | +| --- | --- | +| 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 | +| 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 | +| 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 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 | + +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 + +| 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 host matrix + +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. 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 | +| --- | --- | --- | +| 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 | 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 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 | + +## 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 | +| 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 | +| 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 and handoff + +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 new file mode 100644 index 0000000000..7879706444 --- /dev/null +++ b/openspec/changes/fix-recovery-backup-freshness/design.md @@ -0,0 +1,57 @@ +> 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. + +## Entry points, consent and user flow + +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. + +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. + +## Local transaction and operation identity + +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. + +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. + +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. + +## Queue-head proof and recovery comparison + +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. + +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 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. + +## Revisioned server contract and logical fanout + +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 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 + +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/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..97406bb7c8 --- /dev/null +++ b/openspec/changes/fix-recovery-backup-freshness/evidence/automatic-backup-source-proof-2026-10-13.md @@ -0,0 +1,62 @@ +# 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: `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. + +## 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 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 | 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 | +| 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. 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. + +## Other Cloud checks + +| Check | Actual outcome / boundary | +| --- | --- | +| 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, 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 | +| 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-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 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. + +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/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..9a5a603f21 --- /dev/null +++ b/openspec/changes/fix-recovery-backup-freshness/nfr-review.md @@ -0,0 +1,54 @@ +# Backup comparison and repair: NFR review + +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. +- `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. + +## Historical native acceptance matrix + +**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. + +## 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 new file mode 100644 index 0000000000..9c55ef90b6 --- /dev/null +++ b/openspec/changes/fix-recovery-backup-freshness/proposal.md @@ -0,0 +1,27 @@ +# 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. 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. + +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/protected-backup-integration.md b/openspec/changes/fix-recovery-backup-freshness/protected-backup-integration.md new file mode 100644 index 0000000000..b23a274f19 --- /dev/null +++ b/openspec/changes/fix-recovery-backup-freshness/protected-backup-integration.md @@ -0,0 +1,37 @@ +# 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. 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 + +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. + +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 + +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. + +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/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..ed49d4bdc7 --- /dev/null +++ b/openspec/changes/fix-recovery-backup-freshness/specs/assisted-server-backup/spec.md @@ -0,0 +1,152 @@ +> 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 +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 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 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 remotely +- **THEN** show a mismatch and Back Up Now through the existing backup area +- **AND** do not replace the backup until the user explicitly starts repair + +#### Scenario: Backups match +- **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 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 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: 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 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 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. + +#### 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 progress +- **WHEN** Back is selected during repair +- **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 display success following backup failure. Recovery Key confirmation SHALL remain a separate state. + +#### Scenario: Server backup fails after Recovery Key confirmation +- **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 new file mode 100644 index 0000000000..dfb8e7aa18 --- /dev/null +++ b/openspec/changes/fix-recovery-backup-freshness/tasks.md @@ -0,0 +1,51 @@ +> 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. + +## Local-first implementation + +- [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 + +- [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 + +- [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 + +- [ ] 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. + +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 new file mode 100644 index 0000000000..b45454ca06 --- /dev/null +++ b/openspec/changes/fix-recovery-backup-freshness/validation.md @@ -0,0 +1,50 @@ +> 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 + +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 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. + +## 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. + +## 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/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..2862d15f90 --- /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 + +- 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. 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/changes/reconcile-r5-release-baseline/.openspec.yaml b/openspec/changes/reconcile-r5-release-baseline/.openspec.yaml new file mode 100644 index 0000000000..7cc41bb519 --- /dev/null +++ b/openspec/changes/reconcile-r5-release-baseline/.openspec.yaml @@ -0,0 +1,2 @@ +schema: spec-driven +created: 2026-10-22 diff --git a/openspec/changes/reconcile-r5-release-baseline/design.md b/openspec/changes/reconcile-r5-release-baseline/design.md new file mode 100644 index 0000000000..67a52b6894 --- /dev/null +++ b/openspec/changes/reconcile-r5-release-baseline/design.md @@ -0,0 +1,52 @@ +## Context + +R4's middleware stamps app/session authority at dispatch and its deletion worker +checks retained Vault references inside the local transaction. R5's earlier UI +confirmation predates that dispatch boundary; its origin must survive an account +switch, including A → B → A. The owner-confirmed release baseline is 1d8a86a. + +## Goals / Non-Goals + +Preserve R4 and R5 history and all released backend/native/dependency inputs; +reject stale deletion confirmations without weakening reference, archived-key or +passcode guards. No schema/MMKV/store-shape change or Persist migration/version +bump; no PSBT, amount, UTXO or hardware interaction. + +## Decisions + +The DeleteKeys screen will capture the rendered storage appId and backup account +session with its one-use intent. Selection and confirmation compare that origin +against current Redux storage, current Realm KeeperApp and session before reading +references or dispatching. Delete/archive actions carry the original app/session +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, 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 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. + +Redux storage.appId is read; existing DELETE/ARCHIVE_SIGINING_DEVICE workers and +backup origin middleware consume the unchanged action fields. UI screens and +flows/login.yaml retain the existing native checkpoints; document the finite +account-switch/deletion Maestro scenarios instead of scripting destructive actions +against arbitrary users. Native execution remains an explicit follow-up. + +## Risks / Trade-offs + +Separate model tests do not prove real Realm/auth provenance or native account +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 new file mode 100644 index 0000000000..77c8117169 --- /dev/null +++ b/openspec/changes/reconcile-r5-release-baseline/proposal.md @@ -0,0 +1,42 @@ +## Why + +The owner confirms R4 / 2.6.4 is live. R5 must use its recorded artifact source +1d8a86a as the baseline and retain its transactional deletion protections while +completing the already-approved bounded key-deletion/startup work. + +## What Changes + +- Integrate the released R4 source into the isolated R5 branch, preserving R5 + history, R4 refs and the exact released dependency/native/configuration inputs. +- Bind a key-deletion confirmation to its originating account/session so a late + passcode callback cannot be rebound by dispatch-time middleware. +- Rerun affected automated checks and record finite native acceptance still needed. + +## Capabilities + +### New Capabilities + +- `r5-account-bound-key-deletion`: confirmation ownership across account/session changes. + +### Modified Capabilities + +None; released R4 backend requirements are preserved. + +## Impact + +Both mainnet and testnet. Signer, Vault and account/session ownership are affected; +no amount/UTXO, hardware transport, subscription tier, schema or key-material +change. Reuse existing DeleteKeys/KeyCard/KeeperModal/passcode components. Existing +flows/login.yaml remains the startup specification; account-switch/deletion native +checkpoints will be documented without deleting real wallets or contacting providers. +The R5 delta remains within the two-feature, approximately 13k–18k token estimate. + +## Non-goals + +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 new file mode 100644 index 0000000000..c5de22bebc --- /dev/null +++ b/openspec/changes/reconcile-r5-release-baseline/specs/r5-account-bound-key-deletion/spec.md @@ -0,0 +1,49 @@ +## ADDED Requirements + +### Requirement: Key deletion confirmation retains account ownership + +DeleteKeys SHALL authorize deletion or archived retention of a Signer only in the +same KeeperApp account/session in which its current one-use confirmation began. +All active Vault references remain blocking. No satoshi amount or UTXO changes. + +#### Scenario: Current account deletes an unused key +- **GIVEN** an unused visible Signer selected in account A and its current session +- **WHEN** its passcode confirmation succeeds and A/Realm/session still match +- **THEN** one delete action carries that original app/session; the Delete CTA is + discoverable without Hide and R4's transaction performs its own reference check + +#### Scenario: Old confirmation survives an account round trip +- **GIVEN** a Signer confirmation from A, followed by A → B → A without a rerender +- **WHEN** the earlier passcode callback completes +- **THEN** no delete/archive action is dispatched and the old confirmation closes + +#### Scenario: Realm and Redux disagree +- **GIVEN** Redux names A while the current Realm KeeperApp belongs to B or is unreadable +- **WHEN** a key selection or confirmation is attempted +- **THEN** no delete/archive action is dispatched; native UI SHALL not report success + +#### Scenario: A retained callback tries a newer account's key card +- **GIVEN** a key-card callback captured in A before account/session changed +- **WHEN** it is invoked after the transition +- **THEN** it cannot open a new actionable deletion confirmation in the new account + +#### Scenario: Referenced and archived keys remain protected offline +- **GIVEN** a Signer with a live active or archived Vault reference and unavailable network +- **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 new file mode 100644 index 0000000000..a08bdf3051 --- /dev/null +++ b/openspec/changes/reconcile-r5-release-baseline/tasks.md @@ -0,0 +1,27 @@ +## 1. UI Components +- [x] 1.1 Preserve existing components, confirmation/cancel/privacy and helper copy against released source. + +## 2. Business Logic / Hooks +- [x] 2.1 Capture and validate deletion origin before selection/confirmation, including A → B → A. + +## 3. Store (Slice + Saga) +- [x] 3.1 Carry original app/session on delete/archive actions; retain R4 workers/middleware unchanged. + +## 4. Storage +- [x] 4.1 Verify no schema/MMKV/store-shape change; no Redux Persist migration bump needed. + +## 5. Tests +- [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) +- [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 +- [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/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 d192ee46fa..73041aa613 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,17 @@ { "name": "hexa_keeper", - "version": "2.5.13", + "version": "2.6.4", "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 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", + "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", @@ -14,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", @@ -23,7 +28,10 @@ "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", + "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", @@ -128,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/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/README.md b/release/README.md new file mode 100644 index 0000000000..7500d40554 --- /dev/null +++ b/release/README.md @@ -0,0 +1,64 @@ +# Release version verification + +**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` 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. + +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. Run all feasible automated, simulator/emulator and disposable checks before public submission; record the owner's post-release manual checks separately. + +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`). 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. + +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. + +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 + +- 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. +- 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/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 new file mode 100644 index 0000000000..bcc10561d0 --- /dev/null +++ b/release/r4-automatic-backup-local-handoff.md @@ -0,0 +1,288 @@ +# 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. + +**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. + +`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. + +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 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, +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 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. + +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. 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=$(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") +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 +./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 +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 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. + +```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. 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. +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: +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: +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. 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/release/r4-inheritance-validation.md b/release/r4-inheritance-validation.md new file mode 100644 index 0000000000..284fb78858 --- /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 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. 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. + +### 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. 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/release/r4-protected-backup-app.md b/release/r4-protected-backup-app.md new file mode 100644 index 0000000000..1f85adac75 --- /dev/null +++ b/release/r4-protected-backup-app.md @@ -0,0 +1,111 @@ +# 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). + +## 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. + +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. + +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. + +## 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. 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. + +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/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/release/version.json b/release/version.json new file mode 100644 index 0000000000..2ee660a4d4 --- /dev/null +++ b/release/version.json @@ -0,0 +1,21 @@ +{ + "version": "2.6.4", + "androidVersionCode": 627, + "iosBuildNumber": 620, + "baselines": { + "android": { + "applicationId": "io.hexawallet.bitcoinkeeper", + "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-21T08:26:48Z", + "highestUploadedBuild": 619, + "highestMarketingVersion": "2.6.3", + "productionRelease": "619 (2.6.3)" + } + } +} diff --git a/reproducibility/README.md b/reproducibility/README.md new file mode 100644 index 0000000000..e40baa5341 --- /dev/null +++ b/reproducibility/README.md @@ -0,0 +1,181 @@ +# 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. + +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 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. 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 + +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. + +## 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 \ + --android-sdk-root /path/to/android-sdk +``` + +The tracked manifest contains seven source-audited environment names and no +values. This is a **provisional future-release allowlist**: the GasFree pause +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. +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..b806f15ca5 --- /dev/null +++ b/reproducibility/android-inputs.json @@ -0,0 +1,27 @@ +{ + "node": "22.23.3", + "yarn": "1.22.22", + "java": "17.0.19", + "yarn_lock_sha256": "a915c8ea0c4446270c0f6c894998f0c4fa481bcc4edbea328e57377613939056", + "gradle_wrapper_jar_sha256": "0336f591bc0ec9aa0c9988929b93ecc916b3c1d52aed202c7381db144aa0ef15", + "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" + }, + "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 new file mode 100644 index 0000000000..ad629ccd4b --- /dev/null +++ b/reproducibility/test_verify_android_inputs.py @@ -0,0 +1,69 @@ +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" + env.write_text("ENVIRONMENT=production\nUNREVIEWED_TOKEN=private-example-value\n") + with self.assertRaises(MODULE.InputError) as caught: + MODULE.check_env_names(env, ["ENVIRONMENT"]) + 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" + env.write_text("ENVIRONMENT=production\n") + with self.assertRaisesRegex(MODULE.InputError, "missing environment names: RELAY"): + 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: + 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..08e0fcc775 --- /dev/null +++ b/reproducibility/verify-android-inputs.py @@ -0,0 +1,186 @@ +#!/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 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 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 pinned environment name: {name}") + names.add(name) + return names + + +def check_env_names(env_file, expected_names): + actual = read_env_names(env_file) + approved = expected_env_names(expected_names) + 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("--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, inputs["environment_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) diff --git a/scripts/check-android-16kb.py b/scripts/check-android-16kb.py new file mode 100644 index 0000000000..d96535674d --- /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, 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"]: + 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/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/release-version.cjs b/scripts/release-version.cjs new file mode 100644 index 0000000000..de30abd6da --- /dev/null +++ b/scripts/release-version.cjs @@ -0,0 +1,170 @@ +#!/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; + } + // 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); + 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..c34db5dee9 --- /dev/null +++ b/scripts/release-version.test.cjs @@ -0,0 +1,87 @@ +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('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'))); +}); +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/run-automatic-backup-genuine.cjs b/scripts/run-automatic-backup-genuine.cjs new file mode 100644 index 0000000000..a72bf4cc0d --- /dev/null +++ b/scripts/run-automatic-backup-genuine.cjs @@ -0,0 +1,56 @@ +// 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 = '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'); +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 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() }, +}); +process.exit(result.status ?? 1); 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/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/scripts/test_check_android_16kb.py b/scripts/test_check_android_16kb.py new file mode 100644 index 0000000000..39db7f238f --- /dev/null +++ b/scripts/test_check_android_16kb.py @@ -0,0 +1,209 @@ +"""Byte-level regressions for the static Android 16 KB evidence checker.""" + +import importlib.util +import io +import json +import pathlib +import struct +import subprocess +import sys +import tempfile +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) +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(" 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 + + 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) + 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 diff --git a/scripts/test_release_artifact.py b/scripts/test_release_artifact.py new file mode 100644 index 0000000000..ae6ee89670 --- /dev/null +++ b/scripts/test_release_artifact.py @@ -0,0 +1,89 @@ +"""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, "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): + 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) + 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}), \ + 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) + 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") + + +if __name__ == "__main__": + unittest.main() diff --git a/scripts/verify-channel-config.cjs b/scripts/verify-channel-config.cjs new file mode 100644 index 0000000000..6c9d85a987 --- /dev/null +++ b/scripts/verify-channel-config.cjs @@ -0,0 +1,85 @@ +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 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) + .map(match => match[1].trim()); + +if (matches.length !== 1 || matches[0] !== expected) { + throw new Error(`Production CHANNEL_URL must be ${expected}`); +} + +const stages = envContents + .split(/\r?\n/) + .map(line => line.match(/^\s*(?:export\s+)?ENVIRONMENT\s*=\s*(.*?)\s*$/)) + .filter(Boolean) + .map(match => match[1].trim()); + +if (stages.length !== 1 || stages[0] !== 'PRODUCTION') { + throw new Error('Production ENVIRONMENT must be PRODUCTION'); +} + +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 new file mode 100644 index 0000000000..8c64475da0 --- /dev/null +++ b/scripts/verify-channel-config.test.cjs @@ -0,0 +1,194 @@ +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/', + '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-')); + 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); + })); + +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')); + 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'], + ['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`, () => + 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/scripts/verify-release-artifact.py b/scripts/verify-release-artifact.py new file mode 100644 index 0000000000..16ebaf120b --- /dev/null +++ b/scripts/verify-release-artifact.py @@ -0,0 +1,159 @@ +#!/usr/bin/env python3 +"""Inspect packaged release metadata and required feature markers without dumping secrets. + +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. +""" +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}" +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): + 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 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: + raise ValueError(f"{Path(command[0]).name} failed; inspect tool/environment configuration") + 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: + 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" + 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([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") + 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(): + 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") + verify_channel(archive, suffix) + 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", + "channelUrl": "verified", + "signing": "separate verification required"} + if suffix == ".apk": + 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") + 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()) 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 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 ( - + {illustrations[contentType]} {descriptions[contentType] && {descriptions[contentType]}} {asbEnabled && {BackupWallet.assistedServerBackupEnabled}} + {contentType !== ContentType.healthCheckSuccessful && ( + {BackupWallet.retainedOlderCopyNotice} + )} ); } @@ -74,17 +78,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); @@ -103,13 +111,12 @@ function BackupHealthCheckList({ isUaiFlow }) { setShowConfirmSeedModal(true); }; - useEffect(() => { - if (backupAllSuccess || backupAllFailure) { + 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 +125,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)); @@ -132,13 +136,14 @@ function BackupHealthCheckList({ isUaiFlow }) { useEffect(() => { if (backupAllFailure && isFocused) { - dispatch(setBackupAllFailure(false)); + setFailedVerificationModal(true); + dispatch(setBackupAllFailure({ appId: id, status: false })); } }, [backupAllFailure]); useEffect(() => { if (backupAllSuccess && isFocused) { - dispatch(setBackupAllSuccess(false)); + dispatch(setBackupAllSuccess({ appId: id, status: false })); dispatch(seedBackedConfirmed(true)); } }, [backupAllSuccess]); @@ -191,7 +196,7 @@ function BackupHealthCheckList({ isUaiFlow }) { } else { setBackupMismatchModal(true); } - }) + }, id) ); } else { dispatch(seedBackedConfirmed(true)); @@ -285,12 +290,12 @@ function BackupHealthCheckList({ isUaiFlow }) { closeOnOverlayClick={false} buttonTextColor={`${colorMode}.buttonText`} buttonBackground={`${colorMode}.pantoneGreen`} - buttonText={'Backup Now'} + buttonText={BackupWallet.backUpNow} buttonCallback={() => { - dispatch(backupAllSignersAndVaults()); + dispatch(backupAllSignersAndVaults(id)); setBackupMismatchModal(false); }} - secondaryButtonText="Home" + secondaryButtonText={BackupWallet.home} secondaryCallback={() => { navigtaion.dispatch(CommonActions.navigate('Home')); }} 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/components/HomeScreenHeader.tsx b/src/components/HomeScreenHeader.tsx index ac9727a035..239ea8af23 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.dispatch(CommonActions.navigate('AssistedBackupStatus')); }, }; diff --git a/src/components/ImportExportLabels/index.tsx b/src/components/ImportExportLabels/index.tsx index 72074f47ae..6125fa5d16 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'; @@ -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; @@ -28,11 +29,15 @@ 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); const handleImportLabels = async () => { + const originBackupAccountSession = currentBackupAccountSession(); + const originBackupWriteIntent = captureBackupWriteIntent(originAppId); try { setIsSyncingLabels(true); await importFile( @@ -101,7 +106,15 @@ const ImportExportLabels: React.FC = ({ origin: standardizeDescriptor(label.origin), })); - dispatch(importLabels({ labels: standardizedLabels })); + 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/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 > { 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/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/components/TransactionElement.tsx b/src/components/TransactionElement.tsx index a5adda3fac..401e0f0121 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,16 @@ 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, + 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'; +import { LocalizationContext } from 'src/context/Localization/LocContext'; function TransactionElement({ transaction, @@ -44,12 +50,19 @@ function TransactionElement({ let amount: number; let confirmations: number; let transactionType: string; + 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 ? 6 : 0; // Assuming 6 confirmations for USDT transactions(no. of conf on Tron doesn't really make any finality sense) + confirmations = + (transaction as USDTTransaction).status === GasFreeTransferStatus.CHAIN_CONFIRMED ? 1 : 0; if ( (transaction as USDTTransaction).to === (wallet as USDTWallet).accountStatus.gasFreeAddress ) { @@ -65,6 +78,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' }); @@ -73,6 +87,7 @@ function TransactionElement({ - {confirmations === 0 && !isCached && ( + {confirmations === 0 && !isCached && !unavailableUSDTStatus && ( )} - {isCached ? ( + {legacyUSDTRequest ? null : isCached ? ( ) : transactionType === 'Received' ? ( colorMode === 'light' ? ( @@ -104,15 +119,22 @@ function TransactionElement({ - {labels[transactionId]?.[0]?.name || transactionId} + {legacyUSDTRequest + ? translations.usdtWalletText.legacyRequestTitle + : labels[transactionId]?.[0]?.name || transactionId} {formattedDate} + {unavailableUSDTStatus && ( + + {translations.usdtWalletText.statusUnavailable} + + )} {(transaction as Transaction).tags?.includes('potential-dust-spend') && ( @@ -123,15 +145,17 @@ function TransactionElement({ - + {!legacyUSDTRequest && ( + + )} {colorMode === 'dark' ? : } @@ -145,11 +169,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', @@ -167,6 +197,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 962b29aee2..35f95a0231 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", @@ -194,7 +195,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", @@ -223,8 +224,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", @@ -449,6 +450,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", @@ -667,11 +672,13 @@ "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", "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", @@ -795,16 +802,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!", - "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", @@ -812,7 +820,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", @@ -1037,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/", @@ -1090,11 +1116,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", @@ -1140,7 +1166,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", @@ -1205,15 +1237,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.", @@ -1224,7 +1256,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", @@ -1291,12 +1325,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", @@ -1758,8 +1792,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.", @@ -2021,7 +2055,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", @@ -2152,6 +2186,15 @@ "psbtMissingSignature": "This PSBT does not include a signature for the selected signer" }, "usdtWalletText": { + "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.", + "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", @@ -2217,5 +2260,77 @@ "disclaimerPoint3": "Your messages are processed securely to help Ask Keeper respond.", "disclaimerLearnMore": "Learn more about Ask Keeper", "disclaimerCta": "Got it" - } -} \ No newline at end of file + }, + "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", + "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": "Automatic backup is off", + "body": "Future automatic updates are stopped. Existing backup copies were not deleted." + }, + "checking": { + "title": "Checking protected backup", + "body": "Comparing the encrypted protected copy with recovery data on this device." + }, + "different": { + "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 protected backup", + "body": "Preparing and encrypting the recovery data on this device." + }, + "uploading": { + "title": "Updating protected backup", + "body": "Sending your encrypted protected copy to Keeper’s server." + }, + "verifying": { + "title": "Verifying protected backup", + "body": "Reading back the protected copy and checking that its recovery data matches." + }, + "verified": { + "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": "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": "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 a59ae5abda..5f9e0ca12b 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", @@ -194,7 +195,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", @@ -223,8 +224,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", @@ -437,6 +438,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", @@ -655,11 +660,13 @@ "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", "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", @@ -783,16 +790,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!", - "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", @@ -800,7 +808,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", @@ -1025,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/", @@ -1078,11 +1104,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", @@ -1128,7 +1154,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", @@ -1193,19 +1225,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", @@ -1272,12 +1306,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", @@ -1739,8 +1773,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.", @@ -2002,7 +2036,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", @@ -2133,6 +2167,15 @@ "psbtMissingSignature": "This PSBT does not include a signature for the selected signer" }, "usdtWalletText": { + "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.", + "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", @@ -2198,5 +2241,77 @@ "disclaimerPoint3": "Your messages are processed securely to help Ask Keeper respond.", "disclaimerLearnMore": "Learn more about Ask Keeper", "disclaimerCta": "Got it" - } -} \ No newline at end of file + }, + "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", + "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": "Copias automáticas desactivadas", + "body": "Las actualizaciones automáticas se detuvieron. Las copias existentes no se eliminaron." + }, + "checking": { + "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 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 protegida", + "body": "Preparando y cifrando los datos de recuperación de este dispositivo." + }, + "uploading": { + "title": "Actualizando la copia protegida", + "body": "Enviando la copia protegida cifrada al servidor de Keeper." + }, + "verifying": { + "title": "Verificando la copia protegida", + "body": "Leyendo la copia protegida y comprobando que sus datos de recuperación coinciden." + }, + "verified": { + "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 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 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/useCollaborativeWalletHandoff.ts b/src/hooks/useCollaborativeWalletHandoff.ts new file mode 100644 index 0000000000..80e79fea6d --- /dev/null +++ b/src/hooks/useCollaborativeWalletHandoff.ts @@ -0,0 +1,136 @@ +import { useCallback, useEffect, useLayoutEffect, useRef, useState } from 'react'; + +type Session = { + signers: Record; + 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/hooks/useSettingKeeper.tsx b/src/hooks/useSettingKeeper.tsx index d9242915b3..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'; @@ -51,16 +52,16 @@ import { setAutomaticCloudBackup, setDeleteBackupSuccess, 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'; @@ -68,13 +69,14 @@ export const useSettingKeeper = () => { const dispatch = useAppDispatch(); const { colorMode, toggleColorMode } = useColorMode(); const navigation = useNavigation(); - const { showToast } = useToastMessage(); const isFocused = useIsFocused(); const { isOnL2Above, isOnL4 } = usePlan(); const data = useQuery(RealmSchema.BackupHistory); 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); @@ -86,18 +88,26 @@ export const useSettingKeeper = () => { common, signer: signerText, error: errorText, + recoveryBackup: backupCopy, } = translations; const { typeBasedIndicator } = useIndicatorHook({ types: [uaiType.RECOVERY_PHRASE_HEALTH_CHECK], }); 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 backupAccountSession = currentBackupAccountSession(); + useEffect(() => setBackupFeedback(undefined), [id, backupAccountSession]); + const backupAllFailure = !!backupAllFailureByAppId?.[id]; + const backupAllSuccess = !!backupAllSuccessByAppId?.[id]; + const deleteBackupSuccess = !!deleteBackupSuccessByAppId?.[id]; + const deleteBackupFailure = !!deleteBackupFailureByAppId?.[id]; useEffect(() => { if (isOnL4) { @@ -142,8 +152,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 +162,34 @@ 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 })); + setBackupFeedback({ appId: id, message: settings.assistedServerDeleteBackupConfirmed }); } - }, [deleteBackupSuccess]); + }, [deleteBackupSuccess, id, isFocused]); useEffect(() => { if (deleteBackupFailure && isFocused) { - dispatch(setDeleteBackupFailure(false)); - showToast(errorText.unableToDeleteAssistedServer, ); + dispatch(setDeleteBackupFailure({ appId: id, status: false })); + const diagnostic = getBackupDiagnostic(id); + const message = + diagnostic?.operation === 'delete' && diagnostic.category === 'never-enrolled' + ? backupCopy.deleteNotEnrolled + : errorText.unableToDeleteAssistedServer; + setBackupFeedback({ appId: id, message }); } }, [deleteBackupFailure]); useEffect(() => { if (backupAllFailure && isFocused) { - dispatch(setBackupAllFailure(false)); + dispatch(setBackupAllFailure({ appId: id, status: false })); } }, [backupAllFailure]); const toggleAutomaticBackupMode = useCallback(() => { - if (!automaticCloudBackup) dispatch(backupAllSignersAndVaults()); + if (!automaticCloudBackup) setShowEnableBackup(true); else setShowDeleteBackup(true); - }, [automaticCloudBackup, dispatch]); + }, [automaticCloudBackup, dispatch, id]); const debouncedAutomaticBackupToggle = useCallback(() => { if (backupToggleTimeoutRef.current) { @@ -261,8 +277,9 @@ export const useSettingKeeper = () => { { title: settings.assistedServerBackup, description: settings.assistedServerBackupSubtitle, + expandDescription: true, icon: , - onPress: debouncedAutomaticBackupToggle, + onPress: () => navigation.dispatch(CommonActions.navigate('AssistedBackupStatus')), rightIcon: isOnL2Above ? ( ) : ( @@ -456,34 +473,96 @@ 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()); - }} - secondaryCallback={() => { - setShowDeleteBackup(false); - dispatch(setAutomaticCloudBackup(false)); - }} - secondaryText={settings.assistedServerDeleteBackupModalSecondaryCTA} - /> - )} - /> + <> + setShowDeleteBackup(false)} + title={settings.assistedServerDeleteBackupTitle} + modalBackground={`${colorMode}.modalWhiteBackground`} + textColor={`${colorMode}.textGreen`} + Content={() => ( + + + {settings.assistedServerDeleteBackupSubtitle} + + { + setShowDeleteBackup(false); + dispatch(deleteBackup(id)); + }} + /> + { + setShowDeleteBackup(false); + dispatch(setAutomaticCloudBackup({ appId: id, enabled: false })); + setBackupFeedback({ appId: id, message: settings.assistedServerBackupDisabled }); + }} + /> + + )} + /> + setShowEnableBackup(false)} + title={settings.assistedServerEnableBackupTitle} + modalBackground={`${colorMode}.modalWhiteBackground`} + textColor={`${colorMode}.textGreen`} + 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, @@ -496,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/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/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/hooks/useUSDTWallets.ts b/src/hooks/useUSDTWallets.ts index dba1d6e868..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,24 +30,15 @@ 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 } from 'react-redux'; -import { useAppSelector } from 'src/store/hooks'; -import { updateAppImage } from 'src/store/sagaActions/bhr'; -import Relay from 'src/services/backend/Relay'; export interface UseUSDTWalletsOptions { getAll?: boolean; 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; @@ -61,7 +71,15 @@ 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 renderAccountSession = currentBackupAccountSession(); + const isOriginCurrent = useCallback( + () => + (dbManager.getObjectByIndex(RealmSchema.KeeperApp) as any)?.id === appId && + reduxStore.getState().storage.appId === appId, + [appId, reduxStore] + ); const filterByNetwork = (wallets: USDTWallet[]) => { if (bitcoinNetworkType) @@ -79,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; @@ -98,7 +114,21 @@ export const useUSDTWallets = (options: UseUSDTWalletsOptions = {}): UseUSDTWall primaryMnemonic?: string; importDetails?: USDTWalletImportDetails; }): Promise<{ newWallet?: USDTWallet; error?: string }> => { + const originSession = currentBackupAccountSession(); + const originBackupWriteIntent = captureBackupWriteIntent(appId); + const isCurrentCreate = () => + isOriginCurrent() && + 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( @@ -107,6 +137,7 @@ export const useUSDTWallets = (options: UseUSDTWalletsOptions = {}): UseUSDTWall null, true // get all wallets )) as any; + if (!isCurrentCreate()) throw new Error('Account changed'); let lastInstanceNum = -1; allUSDTWallets.forEach((wallet) => { @@ -125,6 +156,7 @@ export const useUSDTWallets = (options: UseUSDTWalletsOptions = {}): UseUSDTWall instanceNum: params.type === USDTWalletType.DEFAULT ? lastInstanceNum + 1 : null, importDetails: params.importDetails, }); + 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); @@ -132,57 +164,137 @@ 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; + 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 - dispatch(updateAppImage({ wallets: [newWallet], signers: null, updateNodes: false })); + dispatch({ + ...updateAppImage({ wallets: [newWallet], signers: null, updateNodes: false }, appId), + originBackupAccountSession: originSession, + originBackupWriteIntent, + committedOperation, + }); 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, renderAccountSession] ); /** * Delete a wallet */ const deleteWallet = useCallback(async (walletId: string): Promise => { + const originAppId = appId; + const originSession = currentBackupAccountSession(); + const originBackupWriteIntent = captureBackupWriteIntent(originAppId); + const isCurrentDelete = () => + isOriginCurrent() && + renderAccountSession === originSession && + originSession === currentBackupAccountSession(); try { - const response = await Relay.deleteAppImageEntity({ - appId, - signers: null, - walletIds: [walletId], + if (!isCurrentDelete()) return false; + 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 (!response.updated) throw new Error('Failed to delete wallet'); - - await dbManager.deleteObjectById(RealmSchema.USDTWallet, walletId); + if (!committedOperation.committed) throw new Error('Failed to delete wallet'); + if (isAutomaticCloudBackupEnabled(reduxStore.getState().bhr, originAppId)) { + 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 })); + } + } return true; } catch (err) { - setError(err.message || 'Failed to delete wallet'); + if (isCurrentDelete()) setError(err.message || 'Failed to delete wallet'); captureError(err); return false; } - }, []); + }, [appId, dispatch, isOriginCurrent, reduxStore, renderAccountSession]); /** * 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 - - return true; - } catch (err) { - setError(err.message || 'Failed to update wallet'); - captureError(err); - return false; - } - }, []); + const updateWallet = useCallback( + async (wallet: USDTWallet): Promise => { + const originAppId = appId; + const operationSession = currentBackupAccountSession(); + const originBackupWriteIntent = captureBackupWriteIntent(originAppId); + const isCurrentUpdate = () => + isOriginCurrent() && + renderAccountSession === operationSession && + operationSession === currentBackupAccountSession(); + 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; + recoveryChanged = + canonical(recoveryContent('wallets', previousWallet)) !== + canonical(recoveryContent('wallets', wallet)); + const { id, ...walletUpdateData } = wallet; + if (!isCurrentUpdate()) throw new Error('Account changed'); + 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'); + + // 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), + 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 && isCurrentUpdate()) { + markBackupMutation(originAppId); + dispatch(invalidateBackupRepair(originAppId)); + dispatch(setPendingAllBackup({ appId: originAppId, pending: true })); + } + if (isCurrentUpdate()) setError(err.message || 'Failed to update wallet'); + captureError(err); + return false; + } + }, + [appId, dispatch, isOriginCurrent, renderAccountSession] + ); /** * Syncs a single wallet account status with latest data @@ -195,13 +307,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 @@ -217,13 +329,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 @@ -236,21 +348,19 @@ 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 */ 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/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/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/hooks/useWallets.tsx b/src/hooks/useWallets.tsx index 79eb7aeee1..284e039d1f 100644 --- a/src/hooks/useWallets.tsx +++ b/src/hooks/useWallets.tsx @@ -2,46 +2,63 @@ 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'; 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[]; + 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. + 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]); + + // 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: filterByNetwork(allWallets) }; + return { wallets: listedWallets }; } - const allWalletsNonHidden = allWallets.filter( - (wallet) => wallet.presentationData.visibility == VisibilityType.DEFAULT - ); - walletIds = walletIds?.filter((item) => !!item); - if (walletIds && walletIds.length) { - 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) }; + if (hasWalletIds) { + return { wallets: selectedWallets }; } - return { wallets: filterByNetwork(allWalletsNonHidden) }; + return { wallets: listedWallets }; }; export default useWallets; diff --git a/src/navigation/AppStateHandler.tsx b/src/navigation/AppStateHandler.tsx index 342f2ab15e..34ef589e62 100644 --- a/src/navigation/AppStateHandler.tsx +++ b/src/navigation/AppStateHandler.tsx @@ -1,8 +1,9 @@ -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'; import { setHasDeepLink } from 'src/store/reducers/login'; +import { autoSyncWallets } from 'src/store/sagaActions/wallets'; const PASSCODE_TIMEOUT = 5 * 60 * 1000; @@ -11,28 +12,29 @@ 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()); } 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'; - - if ( - notLoginStack && - lastBackgroundTime && - Date.now() - lastBackgroundTime > PASSCODE_TIMEOUT - ) { - dispatch(setHasDeepLink(triggeredDeepLink)); + const activeRoute = state?.routes[state.index]; + const notLoginStack = activeRoute?.name !== 'LoginStack'; + const timedOut = lastBackgroundTime && Date.now() - lastBackgroundTime > PASSCODE_TIMEOUT; + + if (notLoginStack && timedOut) { + dispatch(setHasDeepLink(triggeredDeepLink.current)); + triggeredDeepLink.current = null; navigation.reset({ index: 0, routes: [ @@ -44,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/navigation/Navigator.tsx b/src/navigation/Navigator.tsx index d5ecfc5bbc..b89ab6a517 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'; @@ -153,13 +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'; import UsdtTransactionDetail from 'src/screens/USDT/UsdtTransactionDetail'; import UsdtTransactionHistory from 'src/screens/USDT/UsdtTransactionHistory'; import Usdtsetting from 'src/screens/USDT/Usdtsetting'; @@ -169,8 +165,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'; @@ -263,6 +257,7 @@ function AppStack() { + @@ -293,13 +288,8 @@ function AppStack() { - - - - - @@ -382,8 +372,6 @@ function AppStack() { - - 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/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/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/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/screens/AppSettings/AssistedBackupStatus.tsx b/src/screens/AppSettings/AssistedBackupStatus.tsx new file mode 100644 index 0000000000..f9bc9ed8dc --- /dev/null +++ b/src/screens/AppSettings/AssistedBackupStatus.tsx @@ -0,0 +1,165 @@ +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 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 { ProtectedBackupAction } from 'src/components/Backup/ProtectedBackupActions'; +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 { + automaticCloudBackupByAppId, + backupRepairStateByAppId = {}, + backupRepairRunningByAppId = {}, + protectedBackupStateByAppId = {}, + } = useAppSelector((state) => state.bhr); + const automaticCloudBackup = isAutomaticCloudBackupEnabled({ automaticCloudBackupByAppId }, id); + const recordedPhase = backupRepairStateByAppId[id]; + const phase = + 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(() => { + // 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; + const timer = setTimeout(() => setSlow(true), 10_000); + return () => clearTimeout(timer); + }, [running, phase]); + const state = running + ? phase || 'checking' + : phase === 'deleted' || phase === 'never-enrolled' + ? phase + : phase === 'unverified' + ? 'unverified' + : !automaticCloudBackup + ? 'disabled' + : ['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} + + {running && } + + {copy[state].body} + + {slow && ( + + {copy.slow} + + )} + {running && ( + + {copy.leave} + + )} + + {deletionRecorded && state !== 'deleted' && ( + + + {copy.deletionRecordedTitle} + + + {copy.deletionRecordedBody} + + + )} + + {copy.retainedCopyNotice} + + {!running && ( + + {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()} /> + + + + ); +} +const styles = StyleSheet.create({ + 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/AppSettings/ManageWallets.tsx b/src/screens/AppSettings/ManageWallets.tsx index aa67be5207..0fe9f6f0ee 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, @@ -51,11 +49,33 @@ 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'; +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', SHOWALL = 'SHOWALL', } + +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, @@ -66,7 +86,8 @@ function ListItem({ icon, type, isWatchOnly, -}) { + canDelete, +}: ListItemProps) { const { colorMode } = useColorMode(); const isDarkMode = colorMode === 'dark'; const { getSatUnit, getBalance, getCurrencyIcon } = useBalance(); @@ -75,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 { isOnL2Above } = usePlan(); const navigation: any = useNavigation(); const dispatch = useDispatch(); + // The verified phase changes after the producer's final remote readback. + // Subscribe so Delete becomes available without leaving this screen. + useSelector((state: any) => state.bhr); const [selectedWallet, setSelectedWallet] = useState(null); const { showToast } = useToastMessage(); const miniscriptPathSelectorRef = useRef(null); - useEffect(() => { - if (selectedWallet?.id) { - if (relayVaultUpdate) { - showToast(errorText.vaultDeleted, ); - } - if (relayVaultError) { - showToast(errorText.failedToDeleteVault, ); - } - dispatch(resetRealyVaultState()); - } - }, [relayVaultUpdate, relayVaultError]); - const isWallet = selectedWallet?.entityKind === EntityKind.WALLET; const isUSDTWallet = selectedWallet?.entityKind === EntityKind.USDT_WALLET; const onProceed = () => { if (passwordMode === PasswordMode.DEFAULT) { updateWalletVisibility(selectedWallet, false); - showToast(errorText.walletUnhidden, ); } if (passwordMode === PasswordMode.SHOWALL) { setshowAll(true); @@ -207,20 +220,64 @@ function ManageWallets() { }; const deleteSelectedEntity = async () => { + if (selectedWallet && !canDeleteRecoveryEntity(selectedWallet)) { + showToast('Verify your assisted backup before deleting', ); + return; + } if (selectedWallet && selectedWallet.entityKind === EntityKind.VAULT) { - 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) { - 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', ); } }; + 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; @@ -240,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); @@ -402,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) { @@ -412,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/AppSettings/Node/NodeSelection.tsx b/src/screens/AppSettings/Node/NodeSelection.tsx index 71f7579a97..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'; @@ -32,8 +30,12 @@ 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'; +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 ( @@ -65,9 +68,9 @@ const PublicServer = ({ currentlySelectedNode, handleSelectNode }) => { ))} ); -}; +} -const NodeSelection = () => { +function NodeSelection() { const dispatch = useDispatch(); const navigation = useNavigation(); const { colorMode } = useColorMode(); @@ -88,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(); @@ -96,6 +101,12 @@ const NodeSelection = () => { }, [nodes.length]); const onSaveCallback = async (nodeDetail: NodeDetail) => { + 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 @@ -110,11 +121,20 @@ const NodeSelection = () => { } const nodeToSave = { ...nodeDetail }; - const { saved, connectionError: nodeConnectionError } = await Node.save(nodeToSave, nodeList); + 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(updateAppImage({ wallets: null, signers: null, updateNodes: true })); + 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 ); @@ -144,6 +164,11 @@ const NodeSelection = () => { }; const onConnectToNode = async (selectedNode: NodeDetail) => { + const originAppId = renderedAppId; + const originBackupAccountSession = renderedAccountSession; + const isCurrent = () => Node.isAccountActive(originAppId) && + currentBackupAccountSession() === originBackupAccountSession; + if (!originAppId || !isCurrent()) return; let nodes = Node.getAllNodes(); if ( currentlySelectedNode && @@ -152,6 +177,7 @@ const NodeSelection = () => { ) { // disconnect currently selected node(if connected) await Node.disconnect(currentlySelectedNode); + if (!isCurrent()) return; currentlySelectedNode.isConnected = false; Node.update(currentlySelectedNode, { isConnected: currentlySelectedNode.isConnected }); @@ -167,11 +193,16 @@ const NodeSelection = () => { setLoading(true); const node = { ...selectedNode }; - const { connected, connectedTo, error } = await Node.connectToSelectedNode(node); + const { connected, connectedTo, error } = await Node.connectToSelectedNode(node, originAppId, originBackupAccountSession); + if (!isCurrent()) { + setLoading(false); + return; + } 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) => { @@ -305,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 dc74de79ca..12a7473804 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,13 +19,16 @@ 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 ServerItem from './components/ServerItem'; +import { CommonActions, useFocusEffect, useNavigation } from '@react-navigation/native'; 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(); @@ -39,6 +42,8 @@ function ElectrumDisconnectWarningContent() { ); } +type SelectedNodeAction = { node: NodeDetail; originAppId: string; originBackupAccountSession: number }; + function NodeSettings() { const { colorMode } = useColorMode(); const navigation = useNavigation(); @@ -48,84 +53,127 @@ function NodeSettings() { const { showToast } = useToastMessage(); 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); - const [nodeToDelete, setNodeToDelete] = useState(null); + 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(() => { + const appId = Node.currentAccountId(); + setNodeListAppId(appId ?? null); + setNodeListAccountSession(currentBackupAccountSession()); + setNodeList(appId ? Node.getAllNodes() : []); + }, []) + ); - const onDelete = async (selectedItem: NodeDetail) => { + 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); - const status = Node.delete(selectedItem); - if (status) { + if (!Node.isAccountActive(originAppId) || currentBackupAccountSession() !== originBackupAccountSession) return; + + 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 })); + dispatch({ + ...updateAppImage({ wallets: null, signers: null, updateNodes: true }, originAppId), + originBackupAccountSession, + originBackupWriteIntent, + committedOperation, + }); } }; 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 - ); - } - + const originAppId = nodeListAppId; + 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 (!isCurrent()) return; + Node.update(currentlySelectedNode, { isConnected: false }); + } - 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 })); + const { connected, connectedTo, error } = await Node.connectToSelectedNode( + selectedNode, + originAppId, + originBackupAccountSession + ); + if (!isCurrent()) return; + if (connected) { + Node.update(selectedNode, { isConnected: true }); + dispatch(electrumClientConnectionExecuted({ successful: true, connectedTo })); + showToast(`${errorString.ConnectedTo} ${connectedTo}`, ); + } else { + dispatch(electrumClientConnectionExecuted({ successful: false, error })); + } + } catch (error) { + if (isCurrent()) { + dispatch( + electrumClientConnectionExecuted({ + successful: false, + error: error?.message || String(error), + }) + ); + } + } finally { + if (isCurrent()) { + setLoading(false); + setNodeList(Node.getAllNodes()); + } } - - updatedNodes = updatedNodes.map((node) => - node.id === selectedNode.id ? { ...selectedNode } : node - ); - setNodeList(updatedNodes); - setLoading(false); }; - const onDisconnectToNode = async (selectedNode: NodeDetail) => { + 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); - 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 + if (!isCurrent()) return; + 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, ); + if (isCurrent()) { + showToast(errorString.failedToDiConnect, ); + } + } finally { + if (isCurrent()) { + setLoading(false); + setNodeList(Node.getAllNodes()); + } } }; @@ -147,8 +195,14 @@ function NodeSettings() { item={item} onDelete={onDelete} onConnectToNode={onConnectToNode} - setNodeToDelete={setNodeToDelete} - setNodeToDisconnect={setNodeToDisconnect} + setNodeToDelete={(node: NodeDetail) => { + if (nodeListAppId && nodeListAccountSession !== null) + setNodeToDelete({ node, originAppId: nodeListAppId, originBackupAccountSession: nodeListAccountSession }); + }} + setNodeToDisconnect={(node: NodeDetail) => { + if (nodeListAppId && nodeListAccountSession !== null) + setNodeToDisconnect({ node, originAppId: nodeListAppId, originBackupAccountSession: nodeListAccountSession }); + }} setElectrumDisconnectWarningVisible={setElectrumDisconnectWarningVisible} /> )} @@ -161,11 +215,11 @@ function NodeSettings() { {isNoNodeConnected && ( - {'Currently in Offline Mode'} + Currently in Offline Mode )} @@ -192,10 +246,10 @@ function NodeSettings() { buttonCallback={async () => { setElectrumDisconnectWarningVisible(false); if (nodeToDisconnect) { - await onDisconnectToNode(nodeToDisconnect); + await onDisconnectToNode(nodeToDisconnect.node, nodeToDisconnect.originAppId, nodeToDisconnect.originBackupAccountSession); setNodeToDisconnect(null); } else if (nodeToDelete) { - await onDelete(nodeToDelete); + await onDelete(nodeToDelete.node, nodeToDelete.originAppId, nodeToDelete.originBackupAccountSession); setNodeToDelete(null); } }} diff --git a/src/screens/BackupWallet/ViewRecoveryKeyScreen.tsx b/src/screens/BackupWallet/ViewRecoveryKeyScreen.tsx index 7ede2e2cde..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; @@ -41,22 +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 || backupAllFailure) { - dispatch(setBackupAllSuccess(false)); - dispatch(setBackupAllFailure(false)); - dispatch(setAutomaticCloudBackup(true)); - setLoader(false); - showToast(backupTxt.recoveryKeyBackedUpSuccessfully, ); - navigation.goBack(); - } - }, [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/Channel/ConnectChannel.tsx b/src/screens/Channel/ConnectChannel.tsx index c4b3dec289..9ef7342a43 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'; @@ -36,8 +36,9 @@ 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'; import WalletOperations from 'src/services/wallets/operations'; import { getKeyUID } from 'src/utils/utilities'; @@ -52,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} + + + + )} + + ); } @@ -199,7 +192,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/HelpAi/HelpAiChat.tsx b/src/screens/HelpAi/HelpAiChat.tsx index afd7bf0d2f..df90b185e5 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'; @@ -37,11 +38,13 @@ import { useAppDispatch, useAppSelector } from 'src/store/hooks'; import { appendHelpAiMessage, createHelpAiThread, + discardHelpAiFailedMessage, incrementHelpAiIssueCount, setHelpAiChatMeta, setHelpAiDraft, setHelpAiDraftStatus, setHelpAiEscalationStage, + setHelpAiFailedMessageId, setHelpAiRawMessages, type HelpAiDraftStatus, type HelpAiRenderMessage, @@ -65,6 +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 { + 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 => { @@ -139,6 +148,12 @@ 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 discardedFailedMessageId = useRef(null); const generatedConversationId = useMemo(() => `conv_${Date.now().toString(36)}`, []); const conversationId = route?.params?.conversationId || generatedConversationId; const initialPrompt = route?.params?.prefillText || ''; @@ -153,6 +168,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 +179,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 +196,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 +241,38 @@ const HelpAiChat = ({ navigation, route }) => { } }; - const sendToChat = async (text: string) => { - if (!text.trim()) return; + 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); + 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,16 +328,23 @@ 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); } }; + const discardFailedMessage = () => { + if (!failedMessage || sendingRef.current) return; + discardedFailedMessageId.current = failedMessage.id; + dispatch(discardHelpAiFailedMessage({ conversationId })); + }; + const submitDraftIssue = async () => { if (!draft || !chatMeta) return; if (!appId) { @@ -496,6 +545,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 && ( @@ -558,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 ? ( @@ -574,12 +641,32 @@ const HelpAiChat = ({ navigation, route }) => { - {lastFailedText ? ( - sendToChat(lastFailedText)}> - - Retry last failed message + {failedMessage ? ( + + + Message not sent. Retry or discard it before sending another. - + + sendToChat(failedMessage.text, failedMessage.id)} + disabled={sending} + > + + Retry + + + + + Discard + + + + ) : null} )} - {content} + {/* Keep tab descendants (including elevated FABs) out of the footer's native parent. */} + + {content} + { 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/Home/Notifications/NotificationsCenter.tsx b/src/screens/Home/Notifications/NotificationsCenter.tsx index 26abf067b2..16fdfe7ba9 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'; @@ -91,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; @@ -298,7 +299,7 @@ const Card = memo(({ uai }: CardProps) => { text: common.View, cta: () => { dispatch(uaiActioned({ uaiId: uai.id, action: false })); - dispatch(backupAllSignersAndVaults()); + navigtaion.dispatch(CommonActions.navigate('AssistedBackupStatus')); }, }, }, diff --git a/src/screens/Home/components/Keys/ManageKeys.tsx b/src/screens/Home/components/Keys/ManageKeys.tsx index 4532415f77..75d4035aca 100644 --- a/src/screens/Home/components/Keys/ManageKeys.tsx +++ b/src/screens/Home/components/Keys/ManageKeys.tsx @@ -1,12 +1,13 @@ 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'; 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'; @@ -106,6 +107,14 @@ const ManageKeys = ({ addedSigner }) => { onPress={handleModalOpen} containerStyle={{ right: wp(22.5) }} /> + navigation.dispatch(CommonActions.navigate('DeleteKeys'))} + style={{ paddingVertical: hp(12) }} + > + {translations.signer.manageKeys} + 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, @@ -51,7 +52,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 ( @@ -68,6 +71,7 @@ const KeeperSettings = ({ route }) => { icon={} showDot={false} /> + {BackupFeedback} { 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 { 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 [pullRefresh, setPullRefresh] = 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 [pickWalletType, setPickWalletType] = useState(false); - const [createUsdtWallet, setCreateUsdtWallet] = useState(false); const syncing = ELECTRUM_CLIENT.isClientConnected && Object.values(walletSyncing).some((isSyncing) => isSyncing); @@ -140,12 +140,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); @@ -157,52 +151,8 @@ const HomeWallet = () => { } }, [pendingDustToast]); - 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 - } - }; - const pullDownRefresh = () => { - setPullRefresh(true); - - dispatch(autoSyncWallets(false, false, true)); - 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}`, ); - } + autoRefresh(); }; const CREATE_WALLET_OPTIONS = [ @@ -211,7 +161,7 @@ const HomeWallet = () => { subtitle: walletText.createWalletDesc, icon: , onPress: () => { - setShowAddWalletModal(false); + collaborativeHandoff.closeAdd(); navigation.navigate('AddNewWallet'); }, id: 'newWallet', @@ -221,7 +171,7 @@ const HomeWallet = () => { subtitle: walletText.restoreExistingWallet, icon: , onPress: () => { - setShowAddWalletModal(false); + collaborativeHandoff.closeAdd(); navigation.navigate('VaultConfigurationCreation'); }, id: 'importWallet', @@ -230,42 +180,10 @@ const HomeWallet = () => { title: common.collaborativeWallet, subtitle: walletText.walletWithFamily, icon: , - onPress: handleCollaborativeWalletCreation, + onPress: collaborativeHandoff.selectCollaborative, 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={collaborativeHandoff.openAdd} /> { ItemSeparatorComponent={() => } /> setShowAddWalletModal(false)} + close={collaborativeHandoff.closeAdd} + onModalHide={collaborativeHandoff.waitForAddHide ? collaborativeHandoff.onAddHide : undefined} textColor={`${colorMode}.textGreen`} subTitleColor={`${colorMode}.modalSubtitleBlack`} showCloseIcon @@ -309,96 +228,17 @@ const HomeWallet = () => { )} /> setCreateUsdtWallet(false)} - textColor={`${colorMode}.textGreen`} - subTitleColor={`${colorMode}.modalSubtitleBlack`} - showCloseIcon - Content={() => ( - - {CREATE_USDT_WALLET_OPTIONS.map((option, index) => ( - - ))} - - )} - /> - 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); - dispatch(resetCollaborativeSession()); - setTimeout(() => { - navigation.navigate('SetupCollaborativeWallet'); - }, 500); - }} - buttonCallback={() => { - setCollabSessionExistsModalVisible(false); - 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} - - )} + secondaryCallback={() => collaborativeHandoff.finishSession('new')} + buttonCallback={() => collaborativeHandoff.finishSession('continue')} /> ); @@ -457,26 +297,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/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/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..96230889fa 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'; @@ -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' }); @@ -104,6 +110,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); @@ -160,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(); @@ -177,53 +222,17 @@ 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) => { - 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(() => { @@ -267,8 +276,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({ @@ -283,9 +298,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)); + } } }; @@ -408,9 +426,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/NewKeeperAppScreen/NewKeeperAppScreen.tsx b/src/screens/NewKeeperAppScreen/NewKeeperAppScreen.tsx index 00477729a0..6b2f9b489e 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'; @@ -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'; @@ -28,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(); @@ -113,24 +115,32 @@ 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 renderedAccountSession = currentBackupAccountSession(); const isMultiAccountFlow = !!useAppSelector((state) => state.account).allAccounts.length; useEffect(() => { + let active = true; + const accountSession = currentBackupAccountSession(); if (appCreated) { setInitiating(false); - updateFCM(); + updateFCM(() => active && accountSession === currentBackupAccountSession()); } 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) { // } @@ -144,12 +154,25 @@ function NewKeeperApp({ navigation }: { navigation }) { useEffect(() => { if (appRecreated) { - setTimeout(() => { - navigation.replace('App', { screen: 'Home' }); + const accountSession = currentBackupAccountSession(); + const timer = setTimeout(() => { + if (accountSession === currentBackupAccountSession()) { + 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); @@ -157,19 +180,24 @@ function NewKeeperApp({ navigation }: { navigation }) { }, []); useEffect(() => { + let active = true; + const accountSession = currentBackupAccountSession(); if (keeperInitiating) { setModalVisible(true); - createNewApp(); + createNewApp(() => active && accountSession === currentBackupAccountSession()); } + 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()); } } @@ -318,23 +346,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} - /> {}} @@ -347,9 +358,16 @@ function NewKeeperApp({ navigation }: { navigation }) { subTitleColor={`${colorMode}.modalSubtitleBlack`} buttonText={appCreated ? common.next : null} buttonCallback={() => { + if ( + !navigationActive.current || + nextNavigation.current !== null || + renderedAccountSession !== currentBackupAccountSession() + ) return; setModalVisible(false); - setTimeout(() => { - navigation.replace('App', { screen: 'Home' }); + nextNavigation.current = setTimeout(() => { + if (renderedAccountSession === currentBackupAccountSession()) { + navigation.replace('App', { screen: 'Home' }); + } }, 500); }} subTitleWidth={wp(300)} @@ -364,6 +382,7 @@ function NewKeeperApp({ navigation }: { navigation }) { Content={Box} buttonText={common.retry} buttonCallback={() => { + dispatch(setAppCreationError(false)); setInitiating(true); }} modalBackground={`${colorMode}.modalWhiteBackground`} diff --git a/src/screens/QRScreens/RegisterWithChannel.tsx b/src/screens/QRScreens/RegisterWithChannel.tsx index f678dadc36..0f98be7318 100644 --- a/src/screens/QRScreens/RegisterWithChannel.tsx +++ b/src/screens/QRScreens/RegisterWithChannel.tsx @@ -22,33 +22,29 @@ 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 { 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'; 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 +60,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 +81,31 @@ 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 (error) { + showToast( + error instanceof InvalidChannelQRCodeError + ? errorText.QrScannedDesptopInvalid + : errorText.failedToConnectDesktop, + + ); + return false; + } }; useEffect(() => { diff --git a/src/screens/QRScreens/SignWithChannel.tsx b/src/screens/QRScreens/SignWithChannel.tsx index 9983e3da1e..675ac4173a 100644 --- a/src/screens/QRScreens/SignWithChannel.tsx +++ b/src/screens/QRScreens/SignWithChannel.tsx @@ -26,7 +26,8 @@ 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 { 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'; @@ -37,23 +38,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 +102,32 @@ 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 (error) { + showToast( + error instanceof InvalidChannelQRCodeError + ? errorText.QrScannedDesptopInvalid + : errorText.failedToConnectDesktop, + + ); + return false; + } }; useEffect(() => { 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/Recovery/EnterSeedScreen.tsx b/src/screens/Recovery/EnterSeedScreen.tsx index ecace068df..3e8bbeb55e 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'; @@ -27,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'; @@ -66,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(); @@ -77,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([]); @@ -100,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' }); @@ -116,23 +133,57 @@ 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); - } else openInvalidSeedsModal(); + } else if (appImageError === 'Invalid mnemonic') { + openInvalidSeedsModal(); + } else { + setRecoveryLoading(false); + showToast(seed.recoveryIncomplete, ); + dispatch(setAppImageError('')); + } } - }, [appImageError]); + }, [appImageError, isRecovery, recoveryDiscovery.status]); const generateSeedWordsArray = useCallback(() => { const seedArray = []; @@ -315,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, ); @@ -588,7 +641,6 @@ function EnterSeedScreen({ route, navigation }) { ); }; - const isRecovery = !isHealthCheck && !isImport && !isSignTransaction && !isIdentification; const rememberModalContent = () => { return ( @@ -627,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)} /> (''); - 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 || backupAllFailure) { - if (!automaticCloudBackup) setAsbEnabled(true); - dispatch(setBackupAllSuccess(false)); - dispatch(setBackupAllFailure(false)); - dispatch(setAutomaticCloudBackup(true)); setBackupSuccessModal(true); } - }, [backupAllSuccess, backupAllFailure]); + }, [backupMethod]); const getNoteSubtitle = () => { @@ -324,7 +299,6 @@ function ExportSeedScreen({ route, navigation }) { }) ); } else { - if (!automaticCloudBackup) setAsbEnabled(true); dispatch(seedBackedUp()); dispatch(setRecoveryKeyBackedUp({ appId, status: true })); } @@ -357,7 +331,6 @@ function ExportSeedScreen({ route, navigation }) { {BackupWallet.backupSuccessParagraph} - {asbEnabled && {BackupWallet.assistedServerBackupEnabled}} )} @@ -383,7 +356,6 @@ function ExportSeedScreen({ route, navigation }) { )} /> - ); } diff --git a/src/screens/Send/SendConfirmation.tsx b/src/screens/Send/SendConfirmation.tsx index c142e84d6d..6b3150e644 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'; @@ -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; @@ -100,6 +101,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, @@ -312,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, @@ -328,8 +335,10 @@ function SendConfirmation({ route }) { ), }, note, - }) - ); + }, originAppId), + originBackupAccountSession, + originBackupWriteIntent, + }); }, 200); }) .catch(() => showToast(errorText.failedToFetchCurrentBlock, )); @@ -1058,4 +1067,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/SigningDevices/DeleteKeys.tsx b/src/screens/SigningDevices/DeleteKeys.tsx index aad8b4b531..77a1bb859e 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'; @@ -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'; @@ -14,14 +13,15 @@ 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 { CommonActions, useNavigation } from '@react-navigation/native'; +import { useQuery } from '@realm/react'; +import { RealmSchema } from 'src/storage/realm/enum'; +import { getJSONFromRealmObject } from 'src/storage/realm/utils'; +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'; @@ -39,6 +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 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(); @@ -59,14 +78,28 @@ function DeleteKeys({ route }) { ); const dispatch = useDispatch(); + const renderedAppId = useAppSelector((state) => state.storage.appId); + const renderedSession = currentBackupAccountSession(); const navigation = useNavigation(); const [unhidingKeyUID, setUnhidingKeyUID] = useState(''); - const [multipleHidden, setMultipleHidden] = useState(false); - const { allVaults } = useVault({ includeArchived: true }); - const { archivedVaults } = useArchivedVault(); + // Deletion safety must include hidden/canary wallets and both networks. + // Presentation hooks intentionally filter those records out. + 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( @@ -81,20 +114,65 @@ 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 onSuccess = () => { - if (signerToDelete) { - const involvedArchivedVaults = archivedVaults.filter((vault) => - vault.signers.find((s) => getKeyUID(s) === getKeyUID(signerToDelete)) - ); - if (involvedArchivedVaults.length) { - dispatch(archiveSigningDevice([signerToDelete])); + const cancelDeletion = (intent: DeletionIntent | null) => { + if (pendingDeletion.current !== intent) return; + pendingDeletion.current = null; + setDeletionIntent(null); + setConfirmPassVisible(false); + }; + + 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); + // 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. + const references = getReferences(signer); + const vaultsInvolved = references.filter((vault) => !vault.archived); + if (vaultsInvolved.length) { + setWarningKeyUID(getKeyUID(signer)); + setHideWarning(true); + return; + } + if (references.some((vault) => vault.archived)) { + dispatch({ + ...archiveSigningDevice([signer]), originAppId: intent.appId, + originBackupAccountSession: intent.accountSession, + }); } else { - dispatch(deleteSigningDevice([signerToDelete])); + dispatch({ + ...deleteSigningDevice([signer]), originAppId: intent.appId, + originBackupAccountSession: intent.accountSession, + }); } - setDeletedSigner(signerToDelete); - setSignerToDelete(null); + setDeletedSigner(signer); } }; const onForceProceed = () => { @@ -125,20 +203,28 @@ function DeleteKeys({ route }) { } }; + const getReferences = (signer: Signer): Vault[] => + readVaults() + .filter((vault) => vault.signers.some((s) => getKeyUID(s) === getKeyUID(signer))); + const handleDelete = (signer: Signer) => { - setMultipleHidden(false); - const vaultsInvolved = allVaults.filter( - (vault) => !vault.archived && vault.signers.find((s) => getKeyUID(s) === getKeyUID(signer)) - ); + const origin = { appId: renderedAppId, accountSession: renderedSession }; + if (!isCurrentDeletionOrigin(origin)) return; + 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 = { ...origin, signer }; + pendingDeletion.current = intent; + setDeletionIntent(intent); setConfirmPassVisible(true); - setSignerToDelete(signer); }; useEffect(() => { @@ -193,7 +279,7 @@ function DeleteKeys({ route }) { showsHorizontalScrollIndicator={false} > {vaultsUsed.map((vault) => ( - + - {multipleHidden + {showMultipleReferences ? signerText.deleteMultipleVaultInstruction : signerText.deleteVaultInstruction} @@ -217,7 +303,7 @@ function DeleteKeys({ route }) { return ( - + {allUnhideSigners.length === 0 && !showAll ? ( @@ -240,13 +326,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 ? : } @@ -287,13 +371,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} @@ -307,7 +395,9 @@ function DeleteKeys({ route }) { navigation.dispatch(CommonActions.navigate('ManageWallets')); }} textColor={`${colorMode}.textGreen`} - Content={() => } + Content={() => + showReferenceDetails ? : null + } /> setConfirmPassVisible(false)} + close={() => cancelDeletion(deletionIntent)} title={signerText.enterPasscode} subTitleWidth={wp(240)} subTitle={signerText.confirmPasscodeToDeleteKey} @@ -371,8 +461,8 @@ function DeleteKeys({ route }) { )} setConfirmPassVisible(false)} - onSuccess={onSuccess} + close={() => cancelDeletion(deletionIntent)} + onSuccess={() => onSuccess(deletionIntent)} /> )} 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/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/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 745964ce3e..c8e751b50e 100644 --- a/src/screens/USDT/UsdtDetails.tsx +++ b/src/screens/USDT/UsdtDetails.tsx @@ -13,10 +13,8 @@ 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'; +import KeeperModal from 'src/components/KeeperModal'; function TransactionsAndUTXOs({ transactions, setPullRefresh, pullRefresh, wallet }) { const [initialLoading, setInitialLoading] = useState(false); @@ -39,14 +37,14 @@ 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' }); + const [showSendPaused, setShowSendPaused] = useState(false); return ( @@ -64,21 +62,26 @@ const UsdtDetails = ({ route }) => { - navigation.dispatch(CommonActions.navigate('sendUsdt', { usdtWallet })) - } + sendCallback={() => setShowSendPaused(true)} 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`} + visible={showSendPaused} + close={() => setShowSendPaused(false)} + title={usdtWalletText.sendingPausedTitle} + subTitle={usdtWalletText.sendingPausedBody} modalBackground={`${colorMode}.modalWhiteBackground`} - Content={() => { - return ( - - { - setShowMore(false); - }} - Icon={} - /> - - ); - }} + textColor={`${colorMode}.textGreen`} + subTitleColor={`${colorMode}.modalSubtitleBlack`} + buttonText={common.ok} + buttonCallback={() => setShowSendPaused(false)} /> ); @@ -168,7 +161,7 @@ const styles = StyleSheet.create({ paddingHorizontal: 18, }, walletContainer: { - paddingTop: wp(30), + paddingTop: hp(8), paddingBottom: 20, flex: 1, justifyContent: 'space-between', @@ -227,6 +220,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, }, diff --git a/src/screens/USDT/UsdtTransactionDetail.tsx b/src/screens/USDT/UsdtTransactionDetail.tsx index 521ac6f099..3b1e0778b6 100644 --- a/src/screens/USDT/UsdtTransactionDetail.tsx +++ b/src/screens/USDT/UsdtTransactionDetail.tsx @@ -18,13 +18,15 @@ 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, + isUSDTStatusUnavailable, + 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 +54,10 @@ 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 unavailableStatus = isUSDTStatusUnavailable(transaction); const status = transaction.status; let transactionType: string; if (transaction.to === wallet.accountStatus.gasFreeAddress) { @@ -66,7 +72,6 @@ const UsdtTransactionDetail = ({ route }) => { const [visible, setVisible] = useState(false); const [updatingLabel, setUpdatingLabel] = useState(false); const close = () => setVisible(false); - const { showToast } = useToastMessage(); function InfoCard({ title, @@ -120,12 +125,14 @@ const UsdtTransactionDetail = ({ route }) => { - {transactionType === 'Received' ? ( + {legacyRequest ? null : transactionType === 'Received' ? ( colorMode === 'dark' ? ( ) : ( @@ -146,6 +153,11 @@ const UsdtTransactionDetail = ({ route }) => { + {legacyRequest && ( + + {usdtWalletText.requestedAmount} + + )} {amount} USDT @@ -182,14 +194,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 +221,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`} showIcon={false} letterSpacing={2.4} /> - {transaction.transferFee || transaction.fee ? ( + {!legacyRequest && (transaction.transferFee || transaction.fee) ? ( { letterSpacing={2.4} /> ) : null} - {transaction.activateFee ? ( + {!legacyRequest && transaction.activateFee ? ( { +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); + // 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 + : chainConfirmed + ? 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 = statusUnavailable + ? Colors.indigoblue + : chainConfirmed + ? Colors.TropicalTeal + : status === GasFreeTransferStatus.CONFIRMING + ? Colors.darkOrange + : Colors.indigoblue; return ( { style={styles.container} > - {status === GasFreeTransferStatus.SUCCEED ? 'SUCCESS' : status} + {statusUnavailable + ? translations.usdtWalletText.statusUnavailable + : chainConfirmed + ? 'SUCCESS' + : status} ); @@ -40,7 +54,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/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/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..5960a974a5 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, ]); @@ -210,7 +212,7 @@ function AddReserveKey({ route }) { {vaultTranslations.inheritanceKeyActivation} - {vaultTranslations.availableAfterDelay} + {vaultTranslations.inheritanceActivationNote} @@ -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/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/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/ResetEmergencyKey.tsx b/src/screens/Vault/ResetEmergencyKey.tsx index 838e055488..f99c452800 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(); @@ -213,7 +213,7 @@ function ResetEmergencyKey({ route }) { {vaultText.chooseNewActivationTimeTitle} - {vaultText.chooseNewActivationTimeDesc} + {vaultText.chooseNewEmergencyActivationTimeDesc} 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/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/screens/Vault/VaultDetails.tsx b/src/screens/Vault/VaultDetails.tsx index 4ae46546d7..c5f4b92445 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'; @@ -155,9 +155,11 @@ 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); + const { refreshing: pullRefresh, refresh } = useWalletRefresh(); const { vaultSigners: keys } = useSigners(vault.id); const transactions = useMemo( () => @@ -372,9 +374,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(() => { @@ -442,7 +442,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/Vault/VaultMigrationController.tsx b/src/screens/Vault/VaultMigrationController.tsx index bfb688e194..d0ea0f63d7 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 { @@ -304,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); }; @@ -375,34 +400,14 @@ function VaultMigrationController({ return; } - let initialTimelock = 0; + const initialTimelock = resolveInitialTimelock( + miniscriptTypes, + initialTimelockDuration, + (label) => getTimelockDuration(label, bitcoinNetworkType) + ); 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; - // } - // } - - if (initialTimelockDuration) { - initialTimelock = getTimelockDuration(initialTimelockDuration, bitcoinNetworkType); - if (!initialTimelock) { - showToast('Failed to determine initial timelock duration', ); - return; - } - } + const currentTimeForLock = await getCurrentTimeForLock(); const inheritanceSignerWithTimelocks = []; const emergencySignerWithTimelocks = []; @@ -416,7 +421,7 @@ function VaultMigrationController({ } inheritanceSignerWithTimelocks.push({ signer: key, - timelock: getCurrentTimeForLock() + initialTimelock + timelock, + timelock: currentTimeForLock + initialTimelock + timelock, }); } } @@ -430,7 +435,7 @@ function VaultMigrationController({ } emergencySignerWithTimelocks.push({ signer: key, - timelock: getCurrentTimeForLock() + initialTimelock + timelock, + timelock: currentTimeForLock + initialTimelock + timelock, }); } } @@ -440,7 +445,7 @@ function VaultMigrationController({ inheritanceSignerWithTimelocks, emergencySignerWithTimelocks, vaultInfo.vaultScheme, - initialTimelock ? getCurrentTimeForLock() + initialTimelock : 0 + initialTimelock ? currentTimeForLock + initialTimelock : 0 ); if (!miniscriptElements) { 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/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/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/screens/ViewTransactions/TransactionHistory.tsx b/src/screens/ViewTransactions/TransactionHistory.tsx index c546c47ce8..bfd56a6faa 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 unknown 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/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/screens/WalletDetails/components/Transactions.tsx b/src/screens/WalletDetails/components/Transactions.tsx index 3e83162957..8aa5b810a6 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'; @@ -48,12 +46,12 @@ function TransactionItem({ item, wallet, navigation, index }) { function Transactions({ transactions, - setPullRefresh, + setPullRefresh = undefined, pullRefresh, currentWallet, - setInitialLoading, + setInitialLoading = undefined, + onBitcoinRefresh = undefined, }) { - 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/services/backend/Relay.ts b/src/services/backend/Relay.ts index 8a35b6ae26..78db61fda9 100644 --- a/src/services/backend/Relay.ts +++ b/src/services/backend/Relay.ts @@ -1,3 +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 { @@ -17,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; @@ -106,16 +113,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; + }, intent: BackupWriteIntent | undefined): 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, false, requireBackupWriteIntent(intent)); const data = res.data || res.json; return data; } catch (err) { @@ -126,13 +142,14 @@ export default class Relay { public static migrateXfp = async ( appId: string, - signerChanges: SignerChange[] + signerChanges: SignerChange[], + intent: BackupWriteIntent | undefined ): Promise<{ updated: boolean; err?: string; }> => { try { - const res = await RestClient.post(`${RELAY}migrateXfps`, { appId, signerChanges }); + const res = await backupPost(`${RELAY}migrateXfps`, { appId, signerChanges }, false, requireBackupWriteIntent(intent)); const data = res.data || res.json; return data; } catch (err) { @@ -142,7 +159,8 @@ export default class Relay { }; public static deleteAppImageEntity = async ( - entityList + entityList, + intent: BackupWriteIntent | undefined ): Promise<{ status: string; updated: boolean; @@ -150,7 +168,7 @@ export default class Relay { message?: string; }> => { try { - const res = await RestClient.post(`${RELAY}deleteAppImageEntity`, entityList); + const res = await backupPost(`${RELAY}deleteAppImageEntity`, entityList, false, requireBackupWriteIntent(intent)); const data = res.data || res.json; return data; } catch (err) { @@ -160,7 +178,8 @@ export default class Relay { }; public static deleteVaultImage = async ( - entityList + entityList, + intent: BackupWriteIntent | undefined ): Promise<{ status: string; updated: boolean; @@ -168,7 +187,7 @@ export default class Relay { message?: string; }> => { try { - const res = await RestClient.post(`${RELAY}deleteVaults`, entityList); + const res = await backupPost(`${RELAY}deleteVaults`, entityList, false, requireBackupWriteIntent(intent)); const data = res.data || res.json; return data; } catch (err) { @@ -178,7 +197,8 @@ export default class Relay { }; public static updateVaultImage = async ( - vaultData + vaultData, + intent: BackupWriteIntent | undefined ): Promise<{ status?: number; data?: { @@ -188,7 +208,7 @@ export default class Relay { message?: string; }> => { try { - const res = await RestClient.post(`${RELAY}updateVaultImage`, vaultData); + const res = await backupPost(`${RELAY}updateVaultImage`, vaultData, false, requireBackupWriteIntent(intent)); const data = res.data || res.json; return data; } catch (err) { @@ -213,32 +233,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'); } }; @@ -270,29 +305,30 @@ export default class Relay { public static modifyLabels = async ( appId: string, addLabels: any[], - deleteLabels: any[] + deleteLabels: any[], + intent: BackupWriteIntent | undefined ): Promise<{ updated: boolean; }> => { try { - const res = (await RestClient.post(`${RELAY}modifyLabels`, { + const res = (await backupPost(`${RELAY}modifyLabels`, { appId, addLabels, deleteLabels, - })) as AxiosResponse; + }, false, requireBackupWriteIntent(intent))) as AxiosResponse; const { updated } = res.data || res.json; return { 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.'); } }; 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) { @@ -427,8 +463,10 @@ export default class Relay { }; public static backupAllSignersAndVaults = async ( - allData + allData, + intent: BackupWriteIntent | undefined ): Promise<{ + updated?: boolean; status?: number; data?: { updated: boolean; @@ -437,7 +475,7 @@ export default class Relay { message?: string; }> => { try { - const res = await RestClient.post(`${RELAY}backupAllSignersAndVaults`, allData); + const res = await backupPost(`${RELAY}backupAllSignersAndVaults`, allData, false, requireBackupWriteIntent(intent)); const data = res.data || res.json; return data; } catch (err) { @@ -472,7 +510,8 @@ export default class Relay { }; public static deleteBackup = async ( - body + body, + intent: BackupWriteIntent | undefined ): Promise<{ status?: number; data?: { @@ -482,7 +521,7 @@ export default class Relay { message?: string; }> => { try { - const res = await RestClient.post(`${RELAY}deleteBackup`, body); + const res = await backupPost(`${RELAY}deleteBackup`, body, true, requireBackupWriteIntent(intent)); const data = res.data || res.json; return data; } catch (err) { @@ -804,4 +843,3 @@ export default class Relay { } }; } - diff --git a/src/services/backup/authority.ts b/src/services/backup/authority.ts new file mode 100644 index 0000000000..e5e95cfd34 --- /dev/null +++ b/src/services/backup/authority.ts @@ -0,0 +1,238 @@ +import './encoding'; +import { Buffer } from 'buffer'; +import { HDKey, HARDENED_OFFSET } from '@scure/bip32'; +import { mnemonicToSeedSync, validateMnemonic } from '@scure/bip39'; +import { wordlist } from '@scure/bip39/wordlists/english.js'; +import { secp256k1 } from '@noble/curves/secp256k1.js'; +import { equalBytes } from '@noble/curves/utils.js'; +import { hkdf } from '@noble/hashes/hkdf.js'; +import { sha256 } from '@noble/hashes/sha2.js'; +import { bytesToHex, hexToBytes, utf8ToBytes } from '@noble/hashes/utils.js'; + +export type ProtectedBackupPath = '/backup/v3/enroll' | '/backup/v3/snapshot' | '/backup/v3/mutate'; +export type PublicBackupJwk = Readonly<{ + kty: 'EC'; + crv: 'secp256k1'; + x: string; + y: string; +}>; +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 new file mode 100644 index 0000000000..3cad428318 --- /dev/null +++ b/src/services/backup/automatic.ts @@ -0,0 +1,761 @@ +/* eslint-disable no-await-in-loop -- CAS fanout must use each acknowledged revision in order. */ +import realmDatabase from 'src/storage/realm/realm'; +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, + recoveryContent, + decodeImage, + decryptRecord, + compareImages, + validateProtectedRecoveryMetadata, +} from './image'; +import { captureLocalRecovery, backupMetadataMatches } from './localSnapshot'; +import { + BACKUP_DEADLINE_MS, + backupBaselineEpoch, + backupConsentEpoch, + canAccessBackupAccount, + clearVerifiedRemoteRevision, + currentBackupAccountSession, + markBackupQueuedMutation, + notifyBackupReconciliation, + recordVerifiedRemoteRevision, + setAutomaticBackupPostHandler, + setBackupLocalEvidenceProvider, + captureBackupWriteIntent, + isCapturedBackupWriteIntent, + withBackupSession, + clearBackupDiagnostic, + recordBackupDiagnostic, + BackupDiagnosticStage, + backupNotEnrolledError, +} 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; + deadline?: number; +}; +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 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_HOSTED_AVAILABLE = true; +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, app: any, deadline?: number, checkpoint = () => {}) { + const result = await readProtectedBackup(app.primaryMnemonic, deadline, appId, checkpoint, app); + if (result.state === 'neverEnrolled') throw backupNotEnrolledError(); + 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( + mutations: AutomaticBackupMutation[], + proof: Proof, + response: any +): Promise { + const { appId } = proof.operation; + const key = generateEncryptionKey(proof.after.app.primarySeed); + 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; + 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 = 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'); + 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'); + 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() && + /^http:\/\/(127\.0\.0\.1|localhost):\d+\/$/.test(config.RELAY); + if ((!AUTOMATIC_BACKUP_SOURCE_READY && !loopbackFixture) || + (!AUTOMATIC_BACKUP_HOSTED_AVAILABLE && !loopbackFixture && + !(AUTOMATIC_BACKUP_SOURCE_READY && nativeLoopback))) + 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( + generateEncryptionKey(proof.after.app.primarySeed), + baseline, + intent.protectedBase.namespaceId, + () => {}, + true + ); + } else await project(mutations, proof, baseline); + assertOwned(); + // One immutable receipt is single-use even if the response is lost. + proofs.delete(intent); + 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)) + throw new Error('Final backup authority changed'); + if (proof.deletion) { + const remaining = await decodeImage( + generateEncryptionKey(proof.after.app.primarySeed), + readback, + expectedBase.namespaceId, + () => {}, + true + ); + 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, expectedBase.namespaceId)) + throw new Error('Backup metadata readback differs'); + const remote = await decodeImage( + generateEncryptionKey(proof.after.app.primarySeed), + readback, + 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, 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({ + updated: true, + backupConfirmed: true, + backupPending: false, + localCommitted: committed, + }); + results.set(result, { proof, session: intent.accountSession, appId }); + if (proof.deletion) clearVerifiedRemoteRevision(appId); + return result; + } 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); + } + }); +} +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(); + 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'); + }; + diagnosticStage = 'snapshot-request'; + const remote = await withBackupSession(appId, () => + snapshot(appId, op.before.app, deadline, assertOrigin)); + 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'); + diagnosticStage = 'delete-validation'; + await decodeImage( + generateEncryptionKey(op.before.app.primarySeed), + remote, + remote.namespaceId, + () => {}, + 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, + protectedBase: protectedBackupBase(remote), + 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, + deadline, + }); + return Object.freeze({ committed: true, intent }); + } catch (error) { + recordBackupDiagnostic(appId, originSession, 'delete', diagnosticStage, error, started); + clearVerifiedRemoteRevision(appId); + notifyBackupReconciliation(appId); + return Object.freeze({ committed: false }); + } +} +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 new file mode 100644 index 0000000000..b6bbab2d87 --- /dev/null +++ b/src/services/backup/image.ts @@ -0,0 +1,379 @@ +/* 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 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); + 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; + 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. + // 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 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 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 { + 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: vaultSchemeIdentity(record.scheme), + // 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') + return { + masterFingerprint: record.masterFingerprint, + // 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, + }; + 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]; + // 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', + '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 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(); + 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 BackupSnapshotValidationError('Invalid backup record'); + await pauseBackup(); + return record; +} + +export async function decodeImage( + key: string, + response: any, + appId: string, + checkpoint = () => {}, + verifyWalletKeys = false +): Promise { + const app = response?.appImage; + if ( + !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 BackupSnapshotValidationError('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 BackupSnapshotValidationError('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 BackupSnapshotValidationError('Invalid backup record'); + totalBytes += value.length; + 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 BackupSnapshotValidationError('Duplicate or unidentified backup record'); + image[kind][id] = record; + }; + 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)) { + 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 BackupSnapshotValidationError('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 BackupSnapshotValidationError('Incomplete vault backup'); + if (Array.isArray(app.labels) && app.labels.length !== Object.keys(image.labels).length) + 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 new file mode 100644 index 0000000000..ac0ed934ee --- /dev/null +++ b/src/services/backup/intent.ts @@ -0,0 +1,13 @@ +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; + operationId?: string; + consentEpoch?: number; +}>; diff --git a/src/services/backup/localMutation.ts b/src/services/backup/localMutation.ts new file mode 100644 index 0000000000..26faf8700e --- /dev/null +++ b/src/services/backup/localMutation.ts @@ -0,0 +1,51 @@ +import { RealmSchema } from 'src/storage/realm/enum'; +import { canonical, recoveryContent } from './image'; + +const backedUpSchemas = new Set([ + RealmSchema.KeeperApp, + 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.KeeperApp) return { + 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) + 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 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 { + 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/services/backup/localSnapshot.ts b/src/services/backup/localSnapshot.ts new file mode 100644 index 0000000000..fcab3b2351 --- /dev/null +++ b/src/services/backup/localSnapshot.ts @@ -0,0 +1,92 @@ +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, namespaceId = local.id) { + try { + const subscription = + typeof remote?.subscription === 'string' + ? JSON.parse(remote.subscription) + : remote?.subscription; + return ( + !!remote && + remote.appId === namespaceId && + 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, + primaryMnemonic: app.primaryMnemonic, + version: app.version, + networkType: app.networkType, + subscription: app.subscription, + }; + return { app, image, fingerprint: hash256(canonical({ metadata, image: normalized })) }; +} diff --git a/src/services/backup/protected.ts b/src/services/backup/protected.ts new file mode 100644 index 0000000000..ea420c0935 --- /dev/null +++ b/src/services/backup/protected.ts @@ -0,0 +1,209 @@ +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'; + +// 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_AVAILABLE && !loopback()) + throw new Error('Protected backup is not enabled in this app'); +} +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 new file mode 100644 index 0000000000..c6e7e8f63c --- /dev/null +++ b/src/services/backup/repair.ts @@ -0,0 +1,515 @@ +/* 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 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 { readProtectedBackup, enrollProtectedBackup, protectedBackupMutation, + protectedBackupBase, sameProtectedBackupBase, assertProtectedBackupWriteReady, + ProtectedBackupBase } from './protected'; +import { + BACKUP_DEADLINE_MS, + BackupRequestError, + backupQueuedRevision, + backupConsentEpoch, + backupRevision, + canAccessBackupAccount, + clearVerifiedRemoteRevision, + currentBackupAccountSession, + recordVerifiedRemoteRevision, + withBackupSession, + clearBackupDiagnostic, + recordBackupDiagnostic, +} from './transport'; +import { + BackupImage, + BackupSnapshotValidationError, + canonical, + compareImages, + validateProtectedRecoveryMetadata, + decodeImage, + encryptRecord, + MAX_BACKUP_BYTES, + MAX_RECORD_BYTES, + pauseBackup, + recoveryContent, +} from './image'; + +export type RepairPhase = + | 'checking' + | 'different' + | 'preparing' + | 'uploading' + | 'verifying' + | 'verified' + | 'conflict' + | 'unverified' + | 'never-enrolled' + | 'deleted'; +type InspectionStage = + | 'local-capture' + | 'snapshot-request' + | 'snapshot-validation' + | 'snapshot-decryption' + | 'comparison' + | 'repair-preparation' + | 'repair-upload' + | 'repair-readback' + | 'local-recheck'; +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)); + +// 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 + // 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], + ['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); + 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) + : 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, + explicitChoice = false, + expectedSession = currentBackupAccountSession() +): Promise { + // 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, 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(operationKey); + if (active) active.phase = phase; + 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); + 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) + ) + throw new Error('Backup changed during check'); + const currentRevision = backupRevision(appId); + if (currentRevision !== revision) { + changedRevision = currentRevision; + 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)); + 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 expectedBase: ProtectedBackupBase; + let discovery: 'never-enrolled' | 'deleted' | undefined; + let needsVaultRepair = false; + let needsLabelRepair = false; + let needsMetadataRepair = false; + let unavailableVaultIds: string[] = []; + let unavailableLabelIds: string[] = []; + const read = async (readback = false) => { + stage = readback ? 'repair-readback' : 'snapshot-request'; + 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(); + 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'); + 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'); + 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 || 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. + 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( + (record) => + typeof record.isArchived === 'boolean' && + record.isArchived !== !!decoded.vaults[record.vaultId]?.archived + ); + needsLabelRepair = unavailableLabels.length > 0; + 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 = + contentComparison === 'matched' && + (needsVaultRepair || needsLabelRepair || needsMetadataRepair) + ? 'different' + : contentComparison; + assertCurrent(); + if (comparison === 'conflict') { + notify('conflict'); + return 'conflict'; + } + if (comparison === 'different' && !repair) { + notify('different'); + 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); + 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'); + stage = 'repair-upload'; + assertProtectedBackupWriteReady(); + let acknowledgedBase: ProtectedBackupBase | undefined; + try { + const reply = await protectedBackupMutation(app.primaryMnemonic, expectedBase, + 'repairAppBackup', {...payload, replaceCurrentState: true}, deadline, assertSource, app); + acknowledgedBase = reply.base; + } 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'; + const verifiedRemote = await read(true); + stage = 'repair-readback'; + if ( + !acknowledgedBase || + !sameProtectedBackupBase(expectedBase, acknowledgedBase) || + !verifiedRemote || + needsVaultRepair || + needsLabelRepair || + needsMetadataRepair || + (await compareImages(local, verifiedRemote, assertCurrent)) !== 'matched' + ) + throw new Error('Backup not verified'); + } + stage = 'local-recheck'; + if ( + (await compareImages(local, await captureBackupImage(assertCurrent), assertCurrent)) !== + 'matched' + ) + 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, expectedBase.revision, expectedSession, expectedBase); + 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 + // deadline. Never retry a repair, invalid response, or account switch. + let accountIsCurrent = false; + try { + accountIsCurrent = + sessionIsCurrent() && + (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 = + failedStage === 'snapshot-request' && + error instanceof BackupRequestError && + ['http-5xx', 'network', 'timeout'].includes(error.category); + if ( + allowRetry && + !repair && + accountIsCurrent && + Date.now() < deadline && + (queuedWriteChanged || transientSnapshotFailure) + ) + return 'retry'; + // 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'; + const diagnosticStage = + 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, + category + ); + notify('unverified'); + return 'unverified'; + } + }; + const runOnce = (allowRetry: boolean) => + withBackupSession(appId, () => { + if (!sessionIsCurrent()) { + 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); + clearBackupDiagnostic(appId, expectedSession); + return inspectOnce(allowRetry); + }); + const result: Promise = runOnce(true).then(async (phase) => + phase === 'retry' ? ((await runOnce(false)) as RepairPhase) : phase + ); + inFlight.set(operationKey, { repair, promise: result, listeners }); + const clean = () => { + if (inFlight.get(operationKey)?.promise === result) inFlight.delete(operationKey); + }; + 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..a6ee634d94 --- /dev/null +++ b/src/services/backup/restore.ts @@ -0,0 +1,152 @@ +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. +export async function prepareRecoveryImage( + encryptionKey: string, + appId: string, + appImage: any, + allVaultImages: any[], + labels: any[], + 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. + const legacyEmptyMap = (value: any) => (Array.isArray(value) && !value.length ? {} : value); + const image = await decodeImage( + encryptionKey, + { + appImage: { + ...appImage, + wallets: legacyEmptyMap(appImage.wallets), + signers: legacyEmptyMap(appImage.signers === undefined ? {} : appImage.signers), + nodes: appImage.nodes === undefined ? [] : appImage.nodes, + }, + allVaultImages, + 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[]) { + 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/src/services/backup/transport.ts b/src/services/backup/transport.ts new file mode 100644 index 0000000000..ca241cfeab --- /dev/null +++ b/src/services/backup/transport.ts @@ -0,0 +1,376 @@ +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>(); +const revisions = new Map(); +const queuedMutationRevisions = new Map(); +const verifiedRemoteRevisions = new Map< + string, + { 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, + 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 +) => { + 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. +// 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; + lastDiagnostic = undefined; + } +} +export const currentBackupAccountSession = () => accountSession; +export const backupRevision = (id: string) => revisions.get(id) || 0; +export const backupQueuedRevision = (id: string) => queuedMutationRevisions.get(id) || 0; +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, protectedBase?: ProtectedBackupBase +) { + if (!/^[a-f0-9]{64}$/.test(revision) || session !== accountSession || appId !== activeAccount) + return; + clearVerifiedRemoteRevision(appId); + verifiedRemoteRevisions.set(appId, { + 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?.protectedBase || appId !== activeAccount || verified.session !== accountSession) + return undefined; + 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, + protectedBase: verified.protectedBase, + 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); +}; +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'; + +export class BackupRequestError extends Error { + 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'; + 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'; + } + 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); + 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 | string, + deadline?: number, + signedHeaders?: Record +) { + 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() + 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 as object, signedHeaders, { + ...(typeof body === 'string' ? { transformRequest: [(data: string) => data], maxRedirects: 0 } : {}), + signal: controller.signal, + onUploadProgress: ({ loaded }) => { + if (loaded > uploaded) { + uploaded = loaded; + resetIdle(); + } + }, + onDownloadProgress: ({ loaded }) => { + if (loaded > downloaded) { + downloaded = loaded; + resetIdle(); + } + }, + }); + } catch (error) { + // Do not propagate Axios errors: they can contain request bodies and headers. + 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); + } +} + +export function backupPost( + _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. + markBackupMutation(appId); + clearVerifiedRemoteRevision(appId); + onBackupReconciliationRequired(appId); + 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')); + // 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/services/channel/ChannelRequestScanner.tsx b/src/services/channel/ChannelRequestScanner.tsx new file mode 100644 index 0000000000..a4b68a9e99 --- /dev/null +++ b/src/services/channel/ChannelRequestScanner.tsx @@ -0,0 +1,32 @@ +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. +export default function ChannelRequestScanner({ + onScanCompleted, + children, +}: { + onScanCompleted: (data: string) => boolean | Promise; + children: ReactElement; +}) { + const [requestSent, setRequestSent] = useState(false); + const [scanAttempt, setScanAttempt] = useState(0); + 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); + } else { + // QRScanner latches after one decoded QR. A new instance can scan again. + setScanAttempt((attempt) => attempt + 1); + } + } 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..dd4842db3e --- /dev/null +++ b/src/services/channel/crypto.ts @@ -0,0 +1,51 @@ +// 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 InvalidChannelQRCodeError(); + 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/services/electrum/client.ts b/src/services/electrum/client.ts index 129db5a320..624e858514 100644 --- a/src/services/electrum/client.ts +++ b/src/services/electrum/client.ts @@ -2,11 +2,16 @@ 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'; import ecc from '../wallets/operations/taproot-utils/noble_ecc'; import { store } from 'src/store/store'; +import { getFailoverPeers } from './peerSelection'; import { classifyElectrumConnectionError, ElectrumConnectionErrorType, @@ -22,13 +27,66 @@ const ELECTRUM_CLIENT_CONFIG: { reconnectDelay: 500, // retry after half a second }; -const ELECTRUM_CLIENT_DEFAULTS = { +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; + 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) + ); +}; + +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: { @@ -37,14 +95,7 @@ let lastConnectionError: { } | 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[]; -} = ELECTRUM_CLIENT_DEFAULTS; +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'; @@ -55,47 +106,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; - ELECTRUM_CLIENT.electrumClient = 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.onError = (error) => { - if (ELECTRUM_CLIENT.isClientConnected) { + state.electrumClient = client; + + client.onError = (error) => { + if ( + stateIsCurrent(state, generation) && + state.electrumClient === client && + state.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; + 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); } }; @@ -105,26 +163,44 @@ 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) ]); + 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; + 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); @@ -132,7 +208,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, }); @@ -140,22 +216,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 = @@ -168,23 +253,84 @@ 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 = 1; + 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 (!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(), + { + isConnected: false, + } + ); + if (!stateIsCurrent(state, generation)) return; + 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); + } + } + if (!stateIsCurrent(state, generation)) return; + 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() { @@ -200,22 +346,33 @@ 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(); + connectionGeneration += 1; ELECTRUM_CLIENT.isClientConnected = false; - ELECTRUM_CLIENT.activePeer.isConnected = 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; @@ -225,9 +382,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; } @@ -246,20 +406,35 @@ 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) { + connectionGeneration += 1; + ELECTRUM_CLIENT.isClientConnected = false; + if (ELECTRUM_CLIENT.electrumClient?.close) { ELECTRUM_CLIENT.electrumClient.close(); } // set defaults - ELECTRUM_CLIENT = ELECTRUM_CLIENT_DEFAULTS; + ELECTRUM_CLIENT = createElectrumClientState(); + ELECTRUM_CLIENT.appId = store.getState().storage?.appId ?? null; + 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; + ELECTRUM_CLIENT.peers = getFailoverPeers(nodes, activeNode); + ELECTRUM_CLIENT.currentPeerIndex = activeNode ? 0 : -1; + } - if (nodes) { - ELECTRUM_CLIENT.peers = nodes; - } + /** Extend a successful manual connection with eligible saved peers for later reconnects. */ + 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; } public static splitIntoChunks(arr, chunkSize) { @@ -277,9 +452,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 = {}; @@ -295,9 +476,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]; @@ -315,6 +495,7 @@ export default class ElectrumClient { } } + ElectrumClient.assertConnectionGeneration(generation, expectedNetworkType); return res; } @@ -332,6 +513,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 = []; @@ -339,6 +525,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 = {}; @@ -354,9 +541,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]; @@ -373,6 +559,7 @@ export default class ElectrumClient { } } + ElectrumClient.assertConnectionGeneration(generation, expectedNetworkType); return { historyByAddress, txids, txidToAddress }; } @@ -386,6 +573,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 @@ -393,10 +583,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) { @@ -418,21 +610,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 1b09f62e35..24e7e8486a 100644 --- a/src/services/electrum/node.tsx +++ b/src/services/electrum/node.tsx @@ -2,12 +2,33 @@ 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 async save(nodeDetail: NodeDetail, nodeList: NodeDetail[]) { + public static currentAccountId(): string | undefined { + const {appId} = store.getState().storage; + 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?: 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; if (!nodeDetail.host || !nodeDetail.port) { return { saved: false, connectionError: 'Missing node host URL or port' }; @@ -19,6 +40,8 @@ export default class Node { if (!isConnectable) { return { saved: false, connectionError }; } + if (!this.isAccountActive(originAppId) || accountSession !== currentBackupAccountSession()) + return { saved: false, connectionError: 'Account changed' }; const node = { ...nodeDetail }; @@ -40,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) { @@ -90,10 +111,29 @@ export default class Node { ); } - public static async connectToSelectedNode(selectedNode: NodeDetail) { - // connects to the selected node + public static async connectToSelectedNode( + selectedNode: NodeDetail, + originAppId = this.currentAccountId(), + originBackupAccountSession = currentBackupAccountSession() + ) { + 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 + // 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(); + // 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) || currentBackupAccountSession() !== originBackupAccountSession) { + return { connected: false, connectedTo: undefined, error: 'Account changed' }; + } + if (connected) { + ElectrumClient.setFailoverPeers( + this.getAllNodes().filter((node) => node.networkType === selectedNode.networkType), + generation + ); + } return { connected, connectedTo, error }; } 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 456df553f4..8ec9f70ed1 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,20 +61,33 @@ 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, + ...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/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..ffbad27df4 100644 --- a/src/services/wallets/factories/USDTWalletFactory.ts +++ b/src/services/wallets/factories/USDTWalletFactory.ts @@ -5,9 +5,9 @@ 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 { GasFreeTransferStatus } from '../operations/dollars/GasFree'; import config from 'src/utils/service-utilities/config'; export const USDTWalletSupportedNetwork = config.isDevMode() @@ -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,25 @@ 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; - }) + // 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 ); - // 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 @@ -292,9 +268,9 @@ 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 cached status with the matching confirmed chain entry. 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] = { @@ -304,9 +280,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/GasFree.ts b/src/services/wallets/operations/dollars/GasFree.ts index ad41f62fab..7c73a63eae 100644 --- a/src/services/wallets/operations/dollars/GasFree.ts +++ b/src/services/wallets/operations/dollars/GasFree.ts @@ -1,544 +1,11 @@ -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', CONFIRMING = 'CONFIRMING', + // SUCCEED was also written for provider-reported results in older builds. SUCCEED = 'SUCCEED', + UNVERIFIED = 'UNVERIFIED', + CHAIN_CONFIRMED = 'CONFIRMED', 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/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/src/services/wallets/operations/dollars/USDT.ts b/src/services/wallets/operations/dollars/USDT.ts index 85b5d4edd0..24c4741be8 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; @@ -70,393 +61,113 @@ export interface USDTTransaction { isGasFree: boolean; } +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 { - /** - * 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,34 +175,27 @@ 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) => ({ - txId: txn.transactionId, - from: txn.from, - to: txn.to, - amount: txn.formattedValue.toString(), - 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 - })); - - return { - transactions: updatedTransactions, - meta: trc20Transactions.meta, - }; + const contract = USDT.getUSDTAddress(networkType); + const result = await getTrc20Transactions(address, contract, networkType, limit, fingerprint); + 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.CHAIN_CONFIRMED + : GasFreeTransferStatus.CONFIRMING, + timestamp: txn.blockTimestamp, + blockNumber: confirmed ? txn.blockNumber : 0, + isGasFree: txn.to !== address, + }; + }); + return { transactions, meta: result.meta }; } } 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/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/storage/realm/dbManager.ts b/src/storage/realm/dbManager.ts index 1bb07e26ab..2324dd46c6 100644 --- a/src/storage/realm/dbManager.ts +++ b/src/storage/realm/dbManager.ts @@ -1,5 +1,20 @@ +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(); +}; +const notifyRecoveryUpdate = (schema: RealmSchema, object: any, updateProps: any) => { + if (changesBackedUpRecovery(schema, object, updateProps)) recoveryMutationObserver(); +}; /** * intializes realm @@ -27,10 +42,12 @@ 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) { console.log(err); + return false; } }; @@ -45,10 +62,12 @@ const createObjectBulk = ( updateMode = Realm.UpdateMode.All ) => { try { + notifyRecoveryCreateOrDelete(schema, !!objects?.length); const hasCreated = realm.createBulk(schema, objects, updateMode); return hasCreated; } catch (err) { console.log(err); + return false; } }; @@ -105,11 +124,12 @@ const getObjectByPrimaryId = (schema: RealmSchema, name: string, primaryId: stri const updateObjectById = (schema: RealmSchema, id: string, updateProps: any) => { try { const object = getObjectById(schema, id); - for (const [key, value] of Object.entries(updateProps)) { - realm.write(() => { + notifyRecoveryUpdate(schema, object, updateProps); + realm.write(() => { + for (const [key, value] of Object.entries(updateProps)) { object[key] = value; - }); - } + } + }); return true; } catch (err) { console.error({ err }); @@ -131,11 +151,12 @@ const updateObjectByPrimaryId = ( ) => { try { const object = getObjectByPrimaryId(schema, name, primaryId); - for (const [key, value] of Object.entries(updateProps)) { - realm.write(() => { + notifyRecoveryUpdate(schema, object, updateProps); + realm.write(() => { + for (const [key, value] of Object.entries(updateProps)) { object[key] = value; - }); - } + } + }); return true; } catch (err) { console.error({ err }); @@ -177,6 +198,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 +214,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,11 +238,12 @@ const updateObjectByQuery = ( const objects = realm.get(schema); const object = objects.find(queryFn); if (object) { - for (const [key, value] of Object.entries(updateProps)) { - realm.write(() => { + notifyRecoveryUpdate(schema, object, updateProps); + realm.write(() => { + for (const [key, value] of Object.entries(updateProps)) { object[key] = value; - }); - } + } + }); return true; } return false; @@ -268,4 +292,6 @@ export default { getObjectByField, updateObjectByQuery, getObjectByQuery, + setRecoveryMutationObserver, + setRecoveryCommitObserver, }; diff --git a/src/storage/realm/realm.ts b/src/storage/realm/realm.ts index d8b70d8e1b..9817bccd48 100644 --- a/src/storage/realm/realm.ts +++ b/src/storage/realm/realm.ts @@ -1,17 +1,55 @@ 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; + + 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 +59,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 +111,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, @@ -81,13 +136,73 @@ 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. */ 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(); }; /** @@ -118,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) { @@ -141,6 +256,7 @@ export class RealmDatabase { return realm.objects(schema); } catch (err) { console.log({ err }); + return undefined; } }; @@ -150,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; }; /** @@ -163,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/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/src/store/reducers/bhr.ts b/src/store/reducers/bhr.ts index fa6788d752..9519fec567 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'; @@ -5,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; @@ -14,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; @@ -41,13 +46,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, @@ -59,6 +75,8 @@ const initialState: { appImageError: '', appImagerecoveryRetry: false, + recoveryDiscovery: { status: 'idle', source: 'protected' }, + protectedBackupStateByAppId: {}, signingDevices: [], relayWalletUpdateLoading: false, relayWalletUpdate: false, @@ -82,17 +100,77 @@ 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; + +// 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, @@ -116,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'); }, @@ -211,29 +302,105 @@ 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; - }, - - setAutomaticCloudBackup: (state, action: PayloadAction) => { - state.automaticCloudBackup = action.payload; - }, - setDeleteBackupSuccess: (state, action: PayloadAction) => { - state.deleteBackupSuccess = action.payload; - }, - setDeleteBackupFailure: (state, action: PayloadAction) => { - state.deleteBackupFailure = 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 }>) => { + const { appId, phase } = action.payload; + (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; + }, + invalidateBackupRepair: (state, action: PayloadAction) => { + (state.backupRepairCompletedByAppId ??= {})[action.payload] = false; + if (state.backupRepairStateByAppId?.[action.payload] === 'verified') + state.backupRepairStateByAppId[action.payload] = 'unverified'; + }, + 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; @@ -248,6 +415,9 @@ export const { setBackupLoading, setAppImageError, appImagerecoveryRetry, + setRecoveryDiscovery, + resetRecoveryDiscovery, + setProtectedBackupState, setSigningDevices, @@ -281,8 +451,13 @@ export const { setBackupAllFailure, setPendingAllBackup, + setBackupRepairState, + commitBackupInspection, + setBackupRepairRunning, + invalidateBackupRepair, setAutomaticCloudBackup, + migrateLegacyAutomaticCloudBackup, setDeleteBackupSuccess, setDeleteBackupFailure, @@ -294,12 +469,14 @@ const bhrPersistConfig = { key: 'bhr', storage: reduxStorage, blacklist: [ + 'backupRepairRunningByAppId', 'isBackupError', 'backupError', 'seedConfirmed', 'loading', 'appImageError', 'appImagerecoveryRetry', + 'recoveryDiscovery', 'relayWalletUpdateLoading', 'relayWalletUpdate', @@ -322,9 +499,14 @@ const bhrPersistConfig = { 'backupAllLoading', 'backupAllFailure', 'backupAllSuccess', + 'backupAllLoadingByAppId', + 'backupAllFailureByAppId', + 'backupAllSuccessByAppId', 'deleteBackupSuccess', 'deleteBackupFailure', + 'deleteBackupSuccessByAppId', + 'deleteBackupFailureByAppId', 'homeToastMessage', ], diff --git a/src/store/reducers/helpAi.ts b/src/store/reducers/helpAi.ts index 78489e77a1..ec7f17e54b 100644 --- a/src/store/reducers/helpAi.ts +++ b/src/store/reducers/helpAi.ts @@ -7,6 +7,7 @@ import { HelpEscalationCard, } from 'src/models/interfaces/HelpAi'; import { reduxStorage } from 'src/storage'; +import { removeFailedChatMessage } from 'src/utils/helpAiChatBehavior'; export type HelpAiDraftStatus = | 'pending_review' @@ -34,6 +35,7 @@ export type HelpAiThread = { lastEscalationStage: string; chatMeta: HelpChatMetadata | null; issueCount: number; + failedMessageId: string | null; }; type HelpAiState = { @@ -66,6 +68,7 @@ const makeThread = (conversationId: string): HelpAiThread => ({ lastEscalationStage: 'none', chatMeta: null, issueCount: 1, + failedMessageId: null, }); const findOrCreateThread = (state: HelpAiState, conversationId: string) => { @@ -153,6 +156,29 @@ 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; + }, + discardHelpAiFailedMessage: (state, action: PayloadAction<{ conversationId: string }>) => { + 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(); + }, }, }); @@ -166,6 +192,8 @@ export const { setHelpAiEscalationStage, setHelpAiChatMeta, incrementHelpAiIssueCount, + setHelpAiFailedMessageId, + discardHelpAiFailedMessage, } = helpAiSlice.actions; const helpAiPersistConfig = { @@ -174,4 +202,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/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/src/store/reducers/network.ts b/src/store/reducers/network.ts index e037782403..19e9e174d9 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; + testnetFallbackNodeAddedByAppId: { [appId: string]: boolean }; } = { exchangeRates: null, averageTxFees: null, initialNodesSaved: false, + testnetFallbackNodeAddedByAppId: {}, }; const networkSlice = createSlice({ @@ -26,9 +28,20 @@ const networkSlice = createSlice({ setInitialNodesSaved: (state, action: PayloadAction) => { state.initialNodesSaved = action.payload; }, + + 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; + }, }, }); -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/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/reducers/wallets.ts b/src/store/reducers/wallets.ts index 9e370fd404..b92b98e642 100644 --- a/src/store/reducers/wallets.ts +++ b/src/store/reducers/wallets.ts @@ -18,10 +18,12 @@ export type WalletsState = { testCoinsReceived: boolean; testCoinsFailed: boolean; testCoinsQuotaReached: boolean; + testCoinsPending: 'propagation' | 'sync' | 'unknown' | null; introModal: boolean; err: string; walletSyncing: {}; + refreshRequests: Record; signerPolicyError?: string; }; @@ -38,11 +40,13 @@ const initialState: WalletsState = { testCoinsReceived: false, testCoinsFailed: false, testCoinsQuotaReached: false, + testCoinsPending: null, introModal: true, err: '', walletSyncing: {}, + refreshRequests: {}, }; export type syncingPayload = { @@ -66,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; }, @@ -94,6 +104,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; @@ -114,12 +141,16 @@ export const { setTestCoinsReceived, setTestCoinsFailed, setTestCoinsQuotaReached, + setTestCoinsPending, setIntroModal, walletGenerationFailed, newWalletCreated, resetWalletStateFlags, resetSyncing, setSyncing, + startRefreshRequest, + finishRefreshRequest, + clearRefreshRequest, setSignerPolicyError, } = walletSlice.actions; @@ -130,10 +161,12 @@ const walletPersistConfig = { 'testCoinsReceived', 'testCoinsFailed', 'testCoinsQuotaReached', + 'testCoinsPending', 'hasNewWalletsGenerationFailed', 'hasNewWalletsGenerationSucceeded', 'isGeneratingNewWallet', 'walletSyncing', + 'refreshRequests', 'setSignerPolicyError', ], }; diff --git a/src/store/sagaActions/bhr.ts b/src/store/sagaActions/bhr.ts index 39e8eadfd3..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'; @@ -15,8 +16,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, @@ -27,15 +29,18 @@ export const updateAppImage = ({ wallets, signers, updateNodes }) => ({ 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: { @@ -47,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, }, }); @@ -90,8 +100,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,11 +110,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, -}); \ No newline at end of file + originAppId, +}); + +export const CHECK_BACKUP_FRESHNESS = 'CHECK_BACKUP_FRESHNESS'; +export const REPAIR_BACKUP = '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 e10adaa871..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) => ({ +export const updateVersionHistory = (previousVersion, newVersion, isRecovery = false, originAppId?: string) => ({ type: UPDATE_VERSION_HISTORY, - payload: { previousVersion, newVersion }, + 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/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/sagaActions/wallets.ts b/src/store/sagaActions/wallets.ts index 0508d24152..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'; @@ -29,13 +31,20 @@ export const getTestcoins = (testWallet: Wallet) => ({ export const autoSyncWallets = ( syncAll?: boolean, hardRefresh?: boolean, - addNotifications?: boolean + addNotifications?: boolean, + originAppId?: string, + archivedOnly?: boolean, + requestId?: string ) => ({ type: AUTO_SYNC_WALLETS, + ...(originAppId ? { originAppId } : {}), payload: { syncAll, hardRefresh, addNotifications, + backupCheckAppId: originAppId, + archivedOnly, + requestId, }, }); @@ -46,9 +55,11 @@ export const updateSignerPolicy = ( restrictions: SignerRestriction; signingDelay: number; }, - verificationToken + verificationToken, + originAppId?: string ) => ({ type: UPDATE_SIGNER_POLICY, + originAppId, payload: { signer, signingKey, @@ -59,12 +70,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, }, }); @@ -119,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: { @@ -137,12 +161,13 @@ 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 }, }); export const updateVaultSignersXpriv = (signers: Signer[]) => ({ type: UPDATED_VAULT_SIGNERS_XPRIV, signers, -}); \ No newline at end of file +}); diff --git a/src/store/sagas/bhr.ts b/src/store/sagas/bhr.ts index 063d9dc263..e2ad5ec807 100644 --- a/src/store/sagas/bhr.ts +++ b/src/store/sagas/bhr.ts @@ -1,13 +1,35 @@ +import { buffers, END, eventChannel } from 'redux-saga'; +import { inspectBackup, RepairPhase } from 'src/services/backup/repair'; +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'; +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 } 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 { - decrypt, - encrypt, - generateEncryptionKey, - hash256, -} from 'src/utils/service-utilities/encryption'; +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'; import { KeeperApp } from 'src/models/interfaces/KeeperApp'; @@ -42,31 +64,48 @@ 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 { appImagerecoveryRetry, setAppImageError, setAutomaticCloudBackup, + isAutomaticCloudBackupEnabled, setBackupAllFailure, setBackupAllLoading, setBackupAllSuccess, + setBackupRepairState, + commitBackupInspection, + setBackupRepairRunning, + invalidateBackupRepair, setBackupLoading, setBackupType, setDeleteBackupFailure, setDeleteBackupSuccess, - setHomeToastMessage, setIsCloudBsmsBackupRequired, setPendingAllBackup, setSeedConfirmed, + setRecoveryDiscovery, + setProtectedBackupState, } from '../reducers/bhr'; import { + CHECK_BACKUP_FRESHNESS, + REPAIR_BACKUP, BACKUP_ALL_SIGNERS_AND_VAULTS, BACKUP_BSMS_ON_CLOUD, BSMS_CLOUD_HEALTH_CHECK, DELETE_APP_IMAGE_ENTITY, DELETE_BACKUP, GET_APP_IMAGE, + getAppImage, HEALTH_CHECK_STATUS_UPDATE, SEED_BACKEDUP, SEED_BACKEDUP_CONFIRMED, @@ -79,213 +118,197 @@ 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 { setupRecoveryKeySigningKey } from 'src/hardware/signerSetup'; +import { RootState, store } from '../store'; 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'; -export function* updateAppImageWorker({ - payload, -}: { - payload: { - wallets?: Wallet[] | USDTWallet[]; - signers?: Signer[]; - updateNodes?: boolean; - }; -}) { +function currentBackupAppId(): string | undefined { try { - const backupResponse = yield call(checkBackupCondition); - 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 - ); - 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; - } - } else if (signers) { - for (const signer of signers) { - const encrytedSigner = encrypt( - encryptionKey, - JSON.stringify(sanitizeSeedKeyForBackup(signer)) - ); - signersObject[getKeyUID(signer)] = encrytedSigner; - } - } else 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 encrytedNode = encrypt(encryptionKey, JSON.stringify(node)); - nodesList.push(encrytedNode); - } - } - } - - const response = yield call(Relay.updateAppImage, { - appId: id, - publicId, - walletsObject, - signersObject, - networkType, - subscription: JSON.stringify(subscription), - version, - nodes: nodesList, - }); - return response; - } catch (err) { - console.log({ err }); - console.error('App image update failed', err); - yield call(setServerBackupFailed); - return { updated: true, error: '' }; + 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* updateVaultImageWorker({ - payload, -}: { - payload: { - vault: Vault; - archiveVaultId?: string; - isUpdate?: boolean; - }; -}) { - const backupResponse = yield call(checkBackupCondition); - if (backupResponse) return { updated: true, error: '' }; - - const { vault, archiveVaultId, isUpdate } = payload; - const { primarySeed, id, subscription }: KeeperApp = yield call( - dbManager.getObjectByIndex, - RealmSchema.KeeperApp - ); - const encryptionKey = generateEncryptionKey(primarySeed); - const vaultEncrypted = encrypt( - encryptionKey, - JSON.stringify(sanitizeVaultSignersForSeedKeyBackup(vault)) - ); - - if (isUpdate) { - const response = yield call(Relay.updateVaultImage, { - isUpdate, - vaultId: vault.id, - vault: vaultEncrypted, - }); - return response; +// 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))); } - - 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), - }); + 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), + })), + scheme: vault.scheme, + isArchived: !!vault.archived, subscription: JSON.stringify(app.subscription), + } }; +} - // TODO to be removed - const subscriptionStrings = JSON.stringify(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, committedOperation }: { + originAppId?: string; + originBackupAccountSession?: number; + originBackupWriteIntent?: BackupWriteIntent; + committedOperation?: BackupLocalCommit; + payload: { wallets?: Wallet[] | USDTWallet[]; signers?: Signer[]; updateNodes?: boolean }; +}) { + const appId = originAppId || currentBackupAppId(); + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); + if (!appId || currentBackupAppId() !== appId || requestSession !== currentBackupAccountSession()) + return { updated: false, error: 'Backup account changed' }; try { - const response = yield call(Relay.updateVaultImage, { - appID: id, - vaultId: vault.id, - signersData, - vault: vaultEncrypted, - subscription: subscriptionStrings, - ...(archiveVaultId && { archiveVaultId }), - }); - return response; - } catch (err) { - captureError(err); - yield call(setServerBackupFailed); - return { updated: 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' }; } } -// TODO: Other functions here only handle Relay backup, but this also updates local DB, should move that part out -export function* deleteAppImageEntityWorker({ - payload, -}: { - payload: { - signerIds?: string[]; - walletIds?: string[]; - }; +export function* updateVaultImageWorker({ payload, originAppId, originBackupAccountSession, committedOperation }: { + originAppId?: string; + originBackupAccountSession?: number; + originBackupWriteIntent?: BackupWriteIntent; + committedOperation?: BackupLocalCommit; + payload: { vault: Vault; archiveVaultId?: string; isUpdate?: boolean }; }) { + const appId = originAppId || currentBackupAppId(); + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); + if (!appId || currentBackupAppId() !== appId || requestSession !== currentBackupAccountSession()) + return { updated: false, error: 'Backup account changed' }; try { - const { signerIds, walletIds } = payload; - const { id }: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); - let response; + 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' }; + } +} - const backupResponse = yield call(checkBackupCondition); - if (backupResponse) response = { updated: true, error: '' }; - else { - response = yield call(Relay.deleteAppImageEntity, { - appId: id, - signers: signerIds, - walletIds, - }); +export function* deleteAppImageEntityWorker({ payload, originAppId, originBackupAccountSession, originBackupWriteIntent }: { + originAppId?: string; + originBackupAccountSession?: number; + originBackupWriteIntent?: BackupWriteIntent; + payload: { signerIds?: string[]; walletIds?: string[]; walletSchema?: RealmSchema }; +}) { + const appId = originAppId || currentBackupAppId(); + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); + if (!appId || currentBackupAppId() !== appId || requestSession !== currentBackupAccountSession()) + return { updated: false, error: 'Backup account changed' }; + 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'); } - - if (walletIds?.length > 0) { - for (const walletId of walletIds) { - yield call(dbManager.deleteObjectById, RealmSchema.Wallet, walletId); - } + 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) { - yield call(dbManager.deleteObjectByPrimaryKey, RealmSchema.Signer, 'id', signerId); - } + 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); - 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, -}: { - payload: { - vaultIds: string[]; - }; +export function* deleteVaultImageWorker({ payload, originAppId, originBackupAccountSession, committedOperation }: { + originAppId?: string; + originBackupAccountSession?: number; + originBackupWriteIntent?: BackupWriteIntent; + committedOperation?: BackupLocalCommit; + payload: { vaultIds: string[] }; }) { - try { - const backupResponse = yield call(checkBackupCondition); - if (backupResponse) return { updated: true, error: '' }; - const { vaultIds } = payload; - const { id }: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); - const response = yield call(Relay.deleteVaultImage, { - appId: id, - vaults: vaultIds, - }); - return response; - } catch (err) { - captureError(err); - return { updated: false, error: err }; - } + const appId = originAppId || currentBackupAppId(); + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); + 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({ @@ -335,16 +358,46 @@ function* seedBackedUpWorker() { } } -function* getAppImageWorker({ payload }) { +let latestRecoveryReadRequest = 0; + +function* getAppImageWorker({ payload }: ReturnType) { + 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 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 = () => { + assertRequestIdentity(); + if (requestSession !== currentBackupAccountSession()) + throw new Error('Backup account changed'); + }; try { - yield put(setAppImageError('')); + 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'); 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); @@ -353,23 +406,53 @@ 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; + 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* putCurrent(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* putCurrent(setRecoveryDiscovery({ + status: 'protected-empty', source, keeperAppId: appID, namespaceId, + legacyMayPredateDeletion: result.snapshot.suppressLegacyFallback, + })); + return; } - } catch (err) { - console.log('Error retrieving app image from relay:', err); - // Continue with empty defaults + } 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* putCurrent(setRecoveryDiscovery({ + status: source === 'protected' ? 'protected-found' : 'legacy-selected', + source, keeperAppId: appID, namespaceId, provenance, + })); + assertRequestSession(); const previousVersion = appImage.version; @@ -388,7 +471,7 @@ function* getAppImageWorker({ payload }) { icon: 'assets/ic_diamond_hands.svg', receipt: '', }; - yield call( + recoverySession = yield call( recoverApp, primaryMnemonic, primarySeed, @@ -398,8 +481,20 @@ function* getAppImageWorker({ payload }) { appImage, allVaultImages, labels, - previousVersion + previousVersion, + 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', + assertRequestIdentity ); + if (request !== latestRecoveryReadRequest || + recoverySession !== currentBackupAccountSession() || currentBackupAppId() !== appID) + throw new Error('Backup account changed'); if ((!appImage.wallets || !Object.entries(appImage.wallets).length) && !allVaultImages.length) { // recreate first wallet @@ -417,42 +512,112 @@ function* getAppImageWorker({ payload }) { instanceNum: 0, }, }; - yield call(addNewWalletsWorker, { payload: [defaultWallet] }); + assertRequestIdentity(); + const created = yield call(addNewWalletsWorker, { + payload: [defaultWallet], + originAppId: appID, + originBackupAccountSession: recoverySession, + }); + if (created !== true) throw new Error('Recovery data unavailable'); } + assertRequestIdentity(); const recoveryKeySigner = setupRecoveryKeySigningKey(primaryMnemonic); - yield call(addSigningDeviceWorker, { payload: { signers: [recoveryKeySigner] } }); + yield call(addSigningDeviceWorker, { + payload: { signers: [recoveryKeySigner] }, + originAppId: appID, + originBackupAccountSession: recoverySession, + }); + 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( + (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 }); - try { - yield call(Relay.updateAppImage, { - appId: appImage.appId, - version: newVersion, - }); - } catch (err) { - console.log(err); - } + assertRequestIdentity(); + yield call(applyUpgradeSequence, { + previousVersion, + newVersion, + isRecovery: true, + originBackupAccountSession: recoverySession, + }); } - yield put(addAccount(appID)); - yield put(autoSyncWallets(true, true, false)); - yield put( + if (request !== latestRecoveryReadRequest || + recoverySession !== currentBackupAccountSession() || currentBackupAppId() !== appID) + throw new Error('Backup account changed'); + 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(setRecoveryKeyBackedUp({ appId: appID, status: true })); + 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] + ); + 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* putCurrent(setProtectedBackupState({ appId: appID, state: protectedState })); + } + yield* putCurrent(setRecoveryDiscovery({ + status: 'restored', source, keeperAppId: appID, namespaceId, provenance, + })); + yield* putCurrent(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* putCurrent(setRecoveryDiscovery({ + status: 'read-error', source, keeperAppId, namespaceId, error, + })); + yield* putCurrent(setAppImageError(error)); + } } finally { - yield put(appImagerecoveryRetry()); + const wasCancelled = yield cancelled(); + if (!wasCancelled && request === latestRecoveryReadRequest && + (recoverySession ?? requestSession) === currentBackupAccountSession()) + yield* putCurrent(appImagerecoveryRetry()); } } @@ -465,14 +630,57 @@ function* recoverApp( appImage, allVaultImages, labels, - previousVersion + previousVersion, + originBackupAccountSession?: number, + onRecoverySession?: (ownedSession: number) => void, + 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'); + }; + assertOrigin(); const { bitcoinNetworkType } = yield select((state: RootState) => state.settings); + const recovered: BackupImage = yield call( + prepareRecoveryImage, + encryptionKey, + appID, + appImage, + allVaultImages, + labels, + protectedMetadata, + assertOrigin + ); + 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, primaryMnemonic ); + assertOrigin(); const imageEncryptionKey = generateEncryptionKey(entropy.toString('hex')); const publicId = WalletUtilities.getFingerprintFromSeed(primarySeed); const app: KeeperApp = { @@ -495,28 +703,38 @@ function* recoverApp( networkType: bitcoinNetworkType, }; - yield call(dbManager.createObject, RealmSchema.KeeperApp, app); + yield call(writeRecoveredObject, RealmSchema.KeeperApp, app, assertOrigin); + assertOrigin(); + yield put(setAppId(appID)); + const recoveryAccountSession = currentBackupAccountSession(); + const assertRecoveryAccount = () => { + assertRecoveryRequest?.(); + if (currentBackupAccountSession() !== recoveryAccountSession || currentBackupAppId() !== appID) + throw new Error('Backup account changed'); + }; + assertRecoveryAccount(); + onRecoverySession?.(recoveryAccountSession); + function* writeRecord(schema: RealmSchema, record: any) { + yield call(writeRecoveredObject, schema, record, assertRecoveryAccount); + } // 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(writeRecord, RealmSchema.USDTWallet, decryptedWallet); + else yield call(writeRecord, 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 +744,18 @@ function* recoverApp( ? NetworkType.TESTNET : NetworkType.MAINNET; } - yield call(dbManager.createObject, RealmSchema.Signer, decrytpedSigner); + yield call(writeRecord, 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 +806,22 @@ function* recoverApp( hidden: false, signerXpubs, }; - yield call(dbManager.createObject, RealmSchema.Signer, signerObject); + yield call(writeRecord, RealmSchema.Signer, signerObject); } } - - yield call(dbManager.createObject, RealmSchema.Vault, vault); } - yield call(dbManager.createObject, RealmSchema.Vault, vault); + yield call(writeRecord, 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(writeRecord, RealmSchema.Tags, label); } - yield call(dbManager.createObjectBulk, RealmSchema.Tags, restoredLabels); } if (appImage.nodes) { @@ -625,21 +832,26 @@ function* recoverApp( ); for (const node of existingNodes) { if (node && node.id) { - yield call(dbManager.deleteObjectById, RealmSchema.NodeConnect, node.id.toString()); + assertRecoveryAccount(); + const deleted = yield call( + dbManager.deleteObjectById, + RealmSchema.NodeConnect, + node.id.toString() + ); + assertRecoveryAccount(); + 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(writeRecord, RealmSchema.NodeConnect, decryptedNode); } catch (err) { - console.log('Error recovering a node: ', err); - continue; + throw new Error('Recovery data unavailable'); } } } @@ -647,38 +859,58 @@ 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; - yield call( + const connected = yield call( dbManager.updateObjectById, RealmSchema.NodeConnect, firstNode.id.toString(), firstNode ); + assertRecoveryAccount(); + if (connected !== true) throw new Error('Recovery data unavailable'); } // seed confirm for recovery - yield call(dbManager.createObject, RealmSchema.BackupHistory, { + yield call(writeRecord, 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. + 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(dbManager.createObject, RealmSchema.VersionHistory, { + yield call(writeRecord, RealmSchema.VersionHistory, { version: `${DeviceInfo.getVersion()}(${DeviceInfo.getBuildNumber()})`, date: new Date().toString(), - title: 'Restored version', + title: 'Recovered Wallet', }); + return recoveryAccountSession; +} - yield put(setAppId(appID)); - yield put(setAppCreated(true)); +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({ @@ -712,9 +944,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]; @@ -928,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( @@ -942,126 +1176,205 @@ export const healthCheckSignerWatcher = createWatcher( ); export const deleteAppImageEntityWatcher = createWatcher( - deleteAppImageEntityWorker, + deleteAppImageEntityRequestWorker, DELETE_APP_IMAGE_ENTITY ); -function* backupAllSignersAndVaultsWorker() { - yield put(setBackupAllSuccess(false)); - yield put(setBackupAllFailure(false)); - yield put(setBackupAllLoading(true)); +function* deleteAppImageEntityRequestWorker( + action: Parameters[0] & { + onComplete?: (deleted: boolean) => void; + } +) { + let deleted = false; try { - // clear backup failure notification - const uaiCollection = dbManager.getObjectByField( - RealmSchema.UAI, - uaiType.SERVER_BACKUP_FAILURE, - 'uaiType' - ); - for (const uai of uaiCollection) { - if (uai.uaiType === uaiType.SERVER_BACKUP_FAILURE) { - yield call(uaiActionedWorker, { - payload: { uaiId: uai.id, action: false }, - }); - } + 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 { 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]; +const backupInspectionOwners = new Map(); - 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)) +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) || requestSession !== currentBackupAccountSession()) + 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 && + sameProtectedBackupBase(currentIntent.protectedBase, verifiedEvidence.intent.protectedBase) && + currentIntent.baselineEpoch === verifiedEvidence.intent.baselineEpoch; + }; + try { + yield put(setBackupRepairRunning({ appId: id, running: true })); + updates = eventChannel((emit) => { + // 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) => { + if (phase === 'verified') { + const intent = captureBackupWriteIntent(id); + verifiedEvidence = intent ? { revision: backupRevision(id), intent } : undefined; + } + emit(phase); + }, explicitChoice, requestSession); + inspection.then( + () => emit(END), + () => emit(END) ); - signersObject[getKeyUID(signer)] = encrytedSigner; + return () => {}; + }, buffers.expanding()); + while (true) { + const phase = yield takeMaybe(updates); + if (phase === END) break; + if (requestSession !== currentBackupAccountSession()) return false; + finalPhase = 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 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), + // 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, + 'uaiType' + ) 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 || + requestSession !== currentBackupAccountSession() || !evidenceStillCurrent()) 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 || + requestSession !== currentBackupAccountSession()) return false; + yield call(addToUaiStackWorker, { + payload: { + uaiType: uaiType.SERVER_BACKUP_FAILURE, + uaiDetails: { + heading: translations.recoveryBackup.notificationTitle, + body: translations.recoveryBackup.notificationBody, + }, + }, }); } - vaultObject[vault.id] = { - vaultId: vault.id, - signersData, - vault: vaultEncrypted, - }; } + return outcome.verified && requestSession === currentBackupAccountSession() && + evidenceStillCurrent(); + } finally { + 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 })); + } +} - 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, appId, originBackupAccountSession }) { + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); + if (requestSession !== currentBackupAccountSession()) return; + const { id }: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); + if (!appId || id !== appId || requestSession !== currentBackupAccountSession()) return; + const enabled = yield select((state: RootState) => + state.storage.appId === appId && isAutomaticCloudBackupEnabled(state.bhr, appId) + ); + 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); +} - 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, +function* backupAllSignersAndVaultsWorker({ + appId, + originBackupAccountSession, +}: { + appId: string; + 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; + 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, requestSession); + 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; }); - 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; } finally { - yield put(setBackupAllLoading(false)); + if (isCurrentChoice()) yield put(setBackupAllLoading({ appId, status: false })); } } @@ -1070,152 +1383,136 @@ export const backupAllSignersAndVaultsWatcher = createWatcher( BACKUP_ALL_SIGNERS_AND_VAULTS ); -function* deleteBackupWorker() { - yield put(setBackupAllLoading(true)); +function* deleteBackupWorker({ appId, originBackupAccountSession }: { + appId: string; + originBackupAccountSession?: number; + originBackupWriteIntent?: BackupWriteIntent; +}) { + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); + const requestConsentEpoch = backupConsentEpoch(appId); + 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 { - const { id }: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); - - yield call(Relay.deleteBackup, { - appId: id, + // 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'); + 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; }); - yield put(setDeleteBackupSuccess(true)); - yield put(setPendingAllBackup(false)); - return true; - } catch (error) { - yield put(setDeleteBackupFailure(true)); - console.log('🚀 ~ deleteBackupWorker ~ error:', error); + 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 })); + } return false; } finally { - yield put(setBackupAllLoading(false)); + if (isCurrent()) yield put(setBackupAllLoading({ appId, status: false })); } } 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; - } - 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); - if (!netInfo.isConnected) { - yield call(setServerBackupFailed); + 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 (pendingAllBackup) { - yield call(backupAllSignersAndVaultsWorker); + if (!netInfo.isConnected) { + yield call(setServerBackupFailed, id); 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; } -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, uaiDetails: { - heading: 'Assisted Server Backup Has Failed', - body: "Retry backup to Keeper's servers", + heading: translations.recoveryBackup.retryNotificationTitle, + body: translations.recoveryBackup.retryNotificationBody, }, }, }); - 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 }) { - 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 }); +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 || requestSession !== currentBackupAccountSession()) { + callback({ status: false, error: true, message: 'Backup account changed.' }); + return; + } + const matched = yield call(runBackupInspection, false, originAppId, false, requestSession); + const stillActive = yield select((state: RootState) => state.storage.appId === originAppId); + if (!stillActive || currentBackupAppId() !== originAppId || + requestSession !== currentBackupAccountSession()) { + callback({ status: false, error: true, message: 'Backup account changed.' }); + return; + } + const { backupRepairStateByAppId = {} } = yield select((state: RootState) => state.bhr); + callback({ + status: matched, + error: backupRepairStateByAppId[originAppId] === 'unverified', + message: matched ? 'Backup verified successfully.' : 'Backup could not be verified.', + }); } export const validateSeverBackupWatcher = createWatcher( diff --git a/src/store/sagas/faucetVisibility.ts b/src/store/sagas/faucetVisibility.ts new file mode 100644 index 0000000000..6352bab485 --- /dev/null +++ b/src/store/sagas/faucetVisibility.ts @@ -0,0 +1,19 @@ +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; + // 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/src/store/sagas/index.ts b/src/store/sagas/index.ts index dc4698337a..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, @@ -45,6 +46,7 @@ import { backupAllSignersAndVaultsWatcher, deleteBackupWatcher, validateSeverBackupWatcher, + backupFreshnessWatcher, } from './bhr'; import { calculateCustomFeeWatcher, @@ -64,7 +66,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, @@ -111,6 +118,7 @@ const rootSaga = function* () { deleteSigningDeviceWatcher, migrateVaultWatcher, updateVaultDetailsWatcher, + updateRecoveryVisibilityWatcher, updateSignerDetails, updateKeyDetails, deleteVaultyWatcher, @@ -154,6 +162,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..0ba70addca 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, @@ -51,14 +51,18 @@ 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 { 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'; import { connectToNode } from '../sagaActions/network'; import { fetchDelayedPolicyUpdate, fetchSignedDelayedTransaction } from '../sagaActions/storage'; -import { setAutomaticCloudBackup, setBackupType } from '../reducers/bhr'; -import { autoWalletsSyncWorker } from './wallets'; +import { + setAutomaticCloudBackup, + setBackupType, + migrateLegacyAutomaticCloudBackup, +} from '../reducers/bhr'; import { addAccount, saveDefaultWalletState, @@ -141,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)); @@ -164,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)); @@ -175,46 +185,94 @@ 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}`); } - const previousVersion = yield select((state) => state.storage.appVersion); - const { plebDueToOffline, wasAutoUpdateEnabledBeforeDowngrade, defaultWalletCreated } = - yield select((state) => state.storage); + const persistedVersion = yield select((state) => state.storage.appVersion); + const { defaultWalletCreated } = yield select((state) => state.storage); // 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 && + (!allAccounts.length || allAccounts[0].appId === keeperApp.id); + yield put( + migrateLegacyAutomaticCloudBackup({ + appId: keeperApp.id, + canAttributeConsent: onlyKnownAccount && appId === keeperApp.id, + }) + ); + } + 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; + const expectedAppId = keeperApp?.id ?? ''; + if (store.getState().storage.appId !== expectedAppId) return; // Store temporary account details if (!allAccounts.length) { + if (currentBackupAccountSession() !== loginAccountSession) return; yield put(setTempDetails({ hash, realmId: REALM_FILE, accountIdentifier: '' })); } 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 (semver.lt(previousVersion, newVersion)) { - yield call(applyUpgradeSequence, { previousVersion, newVersion }); - } 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 ( + currentBackupAccountSession() !== loginAccountSession || + store.getState().storage.appId !== expectedAppId + ) + return; + 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 || + store.getState().storage.appId !== expectedAppId + ) + return; if (appId) { try { - const { id, publicId, subscription }: KeeperApp = yield call( + const { id }: KeeperApp = yield call( dbManager.getObjectByIndex, RealmSchema.KeeperApp ); @@ -225,16 +283,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, id)); yield put( uaiChecks([ @@ -247,12 +299,6 @@ function* credentialsAuthWorker({ payload }) { ]) ); - yield put(resetSyncing()); - - const { pendingAllBackup, automaticCloudBackup } = yield select( - (state: RootState) => state.bhr - ); - if (pendingAllBackup && automaticCloudBackup) yield put(backupAllSignersAndVaults()); if (!allAccounts.length) { // upgraded app yield put(addAccount(appId)); @@ -285,8 +331,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)); } @@ -305,14 +355,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) { @@ -335,9 +387,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/network.ts b/src/store/sagas/network.ts index 4b847db49e..8355c628a8 100644 --- a/src/store/sagas/network.ts +++ b/src/store/sagas/network.ts @@ -1,15 +1,26 @@ -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, } from 'src/services/electrum/predefinedNodes'; import ElectrumClient from 'src/services/electrum/client'; import { captureError } from 'src/services/sentry'; -import { setInitialNodesSaved } from '../reducers/network'; -import { RootState } from '../store'; +import { setInitialNodesSaved, setTestnetFallbackNodeAdded } from '../reducers/network'; +import { RootState, store } from '../store'; import { electrumClientConnectionExecuted, electrumClientConnectionInitiated, @@ -17,10 +28,60 @@ 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() { +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(); + let realmAppId: string; + try { + const keeper = dbManager.getObjectByIndex(RealmSchema.KeeperApp) as unknown as { + id?: string; + }; + realmAppId = keeper?.id; + if (realmDatabase.getDatabase() !== sourceRealm) return false; + } catch { + return false; + } + return ( + workerId === latestConnectWorker && + !!appId && + realmAppId === appId && + current.storage.appId === appId && + current.settings.bitcoinNetworkType === bitcoinNetworkType && + currentBackupAccountSession() === requestSession + ); + }; try { - const { bitcoinNetworkType } = yield select((state: RootState) => state.settings); + // 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()); @@ -28,39 +89,143 @@ 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) { - const currentNodes = yield call(dbManager.getCollection, RealmSchema.NodeConnect); const defaultNodes = yield call(dbManager.getCollection, RealmSchema.DefaultNodeConnect); - let addInitialNode = defaultNodes && defaultNodes.length != 0; - if (!addInitialNode && currentNodes.length == 0) { - addInitialNode = true; - } - - if (addInitialNode) { - const hardcodedInitialNodes = + if (!contextIsCurrent()) return; + if (defaultNodes.length || currentNodes.length === 0) { + const defaults = bitcoinNetworkType === NetworkType.TESTNET ? predefinedTestnetNodes : predefinedMainnetNodes; - 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 + ) + ) + ); } + } + // 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) { + const savedNodes = [...currentNodes, ...additions]; + 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) + ) { + 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)); + } + if (!contextIsCurrent()) return; 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 = () => + 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, + selectedNode.id.toString(), + { + isConnected: false, + } + ); + if (!connectionIsCurrent()) return; + yield call( + dbManager.updateObjectById, + RealmSchema.NodeConnect, + connectedNode.id.toString(), + { + isConnected: true, + } + ); + } + 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({ + successful: false, + error: err?.message || 'Unable to connect to Electrum server', + }) + ); } } diff --git a/src/store/sagas/send_and_receive.ts b/src/store/sagas/send_and_receive.ts index f9c988a13b..4d5e3600f0 100644 --- a/src/store/sagas/send_and_receive.ts +++ b/src/store/sagas/send_and_receive.ts @@ -1,9 +1,9 @@ import { AverageTxFeesByNetwork, SerializedPSBTEnvelop } from 'src/services/wallets/interfaces'; -import { EntityKind, LabelRefType, TxPriority } from 'src/services/wallets/enums'; -import { call, fork, put, select } from 'redux-saga/effects'; +import { EntityKind, TxPriority } from 'src/services/wallets/enums'; +import type { BackupWriteIntent } from 'src/services/backup/intent'; +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'; @@ -39,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'; @@ -129,10 +129,13 @@ function* sendPhaseOneWorker({ payload }: SendPhaseOneAction) { export const sendPhaseOneWatcher = createWatcher(sendPhaseOneWorker, SEND_PHASE_ONE); -function* sendPhaseTwoWorker({ payload }: SendPhaseTwoAction) { +function* sendPhaseTwoWorker({ payload, originAppId, originBackupAccountSession, originBackupWriteIntent }: + 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 @@ -168,16 +171,6 @@ function* sendPhaseTwoWorker({ payload }: SendPhaseTwoAction) { switch (wallet.entityKind) { case EntityKind.WALLET: if (!txid) throw new Error('Send failed: unable to generate txid'); - if (note) { - yield call(addLabelsWorker, { - payload: { - txId: txid, - wallet, - labels: [{ name: note, isSystem: false }], - type: LabelRefType.TXN, - }, - }); - } yield put( sendPhaseTwoExecuted({ successful: true, @@ -204,8 +197,12 @@ function* sendPhaseTwoWorker({ payload }: SendPhaseTwoAction) { throw new Error('Invalid Entity: not a vault/Wallet'); } - if (finalOutputs) { - yield call(handleChangeOutputLabels, finalOutputs, inputs, wallet, txid); + 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)) { @@ -223,10 +220,13 @@ function* sendPhaseTwoWorker({ payload }: SendPhaseTwoAction) { export const sendPhaseTwoWatcher = createWatcher(sendPhaseTwoWorker, SEND_PHASE_TWO); -function* sendPhaseThreeWorker({ payload }: SendPhaseThreeAction) { +function* sendPhaseThreeWorker({ payload, originAppId, originBackupAccountSession, originBackupWriteIntent }: + 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( @@ -274,19 +274,11 @@ function* sendPhaseThreeWorker({ payload }: SendPhaseThreeAction) { }) ); - if (note) { - yield call(addLabelsWorker, { - payload: { - txId: txid, - wallet, - labels: [{ name: note, isSystem: false }], - type: LabelRefType.TXN, - }, - }); - } - - if (finalOutputs) { - yield call(handleChangeOutputLabels, finalOutputs, inputs, wallet, txid); + 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)) { @@ -304,49 +296,22 @@ function* sendPhaseThreeWorker({ payload }: SendPhaseThreeAction) { export const sendPhaseThreeWatcher = createWatcher(sendPhaseThreeWorker, SEND_PHASE_THREE); -function* handleChangeOutputLabels(finalOutputs, inputs, wallet, txid) { - 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, { - payload: { - labelChanges, - UTXO: { txId: txid, vout: changeOutputIndex }, - wallet: wallet as any, - }, - }); - } +function* handleChangeOutputLabels( + finalOutputs, + inputs, + wallet, + txid, + originAppId?: string, + originBackupAccountSession?: number, + originBackupWriteIntent?: BackupWriteIntent, + note?: string +) { + yield call(transactionLabelsWorker, { + originAppId, + originBackupAccountSession, + originBackupWriteIntent, + payload: { txId: txid, wallet, note, finalOutputs, inputs }, + }); } function* calculateSendMaxFee({ payload }: CalculateSendMaxFeeAction) { @@ -463,7 +428,7 @@ function* calculateCustomFee({ payload }: CalculateCustomFeeAction) { export const calculateCustomFeeWatcher = createWatcher(calculateCustomFee, CALCULATE_CUSTOM_FEE); -function* discardBroadcastedTnxWorker({ payload }) { +function* discardBroadcastedTnxWorker({ payload, originAppId, originBackupAccountSession, originBackupWriteIntent }) { const { cachedTxid, vault } = payload; const snapshots = yield select((state) => state.cachedTxn.snapshots); if (snapshots[cachedTxid]) { @@ -476,17 +441,10 @@ function* discardBroadcastedTnxWorker({ payload }) { }); const potentialTxId = snapshots[cachedTxid].potentialTxId; const note = snapshots[cachedTxid].routeParams.note; - yield call(handleChangeOutputLabels, finalOutputs, inputs, vault, potentialTxId); - if (note.length) { - yield call(addLabelsWorker, { - 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 })); } } @@ -512,4 +470,4 @@ const reformatExchangeRates = (rawExchangeRates) => { } }); return formattedExchangeRates; -}; \ No newline at end of file +}; 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 99eaa08f39..4654c02e5a 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, @@ -41,10 +43,21 @@ 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 } = 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 +65,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 +105,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 +128,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 +160,60 @@ 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, + originBackupAccountSession: currentBackupAccountSession(), + originBackupWriteIntent, + 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, + originBackupAccountSession: currentBackupAccountSession(), + originBackupWriteIntent, + 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/upgrade.ts b/src/store/sagas/upgrade.ts index 133d332277..876beefa20 100644 --- a/src/store/sagas/upgrade.ts +++ b/src/store/sagas/upgrade.ts @@ -1,8 +1,9 @@ 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 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,16 +31,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 { setPendingAllBackup } from '../reducers/bhr'; -import { RootState } from '../store'; +import { RootState, store } from '../store'; export const LABELS_INTRODUCTION_VERSION = '1.0.4'; export const BIP329_INTRODUCTION_VERSION = '1.0.7'; @@ -48,64 +53,243 @@ 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, accountSession?: number) => + !!appId && + (accountSession === undefined || accountSession === currentBackupAccountSession()) && + 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.objectForPrimaryKey(RealmSchema.VersionHistory, history.version)) + 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)); +} + +// 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, + isRecovery = false, + originBackupAccountSession, + originBackupWriteIntent, }: { previousVersion: string; newVersion: string; + isRecovery?: boolean; + originBackupAccountSession?: number; + originBackupWriteIntent?: BackupWriteIntent; }) { - console.log(`applying upgrade sequence - from: ${previousVersion} to ${newVersion}`); - - if ( - semver.gte(previousVersion, LABELS_INTRODUCTION_VERSION) && - semver.lt(previousVersion, BIP329_INTRODUCTION_VERSION) - ) { - yield put(migrateLabelsToBip329()); - } - - if (semver.lt(previousVersion, KEY_MANAGEMENT_VERSION)) { - yield call(migrateStructureforSignersInAppImage); - yield call(migrateStructureforVaultInAppImage); - } - if (semver.lt(previousVersion, APP_KEY_UPGRADE_VERSION)) yield call(updateAppKeysToEnableSigning); - - if (semver.lt(previousVersion, HEALTH_CHECK_TIMELINE_MIGRATION_VERSION)) { - yield call(healthCheckTimelineMigration); - } - - if (semver.lt(previousVersion, SIGNER_POLICY_MIGRATION_VERSION)) { - yield call(migrateServerKeyPolicy); - yield put(setPendingAllBackup(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; } - - yield put(setAppVersion(newVersion)); - yield put(updateVersionHistory(previousVersion, newVersion)); } -function* updateVersionHistoryWorker({ +export function* updateVersionHistoryWorker({ payload, + originAppId, + originBackupAccountSession, + originBackupWriteIntent, }: { - payload: { previousVersion: string; newVersion: string }; + payload: { previousVersion: string; newVersion: string; isRecovery?: boolean }; + originAppId?: string; + originBackupAccountSession?: number; + originBackupWriteIntent?: BackupWriteIntent; }) { - const { previousVersion, newVersion } = payload; + const { previousVersion, newVersion, isRecovery = false } = payload; + const session = originBackupAccountSession ?? currentBackupAccountSession(); try { - const app: KeeperApp = yield call(dbManager.getObjectByIndex, RealmSchema.KeeperApp); - const response = yield call(Relay.updateAppImage, { - appId: app.id, - version: newVersion, - }); - 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; } } @@ -114,254 +298,217 @@ export const updateVersionHistoryWatcher = createWatcher( UPDATE_VERSION_HISTORY ); -function* migrateLablesWorker() { - try { - 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) { - yield call(dbManager.createObjectBulk, RealmSchema.Tags, tags); - - const { id, primarySeed }: KeeperApp = yield call( - dbManager.getObjectByIndex, - RealmSchema.KeeperApp - ); - 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 : [], []); +function* migrateLablesWorker({ + originAppId, + originBackupAccountSession, + originBackupWriteIntent, + upgradeGroup, +}: { + originAppId?: string; + originBackupAccountSession?: number; + originBackupWriteIntent?: BackupWriteIntent; + upgradeGroup?: UpgradeGroup; +}) { + 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() { - try { - const wallets = yield call(dbManager.getCollection, RealmSchema.Wallet); - const signers = yield call(dbManager.getCollection, RealmSchema.Signer); - const response = yield call(updateAppImageWorker, { payload: { wallets, signers } }); - 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) {} +function* migrateStructureforSignersInAppImage( + appId: string, + session: number, + intent?: BackupWriteIntent, + parent?: UpgradeGroup +) { + 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() { - try { - 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 }, - }); - } catch (err) { - console.log('Something went wrong in updating the vault image', err); - } +function* migrateStructureforVaultInAppImage( + appId: string, + session: number, + intent?: BackupWriteIntent, + parent?: UpgradeGroup +) { + 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() { - try { - 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); - updateVaultSigners(extendedKeyMap, signers); - updateSignerDetails(myAppKeySigners, extendedKeyMap); - const response = yield call(updateAppImageWorker, { payload: { signers } }); - 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); - } +function* updateAppKeysToEnableSigning( + appId: string, + session: number, + intent?: BackupWriteIntent, + parent?: UpgradeGroup +) { + 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() { - // 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); - - 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 (!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/src/store/sagas/utxos.ts b/src/store/sagas/utxos.ts index 909c03dec7..73b95d1f89 100644 --- a/src/store/sagas/utxos.ts +++ b/src/store/sagas/utxos.ts @@ -1,23 +1,138 @@ 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 Relay from 'src/services/backend/Relay'; +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 { 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'; 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, 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 { + setServerBackupFailed, + updateAppImageWorker, + updateVaultImageWorker, +} from './bhr'; import { encrypt, generateEncryptionKey, hash256 } from 'src/utils/service-utilities/encryption'; +type LabelSyncScope = { appId: string; networkType: NetworkType }; + +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[] = [], + scope?: LabelSyncScope, + accountSession?: number, + operation?: BackupLocalOperation +) { + 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 { 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( + originAppId: string, + schema: RealmSchema, + id: string, + specs: any, + 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 (!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 } { + return { ...action, originAppId }; +} + export function* addLabelsWorker({ payload, + originAppId, + originBackupAccountSession, + originBackupWriteIntent, }: { payload: { txId: string; @@ -26,9 +141,18 @@ export function* addLabelsWorker({ labels: { name: string; isSystem: boolean }[]; type; }; + originAppId?: string; + originBackupAccountSession?: number; + originBackupWriteIntent?: BackupWriteIntent; }) { try { - yield put(setSyncingUTXOs(true)); + if (!originAppId) return; + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); + if (requestSession !== currentBackupAccountSession()) return; + const app = currentOriginApp(originAppId); + if (!app) return; + const operation = beginBackupLocalOperation(originAppId, originBackupWriteIntent, requestSession); + yield put(originUiAction(setSyncingUTXOs(true), originAppId)); const { txId, vout, wallet, labels, type } = payload; const origin = generateAbbreviatedOutputDescriptors(wallet); const tags = []; @@ -44,35 +168,29 @@ export function* addLabelsWorker({ }; tags.push(tag); }); - const { id, primarySeed }: KeeperApp = yield call( - dbManager.getObjectByIndex, - RealmSchema.KeeperApp + const committedOperation: BackupLocalCommit = yield call( + persistTagsForOrigin, originAppId, tags, [], undefined, requestSession, operation ); - yield call(dbManager.createObjectBulk, RealmSchema.Tags, tags); + if (!committedOperation.committed) return; try { - const backupResponse = yield call(checkBackupCondition); - if (!backupResponse) { - 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, []); - } else yield delay(100); - } catch (error) { - console.log('🚀 ~ addLabelsWorker error:', error); - yield call(setServerBackupFailed); + yield call(backupCommittedLabels, originAppId, requestSession, committedOperation, tags); + } catch { + if (currentOriginApp(originAppId) && requestSession === currentBackupAccountSession()) + 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, + originBackupAccountSession, + originBackupWriteIntent, }: { payload: { labelChanges: { @@ -83,11 +201,28 @@ export function* bulkUpdateLabelsWorker({ txId?: string; address?: string; wallet: Wallet; + scope?: LabelSyncScope; }; + originAppId?: string; + originBackupAccountSession?: number; + originBackupWriteIntent?: BackupWriteIntent; }) { try { - yield put(setSyncingUTXOs(true)); - const { labelChanges, wallet, UTXO, txId, address } = payload; + if (!originAppId) return; + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); + if (requestSession !== currentBackupAccountSession()) return; + const app = currentOriginApp(originAppId); + if (!app) return; + const operation = beginBackupLocalOperation(originAppId, originBackupWriteIntent, requestSession); + yield put(originUiAction(setSyncingUTXOs(true), originAppId)); + const { labelChanges, wallet, UTXO, txId, address, scope } = payload; + if ( + scope && + (scope.appId !== originAppId || + store.getState().settings.bitcoinNetworkType !== scope.networkType || + wallet.networkType !== scope.networkType) + ) + return; const origin = generateAbbreviatedOutputDescriptors(wallet); let addedTags: BIP329Label[] = []; let deletedTagIds: string[] = []; @@ -107,43 +242,29 @@ 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 committedOperation: BackupLocalCommit = yield call( + persistTagsForOrigin, originAppId, addedTags, deletedTagIds, scope, requestSession, operation ); - yield call(dbManager.createObjectBulk, RealmSchema.Tags, addedTags); - for (const element of deletedTagIds) { - yield call(dbManager.deleteObjectById, RealmSchema.Tags, element); - } + if (!committedOperation.committed) return; try { - const backupResponse = yield call(checkBackupCondition); - if (!backupResponse) { - 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( - Relay.modifyLabels, - id, - tagsToBackup.length ? tagsToBackup : [], - tagsToDelete.length ? tagsToDelete : [] - ); - } else yield delay(100); - } catch (error) { - yield call(setServerBackupFailed); + yield call(backupCommittedLabels, originAppId, requestSession, committedOperation, addedTags, deletedTagIds); + } catch { + if (currentOriginApp(originAppId) && requestSession === currentBackupAccountSession()) + 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, + originBackupAccountSession, + originBackupWriteIntent, }: { payload: { labels: [ @@ -155,9 +276,18 @@ export function* importLabelsWorker({ } ]; }; + originAppId?: string; + originBackupAccountSession?: number; + originBackupWriteIntent?: BackupWriteIntent; }) { try { - yield put(setSyncingUTXOs(true)); + if (!originAppId) return; + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); + if (requestSession !== currentBackupAccountSession()) return; + const app = currentOriginApp(originAppId); + if (!app) return; + const operation = beginBackupLocalOperation(originAppId, originBackupWriteIntent, requestSession); + yield put(originUiAction(setSyncingUTXOs(true), originAppId)); const { labels } = payload; let addedTags: BIP329Label[] = []; if (labels) { @@ -173,30 +303,87 @@ export function* importLabelsWorker({ })); } - const { primarySeed, id }: KeeperApp = yield call( - dbManager.getObjectByIndex, - RealmSchema.KeeperApp + const committedOperation: BackupLocalCommit = yield call( + persistTagsForOrigin, originAppId, addedTags, [], undefined, requestSession, operation ); - yield call(dbManager.createObjectBulk, RealmSchema.Tags, addedTags); - + if (!committedOperation.committed) return; try { - const backupResponse = yield call(checkBackupCondition); - if (!backupResponse) { - 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 : [], []); - } else yield delay(100); - } catch (error) { - yield call(setServerBackupFailed); + yield call(backupCommittedLabels, originAppId, requestSession, committedOperation, addedTags); + } catch { + if (currentOriginApp(originAppId) && requestSession === currentBackupAccountSession()) + 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)); + } +} + +// 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 = (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]; + 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)); } } @@ -206,6 +393,9 @@ export const importLabelsWatcher = createWatcher(importLabelsWorker, IMPORT_LABE export function* markUTXOSpendabilityWorker({ payload, + originAppId, + originBackupAccountSession, + originBackupWriteIntent, }: { payload: { wallet: any; @@ -213,34 +403,74 @@ export function* markUTXOSpendabilityWorker({ vout: number; spendability: UTXOSpendability; }; + originAppId?: string; + originBackupAccountSession?: number; + originBackupWriteIntent?: BackupWriteIntent; }) { try { + if (!originAppId) return; + 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; + 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 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: freshWallet as Vault, isUpdate: true }, + originAppId, + originBackupAccountSession: requestSession, + originBackupWriteIntent, + committedOperation, + }) + : yield call(updateAppImageWorker, { + payload: { wallets: [freshWallet as Wallet] }, + originAppId, + originBackupAccountSession: requestSession, + originBackupWriteIntent, + committedOperation, + }); + if (!response?.updated) yield call(setServerBackupFailed, originAppId); + } catch { + yield call(setServerBackupFailed, originAppId); + } } catch (e) { console.log('markUTXOSpendabilityWorker error:', e); } diff --git a/src/store/sagas/walletRefreshCoordinator.js b/src/store/sagas/walletRefreshCoordinator.js new file mode 100644 index 0000000000..e7389e7fd7 --- /dev/null +++ b/src/store/sagas/walletRefreshCoordinator.js @@ -0,0 +1,44 @@ +// 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 = {}, 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 }; + + 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 4f54a44ec0..451924a61a 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, @@ -17,28 +18,34 @@ 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, fork, put, select } from 'redux-saga/effects'; +import { call, delay, put, select } from 'redux-saga/effects'; import { setSyncing, setTestCoinsFailed, setTestCoinsReceived, setTestCoinsQuotaReached, + setTestCoinsPending, setSignerPolicyError, + finishRefreshRequest, } from 'src/store/reducers/wallets'; 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, + 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'; @@ -63,9 +70,19 @@ 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'; +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 , backupBsmsOnCloud } from '../sagaActions/bhr'; import { initiateVaultMigration, @@ -81,10 +98,10 @@ import { TEST_SATS_RECIEVE, UPDATE_SIGNER_POLICY, UPDATE_WALLET_DETAILS, - refreshWallets, UPDATE_SIGNER_DETAILS, UPDATE_KEY_DETAILS, UPDATE_VAULT_DETAILS, + UPDATE_RECOVERY_VISIBILITY, GENERATE_NEW_ADDRESS, UPDATED_VAULT_SIGNERS_XPRIV, } from '../sagaActions/wallets'; @@ -109,6 +126,9 @@ import { deleteVaultImageWorker, updateAppImageWorker, updateVaultImageWorker, + buildAutomaticAppMutation, + buildAutomaticVaultMutation, + queueCommittedBackupWorker, } from './bhr'; import { relaySignersUpdateFail, @@ -124,15 +144,133 @@ import { showDeletingKeyModal, hideDeletingKeyModal, showKeyDeletedSuccessModal, + isAutomaticCloudBackupEnabled, + invalidateBackupRepair, + setPendingAllBackup, } 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'; + +const WALLET_SYNC_SCOPE_CHANGED = 'WALLET_SYNC_SCOPE_CHANGED'; + +const currentWalletAccountId = (): string | undefined => { + try { + return (dbManager.getObjectByIndex(RealmSchema.KeeperApp) as unknown as KeeperApp)?.id; + } catch { + return undefined; + } +}; +const assertWalletAccount = (appId: string, expectedSession?: number) => { + if ( + !appId || + currentWalletAccountId() !== appId || + store.getState().storage.appId !== appId || + (expectedSession !== undefined && currentBackupAccountSession() !== expectedSession) + ) 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; @@ -203,10 +341,25 @@ function* addNewWallet( } } -export function* addNewWalletsWorker({ payload: newWalletInfo }: { payload: NewWalletInfo[] }) { +export function* addNewWalletsWorker({ + payload: newWalletInfo, + originAppId, + originBackupAccountSession, + originBackupWriteIntent, +}: { + payload: NewWalletInfo[]; + originAppId?: string; + originBackupAccountSession?: number; + originBackupWriteIntent?: BackupWriteIntent; +}) { + 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, requestSession); + const operation = beginBackupLocalOperation(sourceAppId, originBackupWriteIntent, requestSession); for (const { walletType, walletDetails, importDetails } of newWalletInfo) { const wallet: Wallet = yield call( @@ -216,26 +369,34 @@ export function* addNewWalletsWorker({ payload: newWalletInfo }: { payload: NewW app, importDetails ); + assertWalletAccount(sourceAppId, requestSession); wallets.push(wallet); } if (wallets.length > 0) { yield put(setRelayWalletUpdateLoading(true)); - const response = yield call(updateAppImageWorker, { payload: { wallets } }); - if (response.updated) { - 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; + 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, + }); + assertWalletAccount(sourceAppId, requestSession); + yield put(relayWalletUpdateSuccess()); + return true; } + return false; } catch (err) { console.log(err); - yield put(relayWalletUpdateFail(err.message ? err.message : err.toString())); + if (currentWalletAccountId() === sourceAppId && + store.getState().storage.appId === sourceAppId && + currentBackupAccountSession() === requestSession) + yield put(relayWalletUpdateFail(err.message ? err.message : err.toString())); return false; } } @@ -251,94 +412,71 @@ export interface NewVaultInfo { miniscriptTypes?: MiniscriptTypes[]; } -export function* addNewVaultWorker({ - payload, -}: { - payload: { - newVaultInfo?: NewVaultInfo; - isMigrated?: boolean; - oldVaultId?: string; - }; +export function* addNewVaultWorker({ payload, originAppId, originBackupAccountSession, originBackupWriteIntent }: { + originAppId?: string; + originBackupAccountSession?: number; + originBackupWriteIntent?: BackupWriteIntent; + payload: { newVaultInfo?: NewVaultInfo; isMigrated?: boolean; oldVaultId?: string }; }) { + const sourceAppId = originAppId || currentWalletAccountId(); + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); try { + 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 } }); - if (newVaultResponse.updated) { - 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, { - payload: { - vault: { - ...oldVault, - ...updatedParams, - }, - }, - }); - if (archivedVaultresponse.updated) { - 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; } } @@ -348,19 +486,28 @@ export const addNewVaultWatcher = createWatcher(addNewVaultWorker, ADD_NEW_VAULT export function* addSigningDeviceWorker({ payload: { signers }, callback, + originAppId, + originBackupAccountSession, + originBackupWriteIntent, }: { payload: { signers: Signer[] }; callback?: () => void; + originAppId?: string; + originBackupAccountSession?: number; + originBackupWriteIntent?: BackupWriteIntent; }) { - if (!signers.length) return; - for (let i = 0; i < signers.length; i++) { - const signer = signers[i]; - yield call(mergeSimilarKeysWorker, { payload: { signer } }); - } + if (!signers.length) return false; + const sourceAppId = originAppId || currentWalletAccountId(); + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); + assertWalletAccount(sourceAppId, requestSession); + 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 ); @@ -383,7 +530,7 @@ export function* addSigningDeviceWorker({ }); } catch (e) { captureError(e); - return; + return false; } const keysMatch = (type, newSigner, existingSigner) => @@ -435,20 +582,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); @@ -477,63 +624,90 @@ export function* addSigningDeviceWorker({ id: getKeyUID(signer), })); yield put(setRelaySignersUpdateLoading(true)); - const response = yield call(updateAppImageWorker, { payload: { signers: signersToUpdate } }); - if (response.updated) { - 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 }); - } else { - const errorMsg = response.error?.message - ? response.error.message.toString() - : response.error.toString(); - yield put(relaySignersUpdateFail(errorMsg)); - } + 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 (!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; } } export const addSigningDeviceWatcher = createWatcher(addSigningDeviceWorker, ADD_SIGINING_DEVICE); -function* deleteSigningDeviceWorker({ payload: { signers } }: { payload: { signers: Signer[] } }) { +function* deleteSigningDeviceWorker({ + payload: { signers }, + originAppId, + originBackupAccountSession, + originBackupWriteIntent, +}: { + payload: { signers: Signer[] }; + originAppId?: string; + originBackupAccountSession?: number; + originBackupWriteIntent?: BackupWriteIntent; +}) { + const sourceAppId = originAppId || currentWalletAccountId(); + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); try { + assertWalletAccount(sourceAppId, requestSession); 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, + originBackupAccountSession: requestSession, + originBackupWriteIntent, + payload: { signerIds: signersToDeleteIds }, + }); + if (!response?.updated) { + assertWalletAccount(sourceAppId, requestSession); + yield put(hideDeletingKeyModal()); + yield put(relaySignersUpdateFail('An error occurred while deleting signers.')); + return false; } + assertWalletAccount(sourceAppId, requestSession); 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 && + currentBackupAccountSession() === requestSession) { + yield put(hideDeletingKeyModal()); + yield put(relaySignersUpdateFail('An error occurred while deleting signers.')); + } + return false; } } @@ -542,27 +716,34 @@ export const deleteSigningDeviceWatcher = createWatcher( DELETE_SIGINING_DEVICE ); -function* archiveSigningDeviceWorker({ payload: { signers } }: { payload: { signers: Signer[] } }) { +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 signersToArchiveIds = []; - for (const signer of signers) { - signersToArchiveIds.push(getKeyUID(signer)); - } - if (signers.length) { - for (let i = 0; i < signers.length; i++) { - yield call(updateSignerDetailsWorker, { - payload: { - signer: signers[i], - key: 'archived', - value: true, - }, - }); + assertWalletAccount(sourceAppId, requestSession); + 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; } } @@ -573,15 +754,30 @@ export const archiveSigningDeviceWatcher = createWatcher( function* migrateVaultWorker({ payload, + originAppId, + originBackupAccountSession, + originBackupWriteIntent, }: { payload: { newVaultInfo: NewVaultInfo; oldVaultId: string }; + originAppId?: string; + originBackupAccountSession?: number; + originBackupWriteIntent?: BackupWriteIntent; }) { + const sourceAppId = originAppId || currentWalletAccountId(); + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); try { + assertWalletAccount(sourceAppId, requestSession); const { newVaultInfo, oldVaultId } = payload; const migrated = yield call(addNewVaultWorker, { + originAppId: sourceAppId, + originBackupAccountSession: requestSession, + 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({ @@ -591,14 +787,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; } } @@ -606,16 +806,32 @@ export const migrateVaultWatcher = createWatcher(migrateVaultWorker, MIGRATE_VAU function* refreshWalletsWorker({ payload, + originAppId, + originBackupAccountSession, + originBackupWriteIntent, }: { + originAppId?: string; + originBackupAccountSession?: number; + originBackupWriteIntent?: BackupWriteIntent; payload: { wallets: (Wallet | Vault)[]; options: { hardRefresh?: boolean; addNotifications?: boolean; dustScan?: boolean }; + requestId?: string; }; }) { - let { wallets, options } = payload; + 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 && + currentBackupAccountSession() === requestSession; // 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 || @@ -627,13 +843,116 @@ function* refreshWalletsWorker({ }); try { - if (!wallets || wallets.length === 0) return; + if ( + !sourceAppId || + currentWalletAccountId() !== sourceAppId || + store.getState().storage.appId !== sourceAppId || + currentBackupAccountSession() !== requestSession + ) + throw new Error(WALLET_SYNC_SCOPE_CHANGED); + const refreshScope = yield select((state: RootState) => ({ + appId: state.storage.appId, + networkType: state.settings.bitcoinNetworkType, + })); + if (refreshScope.appId !== sourceAppId) throw new Error(WALLET_SYNC_SCOPE_CHANGED); + const assertAccountRealmScope = () => { + const currentState = store.getState(); + 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 || + keeperApp?.id !== refreshScope.appId || + currentBackupAccountSession() !== requestSession + ) + throw new Error(WALLET_SYNC_SCOPE_CHANGED); + }; + wallets = wallets.filter((wallet) => wallet.networkType === refreshScope.networkType); + if (wallets.length === 0) { + succeeded = true; + return true; + } - if (!ELECTRUM_CLIENT.isClientConnected) { + lease = acquireWalletRefresh( + refreshScope.appId, + refreshScope.networkType, + wallets, + options, + requestSession + ); + 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 || + currentBackupAccountSession() !== requestSession + ) + 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(() => { + assertAccountRealmScope(); + return dbManager.getObjectById(schema, wallet.id); + }); + if (!currentWallet || currentWallet.networkType !== refreshScope.networkType) return false; + 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 { + 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); + yield call(connectToNodeWorker, { + originAppId: sourceAppId, + originBackupAccountSession: requestSession, + originBackupWriteIntent, + }); } + 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(ELECTRUM_NOT_CONNECTED_ERR); + } + }; + assertCurrentScope(); + yield put(setSyncing({ wallets, isSyncing: true })); const network = WalletUtilities.getNetworkByType(wallets[0].networkType); @@ -648,7 +967,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) { @@ -672,8 +991,10 @@ function* refreshWalletsWorker({ WalletOperations.syncWalletsViaElectrumClient, wallets, network, - options.hardRefresh + options.hardRefresh, + assertCurrentScope ); + assertCurrentScope(); let labels: { ref: string; label: string; isSystem: boolean }[]; @@ -681,6 +1002,15 @@ 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 @@ -700,9 +1030,21 @@ function* refreshWalletsWorker({ ); } - yield fork(bulkUpdateLabelsWorker, { - payload: { labelChanges, UTXO: utxo, wallet: synchedWallet as any }, - }); + if (labelChanges.added.length || labelChanges.deleted.length) { + assertCurrentScope(); + yield call(bulkUpdateLabelsWorker, { + originAppId: sourceAppId, + originBackupAccountSession: requestSession, + originBackupWriteIntent, + payload: { + labelChanges, + UTXO: utxo, + wallet: synchedWallet as any, + scope: refreshScope, + }, + }); + assertCurrentScope(); + } if (options.addNotifications) { if (synchedWallet.type !== VaultType.CANARY) { @@ -725,7 +1067,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])); } @@ -757,6 +1102,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 @@ -800,14 +1146,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. @@ -860,21 +1205,49 @@ 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 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(() => { + 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'); + 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); + yield put(invalidateBackupRepair(sourceAppId)); + yield put(setPendingAllBackup({ appId: sourceAppId, pending: true })); + } } if (options.addNotifications && newDustUTXOs.length > 0) { yield put(setPendingDustToast(synchedWallet.id)); } } // end for (synchedWalletWithUTXOs) + succeeded = true; + return true; } catch (err) { + 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( @@ -889,8 +1262,14 @@ function* refreshWalletsWorker({ ); captureError(err); } + return false; } finally { - yield put(setSyncing({ wallets, isSyncing: false })); + if (lease?.owner) { + if (isSourceAccountCurrent()) yield put(setSyncing({ wallets, isSyncing: false })); + lease.complete(succeeded); + } + if (requestId && isSourceAccountCurrent()) + yield put(finishRefreshRequest({ requestId, succeeded })); } } @@ -898,33 +1277,159 @@ export const refreshWalletsWatcher = createWatcher(refreshWalletsWorker, REFRESH export function* autoWalletsSyncWorker({ payload, + originAppId, + originBackupAccountSession, + originBackupWriteIntent, }: { - payload: { syncAll?: boolean; hardRefresh?: boolean; addNotifications?: boolean }; + originAppId?: string; + originBackupAccountSession?: number; + originBackupWriteIntent?: BackupWriteIntent; + payload: { + syncAll?: boolean; + hardRefresh?: boolean; + addNotifications?: boolean; + backupCheckAppId?: string; + archivedOnly?: boolean; + requestId?: string; + }; }) { - const { syncAll, hardRefresh, addNotifications } = 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); + const sourceAppId = originAppId || currentWalletAccountId(); + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); + const { syncAll, hardRefresh, addNotifications, backupCheckAppId, archivedOnly, requestId } = + payload; + let activeSucceeded = true; + let archivedSucceeded = true; + const isSourceAccountCurrent = () => + !!sourceAppId && + currentWalletAccountId() === sourceAppId && + store.getState().storage.appId === sourceAppId && + currentBackupAccountSession() === requestSession; + try { + if (!isSourceAccountCurrent()) { + activeSucceeded = false; + archivedSucceeded = false; + return; + } + const syncScope = yield select((state: RootState) => ({ + appId: state.storage.appId, + networkType: state.settings.bitcoinNetworkType, + })); + if (syncScope.appId !== sourceAppId || !isSourceAccountCurrent()) { + activeSucceeded = false; + archivedSucceeded = false; + return; + } + const wallets: Wallet[] = yield call( + dbManager.getObjectByIndex, + RealmSchema.Wallet, + null, + true + ); + if (!isSourceAccountCurrent()) { + activeSucceeded = false; + archivedSucceeded = false; + return; + } + 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 !== sourceAppId || + currentNetworkType !== syncScope.networkType || + !isSourceAccountCurrent() + ) { + activeSucceeded = false; + archivedSucceeded = false; + return; + } + const { active, archived } = selectWalletsForSync( + wallets || [], + vaults || [], + syncScope.networkType, + { syncAll, archivedOnly } + ); - 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)); + if (active.length) { + activeSucceeded = yield call(refreshWalletsWorker, { + originAppId: sourceAppId, + originBackupAccountSession: requestSession, + originBackupWriteIntent, + payload: { + wallets: active.map((wallet) => getJSONFromRealmObject(wallet) as unknown as Wallet | Vault), + options: { hardRefresh, addNotifications }, + }, + }); } - } - walletsToSync = walletsToSync.filter((wallet) => wallet.networkType === bitcoinNetworkType); - - if (walletsToSync.length) { - yield call(refreshWalletsWorker, { - payload: { - wallets: walletsToSync, - options: { - hardRefresh, - addNotifications, + const beforeArchive = yield select((state: RootState) => ({ + appId: state.storage.appId, + networkType: state.settings.bitcoinNetworkType, + })); + if ( + beforeArchive.appId !== sourceAppId || + beforeArchive.networkType !== syncScope.networkType || + !isSourceAccountCurrent() + ) { + 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, { + originAppId: sourceAppId, + originBackupAccountSession: requestSession, + originBackupWriteIntent, + payload: { + wallets: archived.map( + (wallet) => getJSONFromRealmObject(wallet) as unknown as Wallet | Vault + ), + options: { hardRefresh, addNotifications: false }, }, - }, - }); + }); + } + + // Inspect the originating account only after every refreshed spec is persisted. + if ( + backupCheckAppId === sourceAppId && + activeSucceeded && + archivedSucceeded && + isSourceAccountCurrent() && + store.getState().settings.bitcoinNetworkType === syncScope.networkType + ) { + const { + automaticCloudBackupByAppId, + pendingAllBackupByAppId, + backupRepairCompletedByAppId = {}, + } = yield select((state: RootState) => state.bhr); + if ( + isSourceAccountCurrent() && + store.getState().settings.bitcoinNetworkType === syncScope.networkType && + isAutomaticCloudBackupEnabled({ automaticCloudBackupByAppId }, sourceAppId) && + (pendingAllBackupByAppId?.[sourceAppId] || !backupRepairCompletedByAppId[sourceAppId]) + ) { + yield put({ + ...checkBackupFreshness(sourceAppId), + originBackupAccountSession: requestSession, + originBackupWriteIntent, + }); + } + } + } catch (err) { + activeSucceeded = false; + archivedSucceeded = false; + captureError(err); + } finally { + if (requestId && isSourceAccountCurrent()) { + yield put( + finishRefreshRequest({ + requestId, + succeeded: activeSucceeded && archivedSucceeded, + }) + ); + } } } @@ -932,6 +1437,9 @@ export const autoWalletsSyncWatcher = createWatcher(autoWalletsSyncWorker, AUTO_ export function* updateSignerPolicyWorker({ payload, + originAppId, + originBackupAccountSession, + originBackupWriteIntent, }: { payload: { signer: Signer; @@ -942,12 +1450,18 @@ export function* updateSignerPolicyWorker({ }; verificationToken: number; }; + originAppId?: string; + originBackupAccountSession?: number; + originBackupWriteIntent?: BackupWriteIntent; }) { - const fcmToken = yield select((state: RootState) => state.notifications.fcmToken); - const { bitcoinNetwork } = yield select((state: RootState) => state.settings); - + 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 = signingKey?.xfp || WalletUtilities.getFingerprintFromExtendedKey( @@ -965,6 +1479,7 @@ export function* updateSignerPolicyWorker({ updates, fcmToken ); + assertWalletAccount(sourceAppId, requestSession); if (delayedPolicyUpdate) { yield put(updateDelayedPolicyUpdate(delayedPolicyUpdate)); @@ -974,22 +1489,22 @@ 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')); } catch (err) { - yield put(setSignerPolicyError('failure')); + if (currentWalletAccountId() === sourceAppId && + store.getState().storage.appId === sourceAppId && + currentBackupAccountSession() === requestSession) + yield put(setSignerPolicyError('failure')); } } @@ -998,273 +1513,429 @@ export const updateSignerPolicyWatcher = createWatcher( UPDATE_SIGNER_POLICY ); -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); +export function* testcoinsWorker({ payload, originAppId, originBackupAccountSession, originBackupWriteIntent }) { + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); + let { wallet } = payload; + const faucetScope = yield select((state: RootState) => ({ + appId: state.storage.appId, + networkType: state.settings.bitcoinNetworkType, + })); + const isCurrentFaucetScope = () => { + const state = store.getState(); + 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 && + keeperApp?.id === faucetScope.appId && + currentBackupAccountSession() === requestSession + ); + } catch { + return false; + } + }; + 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; + } + + 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 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') { + yield put(setTestCoinsPending('unknown')); } else { yield put(setTestCoinsFailed(true)); } + return; + } + + if (!isCurrentFaucetScope()) return; + + let lastAttemptSynced = false; + for (let attempt = 0; attempt < 8; attempt += 1) { + let attemptSynced = false; + try { + if (!isCurrentFaucetScope()) 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, { + originAppId: faucetScope.appId, + originBackupAccountSession: requestSession, + originBackupWriteIntent, + }); + } + if (!isCurrentFaucetScope()) 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()) return; + attemptSynced = true; + if (txids.some((seenTxid: string) => seenTxid.toLowerCase() === txid.toLowerCase())) { + const refreshed = yield call(refreshWalletsWorker, { + originAppId: faucetScope.appId, + originBackupAccountSession: requestSession, + originBackupWriteIntent, + 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)) { + if (!isCurrentFaucetScope()) return; + yield put(setTestCoinsReceived(true)); + return; + } + } else attemptSynced = false; + } + } catch (err) { + attemptSynced = false; + } + lastAttemptSynced = attemptSynced; + if (attempt < 7) yield delay(4000); } + if (!isCurrentFaucetScope()) return; + yield put(setTestCoinsPending(lastAttemptSynced ? 'propagation' : 'sync')); } export const testcoinsWatcher = createWatcher(testcoinsWorker, TEST_SATS_RECIEVE); -function* updateWalletDetailsWorker({ payload }) { - const { - wallet, - details, - }: { - wallet: Wallet; - details: { - name: string; - description: string; - }; - } = payload; +function* updateWalletDetailsWorker({ payload, originAppId, originBackupAccountSession, originBackupWriteIntent }) { + const sourceAppId = originAppId || currentWalletAccountId(); + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); try { - 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] } }); - if (response.updated) { - 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!')); + assertWalletAccount(sourceAppId, requestSession); + 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 (!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 }) { - const { - vault, - details, - }: { - vault: Vault; - details: { - name: string; - description: string; - }; - } = payload; +function* updateVaultDetailsWorker({ payload, originAppId, originBackupAccountSession, originBackupWriteIntent }) { + const sourceAppId = originAppId || currentWalletAccountId(); + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); try { - 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; + assertWalletAccount(sourceAppId, requestSession); + 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 (!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); - const response = yield call(updateVaultImageWorker, { - payload: { vault }, +// 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 (response.updated) { - yield call(dbManager.updateObjectById, RealmSchema.Vault, vault.id, { - presentationData, + 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, }); - 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!')); + 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 updateVaultDetailsWatcher = createWatcher( - updateVaultDetailsWorker, - UPDATE_VAULT_DETAILS +export const updateRecoveryVisibilityWatcher = createWatcher( + updateRecoveryVisibilityWorker, UPDATE_RECOVERY_VISIBILITY ); -export function* updateSignerDetailsWorker({ payload }) { - const { - signer, - key, - value, - }: { - signer: Signer; - key: string; - value: any; - } = payload; - yield put(setRelaySignersUpdateLoading(true)); +export function* updateSignerDetailsWorker({ payload, originAppId, originBackupAccountSession, originBackupWriteIntent }) { + const sourceAppId = originAppId || currentWalletAccountId(); + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); try { - signer[key] = value; - const response = yield call(updateAppImageWorker, { payload: { signers: [signer] } }); - if (response.updated) { - 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')); + assertWalletAccount(sourceAppId, requestSession); + 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 (!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; } } export const updateSignerDetails = createWatcher(updateSignerDetailsWorker, UPDATE_SIGNER_DETAILS); -function* updateKeyDetailsWorker({ payload }) { - 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; +function* updateKeyDetailsWorker({ payload, originAppId, originBackupAccountSession, originBackupWriteIntent }) { + const sourceAppId = originAppId || currentWalletAccountId(); + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); + 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); - } - yield call(dbManager.updateObjectByPrimaryId, RealmSchema.VaultSigner, 'xpub', signer.xpub, { - registeredVaults: updatedRegsteredVaults, - }); - return; + 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; } - - yield call(dbManager.updateObjectByPrimaryId, RealmSchema.VaultSigner, 'xpub', signer.xpub, { - [key]: value, - }); } export const updateKeyDetails = createWatcher(updateKeyDetailsWorker, UPDATE_KEY_DETAILS); -function* deleteVaultWorker({ payload }) { - const { vaultId } = payload; +function* deleteVaultWorker({ payload, originAppId, originBackupAccountSession, originBackupWriteIntent, onComplete }) { + const sourceAppId = originAppId || currentWalletAccountId(); + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); + const reportsThroughCallback = typeof onComplete === 'function'; try { - yield put(setRelayVaultUpdateLoading(true)); - const response = yield call(deleteVaultImageWorker, { payload: { vaultIds: [vaultId] } }); - if (response.updated) { - yield call(dbManager.deleteObjectById, RealmSchema.Vault, vaultId); - yield put(relayVaultUpdateSuccess()); - } else { - const errorMsg = response.error?.message - ? response.error.message.toString() - : response.error.toString(); - yield put(relayVaultUpdateFail(errorMsg)); + assertWalletAccount(sourceAppId, requestSession); + if (!reportsThroughCallback) yield put(setRelayVaultUpdateLoading(true)); + 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 (!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 && + currentBackupAccountSession() === requestSession) + yield put(relayVaultUpdateFail('Vault deletion could not be saved')); + return false; + } finally { + if (!reportsThroughCallback && currentWalletAccountId() === sourceAppId && + currentBackupAccountSession() === requestSession) + yield put(setRelayVaultUpdateLoading(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); + } } - } catch (err) { - yield put(relayVaultUpdateFail('Something went wrong while deleting the vault!')); } + return deleted; } -export const deleteVaultyWatcher = createWatcher(deleteVaultWorker, DELETE_VAULT); +export const deleteVaultyWatcher = createWatcher(deleteVaultRequestWorker, DELETE_VAULT); -function* reinstateVaultWorker({ payload }) { - const { vaultId } = payload; +function* reinstateVaultWorker({ payload, originAppId, originBackupAccountSession, originBackupWriteIntent }) { + const sourceAppId = originAppId || currentWalletAccountId(); + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); try { - 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, { - 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) { - yield call(dbManager.updateObjectById, RealmSchema.Vault, vaultId, updatedParams); - for (let i = 0; i < vault.signers.length; i++) { - yield call(updateSignerDetailsWorker, { - payload: { - signer: signerMap[getKeyUID(vault.signers[i])], - key: 'archived', - value: false, - }, - }); + assertWalletAccount(sourceAppId, requestSession); + 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; } } export const reinstateVaultWatcher = createWatcher(reinstateVaultWorker, REINSTATE_VAULT); -function* refillMobileKeyWorker({ payload }) { +function* refillMobileKeyWorker({ payload, originAppId, originBackupAccountSession, originBackupWriteIntent }) { + const sourceAppId = originAppId || currentWalletAccountId(); + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); const { vaultKey } = payload; try { + assertWalletAccount(sourceAppId, requestSession); const { xpriv } = vaultKey; if (!xpriv) { const signer: Signer = dbManager @@ -1282,6 +1953,9 @@ function* refillMobileKeyWorker({ payload }) { if (xpub === signerXpub) { const { xpriv } = details.xpubDetails[XpubTypes.P2WSH]; yield call(updateKeyDetailsWorker, { + originAppId: sourceAppId, + originBackupAccountSession: requestSession, + originBackupWriteIntent, payload: { signer: signer.signerXpubs[XpubTypes.P2WSH][0], key: 'xpriv', value: xpriv }, }); } @@ -1293,12 +1967,26 @@ function* refillMobileKeyWorker({ payload }) { export const refillMobileKeyWatcher = createWatcher(refillMobileKeyWorker, REFILL_MOBILEKEY); -function* refreshCanaryWalletsWorker() { +function* refreshCanaryWalletsWorker({ + originAppId, + originBackupAccountSession, + originBackupWriteIntent, +}: { + originAppId?: string; + originBackupAccountSession?: number; + originBackupWriteIntent?: BackupWriteIntent; +} = {}) { try { + const sourceAppId = originAppId || currentWalletAccountId(); + 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, + originBackupWriteIntent, payload: { wallets: canaryWallets, options: { @@ -1317,95 +2005,66 @@ export const refreshCanaryWalletsWatcher = createWatcher( REFRESH_CANARY_VAULT ); -function* mergeSimilarKeysWorker({ payload }: { payload: { signer: Signer } }) { +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 { 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( - 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++) { - yield call( - dbManager.updateObjectByPrimaryId, - RealmSchema.VaultSigner, - 'xpub', - keys[i].xpub, - { - masterFingerprint: signer.masterFingerprint, - } - ); - } - const { primarySeed, id }: KeeperApp = yield call( - dbManager.getObjectByIndex, - RealmSchema.KeeperApp - ); - const encryptionKey = generateEncryptionKey(primarySeed); - const encrytedSigner = encrypt(encryptionKey, JSON.stringify(signer)); - const updated = yield call(Relay.migrateXfp, id, [ - { - oldSignerId: s.masterFingerprint, - newSignerId: signer.masterFingerprint, - newSignerDetails: encrytedSigner, - }, - ]); - if (updated) { - console.log( - `Signer ${s.masterFingerprint} has been merged with ${signer.masterFingerprint}` - ); - } - return true; - } - } + assertWalletAccount(sourceAppId, requestSession); + 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, -}: { - payload: { - wallet: Wallet | Vault; - }; +function* generateNewExternalAddressWorker({ payload, originAppId, originBackupAccountSession, originBackupWriteIntent }: { + payload: { wallet: Wallet | Vault }; + originAppId?: string; + originBackupAccountSession?: number; + originBackupWriteIntent?: BackupWriteIntent; }) { - 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 sourceAppId = originAppId || currentWalletAccountId(); + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); + try { + assertWalletAccount(sourceAppId, requestSession); + 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; } } @@ -1486,52 +2145,23 @@ export const fetchCollaborativeChannelWatcher = createWatcher( FETCH_COLLABORATIVE_CHANNEL ); -function* updateVaultSignerXprivWorker({ signers }) { - 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( - dbManager.updateObjectByPrimaryId, - RealmSchema.VaultSigner, - 'xpub', - vaultSigner.xpub, - { - xpriv: uptXpriv, - } - ); - } - } - } - } - } +function* updateVaultSignerXprivWorker({ signers, originAppId, originBackupAccountSession, originBackupWriteIntent }) { + const sourceAppId = originAppId || currentWalletAccountId(); + const requestSession = originBackupAccountSession ?? currentBackupAccountSession(); + 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/src/store/store.ts b/src/store/store.ts index bf33c248fc..d5d5fd8e14 100644 --- a/src/store/store.ts +++ b/src/store/store.ts @@ -4,9 +4,39 @@ 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, + takeBackupRecoveryConsentDisable, + currentBackupAccountSession, + markBackupMutation, + observeBackupAccount, + setBackupReconciliationHandler, + setBackupUploadGuard, +} from 'src/services/backup/transport'; +import { + isBackupVerificationCurrent, + 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'; -import bhrReducer from './reducers/bhr'; +import bhrReducer, { + invalidateBackupRepair, + isAutomaticCloudBackupEnabled, + setPendingAllBackup, + commitBackupInspection, + setAutomaticCloudBackup, + invalidateRehydratedBackupState, + setProtectedBackupState, +} from './reducers/bhr'; import rootSaga from './sagas'; import sendAndReceiveReducer from './reducers/send_and_receive'; import settingsReducer from './reducers/settings'; @@ -70,7 +100,173 @@ 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', + 'TEST_SATS_RECIEVE', + 'AUTO_SYNC_WALLETS', + 'CONNECT_TO_NODE', + 'REFRESH_CANARY_VAULT', + 'UPDATE_WALLET_DETAILS', + 'UPDATE_VAULT_DETAILS', + 'UPDATE_RECOVERY_VISIBILITY', + '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', + 'CHECK_BACKUP_FRESHNESS', + 'REPAIR_BACKUP', + 'VALIDATE_SERVER_BACKUP', + 'UPDATE_VERSION_HISTORY', + 'MIGRATE_LABELS_329', + 'CHANGE_BITCOIN_NETWORK', + 'SETUP_KEEPER_APP', +]); +const accountOwnedUtxoUiActions = new Set([ + 'utxos/setSyncingUTXOs', + 'utxos/setSyncingUTXOError', + 'utxos/resetState', +]); +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 ( + 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 recoveryDisable = disablesConsent && takeBackupRecoveryConsentDisable(action, choiceAppId); + if ( + !choiceAppId || + (choiceAppId !== originAppId && !recoveryDisable) || + (!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; + 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]; if (__DEV__) { // const createDebugger = require('redux-flipper').default; @@ -85,5 +281,94 @@ export const store = configureStore({ }).concat(middlewars), }); +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(); + 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; + } +}); + +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 })); + // 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. +}); + +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 { + 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); + store.dispatch(invalidateBackupRepair(appId)); + store.dispatch(setPendingAllBackup({ appId, pending: true })); +}); + sagaMiddleware.run(rootSaga); export const persistor = persistStore(store); 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/src/utils/helpAiChatBehavior.ts b/src/utils/helpAiChatBehavior.ts new file mode 100644 index 0000000000..caa67bf999 --- /dev/null +++ b/src/utils/helpAiChatBehavior.ts @@ -0,0 +1,33 @@ +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 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/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..bacaebd5a3 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,19 +81,12 @@ class Configuration { public ENVIRONMENT: string; - public CHANNEL_URL: string = config.CHANNEL_URL?.trim() - ? 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 CHANNEL_URL: string = resolveChannelURL(config.CHANNEL_URL); 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/src/utils/service-utilities/utils.ts b/src/utils/service-utilities/utils.ts index b000165599..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]+$/, '') @@ -713,43 +710,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 @@ -815,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/src/utils/utilities.ts b/src/utils/utilities.ts index 67d4a549df..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) { @@ -714,12 +740,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 +913,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 +978,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/channel/ChannelRequestScanner.test.tsx b/tests/channel/ChannelRequestScanner.test.tsx new file mode 100644 index 0000000000..73c0441ad9 --- /dev/null +++ b/tests/channel/ChannelRequestScanner.test.tsx @@ -0,0 +1,77 @@ +import React from 'react'; +import renderer, { act } from 'react-test-renderer'; +import ChannelRequestScanner from '../../src/services/channel/ChannelRequestScanner'; + +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 () => { + 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..dcdbf5f840 --- /dev/null +++ b/tests/channel/crypto.test.ts @@ -0,0 +1,61 @@ +import { + createCipherGcm, + createDecipherGcm, + InvalidChannelQRCodeError, +} 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( + InvalidChannelQRCodeError + ); + } + ); + + 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" + } + } +} 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/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/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); + } + ); +}); diff --git a/tests/helpAiChatBehavior.test.ts b/tests/helpAiChatBehavior.test.ts new file mode 100644 index 0000000000..20712e7b82 --- /dev/null +++ b/tests/helpAiChatBehavior.test.ts @@ -0,0 +1,61 @@ +import { + canSendChatMessage, + findRetryableChatMessage, + isNearChatBottom, + planChatSend, + removeFailedChatMessage, +} from '../src/utils/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(); +}); + +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); +}); diff --git a/tests/hooks/walletCollectionMemo.test.tsx b/tests/hooks/walletCollectionMemo.test.tsx new file mode 100644 index 0000000000..d66a3c4bfa --- /dev/null +++ b/tests/hooks/walletCollectionMemo.test.tsx @@ -0,0 +1,169 @@ +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()); + }); + + 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()); + }); +}); 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); + } +}); diff --git a/tests/recovery/accountBinding.test.cjs b/tests/recovery/accountBinding.test.cjs new file mode 100644 index 0000000000..befac795e8 --- /dev/null +++ b/tests/recovery/accountBinding.test.cjs @@ -0,0 +1,610 @@ +const { test } = require('node:test'); +const assert = require('node:assert/strict'); +const { fixture, wallet, signer, enums, loadModule, functions } = require('./sagaHarness.cjs'); +const { harness, loadModule: loadTransportModule } = require('./helpers.cjs'); +const vm = require('node:vm'); +const ts = require('typescript'); + +function switchToOptedInB(f) { + f.app.id = 'B'; + f.app.primarySeed = 'cd'.repeat(32); + f.state.storage.appId = 'B'; + 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; + f.scope.Relay.updateVaultImage = async (body) => { + 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 } }, + }); + 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; + 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('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.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); +}); + +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', () => { + 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: consentTransport.observeBackupConsentAction, + takeBackupRecoveryConsentDisable: consentTransport.takeBackupRecoveryConsentDisable, + captureBackupWriteIntent: (appId) => appId === selectedAppId ? { + appId, accountSession: session, baseRevision: 'a'.repeat(64), baselineEpoch: 1, + } : undefined, + }; + 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, 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: {} }); + 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, + 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' }); + assert.equal(seen.at(-1).originBackupAccountSession, 3); +}); + +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)); + 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); + 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]); + 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 () => { + 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, (error) => error.category === 'protocol'); + 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, (error) => error.category === 'protocol'); + assert.equal(remote.wallets.shared, 'newer-A-ciphertext'); + assert.equal(calls.length, 0, 'stale ciphertext must never reach Relay'); +}); + +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; + 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'; + gate.release(); + assert.equal((await updating).updated, false); + assert.equal(f.remote.wallets.shared, 'newer-A-ciphertext'); + 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('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; + 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++; + gate.release(); + 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; + 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, + committedOperation, + payload: { wallets: [saved] }, + }); + 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('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.P2WSH] = [{ xpub: 'vault-xpub', xpriv: 'old-secret' }]; + 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++; + 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', () => { + 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', 'revisionedBackupMutation', '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 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)); + f.scope.Relay.deleteAppImageEntity = async () => ({ updated: false, error: 'rejected' }); + const result = await f.run('deleteAppImageEntityWorker', { + originAppId: id, + payload: { walletIds: ['keep'] }, + }); + assert.equal(result.updated, true); + assert.deepEqual(f.collections.Wallet, []); + assert.equal(f.state.bhr.pendingAllBackupByAppId[id], true); +}); + +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)); + 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('failed local vault deletion reports failure and pending repair without remote writes', async () => { + const f = fixture(); + 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')); + 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'))); +}); + +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); +}); diff --git a/tests/recovery/accountSessionDestructive.test.cjs b/tests/recovery/accountSessionDestructive.test.cjs new file mode 100644 index 0000000000..b2f097ccb7 --- /dev/null +++ b/tests/recovery/accountSessionDestructive.test.cjs @@ -0,0 +1,303 @@ +const assert = require('node:assert/strict'); +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 reach its intended pause'); +} + +function accountRoundTrip(f, originalAppId, advanceSession) { + f.app.id = 'other-disposable-account'; + f.state.storage.appId = f.app.id; + advanceSession(); + f.app.id = originalAppId; + f.state.storage.appId = originalAppId; + advanceSession(); +} + +function owner(f) { + const appId = f.app.id; + let session = 1; + f.scope.currentBackupAccountSession = () => session; + return { + appId, + session: () => session, + roundTrip: () => + accountRoundTrip(f, appId, () => { + 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] }, + }; + active.roundTrip(); + f.remote.wallets[walletId] = 'newer-A-ciphertext'; + + 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] + ); +}); + +test('old committed vault deletion receipt cannot delete newer A after returning to A', async () => { + const f = fixture(); + const active = owner(f); + const vaultId = 'disposable-vault'; + 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 result = await f.run('deleteVaultImageWorker', { + originAppId: active.appId, + originBackupAccountSession: originalSession, + committedOperation: receipt, + payload: { vaultIds: [vaultId] }, + }); + 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 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 beginDelete = f.scope.beginBackupDeleteOperation; + let resumePreflight; + f.scope.beginBackupDeleteOperation = async (...args) => { + const receipt = await beginDelete(...args); + return new Promise((resolve) => { + resumePreflight = () => resolve(receipt); + }); + }; + + const deleting = f.run('deleteBackupWorker', { + appId: active.appId, + originBackupAccountSession: active.session(), + }); + await waitForPause(() => resumePreflight); + active.roundTrip(); + f.remote.wallets.shared = 'newer-A-ciphertext'; + resumePreflight(); + + assert.equal(await deleting, false); + assert.deepEqual(f.calls, []); + assert.equal(f.remote.wallets.shared, 'newer-A-ciphertext'); + assertConsentRetained(f, active.appId); +}); + +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 } }; + }, + }, + }); + 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, (error) => error.category === 'protocol'); + assert.deepEqual(requests, []); + assert.equal(remote.wallets.shared, 'newer-A-ciphertext'); +}); + +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 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 = () => { + f.calls.push(['delete-backup']); + f.remote.wallets = {}; + return new Promise((resolve) => { + finishDelete = resolve; + }); + }; + const deleting = f.run('deleteBackupWorker', { + appId: active.appId, + originBackupAccountSession: active.session(), + }); + await waitForPause(() => finishDelete); + active.roundTrip(); + f.remote.wallets.shared = 'newer-A-ciphertext'; + finishDelete({ updated: true }); + + assert.equal(await deleting, false); + assert.equal(f.remote.wallets.shared, 'newer-A-ciphertext'); + assert.equal(f.state.bhr.pendingAllBackupByAppId[active.appId], true); + assertConsentRetained(f, active.appId); +}); + +test('wallet delete acknowledgement after A to B to A cannot report a new-session deletion', async () => { + const f = fixture(); + const active = owner(f); + const walletId = 'disposable-wallet'; + f.collections.Wallet.push(wallet(walletId, enums.NetworkType.MAINNET)); + let finishDelete; + 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); + 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, 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); +}); 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'); +}); 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/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/automaticRecoveryRestore.integration.cjs b/tests/recovery/automaticRecoveryRestore.integration.cjs new file mode 100644 index 0000000000..c55317b9ab --- /dev/null +++ b/tests/recovery/automaticRecoveryRestore.integration.cjs @@ -0,0 +1,210 @@ +// 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'); +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', 'latestRecoveryReadRequest', + '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' }, + ...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; } }, + }); + 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, + 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, ''); + 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/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' + ); +}); 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/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); +}); diff --git a/tests/recovery/automaticWalletProducer.integration.cjs b/tests/recovery/automaticWalletProducer.integration.cjs new file mode 100644 index 0000000000..444ae047ad --- /dev/null +++ b/tests/recovery/automaticWalletProducer.integration.cjs @@ -0,0 +1,287 @@ +// 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, 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); +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', + 'deleteAppImageEntityWorker', 'deleteVaultImageWorker', + ], { + ...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', '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(), + 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('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; + 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); + }); +} + +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: { + 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); +}); diff --git a/tests/recovery/automaticWriteRealmRelay.fixture.cjs b/tests/recovery/automaticWriteRealmRelay.fixture.cjs new file mode 100644 index 0000000000..e7c6ae9590 --- /dev/null +++ b/tests/recovery/automaticWriteRealmRelay.fixture.cjs @@ -0,0 +1,369 @@ +// 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 { 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'); + +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 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); + 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, 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; + return vm.runInNewContext(code, globals, { filename: 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()); + 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(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('.')) { + 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 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'); } }, + // 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': { ...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, + }; + 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 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'); + 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, + ...(network ? { network: network.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, + middleware: (defaults) => defaults({ serializableCheck: false, immutableCheck: false }).concat(() => (next) => (action) => { + beforeDispatch?.(action); + return next(action); + }, middlewareScope.bindActionOrigin, () => (next) => (action) => { + actions.push(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)); + 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(); + 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); + 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|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, + ...load('src/services/backup/protected'), ...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'), + ...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(), + 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, network, utilities: utilitiesScope, + 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() { persistor?.pause(); realmDatabase.closeDatabase(); fs.rmSync(directory, { recursive: true, force: true }); }, + }; +} + +module.exports = { createRealmClient, declarations, componentDeclarations, 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..04c2d8b5e8 --- /dev/null +++ b/tests/recovery/automaticWriteRealmRelay.integration.cjs @@ -0,0 +1,1192 @@ +// 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, 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'); +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); +} + +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); + 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('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'); + 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 availability 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_HOSTED_AVAILABLE, true, 'App availability does not certify hosted backend readiness'); + }); +} + +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); +}); + +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); +}); 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/backupFeedback.test.cjs b/tests/recovery/backupFeedback.test.cjs new file mode 100644 index 0000000000..2c824c71fa --- /dev/null +++ b/tests/recovery/backupFeedback.test.cjs @@ -0,0 +1,84 @@ +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', + 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'); + 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); + 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}), + 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])); + }); +} + + +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' }), + 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'); +}); diff --git a/tests/recovery/backupFreshness.test.cjs b/tests/recovery/backupFreshness.test.cjs new file mode 100644 index 0000000000..d59d6ea0b9 --- /dev/null +++ b/tests/recovery/backupFreshness.test.cjs @@ -0,0 +1,454 @@ +const { test } = require('node:test'); +const assert = require('node:assert/strict'); +const vm = require('node:vm'); +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)); + 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 } }); + f.state.bhr.automaticCloudBackupByAppId[f.app.id] = true; + f.state.bhr.pendingAllBackupByAppId[f.app.id] = 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.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); + 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 }, requestId: 'failed-pull' }, + }), 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'))); + 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 () => { + 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 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('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('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('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; }); + 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 }, + }); + 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.state.bhr.pendingAllBackupByAppId[f.app.id] = 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(); + const initial = wallet('initial', NetworkType.MAINNET); + 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 } }], + }), + 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', + selection: { source: 'legacy-older', olderCopyConfirmed: true } } }); + 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('recovery upgrade does not bypass per-account backup consent', async () => { + const f = fixture({ bhr: { automaticCloudBackup: false } }); + const seed = f.scope.bip39.mnemonicToSeedSync().toString('hex'); + const key = encryption.generateEncryptionKey(seed); + f.remote.version = '2.5.15'; + f.remote.wallets.disposable = encryption.encrypt( + key, + JSON.stringify(wallet('disposable', NetworkType.MAINNET)) + ); + + 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); + assert.equal(f.collections.VersionHistory.filter((record) => record.title === 'Recovered Wallet').length, 1); +}); + +test('relay rejection preserves committed wallet creation and leaves backup pending', 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, 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.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]) { + 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], enabled ? false : 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/backupRehydration.test.cjs b/tests/recovery/backupRehydration.test.cjs new file mode 100644 index 0000000000..52fd0bb1e1 --- /dev/null +++ b/tests/recovery/backupRehydration.test.cjs @@ -0,0 +1,220 @@ +// 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 { takeBackupRecoveryConsentDisable } = loadModule('src/services/backup/transport.ts', { + '../rest/RestClient': {}, + }); + 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, + takeBackupRecoveryConsentDisable, + 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); +}); 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/deleteResultTruthfulness.test.cjs b/tests/recovery/deleteResultTruthfulness.test.cjs new file mode 100644 index 0000000000..8a2dca1cbb --- /dev/null +++ b/tests/recovery/deleteResultTruthfulness.test.cjs @@ -0,0 +1,329 @@ +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 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' }); + const completed = []; + + const result = await f.run('deleteAppImageEntityRequestWorker', { + originAppId: f.app.id, + payload: { walletIds: ['keep'] }, + onComplete: (deleted) => completed.push(deleted), + }); + + 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 () => { + 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('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, []); + assert.ok(!f.actions.some((action) => + ['setRelayVaultUpdateLoading', 'relayVaultUpdateSuccess', 'relayVaultUpdateFail'] + .includes(action.type))); +}); + +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.Relay.deleteVaults = async () => ({ 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, 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))); +}); + +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 () => { + 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, 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 () => { + 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; +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 ${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, consent: true, verified: true, + 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 }, + dbManager: { + getObjectByIndex: () => ({ id: state.active }), + getObjectById: (_schema, id) => + (state.presentById.has(id) ? state.presentById.get(id) : state.present) + ? { id } + : undefined, + }, + 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, + 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 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(); + 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 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, []); +}); 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); +}); diff --git a/tests/recovery/faucetVisibilityAcceptance.test.cjs b/tests/recovery/faucetVisibilityAcceptance.test.cjs new file mode 100644 index 0000000000..0359ea76f7 --- /dev/null +++ b/tests/recovery/faucetVisibilityAcceptance.test.cjs @@ -0,0 +1,170 @@ +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')); +}); + +test('Receive Test Sats dispatch keeps its original account when queued across a switch', () => { + 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); + 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/fingerprintTransport.test.cjs b/tests/recovery/fingerprintTransport.test.cjs new file mode 100644 index 0000000000..f5f68241c4 --- /dev/null +++ b/tests/recovery/fingerprintTransport.test.cjs @@ -0,0 +1,119 @@ +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', +}]; +const intent = (appId, accountSession = 0) => ({ + appId, accountSession, baseRevision: 'a'.repeat(64), baselineEpoch: 0, +}); + +function relay(transport, errors = []) { + return loadModule('src/services/backend/Relay.ts', { + '../backup/transport': transport, + '../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/' }, + 'react-native': { Platform: { OS: 'fixture' } }, + }).default; +} + +for (const repair of [false, true]) { + 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()); + 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.run(repair); + await started; + const previousRevision = f.transport.backupRevision(f.app.id); + await assert.rejects( + 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); + assert.deepEqual(reconciliation, [f.app.id]); + release(); + 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('direct migration attempts never send ciphertext and keep redacted errors', async () => { + const errors = []; + const transport = loadModule('src/services/backup/transport.ts', { + '../rest/RestClient': { post: () => assert.fail('Migration reached Relay') }, + }); + transport.setBackupUploadGuard((appId) => appId === 'fixture-account'); + 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, intent('fixture-account')), + /Failed to do migrate the xfp/); + } + assert.equal(errors.length, 2); + assert.equal(errors[0].category, 'protocol'); + assert.equal(errors[0].message, 'Backup request could not be completed'); + assert.equal(transport.backupRevision('fixture-account'), 2); + assert.deepEqual(pending, ['fixture-account', 'fixture-account']); +}); + +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] = [{ + 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); + oldSigner.id = oldKey; + 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); + 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); + assert.deepEqual(Object.keys(f.remote.signers), [newKey]); +}); diff --git a/tests/recovery/freshInstallWallet.test.cjs b/tests/recovery/freshInstallWallet.test.cjs new file mode 100644 index 0000000000..745d43e4cf --- /dev/null +++ b/tests/recovery/freshInstallWallet.test.cjs @@ -0,0 +1,222 @@ +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 === 'Wallet' && options.rejectWalletWrite) || + (schema === 'Signer' && rejectKeyWrite)) return false; + 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/helpers.cjs b/tests/recovery/helpers.cjs new file mode 100644 index 0000000000..94ba698243 --- /dev/null +++ b/tests/recovery/helpers.cjs @@ -0,0 +1,257 @@ +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: {} }); +// 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', + 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 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': 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, + './protected': unitProtectedFacade(transport, app), + './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: [], + }, +}); +module.exports = { loadModule, encryption, image, makeImage, harness, wallet, unitProtectedFacade }; diff --git a/tests/recovery/keyRecovery.test.cjs b/tests/recovery/keyRecovery.test.cjs new file mode 100644 index 0000000000..198edffd46 --- /dev/null +++ b/tests/recovery/keyRecovery.test.cjs @@ -0,0 +1,329 @@ +// 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] } }); + f.mockVerifiedBackupBaseline(); + 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, true, + 'an acknowledged unit adapter write is not complete server readback proof'); + }); + } +} + +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)] }, + }); + } + 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); + 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: [storedWallet], signers: [key], updateNodes: true }, committedOperation, + }); + 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); + 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)] } }); + 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' }], + })) { + f.mockVerifiedBackupBaseline(); + 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]) { + f.mockVerifiedBackupBaseline(); + 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); + 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.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('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'); + 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.VaultSigner.push(key); + f.collections.Vault.push(...['vault-a', 'vault-b'].map((id) => ({ id, networkType: NetworkType.MAINNET, + signers: [key], scheme: { m: 1, n: 1 } }))); + const update = f.scope.dbManager.updateObjectByPrimaryId; + f.scope.dbManager.updateObjectByPrimaryId = (_schema, _field, _id, patch) => { + if (options.writeFails) return false; + return update(_schema, _field, _id, patch); + }; + 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.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 () => { + 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 changes 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/labelAccount.test.cjs b/tests/recovery/labelAccount.test.cjs new file mode 100644 index 0000000000..2cb863aaff --- /dev/null +++ b/tests/recovery/labelAccount.test.cjs @@ -0,0 +1,294 @@ +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 { encryption } = require('./sagaHarness.cjs'); + +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', 'backupCommittedLabels', 'transactionLabelsWorker', + '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, 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: 'ab'.repeat(32) }), + 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: (schema, id) => { + if (schema === 'Tags') return state.tags.find(tag => tag.id === id); + if (switchDuringWalletRead) state.active = 'B'; + return walletRecord; + }, + getCollection: (schema) => schema === 'Tags' ? state.tags : [], + updateObjectById: () => { + state.saved.push({ appId: state.active, walletUpdated: true }); + return true; + }, + }; + const scope = { + call, delay, put, dbManager, console, + 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' }, + EntityKind: { VAULT: 'VAULT' }, + generateAbbreviatedOutputDescriptors: () => { + if (switchDuringDescriptor) state.networkType = 'TESTNET'; + return 'disposable-origin'; + }, + ...encryption, + getJSONFromRealmObject: (value) => structuredClone(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', metadata = {}) => runSaga( + { dispatch: (action) => state.uiActions.push({ ...action, active: state.active }), + getState: () => ({}) }, scope[name], { payload, originAppId, ...metadata } + ).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('network switch before scoped label persistence writes neither Realm nor relay', async () => { + const f = fixture({ switchDuringDescriptor: true }); + await f.run('bulkUpdateLabelsWorker', { + labelChanges: { added: [{ name: 'note', isSystem: false }], deleted: [] }, + UTXO: { txId: 'tx', vout: 0 }, + wallet: { ...wallet, networkType: 'MAINNET' }, + scope: { appId: 'A', networkType: 'MAINNET' }, + }); + assert.deepEqual(f.state.saved, []); + 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, []); +}); + +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/localMutationInvalidation.test.cjs b/tests/recovery/localMutationInvalidation.test.cjs new file mode 100644 index 0000000000..18c7b9ab9c --- /dev/null +++ b/tests/recovery/localMutationInvalidation.test.cjs @@ -0,0 +1,165 @@ +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; + } +}); + +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); +}); diff --git a/tests/recovery/loginOriginSession.test.cjs b/tests/recovery/loginOriginSession.test.cjs new file mode 100644 index 0000000000..424469c63a --- /dev/null +++ b/tests/recovery/loginOriginSession.test.cjs @@ -0,0 +1,208 @@ +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, 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: persistedAppId, appVersion, defaultWalletCreated: {} }, + account: { + allAccounts, + 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: () => 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'), + applyUpgradeSequence: (args) => { + upgradeCalls.push(args); + throw Error('fixture stop'); + }, + console: { log() {} }, + }; + for (const name of [ + 'credsAuthenticatedError', + 'setRecepitVerificationError', + 'setRecepitVerificationFailed', + 'setupLoading', + 'setKey', + 'setPinHash', + 'migrateLegacyAutomaticCloudBackup', + 'setAppId', + 'setTempDetails', + '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); +}); + +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); +}); diff --git a/tests/recovery/manualCoinChoices.test.cjs b/tests/recovery/manualCoinChoices.test.cjs new file mode 100644 index 0000000000..c4effc46f6 --- /dev/null +++ b/tests/recovery/manualCoinChoices.test.cjs @@ -0,0 +1,177 @@ +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 }; + }; + // 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', + ]); + 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 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), 'conflict'); + const restored = JSON.parse(encryption.decrypt(encryption.generateEncryptionKey(f.app.primarySeed), f.remote.wallets['manual-choice'])); + 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 () => { + 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', { 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'); + 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', { 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.pendingAllBackupByAppId[f.app.id], 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', { 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', { 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); +}); + +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.WALLET_SYNC_SCOPE_CHANGED = 'WALLET_SYNC_SCOPE_CHANGED'; + f.scope.ElectrumClient = { + getConnectionGeneration: () => 1, + assertConnectionGeneration: (_generation, networkType) => { + assert.equal(networkType, enums.NetworkType.MAINNET); + }, + resetCurrentPeerIndex: () => { + throw new Error('Current-network refresh should not reconnect'); + }, + }; + 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', { + originAppId: f.app.id, + 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/nodeAccountActions.test.cjs b/tests/recovery/nodeAccountActions.test.cjs new file mode 100644 index 0000000000..b21bf1c16c --- /dev/null +++ b/tests/recovery/nodeAccountActions.test.cjs @@ -0,0 +1,134 @@ +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/screens/AppSettings/Node/NodeSettings.tsx'; +const source = ts.createSourceFile( + file, + fs.readFileSync(path.join(__dirname, '../..', file), 'utf8'), + ts.ScriptTarget.Latest, + true, + ts.ScriptKind.TSX +); +const component = source.statements.find( + (statement) => ts.isFunctionDeclaration(statement) && statement.name?.text === 'NodeSettings' +); +const names = new Set(['onDelete', 'onDisconnectToNode']); +const handlers = component.body.statements + .filter( + (statement) => + ts.isVariableStatement(statement) && + statement.declarationList.declarations.some((declaration) => + names.has(declaration.name.getText(source)) + ) + ) + .map((statement) => statement.getText(source)) + .join('\n'); +const code = ts.transpileModule( + `${handlers}\nglobalThis.handlers = { onDelete, onDisconnectToNode };`, + { compilerOptions: { target: ts.ScriptTarget.ES2020, jsx: ts.JsxEmit.React } } +).outputText; + +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; + return true; + }, + update: () => { state.updated += 1; }, + getAllNodes: () => [], + }, + setLoading: () => {}, + setNodeList: () => {}, + dispatch: () => { state.dispatched += 1; }, + showToast: () => {}, + console, + }; + vm.runInNewContext(code, scope); + return { state, handlers: scope.handlers }; +} + +test('delayed node confirmation from A cannot act on B', async () => { + const f = fixture(); + f.state.activeAppId = 'B'; + const selected = { id: 7, host: 'a.example', port: 50002 }; + + await f.handlers.onDisconnectToNode(selected, 'A'); + await f.handlers.onDelete(selected, 'A'); + + 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('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'); + + assert.equal(f.state.disconnected, 1); + assert.equal(f.state.updated, 0); + assert.equal(f.state.dispatched, 0); +}); diff --git a/tests/recovery/nodeReplacement.test.cjs b/tests/recovery/nodeReplacement.test.cjs new file mode 100644 index 0000000000..f5fc5a1de0 --- /dev/null +++ b/tests/recovery/nodeReplacement.test.cjs @@ -0,0 +1,45 @@ +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(); + 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), []); +}); + +test('unrelated wallet update never requests node replacement', async () => { + const f = fixture(); + 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 }; + 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); +}); 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/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/recovery/protectedWriteRealmRelay.fixture.cjs b/tests/recovery/protectedWriteRealmRelay.fixture.cjs new file mode 100644 index 0000000000..df58c2a617 --- /dev/null +++ b/tests/recovery/protectedWriteRealmRelay.fixture.cjs @@ -0,0 +1,209 @@ +// 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); + 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 */ } + 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..3a4dfdbace --- /dev/null +++ b/tests/recovery/protectedWriteRealmRelay.proof.cjs @@ -0,0 +1,803 @@ +// 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, { 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('')); + 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 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 } }); + 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); + // 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); +}); + +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 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 { + 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 => { + 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); +}); + +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'); + } +}); 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 new file mode 100644 index 0000000000..c595268a0c --- /dev/null +++ b/tests/recovery/realmOpenGeneration.test.cjs @@ -0,0 +1,123 @@ +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) }, + // 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: {} }, + }, + { 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']); +}); diff --git a/tests/recovery/recoveryAccountSession.test.cjs b/tests/recovery/recoveryAccountSession.test.cjs new file mode 100644 index 0000000000..7539d013b6 --- /dev/null +++ b/tests/recovery/recoveryAccountSession.test.cjs @@ -0,0 +1,131 @@ +// 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', + selection: { source: 'legacy-older', olderCopyConfirmed: true } } }); + + 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)); +}); diff --git a/tests/recovery/recoveryErrorFeedback.test.cjs b/tests/recovery/recoveryErrorFeedback.test.cjs new file mode 100644 index 0000000000..72bbb0b276 --- /dev/null +++ b/tests/recovery/recoveryErrorFeedback.test.cjs @@ -0,0 +1,255 @@ +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[0]?.getText(source).includes('if (appImageError && isRecovery)')) + 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, 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', + 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], /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: '' }]); + }); +} + +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, []); +}); + + +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 new file mode 100644 index 0000000000..14a58fbb2b --- /dev/null +++ b/tests/recovery/repair.test.cjs @@ -0,0 +1,488 @@ +const { test } = require('node:test'); +const assert = require('node:assert/strict'); +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()); + 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']); +}); + +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')); + 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', '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')); +}); + +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', publicId: 'fixture', version: '2.5.16', + subscription: JSON.stringify({ level: 1 }), wallets: {}, signers: {}, nodes: [], vaults: [], labels: [] }, + allVaultImages: [], labels: [], revision: 'a'.repeat(64), revisionedMutations: true, + } }; + } }, + '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']); + } +}); + +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']; }], + ['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), + revisionedMutations: true, + 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), + revisionedMutations: true, + 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('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), 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']]); + + 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), 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']]); + + 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'; + 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('incremental attempt during read-only check fails closed and invalidates completion', async () => { + let f, changed = false, write; + f = harness({ post: async ({ path }) => { + if (path.endsWith('getBackupSnapshot') && !changed) { + changed = true; + write = f.transport.backupPost('fixture/updateAppImage', { appId: f.app.id }) + .catch((error) => error); + } + } }); + f.local.Wallet.push(wallet()); + 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-request', 'non-transport']]); +}); + +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('multiple incremental attempts during inspection remain local and invalidate the check', async () => { + let f; + const writes = []; + f = harness({ post: async ({ path }) => { + 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'); + 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 v2 repair acknowledgement remains unverified after readback', async () => { + const f = harness({ lostResponse: true }); f.local.Wallet.push(wallet()); + 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('an old relay returns 404 for v2 repair and cannot acknowledge it', async () => { + const f = harness({ post: async ({ path, payload }) => { + 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), '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 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), '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 () => { + 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 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.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 () => { + 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('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,[]); +}); + + +test('server change after read rejects stale upload and preserves remote data', async () => { + const f = harness({ post: async ({ path, remote }) => { + if (path.endsWith('revisionedBackupMutation')) 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('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'); + assert.equal(archived,true); +}); diff --git a/tests/recovery/repairAccountSession.test.cjs b/tests/recovery/repairAccountSession.test.cjs new file mode 100644 index 0000000000..101581c5c9 --- /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 === 'revisionedBackupMutation').length, 1); + assert.equal(f.calls.filter((path) => path === 'getBackupSnapshot').length, 3); +}); diff --git a/tests/recovery/restoreIntegrity.test.cjs b/tests/recovery/restoreIntegrity.test.cjs new file mode 100644 index 0000000000..e326292a07 --- /dev/null +++ b/tests/recovery/restoreIntegrity.test.cjs @@ -0,0 +1,254 @@ +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' }, + accountStatus: { + address: 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t', + gasFreeAddress: 'TXYZopYRdj2D9XRtbG411XZZ3kM5VkAeBf', + }, + }; + 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', + selection: { source: 'legacy-older', olderCopyConfirmed: true } } }), + }; +} +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(); + 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 [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(); + 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', + selection: { source: 'legacy-older', olderCopyConfirmed: true } } }); + 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', + selection: { source: 'legacy-older', olderCopyConfirmed: true } } }); + 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/revisionedMutation.test.cjs b/tests/recovery/revisionedMutation.test.cjs new file mode 100644 index 0000000000..c2123c42df --- /dev/null +++ b/tests/recovery/revisionedMutation.test.cjs @@ -0,0 +1,101 @@ +const { test } = require('node:test'); +const assert = require('node:assert/strict'); +const { loadModule } = require('./helpers.cjs'); + +function fixture() { + const calls = []; + const reconciliations = []; + const transport = loadModule('src/services/backup/transport.ts', { + '../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)); + transport.observeBackupAccount('A'); + return { transport, calls, reconciliations }; +} + +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) { + const body = operation === 'updateVaultImage' ? { appID: 'A' } : { appId: 'A' }; + await assert.rejects( + f.transport.backupPost(`fixture/${operation}`, body, operation === 'deleteBackup'), + (error) => error.category === 'protocol' + ); + } + assert.deepEqual(f.calls, []); + assert.deepEqual(f.reconciliations, operations.map(() => 'A')); + assert.equal(f.transport.backupRevision('A'), operations.length); +}); + +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('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, + { 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, + { 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'); + f.transport.observeBackupAccount('A'); + assert.equal(f.transport.captureBackupWriteIntent('A'), undefined); + assert.deepEqual(f.calls, []); +}); + +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, oldIntent), + /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('the old full writer remains disabled', async () => { + const f = fixture(); + await assert.rejects( + f.transport.backupPost('fixture/backupAllSignersAndVaults', { appId: 'A' }), + (error) => error.category === 'protocol' + ); + assert.deepEqual(f.calls, []); + assert.deepEqual(f.reconciliations, ['A']); +}); diff --git a/tests/recovery/sagaHarness.cjs b/tests/recovery/sagaHarness.cjs new file mode 100644 index 0000000000..b5e58f3094 --- /dev/null +++ b/tests/recovery/sagaHarness.cjs @@ -0,0 +1,631 @@ +// 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, 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', { + '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 = { + storage: { appId }, + bhr: { + automaticCloudBackup: false, + automaticCloudBackupByAppId: { + [appId]: options.bhr?.automaticCloudBackup !== false, + ...options.bhr?.automaticCloudBackupByAppId, + }, + pendingAllBackup: false, + pendingAllBackupByAppId: {}, + backupRepairCompletedByAppId: { [appId]: true }, + ...options.bhr, + }, + settings: { bitcoinNetworkType: NetworkType.MAINNET, bitcoinNetwork: NetworkType.MAINNET }, + notifications: { fcmToken: 'disposable-token' }, + 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: [], + VaultSigner: [], + Tags: [], + NodeConnect: [], + UAI: [], + VersionHistory: [], + BackupHistory: [], + }; + const remote = { appId, wallets: {}, signers: {}, nodes: [], version: '2.5.16' }; + const calls = []; + const actions = []; + const scope = { + ...effects, + markBackupMutation: () => {}, + 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'), + // 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?.toJSON ? object.toJSON() : JSON.parse(JSON.stringify(object)), + RealmSchema: new Proxy({}, { get: (_, name) => name }), + dbManager: { + 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) => 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 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; + } + 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; + }, + deleteObjectByPrimaryKey: (schema, key, value) => { + collections[schema] = collections[schema].filter((row) => String(row[key]) !== String(value)); + return true; + }, + updateObjectById: (schema, id, patch) => { + 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) => { + 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: { + 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' }, + SigningServer: { updatePolicy: async () => ({ updated: true }) }, + Alert: { alert: () => {} }, + 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: () => {}, + refreshWalletsWorker: async () => { + calls.push(['sync']); + if (options.syncGate) await options.syncGate; + if (options.switchAppDuringSync) state.storage.appId = 'another-app'; + 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(); + 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; + }; + 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, markRevision = true) => { + // Production automatic.ts notifies reconciliation after a failed local + // commit. This adapter models that state boundary without claiming proof. + 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()) + 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', + 'setBackupAllSuccess', + 'setBackupAllFailure', + 'setBackupAllLoading', + 'setDeleteBackupSuccess', + 'setDeleteBackupFailure', + 'setPendingAllBackup', + 'setAutomaticCloudBackup', + 'setHomeToastMessage', + 'setRelayWalletUpdateLoading', + 'relayWalletUpdateSuccess', + 'relayWalletUpdateFail', + 'setRelaySignersUpdateLoading', + 'relaySignersUpdateSuccess', + 'relaySignersUpdateFail', + 'hideDeletingKeyModal', + 'showDeletingKeyModal', + 'showKeyDeletedSuccessModal', + 'setSignerPolicyError', + 'updateDelayedPolicyUpdate', + 'setRelayVaultUpdateLoading', + 'relayVaultUpdateSuccess', + 'relayVaultUpdateFail', + 'setAppImageError', + 'setRecoveryDiscovery', + 'setProtectedBackupState', + 'setSeedConfirmed', + 'setBackupType', + 'uaiActioned', + 'setAppId', + 'setAppCreated', + 'autoSyncWallets', + 'checkBackupFreshness', + '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', [ + 'currentBackupAppId', + 'buildAutomaticAppMutation', 'buildAutomaticVaultMutation', 'queueCommittedBackupWorker', + 'flagUncertainBackupOutcome', + 'deleteAppImageEntityWorker', + 'deleteAppImageEntityRequestWorker', + 'deleteVaultImageWorker', + 'deleteBackupWorker', + 'backupAllSignersAndVaultsWorker', + 'checkBackupCondition', + 'setServerBackupFailed', + 'updateAppImageWorker', + 'updateVaultImageWorker', + 'recoverApp', + 'writeRecoveredObject', + 'getAppImageWorker', + 'latestRecoveryReadRequest', + ]) + + '\n' + + functions('src/store/sagas/wallets.ts', [ + 'currentWalletAccountId', 'assertWalletAccount', + 'rewriteSignerFingerprintReferences', 'planSignerIdentityMerges', 'applySignerIdentityMerges', + 'applySignerXprivUpdates', 'buildSignerMigrationMutation', + 'addNewWalletsWorker', 'addSigningDeviceWorker', 'mergeSimilarKeysWorker', + 'deleteSigningDeviceWorker', 'deleteVaultWorker', + 'deleteVaultRequestWorker', + 'generateNewExternalAddressWorker', 'updateSignerPolicyWorker', + 'updateVaultSignerXprivWorker', 'updateSignerDetailsWorker', + 'updateKeyDetailsWorker', 'autoWalletsSyncWorker', + ]); + vm.runInNewContext( + ts.transpileModule(code, { compilerOptions: { target: ts.ScriptTarget.ES2020 } }).outputText, + 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); + 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 === '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') + 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', 'mergeSimilarKeysWorker', '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, + }, + scope[name], + ...args + ).toPromise(); + }; + 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 }; diff --git a/tests/recovery/sagaState.test.cjs b/tests/recovery/sagaState.test.cjs new file mode 100644 index 0000000000..c791631dbf --- /dev/null +++ b/tests/recovery/sagaState.test.cjs @@ -0,0 +1,177 @@ +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 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 => + (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} }); + 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; + }, + translations: require('../../src/context/Localization/language/en.json'), + RealmSchema:{KeeperApp:'KeeperApp',UAI:'UAI'}, + dbManager:{ getObjectByIndex:()=>f.app, getObjectByField:()=>[] }, + currentBackupAppId:()=>f.app.id, + 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(baseComparison + '\n' + code,{ compilerOptions:{ target:ts.ScriptTarget.ES2020 }}).outputText,scope); + return { + start(repair) { + const sagaTask = saga.runSaga({getState,dispatch},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('revisionedBackupMutation')) { 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('revisionedBackupMutation')) { 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 === 'revisionedBackupMutation').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); + 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.pendingAllBackupByAppId.disposable,false); + 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 () => { + 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.pendingAllBackupByAppId.disposable,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); +}); + +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); +}); diff --git a/tests/recovery/sendLabelOrigin.test.cjs b/tests/recovery/sendLabelOrigin.test.cjs new file mode 100644 index 0000000000..9f458415ca --- /dev/null +++ b/tests/recovery/sendLabelOrigin.test.cjs @@ -0,0 +1,180 @@ +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, put, select } = require('redux-saga/effects'); + +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' }, + currentBackupAccountSession: () => 0, + captureBackupWriteIntent: () => undefined, + 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'); +}); + +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); +}); + +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); +}); diff --git a/tests/recovery/sharedVaultRepair.test.cjs b/tests/recovery/sharedVaultRepair.test.cjs new file mode 100644 index 0000000000..0b00cb638b --- /dev/null +++ b/tests/recovery/sharedVaultRepair.test.cjs @@ -0,0 +1,301 @@ +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, + ackRevision = 'valid', +} = {}) { + 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, '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); +}); +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); +}); +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('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 = [ + { + vaultId: 'shared', + vault: encryption.encrypt(key, JSON.stringify(f.local.Vault[0])), + isArchived: false, + }, + ]; + 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), '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) { + 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); + 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 }; +} + +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(); + 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/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/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/transport.test.cjs b/tests/recovery/transport.test.cjs new file mode 100644 index 0000000000..6ec6543149 --- /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','revisionedBackupMutation','getBackupSnapshot']); +}); diff --git a/tests/recovery/upgradeAccountSession.test.cjs b/tests/recovery/upgradeAccountSession.test.cjs new file mode 100644 index 0000000000..05a6f0915e --- /dev/null +++ b/tests/recovery/upgradeAccountSession.test.cjs @@ -0,0 +1,238 @@ +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 { 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)); + }, + }; + const dbManager = { + getObjectByIndex: () => state.records.KeeperApp[0], + getCollection: (schema) => plain(state.records[schema]), + }; + const scope = { + 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, + 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: (_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' }, ...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(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 }; +} + +function deferred() { + let resolve; + const promise = new Promise((done) => { resolve = done; }); + return { promise, resolve }; +} + +const versionAction = { originAppId: 'A', originBackupAccountSession: 7, originBackupWriteIntent: writeIntent, + payload: { previousVersion: '2.5.16', newVersion: '2.5.17', isRecovery: true } }; + +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('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); + }; + 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(getCollection('UTXOInfo')); + assert.equal(await pending, false); + assert.equal(f.state.commits, 0); + assert.deepEqual(f.state.queued, []); + assert.deepEqual(f.state.actions, []); +}); + +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 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, []); +}); + +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('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']); +}); + +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; + f.dbManager.getCollection = (schema) => { + if (schema === 'UTXOInfo') { entered.resolve(); return resume.promise; } + return getCollection(schema); + }; + 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')); + assert.equal(await pending, false); + assert.equal(f.state.commits, 0); + assert.deepEqual(f.state.records.Tags, []); + assert.deepEqual(f.state.queued, []); +}); + +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); +}); + +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, []); +}); diff --git a/tests/recovery/usdtAccountDelete.test.cjs b/tests/recovery/usdtAccountDelete.test.cjs new file mode 100644 index 0000000000..50fc26d710 --- /dev/null +++ b/tests/recovery/usdtAccountDelete.test.cjs @@ -0,0 +1,320 @@ +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', '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', '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'), { + compilerOptions: { target: ts.ScriptTarget.ES2020 }, +}).outputText; + +function fixture({ consent = true, creationPaused = true, switchDuringRemote = false, + switchDuringRemoteRoundTrip = false, + switchDuringGeneration = false, switchDuringGenerationRoundTrip = false, + switchDuringUpdate = false, + switchDuringUpdateRoundTrip = false, switchDuringAccountSyncRoundTrip = false, + pretendLocalDelete = 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: { + getObjectByIndex: (schema) => schema === 'KeeperApp' ? { id: state.active } : [], + 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; + }, + 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) => { + 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], + USDT_WALLET_CREATION_PAUSED: creationPaused, + USDTWalletType: { DEFAULT: 'default' }, + 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, + captureBackupWriteIntent: (appId) => ({ + appId, accountSession: state.session, baseRevision: 'a'.repeat(64), baselineEpoch: 0, + }), + dispatch: (action) => state.dispatched.push(action), + Relay: { deleteAppImageEntity: async (body, intent) => { + state.remote.push({ ...body, accountSession: intent?.accountSession, intent }); + 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: () => {}, + }; + // 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 }; +} + +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('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, 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('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'), 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.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('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'), 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.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('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('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: '' }); + assert.equal(result.error, 'USDT wallet setup is paused'); + assert.equal(f.state.active, 'A', 'wallet generation must not run during the pause'); + assert.deepEqual(f.state.local, []); + assert.deepEqual(f.state.dispatched, []); +}); + +test('when setup resumes, wallet generation after account switch cannot persist to B', async () => { + const f = fixture({ creationPaused: false, 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('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: '' }); + assert.equal(result.newWallet.id, 'wallet'); + assert.equal(f.state.local[0].appId, '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 () => { + 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, []); +}); + +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 new file mode 100644 index 0000000000..afa028531a --- /dev/null +++ b/tests/recovery/usdtBackup.test.cjs @@ -0,0 +1,209 @@ +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); + // 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); + 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 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); + 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 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, { + dbManager, + RealmSchema: { USDTWallet: 'USDTWallet' }, + canonical: image.canonical, + recoveryContent: image.recoveryContent, + appId: f.app.id, + 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 }), + setPendingAllBackup: (payload) => ({ type: 'setPendingAllBackup', payload }), + updateAppImage, + 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)); + } else { + f.actions.push(action); + 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), + 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: { + address: 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t', + gasFreeAddress: 'TXYZopYRdj2D9XRtbG411XZZ3kM5VkAeBf', + isActive: true, + frozen: 0, + canTransfer: true, + nextNonce: 0, + fees: { transferFee: 1, activateFee: 2 }, + }, + 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)); + 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: { ...original.accountStatus, 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/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/recovery/vaultRegistration.test.cjs b/tests/recovery/vaultRegistration.test.cjs new file mode 100644 index 0000000000..fbbaac759e --- /dev/null +++ b/tests/recovery/vaultRegistration.test.cjs @@ -0,0 +1,139 @@ +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, afterSnapshot, afterUpload } = {}) { + const state = { + appImage: { + appId: 'disposable', wallets: {}, signers: {}, nodes: [], + vaults: ['first', 'second'], labels: [], + }, + allVaultImages: [], labels: [], revision: 'a'.repeat(64), revisionedMutations: true, + }; + let uploads = 0; + let snapshotReads = 0; + const f = harness({ adapter: async (endpoint, payload) => { + if (endpoint === 'getBackupSnapshot') { + snapshotReads++; + if (afterSnapshot) await afterSnapshot(snapshotReads); + 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((label) => label.id), + }); + state.allVaultImages = Object.values(payload.vaultObject); + if (incompleteReadback) state.allVaultImages.pop(); + state.labels = payload.labels; + state.revision = 'b'.repeat(64); + if (afterUpload) await afterUpload(); + return { data: { updated: true, preconditionApplied: 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 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), '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, key.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('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), 'conflict'); + assert.equal(uploads(), 0); + assert.deepEqual(f.calls, ['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('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), 'conflict'); + assert.equal(fixture.uploads(), 0); + assert.deepEqual(fixture.f.calls, ['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'); + 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'); + } +}); 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/recovery/walletRefreshIsolation.test.cjs b/tests/recovery/walletRefreshIsolation.test.cjs new file mode 100644 index 0000000000..2c63ccaa5e --- /dev/null +++ b/tests/recovery/walletRefreshIsolation.test.cjs @@ -0,0 +1,281 @@ +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 { 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' }, + '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, []); + }); +} + +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 = []; + // 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, + './protected': unitProtectedFacade(transport, f.app), + }); + transport.recordVerifiedRemoteRevision( + originAppId, + 'a'.repeat(64), + transport.currentBackupAccountSession(), + { namespaceId: `unit-protected-${originAppId}`, revision: 'a'.repeat(64), generation: 0, authEpoch: 0 } + ); + 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) => { + 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.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); +}); + +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); +}); + +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); +}); 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/screens/DeleteKeys.test.tsx b/tests/screens/DeleteKeys.test.tsx new file mode 100644 index 0000000000..c925976cf5 --- /dev/null +++ b/tests/screens/DeleteKeys.test.tsx @@ -0,0 +1,791 @@ +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 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, + 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: {}, 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 })); +jest.mock('src/hooks/useSigners', () => ({ + __esModule: true, + default: () => ({ signers: mockSigners }), +})); +jest.mock('src/store/hooks', () => ({ useAppSelector: (select) => select(mockState) })); +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', () => ({ + __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/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() }), + useFocusEffect: (effect) => { + require('react').useEffect(() => { + mockFocus.effect = effect; + mockFocus.cleanup = effect(); + return () => mockFocus.cleanup?.(); + }, [effect]); + }, + CommonActions: {}, +})); +jest.mock('src/hardware', () => ({ + getSignerDescription: () => '', + getSignerNameFromType: () => 'Key', +})); +jest.mock('src/screens/Vault/SigningDeviceIcons', () => ({ SDIcons: () => ({ Icon: null }) })); +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(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 { 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') + .find((m) => m.props.visible && m.props.title.startsWith('delete')); + const Content = warning.props.Content; + let content; + act(() => { + content = renderer.create(); + }); + const output = content.toJSON(); + act(() => content.unmount()); + 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]; + 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(expectedAction('DELETE_SIGINING_DEVICE', mockSigner)); +}); + +test.each(['normal', 'hidden', 'canary', 'other-network'])( + 'blocks key referenced by %s wallet', + (kind) => { + mockVaults = [{ id: 'reference', 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 = [{ id: 'new-active-reference', 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 = [{ id: 'archived-reference', signers: [mockSigner], archived: true }]; + mount(); + chooseDelete(); + verifyPasscode(); + expect(mockDispatch).toHaveBeenCalledWith(expectedAction('ARCHIVE_SIGINING_DEVICE', 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('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(expectedAction('DELETE_SIGINING_DEVICE', mockSigner)); +}); + +test('repeated confirmation does not dispatch a second deletion', () => { + mount(); + chooseDelete(); + verifyPasscode(); + 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(expectedAction('DELETE_SIGINING_DEVICE', 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(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(); + } +); + +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(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(); + } +); + +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(); +}); + +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(); + 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(); +}); diff --git a/tests/screens/HomeWalletCollaborative.test.tsx b/tests/screens/HomeWalletCollaborative.test.tsx new file mode 100644 index 0000000000..0bd49a9a3f --- /dev/null +++ b/tests/screens/HomeWalletCollaborative.test.tsx @@ -0,0 +1,272 @@ +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 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 unknown 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); + }); +}); diff --git a/tests/screens/NewKeeperApp.test.tsx b/tests/screens/NewKeeperApp.test.tsx new file mode 100644 index 0000000000..2ea5556bb7 --- /dev/null +++ b/tests/screens/NewKeeperApp.test.tsx @@ -0,0 +1,381 @@ +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 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', + 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: (...args) => mockGetToken(...args), +})); +jest.mock('src/hooks/useToastMessage', () => ({ + __esModule: true, + default: () => ({ showToast: jest.fn() }), +})); +jest.mock('src/store/sagaActions/notifications', () => ({ + updateFCMTokens: (tokens) => ({ type: 'UPDATE_FCM', tokens }), +})); +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; + +let screen; +let store; +let actions; +let view; +let navigation; + +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, recreated = 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, 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); + const result = next(action); + observeBackupAccount(store.getState().storage.appId); + return result; + }), + }); + navigation = { replace: jest.fn(), navigate: jest.fn() }; + view = () => ( + + + + ); + await act(async () => { + screen = renderer.create(view()); + }); +} + +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 () => { + screen.root.findByProps({ testID: 'view_startNewTile' }).props.onPress(); + }); +} + +beforeEach(() => { + mockGetToken.mockReset().mockResolvedValue('test-token'); + observeBackupAccount(undefined); + observeBackupAccount('disposable-account-A'); +}); + +afterEach(() => { + if (screen) { + act(() => screen.unmount()); + screen = null; + } + jest.useRealTimers(); +}); + +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' } }, + ]); +}); + +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); +}); + +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(); +}); + +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(); + } +); diff --git a/tests/screens/PinEntry.test.tsx b/tests/screens/PinEntry.test.tsx new file mode 100644 index 0000000000..685f461659 --- /dev/null +++ b/tests/screens/PinEntry.test.tsx @@ -0,0 +1,266 @@ +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 mockCreateSignature = jest.fn(); +const mockState = { + login: { hasCreds: false, authenticationFailed: false, isAuthenticated: false }, + settings: { loginMethod: 'PIN' }, + account: { allAccounts: [], biometricEnabledAppId: null as string | null }, + 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', () => ({ + __esModule: true, + default: { getAccountManagerDetails: jest.fn(async () => null) }, +})); +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 }, + 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(); + 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() }; + 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'); + 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/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 }, + ]); +}); 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/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); + }); +}); diff --git a/tests/services/client.test.ts b/tests/services/client.test.ts index 7b1381530b..a04a186206 100644 --- a/tests/services/client.test.ts +++ b/tests/services/client.test.ts @@ -3,24 +3,85 @@ 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[1], + isConnected: true, +}; +const offlineNode = { + ...predefinedTestnetNodes[0], + isConnected: true, +}; +const addresses = [ + 'tb1qd2u9tvuqzadgeh02vppd33e7u2fatuwrw7h4q5', + 'tb1qwdm8hdyvv5jn05qq858lgnk50heucvxvqtl4sx', +]; + +beforeEach(async () => { + mockClients.length = 0; + ElectrumClient.setActivePeer([onlineNode], onlineNode); + const connection = await ElectrumClient.connect(); + assert.ok(connection.connected); }); afterAll(() => { @@ -30,23 +91,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 +127,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/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); + } + } + }); +}); diff --git a/tests/services/electrumConnectionGeneration.test.ts b/tests/services/electrumConnectionGeneration.test.ts new file mode 100644 index 0000000000..ba7ce5c351 --- /dev/null +++ b/tests/services/electrumConnectionGeneration.test.ts @@ -0,0 +1,219 @@ +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> = []; +let mockRealmAppId = 'account-a'; +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: { + getObjectByIndex: jest.fn(() => ({ id: mockRealmAppId })), + 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(() => { + mockRealmAppId = 'account-a'; + 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'; + mockRealmAppId = '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'; + mockRealmAppId = '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 }, + }) + ); + }); + + 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/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/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); }); }); 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..9b2c4cfc47 --- /dev/null +++ b/tests/services/faucetVisibility.test.ts @@ -0,0 +1,34 @@ +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); + }); + + 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); + }); +}); 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/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/manualNodeFailover.test.ts b/tests/services/manualNodeFailover.test.ts new file mode 100644 index 0000000000..99811c99f6 --- /dev/null +++ b/tests/services/manualNodeFailover.test.ts @@ -0,0 +1,105 @@ +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, +} 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: { getObjectByIndex: jest.fn() }, +})); +jest.mock('src/store/store', () => ({ store: { getState: jest.fn() } })); + +describe('manual Electrum selection', () => { + let activeAppId: string; + + beforeEach(() => { + activeAppId = 'A'; + observeBackupAccount(activeAppId); + (store.getState as jest.Mock).mockImplementation(() => ({ storage: { appId: activeAppId } })); + (dbManager.getObjectByIndex as jest.Mock).mockImplementation(() => ({ id: activeAppId })); + }); + + 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, + generation: 2, + }); + + await Node.connectToSelectedNode(selected); + + expect(ElectrumClient.setActivePeer).toHaveBeenCalledWith([], selected); + expect(ElectrumClient.setFailoverPeers).toHaveBeenCalledWith(predefinedTestnetNodes, 2); + }); + + 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(); + }); + + it('does not read another account’s nodes or equip peers after an account switch', 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); + expect(ElectrumClient.setActivePeer).toHaveBeenCalledWith([], selected); + + activeAppId = 'B'; + 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(); + }); + + 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(); + }); +}); diff --git a/tests/services/networkConnectRace.test.ts b/tests/services/networkConnectRace.test.ts new file mode 100644 index 0000000000..5783028367 --- /dev/null +++ b/tests/services/networkConnectRace.test.ts @@ -0,0 +1,176 @@ +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; +let mockRealmAppId = 'account-a'; +const mockNodes = predefinedTestnetNodes; +const mockRealm = { create: jest.fn() }; +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) } })); +// 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), + updateObjectById: 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; + mockRealmAppId = 'account-a'; + 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'; + mockRealmAppId = '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(mockRealm.create).toHaveBeenCalledWith(RealmSchema.NodeConnect, mockNodes[1]); + expect(actions).toContainEqual( + expect.objectContaining({ + type: 'network/setTestnetFallbackNodeAdded', + payload: 'account-b', + }) + ); + }); + + 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([]); + }); +}); 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/passphraseDecayPolicy.test.ts b/tests/services/passphraseDecayPolicy.test.ts new file mode 100644 index 0000000000..bd6f1a2a7e --- /dev/null +++ b/tests/services/passphraseDecayPolicy.test.ts @@ -0,0 +1,140 @@ +import * as bip39 from 'bip39'; +import { + generateSeedWordsKey, + generateMiniscriptScheme, + generateVault, + getAvailableMiniscriptPhase, +} from 'src/services/wallets/factories/VaultFactory'; +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' } }) }, +})); +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); + }); + + 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; + 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); + + 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); + }); +}); 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/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' }), + }) + ); + }); +}); 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' }], + ]); +}); 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, + }); + }); +}); diff --git a/tests/services/tronConfirmedHistory.test.cjs b/tests/services/tronConfirmedHistory.test.cjs new file mode 100644 index 0000000000..ffcf754af0 --- /dev/null +++ b/tests/services/tronConfirmedHistory.test.cjs @@ -0,0 +1,127 @@ +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: { + CHAIN_CONFIRMED: 'CONFIRMED', + 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, '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'); + 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 new file mode 100644 index 0000000000..ff9f2c12a1 --- /dev/null +++ b/tests/services/usdtPause.test.cjs @@ -0,0 +1,264 @@ +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: 'TStoredGasFreeAddress', + to: 'TRecipient', + formattedValue: 2, + confirmed: true, + blockNumber: 1, + blockTimestamp: 100, + }, + { + transactionId: 'chain-no-time', + from: 'TStoredGasFreeAddress', + to: 'TRecipient', + formattedValue: 1, + confirmed: true, + blockNumber: 0, + blockTimestamp: 0, + }, + ], + meta: { fingerprint: '', hasMore: false }, + }; + }, +}; +const usdtModule = loadTypeScript('src/services/wallets/operations/dollars/USDT.ts', { + './GasFree': historical, + 'src/services/wallets/enums': { NetworkType: network }, + './Tron': tron, +}); +const service = usdtModule.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 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); + assert.equal(factory.getAvailableBalanceUSDTWallet(wallet), 10); + + const transactions = await factory.syncUSDTWalletTransactions(wallet); + 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.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); + 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); + 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', + status: historical.GasFreeTransferStatus.SUCCEED, + }), + true, + 'a stored provider status is not independent proof of an on-chain transfer' + ); +}); + +test('refresh upgrades a cached pending entry when its confirmed chain record appears', 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.CONFIRMING, + timestamp: 90, + blockNumber: 0, + isGasFree: true, + }, + ], + }, + }; + const transactions = await factory.syncUSDTWalletTransactions(cached); + const confirmed = transactions.find((tx) => tx.txId === 'chain-1'); + assert.equal(confirmed.status, historical.GasFreeTransferStatus.CHAIN_CONFIRMED); + 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 + ); +}); + +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/); + await assert.rejects(factory.syncUSDTWalletTransactions(missing), /Stored USDT address/); +}); 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/tests/store/versionHistory.test.ts b/tests/store/versionHistory.test.ts new file mode 100644 index 0000000000..71c3cc7798 --- /dev/null +++ b/tests/store/versionHistory.test.ts @@ -0,0 +1,485 @@ +import fs from 'fs'; +import path from 'path'; +import ts from 'typescript'; +import vm from 'vm'; +import semver from 'semver'; +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 * 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, 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, + './taproot-utils/noble_ecc': ecc, +}).default; +const bip32 = BIP32Factory(ecc); +const previousVersion = '2.5.15'; +const newVersion = '2.5.16'; +const writeIntent = Object.freeze({ + appId: 'test-app', + accountSession: 1, + baseRevision: 'a'.repeat(64), + baselineEpoch: 1, +}); +const clone = (value) => JSON.parse(JSON.stringify(value)); + +function sourceDeclarations(file: string, names?: string[]) { + const source = ts.createSourceFile( + file, + fs.readFileSync(path.join(process.cwd(), file), 'utf8'), + ts.ScriptTarget.Latest, + true + ); + 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[schema === RealmSchema.VersionHistory ? 'version' : 'id'] === id + ), + objects: (schema) => records[schema], + create, + }; + const backupState = { automaticCloudBackupByAppId: { [appId]: false } }; + const consentEpochs = new Map(); + const recoveryDisables = new WeakMap(); + const scope: any = { + ...effects, + ...encryption, + ...enums, + RealmSchema, + semver, + Date, + Buffer, + Promise, + console: { log() {}, error() {} }, + Realm: { UpdateMode: { Modified: 'modified' } }, + 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, + 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; + }, + }, + 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) => { + 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); + }; + scope.store.dispatch = dispatch; + for (const name of [ + 'setAppVersion', + 'setAppImageError', + 'setRecoveryDiscovery', + 'setProtectedBackupState', + 'setAutomaticCloudBackup', + 'setSeedConfirmed', + 'setBackupType', + 'uaiActioned', + 'setAppId', + 'setAppCreated', + 'addAccount', + 'autoSyncWallets', + 'saveDefaultWalletState', + 'uaiChecks', + 'loadConciergeUser', + 'loadConciergeTickets', + 'setRecoveryKeyStatus', + 'appImagerecoveryRetry', + ]) + scope[name] = (payload) => ({ type: name, payload }); + vm.runInNewContext( + ts.transpileModule(sourceDeclarations('src/store/sagas/upgrade.ts'), { + compilerOptions: { target: ts.ScriptTarget.ES2020 }, + }).outputText, + scope + ); + 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 }); + } + ); + + 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('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; + 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, + 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) => + 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', + 'latestRecoveryReadRequest', + 'recoverApp', + 'writeRecoveredObject', + ]), + { + compilerOptions: { target: ts.ScriptTarget.ES2020 }, + } + ).outputText, + f.scope + ); + await f.run('getAppImageWorker', { payload: { primaryMnemonic: mnemonic, + selection: { source: 'legacy-older', olderCopyConfirmed: true } } }); + return { writes: f.records.VersionHistory, requestedUpgrades, errors: f.state.errors }; +} + +test.each(['2.5.15', '2.5.16'])( + '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(requestedUpgrades).toEqual( + backupVersion === '2.5.15' + ? [ + { + previousVersion: backupVersion, + newVersion, + isRecovery: true, + originBackupAccountSession: 1, + }, + ] + : [] + ); + } +); 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); +}); 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"] } diff --git a/yarn.lock b/yarn.lock index d629fa670f..563255662a 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== @@ -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" @@ -4411,11 +4449,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 +4488,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 +4502,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 +4522,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 +4535,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 +4546,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 +4566,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 +4576,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 +4963,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 +6542,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 +6839,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 +7057,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 +7073,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 +7111,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 +7132,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 +7361,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 +7861,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 +9420,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 +9985,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 +10174,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 +11847,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 +12290,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 +12677,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"